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 /* Avoid spurious reference count increments */
269 if (old_adj_index == adj_index && !(a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
271 ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
272 if (adj->share_count > 0)
276 /* Delete old adjacency index if present and changed. */
278 if (! (a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
279 && old_adj_index != ~0
280 && old_adj_index != adj_index)
281 ip_del_adjacency (lm, old_adj_index);
286 ip6_add_del_route_next_hop (ip6_main_t * im,
288 ip6_address_t * dst_address,
289 u32 dst_address_length,
290 ip6_address_t * next_hop,
291 u32 next_hop_sw_if_index,
292 u32 next_hop_weight, u32 adj_index,
293 u32 explicit_fib_index)
295 vnet_main_t * vnm = vnet_get_main();
296 ip_lookup_main_t * lm = &im->lookup_main;
299 ip6_address_t masked_dst_address;
300 u32 old_mp_adj_index, new_mp_adj_index;
301 u32 dst_adj_index, nh_adj_index;
303 ip_adjacency_t * dst_adj;
304 ip_multipath_adjacency_t * old_mp, * new_mp;
305 int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
306 int is_interface_next_hop;
307 clib_error_t * error = 0;
309 BVT(clib_bihash_kv) kv, value;
311 vlib_smp_unsafe_warning();
313 if (explicit_fib_index == (u32)~0)
314 fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
316 fib_index = explicit_fib_index;
318 fib = vec_elt_at_index (im->fibs, fib_index);
320 /* Lookup next hop to be added or deleted. */
321 is_interface_next_hop = ip6_address_is_zero (next_hop);
322 if (adj_index == (u32)~0)
324 if (is_interface_next_hop)
326 nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index,
327 next_hop_sw_if_index);
329 nh_adj_index = *nh_result;
332 ip_adjacency_t * adj;
333 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
335 ip6_adjacency_set_interface_route (vnm, adj,
336 next_hop_sw_if_index, ~0);
337 ip_call_add_del_adjacency_callbacks
338 (lm, next_hop_sw_if_index, /* is_del */ 0);
339 hash_set (im->interface_route_adj_index_by_sw_if_index,
340 next_hop_sw_if_index, nh_adj_index);
345 /* Look for the interface /128 route */
346 kv.key[0] = next_hop->as_u64[0];
347 kv.key[1] = next_hop->as_u64[1];
348 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
350 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
352 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
353 error = clib_error_return (0, "next-hop %U/128 not in FIB",
354 format_ip6_address, next_hop);
358 nh_adj_index = value.value;
363 /* Look for the interface /128 route */
364 kv.key[0] = next_hop->as_u64[0];
365 kv.key[1] = next_hop->as_u64[1];
366 kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
368 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
370 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
371 error = clib_error_return (0, "next-hop %U/128 not in FIB",
372 format_ip6_address, next_hop);
376 nh_adj_index = value.value;
379 ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
380 masked_dst_address = dst_address[0];
381 ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
383 kv.key[0] = masked_dst_address.as_u64[0];
384 kv.key[1] = masked_dst_address.as_u64[1];
385 kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
387 rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
391 dst_adj_index = value.value;
392 dst_adj = ip_get_adjacency (lm, dst_adj_index);
396 /* For deletes destination must be known. */
399 vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
400 error = clib_error_return (0, "unknown destination %U/%d",
401 format_ip6_address, dst_address,
410 /* Ignore adds of X/128 with next hop of X. */
412 && dst_address_length == 128
413 && ip6_address_is_equal (dst_address, next_hop))
415 vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
416 error = clib_error_return (0, "prefix matches next hop %U/%d",
417 format_ip6_address, dst_address,
422 old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
424 if (! ip_multipath_adjacency_add_del_next_hop
426 dst_adj ? dst_adj->heap_handle : ~0,
431 vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
432 error = clib_error_return
433 (0, "requested deleting next-hop %U not found in multi-path",
434 format_ip6_address, next_hop);
439 if (old_mp_adj_index != ~0)
440 old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
441 if (new_mp_adj_index != ~0)
442 new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
444 if (old_mp != new_mp)
446 ip6_add_del_route_args_t a;
447 a.table_index_or_table_id = fib_index;
448 a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
449 | IP6_ROUTE_FLAG_FIB_INDEX
450 | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
451 | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
452 a.dst_address = dst_address[0];
453 a.dst_address_length = dst_address_length;
454 a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
458 ip6_add_del_route (im, &a);
463 clib_error_report (error);
467 ip6_get_route (ip6_main_t * im,
468 u32 table_index_or_table_id,
470 ip6_address_t * address,
473 ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
474 ip6_address_t masked_address;
475 BVT(clib_bihash_kv) kv, value;
477 ASSERT (address_length < ARRAY_LEN (im->fib_masks));
478 memcpy (&masked_address, address, sizeof (masked_address));
479 ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
481 kv.key[0] = masked_address.as_u64[0];
482 kv.key[1] = masked_address.as_u64[1];
483 kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
485 if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
486 return (value.value);
491 ip6_foreach_matching_route (ip6_main_t * im,
492 u32 table_index_or_table_id,
494 ip6_address_t * dst_address,
496 ip6_address_t ** results,
497 u8 ** result_lengths)
500 find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
501 BVT(clib_bihash) * h = &im->ip6_lookup_table;
502 BVT(clib_bihash_value) * v;
503 clib_bihash_bucket_t * b;
507 _vec_len (*results) = 0;
509 _vec_len (*result_lengths) = 0;
511 /* Walk the table looking for routes which match the supplied address */
512 for (i = 0; i < h->nbuckets; i++)
518 v = BV(clib_bihash_get_value) (h, b->offset);
519 for (j = 0; j < (1<<b->log2_pages); j++)
521 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
523 if (BV(clib_bihash_is_free)(&v->kvp[k]))
526 if ((v->kvp[k].key[2]
527 == (((u64)((fib - im->fibs))<<32) | address_length))
528 && ip6_destination_matches_route
529 (im, dst_address, (ip6_address_t *) &v->kvp[k],
534 a = (ip6_address_t *)(&v->kvp[k]);
536 vec_add1 (*results, a[0]);
537 vec_add1 (*result_lengths, address_length);
545 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
546 u32 table_index_or_table_id,
551 = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
553 ip_lookup_main_t * lm = &im->lookup_main;
555 if (lm->n_adjacency_remaps == 0)
558 clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
560 /* All remaps have been performed. */
561 lm->n_adjacency_remaps = 0;
564 void ip6_delete_matching_routes (ip6_main_t * im,
565 u32 table_index_or_table_id,
567 ip6_address_t * address,
570 /* $$$$ static may be OK - this should happen only on thread 0 */
571 static ip6_address_t * matching_addresses;
572 static u8 * matching_address_lengths;
574 ip6_add_del_route_args_t a;
576 vlib_smp_unsafe_warning();
578 a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
579 a.table_index_or_table_id = table_index_or_table_id;
584 for (l = address_length + 1; l <= 128; l++)
586 ip6_foreach_matching_route (im, table_index_or_table_id, flags,
590 &matching_address_lengths);
591 for (i = 0; i < vec_len (matching_addresses); i++)
593 a.dst_address = matching_addresses[i];
594 a.dst_address_length = matching_address_lengths[i];
595 ip6_add_del_route (im, &a);
599 ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
603 ip6_lookup (vlib_main_t * vm,
604 vlib_node_runtime_t * node,
605 vlib_frame_t * frame)
607 ip6_main_t * im = &ip6_main;
608 ip_lookup_main_t * lm = &im->lookup_main;
609 vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
610 u32 n_left_from, n_left_to_next, * from, * to_next;
611 ip_lookup_next_t next;
612 u32 cpu_index = os_get_cpu_number();
614 from = vlib_frame_vector_args (frame);
615 n_left_from = frame->n_vectors;
616 next = node->cached_next_index;
618 while (n_left_from > 0)
620 vlib_get_next_frame (vm, node, next,
621 to_next, n_left_to_next);
623 while (n_left_from >= 4 && n_left_to_next >= 2)
625 vlib_buffer_t * p0, * p1;
626 u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
627 ip_lookup_next_t next0, next1;
628 ip6_header_t * ip0, * ip1;
629 ip_adjacency_t * adj0, * adj1;
630 u32 fib_index0, fib_index1;
631 u32 flow_hash_config0, flow_hash_config1;
633 /* Prefetch next iteration. */
635 vlib_buffer_t * p2, * p3;
637 p2 = vlib_get_buffer (vm, from[2]);
638 p3 = vlib_get_buffer (vm, from[3]);
640 vlib_prefetch_buffer_header (p2, LOAD);
641 vlib_prefetch_buffer_header (p3, LOAD);
642 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
643 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
646 pi0 = to_next[0] = from[0];
647 pi1 = to_next[1] = from[1];
649 p0 = vlib_get_buffer (vm, pi0);
650 p1 = vlib_get_buffer (vm, pi1);
652 ip0 = vlib_buffer_get_current (p0);
653 ip1 = vlib_buffer_get_current (p1);
655 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
656 fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
658 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
659 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
660 fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
661 fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
663 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
665 adj_index1 = ip6_fib_lookup_with_table (im, fib_index1,
668 adj0 = ip_get_adjacency (lm, adj_index0);
669 adj1 = ip_get_adjacency (lm, adj_index1);
671 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
673 adj_index0 = ip6_fib_lookup_with_table
674 (im, adj0->explicit_fib_index, &ip0->dst_address);
675 adj0 = ip_get_adjacency (lm, adj_index0);
677 if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
679 adj_index1 = ip6_fib_lookup_with_table
680 (im, adj1->explicit_fib_index, &ip1->dst_address);
681 adj1 = ip_get_adjacency (lm, adj_index1);
684 next0 = adj0->lookup_next_index;
685 next1 = adj1->lookup_next_index;
687 /* Process hop-by-hop options if present */
688 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
689 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
690 next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
691 IP_LOOKUP_NEXT_HOP_BY_HOP : next1;
693 vnet_buffer (p0)->ip.flow_hash =
694 vnet_buffer(p1)->ip.flow_hash = 0;
696 if (PREDICT_FALSE(adj0->n_adj > 1))
699 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
700 vnet_buffer (p0)->ip.flow_hash =
701 ip6_compute_flow_hash (ip0, flow_hash_config0);
704 if (PREDICT_FALSE(adj1->n_adj > 1))
707 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
709 vnet_buffer (p1)->ip.flow_hash =
710 ip6_compute_flow_hash (ip1, flow_hash_config1);
713 ASSERT (adj0->n_adj > 0);
714 ASSERT (adj1->n_adj > 0);
715 ASSERT (is_pow2 (adj0->n_adj));
716 ASSERT (is_pow2 (adj1->n_adj));
717 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
718 adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
720 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
721 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
723 vlib_increment_combined_counter
724 (cm, cpu_index, adj_index0, 1,
725 vlib_buffer_length_in_chain (vm, p0));
726 vlib_increment_combined_counter
727 (cm, cpu_index, adj_index1, 1,
728 vlib_buffer_length_in_chain (vm, p1));
735 wrong_next = (next0 != next) + 2*(next1 != next);
736 if (PREDICT_FALSE (wrong_next != 0))
745 vlib_set_next_frame_buffer (vm, node, next0, pi0);
752 vlib_set_next_frame_buffer (vm, node, next1, pi1);
759 vlib_set_next_frame_buffer (vm, node, next0, pi0);
760 vlib_set_next_frame_buffer (vm, node, next1, pi1);
764 vlib_put_next_frame (vm, node, next, n_left_to_next);
766 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
772 while (n_left_from > 0 && n_left_to_next > 0)
777 ip_lookup_next_t next0;
778 ip_adjacency_t * adj0;
779 u32 fib_index0, flow_hash_config0;
784 p0 = vlib_get_buffer (vm, pi0);
786 ip0 = vlib_buffer_get_current (p0);
788 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
789 fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
790 fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
793 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
795 adj_index0 = ip6_fib_lookup_with_table (im, fib_index0,
798 adj0 = ip_get_adjacency (lm, adj_index0);
800 if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
802 adj_index0 = ip6_fib_lookup_with_table
803 (im, adj0->explicit_fib_index, &ip0->dst_address);
804 adj0 = ip_get_adjacency (lm, adj_index0);
807 next0 = adj0->lookup_next_index;
808 next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
809 IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
811 vnet_buffer (p0)->ip.flow_hash = 0;
813 if (PREDICT_FALSE(adj0->n_adj > 1))
816 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
817 vnet_buffer (p0)->ip.flow_hash =
818 ip6_compute_flow_hash (ip0, flow_hash_config0);
821 ASSERT (adj0->n_adj > 0);
822 ASSERT (is_pow2 (adj0->n_adj));
823 adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
825 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
827 vlib_increment_combined_counter
828 (cm, cpu_index, adj_index0, 1,
829 vlib_buffer_length_in_chain (vm, p0));
836 if (PREDICT_FALSE (next0 != next))
839 vlib_put_next_frame (vm, node, next, n_left_to_next);
841 vlib_get_next_frame (vm, node, next,
842 to_next, n_left_to_next);
849 vlib_put_next_frame (vm, node, next, n_left_to_next);
852 return frame->n_vectors;
855 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
856 ip_adjacency_t * adj,
858 u32 if_address_index)
860 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
864 if (hw->hw_class_index == ethernet_hw_interface_class.index
865 || hw->hw_class_index == srp_hw_interface_class.index)
867 n = IP_LOOKUP_NEXT_ARP;
868 node_index = ip6_discover_neighbor_node.index;
869 adj->if_address_index = if_address_index;
873 n = IP_LOOKUP_NEXT_REWRITE;
874 node_index = ip6_rewrite_node.index;
877 adj->lookup_next_index = n;
878 adj->explicit_fib_index = ~0;
880 vnet_rewrite_for_sw_interface
882 VNET_L3_PACKET_TYPE_IP6,
885 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
886 &adj->rewrite_header,
887 sizeof (adj->rewrite_data));
891 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
892 ip6_main_t * im, u32 fib_index,
893 ip_interface_address_t * a)
895 ip_lookup_main_t * lm = &im->lookup_main;
896 ip_adjacency_t * adj;
897 ip6_address_t * address = ip_interface_address_get_address (lm, a);
898 ip6_add_del_route_args_t x;
899 vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
900 u32 classify_table_index;
902 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
903 x.table_index_or_table_id = fib_index;
904 x.flags = (IP6_ROUTE_FLAG_ADD
905 | IP6_ROUTE_FLAG_FIB_INDEX
906 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
907 x.dst_address = address[0];
908 x.dst_address_length = a->address_length;
912 a->neighbor_probe_adj_index = ~0;
913 if (a->address_length < 128)
915 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
917 ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
918 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
919 ip6_add_del_route (im, &x);
920 a->neighbor_probe_adj_index = x.adj_index;
923 /* Add e.g. ::1/128 as local to this host. */
924 adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
927 classify_table_index = ~0;
928 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
929 classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
930 if (classify_table_index != (u32) ~0)
932 adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
933 adj->classify.table_index = classify_table_index;
936 adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
938 adj->if_address_index = a - lm->if_address_pool;
939 adj->rewrite_header.sw_if_index = sw_if_index;
940 adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
941 adj->rewrite_header.data_bytes = 0;
942 ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
943 x.dst_address_length = 128;
944 ip6_add_del_route (im, &x);
948 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
949 ip6_address_t * address, u32 address_length)
951 ip6_add_del_route_args_t x;
953 /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
954 x.table_index_or_table_id = fib_index;
955 x.flags = (IP6_ROUTE_FLAG_DEL
956 | IP6_ROUTE_FLAG_FIB_INDEX
957 | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
958 x.dst_address = address[0];
959 x.dst_address_length = address_length;
964 if (address_length < 128)
966 /* Don't wipe out fe80::0/64 */
967 if (address_length != 64 ||
968 address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
969 ip6_add_del_route (im, &x);
972 x.dst_address_length = 128;
973 ip6_add_del_route (im, &x);
975 ip6_delete_matching_routes (im,
977 IP6_ROUTE_FLAG_FIB_INDEX,
984 ip6_address_t address;
986 } ip6_interface_address_t;
988 static clib_error_t *
989 ip6_add_del_interface_address_internal (vlib_main_t * vm,
991 ip6_address_t * new_address,
997 static clib_error_t *
998 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1000 ip6_address_t * address,
1006 vnet_main_t * vnm = vnet_get_main();
1007 ip6_main_t * im = &ip6_main;
1008 ip_lookup_main_t * lm = &im->lookup_main;
1009 clib_error_t * error;
1010 u32 if_address_index;
1011 ip6_address_fib_t ip6_af, * addr_fib = 0;
1013 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1014 ip6_addr_fib_init (&ip6_af, address,
1015 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1016 vec_add1 (addr_fib, ip6_af);
1019 uword elts_before = pool_elts (lm->if_address_pool);
1021 error = ip_interface_address_add_del
1031 /* Pool did not grow: add duplicate address. */
1032 if (elts_before == pool_elts (lm->if_address_pool))
1036 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1039 ip6_del_interface_routes (im, ip6_af.fib_index, address,
1043 ip6_add_interface_routes (vnm, sw_if_index,
1044 im, ip6_af.fib_index,
1045 pool_elt_at_index (lm->if_address_pool, if_address_index));
1049 ip6_add_del_interface_address_callback_t * cb;
1050 vec_foreach (cb, im->add_del_interface_address_callbacks)
1051 cb->function (im, cb->function_opaque, sw_if_index,
1052 address, address_length,
1058 vec_free (addr_fib);
1063 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1064 ip6_address_t * address, u32 address_length,
1067 return ip6_add_del_interface_address_internal
1068 (vm, sw_if_index, address, address_length,
1069 /* redistribute */ 1,
1070 /* insert_routes */ 1,
1075 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1079 ip6_main_t * im = &ip6_main;
1080 ip_interface_address_t * ia;
1082 u32 is_admin_up, fib_index;
1084 /* Fill in lookup tables with default table (0). */
1085 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1087 vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1089 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1091 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1093 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
1094 0 /* honor unnumbered */,
1096 a = ip_interface_address_get_address (&im->lookup_main, ia);
1098 ip6_add_interface_routes (vnm, sw_if_index,
1102 ip6_del_interface_routes (im, fib_index,
1103 a, ia->address_length);
1109 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1112 ip6_sw_interface_add_del (vnet_main_t * vnm,
1116 vlib_main_t * vm = vnm->vlib_main;
1117 ip6_main_t * im = &ip6_main;
1118 ip_lookup_main_t * lm = &im->lookup_main;
1121 for (cast = 0; cast < VNET_N_CAST; cast++)
1123 ip_config_main_t * cm = &lm->rx_config_mains[cast];
1124 vnet_config_main_t * vcm = &cm->config_main;
1126 /* FIXME multicast. */
1127 if (! vcm->node_index_by_feature_index)
1129 char * start_nodes[] = { "ip6-input", };
1130 char * feature_nodes[] = {
1131 [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1132 [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1133 [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1134 [IP6_RX_FEATURE_VPATH] = "vpath-input-ip6",
1135 [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1137 vnet_config_init (vm, vcm,
1138 start_nodes, ARRAY_LEN (start_nodes),
1139 feature_nodes, ARRAY_LEN (feature_nodes));
1142 vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1143 ci = cm->config_index_by_sw_if_index[sw_if_index];
1146 ci = vnet_config_add_feature (vm, vcm,
1148 IP6_RX_FEATURE_LOOKUP,
1149 /* config data */ 0,
1150 /* # bytes of config data */ 0);
1152 ci = vnet_config_del_feature (vm, vcm,
1154 IP6_RX_FEATURE_LOOKUP,
1155 /* config data */ 0,
1156 /* # bytes of config data */ 0);
1158 cm->config_index_by_sw_if_index[sw_if_index] = ci;
1160 return /* no error */ 0;
1163 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1165 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1166 .function = ip6_lookup,
1167 .name = "ip6-lookup",
1168 .vector_size = sizeof (u32),
1170 .n_next_nodes = IP_LOOKUP_N_NEXT,
1172 [IP_LOOKUP_NEXT_MISS] = "ip6-miss",
1173 [IP_LOOKUP_NEXT_DROP] = "ip6-drop",
1174 [IP_LOOKUP_NEXT_PUNT] = "ip6-punt",
1175 [IP_LOOKUP_NEXT_LOCAL] = "ip6-local",
1176 [IP_LOOKUP_NEXT_ARP] = "ip6-discover-neighbor",
1177 [IP_LOOKUP_NEXT_REWRITE] = "ip6-rewrite",
1178 [IP_LOOKUP_NEXT_CLASSIFY] = "ip6-classify",
1179 [IP_LOOKUP_NEXT_MAP] = "ip6-map",
1180 [IP_LOOKUP_NEXT_MAP_T] = "ip6-map-t",
1181 [IP_LOOKUP_NEXT_SIXRD] = "ip6-sixrd",
1182 [IP_LOOKUP_NEXT_HOP_BY_HOP] = "ip6-hop-by-hop",
1183 [IP_LOOKUP_NEXT_ADD_HOP_BY_HOP] = "ip6-add-hop-by-hop",
1184 [IP_LOOKUP_NEXT_POP_HOP_BY_HOP] = "ip6-pop-hop-by-hop",
1189 /* Adjacency taken. */
1194 /* Packet data, possibly *after* rewrite. */
1195 u8 packet_data[64 - 1*sizeof(u32)];
1196 } ip6_forward_next_trace_t;
1198 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1200 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1201 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1202 ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1203 vnet_main_t * vnm = vnet_get_main();
1204 ip6_main_t * im = &ip6_main;
1205 ip_adjacency_t * adj;
1206 uword indent = format_get_indent (s);
1208 adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1209 s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1210 t->fib_index, t->adj_index, format_ip_adjacency,
1211 vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1212 switch (adj->lookup_next_index)
1214 case IP_LOOKUP_NEXT_REWRITE:
1215 s = format (s, "\n%U%U",
1216 format_white_space, indent,
1217 format_ip_adjacency_packet_data,
1218 vnm, &im->lookup_main, t->adj_index,
1219 t->packet_data, sizeof (t->packet_data));
1229 /* Common trace function for all ip6-forward next nodes. */
1231 ip6_forward_next_trace (vlib_main_t * vm,
1232 vlib_node_runtime_t * node,
1233 vlib_frame_t * frame,
1234 vlib_rx_or_tx_t which_adj_index)
1237 ip6_main_t * im = &ip6_main;
1239 n_left = frame->n_vectors;
1240 from = vlib_frame_vector_args (frame);
1245 vlib_buffer_t * b0, * b1;
1246 ip6_forward_next_trace_t * t0, * t1;
1248 /* Prefetch next iteration. */
1249 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1250 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1255 b0 = vlib_get_buffer (vm, bi0);
1256 b1 = vlib_get_buffer (vm, bi1);
1258 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1260 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1261 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1262 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1263 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1264 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1265 memcpy (t0->packet_data,
1266 vlib_buffer_get_current (b0),
1267 sizeof (t0->packet_data));
1269 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1271 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1272 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1273 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1274 t1->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1275 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1276 memcpy (t1->packet_data,
1277 vlib_buffer_get_current (b1),
1278 sizeof (t1->packet_data));
1288 ip6_forward_next_trace_t * t0;
1292 b0 = vlib_get_buffer (vm, bi0);
1294 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1296 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1297 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1298 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1299 t0->fib_index = vec_elt (im->fib_index_by_sw_if_index,
1300 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1301 memcpy (t0->packet_data,
1302 vlib_buffer_get_current (b0),
1303 sizeof (t0->packet_data));
1311 ip6_drop_or_punt (vlib_main_t * vm,
1312 vlib_node_runtime_t * node,
1313 vlib_frame_t * frame,
1314 ip6_error_t error_code)
1316 u32 * buffers = vlib_frame_vector_args (frame);
1317 uword n_packets = frame->n_vectors;
1319 vlib_error_drop_buffers (vm, node,
1324 ip6_input_node.index,
1327 if (node->flags & VLIB_NODE_FLAG_TRACE)
1328 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1334 ip6_drop (vlib_main_t * vm,
1335 vlib_node_runtime_t * node,
1336 vlib_frame_t * frame)
1337 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1340 ip6_punt (vlib_main_t * vm,
1341 vlib_node_runtime_t * node,
1342 vlib_frame_t * frame)
1343 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1346 ip6_miss (vlib_main_t * vm,
1347 vlib_node_runtime_t * node,
1348 vlib_frame_t * frame)
1349 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1351 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1352 .function = ip6_drop,
1354 .vector_size = sizeof (u32),
1356 .format_trace = format_ip6_forward_next_trace,
1364 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1365 .function = ip6_punt,
1367 .vector_size = sizeof (u32),
1369 .format_trace = format_ip6_forward_next_trace,
1377 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1378 .function = ip6_miss,
1380 .vector_size = sizeof (u32),
1382 .format_trace = format_ip6_forward_next_trace,
1390 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1391 .function = ip6_drop,
1392 .name = "ip6-multicast",
1393 .vector_size = sizeof (u32),
1395 .format_trace = format_ip6_forward_next_trace,
1403 /* Compute TCP/UDP/ICMP6 checksum in software. */
1404 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1407 u16 sum16, payload_length_host_byte_order;
1408 u32 i, n_this_buffer, n_bytes_left;
1409 u32 headers_size = sizeof(ip0[0]);
1410 void * data_this_buffer;
1412 ASSERT(bogus_lengthp);
1415 /* Initialize checksum with ip header. */
1416 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1417 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1418 data_this_buffer = (void *) (ip0 + 1);
1420 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1422 sum0 = ip_csum_with_carry (sum0,
1423 clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1424 sum0 = ip_csum_with_carry (sum0,
1425 clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1428 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1429 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1432 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *)data_this_buffer;
1434 /* validate really icmp6 next */
1435 ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1437 skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1438 data_this_buffer = (void *)((u8 *)data_this_buffer + skip_bytes);
1440 payload_length_host_byte_order -= skip_bytes;
1441 headers_size += skip_bytes;
1444 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1448 struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1449 u8 nb_segs = mb->nb_segs;
1451 n_this_buffer = (p0->current_length > headers_size ?
1452 p0->current_length - headers_size : 0);
1453 while (n_bytes_left)
1455 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1456 n_bytes_left -= n_this_buffer;
1460 if ((nb_segs == 0) || (mb == 0))
1463 data_this_buffer = rte_ctrlmbuf_data(mb);
1464 n_this_buffer = mb->data_len;
1466 if (n_bytes_left || nb_segs)
1472 else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1474 if (p0 && n_this_buffer + headers_size > p0->current_length)
1475 n_this_buffer = p0->current_length > headers_size ? p0->current_length - headers_size : 0;
1478 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1479 n_bytes_left -= n_this_buffer;
1480 if (n_bytes_left == 0)
1483 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1488 p0 = vlib_get_buffer (vm, p0->next_buffer);
1489 data_this_buffer = vlib_buffer_get_current (p0);
1490 n_this_buffer = p0->current_length;
1494 sum16 = ~ ip_csum_fold (sum0);
1499 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1501 ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1502 udp_header_t * udp0;
1506 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1507 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1508 || ip0->protocol == IP_PROTOCOL_ICMP6
1509 || ip0->protocol == IP_PROTOCOL_UDP
1510 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1512 udp0 = (void *) (ip0 + 1);
1513 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1515 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1516 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1520 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1522 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1523 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1529 ip6_local (vlib_main_t * vm,
1530 vlib_node_runtime_t * node,
1531 vlib_frame_t * frame)
1533 ip6_main_t * im = &ip6_main;
1534 ip_lookup_main_t * lm = &im->lookup_main;
1535 ip_local_next_t next_index;
1536 u32 * from, * to_next, n_left_from, n_left_to_next;
1537 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1539 from = vlib_frame_vector_args (frame);
1540 n_left_from = frame->n_vectors;
1541 next_index = node->cached_next_index;
1543 if (node->flags & VLIB_NODE_FLAG_TRACE)
1544 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1546 while (n_left_from > 0)
1548 vlib_get_next_frame (vm, node, next_index,
1549 to_next, n_left_to_next);
1551 while (n_left_from >= 4 && n_left_to_next >= 2)
1553 vlib_buffer_t * p0, * p1;
1554 ip6_header_t * ip0, * ip1;
1555 udp_header_t * udp0, * udp1;
1556 u32 pi0, ip_len0, udp_len0, flags0, next0;
1557 u32 pi1, ip_len1, udp_len1, flags1, next1;
1558 i32 len_diff0, len_diff1;
1559 u8 error0, type0, good_l4_checksum0;
1560 u8 error1, type1, good_l4_checksum1;
1562 pi0 = to_next[0] = from[0];
1563 pi1 = to_next[1] = from[1];
1567 n_left_to_next -= 2;
1569 p0 = vlib_get_buffer (vm, pi0);
1570 p1 = vlib_get_buffer (vm, pi1);
1572 ip0 = vlib_buffer_get_current (p0);
1573 ip1 = vlib_buffer_get_current (p1);
1575 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1576 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1578 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1579 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1584 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1585 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1587 udp0 = ip6_next_header (ip0);
1588 udp1 = ip6_next_header (ip1);
1590 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1591 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1592 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1594 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1595 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1597 /* Verify UDP length. */
1598 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1599 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1600 udp_len0 = clib_net_to_host_u16 (udp0->length);
1601 udp_len1 = clib_net_to_host_u16 (udp1->length);
1603 len_diff0 = ip_len0 - udp_len0;
1604 len_diff1 = ip_len1 - udp_len1;
1606 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1607 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1609 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1610 && ! good_l4_checksum0
1611 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1613 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1615 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1617 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1618 && ! good_l4_checksum1
1619 && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1621 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1623 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1626 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1628 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1629 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1631 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1632 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1633 error0 = (! good_l4_checksum0
1634 ? IP6_ERROR_UDP_CHECKSUM + type0
1636 error1 = (! good_l4_checksum1
1637 ? IP6_ERROR_UDP_CHECKSUM + type1
1640 /* Drop packets from unroutable hosts. */
1641 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1642 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1644 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1645 error0 = (lm->miss_adj_index == src_adj_index0
1646 ? IP6_ERROR_SRC_LOOKUP_MISS
1649 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1651 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1652 error1 = (lm->miss_adj_index == src_adj_index1
1653 ? IP6_ERROR_SRC_LOOKUP_MISS
1657 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1658 next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1660 p0->error = error_node->errors[error0];
1661 p1->error = error_node->errors[error1];
1663 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1664 to_next, n_left_to_next,
1665 pi0, pi1, next0, next1);
1668 while (n_left_from > 0 && n_left_to_next > 0)
1672 udp_header_t * udp0;
1673 u32 pi0, ip_len0, udp_len0, flags0, next0;
1675 u8 error0, type0, good_l4_checksum0;
1677 pi0 = to_next[0] = from[0];
1681 n_left_to_next -= 1;
1683 p0 = vlib_get_buffer (vm, pi0);
1685 ip0 = vlib_buffer_get_current (p0);
1687 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1688 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1692 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1694 udp0 = ip6_next_header (ip0);
1696 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1697 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1699 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1701 /* Verify UDP length. */
1702 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1703 udp_len0 = clib_net_to_host_u16 (udp0->length);
1705 len_diff0 = ip_len0 - udp_len0;
1707 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1709 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1710 && ! good_l4_checksum0
1711 && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1713 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1715 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1718 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1720 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1722 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1723 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1724 error0 = (! good_l4_checksum0
1725 ? IP6_ERROR_UDP_CHECKSUM + type0
1728 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1729 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1731 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1732 error0 = (lm->miss_adj_index == src_adj_index0
1733 ? IP6_ERROR_SRC_LOOKUP_MISS
1737 next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1739 p0->error = error_node->errors[error0];
1741 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1742 to_next, n_left_to_next,
1746 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1749 return frame->n_vectors;
1752 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1753 .function = ip6_local,
1754 .name = "ip6-local",
1755 .vector_size = sizeof (u32),
1757 .format_trace = format_ip6_forward_next_trace,
1759 .n_next_nodes = IP_LOCAL_N_NEXT,
1761 [IP_LOCAL_NEXT_DROP] = "error-drop",
1762 [IP_LOCAL_NEXT_PUNT] = "error-punt",
1763 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1764 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1768 void ip6_register_protocol (u32 protocol, u32 node_index)
1770 vlib_main_t * vm = vlib_get_main();
1771 ip6_main_t * im = &ip6_main;
1772 ip_lookup_main_t * lm = &im->lookup_main;
1774 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1775 lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1779 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1780 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1781 } ip6_discover_neighbor_next_t;
1784 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1785 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1786 } ip6_discover_neighbor_error_t;
1789 ip6_discover_neighbor (vlib_main_t * vm,
1790 vlib_node_runtime_t * node,
1791 vlib_frame_t * frame)
1793 vnet_main_t * vnm = vnet_get_main();
1794 ip6_main_t * im = &ip6_main;
1795 ip_lookup_main_t * lm = &im->lookup_main;
1796 u32 * from, * to_next_drop;
1797 uword n_left_from, n_left_to_next_drop;
1798 static f64 time_last_seed_change = -1e100;
1799 static u32 hash_seeds[3];
1800 static uword hash_bitmap[256 / BITS (uword)];
1804 if (node->flags & VLIB_NODE_FLAG_TRACE)
1805 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1807 time_now = vlib_time_now (vm);
1808 if (time_now - time_last_seed_change > 1e-3)
1811 u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1812 sizeof (hash_seeds));
1813 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1814 hash_seeds[i] = r[i];
1816 /* Mark all hash keys as been not-seen before. */
1817 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1820 time_last_seed_change = time_now;
1823 from = vlib_frame_vector_args (frame);
1824 n_left_from = frame->n_vectors;
1826 while (n_left_from > 0)
1828 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1829 to_next_drop, n_left_to_next_drop);
1831 while (n_left_from > 0 && n_left_to_next_drop > 0)
1835 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1837 ip_adjacency_t * adj0;
1838 vnet_hw_interface_t * hw_if0;
1843 p0 = vlib_get_buffer (vm, pi0);
1845 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1847 ip0 = vlib_buffer_get_current (p0);
1849 adj0 = ip_get_adjacency (lm, adj_index0);
1855 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1856 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1859 b0 ^= ip0->dst_address.as_u32[0];
1860 c0 ^= ip0->dst_address.as_u32[1];
1862 hash_v3_mix32 (a0, b0, c0);
1864 b0 ^= ip0->dst_address.as_u32[2];
1865 c0 ^= ip0->dst_address.as_u32[3];
1867 hash_v3_finalize32 (a0, b0, c0);
1869 c0 &= BITS (hash_bitmap) - 1;
1870 c0 = c0 / BITS (uword);
1871 m0 = (uword) 1 << (c0 % BITS (uword));
1873 bm0 = hash_bitmap[c0];
1874 drop0 = (bm0 & m0) != 0;
1876 /* Mark it as seen. */
1877 hash_bitmap[c0] = bm0 | m0;
1881 to_next_drop[0] = pi0;
1883 n_left_to_next_drop -= 1;
1885 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1887 /* If the interface is link-down, drop the pkt */
1888 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1892 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1893 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1899 icmp6_neighbor_solicitation_header_t * h0;
1902 h0 = vlib_packet_template_get_packet
1903 (vm, &im->discover_neighbor_packet_template, &bi0);
1906 * Build ethernet header.
1907 * Choose source address based on destination lookup
1910 ip6_src_address_for_packet (im, p0, &h0->ip.src_address,
1914 * Destination address is a solicited node multicast address.
1915 * We need to fill in
1916 * the low 24 bits with low 24 bits of target's address.
1918 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1919 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1920 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1922 h0->neighbor.target_address = ip0->dst_address;
1924 memcpy (h0->link_layer_option.ethernet_address,
1925 hw_if0->hw_address, vec_len (hw_if0->hw_address));
1927 /* $$$$ appears we need this; why is the checksum non-zero? */
1928 h0->neighbor.icmp.checksum = 0;
1929 h0->neighbor.icmp.checksum =
1930 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1933 ASSERT (bogus_length == 0);
1935 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1936 b0 = vlib_get_buffer (vm, bi0);
1937 vnet_buffer (b0)->sw_if_index[VLIB_TX]
1938 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1940 /* Add rewrite/encap string. */
1941 vnet_rewrite_one_header (adj0[0], h0,
1942 sizeof (ethernet_header_t));
1943 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1945 /* $$$$ hack in case next0 == 0 */
1946 b0->error = node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_DROP];
1948 vec_elt (im->discover_neighbor_next_index_by_hw_if_index,
1949 hw_if0->hw_if_index);
1951 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1955 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1956 n_left_to_next_drop);
1959 return frame->n_vectors;
1962 static char * ip6_discover_neighbor_error_strings[] = {
1963 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1964 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT]
1965 = "neighbor solicitations sent",
1968 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
1969 .function = ip6_discover_neighbor,
1970 .name = "ip6-discover-neighbor",
1971 .vector_size = sizeof (u32),
1973 .format_trace = format_ip6_forward_next_trace,
1975 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1976 .error_strings = ip6_discover_neighbor_error_strings,
1978 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1980 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1985 ip6_discover_neighbor_hw_interface_link_up_down (vnet_main_t * vnm,
1989 vlib_main_t * vm = vnm->vlib_main;
1990 ip6_main_t * im = &ip6_main;
1991 vnet_hw_interface_t * hw_if;
1993 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1995 vec_validate_init_empty
1996 (im->discover_neighbor_next_index_by_hw_if_index, hw_if_index, 0);
1997 im->discover_neighbor_next_index_by_hw_if_index[hw_if_index]
1998 = vlib_node_add_next (vm, ip6_discover_neighbor_node.index,
1999 hw_if->output_node_index);
2003 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION
2004 (ip6_discover_neighbor_hw_interface_link_up_down);
2007 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2009 vnet_main_t * vnm = vnet_get_main();
2010 ip6_main_t * im = &ip6_main;
2011 icmp6_neighbor_solicitation_header_t * h;
2012 ip6_address_t * src;
2013 ip_interface_address_t * ia;
2014 ip_adjacency_t * adj;
2015 vnet_hw_interface_t * hi;
2016 vnet_sw_interface_t * si;
2021 si = vnet_get_sw_interface (vnm, sw_if_index);
2023 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2025 return clib_error_return (0, "%U: interface %U down",
2026 format_ip6_address, dst,
2027 format_vnet_sw_if_index_name, vnm,
2031 src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2034 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2035 return clib_error_return
2036 (0, "no matching interface address for destination %U (interface %U)",
2037 format_ip6_address, dst,
2038 format_vnet_sw_if_index_name, vnm, sw_if_index);
2041 h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2043 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2045 /* Destination address is a solicited node multicast address. We need to fill in
2046 the low 24 bits with low 24 bits of target's address. */
2047 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2048 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2049 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2051 h->ip.src_address = src[0];
2052 h->neighbor.target_address = dst[0];
2054 memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2056 h->neighbor.icmp.checksum =
2057 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2058 ASSERT(bogus_length == 0);
2060 b = vlib_get_buffer (vm, bi);
2061 vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2063 /* Add encapsulation string for software interface (e.g. ethernet header). */
2064 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2065 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2066 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2069 vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2070 u32 * to_next = vlib_frame_vector_args (f);
2073 vlib_put_frame_to_node (vm, hi->output_node_index, f);
2076 return /* no error */ 0;
2080 IP6_REWRITE_NEXT_DROP,
2081 } ip6_rewrite_next_t;
2084 ip6_rewrite_inline (vlib_main_t * vm,
2085 vlib_node_runtime_t * node,
2086 vlib_frame_t * frame,
2087 int rewrite_for_locally_received_packets)
2089 ip_lookup_main_t * lm = &ip6_main.lookup_main;
2090 u32 * from = vlib_frame_vector_args (frame);
2091 u32 n_left_from, n_left_to_next, * to_next, next_index;
2092 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2093 vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2095 n_left_from = frame->n_vectors;
2096 next_index = node->cached_next_index;
2097 u32 cpu_index = os_get_cpu_number();
2099 while (n_left_from > 0)
2101 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2103 while (n_left_from >= 4 && n_left_to_next >= 2)
2105 ip_adjacency_t * adj0, * adj1;
2106 vlib_buffer_t * p0, * p1;
2107 ip6_header_t * ip0, * ip1;
2108 u32 pi0, rw_len0, next0, error0, adj_index0;
2109 u32 pi1, rw_len1, next1, error1, adj_index1;
2111 /* Prefetch next iteration. */
2113 vlib_buffer_t * p2, * p3;
2115 p2 = vlib_get_buffer (vm, from[2]);
2116 p3 = vlib_get_buffer (vm, from[3]);
2118 vlib_prefetch_buffer_header (p2, LOAD);
2119 vlib_prefetch_buffer_header (p3, LOAD);
2121 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2122 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2124 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2125 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2128 pi0 = to_next[0] = from[0];
2129 pi1 = to_next[1] = from[1];
2134 n_left_to_next -= 2;
2136 p0 = vlib_get_buffer (vm, pi0);
2137 p1 = vlib_get_buffer (vm, pi1);
2139 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2140 adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2142 /* We should never rewrite a pkt using the MISS adjacency */
2143 ASSERT(adj_index0 && adj_index1);
2145 ip0 = vlib_buffer_get_current (p0);
2146 ip1 = vlib_buffer_get_current (p1);
2148 error0 = error1 = IP6_ERROR_NONE;
2150 if (! rewrite_for_locally_received_packets)
2152 i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2154 /* Input node should have reject packets with hop limit 0. */
2155 ASSERT (ip0->hop_limit > 0);
2156 ASSERT (ip1->hop_limit > 0);
2161 ip0->hop_limit = hop_limit0;
2162 ip1->hop_limit = hop_limit1;
2164 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2165 error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2168 adj0 = ip_get_adjacency (lm, adj_index0);
2169 adj1 = ip_get_adjacency (lm, adj_index1);
2171 if (rewrite_for_locally_received_packets)
2174 * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2175 * we end up here with a local adjacency in hand
2177 if (PREDICT_FALSE(adj0->lookup_next_index
2178 == IP_LOOKUP_NEXT_LOCAL))
2179 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2180 if (PREDICT_FALSE(adj1->lookup_next_index
2181 == IP_LOOKUP_NEXT_LOCAL))
2182 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2185 rw_len0 = adj0[0].rewrite_header.data_bytes;
2186 rw_len1 = adj1[0].rewrite_header.data_bytes;
2188 vlib_increment_combined_counter (&lm->adjacency_counters,
2191 /* packet increment */ 0,
2192 /* byte increment */ rw_len0);
2193 vlib_increment_combined_counter (&lm->adjacency_counters,
2196 /* packet increment */ 0,
2197 /* byte increment */ rw_len1);
2199 /* Check MTU of outgoing interface. */
2200 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2201 ? IP6_ERROR_MTU_EXCEEDED
2203 error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2204 ? IP6_ERROR_MTU_EXCEEDED
2207 p0->current_data -= rw_len0;
2208 p1->current_data -= rw_len1;
2210 p0->current_length += rw_len0;
2211 p1->current_length += rw_len1;
2213 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2214 vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2216 next0 = (error0 == IP6_ERROR_NONE) ?
2217 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2218 next1 = (error1 == IP6_ERROR_NONE) ?
2219 adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2221 /* Guess we are only writing on simple Ethernet header. */
2222 vnet_rewrite_two_headers (adj0[0], adj1[0],
2224 sizeof (ethernet_header_t));
2226 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2227 to_next, n_left_to_next,
2228 pi0, pi1, next0, next1);
2231 while (n_left_from > 0 && n_left_to_next > 0)
2233 ip_adjacency_t * adj0;
2237 u32 adj_index0, next0, error0;
2239 pi0 = to_next[0] = from[0];
2241 p0 = vlib_get_buffer (vm, pi0);
2243 adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2245 /* We should never rewrite a pkt using the MISS adjacency */
2248 adj0 = ip_get_adjacency (lm, adj_index0);
2250 ip0 = vlib_buffer_get_current (p0);
2252 error0 = IP6_ERROR_NONE;
2254 /* Check hop limit */
2255 if (! rewrite_for_locally_received_packets)
2257 i32 hop_limit0 = ip0->hop_limit;
2259 ASSERT (ip0->hop_limit > 0);
2263 ip0->hop_limit = hop_limit0;
2265 error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2268 if (rewrite_for_locally_received_packets)
2270 if (PREDICT_FALSE(adj0->lookup_next_index
2271 == IP_LOOKUP_NEXT_LOCAL))
2272 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2275 /* Guess we are only writing on simple Ethernet header. */
2276 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2278 /* Update packet buffer attributes/set output interface. */
2279 rw_len0 = adj0[0].rewrite_header.data_bytes;
2281 vlib_increment_combined_counter (&lm->adjacency_counters,
2284 /* packet increment */ 0,
2285 /* byte increment */ rw_len0);
2287 /* Check MTU of outgoing interface. */
2288 error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2289 ? IP6_ERROR_MTU_EXCEEDED
2292 p0->current_data -= rw_len0;
2293 p0->current_length += rw_len0;
2294 vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2296 next0 = (error0 == IP6_ERROR_NONE) ?
2297 adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2299 p0->error = error_node->errors[error0];
2304 n_left_to_next -= 1;
2306 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2307 to_next, n_left_to_next,
2311 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2314 /* Need to do trace after rewrites to pick up new packet data. */
2315 if (node->flags & VLIB_NODE_FLAG_TRACE)
2316 ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2318 return frame->n_vectors;
2322 ip6_rewrite_transit (vlib_main_t * vm,
2323 vlib_node_runtime_t * node,
2324 vlib_frame_t * frame)
2326 return ip6_rewrite_inline (vm, node, frame,
2327 /* rewrite_for_locally_received_packets */ 0);
2331 ip6_rewrite_local (vlib_main_t * vm,
2332 vlib_node_runtime_t * node,
2333 vlib_frame_t * frame)
2335 return ip6_rewrite_inline (vm, node, frame,
2336 /* rewrite_for_locally_received_packets */ 1);
2339 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2340 .function = ip6_rewrite_transit,
2341 .name = "ip6-rewrite",
2342 .vector_size = sizeof (u32),
2344 .format_trace = format_ip6_forward_next_trace,
2348 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2352 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2353 .function = ip6_rewrite_local,
2354 .name = "ip6-rewrite-local",
2355 .vector_size = sizeof (u32),
2357 .sibling_of = "ip6-rewrite",
2359 .format_trace = format_ip6_forward_next_trace,
2363 [IP6_REWRITE_NEXT_DROP] = "error-drop",
2367 /* Global IP6 main. */
2368 ip6_main_t ip6_main;
2370 static clib_error_t *
2371 ip6_lookup_init (vlib_main_t * vm)
2373 ip6_main_t * im = &ip6_main;
2376 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2383 for (j = 0; j < i0; j++)
2384 im->fib_masks[i].as_u32[j] = ~0;
2387 im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2390 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2392 if (im->lookup_table_nbuckets == 0)
2393 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2395 im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2397 if (im->lookup_table_size == 0)
2398 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2400 BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2401 im->lookup_table_nbuckets,
2402 im->lookup_table_size);
2404 /* Create FIB with index 0 and table id of 0. */
2405 find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2409 pn = pg_get_node (ip6_lookup_node.index);
2410 pn->unformat_edit = unformat_pg_ip6_header;
2414 icmp6_neighbor_solicitation_header_t p;
2416 memset (&p, 0, sizeof (p));
2418 p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2419 p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2420 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2421 p.ip.protocol = IP_PROTOCOL_ICMP6;
2422 p.ip.hop_limit = 255;
2423 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2425 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2427 p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2428 p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2430 vlib_packet_template_init (vm,
2431 &im->discover_neighbor_packet_template,
2433 /* alloc chunk size */ 8,
2434 "ip6 neighbor discovery");
2440 VLIB_INIT_FUNCTION (ip6_lookup_init);
2442 static clib_error_t *
2443 add_del_ip6_interface_table (vlib_main_t * vm,
2444 unformat_input_t * input,
2445 vlib_cli_command_t * cmd)
2447 vnet_main_t * vnm = vnet_get_main();
2448 clib_error_t * error = 0;
2449 u32 sw_if_index, table_id;
2453 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2455 error = clib_error_return (0, "unknown interface `%U'",
2456 format_unformat_error, input);
2460 if (unformat (input, "%d", &table_id))
2464 error = clib_error_return (0, "expected table id `%U'",
2465 format_unformat_error, input);
2470 ip6_main_t * im = &ip6_main;
2472 find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2476 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2477 im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2485 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2486 .path = "set interface ip6 table",
2487 .function = add_del_ip6_interface_table,
2488 .short_help = "set interface ip6 table <intfc> <table-id>"
2492 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2495 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2496 /* Invert the "u" bit */
2497 ip->as_u8 [8] = mac[0] ^ (1<<1);
2498 ip->as_u8 [9] = mac[1];
2499 ip->as_u8 [10] = mac[2];
2500 ip->as_u8 [11] = 0xFF;
2501 ip->as_u8 [12] = 0xFE;
2502 ip->as_u8 [13] = mac[3];
2503 ip->as_u8 [14] = mac[4];
2504 ip->as_u8 [15] = mac[5];
2508 ip6_ethernet_mac_address_from_link_local_address (u8 *mac,
2511 /* Invert the previously inverted "u" bit */
2512 mac[0] = ip->as_u8 [8] ^ (1<<1);
2513 mac[1] = ip->as_u8 [9];
2514 mac[2] = ip->as_u8 [10];
2515 mac[3] = ip->as_u8 [13];
2516 mac[4] = ip->as_u8 [14];
2517 mac[5] = ip->as_u8 [15];
2520 static clib_error_t *
2521 test_ip6_link_command_fn (vlib_main_t * vm,
2522 unformat_input_t * input,
2523 vlib_cli_command_t * cmd)
2526 ip6_address_t _a, *a = &_a;
2528 if (unformat (input, "%U", unformat_ethernet_address, mac))
2530 ip6_link_local_address_from_ethernet_mac_address (a, mac);
2531 vlib_cli_output (vm, "Link local address: %U",
2532 format_ip6_address, a);
2533 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2534 vlib_cli_output (vm, "Original MAC address: %U",
2535 format_ethernet_address, mac);
2541 VLIB_CLI_COMMAND (test_link_command, static) = {
2542 .path = "test ip6 link",
2543 .function = test_ip6_link_command_fn,
2544 .short_help = "test ip6 link <mac-address>",
2547 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2549 ip6_main_t * im6 = &ip6_main;
2551 uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2556 fib = vec_elt_at_index (im6->fibs, p[0]);
2558 fib->flow_hash_config = flow_hash_config;
2562 static clib_error_t *
2563 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2564 unformat_input_t * input,
2565 vlib_cli_command_t * cmd)
2569 u32 flow_hash_config = 0;
2572 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2573 if (unformat (input, "table %d", &table_id))
2576 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2577 foreach_flow_hash_bit
2583 return clib_error_return (0, "unknown input `%U'",
2584 format_unformat_error, input);
2586 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2593 return clib_error_return (0, "no such FIB table %d", table_id);
2596 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2603 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2604 .path = "set ip6 flow-hash",
2606 "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2607 .function = set_ip6_flow_hash_command_fn,
2610 static clib_error_t *
2611 show_ip6_local_command_fn (vlib_main_t * vm,
2612 unformat_input_t * input,
2613 vlib_cli_command_t * cmd)
2615 ip6_main_t * im = &ip6_main;
2616 ip_lookup_main_t * lm = &im->lookup_main;
2619 vlib_cli_output (vm, "Protocols handled by ip6_local");
2620 for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2622 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2623 vlib_cli_output (vm, "%d", i);
2630 VLIB_CLI_COMMAND (show_ip_local, static) = {
2631 .path = "show ip6 local",
2632 .function = show_ip6_local_command_fn,
2633 .short_help = "Show ip6 local protocol table",
2636 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2639 vnet_main_t * vnm = vnet_get_main();
2640 vnet_interface_main_t * im = &vnm->interface_main;
2641 ip6_main_t * ipm = &ip6_main;
2642 ip_lookup_main_t * lm = &ipm->lookup_main;
2643 vnet_classify_main_t * cm = &vnet_classify_main;
2645 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2646 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2648 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2649 return VNET_API_ERROR_NO_SUCH_ENTRY;
2651 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2652 lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2657 static clib_error_t *
2658 set_ip6_classify_command_fn (vlib_main_t * vm,
2659 unformat_input_t * input,
2660 vlib_cli_command_t * cmd)
2662 u32 table_index = ~0;
2663 int table_index_set = 0;
2664 u32 sw_if_index = ~0;
2667 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2668 if (unformat (input, "table-index %d", &table_index))
2669 table_index_set = 1;
2670 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2671 vnet_get_main(), &sw_if_index))
2677 if (table_index_set == 0)
2678 return clib_error_return (0, "classify table-index must be specified");
2680 if (sw_if_index == ~0)
2681 return clib_error_return (0, "interface / subif must be specified");
2683 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2690 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2691 return clib_error_return (0, "No such interface");
2693 case VNET_API_ERROR_NO_SUCH_ENTRY:
2694 return clib_error_return (0, "No such classifier table");
2699 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2700 .path = "set ip6 classify",
2702 "set ip6 classify intfc <int> table-index <index>",
2703 .function = set_ip6_classify_command_fn,
2706 static clib_error_t *
2707 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2709 ip6_main_t * im = &ip6_main;
2714 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2715 if (unformat (input, "hash-buckets %d", &tmp))
2717 else if (unformat (input, "heap-size %dm", &tmp))
2718 heapsize = ((u64)tmp) << 20;
2719 else if (unformat (input, "heap-size %dM", &tmp))
2720 heapsize = ((u64)tmp) << 20;
2721 else if (unformat (input, "heap-size %dg", &tmp))
2722 heapsize = ((u64)tmp) << 30;
2723 else if (unformat (input, "heap-size %dG", &tmp))
2724 heapsize = ((u64)tmp) << 30;
2726 return clib_error_return (0, "unknown input '%U'",
2727 format_unformat_error, input);
2730 im->lookup_table_nbuckets = nbuckets;
2731 im->lookup_table_size = heapsize;
2736 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");