2 * ip/ip6_neighbor.c: IP6 neighbor handling
4 * Copyright (c) 2010 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/ip/ip.h>
19 #include <vnet/ip/ip6_neighbor.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vppinfra/mhash.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/adj/adj_mcast.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/fib/ip6_fib.h>
26 #include <vnet/mfib/ip6_mfib.h>
27 #include <vnet/ip/ip6_ll_table.h>
28 #include <vnet/l2/l2_input.h>
32 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
34 * The files contains the API and CLI code for managing IPv6 neighbor
35 * adjacency tables and neighbor discovery logic.
38 /* can't use sizeof link_layer_address, that's 8 */
39 #define ETHER_MAC_ADDR_LEN 6
41 /* advertised prefix option */
44 /* basic advertised information */
48 int adv_autonomous_flag;
49 u32 adv_valid_lifetime_in_secs;
50 u32 adv_pref_lifetime_in_secs;
52 /* advertised values are computed from these times if decrementing */
53 f64 valid_lifetime_expires;
54 f64 pref_lifetime_expires;
56 /* local information */
58 int deprecated_prefix_flag;
59 int decrement_lifetime_flag;
61 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
62 #define DEF_ADV_VALID_LIFETIME 2592000
63 #define DEF_ADV_PREF_LIFETIME 604800
65 /* extensions are added here, mobile, DNS etc.. */
71 /* group information */
73 ip6_address_t mcast_address;
75 ip6_address_t *mcast_source_address_pool;
78 /* configured router advertisement information per ipv6 interface */
82 /* advertised config information, zero means unspecified */
86 u16 adv_router_lifetime_in_sec;
87 u32 adv_neighbor_reachable_time_in_msec;
88 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
93 /* source link layer option */
94 u8 link_layer_address[8];
95 u8 link_layer_addr_len;
98 ip6_radv_prefix_t *adv_prefixes_pool;
100 /* Hash table mapping address to index in interface advertised prefix pool. */
101 mhash_t address_to_prefix_index;
103 /* MLDP group information */
104 ip6_mldp_group_t *mldp_group_pool;
106 /* Hash table mapping address to index in mldp address pool. */
107 mhash_t address_to_mldp_index;
109 /* local information */
111 int send_radv; /* radv on/off on this interface - set by config */
112 int cease_radv; /* we are ceasing to send - set byf config */
114 int adv_link_layer_address;
116 int failed_device_check;
117 int all_routers_mcast;
121 adj_index_t mcast_adj_index;
123 /* timing information */
124 #define DEF_MAX_RADV_INTERVAL 200
125 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
126 #define DEF_CURR_HOP_LIMIT 64
127 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
128 #define MAX_DEF_RTR_LIFETIME 9000
130 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
131 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
132 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
133 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
134 #define MAX_RA_DELAY_TIME .5 /* seconds */
136 f64 max_radv_interval;
137 f64 min_radv_interval;
138 f64 min_delay_between_radv;
139 f64 max_delay_between_radv;
140 f64 max_rtr_default_lifetime;
143 f64 last_multicast_time;
144 f64 next_multicast_time;
147 u32 initial_adverts_count;
148 f64 initial_adverts_interval;
149 u32 initial_adverts_sent;
152 u32 n_advertisements_sent;
153 u32 n_solicitations_rcvd;
154 u32 n_solicitations_dropped;
156 /* Link local address to use (defaults to underlying physical for logical interfaces */
157 ip6_address_t link_local_address;
159 /* router solicitations sending state */
160 u8 keep_sending_rs; /* when true then next fields are valid */
161 icmp6_send_router_solicitation_params_t params;
166 vlib_buffer_t *buffer;
175 /* Used for nd event notification only */
178 } pending_resolution_t;
183 /* Hash tables mapping name to opcode. */
184 uword *opcode_by_name;
186 /* lite beer "glean" adjacency handling */
187 mhash_t pending_resolutions_by_address;
188 pending_resolution_t *pending_resolutions;
190 /* Mac address change notification */
191 mhash_t mac_changes_by_address;
192 pending_resolution_t *mac_changes;
194 u32 *neighbor_input_next_index_by_hw_if_index;
196 ip6_neighbor_t *neighbor_pool;
198 mhash_t neighbor_index_by_key;
200 u32 *if_radv_pool_index_by_sw_if_index;
202 ip6_radv_t *if_radv_pool;
204 /* Neighbor attack mitigation */
205 u32 limit_neighbor_cache_size;
206 u32 neighbor_delete_rotor;
208 /* Wildcard nd report publisher */
209 uword wc_ip6_nd_publisher_node;
210 uword wc_ip6_nd_publisher_et;
212 /* Router advertisement report publisher */
213 uword ip6_ra_publisher_node;
214 uword ip6_ra_publisher_et;
215 } ip6_neighbor_main_t;
217 /* ipv6 neighbor discovery - timer/event types */
221 } ip6_icmp_neighbor_discovery_event_type_t;
231 } ip6_icmp_neighbor_discovery_event_data_t;
233 static ip6_neighbor_main_t ip6_neighbor_main;
234 ip6_neighbor_public_main_t ip6_neighbor_public_main;
235 static ip6_address_t ip6a_zero; /* ip6 address 0 */
237 static void wc_nd_signal_report (wc_nd_report_t * r);
238 static void ra_signal_report (ra_report_t * r);
241 ip6_neighbor_get_link_local_address (u32 sw_if_index)
243 static ip6_address_t empty_address = { {0} };
244 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
245 ip6_radv_t *radv_info;
248 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index)
249 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
252 clib_warning ("IPv6 is not enabled for sw_if_index %d", sw_if_index);
253 return empty_address;
255 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
256 if (radv_info == NULL)
258 clib_warning ("Internal error");
259 return empty_address;
261 return radv_info->link_local_address;
265 * @brief publish wildcard arp event
266 * @param sw_if_index The interface on which the ARP entires are acted
269 vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
272 .sw_if_index = sw_if_index,
275 memcpy (r.mac, mac, sizeof r.mac);
277 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
278 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
283 wc_nd_signal_report (wc_nd_report_t * r)
285 vlib_main_t *vm = vlib_get_main ();
286 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
287 uword ni = nm->wc_ip6_nd_publisher_node;
288 uword et = nm->wc_ip6_nd_publisher_et;
290 if (ni == (uword) ~ 0)
293 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
299 wc_nd_set_publisher_node (uword node_index, uword event_type)
301 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
302 nm->wc_ip6_nd_publisher_node = node_index;
303 nm->wc_ip6_nd_publisher_et = event_type;
307 ra_publish (ra_report_t * r)
309 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
310 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
315 ra_signal_report (ra_report_t * r)
317 vlib_main_t *vm = vlib_get_main ();
318 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
319 uword ni = nm->ip6_ra_publisher_node;
320 uword et = nm->ip6_ra_publisher_et;
322 if (ni == (uword) ~ 0)
324 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
330 ra_set_publisher_node (uword node_index, uword event_type)
332 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
333 nm->ip6_ra_publisher_node = node_index;
334 nm->ip6_ra_publisher_et = event_type;
338 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
340 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
341 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
342 vnet_main_t *vnm = vnet_get_main ();
343 vnet_sw_interface_t *si;
347 return format (s, "%=12s%=45s%=6s%=20s%=40s", "Time", "Address", "Flags",
348 "Link layer", "Interface");
350 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
351 flags = format (flags, "D");
353 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
354 flags = format (flags, "S");
356 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
357 flags = format (flags, "N");
359 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
360 s = format (s, "%=12U%=45U%=6s%=20U%=40U",
361 format_vlib_time, vm, n->time_last_updated,
362 format_ip6_address, &n->key.ip6_address,
363 flags ? (char *) flags : "",
364 format_ethernet_address, n->link_layer_address,
365 format_vnet_sw_interface_name, vnm, si);
372 ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
374 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
376 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
378 ip6_ll_prefix_t pfx = {
379 .ilp_addr = n->key.ip6_address,
380 .ilp_sw_if_index = n->key.sw_if_index,
382 ip6_ll_table_entry_delete (&pfx);
388 .fp_proto = FIB_PROTOCOL_IP6,
389 .fp_addr.ip6 = n->key.ip6_address,
391 fib_table_entry_path_remove (fib_index,
396 n->key.sw_if_index, ~0,
397 1, FIB_ROUTE_PATH_FLAG_NONE);
407 u8 link_layer_address[6];
410 } ip6_neighbor_set_unset_rpc_args_t;
412 static void ip6_neighbor_set_unset_rpc_callback
413 (ip6_neighbor_set_unset_rpc_args_t * a);
415 static void set_unset_ip6_neighbor_rpc
418 const ip6_address_t * a,
419 const u8 * link_layer_address,
420 int is_add, int is_static, int is_no_fib_entry)
422 ip6_neighbor_set_unset_rpc_args_t args;
423 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
425 args.sw_if_index = sw_if_index;
426 args.is_add = is_add;
427 args.is_static = is_static;
428 args.is_no_fib_entry = is_no_fib_entry;
429 clib_memcpy (&args.addr, a, sizeof (*a));
430 if (NULL != link_layer_address)
431 clib_memcpy (args.link_layer_address, link_layer_address, 6);
433 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
434 (u8 *) & args, sizeof (args));
438 ip6_nbr_probe (ip_adjacency_t * adj)
440 icmp6_neighbor_solicitation_header_t *h;
441 vnet_main_t *vnm = vnet_get_main ();
442 ip6_main_t *im = &ip6_main;
443 ip_interface_address_t *ia;
444 ip6_address_t *dst, *src;
445 vnet_hw_interface_t *hi;
446 vnet_sw_interface_t *si;
452 vm = vlib_get_main ();
454 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
455 dst = &adj->sub_type.nbr.next_hop.ip6;
457 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
461 src = ip6_interface_address_matching_destination (im, dst,
469 h = vlib_packet_template_get_packet (vm,
470 &im->discover_neighbor_packet_template,
475 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
477 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
478 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
479 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
480 h->ip.src_address = src[0];
481 h->neighbor.target_address = dst[0];
483 clib_memcpy (h->link_layer_option.ethernet_address,
484 hi->hw_address, vec_len (hi->hw_address));
486 h->neighbor.icmp.checksum =
487 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
488 ASSERT (bogus_length == 0);
490 b = vlib_get_buffer (vm, bi);
491 vnet_buffer (b)->sw_if_index[VLIB_RX] =
492 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
494 /* Add encapsulation string for software interface (e.g. ethernet header). */
495 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
496 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
499 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
500 u32 *to_next = vlib_frame_vector_args (f);
503 vlib_put_frame_to_node (vm, hi->output_node_index, f);
508 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
510 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
511 ethernet_build_rewrite (vnet_get_main (),
512 nbr->key.sw_if_index,
513 adj_get_link_type (ai),
514 nbr->link_layer_address));
518 ip6_nd_mk_incomplete (adj_index_t ai)
520 ip_adjacency_t *adj = adj_get (ai);
522 adj_nbr_update_rewrite (ai,
523 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
524 ethernet_build_rewrite (vnet_get_main (),
527 adj_get_link_type (ai),
528 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
531 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
533 k.sw_if_index = sw_if_index; \
534 k.ip6_address = *addr; \
538 static ip6_neighbor_t *
539 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
541 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
542 ip6_neighbor_t *n = NULL;
543 ip6_neighbor_key_t k;
546 IP6_NBR_MK_KEY (k, sw_if_index, addr);
548 p = mhash_get (&nm->neighbor_index_by_key, &k);
551 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
558 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
560 ip6_neighbor_t *nbr = ctx;
562 ip6_nd_mk_complete (ai, nbr);
564 return (ADJ_WALK_RC_CONTINUE);
568 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
570 ip6_nd_mk_incomplete (ai);
572 return (ADJ_WALK_RC_CONTINUE);
575 static clib_error_t *
576 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
577 u32 sw_if_index, u32 flags)
579 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
581 u32 i, *to_delete = 0;
584 pool_foreach (n, nm->neighbor_pool,
586 if (n->key.sw_if_index == sw_if_index)
587 vec_add1 (to_delete, n - nm->neighbor_pool);
591 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
593 for (i = 0; i < vec_len (to_delete); i++)
595 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
596 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
597 ip6_nd_mk_complete_walk, n);
602 for (i = 0; i < vec_len (to_delete); i++)
604 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
605 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
606 ip6_nd_mk_incomplete_walk, NULL);
607 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
609 ip6_neighbor_adj_fib_remove (n,
610 ip6_fib_table_get_index_for_sw_if_index
611 (n->key.sw_if_index));
612 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
613 pool_put (nm->neighbor_pool, n);
617 vec_free (to_delete);
621 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
624 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
631 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
633 switch (adj->lookup_next_index)
635 case IP_LOOKUP_NEXT_GLEAN:
636 adj_glean_update_rewrite (ai);
638 case IP_LOOKUP_NEXT_ARP:
641 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
642 ip6_nd_mk_complete_walk, nbr);
647 * no matching ND entry.
648 * construct the rewrite required to for an ND packet, and stick
649 * that in the adj's pipe to smoke.
651 adj_nbr_update_rewrite (ai,
652 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
653 ethernet_build_rewrite (vnm,
656 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
659 * since the FIB has added this adj for a route, it makes sense it may
660 * want to forward traffic sometime soon. Let's send a speculative ND.
661 * just one. If we were to do periodically that wouldn't be bad either,
662 * but that's more code than i'm prepared to write at this time for
663 * relatively little reward.
668 case IP_LOOKUP_NEXT_BCAST:
669 adj_nbr_update_rewrite (ai,
670 ADJ_NBR_REWRITE_FLAG_COMPLETE,
671 ethernet_build_rewrite (vnm,
674 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
676 case IP_LOOKUP_NEXT_MCAST:
679 * Construct a partial rewrite from the known ethernet mcast dest MAC
684 rewrite = ethernet_build_rewrite (vnm,
687 ethernet_ip6_mcast_dst_addr ());
690 * Complete the remaining fields of the adj's rewrite to direct the
691 * complete of the rewrite at switch time by copying in the IP
692 * dst address's bytes.
693 * Ofset is 2 bytes into the desintation address.
695 offset = vec_len (rewrite) - 2;
696 adj_mcast_update_rewrite (ai, rewrite, offset);
700 case IP_LOOKUP_NEXT_DROP:
701 case IP_LOOKUP_NEXT_PUNT:
702 case IP_LOOKUP_NEXT_LOCAL:
703 case IP_LOOKUP_NEXT_REWRITE:
704 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
705 case IP_LOOKUP_NEXT_MIDCHAIN:
706 case IP_LOOKUP_NEXT_ICMP_ERROR:
707 case IP_LOOKUP_N_NEXT:
715 ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
717 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
719 ip6_ll_prefix_t pfx = {
720 .ilp_addr = n->key.ip6_address,
721 .ilp_sw_if_index = n->key.sw_if_index,
724 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
730 .fp_proto = FIB_PROTOCOL_IP6,
731 .fp_addr.ip6 = n->key.ip6_address,
735 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
736 FIB_ENTRY_FLAG_ATTACHED,
737 DPO_PROTO_IP6, &pfx.fp_addr,
738 n->key.sw_if_index, ~0, 1, NULL,
739 FIB_ROUTE_PATH_FLAG_NONE);
743 static ip6_neighbor_t *
744 force_reuse_neighbor_entry (void)
747 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
749 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
750 if (index == ~0) /* Try again from elt 0 */
751 index = pool_next_index (nm->neighbor_pool, index);
753 /* Find a non-static random entry to free up for reuse */
756 if ((count++ == 100) || (index == ~0))
757 return NULL; /* give up after 100 entries */
758 n = pool_elt_at_index (nm->neighbor_pool, index);
759 nm->neighbor_delete_rotor = index;
760 index = pool_next_index (nm->neighbor_pool, index);
762 while (n->flags & IP6_NEIGHBOR_FLAG_STATIC);
764 /* Remove ARP entry from its interface and update fib */
765 adj_nbr_walk_nh6 (n->key.sw_if_index,
766 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
767 ip6_neighbor_adj_fib_remove
768 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
769 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
775 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
777 const ip6_address_t * a,
778 const u8 * link_layer_address,
779 uword n_bytes_link_layer_address,
780 int is_static, int is_no_fib_entry)
782 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
783 ip6_neighbor_key_t k;
784 ip6_neighbor_t *n = 0;
785 int make_new_nd_cache_entry = 1;
788 pending_resolution_t *pr, *mc;
790 if (vlib_get_thread_index ())
792 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
793 1 /* set new neighbor */ , is_static,
798 k.sw_if_index = sw_if_index;
799 k.ip6_address = a[0];
802 p = mhash_get (&nm->neighbor_index_by_key, &k);
805 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
806 /* Refuse to over-write static neighbor entry. */
807 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
809 /* if MAC address match, still check to send event */
810 if (0 == memcmp (n->link_layer_address,
811 link_layer_address, n_bytes_link_layer_address))
812 goto check_customers;
815 make_new_nd_cache_entry = 0;
818 if (make_new_nd_cache_entry)
820 if (nm->limit_neighbor_cache_size &&
821 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
823 n = force_reuse_neighbor_entry ();
828 pool_get (nm->neighbor_pool, n);
830 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
833 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
835 clib_memcpy (n->link_layer_address,
836 link_layer_address, n_bytes_link_layer_address);
839 * create the adj-fib. the entry in the FIB table for and to the peer.
841 if (!is_no_fib_entry)
843 ip6_neighbor_adj_fib_add
844 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
848 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
854 * prevent a DoS attack from the data-plane that
855 * spams us with no-op updates to the MAC address
857 if (0 == memcmp (n->link_layer_address,
858 link_layer_address, n_bytes_link_layer_address))
860 n->time_last_updated = vlib_time_now (vm);
861 goto check_customers;
864 clib_memcpy (n->link_layer_address,
865 link_layer_address, n_bytes_link_layer_address);
868 /* Update time stamp and flags. */
869 n->time_last_updated = vlib_time_now (vm);
872 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
873 n->flags &= ~IP6_NEIGHBOR_FLAG_DYNAMIC;
877 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
878 n->flags &= ~IP6_NEIGHBOR_FLAG_STATIC;
881 adj_nbr_walk_nh6 (sw_if_index,
882 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
885 /* Customer(s) waiting for this address to be resolved? */
886 p = mhash_get (&nm->pending_resolutions_by_address, a);
891 while (next_index != (u32) ~ 0)
893 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
894 vlib_process_signal_event (vm, pr->node_index,
895 pr->type_opaque, pr->data);
896 next_index = pr->next_index;
897 pool_put (nm->pending_resolutions, pr);
900 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
903 /* Customer(s) requesting ND event for this address? */
904 p = mhash_get (&nm->mac_changes_by_address, a);
909 while (next_index != (u32) ~ 0)
911 int (*fp) (u32, u8 *, u32, ip6_address_t *);
913 mc = pool_elt_at_index (nm->mac_changes, next_index);
914 fp = mc->data_callback;
916 /* Call the user's data callback, return 1 to suppress dup events */
919 (*fp) (mc->data, (u8 *) link_layer_address, sw_if_index,
922 * Signal the resolver process, as long as the user
923 * says they want to be notified
926 vlib_process_signal_event (vm, mc->node_index,
927 mc->type_opaque, mc->data);
928 next_index = mc->next_index;
936 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
937 u32 sw_if_index, const ip6_address_t * a)
939 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
940 ip6_neighbor_key_t k;
945 if (vlib_get_thread_index ())
947 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL,
948 0 /* unset */ , 0, 0);
952 k.sw_if_index = sw_if_index;
953 k.ip6_address = a[0];
956 p = mhash_get (&nm->neighbor_index_by_key, &k);
963 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
965 adj_nbr_walk_nh6 (sw_if_index,
966 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
967 ip6_neighbor_adj_fib_remove
968 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
970 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
971 pool_put (nm->neighbor_pool, n);
977 static void ip6_neighbor_set_unset_rpc_callback
978 (ip6_neighbor_set_unset_rpc_args_t * a)
980 vlib_main_t *vm = vlib_get_main ();
982 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
983 a->link_layer_address, 6, a->is_static,
986 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
990 ip6_neighbor_sort (void *a1, void *a2)
992 vnet_main_t *vnm = vnet_get_main ();
993 ip6_neighbor_t *n1 = a1, *n2 = a2;
995 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
996 n2->key.sw_if_index);
998 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
1003 ip6_neighbors_pool (void)
1005 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1006 return nm->neighbor_pool;
1010 ip6_neighbors_entries (u32 sw_if_index)
1012 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1013 ip6_neighbor_t *n, *ns = 0;
1016 pool_foreach (n, nm->neighbor_pool,
1018 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
1020 vec_add1 (ns, n[0]);
1025 vec_sort_with_function (ns, ip6_neighbor_sort);
1029 static clib_error_t *
1030 show_ip6_neighbors (vlib_main_t * vm,
1031 unformat_input_t * input, vlib_cli_command_t * cmd)
1033 vnet_main_t *vnm = vnet_get_main ();
1034 ip6_neighbor_t *n, *ns;
1035 clib_error_t *error = 0;
1038 /* Filter entries by interface if given. */
1040 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1042 ns = ip6_neighbors_entries (sw_if_index);
1045 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
1048 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
1057 * This command is used to display the adjacent IPv6 hosts found via
1058 * neighbor discovery. Optionally, limit the output to the specified
1062 * Example of how to display the IPv6 neighbor adjacency table:
1063 * @cliexstart{show ip6 neighbors}
1064 * Time Address Flags Link layer Interface
1065 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1066 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1067 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1069 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1070 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1071 * Time Address Flags Link layer Interface
1072 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1073 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1077 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1078 .path = "show ip6 neighbors",
1079 .function = show_ip6_neighbors,
1080 .short_help = "show ip6 neighbors [<interface>]",
1084 static clib_error_t *
1085 set_ip6_neighbor (vlib_main_t * vm,
1086 unformat_input_t * input, vlib_cli_command_t * cmd)
1088 vnet_main_t *vnm = vnet_get_main ();
1094 int is_no_fib_entry = 0;
1097 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1099 /* intfc, ip6-address, mac-address */
1100 if (unformat (input, "%U %U %U",
1101 unformat_vnet_sw_interface, vnm, &sw_if_index,
1102 unformat_ip6_address, &addr,
1103 unformat_ethernet_address, mac_address))
1106 else if (unformat (input, "delete") || unformat (input, "del"))
1108 else if (unformat (input, "static"))
1110 else if (unformat (input, "no-fib-entry"))
1111 is_no_fib_entry = 1;
1117 return clib_error_return (0, "Missing interface, ip6 or hw address");
1120 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
1121 mac_address, sizeof (mac_address),
1122 is_static, is_no_fib_entry);
1124 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
1129 * This command is used to manually add an entry to the IPv6 neighbor
1130 * adjacency table. Optionally, the entry can be added as static. It is
1131 * also used to remove an entry from the table. Use the '<em>show ip6
1132 * neighbors</em>' command to display all learned and manually entered entries.
1135 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1136 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1137 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1138 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1141 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1143 .path = "set ip6 neighbor",
1144 .function = set_ip6_neighbor,
1145 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
1151 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1152 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1153 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1154 } icmp6_neighbor_solicitation_or_advertisement_next_t;
1156 static_always_inline uword
1157 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1158 vlib_node_runtime_t * node,
1159 vlib_frame_t * frame,
1160 uword is_solicitation)
1162 vnet_main_t *vnm = vnet_get_main ();
1163 ip6_main_t *im = &ip6_main;
1164 uword n_packets = frame->n_vectors;
1165 u32 *from, *to_next;
1166 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1167 icmp6_neighbor_discovery_option_type_t option_type;
1168 vlib_node_runtime_t *error_node =
1169 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1172 from = vlib_frame_vector_args (frame);
1173 n_left_from = n_packets;
1174 next_index = node->cached_next_index;
1176 if (node->flags & VLIB_NODE_FLAG_TRACE)
1177 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1179 sizeof (icmp6_input_trace_t));
1183 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1184 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1185 n_advertisements_sent = 0;
1187 while (n_left_from > 0)
1189 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1191 while (n_left_from > 0 && n_left_to_next > 0)
1195 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1196 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1197 u32 bi0, options_len0, sw_if_index0, next0, error0;
1198 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1202 bi0 = to_next[0] = from[0];
1207 n_left_to_next -= 1;
1209 p0 = vlib_get_buffer (vm, bi0);
1210 ip0 = vlib_buffer_get_current (p0);
1211 h0 = ip6_next_header (ip0);
1213 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1215 error0 = ICMP6_ERROR_NONE;
1216 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1217 ip6_sadd_link_local =
1218 ip6_address_is_link_local_unicast (&ip0->src_address);
1219 ip6_sadd_unspecified =
1220 ip6_address_is_unspecified (&ip0->src_address);
1222 /* Check that source address is unspecified, link-local or else on-link. */
1223 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1225 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1227 if (ADJ_INDEX_INVALID != src_adj_index0)
1229 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1231 /* Allow all realistic-looking rewrite adjacencies to pass */
1232 ni0 = adj0->lookup_next_index;
1233 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1234 (ni0 < IP6_LOOKUP_N_NEXT);
1236 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1239 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1245 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1249 o0 = (void *) (h0 + 1);
1250 o0 = ((options_len0 == 8 && o0->header.type == option_type
1251 && o0->header.n_data_u64s == 1) ? o0 : 0);
1253 /* If src address unspecified or link local, donot learn neighbor MAC */
1254 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1255 !ip6_sadd_unspecified))
1257 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1260 &h0->target_address,
1261 o0->ethernet_address,
1262 sizeof (o0->ethernet_address),
1266 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1268 /* Check that target address is local to this router. */
1269 fib_node_index_t fei;
1273 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1275 if (~0 == fib_index)
1277 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1281 if (ip6_address_is_link_local_unicast (&h0->target_address))
1283 fei = ip6_fib_table_lookup_exact_match
1284 (ip6_ll_fib_get (sw_if_index0),
1285 &h0->target_address, 128);
1289 fei = ip6_fib_table_lookup_exact_match (fib_index,
1290 &h0->target_address,
1294 if (FIB_NODE_INDEX_INVALID == fei)
1296 /* The target address is not in the FIB */
1298 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1302 if (FIB_ENTRY_FLAG_LOCAL &
1303 fib_entry_get_flags_for_source (fei,
1304 FIB_SOURCE_INTERFACE))
1306 /* It's an address that belongs to one of our interfaces
1310 if (fib_entry_is_sourced
1311 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1312 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
1314 /* The address was added by IPv6 Proxy ND config.
1315 * We should only respond to these if the NS arrived on
1316 * the link that has a matching covering prefix */
1321 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1327 if (is_solicitation)
1328 next0 = (error0 != ICMP6_ERROR_NONE
1329 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1330 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1334 error0 = error0 == ICMP6_ERROR_NONE ?
1335 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1338 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1340 vnet_sw_interface_t *sw_if0;
1341 ethernet_interface_t *eth_if0;
1342 ethernet_header_t *eth0;
1344 /* dst address is either source address or the all-nodes mcast addr */
1345 if (!ip6_sadd_unspecified)
1346 ip0->dst_address = ip0->src_address;
1348 ip6_set_reserved_multicast_address (&ip0->dst_address,
1349 IP6_MULTICAST_SCOPE_link_local,
1350 IP6_MULTICAST_GROUP_ID_all_hosts);
1352 ip0->src_address = h0->target_address;
1353 ip0->hop_limit = 255;
1354 h0->icmp.type = ICMP6_neighbor_advertisement;
1356 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1357 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1359 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1362 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1364 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1367 h0->advertisement_flags = clib_host_to_net_u32
1368 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1369 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1371 h0->icmp.checksum = 0;
1373 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1375 ASSERT (bogus_length == 0);
1377 /* Reuse current MAC header, copy SMAC to DMAC and
1378 * interface MAC to SMAC */
1379 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1380 eth0 = vlib_buffer_get_current (p0);
1381 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1383 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1385 /* Setup input and output sw_if_index for packet */
1386 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1387 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1388 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1389 vnet_main.local_interface_sw_if_index;
1391 n_advertisements_sent++;
1394 p0->error = error_node->errors[error0];
1396 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1397 to_next, n_left_to_next,
1401 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1404 /* Account for advertisements sent. */
1405 vlib_error_count (vm, error_node->node_index,
1406 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1407 n_advertisements_sent);
1409 return frame->n_vectors;
1412 /* for "syslogging" - use elog for now */
1413 #define foreach_log_level \
1414 _ (DEBUG, "DEBUG") \
1415 _ (INFO, "INFORMATION") \
1416 _ (NOTICE, "NOTICE") \
1417 _ (WARNING, "WARNING") \
1419 _ (CRIT, "CRITICAL") \
1420 _ (ALERT, "ALERT") \
1421 _ (EMERG, "EMERGENCY")
1425 #define _(f,s) LOG_##f,
1430 static char *log_level_strings[] = {
1436 static int logmask = 1 << LOG_DEBUG;
1439 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1441 /* just use elog for now */
1445 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1451 what = va_format (0, fmt, &va);
1453 ELOG_TYPE_DECLARE (e) =
1455 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1460 ed = ELOG_DATA (&vm->elog_main, e);
1461 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1462 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1469 call_ip6_neighbor_callbacks (void *data,
1470 _vnet_ip6_neighbor_function_list_elt_t * elt)
1472 clib_error_t *error = 0;
1476 error = elt->fp (data);
1479 elt = elt->next_ip6_neighbor_function;
1485 /* ipv6 neighbor discovery - router advertisements */
1488 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1489 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1490 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1491 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1492 } icmp6_router_solicitation_or_advertisement_next_t;
1494 static_always_inline uword
1495 icmp6_router_solicitation (vlib_main_t * vm,
1496 vlib_node_runtime_t * node, vlib_frame_t * frame)
1498 vnet_main_t *vnm = vnet_get_main ();
1499 ip6_main_t *im = &ip6_main;
1500 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1501 uword n_packets = frame->n_vectors;
1502 u32 *from, *to_next;
1503 u32 n_left_from, n_left_to_next, next_index;
1504 u32 n_advertisements_sent = 0;
1507 icmp6_neighbor_discovery_option_type_t option_type;
1509 vlib_node_runtime_t *error_node =
1510 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1512 from = vlib_frame_vector_args (frame);
1513 n_left_from = n_packets;
1514 next_index = node->cached_next_index;
1516 if (node->flags & VLIB_NODE_FLAG_TRACE)
1517 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1519 sizeof (icmp6_input_trace_t));
1521 /* source may append his LL address */
1522 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1524 while (n_left_from > 0)
1526 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1528 while (n_left_from > 0 && n_left_to_next > 0)
1532 ip6_radv_t *radv_info = 0;
1534 icmp6_neighbor_discovery_header_t *h0;
1535 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1537 u32 bi0, options_len0, sw_if_index0, next0, error0;
1538 u32 is_solicitation = 1, is_dropped = 0;
1539 u32 is_unspecified, is_link_local;
1541 bi0 = to_next[0] = from[0];
1546 n_left_to_next -= 1;
1548 p0 = vlib_get_buffer (vm, bi0);
1549 ip0 = vlib_buffer_get_current (p0);
1550 h0 = ip6_next_header (ip0);
1552 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1553 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1555 ip6_address_is_link_local_unicast (&ip0->src_address);
1557 error0 = ICMP6_ERROR_NONE;
1558 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1560 /* check if solicitation (not from nd_timer node) */
1561 if (ip6_address_is_unspecified (&ip0->dst_address))
1562 is_solicitation = 0;
1564 /* Check that source address is unspecified, link-local or else on-link. */
1565 if (!is_unspecified && !is_link_local)
1567 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1569 if (ADJ_INDEX_INVALID != src_adj_index0)
1571 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1573 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1575 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1580 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1584 /* check for source LL option and process */
1585 o0 = (void *) (h0 + 1);
1586 o0 = ((options_len0 == 8
1587 && o0->header.type == option_type
1588 && o0->header.n_data_u64s == 1) ? o0 : 0);
1590 /* if src address unspecified IGNORE any options */
1591 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1592 !is_unspecified && !is_link_local))
1594 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1596 o0->ethernet_address,
1597 sizeof (o0->ethernet_address),
1601 /* default is to drop */
1602 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1604 if (error0 == ICMP6_ERROR_NONE)
1606 vnet_sw_interface_t *sw_if0;
1607 ethernet_interface_t *eth_if0;
1610 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1611 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1613 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1615 /* only support ethernet interface type for now */
1617 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1620 if (error0 == ICMP6_ERROR_NONE)
1624 /* adjust the sizeof the buffer to just include the ipv6 header */
1625 p0->current_length -=
1627 sizeof (icmp6_neighbor_discovery_header_t));
1629 /* look up the radv_t information for this interface */
1630 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1634 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1637 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1642 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1645 if (error0 == ICMP6_ERROR_NONE)
1647 f64 now = vlib_time_now (vm);
1649 /* for solicited adverts - need to rate limit */
1650 if (is_solicitation)
1652 if (0 != radv_info->last_radv_time &&
1653 (now - radv_info->last_radv_time) <
1654 MIN_DELAY_BETWEEN_RAS)
1657 radv_info->last_radv_time = now;
1661 icmp6_router_advertisement_header_t rh;
1663 rh.icmp.type = ICMP6_router_advertisement;
1665 rh.icmp.checksum = 0;
1667 rh.current_hop_limit = radv_info->curr_hop_limit;
1668 rh.router_lifetime_in_sec =
1669 clib_host_to_net_u16
1670 (radv_info->adv_router_lifetime_in_sec);
1672 time_in_msec_between_retransmitted_neighbor_solicitations
1674 clib_host_to_net_u32 (radv_info->
1675 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1676 rh.neighbor_reachable_time_in_msec =
1677 clib_host_to_net_u32 (radv_info->
1678 adv_neighbor_reachable_time_in_msec);
1681 (radv_info->adv_managed_flag) ?
1682 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1685 ((radv_info->adv_other_flag) ?
1686 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1690 u16 payload_length =
1691 sizeof (icmp6_router_advertisement_header_t);
1693 if (vlib_buffer_add_data
1694 (vm, &bi0, (void *) &rh,
1695 sizeof (icmp6_router_advertisement_header_t)))
1697 /* buffer allocation failed, drop the pkt */
1698 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1702 if (radv_info->adv_link_layer_address)
1704 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1708 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1709 h.header.n_data_u64s = 1;
1711 /* copy ll address */
1712 clib_memcpy (&h.ethernet_address[0],
1713 eth_if0->address, 6);
1715 if (vlib_buffer_add_data
1716 (vm, &bi0, (void *) &h,
1718 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
1720 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1726 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1729 /* add MTU option */
1730 if (radv_info->adv_link_mtu)
1732 icmp6_neighbor_discovery_mtu_option_t h;
1736 clib_host_to_net_u32 (radv_info->adv_link_mtu);
1737 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1738 h.header.n_data_u64s = 1;
1741 sizeof (icmp6_neighbor_discovery_mtu_option_t);
1743 if (vlib_buffer_add_data
1744 (vm, &bi0, (void *) &h,
1746 (icmp6_neighbor_discovery_mtu_option_t)))
1748 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1753 /* add advertised prefix options */
1754 ip6_radv_prefix_t *pr_info;
1757 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1759 if(pr_info->enabled &&
1760 (!pr_info->decrement_lifetime_flag
1761 || (pr_info->pref_lifetime_expires >0)))
1763 /* advertise this prefix */
1764 icmp6_neighbor_discovery_prefix_information_option_t h;
1766 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1767 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1769 h.dst_address_length = pr_info->prefix_len;
1771 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1772 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1774 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1776 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1777 h.preferred_time = 0;
1781 if(pr_info->decrement_lifetime_flag)
1783 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1784 (pr_info->valid_lifetime_expires - now) : 0;
1786 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1787 (pr_info->pref_lifetime_expires - now) : 0;
1790 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1791 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1795 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1797 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1799 if (vlib_buffer_add_data
1800 (vm, &bi0, (void *)&h,
1801 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
1803 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1811 /* add additional options before here */
1813 /* finish building the router advertisement... */
1814 if (!is_unspecified && radv_info->send_unicast)
1816 ip0->dst_address = ip0->src_address;
1820 /* target address is all-nodes mcast addr */
1821 ip6_set_reserved_multicast_address
1823 IP6_MULTICAST_SCOPE_link_local,
1824 IP6_MULTICAST_GROUP_ID_all_hosts);
1827 /* source address MUST be the link-local address */
1828 ip0->src_address = radv_info->link_local_address;
1830 ip0->hop_limit = 255;
1831 ip0->payload_length =
1832 clib_host_to_net_u16 (payload_length);
1834 icmp6_router_advertisement_header_t *rh0 =
1835 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1836 rh0->icmp.checksum =
1837 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1839 ASSERT (bogus_length == 0);
1841 /* setup output if and adjacency */
1842 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1843 vnet_main.local_interface_sw_if_index;
1845 if (is_solicitation)
1847 ethernet_header_t *eth0;
1848 /* Reuse current MAC header, copy SMAC to DMAC and
1849 * interface MAC to SMAC */
1850 vlib_buffer_reset (p0);
1851 eth0 = vlib_buffer_get_current (p0);
1852 clib_memcpy (eth0->dst_address, eth0->src_address,
1854 clib_memcpy (eth0->src_address, eth_if0->address,
1857 is_dropped ? next0 :
1858 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1859 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1864 adj_index0 = radv_info->mcast_adj_index;
1865 if (adj_index0 == 0)
1866 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1870 is_dropped ? next0 :
1871 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1872 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1876 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1878 radv_info->n_solicitations_dropped += is_dropped;
1879 radv_info->n_solicitations_rcvd += is_solicitation;
1881 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1883 radv_info->n_advertisements_sent++;
1884 n_advertisements_sent++;
1891 p0->error = error_node->errors[error0];
1893 if (error0 != ICMP6_ERROR_NONE)
1894 vlib_error_count (vm, error_node->node_index, error0, 1);
1896 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1897 to_next, n_left_to_next,
1902 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1905 /* Account for router advertisements sent. */
1906 vlib_error_count (vm, error_node->node_index,
1907 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1908 n_advertisements_sent);
1910 return frame->n_vectors;
1913 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1914 static_always_inline uword
1915 icmp6_router_advertisement (vlib_main_t * vm,
1916 vlib_node_runtime_t * node, vlib_frame_t * frame)
1918 vnet_main_t *vnm = vnet_get_main ();
1919 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1920 uword n_packets = frame->n_vectors;
1921 u32 *from, *to_next;
1922 u32 n_left_from, n_left_to_next, next_index;
1923 u32 n_advertisements_rcvd = 0;
1925 vlib_node_runtime_t *error_node =
1926 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1928 from = vlib_frame_vector_args (frame);
1929 n_left_from = n_packets;
1930 next_index = node->cached_next_index;
1932 if (node->flags & VLIB_NODE_FLAG_TRACE)
1933 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1935 sizeof (icmp6_input_trace_t));
1937 while (n_left_from > 0)
1939 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1941 while (n_left_from > 0 && n_left_to_next > 0)
1945 ip6_radv_t *radv_info = 0;
1946 icmp6_router_advertisement_header_t *h0;
1947 u32 bi0, options_len0, sw_if_index0, next0, error0;
1949 bi0 = to_next[0] = from[0];
1954 n_left_to_next -= 1;
1956 p0 = vlib_get_buffer (vm, bi0);
1957 ip0 = vlib_buffer_get_current (p0);
1958 h0 = ip6_next_header (ip0);
1960 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1962 error0 = ICMP6_ERROR_NONE;
1963 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1965 /* Check that source address is link-local */
1966 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1967 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1969 /* default is to drop */
1970 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1972 n_advertisements_rcvd++;
1974 if (error0 == ICMP6_ERROR_NONE)
1976 vnet_sw_interface_t *sw_if0;
1977 ethernet_interface_t *eth_if0;
1979 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1980 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1982 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1984 /* only support ethernet interface type for now */
1986 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1989 if (error0 == ICMP6_ERROR_NONE)
1993 /* look up the radv_t information for this interface */
1994 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1998 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
2001 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2006 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
2009 if (error0 == ICMP6_ERROR_NONE)
2011 radv_info->keep_sending_rs = 0;
2015 r.sw_if_index = sw_if_index0;
2016 memcpy (r.router_address, &ip0->src_address, 16);
2017 r.current_hop_limit = h0->current_hop_limit;
2018 r.flags = h0->flags;
2019 r.router_lifetime_in_sec =
2020 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2021 r.neighbor_reachable_time_in_msec =
2022 clib_net_to_host_u32
2023 (h0->neighbor_reachable_time_in_msec);
2024 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2027 /* validate advertised information */
2028 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2029 && (h0->current_hop_limit !=
2030 radv_info->curr_hop_limit))
2032 ip6_neighbor_syslog (vm, LOG_WARNING,
2033 "our AdvCurHopLimit on %U doesn't agree with %U",
2034 format_vnet_sw_if_index_name,
2041 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2042 != radv_info->adv_managed_flag)
2044 ip6_neighbor_syslog (vm, LOG_WARNING,
2045 "our AdvManagedFlag on %U doesn't agree with %U",
2046 format_vnet_sw_if_index_name,
2053 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2054 != radv_info->adv_other_flag)
2056 ip6_neighbor_syslog (vm, LOG_WARNING,
2057 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2058 format_vnet_sw_if_index_name,
2065 time_in_msec_between_retransmitted_neighbor_solicitations
2067 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2069 time_in_msec_between_retransmitted_neighbor_solicitations
2071 clib_host_to_net_u32 (radv_info->
2072 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
2074 ip6_neighbor_syslog (vm, LOG_WARNING,
2075 "our AdvRetransTimer on %U doesn't agree with %U",
2076 format_vnet_sw_if_index_name,
2082 if ((h0->neighbor_reachable_time_in_msec &&
2083 radv_info->adv_neighbor_reachable_time_in_msec) &&
2084 (h0->neighbor_reachable_time_in_msec !=
2085 clib_host_to_net_u32
2086 (radv_info->adv_neighbor_reachable_time_in_msec)))
2088 ip6_neighbor_syslog (vm, LOG_WARNING,
2089 "our AdvReachableTime on %U doesn't agree with %U",
2090 format_vnet_sw_if_index_name,
2096 /* check for MTU or prefix options or .. */
2097 u8 *opt_hdr = (u8 *) (h0 + 1);
2098 while (options_len0 > 0)
2100 icmp6_neighbor_discovery_option_header_t *o0 =
2101 (icmp6_neighbor_discovery_option_header_t *)
2103 int opt_len = o0->n_data_u64s << 3;
2104 icmp6_neighbor_discovery_option_type_t option_type =
2107 if (options_len0 < 2)
2109 ip6_neighbor_syslog (vm, LOG_ERR,
2110 "malformed RA packet on %U from %U",
2111 format_vnet_sw_if_index_name,
2120 ip6_neighbor_syslog (vm, LOG_ERR,
2121 " zero length option in RA on %U from %U",
2122 format_vnet_sw_if_index_name,
2128 else if (opt_len > options_len0)
2130 ip6_neighbor_syslog (vm, LOG_ERR,
2131 "option length in RA packet greater than total length on %U from %U",
2132 format_vnet_sw_if_index_name,
2139 options_len0 -= opt_len;
2142 switch (option_type)
2144 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2146 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2148 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2151 if (opt_len < sizeof (*h))
2154 memcpy (r.slla, h->ethernet_address, 6);
2158 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
2160 icmp6_neighbor_discovery_mtu_option_t *h =
2161 (icmp6_neighbor_discovery_mtu_option_t
2164 if (opt_len < sizeof (*h))
2167 r.mtu = clib_net_to_host_u32 (h->mtu);
2169 if ((h->mtu && radv_info->adv_link_mtu) &&
2171 clib_host_to_net_u32
2172 (radv_info->adv_link_mtu)))
2174 ip6_neighbor_syslog (vm, LOG_WARNING,
2175 "our AdvLinkMTU on %U doesn't agree with %U",
2176 format_vnet_sw_if_index_name,
2184 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2186 icmp6_neighbor_discovery_prefix_information_option_t
2188 (icmp6_neighbor_discovery_prefix_information_option_t
2191 /* validate advertised prefix options */
2192 ip6_radv_prefix_t *pr_info;
2193 u32 preferred, valid;
2195 if (opt_len < sizeof (*h))
2198 vec_validate (r.prefixes,
2199 vec_len (r.prefixes));
2200 ra_report_prefix_info_t *prefix =
2201 vec_elt_at_index (r.prefixes,
2202 vec_len (r.prefixes) - 1);
2205 clib_net_to_host_u32 (h->preferred_time);
2206 valid = clib_net_to_host_u32 (h->valid_time);
2208 prefix->preferred_time = preferred;
2209 prefix->valid_time = valid;
2210 prefix->flags = h->flags & 0xc0;
2211 prefix->dst_address_length =
2212 h->dst_address_length;
2213 prefix->dst_address = h->dst_address;
2215 /* look for matching prefix - if we our advertising it, it better be consistant */
2217 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2221 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2223 if(pr_info->enabled &&
2224 (pr_info->prefix_len == h->dst_address_length) &&
2225 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2228 if(!pr_info->decrement_lifetime_flag &&
2229 valid != pr_info->adv_valid_lifetime_in_secs)
2231 ip6_neighbor_syslog(vm, LOG_WARNING,
2232 "our ADV validlifetime on %U for %U does not agree with %U",
2233 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2234 format_ip6_address, &h->dst_address);
2236 if(!pr_info->decrement_lifetime_flag &&
2237 preferred != pr_info->adv_pref_lifetime_in_secs)
2239 ip6_neighbor_syslog(vm, LOG_WARNING,
2240 "our ADV preferredlifetime on %U for %U does not agree with %U",
2241 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2242 format_ip6_address, &h->dst_address);
2260 p0->error = error_node->errors[error0];
2262 if (error0 != ICMP6_ERROR_NONE)
2263 vlib_error_count (vm, error_node->node_index, error0, 1);
2265 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2266 to_next, n_left_to_next,
2270 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2273 /* Account for router advertisements received. */
2274 vlib_error_count (vm, error_node->node_index,
2275 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2276 n_advertisements_rcvd);
2278 return frame->n_vectors;
2282 random_f64_from_to (f64 from, f64 to)
2284 static u32 seed = 0;
2285 static u8 seed_set = 0;
2288 seed = random_default_seed ();
2291 return random_f64 (&seed) * (to - from) + from;
2295 get_mac_address (u32 sw_if_index, u8 * address)
2297 vnet_main_t *vnm = vnet_get_main ();
2298 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2299 if (!hw_if->hw_address)
2301 clib_memcpy (address, hw_if->hw_address, 6);
2305 static inline vlib_buffer_t *
2306 create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2310 vlib_buffer_free_list_t *fl;
2311 icmp6_router_solicitation_header_t *rh;
2316 sw_if_index = radv_info->sw_if_index;
2318 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2320 clib_warning ("buffer allocation failure");
2324 p0 = vlib_get_buffer (vm, bi0);
2325 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2326 vlib_buffer_init_for_free_list (p0, fl);
2327 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2328 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2330 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2331 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2333 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2335 rh = vlib_buffer_get_current (p0);
2336 p0->current_length = sizeof (*rh);
2338 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2339 rh->neighbor.icmp.code = 0;
2340 rh->neighbor.icmp.checksum = 0;
2341 rh->neighbor.reserved_must_be_zero = 0;
2343 rh->link_layer_option.header.type =
2344 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2345 if (0 != get_mac_address (sw_if_index,
2346 rh->link_layer_option.ethernet_address))
2348 clib_warning ("interface with sw_if_index %u has no mac address",
2350 vlib_buffer_free (vm, &bi0, 1);
2353 rh->link_layer_option.header.n_data_u64s = 1;
2355 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2357 rh->ip.ip_version_traffic_class_and_flow_label =
2358 clib_host_to_net_u32 (0x6 << 28);
2359 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2360 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2361 rh->ip.hop_limit = 255;
2362 rh->ip.src_address = radv_info->link_local_address;
2363 /* set address ff02::2 */
2364 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
2365 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2367 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2375 stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2379 ra->keep_sending_rs = 0;
2382 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2383 vlib_buffer_free (vm, &bi0, 1);
2389 check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2399 icmp6_send_router_solicitation_params_t *params;
2401 if (!radv_info->keep_sending_rs)
2404 params = &radv_info->params;
2406 if (radv_info->due_time > current_time)
2408 *due_time = radv_info->due_time;
2412 p0 = radv_info->buffer;
2414 next_index = ip6_rewrite_mcast_node.index;
2416 c0 = vlib_buffer_copy (vm, p0);
2417 ci0 = vlib_get_buffer_index (vm, c0);
2419 f = vlib_get_frame_to_node (vm, next_index);
2420 to_next = vlib_frame_vector_args (f);
2423 vlib_put_frame_to_node (vm, next_index, f);
2425 if (params->mrc != 0 && --radv_info->n_left == 0)
2426 stop_sending_rs (vm, radv_info);
2429 radv_info->sleep_interval =
2430 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2431 if (radv_info->sleep_interval > params->mrt)
2432 radv_info->sleep_interval =
2433 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2435 radv_info->due_time = current_time + radv_info->sleep_interval;
2437 if (params->mrd != 0
2438 && current_time > radv_info->start_time + params->mrd)
2439 stop_sending_rs (vm, radv_info);
2441 *due_time = radv_info->due_time;
2444 return radv_info->keep_sending_rs;
2448 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2451 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2452 ip6_radv_t *radv_info;
2453 uword *event_data = 0;
2454 f64 sleep_time = 1e9;
2461 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2462 vlib_process_get_events (vm, &event_data);
2463 vec_reset_length (event_data);
2465 current_time = vlib_time_now (vm);
2468 due_time = current_time + 1e9;
2470 pool_foreach (radv_info, nm->if_radv_pool,
2472 if (check_send_rs (vm, radv_info, current_time, &dt)
2477 current_time = vlib_time_now (vm);
2479 while (due_time < current_time);
2481 sleep_time = due_time - current_time;
2488 VLIB_REGISTER_NODE (send_rs_process_node) = {
2489 .function = send_rs_process,
2490 .type = VLIB_NODE_TYPE_PROCESS,
2491 .name = "send-rs-process",
2496 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2497 icmp6_send_router_solicitation_params_t *
2500 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2504 ASSERT (~0 != sw_if_index);
2506 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2507 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2509 stop_sending_rs (vm, ra);
2513 ra->keep_sending_rs = 1;
2514 ra->params = *params;
2515 ra->n_left = params->mrc;
2516 ra->start_time = vlib_time_now (vm);
2517 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2518 ra->due_time = 0; /* send first packet ASAP */
2519 ra->buffer = create_buffer_for_rs (vm, ra);
2521 ra->keep_sending_rs = 0;
2523 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2528 * @brief Add a multicast Address to the advertised MLD set
2531 ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2533 ip6_mldp_group_t *mcast_group_info;
2536 /* lookup mldp info for this interface */
2537 p = mhash_get (&radv_info->address_to_mldp_index, addr);
2539 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2542 if (!mcast_group_info)
2546 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2548 mi = mcast_group_info - radv_info->mldp_group_pool;
2549 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
2552 mcast_group_info->type = 4;
2553 mcast_group_info->mcast_source_address_pool = 0;
2554 mcast_group_info->num_sources = 0;
2555 clib_memcpy (&mcast_group_info->mcast_address, addr,
2556 sizeof (ip6_address_t));
2561 * @brief Delete a multicast Address from the advertised MLD set
2564 ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2566 ip6_mldp_group_t *mcast_group_info;
2569 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2571 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2573 if (mcast_group_info)
2575 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2577 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2582 * @brief Add a multicast Address to the advertised MLD set
2585 ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2586 ip6_multicast_address_scope_t scope,
2587 ip6_multicast_link_local_group_id_t group)
2591 ip6_set_reserved_multicast_address (&addr, scope, group);
2593 ip6_neighbor_add_mld_prefix (a, &addr);
2597 * @brief create and initialize router advertisement parameters with default
2598 * values for this intfc
2601 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
2602 u32 sw_if_index, u32 is_add)
2604 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2607 vnet_sw_interface_t *sw_if0;
2608 ethernet_interface_t *eth_if0 = 0;
2610 /* lookup radv container - ethernet interfaces only */
2611 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2612 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2613 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2618 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2620 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2624 a = pool_elt_at_index (nm->if_radv_pool, ri);
2628 ip6_radv_prefix_t *p;
2629 ip6_mldp_group_t *m;
2631 /* release the lock on the interface's mcast adj */
2632 adj_unlock (a->mcast_adj_index);
2634 /* clean up prefix and MDP pools */
2636 pool_flush(p, a->adv_prefixes_pool,
2638 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
2640 pool_flush (m, a->mldp_group_pool,
2642 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
2646 pool_free (a->mldp_group_pool);
2647 pool_free (a->adv_prefixes_pool);
2649 mhash_free (&a->address_to_prefix_index);
2650 mhash_free (&a->address_to_mldp_index);
2652 if (a->keep_sending_rs)
2653 a->keep_sending_rs = 0;
2655 pool_put (nm->if_radv_pool, a);
2656 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2664 pool_get (nm->if_radv_pool, a);
2666 ri = a - nm->if_radv_pool;
2667 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2669 /* initialize default values (most of which are zero) */
2670 clib_memset (a, 0, sizeof (a[0]));
2672 a->sw_if_index = sw_if_index;
2673 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2674 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2675 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2676 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2678 /* send ll address source address option */
2679 a->adv_link_layer_address = 1;
2681 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2682 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2683 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2684 a->seed = (u32) clib_cpu_time_now ();
2685 (void) random_u32 (&a->seed);
2686 a->randomizer = clib_cpu_time_now ();
2687 (void) random_u64 (&a->randomizer);
2689 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2690 a->initial_adverts_sent = a->initial_adverts_count - 1;
2691 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2693 /* deafult is to send */
2696 /* fill in radv_info for this interface that will be needed later */
2698 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
2700 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2702 /* fill in default link-local address (this may be overridden) */
2703 ip6_link_local_address_from_ethernet_address
2704 (&a->link_local_address, eth_if0->address);
2706 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2707 sizeof (ip6_address_t));
2708 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2709 sizeof (ip6_address_t));
2711 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2715 a->keep_sending_rs = 0;
2717 /* add multicast groups we will always be reporting */
2718 ip6_neighbor_add_mld_grp (a,
2719 IP6_MULTICAST_SCOPE_link_local,
2720 IP6_MULTICAST_GROUP_ID_all_hosts);
2721 ip6_neighbor_add_mld_grp (a,
2722 IP6_MULTICAST_SCOPE_link_local,
2723 IP6_MULTICAST_GROUP_ID_all_routers);
2724 ip6_neighbor_add_mld_grp (a,
2725 IP6_MULTICAST_SCOPE_link_local,
2726 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2732 /* send an mldpv2 report */
2734 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2736 vnet_main_t *vnm = vnet_get_main ();
2737 vlib_main_t *vm = vnm->vlib_main;
2738 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2739 vnet_sw_interface_t *sw_if0;
2740 ethernet_interface_t *eth_if0;
2744 ip6_radv_t *radv_info;
2754 icmp6_multicast_listener_report_header_t *rh0;
2755 icmp6_multicast_listener_report_packet_t *rp0;
2757 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2758 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2759 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2761 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2764 /* look up the radv_t information for this interface */
2765 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2768 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2773 /* send report now - build a mldpv2 report packet */
2774 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
2775 if (PREDICT_FALSE (n_allocated == 0))
2778 clib_warning ("buffer allocation failure");
2782 b0 = vlib_get_buffer (vm, bo0);
2784 /* adjust the sizeof the buffer to just include the ipv6 header */
2785 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2787 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2789 b0->error = ICMP6_ERROR_NONE;
2791 rp0 = vlib_buffer_get_current (b0);
2792 ip0 = (ip6_header_t *) & rp0->ip;
2793 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2795 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2797 ip0->ip_version_traffic_class_and_flow_label =
2798 clib_host_to_net_u32 (0x6 << 28);
2800 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2801 /* for DEBUG - vnet driver won't seem to emit router alerts */
2802 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2805 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2807 /* source address MUST be the link-local address */
2808 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2809 ip0->src_address = radv_info->link_local_address;
2811 /* destination is all mldpv2 routers */
2812 ip6_set_reserved_multicast_address (&ip0->dst_address,
2813 IP6_MULTICAST_SCOPE_link_local,
2814 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2816 /* add reports here */
2817 ip6_mldp_group_t *m;
2818 int num_addr_records = 0;
2819 icmp6_multicast_address_record_t rr;
2821 /* fill in the hop-by-hop extension header (router alert) info */
2822 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2823 rh0->ext_hdr.n_data_u64s = 0;
2825 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2827 rh0->alert.value = 0;
2832 rh0->icmp.checksum = 0;
2835 pool_foreach (m, radv_info->mldp_group_pool,
2838 rr.aux_data_len_u32s = 0;
2839 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2840 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2844 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
2845 sizeof(icmp6_multicast_address_record_t)))
2847 vlib_buffer_free (vm, &bo0, 1);
2851 payload_length += sizeof( icmp6_multicast_address_record_t);
2856 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2858 /* update lengths */
2859 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2861 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2863 ASSERT (bogus_length == 0);
2866 * OK to override w/ no regard for actual FIB, because
2867 * ip6-rewrite only looks at the adjacency.
2869 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2870 vnet_main.local_interface_sw_if_index;
2872 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2873 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2875 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2877 f = vlib_get_frame_to_node (vm, node->index);
2878 to_next = vlib_frame_vector_args (f);
2882 vlib_put_frame_to_node (vm, node->index, f);
2887 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2889 .function = icmp6_router_solicitation,
2890 .name = "icmp6-router-solicitation",
2892 .vector_size = sizeof (u32),
2894 .format_trace = format_icmp6_input_trace,
2896 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2898 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
2899 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2900 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2905 /* send a RA or update the timer info etc.. */
2907 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2908 vlib_node_runtime_t * node,
2909 vlib_frame_t * frame)
2911 vnet_main_t *vnm = vnet_get_main ();
2912 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2913 ip6_radv_t *radv_info;
2914 vlib_frame_t *f = 0;
2915 u32 n_this_frame = 0;
2916 u32 n_left_to_next = 0;
2919 icmp6_router_solicitation_header_t *h0;
2921 f64 now = vlib_time_now (vm);
2923 /* Interface ip6 radv info list */
2925 pool_foreach (radv_info, nm->if_radv_pool,
2927 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2929 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2930 radv_info->next_multicast_time = now;
2931 radv_info->last_multicast_time = now;
2932 radv_info->last_radv_time = 0;
2933 radv_info->all_routers_mcast = 0;
2937 /* Make sure that we've joined the all-routers multicast group */
2938 if(!radv_info->all_routers_mcast)
2940 /* send MDLP_REPORT_EVENT message */
2941 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2942 radv_info->all_routers_mcast = 1;
2945 /* is it time to send a multicast RA on this interface? */
2946 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2951 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2952 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2954 /* multicast send - compute next multicast send time */
2955 if( radv_info->initial_adverts_sent > 0)
2957 radv_info->initial_adverts_sent--;
2958 if(rfn > radv_info-> initial_adverts_interval)
2959 rfn = radv_info-> initial_adverts_interval;
2961 /* check to see if we are ceasing to send */
2962 if( radv_info->initial_adverts_sent == 0)
2963 if(radv_info->cease_radv)
2964 radv_info->send_radv = 0;
2967 radv_info->next_multicast_time = rfn + now;
2968 radv_info->last_multicast_time = now;
2970 /* send advert now - build a "solicted" router advert with unspecified source address */
2971 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
2973 if (PREDICT_FALSE(n_allocated == 0))
2975 clib_warning ("buffer allocation failure");
2978 b0 = vlib_get_buffer (vm, bo0);
2979 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2980 b0->error = ICMP6_ERROR_NONE;
2981 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2983 h0 = vlib_buffer_get_current (b0);
2985 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2987 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2988 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2989 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2990 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2991 h0->ip.hop_limit = 255;
2993 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2994 h0->ip.src_address.as_u64[0] = 0;
2995 h0->ip.src_address.as_u64[1] = 0;
2997 h0->ip.dst_address.as_u64[0] = 0;
2998 h0->ip.dst_address.as_u64[1] = 0;
3000 h0->neighbor.icmp.type = ICMP6_router_solicitation;
3002 if (PREDICT_FALSE(f == 0))
3004 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
3005 to_next = vlib_frame_vector_args (f);
3006 n_left_to_next = VLIB_FRAME_SIZE;
3015 if (PREDICT_FALSE(n_left_to_next == 0))
3017 f->n_vectors = n_this_frame;
3018 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3027 ASSERT (n_this_frame);
3028 f->n_vectors = n_this_frame;
3029 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3035 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3036 vlib_node_runtime_t * node,
3037 vlib_frame_t * frame)
3040 ip6_icmp_neighbor_discovery_event_data_t *event_data;
3042 /* init code here */
3046 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
3048 event_data = vlib_process_get_event_data (vm, &event_type);
3052 /* No events found: timer expired. */
3053 /* process interface list and send RAs as appropriate, update timer info */
3054 ip6_neighbor_process_timer_event (vm, node, frame);
3061 case ICMP6_ND_EVENT_INIT:
3072 _vec_len (event_data) = 0;
3075 return frame->n_vectors;
3079 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3081 .function = icmp6_router_advertisement,
3082 .name = "icmp6-router-advertisement",
3084 .vector_size = sizeof (u32),
3086 .format_trace = format_icmp6_input_trace,
3095 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3097 .function = ip6_icmp_neighbor_discovery_event_process,
3098 .name = "ip6-icmp-neighbor-discovery-event-process",
3099 .type = VLIB_NODE_TYPE_PROCESS,
3103 icmp6_neighbor_solicitation (vlib_main_t * vm,
3104 vlib_node_runtime_t * node, vlib_frame_t * frame)
3106 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3107 /* is_solicitation */
3112 icmp6_neighbor_advertisement (vlib_main_t * vm,
3113 vlib_node_runtime_t * node,
3114 vlib_frame_t * frame)
3116 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3117 /* is_solicitation */
3122 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3124 .function = icmp6_neighbor_solicitation,
3125 .name = "icmp6-neighbor-solicitation",
3127 .vector_size = sizeof (u32),
3129 .format_trace = format_icmp6_input_trace,
3131 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3133 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
3134 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3140 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3142 .function = icmp6_neighbor_advertisement,
3143 .name = "icmp6-neighbor-advertisement",
3145 .vector_size = sizeof (u32),
3147 .format_trace = format_icmp6_input_trace,
3158 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3159 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3160 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3161 } ip6_discover_neighbor_next_t;
3165 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3166 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3167 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3168 } ip6_discover_neighbor_error_t;
3171 ip6_discover_neighbor_inline (vlib_main_t * vm,
3172 vlib_node_runtime_t * node,
3173 vlib_frame_t * frame, int is_glean)
3175 vnet_main_t *vnm = vnet_get_main ();
3176 ip6_main_t *im = &ip6_main;
3177 ip_lookup_main_t *lm = &im->lookup_main;
3178 u32 *from, *to_next_drop;
3179 uword n_left_from, n_left_to_next_drop;
3181 u32 thread_index = vm->thread_index;
3183 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3185 if (node->flags & VLIB_NODE_FLAG_TRACE)
3186 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3188 seed = throttle_seed (&im->nd_throttle, thread_index, vlib_time_now (vm));
3190 from = vlib_frame_vector_args (frame);
3191 n_left_from = frame->n_vectors;
3193 while (n_left_from > 0)
3195 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3196 to_next_drop, n_left_to_next_drop);
3198 while (n_left_from > 0 && n_left_to_next_drop > 0)
3200 u32 pi0, adj_index0, sw_if_index0, drop0, r0, next0;
3201 vnet_hw_interface_t *hw_if0;
3202 ip6_radv_t *radv_info;
3203 ip_adjacency_t *adj0;
3209 p0 = vlib_get_buffer (vm, pi0);
3211 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3213 ip0 = vlib_buffer_get_current (p0);
3215 adj0 = adj_get (adj_index0);
3219 ip0->dst_address.as_u64[0] =
3220 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3221 ip0->dst_address.as_u64[1] =
3222 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3225 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3226 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3228 /* combine the address and interface for a hash */
3229 r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;
3231 drop0 = throttle_check (&im->nd_throttle, thread_index, r0, seed);
3235 to_next_drop[0] = pi0;
3237 n_left_to_next_drop -= 1;
3239 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3241 /* If the interface is link-down, drop the pkt */
3242 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3245 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3247 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3250 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3258 * the adj has been updated to a rewrite but the node the DPO that got
3259 * us here hasn't - yet. no big deal. we'll drop while we wait.
3261 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3265 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3266 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3273 icmp6_neighbor_solicitation_header_t *h0;
3276 h0 = vlib_packet_template_get_packet
3277 (vm, &im->discover_neighbor_packet_template, &bi0);
3281 /* copy the persistent fields from the original */
3282 b0 = vlib_get_buffer (vm, bi0);
3283 clib_memcpy_fast (b0->opaque2, p0->opaque2, sizeof (p0->opaque2));
3286 * Build ethernet header.
3287 * Choose source address based on destination lookup
3290 if (!ip6_src_address_for_packet (lm,
3293 &h0->ip.src_address))
3295 /* There is no address on the interface */
3297 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3298 vlib_buffer_free (vm, &bi0, 1);
3303 * Destination address is a solicited node multicast address.
3304 * We need to fill in
3305 * the low 24 bits with low 24 bits of target's address.
3307 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3308 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3309 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3311 h0->neighbor.target_address = ip0->dst_address;
3313 clib_memcpy (h0->link_layer_option.ethernet_address,
3314 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3316 /* $$$$ appears we need this; why is the checksum non-zero? */
3317 h0->neighbor.icmp.checksum = 0;
3318 h0->neighbor.icmp.checksum =
3319 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3322 ASSERT (bogus_length == 0);
3324 vlib_buffer_copy_trace_flag (vm, p0, bi0);
3325 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3326 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3328 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3329 radv_info->mcast_adj_index;
3331 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3332 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3334 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3338 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3339 n_left_to_next_drop);
3342 return frame->n_vectors;
3346 ip6_discover_neighbor (vlib_main_t * vm,
3347 vlib_node_runtime_t * node, vlib_frame_t * frame)
3349 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3353 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3355 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3358 static char *ip6_discover_neighbor_error_strings[] = {
3359 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3360 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3361 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3362 = "no source address for ND solicitation",
3366 VLIB_REGISTER_NODE (ip6_glean_node) =
3368 .function = ip6_glean,
3369 .name = "ip6-glean",
3370 .vector_size = sizeof (u32),
3371 .format_trace = format_ip6_forward_next_trace,
3372 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3373 .error_strings = ip6_discover_neighbor_error_strings,
3374 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3377 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3378 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3381 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3383 .function = ip6_discover_neighbor,
3384 .name = "ip6-discover-neighbor",
3385 .vector_size = sizeof (u32),
3386 .format_trace = format_ip6_forward_next_trace,
3387 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3388 .error_strings = ip6_discover_neighbor_error_strings,
3389 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3392 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3393 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3398 /* API support functions */
3400 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3401 u8 suppress, u8 managed, u8 other,
3402 u8 ll_option, u8 send_unicast, u8 cease,
3403 u8 use_lifetime, u32 lifetime,
3404 u32 initial_count, u32 initial_interval,
3405 u32 max_interval, u32 min_interval, u8 is_no)
3407 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3411 /* look up the radv_t information for this interface */
3412 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3414 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3415 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3420 ip6_radv_t *radv_info;
3421 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3423 if ((max_interval != 0) && (min_interval == 0))
3424 min_interval = .75 * max_interval;
3428 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3429 radv_info->max_radv_interval;
3432 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3433 radv_info->min_radv_interval;
3436 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3437 radv_info->adv_router_lifetime_in_sec;
3441 if (lifetime > MAX_DEF_RTR_LIFETIME)
3442 lifetime = MAX_DEF_RTR_LIFETIME;
3444 if (lifetime <= max_interval)
3445 return VNET_API_ERROR_INVALID_VALUE;
3448 if (min_interval != 0)
3450 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3451 return VNET_API_ERROR_INVALID_VALUE;
3454 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3455 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3456 return VNET_API_ERROR_INVALID_VALUE;
3459 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3460 if "flag" is clear don't change corresponding value
3462 radv_info->send_radv =
3463 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3464 radv_info->adv_managed_flag =
3465 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3466 radv_info->adv_other_flag =
3467 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3468 radv_info->adv_link_layer_address =
3470 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3471 radv_info->send_unicast =
3472 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3473 radv_info->cease_radv =
3474 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3476 radv_info->min_radv_interval = min_interval;
3477 radv_info->max_radv_interval = max_interval;
3478 radv_info->adv_router_lifetime_in_sec = lifetime;
3480 radv_info->initial_adverts_count =
3482 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3483 radv_info->initial_adverts_count;
3484 radv_info->initial_adverts_interval =
3485 (initial_interval !=
3486 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3487 radv_info->initial_adverts_interval;
3490 if ((cease != 0) && (is_no))
3491 radv_info->send_radv = 1;
3493 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3494 radv_info->next_multicast_time = vlib_time_now (vm);
3495 radv_info->last_multicast_time = vlib_time_now (vm);
3496 radv_info->last_radv_time = 0;
3502 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3503 ip6_address_t * prefix_addr, u8 prefix_len,
3504 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3505 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3506 u8 no_onlink, u8 is_no)
3508 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3513 /* look up the radv_t information for this interface */
3514 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3517 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3519 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3523 f64 now = vlib_time_now (vm);
3524 ip6_radv_t *radv_info;
3525 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3527 /* prefix info add, delete or update */
3528 ip6_radv_prefix_t *prefix;
3530 /* lookup prefix info for this address on this interface */
3531 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3533 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3539 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3541 if (prefix->prefix_len != prefix_len)
3542 return VNET_API_ERROR_INVALID_VALUE_2;
3544 /* FIXME - Should the DP do this or the CP ? */
3545 /* do specific delete processing here before returning */
3546 /* try to remove from routing table */
3548 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3550 pool_put (radv_info->adv_prefixes_pool, prefix);
3552 radv_info->initial_adverts_sent =
3553 radv_info->initial_adverts_count - 1;
3554 radv_info->next_multicast_time = vlib_time_now (vm);
3555 radv_info->last_multicast_time = vlib_time_now (vm);
3556 radv_info->last_radv_time = 0;
3560 /* adding or changing */
3565 pool_get (radv_info->adv_prefixes_pool, prefix);
3566 pi = prefix - radv_info->adv_prefixes_pool;
3567 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3570 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3572 prefix->prefix_len = prefix_len;
3573 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3575 /* initialize default values */
3576 prefix->adv_on_link_flag = 1; /* L bit set */
3577 prefix->adv_autonomous_flag = 1; /* A bit set */
3578 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
3579 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3580 prefix->enabled = 1;
3581 prefix->decrement_lifetime_flag = 1;
3582 prefix->deprecated_prefix_flag = 1;
3586 /* FIXME - Should the DP do this or the CP ? */
3587 /* insert prefix into routing table as a connected prefix */
3596 if (prefix->prefix_len != prefix_len)
3597 return VNET_API_ERROR_INVALID_VALUE_2;
3601 /* FIXME - Should the DP do this or the CP ? */
3602 /* remove from routing table if already there */
3606 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
3608 prefix->adv_valid_lifetime_in_secs = ~0;
3609 prefix->adv_pref_lifetime_in_secs = ~0;
3610 prefix->decrement_lifetime_flag = 0;
3614 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3615 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
3618 /* copy remaining */
3619 prefix->enabled = !(no_advertise != 0);
3620 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3621 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3625 /* fill in the expiration times */
3626 prefix->valid_lifetime_expires =
3627 now + prefix->adv_valid_lifetime_in_secs;
3628 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
3630 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3631 radv_info->next_multicast_time = vlib_time_now (vm);
3632 radv_info->last_multicast_time = vlib_time_now (vm);
3633 radv_info->last_radv_time = 0;
3639 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3640 vlib_cli_command_t * cmd)
3642 vnet_main_t *vnm = vnet_get_main ();
3643 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3644 clib_error_t *error = 0;
3646 u8 suppress = 0, managed = 0, other = 0;
3647 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
3648 u8 use_lifetime = 0;
3649 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3650 0, ra_initial_interval = 0;
3651 u32 ra_max_interval = 0, ra_min_interval = 0;
3653 unformat_input_t _line_input, *line_input = &_line_input;
3654 vnet_sw_interface_t *sw_if0;
3656 int add_radv_info = 1;
3657 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
3658 ip6_address_t ip6_addr;
3662 /* Get a line of input. */
3663 if (!unformat_user (main_input, unformat_line_input, line_input))
3666 /* get basic radv info for this interface */
3667 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3670 if (unformat_user (line_input,
3671 unformat_vnet_sw_interface, vnm, &sw_if_index))
3674 ethernet_interface_t *eth_if0 = 0;
3676 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3677 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3679 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
3684 clib_error_return (0, "Interface must be of ethernet type");
3688 /* look up the radv_t information for this interface */
3689 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3692 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3696 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3700 error = clib_error_return (0, "unknown interface %U'",
3701 format_unformat_error, line_input);
3707 error = clib_error_return (0, "invalid interface name %U'",
3708 format_unformat_error, line_input);
3713 /* get the rest of the command */
3714 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3716 if (unformat (line_input, "no"))
3718 else if (unformat (line_input, "prefix %U/%d",
3719 unformat_ip6_address, &ip6_addr, &addr_len))
3724 else if (unformat (line_input, "ra-managed-config-flag"))
3729 else if (unformat (line_input, "ra-other-config-flag"))
3734 else if (unformat (line_input, "ra-suppress") ||
3735 unformat (line_input, "ra-surpress"))
3740 else if (unformat (line_input, "ra-suppress-link-layer") ||
3741 unformat (line_input, "ra-surpress-link-layer"))
3743 suppress_ll_option = 1;
3746 else if (unformat (line_input, "ra-send-unicast"))
3751 else if (unformat (line_input, "ra-lifetime"))
3753 if (!unformat (line_input, "%d", &ra_lifetime))
3755 error = unformat_parse_error (line_input);
3761 else if (unformat (line_input, "ra-initial"))
3764 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
3766 error = unformat_parse_error (line_input);
3771 else if (unformat (line_input, "ra-interval"))
3773 if (!unformat (line_input, "%d", &ra_max_interval))
3775 error = unformat_parse_error (line_input);
3779 if (!unformat (line_input, "%d", &ra_min_interval))
3780 ra_min_interval = 0;
3783 else if (unformat (line_input, "ra-cease"))
3790 error = unformat_parse_error (line_input);
3797 ip6_neighbor_ra_config (vm, sw_if_index,
3798 suppress, managed, other,
3799 suppress_ll_option, send_unicast, cease,
3800 use_lifetime, ra_lifetime,
3801 ra_initial_count, ra_initial_interval,
3802 ra_max_interval, ra_min_interval, is_no);
3806 u32 valid_lifetime_in_secs = 0;
3807 u32 pref_lifetime_in_secs = 0;
3808 u8 use_prefix_default_values = 0;
3809 u8 no_advertise = 0;
3811 u8 no_autoconfig = 0;
3814 /* get the rest of the command */
3815 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3817 if (unformat (line_input, "default"))
3819 use_prefix_default_values = 1;
3822 else if (unformat (line_input, "infinite"))
3824 valid_lifetime_in_secs = ~0;
3825 pref_lifetime_in_secs = ~0;
3828 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3829 &pref_lifetime_in_secs))
3836 /* get the rest of the command */
3837 while (!use_prefix_default_values &&
3838 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3840 if (unformat (line_input, "no-advertise"))
3842 else if (unformat (line_input, "off-link"))
3844 else if (unformat (line_input, "no-autoconfig"))
3846 else if (unformat (line_input, "no-onlink"))
3850 error = unformat_parse_error (line_input);
3855 ip6_neighbor_ra_prefix (vm, sw_if_index,
3856 &ip6_addr, addr_len,
3857 use_prefix_default_values,
3858 valid_lifetime_in_secs,
3859 pref_lifetime_in_secs,
3861 off_link, no_autoconfig, no_onlink, is_no);
3865 unformat_free (line_input);
3871 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3873 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3876 for (i = 0; i < vec_len (addrs); i++)
3878 ip_interface_address_t *a =
3879 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3880 ip6_address_t *address = ip_interface_address_get_address (lm, a);
3882 vlib_cli_output (vm, "\t\t%U/%d",
3883 format_ip6_address, address, a->address_length);
3887 static clib_error_t *
3888 show_ip6_interface_cmd (vlib_main_t * vm,
3889 unformat_input_t * input, vlib_cli_command_t * cmd)
3891 vnet_main_t *vnm = vnet_get_main ();
3892 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3893 clib_error_t *error = 0;
3898 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3902 /* look up the radv_t information for this interface */
3903 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3906 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3910 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3911 ip6_radv_t *radv_info;
3912 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3914 vlib_cli_output (vm, "%U is admin %s\n",
3915 format_vnet_sw_interface_name, vnm,
3916 vnet_get_sw_interface (vnm, sw_if_index),
3917 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3921 u32 *link_scope = 0, *global_scope = 0;
3922 u32 *local_scope = 0, *unknown_scope = 0;
3923 ip_interface_address_t *a;
3925 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3927 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3929 while (ai != (u32) ~ 0)
3931 a = pool_elt_at_index (lm->if_address_pool, ai);
3932 ip6_address_t *address =
3933 ip_interface_address_get_address (lm, a);
3935 if (ip6_address_is_link_local_unicast (address))
3936 vec_add1 (link_scope, ai);
3937 else if (ip6_address_is_global_unicast (address))
3938 vec_add1 (global_scope, ai);
3939 else if (ip6_address_is_local_unicast (address))
3940 vec_add1 (local_scope, ai);
3942 vec_add1 (unknown_scope, ai);
3944 ai = a->next_this_sw_interface;
3947 if (vec_len (link_scope))
3949 vlib_cli_output (vm, "\tLink-local address(es):\n");
3950 ip6_print_addrs (vm, link_scope);
3951 vec_free (link_scope);
3954 if (vec_len (local_scope))
3956 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3957 ip6_print_addrs (vm, local_scope);
3958 vec_free (local_scope);
3961 if (vec_len (global_scope))
3963 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3964 ip6_print_addrs (vm, global_scope);
3965 vec_free (global_scope);
3968 if (vec_len (unknown_scope))
3970 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3971 ip6_print_addrs (vm, unknown_scope);
3972 vec_free (unknown_scope);
3975 vlib_cli_output (vm, "\tLink-local address(es):\n");
3976 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3977 &radv_info->link_local_address);
3979 vlib_cli_output (vm, "\tJoined group address(es):\n");
3980 ip6_mldp_group_t *m;
3982 pool_foreach (m, radv_info->mldp_group_pool,
3984 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3989 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3990 ip6_radv_prefix_t *p;
3992 pool_foreach (p, radv_info->adv_prefixes_pool,
3994 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3995 format_ip6_address, &p->prefix, p->prefix_len);
3999 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
4000 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
4001 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
4002 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
4003 vlib_cli_output (vm, "\tND DAD is disabled\n");
4004 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
4005 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
4006 radv_info->adv_neighbor_reachable_time_in_msec);
4007 vlib_cli_output (vm,
4008 "\tND advertised retransmit interval is %d (msec)\n",
4010 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
4012 u32 ra_interval = radv_info->max_radv_interval;
4013 u32 ra_interval_min = radv_info->min_radv_interval;
4014 vlib_cli_output (vm,
4015 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
4016 ra_interval, ra_interval_min);
4017 vlib_cli_output (vm,
4018 "\tND router advertisements live for %d seconds\n",
4019 radv_info->adv_router_lifetime_in_sec);
4020 vlib_cli_output (vm,
4021 "\tHosts %s stateless autoconfig for addresses\n",
4022 (radv_info->adv_managed_flag) ? "use" :
4024 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4025 radv_info->n_advertisements_sent);
4026 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4027 radv_info->n_solicitations_rcvd);
4028 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4029 radv_info->n_solicitations_dropped);
4033 error = clib_error_return (0, "IPv6 not enabled on interface",
4034 format_unformat_error, input);
4042 * This command is used to display various IPv6 attributes on a given
4046 * Example of how to display IPv6 settings:
4047 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4048 * GigabitEthernet2/0/0 is admin up
4049 * Link-local address(es):
4051 * Joined group address(es):
4056 * Advertised Prefixes:
4057 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4059 * ICMP error messages are unlimited
4060 * ICMP redirects are disabled
4061 * ICMP unreachables are not sent
4062 * ND DAD is disabled
4063 * ND advertised reachable time is 0
4064 * ND advertised retransmit interval is 0 (msec)
4065 * ND router advertisements are sent every 200 seconds (min interval is 150)
4066 * ND router advertisements live for 600 seconds
4067 * Hosts use stateless autoconfig for addresses
4068 * ND router advertisements sent 19336
4069 * ND router solicitations received 0
4070 * ND router solicitations dropped 0
4072 * Example of output if IPv6 is not enabled on the interface:
4073 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4074 * show ip6 interface: IPv6 not enabled on interface
4078 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4080 .path = "show ip6 interface",
4081 .function = show_ip6_interface_cmd,
4082 .short_help = "show ip6 interface <interface>",
4087 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4089 clib_error_t *error = 0;
4090 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4093 /* look up the radv_t information for this interface */
4094 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4096 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4098 /* if not created - do nothing */
4101 ip6_radv_t *radv_info;
4103 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4105 /* check radv_info ref count for other ip6 addresses on this interface */
4106 /* This implicitly excludes the link local address */
4107 if (radv_info->ref_count == 0)
4109 /* essentially "disables" ipv6 on this interface */
4110 ip6_ll_prefix_t ilp = {
4111 .ilp_addr = radv_info->link_local_address,
4112 .ilp_sw_if_index = sw_if_index,
4114 ip6_ll_table_entry_delete (&ilp);
4115 ip6_sw_interface_enable_disable (sw_if_index, 0);
4116 ip6_mfib_interface_enable_disable (sw_if_index, 0);
4117 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4125 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
4127 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4130 /* look up the radv_t information for this interface */
4131 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4134 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4140 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4142 clib_error_t *error = 0;
4143 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4147 /* look up the radv_t information for this interface */
4148 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4151 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4153 /* if not created yet */
4156 vnet_main_t *vnm = vnet_get_main ();
4157 vnet_sw_interface_t *sw_if0;
4159 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4160 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4162 ethernet_interface_t *eth_if0;
4165 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
4168 /* create radv_info. for this interface. This holds all the info needed for router adverts */
4170 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
4174 ip6_radv_t *radv_info;
4175 ip6_address_t link_local_address;
4177 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4179 ip6_link_local_address_from_ethernet_mac_address
4180 (&link_local_address, eth_if0->address);
4182 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
4183 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
4184 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
4185 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
4187 /* make up an interface id */
4188 link_local_address.as_u64[1] =
4189 random_u64 (&radv_info->randomizer);
4191 link_local_address.as_u64[0] =
4192 clib_host_to_net_u64 (0xFE80000000000000ULL);
4194 link_local_address.as_u8[8] &= 0xfd;
4197 ip6_ll_prefix_t ilp = {
4198 .ilp_addr = link_local_address,
4199 .ilp_sw_if_index = sw_if_index,
4202 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4205 /* essentially "enables" ipv6 on this interface */
4206 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4207 ip6_sw_interface_enable_disable (sw_if_index, 1);
4211 radv_info->link_local_address = link_local_address;
4221 ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4223 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4224 ip6_radv_t *radv_info;
4227 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
4230 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4235 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4236 *addr = radv_info->link_local_address;
4241 static clib_error_t *
4242 enable_ip6_interface_cmd (vlib_main_t * vm,
4243 unformat_input_t * input, vlib_cli_command_t * cmd)
4245 vnet_main_t *vnm = vnet_get_main ();
4246 clib_error_t *error = 0;
4251 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4253 enable_ip6_interface (vm, sw_if_index);
4257 error = clib_error_return (0, "unknown interface\n'",
4258 format_unformat_error, input);
4265 * This command is used to enable IPv6 on a given interface.
4268 * Example of how enable IPv6 on a given interface:
4269 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4272 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4274 .path = "enable ip6 interface",
4275 .function = enable_ip6_interface_cmd,
4276 .short_help = "enable ip6 interface <interface>",
4280 static clib_error_t *
4281 disable_ip6_interface_cmd (vlib_main_t * vm,
4282 unformat_input_t * input, vlib_cli_command_t * cmd)
4284 vnet_main_t *vnm = vnet_get_main ();
4285 clib_error_t *error = 0;
4290 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4292 error = disable_ip6_interface (vm, sw_if_index);
4296 error = clib_error_return (0, "unknown interface\n'",
4297 format_unformat_error, input);
4304 * This command is used to disable IPv6 on a given interface.
4307 * Example of how disable IPv6 on a given interface:
4308 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4311 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4313 .path = "disable ip6 interface",
4314 .function = disable_ip6_interface_cmd,
4315 .short_help = "disable ip6 interface <interface>",
4320 * This command is used to configure the neighbor discovery
4321 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4322 * command to display some of the current neighbor discovery parameters
4323 * on a given interface. This command has three formats:
4326 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4328 * '<em><b>ip6 nd <interface> [no] [ra-managed-config-flag] | [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] | [ra-send-unicast] | [ra-lifetime <lifetime>] | [ra-initial <cnt> <interval>] | [ra-interval <max-interval> [<min-interval>]] | [ra-cease]</b></em>'
4332 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4333 * router-advertisement messages to use stateful address
4334 * auto-configuration to obtain address information (sets the M-bit).
4335 * Default is the M-bit is not set and the '<em>no</em>' option
4336 * returns it to this default state.
4338 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4339 * router-advertisement messages that hosts use stateful auto
4340 * configuration to obtain nonaddress related information (sets
4341 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4342 * option returns it to this default state.
4344 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4345 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4346 * router-advertisement messages.
4348 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4349 * optional source link-layer address in the ICMPv6 router-advertisement
4350 * messages. Default is to include the optional source link-layer address
4351 * and the '<em>no</em>' option returns it to this default state.
4353 * <em>[no] ra-send-unicast</em> - Use the source address of the
4354 * router-solicitation message if availiable. The default is to use
4355 * multicast address of all nodes, and the '<em>no</em>' option returns
4356 * it to this default state.
4358 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4359 * default router in ICMPv6 router-advertisement messages. The range is
4360 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4361 * '<em><max-interval></em>'. The default value is 600 seconds and the
4362 * '<em>no</em>' option returns it to this default value.
4364 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4365 * router-advertisement messages sent and the interval between each
4366 * message. Range for count is 1 - 3 and default is 3. Range for interval
4367 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4368 * returns both to their default value.
4370 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4371 * interval between sending ICMPv6 router-advertisement messages. The
4372 * range for max-interval is from 4 to 200 seconds. min-interval can not
4373 * be more than 75% of max-interval. If not set, min-interval will be
4374 * set to 75% of max-interval. The range for min-interval is from 3 to
4375 * 150 seconds. The '<em>no</em>' option returns both to their default
4378 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4379 * The '<em>no</em>' options implies to start (or restart) sending
4380 * ICMPv6 router-advertisement messages.
4383 * <b>Format 2 - Prefix Options:</b>
4385 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link] [no-autoconfig] [no-onlink]</b></em>'
4389 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4391 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4392 * length of time in seconds during what the prefix is valid for the purpose of
4393 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4394 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4395 * length of time in seconds during what addresses generated from the prefix remain
4396 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4398 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4399 * are inifinte, no timeout.
4401 * <em>no-advertise</em> - Do not send full router address in prefix
4402 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4404 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4405 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4406 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4408 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4409 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4411 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4412 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4413 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4417 * <b>Format 3: - Default of Prefix:</b>
4419 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4421 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4422 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4423 * is ignored and the prefix is deleted.
4427 * Example of how set a router advertisement option:
4428 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4429 * Example of how to add a prefix:
4430 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4431 * Example of how to delete a prefix:
4432 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4435 VLIB_CLI_COMMAND (ip6_nd_command, static) =
4438 .short_help = "ip6 nd <interface> ...",
4439 .function = ip6_neighbor_cmd,
4444 ip6_neighbor_set_link_local_address (vlib_main_t * vm, u32 sw_if_index,
4445 ip6_address_t * address)
4447 clib_error_t *error = 0;
4448 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4450 ip6_radv_t *radv_info;
4451 vnet_main_t *vnm = vnet_get_main ();
4452 ip6_ll_prefix_t pfx = { 0, };
4454 if (!ip6_address_is_link_local_unicast (address))
4455 return (error = clib_error_return (0, "address not link-local",
4456 format_unformat_error));
4458 /* call enable ipv6 */
4459 enable_ip6_interface (vm, sw_if_index);
4461 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4465 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4467 pfx.ilp_sw_if_index = sw_if_index;
4469 pfx.ilp_addr = radv_info->link_local_address;
4470 ip6_ll_table_entry_delete (&pfx);
4472 pfx.ilp_addr = *address;
4473 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_LOCAL);
4475 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4476 radv_info->link_local_address = *address;
4480 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4481 error = clib_error_return (0, "ip6 not enabled for interface",
4482 format_unformat_error);
4488 * @brief callback when an interface address is added or deleted
4491 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4494 ip6_address_t * address,
4496 u32 if_address_index, u32 is_delete)
4498 vnet_main_t *vnm = vnet_get_main ();
4499 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4501 vlib_main_t *vm = vnm->vlib_main;
4502 ip6_radv_t *radv_info;
4505 /* create solicited node multicast address for this interface adddress */
4506 ip6_set_solicited_node_multicast_address (&a, 0);
4508 a.as_u8[0xd] = address->as_u8[0xd];
4509 a.as_u8[0xe] = address->as_u8[0xe];
4510 a.as_u8[0xf] = address->as_u8[0xf];
4514 /* try to create radv_info - does nothing if ipv6 already enabled */
4515 enable_ip6_interface (vm, sw_if_index);
4517 /* look up the radv_t information for this interface */
4518 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4520 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4524 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4527 if (!ip6_address_is_link_local_unicast (address))
4528 radv_info->ref_count++;
4530 ip6_neighbor_add_mld_prefix (radv_info, &a);
4537 /* look up the radv_t information for this interface */
4538 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4540 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4545 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4547 ip6_neighbor_del_mld_prefix (radv_info, &a);
4549 /* if interface up send MLDP "report" */
4550 radv_info->all_routers_mcast = 0;
4553 if (!ip6_address_is_link_local_unicast (address))
4554 radv_info->ref_count--;
4556 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4557 disable_ip6_interface (vm, sw_if_index);
4562 ip6_set_neighbor_limit (u32 neighbor_limit)
4564 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4566 nm->limit_neighbor_cache_size = neighbor_limit;
4571 ip6_neighbor_table_bind (ip6_main_t * im,
4574 u32 new_fib_index, u32 old_fib_index)
4576 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4577 ip6_neighbor_t *n = NULL;
4578 u32 i, *to_re_add = 0;
4581 pool_foreach (n, nm->neighbor_pool,
4583 if (n->key.sw_if_index == sw_if_index)
4584 vec_add1 (to_re_add, n - nm->neighbor_pool);
4588 for (i = 0; i < vec_len (to_re_add); i++)
4590 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4591 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4592 ip6_neighbor_adj_fib_add (n, new_fib_index);
4594 vec_free (to_re_add);
4597 static clib_error_t *
4598 ip6_neighbor_init (vlib_main_t * vm)
4600 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4601 ip6_main_t *im = &ip6_main;
4603 mhash_init (&nm->neighbor_index_by_key,
4604 /* value size */ sizeof (uword),
4605 /* key size */ sizeof (ip6_neighbor_key_t));
4607 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4608 ip6_icmp_neighbor_solicitation_node.index);
4609 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4610 ip6_icmp_neighbor_advertisement_node.index);
4611 icmp6_register_type (vm, ICMP6_router_solicitation,
4612 ip6_icmp_router_solicitation_node.index);
4613 icmp6_register_type (vm, ICMP6_router_advertisement,
4614 ip6_icmp_router_advertisement_node.index);
4616 /* handler node for ip6 neighbor discovery events and timers */
4617 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4619 /* add call backs */
4620 ip6_add_del_interface_address_callback_t cb;
4621 clib_memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4623 /* when an interface address changes... */
4624 cb.function = ip6_neighbor_add_del_interface_address;
4625 cb.function_opaque = 0;
4626 vec_add1 (im->add_del_interface_address_callbacks, cb);
4628 ip6_table_bind_callback_t cbt;
4629 cbt.function = ip6_neighbor_table_bind;
4630 cbt.function_opaque = 0;
4631 vec_add1 (im->table_bind_callbacks, cbt);
4633 mhash_init (&nm->pending_resolutions_by_address,
4634 /* value size */ sizeof (uword),
4635 /* key size */ sizeof (ip6_address_t));
4637 mhash_init (&nm->mac_changes_by_address,
4638 /* value size */ sizeof (uword),
4639 /* key size */ sizeof (ip6_address_t));
4641 /* default, configurable */
4642 nm->limit_neighbor_cache_size = 50000;
4644 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4646 nm->ip6_ra_publisher_node = (uword) ~ 0;
4649 /* $$$$ Hack fix for today */
4650 vec_validate_init_empty
4651 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
4657 VLIB_INIT_FUNCTION (ip6_neighbor_init);
4661 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4664 uword type_opaque, uword data)
4666 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4667 ip6_address_t *address = address_arg;
4669 pending_resolution_t *pr;
4671 pool_get (nm->pending_resolutions, pr);
4673 pr->next_index = ~0;
4674 pr->node_index = node_index;
4675 pr->type_opaque = type_opaque;
4678 p = mhash_get (&nm->pending_resolutions_by_address, address);
4681 /* Insert new resolution at the head of the list */
4682 pr->next_index = p[0];
4683 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4686 mhash_set (&nm->pending_resolutions_by_address, address,
4687 pr - nm->pending_resolutions, 0 /* old value */ );
4691 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4692 void *data_callback,
4696 uword type_opaque, uword data, int is_add)
4698 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4699 ip6_address_t *address = address_arg;
4701 /* Try to find an existing entry */
4702 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4704 pending_resolution_t *mc;
4705 while (p && *p != ~0)
4707 mc = pool_elt_at_index (nm->mac_changes, *p);
4708 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4711 p = &mc->next_index;
4714 int found = p && *p != ~0;
4718 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4720 pool_get (nm->mac_changes, mc);
4722 *mc = (pending_resolution_t)
4725 .node_index = node_index,
4726 .type_opaque = type_opaque,
4728 .data_callback = data_callback,
4733 /* Insert new resolution at the end of the list */
4734 u32 new_idx = mc - nm->mac_changes;
4738 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
4743 return VNET_API_ERROR_NO_SUCH_ENTRY;
4745 /* Clients may need to clean up pool entries, too */
4746 void (*fp) (u32, u8 *) = data_callback;
4748 (*fp) (mc->data, 0 /* no new mac addrs */ );
4750 /* Remove the entry from the list and delete the entry */
4751 *p = mc->next_index;
4752 pool_put (nm->mac_changes, mc);
4754 /* Remove from hash if we deleted the last entry */
4755 if (*p == ~0 && p == first)
4756 mhash_unset (&nm->mac_changes_by_address, address, 0);
4762 vnet_ip6_nd_term (vlib_main_t * vm,
4763 vlib_node_runtime_t * node,
4765 ethernet_header_t * eth,
4766 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
4768 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4769 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4771 ndh = ip6_next_header (ip);
4772 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4773 ndh->icmp.type != ICMP6_neighbor_advertisement)
4776 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4777 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4779 u8 *t0 = vlib_add_trace (vm, node, p0,
4780 sizeof (icmp6_input_trace_t));
4781 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4784 /* Check if anyone want ND events for L2 BDs */
4786 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4787 && !ip6_address_is_link_local_unicast (&ip->src_address)))
4789 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
4792 /* Check if MAC entry exsist for solicited target IP */
4793 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4795 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4796 l2_bridge_domain_t *bd_config;
4799 opt = (void *) (ndh + 1);
4800 if ((opt->header.type !=
4801 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4802 (opt->header.n_data_u64s != 1))
4803 return 0; /* source link layer address option not present */
4805 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4807 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4809 { /* found ip-mac entry, generate eighbor advertisement response */
4811 vlib_node_runtime_t *error_node =
4812 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4813 ip->dst_address = ip->src_address;
4814 ip->src_address = ndh->target_address;
4815 ip->hop_limit = 255;
4817 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4818 clib_memcpy (opt->ethernet_address, macp, 6);
4819 ndh->icmp.type = ICMP6_neighbor_advertisement;
4820 ndh->advertisement_flags = clib_host_to_net_u32
4821 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4822 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4823 ndh->icmp.checksum = 0;
4824 ndh->icmp.checksum =
4825 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4826 clib_memcpy (eth->dst_address, eth->src_address, 6);
4827 clib_memcpy (eth->src_address, macp, 6);
4828 vlib_error_count (vm, error_node->node_index,
4829 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4839 ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4843 fib_prefix_t pfx = {
4845 .fp_proto = FIB_PROTOCOL_IP6,
4850 ip46_address_t nh = {
4854 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4856 if (~0 == fib_index)
4857 return VNET_API_ERROR_NO_SUCH_FIB;
4861 fib_table_entry_path_remove (fib_index,
4863 FIB_SOURCE_IP6_ND_PROXY,
4867 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4868 /* flush the ND cache of this address if it's there */
4869 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
4873 fib_table_entry_path_add (fib_index,
4875 FIB_SOURCE_IP6_ND_PROXY,
4876 FIB_ENTRY_FLAG_NONE,
4880 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4885 static clib_error_t *
4886 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4887 unformat_input_t * input, vlib_cli_command_t * cmd)
4889 vnet_main_t *vnm = vnet_get_main ();
4890 clib_error_t *error = 0;
4895 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4897 /* get the rest of the command */
4898 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4900 if (unformat (input, "%U", unformat_ip6_address, &addr))
4902 else if (unformat (input, "delete") || unformat (input, "del"))
4905 return (unformat_parse_error (input));
4909 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4915 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4917 .path = "set ip6 nd proxy",
4918 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4919 .function = set_ip6_nd_proxy_cmd,
4924 ethernet_ndp_change_mac (u32 sw_if_index)
4926 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4931 pool_foreach (n, nm->neighbor_pool,
4933 if (n->key.sw_if_index == sw_if_index)
4935 adj_nbr_walk_nh6 (sw_if_index,
4936 &n->key.ip6_address,
4937 ip6_nd_mk_complete_walk, n);
4942 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4944 if (ADJ_INDEX_INVALID != ai)
4945 adj_glean_update_rewrite (ai);
4949 send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
4951 ip6_main_t *i6m = &ip6_main;
4952 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4954 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
4958 send_ip6_na_w_addr (vlib_main_t * vm,
4959 const ip6_address_t * ip6_addr, u32 sw_if_index)
4961 ip6_main_t *i6m = &ip6_main;
4962 vnet_main_t *vnm = vnet_get_main ();
4963 u8 *rewrite, rewrite_len;
4964 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4970 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4971 format_ip6_address, ip6_addr, sw_if_index);
4973 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4976 icmp6_neighbor_solicitation_header_t *h =
4977 vlib_packet_template_get_packet (vm,
4978 &i6m->discover_neighbor_packet_template,
4983 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4984 IP6_MULTICAST_SCOPE_link_local,
4985 IP6_MULTICAST_GROUP_ID_all_hosts);
4986 h->ip.src_address = ip6_addr[0];
4987 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4988 h->neighbor.target_address = ip6_addr[0];
4989 h->neighbor.advertisement_flags = clib_host_to_net_u32
4990 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4991 h->link_layer_option.header.type =
4992 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4993 clib_memcpy (h->link_layer_option.ethernet_address,
4994 hi->hw_address, vec_len (hi->hw_address));
4995 h->neighbor.icmp.checksum =
4996 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4997 ASSERT (bogus_length == 0);
4999 /* Setup MAC header with IP6 Etype and mcast DMAC */
5000 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
5001 ip6_multicast_ethernet_address (dst_address,
5002 IP6_MULTICAST_GROUP_ID_all_hosts);
5004 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
5005 rewrite_len = vec_len (rewrite);
5006 vlib_buffer_advance (b, -rewrite_len);
5007 ethernet_header_t *e = vlib_buffer_get_current (b);
5008 clib_memcpy (e->dst_address, rewrite, rewrite_len);
5011 /* Send unsolicited ND advertisement packet out the specified interface */
5012 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5013 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5014 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5015 u32 *to_next = vlib_frame_vector_args (f);
5018 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5023 * fd.io coding-style-patch-verification: ON
5026 * eval: (c-set-style "gnu")