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/ip6_fib.h>
46 #include <vnet/mfib/ip6_mfib.h>
47 #include <vnet/dpo/load_balance.h>
48 #include <vnet/dpo/classify_dpo.h>
50 #include <vppinfra/bihash_template.c>
54 * @brief IPv6 Forwarding.
56 * This file contains the source code for IPv6 forwarding.
60 ip6_forward_next_trace (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
63 vlib_rx_or_tx_t which_adj_index);
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 cpu_index = os_get_cpu_number ();
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);
138 vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
140 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
142 flow_hash_config0 = lb0->lb_hash_config;
143 vnet_buffer (p0)->ip.flow_hash =
144 ip6_compute_flow_hash (ip0, flow_hash_config0);
146 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
148 flow_hash_config1 = lb1->lb_hash_config;
149 vnet_buffer (p1)->ip.flow_hash =
150 ip6_compute_flow_hash (ip1, flow_hash_config1);
153 ASSERT (lb0->lb_n_buckets > 0);
154 ASSERT (lb1->lb_n_buckets > 0);
155 ASSERT (is_pow2 (lb0->lb_n_buckets));
156 ASSERT (is_pow2 (lb1->lb_n_buckets));
157 dpo0 = load_balance_get_bucket_i (lb0,
158 (vnet_buffer (p0)->ip.flow_hash &
159 lb0->lb_n_buckets_minus_1));
160 dpo1 = load_balance_get_bucket_i (lb1,
161 (vnet_buffer (p1)->ip.flow_hash &
162 lb1->lb_n_buckets_minus_1));
164 next0 = dpo0->dpoi_next_node;
165 next1 = dpo1->dpoi_next_node;
167 /* Only process the HBH Option Header if explicitly configured to do so */
169 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
171 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
172 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
175 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
177 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
178 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
180 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
181 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
183 vlib_increment_combined_counter
184 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
185 vlib_increment_combined_counter
186 (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
193 wrong_next = (next0 != next) + 2 * (next1 != next);
194 if (PREDICT_FALSE (wrong_next != 0))
203 vlib_set_next_frame_buffer (vm, node, next0, pi0);
210 vlib_set_next_frame_buffer (vm, node, next1, pi1);
217 vlib_set_next_frame_buffer (vm, node, next0, pi0);
218 vlib_set_next_frame_buffer (vm, node, next1, pi1);
222 vlib_put_next_frame (vm, node, next, n_left_to_next);
224 vlib_get_next_frame (vm, node, next, to_next,
231 while (n_left_from > 0 && n_left_to_next > 0)
236 ip_lookup_next_t next0;
238 ip6_address_t *dst_addr0;
239 u32 fib_index0, flow_hash_config0;
240 const dpo_id_t *dpo0;
245 p0 = vlib_get_buffer (vm, pi0);
247 ip0 = vlib_buffer_get_current (p0);
249 dst_addr0 = &ip0->dst_address;
252 vec_elt (im->fib_index_by_sw_if_index,
253 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
255 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
256 (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
258 flow_hash_config0 = ip6_fib_get (fib_index0)->flow_hash_config;
260 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
262 lb0 = load_balance_get (lbi0);
264 vnet_buffer (p0)->ip.flow_hash = 0;
266 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
268 flow_hash_config0 = lb0->lb_hash_config;
269 vnet_buffer (p0)->ip.flow_hash =
270 ip6_compute_flow_hash (ip0, flow_hash_config0);
273 ASSERT (lb0->lb_n_buckets > 0);
274 ASSERT (is_pow2 (lb0->lb_n_buckets));
275 dpo0 = load_balance_get_bucket_i (lb0,
276 (vnet_buffer (p0)->ip.flow_hash &
277 lb0->lb_n_buckets_minus_1));
278 next0 = dpo0->dpoi_next_node;
280 /* Only process the HBH Option Header if explicitly configured to do so */
282 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
284 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
285 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
287 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
289 vlib_increment_combined_counter
290 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
297 if (PREDICT_FALSE (next0 != next))
300 vlib_put_next_frame (vm, node, next, n_left_to_next);
302 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
309 vlib_put_next_frame (vm, node, next, n_left_to_next);
312 if (node->flags & VLIB_NODE_FLAG_TRACE)
313 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
315 return frame->n_vectors;
319 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
320 ip6_main_t * im, u32 fib_index,
321 ip_interface_address_t * a)
323 ip_lookup_main_t *lm = &im->lookup_main;
324 ip6_address_t *address = ip_interface_address_get_address (lm, a);
326 .fp_len = a->address_length,
327 .fp_proto = FIB_PROTOCOL_IP6,
328 .fp_addr.ip6 = *address,
331 a->neighbor_probe_adj_index = ~0;
332 if (a->address_length < 128)
334 fib_node_index_t fei;
336 fei = fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_ATTACHED), FIB_PROTOCOL_IP6, NULL, /* No next-hop address */
337 sw_if_index, ~0, // invalid FIB index
338 1, NULL, // no label stack
339 FIB_ROUTE_PATH_FLAG_NONE);
340 a->neighbor_probe_adj_index = fib_entry_get_adj (fei);
344 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
346 u32 classify_table_index =
347 lm->classify_table_index_by_sw_if_index[sw_if_index];
348 if (classify_table_index != (u32) ~ 0)
350 dpo_id_t dpo = DPO_INVALID;
355 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
357 fib_table_entry_special_dpo_add (fib_index,
360 FIB_ENTRY_FLAG_NONE, &dpo);
365 fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL), FIB_PROTOCOL_IP6, &pfx.fp_addr, sw_if_index, ~0, // invalid FIB index
366 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
370 ip6_del_interface_routes (ip6_main_t * im,
372 ip6_address_t * address, u32 address_length)
375 .fp_len = address_length,
376 .fp_proto = FIB_PROTOCOL_IP6,
377 .fp_addr.ip6 = *address,
380 if (pfx.fp_len < 128)
382 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
387 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
391 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
393 ip6_main_t *im = &ip6_main;
395 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
398 * enable/disable only on the 1<->0 transition
402 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
407 /* The ref count is 0 when an address is removed from an interface that has
408 * no address - this is not a ciritical error */
409 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
410 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
414 if (sw_if_index != 0)
415 ip6_mfib_interface_enable_disable (sw_if_index, is_enable);
417 vnet_feature_enable_disable ("ip6-unicast", "ip6-lookup", sw_if_index,
420 vnet_feature_enable_disable ("ip6-multicast", "ip6-mfib-forward-lookup",
421 sw_if_index, is_enable, 0, 0);
425 /* get first interface address */
427 ip6_interface_first_address (ip6_main_t * im,
429 ip_interface_address_t ** result_ia)
431 ip_lookup_main_t *lm = &im->lookup_main;
432 ip_interface_address_t *ia = 0;
433 ip6_address_t *result = 0;
436 foreach_ip_interface_address (lm, ia, sw_if_index,
437 1 /* honor unnumbered */,
439 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
445 *result_ia = result ? ia : 0;
450 ip6_add_del_interface_address (vlib_main_t * vm,
452 ip6_address_t * address,
453 u32 address_length, u32 is_del)
455 vnet_main_t *vnm = vnet_get_main ();
456 ip6_main_t *im = &ip6_main;
457 ip_lookup_main_t *lm = &im->lookup_main;
459 u32 if_address_index;
460 ip6_address_fib_t ip6_af, *addr_fib = 0;
462 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
463 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
465 ip6_addr_fib_init (&ip6_af, address,
466 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
467 vec_add1 (addr_fib, ip6_af);
470 uword elts_before = pool_elts (lm->if_address_pool);
472 error = ip_interface_address_add_del
473 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
477 /* Pool did not grow: add duplicate address. */
478 if (elts_before == pool_elts (lm->if_address_pool))
482 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
485 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
487 ip6_add_interface_routes (vnm, sw_if_index,
488 im, ip6_af.fib_index,
489 pool_elt_at_index (lm->if_address_pool,
493 ip6_add_del_interface_address_callback_t *cb;
494 vec_foreach (cb, im->add_del_interface_address_callbacks)
495 cb->function (im, cb->function_opaque, sw_if_index,
496 address, address_length, if_address_index, is_del);
505 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
507 ip6_main_t *im = &ip6_main;
508 ip_interface_address_t *ia;
510 u32 is_admin_up, fib_index;
512 /* Fill in lookup tables with default table (0). */
513 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
515 vec_validate_init_empty (im->
516 lookup_main.if_address_pool_index_by_sw_if_index,
519 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
521 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
524 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
525 0 /* honor unnumbered */,
527 a = ip_interface_address_get_address (&im->lookup_main, ia);
529 ip6_add_interface_routes (vnm, sw_if_index,
533 ip6_del_interface_routes (im, fib_index,
534 a, ia->address_length);
541 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
543 /* Built-in ip6 unicast rx feature path definition */
545 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
547 .arc_name = "ip6-unicast",
548 .start_nodes = VNET_FEATURES ("ip6-input"),
549 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
552 VNET_FEATURE_INIT (ip6_flow_classify, static) =
554 .arc_name = "ip6-unicast",
555 .node_name = "ip6-flow-classify",
556 .runs_before = VNET_FEATURES ("ip6-inacl"),
559 VNET_FEATURE_INIT (ip6_inacl, static) =
561 .arc_name = "ip6-unicast",
562 .node_name = "ip6-inacl",
563 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
566 VNET_FEATURE_INIT (ip6_policer_classify, static) =
568 .arc_name = "ip6-unicast",
569 .node_name = "ip6-policer-classify",
570 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
573 VNET_FEATURE_INIT (ip6_ipsec, static) =
575 .arc_name = "ip6-unicast",
576 .node_name = "ipsec-input-ip6",
577 .runs_before = VNET_FEATURES ("l2tp-decap"),
580 VNET_FEATURE_INIT (ip6_l2tp, static) =
582 .arc_name = "ip6-unicast",
583 .node_name = "l2tp-decap",
584 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
587 VNET_FEATURE_INIT (ip6_vpath, static) =
589 .arc_name = "ip6-unicast",
590 .node_name = "vpath-input-ip6",
591 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
594 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
596 .arc_name = "ip6-unicast",
597 .node_name = "ip6-vxlan-bypass",
598 .runs_before = VNET_FEATURES ("ip6-lookup"),
601 VNET_FEATURE_INIT (ip6_lookup, static) =
603 .arc_name = "ip6-unicast",
604 .node_name = "ip6-lookup",
605 .runs_before = VNET_FEATURES ("ip6-drop"),
608 VNET_FEATURE_INIT (ip6_drop, static) =
610 .arc_name = "ip6-unicast",
611 .node_name = "ip6-drop",
612 .runs_before = 0, /*last feature*/
615 /* Built-in ip6 multicast rx feature path definition (none now) */
616 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
618 .arc_name = "ip6-multicast",
619 .start_nodes = VNET_FEATURES ("ip6-input"),
620 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
623 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
624 .arc_name = "ip6-multicast",
625 .node_name = "vpath-input-ip6",
626 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
629 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
630 .arc_name = "ip6-multicast",
631 .node_name = "ip6-mfib-forward-lookup",
632 .runs_before = VNET_FEATURES ("ip6-drop"),
635 VNET_FEATURE_INIT (ip6_drop_mc, static) = {
636 .arc_name = "ip6-multicast",
637 .node_name = "ip6-drop",
638 .runs_before = 0, /* last feature */
641 /* Built-in ip4 tx feature path definition */
642 VNET_FEATURE_ARC_INIT (ip6_output, static) =
644 .arc_name = "ip6-output",
645 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain"),
646 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
649 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
650 .arc_name = "ip6-output",
651 .node_name = "ipsec-output-ip6",
652 .runs_before = VNET_FEATURES ("interface-output"),
655 VNET_FEATURE_INIT (ip6_interface_output, static) = {
656 .arc_name = "ip6-output",
657 .node_name = "interface-output",
658 .runs_before = 0, /* not before any other features */
663 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
665 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
668 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
671 vnet_feature_enable_disable ("ip6-output", "interface-output", sw_if_index,
674 return /* no error */ 0;
677 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
680 ip6_lookup (vlib_main_t * vm,
681 vlib_node_runtime_t * node, vlib_frame_t * frame)
683 return ip6_lookup_inline (vm, node, frame);
686 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
689 VLIB_REGISTER_NODE (ip6_lookup_node) =
691 .function = ip6_lookup,
692 .name = "ip6-lookup",
693 .vector_size = sizeof (u32),
694 .format_trace = format_ip6_lookup_trace,
695 .n_next_nodes = IP6_LOOKUP_N_NEXT,
696 .next_nodes = IP6_LOOKUP_NEXT_NODES,
700 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
703 ip6_load_balance (vlib_main_t * vm,
704 vlib_node_runtime_t * node, vlib_frame_t * frame)
706 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
707 u32 n_left_from, n_left_to_next, *from, *to_next;
708 ip_lookup_next_t next;
709 u32 cpu_index = os_get_cpu_number ();
710 ip6_main_t *im = &ip6_main;
712 from = vlib_frame_vector_args (frame);
713 n_left_from = frame->n_vectors;
714 next = node->cached_next_index;
716 if (node->flags & VLIB_NODE_FLAG_TRACE)
717 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
719 while (n_left_from > 0)
721 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
724 while (n_left_from >= 4 && n_left_to_next >= 2)
726 ip_lookup_next_t next0, next1;
727 const load_balance_t *lb0, *lb1;
728 vlib_buffer_t *p0, *p1;
729 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
730 const ip6_header_t *ip0, *ip1;
731 const dpo_id_t *dpo0, *dpo1;
733 /* Prefetch next iteration. */
735 vlib_buffer_t *p2, *p3;
737 p2 = vlib_get_buffer (vm, from[2]);
738 p3 = vlib_get_buffer (vm, from[3]);
740 vlib_prefetch_buffer_header (p2, STORE);
741 vlib_prefetch_buffer_header (p3, STORE);
743 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
744 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
747 pi0 = to_next[0] = from[0];
748 pi1 = to_next[1] = from[1];
755 p0 = vlib_get_buffer (vm, pi0);
756 p1 = vlib_get_buffer (vm, pi1);
758 ip0 = vlib_buffer_get_current (p0);
759 ip1 = vlib_buffer_get_current (p1);
760 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
761 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
763 lb0 = load_balance_get (lbi0);
764 lb1 = load_balance_get (lbi1);
767 * this node is for via FIBs we can re-use the hash value from the
768 * to node if present.
769 * We don't want to use the same hash value at each level in the recursion
770 * graph as that would lead to polarisation
772 hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
773 hc1 = vnet_buffer (p1)->ip.flow_hash = 0;
775 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
777 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
779 hc0 = vnet_buffer (p0)->ip.flow_hash =
780 vnet_buffer (p0)->ip.flow_hash >> 1;
784 hc0 = vnet_buffer (p0)->ip.flow_hash =
785 ip6_compute_flow_hash (ip0, hc0);
788 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
790 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
792 hc1 = vnet_buffer (p1)->ip.flow_hash =
793 vnet_buffer (p1)->ip.flow_hash >> 1;
797 hc1 = vnet_buffer (p1)->ip.flow_hash =
798 ip6_compute_flow_hash (ip1, hc1);
803 load_balance_get_bucket_i (lb0,
804 hc0 & (lb0->lb_n_buckets_minus_1));
806 load_balance_get_bucket_i (lb1,
807 hc1 & (lb1->lb_n_buckets_minus_1));
809 next0 = dpo0->dpoi_next_node;
810 next1 = dpo1->dpoi_next_node;
812 /* Only process the HBH Option Header if explicitly configured to do so */
814 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
816 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
817 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
819 /* Only process the HBH Option Header if explicitly configured to do so */
821 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
823 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
824 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
827 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
828 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
830 vlib_increment_combined_counter
831 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
832 vlib_increment_combined_counter
833 (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
835 vlib_validate_buffer_enqueue_x2 (vm, node, next,
836 to_next, n_left_to_next,
837 pi0, pi1, next0, next1);
840 while (n_left_from > 0 && n_left_to_next > 0)
842 ip_lookup_next_t next0;
843 const load_balance_t *lb0;
846 const ip6_header_t *ip0;
847 const dpo_id_t *dpo0;
856 p0 = vlib_get_buffer (vm, pi0);
858 ip0 = vlib_buffer_get_current (p0);
859 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
861 lb0 = load_balance_get (lbi0);
863 hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
864 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
866 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
868 hc0 = vnet_buffer (p0)->ip.flow_hash =
869 vnet_buffer (p0)->ip.flow_hash >> 1;
873 hc0 = vnet_buffer (p0)->ip.flow_hash =
874 ip6_compute_flow_hash (ip0, hc0);
878 load_balance_get_bucket_i (lb0,
879 hc0 & (lb0->lb_n_buckets_minus_1));
881 next0 = dpo0->dpoi_next_node;
882 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
884 /* Only process the HBH Option Header if explicitly configured to do so */
886 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
888 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
889 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
892 vlib_increment_combined_counter
893 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
895 vlib_validate_buffer_enqueue_x1 (vm, node, next,
896 to_next, n_left_to_next,
900 vlib_put_next_frame (vm, node, next, n_left_to_next);
903 return frame->n_vectors;
907 VLIB_REGISTER_NODE (ip6_load_balance_node) =
909 .function = ip6_load_balance,
910 .name = "ip6-load-balance",
911 .vector_size = sizeof (u32),
912 .sibling_of = "ip6-lookup",
913 .format_trace = format_ip6_lookup_trace,
917 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
921 /* Adjacency taken. */
926 /* Packet data, possibly *after* rewrite. */
927 u8 packet_data[128 - 1 * sizeof (u32)];
929 ip6_forward_next_trace_t;
932 format_ip6_forward_next_trace (u8 * s, va_list * args)
934 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
935 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
936 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
937 uword indent = format_get_indent (s);
939 s = format (s, "%U%U",
940 format_white_space, indent,
941 format_ip6_header, t->packet_data, sizeof (t->packet_data));
946 format_ip6_lookup_trace (u8 * s, va_list * args)
948 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
949 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
950 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
951 uword indent = format_get_indent (s);
953 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
954 t->fib_index, t->adj_index, t->flow_hash);
955 s = format (s, "\n%U%U",
956 format_white_space, indent,
957 format_ip6_header, t->packet_data, sizeof (t->packet_data));
963 format_ip6_rewrite_trace (u8 * s, va_list * args)
965 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
966 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
967 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
968 vnet_main_t *vnm = vnet_get_main ();
969 uword indent = format_get_indent (s);
971 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
972 t->fib_index, t->adj_index, format_ip_adjacency,
973 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
974 s = format (s, "\n%U%U",
975 format_white_space, indent,
976 format_ip_adjacency_packet_data,
977 vnm, t->adj_index, t->packet_data, sizeof (t->packet_data));
981 /* Common trace function for all ip6-forward next nodes. */
983 ip6_forward_next_trace (vlib_main_t * vm,
984 vlib_node_runtime_t * node,
985 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
988 ip6_main_t *im = &ip6_main;
990 n_left = frame->n_vectors;
991 from = vlib_frame_vector_args (frame);
996 vlib_buffer_t *b0, *b1;
997 ip6_forward_next_trace_t *t0, *t1;
999 /* Prefetch next iteration. */
1000 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1001 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1006 b0 = vlib_get_buffer (vm, bi0);
1007 b1 = vlib_get_buffer (vm, bi1);
1009 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1011 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1012 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1013 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1015 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1016 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1017 vec_elt (im->fib_index_by_sw_if_index,
1018 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1020 clib_memcpy (t0->packet_data,
1021 vlib_buffer_get_current (b0),
1022 sizeof (t0->packet_data));
1024 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1026 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1027 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1028 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1030 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1031 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1032 vec_elt (im->fib_index_by_sw_if_index,
1033 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
1035 clib_memcpy (t1->packet_data,
1036 vlib_buffer_get_current (b1),
1037 sizeof (t1->packet_data));
1047 ip6_forward_next_trace_t *t0;
1051 b0 = vlib_get_buffer (vm, bi0);
1053 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1055 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1056 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1057 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1059 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1060 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1061 vec_elt (im->fib_index_by_sw_if_index,
1062 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1064 clib_memcpy (t0->packet_data,
1065 vlib_buffer_get_current (b0),
1066 sizeof (t0->packet_data));
1074 ip6_drop_or_punt (vlib_main_t * vm,
1075 vlib_node_runtime_t * node,
1076 vlib_frame_t * frame, ip6_error_t error_code)
1078 u32 *buffers = vlib_frame_vector_args (frame);
1079 uword n_packets = frame->n_vectors;
1081 vlib_error_drop_buffers (vm, node, buffers,
1085 ip6_input_node.index, error_code);
1087 if (node->flags & VLIB_NODE_FLAG_TRACE)
1088 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1094 ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1096 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP);
1100 ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1102 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT);
1106 VLIB_REGISTER_NODE (ip6_drop_node, static) =
1108 .function = ip6_drop,
1110 .vector_size = sizeof (u32),
1111 .format_trace = format_ip6_forward_next_trace,
1115 [0] = "error-drop",},
1119 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
1122 VLIB_REGISTER_NODE (ip6_punt_node, static) =
1124 .function = ip6_punt,
1126 .vector_size = sizeof (u32),
1127 .format_trace = format_ip6_forward_next_trace,
1131 [0] = "error-punt",},
1135 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
1137 /* Compute TCP/UDP/ICMP6 checksum in software. */
1139 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1140 ip6_header_t * ip0, int *bogus_lengthp)
1143 u16 sum16, payload_length_host_byte_order;
1144 u32 i, n_this_buffer, n_bytes_left;
1145 u32 headers_size = sizeof (ip0[0]);
1146 void *data_this_buffer;
1148 ASSERT (bogus_lengthp);
1151 /* Initialize checksum with ip header. */
1152 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1153 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1154 data_this_buffer = (void *) (ip0 + 1);
1156 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1158 sum0 = ip_csum_with_carry (sum0,
1159 clib_mem_unaligned (&ip0->
1160 src_address.as_uword[i],
1163 ip_csum_with_carry (sum0,
1164 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1168 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1169 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1172 ip6_hop_by_hop_ext_t *ext_hdr =
1173 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1175 /* validate really icmp6 next */
1176 ASSERT (ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1178 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1179 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1181 payload_length_host_byte_order -= skip_bytes;
1182 headers_size += skip_bytes;
1185 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1186 if (p0 && n_this_buffer + headers_size > p0->current_length)
1188 p0->current_length >
1189 headers_size ? p0->current_length - headers_size : 0;
1192 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1193 n_bytes_left -= n_this_buffer;
1194 if (n_bytes_left == 0)
1197 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1202 p0 = vlib_get_buffer (vm, p0->next_buffer);
1203 data_this_buffer = vlib_buffer_get_current (p0);
1204 n_this_buffer = p0->current_length;
1207 sum16 = ~ip_csum_fold (sum0);
1213 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1215 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1220 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1221 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1222 || ip0->protocol == IP_PROTOCOL_ICMP6
1223 || ip0->protocol == IP_PROTOCOL_UDP
1224 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1226 udp0 = (void *) (ip0 + 1);
1227 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1229 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1230 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1234 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1236 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1237 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1242 /* ip6_locate_header
1244 * This function is to search for the header specified by the find_hdr number.
1245 * 1. If the find_hdr < 0 then it finds and returns the protocol number and
1246 * offset stored in *offset of the transport or ESP header in the chain if
1248 * 2. If a header with find_hdr > 0 protocol number is found then the
1249 * offset is stored in *offset and protocol number of the header is
1251 * 3. If find_hdr header is not found or packet is malformed or
1252 * it is a non-first fragment -1 is returned.
1255 ip6_locate_header (vlib_buffer_t * p0,
1256 ip6_header_t * ip0, int find_hdr, u32 * offset)
1258 u8 next_proto = ip0->protocol;
1262 u8 *temp_nxthdr = 0;
1265 next_header = ip6_next_header (ip0);
1266 cur_offset = sizeof (ip6_header_t);
1269 done = (next_proto == find_hdr);
1272 (u8 *) vlib_buffer_get_current (p0) + p0->current_length))
1274 //A malicious packet could set an extension header with a too big size
1279 if ((!ip6_ext_hdr (next_proto)) || next_proto == IP_PROTOCOL_IP6_NONXT)
1285 if (next_proto == IP_PROTOCOL_IPV6_FRAGMENTATION)
1287 ip6_frag_hdr_t *frag_hdr = (ip6_frag_hdr_t *) next_header;
1288 u16 frag_off = ip6_frag_hdr_offset (frag_hdr);
1289 /* Non first fragment return -1 */
1292 exthdr_len = sizeof (ip6_frag_hdr_t);
1293 temp_nxthdr = next_header + exthdr_len;
1295 else if (next_proto == IP_PROTOCOL_IPSEC_AH)
1298 ip6_ext_authhdr_len (((ip6_ext_header_t *) next_header));
1299 temp_nxthdr = next_header + exthdr_len;
1304 ip6_ext_header_len (((ip6_ext_header_t *) next_header));
1305 temp_nxthdr = next_header + exthdr_len;
1307 next_proto = ((ip6_ext_header_t *) next_header)->next_hdr;
1308 next_header = temp_nxthdr;
1309 cur_offset += exthdr_len;
1312 *offset = cur_offset;
1313 return (next_proto);
1317 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1319 ip6_main_t *im = &ip6_main;
1320 ip_lookup_main_t *lm = &im->lookup_main;
1321 ip_local_next_t next_index;
1322 u32 *from, *to_next, n_left_from, n_left_to_next;
1323 vlib_node_runtime_t *error_node =
1324 vlib_node_get_runtime (vm, ip6_input_node.index);
1326 from = vlib_frame_vector_args (frame);
1327 n_left_from = frame->n_vectors;
1328 next_index = node->cached_next_index;
1330 if (node->flags & VLIB_NODE_FLAG_TRACE)
1331 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1333 while (n_left_from > 0)
1335 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1337 while (n_left_from >= 4 && n_left_to_next >= 2)
1339 vlib_buffer_t *p0, *p1;
1340 ip6_header_t *ip0, *ip1;
1341 udp_header_t *udp0, *udp1;
1342 u32 pi0, ip_len0, udp_len0, flags0, next0;
1343 u32 pi1, ip_len1, udp_len1, flags1, next1;
1344 i32 len_diff0, len_diff1;
1345 u8 error0, type0, good_l4_checksum0;
1346 u8 error1, type1, good_l4_checksum1;
1347 u32 udp_offset0, udp_offset1;
1349 pi0 = to_next[0] = from[0];
1350 pi1 = to_next[1] = from[1];
1354 n_left_to_next -= 2;
1356 p0 = vlib_get_buffer (vm, pi0);
1357 p1 = vlib_get_buffer (vm, pi1);
1359 ip0 = vlib_buffer_get_current (p0);
1360 ip1 = vlib_buffer_get_current (p1);
1362 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1363 vnet_buffer (p1)->ip.start_of_ip_header = p1->current_data;
1365 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1366 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1368 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1369 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1374 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1375 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1379 /* Skip HBH local processing */
1381 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1383 ip6_hop_by_hop_ext_t *ext_hdr =
1384 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip0);
1385 next0 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1386 type0 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1389 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1391 ip6_hop_by_hop_ext_t *ext_hdr =
1392 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip1);
1393 next1 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1394 type1 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1396 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1400 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1401 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1402 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1403 && udp0->checksum == 0;
1404 /* Verify UDP length. */
1405 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1406 udp_len0 = clib_net_to_host_u16 (udp0->length);
1407 len_diff0 = ip_len0 - udp_len0;
1409 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p1, ip1,
1413 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1414 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1415 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1416 && udp1->checksum == 0;
1417 /* Verify UDP length. */
1418 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1419 udp_len1 = clib_net_to_host_u16 (udp1->length);
1420 len_diff1 = ip_len1 - udp_len1;
1423 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1424 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1426 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1427 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1429 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1430 && !good_l4_checksum0
1431 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1433 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1435 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1437 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1438 && !good_l4_checksum1
1439 && !(flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1441 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1443 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1446 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1448 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1449 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1451 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1452 IP6_ERROR_UDP_CHECKSUM);
1453 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1454 IP6_ERROR_ICMP_CHECKSUM);
1456 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1458 (!good_l4_checksum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1460 /* Drop packets from unroutable hosts. */
1461 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1462 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1463 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1464 !ip6_address_is_link_local_unicast (&ip0->src_address))
1466 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1467 error0 = (ADJ_INDEX_INVALID == src_adj_index0
1468 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1470 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1471 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1472 !ip6_address_is_link_local_unicast (&ip1->src_address))
1474 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1475 error1 = (ADJ_INDEX_INVALID == src_adj_index1
1476 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1480 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1482 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1484 p0->error = error_node->errors[error0];
1485 p1->error = error_node->errors[error1];
1487 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1488 to_next, n_left_to_next,
1489 pi0, pi1, next0, next1);
1492 while (n_left_from > 0 && n_left_to_next > 0)
1497 u32 pi0, ip_len0, udp_len0, flags0, next0;
1499 u8 error0, type0, good_l4_checksum0;
1502 pi0 = to_next[0] = from[0];
1506 n_left_to_next -= 1;
1508 p0 = vlib_get_buffer (vm, pi0);
1510 ip0 = vlib_buffer_get_current (p0);
1512 vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1514 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1515 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1519 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1522 /* Skip HBH local processing */
1524 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1526 ip6_hop_by_hop_ext_t *ext_hdr =
1527 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip0);
1528 next0 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1529 type0 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1531 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1535 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1536 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1537 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1538 && udp0->checksum == 0;
1539 /* Verify UDP length. */
1540 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1541 udp_len0 = clib_net_to_host_u16 (udp0->length);
1542 len_diff0 = ip_len0 - udp_len0;
1545 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1546 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1548 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1549 && !good_l4_checksum0
1550 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1552 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1554 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1557 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1559 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1561 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1562 IP6_ERROR_UDP_CHECKSUM);
1563 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1564 IP6_ERROR_ICMP_CHECKSUM);
1566 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1568 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1569 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1570 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1571 !ip6_address_is_link_local_unicast (&ip0->src_address))
1573 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1574 error0 = (ADJ_INDEX_INVALID == src_adj_index0
1575 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1579 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1581 p0->error = error_node->errors[error0];
1583 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1584 to_next, n_left_to_next,
1588 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1591 return frame->n_vectors;
1595 VLIB_REGISTER_NODE (ip6_local_node, static) =
1597 .function = ip6_local,
1598 .name = "ip6-local",
1599 .vector_size = sizeof (u32),
1600 .format_trace = format_ip6_forward_next_trace,
1601 .n_next_nodes = IP_LOCAL_N_NEXT,
1604 [IP_LOCAL_NEXT_DROP] = "error-drop",
1605 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1606 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1607 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1612 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1615 ip6_register_protocol (u32 protocol, u32 node_index)
1617 vlib_main_t *vm = vlib_get_main ();
1618 ip6_main_t *im = &ip6_main;
1619 ip_lookup_main_t *lm = &im->lookup_main;
1621 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1622 lm->local_next_by_ip_protocol[protocol] =
1623 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1628 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1629 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1630 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1631 } ip6_discover_neighbor_next_t;
1635 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1636 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1637 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1638 } ip6_discover_neighbor_error_t;
1641 ip6_discover_neighbor_inline (vlib_main_t * vm,
1642 vlib_node_runtime_t * node,
1643 vlib_frame_t * frame, int is_glean)
1645 vnet_main_t *vnm = vnet_get_main ();
1646 ip6_main_t *im = &ip6_main;
1647 ip_lookup_main_t *lm = &im->lookup_main;
1648 u32 *from, *to_next_drop;
1649 uword n_left_from, n_left_to_next_drop;
1650 static f64 time_last_seed_change = -1e100;
1651 static u32 hash_seeds[3];
1652 static uword hash_bitmap[256 / BITS (uword)];
1656 if (node->flags & VLIB_NODE_FLAG_TRACE)
1657 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1659 time_now = vlib_time_now (vm);
1660 if (time_now - time_last_seed_change > 1e-3)
1663 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1664 sizeof (hash_seeds));
1665 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1666 hash_seeds[i] = r[i];
1668 /* Mark all hash keys as been not-seen before. */
1669 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1672 time_last_seed_change = time_now;
1675 from = vlib_frame_vector_args (frame);
1676 n_left_from = frame->n_vectors;
1678 while (n_left_from > 0)
1680 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1681 to_next_drop, n_left_to_next_drop);
1683 while (n_left_from > 0 && n_left_to_next_drop > 0)
1687 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1689 ip_adjacency_t *adj0;
1690 vnet_hw_interface_t *hw_if0;
1695 p0 = vlib_get_buffer (vm, pi0);
1697 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1699 ip0 = vlib_buffer_get_current (p0);
1701 adj0 = ip_get_adjacency (lm, adj_index0);
1705 ip0->dst_address.as_u64[0] =
1706 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1707 ip0->dst_address.as_u64[1] =
1708 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1715 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1716 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1719 b0 ^= ip0->dst_address.as_u32[0];
1720 c0 ^= ip0->dst_address.as_u32[1];
1722 hash_v3_mix32 (a0, b0, c0);
1724 b0 ^= ip0->dst_address.as_u32[2];
1725 c0 ^= ip0->dst_address.as_u32[3];
1727 hash_v3_finalize32 (a0, b0, c0);
1729 c0 &= BITS (hash_bitmap) - 1;
1730 c0 = c0 / BITS (uword);
1731 m0 = (uword) 1 << (c0 % BITS (uword));
1733 bm0 = hash_bitmap[c0];
1734 drop0 = (bm0 & m0) != 0;
1736 /* Mark it as seen. */
1737 hash_bitmap[c0] = bm0 | m0;
1741 to_next_drop[0] = pi0;
1743 n_left_to_next_drop -= 1;
1745 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1747 /* If the interface is link-down, drop the pkt */
1748 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1752 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1753 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1758 * the adj has been updated to a rewrite but the node the DPO that got
1759 * us here hasn't - yet. no big deal. we'll drop while we wait.
1761 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1766 icmp6_neighbor_solicitation_header_t *h0;
1769 h0 = vlib_packet_template_get_packet
1770 (vm, &im->discover_neighbor_packet_template, &bi0);
1773 * Build ethernet header.
1774 * Choose source address based on destination lookup
1777 if (ip6_src_address_for_packet (lm,
1779 &h0->ip.src_address))
1781 /* There is no address on the interface */
1783 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1784 vlib_buffer_free (vm, &bi0, 1);
1789 * Destination address is a solicited node multicast address.
1790 * We need to fill in
1791 * the low 24 bits with low 24 bits of target's address.
1793 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1794 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1795 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1797 h0->neighbor.target_address = ip0->dst_address;
1799 clib_memcpy (h0->link_layer_option.ethernet_address,
1800 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1802 /* $$$$ appears we need this; why is the checksum non-zero? */
1803 h0->neighbor.icmp.checksum = 0;
1804 h0->neighbor.icmp.checksum =
1805 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1808 ASSERT (bogus_length == 0);
1810 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1811 b0 = vlib_get_buffer (vm, bi0);
1812 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1813 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1815 /* Add rewrite/encap string. */
1816 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1817 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1819 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1821 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1825 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1826 n_left_to_next_drop);
1829 return frame->n_vectors;
1833 ip6_discover_neighbor (vlib_main_t * vm,
1834 vlib_node_runtime_t * node, vlib_frame_t * frame)
1836 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1840 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1842 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1845 static char *ip6_discover_neighbor_error_strings[] = {
1846 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1847 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1848 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1849 = "no source address for ND solicitation",
1853 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1855 .function = ip6_discover_neighbor,
1856 .name = "ip6-discover-neighbor",
1857 .vector_size = sizeof (u32),
1858 .format_trace = format_ip6_forward_next_trace,
1859 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1860 .error_strings = ip6_discover_neighbor_error_strings,
1861 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1864 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1865 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1871 VLIB_REGISTER_NODE (ip6_glean_node) =
1873 .function = ip6_glean,
1874 .name = "ip6-glean",
1875 .vector_size = sizeof (u32),
1876 .format_trace = format_ip6_forward_next_trace,
1877 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1878 .error_strings = ip6_discover_neighbor_error_strings,
1879 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1882 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1883 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1889 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1891 vnet_main_t *vnm = vnet_get_main ();
1892 ip6_main_t *im = &ip6_main;
1893 icmp6_neighbor_solicitation_header_t *h;
1895 ip_interface_address_t *ia;
1896 ip_adjacency_t *adj;
1897 vnet_hw_interface_t *hi;
1898 vnet_sw_interface_t *si;
1903 si = vnet_get_sw_interface (vnm, sw_if_index);
1905 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1907 return clib_error_return (0, "%U: interface %U down",
1908 format_ip6_address, dst,
1909 format_vnet_sw_if_index_name, vnm,
1914 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1917 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1918 return clib_error_return
1919 (0, "no matching interface address for destination %U (interface %U)",
1920 format_ip6_address, dst,
1921 format_vnet_sw_if_index_name, vnm, sw_if_index);
1925 vlib_packet_template_get_packet (vm,
1926 &im->discover_neighbor_packet_template,
1929 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1931 /* Destination address is a solicited node multicast address. We need to fill in
1932 the low 24 bits with low 24 bits of target's address. */
1933 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1934 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1935 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1937 h->ip.src_address = src[0];
1938 h->neighbor.target_address = dst[0];
1940 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1941 vec_len (hi->hw_address));
1943 h->neighbor.icmp.checksum =
1944 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1945 ASSERT (bogus_length == 0);
1947 b = vlib_get_buffer (vm, bi);
1948 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1949 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1951 /* Add encapsulation string for software interface (e.g. ethernet header). */
1952 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
1953 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1954 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1957 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1958 u32 *to_next = vlib_frame_vector_args (f);
1961 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1964 return /* no error */ 0;
1969 IP6_REWRITE_NEXT_DROP,
1970 IP6_REWRITE_NEXT_ICMP_ERROR,
1971 } ip6_rewrite_next_t;
1974 ip6_rewrite_inline (vlib_main_t * vm,
1975 vlib_node_runtime_t * node,
1976 vlib_frame_t * frame, int is_midchain, int is_mcast)
1978 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1979 u32 *from = vlib_frame_vector_args (frame);
1980 u32 n_left_from, n_left_to_next, *to_next, next_index;
1981 vlib_node_runtime_t *error_node =
1982 vlib_node_get_runtime (vm, ip6_input_node.index);
1984 n_left_from = frame->n_vectors;
1985 next_index = node->cached_next_index;
1986 u32 cpu_index = os_get_cpu_number ();
1988 while (n_left_from > 0)
1990 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1992 while (n_left_from >= 4 && n_left_to_next >= 2)
1994 ip_adjacency_t *adj0, *adj1;
1995 vlib_buffer_t *p0, *p1;
1996 ip6_header_t *ip0, *ip1;
1997 u32 pi0, rw_len0, next0, error0, adj_index0;
1998 u32 pi1, rw_len1, next1, error1, adj_index1;
1999 u32 tx_sw_if_index0, tx_sw_if_index1;
2001 /* Prefetch next iteration. */
2003 vlib_buffer_t *p2, *p3;
2005 p2 = vlib_get_buffer (vm, from[2]);
2006 p3 = vlib_get_buffer (vm, from[3]);
2008 vlib_prefetch_buffer_header (p2, LOAD);
2009 vlib_prefetch_buffer_header (p3, LOAD);
2011 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2012 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2014 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2015 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2018 pi0 = to_next[0] = from[0];
2019 pi1 = to_next[1] = from[1];
2024 n_left_to_next -= 2;
2026 p0 = vlib_get_buffer (vm, pi0);
2027 p1 = vlib_get_buffer (vm, pi1);
2029 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2030 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
2032 /* We should never rewrite a pkt using the MISS adjacency */
2033 ASSERT (adj_index0 && adj_index1);
2035 ip0 = vlib_buffer_get_current (p0);
2036 ip1 = vlib_buffer_get_current (p1);
2038 error0 = error1 = IP6_ERROR_NONE;
2039 next0 = next1 = IP6_REWRITE_NEXT_DROP;
2041 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2043 i32 hop_limit0 = ip0->hop_limit;
2045 /* Input node should have reject packets with hop limit 0. */
2046 ASSERT (ip0->hop_limit > 0);
2050 ip0->hop_limit = hop_limit0;
2053 * If the hop count drops below 1 when forwarding, generate
2056 if (PREDICT_FALSE (hop_limit0 <= 0))
2058 error0 = IP6_ERROR_TIME_EXPIRED;
2059 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2060 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2061 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2062 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2068 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2070 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2072 i32 hop_limit1 = ip1->hop_limit;
2074 /* Input node should have reject packets with hop limit 0. */
2075 ASSERT (ip1->hop_limit > 0);
2079 ip1->hop_limit = hop_limit1;
2082 * If the hop count drops below 1 when forwarding, generate
2085 if (PREDICT_FALSE (hop_limit1 <= 0))
2087 error1 = IP6_ERROR_TIME_EXPIRED;
2088 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2089 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2090 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2091 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2097 p1->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2099 adj0 = ip_get_adjacency (lm, adj_index0);
2100 adj1 = ip_get_adjacency (lm, adj_index1);
2102 rw_len0 = adj0[0].rewrite_header.data_bytes;
2103 rw_len1 = adj1[0].rewrite_header.data_bytes;
2104 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2105 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
2107 vlib_increment_combined_counter
2108 (&adjacency_counters,
2110 adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2111 vlib_increment_combined_counter
2112 (&adjacency_counters,
2113 cpu_index, adj_index1,
2114 1, vlib_buffer_length_in_chain (vm, p1) + rw_len1);
2116 /* Check MTU of outgoing interface. */
2118 (vlib_buffer_length_in_chain (vm, p0) >
2120 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2123 (vlib_buffer_length_in_chain (vm, p1) >
2125 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2128 /* Don't adjust the buffer for hop count issue; icmp-error node
2129 * wants to see the IP headerr */
2130 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2132 p0->current_data -= rw_len0;
2133 p0->current_length += rw_len0;
2135 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2136 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2137 next0 = adj0[0].rewrite_header.next_index;
2139 vnet_feature_arc_start (lm->output_feature_arc_index,
2140 tx_sw_if_index0, &next0, p0);
2142 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2144 p1->current_data -= rw_len1;
2145 p1->current_length += rw_len1;
2147 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2148 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2149 next1 = adj1[0].rewrite_header.next_index;
2151 vnet_feature_arc_start (lm->output_feature_arc_index,
2152 tx_sw_if_index1, &next1, p1);
2155 /* Guess we are only writing on simple Ethernet header. */
2156 vnet_rewrite_two_headers (adj0[0], adj1[0],
2157 ip0, ip1, sizeof (ethernet_header_t));
2161 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2162 adj1->sub_type.midchain.fixup_func (vm, adj1, p1);
2167 * copy bytes from the IP address into the MAC rewrite
2169 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0, 0);
2170 vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1, 0);
2173 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2174 to_next, n_left_to_next,
2175 pi0, pi1, next0, next1);
2178 while (n_left_from > 0 && n_left_to_next > 0)
2180 ip_adjacency_t *adj0;
2184 u32 adj_index0, next0, error0;
2185 u32 tx_sw_if_index0;
2187 pi0 = to_next[0] = from[0];
2189 p0 = vlib_get_buffer (vm, pi0);
2191 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2193 /* We should never rewrite a pkt using the MISS adjacency */
2194 ASSERT (adj_index0);
2196 adj0 = ip_get_adjacency (lm, adj_index0);
2198 ip0 = vlib_buffer_get_current (p0);
2200 error0 = IP6_ERROR_NONE;
2201 next0 = IP6_REWRITE_NEXT_DROP;
2203 /* Check hop limit */
2204 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2206 i32 hop_limit0 = ip0->hop_limit;
2208 ASSERT (ip0->hop_limit > 0);
2212 ip0->hop_limit = hop_limit0;
2214 if (PREDICT_FALSE (hop_limit0 <= 0))
2217 * If the hop count drops below 1 when forwarding, generate
2220 error0 = IP6_ERROR_TIME_EXPIRED;
2221 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2222 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2223 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2224 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2230 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2233 /* Guess we are only writing on simple Ethernet header. */
2234 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2236 /* Update packet buffer attributes/set output interface. */
2237 rw_len0 = adj0[0].rewrite_header.data_bytes;
2238 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2240 vlib_increment_combined_counter
2241 (&adjacency_counters,
2243 adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2245 /* Check MTU of outgoing interface. */
2247 (vlib_buffer_length_in_chain (vm, p0) >
2249 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2252 /* Don't adjust the buffer for hop count issue; icmp-error node
2253 * wants to see the IP headerr */
2254 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2256 p0->current_data -= rw_len0;
2257 p0->current_length += rw_len0;
2259 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2261 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2262 next0 = adj0[0].rewrite_header.next_index;
2264 vnet_feature_arc_start (lm->output_feature_arc_index,
2265 tx_sw_if_index0, &next0, p0);
2270 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2274 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0, 0);
2277 p0->error = error_node->errors[error0];
2282 n_left_to_next -= 1;
2284 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2285 to_next, n_left_to_next,
2289 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2292 /* Need to do trace after rewrites to pick up new packet data. */
2293 if (node->flags & VLIB_NODE_FLAG_TRACE)
2294 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2296 return frame->n_vectors;
2300 ip6_rewrite (vlib_main_t * vm,
2301 vlib_node_runtime_t * node, vlib_frame_t * frame)
2303 return ip6_rewrite_inline (vm, node, frame, 0, 0);
2307 ip6_rewrite_mcast (vlib_main_t * vm,
2308 vlib_node_runtime_t * node, vlib_frame_t * frame)
2310 return ip6_rewrite_inline (vm, node, frame, 0, 1);
2314 ip6_midchain (vlib_main_t * vm,
2315 vlib_node_runtime_t * node, vlib_frame_t * frame)
2317 return ip6_rewrite_inline (vm, node, frame, 1, 0);
2321 VLIB_REGISTER_NODE (ip6_midchain_node) =
2323 .function = ip6_midchain,
2324 .name = "ip6-midchain",
2325 .vector_size = sizeof (u32),
2326 .format_trace = format_ip6_forward_next_trace,
2327 .sibling_of = "ip6-rewrite",
2331 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2334 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2336 .function = ip6_rewrite,
2337 .name = "ip6-rewrite",
2338 .vector_size = sizeof (u32),
2339 .format_trace = format_ip6_rewrite_trace,
2343 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2344 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2349 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2352 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2354 .function = ip6_rewrite_mcast,
2355 .name = "ip6-rewrite-mcast",
2356 .vector_size = sizeof (u32),
2357 .format_trace = format_ip6_rewrite_trace,
2358 .sibling_of = "ip6-rewrite",
2362 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2365 * Hop-by-Hop handling
2367 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2369 #define foreach_ip6_hop_by_hop_error \
2370 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2371 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2372 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2377 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2378 foreach_ip6_hop_by_hop_error
2380 IP6_HOP_BY_HOP_N_ERROR,
2381 } ip6_hop_by_hop_error_t;
2385 * Primary h-b-h handler trace support
2386 * We work pretty hard on the problem for obvious reasons
2392 u8 option_data[256];
2393 } ip6_hop_by_hop_trace_t;
2395 vlib_node_registration_t ip6_hop_by_hop_node;
2397 static char *ip6_hop_by_hop_error_strings[] = {
2398 #define _(sym,string) string,
2399 foreach_ip6_hop_by_hop_error
2404 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2406 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2407 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2408 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2409 ip6_hop_by_hop_header_t *hbh0;
2410 ip6_hop_by_hop_option_t *opt0, *limit0;
2411 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2415 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2417 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2418 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2420 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2421 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2423 while (opt0 < limit0)
2428 case 0: /* Pad, just stop */
2429 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2433 if (hm->trace[type0])
2435 s = (*hm->trace[type0]) (s, opt0);
2440 format (s, "\n unrecognized option %d length %d", type0,
2444 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2445 sizeof (ip6_hop_by_hop_option_t));
2453 ip6_scan_hbh_options (vlib_buffer_t * b0,
2455 ip6_hop_by_hop_header_t * hbh0,
2456 ip6_hop_by_hop_option_t * opt0,
2457 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2459 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2463 while (opt0 < limit0)
2469 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2474 if (hm->options[type0])
2476 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2478 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2484 /* Unrecognized mandatory option, check the two high order bits */
2485 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2487 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2489 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2490 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2491 *next0 = IP_LOOKUP_NEXT_DROP;
2493 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2494 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2495 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2496 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2497 ICMP6_parameter_problem_unrecognized_option,
2498 (u8 *) opt0 - (u8 *) ip0);
2500 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2501 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2502 if (!ip6_address_is_multicast (&ip0->dst_address))
2504 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2505 icmp6_error_set_vnet_buffer (b0,
2506 ICMP6_parameter_problem,
2507 ICMP6_parameter_problem_unrecognized_option,
2508 (u8 *) opt0 - (u8 *) ip0);
2512 *next0 = IP_LOOKUP_NEXT_DROP;
2520 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2521 sizeof (ip6_hop_by_hop_option_t));
2527 * Process the Hop-by-Hop Options header
2530 ip6_hop_by_hop (vlib_main_t * vm,
2531 vlib_node_runtime_t * node, vlib_frame_t * frame)
2533 vlib_node_runtime_t *error_node =
2534 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2535 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2536 u32 n_left_from, *from, *to_next;
2537 ip_lookup_next_t next_index;
2538 ip6_main_t *im = &ip6_main;
2539 ip_lookup_main_t *lm = &im->lookup_main;
2541 from = vlib_frame_vector_args (frame);
2542 n_left_from = frame->n_vectors;
2543 next_index = node->cached_next_index;
2545 while (n_left_from > 0)
2549 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2551 while (n_left_from >= 4 && n_left_to_next >= 2)
2554 vlib_buffer_t *b0, *b1;
2556 ip6_header_t *ip0, *ip1;
2557 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2558 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2559 u8 error0 = 0, error1 = 0;
2561 /* Prefetch next iteration. */
2563 vlib_buffer_t *p2, *p3;
2565 p2 = vlib_get_buffer (vm, from[2]);
2566 p3 = vlib_get_buffer (vm, from[3]);
2568 vlib_prefetch_buffer_header (p2, LOAD);
2569 vlib_prefetch_buffer_header (p3, LOAD);
2571 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2572 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2575 /* Speculatively enqueue b0, b1 to the current next frame */
2576 to_next[0] = bi0 = from[0];
2577 to_next[1] = bi1 = from[1];
2581 n_left_to_next -= 2;
2583 b0 = vlib_get_buffer (vm, bi0);
2584 b1 = vlib_get_buffer (vm, bi1);
2586 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2587 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2588 ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2589 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2590 ip_adjacency_t *adj1 = ip_get_adjacency (lm, adj_index1);
2592 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2593 next0 = adj0->lookup_next_index;
2594 next1 = adj1->lookup_next_index;
2596 ip0 = vlib_buffer_get_current (b0);
2597 ip1 = vlib_buffer_get_current (b1);
2598 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2599 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2600 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2601 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2603 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2604 ((hbh0->length + 1) << 3));
2606 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2607 ((hbh1->length + 1) << 3));
2610 * Basic validity checks
2612 if ((hbh0->length + 1) << 3 >
2613 clib_net_to_host_u16 (ip0->payload_length))
2615 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2616 next0 = IP_LOOKUP_NEXT_DROP;
2619 /* Scan the set of h-b-h options, process ones that we understand */
2620 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2622 if ((hbh1->length + 1) << 3 >
2623 clib_net_to_host_u16 (ip1->payload_length))
2625 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2626 next1 = IP_LOOKUP_NEXT_DROP;
2629 /* Scan the set of h-b-h options, process ones that we understand */
2630 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2633 /* Has the classifier flagged this buffer for special treatment? */
2636 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2637 next0 = hm->next_override;
2639 /* Has the classifier flagged this buffer for special treatment? */
2642 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2643 next1 = hm->next_override;
2645 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2647 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2649 ip6_hop_by_hop_trace_t *t =
2650 vlib_add_trace (vm, node, b0, sizeof (*t));
2651 u32 trace_len = (hbh0->length + 1) << 3;
2652 t->next_index = next0;
2653 /* Capture the h-b-h option verbatim */
2656 ARRAY_LEN (t->option_data) ? trace_len :
2657 ARRAY_LEN (t->option_data);
2658 t->trace_len = trace_len;
2659 clib_memcpy (t->option_data, hbh0, trace_len);
2661 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2663 ip6_hop_by_hop_trace_t *t =
2664 vlib_add_trace (vm, node, b1, sizeof (*t));
2665 u32 trace_len = (hbh1->length + 1) << 3;
2666 t->next_index = next1;
2667 /* Capture the h-b-h option verbatim */
2670 ARRAY_LEN (t->option_data) ? trace_len :
2671 ARRAY_LEN (t->option_data);
2672 t->trace_len = trace_len;
2673 clib_memcpy (t->option_data, hbh1, trace_len);
2678 b0->error = error_node->errors[error0];
2679 b1->error = error_node->errors[error1];
2681 /* verify speculative enqueue, maybe switch current next frame */
2682 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2683 n_left_to_next, bi0, bi1, next0,
2687 while (n_left_from > 0 && n_left_to_next > 0)
2693 ip6_hop_by_hop_header_t *hbh0;
2694 ip6_hop_by_hop_option_t *opt0, *limit0;
2697 /* Speculatively enqueue b0 to the current next frame */
2703 n_left_to_next -= 1;
2705 b0 = vlib_get_buffer (vm, bi0);
2707 * Default use the next_index from the adjacency.
2708 * A HBH option rarely redirects to a different node
2710 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2711 ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2712 next0 = adj0->lookup_next_index;
2714 ip0 = vlib_buffer_get_current (b0);
2715 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2716 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2718 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2719 ((hbh0->length + 1) << 3));
2722 * Basic validity checks
2724 if ((hbh0->length + 1) << 3 >
2725 clib_net_to_host_u16 (ip0->payload_length))
2727 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2728 next0 = IP_LOOKUP_NEXT_DROP;
2732 /* Scan the set of h-b-h options, process ones that we understand */
2733 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2736 /* Has the classifier flagged this buffer for special treatment? */
2739 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2740 next0 = hm->next_override;
2742 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2744 ip6_hop_by_hop_trace_t *t =
2745 vlib_add_trace (vm, node, b0, sizeof (*t));
2746 u32 trace_len = (hbh0->length + 1) << 3;
2747 t->next_index = next0;
2748 /* Capture the h-b-h option verbatim */
2751 ARRAY_LEN (t->option_data) ? trace_len :
2752 ARRAY_LEN (t->option_data);
2753 t->trace_len = trace_len;
2754 clib_memcpy (t->option_data, hbh0, trace_len);
2757 b0->error = error_node->errors[error0];
2759 /* verify speculative enqueue, maybe switch current next frame */
2760 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2761 n_left_to_next, bi0, next0);
2763 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2765 return frame->n_vectors;
2769 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2771 .function = ip6_hop_by_hop,
2772 .name = "ip6-hop-by-hop",
2773 .sibling_of = "ip6-lookup",
2774 .vector_size = sizeof (u32),
2775 .format_trace = format_ip6_hop_by_hop_trace,
2776 .type = VLIB_NODE_TYPE_INTERNAL,
2777 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2778 .error_strings = ip6_hop_by_hop_error_strings,
2783 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2785 static clib_error_t *
2786 ip6_hop_by_hop_init (vlib_main_t * vm)
2788 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2789 memset (hm->options, 0, sizeof (hm->options));
2790 memset (hm->trace, 0, sizeof (hm->trace));
2791 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2795 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2798 ip6_hbh_set_next_override (uword next)
2800 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2802 hm->next_override = next;
2806 ip6_hbh_register_option (u8 option,
2807 int options (vlib_buffer_t * b, ip6_header_t * ip,
2808 ip6_hop_by_hop_option_t * opt),
2809 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2811 ip6_main_t *im = &ip6_main;
2812 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2814 ASSERT (option < ARRAY_LEN (hm->options));
2816 /* Already registered */
2817 if (hm->options[option])
2820 hm->options[option] = options;
2821 hm->trace[option] = trace;
2823 /* Set global variable */
2824 im->hbh_enabled = 1;
2830 ip6_hbh_unregister_option (u8 option)
2832 ip6_main_t *im = &ip6_main;
2833 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2835 ASSERT (option < ARRAY_LEN (hm->options));
2837 /* Not registered */
2838 if (!hm->options[option])
2841 hm->options[option] = NULL;
2842 hm->trace[option] = NULL;
2844 /* Disable global knob if this was the last option configured */
2847 for (i = 0; i < 256; i++)
2849 if (hm->options[option])
2856 im->hbh_enabled = 0;
2861 /* Global IP6 main. */
2862 ip6_main_t ip6_main;
2864 static clib_error_t *
2865 ip6_lookup_init (vlib_main_t * vm)
2867 ip6_main_t *im = &ip6_main;
2868 clib_error_t *error;
2871 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2874 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2881 for (j = 0; j < i0; j++)
2882 im->fib_masks[i].as_u32[j] = ~0;
2885 im->fib_masks[i].as_u32[i0] =
2886 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2889 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2891 if (im->lookup_table_nbuckets == 0)
2892 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2894 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2896 if (im->lookup_table_size == 0)
2897 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2899 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2900 "ip6 FIB fwding table",
2901 im->lookup_table_nbuckets, im->lookup_table_size);
2902 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2903 "ip6 FIB non-fwding table",
2904 im->lookup_table_nbuckets, im->lookup_table_size);
2906 /* Create FIB with index 0 and table id of 0. */
2907 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2908 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2912 pn = pg_get_node (ip6_lookup_node.index);
2913 pn->unformat_edit = unformat_pg_ip6_header;
2916 /* Unless explicitly configured, don't process HBH options */
2917 im->hbh_enabled = 0;
2920 icmp6_neighbor_solicitation_header_t p;
2922 memset (&p, 0, sizeof (p));
2924 p.ip.ip_version_traffic_class_and_flow_label =
2925 clib_host_to_net_u32 (0x6 << 28);
2926 p.ip.payload_length =
2927 clib_host_to_net_u16 (sizeof (p) -
2929 (icmp6_neighbor_solicitation_header_t, neighbor));
2930 p.ip.protocol = IP_PROTOCOL_ICMP6;
2931 p.ip.hop_limit = 255;
2932 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2934 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2936 p.link_layer_option.header.type =
2937 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2938 p.link_layer_option.header.n_data_u64s =
2939 sizeof (p.link_layer_option) / sizeof (u64);
2941 vlib_packet_template_init (vm,
2942 &im->discover_neighbor_packet_template,
2944 /* alloc chunk size */ 8,
2945 "ip6 neighbor discovery");
2951 VLIB_INIT_FUNCTION (ip6_lookup_init);
2953 static clib_error_t *
2954 add_del_ip6_interface_table (vlib_main_t * vm,
2955 unformat_input_t * input,
2956 vlib_cli_command_t * cmd)
2958 vnet_main_t *vnm = vnet_get_main ();
2959 clib_error_t *error = 0;
2960 u32 sw_if_index, table_id;
2964 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2966 error = clib_error_return (0, "unknown interface `%U'",
2967 format_unformat_error, input);
2971 if (unformat (input, "%d", &table_id))
2975 error = clib_error_return (0, "expected table id `%U'",
2976 format_unformat_error, input);
2981 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2984 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
2985 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
2987 fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2990 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
2991 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = fib_index;
3000 * Place the indicated interface into the supplied IPv6 FIB table (also known
3001 * as a VRF). If the FIB table does not exist, this command creates it. To
3002 * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
3003 * FIB table will only be displayed if a route has been added to the table, or
3004 * an IP Address is assigned to an interface in the table (which adds a route
3007 * @note IP addresses added after setting the interface IP table end up in
3008 * the indicated FIB table. If the IP address is added prior to adding the
3009 * interface to the FIB table, it will NOT be part of the FIB table. Predictable
3010 * but potentially counter-intuitive results occur if you provision interface
3011 * addresses in multiple FIBs. Upon RX, packets will be processed in the last
3012 * IP table ID provisioned. It might be marginally useful to evade source RPF
3013 * drops to put an interface address into multiple FIBs.
3016 * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
3017 * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
3020 VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
3022 .path = "set interface ip6 table",
3023 .function = add_del_ip6_interface_table,
3024 .short_help = "set interface ip6 table <interface> <table-id>"
3029 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
3032 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3033 /* Invert the "u" bit */
3034 ip->as_u8[8] = mac[0] ^ (1 << 1);
3035 ip->as_u8[9] = mac[1];
3036 ip->as_u8[10] = mac[2];
3037 ip->as_u8[11] = 0xFF;
3038 ip->as_u8[12] = 0xFE;
3039 ip->as_u8[13] = mac[3];
3040 ip->as_u8[14] = mac[4];
3041 ip->as_u8[15] = mac[5];
3045 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3048 /* Invert the previously inverted "u" bit */
3049 mac[0] = ip->as_u8[8] ^ (1 << 1);
3050 mac[1] = ip->as_u8[9];
3051 mac[2] = ip->as_u8[10];
3052 mac[3] = ip->as_u8[13];
3053 mac[4] = ip->as_u8[14];
3054 mac[5] = ip->as_u8[15];
3057 static clib_error_t *
3058 test_ip6_link_command_fn (vlib_main_t * vm,
3059 unformat_input_t * input, vlib_cli_command_t * cmd)
3062 ip6_address_t _a, *a = &_a;
3064 if (unformat (input, "%U", unformat_ethernet_address, mac))
3066 ip6_link_local_address_from_ethernet_mac_address (a, mac);
3067 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
3068 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3069 vlib_cli_output (vm, "Original MAC address: %U",
3070 format_ethernet_address, mac);
3077 * This command converts the given MAC Address into an IPv6 link-local
3081 * Example of how to create an IPv6 link-local address:
3082 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3083 * Link local address: fe80::14d9:e0ff:fe91:7986
3084 * Original MAC address: 16:d9:e0:91:79:86
3088 VLIB_CLI_COMMAND (test_link_command, static) =
3090 .path = "test ip6 link",
3091 .function = test_ip6_link_command_fn,
3092 .short_help = "test ip6 link <mac-address>",
3097 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3099 ip6_main_t *im6 = &ip6_main;
3101 uword *p = hash_get (im6->fib_index_by_table_id, table_id);
3106 fib = ip6_fib_get (p[0]);
3108 fib->flow_hash_config = flow_hash_config;
3112 static clib_error_t *
3113 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3114 unformat_input_t * input,
3115 vlib_cli_command_t * cmd)
3119 u32 flow_hash_config = 0;
3122 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3124 if (unformat (input, "table %d", &table_id))
3127 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3128 foreach_flow_hash_bit
3135 return clib_error_return (0, "unknown input `%U'",
3136 format_unformat_error, input);
3138 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3145 return clib_error_return (0, "no such FIB table %d", table_id);
3148 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3156 * Configure the set of IPv6 fields used by the flow hash.
3160 * Example of how to set the flow hash on a given table:
3161 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3163 * Example of display the configured flow hash:
3164 * @cliexstart{show ip6 fib}
3165 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3168 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3169 * [0] [@0]: dpo-drop ip6
3172 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3173 * [0] [@2]: dpo-receive
3176 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3177 * [0] [@2]: dpo-receive
3180 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3181 * [0] [@2]: dpo-receive
3184 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3185 * [0] [@2]: dpo-receive
3186 * ff02::1:ff00:0/104
3188 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3189 * [0] [@2]: dpo-receive
3190 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3193 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3194 * [0] [@0]: dpo-drop ip6
3197 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3198 * [0] [@4]: ipv6-glean: af_packet0
3201 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3202 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3205 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3206 * [0] [@2]: dpo-receive
3207 * fe80::fe:3eff:fe3e:9222/128
3209 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3210 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3213 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3214 * [0] [@2]: dpo-receive
3217 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3218 * [0] [@2]: dpo-receive
3221 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3222 * [0] [@2]: dpo-receive
3223 * ff02::1:ff00:0/104
3225 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3226 * [0] [@2]: dpo-receive
3231 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3233 .path = "set ip6 flow-hash",
3235 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3236 .function = set_ip6_flow_hash_command_fn,
3240 static clib_error_t *
3241 show_ip6_local_command_fn (vlib_main_t * vm,
3242 unformat_input_t * input, vlib_cli_command_t * cmd)
3244 ip6_main_t *im = &ip6_main;
3245 ip_lookup_main_t *lm = &im->lookup_main;
3248 vlib_cli_output (vm, "Protocols handled by ip6_local");
3249 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3251 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3252 vlib_cli_output (vm, "%d", i);
3260 * Display the set of protocols handled by the local IPv6 stack.
3263 * Example of how to display local protocol table:
3264 * @cliexstart{show ip6 local}
3265 * Protocols handled by ip6_local
3273 VLIB_CLI_COMMAND (show_ip6_local, static) =
3275 .path = "show ip6 local",
3276 .function = show_ip6_local_command_fn,
3277 .short_help = "show ip6 local",
3282 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3285 vnet_main_t *vnm = vnet_get_main ();
3286 vnet_interface_main_t *im = &vnm->interface_main;
3287 ip6_main_t *ipm = &ip6_main;
3288 ip_lookup_main_t *lm = &ipm->lookup_main;
3289 vnet_classify_main_t *cm = &vnet_classify_main;
3290 ip6_address_t *if_addr;
3292 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3293 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3295 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3296 return VNET_API_ERROR_NO_SUCH_ENTRY;
3298 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3299 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3301 if_addr = ip6_interface_first_address (ipm, sw_if_index, NULL);
3303 if (NULL != if_addr)
3305 fib_prefix_t pfx = {
3307 .fp_proto = FIB_PROTOCOL_IP6,
3308 .fp_addr.ip6 = *if_addr,
3312 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3316 if (table_index != (u32) ~ 0)
3318 dpo_id_t dpo = DPO_INVALID;
3323 classify_dpo_create (DPO_PROTO_IP6, table_index));
3325 fib_table_entry_special_dpo_add (fib_index,
3327 FIB_SOURCE_CLASSIFY,
3328 FIB_ENTRY_FLAG_NONE, &dpo);
3333 fib_table_entry_special_remove (fib_index,
3334 &pfx, FIB_SOURCE_CLASSIFY);
3341 static clib_error_t *
3342 set_ip6_classify_command_fn (vlib_main_t * vm,
3343 unformat_input_t * input,
3344 vlib_cli_command_t * cmd)
3346 u32 table_index = ~0;
3347 int table_index_set = 0;
3348 u32 sw_if_index = ~0;
3351 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3353 if (unformat (input, "table-index %d", &table_index))
3354 table_index_set = 1;
3355 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3356 vnet_get_main (), &sw_if_index))
3362 if (table_index_set == 0)
3363 return clib_error_return (0, "classify table-index must be specified");
3365 if (sw_if_index == ~0)
3366 return clib_error_return (0, "interface / subif must be specified");
3368 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3375 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3376 return clib_error_return (0, "No such interface");
3378 case VNET_API_ERROR_NO_SUCH_ENTRY:
3379 return clib_error_return (0, "No such classifier table");
3385 * Assign a classification table to an interface. The classification
3386 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3387 * commands. Once the table is create, use this command to filter packets
3391 * Example of how to assign a classification table to an interface:
3392 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3395 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3397 .path = "set ip6 classify",
3399 "set ip6 classify intfc <interface> table-index <classify-idx>",
3400 .function = set_ip6_classify_command_fn,
3404 static clib_error_t *
3405 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3407 ip6_main_t *im = &ip6_main;
3412 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3414 if (unformat (input, "hash-buckets %d", &tmp))
3416 else if (unformat (input, "heap-size %dm", &tmp))
3417 heapsize = ((u64) tmp) << 20;
3418 else if (unformat (input, "heap-size %dM", &tmp))
3419 heapsize = ((u64) tmp) << 20;
3420 else if (unformat (input, "heap-size %dg", &tmp))
3421 heapsize = ((u64) tmp) << 30;
3422 else if (unformat (input, "heap-size %dG", &tmp))
3423 heapsize = ((u64) tmp) << 30;
3425 return clib_error_return (0, "unknown input '%U'",
3426 format_unformat_error, input);
3429 im->lookup_table_nbuckets = nbuckets;
3430 im->lookup_table_size = heapsize;
3435 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3438 * fd.io coding-style-patch-verification: ON
3441 * eval: (c-set-style "gnu")