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;
68 len = vec_len (im->prefix_lengths_in_search_order);
70 for (i = 0; i < len; i++)
72 int dst_address_length = im->prefix_lengths_in_search_order[i];
73 ip6_address_t * mask = &im->fib_masks[dst_address_length];
75 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
77 kv.key[0] = dst->as_u64[0] & mask->as_u64[0];
78 kv.key[1] = dst->as_u64[1] & mask->as_u64[1];
79 kv.key[2] = ((u64)((fib_index))<<32) | dst_address_length;
81 rv = BV(clib_bihash_search_inline_2)(&im->ip6_lookup_table, &kv, &value);
86 return lm->miss_adj_index;
89 u32 ip6_fib_lookup (ip6_main_t * im, u32 sw_if_index, ip6_address_t * dst)
91 u32 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
92 return ip6_fib_lookup_with_table (im, fib_index, dst);
96 vnet_ip6_fib_init (ip6_main_t * im, u32 fib_index)
98 ip_lookup_main_t * lm = &im->lookup_main;
99 ip6_add_del_route_args_t a;
100 ip_adjacency_t * adj;
102 memset(&a, 0x0, sizeof(ip6_add_del_route_args_t));
104 a.table_index_or_table_id = fib_index;
105 a.flags = (IP6_ROUTE_FLAG_ADD
106 | IP6_ROUTE_FLAG_FIB_INDEX
107 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
108 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
110 /* Add ff02::1:ff00:0/104 via local route for all tables.
111 This is required for neighbor discovery to work. */
112 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
114 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
115 adj->if_address_index = ~0;
116 adj->rewrite_header.data_bytes = 0;
118 ip6_set_solicited_node_multicast_address (&a.dst_address, 0);
120 a.dst_address_length = 104;
121 ip6_add_del_route (im, &a);
123 /* Add all-routers multicast address via local route for all tables */
124 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
126 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
127 adj->if_address_index = ~0;
128 adj->rewrite_header.data_bytes = 0;
130 ip6_set_reserved_multicast_address (&a.dst_address,
131 IP6_MULTICAST_SCOPE_link_local,
132 IP6_MULTICAST_GROUP_ID_all_routers);
134 a.dst_address_length = 128;
135 ip6_add_del_route (im, &a);
137 /* Add all-nodes multicast address via local route for all tables */
138 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
140 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
141 adj->if_address_index = ~0;
142 adj->rewrite_header.data_bytes = 0;
144 ip6_set_reserved_multicast_address (&a.dst_address,
145 IP6_MULTICAST_SCOPE_link_local,
146 IP6_MULTICAST_GROUP_ID_all_hosts);
148 a.dst_address_length = 128;
149 ip6_add_del_route (im, &a);
151 /* Add all-mldv2 multicast address via local route for all tables */
152 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
154 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
155 adj->if_address_index = ~0;
156 adj->rewrite_header.data_bytes = 0;
158 ip6_set_reserved_multicast_address (&a.dst_address,
159 IP6_MULTICAST_SCOPE_link_local,
160 IP6_MULTICAST_GROUP_ID_mldv2_routers);
162 a.dst_address_length = 128;
163 ip6_add_del_route (im, &a);
167 create_fib_with_table_id (ip6_main_t * im, u32 table_id)
170 hash_set (im->fib_index_by_table_id, table_id, vec_len (im->fibs));
171 vec_add2 (im->fibs, fib, 1);
172 fib->table_id = table_id;
173 fib->index = fib - im->fibs;
174 fib->flow_hash_config = IP_FLOW_HASH_DEFAULT;
175 vnet_ip6_fib_init (im, fib->index);
180 find_ip6_fib_by_table_index_or_id (ip6_main_t * im, u32 table_index_or_id, u32 flags)
182 uword * p, fib_index;
184 fib_index = table_index_or_id;
185 if (! (flags & IP6_ROUTE_FLAG_FIB_INDEX))
187 p = hash_get (im->fib_index_by_table_id, table_index_or_id);
189 return create_fib_with_table_id (im, table_index_or_id);
192 return vec_elt_at_index (im->fibs, fib_index);
195 void ip6_add_del_route (ip6_main_t * im, ip6_add_del_route_args_t * a)
197 ip_lookup_main_t * lm = &im->lookup_main;
199 ip6_address_t dst_address;
200 u32 dst_address_length, adj_index;
202 u32 old_adj_index = ~0;
203 BVT(clib_bihash_kv) kv, value;
205 vlib_smp_unsafe_warning();
207 is_del = (a->flags & IP6_ROUTE_FLAG_DEL) != 0;
209 /* Either create new adjacency or use given one depending on arguments. */
210 if (a->n_add_adj > 0)
212 ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
213 ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
216 adj_index = a->adj_index;
218 dst_address = a->dst_address;
219 dst_address_length = a->dst_address_length;
220 fib = find_ip6_fib_by_table_index_or_id (im, a->table_index_or_table_id,
223 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
224 ip6_address_mask (&dst_address, &im->fib_masks[dst_address_length]);
226 /* refcount accounting */
229 ASSERT (im->dst_address_length_refcounts[dst_address_length] > 0);
230 if (--im->dst_address_length_refcounts[dst_address_length] == 0)
232 im->non_empty_dst_address_length_bitmap =
233 clib_bitmap_set (im->non_empty_dst_address_length_bitmap,
234 128 - dst_address_length, 0);
235 compute_prefix_lengths_in_search_order (im);
240 im->dst_address_length_refcounts[dst_address_length]++;
242 im->non_empty_dst_address_length_bitmap =
243 clib_bitmap_set (im->non_empty_dst_address_length_bitmap,
244 128 - dst_address_length, 1);
245 compute_prefix_lengths_in_search_order (im);
248 kv.key[0] = dst_address.as_u64[0];
249 kv.key[1] = dst_address.as_u64[1];
250 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
252 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
253 old_adj_index = value.value;
256 BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 0 /* is_add */);
259 /* Make sure adj index is valid. */
261 (void) ip_get_adjacency (lm, adj_index);
263 kv.value = adj_index;
265 BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 1 /* is_add */);
268 /* Delete old adjacency index if present and changed. */
270 if (! (a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
271 && old_adj_index != ~0
272 && old_adj_index != adj_index)
273 ip_del_adjacency (lm, old_adj_index);
278 ip6_add_del_route_next_hop (ip6_main_t * im,
280 ip6_address_t * dst_address,
281 u32 dst_address_length,
282 ip6_address_t * next_hop,
283 u32 next_hop_sw_if_index,
284 u32 next_hop_weight, u32 adj_index,
285 u32 explicit_fib_index)
287 vnet_main_t * vnm = vnet_get_main();
288 ip_lookup_main_t * lm = &im->lookup_main;
291 ip6_address_t masked_dst_address;
292 u32 old_mp_adj_index, new_mp_adj_index;
293 u32 dst_adj_index, nh_adj_index;
295 ip_adjacency_t * dst_adj;
296 ip_multipath_adjacency_t * old_mp, * new_mp;
297 int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
298 int is_interface_next_hop;
299 clib_error_t * error = 0;
301 BVT(clib_bihash_kv) kv, value;
303 vlib_smp_unsafe_warning();
305 if (explicit_fib_index == (u32)~0)
306 fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
308 fib_index = explicit_fib_index;
310 fib = vec_elt_at_index (im->fibs, fib_index);
312 /* Lookup next hop to be added or deleted. */
313 is_interface_next_hop = ip6_address_is_zero (next_hop);
314 if (adj_index == (u32)~0)
316 if (is_interface_next_hop)
318 nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index,
319 next_hop_sw_if_index);
321 nh_adj_index = *nh_result;
324 ip_adjacency_t * adj;
325 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
327 ip6_adjacency_set_interface_route (vnm, adj,
328 next_hop_sw_if_index, ~0);
329 ip_call_add_del_adjacency_callbacks
330 (lm, next_hop_sw_if_index, /* is_del */ 0);
331 hash_set (im->interface_route_adj_index_by_sw_if_index,
332 next_hop_sw_if_index, nh_adj_index);
337 /* Look for the interface /128 route */
338 kv.key[0] = next_hop->as_u64[0];
339 kv.key[1] = next_hop->as_u64[1];
340 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
342 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
344 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
345 error = clib_error_return (0, "next-hop %U/128 not in FIB",
346 format_ip6_address, next_hop);
350 nh_adj_index = value.value;
355 /* Look for the interface /128 route */
356 kv.key[0] = next_hop->as_u64[0];
357 kv.key[1] = next_hop->as_u64[1];
358 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
360 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
362 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
363 error = clib_error_return (0, "next-hop %U/128 not in FIB",
364 format_ip6_address, next_hop);
368 nh_adj_index = value.value;
371 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
372 masked_dst_address = dst_address[0];
373 ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
375 kv.key[0] = masked_dst_address.as_u64[0];
376 kv.key[1] = masked_dst_address.as_u64[1];
377 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
379 rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
383 dst_adj_index = value.value;
384 dst_adj = ip_get_adjacency (lm, dst_adj_index);
388 /* For deletes destination must be known. */
391 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
392 error = clib_error_return (0, "unknown destination %U/%d",
393 format_ip6_address, dst_address,
402 /* Ignore adds of X/128 with next hop of X. */
404 && dst_address_length == 128
405 && ip6_address_is_equal (dst_address, next_hop))
407 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
408 error = clib_error_return (0, "prefix matches next hop %U/%d",
409 format_ip6_address, dst_address,
414 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
416 if (! ip_multipath_adjacency_add_del_next_hop
418 dst_adj ? dst_adj->heap_handle : ~0,
423 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
424 error = clib_error_return
425 (0, "requested deleting next-hop %U not found in multi-path",
426 format_ip6_address, next_hop);
431 if (old_mp_adj_index != ~0)
432 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
433 if (new_mp_adj_index != ~0)
434 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
436 if (old_mp != new_mp)
438 ip6_add_del_route_args_t a;
439 a.table_index_or_table_id = fib_index;
440 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
441 | IP6_ROUTE_FLAG_FIB_INDEX
442 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
443 | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
444 a.dst_address = dst_address[0];
445 a.dst_address_length = dst_address_length;
446 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
450 ip6_add_del_route (im, &a);
455 clib_error_report (error);
459 ip6_get_route (ip6_main_t * im,
460 u32 table_index_or_table_id,
462 ip6_address_t * address,
465 ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
466 ip6_address_t masked_address;
467 BVT(clib_bihash_kv) kv, value;
469 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
470 memcpy (&masked_address, address, sizeof (masked_address));
471 ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
473 kv.key[0] = masked_address.as_u64[0];
474 kv.key[1] = masked_address.as_u64[1];
475 kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
477 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
478 return (value.value);
483 ip6_foreach_matching_route (ip6_main_t * im,
484 u32 table_index_or_table_id,
486 ip6_address_t * dst_address,
488 ip6_address_t ** results,
489 u8 ** result_lengths)
492 find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
493 BVT(clib_bihash) * h = &im->ip6_lookup_table;
494 BVT(clib_bihash_value) * v;
495 clib_bihash_bucket_t * b;
499 _vec_len (*results) = 0;
501 _vec_len (*result_lengths) = 0;
503 /* Walk the table looking for routes which match the supplied address */
504 for (i = 0; i < h->nbuckets; i++)
510 v = BV(clib_bihash_get_value) (h, b->offset);
511 for (j = 0; j < (1<<b->log2_pages); j++)
513 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
515 if (BV(clib_bihash_is_free)(&v->kvp[k]))
518 if ((v->kvp[k].key[2]
519 == (((u64)((fib - im->fibs))<<32) | address_length))
520 && ip6_destination_matches_route
521 (im, dst_address, (ip6_address_t *) &v->kvp[k],
526 a = (ip6_address_t *)(&v->kvp[k]);
528 vec_add1 (*results, a[0]);
529 vec_add1 (*result_lengths, address_length);
537 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
538 u32 table_index_or_table_id,
543 = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
545 ip_lookup_main_t * lm = &im->lookup_main;
547 if (lm->n_adjacency_remaps == 0)
550 clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
552 /* All remaps have been performed. */
553 lm->n_adjacency_remaps = 0;
556 void ip6_delete_matching_routes (ip6_main_t * im,
557 u32 table_index_or_table_id,
559 ip6_address_t * address,
562 /* $$$$ static may be OK - this should happen only on thread 0 */
563 static ip6_address_t * matching_addresses;
564 static u8 * matching_address_lengths;
566 ip6_add_del_route_args_t a;
568 vlib_smp_unsafe_warning();
570 a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
571 a.table_index_or_table_id = table_index_or_table_id;
576 for (l = address_length + 1; l <= 128; l++)
578 ip6_foreach_matching_route (im, table_index_or_table_id, flags,
582 &matching_address_lengths);
583 for (i = 0; i < vec_len (matching_addresses); i++)
585 a.dst_address = matching_addresses[i];
586 a.dst_address_length = matching_address_lengths[i];
587 ip6_add_del_route (im, &a);
591 ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
595 ip6_lookup (vlib_main_t * vm,
596 vlib_node_runtime_t * node,
597 vlib_frame_t * frame)
599 ip6_main_t * im = &ip6_main;
600 ip_lookup_main_t * lm = &im->lookup_main;
601 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
602 u32 n_left_from, n_left_to_next, * from, * to_next;
603 ip_lookup_next_t next;
604 u32 cpu_index = os_get_cpu_number();
606 from = vlib_frame_vector_args (frame);
607 n_left_from = frame->n_vectors;
608 next = node->cached_next_index;
610 while (n_left_from > 0)
612 vlib_get_next_frame (vm, node, next,
613 to_next, n_left_to_next);
615 while (n_left_from >= 4 && n_left_to_next >= 2)
617 vlib_buffer_t * p0, * p1;
618 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
619 ip_lookup_next_t next0, next1;
620 ip6_header_t * ip0, * ip1;
621 ip_adjacency_t * adj0, * adj1;
622 u32 fib_index0, fib_index1;
623 u32 flow_hash_config0, flow_hash_config1;
625 /* Prefetch next iteration. */
627 vlib_buffer_t * p2, * p3;
629 p2 = vlib_get_buffer (vm, from[2]);
630 p3 = vlib_get_buffer (vm, from[3]);
632 vlib_prefetch_buffer_header (p2, LOAD);
633 vlib_prefetch_buffer_header (p3, LOAD);
634 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
635 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
638 pi0 = to_next[0] = from[0];
639 pi1 = to_next[1] = from[1];
641 p0 = vlib_get_buffer (vm, pi0);
642 p1 = vlib_get_buffer (vm, pi1);
644 ip0 = vlib_buffer_get_current (p0);
645 ip1 = vlib_buffer_get_current (p1);
647 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
648 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
650 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
651 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
652 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
653 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
655 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
657 adj_index1 = ip6_fib_lookup_with_table (im, fib_index1,
660 adj0 = ip_get_adjacency (lm, adj_index0);
661 adj1 = ip_get_adjacency (lm, adj_index1);
663 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
665 adj_index0 = ip6_fib_lookup_with_table
666 (im, adj0->explicit_fib_index, &ip0->dst_address);
667 adj0 = ip_get_adjacency (lm, adj_index0);
669 if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
671 adj_index1 = ip6_fib_lookup_with_table
672 (im, adj1->explicit_fib_index, &ip1->dst_address);
673 adj1 = ip_get_adjacency (lm, adj_index1);
676 next0 = adj0->lookup_next_index;
677 next1 = adj1->lookup_next_index;
679 /* Process hop-by-hop options if present */
680 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
681 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
682 next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
683 IP_LOOKUP_NEXT_HOP_BY_HOP : next1;
685 vnet_buffer (p0)->ip.flow_hash =
686 vnet_buffer(p1)->ip.flow_hash = 0;
688 if (PREDICT_FALSE(adj0->n_adj > 1))
691 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
692 vnet_buffer (p0)->ip.flow_hash =
693 ip6_compute_flow_hash (ip0, flow_hash_config0);
696 if (PREDICT_FALSE(adj1->n_adj > 1))
699 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
701 vnet_buffer (p1)->ip.flow_hash =
702 ip6_compute_flow_hash (ip1, flow_hash_config1);
705 ASSERT (adj0->n_adj > 0);
706 ASSERT (adj1->n_adj > 0);
707 ASSERT (is_pow2 (adj0->n_adj));
708 ASSERT (is_pow2 (adj1->n_adj));
709 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
710 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
712 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
713 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
715 vlib_increment_combined_counter
716 (cm, cpu_index, adj_index0, 1,
717 vlib_buffer_length_in_chain (vm, p0));
718 vlib_increment_combined_counter
719 (cm, cpu_index, adj_index1, 1,
720 vlib_buffer_length_in_chain (vm, p1));
727 wrong_next = (next0 != next) + 2*(next1 != next);
728 if (PREDICT_FALSE (wrong_next != 0))
737 vlib_set_next_frame_buffer (vm, node, next0, pi0);
744 vlib_set_next_frame_buffer (vm, node, next1, pi1);
751 vlib_set_next_frame_buffer (vm, node, next0, pi0);
752 vlib_set_next_frame_buffer (vm, node, next1, pi1);
756 vlib_put_next_frame (vm, node, next, n_left_to_next);
758 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
764 while (n_left_from > 0 && n_left_to_next > 0)
769 ip_lookup_next_t next0;
770 ip_adjacency_t * adj0;
771 u32 fib_index0, flow_hash_config0;
776 p0 = vlib_get_buffer (vm, pi0);
778 ip0 = vlib_buffer_get_current (p0);
780 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
781 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
782 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
785 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
787 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
790 adj0 = ip_get_adjacency (lm, adj_index0);
792 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
794 adj_index0 = ip6_fib_lookup_with_table
795 (im, adj0->explicit_fib_index, &ip0->dst_address);
796 adj0 = ip_get_adjacency (lm, adj_index0);
799 next0 = adj0->lookup_next_index;
800 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
801 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
803 vnet_buffer (p0)->ip.flow_hash = 0;
805 if (PREDICT_FALSE(adj0->n_adj > 1))
808 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
809 vnet_buffer (p0)->ip.flow_hash =
810 ip6_compute_flow_hash (ip0, flow_hash_config0);
813 ASSERT (adj0->n_adj > 0);
814 ASSERT (is_pow2 (adj0->n_adj));
815 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
817 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
819 vlib_increment_combined_counter
820 (cm, cpu_index, adj_index0, 1,
821 vlib_buffer_length_in_chain (vm, p0));
828 if (PREDICT_FALSE (next0 != next))
831 vlib_put_next_frame (vm, node, next, n_left_to_next);
833 vlib_get_next_frame (vm, node, next,
834 to_next, n_left_to_next);
841 vlib_put_next_frame (vm, node, next, n_left_to_next);
844 return frame->n_vectors;
847 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
848 ip_adjacency_t * adj,
850 u32 if_address_index)
852 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
856 if (hw->hw_class_index == ethernet_hw_interface_class.index
857 || hw->hw_class_index == srp_hw_interface_class.index)
859 n = IP_LOOKUP_NEXT_ARP;
860 node_index = ip6_discover_neighbor_node.index;
861 adj->if_address_index = if_address_index;
865 n = IP_LOOKUP_NEXT_REWRITE;
866 node_index = ip6_rewrite_node.index;
869 adj->lookup_next_index = n;
870 adj->explicit_fib_index = ~0;
872 vnet_rewrite_for_sw_interface
874 VNET_L3_PACKET_TYPE_IP6,
877 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
878 &adj->rewrite_header,
879 sizeof (adj->rewrite_data));
883 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
884 ip6_main_t * im, u32 fib_index,
885 ip_interface_address_t * a)
887 ip_lookup_main_t * lm = &im->lookup_main;
888 ip_adjacency_t * adj;
889 ip6_address_t * address = ip_interface_address_get_address (lm, a);
890 ip6_add_del_route_args_t x;
891 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
892 u32 classify_table_index;
894 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
895 x.table_index_or_table_id = fib_index;
896 x.flags = (IP6_ROUTE_FLAG_ADD
897 | IP6_ROUTE_FLAG_FIB_INDEX
898 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
899 x.dst_address = address[0];
900 x.dst_address_length = a->address_length;
904 a->neighbor_probe_adj_index = ~0;
905 if (a->address_length < 128)
907 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
909 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
910 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
911 ip6_add_del_route (im, &x);
912 a->neighbor_probe_adj_index = x.adj_index;
915 /* Add e.g. ::1/128 as local to this host. */
916 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
919 classify_table_index = ~0;
920 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
921 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
922 if (classify_table_index != (u32) ~0)
924 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
925 adj->classify_table_index = classify_table_index;
928 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
930 adj->if_address_index = a - lm->if_address_pool;
931 adj->rewrite_header.sw_if_index = sw_if_index;
932 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
933 adj->rewrite_header.data_bytes = 0;
934 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
935 x.dst_address_length = 128;
936 ip6_add_del_route (im, &x);
940 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
941 ip6_address_t * address, u32 address_length)
943 ip6_add_del_route_args_t x;
945 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
946 x.table_index_or_table_id = fib_index;
947 x.flags = (IP6_ROUTE_FLAG_DEL
948 | IP6_ROUTE_FLAG_FIB_INDEX
949 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
950 x.dst_address = address[0];
951 x.dst_address_length = address_length;
956 if (address_length < 128)
958 /* Don't wipe out fe80::0/64 */
959 if (address_length != 64 ||
960 address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
961 ip6_add_del_route (im, &x);
964 x.dst_address_length = 128;
965 ip6_add_del_route (im, &x);
967 ip6_delete_matching_routes (im,
969 IP6_ROUTE_FLAG_FIB_INDEX,
976 ip6_address_t address;
978 } ip6_interface_address_t;
980 static clib_error_t *
981 ip6_add_del_interface_address_internal (vlib_main_t * vm,
983 ip6_address_t * new_address,
989 static clib_error_t *
990 ip6_add_del_interface_address_internal (vlib_main_t * vm,
992 ip6_address_t * address,
998 vnet_main_t * vnm = vnet_get_main();
999 ip6_main_t * im = &ip6_main;
1000 ip_lookup_main_t * lm = &im->lookup_main;
1001 clib_error_t * error;
1002 u32 if_address_index;
1003 ip6_address_fib_t ip6_af, * addr_fib = 0;
1005 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1006 ip6_addr_fib_init (&ip6_af, address,
1007 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1008 vec_add1 (addr_fib, ip6_af);
1011 uword elts_before = pool_elts (lm->if_address_pool);
1013 error = ip_interface_address_add_del
1023 /* Pool did not grow: add duplicate address. */
1024 if (elts_before == pool_elts (lm->if_address_pool))
1028 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1031 ip6_del_interface_routes (im, ip6_af.fib_index, address,
1035 ip6_add_interface_routes (vnm, sw_if_index,
1036 im, ip6_af.fib_index,
1037 pool_elt_at_index (lm->if_address_pool, if_address_index));
1041 ip6_add_del_interface_address_callback_t * cb;
1042 vec_foreach (cb, im->add_del_interface_address_callbacks)
1043 cb->function (im, cb->function_opaque, sw_if_index,
1044 address, address_length,
1050 vec_free (addr_fib);
1055 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1056 ip6_address_t * address, u32 address_length,
1059 return ip6_add_del_interface_address_internal
1060 (vm, sw_if_index, address, address_length,
1061 /* redistribute */ 1,
1062 /* insert_routes */ 1,
1067 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1071 ip6_main_t * im = &ip6_main;
1072 ip_interface_address_t * ia;
1074 u32 is_admin_up, fib_index;
1076 /* Fill in lookup tables with default table (0). */
1077 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1079 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1081 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1083 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1085 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1086 0 /* honor unnumbered */,
1088 a = ip_interface_address_get_address (&im->lookup_main, ia);
1090 ip6_add_interface_routes (vnm, sw_if_index,
1094 ip6_del_interface_routes (im, fib_index,
1095 a, ia->address_length);
1101 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1104 ip6_sw_interface_add_del (vnet_main_t * vnm,
1108 vlib_main_t * vm = vnm->vlib_main;
1109 ip6_main_t * im = &ip6_main;
1110 ip_lookup_main_t * lm = &im->lookup_main;
1113 for (cast = 0; cast < VNET_N_CAST; cast++)
1115 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1116 vnet_config_main_t * vcm = &cm->config_main;
1118 /* FIXME multicast. */
1119 if (! vcm->node_index_by_feature_index)
1121 char * start_nodes[] = { "ip6-input", };
1122 char * feature_nodes[] = {
1123 [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1124 [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1125 [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1126 [IP6_RX_FEATURE_VPATH] = "vpath-input-ip6",
1127 [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1129 vnet_config_init (vm, vcm,
1130 start_nodes, ARRAY_LEN (start_nodes),
1131 feature_nodes, ARRAY_LEN (feature_nodes));
1134 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1135 ci = cm->config_index_by_sw_if_index[sw_if_index];
1138 ci = vnet_config_add_feature (vm, vcm,
1140 IP6_RX_FEATURE_LOOKUP,
1141 /* config data */ 0,
1142 /* # bytes of config data */ 0);
1144 ci = vnet_config_del_feature (vm, vcm,
1146 IP6_RX_FEATURE_LOOKUP,
1147 /* config data */ 0,
1148 /* # bytes of config data */ 0);
1150 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1152 return /* no error */ 0;
1155 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1157 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1158 .function = ip6_lookup,
1159 .name = "ip6-lookup",
1160 .vector_size = sizeof (u32),
1162 .n_next_nodes = IP_LOOKUP_N_NEXT,
1164 [IP_LOOKUP_NEXT_MISS] = "ip6-miss",
1165 [IP_LOOKUP_NEXT_DROP] = "ip6-drop",
1166 [IP_LOOKUP_NEXT_PUNT] = "ip6-punt",
1167 [IP_LOOKUP_NEXT_LOCAL] = "ip6-local",
1168 [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor",
1169 [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite",
1170 [IP_LOOKUP_NEXT_CLASSIFY] = "ip6-classify",
1171 [IP_LOOKUP_NEXT_MAP] = "ip6-map",
1172 [IP_LOOKUP_NEXT_MAP_T] = "ip6-map-t",
1173 [IP_LOOKUP_NEXT_SIXRD] = "ip6-sixrd",
1174 [IP_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop",
1175 [IP_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop",
1176 [IP_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop",
1181 /* 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, "adjacency: %U flow hash: 0x%08x",
1201 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)
1229 n_left = frame->n_vectors;
1230 from = vlib_frame_vector_args (frame);
1235 vlib_buffer_t * b0, * b1;
1236 ip6_forward_next_trace_t * t0, * t1;
1238 /* Prefetch next iteration. */
1239 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1240 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1245 b0 = vlib_get_buffer (vm, bi0);
1246 b1 = vlib_get_buffer (vm, bi1);
1248 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1250 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1251 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1252 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1253 memcpy (t0->packet_data,
1254 vlib_buffer_get_current (b0),
1255 sizeof (t0->packet_data));
1257 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1259 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1260 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1261 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1262 memcpy (t1->packet_data,
1263 vlib_buffer_get_current (b1),
1264 sizeof (t1->packet_data));
1274 ip6_forward_next_trace_t * t0;
1278 b0 = vlib_get_buffer (vm, bi0);
1280 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1282 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1283 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1284 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1285 memcpy (t0->packet_data,
1286 vlib_buffer_get_current (b0),
1287 sizeof (t0->packet_data));
1295 ip6_drop_or_punt (vlib_main_t * vm,
1296 vlib_node_runtime_t * node,
1297 vlib_frame_t * frame,
1298 ip6_error_t error_code)
1300 u32 * buffers = vlib_frame_vector_args (frame);
1301 uword n_packets = frame->n_vectors;
1303 vlib_error_drop_buffers (vm, node,
1308 ip6_input_node.index,
1311 if (node->flags & VLIB_NODE_FLAG_TRACE)
1312 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1318 ip6_drop (vlib_main_t * vm,
1319 vlib_node_runtime_t * node,
1320 vlib_frame_t * frame)
1321 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1324 ip6_punt (vlib_main_t * vm,
1325 vlib_node_runtime_t * node,
1326 vlib_frame_t * frame)
1327 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1330 ip6_miss (vlib_main_t * vm,
1331 vlib_node_runtime_t * node,
1332 vlib_frame_t * frame)
1333 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1335 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1336 .function = ip6_drop,
1338 .vector_size = sizeof (u32),
1340 .format_trace = format_ip6_forward_next_trace,
1348 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1349 .function = ip6_punt,
1351 .vector_size = sizeof (u32),
1353 .format_trace = format_ip6_forward_next_trace,
1361 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1362 .function = ip6_miss,
1364 .vector_size = sizeof (u32),
1366 .format_trace = format_ip6_forward_next_trace,
1374 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1375 .function = ip6_drop,
1376 .name = "ip6-multicast",
1377 .vector_size = sizeof (u32),
1379 .format_trace = format_ip6_forward_next_trace,
1387 /* Compute TCP/UDP/ICMP6 checksum in software. */
1388 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1391 u16 sum16, payload_length_host_byte_order;
1392 u32 i, n_this_buffer, n_bytes_left;
1393 u32 headers_size = sizeof(ip0[0]);
1394 void * data_this_buffer;
1396 ASSERT(bogus_lengthp);
1399 /* Initialize checksum with ip header. */
1400 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1401 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1402 data_this_buffer = (void *) (ip0 + 1);
1404 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1406 sum0 = ip_csum_with_carry (sum0,
1407 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1408 sum0 = ip_csum_with_carry (sum0,
1409 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1412 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1413 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1416 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1418 /* validate really icmp6 next */
1419 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1421 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1422 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1424 payload_length_host_byte_order -= skip_bytes;
1425 headers_size += skip_bytes;
1428 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1432 struct rte_mbuf *mb = ((struct rte_mbuf *)p0)-1;
1433 u8 nb_segs = mb->nb_segs;
1435 n_this_buffer = (p0->current_length > headers_size ?
1436 p0->current_length - headers_size : 0);
1437 while (n_bytes_left)
1439 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1440 n_bytes_left -= n_this_buffer;
1444 if ((nb_segs == 0) || (mb == 0))
1447 data_this_buffer = rte_ctrlmbuf_data(mb);
1448 n_this_buffer = mb->data_len;
1450 if (n_bytes_left || nb_segs)
1456 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1458 if (p0 && n_this_buffer + headers_size > p0->current_length)
1459 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1462 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1463 n_bytes_left -= n_this_buffer;
1464 if (n_bytes_left == 0)
1467 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1472 p0 = vlib_get_buffer (vm, p0->next_buffer);
1473 data_this_buffer = vlib_buffer_get_current (p0);
1474 n_this_buffer = p0->current_length;
1478 sum16 = ~ ip_csum_fold (sum0);
1483 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1485 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1486 udp_header_t * udp0;
1490 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1491 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1492 || ip0->protocol == IP_PROTOCOL_ICMP6
1493 || ip0->protocol == IP_PROTOCOL_UDP
1494 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1496 udp0 = (void *) (ip0 + 1);
1497 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1499 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1500 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1504 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1506 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1507 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1513 ip6_local (vlib_main_t * vm,
1514 vlib_node_runtime_t * node,
1515 vlib_frame_t * frame)
1517 ip6_main_t * im = &ip6_main;
1518 ip_lookup_main_t * lm = &im->lookup_main;
1519 ip_local_next_t next_index;
1520 u32 * from, * to_next, n_left_from, n_left_to_next;
1521 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1523 from = vlib_frame_vector_args (frame);
1524 n_left_from = frame->n_vectors;
1525 next_index = node->cached_next_index;
1527 if (node->flags & VLIB_NODE_FLAG_TRACE)
1528 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1530 while (n_left_from > 0)
1532 vlib_get_next_frame (vm, node, next_index,
1533 to_next, n_left_to_next);
1535 while (n_left_from >= 4 && n_left_to_next >= 2)
1537 vlib_buffer_t * p0, * p1;
1538 ip6_header_t * ip0, * ip1;
1539 udp_header_t * udp0, * udp1;
1540 u32 pi0, ip_len0, udp_len0, flags0, next0;
1541 u32 pi1, ip_len1, udp_len1, flags1, next1;
1542 i32 len_diff0, len_diff1;
1543 u8 error0, type0, good_l4_checksum0;
1544 u8 error1, type1, good_l4_checksum1;
1546 pi0 = to_next[0] = from[0];
1547 pi1 = to_next[1] = from[1];
1551 n_left_to_next -= 2;
1553 p0 = vlib_get_buffer (vm, pi0);
1554 p1 = vlib_get_buffer (vm, pi1);
1556 ip0 = vlib_buffer_get_current (p0);
1557 ip1 = vlib_buffer_get_current (p1);
1559 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1560 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1562 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1563 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1568 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1569 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1571 udp0 = ip6_next_header (ip0);
1572 udp1 = ip6_next_header (ip1);
1574 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1575 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1576 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1578 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1579 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1581 /* Verify UDP length. */
1582 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1583 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1584 udp_len0 = clib_net_to_host_u16 (udp0->length);
1585 udp_len1 = clib_net_to_host_u16 (udp1->length);
1587 len_diff0 = ip_len0 - udp_len0;
1588 len_diff1 = ip_len1 - udp_len1;
1590 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1591 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1593 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1594 && ! good_l4_checksum0
1595 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1597 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1599 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1601 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1602 && ! good_l4_checksum1
1603 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1605 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1607 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1610 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1612 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1613 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1615 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1616 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_TCP == IP6_ERROR_TCP_CHECKSUM);
1617 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1618 error0 = (! good_l4_checksum0
1619 ? IP6_ERROR_UDP_CHECKSUM + type0
1621 error1 = (! good_l4_checksum1
1622 ? IP6_ERROR_UDP_CHECKSUM + type1
1625 /* Drop packets from unroutable hosts. */
1626 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1627 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1629 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1630 error0 = (lm->miss_adj_index == src_adj_index0
1631 ? IP6_ERROR_SRC_LOOKUP_MISS
1634 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1636 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1637 error1 = (lm->miss_adj_index == src_adj_index1
1638 ? IP6_ERROR_SRC_LOOKUP_MISS
1642 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1643 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1645 p0->error = error_node->errors[error0];
1646 p1->error = error_node->errors[error1];
1648 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1649 to_next, n_left_to_next,
1650 pi0, pi1, next0, next1);
1653 while (n_left_from > 0 && n_left_to_next > 0)
1657 udp_header_t * udp0;
1658 u32 pi0, ip_len0, udp_len0, flags0, next0;
1660 u8 error0, type0, good_l4_checksum0;
1662 pi0 = to_next[0] = from[0];
1666 n_left_to_next -= 1;
1668 p0 = vlib_get_buffer (vm, pi0);
1670 ip0 = vlib_buffer_get_current (p0);
1672 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1673 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1677 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1679 udp0 = ip6_next_header (ip0);
1681 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1682 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1684 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1686 /* Verify UDP length. */
1687 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1688 udp_len0 = clib_net_to_host_u16 (udp0->length);
1690 len_diff0 = ip_len0 - udp_len0;
1692 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1694 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1695 && ! good_l4_checksum0
1696 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1698 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1700 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1703 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1705 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1707 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1708 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_TCP == IP6_ERROR_TCP_CHECKSUM);
1709 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1710 error0 = (! good_l4_checksum0
1711 ? IP6_ERROR_UDP_CHECKSUM + type0
1714 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1715 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1717 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1718 error0 = (lm->miss_adj_index == src_adj_index0
1719 ? IP6_ERROR_SRC_LOOKUP_MISS
1723 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1725 p0->error = error_node->errors[error0];
1727 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1728 to_next, n_left_to_next,
1732 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1735 return frame->n_vectors;
1738 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1739 .function = ip6_local,
1740 .name = "ip6-local",
1741 .vector_size = sizeof (u32),
1743 .format_trace = format_ip6_forward_next_trace,
1745 .n_next_nodes = IP_LOCAL_N_NEXT,
1747 [IP_LOCAL_NEXT_DROP] = "error-drop",
1748 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1749 // [IP_LOCAL_NEXT_TCP_LOOKUP] = "ip6-tcp-lookup",
1750 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1751 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1755 void ip6_register_protocol (u32 protocol, u32 node_index)
1757 vlib_main_t * vm = vlib_get_main();
1758 ip6_main_t * im = &ip6_main;
1759 ip_lookup_main_t * lm = &im->lookup_main;
1761 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1762 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1766 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1767 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1768 } ip6_discover_neighbor_next_t;
1771 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1772 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1773 } ip6_discover_neighbor_error_t;
1776 ip6_discover_neighbor (vlib_main_t * vm,
1777 vlib_node_runtime_t * node,
1778 vlib_frame_t * frame)
1780 vnet_main_t * vnm = vnet_get_main();
1781 ip6_main_t * im = &ip6_main;
1782 ip_lookup_main_t * lm = &im->lookup_main;
1783 u32 * from, * to_next_drop;
1784 uword n_left_from, n_left_to_next_drop;
1785 static f64 time_last_seed_change = -1e100;
1786 static u32 hash_seeds[3];
1787 static uword hash_bitmap[256 / BITS (uword)];
1791 if (node->flags & VLIB_NODE_FLAG_TRACE)
1792 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1794 time_now = vlib_time_now (vm);
1795 if (time_now - time_last_seed_change > 1e-3)
1798 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1799 sizeof (hash_seeds));
1800 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1801 hash_seeds[i] = r[i];
1803 /* Mark all hash keys as been not-seen before. */
1804 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1807 time_last_seed_change = time_now;
1810 from = vlib_frame_vector_args (frame);
1811 n_left_from = frame->n_vectors;
1813 while (n_left_from > 0)
1815 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1816 to_next_drop, n_left_to_next_drop);
1818 while (n_left_from > 0 && n_left_to_next_drop > 0)
1822 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1824 ip_adjacency_t * adj0;
1825 vnet_hw_interface_t * hw_if0;
1830 p0 = vlib_get_buffer (vm, pi0);
1832 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1834 ip0 = vlib_buffer_get_current (p0);
1836 adj0 = ip_get_adjacency (lm, adj_index0);
1842 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1843 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1846 b0 ^= ip0->dst_address.as_u32[0];
1847 c0 ^= ip0->dst_address.as_u32[1];
1849 hash_v3_mix32 (a0, b0, c0);
1851 b0 ^= ip0->dst_address.as_u32[2];
1852 c0 ^= ip0->dst_address.as_u32[3];
1854 hash_v3_finalize32 (a0, b0, c0);
1856 c0 &= BITS (hash_bitmap) - 1;
1857 c0 = c0 / BITS (uword);
1858 m0 = (uword) 1 << (c0 % BITS (uword));
1860 bm0 = hash_bitmap[c0];
1861 drop0 = (bm0 & m0) != 0;
1863 /* Mark it as seen. */
1864 hash_bitmap[c0] = bm0 | m0;
1868 to_next_drop[0] = pi0;
1870 n_left_to_next_drop -= 1;
1872 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1874 /* If the interface is link-down, drop the pkt */
1875 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1879 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1880 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1886 icmp6_neighbor_solicitation_header_t * h0;
1889 h0 = vlib_packet_template_get_packet
1890 (vm, &im->discover_neighbor_packet_template, &bi0);
1893 * Build ethernet header.
1894 * Choose source address based on destination lookup
1897 ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
1901 * Destination address is a solicited node multicast address.
1902 * We need to fill in
1903 * the low 24 bits with low 24 bits of target's address.
1905 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1906 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1907 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1909 h0->neighbor.target_address = ip0->dst_address;
1911 memcpy (h0->link_layer_option.ethernet_address,
1912 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1914 /* $$$$ appears we need this; why is the checksum non-zero? */
1915 h0->neighbor.icmp.checksum = 0;
1916 h0->neighbor.icmp.checksum =
1917 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1920 ASSERT (bogus_length == 0);
1922 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1923 b0 = vlib_get_buffer (vm, bi0);
1924 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1925 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1927 /* Add rewrite/encap string. */
1928 vnet_rewrite_one_header (adj0[0], h0,
1929 sizeof (ethernet_header_t));
1930 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1932 /* $$$$ hack in case next0 == 0 */
1933 b0->error = node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_DROP];
1935 vec_elt (im->discover_neighbor_next_index_by_hw_if_index,
1936 hw_if0->hw_if_index);
1938 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1942 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1943 n_left_to_next_drop);
1946 return frame->n_vectors;
1949 static char * ip6_discover_neighbor_error_strings[] = {
1950 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1951 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
1952 = "neighbor solicitations sent",
1955 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
1956 .function = ip6_discover_neighbor,
1957 .name = "ip6-discover-neighbor",
1958 .vector_size = sizeof (u32),
1960 .format_trace = format_ip6_forward_next_trace,
1962 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1963 .error_strings = ip6_discover_neighbor_error_strings,
1965 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1967 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1972 ip6_discover_neighbor_hw_interface_link_up_down (vnet_main_t * vnm,
1976 vlib_main_t * vm = vnm->vlib_main;
1977 ip6_main_t * im = &ip6_main;
1978 vnet_hw_interface_t * hw_if;
1980 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1982 vec_validate_init_empty
1983 (im->discover_neighbor_next_index_by_hw_if_index, hw_if_index, 0);
1984 im->discover_neighbor_next_index_by_hw_if_index[hw_if_index]
1985 = vlib_node_add_next (vm, ip6_discover_neighbor_node.index,
1986 hw_if->output_node_index);
1990 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION
1991 (ip6_discover_neighbor_hw_interface_link_up_down);
1994 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1996 vnet_main_t * vnm = vnet_get_main();
1997 ip6_main_t * im = &ip6_main;
1998 icmp6_neighbor_solicitation_header_t * h;
1999 ip6_address_t * src;
2000 ip_interface_address_t * ia;
2001 ip_adjacency_t * adj;
2002 vnet_hw_interface_t * hi;
2003 vnet_sw_interface_t * si;
2008 si = vnet_get_sw_interface (vnm, sw_if_index);
2010 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2012 return clib_error_return (0, "%U: interface %U down",
2013 format_ip6_address, dst,
2014 format_vnet_sw_if_index_name, vnm,
2018 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2021 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2022 return clib_error_return
2023 (0, "no matching interface address for destination %U (interface %U)",
2024 format_ip6_address, dst,
2025 format_vnet_sw_if_index_name, vnm, sw_if_index);
2028 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2030 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2032 /* Destination address is a solicited node multicast address. We need to fill in
2033 the low 24 bits with low 24 bits of target's address. */
2034 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2035 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2036 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2038 h->ip.src_address = src[0];
2039 h->neighbor.target_address = dst[0];
2041 memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2043 h->neighbor.icmp.checksum =
2044 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2045 ASSERT(bogus_length == 0);
2047 b = vlib_get_buffer (vm, bi);
2048 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2050 /* Add encapsulation string for software interface (e.g. ethernet header). */
2051 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2052 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2053 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2056 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2057 u32 * to_next = vlib_frame_vector_args (f);
2060 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2063 return /* no error */ 0;
2067 IP6_REWRITE_NEXT_DROP,
2068 } ip6_rewrite_next_t;
2071 ip6_rewrite_inline (vlib_main_t * vm,
2072 vlib_node_runtime_t * node,
2073 vlib_frame_t * frame,
2074 int rewrite_for_locally_received_packets)
2076 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2077 u32 * from = vlib_frame_vector_args (frame);
2078 u32 n_left_from, n_left_to_next, * to_next, next_index;
2079 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2080 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2082 n_left_from = frame->n_vectors;
2083 next_index = node->cached_next_index;
2084 u32 cpu_index = os_get_cpu_number();
2086 while (n_left_from > 0)
2088 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2090 while (n_left_from >= 4 && n_left_to_next >= 2)
2092 ip_adjacency_t * adj0, * adj1;
2093 vlib_buffer_t * p0, * p1;
2094 ip6_header_t * ip0, * ip1;
2095 u32 pi0, rw_len0, next0, error0, adj_index0;
2096 u32 pi1, rw_len1, next1, error1, adj_index1;
2098 /* Prefetch next iteration. */
2100 vlib_buffer_t * p2, * p3;
2102 p2 = vlib_get_buffer (vm, from[2]);
2103 p3 = vlib_get_buffer (vm, from[3]);
2105 vlib_prefetch_buffer_header (p2, LOAD);
2106 vlib_prefetch_buffer_header (p3, LOAD);
2108 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2109 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2111 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2112 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2115 pi0 = to_next[0] = from[0];
2116 pi1 = to_next[1] = from[1];
2121 n_left_to_next -= 2;
2123 p0 = vlib_get_buffer (vm, pi0);
2124 p1 = vlib_get_buffer (vm, pi1);
2126 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2127 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2129 /* We should never rewrite a pkt using the MISS adjacency */
2130 ASSERT(adj_index0 && adj_index1);
2132 ip0 = vlib_buffer_get_current (p0);
2133 ip1 = vlib_buffer_get_current (p1);
2135 error0 = error1 = IP6_ERROR_NONE;
2137 if (! rewrite_for_locally_received_packets)
2139 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2141 /* Input node should have reject packets with hop limit 0. */
2142 ASSERT (ip0->hop_limit > 0);
2143 ASSERT (ip1->hop_limit > 0);
2148 ip0->hop_limit = hop_limit0;
2149 ip1->hop_limit = hop_limit1;
2151 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2152 error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2155 adj0 = ip_get_adjacency (lm, adj_index0);
2156 adj1 = ip_get_adjacency (lm, adj_index1);
2158 if (rewrite_for_locally_received_packets)
2161 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2162 * we end up here with a local adjacency in hand
2164 if (PREDICT_FALSE(adj0->lookup_next_index
2165 == IP_LOOKUP_NEXT_LOCAL))
2166 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2167 if (PREDICT_FALSE(adj1->lookup_next_index
2168 == IP_LOOKUP_NEXT_LOCAL))
2169 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2172 rw_len0 = adj0[0].rewrite_header.data_bytes;
2173 rw_len1 = adj1[0].rewrite_header.data_bytes;
2175 vlib_increment_combined_counter (&lm->adjacency_counters,
2178 /* packet increment */ 0,
2179 /* byte increment */ rw_len0);
2180 vlib_increment_combined_counter (&lm->adjacency_counters,
2183 /* packet increment */ 0,
2184 /* byte increment */ rw_len1);
2186 /* Check MTU of outgoing interface. */
2187 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2188 ? IP6_ERROR_MTU_EXCEEDED
2190 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2191 ? IP6_ERROR_MTU_EXCEEDED
2194 p0->current_data -= rw_len0;
2195 p1->current_data -= rw_len1;
2197 p0->current_length += rw_len0;
2198 p1->current_length += rw_len1;
2200 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2201 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2203 next0 = (error0 == IP6_ERROR_NONE) ?
2204 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2205 next1 = (error1 == IP6_ERROR_NONE) ?
2206 adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2208 /* Guess we are only writing on simple Ethernet header. */
2209 vnet_rewrite_two_headers (adj0[0], adj1[0],
2211 sizeof (ethernet_header_t));
2213 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2214 to_next, n_left_to_next,
2215 pi0, pi1, next0, next1);
2218 while (n_left_from > 0 && n_left_to_next > 0)
2220 ip_adjacency_t * adj0;
2224 u32 adj_index0, next0, error0;
2226 pi0 = to_next[0] = from[0];
2228 p0 = vlib_get_buffer (vm, pi0);
2230 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2232 /* We should never rewrite a pkt using the MISS adjacency */
2235 adj0 = ip_get_adjacency (lm, adj_index0);
2237 ip0 = vlib_buffer_get_current (p0);
2239 error0 = IP6_ERROR_NONE;
2241 /* Check hop limit */
2242 if (! rewrite_for_locally_received_packets)
2244 i32 hop_limit0 = ip0->hop_limit;
2246 ASSERT (ip0->hop_limit > 0);
2250 ip0->hop_limit = hop_limit0;
2252 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2255 if (rewrite_for_locally_received_packets)
2257 if (PREDICT_FALSE(adj0->lookup_next_index
2258 == IP_LOOKUP_NEXT_LOCAL))
2259 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2262 /* Guess we are only writing on simple Ethernet header. */
2263 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2265 /* Update packet buffer attributes/set output interface. */
2266 rw_len0 = adj0[0].rewrite_header.data_bytes;
2268 vlib_increment_combined_counter (&lm->adjacency_counters,
2271 /* packet increment */ 0,
2272 /* byte increment */ rw_len0);
2274 /* Check MTU of outgoing interface. */
2275 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2276 ? IP6_ERROR_MTU_EXCEEDED
2279 p0->current_data -= rw_len0;
2280 p0->current_length += rw_len0;
2281 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2283 next0 = (error0 == IP6_ERROR_NONE) ?
2284 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2286 p0->error = error_node->errors[error0];
2291 n_left_to_next -= 1;
2293 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2294 to_next, n_left_to_next,
2298 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2301 /* Need to do trace after rewrites to pick up new packet data. */
2302 if (node->flags & VLIB_NODE_FLAG_TRACE)
2303 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2305 return frame->n_vectors;
2309 ip6_rewrite_transit (vlib_main_t * vm,
2310 vlib_node_runtime_t * node,
2311 vlib_frame_t * frame)
2313 return ip6_rewrite_inline (vm, node, frame,
2314 /* rewrite_for_locally_received_packets */ 0);
2318 ip6_rewrite_local (vlib_main_t * vm,
2319 vlib_node_runtime_t * node,
2320 vlib_frame_t * frame)
2322 return ip6_rewrite_inline (vm, node, frame,
2323 /* rewrite_for_locally_received_packets */ 1);
2326 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2327 .function = ip6_rewrite_transit,
2328 .name = "ip6-rewrite",
2329 .vector_size = sizeof (u32),
2331 .format_trace = format_ip6_forward_next_trace,
2335 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2339 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2340 .function = ip6_rewrite_local,
2341 .name = "ip6-rewrite-local",
2342 .vector_size = sizeof (u32),
2344 .sibling_of = "ip6-rewrite",
2346 .format_trace = format_ip6_forward_next_trace,
2350 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2354 /* Global IP6 main. */
2355 ip6_main_t ip6_main;
2357 static clib_error_t *
2358 ip6_lookup_init (vlib_main_t * vm)
2360 ip6_main_t * im = &ip6_main;
2363 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2370 for (j = 0; j < i0; j++)
2371 im->fib_masks[i].as_u32[j] = ~0;
2374 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2377 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2379 if (im->lookup_table_nbuckets == 0)
2380 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2382 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2384 if (im->lookup_table_size == 0)
2385 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2387 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2388 im->lookup_table_nbuckets,
2389 im->lookup_table_size);
2391 /* Create FIB with index 0 and table id of 0. */
2392 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2396 pn = pg_get_node (ip6_lookup_node.index);
2397 pn->unformat_edit = unformat_pg_ip6_header;
2401 icmp6_neighbor_solicitation_header_t p;
2403 memset (&p, 0, sizeof (p));
2405 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2406 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2407 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2408 p.ip.protocol = IP_PROTOCOL_ICMP6;
2409 p.ip.hop_limit = 255;
2410 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2412 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2414 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2415 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2417 vlib_packet_template_init (vm,
2418 &im->discover_neighbor_packet_template,
2420 /* alloc chunk size */ 8,
2421 "ip6 neighbor discovery");
2427 VLIB_INIT_FUNCTION (ip6_lookup_init);
2429 static clib_error_t *
2430 add_del_ip6_interface_table (vlib_main_t * vm,
2431 unformat_input_t * input,
2432 vlib_cli_command_t * cmd)
2434 vnet_main_t * vnm = vnet_get_main();
2435 clib_error_t * error = 0;
2436 u32 sw_if_index, table_id;
2440 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2442 error = clib_error_return (0, "unknown interface `%U'",
2443 format_unformat_error, input);
2447 if (unformat (input, "%d", &table_id))
2451 error = clib_error_return (0, "expected table id `%U'",
2452 format_unformat_error, input);
2457 ip6_main_t * im = &ip6_main;
2459 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2463 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2464 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2472 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2473 .path = "set interface ip6 table",
2474 .function = add_del_ip6_interface_table,
2475 .short_help = "set interface ip6 table <intfc> <table-id>"
2479 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2482 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2483 /* Invert the "u" bit */
2484 ip->as_u8 [8] = mac[0] ^ (1<<1);
2485 ip->as_u8 [9] = mac[1];
2486 ip->as_u8 [10] = mac[2];
2487 ip->as_u8 [11] = 0xFF;
2488 ip->as_u8 [12] = 0xFE;
2489 ip->as_u8 [13] = mac[3];
2490 ip->as_u8 [14] = mac[4];
2491 ip->as_u8 [15] = mac[5];
2495 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
2498 /* Invert the previously inverted "u" bit */
2499 mac[0] = ip->as_u8 [8] ^ (1<<1);
2500 mac[1] = ip->as_u8 [9];
2501 mac[2] = ip->as_u8 [10];
2502 mac[3] = ip->as_u8 [13];
2503 mac[4] = ip->as_u8 [14];
2504 mac[5] = ip->as_u8 [15];
2507 static clib_error_t *
2508 test_ip6_link_command_fn (vlib_main_t * vm,
2509 unformat_input_t * input,
2510 vlib_cli_command_t * cmd)
2513 ip6_address_t _a, *a = &_a;
2515 if (unformat (input, "%U", unformat_ethernet_address, mac))
2517 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2518 vlib_cli_output (vm, "Link local address: %U",
2519 format_ip6_address, a);
2520 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2521 vlib_cli_output (vm, "Original MAC address: %U",
2522 format_ethernet_address, mac);
2528 VLIB_CLI_COMMAND (test_link_command, static) = {
2529 .path = "test ip6 link",
2530 .function = test_ip6_link_command_fn,
2531 .short_help = "test ip6 link <mac-address>",
2534 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2536 ip6_main_t * im6 = &ip6_main;
2538 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2543 fib = vec_elt_at_index (im6->fibs, p[0]);
2545 fib->flow_hash_config = flow_hash_config;
2549 static clib_error_t *
2550 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2551 unformat_input_t * input,
2552 vlib_cli_command_t * cmd)
2556 u32 flow_hash_config = 0;
2559 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2560 if (unformat (input, "table %d", &table_id))
2563 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2564 foreach_flow_hash_bit
2570 return clib_error_return (0, "unknown input `%U'",
2571 format_unformat_error, input);
2573 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2580 return clib_error_return (0, "no such FIB table %d", table_id);
2583 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2590 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2591 .path = "set ip6 flow-hash",
2593 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2594 .function = set_ip6_flow_hash_command_fn,
2597 static clib_error_t *
2598 show_ip6_local_command_fn (vlib_main_t * vm,
2599 unformat_input_t * input,
2600 vlib_cli_command_t * cmd)
2602 ip6_main_t * im = &ip6_main;
2603 ip_lookup_main_t * lm = &im->lookup_main;
2606 vlib_cli_output (vm, "Protocols handled by ip6_local");
2607 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2609 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2610 vlib_cli_output (vm, "%d", i);
2617 VLIB_CLI_COMMAND (show_ip_local, static) = {
2618 .path = "show ip6 local",
2619 .function = show_ip6_local_command_fn,
2620 .short_help = "Show ip6 local protocol table",
2623 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2626 vnet_main_t * vnm = vnet_get_main();
2627 vnet_interface_main_t * im = &vnm->interface_main;
2628 ip6_main_t * ipm = &ip6_main;
2629 ip_lookup_main_t * lm = &ipm->lookup_main;
2630 vnet_classify_main_t * cm = &vnet_classify_main;
2632 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2633 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2635 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2636 return VNET_API_ERROR_NO_SUCH_ENTRY;
2638 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2639 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2644 static clib_error_t *
2645 set_ip6_classify_command_fn (vlib_main_t * vm,
2646 unformat_input_t * input,
2647 vlib_cli_command_t * cmd)
2649 u32 table_index = ~0;
2650 int table_index_set = 0;
2651 u32 sw_if_index = ~0;
2654 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2655 if (unformat (input, "table-index %d", &table_index))
2656 table_index_set = 1;
2657 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2658 vnet_get_main(), &sw_if_index))
2664 if (table_index_set == 0)
2665 return clib_error_return (0, "classify table-index must be specified");
2667 if (sw_if_index == ~0)
2668 return clib_error_return (0, "interface / subif must be specified");
2670 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2677 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2678 return clib_error_return (0, "No such interface");
2680 case VNET_API_ERROR_NO_SUCH_ENTRY:
2681 return clib_error_return (0, "No such classifier table");
2686 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2687 .path = "set ip6 classify",
2689 "set ip6 classify intfc <int> table-index <index>",
2690 .function = set_ip6_classify_command_fn,
2693 static clib_error_t *
2694 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2696 ip6_main_t * im = &ip6_main;
2701 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2702 if (unformat (input, "hash-buckets %d", &tmp))
2704 else if (unformat (input, "heap-size %dm", &tmp))
2705 heapsize = ((u64)tmp) << 20;
2706 else if (unformat (input, "heap-size %dM", &tmp))
2707 heapsize = ((u64)tmp) << 20;
2708 else if (unformat (input, "heap-size %dg", &tmp))
2709 heapsize = ((u64)tmp) << 30;
2710 else if (unformat (input, "heap-size %dG", &tmp))
2711 heapsize = ((u64)tmp) << 30;
2713 return clib_error_return (0, "unknown input '%U'",
2714 format_unformat_error, input);
2717 im->lookup_table_nbuckets = nbuckets;
2718 im->lookup_table_size = heapsize;
2723 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");