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
276 && !(a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
278 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
279 if (adj->share_count > 0)
283 /* Delete old adjacency index if present and changed. */
285 if (! (a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
286 && old_adj_index != ~0
287 && old_adj_index != adj_index)
288 ip_del_adjacency (lm, old_adj_index);
293 ip6_add_del_route_next_hop (ip6_main_t * im,
295 ip6_address_t * dst_address,
296 u32 dst_address_length,
297 ip6_address_t * next_hop,
298 u32 next_hop_sw_if_index,
299 u32 next_hop_weight, u32 adj_index,
300 u32 explicit_fib_index)
302 vnet_main_t * vnm = vnet_get_main();
303 ip_lookup_main_t * lm = &im->lookup_main;
306 ip6_address_t masked_dst_address;
307 u32 old_mp_adj_index, new_mp_adj_index;
308 u32 dst_adj_index, nh_adj_index;
310 ip_adjacency_t * dst_adj;
311 ip_multipath_adjacency_t * old_mp, * new_mp;
312 int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
313 int is_interface_next_hop;
314 clib_error_t * error = 0;
316 BVT(clib_bihash_kv) kv, value;
318 vlib_smp_unsafe_warning();
320 if (explicit_fib_index == (u32)~0)
321 fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
323 fib_index = explicit_fib_index;
325 fib = vec_elt_at_index (im->fibs, fib_index);
327 /* Lookup next hop to be added or deleted. */
328 is_interface_next_hop = ip6_address_is_zero (next_hop);
329 if (adj_index == (u32)~0)
331 if (is_interface_next_hop)
333 nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index,
334 next_hop_sw_if_index);
336 nh_adj_index = *nh_result;
339 ip_adjacency_t * adj;
340 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
342 ip6_adjacency_set_interface_route (vnm, adj,
343 next_hop_sw_if_index, ~0);
344 ip_call_add_del_adjacency_callbacks
345 (lm, next_hop_sw_if_index, /* is_del */ 0);
346 hash_set (im->interface_route_adj_index_by_sw_if_index,
347 next_hop_sw_if_index, nh_adj_index);
352 /* Look for the interface /128 route */
353 kv.key[0] = next_hop->as_u64[0];
354 kv.key[1] = next_hop->as_u64[1];
355 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
357 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
359 ip_adjacency_t * adj;
360 nh_adj_index = ip6_fib_lookup_with_table (im, fib_index, next_hop);
361 adj = ip_get_adjacency (lm, nh_adj_index);
362 /* if ND interface adjacencty is present, we need to
363 install ND adjaceny for specific next hop */
364 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
365 adj->arp.next_hop.ip6.as_u64[0] == 0 &&
366 adj->arp.next_hop.ip6.as_u64[1] == 0)
368 nh_adj_index = vnet_ip6_neighbor_glean_add(fib_index, next_hop);
372 ip_adjacency_t add_adj;
373 add_adj.lookup_next_index = IP_LOOKUP_NEXT_INDIRECT;
374 add_adj.indirect.next_hop.ip6.as_u64[0] = next_hop->as_u64[0];
375 add_adj.indirect.next_hop.ip6.as_u64[1] = next_hop->as_u64[1];
376 add_adj.explicit_fib_index = explicit_fib_index;
377 ip_add_adjacency (lm, &add_adj, 1, &nh_adj_index);
381 nh_adj_index = value.value;
387 /* Look for the interface /128 route */
388 kv.key[0] = next_hop->as_u64[0];
389 kv.key[1] = next_hop->as_u64[1];
390 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
392 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
394 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
395 error = clib_error_return (0, "next-hop %U/128 not in FIB",
396 format_ip6_address, next_hop);
400 nh_adj_index = value.value;
403 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
404 masked_dst_address = dst_address[0];
405 ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
407 kv.key[0] = masked_dst_address.as_u64[0];
408 kv.key[1] = masked_dst_address.as_u64[1];
409 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
411 rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
415 dst_adj_index = value.value;
416 dst_adj = ip_get_adjacency (lm, dst_adj_index);
420 /* For deletes destination must be known. */
423 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
424 error = clib_error_return (0, "unknown destination %U/%d",
425 format_ip6_address, dst_address,
434 /* Ignore adds of X/128 with next hop of X. */
436 && dst_address_length == 128
437 && ip6_address_is_equal (dst_address, next_hop))
439 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
440 error = clib_error_return (0, "prefix matches next hop %U/%d",
441 format_ip6_address, dst_address,
446 /* Destination is not known and default weight is set so add route
447 to existing non-multipath adjacency */
448 if (dst_adj_index == ~0 && next_hop_weight == 1 && next_hop_sw_if_index == ~0)
450 /* create new adjacency */
451 ip6_add_del_route_args_t a;
452 a.table_index_or_table_id = fib_index;
453 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
454 | IP6_ROUTE_FLAG_FIB_INDEX
455 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
456 | (flags & (IP6_ROUTE_FLAG_NO_REDISTRIBUTE
457 | IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP)));
458 a.dst_address = dst_address[0];
459 a.dst_address_length = dst_address_length;
460 a.adj_index = nh_adj_index;
464 ip6_add_del_route (im, &a);
468 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
470 if (! ip_multipath_adjacency_add_del_next_hop
472 dst_adj ? dst_adj->heap_handle : ~0,
477 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
478 error = clib_error_return
479 (0, "requested deleting next-hop %U not found in multi-path",
480 format_ip6_address, next_hop);
485 if (old_mp_adj_index != ~0)
486 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
487 if (new_mp_adj_index != ~0)
488 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
490 if (old_mp != new_mp)
492 ip6_add_del_route_args_t a;
493 a.table_index_or_table_id = fib_index;
494 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
495 | IP6_ROUTE_FLAG_FIB_INDEX
496 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
497 | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
498 a.dst_address = dst_address[0];
499 a.dst_address_length = dst_address_length;
500 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
504 ip6_add_del_route (im, &a);
509 clib_error_report (error);
513 ip6_get_route (ip6_main_t * im,
514 u32 table_index_or_table_id,
516 ip6_address_t * address,
519 ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
520 ip6_address_t masked_address;
521 BVT(clib_bihash_kv) kv, value;
523 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
524 clib_memcpy (&masked_address, address, sizeof (masked_address));
525 ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
527 kv.key[0] = masked_address.as_u64[0];
528 kv.key[1] = masked_address.as_u64[1];
529 kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
531 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
532 return (value.value);
537 ip6_foreach_matching_route (ip6_main_t * im,
538 u32 table_index_or_table_id,
540 ip6_address_t * dst_address,
542 ip6_address_t ** results,
543 u8 ** result_lengths)
546 find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
547 BVT(clib_bihash) * h = &im->ip6_lookup_table;
548 BVT(clib_bihash_value) * v;
549 clib_bihash_bucket_t * b;
553 _vec_len (*results) = 0;
555 _vec_len (*result_lengths) = 0;
557 /* Walk the table looking for routes which match the supplied address */
558 for (i = 0; i < h->nbuckets; i++)
564 v = BV(clib_bihash_get_value) (h, b->offset);
565 for (j = 0; j < (1<<b->log2_pages); j++)
567 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
569 if (BV(clib_bihash_is_free)(&v->kvp[k]))
572 if ((v->kvp[k].key[2]
573 == (((u64)((fib - im->fibs))<<32) | address_length))
574 && ip6_destination_matches_route
575 (im, dst_address, (ip6_address_t *) &v->kvp[k],
580 a = (ip6_address_t *)(&v->kvp[k]);
582 vec_add1 (*results, a[0]);
583 vec_add1 (*result_lengths, address_length);
591 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
592 u32 table_index_or_table_id,
597 = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
599 ip_lookup_main_t * lm = &im->lookup_main;
601 if (lm->n_adjacency_remaps == 0)
604 clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
606 /* All remaps have been performed. */
607 lm->n_adjacency_remaps = 0;
610 void ip6_delete_matching_routes (ip6_main_t * im,
611 u32 table_index_or_table_id,
613 ip6_address_t * address,
616 /* $$$$ static may be OK - this should happen only on thread 0 */
617 static ip6_address_t * matching_addresses;
618 static u8 * matching_address_lengths;
620 ip6_add_del_route_args_t a;
622 vlib_smp_unsafe_warning();
624 a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
625 a.table_index_or_table_id = table_index_or_table_id;
630 for (l = address_length + 1; l <= 128; l++)
632 ip6_foreach_matching_route (im, table_index_or_table_id, flags,
636 &matching_address_lengths);
637 for (i = 0; i < vec_len (matching_addresses); i++)
639 a.dst_address = matching_addresses[i];
640 a.dst_address_length = matching_address_lengths[i];
641 ip6_add_del_route (im, &a);
645 ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
649 ip6_lookup_inline (vlib_main_t * vm,
650 vlib_node_runtime_t * node,
651 vlib_frame_t * frame,
654 ip6_main_t * im = &ip6_main;
655 ip_lookup_main_t * lm = &im->lookup_main;
656 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
657 u32 n_left_from, n_left_to_next, * from, * to_next;
658 ip_lookup_next_t next;
659 u32 cpu_index = os_get_cpu_number();
661 from = vlib_frame_vector_args (frame);
662 n_left_from = frame->n_vectors;
663 next = node->cached_next_index;
665 while (n_left_from > 0)
667 vlib_get_next_frame (vm, node, next,
668 to_next, n_left_to_next);
670 while (n_left_from >= 4 && n_left_to_next >= 2)
672 vlib_buffer_t * p0, * p1;
673 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
674 ip_lookup_next_t next0, next1;
675 ip6_header_t * ip0, * ip1;
676 ip_adjacency_t * adj0, * adj1;
677 ip6_address_t * dst_addr0, * dst_addr1;
678 u32 fib_index0, fib_index1;
679 u32 flow_hash_config0, flow_hash_config1;
681 /* Prefetch next iteration. */
683 vlib_buffer_t * p2, * p3;
685 p2 = vlib_get_buffer (vm, from[2]);
686 p3 = vlib_get_buffer (vm, from[3]);
688 vlib_prefetch_buffer_header (p2, LOAD);
689 vlib_prefetch_buffer_header (p3, LOAD);
690 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
691 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
694 pi0 = to_next[0] = from[0];
695 pi1 = to_next[1] = from[1];
697 p0 = vlib_get_buffer (vm, pi0);
698 p1 = vlib_get_buffer (vm, pi1);
700 ip0 = vlib_buffer_get_current (p0);
701 ip1 = vlib_buffer_get_current (p1);
705 ip_adjacency_t * iadj0, * iadj1;
706 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
707 iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
708 dst_addr0 = &iadj0->indirect.next_hop.ip6;
709 dst_addr1 = &iadj1->indirect.next_hop.ip6;
713 dst_addr0 = &ip0->dst_address;
714 dst_addr1 = &ip1->dst_address;
717 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
718 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
720 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
721 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
722 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
723 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
725 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
726 adj_index1 = ip6_fib_lookup_with_table (im, fib_index1, dst_addr1);
728 adj0 = ip_get_adjacency (lm, adj_index0);
729 adj1 = ip_get_adjacency (lm, adj_index1);
731 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
733 adj_index0 = ip6_fib_lookup_with_table
734 (im, adj0->explicit_fib_index, dst_addr0);
735 adj0 = ip_get_adjacency (lm, adj_index0);
737 if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
739 adj_index1 = ip6_fib_lookup_with_table
740 (im, adj1->explicit_fib_index, dst_addr1);
741 adj1 = ip_get_adjacency (lm, adj_index1);
744 next0 = adj0->lookup_next_index;
745 next1 = adj1->lookup_next_index;
747 /* Process hop-by-hop options if present */
748 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
749 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
750 next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
751 IP_LOOKUP_NEXT_HOP_BY_HOP : next1;
753 vnet_buffer (p0)->ip.flow_hash =
754 vnet_buffer(p1)->ip.flow_hash = 0;
756 if (PREDICT_FALSE(adj0->n_adj > 1))
759 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
760 vnet_buffer (p0)->ip.flow_hash =
761 ip6_compute_flow_hash (ip0, flow_hash_config0);
764 if (PREDICT_FALSE(adj1->n_adj > 1))
767 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
769 vnet_buffer (p1)->ip.flow_hash =
770 ip6_compute_flow_hash (ip1, flow_hash_config1);
773 ASSERT (adj0->n_adj > 0);
774 ASSERT (adj1->n_adj > 0);
775 ASSERT (is_pow2 (adj0->n_adj));
776 ASSERT (is_pow2 (adj1->n_adj));
777 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
778 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
780 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
781 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
783 vlib_increment_combined_counter
784 (cm, cpu_index, adj_index0, 1,
785 vlib_buffer_length_in_chain (vm, p0));
786 vlib_increment_combined_counter
787 (cm, cpu_index, adj_index1, 1,
788 vlib_buffer_length_in_chain (vm, p1));
795 wrong_next = (next0 != next) + 2*(next1 != next);
796 if (PREDICT_FALSE (wrong_next != 0))
805 vlib_set_next_frame_buffer (vm, node, next0, pi0);
812 vlib_set_next_frame_buffer (vm, node, next1, pi1);
819 vlib_set_next_frame_buffer (vm, node, next0, pi0);
820 vlib_set_next_frame_buffer (vm, node, next1, pi1);
824 vlib_put_next_frame (vm, node, next, n_left_to_next);
826 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
832 while (n_left_from > 0 && n_left_to_next > 0)
837 ip_lookup_next_t next0;
838 ip_adjacency_t * adj0;
839 ip6_address_t * dst_addr0;
840 u32 fib_index0, flow_hash_config0;
845 p0 = vlib_get_buffer (vm, pi0);
847 ip0 = vlib_buffer_get_current (p0);
851 ip_adjacency_t * iadj0;
852 iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
853 dst_addr0 = &iadj0->indirect.next_hop.ip6;
857 dst_addr0 = &ip0->dst_address;
860 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
861 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
862 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
865 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
867 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
869 adj0 = ip_get_adjacency (lm, adj_index0);
871 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
873 adj_index0 = ip6_fib_lookup_with_table
874 (im, adj0->explicit_fib_index, dst_addr0);
875 adj0 = ip_get_adjacency (lm, adj_index0);
878 next0 = adj0->lookup_next_index;
879 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
880 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
882 vnet_buffer (p0)->ip.flow_hash = 0;
884 if (PREDICT_FALSE(adj0->n_adj > 1))
887 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
888 vnet_buffer (p0)->ip.flow_hash =
889 ip6_compute_flow_hash (ip0, flow_hash_config0);
892 ASSERT (adj0->n_adj > 0);
893 ASSERT (is_pow2 (adj0->n_adj));
894 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
896 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
898 vlib_increment_combined_counter
899 (cm, cpu_index, adj_index0, 1,
900 vlib_buffer_length_in_chain (vm, p0));
907 if (PREDICT_FALSE (next0 != next))
910 vlib_put_next_frame (vm, node, next, n_left_to_next);
912 vlib_get_next_frame (vm, node, next,
913 to_next, n_left_to_next);
920 vlib_put_next_frame (vm, node, next, n_left_to_next);
923 return frame->n_vectors;
926 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
927 ip_adjacency_t * adj,
929 u32 if_address_index)
931 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
935 if (hw->hw_class_index == ethernet_hw_interface_class.index
936 || hw->hw_class_index == srp_hw_interface_class.index)
938 n = IP_LOOKUP_NEXT_ARP;
939 node_index = ip6_discover_neighbor_node.index;
940 adj->if_address_index = if_address_index;
941 adj->arp.next_hop.ip6.as_u64[0] = 0;
942 adj->arp.next_hop.ip6.as_u64[1] = 0;
946 n = IP_LOOKUP_NEXT_REWRITE;
947 node_index = ip6_rewrite_node.index;
950 adj->lookup_next_index = n;
951 adj->explicit_fib_index = ~0;
953 vnet_rewrite_for_sw_interface
955 VNET_L3_PACKET_TYPE_IP6,
958 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
959 &adj->rewrite_header,
960 sizeof (adj->rewrite_data));
964 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
965 ip6_main_t * im, u32 fib_index,
966 ip_interface_address_t * a)
968 ip_lookup_main_t * lm = &im->lookup_main;
969 ip_adjacency_t * adj;
970 ip6_address_t * address = ip_interface_address_get_address (lm, a);
971 ip6_add_del_route_args_t x;
972 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
973 u32 classify_table_index;
975 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
976 x.table_index_or_table_id = fib_index;
977 x.flags = (IP6_ROUTE_FLAG_ADD
978 | IP6_ROUTE_FLAG_FIB_INDEX
979 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
980 x.dst_address = address[0];
981 x.dst_address_length = a->address_length;
985 a->neighbor_probe_adj_index = ~0;
986 if (a->address_length < 128)
988 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
990 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
991 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
992 ip6_add_del_route (im, &x);
993 a->neighbor_probe_adj_index = x.adj_index;
996 /* Add e.g. ::1/128 as local to this host. */
997 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1000 classify_table_index = ~0;
1001 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
1002 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
1003 if (classify_table_index != (u32) ~0)
1005 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
1006 adj->classify.table_index = classify_table_index;
1009 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1011 adj->if_address_index = a - lm->if_address_pool;
1012 adj->rewrite_header.sw_if_index = sw_if_index;
1013 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
1014 adj->rewrite_header.data_bytes = 0;
1015 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1016 x.dst_address_length = 128;
1017 ip6_add_del_route (im, &x);
1021 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
1022 ip6_address_t * address, u32 address_length)
1024 ip6_add_del_route_args_t x;
1026 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1027 x.table_index_or_table_id = fib_index;
1028 x.flags = (IP6_ROUTE_FLAG_DEL
1029 | IP6_ROUTE_FLAG_FIB_INDEX
1030 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
1031 x.dst_address = address[0];
1032 x.dst_address_length = address_length;
1037 if (address_length < 128)
1039 /* Don't wipe out fe80::0/64 */
1040 if (address_length != 64 ||
1041 address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
1042 ip6_add_del_route (im, &x);
1045 x.dst_address_length = 128;
1046 ip6_add_del_route (im, &x);
1048 ip6_delete_matching_routes (im,
1050 IP6_ROUTE_FLAG_FIB_INDEX,
1057 ip6_address_t address;
1059 } ip6_interface_address_t;
1061 static clib_error_t *
1062 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1064 ip6_address_t * new_address,
1070 static clib_error_t *
1071 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1073 ip6_address_t * address,
1079 vnet_main_t * vnm = vnet_get_main();
1080 ip6_main_t * im = &ip6_main;
1081 ip_lookup_main_t * lm = &im->lookup_main;
1082 clib_error_t * error;
1083 u32 if_address_index;
1084 ip6_address_fib_t ip6_af, * addr_fib = 0;
1086 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1087 ip6_addr_fib_init (&ip6_af, address,
1088 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1089 vec_add1 (addr_fib, ip6_af);
1092 uword elts_before = pool_elts (lm->if_address_pool);
1094 error = ip_interface_address_add_del
1104 /* Pool did not grow: add duplicate address. */
1105 if (elts_before == pool_elts (lm->if_address_pool))
1109 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1112 ip6_del_interface_routes (im, ip6_af.fib_index, address,
1116 ip6_add_interface_routes (vnm, sw_if_index,
1117 im, ip6_af.fib_index,
1118 pool_elt_at_index (lm->if_address_pool, if_address_index));
1122 ip6_add_del_interface_address_callback_t * cb;
1123 vec_foreach (cb, im->add_del_interface_address_callbacks)
1124 cb->function (im, cb->function_opaque, sw_if_index,
1125 address, address_length,
1131 vec_free (addr_fib);
1136 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1137 ip6_address_t * address, u32 address_length,
1140 return ip6_add_del_interface_address_internal
1141 (vm, sw_if_index, address, address_length,
1142 /* redistribute */ 1,
1143 /* insert_routes */ 1,
1148 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1152 ip6_main_t * im = &ip6_main;
1153 ip_interface_address_t * ia;
1155 u32 is_admin_up, fib_index;
1157 /* Fill in lookup tables with default table (0). */
1158 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1160 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1162 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1164 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1166 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1167 0 /* honor unnumbered */,
1169 a = ip_interface_address_get_address (&im->lookup_main, ia);
1171 ip6_add_interface_routes (vnm, sw_if_index,
1175 ip6_del_interface_routes (im, fib_index,
1176 a, ia->address_length);
1182 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1185 ip6_sw_interface_add_del (vnet_main_t * vnm,
1189 vlib_main_t * vm = vnm->vlib_main;
1190 ip6_main_t * im = &ip6_main;
1191 ip_lookup_main_t * lm = &im->lookup_main;
1194 for (cast = 0; cast < VNET_N_CAST; cast++)
1196 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1197 vnet_config_main_t * vcm = &cm->config_main;
1199 /* FIXME multicast. */
1200 if (! vcm->node_index_by_feature_index)
1202 char * start_nodes[] = { "ip6-input", };
1203 char * feature_nodes[] = {
1204 [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1205 [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1206 [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1207 [IP6_RX_FEATURE_VPATH] = "vpath-input-ip6",
1208 [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1210 vnet_config_init (vm, vcm,
1211 start_nodes, ARRAY_LEN (start_nodes),
1212 feature_nodes, ARRAY_LEN (feature_nodes));
1215 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1216 ci = cm->config_index_by_sw_if_index[sw_if_index];
1219 ci = vnet_config_add_feature (vm, vcm,
1221 IP6_RX_FEATURE_LOOKUP,
1222 /* config data */ 0,
1223 /* # bytes of config data */ 0);
1225 ci = vnet_config_del_feature (vm, vcm,
1227 IP6_RX_FEATURE_LOOKUP,
1228 /* config data */ 0,
1229 /* # bytes of config data */ 0);
1231 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1233 return /* no error */ 0;
1236 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1239 ip6_lookup (vlib_main_t * vm,
1240 vlib_node_runtime_t * node,
1241 vlib_frame_t * frame)
1243 return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 0);
1246 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1247 .function = ip6_lookup,
1248 .name = "ip6-lookup",
1249 .vector_size = sizeof (u32),
1251 .n_next_nodes = IP_LOOKUP_N_NEXT,
1252 .next_nodes = IP6_LOOKUP_NEXT_NODES,
1256 ip6_indirect (vlib_main_t * vm,
1257 vlib_node_runtime_t * node,
1258 vlib_frame_t * frame)
1260 return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 1);
1264 VLIB_REGISTER_NODE (ip6_indirect_node) = {
1265 .function = ip6_indirect,
1266 .name = "ip6-indirect",
1267 .vector_size = sizeof (u32),
1269 .n_next_nodes = IP_LOOKUP_N_NEXT,
1270 .next_nodes = IP6_LOOKUP_NEXT_NODES,
1274 /* Adjacency taken. */
1279 /* Packet data, possibly *after* rewrite. */
1280 u8 packet_data[128 - 1*sizeof(u32)];
1281 } ip6_forward_next_trace_t;
1283 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1285 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1286 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1287 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1288 vnet_main_t * vnm = vnet_get_main();
1289 ip6_main_t * im = &ip6_main;
1290 ip_adjacency_t * adj;
1291 uword indent = format_get_indent (s);
1293 adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1294 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1295 t->fib_index, t->adj_index, format_ip_adjacency,
1296 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1297 switch (adj->lookup_next_index)
1299 case IP_LOOKUP_NEXT_REWRITE:
1300 s = format (s, "\n%U%U",
1301 format_white_space, indent,
1302 format_ip_adjacency_packet_data,
1303 vnm, &im->lookup_main, t->adj_index,
1304 t->packet_data, sizeof (t->packet_data));
1314 /* Common trace function for all ip6-forward next nodes. */
1316 ip6_forward_next_trace (vlib_main_t * vm,
1317 vlib_node_runtime_t * node,
1318 vlib_frame_t * frame,
1319 vlib_rx_or_tx_t which_adj_index)
1322 ip6_main_t * im = &ip6_main;
1324 n_left = frame->n_vectors;
1325 from = vlib_frame_vector_args (frame);
1330 vlib_buffer_t * b0, * b1;
1331 ip6_forward_next_trace_t * t0, * t1;
1333 /* Prefetch next iteration. */
1334 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1335 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1340 b0 = vlib_get_buffer (vm, bi0);
1341 b1 = vlib_get_buffer (vm, bi1);
1343 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1345 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1346 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1347 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1348 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1349 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1350 clib_memcpy (t0->packet_data,
1351 vlib_buffer_get_current (b0),
1352 sizeof (t0->packet_data));
1354 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1356 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1357 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1358 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1359 t1->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1360 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1361 clib_memcpy (t1->packet_data,
1362 vlib_buffer_get_current (b1),
1363 sizeof (t1->packet_data));
1373 ip6_forward_next_trace_t * t0;
1377 b0 = vlib_get_buffer (vm, bi0);
1379 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1381 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1382 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1383 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1384 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1385 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1386 clib_memcpy (t0->packet_data,
1387 vlib_buffer_get_current (b0),
1388 sizeof (t0->packet_data));
1396 ip6_drop_or_punt (vlib_main_t * vm,
1397 vlib_node_runtime_t * node,
1398 vlib_frame_t * frame,
1399 ip6_error_t error_code)
1401 u32 * buffers = vlib_frame_vector_args (frame);
1402 uword n_packets = frame->n_vectors;
1404 vlib_error_drop_buffers (vm, node,
1409 ip6_input_node.index,
1412 if (node->flags & VLIB_NODE_FLAG_TRACE)
1413 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1419 ip6_drop (vlib_main_t * vm,
1420 vlib_node_runtime_t * node,
1421 vlib_frame_t * frame)
1422 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1425 ip6_punt (vlib_main_t * vm,
1426 vlib_node_runtime_t * node,
1427 vlib_frame_t * frame)
1428 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1431 ip6_miss (vlib_main_t * vm,
1432 vlib_node_runtime_t * node,
1433 vlib_frame_t * frame)
1434 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1436 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1437 .function = ip6_drop,
1439 .vector_size = sizeof (u32),
1441 .format_trace = format_ip6_forward_next_trace,
1449 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1450 .function = ip6_punt,
1452 .vector_size = sizeof (u32),
1454 .format_trace = format_ip6_forward_next_trace,
1462 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1463 .function = ip6_miss,
1465 .vector_size = sizeof (u32),
1467 .format_trace = format_ip6_forward_next_trace,
1475 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1476 .function = ip6_drop,
1477 .name = "ip6-multicast",
1478 .vector_size = sizeof (u32),
1480 .format_trace = format_ip6_forward_next_trace,
1488 /* Compute TCP/UDP/ICMP6 checksum in software. */
1489 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1492 u16 sum16, payload_length_host_byte_order;
1493 u32 i, n_this_buffer, n_bytes_left;
1494 u32 headers_size = sizeof(ip0[0]);
1495 void * data_this_buffer;
1497 ASSERT(bogus_lengthp);
1500 /* Initialize checksum with ip header. */
1501 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1502 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1503 data_this_buffer = (void *) (ip0 + 1);
1505 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1507 sum0 = ip_csum_with_carry (sum0,
1508 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1509 sum0 = ip_csum_with_carry (sum0,
1510 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1513 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1514 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1517 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1519 /* validate really icmp6 next */
1520 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1522 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1523 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1525 payload_length_host_byte_order -= skip_bytes;
1526 headers_size += skip_bytes;
1529 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1531 if (p0 && n_this_buffer + headers_size > p0->current_length)
1533 struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1534 u8 nb_segs = mb->nb_segs;
1536 n_this_buffer = (p0->current_length > headers_size ?
1537 p0->current_length - headers_size : 0);
1538 while (n_bytes_left)
1540 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1541 n_bytes_left -= n_this_buffer;
1545 if ((nb_segs == 0) || (mb == 0))
1548 data_this_buffer = rte_ctrlmbuf_data(mb);
1549 n_this_buffer = mb->data_len;
1551 if (n_bytes_left || nb_segs)
1557 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1559 if (p0 && n_this_buffer + headers_size > p0->current_length)
1560 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1563 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1564 n_bytes_left -= n_this_buffer;
1565 if (n_bytes_left == 0)
1568 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1573 p0 = vlib_get_buffer (vm, p0->next_buffer);
1574 data_this_buffer = vlib_buffer_get_current (p0);
1575 n_this_buffer = p0->current_length;
1579 sum16 = ~ ip_csum_fold (sum0);
1584 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1586 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1587 udp_header_t * udp0;
1591 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1592 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1593 || ip0->protocol == IP_PROTOCOL_ICMP6
1594 || ip0->protocol == IP_PROTOCOL_UDP
1595 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1597 udp0 = (void *) (ip0 + 1);
1598 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1600 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1601 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1605 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1607 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1608 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1614 ip6_local (vlib_main_t * vm,
1615 vlib_node_runtime_t * node,
1616 vlib_frame_t * frame)
1618 ip6_main_t * im = &ip6_main;
1619 ip_lookup_main_t * lm = &im->lookup_main;
1620 ip_local_next_t next_index;
1621 u32 * from, * to_next, n_left_from, n_left_to_next;
1622 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1624 from = vlib_frame_vector_args (frame);
1625 n_left_from = frame->n_vectors;
1626 next_index = node->cached_next_index;
1628 if (node->flags & VLIB_NODE_FLAG_TRACE)
1629 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1631 while (n_left_from > 0)
1633 vlib_get_next_frame (vm, node, next_index,
1634 to_next, n_left_to_next);
1636 while (n_left_from >= 4 && n_left_to_next >= 2)
1638 vlib_buffer_t * p0, * p1;
1639 ip6_header_t * ip0, * ip1;
1640 udp_header_t * udp0, * udp1;
1641 u32 pi0, ip_len0, udp_len0, flags0, next0;
1642 u32 pi1, ip_len1, udp_len1, flags1, next1;
1643 i32 len_diff0, len_diff1;
1644 u8 error0, type0, good_l4_checksum0;
1645 u8 error1, type1, good_l4_checksum1;
1647 pi0 = to_next[0] = from[0];
1648 pi1 = to_next[1] = from[1];
1652 n_left_to_next -= 2;
1654 p0 = vlib_get_buffer (vm, pi0);
1655 p1 = vlib_get_buffer (vm, pi1);
1657 ip0 = vlib_buffer_get_current (p0);
1658 ip1 = vlib_buffer_get_current (p1);
1660 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1661 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1663 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1664 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1669 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1670 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1672 udp0 = ip6_next_header (ip0);
1673 udp1 = ip6_next_header (ip1);
1675 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1676 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1677 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1679 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1680 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1682 /* Verify UDP length. */
1683 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1684 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1685 udp_len0 = clib_net_to_host_u16 (udp0->length);
1686 udp_len1 = clib_net_to_host_u16 (udp1->length);
1688 len_diff0 = ip_len0 - udp_len0;
1689 len_diff1 = ip_len1 - udp_len1;
1691 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1692 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1694 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1695 && ! good_l4_checksum0
1696 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1698 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1700 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1702 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1703 && ! good_l4_checksum1
1704 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1706 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1708 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1711 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1713 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1714 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1716 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1717 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1718 error0 = (! good_l4_checksum0
1719 ? IP6_ERROR_UDP_CHECKSUM + type0
1721 error1 = (! good_l4_checksum1
1722 ? IP6_ERROR_UDP_CHECKSUM + type1
1725 /* Drop packets from unroutable hosts. */
1726 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1727 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1729 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1730 error0 = (lm->miss_adj_index == src_adj_index0
1731 ? IP6_ERROR_SRC_LOOKUP_MISS
1734 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1736 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1737 error1 = (lm->miss_adj_index == src_adj_index1
1738 ? IP6_ERROR_SRC_LOOKUP_MISS
1742 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1743 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1745 p0->error = error_node->errors[error0];
1746 p1->error = error_node->errors[error1];
1748 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1749 to_next, n_left_to_next,
1750 pi0, pi1, next0, next1);
1753 while (n_left_from > 0 && n_left_to_next > 0)
1757 udp_header_t * udp0;
1758 u32 pi0, ip_len0, udp_len0, flags0, next0;
1760 u8 error0, type0, good_l4_checksum0;
1762 pi0 = to_next[0] = from[0];
1766 n_left_to_next -= 1;
1768 p0 = vlib_get_buffer (vm, pi0);
1770 ip0 = vlib_buffer_get_current (p0);
1772 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1773 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1777 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1779 udp0 = ip6_next_header (ip0);
1781 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1782 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1784 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1786 /* Verify UDP length. */
1787 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1788 udp_len0 = clib_net_to_host_u16 (udp0->length);
1790 len_diff0 = ip_len0 - udp_len0;
1792 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1794 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1795 && ! good_l4_checksum0
1796 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1798 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1800 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1803 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1805 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1807 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1808 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1809 error0 = (! good_l4_checksum0
1810 ? IP6_ERROR_UDP_CHECKSUM + type0
1813 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1814 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1816 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1817 error0 = (lm->miss_adj_index == src_adj_index0
1818 ? IP6_ERROR_SRC_LOOKUP_MISS
1822 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1824 p0->error = error_node->errors[error0];
1826 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1827 to_next, n_left_to_next,
1831 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1834 return frame->n_vectors;
1837 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1838 .function = ip6_local,
1839 .name = "ip6-local",
1840 .vector_size = sizeof (u32),
1842 .format_trace = format_ip6_forward_next_trace,
1844 .n_next_nodes = IP_LOCAL_N_NEXT,
1846 [IP_LOCAL_NEXT_DROP] = "error-drop",
1847 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1848 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1849 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1853 void ip6_register_protocol (u32 protocol, u32 node_index)
1855 vlib_main_t * vm = vlib_get_main();
1856 ip6_main_t * im = &ip6_main;
1857 ip_lookup_main_t * lm = &im->lookup_main;
1859 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1860 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1864 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1865 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1866 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1867 } ip6_discover_neighbor_next_t;
1870 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1871 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1872 } ip6_discover_neighbor_error_t;
1875 ip6_discover_neighbor (vlib_main_t * vm,
1876 vlib_node_runtime_t * node,
1877 vlib_frame_t * frame)
1879 vnet_main_t * vnm = vnet_get_main();
1880 ip6_main_t * im = &ip6_main;
1881 ip_lookup_main_t * lm = &im->lookup_main;
1882 u32 * from, * to_next_drop;
1883 uword n_left_from, n_left_to_next_drop;
1884 static f64 time_last_seed_change = -1e100;
1885 static u32 hash_seeds[3];
1886 static uword hash_bitmap[256 / BITS (uword)];
1890 if (node->flags & VLIB_NODE_FLAG_TRACE)
1891 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1893 time_now = vlib_time_now (vm);
1894 if (time_now - time_last_seed_change > 1e-3)
1897 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1898 sizeof (hash_seeds));
1899 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1900 hash_seeds[i] = r[i];
1902 /* Mark all hash keys as been not-seen before. */
1903 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1906 time_last_seed_change = time_now;
1909 from = vlib_frame_vector_args (frame);
1910 n_left_from = frame->n_vectors;
1912 while (n_left_from > 0)
1914 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1915 to_next_drop, n_left_to_next_drop);
1917 while (n_left_from > 0 && n_left_to_next_drop > 0)
1921 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1923 ip_adjacency_t * adj0;
1924 vnet_hw_interface_t * hw_if0;
1929 p0 = vlib_get_buffer (vm, pi0);
1931 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1933 ip0 = vlib_buffer_get_current (p0);
1935 adj0 = ip_get_adjacency (lm, adj_index0);
1937 if (adj0->arp.next_hop.ip6.as_u64[0] ||
1938 adj0->arp.next_hop.ip6.as_u64[1]) {
1939 ip0->dst_address.as_u64[0] = adj0->arp.next_hop.ip6.as_u64[0];
1940 ip0->dst_address.as_u64[1] = adj0->arp.next_hop.ip6.as_u64[1];
1947 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1948 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1951 b0 ^= ip0->dst_address.as_u32[0];
1952 c0 ^= ip0->dst_address.as_u32[1];
1954 hash_v3_mix32 (a0, b0, c0);
1956 b0 ^= ip0->dst_address.as_u32[2];
1957 c0 ^= ip0->dst_address.as_u32[3];
1959 hash_v3_finalize32 (a0, b0, c0);
1961 c0 &= BITS (hash_bitmap) - 1;
1962 c0 = c0 / BITS (uword);
1963 m0 = (uword) 1 << (c0 % BITS (uword));
1965 bm0 = hash_bitmap[c0];
1966 drop0 = (bm0 & m0) != 0;
1968 /* Mark it as seen. */
1969 hash_bitmap[c0] = bm0 | m0;
1973 to_next_drop[0] = pi0;
1975 n_left_to_next_drop -= 1;
1977 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1979 /* If the interface is link-down, drop the pkt */
1980 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1984 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1985 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1991 icmp6_neighbor_solicitation_header_t * h0;
1994 h0 = vlib_packet_template_get_packet
1995 (vm, &im->discover_neighbor_packet_template, &bi0);
1998 * Build ethernet header.
1999 * Choose source address based on destination lookup
2002 ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
2006 * Destination address is a solicited node multicast address.
2007 * We need to fill in
2008 * the low 24 bits with low 24 bits of target's address.
2010 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
2011 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
2012 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
2014 h0->neighbor.target_address = ip0->dst_address;
2016 clib_memcpy (h0->link_layer_option.ethernet_address,
2017 hw_if0->hw_address, vec_len (hw_if0->hw_address));
2019 /* $$$$ appears we need this; why is the checksum non-zero? */
2020 h0->neighbor.icmp.checksum = 0;
2021 h0->neighbor.icmp.checksum =
2022 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
2025 ASSERT (bogus_length == 0);
2027 vlib_buffer_copy_trace_flag (vm, p0, bi0);
2028 b0 = vlib_get_buffer (vm, bi0);
2029 vnet_buffer (b0)->sw_if_index[VLIB_TX]
2030 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
2032 /* Add rewrite/encap string. */
2033 vnet_rewrite_one_header (adj0[0], h0,
2034 sizeof (ethernet_header_t));
2035 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
2037 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
2039 vlib_set_next_frame_buffer (vm, node, next0, bi0);
2043 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
2044 n_left_to_next_drop);
2047 return frame->n_vectors;
2050 static char * ip6_discover_neighbor_error_strings[] = {
2051 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
2052 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
2053 = "neighbor solicitations sent",
2056 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
2057 .function = ip6_discover_neighbor,
2058 .name = "ip6-discover-neighbor",
2059 .vector_size = sizeof (u32),
2061 .format_trace = format_ip6_forward_next_trace,
2063 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
2064 .error_strings = ip6_discover_neighbor_error_strings,
2066 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
2068 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
2069 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
2074 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2076 vnet_main_t * vnm = vnet_get_main();
2077 ip6_main_t * im = &ip6_main;
2078 icmp6_neighbor_solicitation_header_t * h;
2079 ip6_address_t * src;
2080 ip_interface_address_t * ia;
2081 ip_adjacency_t * adj;
2082 vnet_hw_interface_t * hi;
2083 vnet_sw_interface_t * si;
2088 si = vnet_get_sw_interface (vnm, sw_if_index);
2090 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2092 return clib_error_return (0, "%U: interface %U down",
2093 format_ip6_address, dst,
2094 format_vnet_sw_if_index_name, vnm,
2098 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2101 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2102 return clib_error_return
2103 (0, "no matching interface address for destination %U (interface %U)",
2104 format_ip6_address, dst,
2105 format_vnet_sw_if_index_name, vnm, sw_if_index);
2108 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2110 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2112 /* Destination address is a solicited node multicast address. We need to fill in
2113 the low 24 bits with low 24 bits of target's address. */
2114 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2115 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2116 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2118 h->ip.src_address = src[0];
2119 h->neighbor.target_address = dst[0];
2121 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2123 h->neighbor.icmp.checksum =
2124 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2125 ASSERT(bogus_length == 0);
2127 b = vlib_get_buffer (vm, bi);
2128 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2130 /* Add encapsulation string for software interface (e.g. ethernet header). */
2131 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2132 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2133 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2136 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2137 u32 * to_next = vlib_frame_vector_args (f);
2140 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2143 return /* no error */ 0;
2147 IP6_REWRITE_NEXT_DROP,
2148 } ip6_rewrite_next_t;
2151 ip6_rewrite_inline (vlib_main_t * vm,
2152 vlib_node_runtime_t * node,
2153 vlib_frame_t * frame,
2154 int rewrite_for_locally_received_packets)
2156 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2157 u32 * from = vlib_frame_vector_args (frame);
2158 u32 n_left_from, n_left_to_next, * to_next, next_index;
2159 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2160 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2162 n_left_from = frame->n_vectors;
2163 next_index = node->cached_next_index;
2164 u32 cpu_index = os_get_cpu_number();
2166 while (n_left_from > 0)
2168 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2170 while (n_left_from >= 4 && n_left_to_next >= 2)
2172 ip_adjacency_t * adj0, * adj1;
2173 vlib_buffer_t * p0, * p1;
2174 ip6_header_t * ip0, * ip1;
2175 u32 pi0, rw_len0, next0, error0, adj_index0;
2176 u32 pi1, rw_len1, next1, error1, adj_index1;
2178 /* Prefetch next iteration. */
2180 vlib_buffer_t * p2, * p3;
2182 p2 = vlib_get_buffer (vm, from[2]);
2183 p3 = vlib_get_buffer (vm, from[3]);
2185 vlib_prefetch_buffer_header (p2, LOAD);
2186 vlib_prefetch_buffer_header (p3, LOAD);
2188 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2189 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2191 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2192 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2195 pi0 = to_next[0] = from[0];
2196 pi1 = to_next[1] = from[1];
2201 n_left_to_next -= 2;
2203 p0 = vlib_get_buffer (vm, pi0);
2204 p1 = vlib_get_buffer (vm, pi1);
2206 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2207 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2209 /* We should never rewrite a pkt using the MISS adjacency */
2210 ASSERT(adj_index0 && adj_index1);
2212 ip0 = vlib_buffer_get_current (p0);
2213 ip1 = vlib_buffer_get_current (p1);
2215 error0 = error1 = IP6_ERROR_NONE;
2217 if (! rewrite_for_locally_received_packets)
2219 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2221 /* Input node should have reject packets with hop limit 0. */
2222 ASSERT (ip0->hop_limit > 0);
2223 ASSERT (ip1->hop_limit > 0);
2228 ip0->hop_limit = hop_limit0;
2229 ip1->hop_limit = hop_limit1;
2231 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2232 error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2235 adj0 = ip_get_adjacency (lm, adj_index0);
2236 adj1 = ip_get_adjacency (lm, adj_index1);
2238 if (rewrite_for_locally_received_packets)
2241 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2242 * we end up here with a local adjacency in hand
2244 if (PREDICT_FALSE(adj0->lookup_next_index
2245 == IP_LOOKUP_NEXT_LOCAL))
2246 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2247 if (PREDICT_FALSE(adj1->lookup_next_index
2248 == IP_LOOKUP_NEXT_LOCAL))
2249 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2252 rw_len0 = adj0[0].rewrite_header.data_bytes;
2253 rw_len1 = adj1[0].rewrite_header.data_bytes;
2255 vlib_increment_combined_counter (&lm->adjacency_counters,
2258 /* packet increment */ 0,
2259 /* byte increment */ rw_len0);
2260 vlib_increment_combined_counter (&lm->adjacency_counters,
2263 /* packet increment */ 0,
2264 /* byte increment */ rw_len1);
2266 /* Check MTU of outgoing interface. */
2267 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2268 ? IP6_ERROR_MTU_EXCEEDED
2270 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2271 ? IP6_ERROR_MTU_EXCEEDED
2274 p0->current_data -= rw_len0;
2275 p1->current_data -= rw_len1;
2277 p0->current_length += rw_len0;
2278 p1->current_length += rw_len1;
2280 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2281 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2283 next0 = (error0 == IP6_ERROR_NONE) ?
2284 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2285 next1 = (error1 == IP6_ERROR_NONE) ?
2286 adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2288 /* Guess we are only writing on simple Ethernet header. */
2289 vnet_rewrite_two_headers (adj0[0], adj1[0],
2291 sizeof (ethernet_header_t));
2293 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2294 to_next, n_left_to_next,
2295 pi0, pi1, next0, next1);
2298 while (n_left_from > 0 && n_left_to_next > 0)
2300 ip_adjacency_t * adj0;
2304 u32 adj_index0, next0, error0;
2306 pi0 = to_next[0] = from[0];
2308 p0 = vlib_get_buffer (vm, pi0);
2310 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2312 /* We should never rewrite a pkt using the MISS adjacency */
2315 adj0 = ip_get_adjacency (lm, adj_index0);
2317 ip0 = vlib_buffer_get_current (p0);
2319 error0 = IP6_ERROR_NONE;
2321 /* Check hop limit */
2322 if (! rewrite_for_locally_received_packets)
2324 i32 hop_limit0 = ip0->hop_limit;
2326 ASSERT (ip0->hop_limit > 0);
2330 ip0->hop_limit = hop_limit0;
2332 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2335 if (rewrite_for_locally_received_packets)
2337 if (PREDICT_FALSE(adj0->lookup_next_index
2338 == IP_LOOKUP_NEXT_LOCAL))
2339 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2342 /* Guess we are only writing on simple Ethernet header. */
2343 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2345 /* Update packet buffer attributes/set output interface. */
2346 rw_len0 = adj0[0].rewrite_header.data_bytes;
2348 vlib_increment_combined_counter (&lm->adjacency_counters,
2351 /* packet increment */ 0,
2352 /* byte increment */ rw_len0);
2354 /* Check MTU of outgoing interface. */
2355 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2356 ? IP6_ERROR_MTU_EXCEEDED
2359 p0->current_data -= rw_len0;
2360 p0->current_length += rw_len0;
2361 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2363 next0 = (error0 == IP6_ERROR_NONE) ?
2364 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2366 p0->error = error_node->errors[error0];
2371 n_left_to_next -= 1;
2373 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2374 to_next, n_left_to_next,
2378 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2381 /* Need to do trace after rewrites to pick up new packet data. */
2382 if (node->flags & VLIB_NODE_FLAG_TRACE)
2383 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2385 return frame->n_vectors;
2389 ip6_rewrite_transit (vlib_main_t * vm,
2390 vlib_node_runtime_t * node,
2391 vlib_frame_t * frame)
2393 return ip6_rewrite_inline (vm, node, frame,
2394 /* rewrite_for_locally_received_packets */ 0);
2398 ip6_rewrite_local (vlib_main_t * vm,
2399 vlib_node_runtime_t * node,
2400 vlib_frame_t * frame)
2402 return ip6_rewrite_inline (vm, node, frame,
2403 /* rewrite_for_locally_received_packets */ 1);
2406 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2407 .function = ip6_rewrite_transit,
2408 .name = "ip6-rewrite",
2409 .vector_size = sizeof (u32),
2411 .format_trace = format_ip6_forward_next_trace,
2415 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2419 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2420 .function = ip6_rewrite_local,
2421 .name = "ip6-rewrite-local",
2422 .vector_size = sizeof (u32),
2424 .sibling_of = "ip6-rewrite",
2426 .format_trace = format_ip6_forward_next_trace,
2430 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2434 /* Global IP6 main. */
2435 ip6_main_t ip6_main;
2437 static clib_error_t *
2438 ip6_lookup_init (vlib_main_t * vm)
2440 ip6_main_t * im = &ip6_main;
2443 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2450 for (j = 0; j < i0; j++)
2451 im->fib_masks[i].as_u32[j] = ~0;
2454 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2457 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2459 if (im->lookup_table_nbuckets == 0)
2460 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2462 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2464 if (im->lookup_table_size == 0)
2465 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2467 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2468 im->lookup_table_nbuckets,
2469 im->lookup_table_size);
2471 /* Create FIB with index 0 and table id of 0. */
2472 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2476 pn = pg_get_node (ip6_lookup_node.index);
2477 pn->unformat_edit = unformat_pg_ip6_header;
2481 icmp6_neighbor_solicitation_header_t p;
2483 memset (&p, 0, sizeof (p));
2485 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2486 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2487 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2488 p.ip.protocol = IP_PROTOCOL_ICMP6;
2489 p.ip.hop_limit = 255;
2490 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2492 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2494 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2495 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2497 vlib_packet_template_init (vm,
2498 &im->discover_neighbor_packet_template,
2500 /* alloc chunk size */ 8,
2501 "ip6 neighbor discovery");
2507 VLIB_INIT_FUNCTION (ip6_lookup_init);
2509 static clib_error_t *
2510 add_del_ip6_interface_table (vlib_main_t * vm,
2511 unformat_input_t * input,
2512 vlib_cli_command_t * cmd)
2514 vnet_main_t * vnm = vnet_get_main();
2515 clib_error_t * error = 0;
2516 u32 sw_if_index, table_id;
2520 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2522 error = clib_error_return (0, "unknown interface `%U'",
2523 format_unformat_error, input);
2527 if (unformat (input, "%d", &table_id))
2531 error = clib_error_return (0, "expected table id `%U'",
2532 format_unformat_error, input);
2537 ip6_main_t * im = &ip6_main;
2539 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2543 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2544 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2552 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2553 .path = "set interface ip6 table",
2554 .function = add_del_ip6_interface_table,
2555 .short_help = "set interface ip6 table <intfc> <table-id>"
2559 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2562 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2563 /* Invert the "u" bit */
2564 ip->as_u8 [8] = mac[0] ^ (1<<1);
2565 ip->as_u8 [9] = mac[1];
2566 ip->as_u8 [10] = mac[2];
2567 ip->as_u8 [11] = 0xFF;
2568 ip->as_u8 [12] = 0xFE;
2569 ip->as_u8 [13] = mac[3];
2570 ip->as_u8 [14] = mac[4];
2571 ip->as_u8 [15] = mac[5];
2575 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
2578 /* Invert the previously inverted "u" bit */
2579 mac[0] = ip->as_u8 [8] ^ (1<<1);
2580 mac[1] = ip->as_u8 [9];
2581 mac[2] = ip->as_u8 [10];
2582 mac[3] = ip->as_u8 [13];
2583 mac[4] = ip->as_u8 [14];
2584 mac[5] = ip->as_u8 [15];
2587 static clib_error_t *
2588 test_ip6_link_command_fn (vlib_main_t * vm,
2589 unformat_input_t * input,
2590 vlib_cli_command_t * cmd)
2593 ip6_address_t _a, *a = &_a;
2595 if (unformat (input, "%U", unformat_ethernet_address, mac))
2597 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2598 vlib_cli_output (vm, "Link local address: %U",
2599 format_ip6_address, a);
2600 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2601 vlib_cli_output (vm, "Original MAC address: %U",
2602 format_ethernet_address, mac);
2608 VLIB_CLI_COMMAND (test_link_command, static) = {
2609 .path = "test ip6 link",
2610 .function = test_ip6_link_command_fn,
2611 .short_help = "test ip6 link <mac-address>",
2614 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2616 ip6_main_t * im6 = &ip6_main;
2618 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2623 fib = vec_elt_at_index (im6->fibs, p[0]);
2625 fib->flow_hash_config = flow_hash_config;
2629 static clib_error_t *
2630 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2631 unformat_input_t * input,
2632 vlib_cli_command_t * cmd)
2636 u32 flow_hash_config = 0;
2639 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2640 if (unformat (input, "table %d", &table_id))
2643 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2644 foreach_flow_hash_bit
2650 return clib_error_return (0, "unknown input `%U'",
2651 format_unformat_error, input);
2653 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2660 return clib_error_return (0, "no such FIB table %d", table_id);
2663 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2670 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2671 .path = "set ip6 flow-hash",
2673 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2674 .function = set_ip6_flow_hash_command_fn,
2677 static clib_error_t *
2678 show_ip6_local_command_fn (vlib_main_t * vm,
2679 unformat_input_t * input,
2680 vlib_cli_command_t * cmd)
2682 ip6_main_t * im = &ip6_main;
2683 ip_lookup_main_t * lm = &im->lookup_main;
2686 vlib_cli_output (vm, "Protocols handled by ip6_local");
2687 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2689 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2690 vlib_cli_output (vm, "%d", i);
2697 VLIB_CLI_COMMAND (show_ip_local, static) = {
2698 .path = "show ip6 local",
2699 .function = show_ip6_local_command_fn,
2700 .short_help = "Show ip6 local protocol table",
2703 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2706 vnet_main_t * vnm = vnet_get_main();
2707 vnet_interface_main_t * im = &vnm->interface_main;
2708 ip6_main_t * ipm = &ip6_main;
2709 ip_lookup_main_t * lm = &ipm->lookup_main;
2710 vnet_classify_main_t * cm = &vnet_classify_main;
2712 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2713 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2715 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2716 return VNET_API_ERROR_NO_SUCH_ENTRY;
2718 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2719 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2724 static clib_error_t *
2725 set_ip6_classify_command_fn (vlib_main_t * vm,
2726 unformat_input_t * input,
2727 vlib_cli_command_t * cmd)
2729 u32 table_index = ~0;
2730 int table_index_set = 0;
2731 u32 sw_if_index = ~0;
2734 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2735 if (unformat (input, "table-index %d", &table_index))
2736 table_index_set = 1;
2737 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2738 vnet_get_main(), &sw_if_index))
2744 if (table_index_set == 0)
2745 return clib_error_return (0, "classify table-index must be specified");
2747 if (sw_if_index == ~0)
2748 return clib_error_return (0, "interface / subif must be specified");
2750 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2757 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2758 return clib_error_return (0, "No such interface");
2760 case VNET_API_ERROR_NO_SUCH_ENTRY:
2761 return clib_error_return (0, "No such classifier table");
2766 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2767 .path = "set ip6 classify",
2769 "set ip6 classify intfc <int> table-index <index>",
2770 .function = set_ip6_classify_command_fn,
2773 static clib_error_t *
2774 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2776 ip6_main_t * im = &ip6_main;
2781 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2782 if (unformat (input, "hash-buckets %d", &tmp))
2784 else if (unformat (input, "heap-size %dm", &tmp))
2785 heapsize = ((u64)tmp) << 20;
2786 else if (unformat (input, "heap-size %dM", &tmp))
2787 heapsize = ((u64)tmp) << 20;
2788 else if (unformat (input, "heap-size %dg", &tmp))
2789 heapsize = ((u64)tmp) << 30;
2790 else if (unformat (input, "heap-size %dG", &tmp))
2791 heapsize = ((u64)tmp) << 30;
2793 return clib_error_return (0, "unknown input '%U'",
2794 format_unformat_error, input);
2797 im->lookup_table_nbuckets = nbuckets;
2798 im->lookup_table_size = heapsize;
2803 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");