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>
53 #include <vnet/ip/ip6_forward.h>
55 /* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
56 #define OI_DECAP 0x80000000
59 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
60 ip6_main_t * im, u32 fib_index,
61 ip_interface_address_t * a)
63 ip_lookup_main_t *lm = &im->lookup_main;
64 ip6_address_t *address = ip_interface_address_get_address (lm, a);
66 .fp_len = a->address_length,
67 .fp_proto = FIB_PROTOCOL_IP6,
68 .fp_addr.ip6 = *address,
71 if (a->address_length < 128)
73 fib_table_entry_update_one_path (fib_index,
76 (FIB_ENTRY_FLAG_CONNECTED |
77 FIB_ENTRY_FLAG_ATTACHED),
79 /* No next-hop address */
81 /* invalid FIB index */
84 NULL, FIB_ROUTE_PATH_FLAG_NONE);
88 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
90 u32 classify_table_index =
91 lm->classify_table_index_by_sw_if_index[sw_if_index];
92 if (classify_table_index != (u32) ~ 0)
94 dpo_id_t dpo = DPO_INVALID;
99 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
101 fib_table_entry_special_dpo_add (fib_index,
104 FIB_ENTRY_FLAG_NONE, &dpo);
109 fib_table_entry_update_one_path (fib_index, &pfx,
110 FIB_SOURCE_INTERFACE,
111 (FIB_ENTRY_FLAG_CONNECTED |
112 FIB_ENTRY_FLAG_LOCAL),
116 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
120 ip6_del_interface_routes (ip6_main_t * im,
122 ip6_address_t * address, u32 address_length)
125 .fp_len = address_length,
126 .fp_proto = FIB_PROTOCOL_IP6,
127 .fp_addr.ip6 = *address,
130 if (pfx.fp_len < 128)
132 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
137 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
141 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
143 ip6_main_t *im = &ip6_main;
145 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
148 * enable/disable only on the 1<->0 transition
152 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
157 /* The ref count is 0 when an address is removed from an interface that has
158 * no address - this is not a ciritical error */
159 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
160 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
164 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
167 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
168 sw_if_index, !is_enable, 0, 0);
171 /* get first interface address */
173 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
175 ip_lookup_main_t *lm = &im->lookup_main;
176 ip_interface_address_t *ia = 0;
177 ip6_address_t *result = 0;
180 foreach_ip_interface_address (lm, ia, sw_if_index,
181 1 /* honor unnumbered */,
183 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
192 ip6_add_del_interface_address (vlib_main_t * vm,
194 ip6_address_t * address,
195 u32 address_length, u32 is_del)
197 vnet_main_t *vnm = vnet_get_main ();
198 ip6_main_t *im = &ip6_main;
199 ip_lookup_main_t *lm = &im->lookup_main;
201 u32 if_address_index;
202 ip6_address_fib_t ip6_af, *addr_fib = 0;
204 /* local0 interface doesn't support IP addressing */
205 if (sw_if_index == 0)
208 clib_error_create ("local0 interface doesn't support IP addressing");
211 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
212 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
214 ip6_addr_fib_init (&ip6_af, address,
215 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
216 vec_add1 (addr_fib, ip6_af);
219 uword elts_before = pool_elts (lm->if_address_pool);
221 error = ip_interface_address_add_del
222 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
226 /* Pool did not grow: add duplicate address. */
227 if (elts_before == pool_elts (lm->if_address_pool))
231 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
234 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
236 ip6_add_interface_routes (vnm, sw_if_index,
237 im, ip6_af.fib_index,
238 pool_elt_at_index (lm->if_address_pool,
242 ip6_add_del_interface_address_callback_t *cb;
243 vec_foreach (cb, im->add_del_interface_address_callbacks)
244 cb->function (im, cb->function_opaque, sw_if_index,
245 address, address_length, if_address_index, is_del);
254 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
256 ip6_main_t *im = &ip6_main;
257 ip_interface_address_t *ia;
259 u32 is_admin_up, fib_index;
261 /* Fill in lookup tables with default table (0). */
262 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
264 vec_validate_init_empty (im->
265 lookup_main.if_address_pool_index_by_sw_if_index,
268 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
270 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
273 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
274 0 /* honor unnumbered */,
276 a = ip_interface_address_get_address (&im->lookup_main, ia);
278 ip6_add_interface_routes (vnm, sw_if_index,
282 ip6_del_interface_routes (im, fib_index,
283 a, ia->address_length);
290 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
292 /* Built-in ip6 unicast rx feature path definition */
294 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
296 .arc_name = "ip6-unicast",
297 .start_nodes = VNET_FEATURES ("ip6-input"),
298 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
301 VNET_FEATURE_INIT (ip6_flow_classify, static) =
303 .arc_name = "ip6-unicast",
304 .node_name = "ip6-flow-classify",
305 .runs_before = VNET_FEATURES ("ip6-inacl"),
308 VNET_FEATURE_INIT (ip6_inacl, static) =
310 .arc_name = "ip6-unicast",
311 .node_name = "ip6-inacl",
312 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
315 VNET_FEATURE_INIT (ip6_policer_classify, static) =
317 .arc_name = "ip6-unicast",
318 .node_name = "ip6-policer-classify",
319 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
322 VNET_FEATURE_INIT (ip6_ipsec, static) =
324 .arc_name = "ip6-unicast",
325 .node_name = "ipsec-input-ip6",
326 .runs_before = VNET_FEATURES ("l2tp-decap"),
329 VNET_FEATURE_INIT (ip6_l2tp, static) =
331 .arc_name = "ip6-unicast",
332 .node_name = "l2tp-decap",
333 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
336 VNET_FEATURE_INIT (ip6_vpath, static) =
338 .arc_name = "ip6-unicast",
339 .node_name = "vpath-input-ip6",
340 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
343 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
345 .arc_name = "ip6-unicast",
346 .node_name = "ip6-vxlan-bypass",
347 .runs_before = VNET_FEATURES ("ip6-lookup"),
350 VNET_FEATURE_INIT (ip6_not_enabled, static) =
352 .arc_name = "ip6-unicast",
353 .node_name = "ip6-not-enabled",
354 .runs_before = VNET_FEATURES ("ip6-lookup"),
357 VNET_FEATURE_INIT (ip6_lookup, static) =
359 .arc_name = "ip6-unicast",
360 .node_name = "ip6-lookup",
361 .runs_before = 0, /*last feature*/
364 /* Built-in ip6 multicast rx feature path definition (none now) */
365 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
367 .arc_name = "ip6-multicast",
368 .start_nodes = VNET_FEATURES ("ip6-input"),
369 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
372 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
373 .arc_name = "ip6-multicast",
374 .node_name = "vpath-input-ip6",
375 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
378 VNET_FEATURE_INIT (ip6_not_enabled_mc, static) = {
379 .arc_name = "ip6-multicast",
380 .node_name = "ip6-not-enabled",
381 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
384 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
385 .arc_name = "ip6-multicast",
386 .node_name = "ip6-mfib-forward-lookup",
387 .runs_before = 0, /* last feature */
390 /* Built-in ip4 tx feature path definition */
391 VNET_FEATURE_ARC_INIT (ip6_output, static) =
393 .arc_name = "ip6-output",
394 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain", "ip6-dvr-dpo"),
395 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
398 VNET_FEATURE_INIT (ip6_outacl, static) = {
399 .arc_name = "ip6-output",
400 .node_name = "ip6-outacl",
401 .runs_before = VNET_FEATURES ("ipsec-output-ip6"),
404 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
405 .arc_name = "ip6-output",
406 .node_name = "ipsec-output-ip6",
407 .runs_before = VNET_FEATURES ("interface-output"),
410 VNET_FEATURE_INIT (ip6_interface_output, static) = {
411 .arc_name = "ip6-output",
412 .node_name = "interface-output",
413 .runs_before = 0, /* not before any other features */
418 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
420 ip6_main_t *im = &ip6_main;
422 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
423 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
427 /* Ensure that IPv6 is disabled */
428 ip6_main_t *im6 = &ip6_main;
429 ip_lookup_main_t *lm6 = &im6->lookup_main;
430 ip_interface_address_t *ia = 0;
431 ip6_address_t *address;
432 vlib_main_t *vm = vlib_get_main ();
434 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0 /* is_add */ );
435 vnet_sw_interface_update_unnumbered (sw_if_index, ~0, 0);
437 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
439 address = ip_interface_address_get_address (lm6, ia);
440 ip6_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1);
443 ip6_mfib_interface_enable_disable (sw_if_index, 0);
446 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
449 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
450 sw_if_index, is_add, 0, 0);
452 return /* no error */ 0;
455 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
458 ip6_lookup (vlib_main_t * vm,
459 vlib_node_runtime_t * node, vlib_frame_t * frame)
461 return ip6_lookup_inline (vm, node, frame);
464 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
467 VLIB_REGISTER_NODE (ip6_lookup_node) =
469 .function = ip6_lookup,
470 .name = "ip6-lookup",
471 .vector_size = sizeof (u32),
472 .format_trace = format_ip6_lookup_trace,
473 .n_next_nodes = IP6_LOOKUP_N_NEXT,
474 .next_nodes = IP6_LOOKUP_NEXT_NODES,
478 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
481 ip6_load_balance (vlib_main_t * vm,
482 vlib_node_runtime_t * node, vlib_frame_t * frame)
484 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
485 u32 n_left_from, n_left_to_next, *from, *to_next;
486 ip_lookup_next_t next;
487 u32 thread_index = vlib_get_thread_index ();
488 ip6_main_t *im = &ip6_main;
490 from = vlib_frame_vector_args (frame);
491 n_left_from = frame->n_vectors;
492 next = node->cached_next_index;
494 if (node->flags & VLIB_NODE_FLAG_TRACE)
495 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
497 while (n_left_from > 0)
499 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
502 while (n_left_from >= 4 && n_left_to_next >= 2)
504 ip_lookup_next_t next0, next1;
505 const load_balance_t *lb0, *lb1;
506 vlib_buffer_t *p0, *p1;
507 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
508 const ip6_header_t *ip0, *ip1;
509 const dpo_id_t *dpo0, *dpo1;
511 /* Prefetch next iteration. */
513 vlib_buffer_t *p2, *p3;
515 p2 = vlib_get_buffer (vm, from[2]);
516 p3 = vlib_get_buffer (vm, from[3]);
518 vlib_prefetch_buffer_header (p2, STORE);
519 vlib_prefetch_buffer_header (p3, STORE);
521 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
522 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
525 pi0 = to_next[0] = from[0];
526 pi1 = to_next[1] = from[1];
533 p0 = vlib_get_buffer (vm, pi0);
534 p1 = vlib_get_buffer (vm, pi1);
536 ip0 = vlib_buffer_get_current (p0);
537 ip1 = vlib_buffer_get_current (p1);
538 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
539 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
541 lb0 = load_balance_get (lbi0);
542 lb1 = load_balance_get (lbi1);
545 * this node is for via FIBs we can re-use the hash value from the
546 * to node if present.
547 * We don't want to use the same hash value at each level in the recursion
548 * graph as that would lead to polarisation
552 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
554 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
556 hc0 = vnet_buffer (p0)->ip.flow_hash =
557 vnet_buffer (p0)->ip.flow_hash >> 1;
561 hc0 = vnet_buffer (p0)->ip.flow_hash =
562 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
565 load_balance_get_fwd_bucket (lb0,
567 lb0->lb_n_buckets_minus_1));
571 dpo0 = load_balance_get_bucket_i (lb0, 0);
573 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
575 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
577 hc1 = vnet_buffer (p1)->ip.flow_hash =
578 vnet_buffer (p1)->ip.flow_hash >> 1;
582 hc1 = vnet_buffer (p1)->ip.flow_hash =
583 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
586 load_balance_get_fwd_bucket (lb1,
588 lb1->lb_n_buckets_minus_1));
592 dpo1 = load_balance_get_bucket_i (lb1, 0);
595 next0 = dpo0->dpoi_next_node;
596 next1 = dpo1->dpoi_next_node;
598 /* Only process the HBH Option Header if explicitly configured to do so */
600 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
602 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
603 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
605 /* Only process the HBH Option Header if explicitly configured to do so */
607 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
609 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
610 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
613 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
614 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
616 vlib_increment_combined_counter
617 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
618 vlib_increment_combined_counter
619 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
621 vlib_validate_buffer_enqueue_x2 (vm, node, next,
622 to_next, n_left_to_next,
623 pi0, pi1, next0, next1);
626 while (n_left_from > 0 && n_left_to_next > 0)
628 ip_lookup_next_t next0;
629 const load_balance_t *lb0;
632 const ip6_header_t *ip0;
633 const dpo_id_t *dpo0;
642 p0 = vlib_get_buffer (vm, pi0);
644 ip0 = vlib_buffer_get_current (p0);
645 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
647 lb0 = load_balance_get (lbi0);
650 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
652 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
654 hc0 = vnet_buffer (p0)->ip.flow_hash =
655 vnet_buffer (p0)->ip.flow_hash >> 1;
659 hc0 = vnet_buffer (p0)->ip.flow_hash =
660 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
663 load_balance_get_fwd_bucket (lb0,
665 lb0->lb_n_buckets_minus_1));
669 dpo0 = load_balance_get_bucket_i (lb0, 0);
672 next0 = dpo0->dpoi_next_node;
673 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
675 /* Only process the HBH Option Header if explicitly configured to do so */
677 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
679 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
680 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
683 vlib_increment_combined_counter
684 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
686 vlib_validate_buffer_enqueue_x1 (vm, node, next,
687 to_next, n_left_to_next,
691 vlib_put_next_frame (vm, node, next, n_left_to_next);
694 return frame->n_vectors;
698 VLIB_REGISTER_NODE (ip6_load_balance_node) =
700 .function = ip6_load_balance,
701 .name = "ip6-load-balance",
702 .vector_size = sizeof (u32),
703 .sibling_of = "ip6-lookup",
704 .format_trace = format_ip6_lookup_trace,
708 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
712 /* Adjacency taken. */
717 /* Packet data, possibly *after* rewrite. */
718 u8 packet_data[128 - 1 * sizeof (u32)];
720 ip6_forward_next_trace_t;
723 format_ip6_forward_next_trace (u8 * s, va_list * args)
725 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
726 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
727 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
728 u32 indent = format_get_indent (s);
730 s = format (s, "%U%U",
731 format_white_space, indent,
732 format_ip6_header, t->packet_data, sizeof (t->packet_data));
737 format_ip6_lookup_trace (u8 * s, va_list * args)
739 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
740 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
741 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
742 u32 indent = format_get_indent (s);
744 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
745 t->fib_index, t->adj_index, t->flow_hash);
746 s = format (s, "\n%U%U",
747 format_white_space, indent,
748 format_ip6_header, t->packet_data, sizeof (t->packet_data));
754 format_ip6_rewrite_trace (u8 * s, va_list * args)
756 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
757 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
758 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
759 u32 indent = format_get_indent (s);
761 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
762 t->fib_index, t->adj_index, format_ip_adjacency,
763 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
764 s = format (s, "\n%U%U",
765 format_white_space, indent,
766 format_ip_adjacency_packet_data,
767 t->adj_index, t->packet_data, sizeof (t->packet_data));
771 /* Common trace function for all ip6-forward next nodes. */
773 ip6_forward_next_trace (vlib_main_t * vm,
774 vlib_node_runtime_t * node,
775 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
778 ip6_main_t *im = &ip6_main;
780 n_left = frame->n_vectors;
781 from = vlib_frame_vector_args (frame);
786 vlib_buffer_t *b0, *b1;
787 ip6_forward_next_trace_t *t0, *t1;
789 /* Prefetch next iteration. */
790 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
791 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
796 b0 = vlib_get_buffer (vm, bi0);
797 b1 = vlib_get_buffer (vm, bi1);
799 if (b0->flags & VLIB_BUFFER_IS_TRACED)
801 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
802 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
803 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
805 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
806 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
807 vec_elt (im->fib_index_by_sw_if_index,
808 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
810 clib_memcpy (t0->packet_data,
811 vlib_buffer_get_current (b0),
812 sizeof (t0->packet_data));
814 if (b1->flags & VLIB_BUFFER_IS_TRACED)
816 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
817 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
818 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
820 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
821 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
822 vec_elt (im->fib_index_by_sw_if_index,
823 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
825 clib_memcpy (t1->packet_data,
826 vlib_buffer_get_current (b1),
827 sizeof (t1->packet_data));
837 ip6_forward_next_trace_t *t0;
841 b0 = vlib_get_buffer (vm, bi0);
843 if (b0->flags & VLIB_BUFFER_IS_TRACED)
845 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
846 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
847 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
849 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
850 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
851 vec_elt (im->fib_index_by_sw_if_index,
852 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
854 clib_memcpy (t0->packet_data,
855 vlib_buffer_get_current (b0),
856 sizeof (t0->packet_data));
863 /* Compute TCP/UDP/ICMP6 checksum in software. */
865 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
866 ip6_header_t * ip0, int *bogus_lengthp)
869 u16 sum16, payload_length_host_byte_order;
870 u32 i, n_this_buffer, n_bytes_left;
871 u32 headers_size = sizeof (ip0[0]);
872 void *data_this_buffer;
874 ASSERT (bogus_lengthp);
877 /* Initialize checksum with ip header. */
878 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
879 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
880 data_this_buffer = (void *) (ip0 + 1);
882 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
884 sum0 = ip_csum_with_carry (sum0,
885 clib_mem_unaligned (&ip0->
886 src_address.as_uword[i],
889 ip_csum_with_carry (sum0,
890 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
894 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
895 * or UDP-Ping packets */
896 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
899 ip6_hop_by_hop_ext_t *ext_hdr =
900 (ip6_hop_by_hop_ext_t *) data_this_buffer;
902 /* validate really icmp6 next */
903 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
904 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
906 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
907 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
909 payload_length_host_byte_order -= skip_bytes;
910 headers_size += skip_bytes;
913 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
914 if (p0 && n_this_buffer + headers_size > p0->current_length)
917 headers_size ? p0->current_length - headers_size : 0;
920 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
921 n_bytes_left -= n_this_buffer;
922 if (n_bytes_left == 0)
925 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
930 p0 = vlib_get_buffer (vm, p0->next_buffer);
931 data_this_buffer = vlib_buffer_get_current (p0);
932 n_this_buffer = p0->current_length;
935 sum16 = ~ip_csum_fold (sum0);
941 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
943 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
948 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
949 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
950 || ip0->protocol == IP_PROTOCOL_ICMP6
951 || ip0->protocol == IP_PROTOCOL_UDP
952 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
954 udp0 = (void *) (ip0 + 1);
955 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
957 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
958 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
962 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
964 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
965 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
971 * @brief returns number of links on which src is reachable.
974 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
976 const load_balance_t *lb0;
980 fib_index = vec_elt (im->fib_index_by_sw_if_index,
981 vnet_buffer (b)->sw_if_index[VLIB_RX]);
983 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
984 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
986 lbi = ip6_fib_table_fwding_lookup (im, fib_index, &i->src_address);
987 lb0 = load_balance_get (lbi);
989 return (fib_urpf_check_size (lb0->lb_urpf));
993 ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
997 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
998 if (proto0 != IP_PROTOCOL_UDP)
1000 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1001 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1007 VNET_FEATURE_ARC_INIT (ip6_local) =
1009 .arc_name = "ip6-local",
1010 .start_nodes = VNET_FEATURES ("ip6-local"),
1015 ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1016 vlib_frame_t * frame, int head_of_feature_arc)
1018 ip6_main_t *im = &ip6_main;
1019 ip_lookup_main_t *lm = &im->lookup_main;
1020 ip_local_next_t next_index;
1021 u32 *from, *to_next, n_left_from, n_left_to_next;
1022 vlib_node_runtime_t *error_node =
1023 vlib_node_get_runtime (vm, ip6_input_node.index);
1024 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
1026 from = vlib_frame_vector_args (frame);
1027 n_left_from = frame->n_vectors;
1028 next_index = node->cached_next_index;
1030 if (node->flags & VLIB_NODE_FLAG_TRACE)
1031 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1033 while (n_left_from > 0)
1035 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1037 while (n_left_from >= 4 && n_left_to_next >= 2)
1039 vlib_buffer_t *p0, *p1;
1040 ip6_header_t *ip0, *ip1;
1041 udp_header_t *udp0, *udp1;
1042 u32 pi0, ip_len0, udp_len0, flags0, next0;
1043 u32 pi1, ip_len1, udp_len1, flags1, next1;
1044 i32 len_diff0, len_diff1;
1045 u8 error0, type0, good_l4_csum0, is_tcp_udp0;
1046 u8 error1, type1, good_l4_csum1, is_tcp_udp1;
1047 u32 udp_offset0, udp_offset1;
1049 pi0 = to_next[0] = from[0];
1050 pi1 = to_next[1] = from[1];
1054 n_left_to_next -= 2;
1056 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1058 p0 = vlib_get_buffer (vm, pi0);
1059 p1 = vlib_get_buffer (vm, pi1);
1061 ip0 = vlib_buffer_get_current (p0);
1062 ip1 = vlib_buffer_get_current (p1);
1064 if (head_of_feature_arc == 0)
1067 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1068 vnet_buffer (p1)->l3_hdr_offset = p1->current_data;
1070 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1071 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1076 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1077 is_tcp_udp1 = ip6_next_proto_is_tcp_udp (p1, ip1, &udp_offset1);
1079 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1080 || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1081 || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1083 good_l4_csum1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1084 || (flags1 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1085 || flags1 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1090 if (PREDICT_TRUE (is_tcp_udp0))
1092 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1093 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1094 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1095 && udp0->checksum == 0;
1096 /* Verify UDP length. */
1097 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1099 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1100 udp_len0 = clib_net_to_host_u16 (udp0->length);
1101 len_diff0 = ip_len0 - udp_len0;
1104 if (PREDICT_TRUE (is_tcp_udp1))
1106 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1107 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1108 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1109 && udp1->checksum == 0;
1110 /* Verify UDP length. */
1111 if (is_tcp_udp1 == IP_PROTOCOL_UDP)
1113 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1114 udp_len1 = clib_net_to_host_u16 (udp1->length);
1115 len_diff1 = ip_len1 - udp_len1;
1119 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1120 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1122 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1123 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1125 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1128 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1130 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1132 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1134 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1137 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1139 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1141 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1144 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1145 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1146 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1148 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1149 IP6_ERROR_UDP_CHECKSUM);
1150 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1151 IP6_ERROR_ICMP_CHECKSUM);
1152 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1153 error1 = (!good_l4_csum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1155 /* Drop packets from unroutable hosts. */
1156 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1157 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1158 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1159 !ip6_address_is_link_local_unicast (&ip0->src_address))
1161 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1162 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1164 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1165 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1166 !ip6_address_is_link_local_unicast (&ip1->src_address))
1168 error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1169 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1172 /* TODO maybe move to lookup? */
1173 vnet_buffer (p0)->ip.fib_index =
1174 vec_elt (im->fib_index_by_sw_if_index,
1175 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1176 vnet_buffer (p0)->ip.fib_index =
1177 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1178 (u32) ~ 0) ? vnet_buffer (p0)->ip.
1179 fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1181 vnet_buffer (p1)->ip.fib_index =
1182 vec_elt (im->fib_index_by_sw_if_index,
1183 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
1184 vnet_buffer (p1)->ip.fib_index =
1185 (vnet_buffer (p1)->sw_if_index[VLIB_TX] ==
1186 (u32) ~ 0) ? vnet_buffer (p1)->ip.
1187 fib_index : vnet_buffer (p1)->sw_if_index[VLIB_TX];
1192 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1193 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1196 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1198 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1200 p0->error = error_node->errors[error0];
1201 p1->error = error_node->errors[error1];
1203 if (head_of_feature_arc)
1205 if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1206 vnet_feature_arc_start (arc_index,
1207 vnet_buffer (p0)->sw_if_index
1208 [VLIB_RX], &next0, p0);
1209 if (PREDICT_TRUE (error1 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1210 vnet_feature_arc_start (arc_index,
1211 vnet_buffer (p1)->sw_if_index
1212 [VLIB_RX], &next1, p1);
1215 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1216 to_next, n_left_to_next,
1217 pi0, pi1, next0, next1);
1220 while (n_left_from > 0 && n_left_to_next > 0)
1225 u32 pi0, ip_len0, udp_len0, flags0, next0;
1227 u8 error0, type0, good_l4_csum0;
1231 pi0 = to_next[0] = from[0];
1235 n_left_to_next -= 1;
1237 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1239 p0 = vlib_get_buffer (vm, pi0);
1240 ip0 = vlib_buffer_get_current (p0);
1242 if (head_of_feature_arc == 0)
1245 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1247 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1249 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1250 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1251 || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1252 || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1256 if (PREDICT_TRUE (is_tcp_udp0))
1258 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1259 /* Don't verify UDP checksum for packets with explicit zero
1261 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1262 && udp0->checksum == 0;
1263 /* Verify UDP length. */
1264 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1266 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1267 udp_len0 = clib_net_to_host_u16 (udp0->length);
1268 len_diff0 = ip_len0 - udp_len0;
1272 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1273 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1275 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1278 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1280 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1282 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1285 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1286 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1288 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1289 IP6_ERROR_UDP_CHECKSUM);
1290 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1291 IP6_ERROR_ICMP_CHECKSUM);
1292 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1294 /* If this is a neighbor solicitation (ICMP), skip src RPF check */
1295 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1296 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1297 !ip6_address_is_link_local_unicast (&ip0->src_address))
1299 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1300 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1303 vnet_buffer (p0)->ip.fib_index =
1304 vec_elt (im->fib_index_by_sw_if_index,
1305 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1306 vnet_buffer (p0)->ip.fib_index =
1307 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1308 (u32) ~ 0) ? vnet_buffer (p0)->ip.
1309 fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1313 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1315 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1317 p0->error = error_node->errors[error0];
1319 if (head_of_feature_arc)
1321 if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1322 vnet_feature_arc_start (arc_index,
1323 vnet_buffer (p0)->sw_if_index
1324 [VLIB_RX], &next0, p0);
1327 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1328 to_next, n_left_to_next,
1332 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1335 return frame->n_vectors;
1339 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1341 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1345 VLIB_REGISTER_NODE (ip6_local_node, static) =
1347 .function = ip6_local,
1348 .name = "ip6-local",
1349 .vector_size = sizeof (u32),
1350 .format_trace = format_ip6_forward_next_trace,
1351 .n_next_nodes = IP_LOCAL_N_NEXT,
1354 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1355 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
1356 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1357 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1362 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1366 ip6_local_end_of_arc (vlib_main_t * vm,
1367 vlib_node_runtime_t * node, vlib_frame_t * frame)
1369 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1373 VLIB_REGISTER_NODE (ip6_local_end_of_arc_node,static) = {
1374 .function = ip6_local_end_of_arc,
1375 .name = "ip6-local-end-of-arc",
1376 .vector_size = sizeof (u32),
1378 .format_trace = format_ip6_forward_next_trace,
1379 .sibling_of = "ip6-local",
1382 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_end_of_arc_node, ip6_local_end_of_arc)
1384 VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1385 .arc_name = "ip6-local",
1386 .node_name = "ip6-local-end-of-arc",
1387 .runs_before = 0, /* not before any other features */
1392 ip6_register_protocol (u32 protocol, u32 node_index)
1394 vlib_main_t *vm = vlib_get_main ();
1395 ip6_main_t *im = &ip6_main;
1396 ip_lookup_main_t *lm = &im->lookup_main;
1398 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1399 lm->local_next_by_ip_protocol[protocol] =
1400 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1405 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1406 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1407 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1408 } ip6_discover_neighbor_next_t;
1412 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1413 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1414 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1415 } ip6_discover_neighbor_error_t;
1418 ip6_discover_neighbor_inline (vlib_main_t * vm,
1419 vlib_node_runtime_t * node,
1420 vlib_frame_t * frame, int is_glean)
1422 vnet_main_t *vnm = vnet_get_main ();
1423 ip6_main_t *im = &ip6_main;
1424 ip_lookup_main_t *lm = &im->lookup_main;
1425 u32 *from, *to_next_drop;
1426 uword n_left_from, n_left_to_next_drop;
1427 static f64 time_last_seed_change = -1e100;
1428 static u32 hash_seeds[3];
1429 static uword hash_bitmap[256 / BITS (uword)];
1433 if (node->flags & VLIB_NODE_FLAG_TRACE)
1434 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1436 time_now = vlib_time_now (vm);
1437 if (time_now - time_last_seed_change > 1e-3)
1440 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1441 sizeof (hash_seeds));
1442 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1443 hash_seeds[i] = r[i];
1445 /* Mark all hash keys as been not-seen before. */
1446 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1449 time_last_seed_change = time_now;
1452 from = vlib_frame_vector_args (frame);
1453 n_left_from = frame->n_vectors;
1455 while (n_left_from > 0)
1457 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1458 to_next_drop, n_left_to_next_drop);
1460 while (n_left_from > 0 && n_left_to_next_drop > 0)
1464 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1466 ip_adjacency_t *adj0;
1467 vnet_hw_interface_t *hw_if0;
1472 p0 = vlib_get_buffer (vm, pi0);
1474 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1476 ip0 = vlib_buffer_get_current (p0);
1478 adj0 = adj_get (adj_index0);
1482 ip0->dst_address.as_u64[0] =
1483 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1484 ip0->dst_address.as_u64[1] =
1485 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1492 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1493 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1496 b0 ^= ip0->dst_address.as_u32[0];
1497 c0 ^= ip0->dst_address.as_u32[1];
1499 hash_v3_mix32 (a0, b0, c0);
1501 b0 ^= ip0->dst_address.as_u32[2];
1502 c0 ^= ip0->dst_address.as_u32[3];
1504 hash_v3_finalize32 (a0, b0, c0);
1506 c0 &= BITS (hash_bitmap) - 1;
1507 c0 = c0 / BITS (uword);
1508 m0 = (uword) 1 << (c0 % BITS (uword));
1510 bm0 = hash_bitmap[c0];
1511 drop0 = (bm0 & m0) != 0;
1513 /* Mark it as seen. */
1514 hash_bitmap[c0] = bm0 | m0;
1518 to_next_drop[0] = pi0;
1520 n_left_to_next_drop -= 1;
1522 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1524 /* If the interface is link-down, drop the pkt */
1525 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1529 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1530 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1535 * the adj has been updated to a rewrite but the node the DPO that got
1536 * us here hasn't - yet. no big deal. we'll drop while we wait.
1538 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1543 icmp6_neighbor_solicitation_header_t *h0;
1546 h0 = vlib_packet_template_get_packet
1547 (vm, &im->discover_neighbor_packet_template, &bi0);
1550 * Build ethernet header.
1551 * Choose source address based on destination lookup
1554 if (!ip6_src_address_for_packet (lm,
1557 &h0->ip.src_address))
1559 /* There is no address on the interface */
1561 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1562 vlib_buffer_free (vm, &bi0, 1);
1567 * Destination address is a solicited node multicast address.
1568 * We need to fill in
1569 * the low 24 bits with low 24 bits of target's address.
1571 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1572 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1573 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1575 h0->neighbor.target_address = ip0->dst_address;
1577 clib_memcpy (h0->link_layer_option.ethernet_address,
1578 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1580 /* $$$$ appears we need this; why is the checksum non-zero? */
1581 h0->neighbor.icmp.checksum = 0;
1582 h0->neighbor.icmp.checksum =
1583 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1586 ASSERT (bogus_length == 0);
1588 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1589 b0 = vlib_get_buffer (vm, bi0);
1590 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1591 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1593 /* Add rewrite/encap string. */
1594 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1595 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1597 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1599 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1603 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1604 n_left_to_next_drop);
1607 return frame->n_vectors;
1611 ip6_discover_neighbor (vlib_main_t * vm,
1612 vlib_node_runtime_t * node, vlib_frame_t * frame)
1614 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1618 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1620 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1623 static char *ip6_discover_neighbor_error_strings[] = {
1624 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1625 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1626 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1627 = "no source address for ND solicitation",
1631 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1633 .function = ip6_discover_neighbor,
1634 .name = "ip6-discover-neighbor",
1635 .vector_size = sizeof (u32),
1636 .format_trace = format_ip6_forward_next_trace,
1637 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1638 .error_strings = ip6_discover_neighbor_error_strings,
1639 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1642 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
1643 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1649 VLIB_REGISTER_NODE (ip6_glean_node) =
1651 .function = ip6_glean,
1652 .name = "ip6-glean",
1653 .vector_size = sizeof (u32),
1654 .format_trace = format_ip6_forward_next_trace,
1655 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1656 .error_strings = ip6_discover_neighbor_error_strings,
1657 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1660 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
1661 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1667 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1669 vnet_main_t *vnm = vnet_get_main ();
1670 ip6_main_t *im = &ip6_main;
1671 icmp6_neighbor_solicitation_header_t *h;
1673 ip_interface_address_t *ia;
1674 ip_adjacency_t *adj;
1675 vnet_hw_interface_t *hi;
1676 vnet_sw_interface_t *si;
1682 si = vnet_get_sw_interface (vnm, sw_if_index);
1684 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1686 return clib_error_return (0, "%U: interface %U down",
1687 format_ip6_address, dst,
1688 format_vnet_sw_if_index_name, vnm,
1693 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1696 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1697 return clib_error_return
1698 (0, "no matching interface address for destination %U (interface %U)",
1699 format_ip6_address, dst,
1700 format_vnet_sw_if_index_name, vnm, sw_if_index);
1704 vlib_packet_template_get_packet (vm,
1705 &im->discover_neighbor_packet_template,
1708 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1710 /* Destination address is a solicited node multicast address. We need to fill in
1711 the low 24 bits with low 24 bits of target's address. */
1712 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1713 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1714 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1716 h->ip.src_address = src[0];
1717 h->neighbor.target_address = dst[0];
1719 if (PREDICT_FALSE (!hi->hw_address))
1721 return clib_error_return (0, "%U: interface %U do not support ip probe",
1722 format_ip6_address, dst,
1723 format_vnet_sw_if_index_name, vnm,
1727 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1728 vec_len (hi->hw_address));
1730 h->neighbor.icmp.checksum =
1731 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1732 ASSERT (bogus_length == 0);
1734 b = vlib_get_buffer (vm, bi);
1735 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1736 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1738 /* Add encapsulation string for software interface (e.g. ethernet header). */
1739 ip46_address_t nh = {
1743 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1744 VNET_LINK_IP6, &nh, sw_if_index);
1747 /* Peer has been previously resolved, retrieve glean adj instead */
1748 if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1751 ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1752 VNET_LINK_IP6, sw_if_index, &nh);
1756 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1757 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1760 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1761 u32 *to_next = vlib_frame_vector_args (f);
1764 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1768 return /* no error */ 0;
1773 IP6_REWRITE_NEXT_DROP,
1774 IP6_REWRITE_NEXT_ICMP_ERROR,
1775 } ip6_rewrite_next_t;
1778 ip6_mtu_check (vlib_buffer_t * b, u16 buffer_packet_bytes,
1779 u16 adj_packet_bytes, u32 * next, u32 * error)
1781 if (adj_packet_bytes >= 1280 && buffer_packet_bytes > adj_packet_bytes)
1783 *error = IP6_ERROR_MTU_EXCEEDED;
1784 icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1786 *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1791 ip6_rewrite_inline (vlib_main_t * vm,
1792 vlib_node_runtime_t * node,
1793 vlib_frame_t * frame,
1794 int do_counters, int is_midchain, int is_mcast)
1796 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1797 u32 *from = vlib_frame_vector_args (frame);
1798 u32 n_left_from, n_left_to_next, *to_next, next_index;
1799 vlib_node_runtime_t *error_node =
1800 vlib_node_get_runtime (vm, ip6_input_node.index);
1802 n_left_from = frame->n_vectors;
1803 next_index = node->cached_next_index;
1804 u32 thread_index = vlib_get_thread_index ();
1806 while (n_left_from > 0)
1808 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1810 while (n_left_from >= 4 && n_left_to_next >= 2)
1812 ip_adjacency_t *adj0, *adj1;
1813 vlib_buffer_t *p0, *p1;
1814 ip6_header_t *ip0, *ip1;
1815 u32 pi0, rw_len0, next0, error0, adj_index0;
1816 u32 pi1, rw_len1, next1, error1, adj_index1;
1817 u32 tx_sw_if_index0, tx_sw_if_index1;
1819 /* Prefetch next iteration. */
1821 vlib_buffer_t *p2, *p3;
1823 p2 = vlib_get_buffer (vm, from[2]);
1824 p3 = vlib_get_buffer (vm, from[3]);
1826 vlib_prefetch_buffer_header (p2, LOAD);
1827 vlib_prefetch_buffer_header (p3, LOAD);
1829 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1830 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1832 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1833 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1836 pi0 = to_next[0] = from[0];
1837 pi1 = to_next[1] = from[1];
1842 n_left_to_next -= 2;
1844 p0 = vlib_get_buffer (vm, pi0);
1845 p1 = vlib_get_buffer (vm, pi1);
1847 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1848 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1850 ip0 = vlib_buffer_get_current (p0);
1851 ip1 = vlib_buffer_get_current (p1);
1853 error0 = error1 = IP6_ERROR_NONE;
1854 next0 = next1 = IP6_REWRITE_NEXT_DROP;
1856 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
1858 i32 hop_limit0 = ip0->hop_limit;
1860 /* Input node should have reject packets with hop limit 0. */
1861 ASSERT (ip0->hop_limit > 0);
1865 ip0->hop_limit = hop_limit0;
1868 * If the hop count drops below 1 when forwarding, generate
1871 if (PREDICT_FALSE (hop_limit0 <= 0))
1873 error0 = IP6_ERROR_TIME_EXPIRED;
1874 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1875 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1876 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1877 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1883 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1885 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
1887 i32 hop_limit1 = ip1->hop_limit;
1889 /* Input node should have reject packets with hop limit 0. */
1890 ASSERT (ip1->hop_limit > 0);
1894 ip1->hop_limit = hop_limit1;
1897 * If the hop count drops below 1 when forwarding, generate
1900 if (PREDICT_FALSE (hop_limit1 <= 0))
1902 error1 = IP6_ERROR_TIME_EXPIRED;
1903 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1904 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1905 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1906 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1912 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1915 adj0 = adj_get (adj_index0);
1916 adj1 = adj_get (adj_index1);
1918 /* Guess we are only writing on simple Ethernet header. */
1919 vnet_rewrite_two_headers (adj0[0], adj1[0],
1920 ip0, ip1, sizeof (ethernet_header_t));
1922 rw_len0 = adj0[0].rewrite_header.data_bytes;
1923 rw_len1 = adj1[0].rewrite_header.data_bytes;
1924 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1925 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
1929 vlib_increment_combined_counter
1930 (&adjacency_counters,
1931 thread_index, adj_index0, 1,
1932 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1933 vlib_increment_combined_counter
1934 (&adjacency_counters,
1935 thread_index, adj_index1, 1,
1936 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1939 /* Check MTU of outgoing interface. */
1940 ip6_mtu_check (p0, vlib_buffer_length_in_chain (vm, p0),
1941 adj0[0].rewrite_header.max_l3_packet_bytes, &next0,
1943 ip6_mtu_check (p1, vlib_buffer_length_in_chain (vm, p1),
1944 adj1[0].rewrite_header.max_l3_packet_bytes, &next1,
1947 /* Don't adjust the buffer for hop count issue; icmp-error node
1948 * wants to see the IP headerr */
1949 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1951 p0->current_data -= rw_len0;
1952 p0->current_length += rw_len0;
1954 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1955 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1956 next0 = adj0[0].rewrite_header.next_index;
1959 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1960 vnet_feature_arc_start (lm->output_feature_arc_index,
1961 tx_sw_if_index0, &next0, p0);
1965 adj0->sub_type.midchain.fixup_func
1966 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1971 * copy bytes from the IP address into the MAC rewrite
1973 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
1976 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1978 p1->current_data -= rw_len1;
1979 p1->current_length += rw_len1;
1981 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1982 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1983 next1 = adj1[0].rewrite_header.next_index;
1986 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1987 vnet_feature_arc_start (lm->output_feature_arc_index,
1988 tx_sw_if_index1, &next1, p1);
1992 adj1->sub_type.midchain.fixup_func
1993 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
1998 * copy bytes from the IP address into the MAC rewrite
2000 vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1);
2004 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2005 to_next, n_left_to_next,
2006 pi0, pi1, next0, next1);
2009 while (n_left_from > 0 && n_left_to_next > 0)
2011 ip_adjacency_t *adj0;
2015 u32 adj_index0, next0, error0;
2016 u32 tx_sw_if_index0;
2018 pi0 = to_next[0] = from[0];
2020 p0 = vlib_get_buffer (vm, pi0);
2022 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2024 adj0 = adj_get (adj_index0);
2026 ip0 = vlib_buffer_get_current (p0);
2028 error0 = IP6_ERROR_NONE;
2029 next0 = IP6_REWRITE_NEXT_DROP;
2031 /* Check hop limit */
2032 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2034 i32 hop_limit0 = ip0->hop_limit;
2036 ASSERT (ip0->hop_limit > 0);
2040 ip0->hop_limit = hop_limit0;
2042 if (PREDICT_FALSE (hop_limit0 <= 0))
2045 * If the hop count drops below 1 when forwarding, generate
2048 error0 = IP6_ERROR_TIME_EXPIRED;
2049 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2050 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2051 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2052 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2058 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2061 /* Guess we are only writing on simple Ethernet header. */
2062 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2064 /* Update packet buffer attributes/set output interface. */
2065 rw_len0 = adj0[0].rewrite_header.data_bytes;
2066 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2070 vlib_increment_combined_counter
2071 (&adjacency_counters,
2072 thread_index, adj_index0, 1,
2073 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2076 /* Check MTU of outgoing interface. */
2077 ip6_mtu_check (p0, vlib_buffer_length_in_chain (vm, p0),
2078 adj0[0].rewrite_header.max_l3_packet_bytes, &next0,
2081 /* Don't adjust the buffer for hop count issue; icmp-error node
2082 * wants to see the IP headerr */
2083 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2085 p0->current_data -= rw_len0;
2086 p0->current_length += rw_len0;
2088 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2090 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2091 next0 = adj0[0].rewrite_header.next_index;
2094 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2095 vnet_feature_arc_start (lm->output_feature_arc_index,
2096 tx_sw_if_index0, &next0, p0);
2100 adj0->sub_type.midchain.fixup_func
2101 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2105 vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2109 p0->error = error_node->errors[error0];
2114 n_left_to_next -= 1;
2116 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2117 to_next, n_left_to_next,
2121 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2124 /* Need to do trace after rewrites to pick up new packet data. */
2125 if (node->flags & VLIB_NODE_FLAG_TRACE)
2126 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2128 return frame->n_vectors;
2132 ip6_rewrite (vlib_main_t * vm,
2133 vlib_node_runtime_t * node, vlib_frame_t * frame)
2135 if (adj_are_counters_enabled ())
2136 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2138 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2142 ip6_rewrite_mcast (vlib_main_t * vm,
2143 vlib_node_runtime_t * node, vlib_frame_t * frame)
2145 if (adj_are_counters_enabled ())
2146 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2148 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2152 ip6_midchain (vlib_main_t * vm,
2153 vlib_node_runtime_t * node, vlib_frame_t * frame)
2155 if (adj_are_counters_enabled ())
2156 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2158 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2162 ip6_mcast_midchain (vlib_main_t * vm,
2163 vlib_node_runtime_t * node, vlib_frame_t * frame)
2165 if (adj_are_counters_enabled ())
2166 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2168 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2172 VLIB_REGISTER_NODE (ip6_midchain_node) =
2174 .function = ip6_midchain,
2175 .name = "ip6-midchain",
2176 .vector_size = sizeof (u32),
2177 .format_trace = format_ip6_forward_next_trace,
2178 .sibling_of = "ip6-rewrite",
2182 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2185 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2187 .function = ip6_rewrite,
2188 .name = "ip6-rewrite",
2189 .vector_size = sizeof (u32),
2190 .format_trace = format_ip6_rewrite_trace,
2194 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
2195 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2200 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2203 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2205 .function = ip6_rewrite_mcast,
2206 .name = "ip6-rewrite-mcast",
2207 .vector_size = sizeof (u32),
2208 .format_trace = format_ip6_rewrite_trace,
2209 .sibling_of = "ip6-rewrite",
2213 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2216 VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
2218 .function = ip6_mcast_midchain,
2219 .name = "ip6-mcast-midchain",
2220 .vector_size = sizeof (u32),
2221 .format_trace = format_ip6_rewrite_trace,
2222 .sibling_of = "ip6-rewrite",
2226 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
2229 * Hop-by-Hop handling
2231 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2233 #define foreach_ip6_hop_by_hop_error \
2234 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2235 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2236 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2241 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2242 foreach_ip6_hop_by_hop_error
2244 IP6_HOP_BY_HOP_N_ERROR,
2245 } ip6_hop_by_hop_error_t;
2249 * Primary h-b-h handler trace support
2250 * We work pretty hard on the problem for obvious reasons
2256 u8 option_data[256];
2257 } ip6_hop_by_hop_trace_t;
2259 vlib_node_registration_t ip6_hop_by_hop_node;
2261 static char *ip6_hop_by_hop_error_strings[] = {
2262 #define _(sym,string) string,
2263 foreach_ip6_hop_by_hop_error
2268 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2270 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2271 int total_len = va_arg (*args, int);
2272 ip6_hop_by_hop_option_t *opt0, *limit0;
2273 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2276 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2277 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2279 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2280 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2282 while (opt0 < limit0)
2287 case 0: /* Pad, just stop */
2288 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2292 if (hm->trace[type0])
2294 s = (*hm->trace[type0]) (s, opt0);
2299 format (s, "\n unrecognized option %d length %d", type0,
2303 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2304 sizeof (ip6_hop_by_hop_option_t));
2312 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2314 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2315 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2316 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2317 ip6_hop_by_hop_header_t *hbh0;
2318 ip6_hop_by_hop_option_t *opt0, *limit0;
2319 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2323 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2325 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2326 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2328 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2329 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2331 while (opt0 < limit0)
2336 case 0: /* Pad, just stop */
2337 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2341 if (hm->trace[type0])
2343 s = (*hm->trace[type0]) (s, opt0);
2348 format (s, "\n unrecognized option %d length %d", type0,
2352 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2353 sizeof (ip6_hop_by_hop_option_t));
2361 ip6_scan_hbh_options (vlib_buffer_t * b0,
2363 ip6_hop_by_hop_header_t * hbh0,
2364 ip6_hop_by_hop_option_t * opt0,
2365 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2367 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2371 while (opt0 < limit0)
2377 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2382 if (hm->options[type0])
2384 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2386 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2392 /* Unrecognized mandatory option, check the two high order bits */
2393 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2395 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2397 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2398 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2399 *next0 = IP_LOOKUP_NEXT_DROP;
2401 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2402 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2403 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2404 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2405 ICMP6_parameter_problem_unrecognized_option,
2406 (u8 *) opt0 - (u8 *) ip0);
2408 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2409 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2410 if (!ip6_address_is_multicast (&ip0->dst_address))
2412 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2413 icmp6_error_set_vnet_buffer (b0,
2414 ICMP6_parameter_problem,
2415 ICMP6_parameter_problem_unrecognized_option,
2416 (u8 *) opt0 - (u8 *) ip0);
2420 *next0 = IP_LOOKUP_NEXT_DROP;
2428 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2429 sizeof (ip6_hop_by_hop_option_t));
2435 * Process the Hop-by-Hop Options header
2438 ip6_hop_by_hop (vlib_main_t * vm,
2439 vlib_node_runtime_t * node, vlib_frame_t * frame)
2441 vlib_node_runtime_t *error_node =
2442 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2443 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2444 u32 n_left_from, *from, *to_next;
2445 ip_lookup_next_t next_index;
2447 from = vlib_frame_vector_args (frame);
2448 n_left_from = frame->n_vectors;
2449 next_index = node->cached_next_index;
2451 while (n_left_from > 0)
2455 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2457 while (n_left_from >= 4 && n_left_to_next >= 2)
2460 vlib_buffer_t *b0, *b1;
2462 ip6_header_t *ip0, *ip1;
2463 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2464 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2465 u8 error0 = 0, error1 = 0;
2467 /* Prefetch next iteration. */
2469 vlib_buffer_t *p2, *p3;
2471 p2 = vlib_get_buffer (vm, from[2]);
2472 p3 = vlib_get_buffer (vm, from[3]);
2474 vlib_prefetch_buffer_header (p2, LOAD);
2475 vlib_prefetch_buffer_header (p3, LOAD);
2477 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2478 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2481 /* Speculatively enqueue b0, b1 to the current next frame */
2482 to_next[0] = bi0 = from[0];
2483 to_next[1] = bi1 = from[1];
2487 n_left_to_next -= 2;
2489 b0 = vlib_get_buffer (vm, bi0);
2490 b1 = vlib_get_buffer (vm, bi1);
2492 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2493 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2494 ip_adjacency_t *adj0 = adj_get (adj_index0);
2495 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2496 ip_adjacency_t *adj1 = adj_get (adj_index1);
2498 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2499 next0 = adj0->lookup_next_index;
2500 next1 = adj1->lookup_next_index;
2502 ip0 = vlib_buffer_get_current (b0);
2503 ip1 = vlib_buffer_get_current (b1);
2504 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2505 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2506 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2507 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2509 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2510 ((hbh0->length + 1) << 3));
2512 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2513 ((hbh1->length + 1) << 3));
2516 * Basic validity checks
2518 if ((hbh0->length + 1) << 3 >
2519 clib_net_to_host_u16 (ip0->payload_length))
2521 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2522 next0 = IP_LOOKUP_NEXT_DROP;
2525 /* Scan the set of h-b-h options, process ones that we understand */
2526 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2528 if ((hbh1->length + 1) << 3 >
2529 clib_net_to_host_u16 (ip1->payload_length))
2531 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2532 next1 = IP_LOOKUP_NEXT_DROP;
2535 /* Scan the set of h-b-h options, process ones that we understand */
2536 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2539 /* Has the classifier flagged this buffer for special treatment? */
2542 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2543 next0 = hm->next_override;
2545 /* Has the classifier flagged this buffer for special treatment? */
2548 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2549 next1 = hm->next_override;
2551 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2553 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2555 ip6_hop_by_hop_trace_t *t =
2556 vlib_add_trace (vm, node, b0, sizeof (*t));
2557 u32 trace_len = (hbh0->length + 1) << 3;
2558 t->next_index = next0;
2559 /* Capture the h-b-h option verbatim */
2562 ARRAY_LEN (t->option_data) ? trace_len :
2563 ARRAY_LEN (t->option_data);
2564 t->trace_len = trace_len;
2565 clib_memcpy (t->option_data, hbh0, trace_len);
2567 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2569 ip6_hop_by_hop_trace_t *t =
2570 vlib_add_trace (vm, node, b1, sizeof (*t));
2571 u32 trace_len = (hbh1->length + 1) << 3;
2572 t->next_index = next1;
2573 /* Capture the h-b-h option verbatim */
2576 ARRAY_LEN (t->option_data) ? trace_len :
2577 ARRAY_LEN (t->option_data);
2578 t->trace_len = trace_len;
2579 clib_memcpy (t->option_data, hbh1, trace_len);
2584 b0->error = error_node->errors[error0];
2585 b1->error = error_node->errors[error1];
2587 /* verify speculative enqueue, maybe switch current next frame */
2588 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2589 n_left_to_next, bi0, bi1, next0,
2593 while (n_left_from > 0 && n_left_to_next > 0)
2599 ip6_hop_by_hop_header_t *hbh0;
2600 ip6_hop_by_hop_option_t *opt0, *limit0;
2603 /* Speculatively enqueue b0 to the current next frame */
2609 n_left_to_next -= 1;
2611 b0 = vlib_get_buffer (vm, bi0);
2613 * Default use the next_index from the adjacency.
2614 * A HBH option rarely redirects to a different node
2616 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2617 ip_adjacency_t *adj0 = adj_get (adj_index0);
2618 next0 = adj0->lookup_next_index;
2620 ip0 = vlib_buffer_get_current (b0);
2621 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2622 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2624 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2625 ((hbh0->length + 1) << 3));
2628 * Basic validity checks
2630 if ((hbh0->length + 1) << 3 >
2631 clib_net_to_host_u16 (ip0->payload_length))
2633 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2634 next0 = IP_LOOKUP_NEXT_DROP;
2638 /* Scan the set of h-b-h options, process ones that we understand */
2639 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2642 /* Has the classifier flagged this buffer for special treatment? */
2645 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2646 next0 = hm->next_override;
2648 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2650 ip6_hop_by_hop_trace_t *t =
2651 vlib_add_trace (vm, node, b0, sizeof (*t));
2652 u32 trace_len = (hbh0->length + 1) << 3;
2653 t->next_index = next0;
2654 /* Capture the h-b-h option verbatim */
2657 ARRAY_LEN (t->option_data) ? trace_len :
2658 ARRAY_LEN (t->option_data);
2659 t->trace_len = trace_len;
2660 clib_memcpy (t->option_data, hbh0, trace_len);
2663 b0->error = error_node->errors[error0];
2665 /* verify speculative enqueue, maybe switch current next frame */
2666 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2667 n_left_to_next, bi0, next0);
2669 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2671 return frame->n_vectors;
2675 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2677 .function = ip6_hop_by_hop,
2678 .name = "ip6-hop-by-hop",
2679 .sibling_of = "ip6-lookup",
2680 .vector_size = sizeof (u32),
2681 .format_trace = format_ip6_hop_by_hop_trace,
2682 .type = VLIB_NODE_TYPE_INTERNAL,
2683 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2684 .error_strings = ip6_hop_by_hop_error_strings,
2689 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2691 static clib_error_t *
2692 ip6_hop_by_hop_init (vlib_main_t * vm)
2694 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2695 memset (hm->options, 0, sizeof (hm->options));
2696 memset (hm->trace, 0, sizeof (hm->trace));
2697 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2701 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2704 ip6_hbh_set_next_override (uword next)
2706 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2708 hm->next_override = next;
2712 ip6_hbh_register_option (u8 option,
2713 int options (vlib_buffer_t * b, ip6_header_t * ip,
2714 ip6_hop_by_hop_option_t * opt),
2715 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2717 ip6_main_t *im = &ip6_main;
2718 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2720 ASSERT ((u32) option < ARRAY_LEN (hm->options));
2722 /* Already registered */
2723 if (hm->options[option])
2726 hm->options[option] = options;
2727 hm->trace[option] = trace;
2729 /* Set global variable */
2730 im->hbh_enabled = 1;
2736 ip6_hbh_unregister_option (u8 option)
2738 ip6_main_t *im = &ip6_main;
2739 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2741 ASSERT ((u32) option < ARRAY_LEN (hm->options));
2743 /* Not registered */
2744 if (!hm->options[option])
2747 hm->options[option] = NULL;
2748 hm->trace[option] = NULL;
2750 /* Disable global knob if this was the last option configured */
2753 for (i = 0; i < 256; i++)
2755 if (hm->options[option])
2762 im->hbh_enabled = 0;
2767 /* Global IP6 main. */
2768 ip6_main_t ip6_main;
2770 static clib_error_t *
2771 ip6_lookup_init (vlib_main_t * vm)
2773 ip6_main_t *im = &ip6_main;
2774 clib_error_t *error;
2777 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2780 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2787 for (j = 0; j < i0; j++)
2788 im->fib_masks[i].as_u32[j] = ~0;
2791 im->fib_masks[i].as_u32[i0] =
2792 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2795 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2797 if (im->lookup_table_nbuckets == 0)
2798 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2800 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2802 if (im->lookup_table_size == 0)
2803 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2805 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2806 "ip6 FIB fwding table",
2807 im->lookup_table_nbuckets, im->lookup_table_size);
2808 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2809 "ip6 FIB non-fwding table",
2810 im->lookup_table_nbuckets, im->lookup_table_size);
2812 /* Create FIB with index 0 and table id of 0. */
2813 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2814 FIB_SOURCE_DEFAULT_ROUTE);
2815 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2816 MFIB_SOURCE_DEFAULT_ROUTE);
2820 pn = pg_get_node (ip6_lookup_node.index);
2821 pn->unformat_edit = unformat_pg_ip6_header;
2824 /* Unless explicitly configured, don't process HBH options */
2825 im->hbh_enabled = 0;
2828 icmp6_neighbor_solicitation_header_t p;
2830 memset (&p, 0, sizeof (p));
2832 p.ip.ip_version_traffic_class_and_flow_label =
2833 clib_host_to_net_u32 (0x6 << 28);
2834 p.ip.payload_length =
2835 clib_host_to_net_u16 (sizeof (p) -
2837 (icmp6_neighbor_solicitation_header_t, neighbor));
2838 p.ip.protocol = IP_PROTOCOL_ICMP6;
2839 p.ip.hop_limit = 255;
2840 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2842 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2844 p.link_layer_option.header.type =
2845 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2846 p.link_layer_option.header.n_data_u64s =
2847 sizeof (p.link_layer_option) / sizeof (u64);
2849 vlib_packet_template_init (vm,
2850 &im->discover_neighbor_packet_template,
2852 /* alloc chunk size */ 8,
2853 "ip6 neighbor discovery");
2859 VLIB_INIT_FUNCTION (ip6_lookup_init);
2862 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
2865 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2866 /* Invert the "u" bit */
2867 ip->as_u8[8] = mac[0] ^ (1 << 1);
2868 ip->as_u8[9] = mac[1];
2869 ip->as_u8[10] = mac[2];
2870 ip->as_u8[11] = 0xFF;
2871 ip->as_u8[12] = 0xFE;
2872 ip->as_u8[13] = mac[3];
2873 ip->as_u8[14] = mac[4];
2874 ip->as_u8[15] = mac[5];
2878 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
2881 /* Invert the previously inverted "u" bit */
2882 mac[0] = ip->as_u8[8] ^ (1 << 1);
2883 mac[1] = ip->as_u8[9];
2884 mac[2] = ip->as_u8[10];
2885 mac[3] = ip->as_u8[13];
2886 mac[4] = ip->as_u8[14];
2887 mac[5] = ip->as_u8[15];
2890 static clib_error_t *
2891 test_ip6_link_command_fn (vlib_main_t * vm,
2892 unformat_input_t * input, vlib_cli_command_t * cmd)
2895 ip6_address_t _a, *a = &_a;
2897 if (unformat (input, "%U", unformat_ethernet_address, mac))
2899 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2900 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
2901 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2902 vlib_cli_output (vm, "Original MAC address: %U",
2903 format_ethernet_address, mac);
2910 * This command converts the given MAC Address into an IPv6 link-local
2914 * Example of how to create an IPv6 link-local address:
2915 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2916 * Link local address: fe80::14d9:e0ff:fe91:7986
2917 * Original MAC address: 16:d9:e0:91:79:86
2921 VLIB_CLI_COMMAND (test_link_command, static) =
2923 .path = "test ip6 link",
2924 .function = test_ip6_link_command_fn,
2925 .short_help = "test ip6 link <mac-address>",
2930 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2934 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
2936 if (~0 == fib_index)
2937 return VNET_API_ERROR_NO_SUCH_FIB;
2939 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2945 static clib_error_t *
2946 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2947 unformat_input_t * input,
2948 vlib_cli_command_t * cmd)
2952 u32 flow_hash_config = 0;
2955 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2957 if (unformat (input, "table %d", &table_id))
2960 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2961 foreach_flow_hash_bit
2968 return clib_error_return (0, "unknown input `%U'",
2969 format_unformat_error, input);
2971 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2978 return clib_error_return (0, "no such FIB table %d", table_id);
2981 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2989 * Configure the set of IPv6 fields used by the flow hash.
2993 * Example of how to set the flow hash on a given table:
2994 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2996 * Example of display the configured flow hash:
2997 * @cliexstart{show ip6 fib}
2998 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3001 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3002 * [0] [@0]: dpo-drop ip6
3005 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3006 * [0] [@2]: dpo-receive
3009 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3010 * [0] [@2]: dpo-receive
3013 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3014 * [0] [@2]: dpo-receive
3017 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3018 * [0] [@2]: dpo-receive
3019 * ff02::1:ff00:0/104
3021 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3022 * [0] [@2]: dpo-receive
3023 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3026 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3027 * [0] [@0]: dpo-drop ip6
3030 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3031 * [0] [@4]: ipv6-glean: af_packet0
3034 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3035 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3038 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3039 * [0] [@2]: dpo-receive
3040 * fe80::fe:3eff:fe3e:9222/128
3042 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3043 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3046 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3047 * [0] [@2]: dpo-receive
3050 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3051 * [0] [@2]: dpo-receive
3054 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3055 * [0] [@2]: dpo-receive
3056 * ff02::1:ff00:0/104
3058 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3059 * [0] [@2]: dpo-receive
3064 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3066 .path = "set ip6 flow-hash",
3068 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3069 .function = set_ip6_flow_hash_command_fn,
3073 static clib_error_t *
3074 show_ip6_local_command_fn (vlib_main_t * vm,
3075 unformat_input_t * input, vlib_cli_command_t * cmd)
3077 ip6_main_t *im = &ip6_main;
3078 ip_lookup_main_t *lm = &im->lookup_main;
3081 vlib_cli_output (vm, "Protocols handled by ip6_local");
3082 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3084 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3087 u32 node_index = vlib_get_node (vm,
3088 ip6_local_node.index)->
3089 next_nodes[lm->local_next_by_ip_protocol[i]];
3090 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3100 * Display the set of protocols handled by the local IPv6 stack.
3103 * Example of how to display local protocol table:
3104 * @cliexstart{show ip6 local}
3105 * Protocols handled by ip6_local
3113 VLIB_CLI_COMMAND (show_ip6_local, static) =
3115 .path = "show ip6 local",
3116 .function = show_ip6_local_command_fn,
3117 .short_help = "show ip6 local",
3122 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3125 vnet_main_t *vnm = vnet_get_main ();
3126 vnet_interface_main_t *im = &vnm->interface_main;
3127 ip6_main_t *ipm = &ip6_main;
3128 ip_lookup_main_t *lm = &ipm->lookup_main;
3129 vnet_classify_main_t *cm = &vnet_classify_main;
3130 ip6_address_t *if_addr;
3132 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3133 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3135 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3136 return VNET_API_ERROR_NO_SUCH_ENTRY;
3138 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3139 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3141 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3143 if (NULL != if_addr)
3145 fib_prefix_t pfx = {
3147 .fp_proto = FIB_PROTOCOL_IP6,
3148 .fp_addr.ip6 = *if_addr,
3152 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3156 if (table_index != (u32) ~ 0)
3158 dpo_id_t dpo = DPO_INVALID;
3163 classify_dpo_create (DPO_PROTO_IP6, table_index));
3165 fib_table_entry_special_dpo_add (fib_index,
3167 FIB_SOURCE_CLASSIFY,
3168 FIB_ENTRY_FLAG_NONE, &dpo);
3173 fib_table_entry_special_remove (fib_index,
3174 &pfx, FIB_SOURCE_CLASSIFY);
3181 static clib_error_t *
3182 set_ip6_classify_command_fn (vlib_main_t * vm,
3183 unformat_input_t * input,
3184 vlib_cli_command_t * cmd)
3186 u32 table_index = ~0;
3187 int table_index_set = 0;
3188 u32 sw_if_index = ~0;
3191 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3193 if (unformat (input, "table-index %d", &table_index))
3194 table_index_set = 1;
3195 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3196 vnet_get_main (), &sw_if_index))
3202 if (table_index_set == 0)
3203 return clib_error_return (0, "classify table-index must be specified");
3205 if (sw_if_index == ~0)
3206 return clib_error_return (0, "interface / subif must be specified");
3208 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3215 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3216 return clib_error_return (0, "No such interface");
3218 case VNET_API_ERROR_NO_SUCH_ENTRY:
3219 return clib_error_return (0, "No such classifier table");
3225 * Assign a classification table to an interface. The classification
3226 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3227 * commands. Once the table is create, use this command to filter packets
3231 * Example of how to assign a classification table to an interface:
3232 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3235 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3237 .path = "set ip6 classify",
3239 "set ip6 classify intfc <interface> table-index <classify-idx>",
3240 .function = set_ip6_classify_command_fn,
3244 static clib_error_t *
3245 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3247 ip6_main_t *im = &ip6_main;
3252 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3254 if (unformat (input, "hash-buckets %d", &tmp))
3256 else if (unformat (input, "heap-size %U",
3257 unformat_memory_size, &heapsize))
3260 return clib_error_return (0, "unknown input '%U'",
3261 format_unformat_error, input);
3264 im->lookup_table_nbuckets = nbuckets;
3265 im->lookup_table_size = heapsize;
3270 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3273 * fd.io coding-style-patch-verification: ON
3276 * eval: (c-set-style "gnu")