2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * ip/ip6_forward.c: IP v6 forwarding
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
43 #include <vnet/srp/srp.h> /* for srp_hw_interface_class */
44 #include <vppinfra/cache.h>
45 #include <vnet/fib/fib_urpf_list.h> /* for FIB uRPF check */
46 #include <vnet/fib/ip6_fib.h>
47 #include <vnet/mfib/ip6_mfib.h>
48 #include <vnet/dpo/load_balance_map.h>
49 #include <vnet/dpo/classify_dpo.h>
51 #include <vppinfra/bihash_template.c>
53 /* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
54 #define OI_DECAP 0x80000000
58 * @brief IPv6 Forwarding.
60 * This file contains the source code for IPv6 forwarding.
64 ip6_forward_next_trace (vlib_main_t * vm,
65 vlib_node_runtime_t * node,
67 vlib_rx_or_tx_t which_adj_index);
70 ip6_lookup_inline (vlib_main_t * vm,
71 vlib_node_runtime_t * node, vlib_frame_t * frame)
73 ip6_main_t *im = &ip6_main;
74 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_to_counters;
75 u32 n_left_from, n_left_to_next, *from, *to_next;
76 ip_lookup_next_t next;
77 u32 thread_index = vlib_get_thread_index ();
79 from = vlib_frame_vector_args (frame);
80 n_left_from = frame->n_vectors;
81 next = node->cached_next_index;
83 while (n_left_from > 0)
85 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
87 while (n_left_from >= 4 && n_left_to_next >= 2)
89 vlib_buffer_t *p0, *p1;
90 u32 pi0, pi1, lbi0, lbi1, wrong_next;
91 ip_lookup_next_t next0, next1;
92 ip6_header_t *ip0, *ip1;
93 ip6_address_t *dst_addr0, *dst_addr1;
94 u32 fib_index0, fib_index1;
95 u32 flow_hash_config0, flow_hash_config1;
96 const dpo_id_t *dpo0, *dpo1;
97 const load_balance_t *lb0, *lb1;
99 /* Prefetch next iteration. */
101 vlib_buffer_t *p2, *p3;
103 p2 = vlib_get_buffer (vm, from[2]);
104 p3 = vlib_get_buffer (vm, from[3]);
106 vlib_prefetch_buffer_header (p2, LOAD);
107 vlib_prefetch_buffer_header (p3, LOAD);
108 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
109 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
112 pi0 = to_next[0] = from[0];
113 pi1 = to_next[1] = from[1];
115 p0 = vlib_get_buffer (vm, pi0);
116 p1 = vlib_get_buffer (vm, pi1);
118 ip0 = vlib_buffer_get_current (p0);
119 ip1 = vlib_buffer_get_current (p1);
121 dst_addr0 = &ip0->dst_address;
122 dst_addr1 = &ip1->dst_address;
125 vec_elt (im->fib_index_by_sw_if_index,
126 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
128 vec_elt (im->fib_index_by_sw_if_index,
129 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
131 fib_index0 = (vnet_buffer (p0)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
132 fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
133 fib_index1 = (vnet_buffer (p1)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
134 fib_index1 : vnet_buffer (p1)->sw_if_index[VLIB_TX];
136 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
137 lbi1 = ip6_fib_table_fwding_lookup (im, fib_index1, dst_addr1);
139 lb0 = load_balance_get (lbi0);
140 lb1 = load_balance_get (lbi1);
141 ASSERT (lb0->lb_n_buckets > 0);
142 ASSERT (lb1->lb_n_buckets > 0);
143 ASSERT (is_pow2 (lb0->lb_n_buckets));
144 ASSERT (is_pow2 (lb1->lb_n_buckets));
146 vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
148 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
150 flow_hash_config0 = lb0->lb_hash_config;
151 vnet_buffer (p0)->ip.flow_hash =
152 ip6_compute_flow_hash (ip0, flow_hash_config0);
154 load_balance_get_fwd_bucket (lb0,
155 (vnet_buffer (p0)->ip.flow_hash &
156 (lb0->lb_n_buckets_minus_1)));
160 dpo0 = load_balance_get_bucket_i (lb0, 0);
162 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
164 flow_hash_config1 = lb1->lb_hash_config;
165 vnet_buffer (p1)->ip.flow_hash =
166 ip6_compute_flow_hash (ip1, flow_hash_config1);
168 load_balance_get_fwd_bucket (lb1,
169 (vnet_buffer (p1)->ip.flow_hash &
170 (lb1->lb_n_buckets_minus_1)));
174 dpo1 = load_balance_get_bucket_i (lb1, 0);
176 next0 = dpo0->dpoi_next_node;
177 next1 = dpo1->dpoi_next_node;
179 /* Only process the HBH Option Header if explicitly configured to do so */
181 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
183 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
184 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
187 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
189 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
190 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
192 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
193 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
195 vlib_increment_combined_counter
196 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
197 vlib_increment_combined_counter
198 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
205 wrong_next = (next0 != next) + 2 * (next1 != next);
206 if (PREDICT_FALSE (wrong_next != 0))
215 vlib_set_next_frame_buffer (vm, node, next0, pi0);
222 vlib_set_next_frame_buffer (vm, node, next1, pi1);
229 vlib_set_next_frame_buffer (vm, node, next0, pi0);
230 vlib_set_next_frame_buffer (vm, node, next1, pi1);
234 vlib_put_next_frame (vm, node, next, n_left_to_next);
236 vlib_get_next_frame (vm, node, next, to_next,
243 while (n_left_from > 0 && n_left_to_next > 0)
248 ip_lookup_next_t next0;
250 ip6_address_t *dst_addr0;
251 u32 fib_index0, flow_hash_config0;
252 const dpo_id_t *dpo0;
257 p0 = vlib_get_buffer (vm, pi0);
259 ip0 = vlib_buffer_get_current (p0);
261 dst_addr0 = &ip0->dst_address;
264 vec_elt (im->fib_index_by_sw_if_index,
265 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
267 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
268 (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
270 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
272 lb0 = load_balance_get (lbi0);
273 flow_hash_config0 = lb0->lb_hash_config;
275 vnet_buffer (p0)->ip.flow_hash = 0;
276 ASSERT (lb0->lb_n_buckets > 0);
277 ASSERT (is_pow2 (lb0->lb_n_buckets));
279 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
281 flow_hash_config0 = lb0->lb_hash_config;
282 vnet_buffer (p0)->ip.flow_hash =
283 ip6_compute_flow_hash (ip0, flow_hash_config0);
285 load_balance_get_fwd_bucket (lb0,
286 (vnet_buffer (p0)->ip.flow_hash &
287 (lb0->lb_n_buckets_minus_1)));
291 dpo0 = load_balance_get_bucket_i (lb0, 0);
294 dpo0 = load_balance_get_bucket_i (lb0,
295 (vnet_buffer (p0)->ip.flow_hash &
296 lb0->lb_n_buckets_minus_1));
297 next0 = dpo0->dpoi_next_node;
299 /* Only process the HBH Option Header if explicitly configured to do so */
301 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
303 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
304 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
306 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
308 vlib_increment_combined_counter
309 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
316 if (PREDICT_FALSE (next0 != next))
319 vlib_put_next_frame (vm, node, next, n_left_to_next);
321 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
328 vlib_put_next_frame (vm, node, next, n_left_to_next);
331 if (node->flags & VLIB_NODE_FLAG_TRACE)
332 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
334 return frame->n_vectors;
338 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
339 ip6_main_t * im, u32 fib_index,
340 ip_interface_address_t * a)
342 ip_lookup_main_t *lm = &im->lookup_main;
343 ip6_address_t *address = ip_interface_address_get_address (lm, a);
345 .fp_len = a->address_length,
346 .fp_proto = FIB_PROTOCOL_IP6,
347 .fp_addr.ip6 = *address,
350 a->neighbor_probe_adj_index = ~0;
351 if (a->address_length < 128)
353 fib_node_index_t fei;
355 fei = fib_table_entry_update_one_path (fib_index,
357 FIB_SOURCE_INTERFACE,
358 (FIB_ENTRY_FLAG_CONNECTED |
359 FIB_ENTRY_FLAG_ATTACHED),
361 /* No next-hop address */
363 /* invalid FIB index */
366 NULL, FIB_ROUTE_PATH_FLAG_NONE);
367 a->neighbor_probe_adj_index = fib_entry_get_adj (fei);
371 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
373 u32 classify_table_index =
374 lm->classify_table_index_by_sw_if_index[sw_if_index];
375 if (classify_table_index != (u32) ~ 0)
377 dpo_id_t dpo = DPO_INVALID;
382 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
384 fib_table_entry_special_dpo_add (fib_index,
387 FIB_ENTRY_FLAG_NONE, &dpo);
392 fib_table_entry_update_one_path (fib_index, &pfx,
393 FIB_SOURCE_INTERFACE,
394 (FIB_ENTRY_FLAG_CONNECTED |
395 FIB_ENTRY_FLAG_LOCAL),
399 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
403 ip6_del_interface_routes (ip6_main_t * im,
405 ip6_address_t * address, u32 address_length)
408 .fp_len = address_length,
409 .fp_proto = FIB_PROTOCOL_IP6,
410 .fp_addr.ip6 = *address,
413 if (pfx.fp_len < 128)
415 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
420 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
424 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
426 ip6_main_t *im = &ip6_main;
428 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
431 * enable/disable only on the 1<->0 transition
435 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
440 /* The ref count is 0 when an address is removed from an interface that has
441 * no address - this is not a ciritical error */
442 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
443 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
447 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
450 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
454 /* get first interface address */
456 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
458 ip_lookup_main_t *lm = &im->lookup_main;
459 ip_interface_address_t *ia = 0;
460 ip6_address_t *result = 0;
463 foreach_ip_interface_address (lm, ia, sw_if_index,
464 1 /* honor unnumbered */,
466 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
475 ip6_add_del_interface_address (vlib_main_t * vm,
477 ip6_address_t * address,
478 u32 address_length, u32 is_del)
480 vnet_main_t *vnm = vnet_get_main ();
481 ip6_main_t *im = &ip6_main;
482 ip_lookup_main_t *lm = &im->lookup_main;
484 u32 if_address_index;
485 ip6_address_fib_t ip6_af, *addr_fib = 0;
487 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
488 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
490 ip6_addr_fib_init (&ip6_af, address,
491 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
492 vec_add1 (addr_fib, ip6_af);
495 uword elts_before = pool_elts (lm->if_address_pool);
497 error = ip_interface_address_add_del
498 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
502 /* Pool did not grow: add duplicate address. */
503 if (elts_before == pool_elts (lm->if_address_pool))
507 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
510 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
512 ip6_add_interface_routes (vnm, sw_if_index,
513 im, ip6_af.fib_index,
514 pool_elt_at_index (lm->if_address_pool,
518 ip6_add_del_interface_address_callback_t *cb;
519 vec_foreach (cb, im->add_del_interface_address_callbacks)
520 cb->function (im, cb->function_opaque, sw_if_index,
521 address, address_length, if_address_index, is_del);
530 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
532 ip6_main_t *im = &ip6_main;
533 ip_interface_address_t *ia;
535 u32 is_admin_up, fib_index;
537 /* Fill in lookup tables with default table (0). */
538 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
540 vec_validate_init_empty (im->
541 lookup_main.if_address_pool_index_by_sw_if_index,
544 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
546 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
549 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
550 0 /* honor unnumbered */,
552 a = ip_interface_address_get_address (&im->lookup_main, ia);
554 ip6_add_interface_routes (vnm, sw_if_index,
558 ip6_del_interface_routes (im, fib_index,
559 a, ia->address_length);
566 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
568 /* Built-in ip6 unicast rx feature path definition */
570 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
572 .arc_name = "ip6-unicast",
573 .start_nodes = VNET_FEATURES ("ip6-input"),
574 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
577 VNET_FEATURE_INIT (ip6_flow_classify, static) =
579 .arc_name = "ip6-unicast",
580 .node_name = "ip6-flow-classify",
581 .runs_before = VNET_FEATURES ("ip6-inacl"),
584 VNET_FEATURE_INIT (ip6_inacl, static) =
586 .arc_name = "ip6-unicast",
587 .node_name = "ip6-inacl",
588 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
591 VNET_FEATURE_INIT (ip6_policer_classify, static) =
593 .arc_name = "ip6-unicast",
594 .node_name = "ip6-policer-classify",
595 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
598 VNET_FEATURE_INIT (ip6_ipsec, static) =
600 .arc_name = "ip6-unicast",
601 .node_name = "ipsec-input-ip6",
602 .runs_before = VNET_FEATURES ("l2tp-decap"),
605 VNET_FEATURE_INIT (ip6_l2tp, static) =
607 .arc_name = "ip6-unicast",
608 .node_name = "l2tp-decap",
609 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
612 VNET_FEATURE_INIT (ip6_vpath, static) =
614 .arc_name = "ip6-unicast",
615 .node_name = "vpath-input-ip6",
616 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
619 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
621 .arc_name = "ip6-unicast",
622 .node_name = "ip6-vxlan-bypass",
623 .runs_before = VNET_FEATURES ("ip6-lookup"),
626 VNET_FEATURE_INIT (ip6_drop, static) =
628 .arc_name = "ip6-unicast",
629 .node_name = "ip6-drop",
630 .runs_before = VNET_FEATURES ("ip6-lookup"),
633 VNET_FEATURE_INIT (ip6_lookup, static) =
635 .arc_name = "ip6-unicast",
636 .node_name = "ip6-lookup",
637 .runs_before = 0, /*last feature*/
640 /* Built-in ip6 multicast rx feature path definition (none now) */
641 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
643 .arc_name = "ip6-multicast",
644 .start_nodes = VNET_FEATURES ("ip6-input"),
645 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
648 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
649 .arc_name = "ip6-multicast",
650 .node_name = "vpath-input-ip6",
651 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
654 VNET_FEATURE_INIT (ip6_drop_mc, static) = {
655 .arc_name = "ip6-multicast",
656 .node_name = "ip6-drop",
657 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
660 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
661 .arc_name = "ip6-multicast",
662 .node_name = "ip6-mfib-forward-lookup",
663 .runs_before = 0, /* last feature */
666 /* Built-in ip4 tx feature path definition */
667 VNET_FEATURE_ARC_INIT (ip6_output, static) =
669 .arc_name = "ip6-output",
670 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain"),
671 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
674 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
675 .arc_name = "ip6-output",
676 .node_name = "ipsec-output-ip6",
677 .runs_before = VNET_FEATURES ("interface-output"),
680 VNET_FEATURE_INIT (ip6_interface_output, static) = {
681 .arc_name = "ip6-output",
682 .node_name = "interface-output",
683 .runs_before = 0, /* not before any other features */
688 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
690 ip6_main_t *im = &ip6_main;
692 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
693 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
695 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
698 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
701 return /* no error */ 0;
704 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
707 ip6_lookup (vlib_main_t * vm,
708 vlib_node_runtime_t * node, vlib_frame_t * frame)
710 return ip6_lookup_inline (vm, node, frame);
713 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
716 VLIB_REGISTER_NODE (ip6_lookup_node) =
718 .function = ip6_lookup,
719 .name = "ip6-lookup",
720 .vector_size = sizeof (u32),
721 .format_trace = format_ip6_lookup_trace,
722 .n_next_nodes = IP6_LOOKUP_N_NEXT,
723 .next_nodes = IP6_LOOKUP_NEXT_NODES,
727 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
730 ip6_load_balance (vlib_main_t * vm,
731 vlib_node_runtime_t * node, vlib_frame_t * frame)
733 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
734 u32 n_left_from, n_left_to_next, *from, *to_next;
735 ip_lookup_next_t next;
736 u32 thread_index = vlib_get_thread_index ();
737 ip6_main_t *im = &ip6_main;
739 from = vlib_frame_vector_args (frame);
740 n_left_from = frame->n_vectors;
741 next = node->cached_next_index;
743 if (node->flags & VLIB_NODE_FLAG_TRACE)
744 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
746 while (n_left_from > 0)
748 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
751 while (n_left_from >= 4 && n_left_to_next >= 2)
753 ip_lookup_next_t next0, next1;
754 const load_balance_t *lb0, *lb1;
755 vlib_buffer_t *p0, *p1;
756 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
757 const ip6_header_t *ip0, *ip1;
758 const dpo_id_t *dpo0, *dpo1;
760 /* Prefetch next iteration. */
762 vlib_buffer_t *p2, *p3;
764 p2 = vlib_get_buffer (vm, from[2]);
765 p3 = vlib_get_buffer (vm, from[3]);
767 vlib_prefetch_buffer_header (p2, STORE);
768 vlib_prefetch_buffer_header (p3, STORE);
770 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
771 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
774 pi0 = to_next[0] = from[0];
775 pi1 = to_next[1] = from[1];
782 p0 = vlib_get_buffer (vm, pi0);
783 p1 = vlib_get_buffer (vm, pi1);
785 ip0 = vlib_buffer_get_current (p0);
786 ip1 = vlib_buffer_get_current (p1);
787 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
788 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
790 lb0 = load_balance_get (lbi0);
791 lb1 = load_balance_get (lbi1);
794 * this node is for via FIBs we can re-use the hash value from the
795 * to node if present.
796 * We don't want to use the same hash value at each level in the recursion
797 * graph as that would lead to polarisation
801 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
803 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
805 hc0 = vnet_buffer (p0)->ip.flow_hash =
806 vnet_buffer (p0)->ip.flow_hash >> 1;
810 hc0 = vnet_buffer (p0)->ip.flow_hash =
811 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
814 load_balance_get_fwd_bucket (lb0,
816 lb0->lb_n_buckets_minus_1));
820 dpo0 = load_balance_get_bucket_i (lb0, 0);
822 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
824 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
826 hc1 = vnet_buffer (p1)->ip.flow_hash =
827 vnet_buffer (p1)->ip.flow_hash >> 1;
831 hc1 = vnet_buffer (p1)->ip.flow_hash =
832 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
835 load_balance_get_fwd_bucket (lb1,
837 lb1->lb_n_buckets_minus_1));
841 dpo1 = load_balance_get_bucket_i (lb1, 0);
844 next0 = dpo0->dpoi_next_node;
845 next1 = dpo1->dpoi_next_node;
847 /* Only process the HBH Option Header if explicitly configured to do so */
849 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
851 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
852 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
854 /* Only process the HBH Option Header if explicitly configured to do so */
856 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
858 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
859 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
862 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
863 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
865 vlib_increment_combined_counter
866 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
867 vlib_increment_combined_counter
868 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
870 vlib_validate_buffer_enqueue_x2 (vm, node, next,
871 to_next, n_left_to_next,
872 pi0, pi1, next0, next1);
875 while (n_left_from > 0 && n_left_to_next > 0)
877 ip_lookup_next_t next0;
878 const load_balance_t *lb0;
881 const ip6_header_t *ip0;
882 const dpo_id_t *dpo0;
891 p0 = vlib_get_buffer (vm, pi0);
893 ip0 = vlib_buffer_get_current (p0);
894 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
896 lb0 = load_balance_get (lbi0);
899 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
901 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
903 hc0 = vnet_buffer (p0)->ip.flow_hash =
904 vnet_buffer (p0)->ip.flow_hash >> 1;
908 hc0 = vnet_buffer (p0)->ip.flow_hash =
909 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
912 load_balance_get_fwd_bucket (lb0,
914 lb0->lb_n_buckets_minus_1));
918 dpo0 = load_balance_get_bucket_i (lb0, 0);
921 next0 = dpo0->dpoi_next_node;
922 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
924 /* Only process the HBH Option Header if explicitly configured to do so */
926 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
928 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
929 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
932 vlib_increment_combined_counter
933 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
935 vlib_validate_buffer_enqueue_x1 (vm, node, next,
936 to_next, n_left_to_next,
940 vlib_put_next_frame (vm, node, next, n_left_to_next);
943 return frame->n_vectors;
947 VLIB_REGISTER_NODE (ip6_load_balance_node) =
949 .function = ip6_load_balance,
950 .name = "ip6-load-balance",
951 .vector_size = sizeof (u32),
952 .sibling_of = "ip6-lookup",
953 .format_trace = format_ip6_lookup_trace,
957 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
961 /* Adjacency taken. */
966 /* Packet data, possibly *after* rewrite. */
967 u8 packet_data[128 - 1 * sizeof (u32)];
969 ip6_forward_next_trace_t;
972 format_ip6_forward_next_trace (u8 * s, va_list * args)
974 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
975 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
976 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
977 uword indent = format_get_indent (s);
979 s = format (s, "%U%U",
980 format_white_space, indent,
981 format_ip6_header, t->packet_data, sizeof (t->packet_data));
986 format_ip6_lookup_trace (u8 * s, va_list * args)
988 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
989 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
990 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
991 uword indent = format_get_indent (s);
993 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
994 t->fib_index, t->adj_index, t->flow_hash);
995 s = format (s, "\n%U%U",
996 format_white_space, indent,
997 format_ip6_header, t->packet_data, sizeof (t->packet_data));
1003 format_ip6_rewrite_trace (u8 * s, va_list * args)
1005 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1006 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1007 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
1008 uword indent = format_get_indent (s);
1010 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1011 t->fib_index, t->adj_index, format_ip_adjacency,
1012 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
1013 s = format (s, "\n%U%U",
1014 format_white_space, indent,
1015 format_ip_adjacency_packet_data,
1016 t->adj_index, t->packet_data, sizeof (t->packet_data));
1020 /* Common trace function for all ip6-forward next nodes. */
1022 ip6_forward_next_trace (vlib_main_t * vm,
1023 vlib_node_runtime_t * node,
1024 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
1027 ip6_main_t *im = &ip6_main;
1029 n_left = frame->n_vectors;
1030 from = vlib_frame_vector_args (frame);
1035 vlib_buffer_t *b0, *b1;
1036 ip6_forward_next_trace_t *t0, *t1;
1038 /* Prefetch next iteration. */
1039 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1040 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1045 b0 = vlib_get_buffer (vm, bi0);
1046 b1 = vlib_get_buffer (vm, bi1);
1048 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1050 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1051 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1052 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1054 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1055 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1056 vec_elt (im->fib_index_by_sw_if_index,
1057 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1059 clib_memcpy (t0->packet_data,
1060 vlib_buffer_get_current (b0),
1061 sizeof (t0->packet_data));
1063 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1065 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1066 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1067 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1069 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1070 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1071 vec_elt (im->fib_index_by_sw_if_index,
1072 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
1074 clib_memcpy (t1->packet_data,
1075 vlib_buffer_get_current (b1),
1076 sizeof (t1->packet_data));
1086 ip6_forward_next_trace_t *t0;
1090 b0 = vlib_get_buffer (vm, bi0);
1092 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1094 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1095 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1096 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1098 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1099 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1100 vec_elt (im->fib_index_by_sw_if_index,
1101 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1103 clib_memcpy (t0->packet_data,
1104 vlib_buffer_get_current (b0),
1105 sizeof (t0->packet_data));
1113 ip6_drop_or_punt (vlib_main_t * vm,
1114 vlib_node_runtime_t * node,
1115 vlib_frame_t * frame, ip6_error_t error_code)
1117 u32 *buffers = vlib_frame_vector_args (frame);
1118 uword n_packets = frame->n_vectors;
1120 vlib_error_drop_buffers (vm, node, buffers,
1124 ip6_input_node.index, error_code);
1126 if (node->flags & VLIB_NODE_FLAG_TRACE)
1127 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1133 ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1135 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP);
1139 ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1141 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT);
1145 VLIB_REGISTER_NODE (ip6_drop_node, static) =
1147 .function = ip6_drop,
1149 .vector_size = sizeof (u32),
1150 .format_trace = format_ip6_forward_next_trace,
1154 [0] = "error-drop",},
1158 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
1161 VLIB_REGISTER_NODE (ip6_punt_node, static) =
1163 .function = ip6_punt,
1165 .vector_size = sizeof (u32),
1166 .format_trace = format_ip6_forward_next_trace,
1170 [0] = "error-punt",},
1174 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
1176 /* Compute TCP/UDP/ICMP6 checksum in software. */
1178 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1179 ip6_header_t * ip0, int *bogus_lengthp)
1182 u16 sum16, payload_length_host_byte_order;
1183 u32 i, n_this_buffer, n_bytes_left;
1184 u32 headers_size = sizeof (ip0[0]);
1185 void *data_this_buffer;
1187 ASSERT (bogus_lengthp);
1190 /* Initialize checksum with ip header. */
1191 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1192 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1193 data_this_buffer = (void *) (ip0 + 1);
1195 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1197 sum0 = ip_csum_with_carry (sum0,
1198 clib_mem_unaligned (&ip0->
1199 src_address.as_uword[i],
1202 ip_csum_with_carry (sum0,
1203 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1207 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1208 * or UDP-Ping packets */
1209 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1212 ip6_hop_by_hop_ext_t *ext_hdr =
1213 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1215 /* validate really icmp6 next */
1216 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1217 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
1219 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1220 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1222 payload_length_host_byte_order -= skip_bytes;
1223 headers_size += skip_bytes;
1226 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1227 if (p0 && n_this_buffer + headers_size > p0->current_length)
1229 p0->current_length >
1230 headers_size ? p0->current_length - headers_size : 0;
1233 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1234 n_bytes_left -= n_this_buffer;
1235 if (n_bytes_left == 0)
1238 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1243 p0 = vlib_get_buffer (vm, p0->next_buffer);
1244 data_this_buffer = vlib_buffer_get_current (p0);
1245 n_this_buffer = p0->current_length;
1248 sum16 = ~ip_csum_fold (sum0);
1254 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1256 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1261 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1262 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1263 || ip0->protocol == IP_PROTOCOL_ICMP6
1264 || ip0->protocol == IP_PROTOCOL_UDP
1265 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1267 udp0 = (void *) (ip0 + 1);
1268 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1270 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1271 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1275 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1277 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1278 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1284 * @brief returns number of links on which src is reachable.
1287 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1289 const load_balance_t *lb0;
1292 lbi = ip6_fib_table_fwding_lookup_with_if_index (im,
1294 (b)->sw_if_index[VLIB_RX],
1297 lb0 = load_balance_get (lbi);
1299 return (fib_urpf_check_size (lb0->lb_urpf));
1303 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1305 ip6_main_t *im = &ip6_main;
1306 ip_lookup_main_t *lm = &im->lookup_main;
1307 ip_local_next_t next_index;
1308 u32 *from, *to_next, n_left_from, n_left_to_next;
1309 vlib_node_runtime_t *error_node =
1310 vlib_node_get_runtime (vm, ip6_input_node.index);
1312 from = vlib_frame_vector_args (frame);
1313 n_left_from = frame->n_vectors;
1314 next_index = node->cached_next_index;
1316 if (node->flags & VLIB_NODE_FLAG_TRACE)
1317 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1319 while (n_left_from > 0)
1321 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1323 while (n_left_from >= 4 && n_left_to_next >= 2)
1325 vlib_buffer_t *p0, *p1;
1326 ip6_header_t *ip0, *ip1;
1327 udp_header_t *udp0, *udp1;
1328 u32 pi0, ip_len0, udp_len0, flags0, next0;
1329 u32 pi1, ip_len1, udp_len1, flags1, next1;
1330 i32 len_diff0, len_diff1;
1331 u8 error0, type0, good_l4_checksum0;
1332 u8 error1, type1, good_l4_checksum1;
1333 u32 udp_offset0, udp_offset1;
1335 pi0 = to_next[0] = from[0];
1336 pi1 = to_next[1] = from[1];
1340 n_left_to_next -= 2;
1342 p0 = vlib_get_buffer (vm, pi0);
1343 p1 = vlib_get_buffer (vm, pi1);
1345 ip0 = vlib_buffer_get_current (p0);
1346 ip1 = vlib_buffer_get_current (p1);
1348 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1349 vnet_buffer (p1)->ip.start_of_ip_header = p1->current_data;
1351 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1352 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1354 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1355 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1360 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1361 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1365 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1369 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1370 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1371 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1372 && udp0->checksum == 0;
1373 /* Verify UDP length. */
1374 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1375 udp_len0 = clib_net_to_host_u16 (udp0->length);
1376 len_diff0 = ip_len0 - udp_len0;
1378 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p1, ip1,
1382 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1383 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1384 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1385 && udp1->checksum == 0;
1386 /* Verify UDP length. */
1387 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1388 udp_len1 = clib_net_to_host_u16 (udp1->length);
1389 len_diff1 = ip_len1 - udp_len1;
1392 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1393 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1395 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1396 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1398 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1399 && !good_l4_checksum0
1400 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1402 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1404 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1406 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1407 && !good_l4_checksum1
1408 && !(flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1410 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1412 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1415 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1417 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1418 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1420 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1421 IP6_ERROR_UDP_CHECKSUM);
1422 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1423 IP6_ERROR_ICMP_CHECKSUM);
1425 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1427 (!good_l4_checksum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1429 /* Drop packets from unroutable hosts. */
1430 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1431 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1432 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1433 !ip6_address_is_link_local_unicast (&ip0->src_address))
1435 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1436 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1438 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1439 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1440 !ip6_address_is_link_local_unicast (&ip1->src_address))
1442 error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1443 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1447 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1449 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1451 p0->error = error_node->errors[error0];
1452 p1->error = error_node->errors[error1];
1454 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1455 to_next, n_left_to_next,
1456 pi0, pi1, next0, next1);
1459 while (n_left_from > 0 && n_left_to_next > 0)
1464 u32 pi0, ip_len0, udp_len0, flags0, next0;
1466 u8 error0, type0, good_l4_checksum0;
1469 pi0 = to_next[0] = from[0];
1473 n_left_to_next -= 1;
1475 p0 = vlib_get_buffer (vm, pi0);
1477 ip0 = vlib_buffer_get_current (p0);
1479 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1481 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1482 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1486 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1489 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1493 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1494 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1495 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1496 && udp0->checksum == 0;
1497 /* Verify UDP length. */
1498 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1499 udp_len0 = clib_net_to_host_u16 (udp0->length);
1500 len_diff0 = ip_len0 - udp_len0;
1503 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1504 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1506 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1507 && !good_l4_checksum0
1508 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1510 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1512 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1515 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1517 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1519 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1520 IP6_ERROR_UDP_CHECKSUM);
1521 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1522 IP6_ERROR_ICMP_CHECKSUM);
1524 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1526 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1527 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1528 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1529 !ip6_address_is_link_local_unicast (&ip0->src_address))
1531 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1532 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1536 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1538 p0->error = error_node->errors[error0];
1540 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1541 to_next, n_left_to_next,
1545 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1548 return frame->n_vectors;
1552 VLIB_REGISTER_NODE (ip6_local_node, static) =
1554 .function = ip6_local,
1555 .name = "ip6-local",
1556 .vector_size = sizeof (u32),
1557 .format_trace = format_ip6_forward_next_trace,
1558 .n_next_nodes = IP_LOCAL_N_NEXT,
1561 [IP_LOCAL_NEXT_DROP] = "error-drop",
1562 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1563 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1564 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1569 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1572 ip6_register_protocol (u32 protocol, u32 node_index)
1574 vlib_main_t *vm = vlib_get_main ();
1575 ip6_main_t *im = &ip6_main;
1576 ip_lookup_main_t *lm = &im->lookup_main;
1578 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1579 lm->local_next_by_ip_protocol[protocol] =
1580 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1585 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1586 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1587 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1588 } ip6_discover_neighbor_next_t;
1592 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1593 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1594 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1595 } ip6_discover_neighbor_error_t;
1598 ip6_discover_neighbor_inline (vlib_main_t * vm,
1599 vlib_node_runtime_t * node,
1600 vlib_frame_t * frame, int is_glean)
1602 vnet_main_t *vnm = vnet_get_main ();
1603 ip6_main_t *im = &ip6_main;
1604 ip_lookup_main_t *lm = &im->lookup_main;
1605 u32 *from, *to_next_drop;
1606 uword n_left_from, n_left_to_next_drop;
1607 static f64 time_last_seed_change = -1e100;
1608 static u32 hash_seeds[3];
1609 static uword hash_bitmap[256 / BITS (uword)];
1613 if (node->flags & VLIB_NODE_FLAG_TRACE)
1614 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1616 time_now = vlib_time_now (vm);
1617 if (time_now - time_last_seed_change > 1e-3)
1620 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1621 sizeof (hash_seeds));
1622 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1623 hash_seeds[i] = r[i];
1625 /* Mark all hash keys as been not-seen before. */
1626 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1629 time_last_seed_change = time_now;
1632 from = vlib_frame_vector_args (frame);
1633 n_left_from = frame->n_vectors;
1635 while (n_left_from > 0)
1637 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1638 to_next_drop, n_left_to_next_drop);
1640 while (n_left_from > 0 && n_left_to_next_drop > 0)
1644 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1646 ip_adjacency_t *adj0;
1647 vnet_hw_interface_t *hw_if0;
1652 p0 = vlib_get_buffer (vm, pi0);
1654 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1656 ip0 = vlib_buffer_get_current (p0);
1658 adj0 = adj_get (adj_index0);
1662 ip0->dst_address.as_u64[0] =
1663 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1664 ip0->dst_address.as_u64[1] =
1665 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1672 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1673 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1676 b0 ^= ip0->dst_address.as_u32[0];
1677 c0 ^= ip0->dst_address.as_u32[1];
1679 hash_v3_mix32 (a0, b0, c0);
1681 b0 ^= ip0->dst_address.as_u32[2];
1682 c0 ^= ip0->dst_address.as_u32[3];
1684 hash_v3_finalize32 (a0, b0, c0);
1686 c0 &= BITS (hash_bitmap) - 1;
1687 c0 = c0 / BITS (uword);
1688 m0 = (uword) 1 << (c0 % BITS (uword));
1690 bm0 = hash_bitmap[c0];
1691 drop0 = (bm0 & m0) != 0;
1693 /* Mark it as seen. */
1694 hash_bitmap[c0] = bm0 | m0;
1698 to_next_drop[0] = pi0;
1700 n_left_to_next_drop -= 1;
1702 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1704 /* If the interface is link-down, drop the pkt */
1705 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1709 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1710 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1715 * the adj has been updated to a rewrite but the node the DPO that got
1716 * us here hasn't - yet. no big deal. we'll drop while we wait.
1718 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1723 icmp6_neighbor_solicitation_header_t *h0;
1726 h0 = vlib_packet_template_get_packet
1727 (vm, &im->discover_neighbor_packet_template, &bi0);
1730 * Build ethernet header.
1731 * Choose source address based on destination lookup
1734 if (ip6_src_address_for_packet (lm,
1736 &h0->ip.src_address))
1738 /* There is no address on the interface */
1740 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1741 vlib_buffer_free (vm, &bi0, 1);
1746 * Destination address is a solicited node multicast address.
1747 * We need to fill in
1748 * the low 24 bits with low 24 bits of target's address.
1750 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1751 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1752 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1754 h0->neighbor.target_address = ip0->dst_address;
1756 clib_memcpy (h0->link_layer_option.ethernet_address,
1757 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1759 /* $$$$ appears we need this; why is the checksum non-zero? */
1760 h0->neighbor.icmp.checksum = 0;
1761 h0->neighbor.icmp.checksum =
1762 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1765 ASSERT (bogus_length == 0);
1767 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1768 b0 = vlib_get_buffer (vm, bi0);
1769 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1770 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1772 /* Add rewrite/encap string. */
1773 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1774 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1776 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1778 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1782 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1783 n_left_to_next_drop);
1786 return frame->n_vectors;
1790 ip6_discover_neighbor (vlib_main_t * vm,
1791 vlib_node_runtime_t * node, vlib_frame_t * frame)
1793 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1797 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1799 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1802 static char *ip6_discover_neighbor_error_strings[] = {
1803 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1804 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1805 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1806 = "no source address for ND solicitation",
1810 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1812 .function = ip6_discover_neighbor,
1813 .name = "ip6-discover-neighbor",
1814 .vector_size = sizeof (u32),
1815 .format_trace = format_ip6_forward_next_trace,
1816 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1817 .error_strings = ip6_discover_neighbor_error_strings,
1818 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1821 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1822 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1828 VLIB_REGISTER_NODE (ip6_glean_node) =
1830 .function = ip6_glean,
1831 .name = "ip6-glean",
1832 .vector_size = sizeof (u32),
1833 .format_trace = format_ip6_forward_next_trace,
1834 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1835 .error_strings = ip6_discover_neighbor_error_strings,
1836 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1839 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1840 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1846 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1848 vnet_main_t *vnm = vnet_get_main ();
1849 ip6_main_t *im = &ip6_main;
1850 icmp6_neighbor_solicitation_header_t *h;
1852 ip_interface_address_t *ia;
1853 ip_adjacency_t *adj;
1854 vnet_hw_interface_t *hi;
1855 vnet_sw_interface_t *si;
1860 si = vnet_get_sw_interface (vnm, sw_if_index);
1862 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1864 return clib_error_return (0, "%U: interface %U down",
1865 format_ip6_address, dst,
1866 format_vnet_sw_if_index_name, vnm,
1871 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1874 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1875 return clib_error_return
1876 (0, "no matching interface address for destination %U (interface %U)",
1877 format_ip6_address, dst,
1878 format_vnet_sw_if_index_name, vnm, sw_if_index);
1882 vlib_packet_template_get_packet (vm,
1883 &im->discover_neighbor_packet_template,
1886 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1888 /* Destination address is a solicited node multicast address. We need to fill in
1889 the low 24 bits with low 24 bits of target's address. */
1890 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1891 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1892 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1894 h->ip.src_address = src[0];
1895 h->neighbor.target_address = dst[0];
1897 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1898 vec_len (hi->hw_address));
1900 h->neighbor.icmp.checksum =
1901 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1902 ASSERT (bogus_length == 0);
1904 b = vlib_get_buffer (vm, bi);
1905 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1906 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1908 /* Add encapsulation string for software interface (e.g. ethernet header). */
1909 adj = adj_get (ia->neighbor_probe_adj_index);
1910 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1911 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1914 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1915 u32 *to_next = vlib_frame_vector_args (f);
1918 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1921 return /* no error */ 0;
1926 IP6_REWRITE_NEXT_DROP,
1927 IP6_REWRITE_NEXT_ICMP_ERROR,
1928 } ip6_rewrite_next_t;
1931 ip6_rewrite_inline (vlib_main_t * vm,
1932 vlib_node_runtime_t * node,
1933 vlib_frame_t * frame,
1934 int do_counters, int is_midchain, int is_mcast)
1936 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1937 u32 *from = vlib_frame_vector_args (frame);
1938 u32 n_left_from, n_left_to_next, *to_next, next_index;
1939 vlib_node_runtime_t *error_node =
1940 vlib_node_get_runtime (vm, ip6_input_node.index);
1942 n_left_from = frame->n_vectors;
1943 next_index = node->cached_next_index;
1944 u32 thread_index = vlib_get_thread_index ();
1946 while (n_left_from > 0)
1948 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1950 while (n_left_from >= 4 && n_left_to_next >= 2)
1952 ip_adjacency_t *adj0, *adj1;
1953 vlib_buffer_t *p0, *p1;
1954 ip6_header_t *ip0, *ip1;
1955 u32 pi0, rw_len0, next0, error0, adj_index0;
1956 u32 pi1, rw_len1, next1, error1, adj_index1;
1957 u32 tx_sw_if_index0, tx_sw_if_index1;
1959 /* Prefetch next iteration. */
1961 vlib_buffer_t *p2, *p3;
1963 p2 = vlib_get_buffer (vm, from[2]);
1964 p3 = vlib_get_buffer (vm, from[3]);
1966 vlib_prefetch_buffer_header (p2, LOAD);
1967 vlib_prefetch_buffer_header (p3, LOAD);
1969 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1970 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1972 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1973 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1976 pi0 = to_next[0] = from[0];
1977 pi1 = to_next[1] = from[1];
1982 n_left_to_next -= 2;
1984 p0 = vlib_get_buffer (vm, pi0);
1985 p1 = vlib_get_buffer (vm, pi1);
1987 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1988 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1990 ip0 = vlib_buffer_get_current (p0);
1991 ip1 = vlib_buffer_get_current (p1);
1993 error0 = error1 = IP6_ERROR_NONE;
1994 next0 = next1 = IP6_REWRITE_NEXT_DROP;
1996 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
1998 i32 hop_limit0 = ip0->hop_limit;
2000 /* Input node should have reject packets with hop limit 0. */
2001 ASSERT (ip0->hop_limit > 0);
2005 ip0->hop_limit = hop_limit0;
2008 * If the hop count drops below 1 when forwarding, generate
2011 if (PREDICT_FALSE (hop_limit0 <= 0))
2013 error0 = IP6_ERROR_TIME_EXPIRED;
2014 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2015 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2016 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2017 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2023 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2025 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2027 i32 hop_limit1 = ip1->hop_limit;
2029 /* Input node should have reject packets with hop limit 0. */
2030 ASSERT (ip1->hop_limit > 0);
2034 ip1->hop_limit = hop_limit1;
2037 * If the hop count drops below 1 when forwarding, generate
2040 if (PREDICT_FALSE (hop_limit1 <= 0))
2042 error1 = IP6_ERROR_TIME_EXPIRED;
2043 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2044 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2045 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2046 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2052 p1->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2054 adj0 = adj_get (adj_index0);
2055 adj1 = adj_get (adj_index1);
2057 rw_len0 = adj0[0].rewrite_header.data_bytes;
2058 rw_len1 = adj1[0].rewrite_header.data_bytes;
2059 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2060 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
2064 vlib_increment_combined_counter
2065 (&adjacency_counters,
2066 thread_index, adj_index0, 1,
2067 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2068 vlib_increment_combined_counter
2069 (&adjacency_counters,
2070 thread_index, adj_index1, 1,
2071 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
2074 /* Check MTU of outgoing interface. */
2076 (vlib_buffer_length_in_chain (vm, p0) >
2078 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2081 (vlib_buffer_length_in_chain (vm, p1) >
2083 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2086 /* Don't adjust the buffer for hop count issue; icmp-error node
2087 * wants to see the IP headerr */
2088 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2090 p0->current_data -= rw_len0;
2091 p0->current_length += rw_len0;
2093 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2094 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2095 next0 = adj0[0].rewrite_header.next_index;
2098 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2099 vnet_feature_arc_start (lm->output_feature_arc_index,
2100 tx_sw_if_index0, &next0, p0);
2102 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2104 p1->current_data -= rw_len1;
2105 p1->current_length += rw_len1;
2107 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2108 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2109 next1 = adj1[0].rewrite_header.next_index;
2112 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2113 vnet_feature_arc_start (lm->output_feature_arc_index,
2114 tx_sw_if_index1, &next1, p1);
2117 /* Guess we are only writing on simple Ethernet header. */
2118 vnet_rewrite_two_headers (adj0[0], adj1[0],
2119 ip0, ip1, sizeof (ethernet_header_t));
2123 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2124 adj1->sub_type.midchain.fixup_func (vm, adj1, p1);
2129 * copy bytes from the IP address into the MAC rewrite
2131 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2132 vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1);
2135 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2136 to_next, n_left_to_next,
2137 pi0, pi1, next0, next1);
2140 while (n_left_from > 0 && n_left_to_next > 0)
2142 ip_adjacency_t *adj0;
2146 u32 adj_index0, next0, error0;
2147 u32 tx_sw_if_index0;
2149 pi0 = to_next[0] = from[0];
2151 p0 = vlib_get_buffer (vm, pi0);
2153 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2155 adj0 = adj_get (adj_index0);
2157 ip0 = vlib_buffer_get_current (p0);
2159 error0 = IP6_ERROR_NONE;
2160 next0 = IP6_REWRITE_NEXT_DROP;
2162 /* Check hop limit */
2163 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2165 i32 hop_limit0 = ip0->hop_limit;
2167 ASSERT (ip0->hop_limit > 0);
2171 ip0->hop_limit = hop_limit0;
2173 if (PREDICT_FALSE (hop_limit0 <= 0))
2176 * If the hop count drops below 1 when forwarding, generate
2179 error0 = IP6_ERROR_TIME_EXPIRED;
2180 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2181 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2182 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2183 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2189 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2192 /* Guess we are only writing on simple Ethernet header. */
2193 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2195 /* Update packet buffer attributes/set output interface. */
2196 rw_len0 = adj0[0].rewrite_header.data_bytes;
2197 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2201 vlib_increment_combined_counter
2202 (&adjacency_counters,
2203 thread_index, adj_index0, 1,
2204 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2207 /* Check MTU of outgoing interface. */
2209 (vlib_buffer_length_in_chain (vm, p0) >
2211 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2214 /* Don't adjust the buffer for hop count issue; icmp-error node
2215 * wants to see the IP headerr */
2216 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2218 p0->current_data -= rw_len0;
2219 p0->current_length += rw_len0;
2221 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2223 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2224 next0 = adj0[0].rewrite_header.next_index;
2227 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2228 vnet_feature_arc_start (lm->output_feature_arc_index,
2229 tx_sw_if_index0, &next0, p0);
2234 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2238 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2241 p0->error = error_node->errors[error0];
2246 n_left_to_next -= 1;
2248 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2249 to_next, n_left_to_next,
2253 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2256 /* Need to do trace after rewrites to pick up new packet data. */
2257 if (node->flags & VLIB_NODE_FLAG_TRACE)
2258 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2260 return frame->n_vectors;
2264 ip6_rewrite (vlib_main_t * vm,
2265 vlib_node_runtime_t * node, vlib_frame_t * frame)
2267 if (adj_are_counters_enabled ())
2268 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2270 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2274 ip6_rewrite_mcast (vlib_main_t * vm,
2275 vlib_node_runtime_t * node, vlib_frame_t * frame)
2277 if (adj_are_counters_enabled ())
2278 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2280 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2284 ip6_midchain (vlib_main_t * vm,
2285 vlib_node_runtime_t * node, vlib_frame_t * frame)
2287 if (adj_are_counters_enabled ())
2288 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2290 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2294 ip6_mcast_midchain (vlib_main_t * vm,
2295 vlib_node_runtime_t * node, vlib_frame_t * frame)
2297 if (adj_are_counters_enabled ())
2298 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2300 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2304 VLIB_REGISTER_NODE (ip6_midchain_node) =
2306 .function = ip6_midchain,
2307 .name = "ip6-midchain",
2308 .vector_size = sizeof (u32),
2309 .format_trace = format_ip6_forward_next_trace,
2310 .sibling_of = "ip6-rewrite",
2314 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2317 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2319 .function = ip6_rewrite,
2320 .name = "ip6-rewrite",
2321 .vector_size = sizeof (u32),
2322 .format_trace = format_ip6_rewrite_trace,
2326 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2327 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2332 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2335 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2337 .function = ip6_rewrite_mcast,
2338 .name = "ip6-rewrite-mcast",
2339 .vector_size = sizeof (u32),
2340 .format_trace = format_ip6_rewrite_trace,
2341 .sibling_of = "ip6-rewrite",
2345 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2348 VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
2350 .function = ip6_mcast_midchain,
2351 .name = "ip6-mcast-midchain",
2352 .vector_size = sizeof (u32),
2353 .format_trace = format_ip6_rewrite_trace,
2354 .sibling_of = "ip6-rewrite",
2358 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
2361 * Hop-by-Hop handling
2363 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2365 #define foreach_ip6_hop_by_hop_error \
2366 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2367 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2368 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2373 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2374 foreach_ip6_hop_by_hop_error
2376 IP6_HOP_BY_HOP_N_ERROR,
2377 } ip6_hop_by_hop_error_t;
2381 * Primary h-b-h handler trace support
2382 * We work pretty hard on the problem for obvious reasons
2388 u8 option_data[256];
2389 } ip6_hop_by_hop_trace_t;
2391 vlib_node_registration_t ip6_hop_by_hop_node;
2393 static char *ip6_hop_by_hop_error_strings[] = {
2394 #define _(sym,string) string,
2395 foreach_ip6_hop_by_hop_error
2400 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2402 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2403 int total_len = va_arg (*args, int);
2404 ip6_hop_by_hop_option_t *opt0, *limit0;
2405 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2408 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2409 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2411 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2412 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2414 while (opt0 < limit0)
2419 case 0: /* Pad, just stop */
2420 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2424 if (hm->trace[type0])
2426 s = (*hm->trace[type0]) (s, opt0);
2431 format (s, "\n unrecognized option %d length %d", type0,
2435 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2436 sizeof (ip6_hop_by_hop_option_t));
2444 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2446 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2447 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2448 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2449 ip6_hop_by_hop_header_t *hbh0;
2450 ip6_hop_by_hop_option_t *opt0, *limit0;
2451 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2455 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2457 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2458 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2460 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2461 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2463 while (opt0 < limit0)
2468 case 0: /* Pad, just stop */
2469 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2473 if (hm->trace[type0])
2475 s = (*hm->trace[type0]) (s, opt0);
2480 format (s, "\n unrecognized option %d length %d", type0,
2484 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2485 sizeof (ip6_hop_by_hop_option_t));
2493 ip6_scan_hbh_options (vlib_buffer_t * b0,
2495 ip6_hop_by_hop_header_t * hbh0,
2496 ip6_hop_by_hop_option_t * opt0,
2497 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2499 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2503 while (opt0 < limit0)
2509 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2514 if (hm->options[type0])
2516 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2518 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2524 /* Unrecognized mandatory option, check the two high order bits */
2525 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2527 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2529 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2530 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2531 *next0 = IP_LOOKUP_NEXT_DROP;
2533 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2534 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2535 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2536 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2537 ICMP6_parameter_problem_unrecognized_option,
2538 (u8 *) opt0 - (u8 *) ip0);
2540 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2541 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2542 if (!ip6_address_is_multicast (&ip0->dst_address))
2544 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2545 icmp6_error_set_vnet_buffer (b0,
2546 ICMP6_parameter_problem,
2547 ICMP6_parameter_problem_unrecognized_option,
2548 (u8 *) opt0 - (u8 *) ip0);
2552 *next0 = IP_LOOKUP_NEXT_DROP;
2560 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2561 sizeof (ip6_hop_by_hop_option_t));
2567 * Process the Hop-by-Hop Options header
2570 ip6_hop_by_hop (vlib_main_t * vm,
2571 vlib_node_runtime_t * node, vlib_frame_t * frame)
2573 vlib_node_runtime_t *error_node =
2574 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2575 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2576 u32 n_left_from, *from, *to_next;
2577 ip_lookup_next_t next_index;
2579 from = vlib_frame_vector_args (frame);
2580 n_left_from = frame->n_vectors;
2581 next_index = node->cached_next_index;
2583 while (n_left_from > 0)
2587 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2589 while (n_left_from >= 4 && n_left_to_next >= 2)
2592 vlib_buffer_t *b0, *b1;
2594 ip6_header_t *ip0, *ip1;
2595 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2596 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2597 u8 error0 = 0, error1 = 0;
2599 /* Prefetch next iteration. */
2601 vlib_buffer_t *p2, *p3;
2603 p2 = vlib_get_buffer (vm, from[2]);
2604 p3 = vlib_get_buffer (vm, from[3]);
2606 vlib_prefetch_buffer_header (p2, LOAD);
2607 vlib_prefetch_buffer_header (p3, LOAD);
2609 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2610 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2613 /* Speculatively enqueue b0, b1 to the current next frame */
2614 to_next[0] = bi0 = from[0];
2615 to_next[1] = bi1 = from[1];
2619 n_left_to_next -= 2;
2621 b0 = vlib_get_buffer (vm, bi0);
2622 b1 = vlib_get_buffer (vm, bi1);
2624 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2625 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2626 ip_adjacency_t *adj0 = adj_get (adj_index0);
2627 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2628 ip_adjacency_t *adj1 = adj_get (adj_index1);
2630 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2631 next0 = adj0->lookup_next_index;
2632 next1 = adj1->lookup_next_index;
2634 ip0 = vlib_buffer_get_current (b0);
2635 ip1 = vlib_buffer_get_current (b1);
2636 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2637 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2638 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2639 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2641 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2642 ((hbh0->length + 1) << 3));
2644 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2645 ((hbh1->length + 1) << 3));
2648 * Basic validity checks
2650 if ((hbh0->length + 1) << 3 >
2651 clib_net_to_host_u16 (ip0->payload_length))
2653 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2654 next0 = IP_LOOKUP_NEXT_DROP;
2657 /* Scan the set of h-b-h options, process ones that we understand */
2658 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2660 if ((hbh1->length + 1) << 3 >
2661 clib_net_to_host_u16 (ip1->payload_length))
2663 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2664 next1 = IP_LOOKUP_NEXT_DROP;
2667 /* Scan the set of h-b-h options, process ones that we understand */
2668 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2671 /* Has the classifier flagged this buffer for special treatment? */
2674 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2675 next0 = hm->next_override;
2677 /* Has the classifier flagged this buffer for special treatment? */
2680 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2681 next1 = hm->next_override;
2683 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2685 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2687 ip6_hop_by_hop_trace_t *t =
2688 vlib_add_trace (vm, node, b0, sizeof (*t));
2689 u32 trace_len = (hbh0->length + 1) << 3;
2690 t->next_index = next0;
2691 /* Capture the h-b-h option verbatim */
2694 ARRAY_LEN (t->option_data) ? trace_len :
2695 ARRAY_LEN (t->option_data);
2696 t->trace_len = trace_len;
2697 clib_memcpy (t->option_data, hbh0, trace_len);
2699 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2701 ip6_hop_by_hop_trace_t *t =
2702 vlib_add_trace (vm, node, b1, sizeof (*t));
2703 u32 trace_len = (hbh1->length + 1) << 3;
2704 t->next_index = next1;
2705 /* Capture the h-b-h option verbatim */
2708 ARRAY_LEN (t->option_data) ? trace_len :
2709 ARRAY_LEN (t->option_data);
2710 t->trace_len = trace_len;
2711 clib_memcpy (t->option_data, hbh1, trace_len);
2716 b0->error = error_node->errors[error0];
2717 b1->error = error_node->errors[error1];
2719 /* verify speculative enqueue, maybe switch current next frame */
2720 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2721 n_left_to_next, bi0, bi1, next0,
2725 while (n_left_from > 0 && n_left_to_next > 0)
2731 ip6_hop_by_hop_header_t *hbh0;
2732 ip6_hop_by_hop_option_t *opt0, *limit0;
2735 /* Speculatively enqueue b0 to the current next frame */
2741 n_left_to_next -= 1;
2743 b0 = vlib_get_buffer (vm, bi0);
2745 * Default use the next_index from the adjacency.
2746 * A HBH option rarely redirects to a different node
2748 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2749 ip_adjacency_t *adj0 = adj_get (adj_index0);
2750 next0 = adj0->lookup_next_index;
2752 ip0 = vlib_buffer_get_current (b0);
2753 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2754 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2756 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2757 ((hbh0->length + 1) << 3));
2760 * Basic validity checks
2762 if ((hbh0->length + 1) << 3 >
2763 clib_net_to_host_u16 (ip0->payload_length))
2765 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2766 next0 = IP_LOOKUP_NEXT_DROP;
2770 /* Scan the set of h-b-h options, process ones that we understand */
2771 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2774 /* Has the classifier flagged this buffer for special treatment? */
2777 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2778 next0 = hm->next_override;
2780 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2782 ip6_hop_by_hop_trace_t *t =
2783 vlib_add_trace (vm, node, b0, sizeof (*t));
2784 u32 trace_len = (hbh0->length + 1) << 3;
2785 t->next_index = next0;
2786 /* Capture the h-b-h option verbatim */
2789 ARRAY_LEN (t->option_data) ? trace_len :
2790 ARRAY_LEN (t->option_data);
2791 t->trace_len = trace_len;
2792 clib_memcpy (t->option_data, hbh0, trace_len);
2795 b0->error = error_node->errors[error0];
2797 /* verify speculative enqueue, maybe switch current next frame */
2798 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2799 n_left_to_next, bi0, next0);
2801 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2803 return frame->n_vectors;
2807 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2809 .function = ip6_hop_by_hop,
2810 .name = "ip6-hop-by-hop",
2811 .sibling_of = "ip6-lookup",
2812 .vector_size = sizeof (u32),
2813 .format_trace = format_ip6_hop_by_hop_trace,
2814 .type = VLIB_NODE_TYPE_INTERNAL,
2815 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2816 .error_strings = ip6_hop_by_hop_error_strings,
2821 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2823 static clib_error_t *
2824 ip6_hop_by_hop_init (vlib_main_t * vm)
2826 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2827 memset (hm->options, 0, sizeof (hm->options));
2828 memset (hm->trace, 0, sizeof (hm->trace));
2829 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2833 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2836 ip6_hbh_set_next_override (uword next)
2838 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2840 hm->next_override = next;
2844 ip6_hbh_register_option (u8 option,
2845 int options (vlib_buffer_t * b, ip6_header_t * ip,
2846 ip6_hop_by_hop_option_t * opt),
2847 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2849 ip6_main_t *im = &ip6_main;
2850 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2852 ASSERT (option < ARRAY_LEN (hm->options));
2854 /* Already registered */
2855 if (hm->options[option])
2858 hm->options[option] = options;
2859 hm->trace[option] = trace;
2861 /* Set global variable */
2862 im->hbh_enabled = 1;
2868 ip6_hbh_unregister_option (u8 option)
2870 ip6_main_t *im = &ip6_main;
2871 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2873 ASSERT (option < ARRAY_LEN (hm->options));
2875 /* Not registered */
2876 if (!hm->options[option])
2879 hm->options[option] = NULL;
2880 hm->trace[option] = NULL;
2882 /* Disable global knob if this was the last option configured */
2885 for (i = 0; i < 256; i++)
2887 if (hm->options[option])
2894 im->hbh_enabled = 0;
2899 /* Global IP6 main. */
2900 ip6_main_t ip6_main;
2902 static clib_error_t *
2903 ip6_lookup_init (vlib_main_t * vm)
2905 ip6_main_t *im = &ip6_main;
2906 clib_error_t *error;
2909 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2912 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2919 for (j = 0; j < i0; j++)
2920 im->fib_masks[i].as_u32[j] = ~0;
2923 im->fib_masks[i].as_u32[i0] =
2924 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2927 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2929 if (im->lookup_table_nbuckets == 0)
2930 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2932 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2934 if (im->lookup_table_size == 0)
2935 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2937 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2938 "ip6 FIB fwding table",
2939 im->lookup_table_nbuckets, im->lookup_table_size);
2940 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2941 "ip6 FIB non-fwding table",
2942 im->lookup_table_nbuckets, im->lookup_table_size);
2944 /* Create FIB with index 0 and table id of 0. */
2945 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2946 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2950 pn = pg_get_node (ip6_lookup_node.index);
2951 pn->unformat_edit = unformat_pg_ip6_header;
2954 /* Unless explicitly configured, don't process HBH options */
2955 im->hbh_enabled = 0;
2958 icmp6_neighbor_solicitation_header_t p;
2960 memset (&p, 0, sizeof (p));
2962 p.ip.ip_version_traffic_class_and_flow_label =
2963 clib_host_to_net_u32 (0x6 << 28);
2964 p.ip.payload_length =
2965 clib_host_to_net_u16 (sizeof (p) -
2967 (icmp6_neighbor_solicitation_header_t, neighbor));
2968 p.ip.protocol = IP_PROTOCOL_ICMP6;
2969 p.ip.hop_limit = 255;
2970 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2972 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2974 p.link_layer_option.header.type =
2975 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2976 p.link_layer_option.header.n_data_u64s =
2977 sizeof (p.link_layer_option) / sizeof (u64);
2979 vlib_packet_template_init (vm,
2980 &im->discover_neighbor_packet_template,
2982 /* alloc chunk size */ 8,
2983 "ip6 neighbor discovery");
2989 VLIB_INIT_FUNCTION (ip6_lookup_init);
2991 static clib_error_t *
2992 add_del_ip6_interface_table (vlib_main_t * vm,
2993 unformat_input_t * input,
2994 vlib_cli_command_t * cmd)
2996 vnet_main_t *vnm = vnet_get_main ();
2997 ip_interface_address_t *ia;
2998 clib_error_t *error = 0;
2999 u32 sw_if_index, table_id;
3003 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3005 error = clib_error_return (0, "unknown interface `%U'",
3006 format_unformat_error, input);
3010 if (unformat (input, "%d", &table_id))
3014 error = clib_error_return (0, "expected table id `%U'",
3015 format_unformat_error, input);
3020 * If the interface already has in IP address, then a change int
3021 * VRF is not allowed. The IP address applied must first be removed.
3022 * We do not do that automatically here, since VPP has no knowledge
3023 * of whether thoses subnets are valid in the destination VRF.
3026 foreach_ip_interface_address (&ip6_main.lookup_main,
3028 1 /* honor unnumbered */,
3032 a = ip_interface_address_get_address (&ip6_main.lookup_main, ia);
3033 error = clib_error_return (0, "interface %U has address %U",
3034 format_vnet_sw_if_index_name, vnm,
3036 format_ip6_address, a);
3042 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3045 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
3046 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
3048 fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3051 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
3052 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = fib_index;
3061 * Place the indicated interface into the supplied IPv6 FIB table (also known
3062 * as a VRF). If the FIB table does not exist, this command creates it. To
3063 * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
3064 * FIB table will only be displayed if a route has been added to the table, or
3065 * an IP Address is assigned to an interface in the table (which adds a route
3068 * @note IP addresses added after setting the interface IP table are added to
3069 * the indicated FIB table. If an IP address is added prior to changing the
3070 * table then this is an error. The control plane must remove these addresses
3071 * first and then change the table. VPP will not automatically move the
3072 * addresses from the old to the new table as it does not know the validity
3076 * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
3077 * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
3080 VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
3082 .path = "set interface ip6 table",
3083 .function = add_del_ip6_interface_table,
3084 .short_help = "set interface ip6 table <interface> <table-id>"
3089 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
3092 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3093 /* Invert the "u" bit */
3094 ip->as_u8[8] = mac[0] ^ (1 << 1);
3095 ip->as_u8[9] = mac[1];
3096 ip->as_u8[10] = mac[2];
3097 ip->as_u8[11] = 0xFF;
3098 ip->as_u8[12] = 0xFE;
3099 ip->as_u8[13] = mac[3];
3100 ip->as_u8[14] = mac[4];
3101 ip->as_u8[15] = mac[5];
3105 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3108 /* Invert the previously inverted "u" bit */
3109 mac[0] = ip->as_u8[8] ^ (1 << 1);
3110 mac[1] = ip->as_u8[9];
3111 mac[2] = ip->as_u8[10];
3112 mac[3] = ip->as_u8[13];
3113 mac[4] = ip->as_u8[14];
3114 mac[5] = ip->as_u8[15];
3117 static clib_error_t *
3118 test_ip6_link_command_fn (vlib_main_t * vm,
3119 unformat_input_t * input, vlib_cli_command_t * cmd)
3122 ip6_address_t _a, *a = &_a;
3124 if (unformat (input, "%U", unformat_ethernet_address, mac))
3126 ip6_link_local_address_from_ethernet_mac_address (a, mac);
3127 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
3128 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3129 vlib_cli_output (vm, "Original MAC address: %U",
3130 format_ethernet_address, mac);
3137 * This command converts the given MAC Address into an IPv6 link-local
3141 * Example of how to create an IPv6 link-local address:
3142 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3143 * Link local address: fe80::14d9:e0ff:fe91:7986
3144 * Original MAC address: 16:d9:e0:91:79:86
3148 VLIB_CLI_COMMAND (test_link_command, static) =
3150 .path = "test ip6 link",
3151 .function = test_ip6_link_command_fn,
3152 .short_help = "test ip6 link <mac-address>",
3157 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3161 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
3163 if (~0 == fib_index)
3164 return VNET_API_ERROR_NO_SUCH_FIB;
3166 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
3172 static clib_error_t *
3173 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3174 unformat_input_t * input,
3175 vlib_cli_command_t * cmd)
3179 u32 flow_hash_config = 0;
3182 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3184 if (unformat (input, "table %d", &table_id))
3187 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3188 foreach_flow_hash_bit
3195 return clib_error_return (0, "unknown input `%U'",
3196 format_unformat_error, input);
3198 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3205 return clib_error_return (0, "no such FIB table %d", table_id);
3208 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3216 * Configure the set of IPv6 fields used by the flow hash.
3220 * Example of how to set the flow hash on a given table:
3221 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3223 * Example of display the configured flow hash:
3224 * @cliexstart{show ip6 fib}
3225 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3228 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3229 * [0] [@0]: dpo-drop ip6
3232 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3233 * [0] [@2]: dpo-receive
3236 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3237 * [0] [@2]: dpo-receive
3240 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3241 * [0] [@2]: dpo-receive
3244 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3245 * [0] [@2]: dpo-receive
3246 * ff02::1:ff00:0/104
3248 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3249 * [0] [@2]: dpo-receive
3250 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3253 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3254 * [0] [@0]: dpo-drop ip6
3257 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3258 * [0] [@4]: ipv6-glean: af_packet0
3261 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3262 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3265 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3266 * [0] [@2]: dpo-receive
3267 * fe80::fe:3eff:fe3e:9222/128
3269 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3270 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3273 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3274 * [0] [@2]: dpo-receive
3277 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3278 * [0] [@2]: dpo-receive
3281 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3282 * [0] [@2]: dpo-receive
3283 * ff02::1:ff00:0/104
3285 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3286 * [0] [@2]: dpo-receive
3291 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3293 .path = "set ip6 flow-hash",
3295 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3296 .function = set_ip6_flow_hash_command_fn,
3300 static clib_error_t *
3301 show_ip6_local_command_fn (vlib_main_t * vm,
3302 unformat_input_t * input, vlib_cli_command_t * cmd)
3304 ip6_main_t *im = &ip6_main;
3305 ip_lookup_main_t *lm = &im->lookup_main;
3308 vlib_cli_output (vm, "Protocols handled by ip6_local");
3309 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3311 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3312 vlib_cli_output (vm, "%d", i);
3320 * Display the set of protocols handled by the local IPv6 stack.
3323 * Example of how to display local protocol table:
3324 * @cliexstart{show ip6 local}
3325 * Protocols handled by ip6_local
3333 VLIB_CLI_COMMAND (show_ip6_local, static) =
3335 .path = "show ip6 local",
3336 .function = show_ip6_local_command_fn,
3337 .short_help = "show ip6 local",
3342 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3345 vnet_main_t *vnm = vnet_get_main ();
3346 vnet_interface_main_t *im = &vnm->interface_main;
3347 ip6_main_t *ipm = &ip6_main;
3348 ip_lookup_main_t *lm = &ipm->lookup_main;
3349 vnet_classify_main_t *cm = &vnet_classify_main;
3350 ip6_address_t *if_addr;
3352 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3353 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3355 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3356 return VNET_API_ERROR_NO_SUCH_ENTRY;
3358 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3359 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3361 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3363 if (NULL != if_addr)
3365 fib_prefix_t pfx = {
3367 .fp_proto = FIB_PROTOCOL_IP6,
3368 .fp_addr.ip6 = *if_addr,
3372 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3376 if (table_index != (u32) ~ 0)
3378 dpo_id_t dpo = DPO_INVALID;
3383 classify_dpo_create (DPO_PROTO_IP6, table_index));
3385 fib_table_entry_special_dpo_add (fib_index,
3387 FIB_SOURCE_CLASSIFY,
3388 FIB_ENTRY_FLAG_NONE, &dpo);
3393 fib_table_entry_special_remove (fib_index,
3394 &pfx, FIB_SOURCE_CLASSIFY);
3401 static clib_error_t *
3402 set_ip6_classify_command_fn (vlib_main_t * vm,
3403 unformat_input_t * input,
3404 vlib_cli_command_t * cmd)
3406 u32 table_index = ~0;
3407 int table_index_set = 0;
3408 u32 sw_if_index = ~0;
3411 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3413 if (unformat (input, "table-index %d", &table_index))
3414 table_index_set = 1;
3415 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3416 vnet_get_main (), &sw_if_index))
3422 if (table_index_set == 0)
3423 return clib_error_return (0, "classify table-index must be specified");
3425 if (sw_if_index == ~0)
3426 return clib_error_return (0, "interface / subif must be specified");
3428 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3435 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3436 return clib_error_return (0, "No such interface");
3438 case VNET_API_ERROR_NO_SUCH_ENTRY:
3439 return clib_error_return (0, "No such classifier table");
3445 * Assign a classification table to an interface. The classification
3446 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3447 * commands. Once the table is create, use this command to filter packets
3451 * Example of how to assign a classification table to an interface:
3452 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3455 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3457 .path = "set ip6 classify",
3459 "set ip6 classify intfc <interface> table-index <classify-idx>",
3460 .function = set_ip6_classify_command_fn,
3464 static clib_error_t *
3465 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3467 ip6_main_t *im = &ip6_main;
3472 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3474 if (unformat (input, "hash-buckets %d", &tmp))
3476 else if (unformat (input, "heap-size %dm", &tmp))
3477 heapsize = ((u64) tmp) << 20;
3478 else if (unformat (input, "heap-size %dM", &tmp))
3479 heapsize = ((u64) tmp) << 20;
3480 else if (unformat (input, "heap-size %dg", &tmp))
3481 heapsize = ((u64) tmp) << 30;
3482 else if (unformat (input, "heap-size %dG", &tmp))
3483 heapsize = ((u64) tmp) << 30;
3485 return clib_error_return (0, "unknown input '%U'",
3486 format_unformat_error, input);
3489 im->lookup_table_nbuckets = nbuckets;
3490 im->lookup_table_size = heapsize;
3495 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3498 * fd.io coding-style-patch-verification: ON
3501 * eval: (c-set-style "gnu")