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.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 cpu_index = os_get_cpu_number ();
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);
142 vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
144 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
146 flow_hash_config0 = lb0->lb_hash_config;
147 vnet_buffer (p0)->ip.flow_hash =
148 ip6_compute_flow_hash (ip0, flow_hash_config0);
150 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
152 flow_hash_config1 = lb1->lb_hash_config;
153 vnet_buffer (p1)->ip.flow_hash =
154 ip6_compute_flow_hash (ip1, flow_hash_config1);
157 ASSERT (lb0->lb_n_buckets > 0);
158 ASSERT (lb1->lb_n_buckets > 0);
159 ASSERT (is_pow2 (lb0->lb_n_buckets));
160 ASSERT (is_pow2 (lb1->lb_n_buckets));
161 dpo0 = load_balance_get_bucket_i (lb0,
162 (vnet_buffer (p0)->ip.flow_hash &
163 lb0->lb_n_buckets_minus_1));
164 dpo1 = load_balance_get_bucket_i (lb1,
165 (vnet_buffer (p1)->ip.flow_hash &
166 lb1->lb_n_buckets_minus_1));
168 next0 = dpo0->dpoi_next_node;
169 next1 = dpo1->dpoi_next_node;
171 /* Only process the HBH Option Header if explicitly configured to do so */
173 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
175 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
176 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
179 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
181 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
182 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
184 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
185 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
187 vlib_increment_combined_counter
188 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
189 vlib_increment_combined_counter
190 (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
197 wrong_next = (next0 != next) + 2 * (next1 != next);
198 if (PREDICT_FALSE (wrong_next != 0))
207 vlib_set_next_frame_buffer (vm, node, next0, pi0);
214 vlib_set_next_frame_buffer (vm, node, next1, pi1);
221 vlib_set_next_frame_buffer (vm, node, next0, pi0);
222 vlib_set_next_frame_buffer (vm, node, next1, pi1);
226 vlib_put_next_frame (vm, node, next, n_left_to_next);
228 vlib_get_next_frame (vm, node, next, to_next,
235 while (n_left_from > 0 && n_left_to_next > 0)
240 ip_lookup_next_t next0;
242 ip6_address_t *dst_addr0;
243 u32 fib_index0, flow_hash_config0;
244 const dpo_id_t *dpo0;
249 p0 = vlib_get_buffer (vm, pi0);
251 ip0 = vlib_buffer_get_current (p0);
253 dst_addr0 = &ip0->dst_address;
256 vec_elt (im->fib_index_by_sw_if_index,
257 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
259 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
260 (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
262 flow_hash_config0 = ip6_fib_get (fib_index0)->flow_hash_config;
264 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
266 lb0 = load_balance_get (lbi0);
268 vnet_buffer (p0)->ip.flow_hash = 0;
270 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
272 flow_hash_config0 = lb0->lb_hash_config;
273 vnet_buffer (p0)->ip.flow_hash =
274 ip6_compute_flow_hash (ip0, flow_hash_config0);
277 ASSERT (lb0->lb_n_buckets > 0);
278 ASSERT (is_pow2 (lb0->lb_n_buckets));
279 dpo0 = load_balance_get_bucket_i (lb0,
280 (vnet_buffer (p0)->ip.flow_hash &
281 lb0->lb_n_buckets_minus_1));
282 next0 = dpo0->dpoi_next_node;
284 /* Only process the HBH Option Header if explicitly configured to do so */
286 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
288 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
289 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
291 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
293 vlib_increment_combined_counter
294 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
301 if (PREDICT_FALSE (next0 != next))
304 vlib_put_next_frame (vm, node, next, n_left_to_next);
306 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
313 vlib_put_next_frame (vm, node, next, n_left_to_next);
316 if (node->flags & VLIB_NODE_FLAG_TRACE)
317 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
319 return frame->n_vectors;
323 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
324 ip6_main_t * im, u32 fib_index,
325 ip_interface_address_t * a)
327 ip_lookup_main_t *lm = &im->lookup_main;
328 ip6_address_t *address = ip_interface_address_get_address (lm, a);
330 .fp_len = a->address_length,
331 .fp_proto = FIB_PROTOCOL_IP6,
332 .fp_addr.ip6 = *address,
335 a->neighbor_probe_adj_index = ~0;
336 if (a->address_length < 128)
338 fib_node_index_t fei;
340 fei = fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_ATTACHED), FIB_PROTOCOL_IP6, NULL, /* No next-hop address */
341 sw_if_index, ~0, // invalid FIB index
342 1, NULL, // no label stack
343 FIB_ROUTE_PATH_FLAG_NONE);
344 a->neighbor_probe_adj_index = fib_entry_get_adj (fei);
348 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
350 u32 classify_table_index =
351 lm->classify_table_index_by_sw_if_index[sw_if_index];
352 if (classify_table_index != (u32) ~ 0)
354 dpo_id_t dpo = DPO_INVALID;
359 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
361 fib_table_entry_special_dpo_add (fib_index,
364 FIB_ENTRY_FLAG_NONE, &dpo);
369 fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL), FIB_PROTOCOL_IP6, &pfx.fp_addr, sw_if_index, ~0, // invalid FIB index
370 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
374 ip6_del_interface_routes (ip6_main_t * im,
376 ip6_address_t * address, u32 address_length)
379 .fp_len = address_length,
380 .fp_proto = FIB_PROTOCOL_IP6,
381 .fp_addr.ip6 = *address,
384 if (pfx.fp_len < 128)
386 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
391 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
395 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
397 ip6_main_t *im = &ip6_main;
399 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
402 * enable/disable only on the 1<->0 transition
406 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
411 /* The ref count is 0 when an address is removed from an interface that has
412 * no address - this is not a ciritical error */
413 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
414 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
418 vnet_feature_enable_disable ("ip6-unicast", "ip6-lookup", sw_if_index,
421 vnet_feature_enable_disable ("ip6-multicast", "ip6-mfib-forward-lookup",
422 sw_if_index, is_enable, 0, 0);
426 /* get first interface address */
428 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
430 ip_lookup_main_t *lm = &im->lookup_main;
431 ip_interface_address_t *ia = 0;
432 ip6_address_t *result = 0;
435 foreach_ip_interface_address (lm, ia, sw_if_index,
436 1 /* honor unnumbered */,
438 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
447 ip6_add_del_interface_address (vlib_main_t * vm,
449 ip6_address_t * address,
450 u32 address_length, u32 is_del)
452 vnet_main_t *vnm = vnet_get_main ();
453 ip6_main_t *im = &ip6_main;
454 ip_lookup_main_t *lm = &im->lookup_main;
456 u32 if_address_index;
457 ip6_address_fib_t ip6_af, *addr_fib = 0;
459 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
460 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
462 ip6_addr_fib_init (&ip6_af, address,
463 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
464 vec_add1 (addr_fib, ip6_af);
467 uword elts_before = pool_elts (lm->if_address_pool);
469 error = ip_interface_address_add_del
470 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
474 /* Pool did not grow: add duplicate address. */
475 if (elts_before == pool_elts (lm->if_address_pool))
479 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
482 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
484 ip6_add_interface_routes (vnm, sw_if_index,
485 im, ip6_af.fib_index,
486 pool_elt_at_index (lm->if_address_pool,
490 ip6_add_del_interface_address_callback_t *cb;
491 vec_foreach (cb, im->add_del_interface_address_callbacks)
492 cb->function (im, cb->function_opaque, sw_if_index,
493 address, address_length, if_address_index, is_del);
502 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
504 ip6_main_t *im = &ip6_main;
505 ip_interface_address_t *ia;
507 u32 is_admin_up, fib_index;
509 /* Fill in lookup tables with default table (0). */
510 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
512 vec_validate_init_empty (im->
513 lookup_main.if_address_pool_index_by_sw_if_index,
516 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
518 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
521 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
522 0 /* honor unnumbered */,
524 a = ip_interface_address_get_address (&im->lookup_main, ia);
526 ip6_add_interface_routes (vnm, sw_if_index,
530 ip6_del_interface_routes (im, fib_index,
531 a, ia->address_length);
538 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
540 /* Built-in ip6 unicast rx feature path definition */
542 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
544 .arc_name = "ip6-unicast",
545 .start_nodes = VNET_FEATURES ("ip6-input"),
546 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
549 VNET_FEATURE_INIT (ip6_flow_classify, static) =
551 .arc_name = "ip6-unicast",
552 .node_name = "ip6-flow-classify",
553 .runs_before = VNET_FEATURES ("ip6-inacl"),
556 VNET_FEATURE_INIT (ip6_inacl, static) =
558 .arc_name = "ip6-unicast",
559 .node_name = "ip6-inacl",
560 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
563 VNET_FEATURE_INIT (ip6_policer_classify, static) =
565 .arc_name = "ip6-unicast",
566 .node_name = "ip6-policer-classify",
567 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
570 VNET_FEATURE_INIT (ip6_ipsec, static) =
572 .arc_name = "ip6-unicast",
573 .node_name = "ipsec-input-ip6",
574 .runs_before = VNET_FEATURES ("l2tp-decap"),
577 VNET_FEATURE_INIT (ip6_l2tp, static) =
579 .arc_name = "ip6-unicast",
580 .node_name = "l2tp-decap",
581 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
584 VNET_FEATURE_INIT (ip6_vpath, static) =
586 .arc_name = "ip6-unicast",
587 .node_name = "vpath-input-ip6",
588 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
591 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
593 .arc_name = "ip6-unicast",
594 .node_name = "ip6-vxlan-bypass",
595 .runs_before = VNET_FEATURES ("ip6-lookup"),
598 VNET_FEATURE_INIT (ip6_lookup, static) =
600 .arc_name = "ip6-unicast",
601 .node_name = "ip6-lookup",
602 .runs_before = VNET_FEATURES ("ip6-drop"),
605 VNET_FEATURE_INIT (ip6_drop, static) =
607 .arc_name = "ip6-unicast",
608 .node_name = "ip6-drop",
609 .runs_before = 0, /*last feature*/
612 /* Built-in ip6 multicast rx feature path definition (none now) */
613 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
615 .arc_name = "ip6-multicast",
616 .start_nodes = VNET_FEATURES ("ip6-input"),
617 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
620 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
621 .arc_name = "ip6-multicast",
622 .node_name = "vpath-input-ip6",
623 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
626 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
627 .arc_name = "ip6-multicast",
628 .node_name = "ip6-mfib-forward-lookup",
629 .runs_before = VNET_FEATURES ("ip6-drop"),
632 VNET_FEATURE_INIT (ip6_drop_mc, static) = {
633 .arc_name = "ip6-multicast",
634 .node_name = "ip6-drop",
635 .runs_before = 0, /* last feature */
638 /* Built-in ip4 tx feature path definition */
639 VNET_FEATURE_ARC_INIT (ip6_output, static) =
641 .arc_name = "ip6-output",
642 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain"),
643 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
646 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
647 .arc_name = "ip6-output",
648 .node_name = "ipsec-output-ip6",
649 .runs_before = VNET_FEATURES ("interface-output"),
652 VNET_FEATURE_INIT (ip6_interface_output, static) = {
653 .arc_name = "ip6-output",
654 .node_name = "interface-output",
655 .runs_before = 0, /* not before any other features */
660 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
662 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
665 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
668 vnet_feature_enable_disable ("ip6-output", "interface-output", sw_if_index,
671 return /* no error */ 0;
674 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
677 ip6_lookup (vlib_main_t * vm,
678 vlib_node_runtime_t * node, vlib_frame_t * frame)
680 return ip6_lookup_inline (vm, node, frame);
683 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
686 VLIB_REGISTER_NODE (ip6_lookup_node) =
688 .function = ip6_lookup,
689 .name = "ip6-lookup",
690 .vector_size = sizeof (u32),
691 .format_trace = format_ip6_lookup_trace,
692 .n_next_nodes = IP6_LOOKUP_N_NEXT,
693 .next_nodes = IP6_LOOKUP_NEXT_NODES,
697 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
700 ip6_load_balance (vlib_main_t * vm,
701 vlib_node_runtime_t * node, vlib_frame_t * frame)
703 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
704 u32 n_left_from, n_left_to_next, *from, *to_next;
705 ip_lookup_next_t next;
706 u32 cpu_index = os_get_cpu_number ();
707 ip6_main_t *im = &ip6_main;
709 from = vlib_frame_vector_args (frame);
710 n_left_from = frame->n_vectors;
711 next = node->cached_next_index;
713 if (node->flags & VLIB_NODE_FLAG_TRACE)
714 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
716 while (n_left_from > 0)
718 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
721 while (n_left_from >= 4 && n_left_to_next >= 2)
723 ip_lookup_next_t next0, next1;
724 const load_balance_t *lb0, *lb1;
725 vlib_buffer_t *p0, *p1;
726 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
727 const ip6_header_t *ip0, *ip1;
728 const dpo_id_t *dpo0, *dpo1;
730 /* Prefetch next iteration. */
732 vlib_buffer_t *p2, *p3;
734 p2 = vlib_get_buffer (vm, from[2]);
735 p3 = vlib_get_buffer (vm, from[3]);
737 vlib_prefetch_buffer_header (p2, STORE);
738 vlib_prefetch_buffer_header (p3, STORE);
740 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
741 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
744 pi0 = to_next[0] = from[0];
745 pi1 = to_next[1] = from[1];
752 p0 = vlib_get_buffer (vm, pi0);
753 p1 = vlib_get_buffer (vm, pi1);
755 ip0 = vlib_buffer_get_current (p0);
756 ip1 = vlib_buffer_get_current (p1);
757 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
758 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
760 lb0 = load_balance_get (lbi0);
761 lb1 = load_balance_get (lbi1);
764 * this node is for via FIBs we can re-use the hash value from the
765 * to node if present.
766 * We don't want to use the same hash value at each level in the recursion
767 * graph as that would lead to polarisation
771 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
773 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
775 hc0 = vnet_buffer (p0)->ip.flow_hash =
776 vnet_buffer (p0)->ip.flow_hash >> 1;
780 hc0 = vnet_buffer (p0)->ip.flow_hash =
781 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
784 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
786 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
788 hc1 = vnet_buffer (p1)->ip.flow_hash =
789 vnet_buffer (p1)->ip.flow_hash >> 1;
793 hc1 = vnet_buffer (p1)->ip.flow_hash =
794 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
799 load_balance_get_bucket_i (lb0,
800 hc0 & (lb0->lb_n_buckets_minus_1));
802 load_balance_get_bucket_i (lb1,
803 hc1 & (lb1->lb_n_buckets_minus_1));
805 next0 = dpo0->dpoi_next_node;
806 next1 = dpo1->dpoi_next_node;
808 /* Only process the HBH Option Header if explicitly configured to do so */
810 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
812 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
813 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
815 /* Only process the HBH Option Header if explicitly configured to do so */
817 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
819 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
820 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
823 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
824 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
826 vlib_increment_combined_counter
827 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
828 vlib_increment_combined_counter
829 (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
831 vlib_validate_buffer_enqueue_x2 (vm, node, next,
832 to_next, n_left_to_next,
833 pi0, pi1, next0, next1);
836 while (n_left_from > 0 && n_left_to_next > 0)
838 ip_lookup_next_t next0;
839 const load_balance_t *lb0;
842 const ip6_header_t *ip0;
843 const dpo_id_t *dpo0;
852 p0 = vlib_get_buffer (vm, pi0);
854 ip0 = vlib_buffer_get_current (p0);
855 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
857 lb0 = load_balance_get (lbi0);
860 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
862 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
864 hc0 = vnet_buffer (p0)->ip.flow_hash =
865 vnet_buffer (p0)->ip.flow_hash >> 1;
869 hc0 = vnet_buffer (p0)->ip.flow_hash =
870 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
874 load_balance_get_bucket_i (lb0,
875 hc0 & (lb0->lb_n_buckets_minus_1));
877 next0 = dpo0->dpoi_next_node;
878 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
880 /* Only process the HBH Option Header if explicitly configured to do so */
882 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
884 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
885 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
888 vlib_increment_combined_counter
889 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
891 vlib_validate_buffer_enqueue_x1 (vm, node, next,
892 to_next, n_left_to_next,
896 vlib_put_next_frame (vm, node, next, n_left_to_next);
899 return frame->n_vectors;
903 VLIB_REGISTER_NODE (ip6_load_balance_node) =
905 .function = ip6_load_balance,
906 .name = "ip6-load-balance",
907 .vector_size = sizeof (u32),
908 .sibling_of = "ip6-lookup",
909 .format_trace = format_ip6_lookup_trace,
913 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
917 /* Adjacency taken. */
922 /* Packet data, possibly *after* rewrite. */
923 u8 packet_data[128 - 1 * sizeof (u32)];
925 ip6_forward_next_trace_t;
928 format_ip6_forward_next_trace (u8 * s, va_list * args)
930 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
931 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
932 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
933 uword indent = format_get_indent (s);
935 s = format (s, "%U%U",
936 format_white_space, indent,
937 format_ip6_header, t->packet_data, sizeof (t->packet_data));
942 format_ip6_lookup_trace (u8 * s, va_list * args)
944 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
945 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
946 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
947 uword indent = format_get_indent (s);
949 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
950 t->fib_index, t->adj_index, t->flow_hash);
951 s = format (s, "\n%U%U",
952 format_white_space, indent,
953 format_ip6_header, t->packet_data, sizeof (t->packet_data));
959 format_ip6_rewrite_trace (u8 * s, va_list * args)
961 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
962 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
963 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
964 vnet_main_t *vnm = vnet_get_main ();
965 uword indent = format_get_indent (s);
967 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
968 t->fib_index, t->adj_index, format_ip_adjacency,
969 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
970 s = format (s, "\n%U%U",
971 format_white_space, indent,
972 format_ip_adjacency_packet_data,
973 vnm, t->adj_index, t->packet_data, sizeof (t->packet_data));
977 /* Common trace function for all ip6-forward next nodes. */
979 ip6_forward_next_trace (vlib_main_t * vm,
980 vlib_node_runtime_t * node,
981 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
984 ip6_main_t *im = &ip6_main;
986 n_left = frame->n_vectors;
987 from = vlib_frame_vector_args (frame);
992 vlib_buffer_t *b0, *b1;
993 ip6_forward_next_trace_t *t0, *t1;
995 /* Prefetch next iteration. */
996 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
997 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1002 b0 = vlib_get_buffer (vm, bi0);
1003 b1 = vlib_get_buffer (vm, bi1);
1005 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1007 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1008 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1009 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1011 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1012 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1013 vec_elt (im->fib_index_by_sw_if_index,
1014 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1016 clib_memcpy (t0->packet_data,
1017 vlib_buffer_get_current (b0),
1018 sizeof (t0->packet_data));
1020 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1022 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1023 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1024 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1026 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1027 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1028 vec_elt (im->fib_index_by_sw_if_index,
1029 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
1031 clib_memcpy (t1->packet_data,
1032 vlib_buffer_get_current (b1),
1033 sizeof (t1->packet_data));
1043 ip6_forward_next_trace_t *t0;
1047 b0 = vlib_get_buffer (vm, bi0);
1049 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1051 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1052 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1053 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1055 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1056 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1057 vec_elt (im->fib_index_by_sw_if_index,
1058 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1060 clib_memcpy (t0->packet_data,
1061 vlib_buffer_get_current (b0),
1062 sizeof (t0->packet_data));
1070 ip6_drop_or_punt (vlib_main_t * vm,
1071 vlib_node_runtime_t * node,
1072 vlib_frame_t * frame, ip6_error_t error_code)
1074 u32 *buffers = vlib_frame_vector_args (frame);
1075 uword n_packets = frame->n_vectors;
1077 vlib_error_drop_buffers (vm, node, buffers,
1081 ip6_input_node.index, error_code);
1083 if (node->flags & VLIB_NODE_FLAG_TRACE)
1084 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1090 ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1092 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP);
1096 ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1098 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT);
1102 VLIB_REGISTER_NODE (ip6_drop_node, static) =
1104 .function = ip6_drop,
1106 .vector_size = sizeof (u32),
1107 .format_trace = format_ip6_forward_next_trace,
1111 [0] = "error-drop",},
1115 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
1118 VLIB_REGISTER_NODE (ip6_punt_node, static) =
1120 .function = ip6_punt,
1122 .vector_size = sizeof (u32),
1123 .format_trace = format_ip6_forward_next_trace,
1127 [0] = "error-punt",},
1131 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
1133 /* Compute TCP/UDP/ICMP6 checksum in software. */
1135 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1136 ip6_header_t * ip0, int *bogus_lengthp)
1139 u16 sum16, payload_length_host_byte_order;
1140 u32 i, n_this_buffer, n_bytes_left;
1141 u32 headers_size = sizeof (ip0[0]);
1142 void *data_this_buffer;
1144 ASSERT (bogus_lengthp);
1147 /* Initialize checksum with ip header. */
1148 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1149 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1150 data_this_buffer = (void *) (ip0 + 1);
1152 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1154 sum0 = ip_csum_with_carry (sum0,
1155 clib_mem_unaligned (&ip0->
1156 src_address.as_uword[i],
1159 ip_csum_with_carry (sum0,
1160 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1164 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1165 * or UDP-Ping packets */
1166 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1169 ip6_hop_by_hop_ext_t *ext_hdr =
1170 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1172 /* validate really icmp6 next */
1173 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1174 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
1176 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1177 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1179 payload_length_host_byte_order -= skip_bytes;
1180 headers_size += skip_bytes;
1183 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1184 if (p0 && n_this_buffer + headers_size > p0->current_length)
1186 p0->current_length >
1187 headers_size ? p0->current_length - headers_size : 0;
1190 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1191 n_bytes_left -= n_this_buffer;
1192 if (n_bytes_left == 0)
1195 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1200 p0 = vlib_get_buffer (vm, p0->next_buffer);
1201 data_this_buffer = vlib_buffer_get_current (p0);
1202 n_this_buffer = p0->current_length;
1205 sum16 = ~ip_csum_fold (sum0);
1211 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1213 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1218 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1219 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1220 || ip0->protocol == IP_PROTOCOL_ICMP6
1221 || ip0->protocol == IP_PROTOCOL_UDP
1222 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1224 udp0 = (void *) (ip0 + 1);
1225 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1227 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1228 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1232 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1234 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1235 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1241 * @brief returns number of links on which src is reachable.
1244 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1246 const load_balance_t *lb0;
1249 lbi = ip6_fib_table_fwding_lookup_with_if_index (im,
1251 (b)->sw_if_index[VLIB_RX],
1254 lb0 = load_balance_get (lbi);
1256 return (fib_urpf_check_size (lb0->lb_urpf));
1260 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1262 ip6_main_t *im = &ip6_main;
1263 ip_lookup_main_t *lm = &im->lookup_main;
1264 ip_local_next_t next_index;
1265 u32 *from, *to_next, n_left_from, n_left_to_next;
1266 vlib_node_runtime_t *error_node =
1267 vlib_node_get_runtime (vm, ip6_input_node.index);
1269 from = vlib_frame_vector_args (frame);
1270 n_left_from = frame->n_vectors;
1271 next_index = node->cached_next_index;
1273 if (node->flags & VLIB_NODE_FLAG_TRACE)
1274 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1276 while (n_left_from > 0)
1278 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1280 while (n_left_from >= 4 && n_left_to_next >= 2)
1282 vlib_buffer_t *p0, *p1;
1283 ip6_header_t *ip0, *ip1;
1284 udp_header_t *udp0, *udp1;
1285 u32 pi0, ip_len0, udp_len0, flags0, next0;
1286 u32 pi1, ip_len1, udp_len1, flags1, next1;
1287 i32 len_diff0, len_diff1;
1288 u8 error0, type0, good_l4_checksum0;
1289 u8 error1, type1, good_l4_checksum1;
1290 u32 udp_offset0, udp_offset1;
1292 pi0 = to_next[0] = from[0];
1293 pi1 = to_next[1] = from[1];
1297 n_left_to_next -= 2;
1299 p0 = vlib_get_buffer (vm, pi0);
1300 p1 = vlib_get_buffer (vm, pi1);
1302 ip0 = vlib_buffer_get_current (p0);
1303 ip1 = vlib_buffer_get_current (p1);
1305 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1306 vnet_buffer (p1)->ip.start_of_ip_header = p1->current_data;
1308 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1309 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1311 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1312 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1317 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1318 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1322 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1326 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1327 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1328 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1329 && udp0->checksum == 0;
1330 /* Verify UDP length. */
1331 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1332 udp_len0 = clib_net_to_host_u16 (udp0->length);
1333 len_diff0 = ip_len0 - udp_len0;
1335 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p1, ip1,
1339 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1340 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1341 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1342 && udp1->checksum == 0;
1343 /* Verify UDP length. */
1344 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1345 udp_len1 = clib_net_to_host_u16 (udp1->length);
1346 len_diff1 = ip_len1 - udp_len1;
1349 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1350 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1352 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1353 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1355 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1356 && !good_l4_checksum0
1357 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1359 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1361 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1363 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1364 && !good_l4_checksum1
1365 && !(flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1367 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1369 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1372 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1374 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1375 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1377 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1378 IP6_ERROR_UDP_CHECKSUM);
1379 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1380 IP6_ERROR_ICMP_CHECKSUM);
1382 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1384 (!good_l4_checksum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1386 /* Drop packets from unroutable hosts. */
1387 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1388 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1389 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1390 !ip6_address_is_link_local_unicast (&ip0->src_address))
1392 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1393 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1395 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1396 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1397 !ip6_address_is_link_local_unicast (&ip1->src_address))
1399 error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1400 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1404 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1406 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1408 p0->error = error_node->errors[error0];
1409 p1->error = error_node->errors[error1];
1411 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1412 to_next, n_left_to_next,
1413 pi0, pi1, next0, next1);
1416 while (n_left_from > 0 && n_left_to_next > 0)
1421 u32 pi0, ip_len0, udp_len0, flags0, next0;
1423 u8 error0, type0, good_l4_checksum0;
1426 pi0 = to_next[0] = from[0];
1430 n_left_to_next -= 1;
1432 p0 = vlib_get_buffer (vm, pi0);
1434 ip0 = vlib_buffer_get_current (p0);
1436 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1438 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1439 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1443 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1446 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1450 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1451 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1452 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1453 && udp0->checksum == 0;
1454 /* Verify UDP length. */
1455 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1456 udp_len0 = clib_net_to_host_u16 (udp0->length);
1457 len_diff0 = ip_len0 - udp_len0;
1460 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1461 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1463 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1464 && !good_l4_checksum0
1465 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1467 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1469 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1472 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1474 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1476 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1477 IP6_ERROR_UDP_CHECKSUM);
1478 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1479 IP6_ERROR_ICMP_CHECKSUM);
1481 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1483 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1484 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1485 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1486 !ip6_address_is_link_local_unicast (&ip0->src_address))
1488 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1489 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1493 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1495 p0->error = error_node->errors[error0];
1497 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1498 to_next, n_left_to_next,
1502 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1505 return frame->n_vectors;
1509 VLIB_REGISTER_NODE (ip6_local_node, static) =
1511 .function = ip6_local,
1512 .name = "ip6-local",
1513 .vector_size = sizeof (u32),
1514 .format_trace = format_ip6_forward_next_trace,
1515 .n_next_nodes = IP_LOCAL_N_NEXT,
1518 [IP_LOCAL_NEXT_DROP] = "error-drop",
1519 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1520 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1521 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1526 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1529 ip6_register_protocol (u32 protocol, u32 node_index)
1531 vlib_main_t *vm = vlib_get_main ();
1532 ip6_main_t *im = &ip6_main;
1533 ip_lookup_main_t *lm = &im->lookup_main;
1535 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1536 lm->local_next_by_ip_protocol[protocol] =
1537 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1542 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1543 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1544 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1545 } ip6_discover_neighbor_next_t;
1549 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1550 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1551 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1552 } ip6_discover_neighbor_error_t;
1555 ip6_discover_neighbor_inline (vlib_main_t * vm,
1556 vlib_node_runtime_t * node,
1557 vlib_frame_t * frame, int is_glean)
1559 vnet_main_t *vnm = vnet_get_main ();
1560 ip6_main_t *im = &ip6_main;
1561 ip_lookup_main_t *lm = &im->lookup_main;
1562 u32 *from, *to_next_drop;
1563 uword n_left_from, n_left_to_next_drop;
1564 static f64 time_last_seed_change = -1e100;
1565 static u32 hash_seeds[3];
1566 static uword hash_bitmap[256 / BITS (uword)];
1570 if (node->flags & VLIB_NODE_FLAG_TRACE)
1571 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1573 time_now = vlib_time_now (vm);
1574 if (time_now - time_last_seed_change > 1e-3)
1577 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1578 sizeof (hash_seeds));
1579 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1580 hash_seeds[i] = r[i];
1582 /* Mark all hash keys as been not-seen before. */
1583 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1586 time_last_seed_change = time_now;
1589 from = vlib_frame_vector_args (frame);
1590 n_left_from = frame->n_vectors;
1592 while (n_left_from > 0)
1594 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1595 to_next_drop, n_left_to_next_drop);
1597 while (n_left_from > 0 && n_left_to_next_drop > 0)
1601 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1603 ip_adjacency_t *adj0;
1604 vnet_hw_interface_t *hw_if0;
1609 p0 = vlib_get_buffer (vm, pi0);
1611 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1613 ip0 = vlib_buffer_get_current (p0);
1615 adj0 = ip_get_adjacency (lm, adj_index0);
1619 ip0->dst_address.as_u64[0] =
1620 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1621 ip0->dst_address.as_u64[1] =
1622 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1629 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1630 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1633 b0 ^= ip0->dst_address.as_u32[0];
1634 c0 ^= ip0->dst_address.as_u32[1];
1636 hash_v3_mix32 (a0, b0, c0);
1638 b0 ^= ip0->dst_address.as_u32[2];
1639 c0 ^= ip0->dst_address.as_u32[3];
1641 hash_v3_finalize32 (a0, b0, c0);
1643 c0 &= BITS (hash_bitmap) - 1;
1644 c0 = c0 / BITS (uword);
1645 m0 = (uword) 1 << (c0 % BITS (uword));
1647 bm0 = hash_bitmap[c0];
1648 drop0 = (bm0 & m0) != 0;
1650 /* Mark it as seen. */
1651 hash_bitmap[c0] = bm0 | m0;
1655 to_next_drop[0] = pi0;
1657 n_left_to_next_drop -= 1;
1659 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1661 /* If the interface is link-down, drop the pkt */
1662 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1666 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1667 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1672 * the adj has been updated to a rewrite but the node the DPO that got
1673 * us here hasn't - yet. no big deal. we'll drop while we wait.
1675 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1680 icmp6_neighbor_solicitation_header_t *h0;
1683 h0 = vlib_packet_template_get_packet
1684 (vm, &im->discover_neighbor_packet_template, &bi0);
1687 * Build ethernet header.
1688 * Choose source address based on destination lookup
1691 if (ip6_src_address_for_packet (lm,
1693 &h0->ip.src_address))
1695 /* There is no address on the interface */
1697 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1698 vlib_buffer_free (vm, &bi0, 1);
1703 * Destination address is a solicited node multicast address.
1704 * We need to fill in
1705 * the low 24 bits with low 24 bits of target's address.
1707 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1708 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1709 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1711 h0->neighbor.target_address = ip0->dst_address;
1713 clib_memcpy (h0->link_layer_option.ethernet_address,
1714 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1716 /* $$$$ appears we need this; why is the checksum non-zero? */
1717 h0->neighbor.icmp.checksum = 0;
1718 h0->neighbor.icmp.checksum =
1719 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1722 ASSERT (bogus_length == 0);
1724 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1725 b0 = vlib_get_buffer (vm, bi0);
1726 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1727 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1729 /* Add rewrite/encap string. */
1730 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1731 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1733 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1735 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1739 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1740 n_left_to_next_drop);
1743 return frame->n_vectors;
1747 ip6_discover_neighbor (vlib_main_t * vm,
1748 vlib_node_runtime_t * node, vlib_frame_t * frame)
1750 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1754 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1756 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1759 static char *ip6_discover_neighbor_error_strings[] = {
1760 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1761 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1762 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1763 = "no source address for ND solicitation",
1767 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1769 .function = ip6_discover_neighbor,
1770 .name = "ip6-discover-neighbor",
1771 .vector_size = sizeof (u32),
1772 .format_trace = format_ip6_forward_next_trace,
1773 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1774 .error_strings = ip6_discover_neighbor_error_strings,
1775 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1778 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1779 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1785 VLIB_REGISTER_NODE (ip6_glean_node) =
1787 .function = ip6_glean,
1788 .name = "ip6-glean",
1789 .vector_size = sizeof (u32),
1790 .format_trace = format_ip6_forward_next_trace,
1791 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1792 .error_strings = ip6_discover_neighbor_error_strings,
1793 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1796 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1797 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1803 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1805 vnet_main_t *vnm = vnet_get_main ();
1806 ip6_main_t *im = &ip6_main;
1807 icmp6_neighbor_solicitation_header_t *h;
1809 ip_interface_address_t *ia;
1810 ip_adjacency_t *adj;
1811 vnet_hw_interface_t *hi;
1812 vnet_sw_interface_t *si;
1817 si = vnet_get_sw_interface (vnm, sw_if_index);
1819 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1821 return clib_error_return (0, "%U: interface %U down",
1822 format_ip6_address, dst,
1823 format_vnet_sw_if_index_name, vnm,
1828 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1831 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1832 return clib_error_return
1833 (0, "no matching interface address for destination %U (interface %U)",
1834 format_ip6_address, dst,
1835 format_vnet_sw_if_index_name, vnm, sw_if_index);
1839 vlib_packet_template_get_packet (vm,
1840 &im->discover_neighbor_packet_template,
1843 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1845 /* Destination address is a solicited node multicast address. We need to fill in
1846 the low 24 bits with low 24 bits of target's address. */
1847 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1848 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1849 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1851 h->ip.src_address = src[0];
1852 h->neighbor.target_address = dst[0];
1854 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1855 vec_len (hi->hw_address));
1857 h->neighbor.icmp.checksum =
1858 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1859 ASSERT (bogus_length == 0);
1861 b = vlib_get_buffer (vm, bi);
1862 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1863 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1865 /* Add encapsulation string for software interface (e.g. ethernet header). */
1866 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
1867 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1868 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1871 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1872 u32 *to_next = vlib_frame_vector_args (f);
1875 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1878 return /* no error */ 0;
1883 IP6_REWRITE_NEXT_DROP,
1884 IP6_REWRITE_NEXT_ICMP_ERROR,
1885 } ip6_rewrite_next_t;
1888 ip6_rewrite_inline (vlib_main_t * vm,
1889 vlib_node_runtime_t * node,
1890 vlib_frame_t * frame,
1891 int do_counters, int is_midchain, int is_mcast)
1893 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1894 u32 *from = vlib_frame_vector_args (frame);
1895 u32 n_left_from, n_left_to_next, *to_next, next_index;
1896 vlib_node_runtime_t *error_node =
1897 vlib_node_get_runtime (vm, ip6_input_node.index);
1899 n_left_from = frame->n_vectors;
1900 next_index = node->cached_next_index;
1901 u32 cpu_index = os_get_cpu_number ();
1903 while (n_left_from > 0)
1905 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1907 while (n_left_from >= 4 && n_left_to_next >= 2)
1909 ip_adjacency_t *adj0, *adj1;
1910 vlib_buffer_t *p0, *p1;
1911 ip6_header_t *ip0, *ip1;
1912 u32 pi0, rw_len0, next0, error0, adj_index0;
1913 u32 pi1, rw_len1, next1, error1, adj_index1;
1914 u32 tx_sw_if_index0, tx_sw_if_index1;
1916 /* Prefetch next iteration. */
1918 vlib_buffer_t *p2, *p3;
1920 p2 = vlib_get_buffer (vm, from[2]);
1921 p3 = vlib_get_buffer (vm, from[3]);
1923 vlib_prefetch_buffer_header (p2, LOAD);
1924 vlib_prefetch_buffer_header (p3, LOAD);
1926 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1927 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1929 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1930 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1933 pi0 = to_next[0] = from[0];
1934 pi1 = to_next[1] = from[1];
1939 n_left_to_next -= 2;
1941 p0 = vlib_get_buffer (vm, pi0);
1942 p1 = vlib_get_buffer (vm, pi1);
1944 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1945 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1947 /* We should never rewrite a pkt using the MISS adjacency */
1948 ASSERT (adj_index0 && adj_index1);
1950 ip0 = vlib_buffer_get_current (p0);
1951 ip1 = vlib_buffer_get_current (p1);
1953 error0 = error1 = IP6_ERROR_NONE;
1954 next0 = next1 = IP6_REWRITE_NEXT_DROP;
1956 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
1958 i32 hop_limit0 = ip0->hop_limit;
1960 /* Input node should have reject packets with hop limit 0. */
1961 ASSERT (ip0->hop_limit > 0);
1965 ip0->hop_limit = hop_limit0;
1968 * If the hop count drops below 1 when forwarding, generate
1971 if (PREDICT_FALSE (hop_limit0 <= 0))
1973 error0 = IP6_ERROR_TIME_EXPIRED;
1974 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1975 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1976 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1977 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1983 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
1985 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
1987 i32 hop_limit1 = ip1->hop_limit;
1989 /* Input node should have reject packets with hop limit 0. */
1990 ASSERT (ip1->hop_limit > 0);
1994 ip1->hop_limit = hop_limit1;
1997 * If the hop count drops below 1 when forwarding, generate
2000 if (PREDICT_FALSE (hop_limit1 <= 0))
2002 error1 = IP6_ERROR_TIME_EXPIRED;
2003 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2004 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2005 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2006 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2012 p1->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2014 adj0 = ip_get_adjacency (lm, adj_index0);
2015 adj1 = ip_get_adjacency (lm, adj_index1);
2017 rw_len0 = adj0[0].rewrite_header.data_bytes;
2018 rw_len1 = adj1[0].rewrite_header.data_bytes;
2019 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2020 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
2024 vlib_increment_combined_counter
2025 (&adjacency_counters,
2026 cpu_index, adj_index0, 1,
2027 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2028 vlib_increment_combined_counter
2029 (&adjacency_counters,
2030 cpu_index, adj_index1, 1,
2031 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
2034 /* Check MTU of outgoing interface. */
2036 (vlib_buffer_length_in_chain (vm, p0) >
2038 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2041 (vlib_buffer_length_in_chain (vm, p1) >
2043 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2046 /* Don't adjust the buffer for hop count issue; icmp-error node
2047 * wants to see the IP headerr */
2048 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2050 p0->current_data -= rw_len0;
2051 p0->current_length += rw_len0;
2053 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2054 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2055 next0 = adj0[0].rewrite_header.next_index;
2057 vnet_feature_arc_start (lm->output_feature_arc_index,
2058 tx_sw_if_index0, &next0, p0);
2060 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2062 p1->current_data -= rw_len1;
2063 p1->current_length += rw_len1;
2065 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2066 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2067 next1 = adj1[0].rewrite_header.next_index;
2069 vnet_feature_arc_start (lm->output_feature_arc_index,
2070 tx_sw_if_index1, &next1, p1);
2073 /* Guess we are only writing on simple Ethernet header. */
2074 vnet_rewrite_two_headers (adj0[0], adj1[0],
2075 ip0, ip1, sizeof (ethernet_header_t));
2079 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2080 adj1->sub_type.midchain.fixup_func (vm, adj1, p1);
2085 * copy bytes from the IP address into the MAC rewrite
2087 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0, 0);
2088 vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1, 0);
2091 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2092 to_next, n_left_to_next,
2093 pi0, pi1, next0, next1);
2096 while (n_left_from > 0 && n_left_to_next > 0)
2098 ip_adjacency_t *adj0;
2102 u32 adj_index0, next0, error0;
2103 u32 tx_sw_if_index0;
2105 pi0 = to_next[0] = from[0];
2107 p0 = vlib_get_buffer (vm, pi0);
2109 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2111 /* We should never rewrite a pkt using the MISS adjacency */
2112 ASSERT (adj_index0);
2114 adj0 = ip_get_adjacency (lm, adj_index0);
2116 ip0 = vlib_buffer_get_current (p0);
2118 error0 = IP6_ERROR_NONE;
2119 next0 = IP6_REWRITE_NEXT_DROP;
2121 /* Check hop limit */
2122 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2124 i32 hop_limit0 = ip0->hop_limit;
2126 ASSERT (ip0->hop_limit > 0);
2130 ip0->hop_limit = hop_limit0;
2132 if (PREDICT_FALSE (hop_limit0 <= 0))
2135 * If the hop count drops below 1 when forwarding, generate
2138 error0 = IP6_ERROR_TIME_EXPIRED;
2139 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2140 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2141 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2142 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2148 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2151 /* Guess we are only writing on simple Ethernet header. */
2152 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2154 /* Update packet buffer attributes/set output interface. */
2155 rw_len0 = adj0[0].rewrite_header.data_bytes;
2156 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2160 vlib_increment_combined_counter
2161 (&adjacency_counters,
2162 cpu_index, adj_index0, 1,
2163 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2166 /* Check MTU of outgoing interface. */
2168 (vlib_buffer_length_in_chain (vm, p0) >
2170 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2173 /* Don't adjust the buffer for hop count issue; icmp-error node
2174 * wants to see the IP headerr */
2175 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2177 p0->current_data -= rw_len0;
2178 p0->current_length += rw_len0;
2180 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2182 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2183 next0 = adj0[0].rewrite_header.next_index;
2185 vnet_feature_arc_start (lm->output_feature_arc_index,
2186 tx_sw_if_index0, &next0, p0);
2191 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2195 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0, 0);
2198 p0->error = error_node->errors[error0];
2203 n_left_to_next -= 1;
2205 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2206 to_next, n_left_to_next,
2210 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2213 /* Need to do trace after rewrites to pick up new packet data. */
2214 if (node->flags & VLIB_NODE_FLAG_TRACE)
2215 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2217 return frame->n_vectors;
2221 ip6_rewrite (vlib_main_t * vm,
2222 vlib_node_runtime_t * node, vlib_frame_t * frame)
2224 if (adj_are_counters_enabled ())
2225 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2227 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2231 ip6_rewrite_mcast (vlib_main_t * vm,
2232 vlib_node_runtime_t * node, vlib_frame_t * frame)
2234 if (adj_are_counters_enabled ())
2235 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2237 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2241 ip6_midchain (vlib_main_t * vm,
2242 vlib_node_runtime_t * node, vlib_frame_t * frame)
2244 if (adj_are_counters_enabled ())
2245 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2247 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2251 VLIB_REGISTER_NODE (ip6_midchain_node) =
2253 .function = ip6_midchain,
2254 .name = "ip6-midchain",
2255 .vector_size = sizeof (u32),
2256 .format_trace = format_ip6_forward_next_trace,
2257 .sibling_of = "ip6-rewrite",
2261 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2264 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2266 .function = ip6_rewrite,
2267 .name = "ip6-rewrite",
2268 .vector_size = sizeof (u32),
2269 .format_trace = format_ip6_rewrite_trace,
2273 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2274 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2279 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2282 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2284 .function = ip6_rewrite_mcast,
2285 .name = "ip6-rewrite-mcast",
2286 .vector_size = sizeof (u32),
2287 .format_trace = format_ip6_rewrite_trace,
2288 .sibling_of = "ip6-rewrite",
2292 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2295 * Hop-by-Hop handling
2297 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2299 #define foreach_ip6_hop_by_hop_error \
2300 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2301 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2302 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2307 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2308 foreach_ip6_hop_by_hop_error
2310 IP6_HOP_BY_HOP_N_ERROR,
2311 } ip6_hop_by_hop_error_t;
2315 * Primary h-b-h handler trace support
2316 * We work pretty hard on the problem for obvious reasons
2322 u8 option_data[256];
2323 } ip6_hop_by_hop_trace_t;
2325 vlib_node_registration_t ip6_hop_by_hop_node;
2327 static char *ip6_hop_by_hop_error_strings[] = {
2328 #define _(sym,string) string,
2329 foreach_ip6_hop_by_hop_error
2334 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2336 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2337 int total_len = va_arg (*args, int);
2338 ip6_hop_by_hop_option_t *opt0, *limit0;
2339 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2342 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2343 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2345 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2346 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2348 while (opt0 < limit0)
2353 case 0: /* Pad, just stop */
2354 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2358 if (hm->trace[type0])
2360 s = (*hm->trace[type0]) (s, opt0);
2365 format (s, "\n unrecognized option %d length %d", type0,
2369 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2370 sizeof (ip6_hop_by_hop_option_t));
2378 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2380 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2381 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2382 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2383 ip6_hop_by_hop_header_t *hbh0;
2384 ip6_hop_by_hop_option_t *opt0, *limit0;
2385 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2389 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2391 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2392 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2394 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2395 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2397 while (opt0 < limit0)
2402 case 0: /* Pad, just stop */
2403 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2407 if (hm->trace[type0])
2409 s = (*hm->trace[type0]) (s, opt0);
2414 format (s, "\n unrecognized option %d length %d", type0,
2418 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2419 sizeof (ip6_hop_by_hop_option_t));
2427 ip6_scan_hbh_options (vlib_buffer_t * b0,
2429 ip6_hop_by_hop_header_t * hbh0,
2430 ip6_hop_by_hop_option_t * opt0,
2431 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2433 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2437 while (opt0 < limit0)
2443 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2448 if (hm->options[type0])
2450 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2452 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2458 /* Unrecognized mandatory option, check the two high order bits */
2459 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2461 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2463 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2464 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2465 *next0 = IP_LOOKUP_NEXT_DROP;
2467 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2468 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2469 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2470 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2471 ICMP6_parameter_problem_unrecognized_option,
2472 (u8 *) opt0 - (u8 *) ip0);
2474 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2475 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2476 if (!ip6_address_is_multicast (&ip0->dst_address))
2478 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2479 icmp6_error_set_vnet_buffer (b0,
2480 ICMP6_parameter_problem,
2481 ICMP6_parameter_problem_unrecognized_option,
2482 (u8 *) opt0 - (u8 *) ip0);
2486 *next0 = IP_LOOKUP_NEXT_DROP;
2494 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2495 sizeof (ip6_hop_by_hop_option_t));
2501 * Process the Hop-by-Hop Options header
2504 ip6_hop_by_hop (vlib_main_t * vm,
2505 vlib_node_runtime_t * node, vlib_frame_t * frame)
2507 vlib_node_runtime_t *error_node =
2508 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2509 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2510 u32 n_left_from, *from, *to_next;
2511 ip_lookup_next_t next_index;
2512 ip6_main_t *im = &ip6_main;
2513 ip_lookup_main_t *lm = &im->lookup_main;
2515 from = vlib_frame_vector_args (frame);
2516 n_left_from = frame->n_vectors;
2517 next_index = node->cached_next_index;
2519 while (n_left_from > 0)
2523 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2525 while (n_left_from >= 4 && n_left_to_next >= 2)
2528 vlib_buffer_t *b0, *b1;
2530 ip6_header_t *ip0, *ip1;
2531 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2532 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2533 u8 error0 = 0, error1 = 0;
2535 /* Prefetch next iteration. */
2537 vlib_buffer_t *p2, *p3;
2539 p2 = vlib_get_buffer (vm, from[2]);
2540 p3 = vlib_get_buffer (vm, from[3]);
2542 vlib_prefetch_buffer_header (p2, LOAD);
2543 vlib_prefetch_buffer_header (p3, LOAD);
2545 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2546 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2549 /* Speculatively enqueue b0, b1 to the current next frame */
2550 to_next[0] = bi0 = from[0];
2551 to_next[1] = bi1 = from[1];
2555 n_left_to_next -= 2;
2557 b0 = vlib_get_buffer (vm, bi0);
2558 b1 = vlib_get_buffer (vm, bi1);
2560 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2561 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2562 ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2563 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2564 ip_adjacency_t *adj1 = ip_get_adjacency (lm, adj_index1);
2566 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2567 next0 = adj0->lookup_next_index;
2568 next1 = adj1->lookup_next_index;
2570 ip0 = vlib_buffer_get_current (b0);
2571 ip1 = vlib_buffer_get_current (b1);
2572 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2573 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2574 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2575 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2577 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2578 ((hbh0->length + 1) << 3));
2580 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2581 ((hbh1->length + 1) << 3));
2584 * Basic validity checks
2586 if ((hbh0->length + 1) << 3 >
2587 clib_net_to_host_u16 (ip0->payload_length))
2589 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2590 next0 = IP_LOOKUP_NEXT_DROP;
2593 /* Scan the set of h-b-h options, process ones that we understand */
2594 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2596 if ((hbh1->length + 1) << 3 >
2597 clib_net_to_host_u16 (ip1->payload_length))
2599 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2600 next1 = IP_LOOKUP_NEXT_DROP;
2603 /* Scan the set of h-b-h options, process ones that we understand */
2604 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2607 /* Has the classifier flagged this buffer for special treatment? */
2610 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2611 next0 = hm->next_override;
2613 /* Has the classifier flagged this buffer for special treatment? */
2616 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2617 next1 = hm->next_override;
2619 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2621 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2623 ip6_hop_by_hop_trace_t *t =
2624 vlib_add_trace (vm, node, b0, sizeof (*t));
2625 u32 trace_len = (hbh0->length + 1) << 3;
2626 t->next_index = next0;
2627 /* Capture the h-b-h option verbatim */
2630 ARRAY_LEN (t->option_data) ? trace_len :
2631 ARRAY_LEN (t->option_data);
2632 t->trace_len = trace_len;
2633 clib_memcpy (t->option_data, hbh0, trace_len);
2635 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2637 ip6_hop_by_hop_trace_t *t =
2638 vlib_add_trace (vm, node, b1, sizeof (*t));
2639 u32 trace_len = (hbh1->length + 1) << 3;
2640 t->next_index = next1;
2641 /* Capture the h-b-h option verbatim */
2644 ARRAY_LEN (t->option_data) ? trace_len :
2645 ARRAY_LEN (t->option_data);
2646 t->trace_len = trace_len;
2647 clib_memcpy (t->option_data, hbh1, trace_len);
2652 b0->error = error_node->errors[error0];
2653 b1->error = error_node->errors[error1];
2655 /* verify speculative enqueue, maybe switch current next frame */
2656 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2657 n_left_to_next, bi0, bi1, next0,
2661 while (n_left_from > 0 && n_left_to_next > 0)
2667 ip6_hop_by_hop_header_t *hbh0;
2668 ip6_hop_by_hop_option_t *opt0, *limit0;
2671 /* Speculatively enqueue b0 to the current next frame */
2677 n_left_to_next -= 1;
2679 b0 = vlib_get_buffer (vm, bi0);
2681 * Default use the next_index from the adjacency.
2682 * A HBH option rarely redirects to a different node
2684 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2685 ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2686 next0 = adj0->lookup_next_index;
2688 ip0 = vlib_buffer_get_current (b0);
2689 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2690 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2692 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2693 ((hbh0->length + 1) << 3));
2696 * Basic validity checks
2698 if ((hbh0->length + 1) << 3 >
2699 clib_net_to_host_u16 (ip0->payload_length))
2701 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2702 next0 = IP_LOOKUP_NEXT_DROP;
2706 /* Scan the set of h-b-h options, process ones that we understand */
2707 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2710 /* Has the classifier flagged this buffer for special treatment? */
2713 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2714 next0 = hm->next_override;
2716 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2718 ip6_hop_by_hop_trace_t *t =
2719 vlib_add_trace (vm, node, b0, sizeof (*t));
2720 u32 trace_len = (hbh0->length + 1) << 3;
2721 t->next_index = next0;
2722 /* Capture the h-b-h option verbatim */
2725 ARRAY_LEN (t->option_data) ? trace_len :
2726 ARRAY_LEN (t->option_data);
2727 t->trace_len = trace_len;
2728 clib_memcpy (t->option_data, hbh0, trace_len);
2731 b0->error = error_node->errors[error0];
2733 /* verify speculative enqueue, maybe switch current next frame */
2734 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2735 n_left_to_next, bi0, next0);
2737 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2739 return frame->n_vectors;
2743 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2745 .function = ip6_hop_by_hop,
2746 .name = "ip6-hop-by-hop",
2747 .sibling_of = "ip6-lookup",
2748 .vector_size = sizeof (u32),
2749 .format_trace = format_ip6_hop_by_hop_trace,
2750 .type = VLIB_NODE_TYPE_INTERNAL,
2751 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2752 .error_strings = ip6_hop_by_hop_error_strings,
2757 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2759 static clib_error_t *
2760 ip6_hop_by_hop_init (vlib_main_t * vm)
2762 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2763 memset (hm->options, 0, sizeof (hm->options));
2764 memset (hm->trace, 0, sizeof (hm->trace));
2765 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2769 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2772 ip6_hbh_set_next_override (uword next)
2774 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2776 hm->next_override = next;
2780 ip6_hbh_register_option (u8 option,
2781 int options (vlib_buffer_t * b, ip6_header_t * ip,
2782 ip6_hop_by_hop_option_t * opt),
2783 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2785 ip6_main_t *im = &ip6_main;
2786 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2788 ASSERT (option < ARRAY_LEN (hm->options));
2790 /* Already registered */
2791 if (hm->options[option])
2794 hm->options[option] = options;
2795 hm->trace[option] = trace;
2797 /* Set global variable */
2798 im->hbh_enabled = 1;
2804 ip6_hbh_unregister_option (u8 option)
2806 ip6_main_t *im = &ip6_main;
2807 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2809 ASSERT (option < ARRAY_LEN (hm->options));
2811 /* Not registered */
2812 if (!hm->options[option])
2815 hm->options[option] = NULL;
2816 hm->trace[option] = NULL;
2818 /* Disable global knob if this was the last option configured */
2821 for (i = 0; i < 256; i++)
2823 if (hm->options[option])
2830 im->hbh_enabled = 0;
2835 /* Global IP6 main. */
2836 ip6_main_t ip6_main;
2838 static clib_error_t *
2839 ip6_lookup_init (vlib_main_t * vm)
2841 ip6_main_t *im = &ip6_main;
2842 clib_error_t *error;
2845 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2848 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2855 for (j = 0; j < i0; j++)
2856 im->fib_masks[i].as_u32[j] = ~0;
2859 im->fib_masks[i].as_u32[i0] =
2860 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2863 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2865 if (im->lookup_table_nbuckets == 0)
2866 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2868 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2870 if (im->lookup_table_size == 0)
2871 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2873 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2874 "ip6 FIB fwding table",
2875 im->lookup_table_nbuckets, im->lookup_table_size);
2876 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2877 "ip6 FIB non-fwding table",
2878 im->lookup_table_nbuckets, im->lookup_table_size);
2880 /* Create FIB with index 0 and table id of 0. */
2881 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2882 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2886 pn = pg_get_node (ip6_lookup_node.index);
2887 pn->unformat_edit = unformat_pg_ip6_header;
2890 /* Unless explicitly configured, don't process HBH options */
2891 im->hbh_enabled = 0;
2894 icmp6_neighbor_solicitation_header_t p;
2896 memset (&p, 0, sizeof (p));
2898 p.ip.ip_version_traffic_class_and_flow_label =
2899 clib_host_to_net_u32 (0x6 << 28);
2900 p.ip.payload_length =
2901 clib_host_to_net_u16 (sizeof (p) -
2903 (icmp6_neighbor_solicitation_header_t, neighbor));
2904 p.ip.protocol = IP_PROTOCOL_ICMP6;
2905 p.ip.hop_limit = 255;
2906 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2908 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2910 p.link_layer_option.header.type =
2911 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2912 p.link_layer_option.header.n_data_u64s =
2913 sizeof (p.link_layer_option) / sizeof (u64);
2915 vlib_packet_template_init (vm,
2916 &im->discover_neighbor_packet_template,
2918 /* alloc chunk size */ 8,
2919 "ip6 neighbor discovery");
2925 VLIB_INIT_FUNCTION (ip6_lookup_init);
2927 static clib_error_t *
2928 add_del_ip6_interface_table (vlib_main_t * vm,
2929 unformat_input_t * input,
2930 vlib_cli_command_t * cmd)
2932 vnet_main_t *vnm = vnet_get_main ();
2933 ip_interface_address_t *ia;
2934 clib_error_t *error = 0;
2935 u32 sw_if_index, table_id;
2939 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2941 error = clib_error_return (0, "unknown interface `%U'",
2942 format_unformat_error, input);
2946 if (unformat (input, "%d", &table_id))
2950 error = clib_error_return (0, "expected table id `%U'",
2951 format_unformat_error, input);
2956 * If the interface already has in IP address, then a change int
2957 * VRF is not allowed. The IP address applied must first be removed.
2958 * We do not do that automatically here, since VPP has no knowledge
2959 * of whether thoses subnets are valid in the destination VRF.
2962 foreach_ip_interface_address (&ip6_main.lookup_main,
2964 1 /* honor unnumbered */,
2968 a = ip_interface_address_get_address (&ip6_main.lookup_main, ia);
2969 error = clib_error_return (0, "interface %U has address %U",
2970 format_vnet_sw_if_index_name, vnm,
2972 format_ip6_address, a);
2978 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2981 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
2982 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
2984 fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2987 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
2988 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = fib_index;
2997 * Place the indicated interface into the supplied IPv6 FIB table (also known
2998 * as a VRF). If the FIB table does not exist, this command creates it. To
2999 * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
3000 * FIB table will only be displayed if a route has been added to the table, or
3001 * an IP Address is assigned to an interface in the table (which adds a route
3004 * @note IP addresses added after setting the interface IP table are added to
3005 * the indicated FIB table. If an IP address is added prior to changing the
3006 * table then this is an error. The control plane must remove these addresses
3007 * first and then change the table. VPP will not automatically move the
3008 * addresses from the old to the new table as it does not know the validity
3012 * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
3013 * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
3016 VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
3018 .path = "set interface ip6 table",
3019 .function = add_del_ip6_interface_table,
3020 .short_help = "set interface ip6 table <interface> <table-id>"
3025 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
3028 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3029 /* Invert the "u" bit */
3030 ip->as_u8[8] = mac[0] ^ (1 << 1);
3031 ip->as_u8[9] = mac[1];
3032 ip->as_u8[10] = mac[2];
3033 ip->as_u8[11] = 0xFF;
3034 ip->as_u8[12] = 0xFE;
3035 ip->as_u8[13] = mac[3];
3036 ip->as_u8[14] = mac[4];
3037 ip->as_u8[15] = mac[5];
3041 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3044 /* Invert the previously inverted "u" bit */
3045 mac[0] = ip->as_u8[8] ^ (1 << 1);
3046 mac[1] = ip->as_u8[9];
3047 mac[2] = ip->as_u8[10];
3048 mac[3] = ip->as_u8[13];
3049 mac[4] = ip->as_u8[14];
3050 mac[5] = ip->as_u8[15];
3053 static clib_error_t *
3054 test_ip6_link_command_fn (vlib_main_t * vm,
3055 unformat_input_t * input, vlib_cli_command_t * cmd)
3058 ip6_address_t _a, *a = &_a;
3060 if (unformat (input, "%U", unformat_ethernet_address, mac))
3062 ip6_link_local_address_from_ethernet_mac_address (a, mac);
3063 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
3064 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3065 vlib_cli_output (vm, "Original MAC address: %U",
3066 format_ethernet_address, mac);
3073 * This command converts the given MAC Address into an IPv6 link-local
3077 * Example of how to create an IPv6 link-local address:
3078 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3079 * Link local address: fe80::14d9:e0ff:fe91:7986
3080 * Original MAC address: 16:d9:e0:91:79:86
3084 VLIB_CLI_COMMAND (test_link_command, static) =
3086 .path = "test ip6 link",
3087 .function = test_ip6_link_command_fn,
3088 .short_help = "test ip6 link <mac-address>",
3093 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3095 ip6_main_t *im6 = &ip6_main;
3097 uword *p = hash_get (im6->fib_index_by_table_id, table_id);
3102 fib = ip6_fib_get (p[0]);
3104 fib->flow_hash_config = flow_hash_config;
3108 static clib_error_t *
3109 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3110 unformat_input_t * input,
3111 vlib_cli_command_t * cmd)
3115 u32 flow_hash_config = 0;
3118 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3120 if (unformat (input, "table %d", &table_id))
3123 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3124 foreach_flow_hash_bit
3131 return clib_error_return (0, "unknown input `%U'",
3132 format_unformat_error, input);
3134 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3141 return clib_error_return (0, "no such FIB table %d", table_id);
3144 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3152 * Configure the set of IPv6 fields used by the flow hash.
3156 * Example of how to set the flow hash on a given table:
3157 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3159 * Example of display the configured flow hash:
3160 * @cliexstart{show ip6 fib}
3161 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3164 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3165 * [0] [@0]: dpo-drop ip6
3168 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3169 * [0] [@2]: dpo-receive
3172 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3173 * [0] [@2]: dpo-receive
3176 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3177 * [0] [@2]: dpo-receive
3180 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3181 * [0] [@2]: dpo-receive
3182 * ff02::1:ff00:0/104
3184 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3185 * [0] [@2]: dpo-receive
3186 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3189 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3190 * [0] [@0]: dpo-drop ip6
3193 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3194 * [0] [@4]: ipv6-glean: af_packet0
3197 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3198 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3201 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3202 * [0] [@2]: dpo-receive
3203 * fe80::fe:3eff:fe3e:9222/128
3205 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3206 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3209 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3210 * [0] [@2]: dpo-receive
3213 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3214 * [0] [@2]: dpo-receive
3217 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3218 * [0] [@2]: dpo-receive
3219 * ff02::1:ff00:0/104
3221 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3222 * [0] [@2]: dpo-receive
3227 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3229 .path = "set ip6 flow-hash",
3231 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3232 .function = set_ip6_flow_hash_command_fn,
3236 static clib_error_t *
3237 show_ip6_local_command_fn (vlib_main_t * vm,
3238 unformat_input_t * input, vlib_cli_command_t * cmd)
3240 ip6_main_t *im = &ip6_main;
3241 ip_lookup_main_t *lm = &im->lookup_main;
3244 vlib_cli_output (vm, "Protocols handled by ip6_local");
3245 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3247 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3248 vlib_cli_output (vm, "%d", i);
3256 * Display the set of protocols handled by the local IPv6 stack.
3259 * Example of how to display local protocol table:
3260 * @cliexstart{show ip6 local}
3261 * Protocols handled by ip6_local
3269 VLIB_CLI_COMMAND (show_ip6_local, static) =
3271 .path = "show ip6 local",
3272 .function = show_ip6_local_command_fn,
3273 .short_help = "show ip6 local",
3278 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3281 vnet_main_t *vnm = vnet_get_main ();
3282 vnet_interface_main_t *im = &vnm->interface_main;
3283 ip6_main_t *ipm = &ip6_main;
3284 ip_lookup_main_t *lm = &ipm->lookup_main;
3285 vnet_classify_main_t *cm = &vnet_classify_main;
3286 ip6_address_t *if_addr;
3288 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3289 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3291 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3292 return VNET_API_ERROR_NO_SUCH_ENTRY;
3294 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3295 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3297 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3299 if (NULL != if_addr)
3301 fib_prefix_t pfx = {
3303 .fp_proto = FIB_PROTOCOL_IP6,
3304 .fp_addr.ip6 = *if_addr,
3308 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3312 if (table_index != (u32) ~ 0)
3314 dpo_id_t dpo = DPO_INVALID;
3319 classify_dpo_create (DPO_PROTO_IP6, table_index));
3321 fib_table_entry_special_dpo_add (fib_index,
3323 FIB_SOURCE_CLASSIFY,
3324 FIB_ENTRY_FLAG_NONE, &dpo);
3329 fib_table_entry_special_remove (fib_index,
3330 &pfx, FIB_SOURCE_CLASSIFY);
3337 static clib_error_t *
3338 set_ip6_classify_command_fn (vlib_main_t * vm,
3339 unformat_input_t * input,
3340 vlib_cli_command_t * cmd)
3342 u32 table_index = ~0;
3343 int table_index_set = 0;
3344 u32 sw_if_index = ~0;
3347 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3349 if (unformat (input, "table-index %d", &table_index))
3350 table_index_set = 1;
3351 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3352 vnet_get_main (), &sw_if_index))
3358 if (table_index_set == 0)
3359 return clib_error_return (0, "classify table-index must be specified");
3361 if (sw_if_index == ~0)
3362 return clib_error_return (0, "interface / subif must be specified");
3364 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3371 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3372 return clib_error_return (0, "No such interface");
3374 case VNET_API_ERROR_NO_SUCH_ENTRY:
3375 return clib_error_return (0, "No such classifier table");
3381 * Assign a classification table to an interface. The classification
3382 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3383 * commands. Once the table is create, use this command to filter packets
3387 * Example of how to assign a classification table to an interface:
3388 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3391 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3393 .path = "set ip6 classify",
3395 "set ip6 classify intfc <interface> table-index <classify-idx>",
3396 .function = set_ip6_classify_command_fn,
3400 static clib_error_t *
3401 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3403 ip6_main_t *im = &ip6_main;
3408 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3410 if (unformat (input, "hash-buckets %d", &tmp))
3412 else if (unformat (input, "heap-size %dm", &tmp))
3413 heapsize = ((u64) tmp) << 20;
3414 else if (unformat (input, "heap-size %dM", &tmp))
3415 heapsize = ((u64) tmp) << 20;
3416 else if (unformat (input, "heap-size %dg", &tmp))
3417 heapsize = ((u64) tmp) << 30;
3418 else if (unformat (input, "heap-size %dG", &tmp))
3419 heapsize = ((u64) tmp) << 30;
3421 return clib_error_return (0, "unknown input '%U'",
3422 format_unformat_error, input);
3425 im->lookup_table_nbuckets = nbuckets;
3426 im->lookup_table_size = heapsize;
3431 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3434 * fd.io coding-style-patch-verification: ON
3437 * eval: (c-set-style "gnu")