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 p = hash_get (im->fib_index_by_table_id, table_index_or_id);
194 return create_fib_with_table_id (im, table_index_or_id);
197 return vec_elt_at_index (im->fibs, fib_index);
200 void ip6_add_del_route (ip6_main_t * im, ip6_add_del_route_args_t * a)
202 ip_lookup_main_t * lm = &im->lookup_main;
204 ip6_address_t dst_address;
205 u32 dst_address_length, adj_index;
207 u32 old_adj_index = ~0;
208 BVT(clib_bihash_kv) kv, value;
210 vlib_smp_unsafe_warning();
212 is_del = (a->flags & IP6_ROUTE_FLAG_DEL) != 0;
214 /* Either create new adjacency or use given one depending on arguments. */
215 if (a->n_add_adj > 0)
217 ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
218 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
221 adj_index = a->adj_index;
223 dst_address = a->dst_address;
224 dst_address_length = a->dst_address_length;
225 fib = find_ip6_fib_by_table_index_or_id (im, a->table_index_or_table_id,
228 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
229 ip6_address_mask (&dst_address, &im->fib_masks[dst_address_length]);
231 /* refcount accounting */
234 ASSERT (im->dst_address_length_refcounts[dst_address_length] > 0);
235 if (--im->dst_address_length_refcounts[dst_address_length] == 0)
237 im->non_empty_dst_address_length_bitmap =
238 clib_bitmap_set (im->non_empty_dst_address_length_bitmap,
239 128 - dst_address_length, 0);
240 compute_prefix_lengths_in_search_order (im);
245 im->dst_address_length_refcounts[dst_address_length]++;
247 im->non_empty_dst_address_length_bitmap =
248 clib_bitmap_set (im->non_empty_dst_address_length_bitmap,
249 128 - dst_address_length, 1);
250 compute_prefix_lengths_in_search_order (im);
253 kv.key[0] = dst_address.as_u64[0];
254 kv.key[1] = dst_address.as_u64[1];
255 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
257 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
258 old_adj_index = value.value;
261 BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 0 /* is_add */);
264 /* Make sure adj index is valid. */
266 (void) ip_get_adjacency (lm, adj_index);
268 kv.value = adj_index;
270 BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 1 /* is_add */);
273 /* Avoid spurious reference count increments */
274 if (old_adj_index == adj_index && !(a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
276 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
277 if (adj->share_count > 0)
281 /* Delete old adjacency index if present and changed. */
283 if (! (a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
284 && old_adj_index != ~0
285 && old_adj_index != adj_index)
286 ip_del_adjacency (lm, old_adj_index);
291 ip6_add_del_route_next_hop (ip6_main_t * im,
293 ip6_address_t * dst_address,
294 u32 dst_address_length,
295 ip6_address_t * next_hop,
296 u32 next_hop_sw_if_index,
297 u32 next_hop_weight, u32 adj_index,
298 u32 explicit_fib_index)
300 vnet_main_t * vnm = vnet_get_main();
301 ip_lookup_main_t * lm = &im->lookup_main;
304 ip6_address_t masked_dst_address;
305 u32 old_mp_adj_index, new_mp_adj_index;
306 u32 dst_adj_index, nh_adj_index;
308 ip_adjacency_t * dst_adj;
309 ip_multipath_adjacency_t * old_mp, * new_mp;
310 int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
311 int is_interface_next_hop;
312 clib_error_t * error = 0;
314 BVT(clib_bihash_kv) kv, value;
316 vlib_smp_unsafe_warning();
318 if (explicit_fib_index == (u32)~0)
319 fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
321 fib_index = explicit_fib_index;
323 fib = vec_elt_at_index (im->fibs, fib_index);
325 /* Lookup next hop to be added or deleted. */
326 is_interface_next_hop = ip6_address_is_zero (next_hop);
327 if (adj_index == (u32)~0)
329 if (is_interface_next_hop)
331 nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index,
332 next_hop_sw_if_index);
334 nh_adj_index = *nh_result;
337 ip_adjacency_t * adj;
338 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
340 ip6_adjacency_set_interface_route (vnm, adj,
341 next_hop_sw_if_index, ~0);
342 ip_call_add_del_adjacency_callbacks
343 (lm, next_hop_sw_if_index, /* is_del */ 0);
344 hash_set (im->interface_route_adj_index_by_sw_if_index,
345 next_hop_sw_if_index, nh_adj_index);
350 /* Look for the interface /128 route */
351 kv.key[0] = next_hop->as_u64[0];
352 kv.key[1] = next_hop->as_u64[1];
353 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
355 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
357 ip_adjacency_t * adj;
358 nh_adj_index = ip6_fib_lookup_with_table (im, fib_index, next_hop);
359 adj = ip_get_adjacency (lm, nh_adj_index);
360 /* if ND interface adjacencty is present, we need to
361 install ND adjaceny for specific next hop */
362 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
363 adj->arp.next_hop.ip6.as_u64[0] == 0 &&
364 adj->arp.next_hop.ip6.as_u64[1] == 0)
366 nh_adj_index = vnet_ip6_neighbor_glean_add(fib_index, next_hop);
370 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
371 error = clib_error_return (0, "next-hop %U/128 not in FIB",
372 format_ip6_address, next_hop);
377 nh_adj_index = value.value;
383 /* Look for the interface /128 route */
384 kv.key[0] = next_hop->as_u64[0];
385 kv.key[1] = next_hop->as_u64[1];
386 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
388 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
390 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
391 error = clib_error_return (0, "next-hop %U/128 not in FIB",
392 format_ip6_address, next_hop);
396 nh_adj_index = value.value;
399 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
400 masked_dst_address = dst_address[0];
401 ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
403 kv.key[0] = masked_dst_address.as_u64[0];
404 kv.key[1] = masked_dst_address.as_u64[1];
405 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
407 rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
411 dst_adj_index = value.value;
412 dst_adj = ip_get_adjacency (lm, dst_adj_index);
416 /* For deletes destination must be known. */
419 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
420 error = clib_error_return (0, "unknown destination %U/%d",
421 format_ip6_address, dst_address,
430 /* Ignore adds of X/128 with next hop of X. */
432 && dst_address_length == 128
433 && ip6_address_is_equal (dst_address, next_hop))
435 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
436 error = clib_error_return (0, "prefix matches next hop %U/%d",
437 format_ip6_address, dst_address,
442 /* Destination is not known and default weight is set so add route
443 to existing non-multipath adjacency */
444 if (dst_adj_index == ~0 && next_hop_weight == 1 && next_hop_sw_if_index == ~0)
446 /* create new adjacency */
447 ip6_add_del_route_args_t a;
448 a.table_index_or_table_id = fib_index;
449 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
450 | IP6_ROUTE_FLAG_FIB_INDEX
451 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
452 | (flags & (IP6_ROUTE_FLAG_NO_REDISTRIBUTE
453 | IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
454 a.dst_address = dst_address[0];
455 a.dst_address_length = dst_address_length;
456 a.adj_index = nh_adj_index;
460 ip6_add_del_route (im, &a);
464 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
466 if (! ip_multipath_adjacency_add_del_next_hop
468 dst_adj ? dst_adj->heap_handle : ~0,
473 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
474 error = clib_error_return
475 (0, "requested deleting next-hop %U not found in multi-path",
476 format_ip6_address, next_hop);
481 if (old_mp_adj_index != ~0)
482 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
483 if (new_mp_adj_index != ~0)
484 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
486 if (old_mp != new_mp)
488 ip6_add_del_route_args_t a;
489 a.table_index_or_table_id = fib_index;
490 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
491 | IP6_ROUTE_FLAG_FIB_INDEX
492 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
493 | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
494 a.dst_address = dst_address[0];
495 a.dst_address_length = dst_address_length;
496 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
500 ip6_add_del_route (im, &a);
505 clib_error_report (error);
509 ip6_get_route (ip6_main_t * im,
510 u32 table_index_or_table_id,
512 ip6_address_t * address,
515 ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
516 ip6_address_t masked_address;
517 BVT(clib_bihash_kv) kv, value;
519 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
520 clib_memcpy (&masked_address, address, sizeof (masked_address));
521 ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
523 kv.key[0] = masked_address.as_u64[0];
524 kv.key[1] = masked_address.as_u64[1];
525 kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
527 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
528 return (value.value);
533 ip6_foreach_matching_route (ip6_main_t * im,
534 u32 table_index_or_table_id,
536 ip6_address_t * dst_address,
538 ip6_address_t ** results,
539 u8 ** result_lengths)
542 find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
543 BVT(clib_bihash) * h = &im->ip6_lookup_table;
544 BVT(clib_bihash_value) * v;
545 clib_bihash_bucket_t * b;
549 _vec_len (*results) = 0;
551 _vec_len (*result_lengths) = 0;
553 /* Walk the table looking for routes which match the supplied address */
554 for (i = 0; i < h->nbuckets; i++)
560 v = BV(clib_bihash_get_value) (h, b->offset);
561 for (j = 0; j < (1<<b->log2_pages); j++)
563 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
565 if (BV(clib_bihash_is_free)(&v->kvp[k]))
568 if ((v->kvp[k].key[2]
569 == (((u64)((fib - im->fibs))<<32) | address_length))
570 && ip6_destination_matches_route
571 (im, dst_address, (ip6_address_t *) &v->kvp[k],
576 a = (ip6_address_t *)(&v->kvp[k]);
578 vec_add1 (*results, a[0]);
579 vec_add1 (*result_lengths, address_length);
587 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
588 u32 table_index_or_table_id,
593 = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
595 ip_lookup_main_t * lm = &im->lookup_main;
597 if (lm->n_adjacency_remaps == 0)
600 clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
602 /* All remaps have been performed. */
603 lm->n_adjacency_remaps = 0;
606 void ip6_delete_matching_routes (ip6_main_t * im,
607 u32 table_index_or_table_id,
609 ip6_address_t * address,
612 /* $$$$ static may be OK - this should happen only on thread 0 */
613 static ip6_address_t * matching_addresses;
614 static u8 * matching_address_lengths;
616 ip6_add_del_route_args_t a;
618 vlib_smp_unsafe_warning();
620 a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
621 a.table_index_or_table_id = table_index_or_table_id;
626 for (l = address_length + 1; l <= 128; l++)
628 ip6_foreach_matching_route (im, table_index_or_table_id, flags,
632 &matching_address_lengths);
633 for (i = 0; i < vec_len (matching_addresses); i++)
635 a.dst_address = matching_addresses[i];
636 a.dst_address_length = matching_address_lengths[i];
637 ip6_add_del_route (im, &a);
641 ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
645 ip6_lookup (vlib_main_t * vm,
646 vlib_node_runtime_t * node,
647 vlib_frame_t * frame)
649 ip6_main_t * im = &ip6_main;
650 ip_lookup_main_t * lm = &im->lookup_main;
651 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
652 u32 n_left_from, n_left_to_next, * from, * to_next;
653 ip_lookup_next_t next;
654 u32 cpu_index = os_get_cpu_number();
656 from = vlib_frame_vector_args (frame);
657 n_left_from = frame->n_vectors;
658 next = node->cached_next_index;
660 while (n_left_from > 0)
662 vlib_get_next_frame (vm, node, next,
663 to_next, n_left_to_next);
665 while (n_left_from >= 4 && n_left_to_next >= 2)
667 vlib_buffer_t * p0, * p1;
668 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
669 ip_lookup_next_t next0, next1;
670 ip6_header_t * ip0, * ip1;
671 ip_adjacency_t * adj0, * adj1;
672 u32 fib_index0, fib_index1;
673 u32 flow_hash_config0, flow_hash_config1;
675 /* Prefetch next iteration. */
677 vlib_buffer_t * p2, * p3;
679 p2 = vlib_get_buffer (vm, from[2]);
680 p3 = vlib_get_buffer (vm, from[3]);
682 vlib_prefetch_buffer_header (p2, LOAD);
683 vlib_prefetch_buffer_header (p3, LOAD);
684 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
685 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
688 pi0 = to_next[0] = from[0];
689 pi1 = to_next[1] = from[1];
691 p0 = vlib_get_buffer (vm, pi0);
692 p1 = vlib_get_buffer (vm, pi1);
694 ip0 = vlib_buffer_get_current (p0);
695 ip1 = vlib_buffer_get_current (p1);
697 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
698 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
700 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
701 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
702 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
703 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
705 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
707 adj_index1 = ip6_fib_lookup_with_table (im, fib_index1,
710 adj0 = ip_get_adjacency (lm, adj_index0);
711 adj1 = ip_get_adjacency (lm, adj_index1);
713 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
715 adj_index0 = ip6_fib_lookup_with_table
716 (im, adj0->explicit_fib_index, &ip0->dst_address);
717 adj0 = ip_get_adjacency (lm, adj_index0);
719 if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
721 adj_index1 = ip6_fib_lookup_with_table
722 (im, adj1->explicit_fib_index, &ip1->dst_address);
723 adj1 = ip_get_adjacency (lm, adj_index1);
726 next0 = adj0->lookup_next_index;
727 next1 = adj1->lookup_next_index;
729 /* Process hop-by-hop options if present */
730 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
731 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
732 next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
733 IP_LOOKUP_NEXT_HOP_BY_HOP : next1;
735 vnet_buffer (p0)->ip.flow_hash =
736 vnet_buffer(p1)->ip.flow_hash = 0;
738 if (PREDICT_FALSE(adj0->n_adj > 1))
741 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
742 vnet_buffer (p0)->ip.flow_hash =
743 ip6_compute_flow_hash (ip0, flow_hash_config0);
746 if (PREDICT_FALSE(adj1->n_adj > 1))
749 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
751 vnet_buffer (p1)->ip.flow_hash =
752 ip6_compute_flow_hash (ip1, flow_hash_config1);
755 ASSERT (adj0->n_adj > 0);
756 ASSERT (adj1->n_adj > 0);
757 ASSERT (is_pow2 (adj0->n_adj));
758 ASSERT (is_pow2 (adj1->n_adj));
759 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
760 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
762 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
763 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
765 vlib_increment_combined_counter
766 (cm, cpu_index, adj_index0, 1,
767 vlib_buffer_length_in_chain (vm, p0));
768 vlib_increment_combined_counter
769 (cm, cpu_index, adj_index1, 1,
770 vlib_buffer_length_in_chain (vm, p1));
777 wrong_next = (next0 != next) + 2*(next1 != next);
778 if (PREDICT_FALSE (wrong_next != 0))
787 vlib_set_next_frame_buffer (vm, node, next0, pi0);
794 vlib_set_next_frame_buffer (vm, node, next1, pi1);
801 vlib_set_next_frame_buffer (vm, node, next0, pi0);
802 vlib_set_next_frame_buffer (vm, node, next1, pi1);
806 vlib_put_next_frame (vm, node, next, n_left_to_next);
808 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
814 while (n_left_from > 0 && n_left_to_next > 0)
819 ip_lookup_next_t next0;
820 ip_adjacency_t * adj0;
821 u32 fib_index0, flow_hash_config0;
826 p0 = vlib_get_buffer (vm, pi0);
828 ip0 = vlib_buffer_get_current (p0);
830 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
831 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
832 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
835 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
837 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
840 adj0 = ip_get_adjacency (lm, adj_index0);
842 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
844 adj_index0 = ip6_fib_lookup_with_table
845 (im, adj0->explicit_fib_index, &ip0->dst_address);
846 adj0 = ip_get_adjacency (lm, adj_index0);
849 next0 = adj0->lookup_next_index;
850 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
851 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
853 vnet_buffer (p0)->ip.flow_hash = 0;
855 if (PREDICT_FALSE(adj0->n_adj > 1))
858 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
859 vnet_buffer (p0)->ip.flow_hash =
860 ip6_compute_flow_hash (ip0, flow_hash_config0);
863 ASSERT (adj0->n_adj > 0);
864 ASSERT (is_pow2 (adj0->n_adj));
865 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
867 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
869 vlib_increment_combined_counter
870 (cm, cpu_index, adj_index0, 1,
871 vlib_buffer_length_in_chain (vm, p0));
878 if (PREDICT_FALSE (next0 != next))
881 vlib_put_next_frame (vm, node, next, n_left_to_next);
883 vlib_get_next_frame (vm, node, next,
884 to_next, n_left_to_next);
891 vlib_put_next_frame (vm, node, next, n_left_to_next);
894 return frame->n_vectors;
897 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
898 ip_adjacency_t * adj,
900 u32 if_address_index)
902 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
906 if (hw->hw_class_index == ethernet_hw_interface_class.index
907 || hw->hw_class_index == srp_hw_interface_class.index)
909 n = IP_LOOKUP_NEXT_ARP;
910 node_index = ip6_discover_neighbor_node.index;
911 adj->if_address_index = if_address_index;
912 adj->arp.next_hop.ip6.as_u64[0] = 0;
913 adj->arp.next_hop.ip6.as_u64[1] = 0;
917 n = IP_LOOKUP_NEXT_REWRITE;
918 node_index = ip6_rewrite_node.index;
921 adj->lookup_next_index = n;
922 adj->explicit_fib_index = ~0;
924 vnet_rewrite_for_sw_interface
926 VNET_L3_PACKET_TYPE_IP6,
929 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
930 &adj->rewrite_header,
931 sizeof (adj->rewrite_data));
935 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
936 ip6_main_t * im, u32 fib_index,
937 ip_interface_address_t * a)
939 ip_lookup_main_t * lm = &im->lookup_main;
940 ip_adjacency_t * adj;
941 ip6_address_t * address = ip_interface_address_get_address (lm, a);
942 ip6_add_del_route_args_t x;
943 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
944 u32 classify_table_index;
946 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
947 x.table_index_or_table_id = fib_index;
948 x.flags = (IP6_ROUTE_FLAG_ADD
949 | IP6_ROUTE_FLAG_FIB_INDEX
950 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
951 x.dst_address = address[0];
952 x.dst_address_length = a->address_length;
956 a->neighbor_probe_adj_index = ~0;
957 if (a->address_length < 128)
959 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
961 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
962 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
963 ip6_add_del_route (im, &x);
964 a->neighbor_probe_adj_index = x.adj_index;
967 /* Add e.g. ::1/128 as local to this host. */
968 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
971 classify_table_index = ~0;
972 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
973 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
974 if (classify_table_index != (u32) ~0)
976 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
977 adj->classify.table_index = classify_table_index;
980 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
982 adj->if_address_index = a - lm->if_address_pool;
983 adj->rewrite_header.sw_if_index = sw_if_index;
984 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
985 adj->rewrite_header.data_bytes = 0;
986 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
987 x.dst_address_length = 128;
988 ip6_add_del_route (im, &x);
992 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
993 ip6_address_t * address, u32 address_length)
995 ip6_add_del_route_args_t x;
997 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
998 x.table_index_or_table_id = fib_index;
999 x.flags = (IP6_ROUTE_FLAG_DEL
1000 | IP6_ROUTE_FLAG_FIB_INDEX
1001 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
1002 x.dst_address = address[0];
1003 x.dst_address_length = address_length;
1008 if (address_length < 128)
1010 /* Don't wipe out fe80::0/64 */
1011 if (address_length != 64 ||
1012 address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
1013 ip6_add_del_route (im, &x);
1016 x.dst_address_length = 128;
1017 ip6_add_del_route (im, &x);
1019 ip6_delete_matching_routes (im,
1021 IP6_ROUTE_FLAG_FIB_INDEX,
1028 ip6_address_t address;
1030 } ip6_interface_address_t;
1032 static clib_error_t *
1033 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1035 ip6_address_t * new_address,
1041 static clib_error_t *
1042 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1044 ip6_address_t * address,
1050 vnet_main_t * vnm = vnet_get_main();
1051 ip6_main_t * im = &ip6_main;
1052 ip_lookup_main_t * lm = &im->lookup_main;
1053 clib_error_t * error;
1054 u32 if_address_index;
1055 ip6_address_fib_t ip6_af, * addr_fib = 0;
1057 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1058 ip6_addr_fib_init (&ip6_af, address,
1059 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1060 vec_add1 (addr_fib, ip6_af);
1063 uword elts_before = pool_elts (lm->if_address_pool);
1065 error = ip_interface_address_add_del
1075 /* Pool did not grow: add duplicate address. */
1076 if (elts_before == pool_elts (lm->if_address_pool))
1080 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1083 ip6_del_interface_routes (im, ip6_af.fib_index, address,
1087 ip6_add_interface_routes (vnm, sw_if_index,
1088 im, ip6_af.fib_index,
1089 pool_elt_at_index (lm->if_address_pool, if_address_index));
1093 ip6_add_del_interface_address_callback_t * cb;
1094 vec_foreach (cb, im->add_del_interface_address_callbacks)
1095 cb->function (im, cb->function_opaque, sw_if_index,
1096 address, address_length,
1102 vec_free (addr_fib);
1107 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1108 ip6_address_t * address, u32 address_length,
1111 return ip6_add_del_interface_address_internal
1112 (vm, sw_if_index, address, address_length,
1113 /* redistribute */ 1,
1114 /* insert_routes */ 1,
1119 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1123 ip6_main_t * im = &ip6_main;
1124 ip_interface_address_t * ia;
1126 u32 is_admin_up, fib_index;
1128 /* Fill in lookup tables with default table (0). */
1129 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1131 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1133 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1135 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1137 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1138 0 /* honor unnumbered */,
1140 a = ip_interface_address_get_address (&im->lookup_main, ia);
1142 ip6_add_interface_routes (vnm, sw_if_index,
1146 ip6_del_interface_routes (im, fib_index,
1147 a, ia->address_length);
1153 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1156 ip6_sw_interface_add_del (vnet_main_t * vnm,
1160 vlib_main_t * vm = vnm->vlib_main;
1161 ip6_main_t * im = &ip6_main;
1162 ip_lookup_main_t * lm = &im->lookup_main;
1165 for (cast = 0; cast < VNET_N_CAST; cast++)
1167 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1168 vnet_config_main_t * vcm = &cm->config_main;
1170 /* FIXME multicast. */
1171 if (! vcm->node_index_by_feature_index)
1173 char * start_nodes[] = { "ip6-input", };
1174 char * feature_nodes[] = {
1175 [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1176 [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1177 [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1178 [IP6_RX_FEATURE_VPATH] = "vpath-input-ip6",
1179 [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1181 vnet_config_init (vm, vcm,
1182 start_nodes, ARRAY_LEN (start_nodes),
1183 feature_nodes, ARRAY_LEN (feature_nodes));
1186 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1187 ci = cm->config_index_by_sw_if_index[sw_if_index];
1190 ci = vnet_config_add_feature (vm, vcm,
1192 IP6_RX_FEATURE_LOOKUP,
1193 /* config data */ 0,
1194 /* # bytes of config data */ 0);
1196 ci = vnet_config_del_feature (vm, vcm,
1198 IP6_RX_FEATURE_LOOKUP,
1199 /* config data */ 0,
1200 /* # bytes of config data */ 0);
1202 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1204 return /* no error */ 0;
1207 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1209 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1210 .function = ip6_lookup,
1211 .name = "ip6-lookup",
1212 .vector_size = sizeof (u32),
1214 .n_next_nodes = IP_LOOKUP_N_NEXT,
1215 .next_nodes = IP6_LOOKUP_NEXT_NODES,
1219 /* Adjacency taken. */
1224 /* Packet data, possibly *after* rewrite. */
1225 u8 packet_data[128 - 1*sizeof(u32)];
1226 } ip6_forward_next_trace_t;
1228 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1230 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1231 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1232 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1233 vnet_main_t * vnm = vnet_get_main();
1234 ip6_main_t * im = &ip6_main;
1235 ip_adjacency_t * adj;
1236 uword indent = format_get_indent (s);
1238 adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1239 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1240 t->fib_index, t->adj_index, format_ip_adjacency,
1241 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1242 switch (adj->lookup_next_index)
1244 case IP_LOOKUP_NEXT_REWRITE:
1245 s = format (s, "\n%U%U",
1246 format_white_space, indent,
1247 format_ip_adjacency_packet_data,
1248 vnm, &im->lookup_main, t->adj_index,
1249 t->packet_data, sizeof (t->packet_data));
1259 /* Common trace function for all ip6-forward next nodes. */
1261 ip6_forward_next_trace (vlib_main_t * vm,
1262 vlib_node_runtime_t * node,
1263 vlib_frame_t * frame,
1264 vlib_rx_or_tx_t which_adj_index)
1267 ip6_main_t * im = &ip6_main;
1269 n_left = frame->n_vectors;
1270 from = vlib_frame_vector_args (frame);
1275 vlib_buffer_t * b0, * b1;
1276 ip6_forward_next_trace_t * t0, * t1;
1278 /* Prefetch next iteration. */
1279 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1280 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1285 b0 = vlib_get_buffer (vm, bi0);
1286 b1 = vlib_get_buffer (vm, bi1);
1288 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1290 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1291 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1292 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1293 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1294 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1295 clib_memcpy (t0->packet_data,
1296 vlib_buffer_get_current (b0),
1297 sizeof (t0->packet_data));
1299 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1301 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1302 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1303 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1304 t1->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1305 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1306 clib_memcpy (t1->packet_data,
1307 vlib_buffer_get_current (b1),
1308 sizeof (t1->packet_data));
1318 ip6_forward_next_trace_t * t0;
1322 b0 = vlib_get_buffer (vm, bi0);
1324 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1326 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1327 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1328 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1329 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1330 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1331 clib_memcpy (t0->packet_data,
1332 vlib_buffer_get_current (b0),
1333 sizeof (t0->packet_data));
1341 ip6_drop_or_punt (vlib_main_t * vm,
1342 vlib_node_runtime_t * node,
1343 vlib_frame_t * frame,
1344 ip6_error_t error_code)
1346 u32 * buffers = vlib_frame_vector_args (frame);
1347 uword n_packets = frame->n_vectors;
1349 vlib_error_drop_buffers (vm, node,
1354 ip6_input_node.index,
1357 if (node->flags & VLIB_NODE_FLAG_TRACE)
1358 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1364 ip6_drop (vlib_main_t * vm,
1365 vlib_node_runtime_t * node,
1366 vlib_frame_t * frame)
1367 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1370 ip6_punt (vlib_main_t * vm,
1371 vlib_node_runtime_t * node,
1372 vlib_frame_t * frame)
1373 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1376 ip6_miss (vlib_main_t * vm,
1377 vlib_node_runtime_t * node,
1378 vlib_frame_t * frame)
1379 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1381 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1382 .function = ip6_drop,
1384 .vector_size = sizeof (u32),
1386 .format_trace = format_ip6_forward_next_trace,
1394 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1395 .function = ip6_punt,
1397 .vector_size = sizeof (u32),
1399 .format_trace = format_ip6_forward_next_trace,
1407 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1408 .function = ip6_miss,
1410 .vector_size = sizeof (u32),
1412 .format_trace = format_ip6_forward_next_trace,
1420 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1421 .function = ip6_drop,
1422 .name = "ip6-multicast",
1423 .vector_size = sizeof (u32),
1425 .format_trace = format_ip6_forward_next_trace,
1433 /* Compute TCP/UDP/ICMP6 checksum in software. */
1434 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1437 u16 sum16, payload_length_host_byte_order;
1438 u32 i, n_this_buffer, n_bytes_left;
1439 u32 headers_size = sizeof(ip0[0]);
1440 void * data_this_buffer;
1442 ASSERT(bogus_lengthp);
1445 /* Initialize checksum with ip header. */
1446 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1447 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1448 data_this_buffer = (void *) (ip0 + 1);
1450 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1452 sum0 = ip_csum_with_carry (sum0,
1453 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1454 sum0 = ip_csum_with_carry (sum0,
1455 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1458 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1459 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1462 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1464 /* validate really icmp6 next */
1465 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1467 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1468 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1470 payload_length_host_byte_order -= skip_bytes;
1471 headers_size += skip_bytes;
1474 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1476 if (p0 && n_this_buffer + headers_size > p0->current_length)
1478 struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1479 u8 nb_segs = mb->nb_segs;
1481 n_this_buffer = (p0->current_length > headers_size ?
1482 p0->current_length - headers_size : 0);
1483 while (n_bytes_left)
1485 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1486 n_bytes_left -= n_this_buffer;
1490 if ((nb_segs == 0) || (mb == 0))
1493 data_this_buffer = rte_ctrlmbuf_data(mb);
1494 n_this_buffer = mb->data_len;
1496 if (n_bytes_left || nb_segs)
1502 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1504 if (p0 && n_this_buffer + headers_size > p0->current_length)
1505 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1508 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1509 n_bytes_left -= n_this_buffer;
1510 if (n_bytes_left == 0)
1513 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1518 p0 = vlib_get_buffer (vm, p0->next_buffer);
1519 data_this_buffer = vlib_buffer_get_current (p0);
1520 n_this_buffer = p0->current_length;
1524 sum16 = ~ ip_csum_fold (sum0);
1529 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1531 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1532 udp_header_t * udp0;
1536 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1537 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1538 || ip0->protocol == IP_PROTOCOL_ICMP6
1539 || ip0->protocol == IP_PROTOCOL_UDP
1540 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1542 udp0 = (void *) (ip0 + 1);
1543 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1545 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1546 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1550 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1552 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1553 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1559 ip6_local (vlib_main_t * vm,
1560 vlib_node_runtime_t * node,
1561 vlib_frame_t * frame)
1563 ip6_main_t * im = &ip6_main;
1564 ip_lookup_main_t * lm = &im->lookup_main;
1565 ip_local_next_t next_index;
1566 u32 * from, * to_next, n_left_from, n_left_to_next;
1567 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1569 from = vlib_frame_vector_args (frame);
1570 n_left_from = frame->n_vectors;
1571 next_index = node->cached_next_index;
1573 if (node->flags & VLIB_NODE_FLAG_TRACE)
1574 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1576 while (n_left_from > 0)
1578 vlib_get_next_frame (vm, node, next_index,
1579 to_next, n_left_to_next);
1581 while (n_left_from >= 4 && n_left_to_next >= 2)
1583 vlib_buffer_t * p0, * p1;
1584 ip6_header_t * ip0, * ip1;
1585 udp_header_t * udp0, * udp1;
1586 u32 pi0, ip_len0, udp_len0, flags0, next0;
1587 u32 pi1, ip_len1, udp_len1, flags1, next1;
1588 i32 len_diff0, len_diff1;
1589 u8 error0, type0, good_l4_checksum0;
1590 u8 error1, type1, good_l4_checksum1;
1592 pi0 = to_next[0] = from[0];
1593 pi1 = to_next[1] = from[1];
1597 n_left_to_next -= 2;
1599 p0 = vlib_get_buffer (vm, pi0);
1600 p1 = vlib_get_buffer (vm, pi1);
1602 ip0 = vlib_buffer_get_current (p0);
1603 ip1 = vlib_buffer_get_current (p1);
1605 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1606 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1608 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1609 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1614 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1615 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1617 udp0 = ip6_next_header (ip0);
1618 udp1 = ip6_next_header (ip1);
1620 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1621 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1622 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1624 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1625 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1627 /* Verify UDP length. */
1628 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1629 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1630 udp_len0 = clib_net_to_host_u16 (udp0->length);
1631 udp_len1 = clib_net_to_host_u16 (udp1->length);
1633 len_diff0 = ip_len0 - udp_len0;
1634 len_diff1 = ip_len1 - udp_len1;
1636 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1637 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1639 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1640 && ! good_l4_checksum0
1641 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1643 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1645 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1647 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1648 && ! good_l4_checksum1
1649 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1651 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1653 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1656 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1658 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1659 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1661 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1662 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1663 error0 = (! good_l4_checksum0
1664 ? IP6_ERROR_UDP_CHECKSUM + type0
1666 error1 = (! good_l4_checksum1
1667 ? IP6_ERROR_UDP_CHECKSUM + type1
1670 /* Drop packets from unroutable hosts. */
1671 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1672 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1674 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1675 error0 = (lm->miss_adj_index == src_adj_index0
1676 ? IP6_ERROR_SRC_LOOKUP_MISS
1679 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1681 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1682 error1 = (lm->miss_adj_index == src_adj_index1
1683 ? IP6_ERROR_SRC_LOOKUP_MISS
1687 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1688 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1690 p0->error = error_node->errors[error0];
1691 p1->error = error_node->errors[error1];
1693 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1694 to_next, n_left_to_next,
1695 pi0, pi1, next0, next1);
1698 while (n_left_from > 0 && n_left_to_next > 0)
1702 udp_header_t * udp0;
1703 u32 pi0, ip_len0, udp_len0, flags0, next0;
1705 u8 error0, type0, good_l4_checksum0;
1707 pi0 = to_next[0] = from[0];
1711 n_left_to_next -= 1;
1713 p0 = vlib_get_buffer (vm, pi0);
1715 ip0 = vlib_buffer_get_current (p0);
1717 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1718 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1722 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1724 udp0 = ip6_next_header (ip0);
1726 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1727 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1729 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1731 /* Verify UDP length. */
1732 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1733 udp_len0 = clib_net_to_host_u16 (udp0->length);
1735 len_diff0 = ip_len0 - udp_len0;
1737 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1739 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1740 && ! good_l4_checksum0
1741 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1743 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1745 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1748 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1750 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1752 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1753 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1754 error0 = (! good_l4_checksum0
1755 ? IP6_ERROR_UDP_CHECKSUM + type0
1758 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1759 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1761 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1762 error0 = (lm->miss_adj_index == src_adj_index0
1763 ? IP6_ERROR_SRC_LOOKUP_MISS
1767 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1769 p0->error = error_node->errors[error0];
1771 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1772 to_next, n_left_to_next,
1776 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1779 return frame->n_vectors;
1782 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1783 .function = ip6_local,
1784 .name = "ip6-local",
1785 .vector_size = sizeof (u32),
1787 .format_trace = format_ip6_forward_next_trace,
1789 .n_next_nodes = IP_LOCAL_N_NEXT,
1791 [IP_LOCAL_NEXT_DROP] = "error-drop",
1792 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1793 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1794 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1798 void ip6_register_protocol (u32 protocol, u32 node_index)
1800 vlib_main_t * vm = vlib_get_main();
1801 ip6_main_t * im = &ip6_main;
1802 ip_lookup_main_t * lm = &im->lookup_main;
1804 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1805 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1809 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1810 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1811 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1812 } ip6_discover_neighbor_next_t;
1815 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1816 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1817 } ip6_discover_neighbor_error_t;
1820 ip6_discover_neighbor (vlib_main_t * vm,
1821 vlib_node_runtime_t * node,
1822 vlib_frame_t * frame)
1824 vnet_main_t * vnm = vnet_get_main();
1825 ip6_main_t * im = &ip6_main;
1826 ip_lookup_main_t * lm = &im->lookup_main;
1827 u32 * from, * to_next_drop;
1828 uword n_left_from, n_left_to_next_drop;
1829 static f64 time_last_seed_change = -1e100;
1830 static u32 hash_seeds[3];
1831 static uword hash_bitmap[256 / BITS (uword)];
1835 if (node->flags & VLIB_NODE_FLAG_TRACE)
1836 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1838 time_now = vlib_time_now (vm);
1839 if (time_now - time_last_seed_change > 1e-3)
1842 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1843 sizeof (hash_seeds));
1844 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1845 hash_seeds[i] = r[i];
1847 /* Mark all hash keys as been not-seen before. */
1848 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1851 time_last_seed_change = time_now;
1854 from = vlib_frame_vector_args (frame);
1855 n_left_from = frame->n_vectors;
1857 while (n_left_from > 0)
1859 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1860 to_next_drop, n_left_to_next_drop);
1862 while (n_left_from > 0 && n_left_to_next_drop > 0)
1866 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1868 ip_adjacency_t * adj0;
1869 vnet_hw_interface_t * hw_if0;
1874 p0 = vlib_get_buffer (vm, pi0);
1876 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1878 ip0 = vlib_buffer_get_current (p0);
1880 adj0 = ip_get_adjacency (lm, adj_index0);
1882 if (adj0->arp.next_hop.ip6.as_u64[0] ||
1883 adj0->arp.next_hop.ip6.as_u64[1]) {
1884 ip0->dst_address.as_u64[0] = adj0->arp.next_hop.ip6.as_u64[0];
1885 ip0->dst_address.as_u64[1] = adj0->arp.next_hop.ip6.as_u64[1];
1892 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1893 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1896 b0 ^= ip0->dst_address.as_u32[0];
1897 c0 ^= ip0->dst_address.as_u32[1];
1899 hash_v3_mix32 (a0, b0, c0);
1901 b0 ^= ip0->dst_address.as_u32[2];
1902 c0 ^= ip0->dst_address.as_u32[3];
1904 hash_v3_finalize32 (a0, b0, c0);
1906 c0 &= BITS (hash_bitmap) - 1;
1907 c0 = c0 / BITS (uword);
1908 m0 = (uword) 1 << (c0 % BITS (uword));
1910 bm0 = hash_bitmap[c0];
1911 drop0 = (bm0 & m0) != 0;
1913 /* Mark it as seen. */
1914 hash_bitmap[c0] = bm0 | m0;
1918 to_next_drop[0] = pi0;
1920 n_left_to_next_drop -= 1;
1922 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1924 /* If the interface is link-down, drop the pkt */
1925 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1929 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1930 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1936 icmp6_neighbor_solicitation_header_t * h0;
1939 h0 = vlib_packet_template_get_packet
1940 (vm, &im->discover_neighbor_packet_template, &bi0);
1943 * Build ethernet header.
1944 * Choose source address based on destination lookup
1947 ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
1951 * Destination address is a solicited node multicast address.
1952 * We need to fill in
1953 * the low 24 bits with low 24 bits of target's address.
1955 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1956 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1957 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1959 h0->neighbor.target_address = ip0->dst_address;
1961 clib_memcpy (h0->link_layer_option.ethernet_address,
1962 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1964 /* $$$$ appears we need this; why is the checksum non-zero? */
1965 h0->neighbor.icmp.checksum = 0;
1966 h0->neighbor.icmp.checksum =
1967 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1970 ASSERT (bogus_length == 0);
1972 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1973 b0 = vlib_get_buffer (vm, bi0);
1974 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1975 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1977 /* Add rewrite/encap string. */
1978 vnet_rewrite_one_header (adj0[0], h0,
1979 sizeof (ethernet_header_t));
1980 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1982 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1984 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1988 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1989 n_left_to_next_drop);
1992 return frame->n_vectors;
1995 static char * ip6_discover_neighbor_error_strings[] = {
1996 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1997 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
1998 = "neighbor solicitations sent",
2001 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
2002 .function = ip6_discover_neighbor,
2003 .name = "ip6-discover-neighbor",
2004 .vector_size = sizeof (u32),
2006 .format_trace = format_ip6_forward_next_trace,
2008 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
2009 .error_strings = ip6_discover_neighbor_error_strings,
2011 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
2013 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
2014 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
2019 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2021 vnet_main_t * vnm = vnet_get_main();
2022 ip6_main_t * im = &ip6_main;
2023 icmp6_neighbor_solicitation_header_t * h;
2024 ip6_address_t * src;
2025 ip_interface_address_t * ia;
2026 ip_adjacency_t * adj;
2027 vnet_hw_interface_t * hi;
2028 vnet_sw_interface_t * si;
2033 si = vnet_get_sw_interface (vnm, sw_if_index);
2035 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2037 return clib_error_return (0, "%U: interface %U down",
2038 format_ip6_address, dst,
2039 format_vnet_sw_if_index_name, vnm,
2043 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2046 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2047 return clib_error_return
2048 (0, "no matching interface address for destination %U (interface %U)",
2049 format_ip6_address, dst,
2050 format_vnet_sw_if_index_name, vnm, sw_if_index);
2053 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2055 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2057 /* Destination address is a solicited node multicast address. We need to fill in
2058 the low 24 bits with low 24 bits of target's address. */
2059 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2060 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2061 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2063 h->ip.src_address = src[0];
2064 h->neighbor.target_address = dst[0];
2066 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2068 h->neighbor.icmp.checksum =
2069 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2070 ASSERT(bogus_length == 0);
2072 b = vlib_get_buffer (vm, bi);
2073 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2075 /* Add encapsulation string for software interface (e.g. ethernet header). */
2076 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2077 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2078 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2081 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2082 u32 * to_next = vlib_frame_vector_args (f);
2085 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2088 return /* no error */ 0;
2092 IP6_REWRITE_NEXT_DROP,
2093 } ip6_rewrite_next_t;
2096 ip6_rewrite_inline (vlib_main_t * vm,
2097 vlib_node_runtime_t * node,
2098 vlib_frame_t * frame,
2099 int rewrite_for_locally_received_packets)
2101 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2102 u32 * from = vlib_frame_vector_args (frame);
2103 u32 n_left_from, n_left_to_next, * to_next, next_index;
2104 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2105 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2107 n_left_from = frame->n_vectors;
2108 next_index = node->cached_next_index;
2109 u32 cpu_index = os_get_cpu_number();
2111 while (n_left_from > 0)
2113 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2115 while (n_left_from >= 4 && n_left_to_next >= 2)
2117 ip_adjacency_t * adj0, * adj1;
2118 vlib_buffer_t * p0, * p1;
2119 ip6_header_t * ip0, * ip1;
2120 u32 pi0, rw_len0, next0, error0, adj_index0;
2121 u32 pi1, rw_len1, next1, error1, adj_index1;
2123 /* Prefetch next iteration. */
2125 vlib_buffer_t * p2, * p3;
2127 p2 = vlib_get_buffer (vm, from[2]);
2128 p3 = vlib_get_buffer (vm, from[3]);
2130 vlib_prefetch_buffer_header (p2, LOAD);
2131 vlib_prefetch_buffer_header (p3, LOAD);
2133 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2134 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2136 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2137 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2140 pi0 = to_next[0] = from[0];
2141 pi1 = to_next[1] = from[1];
2146 n_left_to_next -= 2;
2148 p0 = vlib_get_buffer (vm, pi0);
2149 p1 = vlib_get_buffer (vm, pi1);
2151 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2152 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2154 /* We should never rewrite a pkt using the MISS adjacency */
2155 ASSERT(adj_index0 && adj_index1);
2157 ip0 = vlib_buffer_get_current (p0);
2158 ip1 = vlib_buffer_get_current (p1);
2160 error0 = error1 = IP6_ERROR_NONE;
2162 if (! rewrite_for_locally_received_packets)
2164 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2166 /* Input node should have reject packets with hop limit 0. */
2167 ASSERT (ip0->hop_limit > 0);
2168 ASSERT (ip1->hop_limit > 0);
2173 ip0->hop_limit = hop_limit0;
2174 ip1->hop_limit = hop_limit1;
2176 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2177 error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2180 adj0 = ip_get_adjacency (lm, adj_index0);
2181 adj1 = ip_get_adjacency (lm, adj_index1);
2183 if (rewrite_for_locally_received_packets)
2186 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2187 * we end up here with a local adjacency in hand
2189 if (PREDICT_FALSE(adj0->lookup_next_index
2190 == IP_LOOKUP_NEXT_LOCAL))
2191 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2192 if (PREDICT_FALSE(adj1->lookup_next_index
2193 == IP_LOOKUP_NEXT_LOCAL))
2194 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2197 rw_len0 = adj0[0].rewrite_header.data_bytes;
2198 rw_len1 = adj1[0].rewrite_header.data_bytes;
2200 vlib_increment_combined_counter (&lm->adjacency_counters,
2203 /* packet increment */ 0,
2204 /* byte increment */ rw_len0);
2205 vlib_increment_combined_counter (&lm->adjacency_counters,
2208 /* packet increment */ 0,
2209 /* byte increment */ rw_len1);
2211 /* Check MTU of outgoing interface. */
2212 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2213 ? IP6_ERROR_MTU_EXCEEDED
2215 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2216 ? IP6_ERROR_MTU_EXCEEDED
2219 p0->current_data -= rw_len0;
2220 p1->current_data -= rw_len1;
2222 p0->current_length += rw_len0;
2223 p1->current_length += rw_len1;
2225 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2226 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2228 next0 = (error0 == IP6_ERROR_NONE) ?
2229 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2230 next1 = (error1 == IP6_ERROR_NONE) ?
2231 adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2233 /* Guess we are only writing on simple Ethernet header. */
2234 vnet_rewrite_two_headers (adj0[0], adj1[0],
2236 sizeof (ethernet_header_t));
2238 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2239 to_next, n_left_to_next,
2240 pi0, pi1, next0, next1);
2243 while (n_left_from > 0 && n_left_to_next > 0)
2245 ip_adjacency_t * adj0;
2249 u32 adj_index0, next0, error0;
2251 pi0 = to_next[0] = from[0];
2253 p0 = vlib_get_buffer (vm, pi0);
2255 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2257 /* We should never rewrite a pkt using the MISS adjacency */
2260 adj0 = ip_get_adjacency (lm, adj_index0);
2262 ip0 = vlib_buffer_get_current (p0);
2264 error0 = IP6_ERROR_NONE;
2266 /* Check hop limit */
2267 if (! rewrite_for_locally_received_packets)
2269 i32 hop_limit0 = ip0->hop_limit;
2271 ASSERT (ip0->hop_limit > 0);
2275 ip0->hop_limit = hop_limit0;
2277 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2280 if (rewrite_for_locally_received_packets)
2282 if (PREDICT_FALSE(adj0->lookup_next_index
2283 == IP_LOOKUP_NEXT_LOCAL))
2284 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2287 /* Guess we are only writing on simple Ethernet header. */
2288 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2290 /* Update packet buffer attributes/set output interface. */
2291 rw_len0 = adj0[0].rewrite_header.data_bytes;
2293 vlib_increment_combined_counter (&lm->adjacency_counters,
2296 /* packet increment */ 0,
2297 /* byte increment */ rw_len0);
2299 /* Check MTU of outgoing interface. */
2300 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2301 ? IP6_ERROR_MTU_EXCEEDED
2304 p0->current_data -= rw_len0;
2305 p0->current_length += rw_len0;
2306 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2308 next0 = (error0 == IP6_ERROR_NONE) ?
2309 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2311 p0->error = error_node->errors[error0];
2316 n_left_to_next -= 1;
2318 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2319 to_next, n_left_to_next,
2323 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2326 /* Need to do trace after rewrites to pick up new packet data. */
2327 if (node->flags & VLIB_NODE_FLAG_TRACE)
2328 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2330 return frame->n_vectors;
2334 ip6_rewrite_transit (vlib_main_t * vm,
2335 vlib_node_runtime_t * node,
2336 vlib_frame_t * frame)
2338 return ip6_rewrite_inline (vm, node, frame,
2339 /* rewrite_for_locally_received_packets */ 0);
2343 ip6_rewrite_local (vlib_main_t * vm,
2344 vlib_node_runtime_t * node,
2345 vlib_frame_t * frame)
2347 return ip6_rewrite_inline (vm, node, frame,
2348 /* rewrite_for_locally_received_packets */ 1);
2351 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2352 .function = ip6_rewrite_transit,
2353 .name = "ip6-rewrite",
2354 .vector_size = sizeof (u32),
2356 .format_trace = format_ip6_forward_next_trace,
2360 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2364 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2365 .function = ip6_rewrite_local,
2366 .name = "ip6-rewrite-local",
2367 .vector_size = sizeof (u32),
2369 .sibling_of = "ip6-rewrite",
2371 .format_trace = format_ip6_forward_next_trace,
2375 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2379 /* Global IP6 main. */
2380 ip6_main_t ip6_main;
2382 static clib_error_t *
2383 ip6_lookup_init (vlib_main_t * vm)
2385 ip6_main_t * im = &ip6_main;
2388 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2395 for (j = 0; j < i0; j++)
2396 im->fib_masks[i].as_u32[j] = ~0;
2399 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2402 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2404 if (im->lookup_table_nbuckets == 0)
2405 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2407 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2409 if (im->lookup_table_size == 0)
2410 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2412 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2413 im->lookup_table_nbuckets,
2414 im->lookup_table_size);
2416 /* Create FIB with index 0 and table id of 0. */
2417 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2421 pn = pg_get_node (ip6_lookup_node.index);
2422 pn->unformat_edit = unformat_pg_ip6_header;
2426 icmp6_neighbor_solicitation_header_t p;
2428 memset (&p, 0, sizeof (p));
2430 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2431 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2432 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2433 p.ip.protocol = IP_PROTOCOL_ICMP6;
2434 p.ip.hop_limit = 255;
2435 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2437 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2439 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2440 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2442 vlib_packet_template_init (vm,
2443 &im->discover_neighbor_packet_template,
2445 /* alloc chunk size */ 8,
2446 "ip6 neighbor discovery");
2452 VLIB_INIT_FUNCTION (ip6_lookup_init);
2454 static clib_error_t *
2455 add_del_ip6_interface_table (vlib_main_t * vm,
2456 unformat_input_t * input,
2457 vlib_cli_command_t * cmd)
2459 vnet_main_t * vnm = vnet_get_main();
2460 clib_error_t * error = 0;
2461 u32 sw_if_index, table_id;
2465 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2467 error = clib_error_return (0, "unknown interface `%U'",
2468 format_unformat_error, input);
2472 if (unformat (input, "%d", &table_id))
2476 error = clib_error_return (0, "expected table id `%U'",
2477 format_unformat_error, input);
2482 ip6_main_t * im = &ip6_main;
2484 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2488 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2489 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2497 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2498 .path = "set interface ip6 table",
2499 .function = add_del_ip6_interface_table,
2500 .short_help = "set interface ip6 table <intfc> <table-id>"
2504 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2507 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2508 /* Invert the "u" bit */
2509 ip->as_u8 [8] = mac[0] ^ (1<<1);
2510 ip->as_u8 [9] = mac[1];
2511 ip->as_u8 [10] = mac[2];
2512 ip->as_u8 [11] = 0xFF;
2513 ip->as_u8 [12] = 0xFE;
2514 ip->as_u8 [13] = mac[3];
2515 ip->as_u8 [14] = mac[4];
2516 ip->as_u8 [15] = mac[5];
2520 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
2523 /* Invert the previously inverted "u" bit */
2524 mac[0] = ip->as_u8 [8] ^ (1<<1);
2525 mac[1] = ip->as_u8 [9];
2526 mac[2] = ip->as_u8 [10];
2527 mac[3] = ip->as_u8 [13];
2528 mac[4] = ip->as_u8 [14];
2529 mac[5] = ip->as_u8 [15];
2532 static clib_error_t *
2533 test_ip6_link_command_fn (vlib_main_t * vm,
2534 unformat_input_t * input,
2535 vlib_cli_command_t * cmd)
2538 ip6_address_t _a, *a = &_a;
2540 if (unformat (input, "%U", unformat_ethernet_address, mac))
2542 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2543 vlib_cli_output (vm, "Link local address: %U",
2544 format_ip6_address, a);
2545 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2546 vlib_cli_output (vm, "Original MAC address: %U",
2547 format_ethernet_address, mac);
2553 VLIB_CLI_COMMAND (test_link_command, static) = {
2554 .path = "test ip6 link",
2555 .function = test_ip6_link_command_fn,
2556 .short_help = "test ip6 link <mac-address>",
2559 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2561 ip6_main_t * im6 = &ip6_main;
2563 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2568 fib = vec_elt_at_index (im6->fibs, p[0]);
2570 fib->flow_hash_config = flow_hash_config;
2574 static clib_error_t *
2575 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2576 unformat_input_t * input,
2577 vlib_cli_command_t * cmd)
2581 u32 flow_hash_config = 0;
2584 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2585 if (unformat (input, "table %d", &table_id))
2588 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2589 foreach_flow_hash_bit
2595 return clib_error_return (0, "unknown input `%U'",
2596 format_unformat_error, input);
2598 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2605 return clib_error_return (0, "no such FIB table %d", table_id);
2608 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2615 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2616 .path = "set ip6 flow-hash",
2618 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2619 .function = set_ip6_flow_hash_command_fn,
2622 static clib_error_t *
2623 show_ip6_local_command_fn (vlib_main_t * vm,
2624 unformat_input_t * input,
2625 vlib_cli_command_t * cmd)
2627 ip6_main_t * im = &ip6_main;
2628 ip_lookup_main_t * lm = &im->lookup_main;
2631 vlib_cli_output (vm, "Protocols handled by ip6_local");
2632 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2634 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2635 vlib_cli_output (vm, "%d", i);
2642 VLIB_CLI_COMMAND (show_ip_local, static) = {
2643 .path = "show ip6 local",
2644 .function = show_ip6_local_command_fn,
2645 .short_help = "Show ip6 local protocol table",
2648 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2651 vnet_main_t * vnm = vnet_get_main();
2652 vnet_interface_main_t * im = &vnm->interface_main;
2653 ip6_main_t * ipm = &ip6_main;
2654 ip_lookup_main_t * lm = &ipm->lookup_main;
2655 vnet_classify_main_t * cm = &vnet_classify_main;
2657 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2658 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2660 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2661 return VNET_API_ERROR_NO_SUCH_ENTRY;
2663 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2664 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2669 static clib_error_t *
2670 set_ip6_classify_command_fn (vlib_main_t * vm,
2671 unformat_input_t * input,
2672 vlib_cli_command_t * cmd)
2674 u32 table_index = ~0;
2675 int table_index_set = 0;
2676 u32 sw_if_index = ~0;
2679 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2680 if (unformat (input, "table-index %d", &table_index))
2681 table_index_set = 1;
2682 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2683 vnet_get_main(), &sw_if_index))
2689 if (table_index_set == 0)
2690 return clib_error_return (0, "classify table-index must be specified");
2692 if (sw_if_index == ~0)
2693 return clib_error_return (0, "interface / subif must be specified");
2695 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2702 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2703 return clib_error_return (0, "No such interface");
2705 case VNET_API_ERROR_NO_SUCH_ENTRY:
2706 return clib_error_return (0, "No such classifier table");
2711 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2712 .path = "set ip6 classify",
2714 "set ip6 classify intfc <int> table-index <index>",
2715 .function = set_ip6_classify_command_fn,
2718 static clib_error_t *
2719 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2721 ip6_main_t * im = &ip6_main;
2726 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2727 if (unformat (input, "hash-buckets %d", &tmp))
2729 else if (unformat (input, "heap-size %dm", &tmp))
2730 heapsize = ((u64)tmp) << 20;
2731 else if (unformat (input, "heap-size %dM", &tmp))
2732 heapsize = ((u64)tmp) << 20;
2733 else if (unformat (input, "heap-size %dg", &tmp))
2734 heapsize = ((u64)tmp) << 30;
2735 else if (unformat (input, "heap-size %dG", &tmp))
2736 heapsize = ((u64)tmp) << 30;
2738 return clib_error_return (0, "unknown input '%U'",
2739 format_unformat_error, input);
2742 im->lookup_table_nbuckets = nbuckets;
2743 im->lookup_table_size = heapsize;
2748 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");