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/ip_frag.h>
43 #include <vnet/ip/ip6_neighbor.h>
44 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
45 #include <vnet/srp/srp.h> /* for srp_hw_interface_class */
46 #include <vppinfra/cache.h>
47 #include <vnet/fib/fib_urpf_list.h> /* for FIB uRPF check */
48 #include <vnet/fib/ip6_fib.h>
49 #include <vnet/mfib/ip6_mfib.h>
50 #include <vnet/dpo/load_balance_map.h>
51 #include <vnet/dpo/classify_dpo.h>
53 #ifndef CLIB_MARCH_VARIANT
54 #include <vppinfra/bihash_template.c>
56 #include <vnet/ip/ip6_forward.h>
57 #include <vnet/interface_output.h>
59 /* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
60 #define OI_DECAP 0x80000000
63 ip6_add_interface_prefix_routes (ip6_main_t * im,
66 ip6_address_t * address, u32 address_length)
68 ip_lookup_main_t *lm = &im->lookup_main;
69 ip_interface_prefix_t *if_prefix;
71 ip_interface_prefix_key_t key = {
73 .fp_len = address_length,
74 .fp_proto = FIB_PROTOCOL_IP6,
78 im->fib_masks[address_length].
82 im->fib_masks[address_length].
87 .sw_if_index = sw_if_index,
90 /* If prefix already set on interface, just increment ref count & return */
91 if_prefix = ip_get_interface_prefix (lm, &key);
94 if_prefix->ref_count += 1;
98 /* New prefix - allocate a pool entry, initialize it, add to the hash */
99 pool_get (lm->if_prefix_pool, if_prefix);
100 if_prefix->ref_count = 1;
101 clib_memcpy (&if_prefix->key, &key, sizeof (key));
102 mhash_set (&lm->prefix_to_if_prefix_index, &key,
103 if_prefix - lm->if_prefix_pool, 0 /* old value */ );
105 /* length < 128 - add glean */
106 if (address_length < 128)
108 /* set the glean route for the prefix */
109 fib_table_entry_update_one_path (fib_index, &key.prefix,
110 FIB_SOURCE_INTERFACE,
111 (FIB_ENTRY_FLAG_CONNECTED |
112 FIB_ENTRY_FLAG_ATTACHED),
114 /* No next-hop address */
116 /* invalid FIB index */
118 /* no out-label stack */
119 NULL, FIB_ROUTE_PATH_FLAG_NONE);
124 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
125 ip6_main_t * im, u32 fib_index,
126 ip_interface_address_t * a)
128 ip_lookup_main_t *lm = &im->lookup_main;
129 ip6_address_t *address = ip_interface_address_get_address (lm, a);
131 .fp_len = a->address_length,
132 .fp_proto = FIB_PROTOCOL_IP6,
133 .fp_addr.ip6 = *address,
136 /* set special routes for the prefix if needed */
137 ip6_add_interface_prefix_routes (im, sw_if_index, fib_index,
138 address, a->address_length);
141 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
143 u32 classify_table_index =
144 lm->classify_table_index_by_sw_if_index[sw_if_index];
145 if (classify_table_index != (u32) ~ 0)
147 dpo_id_t dpo = DPO_INVALID;
152 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
154 fib_table_entry_special_dpo_add (fib_index,
157 FIB_ENTRY_FLAG_NONE, &dpo);
162 fib_table_entry_update_one_path (fib_index, &pfx,
163 FIB_SOURCE_INTERFACE,
164 (FIB_ENTRY_FLAG_CONNECTED |
165 FIB_ENTRY_FLAG_LOCAL),
169 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
173 ip6_del_interface_prefix_routes (ip6_main_t * im,
176 ip6_address_t * address, u32 address_length)
178 ip_lookup_main_t *lm = &im->lookup_main;
179 ip_interface_prefix_t *if_prefix;
181 ip_interface_prefix_key_t key = {
183 .fp_len = address_length,
184 .fp_proto = FIB_PROTOCOL_IP6,
188 im->fib_masks[address_length].
192 im->fib_masks[address_length].
197 .sw_if_index = sw_if_index,
200 if_prefix = ip_get_interface_prefix (lm, &key);
203 clib_warning ("Prefix not found while deleting %U",
204 format_ip4_address_and_length, address, address_length);
208 /* If not deleting last intf addr in prefix, decrement ref count & return */
209 if_prefix->ref_count -= 1;
210 if (if_prefix->ref_count > 0)
213 /* length <= 30, delete glean route */
214 if (address_length <= 128)
216 /* remove glean route for prefix */
217 fib_table_entry_delete (fib_index, &key.prefix, FIB_SOURCE_INTERFACE);
221 mhash_unset (&lm->prefix_to_if_prefix_index, &key, 0 /* old_value */ );
222 pool_put (lm->if_prefix_pool, if_prefix);
226 ip6_del_interface_routes (u32 sw_if_index, ip6_main_t * im,
228 ip6_address_t * address, u32 address_length)
232 .fp_proto = FIB_PROTOCOL_IP6,
233 .fp_addr.ip6 = *address,
236 /* delete special routes for the prefix if needed */
237 ip6_del_interface_prefix_routes (im, sw_if_index, fib_index,
238 address, address_length);
240 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
243 #ifndef CLIB_MARCH_VARIANT
245 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
247 ip6_main_t *im = &ip6_main;
249 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
252 * enable/disable only on the 1<->0 transition
256 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
261 /* The ref count is 0 when an address is removed from an interface that has
262 * no address - this is not a ciritical error */
263 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
264 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
268 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
271 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
272 sw_if_index, !is_enable, 0, 0);
275 /* get first interface address */
277 ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
279 ip_lookup_main_t *lm = &im->lookup_main;
280 ip_interface_address_t *ia = 0;
281 ip6_address_t *result = 0;
284 foreach_ip_interface_address (lm, ia, sw_if_index,
285 1 /* honor unnumbered */,
287 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
296 ip6_add_del_interface_address (vlib_main_t * vm,
298 ip6_address_t * address,
299 u32 address_length, u32 is_del)
301 vnet_main_t *vnm = vnet_get_main ();
302 ip6_main_t *im = &ip6_main;
303 ip_lookup_main_t *lm = &im->lookup_main;
305 u32 if_address_index;
306 ip6_address_fib_t ip6_af, *addr_fib = 0;
307 ip6_address_t ll_addr;
309 /* local0 interface doesn't support IP addressing */
310 if (sw_if_index == 0)
313 clib_error_create ("local0 interface doesn't support IP addressing");
316 if (ip6_address_is_link_local_unicast (address))
318 if (address_length != 128)
320 vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
323 ("prefix length of link-local address must be 128");
327 return ip6_neighbor_set_link_local_address (vm, sw_if_index,
332 ll_addr = ip6_neighbor_get_link_local_address (sw_if_index);
333 if (ip6_address_is_equal (&ll_addr, address))
335 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_DELETABLE;
336 return clib_error_create ("address not deletable");
340 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
341 return clib_error_create ("address not found");
346 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
347 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
349 ip6_addr_fib_init (&ip6_af, address,
350 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
351 vec_add1 (addr_fib, ip6_af);
356 /* When adding an address check that it does not conflict
357 with an existing address on any interface in this table. */
358 ip_interface_address_t *ia;
359 vnet_sw_interface_t *sif;
361 pool_foreach(sif, vnm->interface_main.sw_interfaces,
363 if (im->fib_index_by_sw_if_index[sw_if_index] ==
364 im->fib_index_by_sw_if_index[sif->sw_if_index])
366 foreach_ip_interface_address
367 (&im->lookup_main, ia, sif->sw_if_index,
368 0 /* honor unnumbered */ ,
371 ip_interface_address_get_address
372 (&im->lookup_main, ia);
373 if (ip6_destination_matches_route
374 (im, address, x, ia->address_length) ||
375 ip6_destination_matches_route (im,
380 /* an intf may have >1 addr from the same prefix */
381 if ((sw_if_index == sif->sw_if_index) &&
382 (ia->address_length == address_length) &&
383 !ip6_address_is_equal (x, address))
386 /* error if the length or intf was different */
387 vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS;
390 ("failed to add %U which conflicts with %U for interface %U",
391 format_ip6_address_and_length, address,
393 format_ip6_address_and_length, x,
395 format_vnet_sw_if_index_name, vnm,
405 uword elts_before = pool_elts (lm->if_address_pool);
407 error = ip_interface_address_add_del
408 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
412 /* Pool did not grow: add duplicate address. */
413 if (elts_before == pool_elts (lm->if_address_pool))
417 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
420 ip6_del_interface_routes (sw_if_index,
421 im, ip6_af.fib_index, address, address_length);
423 ip6_add_interface_routes (vnm, sw_if_index,
424 im, ip6_af.fib_index,
425 pool_elt_at_index (lm->if_address_pool,
429 ip6_add_del_interface_address_callback_t *cb;
430 vec_foreach (cb, im->add_del_interface_address_callbacks)
431 cb->function (im, cb->function_opaque, sw_if_index,
432 address, address_length, if_address_index, is_del);
442 static clib_error_t *
443 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
445 ip6_main_t *im = &ip6_main;
446 ip_interface_address_t *ia;
448 u32 is_admin_up, fib_index;
450 /* Fill in lookup tables with default table (0). */
451 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
453 vec_validate_init_empty (im->
454 lookup_main.if_address_pool_index_by_sw_if_index,
457 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
459 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
462 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
463 0 /* honor unnumbered */,
465 a = ip_interface_address_get_address (&im->lookup_main, ia);
467 ip6_add_interface_routes (vnm, sw_if_index,
471 ip6_del_interface_routes (sw_if_index, im, fib_index,
472 a, ia->address_length);
479 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
481 /* Built-in ip6 unicast rx feature path definition */
483 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
485 .arc_name = "ip6-unicast",
486 .start_nodes = VNET_FEATURES ("ip6-input"),
487 .last_in_arc = "ip6-lookup",
488 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
491 VNET_FEATURE_INIT (ip6_flow_classify, static) =
493 .arc_name = "ip6-unicast",
494 .node_name = "ip6-flow-classify",
495 .runs_before = VNET_FEATURES ("ip6-inacl"),
498 VNET_FEATURE_INIT (ip6_inacl, static) =
500 .arc_name = "ip6-unicast",
501 .node_name = "ip6-inacl",
502 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
505 VNET_FEATURE_INIT (ip6_policer_classify, static) =
507 .arc_name = "ip6-unicast",
508 .node_name = "ip6-policer-classify",
509 .runs_before = VNET_FEATURES ("ipsec6-input-feature"),
512 VNET_FEATURE_INIT (ip6_ipsec, static) =
514 .arc_name = "ip6-unicast",
515 .node_name = "ipsec6-input-feature",
516 .runs_before = VNET_FEATURES ("l2tp-decap"),
519 VNET_FEATURE_INIT (ip6_l2tp, static) =
521 .arc_name = "ip6-unicast",
522 .node_name = "l2tp-decap",
523 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
526 VNET_FEATURE_INIT (ip6_vpath, static) =
528 .arc_name = "ip6-unicast",
529 .node_name = "vpath-input-ip6",
530 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
533 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
535 .arc_name = "ip6-unicast",
536 .node_name = "ip6-vxlan-bypass",
537 .runs_before = VNET_FEATURES ("ip6-lookup"),
540 VNET_FEATURE_INIT (ip6_not_enabled, static) =
542 .arc_name = "ip6-unicast",
543 .node_name = "ip6-not-enabled",
544 .runs_before = VNET_FEATURES ("ip6-lookup"),
547 VNET_FEATURE_INIT (ip6_lookup, static) =
549 .arc_name = "ip6-unicast",
550 .node_name = "ip6-lookup",
551 .runs_before = 0, /*last feature*/
554 /* Built-in ip6 multicast rx feature path definition (none now) */
555 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
557 .arc_name = "ip6-multicast",
558 .start_nodes = VNET_FEATURES ("ip6-input"),
559 .last_in_arc = "ip6-mfib-forward-lookup",
560 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
563 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
564 .arc_name = "ip6-multicast",
565 .node_name = "vpath-input-ip6",
566 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
569 VNET_FEATURE_INIT (ip6_not_enabled_mc, static) = {
570 .arc_name = "ip6-multicast",
571 .node_name = "ip6-not-enabled",
572 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
575 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
576 .arc_name = "ip6-multicast",
577 .node_name = "ip6-mfib-forward-lookup",
578 .runs_before = 0, /* last feature */
581 /* Built-in ip4 tx feature path definition */
582 VNET_FEATURE_ARC_INIT (ip6_output, static) =
584 .arc_name = "ip6-output",
585 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain", "ip6-dvr-dpo"),
586 .last_in_arc = "interface-output",
587 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
590 VNET_FEATURE_INIT (ip6_outacl, static) = {
591 .arc_name = "ip6-output",
592 .node_name = "ip6-outacl",
593 .runs_before = VNET_FEATURES ("ipsec6-output-feature"),
596 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
597 .arc_name = "ip6-output",
598 .node_name = "ipsec6-output-feature",
599 .runs_before = VNET_FEATURES ("interface-output"),
602 VNET_FEATURE_INIT (ip6_interface_output, static) = {
603 .arc_name = "ip6-output",
604 .node_name = "interface-output",
605 .runs_before = 0, /* not before any other features */
609 static clib_error_t *
610 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
612 ip6_main_t *im = &ip6_main;
614 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
615 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
619 /* Ensure that IPv6 is disabled */
620 ip6_main_t *im6 = &ip6_main;
621 ip_lookup_main_t *lm6 = &im6->lookup_main;
622 ip_interface_address_t *ia = 0;
623 ip6_address_t *address;
624 vlib_main_t *vm = vlib_get_main ();
626 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0 /* is_add */ );
627 vnet_sw_interface_update_unnumbered (sw_if_index, ~0, 0);
629 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
631 address = ip_interface_address_get_address (lm6, ia);
632 ip6_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1);
635 ip6_mfib_interface_enable_disable (sw_if_index, 0);
638 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
641 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
642 sw_if_index, is_add, 0, 0);
644 return /* no error */ 0;
647 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
649 VLIB_NODE_FN (ip6_lookup_node) (vlib_main_t * vm,
650 vlib_node_runtime_t * node,
651 vlib_frame_t * frame)
653 return ip6_lookup_inline (vm, node, frame);
656 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
659 VLIB_REGISTER_NODE (ip6_lookup_node) =
661 .name = "ip6-lookup",
662 .vector_size = sizeof (u32),
663 .format_trace = format_ip6_lookup_trace,
664 .n_next_nodes = IP6_LOOKUP_N_NEXT,
665 .next_nodes = IP6_LOOKUP_NEXT_NODES,
669 VLIB_NODE_FN (ip6_load_balance_node) (vlib_main_t * vm,
670 vlib_node_runtime_t * node,
671 vlib_frame_t * frame)
673 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
675 u32 thread_index = vm->thread_index;
676 ip6_main_t *im = &ip6_main;
677 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
678 u16 nexts[VLIB_FRAME_SIZE], *next;
680 from = vlib_frame_vector_args (frame);
681 n_left = frame->n_vectors;
684 vlib_get_buffers (vm, from, bufs, n_left);
688 const load_balance_t *lb0, *lb1;
689 const ip6_header_t *ip0, *ip1;
690 u32 lbi0, hc0, lbi1, hc1;
691 const dpo_id_t *dpo0, *dpo1;
693 /* Prefetch next iteration. */
695 vlib_prefetch_buffer_header (b[2], STORE);
696 vlib_prefetch_buffer_header (b[3], STORE);
698 CLIB_PREFETCH (b[2]->data, sizeof (ip0[0]), STORE);
699 CLIB_PREFETCH (b[3]->data, sizeof (ip0[0]), STORE);
702 ip0 = vlib_buffer_get_current (b[0]);
703 ip1 = vlib_buffer_get_current (b[1]);
704 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
705 lbi1 = vnet_buffer (b[1])->ip.adj_index[VLIB_TX];
707 lb0 = load_balance_get (lbi0);
708 lb1 = load_balance_get (lbi1);
711 * this node is for via FIBs we can re-use the hash value from the
712 * to node if present.
713 * We don't want to use the same hash value at each level in the recursion
714 * graph as that would lead to polarisation
718 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
720 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
722 hc0 = vnet_buffer (b[0])->ip.flow_hash =
723 vnet_buffer (b[0])->ip.flow_hash >> 1;
727 hc0 = vnet_buffer (b[0])->ip.flow_hash =
728 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
730 dpo0 = load_balance_get_fwd_bucket
731 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
735 dpo0 = load_balance_get_bucket_i (lb0, 0);
737 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
739 if (PREDICT_TRUE (vnet_buffer (b[1])->ip.flow_hash))
741 hc1 = vnet_buffer (b[1])->ip.flow_hash =
742 vnet_buffer (b[1])->ip.flow_hash >> 1;
746 hc1 = vnet_buffer (b[1])->ip.flow_hash =
747 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
749 dpo1 = load_balance_get_fwd_bucket
750 (lb1, (hc1 & (lb1->lb_n_buckets_minus_1)));
754 dpo1 = load_balance_get_bucket_i (lb1, 0);
757 next[0] = dpo0->dpoi_next_node;
758 next[1] = dpo1->dpoi_next_node;
760 /* Only process the HBH Option Header if explicitly configured to do so */
761 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
763 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
764 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
766 /* Only process the HBH Option Header if explicitly configured to do so */
767 if (PREDICT_FALSE (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
769 next[1] = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
770 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[1];
773 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
774 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
776 vlib_increment_combined_counter
777 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
778 vlib_increment_combined_counter
779 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, b[1]));
788 const load_balance_t *lb0;
789 const ip6_header_t *ip0;
790 const dpo_id_t *dpo0;
793 ip0 = vlib_buffer_get_current (b[0]);
794 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
796 lb0 = load_balance_get (lbi0);
799 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
801 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
803 hc0 = vnet_buffer (b[0])->ip.flow_hash =
804 vnet_buffer (b[0])->ip.flow_hash >> 1;
808 hc0 = vnet_buffer (b[0])->ip.flow_hash =
809 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
811 dpo0 = load_balance_get_fwd_bucket
812 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
816 dpo0 = load_balance_get_bucket_i (lb0, 0);
819 next[0] = dpo0->dpoi_next_node;
820 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
822 /* Only process the HBH Option Header if explicitly configured to do so */
823 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
825 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
826 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
829 vlib_increment_combined_counter
830 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
837 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
839 if (node->flags & VLIB_NODE_FLAG_TRACE)
840 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
842 return frame->n_vectors;
846 VLIB_REGISTER_NODE (ip6_load_balance_node) =
848 .name = "ip6-load-balance",
849 .vector_size = sizeof (u32),
850 .sibling_of = "ip6-lookup",
851 .format_trace = format_ip6_lookup_trace,
857 /* Adjacency taken. */
862 /* Packet data, possibly *after* rewrite. */
863 u8 packet_data[128 - 1 * sizeof (u32)];
865 ip6_forward_next_trace_t;
867 #ifndef CLIB_MARCH_VARIANT
869 format_ip6_forward_next_trace (u8 * s, va_list * args)
871 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
872 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
873 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
874 u32 indent = format_get_indent (s);
876 s = format (s, "%U%U",
877 format_white_space, indent,
878 format_ip6_header, t->packet_data, sizeof (t->packet_data));
884 format_ip6_lookup_trace (u8 * s, va_list * args)
886 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
887 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
888 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
889 u32 indent = format_get_indent (s);
891 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
892 t->fib_index, t->adj_index, t->flow_hash);
893 s = format (s, "\n%U%U",
894 format_white_space, indent,
895 format_ip6_header, t->packet_data, sizeof (t->packet_data));
901 format_ip6_rewrite_trace (u8 * s, va_list * args)
903 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
904 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
905 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
906 u32 indent = format_get_indent (s);
908 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
909 t->fib_index, t->adj_index, format_ip_adjacency,
910 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
911 s = format (s, "\n%U%U",
912 format_white_space, indent,
913 format_ip_adjacency_packet_data,
914 t->adj_index, t->packet_data, sizeof (t->packet_data));
918 /* Common trace function for all ip6-forward next nodes. */
919 #ifndef CLIB_MARCH_VARIANT
921 ip6_forward_next_trace (vlib_main_t * vm,
922 vlib_node_runtime_t * node,
923 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
926 ip6_main_t *im = &ip6_main;
928 n_left = frame->n_vectors;
929 from = vlib_frame_vector_args (frame);
934 vlib_buffer_t *b0, *b1;
935 ip6_forward_next_trace_t *t0, *t1;
937 /* Prefetch next iteration. */
938 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
939 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
944 b0 = vlib_get_buffer (vm, bi0);
945 b1 = vlib_get_buffer (vm, bi1);
947 if (b0->flags & VLIB_BUFFER_IS_TRACED)
949 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
950 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
951 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
953 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
954 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
955 vec_elt (im->fib_index_by_sw_if_index,
956 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
958 clib_memcpy_fast (t0->packet_data,
959 vlib_buffer_get_current (b0),
960 sizeof (t0->packet_data));
962 if (b1->flags & VLIB_BUFFER_IS_TRACED)
964 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
965 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
966 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
968 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
969 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
970 vec_elt (im->fib_index_by_sw_if_index,
971 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
973 clib_memcpy_fast (t1->packet_data,
974 vlib_buffer_get_current (b1),
975 sizeof (t1->packet_data));
985 ip6_forward_next_trace_t *t0;
989 b0 = vlib_get_buffer (vm, bi0);
991 if (b0->flags & VLIB_BUFFER_IS_TRACED)
993 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
994 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
995 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
997 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
998 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
999 vec_elt (im->fib_index_by_sw_if_index,
1000 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1002 clib_memcpy_fast (t0->packet_data,
1003 vlib_buffer_get_current (b0),
1004 sizeof (t0->packet_data));
1011 /* Compute TCP/UDP/ICMP6 checksum in software. */
1013 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1014 ip6_header_t * ip0, int *bogus_lengthp)
1017 u16 sum16, payload_length_host_byte_order;
1018 u32 i, n_this_buffer, n_bytes_left;
1019 u32 headers_size = sizeof (ip0[0]);
1020 void *data_this_buffer;
1022 ASSERT (bogus_lengthp);
1025 /* Initialize checksum with ip header. */
1026 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1027 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1028 data_this_buffer = (void *) (ip0 + 1);
1030 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1032 sum0 = ip_csum_with_carry (sum0,
1033 clib_mem_unaligned (&ip0->
1034 src_address.as_uword[i],
1037 ip_csum_with_carry (sum0,
1038 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1042 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1043 * or UDP-Ping packets */
1044 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1047 ip6_hop_by_hop_ext_t *ext_hdr =
1048 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1050 /* validate really icmp6 next */
1051 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1052 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
1054 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1055 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1057 payload_length_host_byte_order -= skip_bytes;
1058 headers_size += skip_bytes;
1061 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1065 u32 n_ip_bytes_this_buffer =
1066 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
1067 if (n_this_buffer + headers_size > n_ip_bytes_this_buffer)
1069 n_this_buffer = p0->current_length > headers_size ?
1070 n_ip_bytes_this_buffer - headers_size : 0;
1076 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1077 n_bytes_left -= n_this_buffer;
1078 if (n_bytes_left == 0)
1081 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1086 p0 = vlib_get_buffer (vm, p0->next_buffer);
1087 data_this_buffer = vlib_buffer_get_current (p0);
1088 n_this_buffer = clib_min (p0->current_length, n_bytes_left);
1091 sum16 = ~ip_csum_fold (sum0);
1097 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1099 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1104 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1105 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1106 || ip0->protocol == IP_PROTOCOL_ICMP6
1107 || ip0->protocol == IP_PROTOCOL_UDP
1108 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1110 udp0 = (void *) (ip0 + 1);
1111 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1113 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1114 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
1118 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1120 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1121 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
1128 * @brief returns number of links on which src is reachable.
1131 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1133 const load_balance_t *lb0;
1137 fib_index = vec_elt (im->fib_index_by_sw_if_index,
1138 vnet_buffer (b)->sw_if_index[VLIB_RX]);
1140 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
1141 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
1143 lbi = ip6_fib_table_fwding_lookup (fib_index, &i->src_address);
1144 lb0 = load_balance_get (lbi);
1146 return (fib_urpf_check_size (lb0->lb_urpf));
1150 ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1154 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1155 if (proto0 != IP_PROTOCOL_UDP)
1157 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1158 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1164 VNET_FEATURE_ARC_INIT (ip6_local) =
1166 .arc_name = "ip6-local",
1167 .start_nodes = VNET_FEATURES ("ip6-local"),
1172 ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1173 vlib_frame_t * frame, int head_of_feature_arc)
1175 ip6_main_t *im = &ip6_main;
1176 ip_lookup_main_t *lm = &im->lookup_main;
1177 u32 *from, n_left_from;
1178 vlib_node_runtime_t *error_node =
1179 vlib_node_get_runtime (vm, ip6_input_node.index);
1180 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
1181 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1182 u16 nexts[VLIB_FRAME_SIZE], *next;
1184 from = vlib_frame_vector_args (frame);
1185 n_left_from = frame->n_vectors;
1187 if (node->flags & VLIB_NODE_FLAG_TRACE)
1188 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1190 vlib_get_buffers (vm, from, bufs, n_left_from);
1194 while (n_left_from > 2)
1196 /* Prefetch next iteration. */
1197 if (n_left_from >= 6)
1199 vlib_prefetch_buffer_header (b[4], STORE);
1200 vlib_prefetch_buffer_header (b[5], STORE);
1201 vlib_prefetch_buffer_data (b[2], LOAD);
1202 vlib_prefetch_buffer_data (b[3], LOAD);
1206 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1207 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1209 ip6_header_t *ip[2];
1210 ip[0] = vlib_buffer_get_current (b[0]);
1211 ip[1] = vlib_buffer_get_current (b[1]);
1213 if (head_of_feature_arc)
1215 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1216 vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data;
1219 type[0] = lm->builtin_protocol_by_ip_protocol[ip[0]->protocol];
1220 type[1] = lm->builtin_protocol_by_ip_protocol[ip[1]->protocol];
1223 flags[0] = b[0]->flags;
1224 flags[1] = b[1]->flags;
1226 u32 good_l4_csum[2];
1228 flags[0] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1229 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1230 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1232 flags[1] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1233 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1234 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1236 u32 udp_offset[2] = { };
1239 ip6_next_proto_is_tcp_udp (b[0], ip[0], &udp_offset[0]);
1241 ip6_next_proto_is_tcp_udp (b[1], ip[1], &udp_offset[1]);
1242 i16 len_diff[2] = { 0 };
1243 if (PREDICT_TRUE (is_tcp_udp[0]))
1246 (udp_header_t *) ((u8 *) ip[0] + udp_offset[0]);
1247 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UDP
1248 && udp->checksum == 0;
1249 /* optimistically verify UDP length. */
1250 u16 ip_len, udp_len;
1251 ip_len = clib_net_to_host_u16 (ip[0]->payload_length);
1252 udp_len = clib_net_to_host_u16 (udp->length);
1253 len_diff[0] = ip_len - udp_len;
1255 if (PREDICT_TRUE (is_tcp_udp[1]))
1258 (udp_header_t *) ((u8 *) ip[1] + udp_offset[1]);
1259 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UDP
1260 && udp->checksum == 0;
1261 /* optimistically verify UDP length. */
1262 u16 ip_len, udp_len;
1263 ip_len = clib_net_to_host_u16 (ip[1]->payload_length);
1264 udp_len = clib_net_to_host_u16 (udp->length);
1265 len_diff[1] = ip_len - udp_len;
1268 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1269 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1271 len_diff[0] = type[0] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[0] : 0;
1272 len_diff[1] = type[1] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[1] : 0;
1275 need_csum[0] = type[0] != IP_BUILTIN_PROTOCOL_UNKNOWN
1277 && !(flags[0] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1278 need_csum[1] = type[1] != IP_BUILTIN_PROTOCOL_UNKNOWN
1280 && !(flags[1] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1281 if (PREDICT_FALSE (need_csum[0]))
1283 flags[0] = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1284 good_l4_csum[0] = flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1286 if (PREDICT_FALSE (need_csum[1]))
1288 flags[1] = ip6_tcp_udp_icmp_validate_checksum (vm, b[1]);
1289 good_l4_csum[1] = flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1292 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1293 error[0] = len_diff[0] < 0 ? IP6_ERROR_UDP_LENGTH : error[0];
1294 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1295 error[1] = len_diff[1] < 0 ? IP6_ERROR_UDP_LENGTH : error[1];
1297 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1298 IP6_ERROR_UDP_CHECKSUM,
1299 "Wrong IP6 errors constants");
1300 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1301 IP6_ERROR_ICMP_CHECKSUM,
1302 "Wrong IP6 errors constants");
1305 !good_l4_csum[0] ? IP6_ERROR_UDP_CHECKSUM + type[0] : error[0];
1307 !good_l4_csum[1] ? IP6_ERROR_UDP_CHECKSUM + type[1] : error[1];
1309 /* Drop packets from unroutable hosts. */
1310 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1312 unroutable[0] = error[0] == IP6_ERROR_UNKNOWN_PROTOCOL
1313 && type[0] != IP_BUILTIN_PROTOCOL_ICMP
1314 && !ip6_address_is_link_local_unicast (&ip[0]->src_address);
1315 unroutable[1] = error[1] == IP6_ERROR_UNKNOWN_PROTOCOL
1316 && type[1] != IP_BUILTIN_PROTOCOL_ICMP
1317 && !ip6_address_is_link_local_unicast (&ip[1]->src_address);
1318 if (PREDICT_FALSE (unroutable[0]))
1321 !ip6_urpf_loose_check (im, b[0],
1322 ip[0]) ? IP6_ERROR_SRC_LOOKUP_MISS
1325 if (PREDICT_FALSE (unroutable[1]))
1328 !ip6_urpf_loose_check (im, b[1],
1329 ip[1]) ? IP6_ERROR_SRC_LOOKUP_MISS
1333 vnet_buffer (b[0])->ip.fib_index =
1334 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1335 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1336 vnet_buffer (b[0])->ip.fib_index;
1337 vnet_buffer (b[1])->ip.fib_index =
1338 vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ?
1339 vnet_buffer (b[1])->sw_if_index[VLIB_TX] :
1340 vnet_buffer (b[1])->ip.fib_index;
1341 } /* head_of_feature_arc */
1343 next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol];
1345 error[0] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1346 next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol];
1348 error[1] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[1];
1350 b[0]->error = error_node->errors[0];
1351 b[1]->error = error_node->errors[1];
1353 if (head_of_feature_arc)
1356 ip6_unknown[0] = error[0] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1357 ip6_unknown[1] = error[1] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1358 if (PREDICT_TRUE (ip6_unknown[0]))
1360 u32 next32 = next[0];
1361 vnet_feature_arc_start (arc_index,
1362 vnet_buffer (b[0])->sw_if_index
1363 [VLIB_RX], &next32, b[0]);
1366 if (PREDICT_TRUE (ip6_unknown[1]))
1368 u32 next32 = next[1];
1369 vnet_feature_arc_start (arc_index,
1370 vnet_buffer (b[1])->sw_if_index
1371 [VLIB_RX], &next32, b[1]);
1385 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1388 ip = vlib_buffer_get_current (b[0]);
1390 if (head_of_feature_arc)
1392 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1393 u8 type = lm->builtin_protocol_by_ip_protocol[ip->protocol];
1395 u32 flags = b[0]->flags;
1397 flags & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1398 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1399 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1403 u8 is_tcp_udp = ip6_next_proto_is_tcp_udp (b[0], ip, &udp_offset);
1404 if (PREDICT_TRUE (is_tcp_udp))
1406 udp_header_t *udp = (udp_header_t *) ((u8 *) ip + udp_offset);
1407 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1408 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UDP
1409 && udp->checksum == 0;
1410 /* optimistically verify UDP length. */
1411 u16 ip_len, udp_len;
1412 ip_len = clib_net_to_host_u16 (ip->payload_length);
1413 udp_len = clib_net_to_host_u16 (udp->length);
1414 len_diff = ip_len - udp_len;
1417 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UNKNOWN;
1418 len_diff = type == IP_BUILTIN_PROTOCOL_UDP ? len_diff : 0;
1420 u8 need_csum = type != IP_BUILTIN_PROTOCOL_UNKNOWN && !good_l4_csum
1421 && !(flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1422 if (PREDICT_FALSE (need_csum))
1424 flags = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1425 good_l4_csum = flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1428 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1429 error = len_diff < 0 ? IP6_ERROR_UDP_LENGTH : error;
1431 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1432 IP6_ERROR_UDP_CHECKSUM,
1433 "Wrong IP6 errors constants");
1434 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1435 IP6_ERROR_ICMP_CHECKSUM,
1436 "Wrong IP6 errors constants");
1438 error = !good_l4_csum ? IP6_ERROR_UDP_CHECKSUM + type : error;
1440 /* Drop packets from unroutable hosts. */
1441 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1442 u8 unroutable = error == IP6_ERROR_UNKNOWN_PROTOCOL
1443 && type != IP_BUILTIN_PROTOCOL_ICMP
1444 && !ip6_address_is_link_local_unicast (&ip->src_address);
1445 if (PREDICT_FALSE (unroutable))
1448 !ip6_urpf_loose_check (im, b[0],
1449 ip) ? IP6_ERROR_SRC_LOOKUP_MISS :
1453 vnet_buffer (b[0])->ip.fib_index =
1454 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1455 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1456 vnet_buffer (b[0])->ip.fib_index;
1457 } /* head_of_feature_arc */
1459 next[0] = lm->local_next_by_ip_protocol[ip->protocol];
1461 error != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1463 b[0]->error = error_node->errors[0];
1465 if (head_of_feature_arc)
1467 if (PREDICT_TRUE (error == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1469 u32 next32 = next[0];
1470 vnet_feature_arc_start (arc_index,
1471 vnet_buffer (b[0])->sw_if_index
1472 [VLIB_RX], &next32, b[0]);
1483 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1484 return frame->n_vectors;
1487 VLIB_NODE_FN (ip6_local_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1488 vlib_frame_t * frame)
1490 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1494 VLIB_REGISTER_NODE (ip6_local_node) =
1496 .name = "ip6-local",
1497 .vector_size = sizeof (u32),
1498 .format_trace = format_ip6_forward_next_trace,
1499 .n_next_nodes = IP_LOCAL_N_NEXT,
1502 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1503 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
1504 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1505 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1506 [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-reassembly",
1511 VLIB_NODE_FN (ip6_local_end_of_arc_node) (vlib_main_t * vm,
1512 vlib_node_runtime_t * node,
1513 vlib_frame_t * frame)
1515 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1519 VLIB_REGISTER_NODE (ip6_local_end_of_arc_node) = {
1520 .name = "ip6-local-end-of-arc",
1521 .vector_size = sizeof (u32),
1523 .format_trace = format_ip6_forward_next_trace,
1524 .sibling_of = "ip6-local",
1527 VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1528 .arc_name = "ip6-local",
1529 .node_name = "ip6-local-end-of-arc",
1530 .runs_before = 0, /* not before any other features */
1534 #ifdef CLIB_MARCH_VARIANT
1535 extern vlib_node_registration_t ip6_local_node;
1540 ip6_register_protocol (u32 protocol, u32 node_index)
1542 vlib_main_t *vm = vlib_get_main ();
1543 ip6_main_t *im = &ip6_main;
1544 ip_lookup_main_t *lm = &im->lookup_main;
1546 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1547 lm->local_next_by_ip_protocol[protocol] =
1548 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1552 ip6_unregister_protocol (u32 protocol)
1554 ip6_main_t *im = &ip6_main;
1555 ip_lookup_main_t *lm = &im->lookup_main;
1557 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1558 lm->local_next_by_ip_protocol[protocol] = IP_LOCAL_NEXT_PUNT;
1562 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index,
1565 vnet_main_t *vnm = vnet_get_main ();
1566 ip6_main_t *im = &ip6_main;
1567 icmp6_neighbor_solicitation_header_t *h;
1569 ip_interface_address_t *ia;
1570 ip_adjacency_t *adj;
1571 vnet_hw_interface_t *hi;
1572 vnet_sw_interface_t *si;
1578 si = vnet_get_sw_interface (vnm, sw_if_index);
1580 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1582 return clib_error_return (0, "%U: interface %U down",
1583 format_ip6_address, dst,
1584 format_vnet_sw_if_index_name, vnm,
1589 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1592 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1593 return clib_error_return
1594 (0, "no matching interface address for destination %U (interface %U)",
1595 format_ip6_address, dst,
1596 format_vnet_sw_if_index_name, vnm, sw_if_index);
1600 vlib_packet_template_get_packet (vm,
1601 &im->discover_neighbor_packet_template,
1604 return clib_error_return (0, "ICMP6 NS packet allocation failed");
1606 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1608 /* Destination address is a solicited node multicast address. We need to fill in
1609 the low 24 bits with low 24 bits of target's address. */
1610 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1611 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1612 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1614 h->ip.src_address = src[0];
1615 h->neighbor.target_address = dst[0];
1617 if (PREDICT_FALSE (!hi->hw_address))
1619 return clib_error_return (0, "%U: interface %U do not support ip probe",
1620 format_ip6_address, dst,
1621 format_vnet_sw_if_index_name, vnm,
1625 clib_memcpy_fast (h->link_layer_option.ethernet_address, hi->hw_address,
1626 vec_len (hi->hw_address));
1628 h->neighbor.icmp.checksum =
1629 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1630 ASSERT (bogus_length == 0);
1632 b = vlib_get_buffer (vm, bi);
1633 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1634 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1636 /* Add encapsulation string for software interface (e.g. ethernet header). */
1637 ip46_address_t nh = {
1641 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1642 VNET_LINK_IP6, &nh, sw_if_index);
1645 /* Peer has been previously resolved, retrieve glean adj instead */
1646 if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE && refresh == 0)
1649 ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1650 VNET_LINK_IP6, sw_if_index, &nh);
1654 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1655 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1658 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1659 u32 *to_next = vlib_frame_vector_args (f);
1662 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1666 return /* no error */ 0;
1672 IP6_REWRITE_NEXT_DROP,
1673 IP6_REWRITE_NEXT_ICMP_ERROR,
1674 IP6_REWRITE_NEXT_FRAGMENT,
1675 IP6_REWRITE_N_NEXT /* Last */
1676 } ip6_rewrite_next_t;
1679 * This bits of an IPv6 address to mask to construct a multicast
1682 #define IP6_MCAST_ADDR_MASK 0xffffffff
1685 ip6_mtu_check (vlib_buffer_t * b, u16 packet_bytes,
1686 u16 adj_packet_bytes, bool is_locally_generated,
1687 u32 * next, u32 * error)
1689 if (adj_packet_bytes >= 1280 && packet_bytes > adj_packet_bytes)
1691 if (is_locally_generated)
1693 /* IP fragmentation */
1694 ip_frag_set_vnet_buffer (b, adj_packet_bytes,
1695 IP6_FRAG_NEXT_IP6_REWRITE, 0);
1696 *next = IP6_REWRITE_NEXT_FRAGMENT;
1697 *error = IP6_ERROR_MTU_EXCEEDED;
1701 *error = IP6_ERROR_MTU_EXCEEDED;
1702 icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1704 *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1710 ip6_rewrite_inline_with_gso (vlib_main_t * vm,
1711 vlib_node_runtime_t * node,
1712 vlib_frame_t * frame,
1713 int do_counters, int is_midchain, int is_mcast,
1716 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1717 u32 *from = vlib_frame_vector_args (frame);
1718 u32 n_left_from, n_left_to_next, *to_next, next_index;
1719 vlib_node_runtime_t *error_node =
1720 vlib_node_get_runtime (vm, ip6_input_node.index);
1722 n_left_from = frame->n_vectors;
1723 next_index = node->cached_next_index;
1724 u32 thread_index = vm->thread_index;
1726 while (n_left_from > 0)
1728 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1730 while (n_left_from >= 4 && n_left_to_next >= 2)
1732 ip_adjacency_t *adj0, *adj1;
1733 vlib_buffer_t *p0, *p1;
1734 ip6_header_t *ip0, *ip1;
1735 u32 pi0, rw_len0, next0, error0, adj_index0;
1736 u32 pi1, rw_len1, next1, error1, adj_index1;
1737 u32 tx_sw_if_index0, tx_sw_if_index1;
1738 bool is_locally_originated0, is_locally_originated1;
1740 /* Prefetch next iteration. */
1742 vlib_buffer_t *p2, *p3;
1744 p2 = vlib_get_buffer (vm, from[2]);
1745 p3 = vlib_get_buffer (vm, from[3]);
1747 vlib_prefetch_buffer_header (p2, LOAD);
1748 vlib_prefetch_buffer_header (p3, LOAD);
1750 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1751 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1753 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1754 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1757 pi0 = to_next[0] = from[0];
1758 pi1 = to_next[1] = from[1];
1763 n_left_to_next -= 2;
1765 p0 = vlib_get_buffer (vm, pi0);
1766 p1 = vlib_get_buffer (vm, pi1);
1768 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1769 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1771 ip0 = vlib_buffer_get_current (p0);
1772 ip1 = vlib_buffer_get_current (p1);
1774 error0 = error1 = IP6_ERROR_NONE;
1775 next0 = next1 = IP6_REWRITE_NEXT_DROP;
1777 is_locally_originated0 =
1778 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1779 if (PREDICT_TRUE (!is_locally_originated0))
1781 i32 hop_limit0 = ip0->hop_limit;
1783 /* Input node should have reject packets with hop limit 0. */
1784 ASSERT (ip0->hop_limit > 0);
1788 ip0->hop_limit = hop_limit0;
1791 * If the hop count drops below 1 when forwarding, generate
1794 if (PREDICT_FALSE (hop_limit0 <= 0))
1796 error0 = IP6_ERROR_TIME_EXPIRED;
1797 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1798 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1799 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1800 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1806 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1808 is_locally_originated1 =
1809 p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1810 if (PREDICT_TRUE (!is_locally_originated1))
1812 i32 hop_limit1 = ip1->hop_limit;
1814 /* Input node should have reject packets with hop limit 0. */
1815 ASSERT (ip1->hop_limit > 0);
1819 ip1->hop_limit = hop_limit1;
1822 * If the hop count drops below 1 when forwarding, generate
1825 if (PREDICT_FALSE (hop_limit1 <= 0))
1827 error1 = IP6_ERROR_TIME_EXPIRED;
1828 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1829 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1830 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1831 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1837 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1839 adj0 = adj_get (adj_index0);
1840 adj1 = adj_get (adj_index1);
1842 rw_len0 = adj0[0].rewrite_header.data_bytes;
1843 rw_len1 = adj1[0].rewrite_header.data_bytes;
1844 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1845 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
1849 vlib_increment_combined_counter
1850 (&adjacency_counters,
1851 thread_index, adj_index0, 1,
1852 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1853 vlib_increment_combined_counter
1854 (&adjacency_counters,
1855 thread_index, adj_index1, 1,
1856 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1859 /* Check MTU of outgoing interface. */
1861 clib_net_to_host_u16 (ip0->payload_length) +
1862 sizeof (ip6_header_t);
1864 clib_net_to_host_u16 (ip1->payload_length) +
1865 sizeof (ip6_header_t);
1866 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
1867 ip0_len = gso_mtu_sz (p0);
1868 if (do_gso && (p1->flags & VNET_BUFFER_F_GSO))
1869 ip1_len = gso_mtu_sz (p1);
1873 ip6_mtu_check (p0, ip0_len,
1874 adj0[0].rewrite_header.max_l3_packet_bytes,
1875 is_locally_originated0, &next0, &error0);
1876 ip6_mtu_check (p1, ip1_len,
1877 adj1[0].rewrite_header.max_l3_packet_bytes,
1878 is_locally_originated1, &next1, &error1);
1880 /* Don't adjust the buffer for hop count issue; icmp-error node
1881 * wants to see the IP header */
1882 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1884 p0->current_data -= rw_len0;
1885 p0->current_length += rw_len0;
1887 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1888 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1889 next0 = adj0[0].rewrite_header.next_index;
1892 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1893 vnet_feature_arc_start (lm->output_feature_arc_index,
1894 tx_sw_if_index0, &next0, p0);
1898 p0->error = error_node->errors[error0];
1900 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1902 p1->current_data -= rw_len1;
1903 p1->current_length += rw_len1;
1905 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1906 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1907 next1 = adj1[0].rewrite_header.next_index;
1910 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1911 vnet_feature_arc_start (lm->output_feature_arc_index,
1912 tx_sw_if_index1, &next1, p1);
1916 p1->error = error_node->errors[error1];
1921 /* before we paint on the next header, update the L4
1922 * checksums if required, since there's no offload on a tunnel */
1923 calc_checksums (vm, p0);
1924 calc_checksums (vm, p1);
1927 /* Guess we are only writing on simple Ethernet header. */
1928 vnet_rewrite_two_headers (adj0[0], adj1[0],
1929 ip0, ip1, sizeof (ethernet_header_t));
1933 if (adj0->sub_type.midchain.fixup_func)
1934 adj0->sub_type.midchain.fixup_func
1935 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1936 if (adj1->sub_type.midchain.fixup_func)
1937 adj1->sub_type.midchain.fixup_func
1938 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
1943 * copy bytes from the IP address into the MAC rewrite
1945 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1947 rewrite_header.dst_mcast_offset,
1948 &ip0->dst_address.as_u32[3],
1950 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1952 rewrite_header.dst_mcast_offset,
1953 &ip1->dst_address.as_u32[3],
1957 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1958 to_next, n_left_to_next,
1959 pi0, pi1, next0, next1);
1962 while (n_left_from > 0 && n_left_to_next > 0)
1964 ip_adjacency_t *adj0;
1968 u32 adj_index0, next0, error0;
1969 u32 tx_sw_if_index0;
1970 bool is_locally_originated0;
1972 pi0 = to_next[0] = from[0];
1974 p0 = vlib_get_buffer (vm, pi0);
1976 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1978 adj0 = adj_get (adj_index0);
1980 ip0 = vlib_buffer_get_current (p0);
1982 error0 = IP6_ERROR_NONE;
1983 next0 = IP6_REWRITE_NEXT_DROP;
1985 /* Check hop limit */
1986 is_locally_originated0 =
1987 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1988 if (PREDICT_TRUE (!is_locally_originated0))
1990 i32 hop_limit0 = ip0->hop_limit;
1992 ASSERT (ip0->hop_limit > 0);
1996 ip0->hop_limit = hop_limit0;
1998 if (PREDICT_FALSE (hop_limit0 <= 0))
2001 * If the hop count drops below 1 when forwarding, generate
2004 error0 = IP6_ERROR_TIME_EXPIRED;
2005 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2006 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2007 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2008 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2014 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2019 calc_checksums (vm, p0);
2022 /* Guess we are only writing on simple Ethernet header. */
2023 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2025 /* Update packet buffer attributes/set output interface. */
2026 rw_len0 = adj0[0].rewrite_header.data_bytes;
2027 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2031 vlib_increment_combined_counter
2032 (&adjacency_counters,
2033 thread_index, adj_index0, 1,
2034 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2037 /* Check MTU of outgoing interface. */
2039 clib_net_to_host_u16 (ip0->payload_length) +
2040 sizeof (ip6_header_t);
2041 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
2042 ip0_len = gso_mtu_sz (p0);
2044 ip6_mtu_check (p0, ip0_len,
2045 adj0[0].rewrite_header.max_l3_packet_bytes,
2046 is_locally_originated0, &next0, &error0);
2048 /* Don't adjust the buffer for hop count issue; icmp-error node
2049 * wants to see the IP header */
2050 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2052 p0->current_data -= rw_len0;
2053 p0->current_length += rw_len0;
2055 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2057 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2058 next0 = adj0[0].rewrite_header.next_index;
2061 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2062 vnet_feature_arc_start (lm->output_feature_arc_index,
2063 tx_sw_if_index0, &next0, p0);
2067 p0->error = error_node->errors[error0];
2072 if (adj0->sub_type.midchain.fixup_func)
2073 adj0->sub_type.midchain.fixup_func
2074 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2078 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2080 rewrite_header.dst_mcast_offset,
2081 &ip0->dst_address.as_u32[3],
2088 n_left_to_next -= 1;
2090 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2091 to_next, n_left_to_next,
2095 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2098 /* Need to do trace after rewrites to pick up new packet data. */
2099 if (node->flags & VLIB_NODE_FLAG_TRACE)
2100 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2102 return frame->n_vectors;
2106 ip6_rewrite_inline (vlib_main_t * vm,
2107 vlib_node_runtime_t * node,
2108 vlib_frame_t * frame,
2109 int do_counters, int is_midchain, int is_mcast)
2111 vnet_main_t *vnm = vnet_get_main ();
2112 if (PREDICT_FALSE (vnm->interface_main.gso_interface_count > 0))
2113 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2114 is_midchain, is_mcast,
2117 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2118 is_midchain, is_mcast,
2119 0 /* no do_gso */ );
2122 VLIB_NODE_FN (ip6_rewrite_node) (vlib_main_t * vm,
2123 vlib_node_runtime_t * node,
2124 vlib_frame_t * frame)
2126 if (adj_are_counters_enabled ())
2127 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2129 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2132 VLIB_NODE_FN (ip6_rewrite_bcast_node) (vlib_main_t * vm,
2133 vlib_node_runtime_t * node,
2134 vlib_frame_t * frame)
2136 if (adj_are_counters_enabled ())
2137 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2139 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2142 VLIB_NODE_FN (ip6_rewrite_mcast_node) (vlib_main_t * vm,
2143 vlib_node_runtime_t * node,
2144 vlib_frame_t * frame)
2146 if (adj_are_counters_enabled ())
2147 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2149 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2152 VLIB_NODE_FN (ip6_midchain_node) (vlib_main_t * vm,
2153 vlib_node_runtime_t * node,
2154 vlib_frame_t * frame)
2156 if (adj_are_counters_enabled ())
2157 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2159 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2162 VLIB_NODE_FN (ip6_mcast_midchain_node) (vlib_main_t * vm,
2163 vlib_node_runtime_t * node,
2164 vlib_frame_t * frame)
2166 if (adj_are_counters_enabled ())
2167 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2169 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2173 VLIB_REGISTER_NODE (ip6_midchain_node) =
2175 .name = "ip6-midchain",
2176 .vector_size = sizeof (u32),
2177 .format_trace = format_ip6_forward_next_trace,
2178 .sibling_of = "ip6-rewrite",
2181 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2183 .name = "ip6-rewrite",
2184 .vector_size = sizeof (u32),
2185 .format_trace = format_ip6_rewrite_trace,
2186 .n_next_nodes = IP6_REWRITE_N_NEXT,
2189 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
2190 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2191 [IP6_REWRITE_NEXT_FRAGMENT] = "ip6-frag",
2195 VLIB_REGISTER_NODE (ip6_rewrite_bcast_node) = {
2196 .name = "ip6-rewrite-bcast",
2197 .vector_size = sizeof (u32),
2199 .format_trace = format_ip6_rewrite_trace,
2200 .sibling_of = "ip6-rewrite",
2203 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2205 .name = "ip6-rewrite-mcast",
2206 .vector_size = sizeof (u32),
2207 .format_trace = format_ip6_rewrite_trace,
2208 .sibling_of = "ip6-rewrite",
2212 VLIB_REGISTER_NODE (ip6_mcast_midchain_node) =
2214 .name = "ip6-mcast-midchain",
2215 .vector_size = sizeof (u32),
2216 .format_trace = format_ip6_rewrite_trace,
2217 .sibling_of = "ip6-rewrite",
2223 * Hop-by-Hop handling
2225 #ifndef CLIB_MARCH_VARIANT
2226 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2227 #endif /* CLIB_MARCH_VARIANT */
2229 #define foreach_ip6_hop_by_hop_error \
2230 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2231 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2232 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2237 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2238 foreach_ip6_hop_by_hop_error
2240 IP6_HOP_BY_HOP_N_ERROR,
2241 } ip6_hop_by_hop_error_t;
2245 * Primary h-b-h handler trace support
2246 * We work pretty hard on the problem for obvious reasons
2252 u8 option_data[256];
2253 } ip6_hop_by_hop_trace_t;
2255 extern vlib_node_registration_t ip6_hop_by_hop_node;
2257 static char *ip6_hop_by_hop_error_strings[] = {
2258 #define _(sym,string) string,
2259 foreach_ip6_hop_by_hop_error
2263 #ifndef CLIB_MARCH_VARIANT
2265 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2267 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2268 int total_len = va_arg (*args, int);
2269 ip6_hop_by_hop_option_t *opt0, *limit0;
2270 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2273 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2274 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2276 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2277 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2279 while (opt0 < limit0)
2284 case 0: /* Pad, just stop */
2285 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2289 if (hm->trace[type0])
2291 s = (*hm->trace[type0]) (s, opt0);
2296 format (s, "\n unrecognized option %d length %d", type0,
2300 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2301 sizeof (ip6_hop_by_hop_option_t));
2310 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2312 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2313 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2314 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2315 ip6_hop_by_hop_header_t *hbh0;
2316 ip6_hop_by_hop_option_t *opt0, *limit0;
2317 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2321 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2323 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2324 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2326 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2327 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2329 while (opt0 < limit0)
2334 case 0: /* Pad, just stop */
2335 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2339 if (hm->trace[type0])
2341 s = (*hm->trace[type0]) (s, opt0);
2346 format (s, "\n unrecognized option %d length %d", type0,
2350 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2351 sizeof (ip6_hop_by_hop_option_t));
2359 ip6_scan_hbh_options (vlib_buffer_t * b0,
2361 ip6_hop_by_hop_header_t * hbh0,
2362 ip6_hop_by_hop_option_t * opt0,
2363 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2365 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2369 while (opt0 < limit0)
2375 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2380 if (hm->options[type0])
2382 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2384 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2390 /* Unrecognized mandatory option, check the two high order bits */
2391 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2393 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2395 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2396 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2397 *next0 = IP_LOOKUP_NEXT_DROP;
2399 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2400 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2401 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2402 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2403 ICMP6_parameter_problem_unrecognized_option,
2404 (u8 *) opt0 - (u8 *) ip0);
2406 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2407 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2408 if (!ip6_address_is_multicast (&ip0->dst_address))
2410 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2411 icmp6_error_set_vnet_buffer (b0,
2412 ICMP6_parameter_problem,
2413 ICMP6_parameter_problem_unrecognized_option,
2414 (u8 *) opt0 - (u8 *) ip0);
2418 *next0 = IP_LOOKUP_NEXT_DROP;
2426 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2427 sizeof (ip6_hop_by_hop_option_t));
2433 * Process the Hop-by-Hop Options header
2435 VLIB_NODE_FN (ip6_hop_by_hop_node) (vlib_main_t * vm,
2436 vlib_node_runtime_t * node,
2437 vlib_frame_t * frame)
2439 vlib_node_runtime_t *error_node =
2440 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2441 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2442 u32 n_left_from, *from, *to_next;
2443 ip_lookup_next_t next_index;
2445 from = vlib_frame_vector_args (frame);
2446 n_left_from = frame->n_vectors;
2447 next_index = node->cached_next_index;
2449 while (n_left_from > 0)
2453 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2455 while (n_left_from >= 4 && n_left_to_next >= 2)
2458 vlib_buffer_t *b0, *b1;
2460 ip6_header_t *ip0, *ip1;
2461 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2462 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2463 u8 error0 = 0, error1 = 0;
2465 /* Prefetch next iteration. */
2467 vlib_buffer_t *p2, *p3;
2469 p2 = vlib_get_buffer (vm, from[2]);
2470 p3 = vlib_get_buffer (vm, from[3]);
2472 vlib_prefetch_buffer_header (p2, LOAD);
2473 vlib_prefetch_buffer_header (p3, LOAD);
2475 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2476 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2479 /* Speculatively enqueue b0, b1 to the current next frame */
2480 to_next[0] = bi0 = from[0];
2481 to_next[1] = bi1 = from[1];
2485 n_left_to_next -= 2;
2487 b0 = vlib_get_buffer (vm, bi0);
2488 b1 = vlib_get_buffer (vm, bi1);
2490 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2491 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2492 ip_adjacency_t *adj0 = adj_get (adj_index0);
2493 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2494 ip_adjacency_t *adj1 = adj_get (adj_index1);
2496 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2497 next0 = adj0->lookup_next_index;
2498 next1 = adj1->lookup_next_index;
2500 ip0 = vlib_buffer_get_current (b0);
2501 ip1 = vlib_buffer_get_current (b1);
2502 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2503 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2504 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2505 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2507 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2508 ((hbh0->length + 1) << 3));
2510 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2511 ((hbh1->length + 1) << 3));
2514 * Basic validity checks
2516 if ((hbh0->length + 1) << 3 >
2517 clib_net_to_host_u16 (ip0->payload_length))
2519 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2520 next0 = IP_LOOKUP_NEXT_DROP;
2523 /* Scan the set of h-b-h options, process ones that we understand */
2524 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2526 if ((hbh1->length + 1) << 3 >
2527 clib_net_to_host_u16 (ip1->payload_length))
2529 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2530 next1 = IP_LOOKUP_NEXT_DROP;
2533 /* Scan the set of h-b-h options, process ones that we understand */
2534 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2537 /* Has the classifier flagged this buffer for special treatment? */
2540 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2541 next0 = hm->next_override;
2543 /* Has the classifier flagged this buffer for special treatment? */
2546 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2547 next1 = hm->next_override;
2549 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2551 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2553 ip6_hop_by_hop_trace_t *t =
2554 vlib_add_trace (vm, node, b0, sizeof (*t));
2555 u32 trace_len = (hbh0->length + 1) << 3;
2556 t->next_index = next0;
2557 /* Capture the h-b-h option verbatim */
2560 ARRAY_LEN (t->option_data) ? trace_len :
2561 ARRAY_LEN (t->option_data);
2562 t->trace_len = trace_len;
2563 clib_memcpy_fast (t->option_data, hbh0, trace_len);
2565 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2567 ip6_hop_by_hop_trace_t *t =
2568 vlib_add_trace (vm, node, b1, sizeof (*t));
2569 u32 trace_len = (hbh1->length + 1) << 3;
2570 t->next_index = next1;
2571 /* Capture the h-b-h option verbatim */
2574 ARRAY_LEN (t->option_data) ? trace_len :
2575 ARRAY_LEN (t->option_data);
2576 t->trace_len = trace_len;
2577 clib_memcpy_fast (t->option_data, hbh1, trace_len);
2582 b0->error = error_node->errors[error0];
2583 b1->error = error_node->errors[error1];
2585 /* verify speculative enqueue, maybe switch current next frame */
2586 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2587 n_left_to_next, bi0, bi1, next0,
2591 while (n_left_from > 0 && n_left_to_next > 0)
2597 ip6_hop_by_hop_header_t *hbh0;
2598 ip6_hop_by_hop_option_t *opt0, *limit0;
2601 /* Speculatively enqueue b0 to the current next frame */
2607 n_left_to_next -= 1;
2609 b0 = vlib_get_buffer (vm, bi0);
2611 * Default use the next_index from the adjacency.
2612 * A HBH option rarely redirects to a different node
2614 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2615 ip_adjacency_t *adj0 = adj_get (adj_index0);
2616 next0 = adj0->lookup_next_index;
2618 ip0 = vlib_buffer_get_current (b0);
2619 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2620 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2622 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2623 ((hbh0->length + 1) << 3));
2626 * Basic validity checks
2628 if ((hbh0->length + 1) << 3 >
2629 clib_net_to_host_u16 (ip0->payload_length))
2631 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2632 next0 = IP_LOOKUP_NEXT_DROP;
2636 /* Scan the set of h-b-h options, process ones that we understand */
2637 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2640 /* Has the classifier flagged this buffer for special treatment? */
2643 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2644 next0 = hm->next_override;
2646 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2648 ip6_hop_by_hop_trace_t *t =
2649 vlib_add_trace (vm, node, b0, sizeof (*t));
2650 u32 trace_len = (hbh0->length + 1) << 3;
2651 t->next_index = next0;
2652 /* Capture the h-b-h option verbatim */
2655 ARRAY_LEN (t->option_data) ? trace_len :
2656 ARRAY_LEN (t->option_data);
2657 t->trace_len = trace_len;
2658 clib_memcpy_fast (t->option_data, hbh0, trace_len);
2661 b0->error = error_node->errors[error0];
2663 /* verify speculative enqueue, maybe switch current next frame */
2664 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2665 n_left_to_next, bi0, next0);
2667 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2669 return frame->n_vectors;
2673 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2675 .name = "ip6-hop-by-hop",
2676 .sibling_of = "ip6-lookup",
2677 .vector_size = sizeof (u32),
2678 .format_trace = format_ip6_hop_by_hop_trace,
2679 .type = VLIB_NODE_TYPE_INTERNAL,
2680 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2681 .error_strings = ip6_hop_by_hop_error_strings,
2686 static clib_error_t *
2687 ip6_hop_by_hop_init (vlib_main_t * vm)
2689 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2690 clib_memset (hm->options, 0, sizeof (hm->options));
2691 clib_memset (hm->trace, 0, sizeof (hm->trace));
2692 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2696 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2698 #ifndef CLIB_MARCH_VARIANT
2700 ip6_hbh_set_next_override (uword next)
2702 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2704 hm->next_override = next;
2708 ip6_hbh_register_option (u8 option,
2709 int options (vlib_buffer_t * b, ip6_header_t * ip,
2710 ip6_hop_by_hop_option_t * opt),
2711 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2713 ip6_main_t *im = &ip6_main;
2714 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2716 ASSERT ((u32) option < ARRAY_LEN (hm->options));
2718 /* Already registered */
2719 if (hm->options[option])
2722 hm->options[option] = options;
2723 hm->trace[option] = trace;
2725 /* Set global variable */
2726 im->hbh_enabled = 1;
2732 ip6_hbh_unregister_option (u8 option)
2734 ip6_main_t *im = &ip6_main;
2735 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2737 ASSERT ((u32) option < ARRAY_LEN (hm->options));
2739 /* Not registered */
2740 if (!hm->options[option])
2743 hm->options[option] = NULL;
2744 hm->trace[option] = NULL;
2746 /* Disable global knob if this was the last option configured */
2749 for (i = 0; i < 256; i++)
2751 if (hm->options[option])
2758 im->hbh_enabled = 0;
2763 /* Global IP6 main. */
2764 ip6_main_t ip6_main;
2767 static clib_error_t *
2768 ip6_lookup_init (vlib_main_t * vm)
2770 ip6_main_t *im = &ip6_main;
2771 clib_error_t *error;
2774 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2777 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2784 for (j = 0; j < i0; j++)
2785 im->fib_masks[i].as_u32[j] = ~0;
2788 im->fib_masks[i].as_u32[i0] =
2789 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2792 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2794 if (im->lookup_table_nbuckets == 0)
2795 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2797 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2799 if (im->lookup_table_size == 0)
2800 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2802 clib_bihash_init_24_8 (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2803 "ip6 FIB fwding table",
2804 im->lookup_table_nbuckets, im->lookup_table_size);
2805 clib_bihash_init_24_8 (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2806 "ip6 FIB non-fwding table",
2807 im->lookup_table_nbuckets, im->lookup_table_size);
2808 clib_bihash_init_40_8 (&im->ip6_mtable.ip6_mhash,
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 clib_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);
2861 static clib_error_t *
2862 test_ip6_link_command_fn (vlib_main_t * vm,
2863 unformat_input_t * input, vlib_cli_command_t * cmd)
2866 ip6_address_t _a, *a = &_a;
2868 if (unformat (input, "%U", unformat_ethernet_address, mac))
2870 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2871 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
2872 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2873 vlib_cli_output (vm, "Original MAC address: %U",
2874 format_ethernet_address, mac);
2881 * This command converts the given MAC Address into an IPv6 link-local
2885 * Example of how to create an IPv6 link-local address:
2886 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2887 * Link local address: fe80::14d9:e0ff:fe91:7986
2888 * Original MAC address: 16:d9:e0:91:79:86
2892 VLIB_CLI_COMMAND (test_link_command, static) =
2894 .path = "test ip6 link",
2895 .function = test_ip6_link_command_fn,
2896 .short_help = "test ip6 link <mac-address>",
2900 #ifndef CLIB_MARCH_VARIANT
2902 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2906 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
2908 if (~0 == fib_index)
2909 return VNET_API_ERROR_NO_SUCH_FIB;
2911 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2918 static clib_error_t *
2919 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2920 unformat_input_t * input,
2921 vlib_cli_command_t * cmd)
2925 u32 flow_hash_config = 0;
2928 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2930 if (unformat (input, "table %d", &table_id))
2933 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2934 foreach_flow_hash_bit
2941 return clib_error_return (0, "unknown input `%U'",
2942 format_unformat_error, input);
2944 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2951 return clib_error_return (0, "no such FIB table %d", table_id);
2954 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2962 * Configure the set of IPv6 fields used by the flow hash.
2966 * Example of how to set the flow hash on a given table:
2967 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2969 * Example of display the configured flow hash:
2970 * @cliexstart{show ip6 fib}
2971 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2974 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2975 * [0] [@0]: dpo-drop ip6
2978 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2979 * [0] [@2]: dpo-receive
2982 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2983 * [0] [@2]: dpo-receive
2986 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2987 * [0] [@2]: dpo-receive
2990 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2991 * [0] [@2]: dpo-receive
2992 * ff02::1:ff00:0/104
2994 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
2995 * [0] [@2]: dpo-receive
2996 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
2999 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3000 * [0] [@0]: dpo-drop ip6
3003 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3004 * [0] [@4]: ipv6-glean: af_packet0
3007 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3008 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3011 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3012 * [0] [@2]: dpo-receive
3013 * fe80::fe:3eff:fe3e:9222/128
3015 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3016 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3019 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3020 * [0] [@2]: dpo-receive
3023 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3024 * [0] [@2]: dpo-receive
3027 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3028 * [0] [@2]: dpo-receive
3029 * ff02::1:ff00:0/104
3031 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3032 * [0] [@2]: dpo-receive
3037 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3039 .path = "set ip6 flow-hash",
3041 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3042 .function = set_ip6_flow_hash_command_fn,
3046 static clib_error_t *
3047 show_ip6_local_command_fn (vlib_main_t * vm,
3048 unformat_input_t * input, vlib_cli_command_t * cmd)
3050 ip6_main_t *im = &ip6_main;
3051 ip_lookup_main_t *lm = &im->lookup_main;
3054 vlib_cli_output (vm, "Protocols handled by ip6_local");
3055 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3057 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3060 u32 node_index = vlib_get_node (vm,
3061 ip6_local_node.index)->
3062 next_nodes[lm->local_next_by_ip_protocol[i]];
3063 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3073 * Display the set of protocols handled by the local IPv6 stack.
3076 * Example of how to display local protocol table:
3077 * @cliexstart{show ip6 local}
3078 * Protocols handled by ip6_local
3086 VLIB_CLI_COMMAND (show_ip6_local, static) =
3088 .path = "show ip6 local",
3089 .function = show_ip6_local_command_fn,
3090 .short_help = "show ip6 local",
3094 #ifndef CLIB_MARCH_VARIANT
3096 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3099 vnet_main_t *vnm = vnet_get_main ();
3100 vnet_interface_main_t *im = &vnm->interface_main;
3101 ip6_main_t *ipm = &ip6_main;
3102 ip_lookup_main_t *lm = &ipm->lookup_main;
3103 vnet_classify_main_t *cm = &vnet_classify_main;
3104 ip6_address_t *if_addr;
3106 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3107 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3109 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3110 return VNET_API_ERROR_NO_SUCH_ENTRY;
3112 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3113 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3115 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3117 if (NULL != if_addr)
3119 fib_prefix_t pfx = {
3121 .fp_proto = FIB_PROTOCOL_IP6,
3122 .fp_addr.ip6 = *if_addr,
3126 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3130 if (table_index != (u32) ~ 0)
3132 dpo_id_t dpo = DPO_INVALID;
3137 classify_dpo_create (DPO_PROTO_IP6, table_index));
3139 fib_table_entry_special_dpo_add (fib_index,
3141 FIB_SOURCE_CLASSIFY,
3142 FIB_ENTRY_FLAG_NONE, &dpo);
3147 fib_table_entry_special_remove (fib_index,
3148 &pfx, FIB_SOURCE_CLASSIFY);
3156 static clib_error_t *
3157 set_ip6_classify_command_fn (vlib_main_t * vm,
3158 unformat_input_t * input,
3159 vlib_cli_command_t * cmd)
3161 u32 table_index = ~0;
3162 int table_index_set = 0;
3163 u32 sw_if_index = ~0;
3166 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3168 if (unformat (input, "table-index %d", &table_index))
3169 table_index_set = 1;
3170 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3171 vnet_get_main (), &sw_if_index))
3177 if (table_index_set == 0)
3178 return clib_error_return (0, "classify table-index must be specified");
3180 if (sw_if_index == ~0)
3181 return clib_error_return (0, "interface / subif must be specified");
3183 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3190 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3191 return clib_error_return (0, "No such interface");
3193 case VNET_API_ERROR_NO_SUCH_ENTRY:
3194 return clib_error_return (0, "No such classifier table");
3200 * Assign a classification table to an interface. The classification
3201 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3202 * commands. Once the table is create, use this command to filter packets
3206 * Example of how to assign a classification table to an interface:
3207 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3210 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3212 .path = "set ip6 classify",
3214 "set ip6 classify intfc <interface> table-index <classify-idx>",
3215 .function = set_ip6_classify_command_fn,
3219 static clib_error_t *
3220 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3222 ip6_main_t *im = &ip6_main;
3227 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3229 if (unformat (input, "hash-buckets %d", &tmp))
3231 else if (unformat (input, "heap-size %U",
3232 unformat_memory_size, &heapsize))
3235 return clib_error_return (0, "unknown input '%U'",
3236 format_unformat_error, input);
3239 im->lookup_table_nbuckets = nbuckets;
3240 im->lookup_table_size = heapsize;
3245 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3248 * fd.io coding-style-patch-verification: ON
3251 * eval: (c-set-style "gnu")