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 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,
1177 [IP_LOOKUP_NEXT_MISS] = "ip6-miss",
1178 [IP_LOOKUP_NEXT_DROP] = "ip6-drop",
1179 [IP_LOOKUP_NEXT_PUNT] = "ip6-punt",
1180 [IP_LOOKUP_NEXT_LOCAL] = "ip6-local",
1181 [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor",
1182 [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite",
1183 [IP_LOOKUP_NEXT_CLASSIFY] = "ip6-classify",
1184 [IP_LOOKUP_NEXT_MAP] = "ip6-map",
1185 [IP_LOOKUP_NEXT_MAP_T] = "ip6-map-t",
1186 [IP_LOOKUP_NEXT_SIXRD] = "ip6-sixrd",
1187 [IP_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop",
1188 [IP_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop",
1189 [IP_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop",
1194 /* Adjacency taken. */
1199 /* Packet data, possibly *after* rewrite. */
1200 u8 packet_data[64 - 1*sizeof(u32)];
1201 } ip6_forward_next_trace_t;
1203 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1205 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1206 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1207 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1208 vnet_main_t * vnm = vnet_get_main();
1209 ip6_main_t * im = &ip6_main;
1210 ip_adjacency_t * adj;
1211 uword indent = format_get_indent (s);
1213 adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1214 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1215 t->fib_index, t->adj_index, format_ip_adjacency,
1216 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1217 switch (adj->lookup_next_index)
1219 case IP_LOOKUP_NEXT_REWRITE:
1220 s = format (s, "\n%U%U",
1221 format_white_space, indent,
1222 format_ip_adjacency_packet_data,
1223 vnm, &im->lookup_main, t->adj_index,
1224 t->packet_data, sizeof (t->packet_data));
1234 /* Common trace function for all ip6-forward next nodes. */
1236 ip6_forward_next_trace (vlib_main_t * vm,
1237 vlib_node_runtime_t * node,
1238 vlib_frame_t * frame,
1239 vlib_rx_or_tx_t which_adj_index)
1242 ip6_main_t * im = &ip6_main;
1244 n_left = frame->n_vectors;
1245 from = vlib_frame_vector_args (frame);
1250 vlib_buffer_t * b0, * b1;
1251 ip6_forward_next_trace_t * t0, * t1;
1253 /* Prefetch next iteration. */
1254 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1255 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1260 b0 = vlib_get_buffer (vm, bi0);
1261 b1 = vlib_get_buffer (vm, bi1);
1263 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1265 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1266 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1267 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1268 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1269 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1270 memcpy (t0->packet_data,
1271 vlib_buffer_get_current (b0),
1272 sizeof (t0->packet_data));
1274 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1276 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1277 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1278 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1279 t1->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1280 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1281 memcpy (t1->packet_data,
1282 vlib_buffer_get_current (b1),
1283 sizeof (t1->packet_data));
1293 ip6_forward_next_trace_t * t0;
1297 b0 = vlib_get_buffer (vm, bi0);
1299 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1301 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1302 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1303 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1304 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1305 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1306 memcpy (t0->packet_data,
1307 vlib_buffer_get_current (b0),
1308 sizeof (t0->packet_data));
1316 ip6_drop_or_punt (vlib_main_t * vm,
1317 vlib_node_runtime_t * node,
1318 vlib_frame_t * frame,
1319 ip6_error_t error_code)
1321 u32 * buffers = vlib_frame_vector_args (frame);
1322 uword n_packets = frame->n_vectors;
1324 vlib_error_drop_buffers (vm, node,
1329 ip6_input_node.index,
1332 if (node->flags & VLIB_NODE_FLAG_TRACE)
1333 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1339 ip6_drop (vlib_main_t * vm,
1340 vlib_node_runtime_t * node,
1341 vlib_frame_t * frame)
1342 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1345 ip6_punt (vlib_main_t * vm,
1346 vlib_node_runtime_t * node,
1347 vlib_frame_t * frame)
1348 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1351 ip6_miss (vlib_main_t * vm,
1352 vlib_node_runtime_t * node,
1353 vlib_frame_t * frame)
1354 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1356 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1357 .function = ip6_drop,
1359 .vector_size = sizeof (u32),
1361 .format_trace = format_ip6_forward_next_trace,
1369 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1370 .function = ip6_punt,
1372 .vector_size = sizeof (u32),
1374 .format_trace = format_ip6_forward_next_trace,
1382 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1383 .function = ip6_miss,
1385 .vector_size = sizeof (u32),
1387 .format_trace = format_ip6_forward_next_trace,
1395 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1396 .function = ip6_drop,
1397 .name = "ip6-multicast",
1398 .vector_size = sizeof (u32),
1400 .format_trace = format_ip6_forward_next_trace,
1408 /* Compute TCP/UDP/ICMP6 checksum in software. */
1409 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1412 u16 sum16, payload_length_host_byte_order;
1413 u32 i, n_this_buffer, n_bytes_left;
1414 u32 headers_size = sizeof(ip0[0]);
1415 void * data_this_buffer;
1417 ASSERT(bogus_lengthp);
1420 /* Initialize checksum with ip header. */
1421 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1422 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1423 data_this_buffer = (void *) (ip0 + 1);
1425 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1427 sum0 = ip_csum_with_carry (sum0,
1428 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1429 sum0 = ip_csum_with_carry (sum0,
1430 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1433 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1434 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1437 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1439 /* validate really icmp6 next */
1440 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1442 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1443 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1445 payload_length_host_byte_order -= skip_bytes;
1446 headers_size += skip_bytes;
1449 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1453 struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1454 u8 nb_segs = mb->nb_segs;
1456 n_this_buffer = (p0->current_length > headers_size ?
1457 p0->current_length - headers_size : 0);
1458 while (n_bytes_left)
1460 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1461 n_bytes_left -= n_this_buffer;
1465 if ((nb_segs == 0) || (mb == 0))
1468 data_this_buffer = rte_ctrlmbuf_data(mb);
1469 n_this_buffer = mb->data_len;
1471 if (n_bytes_left || nb_segs)
1477 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1479 if (p0 && n_this_buffer + headers_size > p0->current_length)
1480 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1483 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1484 n_bytes_left -= n_this_buffer;
1485 if (n_bytes_left == 0)
1488 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1493 p0 = vlib_get_buffer (vm, p0->next_buffer);
1494 data_this_buffer = vlib_buffer_get_current (p0);
1495 n_this_buffer = p0->current_length;
1499 sum16 = ~ ip_csum_fold (sum0);
1504 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1506 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1507 udp_header_t * udp0;
1511 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1512 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1513 || ip0->protocol == IP_PROTOCOL_ICMP6
1514 || ip0->protocol == IP_PROTOCOL_UDP
1515 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1517 udp0 = (void *) (ip0 + 1);
1518 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1520 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1521 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1525 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1527 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1528 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1534 ip6_local (vlib_main_t * vm,
1535 vlib_node_runtime_t * node,
1536 vlib_frame_t * frame)
1538 ip6_main_t * im = &ip6_main;
1539 ip_lookup_main_t * lm = &im->lookup_main;
1540 ip_local_next_t next_index;
1541 u32 * from, * to_next, n_left_from, n_left_to_next;
1542 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1544 from = vlib_frame_vector_args (frame);
1545 n_left_from = frame->n_vectors;
1546 next_index = node->cached_next_index;
1548 if (node->flags & VLIB_NODE_FLAG_TRACE)
1549 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1551 while (n_left_from > 0)
1553 vlib_get_next_frame (vm, node, next_index,
1554 to_next, n_left_to_next);
1556 while (n_left_from >= 4 && n_left_to_next >= 2)
1558 vlib_buffer_t * p0, * p1;
1559 ip6_header_t * ip0, * ip1;
1560 udp_header_t * udp0, * udp1;
1561 u32 pi0, ip_len0, udp_len0, flags0, next0;
1562 u32 pi1, ip_len1, udp_len1, flags1, next1;
1563 i32 len_diff0, len_diff1;
1564 u8 error0, type0, good_l4_checksum0;
1565 u8 error1, type1, good_l4_checksum1;
1567 pi0 = to_next[0] = from[0];
1568 pi1 = to_next[1] = from[1];
1572 n_left_to_next -= 2;
1574 p0 = vlib_get_buffer (vm, pi0);
1575 p1 = vlib_get_buffer (vm, pi1);
1577 ip0 = vlib_buffer_get_current (p0);
1578 ip1 = vlib_buffer_get_current (p1);
1580 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1581 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1583 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1584 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1589 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1590 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1592 udp0 = ip6_next_header (ip0);
1593 udp1 = ip6_next_header (ip1);
1595 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1596 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1597 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1599 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1600 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1602 /* Verify UDP length. */
1603 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1604 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1605 udp_len0 = clib_net_to_host_u16 (udp0->length);
1606 udp_len1 = clib_net_to_host_u16 (udp1->length);
1608 len_diff0 = ip_len0 - udp_len0;
1609 len_diff1 = ip_len1 - udp_len1;
1611 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1612 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1614 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1615 && ! good_l4_checksum0
1616 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1618 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1620 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1622 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1623 && ! good_l4_checksum1
1624 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1626 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1628 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1631 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1633 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1634 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1636 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1637 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1638 error0 = (! good_l4_checksum0
1639 ? IP6_ERROR_UDP_CHECKSUM + type0
1641 error1 = (! good_l4_checksum1
1642 ? IP6_ERROR_UDP_CHECKSUM + type1
1645 /* Drop packets from unroutable hosts. */
1646 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1647 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1649 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1650 error0 = (lm->miss_adj_index == src_adj_index0
1651 ? IP6_ERROR_SRC_LOOKUP_MISS
1654 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1656 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1657 error1 = (lm->miss_adj_index == src_adj_index1
1658 ? IP6_ERROR_SRC_LOOKUP_MISS
1662 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1663 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1665 p0->error = error_node->errors[error0];
1666 p1->error = error_node->errors[error1];
1668 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1669 to_next, n_left_to_next,
1670 pi0, pi1, next0, next1);
1673 while (n_left_from > 0 && n_left_to_next > 0)
1677 udp_header_t * udp0;
1678 u32 pi0, ip_len0, udp_len0, flags0, next0;
1680 u8 error0, type0, good_l4_checksum0;
1682 pi0 = to_next[0] = from[0];
1686 n_left_to_next -= 1;
1688 p0 = vlib_get_buffer (vm, pi0);
1690 ip0 = vlib_buffer_get_current (p0);
1692 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1693 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1697 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1699 udp0 = ip6_next_header (ip0);
1701 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1702 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1704 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1706 /* Verify UDP length. */
1707 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1708 udp_len0 = clib_net_to_host_u16 (udp0->length);
1710 len_diff0 = ip_len0 - udp_len0;
1712 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1714 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1715 && ! good_l4_checksum0
1716 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1718 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1720 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1723 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1725 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1727 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1728 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1729 error0 = (! good_l4_checksum0
1730 ? IP6_ERROR_UDP_CHECKSUM + type0
1733 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1734 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1736 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1737 error0 = (lm->miss_adj_index == src_adj_index0
1738 ? IP6_ERROR_SRC_LOOKUP_MISS
1742 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1744 p0->error = error_node->errors[error0];
1746 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1747 to_next, n_left_to_next,
1751 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1754 return frame->n_vectors;
1757 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1758 .function = ip6_local,
1759 .name = "ip6-local",
1760 .vector_size = sizeof (u32),
1762 .format_trace = format_ip6_forward_next_trace,
1764 .n_next_nodes = IP_LOCAL_N_NEXT,
1766 [IP_LOCAL_NEXT_DROP] = "error-drop",
1767 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1768 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1769 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1773 void ip6_register_protocol (u32 protocol, u32 node_index)
1775 vlib_main_t * vm = vlib_get_main();
1776 ip6_main_t * im = &ip6_main;
1777 ip_lookup_main_t * lm = &im->lookup_main;
1779 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1780 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1784 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1785 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1786 } ip6_discover_neighbor_next_t;
1789 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1790 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1791 } ip6_discover_neighbor_error_t;
1794 ip6_discover_neighbor (vlib_main_t * vm,
1795 vlib_node_runtime_t * node,
1796 vlib_frame_t * frame)
1798 vnet_main_t * vnm = vnet_get_main();
1799 ip6_main_t * im = &ip6_main;
1800 ip_lookup_main_t * lm = &im->lookup_main;
1801 u32 * from, * to_next_drop;
1802 uword n_left_from, n_left_to_next_drop;
1803 static f64 time_last_seed_change = -1e100;
1804 static u32 hash_seeds[3];
1805 static uword hash_bitmap[256 / BITS (uword)];
1809 if (node->flags & VLIB_NODE_FLAG_TRACE)
1810 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1812 time_now = vlib_time_now (vm);
1813 if (time_now - time_last_seed_change > 1e-3)
1816 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1817 sizeof (hash_seeds));
1818 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1819 hash_seeds[i] = r[i];
1821 /* Mark all hash keys as been not-seen before. */
1822 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1825 time_last_seed_change = time_now;
1828 from = vlib_frame_vector_args (frame);
1829 n_left_from = frame->n_vectors;
1831 while (n_left_from > 0)
1833 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1834 to_next_drop, n_left_to_next_drop);
1836 while (n_left_from > 0 && n_left_to_next_drop > 0)
1840 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1842 ip_adjacency_t * adj0;
1843 vnet_hw_interface_t * hw_if0;
1848 p0 = vlib_get_buffer (vm, pi0);
1850 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1852 ip0 = vlib_buffer_get_current (p0);
1854 adj0 = ip_get_adjacency (lm, adj_index0);
1860 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1861 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1864 b0 ^= ip0->dst_address.as_u32[0];
1865 c0 ^= ip0->dst_address.as_u32[1];
1867 hash_v3_mix32 (a0, b0, c0);
1869 b0 ^= ip0->dst_address.as_u32[2];
1870 c0 ^= ip0->dst_address.as_u32[3];
1872 hash_v3_finalize32 (a0, b0, c0);
1874 c0 &= BITS (hash_bitmap) - 1;
1875 c0 = c0 / BITS (uword);
1876 m0 = (uword) 1 << (c0 % BITS (uword));
1878 bm0 = hash_bitmap[c0];
1879 drop0 = (bm0 & m0) != 0;
1881 /* Mark it as seen. */
1882 hash_bitmap[c0] = bm0 | m0;
1886 to_next_drop[0] = pi0;
1888 n_left_to_next_drop -= 1;
1890 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1892 /* If the interface is link-down, drop the pkt */
1893 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1897 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1898 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1904 icmp6_neighbor_solicitation_header_t * h0;
1907 h0 = vlib_packet_template_get_packet
1908 (vm, &im->discover_neighbor_packet_template, &bi0);
1911 * Build ethernet header.
1912 * Choose source address based on destination lookup
1915 ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
1919 * Destination address is a solicited node multicast address.
1920 * We need to fill in
1921 * the low 24 bits with low 24 bits of target's address.
1923 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1924 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1925 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1927 h0->neighbor.target_address = ip0->dst_address;
1929 memcpy (h0->link_layer_option.ethernet_address,
1930 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1932 /* $$$$ appears we need this; why is the checksum non-zero? */
1933 h0->neighbor.icmp.checksum = 0;
1934 h0->neighbor.icmp.checksum =
1935 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1938 ASSERT (bogus_length == 0);
1940 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1941 b0 = vlib_get_buffer (vm, bi0);
1942 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1943 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1945 /* Add rewrite/encap string. */
1946 vnet_rewrite_one_header (adj0[0], h0,
1947 sizeof (ethernet_header_t));
1948 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1950 /* $$$$ hack in case next0 == 0 */
1951 b0->error = node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_DROP];
1953 vec_elt (im->discover_neighbor_next_index_by_hw_if_index,
1954 hw_if0->hw_if_index);
1956 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1960 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1961 n_left_to_next_drop);
1964 return frame->n_vectors;
1967 static char * ip6_discover_neighbor_error_strings[] = {
1968 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1969 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
1970 = "neighbor solicitations sent",
1973 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
1974 .function = ip6_discover_neighbor,
1975 .name = "ip6-discover-neighbor",
1976 .vector_size = sizeof (u32),
1978 .format_trace = format_ip6_forward_next_trace,
1980 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1981 .error_strings = ip6_discover_neighbor_error_strings,
1983 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1985 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1990 ip6_discover_neighbor_hw_interface_link_up_down (vnet_main_t * vnm,
1994 vlib_main_t * vm = vnm->vlib_main;
1995 ip6_main_t * im = &ip6_main;
1996 vnet_hw_interface_t * hw_if;
1998 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
2000 vec_validate_init_empty
2001 (im->discover_neighbor_next_index_by_hw_if_index, hw_if_index, 0);
2002 im->discover_neighbor_next_index_by_hw_if_index[hw_if_index]
2003 = vlib_node_add_next (vm, ip6_discover_neighbor_node.index,
2004 hw_if->output_node_index);
2008 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION
2009 (ip6_discover_neighbor_hw_interface_link_up_down);
2012 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2014 vnet_main_t * vnm = vnet_get_main();
2015 ip6_main_t * im = &ip6_main;
2016 icmp6_neighbor_solicitation_header_t * h;
2017 ip6_address_t * src;
2018 ip_interface_address_t * ia;
2019 ip_adjacency_t * adj;
2020 vnet_hw_interface_t * hi;
2021 vnet_sw_interface_t * si;
2026 si = vnet_get_sw_interface (vnm, sw_if_index);
2028 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2030 return clib_error_return (0, "%U: interface %U down",
2031 format_ip6_address, dst,
2032 format_vnet_sw_if_index_name, vnm,
2036 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2039 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2040 return clib_error_return
2041 (0, "no matching interface address for destination %U (interface %U)",
2042 format_ip6_address, dst,
2043 format_vnet_sw_if_index_name, vnm, sw_if_index);
2046 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2048 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2050 /* Destination address is a solicited node multicast address. We need to fill in
2051 the low 24 bits with low 24 bits of target's address. */
2052 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2053 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2054 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2056 h->ip.src_address = src[0];
2057 h->neighbor.target_address = dst[0];
2059 memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2061 h->neighbor.icmp.checksum =
2062 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2063 ASSERT(bogus_length == 0);
2065 b = vlib_get_buffer (vm, bi);
2066 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2068 /* Add encapsulation string for software interface (e.g. ethernet header). */
2069 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2070 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2071 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2074 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2075 u32 * to_next = vlib_frame_vector_args (f);
2078 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2081 return /* no error */ 0;
2085 IP6_REWRITE_NEXT_DROP,
2086 } ip6_rewrite_next_t;
2089 ip6_rewrite_inline (vlib_main_t * vm,
2090 vlib_node_runtime_t * node,
2091 vlib_frame_t * frame,
2092 int rewrite_for_locally_received_packets)
2094 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2095 u32 * from = vlib_frame_vector_args (frame);
2096 u32 n_left_from, n_left_to_next, * to_next, next_index;
2097 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2098 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2100 n_left_from = frame->n_vectors;
2101 next_index = node->cached_next_index;
2102 u32 cpu_index = os_get_cpu_number();
2104 while (n_left_from > 0)
2106 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2108 while (n_left_from >= 4 && n_left_to_next >= 2)
2110 ip_adjacency_t * adj0, * adj1;
2111 vlib_buffer_t * p0, * p1;
2112 ip6_header_t * ip0, * ip1;
2113 u32 pi0, rw_len0, next0, error0, adj_index0;
2114 u32 pi1, rw_len1, next1, error1, adj_index1;
2116 /* Prefetch next iteration. */
2118 vlib_buffer_t * p2, * p3;
2120 p2 = vlib_get_buffer (vm, from[2]);
2121 p3 = vlib_get_buffer (vm, from[3]);
2123 vlib_prefetch_buffer_header (p2, LOAD);
2124 vlib_prefetch_buffer_header (p3, LOAD);
2126 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2127 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2129 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2130 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2133 pi0 = to_next[0] = from[0];
2134 pi1 = to_next[1] = from[1];
2139 n_left_to_next -= 2;
2141 p0 = vlib_get_buffer (vm, pi0);
2142 p1 = vlib_get_buffer (vm, pi1);
2144 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2145 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2147 /* We should never rewrite a pkt using the MISS adjacency */
2148 ASSERT(adj_index0 && adj_index1);
2150 ip0 = vlib_buffer_get_current (p0);
2151 ip1 = vlib_buffer_get_current (p1);
2153 error0 = error1 = IP6_ERROR_NONE;
2155 if (! rewrite_for_locally_received_packets)
2157 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2159 /* Input node should have reject packets with hop limit 0. */
2160 ASSERT (ip0->hop_limit > 0);
2161 ASSERT (ip1->hop_limit > 0);
2166 ip0->hop_limit = hop_limit0;
2167 ip1->hop_limit = hop_limit1;
2169 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2170 error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2173 adj0 = ip_get_adjacency (lm, adj_index0);
2174 adj1 = ip_get_adjacency (lm, adj_index1);
2176 if (rewrite_for_locally_received_packets)
2179 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2180 * we end up here with a local adjacency in hand
2182 if (PREDICT_FALSE(adj0->lookup_next_index
2183 == IP_LOOKUP_NEXT_LOCAL))
2184 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2185 if (PREDICT_FALSE(adj1->lookup_next_index
2186 == IP_LOOKUP_NEXT_LOCAL))
2187 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2190 rw_len0 = adj0[0].rewrite_header.data_bytes;
2191 rw_len1 = adj1[0].rewrite_header.data_bytes;
2193 vlib_increment_combined_counter (&lm->adjacency_counters,
2196 /* packet increment */ 0,
2197 /* byte increment */ rw_len0);
2198 vlib_increment_combined_counter (&lm->adjacency_counters,
2201 /* packet increment */ 0,
2202 /* byte increment */ rw_len1);
2204 /* Check MTU of outgoing interface. */
2205 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2206 ? IP6_ERROR_MTU_EXCEEDED
2208 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2209 ? IP6_ERROR_MTU_EXCEEDED
2212 p0->current_data -= rw_len0;
2213 p1->current_data -= rw_len1;
2215 p0->current_length += rw_len0;
2216 p1->current_length += rw_len1;
2218 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2219 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2221 next0 = (error0 == IP6_ERROR_NONE) ?
2222 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2223 next1 = (error1 == IP6_ERROR_NONE) ?
2224 adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2226 /* Guess we are only writing on simple Ethernet header. */
2227 vnet_rewrite_two_headers (adj0[0], adj1[0],
2229 sizeof (ethernet_header_t));
2231 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2232 to_next, n_left_to_next,
2233 pi0, pi1, next0, next1);
2236 while (n_left_from > 0 && n_left_to_next > 0)
2238 ip_adjacency_t * adj0;
2242 u32 adj_index0, next0, error0;
2244 pi0 = to_next[0] = from[0];
2246 p0 = vlib_get_buffer (vm, pi0);
2248 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2250 /* We should never rewrite a pkt using the MISS adjacency */
2253 adj0 = ip_get_adjacency (lm, adj_index0);
2255 ip0 = vlib_buffer_get_current (p0);
2257 error0 = IP6_ERROR_NONE;
2259 /* Check hop limit */
2260 if (! rewrite_for_locally_received_packets)
2262 i32 hop_limit0 = ip0->hop_limit;
2264 ASSERT (ip0->hop_limit > 0);
2268 ip0->hop_limit = hop_limit0;
2270 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2273 if (rewrite_for_locally_received_packets)
2275 if (PREDICT_FALSE(adj0->lookup_next_index
2276 == IP_LOOKUP_NEXT_LOCAL))
2277 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2280 /* Guess we are only writing on simple Ethernet header. */
2281 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2283 /* Update packet buffer attributes/set output interface. */
2284 rw_len0 = adj0[0].rewrite_header.data_bytes;
2286 vlib_increment_combined_counter (&lm->adjacency_counters,
2289 /* packet increment */ 0,
2290 /* byte increment */ rw_len0);
2292 /* Check MTU of outgoing interface. */
2293 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2294 ? IP6_ERROR_MTU_EXCEEDED
2297 p0->current_data -= rw_len0;
2298 p0->current_length += rw_len0;
2299 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2301 next0 = (error0 == IP6_ERROR_NONE) ?
2302 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2304 p0->error = error_node->errors[error0];
2309 n_left_to_next -= 1;
2311 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2312 to_next, n_left_to_next,
2316 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2319 /* Need to do trace after rewrites to pick up new packet data. */
2320 if (node->flags & VLIB_NODE_FLAG_TRACE)
2321 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2323 return frame->n_vectors;
2327 ip6_rewrite_transit (vlib_main_t * vm,
2328 vlib_node_runtime_t * node,
2329 vlib_frame_t * frame)
2331 return ip6_rewrite_inline (vm, node, frame,
2332 /* rewrite_for_locally_received_packets */ 0);
2336 ip6_rewrite_local (vlib_main_t * vm,
2337 vlib_node_runtime_t * node,
2338 vlib_frame_t * frame)
2340 return ip6_rewrite_inline (vm, node, frame,
2341 /* rewrite_for_locally_received_packets */ 1);
2344 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2345 .function = ip6_rewrite_transit,
2346 .name = "ip6-rewrite",
2347 .vector_size = sizeof (u32),
2349 .format_trace = format_ip6_forward_next_trace,
2353 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2357 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2358 .function = ip6_rewrite_local,
2359 .name = "ip6-rewrite-local",
2360 .vector_size = sizeof (u32),
2362 .sibling_of = "ip6-rewrite",
2364 .format_trace = format_ip6_forward_next_trace,
2368 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2372 /* Global IP6 main. */
2373 ip6_main_t ip6_main;
2375 static clib_error_t *
2376 ip6_lookup_init (vlib_main_t * vm)
2378 ip6_main_t * im = &ip6_main;
2381 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2388 for (j = 0; j < i0; j++)
2389 im->fib_masks[i].as_u32[j] = ~0;
2392 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2395 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2397 if (im->lookup_table_nbuckets == 0)
2398 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2400 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2402 if (im->lookup_table_size == 0)
2403 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2405 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2406 im->lookup_table_nbuckets,
2407 im->lookup_table_size);
2409 /* Create FIB with index 0 and table id of 0. */
2410 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2414 pn = pg_get_node (ip6_lookup_node.index);
2415 pn->unformat_edit = unformat_pg_ip6_header;
2419 icmp6_neighbor_solicitation_header_t p;
2421 memset (&p, 0, sizeof (p));
2423 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2424 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2425 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2426 p.ip.protocol = IP_PROTOCOL_ICMP6;
2427 p.ip.hop_limit = 255;
2428 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2430 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2432 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2433 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2435 vlib_packet_template_init (vm,
2436 &im->discover_neighbor_packet_template,
2438 /* alloc chunk size */ 8,
2439 "ip6 neighbor discovery");
2445 VLIB_INIT_FUNCTION (ip6_lookup_init);
2447 static clib_error_t *
2448 add_del_ip6_interface_table (vlib_main_t * vm,
2449 unformat_input_t * input,
2450 vlib_cli_command_t * cmd)
2452 vnet_main_t * vnm = vnet_get_main();
2453 clib_error_t * error = 0;
2454 u32 sw_if_index, table_id;
2458 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2460 error = clib_error_return (0, "unknown interface `%U'",
2461 format_unformat_error, input);
2465 if (unformat (input, "%d", &table_id))
2469 error = clib_error_return (0, "expected table id `%U'",
2470 format_unformat_error, input);
2475 ip6_main_t * im = &ip6_main;
2477 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2481 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2482 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2490 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2491 .path = "set interface ip6 table",
2492 .function = add_del_ip6_interface_table,
2493 .short_help = "set interface ip6 table <intfc> <table-id>"
2497 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2500 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2501 /* Invert the "u" bit */
2502 ip->as_u8 [8] = mac[0] ^ (1<<1);
2503 ip->as_u8 [9] = mac[1];
2504 ip->as_u8 [10] = mac[2];
2505 ip->as_u8 [11] = 0xFF;
2506 ip->as_u8 [12] = 0xFE;
2507 ip->as_u8 [13] = mac[3];
2508 ip->as_u8 [14] = mac[4];
2509 ip->as_u8 [15] = mac[5];
2513 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
2516 /* Invert the previously inverted "u" bit */
2517 mac[0] = ip->as_u8 [8] ^ (1<<1);
2518 mac[1] = ip->as_u8 [9];
2519 mac[2] = ip->as_u8 [10];
2520 mac[3] = ip->as_u8 [13];
2521 mac[4] = ip->as_u8 [14];
2522 mac[5] = ip->as_u8 [15];
2525 static clib_error_t *
2526 test_ip6_link_command_fn (vlib_main_t * vm,
2527 unformat_input_t * input,
2528 vlib_cli_command_t * cmd)
2531 ip6_address_t _a, *a = &_a;
2533 if (unformat (input, "%U", unformat_ethernet_address, mac))
2535 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2536 vlib_cli_output (vm, "Link local address: %U",
2537 format_ip6_address, a);
2538 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2539 vlib_cli_output (vm, "Original MAC address: %U",
2540 format_ethernet_address, mac);
2546 VLIB_CLI_COMMAND (test_link_command, static) = {
2547 .path = "test ip6 link",
2548 .function = test_ip6_link_command_fn,
2549 .short_help = "test ip6 link <mac-address>",
2552 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2554 ip6_main_t * im6 = &ip6_main;
2556 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2561 fib = vec_elt_at_index (im6->fibs, p[0]);
2563 fib->flow_hash_config = flow_hash_config;
2567 static clib_error_t *
2568 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2569 unformat_input_t * input,
2570 vlib_cli_command_t * cmd)
2574 u32 flow_hash_config = 0;
2577 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2578 if (unformat (input, "table %d", &table_id))
2581 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2582 foreach_flow_hash_bit
2588 return clib_error_return (0, "unknown input `%U'",
2589 format_unformat_error, input);
2591 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2598 return clib_error_return (0, "no such FIB table %d", table_id);
2601 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2608 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2609 .path = "set ip6 flow-hash",
2611 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2612 .function = set_ip6_flow_hash_command_fn,
2615 static clib_error_t *
2616 show_ip6_local_command_fn (vlib_main_t * vm,
2617 unformat_input_t * input,
2618 vlib_cli_command_t * cmd)
2620 ip6_main_t * im = &ip6_main;
2621 ip_lookup_main_t * lm = &im->lookup_main;
2624 vlib_cli_output (vm, "Protocols handled by ip6_local");
2625 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2627 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2628 vlib_cli_output (vm, "%d", i);
2635 VLIB_CLI_COMMAND (show_ip_local, static) = {
2636 .path = "show ip6 local",
2637 .function = show_ip6_local_command_fn,
2638 .short_help = "Show ip6 local protocol table",
2641 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2644 vnet_main_t * vnm = vnet_get_main();
2645 vnet_interface_main_t * im = &vnm->interface_main;
2646 ip6_main_t * ipm = &ip6_main;
2647 ip_lookup_main_t * lm = &ipm->lookup_main;
2648 vnet_classify_main_t * cm = &vnet_classify_main;
2650 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2651 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2653 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2654 return VNET_API_ERROR_NO_SUCH_ENTRY;
2656 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2657 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2662 static clib_error_t *
2663 set_ip6_classify_command_fn (vlib_main_t * vm,
2664 unformat_input_t * input,
2665 vlib_cli_command_t * cmd)
2667 u32 table_index = ~0;
2668 int table_index_set = 0;
2669 u32 sw_if_index = ~0;
2672 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2673 if (unformat (input, "table-index %d", &table_index))
2674 table_index_set = 1;
2675 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2676 vnet_get_main(), &sw_if_index))
2682 if (table_index_set == 0)
2683 return clib_error_return (0, "classify table-index must be specified");
2685 if (sw_if_index == ~0)
2686 return clib_error_return (0, "interface / subif must be specified");
2688 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2695 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2696 return clib_error_return (0, "No such interface");
2698 case VNET_API_ERROR_NO_SUCH_ENTRY:
2699 return clib_error_return (0, "No such classifier table");
2704 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2705 .path = "set ip6 classify",
2707 "set ip6 classify intfc <int> table-index <index>",
2708 .function = set_ip6_classify_command_fn,
2711 static clib_error_t *
2712 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2714 ip6_main_t * im = &ip6_main;
2719 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2720 if (unformat (input, "hash-buckets %d", &tmp))
2722 else if (unformat (input, "heap-size %dm", &tmp))
2723 heapsize = ((u64)tmp) << 20;
2724 else if (unformat (input, "heap-size %dM", &tmp))
2725 heapsize = ((u64)tmp) << 20;
2726 else if (unformat (input, "heap-size %dg", &tmp))
2727 heapsize = ((u64)tmp) << 30;
2728 else if (unformat (input, "heap-size %dG", &tmp))
2729 heapsize = ((u64)tmp) << 30;
2731 return clib_error_return (0, "unknown input '%U'",
2732 format_unformat_error, input);
2735 im->lookup_table_nbuckets = nbuckets;
2736 im->lookup_table_size = heapsize;
2741 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");