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 u8 *data_this_buffer;
1023 ASSERT (bogus_lengthp);
1026 /* Initialize checksum with ip header. */
1027 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1028 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1029 data_this_buffer = (u8 *) (ip0 + 1);
1031 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1033 sum0 = ip_csum_with_carry (sum0,
1034 clib_mem_unaligned (&ip0->
1035 src_address.as_uword[i],
1038 ip_csum_with_carry (sum0,
1039 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1043 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1044 * or UDP-Ping packets */
1045 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1048 ip6_hop_by_hop_ext_t *ext_hdr =
1049 (ip6_hop_by_hop_ext_t *) data_this_buffer;
1051 /* validate really icmp6 next */
1052 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1053 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
1055 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1056 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1058 payload_length_host_byte_order -= skip_bytes;
1059 headers_size += skip_bytes;
1062 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1066 u32 n_ip_bytes_this_buffer =
1067 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
1068 if (n_this_buffer + headers_size > n_ip_bytes_this_buffer)
1070 n_this_buffer = p0->current_length > headers_size ?
1071 n_ip_bytes_this_buffer - headers_size : 0;
1077 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1078 n_bytes_left -= n_this_buffer;
1079 if (n_bytes_left == 0)
1082 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
1083 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1089 length_odd = (n_this_buffer & 1);
1091 p0 = vlib_get_buffer (vm, p0->next_buffer);
1092 data_this_buffer = vlib_buffer_get_current (p0);
1093 n_this_buffer = clib_min (p0->current_length, n_bytes_left);
1095 if (PREDICT_FALSE (length_odd))
1097 /* Prepend a 0 or the resulting checksum will be incorrect. */
1101 data_this_buffer[0] = 0;
1105 sum16 = ~ip_csum_fold (sum0);
1111 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1113 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1118 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1119 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1120 || ip0->protocol == IP_PROTOCOL_ICMP6
1121 || ip0->protocol == IP_PROTOCOL_UDP
1122 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1124 udp0 = (void *) (ip0 + 1);
1125 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1127 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1128 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
1132 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1134 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1135 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
1142 * @brief returns number of links on which src is reachable.
1145 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1147 const load_balance_t *lb0;
1151 fib_index = vec_elt (im->fib_index_by_sw_if_index,
1152 vnet_buffer (b)->sw_if_index[VLIB_RX]);
1154 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
1155 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
1157 lbi = ip6_fib_table_fwding_lookup (fib_index, &i->src_address);
1158 lb0 = load_balance_get (lbi);
1160 return (fib_urpf_check_size (lb0->lb_urpf));
1164 ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1168 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1169 if (proto0 != IP_PROTOCOL_UDP)
1171 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1172 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1178 VNET_FEATURE_ARC_INIT (ip6_local) =
1180 .arc_name = "ip6-local",
1181 .start_nodes = VNET_FEATURES ("ip6-local"),
1186 ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1187 vlib_frame_t * frame, int head_of_feature_arc)
1189 ip6_main_t *im = &ip6_main;
1190 ip_lookup_main_t *lm = &im->lookup_main;
1191 u32 *from, n_left_from;
1192 vlib_node_runtime_t *error_node =
1193 vlib_node_get_runtime (vm, ip6_input_node.index);
1194 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
1195 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1196 u16 nexts[VLIB_FRAME_SIZE], *next;
1198 from = vlib_frame_vector_args (frame);
1199 n_left_from = frame->n_vectors;
1201 if (node->flags & VLIB_NODE_FLAG_TRACE)
1202 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1204 vlib_get_buffers (vm, from, bufs, n_left_from);
1208 while (n_left_from > 2)
1210 /* Prefetch next iteration. */
1211 if (n_left_from >= 6)
1213 vlib_prefetch_buffer_header (b[4], STORE);
1214 vlib_prefetch_buffer_header (b[5], STORE);
1215 vlib_prefetch_buffer_data (b[2], LOAD);
1216 vlib_prefetch_buffer_data (b[3], LOAD);
1220 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1221 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1223 ip6_header_t *ip[2];
1224 ip[0] = vlib_buffer_get_current (b[0]);
1225 ip[1] = vlib_buffer_get_current (b[1]);
1227 if (head_of_feature_arc)
1229 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1230 vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data;
1233 type[0] = lm->builtin_protocol_by_ip_protocol[ip[0]->protocol];
1234 type[1] = lm->builtin_protocol_by_ip_protocol[ip[1]->protocol];
1237 flags[0] = b[0]->flags;
1238 flags[1] = b[1]->flags;
1240 u32 good_l4_csum[2];
1242 flags[0] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1243 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1244 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1246 flags[1] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1247 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1248 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1250 u32 udp_offset[2] = { };
1253 ip6_next_proto_is_tcp_udp (b[0], ip[0], &udp_offset[0]);
1255 ip6_next_proto_is_tcp_udp (b[1], ip[1], &udp_offset[1]);
1256 i16 len_diff[2] = { 0 };
1257 if (PREDICT_TRUE (is_tcp_udp[0]))
1260 (udp_header_t *) ((u8 *) ip[0] + udp_offset[0]);
1261 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UDP
1262 && udp->checksum == 0;
1263 /* optimistically verify UDP length. */
1264 u16 ip_len, udp_len;
1265 ip_len = clib_net_to_host_u16 (ip[0]->payload_length);
1266 udp_len = clib_net_to_host_u16 (udp->length);
1267 len_diff[0] = ip_len - udp_len;
1269 if (PREDICT_TRUE (is_tcp_udp[1]))
1272 (udp_header_t *) ((u8 *) ip[1] + udp_offset[1]);
1273 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UDP
1274 && udp->checksum == 0;
1275 /* optimistically verify UDP length. */
1276 u16 ip_len, udp_len;
1277 ip_len = clib_net_to_host_u16 (ip[1]->payload_length);
1278 udp_len = clib_net_to_host_u16 (udp->length);
1279 len_diff[1] = ip_len - udp_len;
1282 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1283 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1285 len_diff[0] = type[0] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[0] : 0;
1286 len_diff[1] = type[1] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[1] : 0;
1289 need_csum[0] = type[0] != IP_BUILTIN_PROTOCOL_UNKNOWN
1291 && !(flags[0] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1292 need_csum[1] = type[1] != IP_BUILTIN_PROTOCOL_UNKNOWN
1294 && !(flags[1] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1295 if (PREDICT_FALSE (need_csum[0]))
1297 flags[0] = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1298 good_l4_csum[0] = flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1300 if (PREDICT_FALSE (need_csum[1]))
1302 flags[1] = ip6_tcp_udp_icmp_validate_checksum (vm, b[1]);
1303 good_l4_csum[1] = flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1306 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1307 error[0] = len_diff[0] < 0 ? IP6_ERROR_UDP_LENGTH : error[0];
1308 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1309 error[1] = len_diff[1] < 0 ? IP6_ERROR_UDP_LENGTH : error[1];
1311 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1312 IP6_ERROR_UDP_CHECKSUM,
1313 "Wrong IP6 errors constants");
1314 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1315 IP6_ERROR_ICMP_CHECKSUM,
1316 "Wrong IP6 errors constants");
1319 !good_l4_csum[0] ? IP6_ERROR_UDP_CHECKSUM + type[0] : error[0];
1321 !good_l4_csum[1] ? IP6_ERROR_UDP_CHECKSUM + type[1] : error[1];
1323 /* Drop packets from unroutable hosts. */
1324 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1326 unroutable[0] = error[0] == IP6_ERROR_UNKNOWN_PROTOCOL
1327 && type[0] != IP_BUILTIN_PROTOCOL_ICMP
1328 && !ip6_address_is_link_local_unicast (&ip[0]->src_address);
1329 unroutable[1] = error[1] == IP6_ERROR_UNKNOWN_PROTOCOL
1330 && type[1] != IP_BUILTIN_PROTOCOL_ICMP
1331 && !ip6_address_is_link_local_unicast (&ip[1]->src_address);
1332 if (PREDICT_FALSE (unroutable[0]))
1335 !ip6_urpf_loose_check (im, b[0],
1336 ip[0]) ? IP6_ERROR_SRC_LOOKUP_MISS
1339 if (PREDICT_FALSE (unroutable[1]))
1342 !ip6_urpf_loose_check (im, b[1],
1343 ip[1]) ? IP6_ERROR_SRC_LOOKUP_MISS
1347 vnet_buffer (b[0])->ip.fib_index =
1348 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1349 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1350 vnet_buffer (b[0])->ip.fib_index;
1351 vnet_buffer (b[1])->ip.fib_index =
1352 vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ?
1353 vnet_buffer (b[1])->sw_if_index[VLIB_TX] :
1354 vnet_buffer (b[1])->ip.fib_index;
1355 } /* head_of_feature_arc */
1357 next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol];
1359 error[0] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1360 next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol];
1362 error[1] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[1];
1364 b[0]->error = error_node->errors[0];
1365 b[1]->error = error_node->errors[1];
1367 if (head_of_feature_arc)
1370 ip6_unknown[0] = error[0] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1371 ip6_unknown[1] = error[1] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1372 if (PREDICT_TRUE (ip6_unknown[0]))
1374 u32 next32 = next[0];
1375 vnet_feature_arc_start (arc_index,
1376 vnet_buffer (b[0])->sw_if_index
1377 [VLIB_RX], &next32, b[0]);
1380 if (PREDICT_TRUE (ip6_unknown[1]))
1382 u32 next32 = next[1];
1383 vnet_feature_arc_start (arc_index,
1384 vnet_buffer (b[1])->sw_if_index
1385 [VLIB_RX], &next32, b[1]);
1399 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1402 ip = vlib_buffer_get_current (b[0]);
1404 if (head_of_feature_arc)
1406 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1407 u8 type = lm->builtin_protocol_by_ip_protocol[ip->protocol];
1409 u32 flags = b[0]->flags;
1411 flags & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1412 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1413 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1417 u8 is_tcp_udp = ip6_next_proto_is_tcp_udp (b[0], ip, &udp_offset);
1418 if (PREDICT_TRUE (is_tcp_udp))
1420 udp_header_t *udp = (udp_header_t *) ((u8 *) ip + udp_offset);
1421 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1422 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UDP
1423 && udp->checksum == 0;
1424 /* optimistically verify UDP length. */
1425 u16 ip_len, udp_len;
1426 ip_len = clib_net_to_host_u16 (ip->payload_length);
1427 udp_len = clib_net_to_host_u16 (udp->length);
1428 len_diff = ip_len - udp_len;
1431 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UNKNOWN;
1432 len_diff = type == IP_BUILTIN_PROTOCOL_UDP ? len_diff : 0;
1434 u8 need_csum = type != IP_BUILTIN_PROTOCOL_UNKNOWN && !good_l4_csum
1435 && !(flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1436 if (PREDICT_FALSE (need_csum))
1438 flags = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1439 good_l4_csum = flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1442 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1443 error = len_diff < 0 ? IP6_ERROR_UDP_LENGTH : error;
1445 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1446 IP6_ERROR_UDP_CHECKSUM,
1447 "Wrong IP6 errors constants");
1448 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1449 IP6_ERROR_ICMP_CHECKSUM,
1450 "Wrong IP6 errors constants");
1452 error = !good_l4_csum ? IP6_ERROR_UDP_CHECKSUM + type : error;
1454 /* Drop packets from unroutable hosts. */
1455 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1456 u8 unroutable = error == IP6_ERROR_UNKNOWN_PROTOCOL
1457 && type != IP_BUILTIN_PROTOCOL_ICMP
1458 && !ip6_address_is_link_local_unicast (&ip->src_address);
1459 if (PREDICT_FALSE (unroutable))
1462 !ip6_urpf_loose_check (im, b[0],
1463 ip) ? IP6_ERROR_SRC_LOOKUP_MISS :
1467 vnet_buffer (b[0])->ip.fib_index =
1468 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1469 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1470 vnet_buffer (b[0])->ip.fib_index;
1471 } /* head_of_feature_arc */
1473 next[0] = lm->local_next_by_ip_protocol[ip->protocol];
1475 error != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1477 b[0]->error = error_node->errors[0];
1479 if (head_of_feature_arc)
1481 if (PREDICT_TRUE (error == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1483 u32 next32 = next[0];
1484 vnet_feature_arc_start (arc_index,
1485 vnet_buffer (b[0])->sw_if_index
1486 [VLIB_RX], &next32, b[0]);
1497 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1498 return frame->n_vectors;
1501 VLIB_NODE_FN (ip6_local_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1502 vlib_frame_t * frame)
1504 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1508 VLIB_REGISTER_NODE (ip6_local_node) =
1510 .name = "ip6-local",
1511 .vector_size = sizeof (u32),
1512 .format_trace = format_ip6_forward_next_trace,
1513 .n_next_nodes = IP_LOCAL_N_NEXT,
1516 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1517 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
1518 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1519 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1520 [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-full-reassembly",
1525 VLIB_NODE_FN (ip6_local_end_of_arc_node) (vlib_main_t * vm,
1526 vlib_node_runtime_t * node,
1527 vlib_frame_t * frame)
1529 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1533 VLIB_REGISTER_NODE (ip6_local_end_of_arc_node) = {
1534 .name = "ip6-local-end-of-arc",
1535 .vector_size = sizeof (u32),
1537 .format_trace = format_ip6_forward_next_trace,
1538 .sibling_of = "ip6-local",
1541 VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1542 .arc_name = "ip6-local",
1543 .node_name = "ip6-local-end-of-arc",
1544 .runs_before = 0, /* not before any other features */
1548 #ifdef CLIB_MARCH_VARIANT
1549 extern vlib_node_registration_t ip6_local_node;
1554 ip6_register_protocol (u32 protocol, u32 node_index)
1556 vlib_main_t *vm = vlib_get_main ();
1557 ip6_main_t *im = &ip6_main;
1558 ip_lookup_main_t *lm = &im->lookup_main;
1560 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1561 lm->local_next_by_ip_protocol[protocol] =
1562 vlib_node_add_next (vm, ip6_local_node.index, node_index);
1566 ip6_unregister_protocol (u32 protocol)
1568 ip6_main_t *im = &ip6_main;
1569 ip_lookup_main_t *lm = &im->lookup_main;
1571 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1572 lm->local_next_by_ip_protocol[protocol] = IP_LOCAL_NEXT_PUNT;
1576 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index,
1579 vnet_main_t *vnm = vnet_get_main ();
1580 ip6_main_t *im = &ip6_main;
1581 icmp6_neighbor_solicitation_header_t *h;
1583 ip_interface_address_t *ia;
1584 ip_adjacency_t *adj;
1585 vnet_hw_interface_t *hi;
1586 vnet_sw_interface_t *si;
1592 si = vnet_get_sw_interface (vnm, sw_if_index);
1594 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1596 return clib_error_return (0, "%U: interface %U down",
1597 format_ip6_address, dst,
1598 format_vnet_sw_if_index_name, vnm,
1603 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1606 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1607 return clib_error_return
1608 (0, "no matching interface address for destination %U (interface %U)",
1609 format_ip6_address, dst,
1610 format_vnet_sw_if_index_name, vnm, sw_if_index);
1614 vlib_packet_template_get_packet (vm,
1615 &im->discover_neighbor_packet_template,
1618 return clib_error_return (0, "ICMP6 NS packet allocation failed");
1620 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1622 /* Destination address is a solicited node multicast address. We need to fill in
1623 the low 24 bits with low 24 bits of target's address. */
1624 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1625 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1626 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1628 h->ip.src_address = src[0];
1629 h->neighbor.target_address = dst[0];
1631 if (PREDICT_FALSE (!hi->hw_address))
1633 return clib_error_return (0, "%U: interface %U do not support ip probe",
1634 format_ip6_address, dst,
1635 format_vnet_sw_if_index_name, vnm,
1639 clib_memcpy_fast (h->link_layer_option.ethernet_address, hi->hw_address,
1640 vec_len (hi->hw_address));
1642 h->neighbor.icmp.checksum =
1643 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1644 ASSERT (bogus_length == 0);
1646 b = vlib_get_buffer (vm, bi);
1647 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1648 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1650 /* Add encapsulation string for software interface (e.g. ethernet header). */
1651 ip46_address_t nh = {
1655 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1656 VNET_LINK_IP6, &nh, sw_if_index);
1659 /* Peer has been previously resolved, retrieve glean adj instead */
1660 if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE && refresh == 0)
1663 ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1664 VNET_LINK_IP6, sw_if_index, &nh);
1668 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1669 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1672 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1673 u32 *to_next = vlib_frame_vector_args (f);
1676 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1680 return /* no error */ 0;
1686 IP6_REWRITE_NEXT_DROP,
1687 IP6_REWRITE_NEXT_ICMP_ERROR,
1688 IP6_REWRITE_NEXT_FRAGMENT,
1689 IP6_REWRITE_N_NEXT /* Last */
1690 } ip6_rewrite_next_t;
1693 * This bits of an IPv6 address to mask to construct a multicast
1696 #define IP6_MCAST_ADDR_MASK 0xffffffff
1699 ip6_mtu_check (vlib_buffer_t * b, u16 packet_bytes,
1700 u16 adj_packet_bytes, bool is_locally_generated,
1701 u32 * next, u32 * error)
1703 if (adj_packet_bytes >= 1280 && packet_bytes > adj_packet_bytes)
1705 if (is_locally_generated)
1707 /* IP fragmentation */
1708 ip_frag_set_vnet_buffer (b, adj_packet_bytes,
1709 IP6_FRAG_NEXT_IP6_REWRITE, 0);
1710 *next = IP6_REWRITE_NEXT_FRAGMENT;
1711 *error = IP6_ERROR_MTU_EXCEEDED;
1715 *error = IP6_ERROR_MTU_EXCEEDED;
1716 icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1718 *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1724 ip6_rewrite_inline_with_gso (vlib_main_t * vm,
1725 vlib_node_runtime_t * node,
1726 vlib_frame_t * frame,
1727 int do_counters, int is_midchain, int is_mcast,
1730 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1731 u32 *from = vlib_frame_vector_args (frame);
1732 u32 n_left_from, n_left_to_next, *to_next, next_index;
1733 vlib_node_runtime_t *error_node =
1734 vlib_node_get_runtime (vm, ip6_input_node.index);
1736 n_left_from = frame->n_vectors;
1737 next_index = node->cached_next_index;
1738 u32 thread_index = vm->thread_index;
1740 while (n_left_from > 0)
1742 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1744 while (n_left_from >= 4 && n_left_to_next >= 2)
1746 ip_adjacency_t *adj0, *adj1;
1747 vlib_buffer_t *p0, *p1;
1748 ip6_header_t *ip0, *ip1;
1749 u32 pi0, rw_len0, next0, error0, adj_index0;
1750 u32 pi1, rw_len1, next1, error1, adj_index1;
1751 u32 tx_sw_if_index0, tx_sw_if_index1;
1752 bool is_locally_originated0, is_locally_originated1;
1754 /* Prefetch next iteration. */
1756 vlib_buffer_t *p2, *p3;
1758 p2 = vlib_get_buffer (vm, from[2]);
1759 p3 = vlib_get_buffer (vm, from[3]);
1761 vlib_prefetch_buffer_header (p2, LOAD);
1762 vlib_prefetch_buffer_header (p3, LOAD);
1764 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1765 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1767 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1768 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1771 pi0 = to_next[0] = from[0];
1772 pi1 = to_next[1] = from[1];
1777 n_left_to_next -= 2;
1779 p0 = vlib_get_buffer (vm, pi0);
1780 p1 = vlib_get_buffer (vm, pi1);
1782 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1783 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1785 ip0 = vlib_buffer_get_current (p0);
1786 ip1 = vlib_buffer_get_current (p1);
1788 error0 = error1 = IP6_ERROR_NONE;
1789 next0 = next1 = IP6_REWRITE_NEXT_DROP;
1791 is_locally_originated0 =
1792 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1793 if (PREDICT_TRUE (!is_locally_originated0))
1795 i32 hop_limit0 = ip0->hop_limit;
1797 /* Input node should have reject packets with hop limit 0. */
1798 ASSERT (ip0->hop_limit > 0);
1802 ip0->hop_limit = hop_limit0;
1805 * If the hop count drops below 1 when forwarding, generate
1808 if (PREDICT_FALSE (hop_limit0 <= 0))
1810 error0 = IP6_ERROR_TIME_EXPIRED;
1811 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1812 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1813 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1814 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1820 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1822 is_locally_originated1 =
1823 p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1824 if (PREDICT_TRUE (!is_locally_originated1))
1826 i32 hop_limit1 = ip1->hop_limit;
1828 /* Input node should have reject packets with hop limit 0. */
1829 ASSERT (ip1->hop_limit > 0);
1833 ip1->hop_limit = hop_limit1;
1836 * If the hop count drops below 1 when forwarding, generate
1839 if (PREDICT_FALSE (hop_limit1 <= 0))
1841 error1 = IP6_ERROR_TIME_EXPIRED;
1842 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1843 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1844 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1845 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1851 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1853 adj0 = adj_get (adj_index0);
1854 adj1 = adj_get (adj_index1);
1856 rw_len0 = adj0[0].rewrite_header.data_bytes;
1857 rw_len1 = adj1[0].rewrite_header.data_bytes;
1858 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1859 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
1863 vlib_increment_combined_counter
1864 (&adjacency_counters,
1865 thread_index, adj_index0, 1,
1866 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1867 vlib_increment_combined_counter
1868 (&adjacency_counters,
1869 thread_index, adj_index1, 1,
1870 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1873 /* Check MTU of outgoing interface. */
1875 clib_net_to_host_u16 (ip0->payload_length) +
1876 sizeof (ip6_header_t);
1878 clib_net_to_host_u16 (ip1->payload_length) +
1879 sizeof (ip6_header_t);
1880 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
1881 ip0_len = gso_mtu_sz (p0);
1882 if (do_gso && (p1->flags & VNET_BUFFER_F_GSO))
1883 ip1_len = gso_mtu_sz (p1);
1887 ip6_mtu_check (p0, ip0_len,
1888 adj0[0].rewrite_header.max_l3_packet_bytes,
1889 is_locally_originated0, &next0, &error0);
1890 ip6_mtu_check (p1, ip1_len,
1891 adj1[0].rewrite_header.max_l3_packet_bytes,
1892 is_locally_originated1, &next1, &error1);
1894 /* Don't adjust the buffer for hop count issue; icmp-error node
1895 * wants to see the IP header */
1896 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1898 p0->current_data -= rw_len0;
1899 p0->current_length += rw_len0;
1901 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1902 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1903 next0 = adj0[0].rewrite_header.next_index;
1906 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1907 vnet_feature_arc_start (lm->output_feature_arc_index,
1908 tx_sw_if_index0, &next0, p0);
1912 p0->error = error_node->errors[error0];
1914 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1916 p1->current_data -= rw_len1;
1917 p1->current_length += rw_len1;
1919 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1920 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1921 next1 = adj1[0].rewrite_header.next_index;
1924 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1925 vnet_feature_arc_start (lm->output_feature_arc_index,
1926 tx_sw_if_index1, &next1, p1);
1930 p1->error = error_node->errors[error1];
1935 /* before we paint on the next header, update the L4
1936 * checksums if required, since there's no offload on a tunnel */
1937 calc_checksums (vm, p0);
1938 calc_checksums (vm, p1);
1941 /* Guess we are only writing on simple Ethernet header. */
1942 vnet_rewrite_two_headers (adj0[0], adj1[0],
1943 ip0, ip1, sizeof (ethernet_header_t));
1947 if (adj0->sub_type.midchain.fixup_func)
1948 adj0->sub_type.midchain.fixup_func
1949 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1950 if (adj1->sub_type.midchain.fixup_func)
1951 adj1->sub_type.midchain.fixup_func
1952 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
1957 * copy bytes from the IP address into the MAC rewrite
1959 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1961 rewrite_header.dst_mcast_offset,
1962 &ip0->dst_address.as_u32[3],
1964 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1966 rewrite_header.dst_mcast_offset,
1967 &ip1->dst_address.as_u32[3],
1971 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1972 to_next, n_left_to_next,
1973 pi0, pi1, next0, next1);
1976 while (n_left_from > 0 && n_left_to_next > 0)
1978 ip_adjacency_t *adj0;
1982 u32 adj_index0, next0, error0;
1983 u32 tx_sw_if_index0;
1984 bool is_locally_originated0;
1986 pi0 = to_next[0] = from[0];
1988 p0 = vlib_get_buffer (vm, pi0);
1990 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1992 adj0 = adj_get (adj_index0);
1994 ip0 = vlib_buffer_get_current (p0);
1996 error0 = IP6_ERROR_NONE;
1997 next0 = IP6_REWRITE_NEXT_DROP;
1999 /* Check hop limit */
2000 is_locally_originated0 =
2001 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
2002 if (PREDICT_TRUE (!is_locally_originated0))
2004 i32 hop_limit0 = ip0->hop_limit;
2006 ASSERT (ip0->hop_limit > 0);
2010 ip0->hop_limit = hop_limit0;
2012 if (PREDICT_FALSE (hop_limit0 <= 0))
2015 * If the hop count drops below 1 when forwarding, generate
2018 error0 = IP6_ERROR_TIME_EXPIRED;
2019 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2020 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2021 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2022 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2028 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2033 calc_checksums (vm, p0);
2036 /* Guess we are only writing on simple Ethernet header. */
2037 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2039 /* Update packet buffer attributes/set output interface. */
2040 rw_len0 = adj0[0].rewrite_header.data_bytes;
2041 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2045 vlib_increment_combined_counter
2046 (&adjacency_counters,
2047 thread_index, adj_index0, 1,
2048 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2051 /* Check MTU of outgoing interface. */
2053 clib_net_to_host_u16 (ip0->payload_length) +
2054 sizeof (ip6_header_t);
2055 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
2056 ip0_len = gso_mtu_sz (p0);
2058 ip6_mtu_check (p0, ip0_len,
2059 adj0[0].rewrite_header.max_l3_packet_bytes,
2060 is_locally_originated0, &next0, &error0);
2062 /* Don't adjust the buffer for hop count issue; icmp-error node
2063 * wants to see the IP header */
2064 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2066 p0->current_data -= rw_len0;
2067 p0->current_length += rw_len0;
2069 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2071 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2072 next0 = adj0[0].rewrite_header.next_index;
2075 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2076 vnet_feature_arc_start (lm->output_feature_arc_index,
2077 tx_sw_if_index0, &next0, p0);
2081 p0->error = error_node->errors[error0];
2086 if (adj0->sub_type.midchain.fixup_func)
2087 adj0->sub_type.midchain.fixup_func
2088 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2092 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2094 rewrite_header.dst_mcast_offset,
2095 &ip0->dst_address.as_u32[3],
2102 n_left_to_next -= 1;
2104 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2105 to_next, n_left_to_next,
2109 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2112 /* Need to do trace after rewrites to pick up new packet data. */
2113 if (node->flags & VLIB_NODE_FLAG_TRACE)
2114 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2116 return frame->n_vectors;
2120 ip6_rewrite_inline (vlib_main_t * vm,
2121 vlib_node_runtime_t * node,
2122 vlib_frame_t * frame,
2123 int do_counters, int is_midchain, int is_mcast)
2125 vnet_main_t *vnm = vnet_get_main ();
2126 if (PREDICT_FALSE (vnm->interface_main.gso_interface_count > 0))
2127 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2128 is_midchain, is_mcast,
2131 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2132 is_midchain, is_mcast,
2133 0 /* no do_gso */ );
2136 VLIB_NODE_FN (ip6_rewrite_node) (vlib_main_t * vm,
2137 vlib_node_runtime_t * node,
2138 vlib_frame_t * frame)
2140 if (adj_are_counters_enabled ())
2141 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2143 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2146 VLIB_NODE_FN (ip6_rewrite_bcast_node) (vlib_main_t * vm,
2147 vlib_node_runtime_t * node,
2148 vlib_frame_t * frame)
2150 if (adj_are_counters_enabled ())
2151 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2153 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2156 VLIB_NODE_FN (ip6_rewrite_mcast_node) (vlib_main_t * vm,
2157 vlib_node_runtime_t * node,
2158 vlib_frame_t * frame)
2160 if (adj_are_counters_enabled ())
2161 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2163 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2166 VLIB_NODE_FN (ip6_midchain_node) (vlib_main_t * vm,
2167 vlib_node_runtime_t * node,
2168 vlib_frame_t * frame)
2170 if (adj_are_counters_enabled ())
2171 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2173 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2176 VLIB_NODE_FN (ip6_mcast_midchain_node) (vlib_main_t * vm,
2177 vlib_node_runtime_t * node,
2178 vlib_frame_t * frame)
2180 if (adj_are_counters_enabled ())
2181 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2183 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2187 VLIB_REGISTER_NODE (ip6_midchain_node) =
2189 .name = "ip6-midchain",
2190 .vector_size = sizeof (u32),
2191 .format_trace = format_ip6_forward_next_trace,
2192 .sibling_of = "ip6-rewrite",
2195 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2197 .name = "ip6-rewrite",
2198 .vector_size = sizeof (u32),
2199 .format_trace = format_ip6_rewrite_trace,
2200 .n_next_nodes = IP6_REWRITE_N_NEXT,
2203 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
2204 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2205 [IP6_REWRITE_NEXT_FRAGMENT] = "ip6-frag",
2209 VLIB_REGISTER_NODE (ip6_rewrite_bcast_node) = {
2210 .name = "ip6-rewrite-bcast",
2211 .vector_size = sizeof (u32),
2213 .format_trace = format_ip6_rewrite_trace,
2214 .sibling_of = "ip6-rewrite",
2217 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2219 .name = "ip6-rewrite-mcast",
2220 .vector_size = sizeof (u32),
2221 .format_trace = format_ip6_rewrite_trace,
2222 .sibling_of = "ip6-rewrite",
2226 VLIB_REGISTER_NODE (ip6_mcast_midchain_node) =
2228 .name = "ip6-mcast-midchain",
2229 .vector_size = sizeof (u32),
2230 .format_trace = format_ip6_rewrite_trace,
2231 .sibling_of = "ip6-rewrite",
2237 * Hop-by-Hop handling
2239 #ifndef CLIB_MARCH_VARIANT
2240 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2241 #endif /* CLIB_MARCH_VARIANT */
2243 #define foreach_ip6_hop_by_hop_error \
2244 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2245 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2246 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2251 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2252 foreach_ip6_hop_by_hop_error
2254 IP6_HOP_BY_HOP_N_ERROR,
2255 } ip6_hop_by_hop_error_t;
2259 * Primary h-b-h handler trace support
2260 * We work pretty hard on the problem for obvious reasons
2266 u8 option_data[256];
2267 } ip6_hop_by_hop_trace_t;
2269 extern vlib_node_registration_t ip6_hop_by_hop_node;
2271 static char *ip6_hop_by_hop_error_strings[] = {
2272 #define _(sym,string) string,
2273 foreach_ip6_hop_by_hop_error
2277 #ifndef CLIB_MARCH_VARIANT
2279 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2281 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2282 int total_len = va_arg (*args, int);
2283 ip6_hop_by_hop_option_t *opt0, *limit0;
2284 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2287 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2288 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2290 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2291 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2293 while (opt0 < limit0)
2298 case 0: /* Pad, just stop */
2299 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2303 if (hm->trace[type0])
2305 s = (*hm->trace[type0]) (s, opt0);
2310 format (s, "\n unrecognized option %d length %d", type0,
2314 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2315 sizeof (ip6_hop_by_hop_option_t));
2324 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2326 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2327 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2328 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2329 ip6_hop_by_hop_header_t *hbh0;
2330 ip6_hop_by_hop_option_t *opt0, *limit0;
2331 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2335 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2337 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2338 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2340 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2341 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2343 while (opt0 < limit0)
2348 case 0: /* Pad, just stop */
2349 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2353 if (hm->trace[type0])
2355 s = (*hm->trace[type0]) (s, opt0);
2360 format (s, "\n unrecognized option %d length %d", type0,
2364 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2365 sizeof (ip6_hop_by_hop_option_t));
2373 ip6_scan_hbh_options (vlib_buffer_t * b0,
2375 ip6_hop_by_hop_header_t * hbh0,
2376 ip6_hop_by_hop_option_t * opt0,
2377 ip6_hop_by_hop_option_t * limit0, u32 * next0)
2379 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2383 while (opt0 < limit0)
2389 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2394 if (hm->options[type0])
2396 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2398 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2404 /* Unrecognized mandatory option, check the two high order bits */
2405 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2407 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2409 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2410 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2411 *next0 = IP_LOOKUP_NEXT_DROP;
2413 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2414 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2415 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2416 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2417 ICMP6_parameter_problem_unrecognized_option,
2418 (u8 *) opt0 - (u8 *) ip0);
2420 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2421 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2422 if (!ip6_address_is_multicast (&ip0->dst_address))
2424 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2425 icmp6_error_set_vnet_buffer (b0,
2426 ICMP6_parameter_problem,
2427 ICMP6_parameter_problem_unrecognized_option,
2428 (u8 *) opt0 - (u8 *) ip0);
2432 *next0 = IP_LOOKUP_NEXT_DROP;
2440 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2441 sizeof (ip6_hop_by_hop_option_t));
2447 * Process the Hop-by-Hop Options header
2449 VLIB_NODE_FN (ip6_hop_by_hop_node) (vlib_main_t * vm,
2450 vlib_node_runtime_t * node,
2451 vlib_frame_t * frame)
2453 vlib_node_runtime_t *error_node =
2454 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2455 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2456 u32 n_left_from, *from, *to_next;
2457 ip_lookup_next_t next_index;
2459 from = vlib_frame_vector_args (frame);
2460 n_left_from = frame->n_vectors;
2461 next_index = node->cached_next_index;
2463 while (n_left_from > 0)
2467 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2469 while (n_left_from >= 4 && n_left_to_next >= 2)
2472 vlib_buffer_t *b0, *b1;
2474 ip6_header_t *ip0, *ip1;
2475 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2476 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2477 u8 error0 = 0, error1 = 0;
2479 /* Prefetch next iteration. */
2481 vlib_buffer_t *p2, *p3;
2483 p2 = vlib_get_buffer (vm, from[2]);
2484 p3 = vlib_get_buffer (vm, from[3]);
2486 vlib_prefetch_buffer_header (p2, LOAD);
2487 vlib_prefetch_buffer_header (p3, LOAD);
2489 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2490 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2493 /* Speculatively enqueue b0, b1 to the current next frame */
2494 to_next[0] = bi0 = from[0];
2495 to_next[1] = bi1 = from[1];
2499 n_left_to_next -= 2;
2501 b0 = vlib_get_buffer (vm, bi0);
2502 b1 = vlib_get_buffer (vm, bi1);
2504 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2505 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2506 ip_adjacency_t *adj0 = adj_get (adj_index0);
2507 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2508 ip_adjacency_t *adj1 = adj_get (adj_index1);
2510 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2511 next0 = adj0->lookup_next_index;
2512 next1 = adj1->lookup_next_index;
2514 ip0 = vlib_buffer_get_current (b0);
2515 ip1 = vlib_buffer_get_current (b1);
2516 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2517 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2518 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2519 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2521 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2522 ((hbh0->length + 1) << 3));
2524 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2525 ((hbh1->length + 1) << 3));
2528 * Basic validity checks
2530 if ((hbh0->length + 1) << 3 >
2531 clib_net_to_host_u16 (ip0->payload_length))
2533 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2534 next0 = IP_LOOKUP_NEXT_DROP;
2537 /* Scan the set of h-b-h options, process ones that we understand */
2538 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2540 if ((hbh1->length + 1) << 3 >
2541 clib_net_to_host_u16 (ip1->payload_length))
2543 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2544 next1 = IP_LOOKUP_NEXT_DROP;
2547 /* Scan the set of h-b-h options, process ones that we understand */
2548 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2551 /* Has the classifier flagged this buffer for special treatment? */
2554 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2555 next0 = hm->next_override;
2557 /* Has the classifier flagged this buffer for special treatment? */
2560 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2561 next1 = hm->next_override;
2563 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2565 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2567 ip6_hop_by_hop_trace_t *t =
2568 vlib_add_trace (vm, node, b0, sizeof (*t));
2569 u32 trace_len = (hbh0->length + 1) << 3;
2570 t->next_index = next0;
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, hbh0, trace_len);
2579 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2581 ip6_hop_by_hop_trace_t *t =
2582 vlib_add_trace (vm, node, b1, sizeof (*t));
2583 u32 trace_len = (hbh1->length + 1) << 3;
2584 t->next_index = next1;
2585 /* Capture the h-b-h option verbatim */
2588 ARRAY_LEN (t->option_data) ? trace_len :
2589 ARRAY_LEN (t->option_data);
2590 t->trace_len = trace_len;
2591 clib_memcpy_fast (t->option_data, hbh1, trace_len);
2596 b0->error = error_node->errors[error0];
2597 b1->error = error_node->errors[error1];
2599 /* verify speculative enqueue, maybe switch current next frame */
2600 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2601 n_left_to_next, bi0, bi1, next0,
2605 while (n_left_from > 0 && n_left_to_next > 0)
2611 ip6_hop_by_hop_header_t *hbh0;
2612 ip6_hop_by_hop_option_t *opt0, *limit0;
2615 /* Speculatively enqueue b0 to the current next frame */
2621 n_left_to_next -= 1;
2623 b0 = vlib_get_buffer (vm, bi0);
2625 * Default use the next_index from the adjacency.
2626 * A HBH option rarely redirects to a different node
2628 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2629 ip_adjacency_t *adj0 = adj_get (adj_index0);
2630 next0 = adj0->lookup_next_index;
2632 ip0 = vlib_buffer_get_current (b0);
2633 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2634 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2636 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2637 ((hbh0->length + 1) << 3));
2640 * Basic validity checks
2642 if ((hbh0->length + 1) << 3 >
2643 clib_net_to_host_u16 (ip0->payload_length))
2645 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2646 next0 = IP_LOOKUP_NEXT_DROP;
2650 /* Scan the set of h-b-h options, process ones that we understand */
2651 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2654 /* Has the classifier flagged this buffer for special treatment? */
2657 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2658 next0 = hm->next_override;
2660 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2662 ip6_hop_by_hop_trace_t *t =
2663 vlib_add_trace (vm, node, b0, sizeof (*t));
2664 u32 trace_len = (hbh0->length + 1) << 3;
2665 t->next_index = next0;
2666 /* Capture the h-b-h option verbatim */
2669 ARRAY_LEN (t->option_data) ? trace_len :
2670 ARRAY_LEN (t->option_data);
2671 t->trace_len = trace_len;
2672 clib_memcpy_fast (t->option_data, hbh0, trace_len);
2675 b0->error = error_node->errors[error0];
2677 /* verify speculative enqueue, maybe switch current next frame */
2678 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2679 n_left_to_next, bi0, next0);
2681 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2683 return frame->n_vectors;
2687 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2689 .name = "ip6-hop-by-hop",
2690 .sibling_of = "ip6-lookup",
2691 .vector_size = sizeof (u32),
2692 .format_trace = format_ip6_hop_by_hop_trace,
2693 .type = VLIB_NODE_TYPE_INTERNAL,
2694 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2695 .error_strings = ip6_hop_by_hop_error_strings,
2700 static clib_error_t *
2701 ip6_hop_by_hop_init (vlib_main_t * vm)
2703 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2704 clib_memset (hm->options, 0, sizeof (hm->options));
2705 clib_memset (hm->trace, 0, sizeof (hm->trace));
2706 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2710 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2712 #ifndef CLIB_MARCH_VARIANT
2714 ip6_hbh_set_next_override (uword next)
2716 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2718 hm->next_override = next;
2722 ip6_hbh_register_option (u8 option,
2723 int options (vlib_buffer_t * b, ip6_header_t * ip,
2724 ip6_hop_by_hop_option_t * opt),
2725 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2727 ip6_main_t *im = &ip6_main;
2728 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2730 ASSERT ((u32) option < ARRAY_LEN (hm->options));
2732 /* Already registered */
2733 if (hm->options[option])
2736 hm->options[option] = options;
2737 hm->trace[option] = trace;
2739 /* Set global variable */
2740 im->hbh_enabled = 1;
2746 ip6_hbh_unregister_option (u8 option)
2748 ip6_main_t *im = &ip6_main;
2749 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2751 ASSERT ((u32) option < ARRAY_LEN (hm->options));
2753 /* Not registered */
2754 if (!hm->options[option])
2757 hm->options[option] = NULL;
2758 hm->trace[option] = NULL;
2760 /* Disable global knob if this was the last option configured */
2763 for (i = 0; i < 256; i++)
2765 if (hm->options[option])
2772 im->hbh_enabled = 0;
2777 /* Global IP6 main. */
2778 ip6_main_t ip6_main;
2781 static clib_error_t *
2782 ip6_lookup_init (vlib_main_t * vm)
2784 ip6_main_t *im = &ip6_main;
2785 clib_error_t *error;
2788 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2791 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2798 for (j = 0; j < i0; j++)
2799 im->fib_masks[i].as_u32[j] = ~0;
2802 im->fib_masks[i].as_u32[i0] =
2803 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2806 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2808 if (im->lookup_table_nbuckets == 0)
2809 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2811 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2813 if (im->lookup_table_size == 0)
2814 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2816 clib_bihash_init_24_8 (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2817 "ip6 FIB fwding table",
2818 im->lookup_table_nbuckets, im->lookup_table_size);
2819 clib_bihash_init_24_8 (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2820 "ip6 FIB non-fwding table",
2821 im->lookup_table_nbuckets, im->lookup_table_size);
2822 clib_bihash_init_40_8 (&im->ip6_mtable.ip6_mhash,
2824 im->lookup_table_nbuckets, im->lookup_table_size);
2826 /* Create FIB with index 0 and table id of 0. */
2827 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2828 FIB_SOURCE_DEFAULT_ROUTE);
2829 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2830 MFIB_SOURCE_DEFAULT_ROUTE);
2834 pn = pg_get_node (ip6_lookup_node.index);
2835 pn->unformat_edit = unformat_pg_ip6_header;
2838 /* Unless explicitly configured, don't process HBH options */
2839 im->hbh_enabled = 0;
2842 icmp6_neighbor_solicitation_header_t p;
2844 clib_memset (&p, 0, sizeof (p));
2846 p.ip.ip_version_traffic_class_and_flow_label =
2847 clib_host_to_net_u32 (0x6 << 28);
2848 p.ip.payload_length =
2849 clib_host_to_net_u16 (sizeof (p) -
2851 (icmp6_neighbor_solicitation_header_t, neighbor));
2852 p.ip.protocol = IP_PROTOCOL_ICMP6;
2853 p.ip.hop_limit = 255;
2854 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2856 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2858 p.link_layer_option.header.type =
2859 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2860 p.link_layer_option.header.n_data_u64s =
2861 sizeof (p.link_layer_option) / sizeof (u64);
2863 vlib_packet_template_init (vm,
2864 &im->discover_neighbor_packet_template,
2866 /* alloc chunk size */ 8,
2867 "ip6 neighbor discovery");
2873 VLIB_INIT_FUNCTION (ip6_lookup_init);
2875 static clib_error_t *
2876 test_ip6_link_command_fn (vlib_main_t * vm,
2877 unformat_input_t * input, vlib_cli_command_t * cmd)
2880 ip6_address_t _a, *a = &_a;
2882 if (unformat (input, "%U", unformat_ethernet_address, mac))
2884 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2885 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
2886 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2887 vlib_cli_output (vm, "Original MAC address: %U",
2888 format_ethernet_address, mac);
2895 * This command converts the given MAC Address into an IPv6 link-local
2899 * Example of how to create an IPv6 link-local address:
2900 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2901 * Link local address: fe80::14d9:e0ff:fe91:7986
2902 * Original MAC address: 16:d9:e0:91:79:86
2906 VLIB_CLI_COMMAND (test_link_command, static) =
2908 .path = "test ip6 link",
2909 .function = test_ip6_link_command_fn,
2910 .short_help = "test ip6 link <mac-address>",
2914 #ifndef CLIB_MARCH_VARIANT
2916 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2920 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
2922 if (~0 == fib_index)
2923 return VNET_API_ERROR_NO_SUCH_FIB;
2925 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2932 static clib_error_t *
2933 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2934 unformat_input_t * input,
2935 vlib_cli_command_t * cmd)
2939 u32 flow_hash_config = 0;
2942 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2944 if (unformat (input, "table %d", &table_id))
2947 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2948 foreach_flow_hash_bit
2955 return clib_error_return (0, "unknown input `%U'",
2956 format_unformat_error, input);
2958 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2965 return clib_error_return (0, "no such FIB table %d", table_id);
2968 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2976 * Configure the set of IPv6 fields used by the flow hash.
2980 * Example of how to set the flow hash on a given table:
2981 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2983 * Example of display the configured flow hash:
2984 * @cliexstart{show ip6 fib}
2985 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2988 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2989 * [0] [@0]: dpo-drop ip6
2992 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2993 * [0] [@2]: dpo-receive
2996 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2997 * [0] [@2]: dpo-receive
3000 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3001 * [0] [@2]: dpo-receive
3004 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3005 * [0] [@2]: dpo-receive
3006 * ff02::1:ff00:0/104
3008 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3009 * [0] [@2]: dpo-receive
3010 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3013 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3014 * [0] [@0]: dpo-drop ip6
3017 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3018 * [0] [@4]: ipv6-glean: af_packet0
3021 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3022 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3025 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3026 * [0] [@2]: dpo-receive
3027 * fe80::fe:3eff:fe3e:9222/128
3029 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3030 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3033 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3034 * [0] [@2]: dpo-receive
3037 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3038 * [0] [@2]: dpo-receive
3041 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3042 * [0] [@2]: dpo-receive
3043 * ff02::1:ff00:0/104
3045 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3046 * [0] [@2]: dpo-receive
3051 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3053 .path = "set ip6 flow-hash",
3055 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3056 .function = set_ip6_flow_hash_command_fn,
3060 static clib_error_t *
3061 show_ip6_local_command_fn (vlib_main_t * vm,
3062 unformat_input_t * input, vlib_cli_command_t * cmd)
3064 ip6_main_t *im = &ip6_main;
3065 ip_lookup_main_t *lm = &im->lookup_main;
3068 vlib_cli_output (vm, "Protocols handled by ip6_local");
3069 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3071 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3074 u32 node_index = vlib_get_node (vm,
3075 ip6_local_node.index)->
3076 next_nodes[lm->local_next_by_ip_protocol[i]];
3077 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3087 * Display the set of protocols handled by the local IPv6 stack.
3090 * Example of how to display local protocol table:
3091 * @cliexstart{show ip6 local}
3092 * Protocols handled by ip6_local
3100 VLIB_CLI_COMMAND (show_ip6_local, static) =
3102 .path = "show ip6 local",
3103 .function = show_ip6_local_command_fn,
3104 .short_help = "show ip6 local",
3108 #ifndef CLIB_MARCH_VARIANT
3110 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3113 vnet_main_t *vnm = vnet_get_main ();
3114 vnet_interface_main_t *im = &vnm->interface_main;
3115 ip6_main_t *ipm = &ip6_main;
3116 ip_lookup_main_t *lm = &ipm->lookup_main;
3117 vnet_classify_main_t *cm = &vnet_classify_main;
3118 ip6_address_t *if_addr;
3120 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3121 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3123 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3124 return VNET_API_ERROR_NO_SUCH_ENTRY;
3126 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3127 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3129 if_addr = ip6_interface_first_address (ipm, sw_if_index);
3131 if (NULL != if_addr)
3133 fib_prefix_t pfx = {
3135 .fp_proto = FIB_PROTOCOL_IP6,
3136 .fp_addr.ip6 = *if_addr,
3140 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3144 if (table_index != (u32) ~ 0)
3146 dpo_id_t dpo = DPO_INVALID;
3151 classify_dpo_create (DPO_PROTO_IP6, table_index));
3153 fib_table_entry_special_dpo_add (fib_index,
3155 FIB_SOURCE_CLASSIFY,
3156 FIB_ENTRY_FLAG_NONE, &dpo);
3161 fib_table_entry_special_remove (fib_index,
3162 &pfx, FIB_SOURCE_CLASSIFY);
3170 static clib_error_t *
3171 set_ip6_classify_command_fn (vlib_main_t * vm,
3172 unformat_input_t * input,
3173 vlib_cli_command_t * cmd)
3175 u32 table_index = ~0;
3176 int table_index_set = 0;
3177 u32 sw_if_index = ~0;
3180 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3182 if (unformat (input, "table-index %d", &table_index))
3183 table_index_set = 1;
3184 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3185 vnet_get_main (), &sw_if_index))
3191 if (table_index_set == 0)
3192 return clib_error_return (0, "classify table-index must be specified");
3194 if (sw_if_index == ~0)
3195 return clib_error_return (0, "interface / subif must be specified");
3197 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3204 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3205 return clib_error_return (0, "No such interface");
3207 case VNET_API_ERROR_NO_SUCH_ENTRY:
3208 return clib_error_return (0, "No such classifier table");
3214 * Assign a classification table to an interface. The classification
3215 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3216 * commands. Once the table is create, use this command to filter packets
3220 * Example of how to assign a classification table to an interface:
3221 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3224 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3226 .path = "set ip6 classify",
3228 "set ip6 classify intfc <interface> table-index <classify-idx>",
3229 .function = set_ip6_classify_command_fn,
3233 static clib_error_t *
3234 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3236 ip6_main_t *im = &ip6_main;
3241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3243 if (unformat (input, "hash-buckets %d", &tmp))
3245 else if (unformat (input, "heap-size %U",
3246 unformat_memory_size, &heapsize))
3249 return clib_error_return (0, "unknown input '%U'",
3250 format_unformat_error, input);
3253 im->lookup_table_nbuckets = nbuckets;
3254 im->lookup_table_size = heapsize;
3259 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3262 * fd.io coding-style-patch-verification: ON
3265 * eval: (c-set-style "gnu")