2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * ip/ip6_forward.c: IP v6 forwarding
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
43 #include <vnet/srp/srp.h> /* for srp_hw_interface_class */
44 #include <vppinfra/cache.h>
46 #include <vppinfra/bihash_template.c>
48 static void compute_prefix_lengths_in_search_order (ip6_main_t * im)
51 vec_reset_length (im->prefix_lengths_in_search_order);
52 /* Note: bitmap reversed so this is in fact a longest prefix match */
53 clib_bitmap_foreach (i, im->non_empty_dst_address_length_bitmap,
55 int dst_address_length = 128 - i;
56 vec_add1 (im->prefix_lengths_in_search_order, dst_address_length);
61 ip6_fib_lookup_with_table (ip6_main_t * im, u32 fib_index, ip6_address_t * dst)
63 ip_lookup_main_t * lm = &im->lookup_main;
66 BVT(clib_bihash_kv) kv, value;
69 len = vec_len (im->prefix_lengths_in_search_order);
71 kv.key[0] = dst->as_u64[0];
72 kv.key[1] = dst->as_u64[1];
73 fib = ((u64)((fib_index))<<32);
75 for (i = 0; i < len; i++)
77 int dst_address_length = im->prefix_lengths_in_search_order[i];
78 ip6_address_t * mask = &im->fib_masks[dst_address_length];
80 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
81 //As lengths are decreasing, masks are increasingly specific.
82 kv.key[0] &= mask->as_u64[0];
83 kv.key[1] &= mask->as_u64[1];
84 kv.key[2] = fib | dst_address_length;
86 rv = BV(clib_bihash_search_inline_2)(&im->ip6_lookup_table, &kv, &value);
91 return lm->miss_adj_index;
94 u32 ip6_fib_lookup (ip6_main_t * im, u32 sw_if_index, ip6_address_t * dst)
96 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
97 return ip6_fib_lookup_with_table (im, fib_index, dst);
101 vnet_ip6_fib_init (ip6_main_t * im, u32 fib_index)
103 ip_lookup_main_t * lm = &im->lookup_main;
104 ip6_add_del_route_args_t a;
105 ip_adjacency_t * adj;
107 memset(&a, 0x0, sizeof(ip6_add_del_route_args_t));
109 a.table_index_or_table_id = fib_index;
110 a.flags = (IP6_ROUTE_FLAG_ADD
111 | IP6_ROUTE_FLAG_FIB_INDEX
112 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
113 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
115 /* Add ff02::1:ff00:0/104 via local route for all tables.
116 This is required for neighbor discovery to work. */
117 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
119 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
120 adj->if_address_index = ~0;
121 adj->rewrite_header.data_bytes = 0;
123 ip6_set_solicited_node_multicast_address (&a.dst_address, 0);
125 a.dst_address_length = 104;
126 ip6_add_del_route (im, &a);
128 /* Add all-routers multicast address via local route for all tables */
129 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
131 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
132 adj->if_address_index = ~0;
133 adj->rewrite_header.data_bytes = 0;
135 ip6_set_reserved_multicast_address (&a.dst_address,
136 IP6_MULTICAST_SCOPE_link_local,
137 IP6_MULTICAST_GROUP_ID_all_routers);
139 a.dst_address_length = 128;
140 ip6_add_del_route (im, &a);
142 /* Add all-nodes multicast address via local route for all tables */
143 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
145 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
146 adj->if_address_index = ~0;
147 adj->rewrite_header.data_bytes = 0;
149 ip6_set_reserved_multicast_address (&a.dst_address,
150 IP6_MULTICAST_SCOPE_link_local,
151 IP6_MULTICAST_GROUP_ID_all_hosts);
153 a.dst_address_length = 128;
154 ip6_add_del_route (im, &a);
156 /* Add all-mldv2 multicast address via local route for all tables */
157 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
159 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
160 adj->if_address_index = ~0;
161 adj->rewrite_header.data_bytes = 0;
163 ip6_set_reserved_multicast_address (&a.dst_address,
164 IP6_MULTICAST_SCOPE_link_local,
165 IP6_MULTICAST_GROUP_ID_mldv2_routers);
167 a.dst_address_length = 128;
168 ip6_add_del_route (im, &a);
172 create_fib_with_table_id (ip6_main_t * im, u32 table_id)
175 hash_set (im->fib_index_by_table_id, table_id, vec_len (im->fibs));
176 vec_add2 (im->fibs, fib, 1);
177 fib->table_id = table_id;
178 fib->index = fib - im->fibs;
179 fib->flow_hash_config = IP_FLOW_HASH_DEFAULT;
180 vnet_ip6_fib_init (im, fib->index);
185 find_ip6_fib_by_table_index_or_id (ip6_main_t * im, u32 table_index_or_id, u32 flags)
187 uword * p, fib_index;
189 fib_index = table_index_or_id;
190 if (! (flags & IP6_ROUTE_FLAG_FIB_INDEX))
192 if (table_index_or_id == ~0) {
193 table_index_or_id = 0;
194 while (hash_get (im->fib_index_by_table_id, table_index_or_id)) {
197 return create_fib_with_table_id (im, table_index_or_id);
200 p = hash_get (im->fib_index_by_table_id, table_index_or_id);
202 return create_fib_with_table_id (im, table_index_or_id);
205 return vec_elt_at_index (im->fibs, fib_index);
208 void ip6_add_del_route (ip6_main_t * im, ip6_add_del_route_args_t * a)
210 ip_lookup_main_t * lm = &im->lookup_main;
212 ip6_address_t dst_address;
213 u32 dst_address_length, adj_index;
215 u32 old_adj_index = ~0;
216 BVT(clib_bihash_kv) kv, value;
218 vlib_smp_unsafe_warning();
220 is_del = (a->flags & IP6_ROUTE_FLAG_DEL) != 0;
222 /* Either create new adjacency or use given one depending on arguments. */
223 if (a->n_add_adj > 0)
225 ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
226 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
229 adj_index = a->adj_index;
231 dst_address = a->dst_address;
232 dst_address_length = a->dst_address_length;
233 fib = find_ip6_fib_by_table_index_or_id (im, a->table_index_or_table_id,
236 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
237 ip6_address_mask (&dst_address, &im->fib_masks[dst_address_length]);
239 /* refcount accounting */
242 ASSERT (im->dst_address_length_refcounts[dst_address_length] > 0);
243 if (--im->dst_address_length_refcounts[dst_address_length] == 0)
245 im->non_empty_dst_address_length_bitmap =
246 clib_bitmap_set (im->non_empty_dst_address_length_bitmap,
247 128 - dst_address_length, 0);
248 compute_prefix_lengths_in_search_order (im);
253 im->dst_address_length_refcounts[dst_address_length]++;
255 im->non_empty_dst_address_length_bitmap =
256 clib_bitmap_set (im->non_empty_dst_address_length_bitmap,
257 128 - dst_address_length, 1);
258 compute_prefix_lengths_in_search_order (im);
261 kv.key[0] = dst_address.as_u64[0];
262 kv.key[1] = dst_address.as_u64[1];
263 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
265 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
266 old_adj_index = value.value;
269 BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 0 /* is_add */);
272 /* Make sure adj index is valid. */
274 (void) ip_get_adjacency (lm, adj_index);
276 kv.value = adj_index;
278 BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 1 /* is_add */);
281 /* Avoid spurious reference count increments */
282 if (old_adj_index == adj_index
284 && !(a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
286 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
287 if (adj->share_count > 0)
291 /* Delete old adjacency index if present and changed. */
293 if (! (a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
294 && old_adj_index != ~0
295 && old_adj_index != adj_index)
296 ip_del_adjacency (lm, old_adj_index);
301 ip6_route_get_next_hop_adj (ip6_main_t * im,
303 ip6_address_t *next_hop,
304 u32 next_hop_sw_if_index,
305 u32 explicit_fib_index)
307 ip_lookup_main_t * lm = &im->lookup_main;
308 vnet_main_t * vnm = vnet_get_main();
309 int is_interface_next_hop;
314 fib = vec_elt_at_index (im->fibs, fib_index);
316 is_interface_next_hop = ip6_address_is_zero (next_hop);
318 if (is_interface_next_hop)
320 nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index,
321 next_hop_sw_if_index);
323 nh_adj_index = *nh_result;
326 ip_adjacency_t * adj;
327 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
329 ip6_adjacency_set_interface_route (vnm, adj,
330 next_hop_sw_if_index, ~0);
331 ip_call_add_del_adjacency_callbacks
332 (lm, next_hop_sw_if_index, /* is_del */ 0);
333 hash_set (im->interface_route_adj_index_by_sw_if_index,
334 next_hop_sw_if_index, nh_adj_index);
337 else if (next_hop_sw_if_index == ~0)
339 /* next-hop is recursive. we always need a indirect adj
340 * for recursive paths. Any LPM we perform now will give
341 * us a valid adj, but without tracking the next-hop we
342 * have no way to keep it valid.
344 ip_adjacency_t add_adj;
345 memset (&add_adj, 0, sizeof(add_adj));
347 add_adj.lookup_next_index = IP_LOOKUP_NEXT_INDIRECT;
348 add_adj.indirect.next_hop.ip6.as_u64[0] = next_hop->as_u64[0];
349 add_adj.indirect.next_hop.ip6.as_u64[1] = next_hop->as_u64[1];
350 add_adj.explicit_fib_index = explicit_fib_index;
351 ip_add_adjacency (lm, &add_adj, 1, &nh_adj_index);
355 BVT(clib_bihash_kv) kv, value;
357 /* Look for the interface /128 route */
358 kv.key[0] = next_hop->as_u64[0];
359 kv.key[1] = next_hop->as_u64[1];
360 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
362 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
364 ip_adjacency_t * adj;
365 nh_adj_index = ip6_fib_lookup_with_table (im, fib_index, next_hop);
366 adj = ip_get_adjacency (lm, nh_adj_index);
367 /* if ND interface adjacencty is present, we need to
368 install ND adjaceny for specific next hop */
369 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
370 adj->arp.next_hop.ip6.as_u64[0] == 0 &&
371 adj->arp.next_hop.ip6.as_u64[1] == 0)
373 nh_adj_index = vnet_ip6_neighbor_glean_add(fib_index, next_hop);
375 else if (next_hop->as_u8[0] == 0xfe)
377 //Next hop is link-local. No indirect in this case.
378 //Let's add it as a possible neighbor on this interface
379 ip6_address_t null_addr= {};
380 ip6_add_del_route_next_hop (im, IP6_ROUTE_FLAG_ADD,
382 &null_addr, next_hop_sw_if_index,
389 nh_adj_index = value.value;
393 return (nh_adj_index);
397 ip6_add_del_route_next_hop (ip6_main_t * im,
399 ip6_address_t * dst_address,
400 u32 dst_address_length,
401 ip6_address_t * next_hop,
402 u32 next_hop_sw_if_index,
403 u32 next_hop_weight, u32 adj_index,
404 u32 explicit_fib_index)
406 vnet_main_t * vnm = vnet_get_main();
407 ip_lookup_main_t * lm = &im->lookup_main;
410 ip6_address_t masked_dst_address;
411 u32 old_mp_adj_index, new_mp_adj_index;
412 u32 dst_adj_index, nh_adj_index;
414 ip_adjacency_t * dst_adj;
415 ip_multipath_adjacency_t * old_mp, * new_mp;
416 int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
417 clib_error_t * error = 0;
418 BVT(clib_bihash_kv) kv, value;
420 vlib_smp_unsafe_warning();
422 if (explicit_fib_index == (u32)~0)
423 fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
425 fib_index = explicit_fib_index;
427 fib = vec_elt_at_index (im->fibs, fib_index);
429 /* Lookup next hop to be added or deleted. */
430 if (adj_index == (u32)~0)
432 nh_adj_index = ip6_route_get_next_hop_adj(im, fib_index,
434 next_hop_sw_if_index,
439 /* Look for the interface /128 route */
440 kv.key[0] = next_hop->as_u64[0];
441 kv.key[1] = next_hop->as_u64[1];
442 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
444 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
446 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
447 error = clib_error_return (0, "next-hop %U/128 not in FIB",
448 format_ip6_address, next_hop);
452 nh_adj_index = value.value;
455 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
456 masked_dst_address = dst_address[0];
457 ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
459 kv.key[0] = masked_dst_address.as_u64[0];
460 kv.key[1] = masked_dst_address.as_u64[1];
461 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
463 rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
467 dst_adj_index = value.value;
468 dst_adj = ip_get_adjacency (lm, dst_adj_index);
472 /* For deletes destination must be known. */
475 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
476 error = clib_error_return (0, "unknown destination %U/%d",
477 format_ip6_address, dst_address,
486 /* Ignore adds of X/128 with next hop of X. */
488 && dst_address_length == 128
489 && ip6_address_is_equal (dst_address, next_hop))
491 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
492 error = clib_error_return (0, "prefix matches next hop %U/%d",
493 format_ip6_address, dst_address,
498 /* Destination is not known and default weight is set so add route
499 to existing non-multipath adjacency */
500 if (dst_adj_index == ~0 && next_hop_weight == 1 && next_hop_sw_if_index == ~0)
502 /* create / delete additional mapping of existing adjacency */
503 ip6_add_del_route_args_t a;
504 ip_adjacency_t * nh_adj = ip_get_adjacency (lm, nh_adj_index);
506 a.table_index_or_table_id = fib_index;
507 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
508 | IP6_ROUTE_FLAG_FIB_INDEX
509 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
510 | (flags & (IP6_ROUTE_FLAG_NO_REDISTRIBUTE
511 | IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
512 a.dst_address = dst_address[0];
513 a.dst_address_length = dst_address_length;
514 a.adj_index = nh_adj_index;
518 ip6_add_del_route (im, &a);
520 /* adjust share count. This cannot be the only use of the adjacency
521 unless next hop is an indiect adj where share count is already
523 if (next_hop_sw_if_index != ~0)
524 nh_adj->share_count += is_del ? -1 : 1;
529 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
531 if (! ip_multipath_adjacency_add_del_next_hop
533 dst_adj ? dst_adj->heap_handle : ~0,
538 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
539 error = clib_error_return
540 (0, "requested deleting next-hop %U not found in multi-path",
541 format_ip6_address, next_hop);
546 if (old_mp_adj_index != ~0)
547 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
548 if (new_mp_adj_index != ~0)
549 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
551 if (old_mp != new_mp)
553 ip6_add_del_route_args_t a;
554 ip_adjacency_t * adj;
556 a.table_index_or_table_id = fib_index;
557 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
558 | IP6_ROUTE_FLAG_FIB_INDEX
559 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
560 | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
561 a.dst_address = dst_address[0];
562 a.dst_address_length = dst_address_length;
563 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
567 ip6_add_del_route (im, &a);
569 adj = ip_get_adjacency (lm, new_mp ? new_mp->adj_index : dst_adj_index);
571 adj->share_count += is_del ? -1 : 1;
576 clib_error_report (error);
580 ip6_get_route (ip6_main_t * im,
581 u32 table_index_or_table_id,
583 ip6_address_t * address,
586 ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
587 ip6_address_t masked_address;
588 BVT(clib_bihash_kv) kv, value;
590 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
591 clib_memcpy (&masked_address, address, sizeof (masked_address));
592 ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
594 kv.key[0] = masked_address.as_u64[0];
595 kv.key[1] = masked_address.as_u64[1];
596 kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
598 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
599 return (value.value);
604 ip6_foreach_matching_route (ip6_main_t * im,
605 u32 table_index_or_table_id,
607 ip6_address_t * dst_address,
609 ip6_address_t ** results,
610 u8 ** result_lengths)
613 find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
614 BVT(clib_bihash) * h = &im->ip6_lookup_table;
615 BVT(clib_bihash_value) * v;
616 clib_bihash_bucket_t * b;
620 _vec_len (*results) = 0;
622 _vec_len (*result_lengths) = 0;
624 /* Walk the table looking for routes which match the supplied address */
625 for (i = 0; i < h->nbuckets; i++)
631 v = BV(clib_bihash_get_value) (h, b->offset);
632 for (j = 0; j < (1<<b->log2_pages); j++)
634 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
636 if (BV(clib_bihash_is_free)(&v->kvp[k]))
639 if ((v->kvp[k].key[2]
640 == (((u64)((fib - im->fibs))<<32) | address_length))
641 && ip6_destination_matches_route
642 (im, dst_address, (ip6_address_t *) &v->kvp[k],
647 a = (ip6_address_t *)(&v->kvp[k]);
649 vec_add1 (*results, a[0]);
650 vec_add1 (*result_lengths, address_length);
658 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
659 u32 table_index_or_table_id,
664 = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
666 ip_lookup_main_t * lm = &im->lookup_main;
668 if (lm->n_adjacency_remaps == 0)
671 clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
673 /* All remaps have been performed. */
674 lm->n_adjacency_remaps = 0;
677 void ip6_delete_matching_routes (ip6_main_t * im,
678 u32 table_index_or_table_id,
680 ip6_address_t * address,
683 /* $$$$ static may be OK - this should happen only on thread 0 */
684 static ip6_address_t * matching_addresses;
685 static u8 * matching_address_lengths;
687 ip6_add_del_route_args_t a;
689 vlib_smp_unsafe_warning();
691 a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
692 a.table_index_or_table_id = table_index_or_table_id;
697 for (l = address_length + 1; l <= 128; l++)
699 ip6_foreach_matching_route (im, table_index_or_table_id, flags,
703 &matching_address_lengths);
704 for (i = 0; i < vec_len (matching_addresses); i++)
706 a.dst_address = matching_addresses[i];
707 a.dst_address_length = matching_address_lengths[i];
708 ip6_add_del_route (im, &a);
712 ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
716 ip6_forward_next_trace (vlib_main_t * vm,
717 vlib_node_runtime_t * node,
718 vlib_frame_t * frame,
719 vlib_rx_or_tx_t which_adj_index);
722 ip6_lookup_inline (vlib_main_t * vm,
723 vlib_node_runtime_t * node,
724 vlib_frame_t * frame,
727 ip6_main_t * im = &ip6_main;
728 ip_lookup_main_t * lm = &im->lookup_main;
729 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
730 u32 n_left_from, n_left_to_next, * from, * to_next;
731 ip_lookup_next_t next;
732 u32 cpu_index = os_get_cpu_number();
734 from = vlib_frame_vector_args (frame);
735 n_left_from = frame->n_vectors;
736 next = node->cached_next_index;
738 while (n_left_from > 0)
740 vlib_get_next_frame (vm, node, next,
741 to_next, n_left_to_next);
743 while (n_left_from >= 4 && n_left_to_next >= 2)
745 vlib_buffer_t * p0, * p1;
746 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
747 ip_lookup_next_t next0, next1;
748 ip6_header_t * ip0, * ip1;
749 ip_adjacency_t * adj0, * adj1;
750 ip6_address_t * dst_addr0, * dst_addr1;
751 u32 fib_index0, fib_index1;
752 u32 flow_hash_config0, flow_hash_config1;
754 /* Prefetch next iteration. */
756 vlib_buffer_t * p2, * p3;
758 p2 = vlib_get_buffer (vm, from[2]);
759 p3 = vlib_get_buffer (vm, from[3]);
761 vlib_prefetch_buffer_header (p2, LOAD);
762 vlib_prefetch_buffer_header (p3, LOAD);
763 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
764 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
767 pi0 = to_next[0] = from[0];
768 pi1 = to_next[1] = from[1];
770 p0 = vlib_get_buffer (vm, pi0);
771 p1 = vlib_get_buffer (vm, pi1);
773 ip0 = vlib_buffer_get_current (p0);
774 ip1 = vlib_buffer_get_current (p1);
778 ip_adjacency_t * iadj0, * iadj1;
779 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
780 iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
781 dst_addr0 = &iadj0->indirect.next_hop.ip6;
782 dst_addr1 = &iadj1->indirect.next_hop.ip6;
786 dst_addr0 = &ip0->dst_address;
787 dst_addr1 = &ip1->dst_address;
790 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
791 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
793 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
794 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
795 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
796 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
798 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
799 adj_index1 = ip6_fib_lookup_with_table (im, fib_index1, dst_addr1);
801 adj0 = ip_get_adjacency (lm, adj_index0);
802 adj1 = ip_get_adjacency (lm, adj_index1);
804 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
806 adj_index0 = ip6_fib_lookup_with_table
807 (im, adj0->explicit_fib_index, dst_addr0);
808 adj0 = ip_get_adjacency (lm, adj_index0);
810 if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
812 adj_index1 = ip6_fib_lookup_with_table
813 (im, adj1->explicit_fib_index, dst_addr1);
814 adj1 = ip_get_adjacency (lm, adj_index1);
817 next0 = adj0->lookup_next_index;
818 next1 = adj1->lookup_next_index;
820 /* Only process the HBH Option Header if explicitly configured to do so */
821 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
822 adj_index0 ? (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : adj0->lookup_next_index;
823 next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
824 adj_index1 ? (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : adj1->lookup_next_index;
826 vnet_buffer (p0)->ip.flow_hash =
827 vnet_buffer(p1)->ip.flow_hash = 0;
829 if (PREDICT_FALSE(adj0->n_adj > 1))
832 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
833 vnet_buffer (p0)->ip.flow_hash =
834 ip6_compute_flow_hash (ip0, flow_hash_config0);
837 if (PREDICT_FALSE(adj1->n_adj > 1))
840 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
842 vnet_buffer (p1)->ip.flow_hash =
843 ip6_compute_flow_hash (ip1, flow_hash_config1);
846 ASSERT (adj0->n_adj > 0);
847 ASSERT (adj1->n_adj > 0);
848 ASSERT (is_pow2 (adj0->n_adj));
849 ASSERT (is_pow2 (adj1->n_adj));
850 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
851 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
853 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
854 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
856 vlib_increment_combined_counter
857 (cm, cpu_index, adj_index0, 1,
858 vlib_buffer_length_in_chain (vm, p0));
859 vlib_increment_combined_counter
860 (cm, cpu_index, adj_index1, 1,
861 vlib_buffer_length_in_chain (vm, p1));
868 wrong_next = (next0 != next) + 2*(next1 != next);
869 if (PREDICT_FALSE (wrong_next != 0))
878 vlib_set_next_frame_buffer (vm, node, next0, pi0);
885 vlib_set_next_frame_buffer (vm, node, next1, pi1);
892 vlib_set_next_frame_buffer (vm, node, next0, pi0);
893 vlib_set_next_frame_buffer (vm, node, next1, pi1);
897 vlib_put_next_frame (vm, node, next, n_left_to_next);
899 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
905 while (n_left_from > 0 && n_left_to_next > 0)
910 ip_lookup_next_t next0;
911 ip_adjacency_t * adj0;
912 ip6_address_t * dst_addr0;
913 u32 fib_index0, flow_hash_config0;
918 p0 = vlib_get_buffer (vm, pi0);
920 ip0 = vlib_buffer_get_current (p0);
924 ip_adjacency_t * iadj0;
925 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
926 dst_addr0 = &iadj0->indirect.next_hop.ip6;
930 dst_addr0 = &ip0->dst_address;
933 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
934 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
935 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
938 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
940 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
942 adj0 = ip_get_adjacency (lm, adj_index0);
944 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
946 adj_index0 = ip6_fib_lookup_with_table
947 (im, adj0->explicit_fib_index, dst_addr0);
948 adj0 = ip_get_adjacency (lm, adj_index0);
951 /* Only process the HBH Option Header if explicitly configured to do so */
952 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
953 adj_index0 ? (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : adj0->lookup_next_index;
955 vnet_buffer (p0)->ip.flow_hash = 0;
957 if (PREDICT_FALSE(adj0->n_adj > 1))
960 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
961 vnet_buffer (p0)->ip.flow_hash =
962 ip6_compute_flow_hash (ip0, flow_hash_config0);
965 ASSERT (adj0->n_adj > 0);
966 ASSERT (is_pow2 (adj0->n_adj));
967 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
969 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
971 vlib_increment_combined_counter
972 (cm, cpu_index, adj_index0, 1,
973 vlib_buffer_length_in_chain (vm, p0));
980 if (PREDICT_FALSE (next0 != next))
983 vlib_put_next_frame (vm, node, next, n_left_to_next);
985 vlib_get_next_frame (vm, node, next,
986 to_next, n_left_to_next);
993 vlib_put_next_frame (vm, node, next, n_left_to_next);
996 if (node->flags & VLIB_NODE_FLAG_TRACE)
997 ip6_forward_next_trace(vm, node, frame, VLIB_TX);
999 return frame->n_vectors;
1002 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
1003 ip_adjacency_t * adj,
1005 u32 if_address_index)
1007 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1011 if (hw->hw_class_index == ethernet_hw_interface_class.index
1012 || hw->hw_class_index == srp_hw_interface_class.index)
1014 n = IP_LOOKUP_NEXT_ARP;
1015 node_index = ip6_discover_neighbor_node.index;
1016 adj->if_address_index = if_address_index;
1017 adj->arp.next_hop.ip6.as_u64[0] = 0;
1018 adj->arp.next_hop.ip6.as_u64[1] = 0;
1022 n = IP_LOOKUP_NEXT_REWRITE;
1023 node_index = ip6_rewrite_node.index;
1026 adj->lookup_next_index = n;
1027 adj->explicit_fib_index = ~0;
1029 vnet_rewrite_for_sw_interface
1031 VNET_L3_PACKET_TYPE_IP6,
1034 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
1035 &adj->rewrite_header,
1036 sizeof (adj->rewrite_data));
1040 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
1041 ip6_main_t * im, u32 fib_index,
1042 ip_interface_address_t * a)
1044 ip_lookup_main_t * lm = &im->lookup_main;
1045 ip_adjacency_t * adj;
1046 ip6_address_t * address = ip_interface_address_get_address (lm, a);
1047 ip6_add_del_route_args_t x;
1048 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1049 u32 classify_table_index;
1051 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1052 x.table_index_or_table_id = fib_index;
1053 x.flags = (IP6_ROUTE_FLAG_ADD
1054 | IP6_ROUTE_FLAG_FIB_INDEX
1055 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
1056 x.dst_address = address[0];
1057 x.dst_address_length = a->address_length;
1061 a->neighbor_probe_adj_index = ~0;
1062 if (a->address_length < 128)
1064 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1066 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
1067 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1068 ip6_add_del_route (im, &x);
1069 a->neighbor_probe_adj_index = x.adj_index;
1072 /* Add e.g. ::1/128 as local to this host. */
1073 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1076 classify_table_index = ~0;
1077 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
1078 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
1079 if (classify_table_index != (u32) ~0)
1081 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
1082 adj->classify.table_index = classify_table_index;
1085 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1087 adj->if_address_index = a - lm->if_address_pool;
1088 adj->rewrite_header.sw_if_index = sw_if_index;
1089 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
1090 adj->rewrite_header.data_bytes = 0;
1091 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1092 x.dst_address_length = 128;
1093 ip6_add_del_route (im, &x);
1097 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
1098 ip6_address_t * address, u32 address_length)
1100 ip6_add_del_route_args_t x;
1102 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1103 x.table_index_or_table_id = fib_index;
1104 x.flags = (IP6_ROUTE_FLAG_DEL
1105 | IP6_ROUTE_FLAG_FIB_INDEX
1106 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
1107 x.dst_address = address[0];
1108 x.dst_address_length = address_length;
1113 if (address_length < 128)
1115 /* Don't wipe out fe80::0/64 */
1116 if (address_length != 64 ||
1117 address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
1118 ip6_add_del_route (im, &x);
1121 x.dst_address_length = 128;
1122 ip6_add_del_route (im, &x);
1124 ip6_delete_matching_routes (im,
1126 IP6_ROUTE_FLAG_FIB_INDEX,
1133 ip6_address_t address;
1135 } ip6_interface_address_t;
1137 static clib_error_t *
1138 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1140 ip6_address_t * new_address,
1146 static clib_error_t *
1147 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1149 ip6_address_t * address,
1155 vnet_main_t * vnm = vnet_get_main();
1156 ip6_main_t * im = &ip6_main;
1157 ip_lookup_main_t * lm = &im->lookup_main;
1158 clib_error_t * error;
1159 u32 if_address_index;
1160 ip6_address_fib_t ip6_af, * addr_fib = 0;
1162 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1163 ip6_addr_fib_init (&ip6_af, address,
1164 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1165 vec_add1 (addr_fib, ip6_af);
1168 uword elts_before = pool_elts (lm->if_address_pool);
1170 error = ip_interface_address_add_del
1180 /* Pool did not grow: add duplicate address. */
1181 if (elts_before == pool_elts (lm->if_address_pool))
1185 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1188 ip6_del_interface_routes (im, ip6_af.fib_index, address,
1192 ip6_add_interface_routes (vnm, sw_if_index,
1193 im, ip6_af.fib_index,
1194 pool_elt_at_index (lm->if_address_pool, if_address_index));
1198 ip6_add_del_interface_address_callback_t * cb;
1199 vec_foreach (cb, im->add_del_interface_address_callbacks)
1200 cb->function (im, cb->function_opaque, sw_if_index,
1201 address, address_length,
1207 vec_free (addr_fib);
1212 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1213 ip6_address_t * address, u32 address_length,
1216 return ip6_add_del_interface_address_internal
1217 (vm, sw_if_index, address, address_length,
1218 /* redistribute */ 1,
1219 /* insert_routes */ 1,
1224 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1228 ip6_main_t * im = &ip6_main;
1229 ip_interface_address_t * ia;
1231 u32 is_admin_up, fib_index;
1233 /* Fill in lookup tables with default table (0). */
1234 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1236 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1238 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1240 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1242 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1243 0 /* honor unnumbered */,
1245 a = ip_interface_address_get_address (&im->lookup_main, ia);
1247 ip6_add_interface_routes (vnm, sw_if_index,
1251 ip6_del_interface_routes (im, fib_index,
1252 a, ia->address_length);
1258 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1260 /* Built-in ip6 unicast rx feature path definition */
1261 VNET_IP6_UNICAST_FEATURE_INIT (ip6_inacl, static) = {
1262 .node_name = "ip6-inacl",
1263 .runs_before = {"ipsec-input-ip6", 0},
1264 .feature_index = &ip6_main.ip6_unicast_rx_feature_check_access,
1267 VNET_IP6_UNICAST_FEATURE_INIT (ip6_ipsec, static) = {
1268 .node_name = "ipsec-input-ip6",
1269 .runs_before = {"l2tp-decap", 0},
1270 .feature_index = &ip6_main.ip6_unicast_rx_feature_ipsec,
1273 VNET_IP6_UNICAST_FEATURE_INIT (ip6_l2tp, static) = {
1274 .node_name = "l2tp-decap",
1275 .runs_before = {"vpath-input-ip6", 0},
1276 .feature_index = &ip6_main.ip6_unicast_rx_feature_l2tp_decap,
1279 VNET_IP6_UNICAST_FEATURE_INIT (ip6_vpath, static) = {
1280 .node_name = "vpath-input-ip6",
1281 .runs_before = {"ip6-lookup", 0},
1282 .feature_index = &ip6_main.ip6_unicast_rx_feature_vpath,
1285 VNET_IP6_UNICAST_FEATURE_INIT (ip6_lookup, static) = {
1286 .node_name = "ip6-lookup",
1287 .runs_before = {0}, /* not before any other features */
1288 .feature_index = &ip6_main.ip6_unicast_rx_feature_lookup,
1291 /* Built-in ip6 multicast rx feature path definition (none now) */
1292 VNET_IP6_MULTICAST_FEATURE_INIT (ip4_vpath_mc, static) = {
1293 .node_name = "vpath-input-ip6",
1294 .runs_before = {"ip6-lookup", 0},
1295 .feature_index = &ip6_main.ip6_multicast_rx_feature_vpath,
1298 VNET_IP6_MULTICAST_FEATURE_INIT (ip6_lookup, static) = {
1299 .node_name = "ip6-lookup",
1300 .runs_before = {0}, /* not before any other features */
1301 .feature_index = &ip6_main.ip6_multicast_rx_feature_lookup,
1304 static char * feature_start_nodes[] =
1307 static clib_error_t *
1308 ip6_feature_init (vlib_main_t * vm, ip6_main_t * im)
1310 ip_lookup_main_t * lm = &im->lookup_main;
1311 clib_error_t * error;
1314 for (cast = 0; cast < VNET_N_CAST; cast++)
1316 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1317 vnet_config_main_t * vcm = &cm->config_main;
1319 if ((error = ip_feature_init_cast (vm, cm, vcm,
1320 feature_start_nodes,
1321 ARRAY_LEN(feature_start_nodes),
1330 ip6_sw_interface_add_del (vnet_main_t * vnm,
1334 vlib_main_t * vm = vnm->vlib_main;
1335 ip6_main_t * im = &ip6_main;
1336 ip_lookup_main_t * lm = &im->lookup_main;
1340 for (cast = 0; cast < VNET_N_CAST; cast++)
1342 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1343 vnet_config_main_t * vcm = &cm->config_main;
1345 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1346 ci = cm->config_index_by_sw_if_index[sw_if_index];
1348 if (cast == VNET_UNICAST)
1349 feature_index = im->ip6_unicast_rx_feature_lookup;
1351 feature_index = im->ip6_multicast_rx_feature_lookup;
1354 ci = vnet_config_add_feature (vm, vcm,
1357 /* config data */ 0,
1358 /* # bytes of config data */ 0);
1360 ci = vnet_config_del_feature (vm, vcm,
1363 /* config data */ 0,
1364 /* # bytes of config data */ 0);
1366 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1368 return /* no error */ 0;
1371 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1374 ip6_lookup (vlib_main_t * vm,
1375 vlib_node_runtime_t * node,
1376 vlib_frame_t * frame)
1378 return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 0);
1381 static u8 * format_ip6_lookup_trace (u8 * s, va_list * args);
1383 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1384 .function = ip6_lookup,
1385 .name = "ip6-lookup",
1386 .vector_size = sizeof (u32),
1388 .format_trace = format_ip6_lookup_trace,
1390 .n_next_nodes = IP6_LOOKUP_N_NEXT,
1391 .next_nodes = IP6_LOOKUP_NEXT_NODES,
1394 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup)
1397 ip6_indirect (vlib_main_t * vm,
1398 vlib_node_runtime_t * node,
1399 vlib_frame_t * frame)
1401 return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 1);
1405 VLIB_REGISTER_NODE (ip6_indirect_node) = {
1406 .function = ip6_indirect,
1407 .name = "ip6-indirect",
1408 .vector_size = sizeof (u32),
1409 .sibling_of = "ip6-lookup",
1410 .format_trace = format_ip6_lookup_trace,
1414 VLIB_NODE_FUNCTION_MULTIARCH (ip6_indirect_node, ip6_indirect)
1417 /* Adjacency taken. */
1422 /* Packet data, possibly *after* rewrite. */
1423 u8 packet_data[128 - 1*sizeof(u32)];
1424 } ip6_forward_next_trace_t;
1426 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1428 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1429 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1430 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1431 uword indent = format_get_indent (s);
1433 s = format(s, "%U%U",
1434 format_white_space, indent,
1435 format_ip6_header, t->packet_data);
1439 static u8 * format_ip6_lookup_trace (u8 * s, va_list * args)
1441 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1442 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1443 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1444 vnet_main_t * vnm = vnet_get_main();
1445 ip6_main_t * im = &ip6_main;
1446 uword indent = format_get_indent (s);
1448 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1449 t->fib_index, t->adj_index, format_ip_adjacency,
1450 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1451 s = format(s, "\n%U%U",
1452 format_white_space, indent,
1453 format_ip6_header, t->packet_data);
1458 static u8 * format_ip6_rewrite_trace (u8 * s, va_list * args)
1460 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1461 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1462 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1463 vnet_main_t * vnm = vnet_get_main();
1464 ip6_main_t * im = &ip6_main;
1465 uword indent = format_get_indent (s);
1467 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1468 t->fib_index, t->adj_index, format_ip_adjacency,
1469 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1470 s = format (s, "\n%U%U",
1471 format_white_space, indent,
1472 format_ip_adjacency_packet_data,
1473 vnm, &im->lookup_main, t->adj_index,
1474 t->packet_data, sizeof (t->packet_data));
1478 /* Common trace function for all ip6-forward next nodes. */
1480 ip6_forward_next_trace (vlib_main_t * vm,
1481 vlib_node_runtime_t * node,
1482 vlib_frame_t * frame,
1483 vlib_rx_or_tx_t which_adj_index)
1486 ip6_main_t * im = &ip6_main;
1488 n_left = frame->n_vectors;
1489 from = vlib_frame_vector_args (frame);
1494 vlib_buffer_t * b0, * b1;
1495 ip6_forward_next_trace_t * t0, * t1;
1497 /* Prefetch next iteration. */
1498 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1499 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1504 b0 = vlib_get_buffer (vm, bi0);
1505 b1 = vlib_get_buffer (vm, bi1);
1507 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1509 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1510 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1511 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1512 t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1513 vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1514 vec_elt (im->fib_index_by_sw_if_index,
1515 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1517 clib_memcpy (t0->packet_data,
1518 vlib_buffer_get_current (b0),
1519 sizeof (t0->packet_data));
1521 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1523 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1524 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1525 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1526 t1->fib_index = (vnet_buffer(b1)->sw_if_index[VLIB_TX] != (u32)~0) ?
1527 vnet_buffer(b1)->sw_if_index[VLIB_TX] :
1528 vec_elt (im->fib_index_by_sw_if_index,
1529 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1531 clib_memcpy (t1->packet_data,
1532 vlib_buffer_get_current (b1),
1533 sizeof (t1->packet_data));
1543 ip6_forward_next_trace_t * t0;
1547 b0 = vlib_get_buffer (vm, bi0);
1549 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1551 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1552 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1553 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1554 t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1555 vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1556 vec_elt (im->fib_index_by_sw_if_index,
1557 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1559 clib_memcpy (t0->packet_data,
1560 vlib_buffer_get_current (b0),
1561 sizeof (t0->packet_data));
1569 ip6_drop_or_punt (vlib_main_t * vm,
1570 vlib_node_runtime_t * node,
1571 vlib_frame_t * frame,
1572 ip6_error_t error_code)
1574 u32 * buffers = vlib_frame_vector_args (frame);
1575 uword n_packets = frame->n_vectors;
1577 vlib_error_drop_buffers (vm, node,
1582 ip6_input_node.index,
1585 if (node->flags & VLIB_NODE_FLAG_TRACE)
1586 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1592 ip6_drop (vlib_main_t * vm,
1593 vlib_node_runtime_t * node,
1594 vlib_frame_t * frame)
1595 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1598 ip6_punt (vlib_main_t * vm,
1599 vlib_node_runtime_t * node,
1600 vlib_frame_t * frame)
1601 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1604 ip6_miss (vlib_main_t * vm,
1605 vlib_node_runtime_t * node,
1606 vlib_frame_t * frame)
1607 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1609 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1610 .function = ip6_drop,
1612 .vector_size = sizeof (u32),
1614 .format_trace = format_ip6_forward_next_trace,
1622 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop)
1624 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1625 .function = ip6_punt,
1627 .vector_size = sizeof (u32),
1629 .format_trace = format_ip6_forward_next_trace,
1637 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt)
1639 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1640 .function = ip6_miss,
1642 .vector_size = sizeof (u32),
1644 .format_trace = format_ip6_forward_next_trace,
1652 VLIB_NODE_FUNCTION_MULTIARCH (ip6_miss_node, ip6_miss)
1654 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1655 .function = ip6_drop,
1656 .name = "ip6-multicast",
1657 .vector_size = sizeof (u32),
1659 .format_trace = format_ip6_forward_next_trace,
1667 /* Compute TCP/UDP/ICMP6 checksum in software. */
1668 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1671 u16 sum16, payload_length_host_byte_order;
1672 u32 i, n_this_buffer, n_bytes_left;
1673 u32 headers_size = sizeof(ip0[0]);
1674 void * data_this_buffer;
1676 ASSERT(bogus_lengthp);
1679 /* Initialize checksum with ip header. */
1680 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1681 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1682 data_this_buffer = (void *) (ip0 + 1);
1684 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1686 sum0 = ip_csum_with_carry (sum0,
1687 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1688 sum0 = ip_csum_with_carry (sum0,
1689 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1692 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1693 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1696 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1698 /* validate really icmp6 next */
1699 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1701 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1702 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1704 payload_length_host_byte_order -= skip_bytes;
1705 headers_size += skip_bytes;
1708 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1710 if (p0 && n_this_buffer + headers_size > p0->current_length)
1712 struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1713 u8 nb_segs = mb->nb_segs;
1715 n_this_buffer = (p0->current_length > headers_size ?
1716 p0->current_length - headers_size : 0);
1717 while (n_bytes_left)
1719 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1720 n_bytes_left -= n_this_buffer;
1724 if ((nb_segs == 0) || (mb == 0))
1727 data_this_buffer = rte_ctrlmbuf_data(mb);
1728 n_this_buffer = mb->data_len;
1730 if (n_bytes_left || nb_segs)
1736 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1738 if (p0 && n_this_buffer + headers_size > p0->current_length)
1739 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1742 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1743 n_bytes_left -= n_this_buffer;
1744 if (n_bytes_left == 0)
1747 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1752 p0 = vlib_get_buffer (vm, p0->next_buffer);
1753 data_this_buffer = vlib_buffer_get_current (p0);
1754 n_this_buffer = p0->current_length;
1758 sum16 = ~ ip_csum_fold (sum0);
1763 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1765 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1766 udp_header_t * udp0;
1770 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1771 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1772 || ip0->protocol == IP_PROTOCOL_ICMP6
1773 || ip0->protocol == IP_PROTOCOL_UDP
1774 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1776 udp0 = (void *) (ip0 + 1);
1777 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1779 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1780 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1784 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1786 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1787 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1793 ip6_local (vlib_main_t * vm,
1794 vlib_node_runtime_t * node,
1795 vlib_frame_t * frame)
1797 ip6_main_t * im = &ip6_main;
1798 ip_lookup_main_t * lm = &im->lookup_main;
1799 ip_local_next_t next_index;
1800 u32 * from, * to_next, n_left_from, n_left_to_next;
1801 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1803 from = vlib_frame_vector_args (frame);
1804 n_left_from = frame->n_vectors;
1805 next_index = node->cached_next_index;
1807 if (node->flags & VLIB_NODE_FLAG_TRACE)
1808 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1810 while (n_left_from > 0)
1812 vlib_get_next_frame (vm, node, next_index,
1813 to_next, n_left_to_next);
1815 while (n_left_from >= 4 && n_left_to_next >= 2)
1817 vlib_buffer_t * p0, * p1;
1818 ip6_header_t * ip0, * ip1;
1819 udp_header_t * udp0, * udp1;
1820 u32 pi0, ip_len0, udp_len0, flags0, next0;
1821 u32 pi1, ip_len1, udp_len1, flags1, next1;
1822 i32 len_diff0, len_diff1;
1823 u8 error0, type0, good_l4_checksum0;
1824 u8 error1, type1, good_l4_checksum1;
1826 pi0 = to_next[0] = from[0];
1827 pi1 = to_next[1] = from[1];
1831 n_left_to_next -= 2;
1833 p0 = vlib_get_buffer (vm, pi0);
1834 p1 = vlib_get_buffer (vm, pi1);
1836 ip0 = vlib_buffer_get_current (p0);
1837 ip1 = vlib_buffer_get_current (p1);
1839 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1840 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1842 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1843 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1848 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1849 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1851 udp0 = ip6_next_header (ip0);
1852 udp1 = ip6_next_header (ip1);
1854 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1855 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1856 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1858 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1859 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1861 /* Verify UDP length. */
1862 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1863 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1864 udp_len0 = clib_net_to_host_u16 (udp0->length);
1865 udp_len1 = clib_net_to_host_u16 (udp1->length);
1867 len_diff0 = ip_len0 - udp_len0;
1868 len_diff1 = ip_len1 - udp_len1;
1870 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1871 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1873 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1874 && ! good_l4_checksum0
1875 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1877 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1879 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1881 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1882 && ! good_l4_checksum1
1883 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1885 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1887 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1890 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1892 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1893 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1895 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1896 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1897 error0 = (! good_l4_checksum0
1898 ? IP6_ERROR_UDP_CHECKSUM + type0
1900 error1 = (! good_l4_checksum1
1901 ? IP6_ERROR_UDP_CHECKSUM + type1
1904 /* Drop packets from unroutable hosts. */
1905 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1906 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1908 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1909 error0 = (lm->miss_adj_index == src_adj_index0
1910 ? IP6_ERROR_SRC_LOOKUP_MISS
1913 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1915 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1916 error1 = (lm->miss_adj_index == src_adj_index1
1917 ? IP6_ERROR_SRC_LOOKUP_MISS
1921 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1922 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1924 p0->error = error_node->errors[error0];
1925 p1->error = error_node->errors[error1];
1927 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1928 to_next, n_left_to_next,
1929 pi0, pi1, next0, next1);
1932 while (n_left_from > 0 && n_left_to_next > 0)
1936 udp_header_t * udp0;
1937 u32 pi0, ip_len0, udp_len0, flags0, next0;
1939 u8 error0, type0, good_l4_checksum0;
1941 pi0 = to_next[0] = from[0];
1945 n_left_to_next -= 1;
1947 p0 = vlib_get_buffer (vm, pi0);
1949 ip0 = vlib_buffer_get_current (p0);
1951 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1952 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1956 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1958 udp0 = ip6_next_header (ip0);
1960 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1961 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1963 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1965 /* Verify UDP length. */
1966 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1967 udp_len0 = clib_net_to_host_u16 (udp0->length);
1969 len_diff0 = ip_len0 - udp_len0;
1971 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1973 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1974 && ! good_l4_checksum0
1975 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1977 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1979 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1982 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1984 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1986 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1987 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1988 error0 = (! good_l4_checksum0
1989 ? IP6_ERROR_UDP_CHECKSUM + type0
1992 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1993 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1995 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1996 error0 = (lm->miss_adj_index == src_adj_index0
1997 ? IP6_ERROR_SRC_LOOKUP_MISS
2001 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
2003 p0->error = error_node->errors[error0];
2005 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2006 to_next, n_left_to_next,
2010 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2013 return frame->n_vectors;
2016 VLIB_REGISTER_NODE (ip6_local_node,static) = {
2017 .function = ip6_local,
2018 .name = "ip6-local",
2019 .vector_size = sizeof (u32),
2021 .format_trace = format_ip6_forward_next_trace,
2023 .n_next_nodes = IP_LOCAL_N_NEXT,
2025 [IP_LOCAL_NEXT_DROP] = "error-drop",
2026 [IP_LOCAL_NEXT_PUNT] = "error-punt",
2027 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
2028 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
2032 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local)
2034 void ip6_register_protocol (u32 protocol, u32 node_index)
2036 vlib_main_t * vm = vlib_get_main();
2037 ip6_main_t * im = &ip6_main;
2038 ip_lookup_main_t * lm = &im->lookup_main;
2040 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
2041 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
2045 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
2046 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
2047 IP6_DISCOVER_NEIGHBOR_N_NEXT,
2048 } ip6_discover_neighbor_next_t;
2051 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
2052 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
2053 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
2054 } ip6_discover_neighbor_error_t;
2057 ip6_discover_neighbor (vlib_main_t * vm,
2058 vlib_node_runtime_t * node,
2059 vlib_frame_t * frame)
2061 vnet_main_t * vnm = vnet_get_main();
2062 ip6_main_t * im = &ip6_main;
2063 ip_lookup_main_t * lm = &im->lookup_main;
2064 u32 * from, * to_next_drop;
2065 uword n_left_from, n_left_to_next_drop;
2066 static f64 time_last_seed_change = -1e100;
2067 static u32 hash_seeds[3];
2068 static uword hash_bitmap[256 / BITS (uword)];
2072 if (node->flags & VLIB_NODE_FLAG_TRACE)
2073 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2075 time_now = vlib_time_now (vm);
2076 if (time_now - time_last_seed_change > 1e-3)
2079 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
2080 sizeof (hash_seeds));
2081 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
2082 hash_seeds[i] = r[i];
2084 /* Mark all hash keys as been not-seen before. */
2085 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
2088 time_last_seed_change = time_now;
2091 from = vlib_frame_vector_args (frame);
2092 n_left_from = frame->n_vectors;
2094 while (n_left_from > 0)
2096 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
2097 to_next_drop, n_left_to_next_drop);
2099 while (n_left_from > 0 && n_left_to_next_drop > 0)
2103 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
2105 ip_adjacency_t * adj0;
2106 vnet_hw_interface_t * hw_if0;
2111 p0 = vlib_get_buffer (vm, pi0);
2113 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2115 ip0 = vlib_buffer_get_current (p0);
2117 adj0 = ip_get_adjacency (lm, adj_index0);
2119 if (adj0->arp.next_hop.ip6.as_u64[0] ||
2120 adj0->arp.next_hop.ip6.as_u64[1]) {
2121 ip0->dst_address.as_u64[0] = adj0->arp.next_hop.ip6.as_u64[0];
2122 ip0->dst_address.as_u64[1] = adj0->arp.next_hop.ip6.as_u64[1];
2129 sw_if_index0 = adj0->rewrite_header.sw_if_index;
2130 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
2133 b0 ^= ip0->dst_address.as_u32[0];
2134 c0 ^= ip0->dst_address.as_u32[1];
2136 hash_v3_mix32 (a0, b0, c0);
2138 b0 ^= ip0->dst_address.as_u32[2];
2139 c0 ^= ip0->dst_address.as_u32[3];
2141 hash_v3_finalize32 (a0, b0, c0);
2143 c0 &= BITS (hash_bitmap) - 1;
2144 c0 = c0 / BITS (uword);
2145 m0 = (uword) 1 << (c0 % BITS (uword));
2147 bm0 = hash_bitmap[c0];
2148 drop0 = (bm0 & m0) != 0;
2150 /* Mark it as seen. */
2151 hash_bitmap[c0] = bm0 | m0;
2155 to_next_drop[0] = pi0;
2157 n_left_to_next_drop -= 1;
2159 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2161 /* If the interface is link-down, drop the pkt */
2162 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
2166 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
2167 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
2173 icmp6_neighbor_solicitation_header_t * h0;
2176 h0 = vlib_packet_template_get_packet
2177 (vm, &im->discover_neighbor_packet_template, &bi0);
2180 * Build ethernet header.
2181 * Choose source address based on destination lookup
2184 if (ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
2186 //There is no address on the interface
2187 p0->error = node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
2188 vlib_buffer_free(vm, &bi0, 1);
2193 * Destination address is a solicited node multicast address.
2194 * We need to fill in
2195 * the low 24 bits with low 24 bits of target's address.
2197 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
2198 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
2199 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
2201 h0->neighbor.target_address = ip0->dst_address;
2203 clib_memcpy (h0->link_layer_option.ethernet_address,
2204 hw_if0->hw_address, vec_len (hw_if0->hw_address));
2206 /* $$$$ appears we need this; why is the checksum non-zero? */
2207 h0->neighbor.icmp.checksum = 0;
2208 h0->neighbor.icmp.checksum =
2209 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
2212 ASSERT (bogus_length == 0);
2214 vlib_buffer_copy_trace_flag (vm, p0, bi0);
2215 b0 = vlib_get_buffer (vm, bi0);
2216 vnet_buffer (b0)->sw_if_index[VLIB_TX]
2217 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
2219 /* Add rewrite/encap string. */
2220 vnet_rewrite_one_header (adj0[0], h0,
2221 sizeof (ethernet_header_t));
2222 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
2224 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
2226 vlib_set_next_frame_buffer (vm, node, next0, bi0);
2230 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
2231 n_left_to_next_drop);
2234 return frame->n_vectors;
2237 static char * ip6_discover_neighbor_error_strings[] = {
2238 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
2239 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
2240 = "neighbor solicitations sent",
2241 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
2242 = "no source address for ND solicitation",
2245 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
2246 .function = ip6_discover_neighbor,
2247 .name = "ip6-discover-neighbor",
2248 .vector_size = sizeof (u32),
2250 .format_trace = format_ip6_forward_next_trace,
2252 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
2253 .error_strings = ip6_discover_neighbor_error_strings,
2255 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
2257 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
2258 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
2263 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2265 vnet_main_t * vnm = vnet_get_main();
2266 ip6_main_t * im = &ip6_main;
2267 icmp6_neighbor_solicitation_header_t * h;
2268 ip6_address_t * src;
2269 ip_interface_address_t * ia;
2270 ip_adjacency_t * adj;
2271 vnet_hw_interface_t * hi;
2272 vnet_sw_interface_t * si;
2277 si = vnet_get_sw_interface (vnm, sw_if_index);
2279 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2281 return clib_error_return (0, "%U: interface %U down",
2282 format_ip6_address, dst,
2283 format_vnet_sw_if_index_name, vnm,
2287 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2290 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2291 return clib_error_return
2292 (0, "no matching interface address for destination %U (interface %U)",
2293 format_ip6_address, dst,
2294 format_vnet_sw_if_index_name, vnm, sw_if_index);
2297 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2299 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2301 /* Destination address is a solicited node multicast address. We need to fill in
2302 the low 24 bits with low 24 bits of target's address. */
2303 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2304 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2305 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2307 h->ip.src_address = src[0];
2308 h->neighbor.target_address = dst[0];
2310 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2312 h->neighbor.icmp.checksum =
2313 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2314 ASSERT(bogus_length == 0);
2316 b = vlib_get_buffer (vm, bi);
2317 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2319 /* Add encapsulation string for software interface (e.g. ethernet header). */
2320 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2321 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2322 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2325 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2326 u32 * to_next = vlib_frame_vector_args (f);
2329 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2332 return /* no error */ 0;
2336 IP6_REWRITE_NEXT_DROP,
2337 IP6_REWRITE_NEXT_ICMP_ERROR,
2338 } ip6_rewrite_next_t;
2341 ip6_rewrite_inline (vlib_main_t * vm,
2342 vlib_node_runtime_t * node,
2343 vlib_frame_t * frame,
2344 int rewrite_for_locally_received_packets)
2346 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2347 u32 * from = vlib_frame_vector_args (frame);
2348 u32 n_left_from, n_left_to_next, * to_next, next_index;
2349 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2350 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2352 n_left_from = frame->n_vectors;
2353 next_index = node->cached_next_index;
2354 u32 cpu_index = os_get_cpu_number();
2356 while (n_left_from > 0)
2358 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2360 while (n_left_from >= 4 && n_left_to_next >= 2)
2362 ip_adjacency_t * adj0, * adj1;
2363 vlib_buffer_t * p0, * p1;
2364 ip6_header_t * ip0, * ip1;
2365 u32 pi0, rw_len0, next0, error0, adj_index0;
2366 u32 pi1, rw_len1, next1, error1, adj_index1;
2368 /* Prefetch next iteration. */
2370 vlib_buffer_t * p2, * p3;
2372 p2 = vlib_get_buffer (vm, from[2]);
2373 p3 = vlib_get_buffer (vm, from[3]);
2375 vlib_prefetch_buffer_header (p2, LOAD);
2376 vlib_prefetch_buffer_header (p3, LOAD);
2378 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2379 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2381 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2382 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2385 pi0 = to_next[0] = from[0];
2386 pi1 = to_next[1] = from[1];
2391 n_left_to_next -= 2;
2393 p0 = vlib_get_buffer (vm, pi0);
2394 p1 = vlib_get_buffer (vm, pi1);
2396 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2397 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2399 /* We should never rewrite a pkt using the MISS adjacency */
2400 ASSERT(adj_index0 && adj_index1);
2402 ip0 = vlib_buffer_get_current (p0);
2403 ip1 = vlib_buffer_get_current (p1);
2405 error0 = error1 = IP6_ERROR_NONE;
2406 next0 = next1 = IP6_REWRITE_NEXT_DROP;
2408 if (! rewrite_for_locally_received_packets)
2410 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2412 /* Input node should have reject packets with hop limit 0. */
2413 ASSERT (ip0->hop_limit > 0);
2414 ASSERT (ip1->hop_limit > 0);
2419 ip0->hop_limit = hop_limit0;
2420 ip1->hop_limit = hop_limit1;
2423 * If the hop count drops below 1 when forwarding, generate
2426 if (PREDICT_FALSE(hop_limit0 <= 0))
2428 error0 = IP6_ERROR_TIME_EXPIRED;
2429 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2430 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32)~0;
2431 icmp6_error_set_vnet_buffer(p0, ICMP6_time_exceeded,
2432 ICMP6_time_exceeded_ttl_exceeded_in_transit, 0);
2434 if (PREDICT_FALSE(hop_limit1 <= 0))
2436 error1 = IP6_ERROR_TIME_EXPIRED;
2437 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2438 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32)~0;
2439 icmp6_error_set_vnet_buffer(p1, ICMP6_time_exceeded,
2440 ICMP6_time_exceeded_ttl_exceeded_in_transit, 0);
2444 adj0 = ip_get_adjacency (lm, adj_index0);
2445 adj1 = ip_get_adjacency (lm, adj_index1);
2447 if (rewrite_for_locally_received_packets)
2450 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2451 * we end up here with a local adjacency in hand
2453 if (PREDICT_FALSE(adj0->lookup_next_index
2454 == IP_LOOKUP_NEXT_LOCAL))
2455 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2456 if (PREDICT_FALSE(adj1->lookup_next_index
2457 == IP_LOOKUP_NEXT_LOCAL))
2458 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2461 rw_len0 = adj0[0].rewrite_header.data_bytes;
2462 rw_len1 = adj1[0].rewrite_header.data_bytes;
2464 vlib_increment_combined_counter (&lm->adjacency_counters,
2467 /* packet increment */ 0,
2468 /* byte increment */ rw_len0);
2469 vlib_increment_combined_counter (&lm->adjacency_counters,
2472 /* packet increment */ 0,
2473 /* byte increment */ rw_len1);
2475 /* Check MTU of outgoing interface. */
2476 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2477 ? IP6_ERROR_MTU_EXCEEDED
2479 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2480 ? IP6_ERROR_MTU_EXCEEDED
2483 /* Don't adjust the buffer for hop count issue; icmp-error node
2484 * wants to see the IP headerr */
2485 if (PREDICT_TRUE(error0 == IP6_ERROR_NONE))
2487 p0->current_data -= rw_len0;
2488 p0->current_length += rw_len0;
2490 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
2491 adj0[0].rewrite_header.sw_if_index;
2492 next0 = adj0[0].rewrite_header.next_index;
2494 if (PREDICT_TRUE(error1 == IP6_ERROR_NONE))
2496 p1->current_data -= rw_len1;
2497 p1->current_length += rw_len1;
2499 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
2500 adj1[0].rewrite_header.sw_if_index;
2501 next1 = adj1[0].rewrite_header.next_index;
2504 /* Guess we are only writing on simple Ethernet header. */
2505 vnet_rewrite_two_headers (adj0[0], adj1[0],
2507 sizeof (ethernet_header_t));
2509 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2510 to_next, n_left_to_next,
2511 pi0, pi1, next0, next1);
2514 while (n_left_from > 0 && n_left_to_next > 0)
2516 ip_adjacency_t * adj0;
2520 u32 adj_index0, next0, error0;
2522 pi0 = to_next[0] = from[0];
2524 p0 = vlib_get_buffer (vm, pi0);
2526 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2528 /* We should never rewrite a pkt using the MISS adjacency */
2531 adj0 = ip_get_adjacency (lm, adj_index0);
2533 ip0 = vlib_buffer_get_current (p0);
2535 error0 = IP6_ERROR_NONE;
2536 next0 = IP6_REWRITE_NEXT_DROP;
2538 /* Check hop limit */
2539 if (! rewrite_for_locally_received_packets)
2541 i32 hop_limit0 = ip0->hop_limit;
2543 ASSERT (ip0->hop_limit > 0);
2547 ip0->hop_limit = hop_limit0;
2549 if (PREDICT_FALSE(hop_limit0 <= 0))
2552 * If the hop count drops below 1 when forwarding, generate
2555 error0 = IP6_ERROR_TIME_EXPIRED;
2556 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2557 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32)~0;
2558 icmp6_error_set_vnet_buffer(p0, ICMP6_time_exceeded,
2559 ICMP6_time_exceeded_ttl_exceeded_in_transit, 0);
2563 if (rewrite_for_locally_received_packets)
2565 if (PREDICT_FALSE(adj0->lookup_next_index
2566 == IP_LOOKUP_NEXT_LOCAL))
2567 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2570 /* Guess we are only writing on simple Ethernet header. */
2571 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2573 /* Update packet buffer attributes/set output interface. */
2574 rw_len0 = adj0[0].rewrite_header.data_bytes;
2576 vlib_increment_combined_counter (&lm->adjacency_counters,
2579 /* packet increment */ 0,
2580 /* byte increment */ rw_len0);
2582 /* Check MTU of outgoing interface. */
2583 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2584 ? IP6_ERROR_MTU_EXCEEDED
2587 /* Don't adjust the buffer for hop count issue; icmp-error node
2588 * wants to see the IP headerr */
2589 if (PREDICT_TRUE(error0 == IP6_ERROR_NONE))
2591 p0->current_data -= rw_len0;
2592 p0->current_length += rw_len0;
2594 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
2595 adj0[0].rewrite_header.sw_if_index;
2596 next0 = adj0[0].rewrite_header.next_index;
2599 p0->error = error_node->errors[error0];
2604 n_left_to_next -= 1;
2606 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2607 to_next, n_left_to_next,
2611 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2614 /* Need to do trace after rewrites to pick up new packet data. */
2615 if (node->flags & VLIB_NODE_FLAG_TRACE)
2616 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2618 return frame->n_vectors;
2622 ip6_rewrite_transit (vlib_main_t * vm,
2623 vlib_node_runtime_t * node,
2624 vlib_frame_t * frame)
2626 return ip6_rewrite_inline (vm, node, frame,
2627 /* rewrite_for_locally_received_packets */ 0);
2631 ip6_rewrite_local (vlib_main_t * vm,
2632 vlib_node_runtime_t * node,
2633 vlib_frame_t * frame)
2635 return ip6_rewrite_inline (vm, node, frame,
2636 /* rewrite_for_locally_received_packets */ 1);
2639 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2640 .function = ip6_rewrite_transit,
2641 .name = "ip6-rewrite",
2642 .vector_size = sizeof (u32),
2644 .format_trace = format_ip6_rewrite_trace,
2648 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2649 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2653 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite_transit)
2655 VLIB_REGISTER_NODE (ip6_rewrite_local_node) = {
2656 .function = ip6_rewrite_local,
2657 .name = "ip6-rewrite-local",
2658 .vector_size = sizeof (u32),
2660 .sibling_of = "ip6-rewrite",
2662 .format_trace = format_ip6_rewrite_trace,
2667 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_local_node, ip6_rewrite_local)
2670 * Hop-by-Hop handling
2673 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2675 #define foreach_ip6_hop_by_hop_error \
2676 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2677 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2678 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2681 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2682 foreach_ip6_hop_by_hop_error
2684 IP6_HOP_BY_HOP_N_ERROR,
2685 } ip6_hop_by_hop_error_t;
2688 * Primary h-b-h handler trace support
2689 * We work pretty hard on the problem for obvious reasons
2694 u8 option_data[256];
2695 } ip6_hop_by_hop_trace_t;
2697 vlib_node_registration_t ip6_hop_by_hop_node;
2699 static char * ip6_hop_by_hop_error_strings[] = {
2700 #define _(sym,string) string,
2701 foreach_ip6_hop_by_hop_error
2706 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2708 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2709 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2710 ip6_hop_by_hop_trace_t * t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2711 ip6_hop_by_hop_header_t *hbh0;
2712 ip6_hop_by_hop_option_t *opt0, *limit0;
2713 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2717 hbh0 = (ip6_hop_by_hop_header_t *)t->option_data;
2719 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2720 t->next_index, (hbh0->length+1)<<3, t->trace_len);
2722 opt0 = (ip6_hop_by_hop_option_t *) (hbh0+1);
2723 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *)hbh0) + t->trace_len;
2725 while (opt0 < limit0) {
2728 case 0: /* Pad, just stop */
2729 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
2733 if (hm->trace[type0]) {
2734 s = (*hm->trace[type0])(s, opt0);
2736 s = format (s, "\n unrecognized option %d length %d", type0, opt0->length);
2738 opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
2746 * Process the Hop-by-Hop Options header
2749 ip6_hop_by_hop (vlib_main_t * vm,
2750 vlib_node_runtime_t * node,
2751 vlib_frame_t * frame)
2753 vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, ip6_hop_by_hop_node.index);
2754 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2755 u32 n_left_from, *from, *to_next;
2756 ip_lookup_next_t next_index;
2757 ip6_main_t * im = &ip6_main;
2758 ip_lookup_main_t *lm = &im->lookup_main;
2760 from = vlib_frame_vector_args (frame);
2761 n_left_from = frame->n_vectors;
2762 next_index = node->cached_next_index;
2764 while (n_left_from > 0) {
2767 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2769 while (n_left_from > 0 && n_left_to_next > 0) {
2774 ip6_hop_by_hop_header_t *hbh0;
2775 ip6_hop_by_hop_option_t *opt0, *limit0;
2779 /* Speculatively enqueue b0 to the current next frame */
2785 n_left_to_next -= 1;
2787 b0 = vlib_get_buffer (vm, bi0);
2788 u32 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
2789 ip_adjacency_t *adj0 = ip_get_adjacency(lm, adj_index0);
2790 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2791 next0 = adj0->lookup_next_index;
2793 ip0 = vlib_buffer_get_current (b0);
2794 hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
2795 opt0 = (ip6_hop_by_hop_option_t *)(hbh0+1);
2796 limit0 = (ip6_hop_by_hop_option_t *)((u8 *)hbh0 + ((hbh0->length + 1) << 3));
2799 * Basic validity checks
2801 if ((hbh0->length + 1) << 3 > clib_net_to_host_u16(ip0->payload_length)) {
2802 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2803 next0 = IP_LOOKUP_NEXT_DROP;
2807 /* Scan the set of h-b-h options, process ones that we understand */
2808 while (opt0 < limit0) {
2812 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
2817 if (hm->options[type0]) {
2818 if ((*hm->options[type0])(b0, ip0, opt0) < 0) {
2819 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2823 /* Unrecognized mandatory option, check the two high order bits */
2824 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS) {
2825 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2827 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2828 next0 = IP_LOOKUP_NEXT_DROP;
2830 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2831 next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2832 icmp6_error_set_vnet_buffer(b0, ICMP6_parameter_problem,
2833 ICMP6_parameter_problem_unrecognized_option, (u8 *)opt0 - (u8 *)ip0);
2835 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2836 if (!ip6_address_is_multicast(&ip0->dst_address)) {
2837 next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2838 icmp6_error_set_vnet_buffer(b0, ICMP6_parameter_problem,
2839 ICMP6_parameter_problem_unrecognized_option, (u8 *)opt0 - (u8 *)ip0);
2841 next0 = IP_LOOKUP_NEXT_DROP;
2845 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2849 opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
2853 /* Has the classifier flagged this buffer for special treatment? */
2854 if ((error0 == 0) && (vnet_buffer(b0)->l2_classify.opaque_index == OI_DECAP))
2855 next0 = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2857 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) {
2858 ip6_hop_by_hop_trace_t *t = vlib_add_trace(vm, node, b0, sizeof (*t));
2859 u32 trace_len = (hbh0->length + 1) << 3;
2860 t->next_index = next0;
2861 /* Capture the h-b-h option verbatim */
2862 trace_len = trace_len < ARRAY_LEN(t->option_data) ? trace_len : ARRAY_LEN(t->option_data);
2863 t->trace_len = trace_len;
2864 clib_memcpy(t->option_data, hbh0, trace_len);
2867 b0->error = error_node->errors[error0];
2869 /* verify speculative enqueue, maybe switch current next frame */
2870 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, bi0, next0);
2872 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2874 return frame->n_vectors;
2877 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) = {
2878 .function = ip6_hop_by_hop,
2879 .name = "ip6-hop-by-hop",
2880 .sibling_of = "ip6-lookup",
2881 .vector_size = sizeof (u32),
2882 .format_trace = format_ip6_hop_by_hop_trace,
2883 .type = VLIB_NODE_TYPE_INTERNAL,
2884 .n_errors = ARRAY_LEN(ip6_hop_by_hop_error_strings),
2885 .error_strings = ip6_hop_by_hop_error_strings,
2889 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop)
2891 static clib_error_t *
2892 ip6_hop_by_hop_init (vlib_main_t * vm)
2894 ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2895 memset(hm->options, 0, sizeof(hm->options));
2896 memset(hm->trace, 0, sizeof(hm->trace));
2901 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2904 ip6_hbh_register_option (u8 option,
2905 int options(vlib_buffer_t *b, ip6_header_t *ip, ip6_hop_by_hop_option_t *opt),
2906 u8 *trace(u8 *s, ip6_hop_by_hop_option_t *opt))
2908 ip6_main_t * im = &ip6_main;
2909 ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2911 ASSERT (option < ARRAY_LEN (hm->options));
2913 /* Already registered */
2914 if (hm->options[option])
2917 hm->options[option] = options;
2918 hm->trace[option] = trace;
2920 /* Set global variable */
2921 im->hbh_enabled = 1;
2927 ip6_hbh_unregister_option (u8 option)
2929 ip6_main_t * im = &ip6_main;
2930 ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2932 ASSERT (option < ARRAY_LEN (hm->options));
2934 /* Not registered */
2935 if (!hm->options[option])
2938 hm->options[option] = NULL;
2939 hm->trace[option] = NULL;
2941 /* Disable global knob if this was the last option configured */
2944 for (i = 0; i < 256; i++) {
2945 if (hm->options[option]) {
2951 im->hbh_enabled = 0;
2956 /* Global IP6 main. */
2957 ip6_main_t ip6_main;
2959 static clib_error_t *
2960 ip6_lookup_init (vlib_main_t * vm)
2962 ip6_main_t * im = &ip6_main;
2963 clib_error_t * error;
2966 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2973 for (j = 0; j < i0; j++)
2974 im->fib_masks[i].as_u32[j] = ~0;
2977 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2980 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2982 if (im->lookup_table_nbuckets == 0)
2983 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2985 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2987 if (im->lookup_table_size == 0)
2988 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2990 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2991 im->lookup_table_nbuckets,
2992 im->lookup_table_size);
2994 /* Create FIB with index 0 and table id of 0. */
2995 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2999 pn = pg_get_node (ip6_lookup_node.index);
3000 pn->unformat_edit = unformat_pg_ip6_header;
3003 /* Unless explicitly configured, don't process HBH options */
3004 im->hbh_enabled = 0;
3007 icmp6_neighbor_solicitation_header_t p;
3009 memset (&p, 0, sizeof (p));
3011 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
3012 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
3013 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
3014 p.ip.protocol = IP_PROTOCOL_ICMP6;
3015 p.ip.hop_limit = 255;
3016 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
3018 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
3020 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
3021 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
3023 vlib_packet_template_init (vm,
3024 &im->discover_neighbor_packet_template,
3026 /* alloc chunk size */ 8,
3027 "ip6 neighbor discovery");
3030 error = ip6_feature_init (vm, im);
3035 VLIB_INIT_FUNCTION (ip6_lookup_init);
3037 static clib_error_t *
3038 add_del_ip6_interface_table (vlib_main_t * vm,
3039 unformat_input_t * input,
3040 vlib_cli_command_t * cmd)
3042 vnet_main_t * vnm = vnet_get_main();
3043 clib_error_t * error = 0;
3044 u32 sw_if_index, table_id;
3048 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3050 error = clib_error_return (0, "unknown interface `%U'",
3051 format_unformat_error, input);
3055 if (unformat (input, "%d", &table_id))
3059 error = clib_error_return (0, "expected table id `%U'",
3060 format_unformat_error, input);
3065 ip6_main_t * im = &ip6_main;
3067 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
3071 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
3072 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
3080 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
3081 .path = "set interface ip6 table",
3082 .function = add_del_ip6_interface_table,
3083 .short_help = "set interface ip6 table <intfc> <table-id>"
3087 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
3090 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3091 /* Invert the "u" bit */
3092 ip->as_u8 [8] = mac[0] ^ (1<<1);
3093 ip->as_u8 [9] = mac[1];
3094 ip->as_u8 [10] = mac[2];
3095 ip->as_u8 [11] = 0xFF;
3096 ip->as_u8 [12] = 0xFE;
3097 ip->as_u8 [13] = mac[3];
3098 ip->as_u8 [14] = mac[4];
3099 ip->as_u8 [15] = mac[5];
3103 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
3106 /* Invert the previously inverted "u" bit */
3107 mac[0] = ip->as_u8 [8] ^ (1<<1);
3108 mac[1] = ip->as_u8 [9];
3109 mac[2] = ip->as_u8 [10];
3110 mac[3] = ip->as_u8 [13];
3111 mac[4] = ip->as_u8 [14];
3112 mac[5] = ip->as_u8 [15];
3115 static clib_error_t *
3116 test_ip6_link_command_fn (vlib_main_t * vm,
3117 unformat_input_t * input,
3118 vlib_cli_command_t * cmd)
3121 ip6_address_t _a, *a = &_a;
3123 if (unformat (input, "%U", unformat_ethernet_address, mac))
3125 ip6_link_local_address_from_ethernet_mac_address (a, mac);
3126 vlib_cli_output (vm, "Link local address: %U",
3127 format_ip6_address, a);
3128 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3129 vlib_cli_output (vm, "Original MAC address: %U",
3130 format_ethernet_address, mac);
3136 VLIB_CLI_COMMAND (test_link_command, static) = {
3137 .path = "test ip6 link",
3138 .function = test_ip6_link_command_fn,
3139 .short_help = "test ip6 link <mac-address>",
3142 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3144 ip6_main_t * im6 = &ip6_main;
3146 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
3151 fib = vec_elt_at_index (im6->fibs, p[0]);
3153 fib->flow_hash_config = flow_hash_config;
3157 static clib_error_t *
3158 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3159 unformat_input_t * input,
3160 vlib_cli_command_t * cmd)
3164 u32 flow_hash_config = 0;
3167 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3168 if (unformat (input, "table %d", &table_id))
3171 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3172 foreach_flow_hash_bit
3178 return clib_error_return (0, "unknown input `%U'",
3179 format_unformat_error, input);
3181 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3188 return clib_error_return (0, "no such FIB table %d", table_id);
3191 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3198 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
3199 .path = "set ip6 flow-hash",
3201 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
3202 .function = set_ip6_flow_hash_command_fn,
3205 static clib_error_t *
3206 show_ip6_local_command_fn (vlib_main_t * vm,
3207 unformat_input_t * input,
3208 vlib_cli_command_t * cmd)
3210 ip6_main_t * im = &ip6_main;
3211 ip_lookup_main_t * lm = &im->lookup_main;
3214 vlib_cli_output (vm, "Protocols handled by ip6_local");
3215 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
3217 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3218 vlib_cli_output (vm, "%d", i);
3225 VLIB_CLI_COMMAND (show_ip_local, static) = {
3226 .path = "show ip6 local",
3227 .function = show_ip6_local_command_fn,
3228 .short_help = "Show ip6 local protocol table",
3231 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3234 vnet_main_t * vnm = vnet_get_main();
3235 vnet_interface_main_t * im = &vnm->interface_main;
3236 ip6_main_t * ipm = &ip6_main;
3237 ip_lookup_main_t * lm = &ipm->lookup_main;
3238 vnet_classify_main_t * cm = &vnet_classify_main;
3240 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3241 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3243 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3244 return VNET_API_ERROR_NO_SUCH_ENTRY;
3246 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3247 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
3252 static clib_error_t *
3253 set_ip6_classify_command_fn (vlib_main_t * vm,
3254 unformat_input_t * input,
3255 vlib_cli_command_t * cmd)
3257 u32 table_index = ~0;
3258 int table_index_set = 0;
3259 u32 sw_if_index = ~0;
3262 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3263 if (unformat (input, "table-index %d", &table_index))
3264 table_index_set = 1;
3265 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3266 vnet_get_main(), &sw_if_index))
3272 if (table_index_set == 0)
3273 return clib_error_return (0, "classify table-index must be specified");
3275 if (sw_if_index == ~0)
3276 return clib_error_return (0, "interface / subif must be specified");
3278 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3285 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3286 return clib_error_return (0, "No such interface");
3288 case VNET_API_ERROR_NO_SUCH_ENTRY:
3289 return clib_error_return (0, "No such classifier table");
3294 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
3295 .path = "set ip6 classify",
3297 "set ip6 classify intfc <int> table-index <index>",
3298 .function = set_ip6_classify_command_fn,
3301 static clib_error_t *
3302 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3304 ip6_main_t * im = &ip6_main;
3309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3310 if (unformat (input, "hash-buckets %d", &tmp))
3312 else if (unformat (input, "heap-size %dm", &tmp))
3313 heapsize = ((u64)tmp) << 20;
3314 else if (unformat (input, "heap-size %dM", &tmp))
3315 heapsize = ((u64)tmp) << 20;
3316 else if (unformat (input, "heap-size %dg", &tmp))
3317 heapsize = ((u64)tmp) << 30;
3318 else if (unformat (input, "heap-size %dG", &tmp))
3319 heapsize = ((u64)tmp) << 30;
3321 return clib_error_return (0, "unknown input '%U'",
3322 format_unformat_error, input);
3325 im->lookup_table_nbuckets = nbuckets;
3326 im->lookup_table_size = heapsize;
3331 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");