2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * ip/ip6_forward.c: IP v6 forwarding
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
43 #include <vnet/srp/srp.h> /* for srp_hw_interface_class */
44 #include <vppinfra/cache.h>
45 #include <vnet/fib/fib_urpf_list.h> /* for FIB uRPF check */
46 #include <vnet/fib/ip6_fib.h>
47 #include <vnet/mfib/ip6_mfib.h>
48 #include <vnet/dpo/load_balance_map.h>
49 #include <vnet/dpo/classify_dpo.h>
51 #include <vppinfra/bihash_template.c>
53 /* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
54 #define OI_DECAP 0x80000000
58 * @brief IPv6 Forwarding.
60 * This file contains the source code for IPv6 forwarding.
64 ip6_forward_next_trace (vlib_main_t * vm,
65 vlib_node_runtime_t * node,
67 vlib_rx_or_tx_t which_adj_index);
70 ip6_lookup_inline (vlib_main_t * vm,
71 vlib_node_runtime_t * node, vlib_frame_t * frame)
73 ip6_main_t *im = &ip6_main;
74 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_to_counters;
75 u32 n_left_from, n_left_to_next, *from, *to_next;
76 ip_lookup_next_t next;
77 u32 thread_index = vlib_get_thread_index ();
79 from = vlib_frame_vector_args (frame);
80 n_left_from = frame->n_vectors;
81 next = node->cached_next_index;
83 while (n_left_from > 0)
85 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
87 while (n_left_from >= 4 && n_left_to_next >= 2)
89 vlib_buffer_t *p0, *p1;
90 u32 pi0, pi1, lbi0, lbi1, wrong_next;
91 ip_lookup_next_t next0, next1;
92 ip6_header_t *ip0, *ip1;
93 ip6_address_t *dst_addr0, *dst_addr1;
94 u32 fib_index0, fib_index1;
95 u32 flow_hash_config0, flow_hash_config1;
96 const dpo_id_t *dpo0, *dpo1;
97 const load_balance_t *lb0, *lb1;
99 /* Prefetch next iteration. */
101 vlib_buffer_t *p2, *p3;
103 p2 = vlib_get_buffer (vm, from[2]);
104 p3 = vlib_get_buffer (vm, from[3]);
106 vlib_prefetch_buffer_header (p2, LOAD);
107 vlib_prefetch_buffer_header (p3, LOAD);
108 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
109 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
112 pi0 = to_next[0] = from[0];
113 pi1 = to_next[1] = from[1];
115 p0 = vlib_get_buffer (vm, pi0);
116 p1 = vlib_get_buffer (vm, pi1);
118 ip0 = vlib_buffer_get_current (p0);
119 ip1 = vlib_buffer_get_current (p1);
121 dst_addr0 = &ip0->dst_address;
122 dst_addr1 = &ip1->dst_address;
125 vec_elt (im->fib_index_by_sw_if_index,
126 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
128 vec_elt (im->fib_index_by_sw_if_index,
129 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
131 fib_index0 = (vnet_buffer (p0)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
132 fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
133 fib_index1 = (vnet_buffer (p1)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
134 fib_index1 : vnet_buffer (p1)->sw_if_index[VLIB_TX];
136 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
137 lbi1 = ip6_fib_table_fwding_lookup (im, fib_index1, dst_addr1);
139 lb0 = load_balance_get (lbi0);
140 lb1 = load_balance_get (lbi1);
141 ASSERT (lb0->lb_n_buckets > 0);
142 ASSERT (lb1->lb_n_buckets > 0);
143 ASSERT (is_pow2 (lb0->lb_n_buckets));
144 ASSERT (is_pow2 (lb1->lb_n_buckets));
146 vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
148 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
150 flow_hash_config0 = lb0->lb_hash_config;
151 vnet_buffer (p0)->ip.flow_hash =
152 ip6_compute_flow_hash (ip0, flow_hash_config0);
154 load_balance_get_fwd_bucket (lb0,
155 (vnet_buffer (p0)->ip.flow_hash &
156 (lb0->lb_n_buckets_minus_1)));
160 dpo0 = load_balance_get_bucket_i (lb0, 0);
162 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
164 flow_hash_config1 = lb1->lb_hash_config;
165 vnet_buffer (p1)->ip.flow_hash =
166 ip6_compute_flow_hash (ip1, flow_hash_config1);
168 load_balance_get_fwd_bucket (lb1,
169 (vnet_buffer (p1)->ip.flow_hash &
170 (lb1->lb_n_buckets_minus_1)));
174 dpo1 = load_balance_get_bucket_i (lb1, 0);
176 next0 = dpo0->dpoi_next_node;
177 next1 = dpo1->dpoi_next_node;
179 /* Only process the HBH Option Header if explicitly configured to do so */
181 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
183 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
184 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
187 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
189 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
190 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
192 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
193 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
195 vlib_increment_combined_counter
196 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
197 vlib_increment_combined_counter
198 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
205 wrong_next = (next0 != next) + 2 * (next1 != next);
206 if (PREDICT_FALSE (wrong_next != 0))
215 vlib_set_next_frame_buffer (vm, node, next0, pi0);
222 vlib_set_next_frame_buffer (vm, node, next1, pi1);
229 vlib_set_next_frame_buffer (vm, node, next0, pi0);
230 vlib_set_next_frame_buffer (vm, node, next1, pi1);
234 vlib_put_next_frame (vm, node, next, n_left_to_next);
236 vlib_get_next_frame (vm, node, next, to_next,
243 while (n_left_from > 0 && n_left_to_next > 0)
248 ip_lookup_next_t next0;
250 ip6_address_t *dst_addr0;
251 u32 fib_index0, flow_hash_config0;
252 const dpo_id_t *dpo0;
257 p0 = vlib_get_buffer (vm, pi0);
259 ip0 = vlib_buffer_get_current (p0);
261 dst_addr0 = &ip0->dst_address;
264 vec_elt (im->fib_index_by_sw_if_index,
265 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
267 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
268 (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
270 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
272 lb0 = load_balance_get (lbi0);
273 flow_hash_config0 = lb0->lb_hash_config;
275 vnet_buffer (p0)->ip.flow_hash = 0;
276 ASSERT (lb0->lb_n_buckets > 0);
277 ASSERT (is_pow2 (lb0->lb_n_buckets));
279 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
281 flow_hash_config0 = lb0->lb_hash_config;
282 vnet_buffer (p0)->ip.flow_hash =
283 ip6_compute_flow_hash (ip0, flow_hash_config0);
285 load_balance_get_fwd_bucket (lb0,
286 (vnet_buffer (p0)->ip.flow_hash &
287 (lb0->lb_n_buckets_minus_1)));
291 dpo0 = load_balance_get_bucket_i (lb0, 0);
294 dpo0 = load_balance_get_bucket_i (lb0,
295 (vnet_buffer (p0)->ip.flow_hash &
296 lb0->lb_n_buckets_minus_1));
297 next0 = dpo0->dpoi_next_node;
299 /* Only process the HBH Option Header if explicitly configured to do so */
301 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
303 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
304 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
306 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
308 vlib_increment_combined_counter
309 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
316 if (PREDICT_FALSE (next0 != next))
319 vlib_put_next_frame (vm, node, next, n_left_to_next);
321 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
328 vlib_put_next_frame (vm, node, next, n_left_to_next);
331 if (node->flags & VLIB_NODE_FLAG_TRACE)
332 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
334 return frame->n_vectors;
338 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
339 ip6_main_t * im, u32 fib_index,
340 ip_interface_address_t * a)
342 ip_lookup_main_t *lm = &im->lookup_main;
343 ip6_address_t *address = ip_interface_address_get_address (lm, a);
345 .fp_len = a->address_length,
346 .fp_proto = FIB_PROTOCOL_IP6,
347 .fp_addr.ip6 = *address,
350 if (a->address_length < 128)
352 fib_table_entry_update_one_path (fib_index,
354 FIB_SOURCE_INTERFACE,
355 (FIB_ENTRY_FLAG_CONNECTED |
356 FIB_ENTRY_FLAG_ATTACHED),
358 /* No next-hop address */
360 /* invalid FIB index */
363 NULL, FIB_ROUTE_PATH_FLAG_NONE);
367 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
369 u32 classify_table_index =
370 lm->classify_table_index_by_sw_if_index[sw_if_index];
371 if (classify_table_index != (u32) ~ 0)
373 dpo_id_t dpo = DPO_INVALID;
378 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
380 fib_table_entry_special_dpo_add (fib_index,
383 FIB_ENTRY_FLAG_NONE, &dpo);
388 fib_table_entry_update_one_path (fib_index, &pfx,
389 FIB_SOURCE_INTERFACE,
390 (FIB_ENTRY_FLAG_CONNECTED |
391 FIB_ENTRY_FLAG_LOCAL),
395 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
399 ip6_del_interface_routes (ip6_main_t * im,
401 ip6_address_t * address, u32 address_length)
404 .fp_len = address_length,
405 .fp_proto = FIB_PROTOCOL_IP6,
406 .fp_addr.ip6 = *address,
409 if (pfx.fp_len < 128)
411 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
416 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
420 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
422 ip6_main_t *im = &ip6_main;
424 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
427 * enable/disable only on the 1<->0 transition
431 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
436 /* The ref count is 0 when an address is removed from an interface that has
437 * no address - this is not a ciritical error */
438 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
439 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
443 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
446 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
450 /* get first interface address */
452 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
454 ip_lookup_main_t *lm = &im->lookup_main;
455 ip_interface_address_t *ia = 0;
456 ip6_address_t *result = 0;
459 foreach_ip_interface_address (lm, ia, sw_if_index,
460 1 /* honor unnumbered */,
462 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
471 ip6_add_del_interface_address (vlib_main_t * vm,
473 ip6_address_t * address,
474 u32 address_length, u32 is_del)
476 vnet_main_t *vnm = vnet_get_main ();
477 ip6_main_t *im = &ip6_main;
478 ip_lookup_main_t *lm = &im->lookup_main;
480 u32 if_address_index;
481 ip6_address_fib_t ip6_af, *addr_fib = 0;
483 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
484 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
486 ip6_addr_fib_init (&ip6_af, address,
487 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
488 vec_add1 (addr_fib, ip6_af);
491 uword elts_before = pool_elts (lm->if_address_pool);
493 error = ip_interface_address_add_del
494 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
498 /* Pool did not grow: add duplicate address. */
499 if (elts_before == pool_elts (lm->if_address_pool))
503 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
506 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
508 ip6_add_interface_routes (vnm, sw_if_index,
509 im, ip6_af.fib_index,
510 pool_elt_at_index (lm->if_address_pool,
514 ip6_add_del_interface_address_callback_t *cb;
515 vec_foreach (cb, im->add_del_interface_address_callbacks)
516 cb->function (im, cb->function_opaque, sw_if_index,
517 address, address_length, if_address_index, is_del);
526 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
528 ip6_main_t *im = &ip6_main;
529 ip_interface_address_t *ia;
531 u32 is_admin_up, fib_index;
533 /* Fill in lookup tables with default table (0). */
534 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
536 vec_validate_init_empty (im->
537 lookup_main.if_address_pool_index_by_sw_if_index,
540 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
542 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
545 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
546 0 /* honor unnumbered */,
548 a = ip_interface_address_get_address (&im->lookup_main, ia);
550 ip6_add_interface_routes (vnm, sw_if_index,
554 ip6_del_interface_routes (im, fib_index,
555 a, ia->address_length);
562 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
564 /* Built-in ip6 unicast rx feature path definition */
566 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
568 .arc_name = "ip6-unicast",
569 .start_nodes = VNET_FEATURES ("ip6-input"),
570 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
573 VNET_FEATURE_INIT (ip6_flow_classify, static) =
575 .arc_name = "ip6-unicast",
576 .node_name = "ip6-flow-classify",
577 .runs_before = VNET_FEATURES ("ip6-inacl"),
580 VNET_FEATURE_INIT (ip6_inacl, static) =
582 .arc_name = "ip6-unicast",
583 .node_name = "ip6-inacl",
584 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
587 VNET_FEATURE_INIT (ip6_policer_classify, static) =
589 .arc_name = "ip6-unicast",
590 .node_name = "ip6-policer-classify",
591 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
594 VNET_FEATURE_INIT (ip6_ipsec, static) =
596 .arc_name = "ip6-unicast",
597 .node_name = "ipsec-input-ip6",
598 .runs_before = VNET_FEATURES ("l2tp-decap"),
601 VNET_FEATURE_INIT (ip6_l2tp, static) =
603 .arc_name = "ip6-unicast",
604 .node_name = "l2tp-decap",
605 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
608 VNET_FEATURE_INIT (ip6_vpath, static) =
610 .arc_name = "ip6-unicast",
611 .node_name = "vpath-input-ip6",
612 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
615 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
617 .arc_name = "ip6-unicast",
618 .node_name = "ip6-vxlan-bypass",
619 .runs_before = VNET_FEATURES ("ip6-lookup"),
622 VNET_FEATURE_INIT (ip6_drop, static) =
624 .arc_name = "ip6-unicast",
625 .node_name = "ip6-drop",
626 .runs_before = VNET_FEATURES ("ip6-lookup"),
629 VNET_FEATURE_INIT (ip6_lookup, static) =
631 .arc_name = "ip6-unicast",
632 .node_name = "ip6-lookup",
633 .runs_before = 0, /*last feature*/
636 /* Built-in ip6 multicast rx feature path definition (none now) */
637 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
639 .arc_name = "ip6-multicast",
640 .start_nodes = VNET_FEATURES ("ip6-input"),
641 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
644 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
645 .arc_name = "ip6-multicast",
646 .node_name = "vpath-input-ip6",
647 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
650 VNET_FEATURE_INIT (ip6_drop_mc, static) = {
651 .arc_name = "ip6-multicast",
652 .node_name = "ip6-drop",
653 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
656 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
657 .arc_name = "ip6-multicast",
658 .node_name = "ip6-mfib-forward-lookup",
659 .runs_before = 0, /* last feature */
662 /* Built-in ip4 tx feature path definition */
663 VNET_FEATURE_ARC_INIT (ip6_output, static) =
665 .arc_name = "ip6-output",
666 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain"),
667 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
670 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
671 .arc_name = "ip6-output",
672 .node_name = "ipsec-output-ip6",
673 .runs_before = VNET_FEATURES ("interface-output"),
676 VNET_FEATURE_INIT (ip6_interface_output, static) = {
677 .arc_name = "ip6-output",
678 .node_name = "interface-output",
679 .runs_before = 0, /* not before any other features */
684 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
686 ip6_main_t *im = &ip6_main;
688 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
689 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
691 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
694 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
697 return /* no error */ 0;
700 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
703 ip6_lookup (vlib_main_t * vm,
704 vlib_node_runtime_t * node, vlib_frame_t * frame)
706 return ip6_lookup_inline (vm, node, frame);
709 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
712 VLIB_REGISTER_NODE (ip6_lookup_node) =
714 .function = ip6_lookup,
715 .name = "ip6-lookup",
716 .vector_size = sizeof (u32),
717 .format_trace = format_ip6_lookup_trace,
718 .n_next_nodes = IP6_LOOKUP_N_NEXT,
719 .next_nodes = IP6_LOOKUP_NEXT_NODES,
723 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
726 ip6_load_balance (vlib_main_t * vm,
727 vlib_node_runtime_t * node, vlib_frame_t * frame)
729 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
730 u32 n_left_from, n_left_to_next, *from, *to_next;
731 ip_lookup_next_t next;
732 u32 thread_index = vlib_get_thread_index ();
733 ip6_main_t *im = &ip6_main;
735 from = vlib_frame_vector_args (frame);
736 n_left_from = frame->n_vectors;
737 next = node->cached_next_index;
739 if (node->flags & VLIB_NODE_FLAG_TRACE)
740 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
742 while (n_left_from > 0)
744 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
747 while (n_left_from >= 4 && n_left_to_next >= 2)
749 ip_lookup_next_t next0, next1;
750 const load_balance_t *lb0, *lb1;
751 vlib_buffer_t *p0, *p1;
752 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
753 const ip6_header_t *ip0, *ip1;
754 const dpo_id_t *dpo0, *dpo1;
756 /* Prefetch next iteration. */
758 vlib_buffer_t *p2, *p3;
760 p2 = vlib_get_buffer (vm, from[2]);
761 p3 = vlib_get_buffer (vm, from[3]);
763 vlib_prefetch_buffer_header (p2, STORE);
764 vlib_prefetch_buffer_header (p3, STORE);
766 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
767 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
770 pi0 = to_next[0] = from[0];
771 pi1 = to_next[1] = from[1];
778 p0 = vlib_get_buffer (vm, pi0);
779 p1 = vlib_get_buffer (vm, pi1);
781 ip0 = vlib_buffer_get_current (p0);
782 ip1 = vlib_buffer_get_current (p1);
783 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
784 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
786 lb0 = load_balance_get (lbi0);
787 lb1 = load_balance_get (lbi1);
790 * this node is for via FIBs we can re-use the hash value from the
791 * to node if present.
792 * We don't want to use the same hash value at each level in the recursion
793 * graph as that would lead to polarisation
797 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
799 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
801 hc0 = vnet_buffer (p0)->ip.flow_hash =
802 vnet_buffer (p0)->ip.flow_hash >> 1;
806 hc0 = vnet_buffer (p0)->ip.flow_hash =
807 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
810 load_balance_get_fwd_bucket (lb0,
812 lb0->lb_n_buckets_minus_1));
816 dpo0 = load_balance_get_bucket_i (lb0, 0);
818 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
820 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
822 hc1 = vnet_buffer (p1)->ip.flow_hash =
823 vnet_buffer (p1)->ip.flow_hash >> 1;
827 hc1 = vnet_buffer (p1)->ip.flow_hash =
828 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
831 load_balance_get_fwd_bucket (lb1,
833 lb1->lb_n_buckets_minus_1));
837 dpo1 = load_balance_get_bucket_i (lb1, 0);
840 next0 = dpo0->dpoi_next_node;
841 next1 = dpo1->dpoi_next_node;
843 /* Only process the HBH Option Header if explicitly configured to do so */
845 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
847 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
848 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
850 /* Only process the HBH Option Header if explicitly configured to do so */
852 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
854 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
855 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
858 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
859 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
861 vlib_increment_combined_counter
862 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
863 vlib_increment_combined_counter
864 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
866 vlib_validate_buffer_enqueue_x2 (vm, node, next,
867 to_next, n_left_to_next,
868 pi0, pi1, next0, next1);
871 while (n_left_from > 0 && n_left_to_next > 0)
873 ip_lookup_next_t next0;
874 const load_balance_t *lb0;
877 const ip6_header_t *ip0;
878 const dpo_id_t *dpo0;
887 p0 = vlib_get_buffer (vm, pi0);
889 ip0 = vlib_buffer_get_current (p0);
890 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
892 lb0 = load_balance_get (lbi0);
895 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
897 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
899 hc0 = vnet_buffer (p0)->ip.flow_hash =
900 vnet_buffer (p0)->ip.flow_hash >> 1;
904 hc0 = vnet_buffer (p0)->ip.flow_hash =
905 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
908 load_balance_get_fwd_bucket (lb0,
910 lb0->lb_n_buckets_minus_1));
914 dpo0 = load_balance_get_bucket_i (lb0, 0);
917 next0 = dpo0->dpoi_next_node;
918 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
920 /* Only process the HBH Option Header if explicitly configured to do so */
922 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
924 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
925 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
928 vlib_increment_combined_counter
929 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
931 vlib_validate_buffer_enqueue_x1 (vm, node, next,
932 to_next, n_left_to_next,
936 vlib_put_next_frame (vm, node, next, n_left_to_next);
939 return frame->n_vectors;
943 VLIB_REGISTER_NODE (ip6_load_balance_node) =
945 .function = ip6_load_balance,
946 .name = "ip6-load-balance",
947 .vector_size = sizeof (u32),
948 .sibling_of = "ip6-lookup",
949 .format_trace = format_ip6_lookup_trace,
953 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
957 /* Adjacency taken. */
962 /* Packet data, possibly *after* rewrite. */
963 u8 packet_data[128 - 1 * sizeof (u32)];
965 ip6_forward_next_trace_t;
968 format_ip6_forward_next_trace (u8 * s, va_list * args)
970 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
971 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
972 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
973 uword indent = format_get_indent (s);
975 s = format (s, "%U%U",
976 format_white_space, indent,
977 format_ip6_header, t->packet_data, sizeof (t->packet_data));
982 format_ip6_lookup_trace (u8 * s, va_list * args)
984 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
985 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
986 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
987 uword indent = format_get_indent (s);
989 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
990 t->fib_index, t->adj_index, t->flow_hash);
991 s = format (s, "\n%U%U",
992 format_white_space, indent,
993 format_ip6_header, t->packet_data, sizeof (t->packet_data));
999 format_ip6_rewrite_trace (u8 * s, va_list * args)
1001 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1002 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1003 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
1004 uword indent = format_get_indent (s);
1006 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1007 t->fib_index, t->adj_index, format_ip_adjacency,
1008 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
1009 s = format (s, "\n%U%U",
1010 format_white_space, indent,
1011 format_ip_adjacency_packet_data,
1012 t->adj_index, t->packet_data, sizeof (t->packet_data));
1016 /* Common trace function for all ip6-forward next nodes. */
1018 ip6_forward_next_trace (vlib_main_t * vm,
1019 vlib_node_runtime_t * node,
1020 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
1023 ip6_main_t *im = &ip6_main;
1025 n_left = frame->n_vectors;
1026 from = vlib_frame_vector_args (frame);
1031 vlib_buffer_t *b0, *b1;
1032 ip6_forward_next_trace_t *t0, *t1;
1034 /* Prefetch next iteration. */
1035 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1036 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1041 b0 = vlib_get_buffer (vm, bi0);
1042 b1 = vlib_get_buffer (vm, bi1);
1044 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1046 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1047 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1048 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1050 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1051 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1052 vec_elt (im->fib_index_by_sw_if_index,
1053 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1055 clib_memcpy (t0->packet_data,
1056 vlib_buffer_get_current (b0),
1057 sizeof (t0->packet_data));
1059 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1061 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1062 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1063 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1065 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1066 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1067 vec_elt (im->fib_index_by_sw_if_index,
1068 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
1070 clib_memcpy (t1->packet_data,
1071 vlib_buffer_get_current (b1),
1072 sizeof (t1->packet_data));
1082 ip6_forward_next_trace_t *t0;
1086 b0 = vlib_get_buffer (vm, bi0);
1088 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1090 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1091 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1092 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1094 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1095 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1096 vec_elt (im->fib_index_by_sw_if_index,
1097 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1099 clib_memcpy (t0->packet_data,
1100 vlib_buffer_get_current (b0),
1101 sizeof (t0->packet_data));
1109 ip6_drop_or_punt (vlib_main_t * vm,
1110 vlib_node_runtime_t * node,
1111 vlib_frame_t * frame, ip6_error_t error_code)
1113 u32 *buffers = vlib_frame_vector_args (frame);
1114 uword n_packets = frame->n_vectors;
1116 vlib_error_drop_buffers (vm, node, buffers,
1120 ip6_input_node.index, error_code);
1122 if (node->flags & VLIB_NODE_FLAG_TRACE)
1123 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1129 ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1131 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP);
1135 ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1137 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT);
1141 VLIB_REGISTER_NODE (ip6_drop_node, static) =
1143 .function = ip6_drop,
1145 .vector_size = sizeof (u32),
1146 .format_trace = format_ip6_forward_next_trace,
1150 [0] = "error-drop",},
1154 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
1157 VLIB_REGISTER_NODE (ip6_punt_node, static) =
1159 .function = ip6_punt,
1161 .vector_size = sizeof (u32),
1162 .format_trace = format_ip6_forward_next_trace,
1166 [0] = "error-punt",},
1170 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
1172 /* Compute TCP/UDP/ICMP6 checksum in software. */
1174 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1175 ip6_header_t * ip0, int *bogus_lengthp)
1178 u16 sum16, payload_length_host_byte_order;
1179 u32 i, n_this_buffer, n_bytes_left;
1180 u32 headers_size = sizeof (ip0[0]);
1181 void *data_this_buffer;
1183 ASSERT (bogus_lengthp);
1186 /* Initialize checksum with ip header. */
1187 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1188 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1189 data_this_buffer = (void *) (ip0 + 1);
1191 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1193 sum0 = ip_csum_with_carry (sum0,
1194 clib_mem_unaligned (&ip0->
1195 src_address.as_uword[i],
1198 ip_csum_with_carry (sum0,
1199 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1203 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1204 * or UDP-Ping packets */
1205 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1208 ip6_hop_by_hop_ext_t *ext_hdr =
1209 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1211 /* validate really icmp6 next */
1212 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1213 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
1215 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1216 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1218 payload_length_host_byte_order -= skip_bytes;
1219 headers_size += skip_bytes;
1222 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1223 if (p0 && n_this_buffer + headers_size > p0->current_length)
1225 p0->current_length >
1226 headers_size ? p0->current_length - headers_size : 0;
1229 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1230 n_bytes_left -= n_this_buffer;
1231 if (n_bytes_left == 0)
1234 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1239 p0 = vlib_get_buffer (vm, p0->next_buffer);
1240 data_this_buffer = vlib_buffer_get_current (p0);
1241 n_this_buffer = p0->current_length;
1244 sum16 = ~ip_csum_fold (sum0);
1250 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1252 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1257 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1258 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1259 || ip0->protocol == IP_PROTOCOL_ICMP6
1260 || ip0->protocol == IP_PROTOCOL_UDP
1261 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1263 udp0 = (void *) (ip0 + 1);
1264 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1266 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1267 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1271 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1273 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1274 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1280 * @brief returns number of links on which src is reachable.
1283 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1285 const load_balance_t *lb0;
1288 lbi = ip6_fib_table_fwding_lookup_with_if_index (im,
1290 (b)->sw_if_index[VLIB_RX],
1293 lb0 = load_balance_get (lbi);
1295 return (fib_urpf_check_size (lb0->lb_urpf));
1299 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1301 ip6_main_t *im = &ip6_main;
1302 ip_lookup_main_t *lm = &im->lookup_main;
1303 ip_local_next_t next_index;
1304 u32 *from, *to_next, n_left_from, n_left_to_next;
1305 vlib_node_runtime_t *error_node =
1306 vlib_node_get_runtime (vm, ip6_input_node.index);
1308 from = vlib_frame_vector_args (frame);
1309 n_left_from = frame->n_vectors;
1310 next_index = node->cached_next_index;
1312 if (node->flags & VLIB_NODE_FLAG_TRACE)
1313 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1315 while (n_left_from > 0)
1317 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1319 while (n_left_from >= 4 && n_left_to_next >= 2)
1321 vlib_buffer_t *p0, *p1;
1322 ip6_header_t *ip0, *ip1;
1323 udp_header_t *udp0, *udp1;
1324 u32 pi0, ip_len0, udp_len0, flags0, next0;
1325 u32 pi1, ip_len1, udp_len1, flags1, next1;
1326 i32 len_diff0, len_diff1;
1327 u8 error0, type0, good_l4_checksum0;
1328 u8 error1, type1, good_l4_checksum1;
1329 u32 udp_offset0, udp_offset1;
1331 pi0 = to_next[0] = from[0];
1332 pi1 = to_next[1] = from[1];
1336 n_left_to_next -= 2;
1338 p0 = vlib_get_buffer (vm, pi0);
1339 p1 = vlib_get_buffer (vm, pi1);
1341 ip0 = vlib_buffer_get_current (p0);
1342 ip1 = vlib_buffer_get_current (p1);
1344 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1345 vnet_buffer (p1)->ip.start_of_ip_header = p1->current_data;
1347 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1348 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1350 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1351 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1356 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1357 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1361 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1365 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1366 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1367 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1368 && udp0->checksum == 0;
1369 /* Verify UDP length. */
1370 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1371 udp_len0 = clib_net_to_host_u16 (udp0->length);
1372 len_diff0 = ip_len0 - udp_len0;
1374 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p1, ip1,
1378 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1379 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1380 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1381 && udp1->checksum == 0;
1382 /* Verify UDP length. */
1383 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1384 udp_len1 = clib_net_to_host_u16 (udp1->length);
1385 len_diff1 = ip_len1 - udp_len1;
1388 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1389 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1391 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1392 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1394 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1395 && !good_l4_checksum0
1396 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1398 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1400 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1402 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1403 && !good_l4_checksum1
1404 && !(flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1406 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1408 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1411 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1413 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1414 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1416 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1417 IP6_ERROR_UDP_CHECKSUM);
1418 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1419 IP6_ERROR_ICMP_CHECKSUM);
1421 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1423 (!good_l4_checksum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1425 /* Drop packets from unroutable hosts. */
1426 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1427 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1428 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1429 !ip6_address_is_link_local_unicast (&ip0->src_address))
1431 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1432 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1434 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1435 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1436 !ip6_address_is_link_local_unicast (&ip1->src_address))
1438 error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1439 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1443 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1445 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1447 p0->error = error_node->errors[error0];
1448 p1->error = error_node->errors[error1];
1450 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1451 to_next, n_left_to_next,
1452 pi0, pi1, next0, next1);
1455 while (n_left_from > 0 && n_left_to_next > 0)
1460 u32 pi0, ip_len0, udp_len0, flags0, next0;
1462 u8 error0, type0, good_l4_checksum0;
1465 pi0 = to_next[0] = from[0];
1469 n_left_to_next -= 1;
1471 p0 = vlib_get_buffer (vm, pi0);
1473 ip0 = vlib_buffer_get_current (p0);
1475 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1477 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1478 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1482 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1485 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1489 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1490 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1491 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1492 && udp0->checksum == 0;
1493 /* Verify UDP length. */
1494 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1495 udp_len0 = clib_net_to_host_u16 (udp0->length);
1496 len_diff0 = ip_len0 - udp_len0;
1499 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1500 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1502 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1503 && !good_l4_checksum0
1504 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1506 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1508 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1511 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1513 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1515 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1516 IP6_ERROR_UDP_CHECKSUM);
1517 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1518 IP6_ERROR_ICMP_CHECKSUM);
1520 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1522 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1523 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1524 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1525 !ip6_address_is_link_local_unicast (&ip0->src_address))
1527 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1528 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1532 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1534 p0->error = error_node->errors[error0];
1536 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1537 to_next, n_left_to_next,
1541 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1544 return frame->n_vectors;
1548 VLIB_REGISTER_NODE (ip6_local_node, static) =
1550 .function = ip6_local,
1551 .name = "ip6-local",
1552 .vector_size = sizeof (u32),
1553 .format_trace = format_ip6_forward_next_trace,
1554 .n_next_nodes = IP_LOCAL_N_NEXT,
1557 [IP_LOCAL_NEXT_DROP] = "error-drop",
1558 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1559 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1560 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1565 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1568 ip6_register_protocol (u32 protocol, u32 node_index)
1570 vlib_main_t *vm = vlib_get_main ();
1571 ip6_main_t *im = &ip6_main;
1572 ip_lookup_main_t *lm = &im->lookup_main;
1574 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1575 lm->local_next_by_ip_protocol[protocol] =
1576 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1581 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1582 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1583 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1584 } ip6_discover_neighbor_next_t;
1588 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1589 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1590 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1591 } ip6_discover_neighbor_error_t;
1594 ip6_discover_neighbor_inline (vlib_main_t * vm,
1595 vlib_node_runtime_t * node,
1596 vlib_frame_t * frame, int is_glean)
1598 vnet_main_t *vnm = vnet_get_main ();
1599 ip6_main_t *im = &ip6_main;
1600 ip_lookup_main_t *lm = &im->lookup_main;
1601 u32 *from, *to_next_drop;
1602 uword n_left_from, n_left_to_next_drop;
1603 static f64 time_last_seed_change = -1e100;
1604 static u32 hash_seeds[3];
1605 static uword hash_bitmap[256 / BITS (uword)];
1609 if (node->flags & VLIB_NODE_FLAG_TRACE)
1610 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1612 time_now = vlib_time_now (vm);
1613 if (time_now - time_last_seed_change > 1e-3)
1616 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1617 sizeof (hash_seeds));
1618 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1619 hash_seeds[i] = r[i];
1621 /* Mark all hash keys as been not-seen before. */
1622 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1625 time_last_seed_change = time_now;
1628 from = vlib_frame_vector_args (frame);
1629 n_left_from = frame->n_vectors;
1631 while (n_left_from > 0)
1633 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1634 to_next_drop, n_left_to_next_drop);
1636 while (n_left_from > 0 && n_left_to_next_drop > 0)
1640 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1642 ip_adjacency_t *adj0;
1643 vnet_hw_interface_t *hw_if0;
1648 p0 = vlib_get_buffer (vm, pi0);
1650 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1652 ip0 = vlib_buffer_get_current (p0);
1654 adj0 = adj_get (adj_index0);
1658 ip0->dst_address.as_u64[0] =
1659 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1660 ip0->dst_address.as_u64[1] =
1661 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1668 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1669 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1672 b0 ^= ip0->dst_address.as_u32[0];
1673 c0 ^= ip0->dst_address.as_u32[1];
1675 hash_v3_mix32 (a0, b0, c0);
1677 b0 ^= ip0->dst_address.as_u32[2];
1678 c0 ^= ip0->dst_address.as_u32[3];
1680 hash_v3_finalize32 (a0, b0, c0);
1682 c0 &= BITS (hash_bitmap) - 1;
1683 c0 = c0 / BITS (uword);
1684 m0 = (uword) 1 << (c0 % BITS (uword));
1686 bm0 = hash_bitmap[c0];
1687 drop0 = (bm0 & m0) != 0;
1689 /* Mark it as seen. */
1690 hash_bitmap[c0] = bm0 | m0;
1694 to_next_drop[0] = pi0;
1696 n_left_to_next_drop -= 1;
1698 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1700 /* If the interface is link-down, drop the pkt */
1701 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1705 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1706 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1711 * the adj has been updated to a rewrite but the node the DPO that got
1712 * us here hasn't - yet. no big deal. we'll drop while we wait.
1714 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1719 icmp6_neighbor_solicitation_header_t *h0;
1722 h0 = vlib_packet_template_get_packet
1723 (vm, &im->discover_neighbor_packet_template, &bi0);
1726 * Build ethernet header.
1727 * Choose source address based on destination lookup
1730 if (ip6_src_address_for_packet (lm,
1732 &h0->ip.src_address))
1734 /* There is no address on the interface */
1736 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1737 vlib_buffer_free (vm, &bi0, 1);
1742 * Destination address is a solicited node multicast address.
1743 * We need to fill in
1744 * the low 24 bits with low 24 bits of target's address.
1746 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1747 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1748 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1750 h0->neighbor.target_address = ip0->dst_address;
1752 clib_memcpy (h0->link_layer_option.ethernet_address,
1753 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1755 /* $$$$ appears we need this; why is the checksum non-zero? */
1756 h0->neighbor.icmp.checksum = 0;
1757 h0->neighbor.icmp.checksum =
1758 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1761 ASSERT (bogus_length == 0);
1763 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1764 b0 = vlib_get_buffer (vm, bi0);
1765 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1766 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1768 /* Add rewrite/encap string. */
1769 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1770 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1772 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1774 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1778 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1779 n_left_to_next_drop);
1782 return frame->n_vectors;
1786 ip6_discover_neighbor (vlib_main_t * vm,
1787 vlib_node_runtime_t * node, vlib_frame_t * frame)
1789 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1793 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1795 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1798 static char *ip6_discover_neighbor_error_strings[] = {
1799 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1800 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1801 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1802 = "no source address for ND solicitation",
1806 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1808 .function = ip6_discover_neighbor,
1809 .name = "ip6-discover-neighbor",
1810 .vector_size = sizeof (u32),
1811 .format_trace = format_ip6_forward_next_trace,
1812 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1813 .error_strings = ip6_discover_neighbor_error_strings,
1814 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1817 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1818 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1824 VLIB_REGISTER_NODE (ip6_glean_node) =
1826 .function = ip6_glean,
1827 .name = "ip6-glean",
1828 .vector_size = sizeof (u32),
1829 .format_trace = format_ip6_forward_next_trace,
1830 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1831 .error_strings = ip6_discover_neighbor_error_strings,
1832 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1835 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1836 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1842 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1844 vnet_main_t *vnm = vnet_get_main ();
1845 ip6_main_t *im = &ip6_main;
1846 icmp6_neighbor_solicitation_header_t *h;
1848 ip_interface_address_t *ia;
1849 ip_adjacency_t *adj;
1850 vnet_hw_interface_t *hi;
1851 vnet_sw_interface_t *si;
1857 si = vnet_get_sw_interface (vnm, sw_if_index);
1859 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1861 return clib_error_return (0, "%U: interface %U down",
1862 format_ip6_address, dst,
1863 format_vnet_sw_if_index_name, vnm,
1868 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1871 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1872 return clib_error_return
1873 (0, "no matching interface address for destination %U (interface %U)",
1874 format_ip6_address, dst,
1875 format_vnet_sw_if_index_name, vnm, sw_if_index);
1879 vlib_packet_template_get_packet (vm,
1880 &im->discover_neighbor_packet_template,
1883 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1885 /* Destination address is a solicited node multicast address. We need to fill in
1886 the low 24 bits with low 24 bits of target's address. */
1887 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1888 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1889 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1891 h->ip.src_address = src[0];
1892 h->neighbor.target_address = dst[0];
1894 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1895 vec_len (hi->hw_address));
1897 h->neighbor.icmp.checksum =
1898 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1899 ASSERT (bogus_length == 0);
1901 b = vlib_get_buffer (vm, bi);
1902 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1903 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1905 /* Add encapsulation string for software interface (e.g. ethernet header). */
1906 ip46_address_t nh = {
1910 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1911 VNET_LINK_IP6, &nh, sw_if_index);
1914 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1915 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1918 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1919 u32 *to_next = vlib_frame_vector_args (f);
1922 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1926 return /* no error */ 0;
1931 IP6_REWRITE_NEXT_DROP,
1932 IP6_REWRITE_NEXT_ICMP_ERROR,
1933 } ip6_rewrite_next_t;
1936 ip6_rewrite_inline (vlib_main_t * vm,
1937 vlib_node_runtime_t * node,
1938 vlib_frame_t * frame,
1939 int do_counters, int is_midchain, int is_mcast)
1941 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1942 u32 *from = vlib_frame_vector_args (frame);
1943 u32 n_left_from, n_left_to_next, *to_next, next_index;
1944 vlib_node_runtime_t *error_node =
1945 vlib_node_get_runtime (vm, ip6_input_node.index);
1947 n_left_from = frame->n_vectors;
1948 next_index = node->cached_next_index;
1949 u32 thread_index = vlib_get_thread_index ();
1951 while (n_left_from > 0)
1953 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1955 while (n_left_from >= 4 && n_left_to_next >= 2)
1957 ip_adjacency_t *adj0, *adj1;
1958 vlib_buffer_t *p0, *p1;
1959 ip6_header_t *ip0, *ip1;
1960 u32 pi0, rw_len0, next0, error0, adj_index0;
1961 u32 pi1, rw_len1, next1, error1, adj_index1;
1962 u32 tx_sw_if_index0, tx_sw_if_index1;
1964 /* Prefetch next iteration. */
1966 vlib_buffer_t *p2, *p3;
1968 p2 = vlib_get_buffer (vm, from[2]);
1969 p3 = vlib_get_buffer (vm, from[3]);
1971 vlib_prefetch_buffer_header (p2, LOAD);
1972 vlib_prefetch_buffer_header (p3, LOAD);
1974 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1975 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1977 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1978 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1981 pi0 = to_next[0] = from[0];
1982 pi1 = to_next[1] = from[1];
1987 n_left_to_next -= 2;
1989 p0 = vlib_get_buffer (vm, pi0);
1990 p1 = vlib_get_buffer (vm, pi1);
1992 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1993 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1995 ip0 = vlib_buffer_get_current (p0);
1996 ip1 = vlib_buffer_get_current (p1);
1998 error0 = error1 = IP6_ERROR_NONE;
1999 next0 = next1 = IP6_REWRITE_NEXT_DROP;
2001 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2003 i32 hop_limit0 = ip0->hop_limit;
2005 /* Input node should have reject packets with hop limit 0. */
2006 ASSERT (ip0->hop_limit > 0);
2010 ip0->hop_limit = hop_limit0;
2013 * If the hop count drops below 1 when forwarding, generate
2016 if (PREDICT_FALSE (hop_limit0 <= 0))
2018 error0 = IP6_ERROR_TIME_EXPIRED;
2019 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2020 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2021 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2022 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2028 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2030 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2032 i32 hop_limit1 = ip1->hop_limit;
2034 /* Input node should have reject packets with hop limit 0. */
2035 ASSERT (ip1->hop_limit > 0);
2039 ip1->hop_limit = hop_limit1;
2042 * If the hop count drops below 1 when forwarding, generate
2045 if (PREDICT_FALSE (hop_limit1 <= 0))
2047 error1 = IP6_ERROR_TIME_EXPIRED;
2048 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2049 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2050 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2051 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2057 p1->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2059 adj0 = adj_get (adj_index0);
2060 adj1 = adj_get (adj_index1);
2062 rw_len0 = adj0[0].rewrite_header.data_bytes;
2063 rw_len1 = adj1[0].rewrite_header.data_bytes;
2064 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2065 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
2069 vlib_increment_combined_counter
2070 (&adjacency_counters,
2071 thread_index, adj_index0, 1,
2072 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2073 vlib_increment_combined_counter
2074 (&adjacency_counters,
2075 thread_index, adj_index1, 1,
2076 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
2079 /* Check MTU of outgoing interface. */
2081 (vlib_buffer_length_in_chain (vm, p0) >
2083 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2086 (vlib_buffer_length_in_chain (vm, p1) >
2088 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2091 /* Don't adjust the buffer for hop count issue; icmp-error node
2092 * wants to see the IP headerr */
2093 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2095 p0->current_data -= rw_len0;
2096 p0->current_length += rw_len0;
2098 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2099 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2100 next0 = adj0[0].rewrite_header.next_index;
2103 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2104 vnet_feature_arc_start (lm->output_feature_arc_index,
2105 tx_sw_if_index0, &next0, p0);
2107 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2109 p1->current_data -= rw_len1;
2110 p1->current_length += rw_len1;
2112 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2113 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2114 next1 = adj1[0].rewrite_header.next_index;
2117 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2118 vnet_feature_arc_start (lm->output_feature_arc_index,
2119 tx_sw_if_index1, &next1, p1);
2122 /* Guess we are only writing on simple Ethernet header. */
2123 vnet_rewrite_two_headers (adj0[0], adj1[0],
2124 ip0, ip1, sizeof (ethernet_header_t));
2128 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2129 adj1->sub_type.midchain.fixup_func (vm, adj1, p1);
2134 * copy bytes from the IP address into the MAC rewrite
2136 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2137 vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1);
2140 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2141 to_next, n_left_to_next,
2142 pi0, pi1, next0, next1);
2145 while (n_left_from > 0 && n_left_to_next > 0)
2147 ip_adjacency_t *adj0;
2151 u32 adj_index0, next0, error0;
2152 u32 tx_sw_if_index0;
2154 pi0 = to_next[0] = from[0];
2156 p0 = vlib_get_buffer (vm, pi0);
2158 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2160 adj0 = adj_get (adj_index0);
2162 ip0 = vlib_buffer_get_current (p0);
2164 error0 = IP6_ERROR_NONE;
2165 next0 = IP6_REWRITE_NEXT_DROP;
2167 /* Check hop limit */
2168 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2170 i32 hop_limit0 = ip0->hop_limit;
2172 ASSERT (ip0->hop_limit > 0);
2176 ip0->hop_limit = hop_limit0;
2178 if (PREDICT_FALSE (hop_limit0 <= 0))
2181 * If the hop count drops below 1 when forwarding, generate
2184 error0 = IP6_ERROR_TIME_EXPIRED;
2185 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2186 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2187 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2188 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2194 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2197 /* Guess we are only writing on simple Ethernet header. */
2198 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2200 /* Update packet buffer attributes/set output interface. */
2201 rw_len0 = adj0[0].rewrite_header.data_bytes;
2202 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2206 vlib_increment_combined_counter
2207 (&adjacency_counters,
2208 thread_index, adj_index0, 1,
2209 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2212 /* Check MTU of outgoing interface. */
2214 (vlib_buffer_length_in_chain (vm, p0) >
2216 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2219 /* Don't adjust the buffer for hop count issue; icmp-error node
2220 * wants to see the IP headerr */
2221 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2223 p0->current_data -= rw_len0;
2224 p0->current_length += rw_len0;
2226 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2228 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2229 next0 = adj0[0].rewrite_header.next_index;
2232 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2233 vnet_feature_arc_start (lm->output_feature_arc_index,
2234 tx_sw_if_index0, &next0, p0);
2239 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2243 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2246 p0->error = error_node->errors[error0];
2251 n_left_to_next -= 1;
2253 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2254 to_next, n_left_to_next,
2258 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2261 /* Need to do trace after rewrites to pick up new packet data. */
2262 if (node->flags & VLIB_NODE_FLAG_TRACE)
2263 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2265 return frame->n_vectors;
2269 ip6_rewrite (vlib_main_t * vm,
2270 vlib_node_runtime_t * node, vlib_frame_t * frame)
2272 if (adj_are_counters_enabled ())
2273 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2275 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2279 ip6_rewrite_mcast (vlib_main_t * vm,
2280 vlib_node_runtime_t * node, vlib_frame_t * frame)
2282 if (adj_are_counters_enabled ())
2283 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2285 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2289 ip6_midchain (vlib_main_t * vm,
2290 vlib_node_runtime_t * node, vlib_frame_t * frame)
2292 if (adj_are_counters_enabled ())
2293 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2295 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2299 ip6_mcast_midchain (vlib_main_t * vm,
2300 vlib_node_runtime_t * node, vlib_frame_t * frame)
2302 if (adj_are_counters_enabled ())
2303 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2305 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2309 VLIB_REGISTER_NODE (ip6_midchain_node) =
2311 .function = ip6_midchain,
2312 .name = "ip6-midchain",
2313 .vector_size = sizeof (u32),
2314 .format_trace = format_ip6_forward_next_trace,
2315 .sibling_of = "ip6-rewrite",
2319 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2322 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2324 .function = ip6_rewrite,
2325 .name = "ip6-rewrite",
2326 .vector_size = sizeof (u32),
2327 .format_trace = format_ip6_rewrite_trace,
2331 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2332 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2337 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2340 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2342 .function = ip6_rewrite_mcast,
2343 .name = "ip6-rewrite-mcast",
2344 .vector_size = sizeof (u32),
2345 .format_trace = format_ip6_rewrite_trace,
2346 .sibling_of = "ip6-rewrite",
2350 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2353 VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
2355 .function = ip6_mcast_midchain,
2356 .name = "ip6-mcast-midchain",
2357 .vector_size = sizeof (u32),
2358 .format_trace = format_ip6_rewrite_trace,
2359 .sibling_of = "ip6-rewrite",
2363 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
2366 * Hop-by-Hop handling
2368 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2370 #define foreach_ip6_hop_by_hop_error \
2371 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2372 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2373 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2378 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2379 foreach_ip6_hop_by_hop_error
2381 IP6_HOP_BY_HOP_N_ERROR,
2382 } ip6_hop_by_hop_error_t;
2386 * Primary h-b-h handler trace support
2387 * We work pretty hard on the problem for obvious reasons
2393 u8 option_data[256];
2394 } ip6_hop_by_hop_trace_t;
2396 vlib_node_registration_t ip6_hop_by_hop_node;
2398 static char *ip6_hop_by_hop_error_strings[] = {
2399 #define _(sym,string) string,
2400 foreach_ip6_hop_by_hop_error
2405 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2407 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2408 int total_len = va_arg (*args, int);
2409 ip6_hop_by_hop_option_t *opt0, *limit0;
2410 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2413 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2414 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2416 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2417 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2419 while (opt0 < limit0)
2424 case 0: /* Pad, just stop */
2425 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2429 if (hm->trace[type0])
2431 s = (*hm->trace[type0]) (s, opt0);
2436 format (s, "\n unrecognized option %d length %d", type0,
2440 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2441 sizeof (ip6_hop_by_hop_option_t));
2449 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2451 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2452 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2453 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2454 ip6_hop_by_hop_header_t *hbh0;
2455 ip6_hop_by_hop_option_t *opt0, *limit0;
2456 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2460 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2462 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2463 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2465 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2466 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2468 while (opt0 < limit0)
2473 case 0: /* Pad, just stop */
2474 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2478 if (hm->trace[type0])
2480 s = (*hm->trace[type0]) (s, opt0);
2485 format (s, "\n unrecognized option %d length %d", type0,
2489 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2490 sizeof (ip6_hop_by_hop_option_t));
2498 ip6_scan_hbh_options (vlib_buffer_t * b0,
2500 ip6_hop_by_hop_header_t * hbh0,
2501 ip6_hop_by_hop_option_t * opt0,
2502 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2504 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2508 while (opt0 < limit0)
2514 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2519 if (hm->options[type0])
2521 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2523 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2529 /* Unrecognized mandatory option, check the two high order bits */
2530 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2532 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2534 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2535 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2536 *next0 = IP_LOOKUP_NEXT_DROP;
2538 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2539 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2540 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2541 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2542 ICMP6_parameter_problem_unrecognized_option,
2543 (u8 *) opt0 - (u8 *) ip0);
2545 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2546 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2547 if (!ip6_address_is_multicast (&ip0->dst_address))
2549 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2550 icmp6_error_set_vnet_buffer (b0,
2551 ICMP6_parameter_problem,
2552 ICMP6_parameter_problem_unrecognized_option,
2553 (u8 *) opt0 - (u8 *) ip0);
2557 *next0 = IP_LOOKUP_NEXT_DROP;
2565 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2566 sizeof (ip6_hop_by_hop_option_t));
2572 * Process the Hop-by-Hop Options header
2575 ip6_hop_by_hop (vlib_main_t * vm,
2576 vlib_node_runtime_t * node, vlib_frame_t * frame)
2578 vlib_node_runtime_t *error_node =
2579 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2580 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2581 u32 n_left_from, *from, *to_next;
2582 ip_lookup_next_t next_index;
2584 from = vlib_frame_vector_args (frame);
2585 n_left_from = frame->n_vectors;
2586 next_index = node->cached_next_index;
2588 while (n_left_from > 0)
2592 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2594 while (n_left_from >= 4 && n_left_to_next >= 2)
2597 vlib_buffer_t *b0, *b1;
2599 ip6_header_t *ip0, *ip1;
2600 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2601 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2602 u8 error0 = 0, error1 = 0;
2604 /* Prefetch next iteration. */
2606 vlib_buffer_t *p2, *p3;
2608 p2 = vlib_get_buffer (vm, from[2]);
2609 p3 = vlib_get_buffer (vm, from[3]);
2611 vlib_prefetch_buffer_header (p2, LOAD);
2612 vlib_prefetch_buffer_header (p3, LOAD);
2614 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2615 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2618 /* Speculatively enqueue b0, b1 to the current next frame */
2619 to_next[0] = bi0 = from[0];
2620 to_next[1] = bi1 = from[1];
2624 n_left_to_next -= 2;
2626 b0 = vlib_get_buffer (vm, bi0);
2627 b1 = vlib_get_buffer (vm, bi1);
2629 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2630 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2631 ip_adjacency_t *adj0 = adj_get (adj_index0);
2632 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2633 ip_adjacency_t *adj1 = adj_get (adj_index1);
2635 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2636 next0 = adj0->lookup_next_index;
2637 next1 = adj1->lookup_next_index;
2639 ip0 = vlib_buffer_get_current (b0);
2640 ip1 = vlib_buffer_get_current (b1);
2641 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2642 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2643 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2644 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2646 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2647 ((hbh0->length + 1) << 3));
2649 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2650 ((hbh1->length + 1) << 3));
2653 * Basic validity checks
2655 if ((hbh0->length + 1) << 3 >
2656 clib_net_to_host_u16 (ip0->payload_length))
2658 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2659 next0 = IP_LOOKUP_NEXT_DROP;
2662 /* Scan the set of h-b-h options, process ones that we understand */
2663 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2665 if ((hbh1->length + 1) << 3 >
2666 clib_net_to_host_u16 (ip1->payload_length))
2668 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2669 next1 = IP_LOOKUP_NEXT_DROP;
2672 /* Scan the set of h-b-h options, process ones that we understand */
2673 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2676 /* Has the classifier flagged this buffer for special treatment? */
2679 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2680 next0 = hm->next_override;
2682 /* Has the classifier flagged this buffer for special treatment? */
2685 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2686 next1 = hm->next_override;
2688 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2690 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2692 ip6_hop_by_hop_trace_t *t =
2693 vlib_add_trace (vm, node, b0, sizeof (*t));
2694 u32 trace_len = (hbh0->length + 1) << 3;
2695 t->next_index = next0;
2696 /* Capture the h-b-h option verbatim */
2699 ARRAY_LEN (t->option_data) ? trace_len :
2700 ARRAY_LEN (t->option_data);
2701 t->trace_len = trace_len;
2702 clib_memcpy (t->option_data, hbh0, trace_len);
2704 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2706 ip6_hop_by_hop_trace_t *t =
2707 vlib_add_trace (vm, node, b1, sizeof (*t));
2708 u32 trace_len = (hbh1->length + 1) << 3;
2709 t->next_index = next1;
2710 /* Capture the h-b-h option verbatim */
2713 ARRAY_LEN (t->option_data) ? trace_len :
2714 ARRAY_LEN (t->option_data);
2715 t->trace_len = trace_len;
2716 clib_memcpy (t->option_data, hbh1, trace_len);
2721 b0->error = error_node->errors[error0];
2722 b1->error = error_node->errors[error1];
2724 /* verify speculative enqueue, maybe switch current next frame */
2725 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2726 n_left_to_next, bi0, bi1, next0,
2730 while (n_left_from > 0 && n_left_to_next > 0)
2736 ip6_hop_by_hop_header_t *hbh0;
2737 ip6_hop_by_hop_option_t *opt0, *limit0;
2740 /* Speculatively enqueue b0 to the current next frame */
2746 n_left_to_next -= 1;
2748 b0 = vlib_get_buffer (vm, bi0);
2750 * Default use the next_index from the adjacency.
2751 * A HBH option rarely redirects to a different node
2753 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2754 ip_adjacency_t *adj0 = adj_get (adj_index0);
2755 next0 = adj0->lookup_next_index;
2757 ip0 = vlib_buffer_get_current (b0);
2758 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2759 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2761 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2762 ((hbh0->length + 1) << 3));
2765 * Basic validity checks
2767 if ((hbh0->length + 1) << 3 >
2768 clib_net_to_host_u16 (ip0->payload_length))
2770 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2771 next0 = IP_LOOKUP_NEXT_DROP;
2775 /* Scan the set of h-b-h options, process ones that we understand */
2776 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2779 /* Has the classifier flagged this buffer for special treatment? */
2782 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2783 next0 = hm->next_override;
2785 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2787 ip6_hop_by_hop_trace_t *t =
2788 vlib_add_trace (vm, node, b0, sizeof (*t));
2789 u32 trace_len = (hbh0->length + 1) << 3;
2790 t->next_index = next0;
2791 /* Capture the h-b-h option verbatim */
2794 ARRAY_LEN (t->option_data) ? trace_len :
2795 ARRAY_LEN (t->option_data);
2796 t->trace_len = trace_len;
2797 clib_memcpy (t->option_data, hbh0, trace_len);
2800 b0->error = error_node->errors[error0];
2802 /* verify speculative enqueue, maybe switch current next frame */
2803 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2804 n_left_to_next, bi0, next0);
2806 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2808 return frame->n_vectors;
2812 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2814 .function = ip6_hop_by_hop,
2815 .name = "ip6-hop-by-hop",
2816 .sibling_of = "ip6-lookup",
2817 .vector_size = sizeof (u32),
2818 .format_trace = format_ip6_hop_by_hop_trace,
2819 .type = VLIB_NODE_TYPE_INTERNAL,
2820 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2821 .error_strings = ip6_hop_by_hop_error_strings,
2826 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2828 static clib_error_t *
2829 ip6_hop_by_hop_init (vlib_main_t * vm)
2831 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2832 memset (hm->options, 0, sizeof (hm->options));
2833 memset (hm->trace, 0, sizeof (hm->trace));
2834 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2838 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2841 ip6_hbh_set_next_override (uword next)
2843 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2845 hm->next_override = next;
2849 ip6_hbh_register_option (u8 option,
2850 int options (vlib_buffer_t * b, ip6_header_t * ip,
2851 ip6_hop_by_hop_option_t * opt),
2852 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2854 ip6_main_t *im = &ip6_main;
2855 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2857 ASSERT (option < ARRAY_LEN (hm->options));
2859 /* Already registered */
2860 if (hm->options[option])
2863 hm->options[option] = options;
2864 hm->trace[option] = trace;
2866 /* Set global variable */
2867 im->hbh_enabled = 1;
2873 ip6_hbh_unregister_option (u8 option)
2875 ip6_main_t *im = &ip6_main;
2876 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2878 ASSERT (option < ARRAY_LEN (hm->options));
2880 /* Not registered */
2881 if (!hm->options[option])
2884 hm->options[option] = NULL;
2885 hm->trace[option] = NULL;
2887 /* Disable global knob if this was the last option configured */
2890 for (i = 0; i < 256; i++)
2892 if (hm->options[option])
2899 im->hbh_enabled = 0;
2904 /* Global IP6 main. */
2905 ip6_main_t ip6_main;
2907 static clib_error_t *
2908 ip6_lookup_init (vlib_main_t * vm)
2910 ip6_main_t *im = &ip6_main;
2911 clib_error_t *error;
2914 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2917 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2924 for (j = 0; j < i0; j++)
2925 im->fib_masks[i].as_u32[j] = ~0;
2928 im->fib_masks[i].as_u32[i0] =
2929 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2932 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2934 if (im->lookup_table_nbuckets == 0)
2935 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2937 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2939 if (im->lookup_table_size == 0)
2940 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2942 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2943 "ip6 FIB fwding table",
2944 im->lookup_table_nbuckets, im->lookup_table_size);
2945 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2946 "ip6 FIB non-fwding table",
2947 im->lookup_table_nbuckets, im->lookup_table_size);
2949 /* Create FIB with index 0 and table id of 0. */
2950 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2951 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2955 pn = pg_get_node (ip6_lookup_node.index);
2956 pn->unformat_edit = unformat_pg_ip6_header;
2959 /* Unless explicitly configured, don't process HBH options */
2960 im->hbh_enabled = 0;
2963 icmp6_neighbor_solicitation_header_t p;
2965 memset (&p, 0, sizeof (p));
2967 p.ip.ip_version_traffic_class_and_flow_label =
2968 clib_host_to_net_u32 (0x6 << 28);
2969 p.ip.payload_length =
2970 clib_host_to_net_u16 (sizeof (p) -
2972 (icmp6_neighbor_solicitation_header_t, neighbor));
2973 p.ip.protocol = IP_PROTOCOL_ICMP6;
2974 p.ip.hop_limit = 255;
2975 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2977 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2979 p.link_layer_option.header.type =
2980 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2981 p.link_layer_option.header.n_data_u64s =
2982 sizeof (p.link_layer_option) / sizeof (u64);
2984 vlib_packet_template_init (vm,
2985 &im->discover_neighbor_packet_template,
2987 /* alloc chunk size */ 8,
2988 "ip6 neighbor discovery");
2994 VLIB_INIT_FUNCTION (ip6_lookup_init);
2996 static clib_error_t *
2997 add_del_ip6_interface_table (vlib_main_t * vm,
2998 unformat_input_t * input,
2999 vlib_cli_command_t * cmd)
3001 vnet_main_t *vnm = vnet_get_main ();
3002 ip_interface_address_t *ia;
3003 clib_error_t *error = 0;
3004 u32 sw_if_index, table_id;
3008 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3010 error = clib_error_return (0, "unknown interface `%U'",
3011 format_unformat_error, input);
3015 if (unformat (input, "%d", &table_id))
3019 error = clib_error_return (0, "expected table id `%U'",
3020 format_unformat_error, input);
3025 * If the interface already has in IP address, then a change int
3026 * VRF is not allowed. The IP address applied must first be removed.
3027 * We do not do that automatically here, since VPP has no knowledge
3028 * of whether thoses subnets are valid in the destination VRF.
3031 foreach_ip_interface_address (&ip6_main.lookup_main,
3033 1 /* honor unnumbered */,
3037 a = ip_interface_address_get_address (&ip6_main.lookup_main, ia);
3038 error = clib_error_return (0, "interface %U has address %U",
3039 format_vnet_sw_if_index_name, vnm,
3041 format_ip6_address, a);
3047 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3050 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
3051 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
3053 fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3056 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
3057 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = fib_index;
3066 * Place the indicated interface into the supplied IPv6 FIB table (also known
3067 * as a VRF). If the FIB table does not exist, this command creates it. To
3068 * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
3069 * FIB table will only be displayed if a route has been added to the table, or
3070 * an IP Address is assigned to an interface in the table (which adds a route
3073 * @note IP addresses added after setting the interface IP table are added to
3074 * the indicated FIB table. If an IP address is added prior to changing the
3075 * table then this is an error. The control plane must remove these addresses
3076 * first and then change the table. VPP will not automatically move the
3077 * addresses from the old to the new table as it does not know the validity
3081 * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
3082 * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
3085 VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
3087 .path = "set interface ip6 table",
3088 .function = add_del_ip6_interface_table,
3089 .short_help = "set interface ip6 table <interface> <table-id>"
3094 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
3097 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3098 /* Invert the "u" bit */
3099 ip->as_u8[8] = mac[0] ^ (1 << 1);
3100 ip->as_u8[9] = mac[1];
3101 ip->as_u8[10] = mac[2];
3102 ip->as_u8[11] = 0xFF;
3103 ip->as_u8[12] = 0xFE;
3104 ip->as_u8[13] = mac[3];
3105 ip->as_u8[14] = mac[4];
3106 ip->as_u8[15] = mac[5];
3110 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3113 /* Invert the previously inverted "u" bit */
3114 mac[0] = ip->as_u8[8] ^ (1 << 1);
3115 mac[1] = ip->as_u8[9];
3116 mac[2] = ip->as_u8[10];
3117 mac[3] = ip->as_u8[13];
3118 mac[4] = ip->as_u8[14];
3119 mac[5] = ip->as_u8[15];
3122 static clib_error_t *
3123 test_ip6_link_command_fn (vlib_main_t * vm,
3124 unformat_input_t * input, vlib_cli_command_t * cmd)
3127 ip6_address_t _a, *a = &_a;
3129 if (unformat (input, "%U", unformat_ethernet_address, mac))
3131 ip6_link_local_address_from_ethernet_mac_address (a, mac);
3132 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
3133 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3134 vlib_cli_output (vm, "Original MAC address: %U",
3135 format_ethernet_address, mac);
3142 * This command converts the given MAC Address into an IPv6 link-local
3146 * Example of how to create an IPv6 link-local address:
3147 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3148 * Link local address: fe80::14d9:e0ff:fe91:7986
3149 * Original MAC address: 16:d9:e0:91:79:86
3153 VLIB_CLI_COMMAND (test_link_command, static) =
3155 .path = "test ip6 link",
3156 .function = test_ip6_link_command_fn,
3157 .short_help = "test ip6 link <mac-address>",
3162 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3166 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
3168 if (~0 == fib_index)
3169 return VNET_API_ERROR_NO_SUCH_FIB;
3171 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
3177 static clib_error_t *
3178 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3179 unformat_input_t * input,
3180 vlib_cli_command_t * cmd)
3184 u32 flow_hash_config = 0;
3187 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3189 if (unformat (input, "table %d", &table_id))
3192 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3193 foreach_flow_hash_bit
3200 return clib_error_return (0, "unknown input `%U'",
3201 format_unformat_error, input);
3203 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3210 return clib_error_return (0, "no such FIB table %d", table_id);
3213 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3221 * Configure the set of IPv6 fields used by the flow hash.
3225 * Example of how to set the flow hash on a given table:
3226 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3228 * Example of display the configured flow hash:
3229 * @cliexstart{show ip6 fib}
3230 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3233 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3234 * [0] [@0]: dpo-drop ip6
3237 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3238 * [0] [@2]: dpo-receive
3241 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3242 * [0] [@2]: dpo-receive
3245 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3246 * [0] [@2]: dpo-receive
3249 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3250 * [0] [@2]: dpo-receive
3251 * ff02::1:ff00:0/104
3253 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3254 * [0] [@2]: dpo-receive
3255 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3258 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3259 * [0] [@0]: dpo-drop ip6
3262 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3263 * [0] [@4]: ipv6-glean: af_packet0
3266 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3267 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3270 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3271 * [0] [@2]: dpo-receive
3272 * fe80::fe:3eff:fe3e:9222/128
3274 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3275 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3278 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3279 * [0] [@2]: dpo-receive
3282 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3283 * [0] [@2]: dpo-receive
3286 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3287 * [0] [@2]: dpo-receive
3288 * ff02::1:ff00:0/104
3290 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3291 * [0] [@2]: dpo-receive
3296 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3298 .path = "set ip6 flow-hash",
3300 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3301 .function = set_ip6_flow_hash_command_fn,
3305 static clib_error_t *
3306 show_ip6_local_command_fn (vlib_main_t * vm,
3307 unformat_input_t * input, vlib_cli_command_t * cmd)
3309 ip6_main_t *im = &ip6_main;
3310 ip_lookup_main_t *lm = &im->lookup_main;
3313 vlib_cli_output (vm, "Protocols handled by ip6_local");
3314 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3316 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3317 vlib_cli_output (vm, "%d", i);
3325 * Display the set of protocols handled by the local IPv6 stack.
3328 * Example of how to display local protocol table:
3329 * @cliexstart{show ip6 local}
3330 * Protocols handled by ip6_local
3338 VLIB_CLI_COMMAND (show_ip6_local, static) =
3340 .path = "show ip6 local",
3341 .function = show_ip6_local_command_fn,
3342 .short_help = "show ip6 local",
3347 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3350 vnet_main_t *vnm = vnet_get_main ();
3351 vnet_interface_main_t *im = &vnm->interface_main;
3352 ip6_main_t *ipm = &ip6_main;
3353 ip_lookup_main_t *lm = &ipm->lookup_main;
3354 vnet_classify_main_t *cm = &vnet_classify_main;
3355 ip6_address_t *if_addr;
3357 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3358 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3360 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3361 return VNET_API_ERROR_NO_SUCH_ENTRY;
3363 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3364 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3366 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3368 if (NULL != if_addr)
3370 fib_prefix_t pfx = {
3372 .fp_proto = FIB_PROTOCOL_IP6,
3373 .fp_addr.ip6 = *if_addr,
3377 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3381 if (table_index != (u32) ~ 0)
3383 dpo_id_t dpo = DPO_INVALID;
3388 classify_dpo_create (DPO_PROTO_IP6, table_index));
3390 fib_table_entry_special_dpo_add (fib_index,
3392 FIB_SOURCE_CLASSIFY,
3393 FIB_ENTRY_FLAG_NONE, &dpo);
3398 fib_table_entry_special_remove (fib_index,
3399 &pfx, FIB_SOURCE_CLASSIFY);
3406 static clib_error_t *
3407 set_ip6_classify_command_fn (vlib_main_t * vm,
3408 unformat_input_t * input,
3409 vlib_cli_command_t * cmd)
3411 u32 table_index = ~0;
3412 int table_index_set = 0;
3413 u32 sw_if_index = ~0;
3416 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3418 if (unformat (input, "table-index %d", &table_index))
3419 table_index_set = 1;
3420 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3421 vnet_get_main (), &sw_if_index))
3427 if (table_index_set == 0)
3428 return clib_error_return (0, "classify table-index must be specified");
3430 if (sw_if_index == ~0)
3431 return clib_error_return (0, "interface / subif must be specified");
3433 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3440 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3441 return clib_error_return (0, "No such interface");
3443 case VNET_API_ERROR_NO_SUCH_ENTRY:
3444 return clib_error_return (0, "No such classifier table");
3450 * Assign a classification table to an interface. The classification
3451 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3452 * commands. Once the table is create, use this command to filter packets
3456 * Example of how to assign a classification table to an interface:
3457 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3460 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3462 .path = "set ip6 classify",
3464 "set ip6 classify intfc <interface> table-index <classify-idx>",
3465 .function = set_ip6_classify_command_fn,
3469 static clib_error_t *
3470 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3472 ip6_main_t *im = &ip6_main;
3477 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3479 if (unformat (input, "hash-buckets %d", &tmp))
3481 else if (unformat (input, "heap-size %dm", &tmp))
3482 heapsize = ((u64) tmp) << 20;
3483 else if (unformat (input, "heap-size %dM", &tmp))
3484 heapsize = ((u64) tmp) << 20;
3485 else if (unformat (input, "heap-size %dg", &tmp))
3486 heapsize = ((u64) tmp) << 30;
3487 else if (unformat (input, "heap-size %dG", &tmp))
3488 heapsize = ((u64) tmp) << 30;
3490 return clib_error_return (0, "unknown input '%U'",
3491 format_unformat_error, input);
3494 im->lookup_table_nbuckets = nbuckets;
3495 im->lookup_table_size = heapsize;
3500 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3503 * fd.io coding-style-patch-verification: ON
3506 * eval: (c-set-style "gnu")