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/ip/ip6_neighbor.h>
43 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
44 #include <vnet/srp/srp.h> /* for srp_hw_interface_class */
45 #include <vppinfra/cache.h>
46 #include <vnet/fib/fib_urpf_list.h> /* for FIB uRPF check */
47 #include <vnet/fib/ip6_fib.h>
48 #include <vnet/mfib/ip6_mfib.h>
49 #include <vnet/dpo/load_balance_map.h>
50 #include <vnet/dpo/classify_dpo.h>
52 #include <vppinfra/bihash_template.c>
54 /* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
55 #define OI_DECAP 0x80000000
59 * @brief IPv6 Forwarding.
61 * This file contains the source code for IPv6 forwarding.
65 ip6_forward_next_trace (vlib_main_t * vm,
66 vlib_node_runtime_t * node,
68 vlib_rx_or_tx_t which_adj_index);
71 ip6_lookup_inline (vlib_main_t * vm,
72 vlib_node_runtime_t * node, vlib_frame_t * frame)
74 ip6_main_t *im = &ip6_main;
75 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_to_counters;
76 u32 n_left_from, n_left_to_next, *from, *to_next;
77 ip_lookup_next_t next;
78 u32 thread_index = vlib_get_thread_index ();
80 from = vlib_frame_vector_args (frame);
81 n_left_from = frame->n_vectors;
82 next = node->cached_next_index;
84 while (n_left_from > 0)
86 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
88 while (n_left_from >= 4 && n_left_to_next >= 2)
90 vlib_buffer_t *p0, *p1;
91 u32 pi0, pi1, lbi0, lbi1, wrong_next;
92 ip_lookup_next_t next0, next1;
93 ip6_header_t *ip0, *ip1;
94 ip6_address_t *dst_addr0, *dst_addr1;
95 u32 fib_index0, fib_index1;
96 u32 flow_hash_config0, flow_hash_config1;
97 const dpo_id_t *dpo0, *dpo1;
98 const load_balance_t *lb0, *lb1;
100 /* Prefetch next iteration. */
102 vlib_buffer_t *p2, *p3;
104 p2 = vlib_get_buffer (vm, from[2]);
105 p3 = vlib_get_buffer (vm, from[3]);
107 vlib_prefetch_buffer_header (p2, LOAD);
108 vlib_prefetch_buffer_header (p3, LOAD);
109 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
110 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
113 pi0 = to_next[0] = from[0];
114 pi1 = to_next[1] = from[1];
116 p0 = vlib_get_buffer (vm, pi0);
117 p1 = vlib_get_buffer (vm, pi1);
119 ip0 = vlib_buffer_get_current (p0);
120 ip1 = vlib_buffer_get_current (p1);
122 dst_addr0 = &ip0->dst_address;
123 dst_addr1 = &ip1->dst_address;
126 vec_elt (im->fib_index_by_sw_if_index,
127 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
129 vec_elt (im->fib_index_by_sw_if_index,
130 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
132 fib_index0 = (vnet_buffer (p0)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
133 fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
134 fib_index1 = (vnet_buffer (p1)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
135 fib_index1 : vnet_buffer (p1)->sw_if_index[VLIB_TX];
137 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
138 lbi1 = ip6_fib_table_fwding_lookup (im, fib_index1, dst_addr1);
140 lb0 = load_balance_get (lbi0);
141 lb1 = load_balance_get (lbi1);
142 ASSERT (lb0->lb_n_buckets > 0);
143 ASSERT (lb1->lb_n_buckets > 0);
144 ASSERT (is_pow2 (lb0->lb_n_buckets));
145 ASSERT (is_pow2 (lb1->lb_n_buckets));
147 vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
149 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
151 flow_hash_config0 = lb0->lb_hash_config;
152 vnet_buffer (p0)->ip.flow_hash =
153 ip6_compute_flow_hash (ip0, flow_hash_config0);
155 load_balance_get_fwd_bucket (lb0,
156 (vnet_buffer (p0)->ip.flow_hash &
157 (lb0->lb_n_buckets_minus_1)));
161 dpo0 = load_balance_get_bucket_i (lb0, 0);
163 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
165 flow_hash_config1 = lb1->lb_hash_config;
166 vnet_buffer (p1)->ip.flow_hash =
167 ip6_compute_flow_hash (ip1, flow_hash_config1);
169 load_balance_get_fwd_bucket (lb1,
170 (vnet_buffer (p1)->ip.flow_hash &
171 (lb1->lb_n_buckets_minus_1)));
175 dpo1 = load_balance_get_bucket_i (lb1, 0);
177 next0 = dpo0->dpoi_next_node;
178 next1 = dpo1->dpoi_next_node;
180 /* Only process the HBH Option Header if explicitly configured to do so */
182 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
184 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
185 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
188 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
190 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
191 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
193 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
194 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
196 vlib_increment_combined_counter
197 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
198 vlib_increment_combined_counter
199 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
206 wrong_next = (next0 != next) + 2 * (next1 != next);
207 if (PREDICT_FALSE (wrong_next != 0))
216 vlib_set_next_frame_buffer (vm, node, next0, pi0);
223 vlib_set_next_frame_buffer (vm, node, next1, pi1);
230 vlib_set_next_frame_buffer (vm, node, next0, pi0);
231 vlib_set_next_frame_buffer (vm, node, next1, pi1);
235 vlib_put_next_frame (vm, node, next, n_left_to_next);
237 vlib_get_next_frame (vm, node, next, to_next,
244 while (n_left_from > 0 && n_left_to_next > 0)
249 ip_lookup_next_t next0;
251 ip6_address_t *dst_addr0;
252 u32 fib_index0, flow_hash_config0;
253 const dpo_id_t *dpo0;
258 p0 = vlib_get_buffer (vm, pi0);
260 ip0 = vlib_buffer_get_current (p0);
262 dst_addr0 = &ip0->dst_address;
265 vec_elt (im->fib_index_by_sw_if_index,
266 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
268 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
269 (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
271 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
273 lb0 = load_balance_get (lbi0);
274 flow_hash_config0 = lb0->lb_hash_config;
276 vnet_buffer (p0)->ip.flow_hash = 0;
277 ASSERT (lb0->lb_n_buckets > 0);
278 ASSERT (is_pow2 (lb0->lb_n_buckets));
280 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
282 flow_hash_config0 = lb0->lb_hash_config;
283 vnet_buffer (p0)->ip.flow_hash =
284 ip6_compute_flow_hash (ip0, flow_hash_config0);
286 load_balance_get_fwd_bucket (lb0,
287 (vnet_buffer (p0)->ip.flow_hash &
288 (lb0->lb_n_buckets_minus_1)));
292 dpo0 = load_balance_get_bucket_i (lb0, 0);
295 dpo0 = load_balance_get_bucket_i (lb0,
296 (vnet_buffer (p0)->ip.flow_hash &
297 lb0->lb_n_buckets_minus_1));
298 next0 = dpo0->dpoi_next_node;
300 /* Only process the HBH Option Header if explicitly configured to do so */
302 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
304 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
305 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
307 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
309 vlib_increment_combined_counter
310 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
317 if (PREDICT_FALSE (next0 != next))
320 vlib_put_next_frame (vm, node, next, n_left_to_next);
322 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
329 vlib_put_next_frame (vm, node, next, n_left_to_next);
332 if (node->flags & VLIB_NODE_FLAG_TRACE)
333 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
335 return frame->n_vectors;
339 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
340 ip6_main_t * im, u32 fib_index,
341 ip_interface_address_t * a)
343 ip_lookup_main_t *lm = &im->lookup_main;
344 ip6_address_t *address = ip_interface_address_get_address (lm, a);
346 .fp_len = a->address_length,
347 .fp_proto = FIB_PROTOCOL_IP6,
348 .fp_addr.ip6 = *address,
351 if (a->address_length < 128)
353 fib_table_entry_update_one_path (fib_index,
355 FIB_SOURCE_INTERFACE,
356 (FIB_ENTRY_FLAG_CONNECTED |
357 FIB_ENTRY_FLAG_ATTACHED),
359 /* No next-hop address */
361 /* invalid FIB index */
364 NULL, FIB_ROUTE_PATH_FLAG_NONE);
368 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
370 u32 classify_table_index =
371 lm->classify_table_index_by_sw_if_index[sw_if_index];
372 if (classify_table_index != (u32) ~ 0)
374 dpo_id_t dpo = DPO_INVALID;
379 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
381 fib_table_entry_special_dpo_add (fib_index,
384 FIB_ENTRY_FLAG_NONE, &dpo);
389 fib_table_entry_update_one_path (fib_index, &pfx,
390 FIB_SOURCE_INTERFACE,
391 (FIB_ENTRY_FLAG_CONNECTED |
392 FIB_ENTRY_FLAG_LOCAL),
396 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
400 ip6_del_interface_routes (ip6_main_t * im,
402 ip6_address_t * address, u32 address_length)
405 .fp_len = address_length,
406 .fp_proto = FIB_PROTOCOL_IP6,
407 .fp_addr.ip6 = *address,
410 if (pfx.fp_len < 128)
412 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
417 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
421 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
423 ip6_main_t *im = &ip6_main;
425 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
428 * enable/disable only on the 1<->0 transition
432 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
437 /* The ref count is 0 when an address is removed from an interface that has
438 * no address - this is not a ciritical error */
439 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
440 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
444 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
447 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
451 /* get first interface address */
453 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
455 ip_lookup_main_t *lm = &im->lookup_main;
456 ip_interface_address_t *ia = 0;
457 ip6_address_t *result = 0;
460 foreach_ip_interface_address (lm, ia, sw_if_index,
461 1 /* honor unnumbered */,
463 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
472 ip6_add_del_interface_address (vlib_main_t * vm,
474 ip6_address_t * address,
475 u32 address_length, u32 is_del)
477 vnet_main_t *vnm = vnet_get_main ();
478 ip6_main_t *im = &ip6_main;
479 ip_lookup_main_t *lm = &im->lookup_main;
481 u32 if_address_index;
482 ip6_address_fib_t ip6_af, *addr_fib = 0;
484 /* local0 interface doesn't support IP addressing */
485 if (sw_if_index == 0)
488 clib_error_create ("local0 interface doesn't support IP addressing");
491 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
492 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
494 ip6_addr_fib_init (&ip6_af, address,
495 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
496 vec_add1 (addr_fib, ip6_af);
499 uword elts_before = pool_elts (lm->if_address_pool);
501 error = ip_interface_address_add_del
502 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
506 /* Pool did not grow: add duplicate address. */
507 if (elts_before == pool_elts (lm->if_address_pool))
511 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
514 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
516 ip6_add_interface_routes (vnm, sw_if_index,
517 im, ip6_af.fib_index,
518 pool_elt_at_index (lm->if_address_pool,
522 ip6_add_del_interface_address_callback_t *cb;
523 vec_foreach (cb, im->add_del_interface_address_callbacks)
524 cb->function (im, cb->function_opaque, sw_if_index,
525 address, address_length, if_address_index, is_del);
534 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
536 ip6_main_t *im = &ip6_main;
537 ip_interface_address_t *ia;
539 u32 is_admin_up, fib_index;
541 /* Fill in lookup tables with default table (0). */
542 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
544 vec_validate_init_empty (im->
545 lookup_main.if_address_pool_index_by_sw_if_index,
548 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
550 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
553 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
554 0 /* honor unnumbered */,
556 a = ip_interface_address_get_address (&im->lookup_main, ia);
558 ip6_add_interface_routes (vnm, sw_if_index,
562 ip6_del_interface_routes (im, fib_index,
563 a, ia->address_length);
570 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
572 /* Built-in ip6 unicast rx feature path definition */
574 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
576 .arc_name = "ip6-unicast",
577 .start_nodes = VNET_FEATURES ("ip6-input"),
578 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
581 VNET_FEATURE_INIT (ip6_flow_classify, static) =
583 .arc_name = "ip6-unicast",
584 .node_name = "ip6-flow-classify",
585 .runs_before = VNET_FEATURES ("ip6-inacl"),
588 VNET_FEATURE_INIT (ip6_inacl, static) =
590 .arc_name = "ip6-unicast",
591 .node_name = "ip6-inacl",
592 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
595 VNET_FEATURE_INIT (ip6_policer_classify, static) =
597 .arc_name = "ip6-unicast",
598 .node_name = "ip6-policer-classify",
599 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
602 VNET_FEATURE_INIT (ip6_ipsec, static) =
604 .arc_name = "ip6-unicast",
605 .node_name = "ipsec-input-ip6",
606 .runs_before = VNET_FEATURES ("l2tp-decap"),
609 VNET_FEATURE_INIT (ip6_l2tp, static) =
611 .arc_name = "ip6-unicast",
612 .node_name = "l2tp-decap",
613 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
616 VNET_FEATURE_INIT (ip6_vpath, static) =
618 .arc_name = "ip6-unicast",
619 .node_name = "vpath-input-ip6",
620 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
623 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
625 .arc_name = "ip6-unicast",
626 .node_name = "ip6-vxlan-bypass",
627 .runs_before = VNET_FEATURES ("ip6-lookup"),
630 VNET_FEATURE_INIT (ip6_drop, static) =
632 .arc_name = "ip6-unicast",
633 .node_name = "ip6-drop",
634 .runs_before = VNET_FEATURES ("ip6-lookup"),
637 VNET_FEATURE_INIT (ip6_lookup, static) =
639 .arc_name = "ip6-unicast",
640 .node_name = "ip6-lookup",
641 .runs_before = 0, /*last feature*/
644 /* Built-in ip6 multicast rx feature path definition (none now) */
645 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
647 .arc_name = "ip6-multicast",
648 .start_nodes = VNET_FEATURES ("ip6-input"),
649 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
652 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
653 .arc_name = "ip6-multicast",
654 .node_name = "vpath-input-ip6",
655 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
658 VNET_FEATURE_INIT (ip6_drop_mc, static) = {
659 .arc_name = "ip6-multicast",
660 .node_name = "ip6-drop",
661 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
664 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
665 .arc_name = "ip6-multicast",
666 .node_name = "ip6-mfib-forward-lookup",
667 .runs_before = 0, /* last feature */
670 /* Built-in ip4 tx feature path definition */
671 VNET_FEATURE_ARC_INIT (ip6_output, static) =
673 .arc_name = "ip6-output",
674 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain"),
675 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
678 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
679 .arc_name = "ip6-output",
680 .node_name = "ipsec-output-ip6",
681 .runs_before = VNET_FEATURES ("interface-output"),
684 VNET_FEATURE_INIT (ip6_interface_output, static) = {
685 .arc_name = "ip6-output",
686 .node_name = "interface-output",
687 .runs_before = 0, /* not before any other features */
692 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
694 ip6_main_t *im = &ip6_main;
696 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
697 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
701 /* Ensure that IPv6 is disabled */
702 ip6_main_t *im6 = &ip6_main;
703 ip_lookup_main_t *lm6 = &im6->lookup_main;
704 ip_interface_address_t *ia = 0;
705 ip6_address_t *address;
706 vlib_main_t *vm = vlib_get_main ();
708 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0 /* is_add */ );
710 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* honor unnumbered */,
712 address = ip_interface_address_get_address (lm6, ia);
713 ip6_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1);
716 ip6_mfib_interface_enable_disable (sw_if_index, 0);
719 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
722 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
725 return /* no error */ 0;
728 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
731 ip6_lookup (vlib_main_t * vm,
732 vlib_node_runtime_t * node, vlib_frame_t * frame)
734 return ip6_lookup_inline (vm, node, frame);
737 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
740 VLIB_REGISTER_NODE (ip6_lookup_node) =
742 .function = ip6_lookup,
743 .name = "ip6-lookup",
744 .vector_size = sizeof (u32),
745 .format_trace = format_ip6_lookup_trace,
746 .n_next_nodes = IP6_LOOKUP_N_NEXT,
747 .next_nodes = IP6_LOOKUP_NEXT_NODES,
751 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
754 ip6_load_balance (vlib_main_t * vm,
755 vlib_node_runtime_t * node, vlib_frame_t * frame)
757 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
758 u32 n_left_from, n_left_to_next, *from, *to_next;
759 ip_lookup_next_t next;
760 u32 thread_index = vlib_get_thread_index ();
761 ip6_main_t *im = &ip6_main;
763 from = vlib_frame_vector_args (frame);
764 n_left_from = frame->n_vectors;
765 next = node->cached_next_index;
767 if (node->flags & VLIB_NODE_FLAG_TRACE)
768 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
770 while (n_left_from > 0)
772 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
775 while (n_left_from >= 4 && n_left_to_next >= 2)
777 ip_lookup_next_t next0, next1;
778 const load_balance_t *lb0, *lb1;
779 vlib_buffer_t *p0, *p1;
780 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
781 const ip6_header_t *ip0, *ip1;
782 const dpo_id_t *dpo0, *dpo1;
784 /* Prefetch next iteration. */
786 vlib_buffer_t *p2, *p3;
788 p2 = vlib_get_buffer (vm, from[2]);
789 p3 = vlib_get_buffer (vm, from[3]);
791 vlib_prefetch_buffer_header (p2, STORE);
792 vlib_prefetch_buffer_header (p3, STORE);
794 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
795 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
798 pi0 = to_next[0] = from[0];
799 pi1 = to_next[1] = from[1];
806 p0 = vlib_get_buffer (vm, pi0);
807 p1 = vlib_get_buffer (vm, pi1);
809 ip0 = vlib_buffer_get_current (p0);
810 ip1 = vlib_buffer_get_current (p1);
811 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
812 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
814 lb0 = load_balance_get (lbi0);
815 lb1 = load_balance_get (lbi1);
818 * this node is for via FIBs we can re-use the hash value from the
819 * to node if present.
820 * We don't want to use the same hash value at each level in the recursion
821 * graph as that would lead to polarisation
825 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
827 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
829 hc0 = vnet_buffer (p0)->ip.flow_hash =
830 vnet_buffer (p0)->ip.flow_hash >> 1;
834 hc0 = vnet_buffer (p0)->ip.flow_hash =
835 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
838 load_balance_get_fwd_bucket (lb0,
840 lb0->lb_n_buckets_minus_1));
844 dpo0 = load_balance_get_bucket_i (lb0, 0);
846 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
848 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
850 hc1 = vnet_buffer (p1)->ip.flow_hash =
851 vnet_buffer (p1)->ip.flow_hash >> 1;
855 hc1 = vnet_buffer (p1)->ip.flow_hash =
856 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
859 load_balance_get_fwd_bucket (lb1,
861 lb1->lb_n_buckets_minus_1));
865 dpo1 = load_balance_get_bucket_i (lb1, 0);
868 next0 = dpo0->dpoi_next_node;
869 next1 = dpo1->dpoi_next_node;
871 /* Only process the HBH Option Header if explicitly configured to do so */
873 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
875 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
876 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
878 /* Only process the HBH Option Header if explicitly configured to do so */
880 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
882 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
883 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
886 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
887 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
889 vlib_increment_combined_counter
890 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
891 vlib_increment_combined_counter
892 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
894 vlib_validate_buffer_enqueue_x2 (vm, node, next,
895 to_next, n_left_to_next,
896 pi0, pi1, next0, next1);
899 while (n_left_from > 0 && n_left_to_next > 0)
901 ip_lookup_next_t next0;
902 const load_balance_t *lb0;
905 const ip6_header_t *ip0;
906 const dpo_id_t *dpo0;
915 p0 = vlib_get_buffer (vm, pi0);
917 ip0 = vlib_buffer_get_current (p0);
918 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
920 lb0 = load_balance_get (lbi0);
923 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
925 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
927 hc0 = vnet_buffer (p0)->ip.flow_hash =
928 vnet_buffer (p0)->ip.flow_hash >> 1;
932 hc0 = vnet_buffer (p0)->ip.flow_hash =
933 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
936 load_balance_get_fwd_bucket (lb0,
938 lb0->lb_n_buckets_minus_1));
942 dpo0 = load_balance_get_bucket_i (lb0, 0);
945 next0 = dpo0->dpoi_next_node;
946 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
948 /* Only process the HBH Option Header if explicitly configured to do so */
950 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
952 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
953 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
956 vlib_increment_combined_counter
957 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
959 vlib_validate_buffer_enqueue_x1 (vm, node, next,
960 to_next, n_left_to_next,
964 vlib_put_next_frame (vm, node, next, n_left_to_next);
967 return frame->n_vectors;
971 VLIB_REGISTER_NODE (ip6_load_balance_node) =
973 .function = ip6_load_balance,
974 .name = "ip6-load-balance",
975 .vector_size = sizeof (u32),
976 .sibling_of = "ip6-lookup",
977 .format_trace = format_ip6_lookup_trace,
981 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
985 /* Adjacency taken. */
990 /* Packet data, possibly *after* rewrite. */
991 u8 packet_data[128 - 1 * sizeof (u32)];
993 ip6_forward_next_trace_t;
996 format_ip6_forward_next_trace (u8 * s, va_list * args)
998 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
999 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1000 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
1001 uword indent = format_get_indent (s);
1003 s = format (s, "%U%U",
1004 format_white_space, indent,
1005 format_ip6_header, t->packet_data, sizeof (t->packet_data));
1010 format_ip6_lookup_trace (u8 * s, va_list * args)
1012 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1013 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1014 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
1015 uword indent = format_get_indent (s);
1017 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
1018 t->fib_index, t->adj_index, t->flow_hash);
1019 s = format (s, "\n%U%U",
1020 format_white_space, indent,
1021 format_ip6_header, t->packet_data, sizeof (t->packet_data));
1027 format_ip6_rewrite_trace (u8 * s, va_list * args)
1029 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1030 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1031 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
1032 uword indent = format_get_indent (s);
1034 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1035 t->fib_index, t->adj_index, format_ip_adjacency,
1036 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
1037 s = format (s, "\n%U%U",
1038 format_white_space, indent,
1039 format_ip_adjacency_packet_data,
1040 t->adj_index, t->packet_data, sizeof (t->packet_data));
1044 /* Common trace function for all ip6-forward next nodes. */
1046 ip6_forward_next_trace (vlib_main_t * vm,
1047 vlib_node_runtime_t * node,
1048 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
1051 ip6_main_t *im = &ip6_main;
1053 n_left = frame->n_vectors;
1054 from = vlib_frame_vector_args (frame);
1059 vlib_buffer_t *b0, *b1;
1060 ip6_forward_next_trace_t *t0, *t1;
1062 /* Prefetch next iteration. */
1063 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1064 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1069 b0 = vlib_get_buffer (vm, bi0);
1070 b1 = vlib_get_buffer (vm, bi1);
1072 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1074 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1075 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1076 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1078 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1079 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1080 vec_elt (im->fib_index_by_sw_if_index,
1081 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1083 clib_memcpy (t0->packet_data,
1084 vlib_buffer_get_current (b0),
1085 sizeof (t0->packet_data));
1087 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1089 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1090 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1091 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1093 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1094 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1095 vec_elt (im->fib_index_by_sw_if_index,
1096 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
1098 clib_memcpy (t1->packet_data,
1099 vlib_buffer_get_current (b1),
1100 sizeof (t1->packet_data));
1110 ip6_forward_next_trace_t *t0;
1114 b0 = vlib_get_buffer (vm, bi0);
1116 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1118 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1119 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1120 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1122 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1123 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1124 vec_elt (im->fib_index_by_sw_if_index,
1125 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1127 clib_memcpy (t0->packet_data,
1128 vlib_buffer_get_current (b0),
1129 sizeof (t0->packet_data));
1137 ip6_drop_or_punt (vlib_main_t * vm,
1138 vlib_node_runtime_t * node,
1139 vlib_frame_t * frame, ip6_error_t error_code)
1141 u32 *buffers = vlib_frame_vector_args (frame);
1142 uword n_packets = frame->n_vectors;
1144 vlib_error_drop_buffers (vm, node, buffers,
1148 ip6_input_node.index, error_code);
1150 if (node->flags & VLIB_NODE_FLAG_TRACE)
1151 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1157 ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1159 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP);
1163 ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1165 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT);
1169 VLIB_REGISTER_NODE (ip6_drop_node, static) =
1171 .function = ip6_drop,
1173 .vector_size = sizeof (u32),
1174 .format_trace = format_ip6_forward_next_trace,
1178 [0] = "error-drop",},
1182 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
1185 VLIB_REGISTER_NODE (ip6_punt_node, static) =
1187 .function = ip6_punt,
1189 .vector_size = sizeof (u32),
1190 .format_trace = format_ip6_forward_next_trace,
1194 [0] = "error-punt",},
1198 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
1200 /* Compute TCP/UDP/ICMP6 checksum in software. */
1202 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1203 ip6_header_t * ip0, int *bogus_lengthp)
1206 u16 sum16, payload_length_host_byte_order;
1207 u32 i, n_this_buffer, n_bytes_left;
1208 u32 headers_size = sizeof (ip0[0]);
1209 void *data_this_buffer;
1211 ASSERT (bogus_lengthp);
1214 /* Initialize checksum with ip header. */
1215 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1216 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1217 data_this_buffer = (void *) (ip0 + 1);
1219 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1221 sum0 = ip_csum_with_carry (sum0,
1222 clib_mem_unaligned (&ip0->
1223 src_address.as_uword[i],
1226 ip_csum_with_carry (sum0,
1227 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1231 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1232 * or UDP-Ping packets */
1233 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1236 ip6_hop_by_hop_ext_t *ext_hdr =
1237 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1239 /* validate really icmp6 next */
1240 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1241 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
1243 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1244 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1246 payload_length_host_byte_order -= skip_bytes;
1247 headers_size += skip_bytes;
1250 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1251 if (p0 && n_this_buffer + headers_size > p0->current_length)
1253 p0->current_length >
1254 headers_size ? p0->current_length - headers_size : 0;
1257 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1258 n_bytes_left -= n_this_buffer;
1259 if (n_bytes_left == 0)
1262 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1267 p0 = vlib_get_buffer (vm, p0->next_buffer);
1268 data_this_buffer = vlib_buffer_get_current (p0);
1269 n_this_buffer = p0->current_length;
1272 sum16 = ~ip_csum_fold (sum0);
1278 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1280 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1285 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1286 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1287 || ip0->protocol == IP_PROTOCOL_ICMP6
1288 || ip0->protocol == IP_PROTOCOL_UDP
1289 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1291 udp0 = (void *) (ip0 + 1);
1292 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1294 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1295 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
1299 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1301 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1302 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
1308 * @brief returns number of links on which src is reachable.
1311 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1313 const load_balance_t *lb0;
1316 lbi = ip6_fib_table_fwding_lookup_with_if_index (im,
1318 (b)->sw_if_index[VLIB_RX],
1321 lb0 = load_balance_get (lbi);
1323 return (fib_urpf_check_size (lb0->lb_urpf));
1327 ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1331 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1332 if (proto0 != IP_PROTOCOL_UDP)
1334 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1335 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1341 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1343 ip6_main_t *im = &ip6_main;
1344 ip_lookup_main_t *lm = &im->lookup_main;
1345 ip_local_next_t next_index;
1346 u32 *from, *to_next, n_left_from, n_left_to_next;
1347 vlib_node_runtime_t *error_node =
1348 vlib_node_get_runtime (vm, ip6_input_node.index);
1350 from = vlib_frame_vector_args (frame);
1351 n_left_from = frame->n_vectors;
1352 next_index = node->cached_next_index;
1354 if (node->flags & VLIB_NODE_FLAG_TRACE)
1355 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1357 while (n_left_from > 0)
1359 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1361 while (n_left_from >= 4 && n_left_to_next >= 2)
1363 vlib_buffer_t *p0, *p1;
1364 ip6_header_t *ip0, *ip1;
1365 udp_header_t *udp0, *udp1;
1366 u32 pi0, ip_len0, udp_len0, flags0, next0;
1367 u32 pi1, ip_len1, udp_len1, flags1, next1;
1368 i32 len_diff0, len_diff1;
1369 u8 error0, type0, good_l4_csum0, is_tcp_udp0;
1370 u8 error1, type1, good_l4_csum1, is_tcp_udp1;
1371 u32 udp_offset0, udp_offset1;
1373 pi0 = to_next[0] = from[0];
1374 pi1 = to_next[1] = from[1];
1378 n_left_to_next -= 2;
1380 p0 = vlib_get_buffer (vm, pi0);
1381 p1 = vlib_get_buffer (vm, pi1);
1383 ip0 = vlib_buffer_get_current (p0);
1384 ip1 = vlib_buffer_get_current (p1);
1386 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1387 vnet_buffer (p1)->l3_hdr_offset = p1->current_data;
1389 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1390 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1392 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1393 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1398 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1399 is_tcp_udp1 = ip6_next_proto_is_tcp_udp (p1, ip1, &udp_offset1);
1401 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1402 good_l4_csum1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1406 if (PREDICT_TRUE (is_tcp_udp0))
1408 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1409 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1410 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1411 && udp0->checksum == 0;
1412 /* Verify UDP length. */
1413 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1415 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1416 udp_len0 = clib_net_to_host_u16 (udp0->length);
1417 len_diff0 = ip_len0 - udp_len0;
1420 if (PREDICT_TRUE (is_tcp_udp1))
1422 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1423 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1424 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1425 && udp1->checksum == 0;
1426 /* Verify UDP length. */
1427 if (is_tcp_udp1 == IP_PROTOCOL_UDP)
1429 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1430 udp_len1 = clib_net_to_host_u16 (udp1->length);
1431 len_diff1 = ip_len1 - udp_len1;
1435 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1436 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1438 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1439 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1441 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1444 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1446 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1448 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1450 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1453 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1455 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1457 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1460 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1461 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1462 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1464 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1465 IP6_ERROR_UDP_CHECKSUM);
1466 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1467 IP6_ERROR_ICMP_CHECKSUM);
1468 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1469 error1 = (!good_l4_csum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1471 /* Drop packets from unroutable hosts. */
1472 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1473 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1474 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1475 !ip6_address_is_link_local_unicast (&ip0->src_address))
1477 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1478 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1480 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1481 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1482 !ip6_address_is_link_local_unicast (&ip1->src_address))
1484 error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1485 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1489 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1491 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1493 p0->error = error_node->errors[error0];
1494 p1->error = error_node->errors[error1];
1496 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1497 to_next, n_left_to_next,
1498 pi0, pi1, next0, next1);
1501 while (n_left_from > 0 && n_left_to_next > 0)
1506 u32 pi0, ip_len0, udp_len0, flags0, next0;
1508 u8 error0, type0, good_l4_csum0;
1512 pi0 = to_next[0] = from[0];
1516 n_left_to_next -= 1;
1518 p0 = vlib_get_buffer (vm, pi0);
1519 ip0 = vlib_buffer_get_current (p0);
1520 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1522 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1523 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1525 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1526 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1529 if (PREDICT_TRUE (is_tcp_udp0))
1531 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1532 /* Don't verify UDP checksum for packets with explicit zero
1534 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1535 && udp0->checksum == 0;
1536 /* Verify UDP length. */
1537 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1539 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1540 udp_len0 = clib_net_to_host_u16 (udp0->length);
1541 len_diff0 = ip_len0 - udp_len0;
1545 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1546 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1548 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1551 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1553 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1555 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1558 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1559 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1561 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1562 IP6_ERROR_UDP_CHECKSUM);
1563 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1564 IP6_ERROR_ICMP_CHECKSUM);
1565 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1567 /* If this is a neighbor solicitation (ICMP), skip src RPF check */
1568 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1569 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1570 !ip6_address_is_link_local_unicast (&ip0->src_address))
1572 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1573 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1577 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1578 p0->error = error_node->errors[error0];
1580 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1581 to_next, n_left_to_next,
1585 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1588 return frame->n_vectors;
1592 VLIB_REGISTER_NODE (ip6_local_node, static) =
1594 .function = ip6_local,
1595 .name = "ip6-local",
1596 .vector_size = sizeof (u32),
1597 .format_trace = format_ip6_forward_next_trace,
1598 .n_next_nodes = IP_LOCAL_N_NEXT,
1601 [IP_LOCAL_NEXT_DROP] = "error-drop",
1602 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1603 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1604 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1609 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1612 ip6_register_protocol (u32 protocol, u32 node_index)
1614 vlib_main_t *vm = vlib_get_main ();
1615 ip6_main_t *im = &ip6_main;
1616 ip_lookup_main_t *lm = &im->lookup_main;
1618 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1619 lm->local_next_by_ip_protocol[protocol] =
1620 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1625 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1626 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1627 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1628 } ip6_discover_neighbor_next_t;
1632 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1633 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1634 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1635 } ip6_discover_neighbor_error_t;
1638 ip6_discover_neighbor_inline (vlib_main_t * vm,
1639 vlib_node_runtime_t * node,
1640 vlib_frame_t * frame, int is_glean)
1642 vnet_main_t *vnm = vnet_get_main ();
1643 ip6_main_t *im = &ip6_main;
1644 ip_lookup_main_t *lm = &im->lookup_main;
1645 u32 *from, *to_next_drop;
1646 uword n_left_from, n_left_to_next_drop;
1647 static f64 time_last_seed_change = -1e100;
1648 static u32 hash_seeds[3];
1649 static uword hash_bitmap[256 / BITS (uword)];
1653 if (node->flags & VLIB_NODE_FLAG_TRACE)
1654 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1656 time_now = vlib_time_now (vm);
1657 if (time_now - time_last_seed_change > 1e-3)
1660 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1661 sizeof (hash_seeds));
1662 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1663 hash_seeds[i] = r[i];
1665 /* Mark all hash keys as been not-seen before. */
1666 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1669 time_last_seed_change = time_now;
1672 from = vlib_frame_vector_args (frame);
1673 n_left_from = frame->n_vectors;
1675 while (n_left_from > 0)
1677 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1678 to_next_drop, n_left_to_next_drop);
1680 while (n_left_from > 0 && n_left_to_next_drop > 0)
1684 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1686 ip_adjacency_t *adj0;
1687 vnet_hw_interface_t *hw_if0;
1692 p0 = vlib_get_buffer (vm, pi0);
1694 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1696 ip0 = vlib_buffer_get_current (p0);
1698 adj0 = adj_get (adj_index0);
1702 ip0->dst_address.as_u64[0] =
1703 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1704 ip0->dst_address.as_u64[1] =
1705 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1712 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1713 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1716 b0 ^= ip0->dst_address.as_u32[0];
1717 c0 ^= ip0->dst_address.as_u32[1];
1719 hash_v3_mix32 (a0, b0, c0);
1721 b0 ^= ip0->dst_address.as_u32[2];
1722 c0 ^= ip0->dst_address.as_u32[3];
1724 hash_v3_finalize32 (a0, b0, c0);
1726 c0 &= BITS (hash_bitmap) - 1;
1727 c0 = c0 / BITS (uword);
1728 m0 = (uword) 1 << (c0 % BITS (uword));
1730 bm0 = hash_bitmap[c0];
1731 drop0 = (bm0 & m0) != 0;
1733 /* Mark it as seen. */
1734 hash_bitmap[c0] = bm0 | m0;
1738 to_next_drop[0] = pi0;
1740 n_left_to_next_drop -= 1;
1742 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1744 /* If the interface is link-down, drop the pkt */
1745 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1749 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1750 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1755 * the adj has been updated to a rewrite but the node the DPO that got
1756 * us here hasn't - yet. no big deal. we'll drop while we wait.
1758 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1763 icmp6_neighbor_solicitation_header_t *h0;
1766 h0 = vlib_packet_template_get_packet
1767 (vm, &im->discover_neighbor_packet_template, &bi0);
1770 * Build ethernet header.
1771 * Choose source address based on destination lookup
1774 if (ip6_src_address_for_packet (lm,
1776 &h0->ip.src_address))
1778 /* There is no address on the interface */
1780 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1781 vlib_buffer_free (vm, &bi0, 1);
1786 * Destination address is a solicited node multicast address.
1787 * We need to fill in
1788 * the low 24 bits with low 24 bits of target's address.
1790 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1791 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1792 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1794 h0->neighbor.target_address = ip0->dst_address;
1796 clib_memcpy (h0->link_layer_option.ethernet_address,
1797 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1799 /* $$$$ appears we need this; why is the checksum non-zero? */
1800 h0->neighbor.icmp.checksum = 0;
1801 h0->neighbor.icmp.checksum =
1802 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1805 ASSERT (bogus_length == 0);
1807 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1808 b0 = vlib_get_buffer (vm, bi0);
1809 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1810 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1812 /* Add rewrite/encap string. */
1813 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1814 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1816 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1818 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1822 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1823 n_left_to_next_drop);
1826 return frame->n_vectors;
1830 ip6_discover_neighbor (vlib_main_t * vm,
1831 vlib_node_runtime_t * node, vlib_frame_t * frame)
1833 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1837 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1839 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1842 static char *ip6_discover_neighbor_error_strings[] = {
1843 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1844 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1845 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1846 = "no source address for ND solicitation",
1850 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1852 .function = ip6_discover_neighbor,
1853 .name = "ip6-discover-neighbor",
1854 .vector_size = sizeof (u32),
1855 .format_trace = format_ip6_forward_next_trace,
1856 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1857 .error_strings = ip6_discover_neighbor_error_strings,
1858 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1861 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1862 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1868 VLIB_REGISTER_NODE (ip6_glean_node) =
1870 .function = ip6_glean,
1871 .name = "ip6-glean",
1872 .vector_size = sizeof (u32),
1873 .format_trace = format_ip6_forward_next_trace,
1874 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1875 .error_strings = ip6_discover_neighbor_error_strings,
1876 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1879 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1880 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1886 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1888 vnet_main_t *vnm = vnet_get_main ();
1889 ip6_main_t *im = &ip6_main;
1890 icmp6_neighbor_solicitation_header_t *h;
1892 ip_interface_address_t *ia;
1893 ip_adjacency_t *adj;
1894 vnet_hw_interface_t *hi;
1895 vnet_sw_interface_t *si;
1901 si = vnet_get_sw_interface (vnm, sw_if_index);
1903 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1905 return clib_error_return (0, "%U: interface %U down",
1906 format_ip6_address, dst,
1907 format_vnet_sw_if_index_name, vnm,
1912 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1915 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1916 return clib_error_return
1917 (0, "no matching interface address for destination %U (interface %U)",
1918 format_ip6_address, dst,
1919 format_vnet_sw_if_index_name, vnm, sw_if_index);
1923 vlib_packet_template_get_packet (vm,
1924 &im->discover_neighbor_packet_template,
1927 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1929 /* Destination address is a solicited node multicast address. We need to fill in
1930 the low 24 bits with low 24 bits of target's address. */
1931 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1932 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1933 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1935 h->ip.src_address = src[0];
1936 h->neighbor.target_address = dst[0];
1938 if (PREDICT_FALSE (!hi->hw_address))
1940 return clib_error_return (0, "%U: interface %U do not support ip probe",
1941 format_ip6_address, dst,
1942 format_vnet_sw_if_index_name, vnm,
1946 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1947 vec_len (hi->hw_address));
1949 h->neighbor.icmp.checksum =
1950 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1951 ASSERT (bogus_length == 0);
1953 b = vlib_get_buffer (vm, bi);
1954 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1955 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1957 /* Add encapsulation string for software interface (e.g. ethernet header). */
1958 ip46_address_t nh = {
1962 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1963 VNET_LINK_IP6, &nh, sw_if_index);
1966 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1967 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1970 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1971 u32 *to_next = vlib_frame_vector_args (f);
1974 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1978 return /* no error */ 0;
1983 IP6_REWRITE_NEXT_DROP,
1984 IP6_REWRITE_NEXT_ICMP_ERROR,
1985 } ip6_rewrite_next_t;
1988 ip6_rewrite_inline (vlib_main_t * vm,
1989 vlib_node_runtime_t * node,
1990 vlib_frame_t * frame,
1991 int do_counters, int is_midchain, int is_mcast)
1993 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1994 u32 *from = vlib_frame_vector_args (frame);
1995 u32 n_left_from, n_left_to_next, *to_next, next_index;
1996 vlib_node_runtime_t *error_node =
1997 vlib_node_get_runtime (vm, ip6_input_node.index);
1999 n_left_from = frame->n_vectors;
2000 next_index = node->cached_next_index;
2001 u32 thread_index = vlib_get_thread_index ();
2003 while (n_left_from > 0)
2005 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2007 while (n_left_from >= 4 && n_left_to_next >= 2)
2009 ip_adjacency_t *adj0, *adj1;
2010 vlib_buffer_t *p0, *p1;
2011 ip6_header_t *ip0, *ip1;
2012 u32 pi0, rw_len0, next0, error0, adj_index0;
2013 u32 pi1, rw_len1, next1, error1, adj_index1;
2014 u32 tx_sw_if_index0, tx_sw_if_index1;
2016 /* Prefetch next iteration. */
2018 vlib_buffer_t *p2, *p3;
2020 p2 = vlib_get_buffer (vm, from[2]);
2021 p3 = vlib_get_buffer (vm, from[3]);
2023 vlib_prefetch_buffer_header (p2, LOAD);
2024 vlib_prefetch_buffer_header (p3, LOAD);
2026 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2027 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2029 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2030 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2033 pi0 = to_next[0] = from[0];
2034 pi1 = to_next[1] = from[1];
2039 n_left_to_next -= 2;
2041 p0 = vlib_get_buffer (vm, pi0);
2042 p1 = vlib_get_buffer (vm, pi1);
2044 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2045 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
2047 ip0 = vlib_buffer_get_current (p0);
2048 ip1 = vlib_buffer_get_current (p1);
2050 error0 = error1 = IP6_ERROR_NONE;
2051 next0 = next1 = IP6_REWRITE_NEXT_DROP;
2053 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2055 i32 hop_limit0 = ip0->hop_limit;
2057 /* Input node should have reject packets with hop limit 0. */
2058 ASSERT (ip0->hop_limit > 0);
2062 ip0->hop_limit = hop_limit0;
2065 * If the hop count drops below 1 when forwarding, generate
2068 if (PREDICT_FALSE (hop_limit0 <= 0))
2070 error0 = IP6_ERROR_TIME_EXPIRED;
2071 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2072 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2073 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2074 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2080 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2082 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2084 i32 hop_limit1 = ip1->hop_limit;
2086 /* Input node should have reject packets with hop limit 0. */
2087 ASSERT (ip1->hop_limit > 0);
2091 ip1->hop_limit = hop_limit1;
2094 * If the hop count drops below 1 when forwarding, generate
2097 if (PREDICT_FALSE (hop_limit1 <= 0))
2099 error1 = IP6_ERROR_TIME_EXPIRED;
2100 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2101 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2102 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2103 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2109 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2111 adj0 = adj_get (adj_index0);
2112 adj1 = adj_get (adj_index1);
2114 rw_len0 = adj0[0].rewrite_header.data_bytes;
2115 rw_len1 = adj1[0].rewrite_header.data_bytes;
2116 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2117 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
2121 vlib_increment_combined_counter
2122 (&adjacency_counters,
2123 thread_index, adj_index0, 1,
2124 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2125 vlib_increment_combined_counter
2126 (&adjacency_counters,
2127 thread_index, adj_index1, 1,
2128 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
2131 /* Check MTU of outgoing interface. */
2133 (vlib_buffer_length_in_chain (vm, p0) >
2135 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2138 (vlib_buffer_length_in_chain (vm, p1) >
2140 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2143 /* Don't adjust the buffer for hop count issue; icmp-error node
2144 * wants to see the IP headerr */
2145 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2147 p0->current_data -= rw_len0;
2148 p0->current_length += rw_len0;
2150 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2151 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2152 next0 = adj0[0].rewrite_header.next_index;
2155 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2156 vnet_feature_arc_start (lm->output_feature_arc_index,
2157 tx_sw_if_index0, &next0, p0);
2159 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2161 p1->current_data -= rw_len1;
2162 p1->current_length += rw_len1;
2164 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2165 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2166 next1 = adj1[0].rewrite_header.next_index;
2169 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2170 vnet_feature_arc_start (lm->output_feature_arc_index,
2171 tx_sw_if_index1, &next1, p1);
2174 /* Guess we are only writing on simple Ethernet header. */
2175 vnet_rewrite_two_headers (adj0[0], adj1[0],
2176 ip0, ip1, sizeof (ethernet_header_t));
2180 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2181 adj1->sub_type.midchain.fixup_func (vm, adj1, p1);
2186 * copy bytes from the IP address into the MAC rewrite
2188 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2189 vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1);
2192 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2193 to_next, n_left_to_next,
2194 pi0, pi1, next0, next1);
2197 while (n_left_from > 0 && n_left_to_next > 0)
2199 ip_adjacency_t *adj0;
2203 u32 adj_index0, next0, error0;
2204 u32 tx_sw_if_index0;
2206 pi0 = to_next[0] = from[0];
2208 p0 = vlib_get_buffer (vm, pi0);
2210 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2212 adj0 = adj_get (adj_index0);
2214 ip0 = vlib_buffer_get_current (p0);
2216 error0 = IP6_ERROR_NONE;
2217 next0 = IP6_REWRITE_NEXT_DROP;
2219 /* Check hop limit */
2220 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2222 i32 hop_limit0 = ip0->hop_limit;
2224 ASSERT (ip0->hop_limit > 0);
2228 ip0->hop_limit = hop_limit0;
2230 if (PREDICT_FALSE (hop_limit0 <= 0))
2233 * If the hop count drops below 1 when forwarding, generate
2236 error0 = IP6_ERROR_TIME_EXPIRED;
2237 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2238 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2239 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2240 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2246 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2249 /* Guess we are only writing on simple Ethernet header. */
2250 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2252 /* Update packet buffer attributes/set output interface. */
2253 rw_len0 = adj0[0].rewrite_header.data_bytes;
2254 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2258 vlib_increment_combined_counter
2259 (&adjacency_counters,
2260 thread_index, adj_index0, 1,
2261 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2264 /* Check MTU of outgoing interface. */
2266 (vlib_buffer_length_in_chain (vm, p0) >
2268 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2271 /* Don't adjust the buffer for hop count issue; icmp-error node
2272 * wants to see the IP headerr */
2273 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2275 p0->current_data -= rw_len0;
2276 p0->current_length += rw_len0;
2278 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2280 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2281 next0 = adj0[0].rewrite_header.next_index;
2284 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2285 vnet_feature_arc_start (lm->output_feature_arc_index,
2286 tx_sw_if_index0, &next0, p0);
2291 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2295 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2298 p0->error = error_node->errors[error0];
2303 n_left_to_next -= 1;
2305 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2306 to_next, n_left_to_next,
2310 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2313 /* Need to do trace after rewrites to pick up new packet data. */
2314 if (node->flags & VLIB_NODE_FLAG_TRACE)
2315 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2317 return frame->n_vectors;
2321 ip6_rewrite (vlib_main_t * vm,
2322 vlib_node_runtime_t * node, vlib_frame_t * frame)
2324 if (adj_are_counters_enabled ())
2325 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2327 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2331 ip6_rewrite_mcast (vlib_main_t * vm,
2332 vlib_node_runtime_t * node, vlib_frame_t * frame)
2334 if (adj_are_counters_enabled ())
2335 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2337 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2341 ip6_midchain (vlib_main_t * vm,
2342 vlib_node_runtime_t * node, vlib_frame_t * frame)
2344 if (adj_are_counters_enabled ())
2345 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2347 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2351 ip6_mcast_midchain (vlib_main_t * vm,
2352 vlib_node_runtime_t * node, vlib_frame_t * frame)
2354 if (adj_are_counters_enabled ())
2355 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2357 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2361 VLIB_REGISTER_NODE (ip6_midchain_node) =
2363 .function = ip6_midchain,
2364 .name = "ip6-midchain",
2365 .vector_size = sizeof (u32),
2366 .format_trace = format_ip6_forward_next_trace,
2367 .sibling_of = "ip6-rewrite",
2371 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2374 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2376 .function = ip6_rewrite,
2377 .name = "ip6-rewrite",
2378 .vector_size = sizeof (u32),
2379 .format_trace = format_ip6_rewrite_trace,
2383 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2384 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2389 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2392 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2394 .function = ip6_rewrite_mcast,
2395 .name = "ip6-rewrite-mcast",
2396 .vector_size = sizeof (u32),
2397 .format_trace = format_ip6_rewrite_trace,
2398 .sibling_of = "ip6-rewrite",
2402 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2405 VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
2407 .function = ip6_mcast_midchain,
2408 .name = "ip6-mcast-midchain",
2409 .vector_size = sizeof (u32),
2410 .format_trace = format_ip6_rewrite_trace,
2411 .sibling_of = "ip6-rewrite",
2415 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
2418 * Hop-by-Hop handling
2420 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2422 #define foreach_ip6_hop_by_hop_error \
2423 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2424 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2425 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2430 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2431 foreach_ip6_hop_by_hop_error
2433 IP6_HOP_BY_HOP_N_ERROR,
2434 } ip6_hop_by_hop_error_t;
2438 * Primary h-b-h handler trace support
2439 * We work pretty hard on the problem for obvious reasons
2445 u8 option_data[256];
2446 } ip6_hop_by_hop_trace_t;
2448 vlib_node_registration_t ip6_hop_by_hop_node;
2450 static char *ip6_hop_by_hop_error_strings[] = {
2451 #define _(sym,string) string,
2452 foreach_ip6_hop_by_hop_error
2457 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2459 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2460 int total_len = va_arg (*args, int);
2461 ip6_hop_by_hop_option_t *opt0, *limit0;
2462 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2465 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2466 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2468 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2469 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2471 while (opt0 < limit0)
2476 case 0: /* Pad, just stop */
2477 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2481 if (hm->trace[type0])
2483 s = (*hm->trace[type0]) (s, opt0);
2488 format (s, "\n unrecognized option %d length %d", type0,
2492 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2493 sizeof (ip6_hop_by_hop_option_t));
2501 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2503 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2504 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2505 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2506 ip6_hop_by_hop_header_t *hbh0;
2507 ip6_hop_by_hop_option_t *opt0, *limit0;
2508 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2512 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2514 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2515 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2517 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2518 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2520 while (opt0 < limit0)
2525 case 0: /* Pad, just stop */
2526 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2530 if (hm->trace[type0])
2532 s = (*hm->trace[type0]) (s, opt0);
2537 format (s, "\n unrecognized option %d length %d", type0,
2541 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2542 sizeof (ip6_hop_by_hop_option_t));
2550 ip6_scan_hbh_options (vlib_buffer_t * b0,
2552 ip6_hop_by_hop_header_t * hbh0,
2553 ip6_hop_by_hop_option_t * opt0,
2554 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2556 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2560 while (opt0 < limit0)
2566 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2571 if (hm->options[type0])
2573 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2575 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2581 /* Unrecognized mandatory option, check the two high order bits */
2582 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2584 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2586 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2587 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2588 *next0 = IP_LOOKUP_NEXT_DROP;
2590 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2591 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2592 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2593 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2594 ICMP6_parameter_problem_unrecognized_option,
2595 (u8 *) opt0 - (u8 *) ip0);
2597 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2598 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2599 if (!ip6_address_is_multicast (&ip0->dst_address))
2601 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2602 icmp6_error_set_vnet_buffer (b0,
2603 ICMP6_parameter_problem,
2604 ICMP6_parameter_problem_unrecognized_option,
2605 (u8 *) opt0 - (u8 *) ip0);
2609 *next0 = IP_LOOKUP_NEXT_DROP;
2617 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2618 sizeof (ip6_hop_by_hop_option_t));
2624 * Process the Hop-by-Hop Options header
2627 ip6_hop_by_hop (vlib_main_t * vm,
2628 vlib_node_runtime_t * node, vlib_frame_t * frame)
2630 vlib_node_runtime_t *error_node =
2631 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2632 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2633 u32 n_left_from, *from, *to_next;
2634 ip_lookup_next_t next_index;
2636 from = vlib_frame_vector_args (frame);
2637 n_left_from = frame->n_vectors;
2638 next_index = node->cached_next_index;
2640 while (n_left_from > 0)
2644 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2646 while (n_left_from >= 4 && n_left_to_next >= 2)
2649 vlib_buffer_t *b0, *b1;
2651 ip6_header_t *ip0, *ip1;
2652 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2653 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2654 u8 error0 = 0, error1 = 0;
2656 /* Prefetch next iteration. */
2658 vlib_buffer_t *p2, *p3;
2660 p2 = vlib_get_buffer (vm, from[2]);
2661 p3 = vlib_get_buffer (vm, from[3]);
2663 vlib_prefetch_buffer_header (p2, LOAD);
2664 vlib_prefetch_buffer_header (p3, LOAD);
2666 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2667 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2670 /* Speculatively enqueue b0, b1 to the current next frame */
2671 to_next[0] = bi0 = from[0];
2672 to_next[1] = bi1 = from[1];
2676 n_left_to_next -= 2;
2678 b0 = vlib_get_buffer (vm, bi0);
2679 b1 = vlib_get_buffer (vm, bi1);
2681 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2682 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2683 ip_adjacency_t *adj0 = adj_get (adj_index0);
2684 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2685 ip_adjacency_t *adj1 = adj_get (adj_index1);
2687 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2688 next0 = adj0->lookup_next_index;
2689 next1 = adj1->lookup_next_index;
2691 ip0 = vlib_buffer_get_current (b0);
2692 ip1 = vlib_buffer_get_current (b1);
2693 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2694 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2695 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2696 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2698 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2699 ((hbh0->length + 1) << 3));
2701 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2702 ((hbh1->length + 1) << 3));
2705 * Basic validity checks
2707 if ((hbh0->length + 1) << 3 >
2708 clib_net_to_host_u16 (ip0->payload_length))
2710 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2711 next0 = IP_LOOKUP_NEXT_DROP;
2714 /* Scan the set of h-b-h options, process ones that we understand */
2715 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2717 if ((hbh1->length + 1) << 3 >
2718 clib_net_to_host_u16 (ip1->payload_length))
2720 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2721 next1 = IP_LOOKUP_NEXT_DROP;
2724 /* Scan the set of h-b-h options, process ones that we understand */
2725 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2728 /* Has the classifier flagged this buffer for special treatment? */
2731 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2732 next0 = hm->next_override;
2734 /* Has the classifier flagged this buffer for special treatment? */
2737 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2738 next1 = hm->next_override;
2740 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2742 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2744 ip6_hop_by_hop_trace_t *t =
2745 vlib_add_trace (vm, node, b0, sizeof (*t));
2746 u32 trace_len = (hbh0->length + 1) << 3;
2747 t->next_index = next0;
2748 /* Capture the h-b-h option verbatim */
2751 ARRAY_LEN (t->option_data) ? trace_len :
2752 ARRAY_LEN (t->option_data);
2753 t->trace_len = trace_len;
2754 clib_memcpy (t->option_data, hbh0, trace_len);
2756 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2758 ip6_hop_by_hop_trace_t *t =
2759 vlib_add_trace (vm, node, b1, sizeof (*t));
2760 u32 trace_len = (hbh1->length + 1) << 3;
2761 t->next_index = next1;
2762 /* Capture the h-b-h option verbatim */
2765 ARRAY_LEN (t->option_data) ? trace_len :
2766 ARRAY_LEN (t->option_data);
2767 t->trace_len = trace_len;
2768 clib_memcpy (t->option_data, hbh1, trace_len);
2773 b0->error = error_node->errors[error0];
2774 b1->error = error_node->errors[error1];
2776 /* verify speculative enqueue, maybe switch current next frame */
2777 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2778 n_left_to_next, bi0, bi1, next0,
2782 while (n_left_from > 0 && n_left_to_next > 0)
2788 ip6_hop_by_hop_header_t *hbh0;
2789 ip6_hop_by_hop_option_t *opt0, *limit0;
2792 /* Speculatively enqueue b0 to the current next frame */
2798 n_left_to_next -= 1;
2800 b0 = vlib_get_buffer (vm, bi0);
2802 * Default use the next_index from the adjacency.
2803 * A HBH option rarely redirects to a different node
2805 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2806 ip_adjacency_t *adj0 = adj_get (adj_index0);
2807 next0 = adj0->lookup_next_index;
2809 ip0 = vlib_buffer_get_current (b0);
2810 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2811 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2813 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2814 ((hbh0->length + 1) << 3));
2817 * Basic validity checks
2819 if ((hbh0->length + 1) << 3 >
2820 clib_net_to_host_u16 (ip0->payload_length))
2822 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2823 next0 = IP_LOOKUP_NEXT_DROP;
2827 /* Scan the set of h-b-h options, process ones that we understand */
2828 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2831 /* Has the classifier flagged this buffer for special treatment? */
2834 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2835 next0 = hm->next_override;
2837 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2839 ip6_hop_by_hop_trace_t *t =
2840 vlib_add_trace (vm, node, b0, sizeof (*t));
2841 u32 trace_len = (hbh0->length + 1) << 3;
2842 t->next_index = next0;
2843 /* Capture the h-b-h option verbatim */
2846 ARRAY_LEN (t->option_data) ? trace_len :
2847 ARRAY_LEN (t->option_data);
2848 t->trace_len = trace_len;
2849 clib_memcpy (t->option_data, hbh0, trace_len);
2852 b0->error = error_node->errors[error0];
2854 /* verify speculative enqueue, maybe switch current next frame */
2855 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2856 n_left_to_next, bi0, next0);
2858 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2860 return frame->n_vectors;
2864 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2866 .function = ip6_hop_by_hop,
2867 .name = "ip6-hop-by-hop",
2868 .sibling_of = "ip6-lookup",
2869 .vector_size = sizeof (u32),
2870 .format_trace = format_ip6_hop_by_hop_trace,
2871 .type = VLIB_NODE_TYPE_INTERNAL,
2872 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2873 .error_strings = ip6_hop_by_hop_error_strings,
2878 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2880 static clib_error_t *
2881 ip6_hop_by_hop_init (vlib_main_t * vm)
2883 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2884 memset (hm->options, 0, sizeof (hm->options));
2885 memset (hm->trace, 0, sizeof (hm->trace));
2886 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2890 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2893 ip6_hbh_set_next_override (uword next)
2895 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2897 hm->next_override = next;
2901 ip6_hbh_register_option (u8 option,
2902 int options (vlib_buffer_t * b, ip6_header_t * ip,
2903 ip6_hop_by_hop_option_t * opt),
2904 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2906 ip6_main_t *im = &ip6_main;
2907 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2909 ASSERT (option < ARRAY_LEN (hm->options));
2911 /* Already registered */
2912 if (hm->options[option])
2915 hm->options[option] = options;
2916 hm->trace[option] = trace;
2918 /* Set global variable */
2919 im->hbh_enabled = 1;
2925 ip6_hbh_unregister_option (u8 option)
2927 ip6_main_t *im = &ip6_main;
2928 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2930 ASSERT (option < ARRAY_LEN (hm->options));
2932 /* Not registered */
2933 if (!hm->options[option])
2936 hm->options[option] = NULL;
2937 hm->trace[option] = NULL;
2939 /* Disable global knob if this was the last option configured */
2942 for (i = 0; i < 256; i++)
2944 if (hm->options[option])
2951 im->hbh_enabled = 0;
2956 /* Global IP6 main. */
2957 ip6_main_t ip6_main;
2959 static clib_error_t *
2960 ip6_lookup_init (vlib_main_t * vm)
2962 ip6_main_t *im = &ip6_main;
2963 clib_error_t *error;
2966 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2969 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2976 for (j = 0; j < i0; j++)
2977 im->fib_masks[i].as_u32[j] = ~0;
2980 im->fib_masks[i].as_u32[i0] =
2981 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2984 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2986 if (im->lookup_table_nbuckets == 0)
2987 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2989 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2991 if (im->lookup_table_size == 0)
2992 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2994 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2995 "ip6 FIB fwding table",
2996 im->lookup_table_nbuckets, im->lookup_table_size);
2997 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2998 "ip6 FIB non-fwding table",
2999 im->lookup_table_nbuckets, im->lookup_table_size);
3001 /* Create FIB with index 0 and table id of 0. */
3002 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
3003 FIB_SOURCE_DEFAULT_ROUTE);
3004 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
3005 MFIB_SOURCE_DEFAULT_ROUTE);
3009 pn = pg_get_node (ip6_lookup_node.index);
3010 pn->unformat_edit = unformat_pg_ip6_header;
3013 /* Unless explicitly configured, don't process HBH options */
3014 im->hbh_enabled = 0;
3017 icmp6_neighbor_solicitation_header_t p;
3019 memset (&p, 0, sizeof (p));
3021 p.ip.ip_version_traffic_class_and_flow_label =
3022 clib_host_to_net_u32 (0x6 << 28);
3023 p.ip.payload_length =
3024 clib_host_to_net_u16 (sizeof (p) -
3026 (icmp6_neighbor_solicitation_header_t, neighbor));
3027 p.ip.protocol = IP_PROTOCOL_ICMP6;
3028 p.ip.hop_limit = 255;
3029 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
3031 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
3033 p.link_layer_option.header.type =
3034 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
3035 p.link_layer_option.header.n_data_u64s =
3036 sizeof (p.link_layer_option) / sizeof (u64);
3038 vlib_packet_template_init (vm,
3039 &im->discover_neighbor_packet_template,
3041 /* alloc chunk size */ 8,
3042 "ip6 neighbor discovery");
3048 VLIB_INIT_FUNCTION (ip6_lookup_init);
3051 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
3054 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3055 /* Invert the "u" bit */
3056 ip->as_u8[8] = mac[0] ^ (1 << 1);
3057 ip->as_u8[9] = mac[1];
3058 ip->as_u8[10] = mac[2];
3059 ip->as_u8[11] = 0xFF;
3060 ip->as_u8[12] = 0xFE;
3061 ip->as_u8[13] = mac[3];
3062 ip->as_u8[14] = mac[4];
3063 ip->as_u8[15] = mac[5];
3067 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3070 /* Invert the previously inverted "u" bit */
3071 mac[0] = ip->as_u8[8] ^ (1 << 1);
3072 mac[1] = ip->as_u8[9];
3073 mac[2] = ip->as_u8[10];
3074 mac[3] = ip->as_u8[13];
3075 mac[4] = ip->as_u8[14];
3076 mac[5] = ip->as_u8[15];
3079 static clib_error_t *
3080 test_ip6_link_command_fn (vlib_main_t * vm,
3081 unformat_input_t * input, vlib_cli_command_t * cmd)
3084 ip6_address_t _a, *a = &_a;
3086 if (unformat (input, "%U", unformat_ethernet_address, mac))
3088 ip6_link_local_address_from_ethernet_mac_address (a, mac);
3089 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
3090 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3091 vlib_cli_output (vm, "Original MAC address: %U",
3092 format_ethernet_address, mac);
3099 * This command converts the given MAC Address into an IPv6 link-local
3103 * Example of how to create an IPv6 link-local address:
3104 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3105 * Link local address: fe80::14d9:e0ff:fe91:7986
3106 * Original MAC address: 16:d9:e0:91:79:86
3110 VLIB_CLI_COMMAND (test_link_command, static) =
3112 .path = "test ip6 link",
3113 .function = test_ip6_link_command_fn,
3114 .short_help = "test ip6 link <mac-address>",
3119 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3123 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
3125 if (~0 == fib_index)
3126 return VNET_API_ERROR_NO_SUCH_FIB;
3128 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
3134 static clib_error_t *
3135 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3136 unformat_input_t * input,
3137 vlib_cli_command_t * cmd)
3141 u32 flow_hash_config = 0;
3144 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3146 if (unformat (input, "table %d", &table_id))
3149 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3150 foreach_flow_hash_bit
3157 return clib_error_return (0, "unknown input `%U'",
3158 format_unformat_error, input);
3160 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3167 return clib_error_return (0, "no such FIB table %d", table_id);
3170 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3178 * Configure the set of IPv6 fields used by the flow hash.
3182 * Example of how to set the flow hash on a given table:
3183 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3185 * Example of display the configured flow hash:
3186 * @cliexstart{show ip6 fib}
3187 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3190 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3191 * [0] [@0]: dpo-drop ip6
3194 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3195 * [0] [@2]: dpo-receive
3198 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3199 * [0] [@2]: dpo-receive
3202 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3203 * [0] [@2]: dpo-receive
3206 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3207 * [0] [@2]: dpo-receive
3208 * ff02::1:ff00:0/104
3210 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3211 * [0] [@2]: dpo-receive
3212 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3215 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3216 * [0] [@0]: dpo-drop ip6
3219 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3220 * [0] [@4]: ipv6-glean: af_packet0
3223 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3224 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3227 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3228 * [0] [@2]: dpo-receive
3229 * fe80::fe:3eff:fe3e:9222/128
3231 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3232 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3235 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3236 * [0] [@2]: dpo-receive
3239 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3240 * [0] [@2]: dpo-receive
3243 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3244 * [0] [@2]: dpo-receive
3245 * ff02::1:ff00:0/104
3247 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3248 * [0] [@2]: dpo-receive
3253 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3255 .path = "set ip6 flow-hash",
3257 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3258 .function = set_ip6_flow_hash_command_fn,
3262 static clib_error_t *
3263 show_ip6_local_command_fn (vlib_main_t * vm,
3264 unformat_input_t * input, vlib_cli_command_t * cmd)
3266 ip6_main_t *im = &ip6_main;
3267 ip_lookup_main_t *lm = &im->lookup_main;
3270 vlib_cli_output (vm, "Protocols handled by ip6_local");
3271 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3273 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3274 vlib_cli_output (vm, "%d", i);
3282 * Display the set of protocols handled by the local IPv6 stack.
3285 * Example of how to display local protocol table:
3286 * @cliexstart{show ip6 local}
3287 * Protocols handled by ip6_local
3295 VLIB_CLI_COMMAND (show_ip6_local, static) =
3297 .path = "show ip6 local",
3298 .function = show_ip6_local_command_fn,
3299 .short_help = "show ip6 local",
3304 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3307 vnet_main_t *vnm = vnet_get_main ();
3308 vnet_interface_main_t *im = &vnm->interface_main;
3309 ip6_main_t *ipm = &ip6_main;
3310 ip_lookup_main_t *lm = &ipm->lookup_main;
3311 vnet_classify_main_t *cm = &vnet_classify_main;
3312 ip6_address_t *if_addr;
3314 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3315 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3317 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3318 return VNET_API_ERROR_NO_SUCH_ENTRY;
3320 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3321 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3323 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3325 if (NULL != if_addr)
3327 fib_prefix_t pfx = {
3329 .fp_proto = FIB_PROTOCOL_IP6,
3330 .fp_addr.ip6 = *if_addr,
3334 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3338 if (table_index != (u32) ~ 0)
3340 dpo_id_t dpo = DPO_INVALID;
3345 classify_dpo_create (DPO_PROTO_IP6, table_index));
3347 fib_table_entry_special_dpo_add (fib_index,
3349 FIB_SOURCE_CLASSIFY,
3350 FIB_ENTRY_FLAG_NONE, &dpo);
3355 fib_table_entry_special_remove (fib_index,
3356 &pfx, FIB_SOURCE_CLASSIFY);
3363 static clib_error_t *
3364 set_ip6_classify_command_fn (vlib_main_t * vm,
3365 unformat_input_t * input,
3366 vlib_cli_command_t * cmd)
3368 u32 table_index = ~0;
3369 int table_index_set = 0;
3370 u32 sw_if_index = ~0;
3373 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3375 if (unformat (input, "table-index %d", &table_index))
3376 table_index_set = 1;
3377 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3378 vnet_get_main (), &sw_if_index))
3384 if (table_index_set == 0)
3385 return clib_error_return (0, "classify table-index must be specified");
3387 if (sw_if_index == ~0)
3388 return clib_error_return (0, "interface / subif must be specified");
3390 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3397 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3398 return clib_error_return (0, "No such interface");
3400 case VNET_API_ERROR_NO_SUCH_ENTRY:
3401 return clib_error_return (0, "No such classifier table");
3407 * Assign a classification table to an interface. The classification
3408 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3409 * commands. Once the table is create, use this command to filter packets
3413 * Example of how to assign a classification table to an interface:
3414 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3417 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3419 .path = "set ip6 classify",
3421 "set ip6 classify intfc <interface> table-index <classify-idx>",
3422 .function = set_ip6_classify_command_fn,
3426 static clib_error_t *
3427 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3429 ip6_main_t *im = &ip6_main;
3434 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3436 if (unformat (input, "hash-buckets %d", &tmp))
3438 else if (unformat (input, "heap-size %dm", &tmp))
3439 heapsize = ((u64) tmp) << 20;
3440 else if (unformat (input, "heap-size %dM", &tmp))
3441 heapsize = ((u64) tmp) << 20;
3442 else if (unformat (input, "heap-size %dg", &tmp))
3443 heapsize = ((u64) tmp) << 30;
3444 else if (unformat (input, "heap-size %dG", &tmp))
3445 heapsize = ((u64) tmp) << 30;
3447 return clib_error_return (0, "unknown input '%U'",
3448 format_unformat_error, input);
3451 im->lookup_table_nbuckets = nbuckets;
3452 im->lookup_table_size = heapsize;
3457 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3460 * fd.io coding-style-patch-verification: ON
3463 * eval: (c-set-style "gnu")