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 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
358 error = clib_error_return (0, "next-hop %U/128 not in FIB",
359 format_ip6_address, next_hop);
363 nh_adj_index = value.value;
368 /* Look for the interface /128 route */
369 kv.key[0] = next_hop->as_u64[0];
370 kv.key[1] = next_hop->as_u64[1];
371 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
373 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
375 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
376 error = clib_error_return (0, "next-hop %U/128 not in FIB",
377 format_ip6_address, next_hop);
381 nh_adj_index = value.value;
384 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
385 masked_dst_address = dst_address[0];
386 ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
388 kv.key[0] = masked_dst_address.as_u64[0];
389 kv.key[1] = masked_dst_address.as_u64[1];
390 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
392 rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
396 dst_adj_index = value.value;
397 dst_adj = ip_get_adjacency (lm, dst_adj_index);
401 /* For deletes destination must be known. */
404 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
405 error = clib_error_return (0, "unknown destination %U/%d",
406 format_ip6_address, dst_address,
415 /* Ignore adds of X/128 with next hop of X. */
417 && dst_address_length == 128
418 && ip6_address_is_equal (dst_address, next_hop))
420 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
421 error = clib_error_return (0, "prefix matches next hop %U/%d",
422 format_ip6_address, dst_address,
427 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
429 if (! ip_multipath_adjacency_add_del_next_hop
431 dst_adj ? dst_adj->heap_handle : ~0,
436 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
437 error = clib_error_return
438 (0, "requested deleting next-hop %U not found in multi-path",
439 format_ip6_address, next_hop);
444 if (old_mp_adj_index != ~0)
445 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
446 if (new_mp_adj_index != ~0)
447 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
449 if (old_mp != new_mp)
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 a.dst_address = dst_address[0];
458 a.dst_address_length = dst_address_length;
459 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
463 ip6_add_del_route (im, &a);
468 clib_error_report (error);
472 ip6_get_route (ip6_main_t * im,
473 u32 table_index_or_table_id,
475 ip6_address_t * address,
478 ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
479 ip6_address_t masked_address;
480 BVT(clib_bihash_kv) kv, value;
482 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
483 clib_memcpy (&masked_address, address, sizeof (masked_address));
484 ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
486 kv.key[0] = masked_address.as_u64[0];
487 kv.key[1] = masked_address.as_u64[1];
488 kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
490 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
491 return (value.value);
496 ip6_foreach_matching_route (ip6_main_t * im,
497 u32 table_index_or_table_id,
499 ip6_address_t * dst_address,
501 ip6_address_t ** results,
502 u8 ** result_lengths)
505 find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
506 BVT(clib_bihash) * h = &im->ip6_lookup_table;
507 BVT(clib_bihash_value) * v;
508 clib_bihash_bucket_t * b;
512 _vec_len (*results) = 0;
514 _vec_len (*result_lengths) = 0;
516 /* Walk the table looking for routes which match the supplied address */
517 for (i = 0; i < h->nbuckets; i++)
523 v = BV(clib_bihash_get_value) (h, b->offset);
524 for (j = 0; j < (1<<b->log2_pages); j++)
526 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
528 if (BV(clib_bihash_is_free)(&v->kvp[k]))
531 if ((v->kvp[k].key[2]
532 == (((u64)((fib - im->fibs))<<32) | address_length))
533 && ip6_destination_matches_route
534 (im, dst_address, (ip6_address_t *) &v->kvp[k],
539 a = (ip6_address_t *)(&v->kvp[k]);
541 vec_add1 (*results, a[0]);
542 vec_add1 (*result_lengths, address_length);
550 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
551 u32 table_index_or_table_id,
556 = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
558 ip_lookup_main_t * lm = &im->lookup_main;
560 if (lm->n_adjacency_remaps == 0)
563 clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
565 /* All remaps have been performed. */
566 lm->n_adjacency_remaps = 0;
569 void ip6_delete_matching_routes (ip6_main_t * im,
570 u32 table_index_or_table_id,
572 ip6_address_t * address,
575 /* $$$$ static may be OK - this should happen only on thread 0 */
576 static ip6_address_t * matching_addresses;
577 static u8 * matching_address_lengths;
579 ip6_add_del_route_args_t a;
581 vlib_smp_unsafe_warning();
583 a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
584 a.table_index_or_table_id = table_index_or_table_id;
589 for (l = address_length + 1; l <= 128; l++)
591 ip6_foreach_matching_route (im, table_index_or_table_id, flags,
595 &matching_address_lengths);
596 for (i = 0; i < vec_len (matching_addresses); i++)
598 a.dst_address = matching_addresses[i];
599 a.dst_address_length = matching_address_lengths[i];
600 ip6_add_del_route (im, &a);
604 ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
608 ip6_lookup (vlib_main_t * vm,
609 vlib_node_runtime_t * node,
610 vlib_frame_t * frame)
612 ip6_main_t * im = &ip6_main;
613 ip_lookup_main_t * lm = &im->lookup_main;
614 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
615 u32 n_left_from, n_left_to_next, * from, * to_next;
616 ip_lookup_next_t next;
617 u32 cpu_index = os_get_cpu_number();
619 from = vlib_frame_vector_args (frame);
620 n_left_from = frame->n_vectors;
621 next = node->cached_next_index;
623 while (n_left_from > 0)
625 vlib_get_next_frame (vm, node, next,
626 to_next, n_left_to_next);
628 while (n_left_from >= 4 && n_left_to_next >= 2)
630 vlib_buffer_t * p0, * p1;
631 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
632 ip_lookup_next_t next0, next1;
633 ip6_header_t * ip0, * ip1;
634 ip_adjacency_t * adj0, * adj1;
635 u32 fib_index0, fib_index1;
636 u32 flow_hash_config0, flow_hash_config1;
638 /* Prefetch next iteration. */
640 vlib_buffer_t * p2, * p3;
642 p2 = vlib_get_buffer (vm, from[2]);
643 p3 = vlib_get_buffer (vm, from[3]);
645 vlib_prefetch_buffer_header (p2, LOAD);
646 vlib_prefetch_buffer_header (p3, LOAD);
647 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
648 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
651 pi0 = to_next[0] = from[0];
652 pi1 = to_next[1] = from[1];
654 p0 = vlib_get_buffer (vm, pi0);
655 p1 = vlib_get_buffer (vm, pi1);
657 ip0 = vlib_buffer_get_current (p0);
658 ip1 = vlib_buffer_get_current (p1);
660 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
661 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
663 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
664 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
665 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
666 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
668 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
670 adj_index1 = ip6_fib_lookup_with_table (im, fib_index1,
673 adj0 = ip_get_adjacency (lm, adj_index0);
674 adj1 = ip_get_adjacency (lm, adj_index1);
676 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
678 adj_index0 = ip6_fib_lookup_with_table
679 (im, adj0->explicit_fib_index, &ip0->dst_address);
680 adj0 = ip_get_adjacency (lm, adj_index0);
682 if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
684 adj_index1 = ip6_fib_lookup_with_table
685 (im, adj1->explicit_fib_index, &ip1->dst_address);
686 adj1 = ip_get_adjacency (lm, adj_index1);
689 next0 = adj0->lookup_next_index;
690 next1 = adj1->lookup_next_index;
692 /* Process hop-by-hop options if present */
693 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
694 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
695 next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
696 IP_LOOKUP_NEXT_HOP_BY_HOP : next1;
698 vnet_buffer (p0)->ip.flow_hash =
699 vnet_buffer(p1)->ip.flow_hash = 0;
701 if (PREDICT_FALSE(adj0->n_adj > 1))
704 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
705 vnet_buffer (p0)->ip.flow_hash =
706 ip6_compute_flow_hash (ip0, flow_hash_config0);
709 if (PREDICT_FALSE(adj1->n_adj > 1))
712 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
714 vnet_buffer (p1)->ip.flow_hash =
715 ip6_compute_flow_hash (ip1, flow_hash_config1);
718 ASSERT (adj0->n_adj > 0);
719 ASSERT (adj1->n_adj > 0);
720 ASSERT (is_pow2 (adj0->n_adj));
721 ASSERT (is_pow2 (adj1->n_adj));
722 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
723 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
725 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
726 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
728 vlib_increment_combined_counter
729 (cm, cpu_index, adj_index0, 1,
730 vlib_buffer_length_in_chain (vm, p0));
731 vlib_increment_combined_counter
732 (cm, cpu_index, adj_index1, 1,
733 vlib_buffer_length_in_chain (vm, p1));
740 wrong_next = (next0 != next) + 2*(next1 != next);
741 if (PREDICT_FALSE (wrong_next != 0))
750 vlib_set_next_frame_buffer (vm, node, next0, pi0);
757 vlib_set_next_frame_buffer (vm, node, next1, pi1);
764 vlib_set_next_frame_buffer (vm, node, next0, pi0);
765 vlib_set_next_frame_buffer (vm, node, next1, pi1);
769 vlib_put_next_frame (vm, node, next, n_left_to_next);
771 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
777 while (n_left_from > 0 && n_left_to_next > 0)
782 ip_lookup_next_t next0;
783 ip_adjacency_t * adj0;
784 u32 fib_index0, flow_hash_config0;
789 p0 = vlib_get_buffer (vm, pi0);
791 ip0 = vlib_buffer_get_current (p0);
793 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
794 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
795 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
798 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
800 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
803 adj0 = ip_get_adjacency (lm, adj_index0);
805 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
807 adj_index0 = ip6_fib_lookup_with_table
808 (im, adj0->explicit_fib_index, &ip0->dst_address);
809 adj0 = ip_get_adjacency (lm, adj_index0);
812 next0 = adj0->lookup_next_index;
813 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
814 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
816 vnet_buffer (p0)->ip.flow_hash = 0;
818 if (PREDICT_FALSE(adj0->n_adj > 1))
821 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
822 vnet_buffer (p0)->ip.flow_hash =
823 ip6_compute_flow_hash (ip0, flow_hash_config0);
826 ASSERT (adj0->n_adj > 0);
827 ASSERT (is_pow2 (adj0->n_adj));
828 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
830 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
832 vlib_increment_combined_counter
833 (cm, cpu_index, adj_index0, 1,
834 vlib_buffer_length_in_chain (vm, p0));
841 if (PREDICT_FALSE (next0 != next))
844 vlib_put_next_frame (vm, node, next, n_left_to_next);
846 vlib_get_next_frame (vm, node, next,
847 to_next, n_left_to_next);
854 vlib_put_next_frame (vm, node, next, n_left_to_next);
857 return frame->n_vectors;
860 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
861 ip_adjacency_t * adj,
863 u32 if_address_index)
865 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
869 if (hw->hw_class_index == ethernet_hw_interface_class.index
870 || hw->hw_class_index == srp_hw_interface_class.index)
872 n = IP_LOOKUP_NEXT_ARP;
873 node_index = ip6_discover_neighbor_node.index;
874 adj->if_address_index = if_address_index;
878 n = IP_LOOKUP_NEXT_REWRITE;
879 node_index = ip6_rewrite_node.index;
882 adj->lookup_next_index = n;
883 adj->explicit_fib_index = ~0;
885 vnet_rewrite_for_sw_interface
887 VNET_L3_PACKET_TYPE_IP6,
890 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
891 &adj->rewrite_header,
892 sizeof (adj->rewrite_data));
896 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
897 ip6_main_t * im, u32 fib_index,
898 ip_interface_address_t * a)
900 ip_lookup_main_t * lm = &im->lookup_main;
901 ip_adjacency_t * adj;
902 ip6_address_t * address = ip_interface_address_get_address (lm, a);
903 ip6_add_del_route_args_t x;
904 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
905 u32 classify_table_index;
907 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
908 x.table_index_or_table_id = fib_index;
909 x.flags = (IP6_ROUTE_FLAG_ADD
910 | IP6_ROUTE_FLAG_FIB_INDEX
911 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
912 x.dst_address = address[0];
913 x.dst_address_length = a->address_length;
917 a->neighbor_probe_adj_index = ~0;
918 if (a->address_length < 128)
920 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
922 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
923 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
924 ip6_add_del_route (im, &x);
925 a->neighbor_probe_adj_index = x.adj_index;
928 /* Add e.g. ::1/128 as local to this host. */
929 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
932 classify_table_index = ~0;
933 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
934 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
935 if (classify_table_index != (u32) ~0)
937 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
938 adj->classify.table_index = classify_table_index;
941 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
943 adj->if_address_index = a - lm->if_address_pool;
944 adj->rewrite_header.sw_if_index = sw_if_index;
945 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
946 adj->rewrite_header.data_bytes = 0;
947 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
948 x.dst_address_length = 128;
949 ip6_add_del_route (im, &x);
953 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
954 ip6_address_t * address, u32 address_length)
956 ip6_add_del_route_args_t x;
958 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
959 x.table_index_or_table_id = fib_index;
960 x.flags = (IP6_ROUTE_FLAG_DEL
961 | IP6_ROUTE_FLAG_FIB_INDEX
962 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
963 x.dst_address = address[0];
964 x.dst_address_length = address_length;
969 if (address_length < 128)
971 /* Don't wipe out fe80::0/64 */
972 if (address_length != 64 ||
973 address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
974 ip6_add_del_route (im, &x);
977 x.dst_address_length = 128;
978 ip6_add_del_route (im, &x);
980 ip6_delete_matching_routes (im,
982 IP6_ROUTE_FLAG_FIB_INDEX,
989 ip6_address_t address;
991 } ip6_interface_address_t;
993 static clib_error_t *
994 ip6_add_del_interface_address_internal (vlib_main_t * vm,
996 ip6_address_t * new_address,
1002 static clib_error_t *
1003 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1005 ip6_address_t * address,
1011 vnet_main_t * vnm = vnet_get_main();
1012 ip6_main_t * im = &ip6_main;
1013 ip_lookup_main_t * lm = &im->lookup_main;
1014 clib_error_t * error;
1015 u32 if_address_index;
1016 ip6_address_fib_t ip6_af, * addr_fib = 0;
1018 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1019 ip6_addr_fib_init (&ip6_af, address,
1020 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1021 vec_add1 (addr_fib, ip6_af);
1024 uword elts_before = pool_elts (lm->if_address_pool);
1026 error = ip_interface_address_add_del
1036 /* Pool did not grow: add duplicate address. */
1037 if (elts_before == pool_elts (lm->if_address_pool))
1041 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1044 ip6_del_interface_routes (im, ip6_af.fib_index, address,
1048 ip6_add_interface_routes (vnm, sw_if_index,
1049 im, ip6_af.fib_index,
1050 pool_elt_at_index (lm->if_address_pool, if_address_index));
1054 ip6_add_del_interface_address_callback_t * cb;
1055 vec_foreach (cb, im->add_del_interface_address_callbacks)
1056 cb->function (im, cb->function_opaque, sw_if_index,
1057 address, address_length,
1063 vec_free (addr_fib);
1068 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1069 ip6_address_t * address, u32 address_length,
1072 return ip6_add_del_interface_address_internal
1073 (vm, sw_if_index, address, address_length,
1074 /* redistribute */ 1,
1075 /* insert_routes */ 1,
1080 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1084 ip6_main_t * im = &ip6_main;
1085 ip_interface_address_t * ia;
1087 u32 is_admin_up, fib_index;
1089 /* Fill in lookup tables with default table (0). */
1090 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1092 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1094 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1096 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1098 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1099 0 /* honor unnumbered */,
1101 a = ip_interface_address_get_address (&im->lookup_main, ia);
1103 ip6_add_interface_routes (vnm, sw_if_index,
1107 ip6_del_interface_routes (im, fib_index,
1108 a, ia->address_length);
1114 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1117 ip6_sw_interface_add_del (vnet_main_t * vnm,
1121 vlib_main_t * vm = vnm->vlib_main;
1122 ip6_main_t * im = &ip6_main;
1123 ip_lookup_main_t * lm = &im->lookup_main;
1126 for (cast = 0; cast < VNET_N_CAST; cast++)
1128 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1129 vnet_config_main_t * vcm = &cm->config_main;
1131 /* FIXME multicast. */
1132 if (! vcm->node_index_by_feature_index)
1134 char * start_nodes[] = { "ip6-input", };
1135 char * feature_nodes[] = {
1136 [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1137 [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1138 [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1139 [IP6_RX_FEATURE_VPATH] = "vpath-input-ip6",
1140 [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1142 vnet_config_init (vm, vcm,
1143 start_nodes, ARRAY_LEN (start_nodes),
1144 feature_nodes, ARRAY_LEN (feature_nodes));
1147 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1148 ci = cm->config_index_by_sw_if_index[sw_if_index];
1151 ci = vnet_config_add_feature (vm, vcm,
1153 IP6_RX_FEATURE_LOOKUP,
1154 /* config data */ 0,
1155 /* # bytes of config data */ 0);
1157 ci = vnet_config_del_feature (vm, vcm,
1159 IP6_RX_FEATURE_LOOKUP,
1160 /* config data */ 0,
1161 /* # bytes of config data */ 0);
1163 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1165 return /* no error */ 0;
1168 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1170 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1171 .function = ip6_lookup,
1172 .name = "ip6-lookup",
1173 .vector_size = sizeof (u32),
1175 .n_next_nodes = IP_LOOKUP_N_NEXT,
1176 .next_nodes = IP6_LOOKUP_NEXT_NODES,
1180 /* Adjacency taken. */
1185 /* Packet data, possibly *after* rewrite. */
1186 u8 packet_data[64 - 1*sizeof(u32)];
1187 } ip6_forward_next_trace_t;
1189 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1191 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1192 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1193 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1194 vnet_main_t * vnm = vnet_get_main();
1195 ip6_main_t * im = &ip6_main;
1196 ip_adjacency_t * adj;
1197 uword indent = format_get_indent (s);
1199 adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1200 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1201 t->fib_index, t->adj_index, format_ip_adjacency,
1202 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1203 switch (adj->lookup_next_index)
1205 case IP_LOOKUP_NEXT_REWRITE:
1206 s = format (s, "\n%U%U",
1207 format_white_space, indent,
1208 format_ip_adjacency_packet_data,
1209 vnm, &im->lookup_main, t->adj_index,
1210 t->packet_data, sizeof (t->packet_data));
1220 /* Common trace function for all ip6-forward next nodes. */
1222 ip6_forward_next_trace (vlib_main_t * vm,
1223 vlib_node_runtime_t * node,
1224 vlib_frame_t * frame,
1225 vlib_rx_or_tx_t which_adj_index)
1228 ip6_main_t * im = &ip6_main;
1230 n_left = frame->n_vectors;
1231 from = vlib_frame_vector_args (frame);
1236 vlib_buffer_t * b0, * b1;
1237 ip6_forward_next_trace_t * t0, * t1;
1239 /* Prefetch next iteration. */
1240 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1241 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1246 b0 = vlib_get_buffer (vm, bi0);
1247 b1 = vlib_get_buffer (vm, bi1);
1249 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1251 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1252 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1253 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1254 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1255 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1256 clib_memcpy (t0->packet_data,
1257 vlib_buffer_get_current (b0),
1258 sizeof (t0->packet_data));
1260 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1262 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1263 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1264 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1265 t1->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1266 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1267 clib_memcpy (t1->packet_data,
1268 vlib_buffer_get_current (b1),
1269 sizeof (t1->packet_data));
1279 ip6_forward_next_trace_t * t0;
1283 b0 = vlib_get_buffer (vm, bi0);
1285 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1287 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1288 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1289 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1290 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1291 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1292 clib_memcpy (t0->packet_data,
1293 vlib_buffer_get_current (b0),
1294 sizeof (t0->packet_data));
1302 ip6_drop_or_punt (vlib_main_t * vm,
1303 vlib_node_runtime_t * node,
1304 vlib_frame_t * frame,
1305 ip6_error_t error_code)
1307 u32 * buffers = vlib_frame_vector_args (frame);
1308 uword n_packets = frame->n_vectors;
1310 vlib_error_drop_buffers (vm, node,
1315 ip6_input_node.index,
1318 if (node->flags & VLIB_NODE_FLAG_TRACE)
1319 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1325 ip6_drop (vlib_main_t * vm,
1326 vlib_node_runtime_t * node,
1327 vlib_frame_t * frame)
1328 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1331 ip6_punt (vlib_main_t * vm,
1332 vlib_node_runtime_t * node,
1333 vlib_frame_t * frame)
1334 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1337 ip6_miss (vlib_main_t * vm,
1338 vlib_node_runtime_t * node,
1339 vlib_frame_t * frame)
1340 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1342 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1343 .function = ip6_drop,
1345 .vector_size = sizeof (u32),
1347 .format_trace = format_ip6_forward_next_trace,
1355 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1356 .function = ip6_punt,
1358 .vector_size = sizeof (u32),
1360 .format_trace = format_ip6_forward_next_trace,
1368 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1369 .function = ip6_miss,
1371 .vector_size = sizeof (u32),
1373 .format_trace = format_ip6_forward_next_trace,
1381 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1382 .function = ip6_drop,
1383 .name = "ip6-multicast",
1384 .vector_size = sizeof (u32),
1386 .format_trace = format_ip6_forward_next_trace,
1394 /* Compute TCP/UDP/ICMP6 checksum in software. */
1395 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1398 u16 sum16, payload_length_host_byte_order;
1399 u32 i, n_this_buffer, n_bytes_left;
1400 u32 headers_size = sizeof(ip0[0]);
1401 void * data_this_buffer;
1403 ASSERT(bogus_lengthp);
1406 /* Initialize checksum with ip header. */
1407 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1408 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1409 data_this_buffer = (void *) (ip0 + 1);
1411 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1413 sum0 = ip_csum_with_carry (sum0,
1414 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1415 sum0 = ip_csum_with_carry (sum0,
1416 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1419 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1420 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1423 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1425 /* validate really icmp6 next */
1426 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1428 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1429 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1431 payload_length_host_byte_order -= skip_bytes;
1432 headers_size += skip_bytes;
1435 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1439 struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1440 u8 nb_segs = mb->nb_segs;
1442 n_this_buffer = (p0->current_length > headers_size ?
1443 p0->current_length - headers_size : 0);
1444 while (n_bytes_left)
1446 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1447 n_bytes_left -= n_this_buffer;
1451 if ((nb_segs == 0) || (mb == 0))
1454 data_this_buffer = rte_ctrlmbuf_data(mb);
1455 n_this_buffer = mb->data_len;
1457 if (n_bytes_left || nb_segs)
1463 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1465 if (p0 && n_this_buffer + headers_size > p0->current_length)
1466 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1469 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1470 n_bytes_left -= n_this_buffer;
1471 if (n_bytes_left == 0)
1474 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1479 p0 = vlib_get_buffer (vm, p0->next_buffer);
1480 data_this_buffer = vlib_buffer_get_current (p0);
1481 n_this_buffer = p0->current_length;
1485 sum16 = ~ ip_csum_fold (sum0);
1490 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1492 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1493 udp_header_t * udp0;
1497 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1498 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1499 || ip0->protocol == IP_PROTOCOL_ICMP6
1500 || ip0->protocol == IP_PROTOCOL_UDP
1501 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1503 udp0 = (void *) (ip0 + 1);
1504 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1506 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1507 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1511 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1513 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1514 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1520 ip6_local (vlib_main_t * vm,
1521 vlib_node_runtime_t * node,
1522 vlib_frame_t * frame)
1524 ip6_main_t * im = &ip6_main;
1525 ip_lookup_main_t * lm = &im->lookup_main;
1526 ip_local_next_t next_index;
1527 u32 * from, * to_next, n_left_from, n_left_to_next;
1528 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1530 from = vlib_frame_vector_args (frame);
1531 n_left_from = frame->n_vectors;
1532 next_index = node->cached_next_index;
1534 if (node->flags & VLIB_NODE_FLAG_TRACE)
1535 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1537 while (n_left_from > 0)
1539 vlib_get_next_frame (vm, node, next_index,
1540 to_next, n_left_to_next);
1542 while (n_left_from >= 4 && n_left_to_next >= 2)
1544 vlib_buffer_t * p0, * p1;
1545 ip6_header_t * ip0, * ip1;
1546 udp_header_t * udp0, * udp1;
1547 u32 pi0, ip_len0, udp_len0, flags0, next0;
1548 u32 pi1, ip_len1, udp_len1, flags1, next1;
1549 i32 len_diff0, len_diff1;
1550 u8 error0, type0, good_l4_checksum0;
1551 u8 error1, type1, good_l4_checksum1;
1553 pi0 = to_next[0] = from[0];
1554 pi1 = to_next[1] = from[1];
1558 n_left_to_next -= 2;
1560 p0 = vlib_get_buffer (vm, pi0);
1561 p1 = vlib_get_buffer (vm, pi1);
1563 ip0 = vlib_buffer_get_current (p0);
1564 ip1 = vlib_buffer_get_current (p1);
1566 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1567 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1569 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1570 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1575 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1576 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1578 udp0 = ip6_next_header (ip0);
1579 udp1 = ip6_next_header (ip1);
1581 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1582 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1583 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1585 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1586 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1588 /* Verify UDP length. */
1589 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1590 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1591 udp_len0 = clib_net_to_host_u16 (udp0->length);
1592 udp_len1 = clib_net_to_host_u16 (udp1->length);
1594 len_diff0 = ip_len0 - udp_len0;
1595 len_diff1 = ip_len1 - udp_len1;
1597 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1598 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1600 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1601 && ! good_l4_checksum0
1602 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1604 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1606 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1608 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1609 && ! good_l4_checksum1
1610 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1612 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1614 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1617 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1619 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1620 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1622 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1623 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1624 error0 = (! good_l4_checksum0
1625 ? IP6_ERROR_UDP_CHECKSUM + type0
1627 error1 = (! good_l4_checksum1
1628 ? IP6_ERROR_UDP_CHECKSUM + type1
1631 /* Drop packets from unroutable hosts. */
1632 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1633 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1635 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1636 error0 = (lm->miss_adj_index == src_adj_index0
1637 ? IP6_ERROR_SRC_LOOKUP_MISS
1640 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1642 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1643 error1 = (lm->miss_adj_index == src_adj_index1
1644 ? IP6_ERROR_SRC_LOOKUP_MISS
1648 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1649 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1651 p0->error = error_node->errors[error0];
1652 p1->error = error_node->errors[error1];
1654 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1655 to_next, n_left_to_next,
1656 pi0, pi1, next0, next1);
1659 while (n_left_from > 0 && n_left_to_next > 0)
1663 udp_header_t * udp0;
1664 u32 pi0, ip_len0, udp_len0, flags0, next0;
1666 u8 error0, type0, good_l4_checksum0;
1668 pi0 = to_next[0] = from[0];
1672 n_left_to_next -= 1;
1674 p0 = vlib_get_buffer (vm, pi0);
1676 ip0 = vlib_buffer_get_current (p0);
1678 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1679 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1683 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1685 udp0 = ip6_next_header (ip0);
1687 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1688 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1690 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1692 /* Verify UDP length. */
1693 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1694 udp_len0 = clib_net_to_host_u16 (udp0->length);
1696 len_diff0 = ip_len0 - udp_len0;
1698 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1700 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1701 && ! good_l4_checksum0
1702 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1704 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1706 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1709 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1711 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1713 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1714 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1715 error0 = (! good_l4_checksum0
1716 ? IP6_ERROR_UDP_CHECKSUM + type0
1719 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1720 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1722 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1723 error0 = (lm->miss_adj_index == src_adj_index0
1724 ? IP6_ERROR_SRC_LOOKUP_MISS
1728 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1730 p0->error = error_node->errors[error0];
1732 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1733 to_next, n_left_to_next,
1737 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1740 return frame->n_vectors;
1743 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1744 .function = ip6_local,
1745 .name = "ip6-local",
1746 .vector_size = sizeof (u32),
1748 .format_trace = format_ip6_forward_next_trace,
1750 .n_next_nodes = IP_LOCAL_N_NEXT,
1752 [IP_LOCAL_NEXT_DROP] = "error-drop",
1753 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1754 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1755 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1759 void ip6_register_protocol (u32 protocol, u32 node_index)
1761 vlib_main_t * vm = vlib_get_main();
1762 ip6_main_t * im = &ip6_main;
1763 ip_lookup_main_t * lm = &im->lookup_main;
1765 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1766 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1770 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1771 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1772 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1773 } ip6_discover_neighbor_next_t;
1776 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1777 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1778 } ip6_discover_neighbor_error_t;
1781 ip6_discover_neighbor (vlib_main_t * vm,
1782 vlib_node_runtime_t * node,
1783 vlib_frame_t * frame)
1785 vnet_main_t * vnm = vnet_get_main();
1786 ip6_main_t * im = &ip6_main;
1787 ip_lookup_main_t * lm = &im->lookup_main;
1788 u32 * from, * to_next_drop;
1789 uword n_left_from, n_left_to_next_drop;
1790 static f64 time_last_seed_change = -1e100;
1791 static u32 hash_seeds[3];
1792 static uword hash_bitmap[256 / BITS (uword)];
1796 if (node->flags & VLIB_NODE_FLAG_TRACE)
1797 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1799 time_now = vlib_time_now (vm);
1800 if (time_now - time_last_seed_change > 1e-3)
1803 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1804 sizeof (hash_seeds));
1805 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1806 hash_seeds[i] = r[i];
1808 /* Mark all hash keys as been not-seen before. */
1809 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1812 time_last_seed_change = time_now;
1815 from = vlib_frame_vector_args (frame);
1816 n_left_from = frame->n_vectors;
1818 while (n_left_from > 0)
1820 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1821 to_next_drop, n_left_to_next_drop);
1823 while (n_left_from > 0 && n_left_to_next_drop > 0)
1827 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1829 ip_adjacency_t * adj0;
1830 vnet_hw_interface_t * hw_if0;
1835 p0 = vlib_get_buffer (vm, pi0);
1837 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1839 ip0 = vlib_buffer_get_current (p0);
1841 adj0 = ip_get_adjacency (lm, adj_index0);
1847 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1848 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1851 b0 ^= ip0->dst_address.as_u32[0];
1852 c0 ^= ip0->dst_address.as_u32[1];
1854 hash_v3_mix32 (a0, b0, c0);
1856 b0 ^= ip0->dst_address.as_u32[2];
1857 c0 ^= ip0->dst_address.as_u32[3];
1859 hash_v3_finalize32 (a0, b0, c0);
1861 c0 &= BITS (hash_bitmap) - 1;
1862 c0 = c0 / BITS (uword);
1863 m0 = (uword) 1 << (c0 % BITS (uword));
1865 bm0 = hash_bitmap[c0];
1866 drop0 = (bm0 & m0) != 0;
1868 /* Mark it as seen. */
1869 hash_bitmap[c0] = bm0 | m0;
1873 to_next_drop[0] = pi0;
1875 n_left_to_next_drop -= 1;
1877 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1879 /* If the interface is link-down, drop the pkt */
1880 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1884 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1885 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1891 icmp6_neighbor_solicitation_header_t * h0;
1894 h0 = vlib_packet_template_get_packet
1895 (vm, &im->discover_neighbor_packet_template, &bi0);
1898 * Build ethernet header.
1899 * Choose source address based on destination lookup
1902 ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
1906 * Destination address is a solicited node multicast address.
1907 * We need to fill in
1908 * the low 24 bits with low 24 bits of target's address.
1910 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1911 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1912 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1914 h0->neighbor.target_address = ip0->dst_address;
1916 clib_memcpy (h0->link_layer_option.ethernet_address,
1917 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1919 /* $$$$ appears we need this; why is the checksum non-zero? */
1920 h0->neighbor.icmp.checksum = 0;
1921 h0->neighbor.icmp.checksum =
1922 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1925 ASSERT (bogus_length == 0);
1927 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1928 b0 = vlib_get_buffer (vm, bi0);
1929 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1930 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1932 /* Add rewrite/encap string. */
1933 vnet_rewrite_one_header (adj0[0], h0,
1934 sizeof (ethernet_header_t));
1935 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1937 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1939 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1943 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1944 n_left_to_next_drop);
1947 return frame->n_vectors;
1950 static char * ip6_discover_neighbor_error_strings[] = {
1951 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1952 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
1953 = "neighbor solicitations sent",
1956 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
1957 .function = ip6_discover_neighbor,
1958 .name = "ip6-discover-neighbor",
1959 .vector_size = sizeof (u32),
1961 .format_trace = format_ip6_forward_next_trace,
1963 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1964 .error_strings = ip6_discover_neighbor_error_strings,
1966 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1968 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1969 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1974 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1976 vnet_main_t * vnm = vnet_get_main();
1977 ip6_main_t * im = &ip6_main;
1978 icmp6_neighbor_solicitation_header_t * h;
1979 ip6_address_t * src;
1980 ip_interface_address_t * ia;
1981 ip_adjacency_t * adj;
1982 vnet_hw_interface_t * hi;
1983 vnet_sw_interface_t * si;
1988 si = vnet_get_sw_interface (vnm, sw_if_index);
1990 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1992 return clib_error_return (0, "%U: interface %U down",
1993 format_ip6_address, dst,
1994 format_vnet_sw_if_index_name, vnm,
1998 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2001 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2002 return clib_error_return
2003 (0, "no matching interface address for destination %U (interface %U)",
2004 format_ip6_address, dst,
2005 format_vnet_sw_if_index_name, vnm, sw_if_index);
2008 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2010 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2012 /* Destination address is a solicited node multicast address. We need to fill in
2013 the low 24 bits with low 24 bits of target's address. */
2014 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2015 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2016 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2018 h->ip.src_address = src[0];
2019 h->neighbor.target_address = dst[0];
2021 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2023 h->neighbor.icmp.checksum =
2024 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2025 ASSERT(bogus_length == 0);
2027 b = vlib_get_buffer (vm, bi);
2028 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2030 /* Add encapsulation string for software interface (e.g. ethernet header). */
2031 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2032 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2033 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2036 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2037 u32 * to_next = vlib_frame_vector_args (f);
2040 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2043 return /* no error */ 0;
2047 IP6_REWRITE_NEXT_DROP,
2048 } ip6_rewrite_next_t;
2051 ip6_rewrite_inline (vlib_main_t * vm,
2052 vlib_node_runtime_t * node,
2053 vlib_frame_t * frame,
2054 int rewrite_for_locally_received_packets)
2056 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2057 u32 * from = vlib_frame_vector_args (frame);
2058 u32 n_left_from, n_left_to_next, * to_next, next_index;
2059 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2060 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2062 n_left_from = frame->n_vectors;
2063 next_index = node->cached_next_index;
2064 u32 cpu_index = os_get_cpu_number();
2066 while (n_left_from > 0)
2068 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2070 while (n_left_from >= 4 && n_left_to_next >= 2)
2072 ip_adjacency_t * adj0, * adj1;
2073 vlib_buffer_t * p0, * p1;
2074 ip6_header_t * ip0, * ip1;
2075 u32 pi0, rw_len0, next0, error0, adj_index0;
2076 u32 pi1, rw_len1, next1, error1, adj_index1;
2078 /* Prefetch next iteration. */
2080 vlib_buffer_t * p2, * p3;
2082 p2 = vlib_get_buffer (vm, from[2]);
2083 p3 = vlib_get_buffer (vm, from[3]);
2085 vlib_prefetch_buffer_header (p2, LOAD);
2086 vlib_prefetch_buffer_header (p3, LOAD);
2088 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2089 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2091 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2092 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2095 pi0 = to_next[0] = from[0];
2096 pi1 = to_next[1] = from[1];
2101 n_left_to_next -= 2;
2103 p0 = vlib_get_buffer (vm, pi0);
2104 p1 = vlib_get_buffer (vm, pi1);
2106 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2107 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2109 /* We should never rewrite a pkt using the MISS adjacency */
2110 ASSERT(adj_index0 && adj_index1);
2112 ip0 = vlib_buffer_get_current (p0);
2113 ip1 = vlib_buffer_get_current (p1);
2115 error0 = error1 = IP6_ERROR_NONE;
2117 if (! rewrite_for_locally_received_packets)
2119 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2121 /* Input node should have reject packets with hop limit 0. */
2122 ASSERT (ip0->hop_limit > 0);
2123 ASSERT (ip1->hop_limit > 0);
2128 ip0->hop_limit = hop_limit0;
2129 ip1->hop_limit = hop_limit1;
2131 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2132 error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2135 adj0 = ip_get_adjacency (lm, adj_index0);
2136 adj1 = ip_get_adjacency (lm, adj_index1);
2138 if (rewrite_for_locally_received_packets)
2141 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2142 * we end up here with a local adjacency in hand
2144 if (PREDICT_FALSE(adj0->lookup_next_index
2145 == IP_LOOKUP_NEXT_LOCAL))
2146 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2147 if (PREDICT_FALSE(adj1->lookup_next_index
2148 == IP_LOOKUP_NEXT_LOCAL))
2149 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2152 rw_len0 = adj0[0].rewrite_header.data_bytes;
2153 rw_len1 = adj1[0].rewrite_header.data_bytes;
2155 vlib_increment_combined_counter (&lm->adjacency_counters,
2158 /* packet increment */ 0,
2159 /* byte increment */ rw_len0);
2160 vlib_increment_combined_counter (&lm->adjacency_counters,
2163 /* packet increment */ 0,
2164 /* byte increment */ rw_len1);
2166 /* Check MTU of outgoing interface. */
2167 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2168 ? IP6_ERROR_MTU_EXCEEDED
2170 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2171 ? IP6_ERROR_MTU_EXCEEDED
2174 p0->current_data -= rw_len0;
2175 p1->current_data -= rw_len1;
2177 p0->current_length += rw_len0;
2178 p1->current_length += rw_len1;
2180 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2181 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2183 next0 = (error0 == IP6_ERROR_NONE) ?
2184 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2185 next1 = (error1 == IP6_ERROR_NONE) ?
2186 adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2188 /* Guess we are only writing on simple Ethernet header. */
2189 vnet_rewrite_two_headers (adj0[0], adj1[0],
2191 sizeof (ethernet_header_t));
2193 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2194 to_next, n_left_to_next,
2195 pi0, pi1, next0, next1);
2198 while (n_left_from > 0 && n_left_to_next > 0)
2200 ip_adjacency_t * adj0;
2204 u32 adj_index0, next0, error0;
2206 pi0 = to_next[0] = from[0];
2208 p0 = vlib_get_buffer (vm, pi0);
2210 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2212 /* We should never rewrite a pkt using the MISS adjacency */
2215 adj0 = ip_get_adjacency (lm, adj_index0);
2217 ip0 = vlib_buffer_get_current (p0);
2219 error0 = IP6_ERROR_NONE;
2221 /* Check hop limit */
2222 if (! rewrite_for_locally_received_packets)
2224 i32 hop_limit0 = ip0->hop_limit;
2226 ASSERT (ip0->hop_limit > 0);
2230 ip0->hop_limit = hop_limit0;
2232 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2235 if (rewrite_for_locally_received_packets)
2237 if (PREDICT_FALSE(adj0->lookup_next_index
2238 == IP_LOOKUP_NEXT_LOCAL))
2239 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2242 /* Guess we are only writing on simple Ethernet header. */
2243 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2245 /* Update packet buffer attributes/set output interface. */
2246 rw_len0 = adj0[0].rewrite_header.data_bytes;
2248 vlib_increment_combined_counter (&lm->adjacency_counters,
2251 /* packet increment */ 0,
2252 /* byte increment */ rw_len0);
2254 /* Check MTU of outgoing interface. */
2255 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2256 ? IP6_ERROR_MTU_EXCEEDED
2259 p0->current_data -= rw_len0;
2260 p0->current_length += rw_len0;
2261 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2263 next0 = (error0 == IP6_ERROR_NONE) ?
2264 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2266 p0->error = error_node->errors[error0];
2271 n_left_to_next -= 1;
2273 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2274 to_next, n_left_to_next,
2278 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2281 /* Need to do trace after rewrites to pick up new packet data. */
2282 if (node->flags & VLIB_NODE_FLAG_TRACE)
2283 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2285 return frame->n_vectors;
2289 ip6_rewrite_transit (vlib_main_t * vm,
2290 vlib_node_runtime_t * node,
2291 vlib_frame_t * frame)
2293 return ip6_rewrite_inline (vm, node, frame,
2294 /* rewrite_for_locally_received_packets */ 0);
2298 ip6_rewrite_local (vlib_main_t * vm,
2299 vlib_node_runtime_t * node,
2300 vlib_frame_t * frame)
2302 return ip6_rewrite_inline (vm, node, frame,
2303 /* rewrite_for_locally_received_packets */ 1);
2306 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2307 .function = ip6_rewrite_transit,
2308 .name = "ip6-rewrite",
2309 .vector_size = sizeof (u32),
2311 .format_trace = format_ip6_forward_next_trace,
2315 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2319 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2320 .function = ip6_rewrite_local,
2321 .name = "ip6-rewrite-local",
2322 .vector_size = sizeof (u32),
2324 .sibling_of = "ip6-rewrite",
2326 .format_trace = format_ip6_forward_next_trace,
2330 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2334 /* Global IP6 main. */
2335 ip6_main_t ip6_main;
2337 static clib_error_t *
2338 ip6_lookup_init (vlib_main_t * vm)
2340 ip6_main_t * im = &ip6_main;
2343 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2350 for (j = 0; j < i0; j++)
2351 im->fib_masks[i].as_u32[j] = ~0;
2354 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2357 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2359 if (im->lookup_table_nbuckets == 0)
2360 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2362 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2364 if (im->lookup_table_size == 0)
2365 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2367 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2368 im->lookup_table_nbuckets,
2369 im->lookup_table_size);
2371 /* Create FIB with index 0 and table id of 0. */
2372 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2376 pn = pg_get_node (ip6_lookup_node.index);
2377 pn->unformat_edit = unformat_pg_ip6_header;
2381 icmp6_neighbor_solicitation_header_t p;
2383 memset (&p, 0, sizeof (p));
2385 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2386 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2387 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2388 p.ip.protocol = IP_PROTOCOL_ICMP6;
2389 p.ip.hop_limit = 255;
2390 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2392 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2394 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2395 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2397 vlib_packet_template_init (vm,
2398 &im->discover_neighbor_packet_template,
2400 /* alloc chunk size */ 8,
2401 "ip6 neighbor discovery");
2407 VLIB_INIT_FUNCTION (ip6_lookup_init);
2409 static clib_error_t *
2410 add_del_ip6_interface_table (vlib_main_t * vm,
2411 unformat_input_t * input,
2412 vlib_cli_command_t * cmd)
2414 vnet_main_t * vnm = vnet_get_main();
2415 clib_error_t * error = 0;
2416 u32 sw_if_index, table_id;
2420 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2422 error = clib_error_return (0, "unknown interface `%U'",
2423 format_unformat_error, input);
2427 if (unformat (input, "%d", &table_id))
2431 error = clib_error_return (0, "expected table id `%U'",
2432 format_unformat_error, input);
2437 ip6_main_t * im = &ip6_main;
2439 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2443 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2444 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2452 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2453 .path = "set interface ip6 table",
2454 .function = add_del_ip6_interface_table,
2455 .short_help = "set interface ip6 table <intfc> <table-id>"
2459 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2462 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2463 /* Invert the "u" bit */
2464 ip->as_u8 [8] = mac[0] ^ (1<<1);
2465 ip->as_u8 [9] = mac[1];
2466 ip->as_u8 [10] = mac[2];
2467 ip->as_u8 [11] = 0xFF;
2468 ip->as_u8 [12] = 0xFE;
2469 ip->as_u8 [13] = mac[3];
2470 ip->as_u8 [14] = mac[4];
2471 ip->as_u8 [15] = mac[5];
2475 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
2478 /* Invert the previously inverted "u" bit */
2479 mac[0] = ip->as_u8 [8] ^ (1<<1);
2480 mac[1] = ip->as_u8 [9];
2481 mac[2] = ip->as_u8 [10];
2482 mac[3] = ip->as_u8 [13];
2483 mac[4] = ip->as_u8 [14];
2484 mac[5] = ip->as_u8 [15];
2487 static clib_error_t *
2488 test_ip6_link_command_fn (vlib_main_t * vm,
2489 unformat_input_t * input,
2490 vlib_cli_command_t * cmd)
2493 ip6_address_t _a, *a = &_a;
2495 if (unformat (input, "%U", unformat_ethernet_address, mac))
2497 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2498 vlib_cli_output (vm, "Link local address: %U",
2499 format_ip6_address, a);
2500 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2501 vlib_cli_output (vm, "Original MAC address: %U",
2502 format_ethernet_address, mac);
2508 VLIB_CLI_COMMAND (test_link_command, static) = {
2509 .path = "test ip6 link",
2510 .function = test_ip6_link_command_fn,
2511 .short_help = "test ip6 link <mac-address>",
2514 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2516 ip6_main_t * im6 = &ip6_main;
2518 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2523 fib = vec_elt_at_index (im6->fibs, p[0]);
2525 fib->flow_hash_config = flow_hash_config;
2529 static clib_error_t *
2530 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2531 unformat_input_t * input,
2532 vlib_cli_command_t * cmd)
2536 u32 flow_hash_config = 0;
2539 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2540 if (unformat (input, "table %d", &table_id))
2543 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2544 foreach_flow_hash_bit
2550 return clib_error_return (0, "unknown input `%U'",
2551 format_unformat_error, input);
2553 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2560 return clib_error_return (0, "no such FIB table %d", table_id);
2563 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2570 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2571 .path = "set ip6 flow-hash",
2573 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2574 .function = set_ip6_flow_hash_command_fn,
2577 static clib_error_t *
2578 show_ip6_local_command_fn (vlib_main_t * vm,
2579 unformat_input_t * input,
2580 vlib_cli_command_t * cmd)
2582 ip6_main_t * im = &ip6_main;
2583 ip_lookup_main_t * lm = &im->lookup_main;
2586 vlib_cli_output (vm, "Protocols handled by ip6_local");
2587 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2589 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2590 vlib_cli_output (vm, "%d", i);
2597 VLIB_CLI_COMMAND (show_ip_local, static) = {
2598 .path = "show ip6 local",
2599 .function = show_ip6_local_command_fn,
2600 .short_help = "Show ip6 local protocol table",
2603 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2606 vnet_main_t * vnm = vnet_get_main();
2607 vnet_interface_main_t * im = &vnm->interface_main;
2608 ip6_main_t * ipm = &ip6_main;
2609 ip_lookup_main_t * lm = &ipm->lookup_main;
2610 vnet_classify_main_t * cm = &vnet_classify_main;
2612 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2613 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2615 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2616 return VNET_API_ERROR_NO_SUCH_ENTRY;
2618 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2619 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2624 static clib_error_t *
2625 set_ip6_classify_command_fn (vlib_main_t * vm,
2626 unformat_input_t * input,
2627 vlib_cli_command_t * cmd)
2629 u32 table_index = ~0;
2630 int table_index_set = 0;
2631 u32 sw_if_index = ~0;
2634 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2635 if (unformat (input, "table-index %d", &table_index))
2636 table_index_set = 1;
2637 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2638 vnet_get_main(), &sw_if_index))
2644 if (table_index_set == 0)
2645 return clib_error_return (0, "classify table-index must be specified");
2647 if (sw_if_index == ~0)
2648 return clib_error_return (0, "interface / subif must be specified");
2650 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2657 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2658 return clib_error_return (0, "No such interface");
2660 case VNET_API_ERROR_NO_SUCH_ENTRY:
2661 return clib_error_return (0, "No such classifier table");
2666 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2667 .path = "set ip6 classify",
2669 "set ip6 classify intfc <int> table-index <index>",
2670 .function = set_ip6_classify_command_fn,
2673 static clib_error_t *
2674 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2676 ip6_main_t * im = &ip6_main;
2681 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2682 if (unformat (input, "hash-buckets %d", &tmp))
2684 else if (unformat (input, "heap-size %dm", &tmp))
2685 heapsize = ((u64)tmp) << 20;
2686 else if (unformat (input, "heap-size %dM", &tmp))
2687 heapsize = ((u64)tmp) << 20;
2688 else if (unformat (input, "heap-size %dg", &tmp))
2689 heapsize = ((u64)tmp) << 30;
2690 else if (unformat (input, "heap-size %dG", &tmp))
2691 heapsize = ((u64)tmp) << 30;
2693 return clib_error_return (0, "unknown input '%U'",
2694 format_unformat_error, input);
2697 im->lookup_table_nbuckets = nbuckets;
2698 im->lookup_table_size = heapsize;
2703 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");