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_add_del_route_next_hop (ip6_main_t * im,
303 ip6_address_t * dst_address,
304 u32 dst_address_length,
305 ip6_address_t * next_hop,
306 u32 next_hop_sw_if_index,
307 u32 next_hop_weight, u32 adj_index,
308 u32 explicit_fib_index)
310 vnet_main_t * vnm = vnet_get_main();
311 ip_lookup_main_t * lm = &im->lookup_main;
314 ip6_address_t masked_dst_address;
315 u32 old_mp_adj_index, new_mp_adj_index;
316 u32 dst_adj_index, nh_adj_index;
318 ip_adjacency_t * dst_adj;
319 ip_multipath_adjacency_t * old_mp, * new_mp;
320 int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
321 int is_interface_next_hop;
322 clib_error_t * error = 0;
324 BVT(clib_bihash_kv) kv, value;
326 vlib_smp_unsafe_warning();
328 if (explicit_fib_index == (u32)~0)
329 fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
331 fib_index = explicit_fib_index;
333 fib = vec_elt_at_index (im->fibs, fib_index);
335 /* Lookup next hop to be added or deleted. */
336 is_interface_next_hop = ip6_address_is_zero (next_hop);
337 if (adj_index == (u32)~0)
339 if (is_interface_next_hop)
341 nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index,
342 next_hop_sw_if_index);
344 nh_adj_index = *nh_result;
347 ip_adjacency_t * adj;
348 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
350 ip6_adjacency_set_interface_route (vnm, adj,
351 next_hop_sw_if_index, ~0);
352 ip_call_add_del_adjacency_callbacks
353 (lm, next_hop_sw_if_index, /* is_del */ 0);
354 hash_set (im->interface_route_adj_index_by_sw_if_index,
355 next_hop_sw_if_index, nh_adj_index);
360 /* Look for the interface /128 route */
361 kv.key[0] = next_hop->as_u64[0];
362 kv.key[1] = next_hop->as_u64[1];
363 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
365 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
367 ip_adjacency_t * adj;
368 nh_adj_index = ip6_fib_lookup_with_table (im, fib_index, next_hop);
369 adj = ip_get_adjacency (lm, nh_adj_index);
370 /* if ND interface adjacencty is present, we need to
371 install ND adjaceny for specific next hop */
372 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
373 adj->arp.next_hop.ip6.as_u64[0] == 0 &&
374 adj->arp.next_hop.ip6.as_u64[1] == 0)
376 nh_adj_index = vnet_ip6_neighbor_glean_add(fib_index, next_hop);
380 ip_adjacency_t add_adj;
381 memset (&add_adj, 0, sizeof(add_adj));
383 add_adj.lookup_next_index = IP_LOOKUP_NEXT_INDIRECT;
384 add_adj.indirect.next_hop.ip6.as_u64[0] = next_hop->as_u64[0];
385 add_adj.indirect.next_hop.ip6.as_u64[1] = next_hop->as_u64[1];
386 add_adj.explicit_fib_index = explicit_fib_index;
387 ip_add_adjacency (lm, &add_adj, 1, &nh_adj_index);
391 nh_adj_index = value.value;
397 /* Look for the interface /128 route */
398 kv.key[0] = next_hop->as_u64[0];
399 kv.key[1] = next_hop->as_u64[1];
400 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
402 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
404 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
405 error = clib_error_return (0, "next-hop %U/128 not in FIB",
406 format_ip6_address, next_hop);
410 nh_adj_index = value.value;
413 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
414 masked_dst_address = dst_address[0];
415 ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
417 kv.key[0] = masked_dst_address.as_u64[0];
418 kv.key[1] = masked_dst_address.as_u64[1];
419 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
421 rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
425 dst_adj_index = value.value;
426 dst_adj = ip_get_adjacency (lm, dst_adj_index);
430 /* For deletes destination must be known. */
433 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
434 error = clib_error_return (0, "unknown destination %U/%d",
435 format_ip6_address, dst_address,
444 /* Ignore adds of X/128 with next hop of X. */
446 && dst_address_length == 128
447 && ip6_address_is_equal (dst_address, next_hop))
449 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
450 error = clib_error_return (0, "prefix matches next hop %U/%d",
451 format_ip6_address, dst_address,
456 /* Destination is not known and default weight is set so add route
457 to existing non-multipath adjacency */
458 if (dst_adj_index == ~0 && next_hop_weight == 1 && next_hop_sw_if_index == ~0)
460 /* create new adjacency */
461 ip6_add_del_route_args_t a;
462 a.table_index_or_table_id = fib_index;
463 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
464 | IP6_ROUTE_FLAG_FIB_INDEX
465 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
466 | (flags & (IP6_ROUTE_FLAG_NO_REDISTRIBUTE
467 | IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
468 a.dst_address = dst_address[0];
469 a.dst_address_length = dst_address_length;
470 a.adj_index = nh_adj_index;
474 ip6_add_del_route (im, &a);
478 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
480 if (! ip_multipath_adjacency_add_del_next_hop
482 dst_adj ? dst_adj->heap_handle : ~0,
487 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
488 error = clib_error_return
489 (0, "requested deleting next-hop %U not found in multi-path",
490 format_ip6_address, next_hop);
495 if (old_mp_adj_index != ~0)
496 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
497 if (new_mp_adj_index != ~0)
498 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
500 if (old_mp != new_mp)
502 ip6_add_del_route_args_t a;
503 a.table_index_or_table_id = fib_index;
504 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
505 | IP6_ROUTE_FLAG_FIB_INDEX
506 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
507 | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
508 a.dst_address = dst_address[0];
509 a.dst_address_length = dst_address_length;
510 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
514 ip6_add_del_route (im, &a);
519 clib_error_report (error);
523 ip6_get_route (ip6_main_t * im,
524 u32 table_index_or_table_id,
526 ip6_address_t * address,
529 ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
530 ip6_address_t masked_address;
531 BVT(clib_bihash_kv) kv, value;
533 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
534 clib_memcpy (&masked_address, address, sizeof (masked_address));
535 ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
537 kv.key[0] = masked_address.as_u64[0];
538 kv.key[1] = masked_address.as_u64[1];
539 kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
541 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
542 return (value.value);
547 ip6_foreach_matching_route (ip6_main_t * im,
548 u32 table_index_or_table_id,
550 ip6_address_t * dst_address,
552 ip6_address_t ** results,
553 u8 ** result_lengths)
556 find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
557 BVT(clib_bihash) * h = &im->ip6_lookup_table;
558 BVT(clib_bihash_value) * v;
559 clib_bihash_bucket_t * b;
563 _vec_len (*results) = 0;
565 _vec_len (*result_lengths) = 0;
567 /* Walk the table looking for routes which match the supplied address */
568 for (i = 0; i < h->nbuckets; i++)
574 v = BV(clib_bihash_get_value) (h, b->offset);
575 for (j = 0; j < (1<<b->log2_pages); j++)
577 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
579 if (BV(clib_bihash_is_free)(&v->kvp[k]))
582 if ((v->kvp[k].key[2]
583 == (((u64)((fib - im->fibs))<<32) | address_length))
584 && ip6_destination_matches_route
585 (im, dst_address, (ip6_address_t *) &v->kvp[k],
590 a = (ip6_address_t *)(&v->kvp[k]);
592 vec_add1 (*results, a[0]);
593 vec_add1 (*result_lengths, address_length);
601 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
602 u32 table_index_or_table_id,
607 = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
609 ip_lookup_main_t * lm = &im->lookup_main;
611 if (lm->n_adjacency_remaps == 0)
614 clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
616 /* All remaps have been performed. */
617 lm->n_adjacency_remaps = 0;
620 void ip6_delete_matching_routes (ip6_main_t * im,
621 u32 table_index_or_table_id,
623 ip6_address_t * address,
626 /* $$$$ static may be OK - this should happen only on thread 0 */
627 static ip6_address_t * matching_addresses;
628 static u8 * matching_address_lengths;
630 ip6_add_del_route_args_t a;
632 vlib_smp_unsafe_warning();
634 a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
635 a.table_index_or_table_id = table_index_or_table_id;
640 for (l = address_length + 1; l <= 128; l++)
642 ip6_foreach_matching_route (im, table_index_or_table_id, flags,
646 &matching_address_lengths);
647 for (i = 0; i < vec_len (matching_addresses); i++)
649 a.dst_address = matching_addresses[i];
650 a.dst_address_length = matching_address_lengths[i];
651 ip6_add_del_route (im, &a);
655 ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
659 ip6_forward_next_trace (vlib_main_t * vm,
660 vlib_node_runtime_t * node,
661 vlib_frame_t * frame,
662 vlib_rx_or_tx_t which_adj_index);
665 ip6_lookup_inline (vlib_main_t * vm,
666 vlib_node_runtime_t * node,
667 vlib_frame_t * frame,
670 ip6_main_t * im = &ip6_main;
671 ip_lookup_main_t * lm = &im->lookup_main;
672 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
673 u32 n_left_from, n_left_to_next, * from, * to_next;
674 ip_lookup_next_t next;
675 u32 cpu_index = os_get_cpu_number();
677 from = vlib_frame_vector_args (frame);
678 n_left_from = frame->n_vectors;
679 next = node->cached_next_index;
681 while (n_left_from > 0)
683 vlib_get_next_frame (vm, node, next,
684 to_next, n_left_to_next);
686 while (n_left_from >= 4 && n_left_to_next >= 2)
688 vlib_buffer_t * p0, * p1;
689 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
690 ip_lookup_next_t next0, next1;
691 ip6_header_t * ip0, * ip1;
692 ip_adjacency_t * adj0, * adj1;
693 ip6_address_t * dst_addr0, * dst_addr1;
694 u32 fib_index0, fib_index1;
695 u32 flow_hash_config0, flow_hash_config1;
697 /* Prefetch next iteration. */
699 vlib_buffer_t * p2, * p3;
701 p2 = vlib_get_buffer (vm, from[2]);
702 p3 = vlib_get_buffer (vm, from[3]);
704 vlib_prefetch_buffer_header (p2, LOAD);
705 vlib_prefetch_buffer_header (p3, LOAD);
706 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
707 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
710 pi0 = to_next[0] = from[0];
711 pi1 = to_next[1] = from[1];
713 p0 = vlib_get_buffer (vm, pi0);
714 p1 = vlib_get_buffer (vm, pi1);
716 ip0 = vlib_buffer_get_current (p0);
717 ip1 = vlib_buffer_get_current (p1);
721 ip_adjacency_t * iadj0, * iadj1;
722 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
723 iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
724 dst_addr0 = &iadj0->indirect.next_hop.ip6;
725 dst_addr1 = &iadj1->indirect.next_hop.ip6;
729 dst_addr0 = &ip0->dst_address;
730 dst_addr1 = &ip1->dst_address;
733 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
734 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
736 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
737 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
738 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
739 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
741 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
742 adj_index1 = ip6_fib_lookup_with_table (im, fib_index1, dst_addr1);
744 adj0 = ip_get_adjacency (lm, adj_index0);
745 adj1 = ip_get_adjacency (lm, adj_index1);
747 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
749 adj_index0 = ip6_fib_lookup_with_table
750 (im, adj0->explicit_fib_index, dst_addr0);
751 adj0 = ip_get_adjacency (lm, adj_index0);
753 if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
755 adj_index1 = ip6_fib_lookup_with_table
756 (im, adj1->explicit_fib_index, dst_addr1);
757 adj1 = ip_get_adjacency (lm, adj_index1);
760 next0 = adj0->lookup_next_index;
761 next1 = adj1->lookup_next_index;
763 /* Only process the HBH Option Header if explicitly configured to do so */
764 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
765 adj_index0 ? IP_LOOKUP_NEXT_HOP_BY_HOP : adj0->lookup_next_index;
766 next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
767 adj_index1 ? IP_LOOKUP_NEXT_HOP_BY_HOP : adj1->lookup_next_index;
769 vnet_buffer (p0)->ip.flow_hash =
770 vnet_buffer(p1)->ip.flow_hash = 0;
772 if (PREDICT_FALSE(adj0->n_adj > 1))
775 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
776 vnet_buffer (p0)->ip.flow_hash =
777 ip6_compute_flow_hash (ip0, flow_hash_config0);
780 if (PREDICT_FALSE(adj1->n_adj > 1))
783 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
785 vnet_buffer (p1)->ip.flow_hash =
786 ip6_compute_flow_hash (ip1, flow_hash_config1);
789 ASSERT (adj0->n_adj > 0);
790 ASSERT (adj1->n_adj > 0);
791 ASSERT (is_pow2 (adj0->n_adj));
792 ASSERT (is_pow2 (adj1->n_adj));
793 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
794 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
796 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
797 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
799 vlib_increment_combined_counter
800 (cm, cpu_index, adj_index0, 1,
801 vlib_buffer_length_in_chain (vm, p0));
802 vlib_increment_combined_counter
803 (cm, cpu_index, adj_index1, 1,
804 vlib_buffer_length_in_chain (vm, p1));
811 wrong_next = (next0 != next) + 2*(next1 != next);
812 if (PREDICT_FALSE (wrong_next != 0))
821 vlib_set_next_frame_buffer (vm, node, next0, pi0);
828 vlib_set_next_frame_buffer (vm, node, next1, pi1);
835 vlib_set_next_frame_buffer (vm, node, next0, pi0);
836 vlib_set_next_frame_buffer (vm, node, next1, pi1);
840 vlib_put_next_frame (vm, node, next, n_left_to_next);
842 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
848 while (n_left_from > 0 && n_left_to_next > 0)
853 ip_lookup_next_t next0;
854 ip_adjacency_t * adj0;
855 ip6_address_t * dst_addr0;
856 u32 fib_index0, flow_hash_config0;
861 p0 = vlib_get_buffer (vm, pi0);
863 ip0 = vlib_buffer_get_current (p0);
867 ip_adjacency_t * iadj0;
868 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
869 dst_addr0 = &iadj0->indirect.next_hop.ip6;
873 dst_addr0 = &ip0->dst_address;
876 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
877 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
878 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
881 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
883 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
885 adj0 = ip_get_adjacency (lm, adj_index0);
887 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
889 adj_index0 = ip6_fib_lookup_with_table
890 (im, adj0->explicit_fib_index, dst_addr0);
891 adj0 = ip_get_adjacency (lm, adj_index0);
894 /* Only process the HBH Option Header if explicitly configured to do so */
895 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
896 adj_index0 ? IP_LOOKUP_NEXT_HOP_BY_HOP : adj0->lookup_next_index;
898 vnet_buffer (p0)->ip.flow_hash = 0;
900 if (PREDICT_FALSE(adj0->n_adj > 1))
903 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
904 vnet_buffer (p0)->ip.flow_hash =
905 ip6_compute_flow_hash (ip0, flow_hash_config0);
908 ASSERT (adj0->n_adj > 0);
909 ASSERT (is_pow2 (adj0->n_adj));
910 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
912 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
914 vlib_increment_combined_counter
915 (cm, cpu_index, adj_index0, 1,
916 vlib_buffer_length_in_chain (vm, p0));
923 if (PREDICT_FALSE (next0 != next))
926 vlib_put_next_frame (vm, node, next, n_left_to_next);
928 vlib_get_next_frame (vm, node, next,
929 to_next, n_left_to_next);
936 vlib_put_next_frame (vm, node, next, n_left_to_next);
939 if (node->flags & VLIB_NODE_FLAG_TRACE)
940 ip6_forward_next_trace(vm, node, frame, VLIB_TX);
942 return frame->n_vectors;
945 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
946 ip_adjacency_t * adj,
948 u32 if_address_index)
950 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
954 if (hw->hw_class_index == ethernet_hw_interface_class.index
955 || hw->hw_class_index == srp_hw_interface_class.index)
957 n = IP_LOOKUP_NEXT_ARP;
958 node_index = ip6_discover_neighbor_node.index;
959 adj->if_address_index = if_address_index;
960 adj->arp.next_hop.ip6.as_u64[0] = 0;
961 adj->arp.next_hop.ip6.as_u64[1] = 0;
965 n = IP_LOOKUP_NEXT_REWRITE;
966 node_index = ip6_rewrite_node.index;
969 adj->lookup_next_index = n;
970 adj->explicit_fib_index = ~0;
972 vnet_rewrite_for_sw_interface
974 VNET_L3_PACKET_TYPE_IP6,
977 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
978 &adj->rewrite_header,
979 sizeof (adj->rewrite_data));
983 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
984 ip6_main_t * im, u32 fib_index,
985 ip_interface_address_t * a)
987 ip_lookup_main_t * lm = &im->lookup_main;
988 ip_adjacency_t * adj;
989 ip6_address_t * address = ip_interface_address_get_address (lm, a);
990 ip6_add_del_route_args_t x;
991 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
992 u32 classify_table_index;
994 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
995 x.table_index_or_table_id = fib_index;
996 x.flags = (IP6_ROUTE_FLAG_ADD
997 | IP6_ROUTE_FLAG_FIB_INDEX
998 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
999 x.dst_address = address[0];
1000 x.dst_address_length = a->address_length;
1004 a->neighbor_probe_adj_index = ~0;
1005 if (a->address_length < 128)
1007 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1009 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
1010 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1011 ip6_add_del_route (im, &x);
1012 a->neighbor_probe_adj_index = x.adj_index;
1015 /* Add e.g. ::1/128 as local to this host. */
1016 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1019 classify_table_index = ~0;
1020 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
1021 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
1022 if (classify_table_index != (u32) ~0)
1024 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
1025 adj->classify.table_index = classify_table_index;
1028 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1030 adj->if_address_index = a - lm->if_address_pool;
1031 adj->rewrite_header.sw_if_index = sw_if_index;
1032 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
1033 adj->rewrite_header.data_bytes = 0;
1034 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1035 x.dst_address_length = 128;
1036 ip6_add_del_route (im, &x);
1040 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
1041 ip6_address_t * address, u32 address_length)
1043 ip6_add_del_route_args_t x;
1045 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1046 x.table_index_or_table_id = fib_index;
1047 x.flags = (IP6_ROUTE_FLAG_DEL
1048 | IP6_ROUTE_FLAG_FIB_INDEX
1049 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
1050 x.dst_address = address[0];
1051 x.dst_address_length = address_length;
1056 if (address_length < 128)
1058 /* Don't wipe out fe80::0/64 */
1059 if (address_length != 64 ||
1060 address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
1061 ip6_add_del_route (im, &x);
1064 x.dst_address_length = 128;
1065 ip6_add_del_route (im, &x);
1067 ip6_delete_matching_routes (im,
1069 IP6_ROUTE_FLAG_FIB_INDEX,
1076 ip6_address_t address;
1078 } ip6_interface_address_t;
1080 static clib_error_t *
1081 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1083 ip6_address_t * new_address,
1089 static clib_error_t *
1090 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1092 ip6_address_t * address,
1098 vnet_main_t * vnm = vnet_get_main();
1099 ip6_main_t * im = &ip6_main;
1100 ip_lookup_main_t * lm = &im->lookup_main;
1101 clib_error_t * error;
1102 u32 if_address_index;
1103 ip6_address_fib_t ip6_af, * addr_fib = 0;
1105 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1106 ip6_addr_fib_init (&ip6_af, address,
1107 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1108 vec_add1 (addr_fib, ip6_af);
1111 uword elts_before = pool_elts (lm->if_address_pool);
1113 error = ip_interface_address_add_del
1123 /* Pool did not grow: add duplicate address. */
1124 if (elts_before == pool_elts (lm->if_address_pool))
1128 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1131 ip6_del_interface_routes (im, ip6_af.fib_index, address,
1135 ip6_add_interface_routes (vnm, sw_if_index,
1136 im, ip6_af.fib_index,
1137 pool_elt_at_index (lm->if_address_pool, if_address_index));
1141 ip6_add_del_interface_address_callback_t * cb;
1142 vec_foreach (cb, im->add_del_interface_address_callbacks)
1143 cb->function (im, cb->function_opaque, sw_if_index,
1144 address, address_length,
1150 vec_free (addr_fib);
1155 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1156 ip6_address_t * address, u32 address_length,
1159 return ip6_add_del_interface_address_internal
1160 (vm, sw_if_index, address, address_length,
1161 /* redistribute */ 1,
1162 /* insert_routes */ 1,
1167 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1171 ip6_main_t * im = &ip6_main;
1172 ip_interface_address_t * ia;
1174 u32 is_admin_up, fib_index;
1176 /* Fill in lookup tables with default table (0). */
1177 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1179 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1181 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1183 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1185 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1186 0 /* honor unnumbered */,
1188 a = ip_interface_address_get_address (&im->lookup_main, ia);
1190 ip6_add_interface_routes (vnm, sw_if_index,
1194 ip6_del_interface_routes (im, fib_index,
1195 a, ia->address_length);
1201 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1204 ip6_sw_interface_add_del (vnet_main_t * vnm,
1208 vlib_main_t * vm = vnm->vlib_main;
1209 ip6_main_t * im = &ip6_main;
1210 ip_lookup_main_t * lm = &im->lookup_main;
1213 for (cast = 0; cast < VNET_N_CAST; cast++)
1215 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1216 vnet_config_main_t * vcm = &cm->config_main;
1218 /* FIXME multicast. */
1219 if (! vcm->node_index_by_feature_index)
1221 char * start_nodes[] = { "ip6-input", };
1222 char * feature_nodes[] = {
1223 [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1224 [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1225 [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1226 [IP6_RX_FEATURE_VPATH] = "vpath-input-ip6",
1227 [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1229 vnet_config_init (vm, vcm,
1230 start_nodes, ARRAY_LEN (start_nodes),
1231 feature_nodes, ARRAY_LEN (feature_nodes));
1234 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1235 ci = cm->config_index_by_sw_if_index[sw_if_index];
1238 ci = vnet_config_add_feature (vm, vcm,
1240 IP6_RX_FEATURE_LOOKUP,
1241 /* config data */ 0,
1242 /* # bytes of config data */ 0);
1244 ci = vnet_config_del_feature (vm, vcm,
1246 IP6_RX_FEATURE_LOOKUP,
1247 /* config data */ 0,
1248 /* # bytes of config data */ 0);
1250 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1252 return /* no error */ 0;
1255 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1258 ip6_lookup (vlib_main_t * vm,
1259 vlib_node_runtime_t * node,
1260 vlib_frame_t * frame)
1262 return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 0);
1265 static u8 * format_ip6_lookup_trace (u8 * s, va_list * args);
1267 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1268 .function = ip6_lookup,
1269 .name = "ip6-lookup",
1270 .vector_size = sizeof (u32),
1272 .format_trace = format_ip6_lookup_trace,
1274 .n_next_nodes = IP_LOOKUP_N_NEXT,
1275 .next_nodes = IP6_LOOKUP_NEXT_NODES,
1278 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup)
1281 ip6_indirect (vlib_main_t * vm,
1282 vlib_node_runtime_t * node,
1283 vlib_frame_t * frame)
1285 return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 1);
1289 VLIB_REGISTER_NODE (ip6_indirect_node) = {
1290 .function = ip6_indirect,
1291 .name = "ip6-indirect",
1292 .vector_size = sizeof (u32),
1294 .format_trace = format_ip6_lookup_trace,
1296 .n_next_nodes = IP_LOOKUP_N_NEXT,
1297 .next_nodes = IP6_LOOKUP_NEXT_NODES,
1300 VLIB_NODE_FUNCTION_MULTIARCH (ip6_indirect_node, ip6_indirect)
1303 /* Adjacency taken. */
1308 /* Packet data, possibly *after* rewrite. */
1309 u8 packet_data[128 - 1*sizeof(u32)];
1310 } ip6_forward_next_trace_t;
1312 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1314 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1315 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1316 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1317 uword indent = format_get_indent (s);
1319 s = format(s, "%U%U",
1320 format_white_space, indent,
1321 format_ip6_header, t->packet_data);
1325 static u8 * format_ip6_lookup_trace (u8 * s, va_list * args)
1327 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1328 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1329 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1330 vnet_main_t * vnm = vnet_get_main();
1331 ip6_main_t * im = &ip6_main;
1332 uword indent = format_get_indent (s);
1334 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1335 t->fib_index, t->adj_index, format_ip_adjacency,
1336 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1337 s = format(s, "\n%U%U",
1338 format_white_space, indent,
1339 format_ip6_header, t->packet_data);
1344 static u8 * format_ip6_rewrite_trace (u8 * s, va_list * args)
1346 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1347 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1348 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1349 vnet_main_t * vnm = vnet_get_main();
1350 ip6_main_t * im = &ip6_main;
1351 uword indent = format_get_indent (s);
1353 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1354 t->fib_index, t->adj_index, format_ip_adjacency,
1355 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1356 s = format (s, "\n%U%U",
1357 format_white_space, indent,
1358 format_ip_adjacency_packet_data,
1359 vnm, &im->lookup_main, t->adj_index,
1360 t->packet_data, sizeof (t->packet_data));
1364 /* Common trace function for all ip6-forward next nodes. */
1366 ip6_forward_next_trace (vlib_main_t * vm,
1367 vlib_node_runtime_t * node,
1368 vlib_frame_t * frame,
1369 vlib_rx_or_tx_t which_adj_index)
1372 ip6_main_t * im = &ip6_main;
1374 n_left = frame->n_vectors;
1375 from = vlib_frame_vector_args (frame);
1380 vlib_buffer_t * b0, * b1;
1381 ip6_forward_next_trace_t * t0, * t1;
1383 /* Prefetch next iteration. */
1384 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1385 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1390 b0 = vlib_get_buffer (vm, bi0);
1391 b1 = vlib_get_buffer (vm, bi1);
1393 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1395 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1396 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1397 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1398 t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1399 vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1400 vec_elt (im->fib_index_by_sw_if_index,
1401 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1403 clib_memcpy (t0->packet_data,
1404 vlib_buffer_get_current (b0),
1405 sizeof (t0->packet_data));
1407 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1409 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1410 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1411 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1412 t1->fib_index = (vnet_buffer(b1)->sw_if_index[VLIB_TX] != (u32)~0) ?
1413 vnet_buffer(b1)->sw_if_index[VLIB_TX] :
1414 vec_elt (im->fib_index_by_sw_if_index,
1415 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1417 clib_memcpy (t1->packet_data,
1418 vlib_buffer_get_current (b1),
1419 sizeof (t1->packet_data));
1429 ip6_forward_next_trace_t * t0;
1433 b0 = vlib_get_buffer (vm, bi0);
1435 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1437 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1438 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1439 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1440 t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1441 vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1442 vec_elt (im->fib_index_by_sw_if_index,
1443 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1445 clib_memcpy (t0->packet_data,
1446 vlib_buffer_get_current (b0),
1447 sizeof (t0->packet_data));
1455 ip6_drop_or_punt (vlib_main_t * vm,
1456 vlib_node_runtime_t * node,
1457 vlib_frame_t * frame,
1458 ip6_error_t error_code)
1460 u32 * buffers = vlib_frame_vector_args (frame);
1461 uword n_packets = frame->n_vectors;
1463 vlib_error_drop_buffers (vm, node,
1468 ip6_input_node.index,
1471 if (node->flags & VLIB_NODE_FLAG_TRACE)
1472 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1478 ip6_drop (vlib_main_t * vm,
1479 vlib_node_runtime_t * node,
1480 vlib_frame_t * frame)
1481 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1484 ip6_punt (vlib_main_t * vm,
1485 vlib_node_runtime_t * node,
1486 vlib_frame_t * frame)
1487 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1490 ip6_miss (vlib_main_t * vm,
1491 vlib_node_runtime_t * node,
1492 vlib_frame_t * frame)
1493 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1495 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1496 .function = ip6_drop,
1498 .vector_size = sizeof (u32),
1500 .format_trace = format_ip6_forward_next_trace,
1508 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop)
1510 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1511 .function = ip6_punt,
1513 .vector_size = sizeof (u32),
1515 .format_trace = format_ip6_forward_next_trace,
1523 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt)
1525 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1526 .function = ip6_miss,
1528 .vector_size = sizeof (u32),
1530 .format_trace = format_ip6_forward_next_trace,
1538 VLIB_NODE_FUNCTION_MULTIARCH (ip6_miss_node, ip6_miss)
1540 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1541 .function = ip6_drop,
1542 .name = "ip6-multicast",
1543 .vector_size = sizeof (u32),
1545 .format_trace = format_ip6_forward_next_trace,
1553 /* Compute TCP/UDP/ICMP6 checksum in software. */
1554 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1557 u16 sum16, payload_length_host_byte_order;
1558 u32 i, n_this_buffer, n_bytes_left;
1559 u32 headers_size = sizeof(ip0[0]);
1560 void * data_this_buffer;
1562 ASSERT(bogus_lengthp);
1565 /* Initialize checksum with ip header. */
1566 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1567 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1568 data_this_buffer = (void *) (ip0 + 1);
1570 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1572 sum0 = ip_csum_with_carry (sum0,
1573 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1574 sum0 = ip_csum_with_carry (sum0,
1575 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1578 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1579 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1582 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1584 /* validate really icmp6 next */
1585 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1587 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1588 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1590 payload_length_host_byte_order -= skip_bytes;
1591 headers_size += skip_bytes;
1594 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1596 if (p0 && n_this_buffer + headers_size > p0->current_length)
1598 struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1599 u8 nb_segs = mb->nb_segs;
1601 n_this_buffer = (p0->current_length > headers_size ?
1602 p0->current_length - headers_size : 0);
1603 while (n_bytes_left)
1605 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1606 n_bytes_left -= n_this_buffer;
1610 if ((nb_segs == 0) || (mb == 0))
1613 data_this_buffer = rte_ctrlmbuf_data(mb);
1614 n_this_buffer = mb->data_len;
1616 if (n_bytes_left || nb_segs)
1622 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1624 if (p0 && n_this_buffer + headers_size > p0->current_length)
1625 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1628 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1629 n_bytes_left -= n_this_buffer;
1630 if (n_bytes_left == 0)
1633 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1638 p0 = vlib_get_buffer (vm, p0->next_buffer);
1639 data_this_buffer = vlib_buffer_get_current (p0);
1640 n_this_buffer = p0->current_length;
1644 sum16 = ~ ip_csum_fold (sum0);
1649 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1651 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1652 udp_header_t * udp0;
1656 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1657 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1658 || ip0->protocol == IP_PROTOCOL_ICMP6
1659 || ip0->protocol == IP_PROTOCOL_UDP
1660 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1662 udp0 = (void *) (ip0 + 1);
1663 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1665 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1666 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1670 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1672 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1673 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1679 ip6_local (vlib_main_t * vm,
1680 vlib_node_runtime_t * node,
1681 vlib_frame_t * frame)
1683 ip6_main_t * im = &ip6_main;
1684 ip_lookup_main_t * lm = &im->lookup_main;
1685 ip_local_next_t next_index;
1686 u32 * from, * to_next, n_left_from, n_left_to_next;
1687 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1689 from = vlib_frame_vector_args (frame);
1690 n_left_from = frame->n_vectors;
1691 next_index = node->cached_next_index;
1693 if (node->flags & VLIB_NODE_FLAG_TRACE)
1694 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1696 while (n_left_from > 0)
1698 vlib_get_next_frame (vm, node, next_index,
1699 to_next, n_left_to_next);
1701 while (n_left_from >= 4 && n_left_to_next >= 2)
1703 vlib_buffer_t * p0, * p1;
1704 ip6_header_t * ip0, * ip1;
1705 udp_header_t * udp0, * udp1;
1706 u32 pi0, ip_len0, udp_len0, flags0, next0;
1707 u32 pi1, ip_len1, udp_len1, flags1, next1;
1708 i32 len_diff0, len_diff1;
1709 u8 error0, type0, good_l4_checksum0;
1710 u8 error1, type1, good_l4_checksum1;
1712 pi0 = to_next[0] = from[0];
1713 pi1 = to_next[1] = from[1];
1717 n_left_to_next -= 2;
1719 p0 = vlib_get_buffer (vm, pi0);
1720 p1 = vlib_get_buffer (vm, pi1);
1722 ip0 = vlib_buffer_get_current (p0);
1723 ip1 = vlib_buffer_get_current (p1);
1725 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1726 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1728 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1729 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1734 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1735 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1737 udp0 = ip6_next_header (ip0);
1738 udp1 = ip6_next_header (ip1);
1740 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1741 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1742 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1744 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1745 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1747 /* Verify UDP length. */
1748 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1749 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1750 udp_len0 = clib_net_to_host_u16 (udp0->length);
1751 udp_len1 = clib_net_to_host_u16 (udp1->length);
1753 len_diff0 = ip_len0 - udp_len0;
1754 len_diff1 = ip_len1 - udp_len1;
1756 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1757 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1759 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1760 && ! good_l4_checksum0
1761 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1763 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1765 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1767 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1768 && ! good_l4_checksum1
1769 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1771 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1773 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1776 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1778 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1779 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1781 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1782 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1783 error0 = (! good_l4_checksum0
1784 ? IP6_ERROR_UDP_CHECKSUM + type0
1786 error1 = (! good_l4_checksum1
1787 ? IP6_ERROR_UDP_CHECKSUM + type1
1790 /* Drop packets from unroutable hosts. */
1791 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1792 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1794 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1795 error0 = (lm->miss_adj_index == src_adj_index0
1796 ? IP6_ERROR_SRC_LOOKUP_MISS
1799 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1801 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1802 error1 = (lm->miss_adj_index == src_adj_index1
1803 ? IP6_ERROR_SRC_LOOKUP_MISS
1807 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1808 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1810 p0->error = error_node->errors[error0];
1811 p1->error = error_node->errors[error1];
1813 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1814 to_next, n_left_to_next,
1815 pi0, pi1, next0, next1);
1818 while (n_left_from > 0 && n_left_to_next > 0)
1822 udp_header_t * udp0;
1823 u32 pi0, ip_len0, udp_len0, flags0, next0;
1825 u8 error0, type0, good_l4_checksum0;
1827 pi0 = to_next[0] = from[0];
1831 n_left_to_next -= 1;
1833 p0 = vlib_get_buffer (vm, pi0);
1835 ip0 = vlib_buffer_get_current (p0);
1837 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1838 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1842 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1844 udp0 = ip6_next_header (ip0);
1846 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1847 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1849 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1851 /* Verify UDP length. */
1852 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1853 udp_len0 = clib_net_to_host_u16 (udp0->length);
1855 len_diff0 = ip_len0 - udp_len0;
1857 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1859 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1860 && ! good_l4_checksum0
1861 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1863 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1865 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1868 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1870 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1872 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1873 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1874 error0 = (! good_l4_checksum0
1875 ? IP6_ERROR_UDP_CHECKSUM + type0
1878 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1879 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1881 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1882 error0 = (lm->miss_adj_index == src_adj_index0
1883 ? IP6_ERROR_SRC_LOOKUP_MISS
1887 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1889 p0->error = error_node->errors[error0];
1891 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1892 to_next, n_left_to_next,
1896 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1899 return frame->n_vectors;
1902 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1903 .function = ip6_local,
1904 .name = "ip6-local",
1905 .vector_size = sizeof (u32),
1907 .format_trace = format_ip6_forward_next_trace,
1909 .n_next_nodes = IP_LOCAL_N_NEXT,
1911 [IP_LOCAL_NEXT_DROP] = "error-drop",
1912 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1913 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1914 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1918 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local)
1920 void ip6_register_protocol (u32 protocol, u32 node_index)
1922 vlib_main_t * vm = vlib_get_main();
1923 ip6_main_t * im = &ip6_main;
1924 ip_lookup_main_t * lm = &im->lookup_main;
1926 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1927 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1931 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1932 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1933 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1934 } ip6_discover_neighbor_next_t;
1937 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1938 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1939 } ip6_discover_neighbor_error_t;
1942 ip6_discover_neighbor (vlib_main_t * vm,
1943 vlib_node_runtime_t * node,
1944 vlib_frame_t * frame)
1946 vnet_main_t * vnm = vnet_get_main();
1947 ip6_main_t * im = &ip6_main;
1948 ip_lookup_main_t * lm = &im->lookup_main;
1949 u32 * from, * to_next_drop;
1950 uword n_left_from, n_left_to_next_drop;
1951 static f64 time_last_seed_change = -1e100;
1952 static u32 hash_seeds[3];
1953 static uword hash_bitmap[256 / BITS (uword)];
1957 if (node->flags & VLIB_NODE_FLAG_TRACE)
1958 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1960 time_now = vlib_time_now (vm);
1961 if (time_now - time_last_seed_change > 1e-3)
1964 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1965 sizeof (hash_seeds));
1966 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1967 hash_seeds[i] = r[i];
1969 /* Mark all hash keys as been not-seen before. */
1970 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1973 time_last_seed_change = time_now;
1976 from = vlib_frame_vector_args (frame);
1977 n_left_from = frame->n_vectors;
1979 while (n_left_from > 0)
1981 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1982 to_next_drop, n_left_to_next_drop);
1984 while (n_left_from > 0 && n_left_to_next_drop > 0)
1988 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1990 ip_adjacency_t * adj0;
1991 vnet_hw_interface_t * hw_if0;
1996 p0 = vlib_get_buffer (vm, pi0);
1998 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2000 ip0 = vlib_buffer_get_current (p0);
2002 adj0 = ip_get_adjacency (lm, adj_index0);
2004 if (adj0->arp.next_hop.ip6.as_u64[0] ||
2005 adj0->arp.next_hop.ip6.as_u64[1]) {
2006 ip0->dst_address.as_u64[0] = adj0->arp.next_hop.ip6.as_u64[0];
2007 ip0->dst_address.as_u64[1] = adj0->arp.next_hop.ip6.as_u64[1];
2014 sw_if_index0 = adj0->rewrite_header.sw_if_index;
2015 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
2018 b0 ^= ip0->dst_address.as_u32[0];
2019 c0 ^= ip0->dst_address.as_u32[1];
2021 hash_v3_mix32 (a0, b0, c0);
2023 b0 ^= ip0->dst_address.as_u32[2];
2024 c0 ^= ip0->dst_address.as_u32[3];
2026 hash_v3_finalize32 (a0, b0, c0);
2028 c0 &= BITS (hash_bitmap) - 1;
2029 c0 = c0 / BITS (uword);
2030 m0 = (uword) 1 << (c0 % BITS (uword));
2032 bm0 = hash_bitmap[c0];
2033 drop0 = (bm0 & m0) != 0;
2035 /* Mark it as seen. */
2036 hash_bitmap[c0] = bm0 | m0;
2040 to_next_drop[0] = pi0;
2042 n_left_to_next_drop -= 1;
2044 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2046 /* If the interface is link-down, drop the pkt */
2047 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
2051 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
2052 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
2058 icmp6_neighbor_solicitation_header_t * h0;
2061 h0 = vlib_packet_template_get_packet
2062 (vm, &im->discover_neighbor_packet_template, &bi0);
2065 * Build ethernet header.
2066 * Choose source address based on destination lookup
2069 ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
2073 * Destination address is a solicited node multicast address.
2074 * We need to fill in
2075 * the low 24 bits with low 24 bits of target's address.
2077 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
2078 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
2079 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
2081 h0->neighbor.target_address = ip0->dst_address;
2083 clib_memcpy (h0->link_layer_option.ethernet_address,
2084 hw_if0->hw_address, vec_len (hw_if0->hw_address));
2086 /* $$$$ appears we need this; why is the checksum non-zero? */
2087 h0->neighbor.icmp.checksum = 0;
2088 h0->neighbor.icmp.checksum =
2089 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
2092 ASSERT (bogus_length == 0);
2094 vlib_buffer_copy_trace_flag (vm, p0, bi0);
2095 b0 = vlib_get_buffer (vm, bi0);
2096 vnet_buffer (b0)->sw_if_index[VLIB_TX]
2097 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
2099 /* Add rewrite/encap string. */
2100 vnet_rewrite_one_header (adj0[0], h0,
2101 sizeof (ethernet_header_t));
2102 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
2104 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
2106 vlib_set_next_frame_buffer (vm, node, next0, bi0);
2110 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
2111 n_left_to_next_drop);
2114 return frame->n_vectors;
2117 static char * ip6_discover_neighbor_error_strings[] = {
2118 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
2119 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
2120 = "neighbor solicitations sent",
2123 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
2124 .function = ip6_discover_neighbor,
2125 .name = "ip6-discover-neighbor",
2126 .vector_size = sizeof (u32),
2128 .format_trace = format_ip6_forward_next_trace,
2130 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
2131 .error_strings = ip6_discover_neighbor_error_strings,
2133 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
2135 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
2136 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
2141 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2143 vnet_main_t * vnm = vnet_get_main();
2144 ip6_main_t * im = &ip6_main;
2145 icmp6_neighbor_solicitation_header_t * h;
2146 ip6_address_t * src;
2147 ip_interface_address_t * ia;
2148 ip_adjacency_t * adj;
2149 vnet_hw_interface_t * hi;
2150 vnet_sw_interface_t * si;
2155 si = vnet_get_sw_interface (vnm, sw_if_index);
2157 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2159 return clib_error_return (0, "%U: interface %U down",
2160 format_ip6_address, dst,
2161 format_vnet_sw_if_index_name, vnm,
2165 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2168 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2169 return clib_error_return
2170 (0, "no matching interface address for destination %U (interface %U)",
2171 format_ip6_address, dst,
2172 format_vnet_sw_if_index_name, vnm, sw_if_index);
2175 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2177 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2179 /* Destination address is a solicited node multicast address. We need to fill in
2180 the low 24 bits with low 24 bits of target's address. */
2181 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2182 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2183 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2185 h->ip.src_address = src[0];
2186 h->neighbor.target_address = dst[0];
2188 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2190 h->neighbor.icmp.checksum =
2191 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2192 ASSERT(bogus_length == 0);
2194 b = vlib_get_buffer (vm, bi);
2195 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2197 /* Add encapsulation string for software interface (e.g. ethernet header). */
2198 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2199 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2200 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2203 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2204 u32 * to_next = vlib_frame_vector_args (f);
2207 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2210 return /* no error */ 0;
2214 IP6_REWRITE_NEXT_DROP,
2215 } ip6_rewrite_next_t;
2218 ip6_rewrite_inline (vlib_main_t * vm,
2219 vlib_node_runtime_t * node,
2220 vlib_frame_t * frame,
2221 int rewrite_for_locally_received_packets)
2223 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2224 u32 * from = vlib_frame_vector_args (frame);
2225 u32 n_left_from, n_left_to_next, * to_next, next_index;
2226 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2227 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2229 n_left_from = frame->n_vectors;
2230 next_index = node->cached_next_index;
2231 u32 cpu_index = os_get_cpu_number();
2233 while (n_left_from > 0)
2235 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2237 while (n_left_from >= 4 && n_left_to_next >= 2)
2239 ip_adjacency_t * adj0, * adj1;
2240 vlib_buffer_t * p0, * p1;
2241 ip6_header_t * ip0, * ip1;
2242 u32 pi0, rw_len0, next0, error0, adj_index0;
2243 u32 pi1, rw_len1, next1, error1, adj_index1;
2245 /* Prefetch next iteration. */
2247 vlib_buffer_t * p2, * p3;
2249 p2 = vlib_get_buffer (vm, from[2]);
2250 p3 = vlib_get_buffer (vm, from[3]);
2252 vlib_prefetch_buffer_header (p2, LOAD);
2253 vlib_prefetch_buffer_header (p3, LOAD);
2255 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2256 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2258 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2259 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2262 pi0 = to_next[0] = from[0];
2263 pi1 = to_next[1] = from[1];
2268 n_left_to_next -= 2;
2270 p0 = vlib_get_buffer (vm, pi0);
2271 p1 = vlib_get_buffer (vm, pi1);
2273 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2274 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2276 /* We should never rewrite a pkt using the MISS adjacency */
2277 ASSERT(adj_index0 && adj_index1);
2279 ip0 = vlib_buffer_get_current (p0);
2280 ip1 = vlib_buffer_get_current (p1);
2282 error0 = error1 = IP6_ERROR_NONE;
2284 if (! rewrite_for_locally_received_packets)
2286 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2288 /* Input node should have reject packets with hop limit 0. */
2289 ASSERT (ip0->hop_limit > 0);
2290 ASSERT (ip1->hop_limit > 0);
2295 ip0->hop_limit = hop_limit0;
2296 ip1->hop_limit = hop_limit1;
2298 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2299 error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2302 adj0 = ip_get_adjacency (lm, adj_index0);
2303 adj1 = ip_get_adjacency (lm, adj_index1);
2305 if (rewrite_for_locally_received_packets)
2308 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2309 * we end up here with a local adjacency in hand
2311 if (PREDICT_FALSE(adj0->lookup_next_index
2312 == IP_LOOKUP_NEXT_LOCAL))
2313 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2314 if (PREDICT_FALSE(adj1->lookup_next_index
2315 == IP_LOOKUP_NEXT_LOCAL))
2316 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2319 rw_len0 = adj0[0].rewrite_header.data_bytes;
2320 rw_len1 = adj1[0].rewrite_header.data_bytes;
2322 vlib_increment_combined_counter (&lm->adjacency_counters,
2325 /* packet increment */ 0,
2326 /* byte increment */ rw_len0);
2327 vlib_increment_combined_counter (&lm->adjacency_counters,
2330 /* packet increment */ 0,
2331 /* byte increment */ rw_len1);
2333 /* Check MTU of outgoing interface. */
2334 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2335 ? IP6_ERROR_MTU_EXCEEDED
2337 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2338 ? IP6_ERROR_MTU_EXCEEDED
2341 p0->current_data -= rw_len0;
2342 p1->current_data -= rw_len1;
2344 p0->current_length += rw_len0;
2345 p1->current_length += rw_len1;
2347 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2348 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2350 next0 = (error0 == IP6_ERROR_NONE) ?
2351 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2352 next1 = (error1 == IP6_ERROR_NONE) ?
2353 adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2355 /* Guess we are only writing on simple Ethernet header. */
2356 vnet_rewrite_two_headers (adj0[0], adj1[0],
2358 sizeof (ethernet_header_t));
2360 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2361 to_next, n_left_to_next,
2362 pi0, pi1, next0, next1);
2365 while (n_left_from > 0 && n_left_to_next > 0)
2367 ip_adjacency_t * adj0;
2371 u32 adj_index0, next0, error0;
2373 pi0 = to_next[0] = from[0];
2375 p0 = vlib_get_buffer (vm, pi0);
2377 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2379 /* We should never rewrite a pkt using the MISS adjacency */
2382 adj0 = ip_get_adjacency (lm, adj_index0);
2384 ip0 = vlib_buffer_get_current (p0);
2386 error0 = IP6_ERROR_NONE;
2388 /* Check hop limit */
2389 if (! rewrite_for_locally_received_packets)
2391 i32 hop_limit0 = ip0->hop_limit;
2393 ASSERT (ip0->hop_limit > 0);
2397 ip0->hop_limit = hop_limit0;
2399 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2402 if (rewrite_for_locally_received_packets)
2404 if (PREDICT_FALSE(adj0->lookup_next_index
2405 == IP_LOOKUP_NEXT_LOCAL))
2406 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2409 /* Guess we are only writing on simple Ethernet header. */
2410 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2412 /* Update packet buffer attributes/set output interface. */
2413 rw_len0 = adj0[0].rewrite_header.data_bytes;
2415 vlib_increment_combined_counter (&lm->adjacency_counters,
2418 /* packet increment */ 0,
2419 /* byte increment */ rw_len0);
2421 /* Check MTU of outgoing interface. */
2422 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2423 ? IP6_ERROR_MTU_EXCEEDED
2426 p0->current_data -= rw_len0;
2427 p0->current_length += rw_len0;
2428 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2430 next0 = (error0 == IP6_ERROR_NONE) ?
2431 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2433 p0->error = error_node->errors[error0];
2438 n_left_to_next -= 1;
2440 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2441 to_next, n_left_to_next,
2445 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2448 /* Need to do trace after rewrites to pick up new packet data. */
2449 if (node->flags & VLIB_NODE_FLAG_TRACE)
2450 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2452 return frame->n_vectors;
2456 ip6_rewrite_transit (vlib_main_t * vm,
2457 vlib_node_runtime_t * node,
2458 vlib_frame_t * frame)
2460 return ip6_rewrite_inline (vm, node, frame,
2461 /* rewrite_for_locally_received_packets */ 0);
2465 ip6_rewrite_local (vlib_main_t * vm,
2466 vlib_node_runtime_t * node,
2467 vlib_frame_t * frame)
2469 return ip6_rewrite_inline (vm, node, frame,
2470 /* rewrite_for_locally_received_packets */ 1);
2473 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2474 .function = ip6_rewrite_transit,
2475 .name = "ip6-rewrite",
2476 .vector_size = sizeof (u32),
2478 .format_trace = format_ip6_rewrite_trace,
2482 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2486 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite_transit)
2488 VLIB_REGISTER_NODE (ip6_rewrite_local_node) = {
2489 .function = ip6_rewrite_local,
2490 .name = "ip6-rewrite-local",
2491 .vector_size = sizeof (u32),
2493 .sibling_of = "ip6-rewrite",
2495 .format_trace = format_ip6_rewrite_trace,
2500 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_local_node, ip6_rewrite_local)
2503 * Hop-by-Hop handling
2506 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2508 #define foreach_ip6_hop_by_hop_error \
2509 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2510 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2511 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2514 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2515 foreach_ip6_hop_by_hop_error
2517 IP6_HOP_BY_HOP_N_ERROR,
2518 } ip6_hop_by_hop_error_t;
2521 * Primary h-b-h handler trace support
2522 * We work pretty hard on the problem for obvious reasons
2527 u8 option_data[256];
2528 } ip6_hop_by_hop_trace_t;
2530 vlib_node_registration_t ip6_hop_by_hop_node;
2532 static char * ip6_hop_by_hop_error_strings[] = {
2533 #define _(sym,string) string,
2534 foreach_ip6_hop_by_hop_error
2539 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2541 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2542 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2543 ip6_hop_by_hop_trace_t * t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2544 ip6_hop_by_hop_header_t *hbh0;
2545 ip6_hop_by_hop_option_t *opt0, *limit0;
2546 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2550 hbh0 = (ip6_hop_by_hop_header_t *)t->option_data;
2552 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2553 t->next_index, (hbh0->length+1)<<3, t->trace_len);
2555 opt0 = (ip6_hop_by_hop_option_t *) (hbh0+1);
2556 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *)hbh0) + t->trace_len;
2558 while (opt0 < limit0) {
2561 case 0: /* Pad, just stop */
2562 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
2566 if (hm->trace[type0]) {
2567 s = (*hm->trace[type0])(s, opt0);
2569 s = format (s, "\n unrecognized option %d length %d", type0, opt0->length);
2571 opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
2579 * Process the Hop-by-Hop Options header
2582 ip6_hop_by_hop (vlib_main_t * vm,
2583 vlib_node_runtime_t * node,
2584 vlib_frame_t * frame)
2586 vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, ip6_hop_by_hop_node.index);
2587 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2588 u32 n_left_from, *from, *to_next;
2589 ip_lookup_next_t next_index;
2590 ip6_main_t * im = &ip6_main;
2591 ip_lookup_main_t *lm = &im->lookup_main;
2593 from = vlib_frame_vector_args (frame);
2594 n_left_from = frame->n_vectors;
2595 next_index = node->cached_next_index;
2597 while (n_left_from > 0) {
2600 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2602 while (n_left_from > 0 && n_left_to_next > 0) {
2607 ip6_hop_by_hop_header_t *hbh0;
2608 ip6_hop_by_hop_option_t *opt0, *limit0;
2612 /* Speculatively enqueue b0 to the current next frame */
2618 n_left_to_next -= 1;
2620 b0 = vlib_get_buffer (vm, bi0);
2621 u32 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
2622 ip_adjacency_t *adj0 = ip_get_adjacency(lm, adj_index0);
2623 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2624 next0 = adj0->lookup_next_index;
2626 ip0 = vlib_buffer_get_current (b0);
2627 hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
2628 opt0 = (ip6_hop_by_hop_option_t *)(hbh0+1);
2629 limit0 = (ip6_hop_by_hop_option_t *)((u8 *)hbh0 + ((hbh0->length + 1) << 3));
2632 * Basic validity checks
2634 if ((hbh0->length + 1) << 3 > clib_net_to_host_u16(ip0->payload_length)) {
2635 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2636 next0 = IP_LOOKUP_NEXT_DROP;
2640 /* Scan the set of h-b-h options, process ones that we understand */
2641 while (opt0 < limit0) {
2645 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
2650 if (hm->options[type0]) {
2651 if ((*hm->options[type0])(b0, ip0, opt0) < 0) {
2652 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2656 /* Unrecognized mandatory option, check the two high order bits */
2657 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS) {
2658 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2660 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2661 next0 = IP_LOOKUP_NEXT_DROP;
2663 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2664 next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2665 icmp6_error_set_vnet_buffer(b0, ICMP6_parameter_problem,
2666 ICMP6_parameter_problem_unrecognized_option, (u8 *)opt0 - (u8 *)ip0);
2668 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2669 if (!ip6_address_is_multicast(&ip0->dst_address)) {
2670 next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2671 icmp6_error_set_vnet_buffer(b0, ICMP6_parameter_problem,
2672 ICMP6_parameter_problem_unrecognized_option, (u8 *)opt0 - (u8 *)ip0);
2674 next0 = IP_LOOKUP_NEXT_DROP;
2678 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2682 opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
2686 /* Has the classifier flagged this buffer for special treatment? */
2687 if ((error0 == 0) && (vnet_buffer(b0)->l2_classify.opaque_index == OI_DECAP))
2688 next0 = IP_LOOKUP_NEXT_POP_HOP_BY_HOP;
2690 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) {
2691 ip6_hop_by_hop_trace_t *t = vlib_add_trace(vm, node, b0, sizeof (*t));
2692 u32 trace_len = (hbh0->length + 1) << 3;
2693 t->next_index = next0;
2694 /* Capture the h-b-h option verbatim */
2695 trace_len = trace_len < ARRAY_LEN(t->option_data) ? trace_len : ARRAY_LEN(t->option_data);
2696 t->trace_len = trace_len;
2697 clib_memcpy(t->option_data, hbh0, trace_len);
2700 b0->error = error_node->errors[error0];
2702 /* verify speculative enqueue, maybe switch current next frame */
2703 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, bi0, next0);
2705 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2707 return frame->n_vectors;
2710 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) = {
2711 .function = ip6_hop_by_hop,
2712 .name = "ip6-hop-by-hop",
2713 .sibling_of = "ip6-lookup",
2714 .vector_size = sizeof (u32),
2715 .format_trace = format_ip6_hop_by_hop_trace,
2716 .type = VLIB_NODE_TYPE_INTERNAL,
2717 .n_errors = ARRAY_LEN(ip6_hop_by_hop_error_strings),
2718 .error_strings = ip6_hop_by_hop_error_strings,
2722 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop)
2724 static clib_error_t *
2725 ip6_hop_by_hop_init (vlib_main_t * vm)
2727 ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2728 memset(hm->options, 0, sizeof(hm->options));
2729 memset(hm->trace, 0, sizeof(hm->trace));
2734 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2737 ip6_hbh_register_option (u8 option,
2738 int options(vlib_buffer_t *b, ip6_header_t *ip, ip6_hop_by_hop_option_t *opt),
2739 u8 *trace(u8 *s, ip6_hop_by_hop_option_t *opt))
2741 ip6_main_t * im = &ip6_main;
2742 ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2744 ASSERT (option < ARRAY_LEN (hm->options));
2746 /* Already registered */
2747 if (hm->options[option])
2750 hm->options[option] = options;
2751 hm->trace[option] = trace;
2753 /* Set global variable */
2754 im->hbh_enabled = 1;
2760 ip6_hbh_unregister_option (u8 option)
2762 ip6_main_t * im = &ip6_main;
2763 ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2765 ASSERT (option < ARRAY_LEN (hm->options));
2767 /* Not registered */
2768 if (!hm->options[option])
2771 hm->options[option] = NULL;
2772 hm->trace[option] = NULL;
2774 /* Disable global knob if this was the last option configured */
2777 for (i = 0; i < 256; i++) {
2778 if (hm->options[option]) {
2784 im->hbh_enabled = 0;
2789 /* Global IP6 main. */
2790 ip6_main_t ip6_main;
2792 static clib_error_t *
2793 ip6_lookup_init (vlib_main_t * vm)
2795 ip6_main_t * im = &ip6_main;
2798 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2805 for (j = 0; j < i0; j++)
2806 im->fib_masks[i].as_u32[j] = ~0;
2809 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2812 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2814 if (im->lookup_table_nbuckets == 0)
2815 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2817 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2819 if (im->lookup_table_size == 0)
2820 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2822 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2823 im->lookup_table_nbuckets,
2824 im->lookup_table_size);
2826 /* Create FIB with index 0 and table id of 0. */
2827 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2831 pn = pg_get_node (ip6_lookup_node.index);
2832 pn->unformat_edit = unformat_pg_ip6_header;
2835 /* Unless explicitly configured, don't process HBH options */
2836 im->hbh_enabled = 0;
2839 icmp6_neighbor_solicitation_header_t p;
2841 memset (&p, 0, sizeof (p));
2843 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2844 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2845 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2846 p.ip.protocol = IP_PROTOCOL_ICMP6;
2847 p.ip.hop_limit = 255;
2848 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2850 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2852 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2853 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2855 vlib_packet_template_init (vm,
2856 &im->discover_neighbor_packet_template,
2858 /* alloc chunk size */ 8,
2859 "ip6 neighbor discovery");
2865 VLIB_INIT_FUNCTION (ip6_lookup_init);
2867 static clib_error_t *
2868 add_del_ip6_interface_table (vlib_main_t * vm,
2869 unformat_input_t * input,
2870 vlib_cli_command_t * cmd)
2872 vnet_main_t * vnm = vnet_get_main();
2873 clib_error_t * error = 0;
2874 u32 sw_if_index, table_id;
2878 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2880 error = clib_error_return (0, "unknown interface `%U'",
2881 format_unformat_error, input);
2885 if (unformat (input, "%d", &table_id))
2889 error = clib_error_return (0, "expected table id `%U'",
2890 format_unformat_error, input);
2895 ip6_main_t * im = &ip6_main;
2897 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2901 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2902 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2910 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2911 .path = "set interface ip6 table",
2912 .function = add_del_ip6_interface_table,
2913 .short_help = "set interface ip6 table <intfc> <table-id>"
2917 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2920 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2921 /* Invert the "u" bit */
2922 ip->as_u8 [8] = mac[0] ^ (1<<1);
2923 ip->as_u8 [9] = mac[1];
2924 ip->as_u8 [10] = mac[2];
2925 ip->as_u8 [11] = 0xFF;
2926 ip->as_u8 [12] = 0xFE;
2927 ip->as_u8 [13] = mac[3];
2928 ip->as_u8 [14] = mac[4];
2929 ip->as_u8 [15] = mac[5];
2933 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
2936 /* Invert the previously inverted "u" bit */
2937 mac[0] = ip->as_u8 [8] ^ (1<<1);
2938 mac[1] = ip->as_u8 [9];
2939 mac[2] = ip->as_u8 [10];
2940 mac[3] = ip->as_u8 [13];
2941 mac[4] = ip->as_u8 [14];
2942 mac[5] = ip->as_u8 [15];
2945 static clib_error_t *
2946 test_ip6_link_command_fn (vlib_main_t * vm,
2947 unformat_input_t * input,
2948 vlib_cli_command_t * cmd)
2951 ip6_address_t _a, *a = &_a;
2953 if (unformat (input, "%U", unformat_ethernet_address, mac))
2955 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2956 vlib_cli_output (vm, "Link local address: %U",
2957 format_ip6_address, a);
2958 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2959 vlib_cli_output (vm, "Original MAC address: %U",
2960 format_ethernet_address, mac);
2966 VLIB_CLI_COMMAND (test_link_command, static) = {
2967 .path = "test ip6 link",
2968 .function = test_ip6_link_command_fn,
2969 .short_help = "test ip6 link <mac-address>",
2972 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2974 ip6_main_t * im6 = &ip6_main;
2976 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2981 fib = vec_elt_at_index (im6->fibs, p[0]);
2983 fib->flow_hash_config = flow_hash_config;
2987 static clib_error_t *
2988 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2989 unformat_input_t * input,
2990 vlib_cli_command_t * cmd)
2994 u32 flow_hash_config = 0;
2997 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2998 if (unformat (input, "table %d", &table_id))
3001 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3002 foreach_flow_hash_bit
3008 return clib_error_return (0, "unknown input `%U'",
3009 format_unformat_error, input);
3011 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3018 return clib_error_return (0, "no such FIB table %d", table_id);
3021 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3028 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
3029 .path = "set ip6 flow-hash",
3031 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
3032 .function = set_ip6_flow_hash_command_fn,
3035 static clib_error_t *
3036 show_ip6_local_command_fn (vlib_main_t * vm,
3037 unformat_input_t * input,
3038 vlib_cli_command_t * cmd)
3040 ip6_main_t * im = &ip6_main;
3041 ip_lookup_main_t * lm = &im->lookup_main;
3044 vlib_cli_output (vm, "Protocols handled by ip6_local");
3045 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
3047 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3048 vlib_cli_output (vm, "%d", i);
3055 VLIB_CLI_COMMAND (show_ip_local, static) = {
3056 .path = "show ip6 local",
3057 .function = show_ip6_local_command_fn,
3058 .short_help = "Show ip6 local protocol table",
3061 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3064 vnet_main_t * vnm = vnet_get_main();
3065 vnet_interface_main_t * im = &vnm->interface_main;
3066 ip6_main_t * ipm = &ip6_main;
3067 ip_lookup_main_t * lm = &ipm->lookup_main;
3068 vnet_classify_main_t * cm = &vnet_classify_main;
3070 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3071 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3073 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3074 return VNET_API_ERROR_NO_SUCH_ENTRY;
3076 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3077 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
3082 static clib_error_t *
3083 set_ip6_classify_command_fn (vlib_main_t * vm,
3084 unformat_input_t * input,
3085 vlib_cli_command_t * cmd)
3087 u32 table_index = ~0;
3088 int table_index_set = 0;
3089 u32 sw_if_index = ~0;
3092 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3093 if (unformat (input, "table-index %d", &table_index))
3094 table_index_set = 1;
3095 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3096 vnet_get_main(), &sw_if_index))
3102 if (table_index_set == 0)
3103 return clib_error_return (0, "classify table-index must be specified");
3105 if (sw_if_index == ~0)
3106 return clib_error_return (0, "interface / subif must be specified");
3108 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3115 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3116 return clib_error_return (0, "No such interface");
3118 case VNET_API_ERROR_NO_SUCH_ENTRY:
3119 return clib_error_return (0, "No such classifier table");
3124 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
3125 .path = "set ip6 classify",
3127 "set ip6 classify intfc <int> table-index <index>",
3128 .function = set_ip6_classify_command_fn,
3131 static clib_error_t *
3132 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3134 ip6_main_t * im = &ip6_main;
3139 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3140 if (unformat (input, "hash-buckets %d", &tmp))
3142 else if (unformat (input, "heap-size %dm", &tmp))
3143 heapsize = ((u64)tmp) << 20;
3144 else if (unformat (input, "heap-size %dM", &tmp))
3145 heapsize = ((u64)tmp) << 20;
3146 else if (unformat (input, "heap-size %dg", &tmp))
3147 heapsize = ((u64)tmp) << 30;
3148 else if (unformat (input, "heap-size %dG", &tmp))
3149 heapsize = ((u64)tmp) << 30;
3151 return clib_error_return (0, "unknown input '%U'",
3152 format_unformat_error, input);
3155 im->lookup_table_nbuckets = nbuckets;
3156 im->lookup_table_size = heapsize;
3161 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");