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.
66 ip6_lookup_inline (vlib_main_t * vm,
67 vlib_node_runtime_t * node, vlib_frame_t * frame)
69 ip6_main_t *im = &ip6_main;
70 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_to_counters;
71 u32 n_left_from, n_left_to_next, *from, *to_next;
72 ip_lookup_next_t next;
73 u32 thread_index = vlib_get_thread_index ();
75 from = vlib_frame_vector_args (frame);
76 n_left_from = frame->n_vectors;
77 next = node->cached_next_index;
79 while (n_left_from > 0)
81 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
83 while (n_left_from >= 4 && n_left_to_next >= 2)
85 vlib_buffer_t *p0, *p1;
86 u32 pi0, pi1, lbi0, lbi1, wrong_next;
87 ip_lookup_next_t next0, next1;
88 ip6_header_t *ip0, *ip1;
89 ip6_address_t *dst_addr0, *dst_addr1;
90 u32 fib_index0, fib_index1;
91 u32 flow_hash_config0, flow_hash_config1;
92 const dpo_id_t *dpo0, *dpo1;
93 const load_balance_t *lb0, *lb1;
95 /* Prefetch next iteration. */
97 vlib_buffer_t *p2, *p3;
99 p2 = vlib_get_buffer (vm, from[2]);
100 p3 = vlib_get_buffer (vm, from[3]);
102 vlib_prefetch_buffer_header (p2, LOAD);
103 vlib_prefetch_buffer_header (p3, LOAD);
104 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
105 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
108 pi0 = to_next[0] = from[0];
109 pi1 = to_next[1] = from[1];
111 p0 = vlib_get_buffer (vm, pi0);
112 p1 = vlib_get_buffer (vm, pi1);
114 ip0 = vlib_buffer_get_current (p0);
115 ip1 = vlib_buffer_get_current (p1);
117 dst_addr0 = &ip0->dst_address;
118 dst_addr1 = &ip1->dst_address;
121 vec_elt (im->fib_index_by_sw_if_index,
122 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
124 vec_elt (im->fib_index_by_sw_if_index,
125 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
127 fib_index0 = (vnet_buffer (p0)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
128 fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
129 fib_index1 = (vnet_buffer (p1)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
130 fib_index1 : vnet_buffer (p1)->sw_if_index[VLIB_TX];
132 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
133 lbi1 = ip6_fib_table_fwding_lookup (im, fib_index1, dst_addr1);
135 lb0 = load_balance_get (lbi0);
136 lb1 = load_balance_get (lbi1);
137 ASSERT (lb0->lb_n_buckets > 0);
138 ASSERT (lb1->lb_n_buckets > 0);
139 ASSERT (is_pow2 (lb0->lb_n_buckets));
140 ASSERT (is_pow2 (lb1->lb_n_buckets));
142 vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
144 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
146 flow_hash_config0 = lb0->lb_hash_config;
147 vnet_buffer (p0)->ip.flow_hash =
148 ip6_compute_flow_hash (ip0, flow_hash_config0);
150 load_balance_get_fwd_bucket (lb0,
151 (vnet_buffer (p0)->ip.flow_hash &
152 (lb0->lb_n_buckets_minus_1)));
156 dpo0 = load_balance_get_bucket_i (lb0, 0);
158 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
160 flow_hash_config1 = lb1->lb_hash_config;
161 vnet_buffer (p1)->ip.flow_hash =
162 ip6_compute_flow_hash (ip1, flow_hash_config1);
164 load_balance_get_fwd_bucket (lb1,
165 (vnet_buffer (p1)->ip.flow_hash &
166 (lb1->lb_n_buckets_minus_1)));
170 dpo1 = load_balance_get_bucket_i (lb1, 0);
172 next0 = dpo0->dpoi_next_node;
173 next1 = dpo1->dpoi_next_node;
175 /* Only process the HBH Option Header if explicitly configured to do so */
177 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
179 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
180 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
183 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
185 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
186 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
188 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
189 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
191 vlib_increment_combined_counter
192 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
193 vlib_increment_combined_counter
194 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
201 wrong_next = (next0 != next) + 2 * (next1 != next);
202 if (PREDICT_FALSE (wrong_next != 0))
211 vlib_set_next_frame_buffer (vm, node, next0, pi0);
218 vlib_set_next_frame_buffer (vm, node, next1, pi1);
225 vlib_set_next_frame_buffer (vm, node, next0, pi0);
226 vlib_set_next_frame_buffer (vm, node, next1, pi1);
230 vlib_put_next_frame (vm, node, next, n_left_to_next);
232 vlib_get_next_frame (vm, node, next, to_next,
239 while (n_left_from > 0 && n_left_to_next > 0)
244 ip_lookup_next_t next0;
246 ip6_address_t *dst_addr0;
247 u32 fib_index0, flow_hash_config0;
248 const dpo_id_t *dpo0;
253 p0 = vlib_get_buffer (vm, pi0);
255 ip0 = vlib_buffer_get_current (p0);
257 dst_addr0 = &ip0->dst_address;
260 vec_elt (im->fib_index_by_sw_if_index,
261 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
263 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
264 (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
266 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
268 lb0 = load_balance_get (lbi0);
269 flow_hash_config0 = lb0->lb_hash_config;
271 vnet_buffer (p0)->ip.flow_hash = 0;
272 ASSERT (lb0->lb_n_buckets > 0);
273 ASSERT (is_pow2 (lb0->lb_n_buckets));
275 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
277 flow_hash_config0 = lb0->lb_hash_config;
278 vnet_buffer (p0)->ip.flow_hash =
279 ip6_compute_flow_hash (ip0, flow_hash_config0);
281 load_balance_get_fwd_bucket (lb0,
282 (vnet_buffer (p0)->ip.flow_hash &
283 (lb0->lb_n_buckets_minus_1)));
287 dpo0 = load_balance_get_bucket_i (lb0, 0);
290 dpo0 = load_balance_get_bucket_i (lb0,
291 (vnet_buffer (p0)->ip.flow_hash &
292 lb0->lb_n_buckets_minus_1));
293 next0 = dpo0->dpoi_next_node;
295 /* Only process the HBH Option Header if explicitly configured to do so */
297 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
299 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
300 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
302 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
304 vlib_increment_combined_counter
305 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
312 if (PREDICT_FALSE (next0 != next))
315 vlib_put_next_frame (vm, node, next, n_left_to_next);
317 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
324 vlib_put_next_frame (vm, node, next, n_left_to_next);
327 if (node->flags & VLIB_NODE_FLAG_TRACE)
328 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
330 return frame->n_vectors;
334 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
335 ip6_main_t * im, u32 fib_index,
336 ip_interface_address_t * a)
338 ip_lookup_main_t *lm = &im->lookup_main;
339 ip6_address_t *address = ip_interface_address_get_address (lm, a);
341 .fp_len = a->address_length,
342 .fp_proto = FIB_PROTOCOL_IP6,
343 .fp_addr.ip6 = *address,
346 if (a->address_length < 128)
348 fib_table_entry_update_one_path (fib_index,
350 FIB_SOURCE_INTERFACE,
351 (FIB_ENTRY_FLAG_CONNECTED |
352 FIB_ENTRY_FLAG_ATTACHED),
354 /* No next-hop address */
356 /* invalid FIB index */
359 NULL, FIB_ROUTE_PATH_FLAG_NONE);
363 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
365 u32 classify_table_index =
366 lm->classify_table_index_by_sw_if_index[sw_if_index];
367 if (classify_table_index != (u32) ~ 0)
369 dpo_id_t dpo = DPO_INVALID;
374 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
376 fib_table_entry_special_dpo_add (fib_index,
379 FIB_ENTRY_FLAG_NONE, &dpo);
384 fib_table_entry_update_one_path (fib_index, &pfx,
385 FIB_SOURCE_INTERFACE,
386 (FIB_ENTRY_FLAG_CONNECTED |
387 FIB_ENTRY_FLAG_LOCAL),
391 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
395 ip6_del_interface_routes (ip6_main_t * im,
397 ip6_address_t * address, u32 address_length)
400 .fp_len = address_length,
401 .fp_proto = FIB_PROTOCOL_IP6,
402 .fp_addr.ip6 = *address,
405 if (pfx.fp_len < 128)
407 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
412 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
416 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
418 ip6_main_t *im = &ip6_main;
420 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
423 * enable/disable only on the 1<->0 transition
427 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
432 /* The ref count is 0 when an address is removed from an interface that has
433 * no address - this is not a ciritical error */
434 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
435 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
439 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
442 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
443 sw_if_index, !is_enable, 0, 0);
446 /* get first interface address */
448 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
450 ip_lookup_main_t *lm = &im->lookup_main;
451 ip_interface_address_t *ia = 0;
452 ip6_address_t *result = 0;
455 foreach_ip_interface_address (lm, ia, sw_if_index,
456 1 /* honor unnumbered */,
458 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
467 ip6_add_del_interface_address (vlib_main_t * vm,
469 ip6_address_t * address,
470 u32 address_length, u32 is_del)
472 vnet_main_t *vnm = vnet_get_main ();
473 ip6_main_t *im = &ip6_main;
474 ip_lookup_main_t *lm = &im->lookup_main;
476 u32 if_address_index;
477 ip6_address_fib_t ip6_af, *addr_fib = 0;
479 /* local0 interface doesn't support IP addressing */
480 if (sw_if_index == 0)
483 clib_error_create ("local0 interface doesn't support IP addressing");
486 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
487 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
489 ip6_addr_fib_init (&ip6_af, address,
490 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
491 vec_add1 (addr_fib, ip6_af);
494 uword elts_before = pool_elts (lm->if_address_pool);
496 error = ip_interface_address_add_del
497 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
501 /* Pool did not grow: add duplicate address. */
502 if (elts_before == pool_elts (lm->if_address_pool))
506 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
509 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
511 ip6_add_interface_routes (vnm, sw_if_index,
512 im, ip6_af.fib_index,
513 pool_elt_at_index (lm->if_address_pool,
517 ip6_add_del_interface_address_callback_t *cb;
518 vec_foreach (cb, im->add_del_interface_address_callbacks)
519 cb->function (im, cb->function_opaque, sw_if_index,
520 address, address_length, if_address_index, is_del);
529 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
531 ip6_main_t *im = &ip6_main;
532 ip_interface_address_t *ia;
534 u32 is_admin_up, fib_index;
536 /* Fill in lookup tables with default table (0). */
537 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
539 vec_validate_init_empty (im->
540 lookup_main.if_address_pool_index_by_sw_if_index,
543 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
545 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
548 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
549 0 /* honor unnumbered */,
551 a = ip_interface_address_get_address (&im->lookup_main, ia);
553 ip6_add_interface_routes (vnm, sw_if_index,
557 ip6_del_interface_routes (im, fib_index,
558 a, ia->address_length);
565 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
567 /* Built-in ip6 unicast rx feature path definition */
569 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
571 .arc_name = "ip6-unicast",
572 .start_nodes = VNET_FEATURES ("ip6-input"),
573 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
576 VNET_FEATURE_INIT (ip6_flow_classify, static) =
578 .arc_name = "ip6-unicast",
579 .node_name = "ip6-flow-classify",
580 .runs_before = VNET_FEATURES ("ip6-inacl"),
583 VNET_FEATURE_INIT (ip6_inacl, static) =
585 .arc_name = "ip6-unicast",
586 .node_name = "ip6-inacl",
587 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
590 VNET_FEATURE_INIT (ip6_policer_classify, static) =
592 .arc_name = "ip6-unicast",
593 .node_name = "ip6-policer-classify",
594 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
597 VNET_FEATURE_INIT (ip6_ipsec, static) =
599 .arc_name = "ip6-unicast",
600 .node_name = "ipsec-input-ip6",
601 .runs_before = VNET_FEATURES ("l2tp-decap"),
604 VNET_FEATURE_INIT (ip6_l2tp, static) =
606 .arc_name = "ip6-unicast",
607 .node_name = "l2tp-decap",
608 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
611 VNET_FEATURE_INIT (ip6_vpath, static) =
613 .arc_name = "ip6-unicast",
614 .node_name = "vpath-input-ip6",
615 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
618 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
620 .arc_name = "ip6-unicast",
621 .node_name = "ip6-vxlan-bypass",
622 .runs_before = VNET_FEATURES ("ip6-lookup"),
625 VNET_FEATURE_INIT (ip6_not_enabled, static) =
627 .arc_name = "ip6-unicast",
628 .node_name = "ip6-not-enabled",
629 .runs_before = VNET_FEATURES ("ip6-lookup"),
632 VNET_FEATURE_INIT (ip6_lookup, static) =
634 .arc_name = "ip6-unicast",
635 .node_name = "ip6-lookup",
636 .runs_before = 0, /*last feature*/
639 /* Built-in ip6 multicast rx feature path definition (none now) */
640 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
642 .arc_name = "ip6-multicast",
643 .start_nodes = VNET_FEATURES ("ip6-input"),
644 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
647 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
648 .arc_name = "ip6-multicast",
649 .node_name = "vpath-input-ip6",
650 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
653 VNET_FEATURE_INIT (ip6_not_enabled_mc, static) = {
654 .arc_name = "ip6-multicast",
655 .node_name = "ip6-not-enabled",
656 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
659 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
660 .arc_name = "ip6-multicast",
661 .node_name = "ip6-mfib-forward-lookup",
662 .runs_before = 0, /* last feature */
665 /* Built-in ip4 tx feature path definition */
666 VNET_FEATURE_ARC_INIT (ip6_output, static) =
668 .arc_name = "ip6-output",
669 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain", "ip6-dvr-dpo"),
670 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
673 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
674 .arc_name = "ip6-output",
675 .node_name = "ipsec-output-ip6",
676 .runs_before = VNET_FEATURES ("interface-output"),
679 VNET_FEATURE_INIT (ip6_interface_output, static) = {
680 .arc_name = "ip6-output",
681 .node_name = "interface-output",
682 .runs_before = 0, /* not before any other features */
687 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
689 ip6_main_t *im = &ip6_main;
691 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
692 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
696 /* Ensure that IPv6 is disabled */
697 ip6_main_t *im6 = &ip6_main;
698 ip_lookup_main_t *lm6 = &im6->lookup_main;
699 ip_interface_address_t *ia = 0;
700 ip6_address_t *address;
701 vlib_main_t *vm = vlib_get_main ();
703 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0 /* is_add */ );
705 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* honor unnumbered */,
707 address = ip_interface_address_get_address (lm6, ia);
708 ip6_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1);
711 ip6_mfib_interface_enable_disable (sw_if_index, 0);
714 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
717 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
718 sw_if_index, is_add, 0, 0);
720 return /* no error */ 0;
723 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
726 ip6_lookup (vlib_main_t * vm,
727 vlib_node_runtime_t * node, vlib_frame_t * frame)
729 return ip6_lookup_inline (vm, node, frame);
732 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
735 VLIB_REGISTER_NODE (ip6_lookup_node) =
737 .function = ip6_lookup,
738 .name = "ip6-lookup",
739 .vector_size = sizeof (u32),
740 .format_trace = format_ip6_lookup_trace,
741 .n_next_nodes = IP6_LOOKUP_N_NEXT,
742 .next_nodes = IP6_LOOKUP_NEXT_NODES,
746 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
749 ip6_load_balance (vlib_main_t * vm,
750 vlib_node_runtime_t * node, vlib_frame_t * frame)
752 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
753 u32 n_left_from, n_left_to_next, *from, *to_next;
754 ip_lookup_next_t next;
755 u32 thread_index = vlib_get_thread_index ();
756 ip6_main_t *im = &ip6_main;
758 from = vlib_frame_vector_args (frame);
759 n_left_from = frame->n_vectors;
760 next = node->cached_next_index;
762 if (node->flags & VLIB_NODE_FLAG_TRACE)
763 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
765 while (n_left_from > 0)
767 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
770 while (n_left_from >= 4 && n_left_to_next >= 2)
772 ip_lookup_next_t next0, next1;
773 const load_balance_t *lb0, *lb1;
774 vlib_buffer_t *p0, *p1;
775 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
776 const ip6_header_t *ip0, *ip1;
777 const dpo_id_t *dpo0, *dpo1;
779 /* Prefetch next iteration. */
781 vlib_buffer_t *p2, *p3;
783 p2 = vlib_get_buffer (vm, from[2]);
784 p3 = vlib_get_buffer (vm, from[3]);
786 vlib_prefetch_buffer_header (p2, STORE);
787 vlib_prefetch_buffer_header (p3, STORE);
789 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
790 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
793 pi0 = to_next[0] = from[0];
794 pi1 = to_next[1] = from[1];
801 p0 = vlib_get_buffer (vm, pi0);
802 p1 = vlib_get_buffer (vm, pi1);
804 ip0 = vlib_buffer_get_current (p0);
805 ip1 = vlib_buffer_get_current (p1);
806 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
807 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
809 lb0 = load_balance_get (lbi0);
810 lb1 = load_balance_get (lbi1);
813 * this node is for via FIBs we can re-use the hash value from the
814 * to node if present.
815 * We don't want to use the same hash value at each level in the recursion
816 * graph as that would lead to polarisation
820 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
822 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
824 hc0 = vnet_buffer (p0)->ip.flow_hash =
825 vnet_buffer (p0)->ip.flow_hash >> 1;
829 hc0 = vnet_buffer (p0)->ip.flow_hash =
830 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
833 load_balance_get_fwd_bucket (lb0,
835 lb0->lb_n_buckets_minus_1));
839 dpo0 = load_balance_get_bucket_i (lb0, 0);
841 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
843 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
845 hc1 = vnet_buffer (p1)->ip.flow_hash =
846 vnet_buffer (p1)->ip.flow_hash >> 1;
850 hc1 = vnet_buffer (p1)->ip.flow_hash =
851 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
854 load_balance_get_fwd_bucket (lb1,
856 lb1->lb_n_buckets_minus_1));
860 dpo1 = load_balance_get_bucket_i (lb1, 0);
863 next0 = dpo0->dpoi_next_node;
864 next1 = dpo1->dpoi_next_node;
866 /* Only process the HBH Option Header if explicitly configured to do so */
868 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
870 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
871 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
873 /* Only process the HBH Option Header if explicitly configured to do so */
875 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
877 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
878 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
881 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
882 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
884 vlib_increment_combined_counter
885 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
886 vlib_increment_combined_counter
887 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
889 vlib_validate_buffer_enqueue_x2 (vm, node, next,
890 to_next, n_left_to_next,
891 pi0, pi1, next0, next1);
894 while (n_left_from > 0 && n_left_to_next > 0)
896 ip_lookup_next_t next0;
897 const load_balance_t *lb0;
900 const ip6_header_t *ip0;
901 const dpo_id_t *dpo0;
910 p0 = vlib_get_buffer (vm, pi0);
912 ip0 = vlib_buffer_get_current (p0);
913 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
915 lb0 = load_balance_get (lbi0);
918 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
920 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
922 hc0 = vnet_buffer (p0)->ip.flow_hash =
923 vnet_buffer (p0)->ip.flow_hash >> 1;
927 hc0 = vnet_buffer (p0)->ip.flow_hash =
928 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
931 load_balance_get_fwd_bucket (lb0,
933 lb0->lb_n_buckets_minus_1));
937 dpo0 = load_balance_get_bucket_i (lb0, 0);
940 next0 = dpo0->dpoi_next_node;
941 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
943 /* Only process the HBH Option Header if explicitly configured to do so */
945 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
947 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
948 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
951 vlib_increment_combined_counter
952 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
954 vlib_validate_buffer_enqueue_x1 (vm, node, next,
955 to_next, n_left_to_next,
959 vlib_put_next_frame (vm, node, next, n_left_to_next);
962 return frame->n_vectors;
966 VLIB_REGISTER_NODE (ip6_load_balance_node) =
968 .function = ip6_load_balance,
969 .name = "ip6-load-balance",
970 .vector_size = sizeof (u32),
971 .sibling_of = "ip6-lookup",
972 .format_trace = format_ip6_lookup_trace,
976 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
980 /* Adjacency taken. */
985 /* Packet data, possibly *after* rewrite. */
986 u8 packet_data[128 - 1 * sizeof (u32)];
988 ip6_forward_next_trace_t;
991 format_ip6_forward_next_trace (u8 * s, va_list * args)
993 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
994 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
995 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
996 u32 indent = format_get_indent (s);
998 s = format (s, "%U%U",
999 format_white_space, indent,
1000 format_ip6_header, t->packet_data, sizeof (t->packet_data));
1005 format_ip6_lookup_trace (u8 * s, va_list * args)
1007 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1008 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1009 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
1010 u32 indent = format_get_indent (s);
1012 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
1013 t->fib_index, t->adj_index, t->flow_hash);
1014 s = format (s, "\n%U%U",
1015 format_white_space, indent,
1016 format_ip6_header, t->packet_data, sizeof (t->packet_data));
1022 format_ip6_rewrite_trace (u8 * s, va_list * args)
1024 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1025 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1026 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
1027 u32 indent = format_get_indent (s);
1029 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1030 t->fib_index, t->adj_index, format_ip_adjacency,
1031 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
1032 s = format (s, "\n%U%U",
1033 format_white_space, indent,
1034 format_ip_adjacency_packet_data,
1035 t->adj_index, t->packet_data, sizeof (t->packet_data));
1039 /* Common trace function for all ip6-forward next nodes. */
1041 ip6_forward_next_trace (vlib_main_t * vm,
1042 vlib_node_runtime_t * node,
1043 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
1046 ip6_main_t *im = &ip6_main;
1048 n_left = frame->n_vectors;
1049 from = vlib_frame_vector_args (frame);
1054 vlib_buffer_t *b0, *b1;
1055 ip6_forward_next_trace_t *t0, *t1;
1057 /* Prefetch next iteration. */
1058 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1059 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1064 b0 = vlib_get_buffer (vm, bi0);
1065 b1 = vlib_get_buffer (vm, bi1);
1067 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1069 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1070 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1071 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1073 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1074 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1075 vec_elt (im->fib_index_by_sw_if_index,
1076 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1078 clib_memcpy (t0->packet_data,
1079 vlib_buffer_get_current (b0),
1080 sizeof (t0->packet_data));
1082 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1084 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1085 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1086 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1088 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1089 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1090 vec_elt (im->fib_index_by_sw_if_index,
1091 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
1093 clib_memcpy (t1->packet_data,
1094 vlib_buffer_get_current (b1),
1095 sizeof (t1->packet_data));
1105 ip6_forward_next_trace_t *t0;
1109 b0 = vlib_get_buffer (vm, bi0);
1111 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1113 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1114 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1115 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1117 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1118 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1119 vec_elt (im->fib_index_by_sw_if_index,
1120 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1122 clib_memcpy (t0->packet_data,
1123 vlib_buffer_get_current (b0),
1124 sizeof (t0->packet_data));
1131 /* Compute TCP/UDP/ICMP6 checksum in software. */
1133 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1134 ip6_header_t * ip0, int *bogus_lengthp)
1137 u16 sum16, payload_length_host_byte_order;
1138 u32 i, n_this_buffer, n_bytes_left;
1139 u32 headers_size = sizeof (ip0[0]);
1140 void *data_this_buffer;
1142 ASSERT (bogus_lengthp);
1145 /* Initialize checksum with ip header. */
1146 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1147 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1148 data_this_buffer = (void *) (ip0 + 1);
1150 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1152 sum0 = ip_csum_with_carry (sum0,
1153 clib_mem_unaligned (&ip0->
1154 src_address.as_uword[i],
1157 ip_csum_with_carry (sum0,
1158 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1162 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1163 * or UDP-Ping packets */
1164 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1167 ip6_hop_by_hop_ext_t *ext_hdr =
1168 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1170 /* validate really icmp6 next */
1171 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1172 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
1174 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1175 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1177 payload_length_host_byte_order -= skip_bytes;
1178 headers_size += skip_bytes;
1181 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1182 if (p0 && n_this_buffer + headers_size > p0->current_length)
1184 p0->current_length >
1185 headers_size ? p0->current_length - headers_size : 0;
1188 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1189 n_bytes_left -= n_this_buffer;
1190 if (n_bytes_left == 0)
1193 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1198 p0 = vlib_get_buffer (vm, p0->next_buffer);
1199 data_this_buffer = vlib_buffer_get_current (p0);
1200 n_this_buffer = p0->current_length;
1203 sum16 = ~ip_csum_fold (sum0);
1209 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1211 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1216 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1217 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1218 || ip0->protocol == IP_PROTOCOL_ICMP6
1219 || ip0->protocol == IP_PROTOCOL_UDP
1220 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1222 udp0 = (void *) (ip0 + 1);
1223 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1225 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1226 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
1230 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1232 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1233 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
1239 * @brief returns number of links on which src is reachable.
1242 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1244 const load_balance_t *lb0;
1247 lbi = ip6_fib_table_fwding_lookup_with_if_index (im,
1249 (b)->sw_if_index[VLIB_RX],
1252 lb0 = load_balance_get (lbi);
1254 return (fib_urpf_check_size (lb0->lb_urpf));
1258 ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1262 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1263 if (proto0 != IP_PROTOCOL_UDP)
1265 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1266 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1272 VNET_FEATURE_ARC_INIT (ip6_local) =
1274 .arc_name = "ip6-local",
1275 .start_nodes = VNET_FEATURES ("ip6-local"),
1280 ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1281 vlib_frame_t * frame, int head_of_feature_arc)
1283 ip6_main_t *im = &ip6_main;
1284 ip_lookup_main_t *lm = &im->lookup_main;
1285 ip_local_next_t next_index;
1286 u32 *from, *to_next, n_left_from, n_left_to_next;
1287 vlib_node_runtime_t *error_node =
1288 vlib_node_get_runtime (vm, ip6_input_node.index);
1289 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
1291 from = vlib_frame_vector_args (frame);
1292 n_left_from = frame->n_vectors;
1293 next_index = node->cached_next_index;
1295 if (node->flags & VLIB_NODE_FLAG_TRACE)
1296 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1298 while (n_left_from > 0)
1300 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1302 while (n_left_from >= 4 && n_left_to_next >= 2)
1304 vlib_buffer_t *p0, *p1;
1305 ip6_header_t *ip0, *ip1;
1306 udp_header_t *udp0, *udp1;
1307 u32 pi0, ip_len0, udp_len0, flags0, next0;
1308 u32 pi1, ip_len1, udp_len1, flags1, next1;
1309 i32 len_diff0, len_diff1;
1310 u8 error0, type0, good_l4_csum0, is_tcp_udp0;
1311 u8 error1, type1, good_l4_csum1, is_tcp_udp1;
1312 u32 udp_offset0, udp_offset1;
1314 pi0 = to_next[0] = from[0];
1315 pi1 = to_next[1] = from[1];
1319 n_left_to_next -= 2;
1321 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1323 p0 = vlib_get_buffer (vm, pi0);
1324 p1 = vlib_get_buffer (vm, pi1);
1326 ip0 = vlib_buffer_get_current (p0);
1327 ip1 = vlib_buffer_get_current (p1);
1329 if (head_of_feature_arc == 0)
1332 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1333 vnet_buffer (p1)->l3_hdr_offset = p1->current_data;
1335 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1336 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1341 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1342 is_tcp_udp1 = ip6_next_proto_is_tcp_udp (p1, ip1, &udp_offset1);
1344 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1345 || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1346 || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1348 good_l4_csum1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1349 || (flags1 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1350 || flags1 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1355 if (PREDICT_TRUE (is_tcp_udp0))
1357 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1358 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1359 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1360 && udp0->checksum == 0;
1361 /* Verify UDP length. */
1362 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1364 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1365 udp_len0 = clib_net_to_host_u16 (udp0->length);
1366 len_diff0 = ip_len0 - udp_len0;
1369 if (PREDICT_TRUE (is_tcp_udp1))
1371 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1372 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1373 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1374 && udp1->checksum == 0;
1375 /* Verify UDP length. */
1376 if (is_tcp_udp1 == IP_PROTOCOL_UDP)
1378 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1379 udp_len1 = clib_net_to_host_u16 (udp1->length);
1380 len_diff1 = ip_len1 - udp_len1;
1384 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1385 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1387 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1388 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1390 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1393 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1395 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1397 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1399 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1402 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1404 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1406 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1409 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1410 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1411 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1413 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1414 IP6_ERROR_UDP_CHECKSUM);
1415 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1416 IP6_ERROR_ICMP_CHECKSUM);
1417 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1418 error1 = (!good_l4_csum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1420 /* Drop packets from unroutable hosts. */
1421 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1422 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1423 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1424 !ip6_address_is_link_local_unicast (&ip0->src_address))
1426 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1427 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1429 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1430 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1431 !ip6_address_is_link_local_unicast (&ip1->src_address))
1433 error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1434 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1437 /* TODO maybe move to lookup? */
1438 vnet_buffer (p0)->ip.fib_index =
1439 vec_elt (im->fib_index_by_sw_if_index,
1440 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1441 vnet_buffer (p0)->ip.fib_index =
1442 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1443 (u32) ~ 0) ? vnet_buffer (p0)->ip.
1444 fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1446 vnet_buffer (p1)->ip.fib_index =
1447 vec_elt (im->fib_index_by_sw_if_index,
1448 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
1449 vnet_buffer (p1)->ip.fib_index =
1450 (vnet_buffer (p1)->sw_if_index[VLIB_TX] ==
1451 (u32) ~ 0) ? vnet_buffer (p1)->ip.
1452 fib_index : vnet_buffer (p1)->sw_if_index[VLIB_TX];
1457 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1458 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1461 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1463 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1465 p0->error = error_node->errors[error0];
1466 p1->error = error_node->errors[error1];
1468 if (head_of_feature_arc)
1470 if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1471 vnet_feature_arc_start (arc_index,
1472 vnet_buffer (p0)->sw_if_index
1473 [VLIB_RX], &next0, p0);
1474 if (PREDICT_TRUE (error1 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1475 vnet_feature_arc_start (arc_index,
1476 vnet_buffer (p1)->sw_if_index
1477 [VLIB_RX], &next1, p1);
1480 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1481 to_next, n_left_to_next,
1482 pi0, pi1, next0, next1);
1485 while (n_left_from > 0 && n_left_to_next > 0)
1490 u32 pi0, ip_len0, udp_len0, flags0, next0;
1492 u8 error0, type0, good_l4_csum0;
1496 pi0 = to_next[0] = from[0];
1500 n_left_to_next -= 1;
1502 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1504 p0 = vlib_get_buffer (vm, pi0);
1505 ip0 = vlib_buffer_get_current (p0);
1507 if (head_of_feature_arc == 0)
1510 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1512 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1514 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1515 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1516 || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1517 || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1521 if (PREDICT_TRUE (is_tcp_udp0))
1523 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1524 /* Don't verify UDP checksum for packets with explicit zero
1526 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1527 && udp0->checksum == 0;
1528 /* Verify UDP length. */
1529 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1531 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1532 udp_len0 = clib_net_to_host_u16 (udp0->length);
1533 len_diff0 = ip_len0 - udp_len0;
1537 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1538 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1540 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1543 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1545 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1547 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1550 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1551 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1553 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1554 IP6_ERROR_UDP_CHECKSUM);
1555 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1556 IP6_ERROR_ICMP_CHECKSUM);
1557 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1559 /* If this is a neighbor solicitation (ICMP), skip src RPF check */
1560 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1561 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1562 !ip6_address_is_link_local_unicast (&ip0->src_address))
1564 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1565 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1568 vnet_buffer (p0)->ip.fib_index =
1569 vec_elt (im->fib_index_by_sw_if_index,
1570 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1571 vnet_buffer (p0)->ip.fib_index =
1572 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1573 (u32) ~ 0) ? vnet_buffer (p0)->ip.
1574 fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1578 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1580 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1582 p0->error = error_node->errors[error0];
1584 if (head_of_feature_arc)
1586 if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1587 vnet_feature_arc_start (arc_index,
1588 vnet_buffer (p0)->sw_if_index
1589 [VLIB_RX], &next0, p0);
1592 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1593 to_next, n_left_to_next,
1597 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1600 return frame->n_vectors;
1604 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1606 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1610 VLIB_REGISTER_NODE (ip6_local_node, static) =
1612 .function = ip6_local,
1613 .name = "ip6-local",
1614 .vector_size = sizeof (u32),
1615 .format_trace = format_ip6_forward_next_trace,
1616 .n_next_nodes = IP_LOCAL_N_NEXT,
1619 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1620 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
1621 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1622 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1623 [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-reassembly",
1628 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1632 ip6_local_end_of_arc (vlib_main_t * vm,
1633 vlib_node_runtime_t * node, vlib_frame_t * frame)
1635 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1639 VLIB_REGISTER_NODE (ip6_local_end_of_arc_node,static) = {
1640 .function = ip6_local_end_of_arc,
1641 .name = "ip6-local-end-of-arc",
1642 .vector_size = sizeof (u32),
1644 .format_trace = format_ip6_forward_next_trace,
1645 .sibling_of = "ip6-local",
1648 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_end_of_arc_node, ip6_local_end_of_arc)
1650 VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1651 .arc_name = "ip6-local",
1652 .node_name = "ip6-local-end-of-arc",
1653 .runs_before = 0, /* not before any other features */
1658 ip6_register_protocol (u32 protocol, u32 node_index)
1660 vlib_main_t *vm = vlib_get_main ();
1661 ip6_main_t *im = &ip6_main;
1662 ip_lookup_main_t *lm = &im->lookup_main;
1664 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1665 lm->local_next_by_ip_protocol[protocol] =
1666 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1671 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1672 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1673 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1674 } ip6_discover_neighbor_next_t;
1678 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1679 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1680 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1681 } ip6_discover_neighbor_error_t;
1684 ip6_discover_neighbor_inline (vlib_main_t * vm,
1685 vlib_node_runtime_t * node,
1686 vlib_frame_t * frame, int is_glean)
1688 vnet_main_t *vnm = vnet_get_main ();
1689 ip6_main_t *im = &ip6_main;
1690 ip_lookup_main_t *lm = &im->lookup_main;
1691 u32 *from, *to_next_drop;
1692 uword n_left_from, n_left_to_next_drop;
1693 static f64 time_last_seed_change = -1e100;
1694 static u32 hash_seeds[3];
1695 static uword hash_bitmap[256 / BITS (uword)];
1699 if (node->flags & VLIB_NODE_FLAG_TRACE)
1700 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1702 time_now = vlib_time_now (vm);
1703 if (time_now - time_last_seed_change > 1e-3)
1706 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1707 sizeof (hash_seeds));
1708 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1709 hash_seeds[i] = r[i];
1711 /* Mark all hash keys as been not-seen before. */
1712 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1715 time_last_seed_change = time_now;
1718 from = vlib_frame_vector_args (frame);
1719 n_left_from = frame->n_vectors;
1721 while (n_left_from > 0)
1723 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1724 to_next_drop, n_left_to_next_drop);
1726 while (n_left_from > 0 && n_left_to_next_drop > 0)
1730 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1732 ip_adjacency_t *adj0;
1733 vnet_hw_interface_t *hw_if0;
1738 p0 = vlib_get_buffer (vm, pi0);
1740 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1742 ip0 = vlib_buffer_get_current (p0);
1744 adj0 = adj_get (adj_index0);
1748 ip0->dst_address.as_u64[0] =
1749 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1750 ip0->dst_address.as_u64[1] =
1751 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1758 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1759 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1762 b0 ^= ip0->dst_address.as_u32[0];
1763 c0 ^= ip0->dst_address.as_u32[1];
1765 hash_v3_mix32 (a0, b0, c0);
1767 b0 ^= ip0->dst_address.as_u32[2];
1768 c0 ^= ip0->dst_address.as_u32[3];
1770 hash_v3_finalize32 (a0, b0, c0);
1772 c0 &= BITS (hash_bitmap) - 1;
1773 c0 = c0 / BITS (uword);
1774 m0 = (uword) 1 << (c0 % BITS (uword));
1776 bm0 = hash_bitmap[c0];
1777 drop0 = (bm0 & m0) != 0;
1779 /* Mark it as seen. */
1780 hash_bitmap[c0] = bm0 | m0;
1784 to_next_drop[0] = pi0;
1786 n_left_to_next_drop -= 1;
1788 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1790 /* If the interface is link-down, drop the pkt */
1791 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1795 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1796 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1801 * the adj has been updated to a rewrite but the node the DPO that got
1802 * us here hasn't - yet. no big deal. we'll drop while we wait.
1804 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1809 icmp6_neighbor_solicitation_header_t *h0;
1812 h0 = vlib_packet_template_get_packet
1813 (vm, &im->discover_neighbor_packet_template, &bi0);
1816 * Build ethernet header.
1817 * Choose source address based on destination lookup
1820 if (ip6_src_address_for_packet (lm,
1822 &h0->ip.src_address))
1824 /* There is no address on the interface */
1826 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1827 vlib_buffer_free (vm, &bi0, 1);
1832 * Destination address is a solicited node multicast address.
1833 * We need to fill in
1834 * the low 24 bits with low 24 bits of target's address.
1836 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1837 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1838 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1840 h0->neighbor.target_address = ip0->dst_address;
1842 clib_memcpy (h0->link_layer_option.ethernet_address,
1843 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1845 /* $$$$ appears we need this; why is the checksum non-zero? */
1846 h0->neighbor.icmp.checksum = 0;
1847 h0->neighbor.icmp.checksum =
1848 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1851 ASSERT (bogus_length == 0);
1853 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1854 b0 = vlib_get_buffer (vm, bi0);
1855 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1856 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1858 /* Add rewrite/encap string. */
1859 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1860 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1862 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1864 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1868 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1869 n_left_to_next_drop);
1872 return frame->n_vectors;
1876 ip6_discover_neighbor (vlib_main_t * vm,
1877 vlib_node_runtime_t * node, vlib_frame_t * frame)
1879 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1883 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1885 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1888 static char *ip6_discover_neighbor_error_strings[] = {
1889 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1890 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1891 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1892 = "no source address for ND solicitation",
1896 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1898 .function = ip6_discover_neighbor,
1899 .name = "ip6-discover-neighbor",
1900 .vector_size = sizeof (u32),
1901 .format_trace = format_ip6_forward_next_trace,
1902 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1903 .error_strings = ip6_discover_neighbor_error_strings,
1904 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1907 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
1908 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1914 VLIB_REGISTER_NODE (ip6_glean_node) =
1916 .function = ip6_glean,
1917 .name = "ip6-glean",
1918 .vector_size = sizeof (u32),
1919 .format_trace = format_ip6_forward_next_trace,
1920 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1921 .error_strings = ip6_discover_neighbor_error_strings,
1922 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1925 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
1926 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1932 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1934 vnet_main_t *vnm = vnet_get_main ();
1935 ip6_main_t *im = &ip6_main;
1936 icmp6_neighbor_solicitation_header_t *h;
1938 ip_interface_address_t *ia;
1939 ip_adjacency_t *adj;
1940 vnet_hw_interface_t *hi;
1941 vnet_sw_interface_t *si;
1947 si = vnet_get_sw_interface (vnm, sw_if_index);
1949 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1951 return clib_error_return (0, "%U: interface %U down",
1952 format_ip6_address, dst,
1953 format_vnet_sw_if_index_name, vnm,
1958 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1961 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1962 return clib_error_return
1963 (0, "no matching interface address for destination %U (interface %U)",
1964 format_ip6_address, dst,
1965 format_vnet_sw_if_index_name, vnm, sw_if_index);
1969 vlib_packet_template_get_packet (vm,
1970 &im->discover_neighbor_packet_template,
1973 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1975 /* Destination address is a solicited node multicast address. We need to fill in
1976 the low 24 bits with low 24 bits of target's address. */
1977 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1978 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1979 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1981 h->ip.src_address = src[0];
1982 h->neighbor.target_address = dst[0];
1984 if (PREDICT_FALSE (!hi->hw_address))
1986 return clib_error_return (0, "%U: interface %U do not support ip probe",
1987 format_ip6_address, dst,
1988 format_vnet_sw_if_index_name, vnm,
1992 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1993 vec_len (hi->hw_address));
1995 h->neighbor.icmp.checksum =
1996 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1997 ASSERT (bogus_length == 0);
1999 b = vlib_get_buffer (vm, bi);
2000 vnet_buffer (b)->sw_if_index[VLIB_RX] =
2001 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2003 /* Add encapsulation string for software interface (e.g. ethernet header). */
2004 ip46_address_t nh = {
2008 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
2009 VNET_LINK_IP6, &nh, sw_if_index);
2012 /* Peer has been previously resolved, retrieve glean adj instead */
2013 if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
2016 ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6, sw_if_index, &nh);
2020 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2021 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2024 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
2025 u32 *to_next = vlib_frame_vector_args (f);
2028 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2032 return /* no error */ 0;
2037 IP6_REWRITE_NEXT_DROP,
2038 IP6_REWRITE_NEXT_ICMP_ERROR,
2039 } ip6_rewrite_next_t;
2042 ip6_rewrite_inline (vlib_main_t * vm,
2043 vlib_node_runtime_t * node,
2044 vlib_frame_t * frame,
2045 int do_counters, int is_midchain, int is_mcast)
2047 ip_lookup_main_t *lm = &ip6_main.lookup_main;
2048 u32 *from = vlib_frame_vector_args (frame);
2049 u32 n_left_from, n_left_to_next, *to_next, next_index;
2050 vlib_node_runtime_t *error_node =
2051 vlib_node_get_runtime (vm, ip6_input_node.index);
2053 n_left_from = frame->n_vectors;
2054 next_index = node->cached_next_index;
2055 u32 thread_index = vlib_get_thread_index ();
2057 while (n_left_from > 0)
2059 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2061 while (n_left_from >= 4 && n_left_to_next >= 2)
2063 ip_adjacency_t *adj0, *adj1;
2064 vlib_buffer_t *p0, *p1;
2065 ip6_header_t *ip0, *ip1;
2066 u32 pi0, rw_len0, next0, error0, adj_index0;
2067 u32 pi1, rw_len1, next1, error1, adj_index1;
2068 u32 tx_sw_if_index0, tx_sw_if_index1;
2070 /* Prefetch next iteration. */
2072 vlib_buffer_t *p2, *p3;
2074 p2 = vlib_get_buffer (vm, from[2]);
2075 p3 = vlib_get_buffer (vm, from[3]);
2077 vlib_prefetch_buffer_header (p2, LOAD);
2078 vlib_prefetch_buffer_header (p3, LOAD);
2080 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2081 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2083 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2084 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2087 pi0 = to_next[0] = from[0];
2088 pi1 = to_next[1] = from[1];
2093 n_left_to_next -= 2;
2095 p0 = vlib_get_buffer (vm, pi0);
2096 p1 = vlib_get_buffer (vm, pi1);
2098 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2099 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
2101 ip0 = vlib_buffer_get_current (p0);
2102 ip1 = vlib_buffer_get_current (p1);
2104 error0 = error1 = IP6_ERROR_NONE;
2105 next0 = next1 = IP6_REWRITE_NEXT_DROP;
2107 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2109 i32 hop_limit0 = ip0->hop_limit;
2111 /* Input node should have reject packets with hop limit 0. */
2112 ASSERT (ip0->hop_limit > 0);
2116 ip0->hop_limit = hop_limit0;
2119 * If the hop count drops below 1 when forwarding, generate
2122 if (PREDICT_FALSE (hop_limit0 <= 0))
2124 error0 = IP6_ERROR_TIME_EXPIRED;
2125 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2126 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2127 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2128 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2134 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2136 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2138 i32 hop_limit1 = ip1->hop_limit;
2140 /* Input node should have reject packets with hop limit 0. */
2141 ASSERT (ip1->hop_limit > 0);
2145 ip1->hop_limit = hop_limit1;
2148 * If the hop count drops below 1 when forwarding, generate
2151 if (PREDICT_FALSE (hop_limit1 <= 0))
2153 error1 = IP6_ERROR_TIME_EXPIRED;
2154 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2155 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2156 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2157 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2163 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2165 adj0 = adj_get (adj_index0);
2166 adj1 = adj_get (adj_index1);
2168 rw_len0 = adj0[0].rewrite_header.data_bytes;
2169 rw_len1 = adj1[0].rewrite_header.data_bytes;
2170 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2171 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
2175 vlib_increment_combined_counter
2176 (&adjacency_counters,
2177 thread_index, adj_index0, 1,
2178 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2179 vlib_increment_combined_counter
2180 (&adjacency_counters,
2181 thread_index, adj_index1, 1,
2182 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
2185 /* Check MTU of outgoing interface. */
2187 (vlib_buffer_length_in_chain (vm, p0) >
2189 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2192 (vlib_buffer_length_in_chain (vm, p1) >
2194 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2197 /* Don't adjust the buffer for hop count issue; icmp-error node
2198 * wants to see the IP headerr */
2199 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2201 p0->current_data -= rw_len0;
2202 p0->current_length += rw_len0;
2204 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2205 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2206 next0 = adj0[0].rewrite_header.next_index;
2209 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2210 vnet_feature_arc_start (lm->output_feature_arc_index,
2211 tx_sw_if_index0, &next0, p0);
2213 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2215 p1->current_data -= rw_len1;
2216 p1->current_length += rw_len1;
2218 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2219 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2220 next1 = adj1[0].rewrite_header.next_index;
2223 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2224 vnet_feature_arc_start (lm->output_feature_arc_index,
2225 tx_sw_if_index1, &next1, p1);
2228 /* Guess we are only writing on simple Ethernet header. */
2229 vnet_rewrite_two_headers (adj0[0], adj1[0],
2230 ip0, ip1, sizeof (ethernet_header_t));
2234 adj0->sub_type.midchain.fixup_func
2235 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2236 adj1->sub_type.midchain.fixup_func
2237 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
2242 * copy bytes from the IP address into the MAC rewrite
2244 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2245 vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1);
2248 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2249 to_next, n_left_to_next,
2250 pi0, pi1, next0, next1);
2253 while (n_left_from > 0 && n_left_to_next > 0)
2255 ip_adjacency_t *adj0;
2259 u32 adj_index0, next0, error0;
2260 u32 tx_sw_if_index0;
2262 pi0 = to_next[0] = from[0];
2264 p0 = vlib_get_buffer (vm, pi0);
2266 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2268 adj0 = adj_get (adj_index0);
2270 ip0 = vlib_buffer_get_current (p0);
2272 error0 = IP6_ERROR_NONE;
2273 next0 = IP6_REWRITE_NEXT_DROP;
2275 /* Check hop limit */
2276 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2278 i32 hop_limit0 = ip0->hop_limit;
2280 ASSERT (ip0->hop_limit > 0);
2284 ip0->hop_limit = hop_limit0;
2286 if (PREDICT_FALSE (hop_limit0 <= 0))
2289 * If the hop count drops below 1 when forwarding, generate
2292 error0 = IP6_ERROR_TIME_EXPIRED;
2293 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2294 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2295 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2296 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2302 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2305 /* Guess we are only writing on simple Ethernet header. */
2306 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2308 /* Update packet buffer attributes/set output interface. */
2309 rw_len0 = adj0[0].rewrite_header.data_bytes;
2310 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2314 vlib_increment_combined_counter
2315 (&adjacency_counters,
2316 thread_index, adj_index0, 1,
2317 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2320 /* Check MTU of outgoing interface. */
2322 (vlib_buffer_length_in_chain (vm, p0) >
2324 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2327 /* Don't adjust the buffer for hop count issue; icmp-error node
2328 * wants to see the IP headerr */
2329 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2331 p0->current_data -= rw_len0;
2332 p0->current_length += rw_len0;
2334 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2336 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2337 next0 = adj0[0].rewrite_header.next_index;
2340 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2341 vnet_feature_arc_start (lm->output_feature_arc_index,
2342 tx_sw_if_index0, &next0, p0);
2347 adj0->sub_type.midchain.fixup_func
2348 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2352 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2355 p0->error = error_node->errors[error0];
2360 n_left_to_next -= 1;
2362 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2363 to_next, n_left_to_next,
2367 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2370 /* Need to do trace after rewrites to pick up new packet data. */
2371 if (node->flags & VLIB_NODE_FLAG_TRACE)
2372 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2374 return frame->n_vectors;
2378 ip6_rewrite (vlib_main_t * vm,
2379 vlib_node_runtime_t * node, vlib_frame_t * frame)
2381 if (adj_are_counters_enabled ())
2382 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2384 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2388 ip6_rewrite_mcast (vlib_main_t * vm,
2389 vlib_node_runtime_t * node, vlib_frame_t * frame)
2391 if (adj_are_counters_enabled ())
2392 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2394 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2398 ip6_midchain (vlib_main_t * vm,
2399 vlib_node_runtime_t * node, vlib_frame_t * frame)
2401 if (adj_are_counters_enabled ())
2402 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2404 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2408 ip6_mcast_midchain (vlib_main_t * vm,
2409 vlib_node_runtime_t * node, vlib_frame_t * frame)
2411 if (adj_are_counters_enabled ())
2412 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2414 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2418 VLIB_REGISTER_NODE (ip6_midchain_node) =
2420 .function = ip6_midchain,
2421 .name = "ip6-midchain",
2422 .vector_size = sizeof (u32),
2423 .format_trace = format_ip6_forward_next_trace,
2424 .sibling_of = "ip6-rewrite",
2428 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2431 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2433 .function = ip6_rewrite,
2434 .name = "ip6-rewrite",
2435 .vector_size = sizeof (u32),
2436 .format_trace = format_ip6_rewrite_trace,
2440 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
2441 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2446 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2449 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2451 .function = ip6_rewrite_mcast,
2452 .name = "ip6-rewrite-mcast",
2453 .vector_size = sizeof (u32),
2454 .format_trace = format_ip6_rewrite_trace,
2455 .sibling_of = "ip6-rewrite",
2459 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2462 VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
2464 .function = ip6_mcast_midchain,
2465 .name = "ip6-mcast-midchain",
2466 .vector_size = sizeof (u32),
2467 .format_trace = format_ip6_rewrite_trace,
2468 .sibling_of = "ip6-rewrite",
2472 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
2475 * Hop-by-Hop handling
2477 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2479 #define foreach_ip6_hop_by_hop_error \
2480 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2481 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2482 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2487 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2488 foreach_ip6_hop_by_hop_error
2490 IP6_HOP_BY_HOP_N_ERROR,
2491 } ip6_hop_by_hop_error_t;
2495 * Primary h-b-h handler trace support
2496 * We work pretty hard on the problem for obvious reasons
2502 u8 option_data[256];
2503 } ip6_hop_by_hop_trace_t;
2505 vlib_node_registration_t ip6_hop_by_hop_node;
2507 static char *ip6_hop_by_hop_error_strings[] = {
2508 #define _(sym,string) string,
2509 foreach_ip6_hop_by_hop_error
2514 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2516 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2517 int total_len = va_arg (*args, int);
2518 ip6_hop_by_hop_option_t *opt0, *limit0;
2519 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2522 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2523 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2525 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2526 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2528 while (opt0 < limit0)
2533 case 0: /* Pad, just stop */
2534 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2538 if (hm->trace[type0])
2540 s = (*hm->trace[type0]) (s, opt0);
2545 format (s, "\n unrecognized option %d length %d", type0,
2549 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2550 sizeof (ip6_hop_by_hop_option_t));
2558 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2560 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2561 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2562 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2563 ip6_hop_by_hop_header_t *hbh0;
2564 ip6_hop_by_hop_option_t *opt0, *limit0;
2565 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2569 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2571 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2572 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2574 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2575 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2577 while (opt0 < limit0)
2582 case 0: /* Pad, just stop */
2583 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2587 if (hm->trace[type0])
2589 s = (*hm->trace[type0]) (s, opt0);
2594 format (s, "\n unrecognized option %d length %d", type0,
2598 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2599 sizeof (ip6_hop_by_hop_option_t));
2607 ip6_scan_hbh_options (vlib_buffer_t * b0,
2609 ip6_hop_by_hop_header_t * hbh0,
2610 ip6_hop_by_hop_option_t * opt0,
2611 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2613 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2617 while (opt0 < limit0)
2623 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2628 if (hm->options[type0])
2630 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2632 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2638 /* Unrecognized mandatory option, check the two high order bits */
2639 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2641 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2643 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2644 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2645 *next0 = IP_LOOKUP_NEXT_DROP;
2647 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2648 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2649 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2650 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2651 ICMP6_parameter_problem_unrecognized_option,
2652 (u8 *) opt0 - (u8 *) ip0);
2654 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2655 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2656 if (!ip6_address_is_multicast (&ip0->dst_address))
2658 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2659 icmp6_error_set_vnet_buffer (b0,
2660 ICMP6_parameter_problem,
2661 ICMP6_parameter_problem_unrecognized_option,
2662 (u8 *) opt0 - (u8 *) ip0);
2666 *next0 = IP_LOOKUP_NEXT_DROP;
2674 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2675 sizeof (ip6_hop_by_hop_option_t));
2681 * Process the Hop-by-Hop Options header
2684 ip6_hop_by_hop (vlib_main_t * vm,
2685 vlib_node_runtime_t * node, vlib_frame_t * frame)
2687 vlib_node_runtime_t *error_node =
2688 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2689 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2690 u32 n_left_from, *from, *to_next;
2691 ip_lookup_next_t next_index;
2693 from = vlib_frame_vector_args (frame);
2694 n_left_from = frame->n_vectors;
2695 next_index = node->cached_next_index;
2697 while (n_left_from > 0)
2701 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2703 while (n_left_from >= 4 && n_left_to_next >= 2)
2706 vlib_buffer_t *b0, *b1;
2708 ip6_header_t *ip0, *ip1;
2709 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2710 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2711 u8 error0 = 0, error1 = 0;
2713 /* Prefetch next iteration. */
2715 vlib_buffer_t *p2, *p3;
2717 p2 = vlib_get_buffer (vm, from[2]);
2718 p3 = vlib_get_buffer (vm, from[3]);
2720 vlib_prefetch_buffer_header (p2, LOAD);
2721 vlib_prefetch_buffer_header (p3, LOAD);
2723 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2724 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2727 /* Speculatively enqueue b0, b1 to the current next frame */
2728 to_next[0] = bi0 = from[0];
2729 to_next[1] = bi1 = from[1];
2733 n_left_to_next -= 2;
2735 b0 = vlib_get_buffer (vm, bi0);
2736 b1 = vlib_get_buffer (vm, bi1);
2738 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2739 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2740 ip_adjacency_t *adj0 = adj_get (adj_index0);
2741 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2742 ip_adjacency_t *adj1 = adj_get (adj_index1);
2744 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2745 next0 = adj0->lookup_next_index;
2746 next1 = adj1->lookup_next_index;
2748 ip0 = vlib_buffer_get_current (b0);
2749 ip1 = vlib_buffer_get_current (b1);
2750 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2751 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2752 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2753 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2755 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2756 ((hbh0->length + 1) << 3));
2758 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2759 ((hbh1->length + 1) << 3));
2762 * Basic validity checks
2764 if ((hbh0->length + 1) << 3 >
2765 clib_net_to_host_u16 (ip0->payload_length))
2767 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2768 next0 = IP_LOOKUP_NEXT_DROP;
2771 /* Scan the set of h-b-h options, process ones that we understand */
2772 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2774 if ((hbh1->length + 1) << 3 >
2775 clib_net_to_host_u16 (ip1->payload_length))
2777 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2778 next1 = IP_LOOKUP_NEXT_DROP;
2781 /* Scan the set of h-b-h options, process ones that we understand */
2782 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2785 /* Has the classifier flagged this buffer for special treatment? */
2788 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2789 next0 = hm->next_override;
2791 /* Has the classifier flagged this buffer for special treatment? */
2794 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2795 next1 = hm->next_override;
2797 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2799 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2801 ip6_hop_by_hop_trace_t *t =
2802 vlib_add_trace (vm, node, b0, sizeof (*t));
2803 u32 trace_len = (hbh0->length + 1) << 3;
2804 t->next_index = next0;
2805 /* Capture the h-b-h option verbatim */
2808 ARRAY_LEN (t->option_data) ? trace_len :
2809 ARRAY_LEN (t->option_data);
2810 t->trace_len = trace_len;
2811 clib_memcpy (t->option_data, hbh0, trace_len);
2813 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2815 ip6_hop_by_hop_trace_t *t =
2816 vlib_add_trace (vm, node, b1, sizeof (*t));
2817 u32 trace_len = (hbh1->length + 1) << 3;
2818 t->next_index = next1;
2819 /* Capture the h-b-h option verbatim */
2822 ARRAY_LEN (t->option_data) ? trace_len :
2823 ARRAY_LEN (t->option_data);
2824 t->trace_len = trace_len;
2825 clib_memcpy (t->option_data, hbh1, trace_len);
2830 b0->error = error_node->errors[error0];
2831 b1->error = error_node->errors[error1];
2833 /* verify speculative enqueue, maybe switch current next frame */
2834 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2835 n_left_to_next, bi0, bi1, next0,
2839 while (n_left_from > 0 && n_left_to_next > 0)
2845 ip6_hop_by_hop_header_t *hbh0;
2846 ip6_hop_by_hop_option_t *opt0, *limit0;
2849 /* Speculatively enqueue b0 to the current next frame */
2855 n_left_to_next -= 1;
2857 b0 = vlib_get_buffer (vm, bi0);
2859 * Default use the next_index from the adjacency.
2860 * A HBH option rarely redirects to a different node
2862 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2863 ip_adjacency_t *adj0 = adj_get (adj_index0);
2864 next0 = adj0->lookup_next_index;
2866 ip0 = vlib_buffer_get_current (b0);
2867 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2868 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2870 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2871 ((hbh0->length + 1) << 3));
2874 * Basic validity checks
2876 if ((hbh0->length + 1) << 3 >
2877 clib_net_to_host_u16 (ip0->payload_length))
2879 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2880 next0 = IP_LOOKUP_NEXT_DROP;
2884 /* Scan the set of h-b-h options, process ones that we understand */
2885 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2888 /* Has the classifier flagged this buffer for special treatment? */
2891 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2892 next0 = hm->next_override;
2894 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2896 ip6_hop_by_hop_trace_t *t =
2897 vlib_add_trace (vm, node, b0, sizeof (*t));
2898 u32 trace_len = (hbh0->length + 1) << 3;
2899 t->next_index = next0;
2900 /* Capture the h-b-h option verbatim */
2903 ARRAY_LEN (t->option_data) ? trace_len :
2904 ARRAY_LEN (t->option_data);
2905 t->trace_len = trace_len;
2906 clib_memcpy (t->option_data, hbh0, trace_len);
2909 b0->error = error_node->errors[error0];
2911 /* verify speculative enqueue, maybe switch current next frame */
2912 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2913 n_left_to_next, bi0, next0);
2915 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2917 return frame->n_vectors;
2921 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2923 .function = ip6_hop_by_hop,
2924 .name = "ip6-hop-by-hop",
2925 .sibling_of = "ip6-lookup",
2926 .vector_size = sizeof (u32),
2927 .format_trace = format_ip6_hop_by_hop_trace,
2928 .type = VLIB_NODE_TYPE_INTERNAL,
2929 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2930 .error_strings = ip6_hop_by_hop_error_strings,
2935 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2937 static clib_error_t *
2938 ip6_hop_by_hop_init (vlib_main_t * vm)
2940 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2941 memset (hm->options, 0, sizeof (hm->options));
2942 memset (hm->trace, 0, sizeof (hm->trace));
2943 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2947 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2950 ip6_hbh_set_next_override (uword next)
2952 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2954 hm->next_override = next;
2958 ip6_hbh_register_option (u8 option,
2959 int options (vlib_buffer_t * b, ip6_header_t * ip,
2960 ip6_hop_by_hop_option_t * opt),
2961 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2963 ip6_main_t *im = &ip6_main;
2964 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2966 ASSERT (option < ARRAY_LEN (hm->options));
2968 /* Already registered */
2969 if (hm->options[option])
2972 hm->options[option] = options;
2973 hm->trace[option] = trace;
2975 /* Set global variable */
2976 im->hbh_enabled = 1;
2982 ip6_hbh_unregister_option (u8 option)
2984 ip6_main_t *im = &ip6_main;
2985 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2987 ASSERT (option < ARRAY_LEN (hm->options));
2989 /* Not registered */
2990 if (!hm->options[option])
2993 hm->options[option] = NULL;
2994 hm->trace[option] = NULL;
2996 /* Disable global knob if this was the last option configured */
2999 for (i = 0; i < 256; i++)
3001 if (hm->options[option])
3008 im->hbh_enabled = 0;
3013 /* Global IP6 main. */
3014 ip6_main_t ip6_main;
3016 static clib_error_t *
3017 ip6_lookup_init (vlib_main_t * vm)
3019 ip6_main_t *im = &ip6_main;
3020 clib_error_t *error;
3023 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
3026 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
3033 for (j = 0; j < i0; j++)
3034 im->fib_masks[i].as_u32[j] = ~0;
3037 im->fib_masks[i].as_u32[i0] =
3038 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
3041 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
3043 if (im->lookup_table_nbuckets == 0)
3044 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
3046 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
3048 if (im->lookup_table_size == 0)
3049 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
3051 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
3052 "ip6 FIB fwding table",
3053 im->lookup_table_nbuckets, im->lookup_table_size);
3054 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
3055 "ip6 FIB non-fwding table",
3056 im->lookup_table_nbuckets, im->lookup_table_size);
3058 /* Create FIB with index 0 and table id of 0. */
3059 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
3060 FIB_SOURCE_DEFAULT_ROUTE);
3061 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
3062 MFIB_SOURCE_DEFAULT_ROUTE);
3066 pn = pg_get_node (ip6_lookup_node.index);
3067 pn->unformat_edit = unformat_pg_ip6_header;
3070 /* Unless explicitly configured, don't process HBH options */
3071 im->hbh_enabled = 0;
3074 icmp6_neighbor_solicitation_header_t p;
3076 memset (&p, 0, sizeof (p));
3078 p.ip.ip_version_traffic_class_and_flow_label =
3079 clib_host_to_net_u32 (0x6 << 28);
3080 p.ip.payload_length =
3081 clib_host_to_net_u16 (sizeof (p) -
3083 (icmp6_neighbor_solicitation_header_t, neighbor));
3084 p.ip.protocol = IP_PROTOCOL_ICMP6;
3085 p.ip.hop_limit = 255;
3086 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
3088 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
3090 p.link_layer_option.header.type =
3091 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
3092 p.link_layer_option.header.n_data_u64s =
3093 sizeof (p.link_layer_option) / sizeof (u64);
3095 vlib_packet_template_init (vm,
3096 &im->discover_neighbor_packet_template,
3098 /* alloc chunk size */ 8,
3099 "ip6 neighbor discovery");
3105 VLIB_INIT_FUNCTION (ip6_lookup_init);
3108 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
3111 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3112 /* Invert the "u" bit */
3113 ip->as_u8[8] = mac[0] ^ (1 << 1);
3114 ip->as_u8[9] = mac[1];
3115 ip->as_u8[10] = mac[2];
3116 ip->as_u8[11] = 0xFF;
3117 ip->as_u8[12] = 0xFE;
3118 ip->as_u8[13] = mac[3];
3119 ip->as_u8[14] = mac[4];
3120 ip->as_u8[15] = mac[5];
3124 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3127 /* Invert the previously inverted "u" bit */
3128 mac[0] = ip->as_u8[8] ^ (1 << 1);
3129 mac[1] = ip->as_u8[9];
3130 mac[2] = ip->as_u8[10];
3131 mac[3] = ip->as_u8[13];
3132 mac[4] = ip->as_u8[14];
3133 mac[5] = ip->as_u8[15];
3136 static clib_error_t *
3137 test_ip6_link_command_fn (vlib_main_t * vm,
3138 unformat_input_t * input, vlib_cli_command_t * cmd)
3141 ip6_address_t _a, *a = &_a;
3143 if (unformat (input, "%U", unformat_ethernet_address, mac))
3145 ip6_link_local_address_from_ethernet_mac_address (a, mac);
3146 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
3147 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3148 vlib_cli_output (vm, "Original MAC address: %U",
3149 format_ethernet_address, mac);
3156 * This command converts the given MAC Address into an IPv6 link-local
3160 * Example of how to create an IPv6 link-local address:
3161 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3162 * Link local address: fe80::14d9:e0ff:fe91:7986
3163 * Original MAC address: 16:d9:e0:91:79:86
3167 VLIB_CLI_COMMAND (test_link_command, static) =
3169 .path = "test ip6 link",
3170 .function = test_ip6_link_command_fn,
3171 .short_help = "test ip6 link <mac-address>",
3176 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3180 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
3182 if (~0 == fib_index)
3183 return VNET_API_ERROR_NO_SUCH_FIB;
3185 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
3191 static clib_error_t *
3192 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3193 unformat_input_t * input,
3194 vlib_cli_command_t * cmd)
3198 u32 flow_hash_config = 0;
3201 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3203 if (unformat (input, "table %d", &table_id))
3206 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3207 foreach_flow_hash_bit
3214 return clib_error_return (0, "unknown input `%U'",
3215 format_unformat_error, input);
3217 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3224 return clib_error_return (0, "no such FIB table %d", table_id);
3227 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3235 * Configure the set of IPv6 fields used by the flow hash.
3239 * Example of how to set the flow hash on a given table:
3240 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3242 * Example of display the configured flow hash:
3243 * @cliexstart{show ip6 fib}
3244 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3247 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3248 * [0] [@0]: dpo-drop ip6
3251 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3252 * [0] [@2]: dpo-receive
3255 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3256 * [0] [@2]: dpo-receive
3259 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3260 * [0] [@2]: dpo-receive
3263 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3264 * [0] [@2]: dpo-receive
3265 * ff02::1:ff00:0/104
3267 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3268 * [0] [@2]: dpo-receive
3269 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3272 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3273 * [0] [@0]: dpo-drop ip6
3276 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3277 * [0] [@4]: ipv6-glean: af_packet0
3280 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3281 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3284 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3285 * [0] [@2]: dpo-receive
3286 * fe80::fe:3eff:fe3e:9222/128
3288 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3289 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3292 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3293 * [0] [@2]: dpo-receive
3296 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3297 * [0] [@2]: dpo-receive
3300 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3301 * [0] [@2]: dpo-receive
3302 * ff02::1:ff00:0/104
3304 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3305 * [0] [@2]: dpo-receive
3310 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3312 .path = "set ip6 flow-hash",
3314 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3315 .function = set_ip6_flow_hash_command_fn,
3319 static clib_error_t *
3320 show_ip6_local_command_fn (vlib_main_t * vm,
3321 unformat_input_t * input, vlib_cli_command_t * cmd)
3323 ip6_main_t *im = &ip6_main;
3324 ip_lookup_main_t *lm = &im->lookup_main;
3327 vlib_cli_output (vm, "Protocols handled by ip6_local");
3328 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3330 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3333 u32 node_index = vlib_get_node (vm,
3334 ip6_local_node.index)->
3335 next_nodes[lm->local_next_by_ip_protocol[i]];
3336 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3346 * Display the set of protocols handled by the local IPv6 stack.
3349 * Example of how to display local protocol table:
3350 * @cliexstart{show ip6 local}
3351 * Protocols handled by ip6_local
3359 VLIB_CLI_COMMAND (show_ip6_local, static) =
3361 .path = "show ip6 local",
3362 .function = show_ip6_local_command_fn,
3363 .short_help = "show ip6 local",
3368 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3371 vnet_main_t *vnm = vnet_get_main ();
3372 vnet_interface_main_t *im = &vnm->interface_main;
3373 ip6_main_t *ipm = &ip6_main;
3374 ip_lookup_main_t *lm = &ipm->lookup_main;
3375 vnet_classify_main_t *cm = &vnet_classify_main;
3376 ip6_address_t *if_addr;
3378 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3379 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3381 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3382 return VNET_API_ERROR_NO_SUCH_ENTRY;
3384 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3385 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3387 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3389 if (NULL != if_addr)
3391 fib_prefix_t pfx = {
3393 .fp_proto = FIB_PROTOCOL_IP6,
3394 .fp_addr.ip6 = *if_addr,
3398 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3402 if (table_index != (u32) ~ 0)
3404 dpo_id_t dpo = DPO_INVALID;
3409 classify_dpo_create (DPO_PROTO_IP6, table_index));
3411 fib_table_entry_special_dpo_add (fib_index,
3413 FIB_SOURCE_CLASSIFY,
3414 FIB_ENTRY_FLAG_NONE, &dpo);
3419 fib_table_entry_special_remove (fib_index,
3420 &pfx, FIB_SOURCE_CLASSIFY);
3427 static clib_error_t *
3428 set_ip6_classify_command_fn (vlib_main_t * vm,
3429 unformat_input_t * input,
3430 vlib_cli_command_t * cmd)
3432 u32 table_index = ~0;
3433 int table_index_set = 0;
3434 u32 sw_if_index = ~0;
3437 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3439 if (unformat (input, "table-index %d", &table_index))
3440 table_index_set = 1;
3441 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3442 vnet_get_main (), &sw_if_index))
3448 if (table_index_set == 0)
3449 return clib_error_return (0, "classify table-index must be specified");
3451 if (sw_if_index == ~0)
3452 return clib_error_return (0, "interface / subif must be specified");
3454 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3461 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3462 return clib_error_return (0, "No such interface");
3464 case VNET_API_ERROR_NO_SUCH_ENTRY:
3465 return clib_error_return (0, "No such classifier table");
3471 * Assign a classification table to an interface. The classification
3472 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3473 * commands. Once the table is create, use this command to filter packets
3477 * Example of how to assign a classification table to an interface:
3478 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3481 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3483 .path = "set ip6 classify",
3485 "set ip6 classify intfc <interface> table-index <classify-idx>",
3486 .function = set_ip6_classify_command_fn,
3490 static clib_error_t *
3491 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3493 ip6_main_t *im = &ip6_main;
3498 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3500 if (unformat (input, "hash-buckets %d", &tmp))
3502 else if (unformat (input, "heap-size %U",
3503 unformat_memory_size, &heapsize))
3506 return clib_error_return (0, "unknown input '%U'",
3507 format_unformat_error, input);
3510 im->lookup_table_nbuckets = nbuckets;
3511 im->lookup_table_size = heapsize;
3516 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3519 * fd.io coding-style-patch-verification: ON
3522 * eval: (c-set-style "gnu")