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>
29 #include <vlibmemory/api.h>
33 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
35 * The files contains the API and CLI code for managing IPv6 neighbor
36 * adjacency tables and neighbor discovery logic.
39 /* can't use sizeof link_layer_address, that's 8 */
40 #define ETHER_MAC_ADDR_LEN 6
42 /* advertised prefix option */
45 /* basic advertised information */
49 int adv_autonomous_flag;
50 u32 adv_valid_lifetime_in_secs;
51 u32 adv_pref_lifetime_in_secs;
53 /* advertised values are computed from these times if decrementing */
54 f64 valid_lifetime_expires;
55 f64 pref_lifetime_expires;
57 /* local information */
59 int deprecated_prefix_flag;
60 int decrement_lifetime_flag;
62 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
63 #define DEF_ADV_VALID_LIFETIME 2592000
64 #define DEF_ADV_PREF_LIFETIME 604800
66 /* extensions are added here, mobile, DNS etc.. */
72 /* group information */
74 ip6_address_t mcast_address;
76 ip6_address_t *mcast_source_address_pool;
79 /* configured router advertisement information per ipv6 interface */
83 /* advertised config information, zero means unspecified */
87 u16 adv_router_lifetime_in_sec;
88 u32 adv_neighbor_reachable_time_in_msec;
89 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
94 /* source link layer option */
95 u8 link_layer_address[8];
96 u8 link_layer_addr_len;
99 ip6_radv_prefix_t *adv_prefixes_pool;
101 /* Hash table mapping address to index in interface advertised prefix pool. */
102 mhash_t address_to_prefix_index;
104 /* MLDP group information */
105 ip6_mldp_group_t *mldp_group_pool;
107 /* Hash table mapping address to index in mldp address pool. */
108 mhash_t address_to_mldp_index;
110 /* local information */
112 int send_radv; /* radv on/off on this interface - set by config */
113 int cease_radv; /* we are ceasing to send - set byf config */
115 int adv_link_layer_address;
117 int failed_device_check;
118 int all_routers_mcast;
122 adj_index_t mcast_adj_index;
124 /* timing information */
125 #define DEF_MAX_RADV_INTERVAL 200
126 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
127 #define DEF_CURR_HOP_LIMIT 64
128 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
129 #define MAX_DEF_RTR_LIFETIME 9000
131 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
132 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
133 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
134 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
135 #define MAX_RA_DELAY_TIME .5 /* seconds */
137 f64 max_radv_interval;
138 f64 min_radv_interval;
139 f64 min_delay_between_radv;
140 f64 max_delay_between_radv;
141 f64 max_rtr_default_lifetime;
144 f64 last_multicast_time;
145 f64 next_multicast_time;
148 u32 initial_adverts_count;
149 f64 initial_adverts_interval;
150 u32 initial_adverts_sent;
153 u32 n_advertisements_sent;
154 u32 n_solicitations_rcvd;
155 u32 n_solicitations_dropped;
157 /* Link local address to use (defaults to underlying physical for logical interfaces */
158 ip6_address_t link_local_address;
160 /* router solicitations sending state */
161 u8 keep_sending_rs; /* when true then next fields are valid */
162 icmp6_send_router_solicitation_params_t params;
167 vlib_buffer_t *buffer;
176 /* Used for nd event notification only */
177 ip6_nd_change_event_cb_t data_callback;
179 } pending_resolution_t;
184 /* Hash tables mapping name to opcode. */
185 uword *opcode_by_name;
187 /* lite beer "glean" adjacency handling */
188 mhash_t pending_resolutions_by_address;
189 pending_resolution_t *pending_resolutions;
191 /* Mac address change notification */
192 mhash_t mac_changes_by_address;
193 pending_resolution_t *mac_changes;
195 u32 *neighbor_input_next_index_by_hw_if_index;
197 ip6_neighbor_t *neighbor_pool;
199 mhash_t neighbor_index_by_key;
201 u32 *if_radv_pool_index_by_sw_if_index;
203 ip6_radv_t *if_radv_pool;
205 /* Neighbor attack mitigation */
206 u32 limit_neighbor_cache_size;
207 u32 neighbor_delete_rotor;
209 /* Wildcard nd report publisher */
210 uword wc_ip6_nd_publisher_node;
211 uword wc_ip6_nd_publisher_et;
213 /* Router advertisement report publisher */
214 uword ip6_ra_publisher_node;
215 uword ip6_ra_publisher_et;
216 } ip6_neighbor_main_t;
218 /* ipv6 neighbor discovery - timer/event types */
222 } ip6_icmp_neighbor_discovery_event_type_t;
232 } ip6_icmp_neighbor_discovery_event_data_t;
234 static ip6_neighbor_main_t ip6_neighbor_main;
235 ip6_neighbor_public_main_t ip6_neighbor_public_main;
236 static ip6_address_t ip6a_zero; /* ip6 address 0 */
238 static void wc_nd_signal_report (wc_nd_report_t * r);
239 static void ra_signal_report (ra_report_t * r);
242 ip6_neighbor_get_link_local_address (u32 sw_if_index)
244 static ip6_address_t empty_address = { {0} };
245 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
246 ip6_radv_t *radv_info;
249 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index)
250 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
253 clib_warning ("IPv6 is not enabled for sw_if_index %d", sw_if_index);
254 return empty_address;
256 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
257 if (radv_info == NULL)
259 clib_warning ("Internal error");
260 return empty_address;
262 return radv_info->link_local_address;
266 * @brief publish wildcard arp event
267 * @param sw_if_index The interface on which the ARP entries are acted
270 vnet_nd_wc_publish (u32 sw_if_index,
271 const mac_address_t * mac, const ip6_address_t * ip6)
274 .sw_if_index = sw_if_index,
279 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
284 wc_nd_signal_report (wc_nd_report_t * r)
286 vlib_main_t *vm = vlib_get_main ();
287 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
288 uword ni = nm->wc_ip6_nd_publisher_node;
289 uword et = nm->wc_ip6_nd_publisher_et;
291 if (ni == (uword) ~ 0)
294 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
300 wc_nd_set_publisher_node (uword node_index, uword event_type)
302 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
303 nm->wc_ip6_nd_publisher_node = node_index;
304 nm->wc_ip6_nd_publisher_et = event_type;
308 ra_publish (ra_report_t * r)
310 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
311 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
316 ra_signal_report (ra_report_t * r)
318 vlib_main_t *vm = vlib_get_main ();
319 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
320 uword ni = nm->ip6_ra_publisher_node;
321 uword et = nm->ip6_ra_publisher_et;
323 if (ni == (uword) ~ 0)
325 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
331 ra_set_publisher_node (uword node_index, uword event_type)
333 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
334 nm->ip6_ra_publisher_node = node_index;
335 nm->ip6_ra_publisher_et = event_type;
339 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
341 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
342 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
343 vnet_main_t *vnm = vnet_get_main ();
344 vnet_sw_interface_t *si;
347 return format (s, "%=12s%=45s%=6s%=20s%=40s", "Time", "Address", "Flags",
348 "Link layer", "Interface");
350 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
352 return format (s, "%=12U%=45U%=6U%=20U%=40U",
353 format_vlib_time, vm, n->time_last_updated,
354 format_ip6_address, &n->key.ip6_address,
355 format_ip_neighbor_flags, n->flags,
356 format_mac_address_t, &n->mac,
357 format_vnet_sw_interface_name, vnm, si);
361 ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
363 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
365 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
367 ip6_ll_prefix_t pfx = {
368 .ilp_addr = n->key.ip6_address,
369 .ilp_sw_if_index = n->key.sw_if_index,
371 ip6_ll_table_entry_delete (&pfx);
377 .fp_proto = FIB_PROTOCOL_IP6,
378 .fp_addr.ip6 = n->key.ip6_address,
380 fib_table_entry_path_remove (fib_index,
385 n->key.sw_if_index, ~0,
386 1, FIB_ROUTE_PATH_FLAG_NONE);
394 ip_neighbor_flags_t flags;
398 } ip6_neighbor_set_unset_rpc_args_t;
400 static void ip6_neighbor_set_unset_rpc_callback
401 (ip6_neighbor_set_unset_rpc_args_t * a);
403 static void set_unset_ip6_neighbor_rpc
406 const ip6_address_t * a,
407 const mac_address_t * mac, int is_add, ip_neighbor_flags_t flags)
409 ip6_neighbor_set_unset_rpc_args_t args;
410 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
412 args.sw_if_index = sw_if_index;
413 args.is_add = is_add;
415 ip6_address_copy (&args.addr, a);
416 mac_address_copy (&args.mac, mac);
418 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
419 (u8 *) & args, sizeof (args));
423 ip6_nbr_probe (ip_adjacency_t * adj)
425 icmp6_neighbor_solicitation_header_t *h;
426 vnet_main_t *vnm = vnet_get_main ();
427 ip6_main_t *im = &ip6_main;
428 ip_interface_address_t *ia;
429 ip6_address_t *dst, *src;
430 vnet_hw_interface_t *hi;
431 vnet_sw_interface_t *si;
437 vm = vlib_get_main ();
439 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
440 dst = &adj->sub_type.nbr.next_hop.ip6;
442 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
446 src = ip6_interface_address_matching_destination (im, dst,
454 h = vlib_packet_template_get_packet (vm,
455 &im->discover_neighbor_packet_template,
460 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
462 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
463 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
464 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
465 h->ip.src_address = src[0];
466 h->neighbor.target_address = dst[0];
468 clib_memcpy (h->link_layer_option.ethernet_address,
469 hi->hw_address, vec_len (hi->hw_address));
471 h->neighbor.icmp.checksum =
472 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
473 ASSERT (bogus_length == 0);
475 b = vlib_get_buffer (vm, bi);
476 vnet_buffer (b)->sw_if_index[VLIB_RX] =
477 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
479 /* Add encapsulation string for software interface (e.g. ethernet header). */
480 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
481 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
484 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
485 u32 *to_next = vlib_frame_vector_args (f);
488 vlib_put_frame_to_node (vm, hi->output_node_index, f);
493 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
495 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
496 ethernet_build_rewrite (vnet_get_main (),
497 nbr->key.sw_if_index,
498 adj_get_link_type (ai),
503 ip6_nd_mk_incomplete (adj_index_t ai)
505 ip_adjacency_t *adj = adj_get (ai);
507 adj_nbr_update_rewrite (ai,
508 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
509 ethernet_build_rewrite (vnet_get_main (),
512 adj_get_link_type (ai),
513 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
516 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
518 k.sw_if_index = sw_if_index; \
519 k.ip6_address = *addr; \
523 static ip6_neighbor_t *
524 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
526 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
527 ip6_neighbor_t *n = NULL;
528 ip6_neighbor_key_t k;
531 IP6_NBR_MK_KEY (k, sw_if_index, addr);
533 p = mhash_get (&nm->neighbor_index_by_key, &k);
536 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
543 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
545 ip6_neighbor_t *nbr = ctx;
547 ip6_nd_mk_complete (ai, nbr);
549 return (ADJ_WALK_RC_CONTINUE);
553 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
555 ip6_nd_mk_incomplete (ai);
557 return (ADJ_WALK_RC_CONTINUE);
560 static clib_error_t *
561 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
562 u32 sw_if_index, u32 flags)
564 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
566 u32 i, *to_delete = 0;
569 pool_foreach (n, nm->neighbor_pool,
571 if (n->key.sw_if_index == sw_if_index)
572 vec_add1 (to_delete, n - nm->neighbor_pool);
576 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
578 for (i = 0; i < vec_len (to_delete); i++)
580 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
581 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
582 ip6_nd_mk_complete_walk, n);
587 for (i = 0; i < vec_len (to_delete); i++)
589 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
590 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
591 ip6_nd_mk_incomplete_walk, NULL);
592 if (n->flags & IP_NEIGHBOR_FLAG_STATIC)
594 ip6_neighbor_adj_fib_remove (n,
595 ip6_fib_table_get_index_for_sw_if_index
596 (n->key.sw_if_index));
597 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
598 pool_put (nm->neighbor_pool, n);
602 vec_free (to_delete);
606 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
609 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
616 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
618 switch (adj->lookup_next_index)
620 case IP_LOOKUP_NEXT_GLEAN:
621 adj_glean_update_rewrite (ai);
623 case IP_LOOKUP_NEXT_ARP:
626 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
627 ip6_nd_mk_complete_walk, nbr);
632 * no matching ND entry.
633 * construct the rewrite required to for an ND packet, and stick
634 * that in the adj's pipe to smoke.
636 adj_nbr_update_rewrite (ai,
637 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
638 ethernet_build_rewrite (vnm,
641 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
644 * since the FIB has added this adj for a route, it makes sense it may
645 * want to forward traffic sometime soon. Let's send a speculative ND.
646 * just one. If we were to do periodically that wouldn't be bad either,
647 * but that's more code than i'm prepared to write at this time for
648 * relatively little reward.
653 case IP_LOOKUP_NEXT_BCAST:
654 adj_nbr_update_rewrite (ai,
655 ADJ_NBR_REWRITE_FLAG_COMPLETE,
656 ethernet_build_rewrite (vnm,
659 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
661 case IP_LOOKUP_NEXT_MCAST:
664 * Construct a partial rewrite from the known ethernet mcast dest MAC
669 rewrite = ethernet_build_rewrite (vnm,
672 ethernet_ip6_mcast_dst_addr ());
675 * Complete the remaining fields of the adj's rewrite to direct the
676 * complete of the rewrite at switch time by copying in the IP
677 * dst address's bytes.
678 * Ofset is 2 bytes into the destintation address.
680 offset = vec_len (rewrite) - 2;
681 adj_mcast_update_rewrite (ai, rewrite, offset);
685 case IP_LOOKUP_NEXT_DROP:
686 case IP_LOOKUP_NEXT_PUNT:
687 case IP_LOOKUP_NEXT_LOCAL:
688 case IP_LOOKUP_NEXT_REWRITE:
689 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
690 case IP_LOOKUP_NEXT_MIDCHAIN:
691 case IP_LOOKUP_NEXT_ICMP_ERROR:
692 case IP_LOOKUP_N_NEXT:
700 ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
702 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
704 ip6_ll_prefix_t pfx = {
705 .ilp_addr = n->key.ip6_address,
706 .ilp_sw_if_index = n->key.sw_if_index,
709 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
715 .fp_proto = FIB_PROTOCOL_IP6,
716 .fp_addr.ip6 = n->key.ip6_address,
720 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
721 FIB_ENTRY_FLAG_ATTACHED,
722 DPO_PROTO_IP6, &pfx.fp_addr,
723 n->key.sw_if_index, ~0, 1, NULL,
724 FIB_ROUTE_PATH_FLAG_NONE);
728 static ip6_neighbor_t *
729 force_reuse_neighbor_entry (void)
732 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
734 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
735 if (index == ~0) /* Try again from elt 0 */
736 index = pool_next_index (nm->neighbor_pool, index);
738 /* Find a non-static random entry to free up for reuse */
741 if ((count++ == 100) || (index == ~0))
742 return NULL; /* give up after 100 entries */
743 n = pool_elt_at_index (nm->neighbor_pool, index);
744 nm->neighbor_delete_rotor = index;
745 index = pool_next_index (nm->neighbor_pool, index);
747 while (n->flags & IP_NEIGHBOR_FLAG_STATIC);
749 /* Remove ARP entry from its interface and update fib */
750 adj_nbr_walk_nh6 (n->key.sw_if_index,
751 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
752 ip6_neighbor_adj_fib_remove
753 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
754 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
760 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
762 const ip6_address_t * a,
763 const mac_address_t * mac,
764 ip_neighbor_flags_t flags)
766 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
767 ip6_neighbor_key_t k;
768 ip6_neighbor_t *n = 0;
769 int make_new_nd_cache_entry = 1;
772 pending_resolution_t *pr, *mc;
774 if (vlib_get_thread_index ())
776 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, mac, 1, flags);
780 k.sw_if_index = sw_if_index;
781 k.ip6_address = a[0];
784 p = mhash_get (&nm->neighbor_index_by_key, &k);
787 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
788 /* Refuse to over-write static neighbor entry. */
789 if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
790 (n->flags & IP_NEIGHBOR_FLAG_STATIC))
792 /* if MAC address match, still check to send event */
793 if (0 == mac_address_cmp (&n->mac, mac))
794 goto check_customers;
797 make_new_nd_cache_entry = 0;
800 if (make_new_nd_cache_entry)
802 if (nm->limit_neighbor_cache_size &&
803 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
805 n = force_reuse_neighbor_entry ();
810 pool_get (nm->neighbor_pool, n);
812 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
815 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
817 mac_address_copy (&n->mac, mac);
820 * create the adj-fib. the entry in the FIB table for and to the peer.
822 if (!(flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY))
824 ip6_neighbor_adj_fib_add
825 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
829 n->flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
835 * prevent a DoS attack from the data-plane that
836 * spams us with no-op updates to the MAC address
838 if (0 == mac_address_cmp (&n->mac, mac))
840 n->time_last_updated = vlib_time_now (vm);
841 goto check_customers;
844 mac_address_copy (&n->mac, mac);
847 /* Update time stamp and flags. */
848 n->time_last_updated = vlib_time_now (vm);
849 if (flags & IP_NEIGHBOR_FLAG_STATIC)
851 n->flags |= IP_NEIGHBOR_FLAG_STATIC;
852 n->flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC;
856 n->flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
857 n->flags &= ~IP_NEIGHBOR_FLAG_STATIC;
860 adj_nbr_walk_nh6 (sw_if_index,
861 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
864 /* Customer(s) waiting for this address to be resolved? */
865 p = mhash_get (&nm->pending_resolutions_by_address, a);
870 while (next_index != (u32) ~ 0)
872 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
873 vlib_process_signal_event (vm, pr->node_index,
874 pr->type_opaque, pr->data);
875 next_index = pr->next_index;
876 pool_put (nm->pending_resolutions, pr);
879 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
882 /* Customer(s) requesting ND event for this address? */
883 p = mhash_get (&nm->mac_changes_by_address, a);
888 while (next_index != (u32) ~ 0)
892 mc = pool_elt_at_index (nm->mac_changes, next_index);
894 /* Call the user's data callback, return 1 to suppress dup events */
895 if (mc->data_callback)
896 rv = (mc->data_callback) (mc->data, mac, sw_if_index, &ip6a_zero);
898 * Signal the resolver process, as long as the user
899 * says they want to be notified
902 vlib_process_signal_event (vm, mc->node_index,
903 mc->type_opaque, mc->data);
904 next_index = mc->next_index;
912 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
913 u32 sw_if_index, const ip6_address_t * a)
915 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
916 ip6_neighbor_key_t k;
921 if (vlib_get_thread_index ())
923 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL, 0,
924 IP_NEIGHBOR_FLAG_NONE);
928 k.sw_if_index = sw_if_index;
929 k.ip6_address = a[0];
932 p = mhash_get (&nm->neighbor_index_by_key, &k);
939 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
941 adj_nbr_walk_nh6 (sw_if_index,
942 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
943 ip6_neighbor_adj_fib_remove
944 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
946 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
947 pool_put (nm->neighbor_pool, n);
953 static void ip6_neighbor_set_unset_rpc_callback
954 (ip6_neighbor_set_unset_rpc_args_t * a)
956 vlib_main_t *vm = vlib_get_main ();
958 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
961 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
965 ip6_neighbor_sort (void *a1, void *a2)
967 vnet_main_t *vnm = vnet_get_main ();
968 ip6_neighbor_t *n1 = a1, *n2 = a2;
970 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
971 n2->key.sw_if_index);
973 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
978 ip6_neighbors_pool (void)
980 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
981 return nm->neighbor_pool;
985 ip6_neighbors_entries (u32 sw_if_index)
987 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
988 ip6_neighbor_t *n, *ns = NULL;
991 pool_foreach (n, nm->neighbor_pool,
993 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
1000 vec_sort_with_function (ns, ip6_neighbor_sort);
1004 static clib_error_t *
1005 show_ip6_neighbors (vlib_main_t * vm,
1006 unformat_input_t * input, vlib_cli_command_t * cmd)
1008 vnet_main_t *vnm = vnet_get_main ();
1009 ip6_neighbor_t *n, *ns;
1010 clib_error_t *error = 0;
1014 if (unformat (input, "verbose"))
1017 /* Filter entries by interface if given. */
1019 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1021 ns = ip6_neighbors_entries (sw_if_index);
1025 * Show the entire table if it's not too big, otherwise just
1026 * show the size of the table.
1028 if (vec_len (ns) < 50)
1032 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
1035 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
1040 (vm, "There are %u ip6 neighbors, "
1041 "'show ip6 neighbors verbose' to display the entire table...",
1046 vlib_cli_output (vm, "No ip6 neighbors");
1052 * This command is used to display the adjacent IPv6 hosts found via
1053 * neighbor discovery. Optionally, limit the output to the specified
1057 * Example of how to display the IPv6 neighbor adjacency table:
1058 * @cliexstart{show ip6 neighbors}
1059 * Time Address Flags Link layer Interface
1060 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1061 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1062 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1064 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1065 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1066 * Time Address Flags Link layer Interface
1067 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1068 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1072 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1073 .path = "show ip6 neighbors",
1074 .function = show_ip6_neighbors,
1075 .short_help = "show ip6 neighbors [<interface>]",
1079 static clib_error_t *
1080 set_ip6_neighbor (vlib_main_t * vm,
1081 unformat_input_t * input, vlib_cli_command_t * cmd)
1083 ip_neighbor_flags_t flags = IP_NEIGHBOR_FLAG_NONE;
1084 vnet_main_t *vnm = vnet_get_main ();
1091 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1093 /* intfc, ip6-address, mac-address */
1094 if (unformat (input, "%U %U %U",
1095 unformat_vnet_sw_interface, vnm, &sw_if_index,
1096 unformat_ip6_address, &addr,
1097 unformat_mac_address_t, &mac))
1100 else if (unformat (input, "delete") || unformat (input, "del"))
1102 else if (unformat (input, "static"))
1103 flags |= IP_NEIGHBOR_FLAG_STATIC;
1104 else if (unformat (input, "no-fib-entry"))
1105 flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
1111 return clib_error_return (0, "Missing interface, ip6 or hw address");
1114 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr, &mac, flags);
1116 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
1121 * This command is used to manually add an entry to the IPv6 neighbor
1122 * adjacency table. Optionally, the entry can be added as static. It is
1123 * also used to remove an entry from the table. Use the '<em>show ip6
1124 * neighbors</em>' command to display all learned and manually entered entries.
1127 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1128 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1129 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1130 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1133 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1135 .path = "set ip6 neighbor",
1136 .function = set_ip6_neighbor,
1137 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
1143 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1144 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1145 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1146 } icmp6_neighbor_solicitation_or_advertisement_next_t;
1148 static_always_inline uword
1149 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1150 vlib_node_runtime_t * node,
1151 vlib_frame_t * frame,
1152 uword is_solicitation)
1154 vnet_main_t *vnm = vnet_get_main ();
1155 ip6_main_t *im = &ip6_main;
1156 uword n_packets = frame->n_vectors;
1157 u32 *from, *to_next;
1158 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1159 icmp6_neighbor_discovery_option_type_t option_type;
1160 vlib_node_runtime_t *error_node =
1161 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1164 from = vlib_frame_vector_args (frame);
1165 n_left_from = n_packets;
1166 next_index = node->cached_next_index;
1168 if (node->flags & VLIB_NODE_FLAG_TRACE)
1169 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1171 sizeof (icmp6_input_trace_t));
1175 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1176 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1177 n_advertisements_sent = 0;
1179 while (n_left_from > 0)
1181 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1183 while (n_left_from > 0 && n_left_to_next > 0)
1187 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1188 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1189 u32 bi0, options_len0, sw_if_index0, next0, error0;
1190 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1194 bi0 = to_next[0] = from[0];
1199 n_left_to_next -= 1;
1201 p0 = vlib_get_buffer (vm, bi0);
1202 ip0 = vlib_buffer_get_current (p0);
1203 h0 = ip6_next_header (ip0);
1205 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1207 error0 = ICMP6_ERROR_NONE;
1208 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1209 ip6_sadd_link_local =
1210 ip6_address_is_link_local_unicast (&ip0->src_address);
1211 ip6_sadd_unspecified =
1212 ip6_address_is_unspecified (&ip0->src_address);
1214 /* Check that source address is unspecified, link-local or else on-link. */
1215 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1217 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1219 if (ADJ_INDEX_INVALID != src_adj_index0)
1221 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1223 /* Allow all realistic-looking rewrite adjacencies to pass */
1224 ni0 = adj0->lookup_next_index;
1225 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1226 (ni0 < IP6_LOOKUP_N_NEXT);
1228 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1231 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1237 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1241 o0 = (void *) (h0 + 1);
1242 o0 = ((options_len0 == 8 && o0->header.type == option_type
1243 && o0->header.n_data_u64s == 1) ? o0 : 0);
1245 /* If src address unspecified or link local, donot learn neighbor MAC */
1246 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1247 !ip6_sadd_unspecified))
1249 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1252 &h0->target_address,
1254 o0->ethernet_address,
1255 IP_NEIGHBOR_FLAG_NONE);
1258 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1260 /* Check that target address is local to this router. */
1261 fib_node_index_t fei;
1265 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1267 if (~0 == fib_index)
1269 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1273 if (ip6_address_is_link_local_unicast (&h0->target_address))
1275 fei = ip6_fib_table_lookup_exact_match
1276 (ip6_ll_fib_get (sw_if_index0),
1277 &h0->target_address, 128);
1281 fei = ip6_fib_table_lookup_exact_match (fib_index,
1282 &h0->target_address,
1286 if (FIB_NODE_INDEX_INVALID == fei)
1288 /* The target address is not in the FIB */
1290 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1294 if (FIB_ENTRY_FLAG_LOCAL &
1295 fib_entry_get_flags_for_source (fei,
1296 FIB_SOURCE_INTERFACE))
1298 /* It's an address that belongs to one of our interfaces
1302 if (fib_entry_is_sourced
1303 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1304 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
1306 /* The address was added by IPv6 Proxy ND config.
1307 * We should only respond to these if the NS arrived on
1308 * the link that has a matching covering prefix */
1313 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1319 if (is_solicitation)
1320 next0 = (error0 != ICMP6_ERROR_NONE
1321 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1322 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1326 error0 = error0 == ICMP6_ERROR_NONE ?
1327 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1330 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1332 vnet_sw_interface_t *sw_if0;
1333 ethernet_interface_t *eth_if0;
1334 ethernet_header_t *eth0;
1336 /* dst address is either source address or the all-nodes mcast addr */
1337 if (!ip6_sadd_unspecified)
1338 ip0->dst_address = ip0->src_address;
1340 ip6_set_reserved_multicast_address (&ip0->dst_address,
1341 IP6_MULTICAST_SCOPE_link_local,
1342 IP6_MULTICAST_GROUP_ID_all_hosts);
1344 ip0->src_address = h0->target_address;
1345 ip0->hop_limit = 255;
1346 h0->icmp.type = ICMP6_neighbor_advertisement;
1348 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1349 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1351 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1354 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1356 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1359 h0->advertisement_flags = clib_host_to_net_u32
1360 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1361 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1363 h0->icmp.checksum = 0;
1365 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1367 ASSERT (bogus_length == 0);
1369 /* Reuse current MAC header, copy SMAC to DMAC and
1370 * interface MAC to SMAC */
1371 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1372 eth0 = vlib_buffer_get_current (p0);
1373 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1375 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1377 /* Setup input and output sw_if_index for packet */
1378 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1379 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1380 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1381 vnet_main.local_interface_sw_if_index;
1383 n_advertisements_sent++;
1386 p0->error = error_node->errors[error0];
1388 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1389 to_next, n_left_to_next,
1393 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1396 /* Account for advertisements sent. */
1397 vlib_error_count (vm, error_node->node_index,
1398 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1399 n_advertisements_sent);
1401 return frame->n_vectors;
1404 /* for "syslogging" - use elog for now */
1405 #define foreach_log_level \
1406 _ (DEBUG, "DEBUG") \
1407 _ (INFO, "INFORMATION") \
1408 _ (NOTICE, "NOTICE") \
1409 _ (WARNING, "WARNING") \
1411 _ (CRIT, "CRITICAL") \
1412 _ (ALERT, "ALERT") \
1413 _ (EMERG, "EMERGENCY")
1417 #define _(f,s) LOG_##f,
1422 static char *log_level_strings[] = {
1428 static int logmask = 1 << LOG_DEBUG;
1431 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1433 /* just use elog for now */
1437 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1443 what = va_format (0, fmt, &va);
1445 ELOG_TYPE_DECLARE (e) =
1447 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1452 ed = ELOG_DATA (&vm->elog_main, e);
1453 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1454 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1461 call_ip6_neighbor_callbacks (void *data,
1462 _vnet_ip6_neighbor_function_list_elt_t * elt)
1464 clib_error_t *error = 0;
1468 error = elt->fp (data);
1471 elt = elt->next_ip6_neighbor_function;
1477 /* ipv6 neighbor discovery - router advertisements */
1480 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1481 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1482 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1483 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1484 } icmp6_router_solicitation_or_advertisement_next_t;
1486 static_always_inline uword
1487 icmp6_router_solicitation (vlib_main_t * vm,
1488 vlib_node_runtime_t * node, vlib_frame_t * frame)
1490 vnet_main_t *vnm = vnet_get_main ();
1491 ip6_main_t *im = &ip6_main;
1492 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1493 uword n_packets = frame->n_vectors;
1494 u32 *from, *to_next;
1495 u32 n_left_from, n_left_to_next, next_index;
1496 u32 n_advertisements_sent = 0;
1499 icmp6_neighbor_discovery_option_type_t option_type;
1501 vlib_node_runtime_t *error_node =
1502 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1504 from = vlib_frame_vector_args (frame);
1505 n_left_from = n_packets;
1506 next_index = node->cached_next_index;
1508 if (node->flags & VLIB_NODE_FLAG_TRACE)
1509 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1511 sizeof (icmp6_input_trace_t));
1513 /* source may append his LL address */
1514 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1516 while (n_left_from > 0)
1518 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1520 while (n_left_from > 0 && n_left_to_next > 0)
1524 ip6_radv_t *radv_info = 0;
1526 icmp6_neighbor_discovery_header_t *h0;
1527 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1529 u32 bi0, options_len0, sw_if_index0, next0, error0;
1530 u32 is_solicitation = 1, is_dropped = 0;
1531 u32 is_unspecified, is_link_local;
1533 bi0 = to_next[0] = from[0];
1538 n_left_to_next -= 1;
1540 p0 = vlib_get_buffer (vm, bi0);
1541 ip0 = vlib_buffer_get_current (p0);
1542 h0 = ip6_next_header (ip0);
1544 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1545 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1547 ip6_address_is_link_local_unicast (&ip0->src_address);
1549 error0 = ICMP6_ERROR_NONE;
1550 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1552 /* check if solicitation (not from nd_timer node) */
1553 if (ip6_address_is_unspecified (&ip0->dst_address))
1554 is_solicitation = 0;
1556 /* Check that source address is unspecified, link-local or else on-link. */
1557 if (!is_unspecified && !is_link_local)
1559 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1561 if (ADJ_INDEX_INVALID != src_adj_index0)
1563 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1565 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1567 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1572 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1576 /* check for source LL option and process */
1577 o0 = (void *) (h0 + 1);
1578 o0 = ((options_len0 == 8
1579 && o0->header.type == option_type
1580 && o0->header.n_data_u64s == 1) ? o0 : 0);
1582 /* if src address unspecified IGNORE any options */
1583 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1584 !is_unspecified && !is_link_local))
1586 vnet_set_ip6_ethernet_neighbor
1589 (mac_address_t *) o0->ethernet_address,
1590 IP_NEIGHBOR_FLAG_NONE);
1593 /* default is to drop */
1594 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1596 if (error0 == ICMP6_ERROR_NONE)
1598 vnet_sw_interface_t *sw_if0;
1599 ethernet_interface_t *eth_if0;
1602 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1603 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1605 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1607 /* only support ethernet interface type for now */
1609 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1612 if (error0 == ICMP6_ERROR_NONE)
1616 /* adjust the sizeof the buffer to just include the ipv6 header */
1617 p0->current_length -=
1619 sizeof (icmp6_neighbor_discovery_header_t));
1621 /* look up the radv_t information for this interface */
1622 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1626 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1629 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1634 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1637 if (error0 == ICMP6_ERROR_NONE)
1639 f64 now = vlib_time_now (vm);
1641 /* for solicited adverts - need to rate limit */
1642 if (is_solicitation)
1644 if (0 != radv_info->last_radv_time &&
1645 (now - radv_info->last_radv_time) <
1646 MIN_DELAY_BETWEEN_RAS)
1649 radv_info->last_radv_time = now;
1653 icmp6_router_advertisement_header_t rh;
1655 rh.icmp.type = ICMP6_router_advertisement;
1657 rh.icmp.checksum = 0;
1659 rh.current_hop_limit = radv_info->curr_hop_limit;
1660 rh.router_lifetime_in_sec =
1661 clib_host_to_net_u16
1662 (radv_info->adv_router_lifetime_in_sec);
1664 time_in_msec_between_retransmitted_neighbor_solicitations
1666 clib_host_to_net_u32 (radv_info->
1667 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1668 rh.neighbor_reachable_time_in_msec =
1669 clib_host_to_net_u32 (radv_info->
1670 adv_neighbor_reachable_time_in_msec);
1673 (radv_info->adv_managed_flag) ?
1674 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1677 ((radv_info->adv_other_flag) ?
1678 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1682 u16 payload_length =
1683 sizeof (icmp6_router_advertisement_header_t);
1685 if (vlib_buffer_add_data
1686 (vm, &bi0, (void *) &rh,
1687 sizeof (icmp6_router_advertisement_header_t)))
1689 /* buffer allocation failed, drop the pkt */
1690 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1694 if (radv_info->adv_link_layer_address)
1696 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1700 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1701 h.header.n_data_u64s = 1;
1703 /* copy ll address */
1704 clib_memcpy (&h.ethernet_address[0],
1705 eth_if0->address, 6);
1707 if (vlib_buffer_add_data
1708 (vm, &bi0, (void *) &h,
1710 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
1712 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1718 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1721 /* add MTU option */
1722 if (radv_info->adv_link_mtu)
1724 icmp6_neighbor_discovery_mtu_option_t h;
1728 clib_host_to_net_u32 (radv_info->adv_link_mtu);
1729 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1730 h.header.n_data_u64s = 1;
1733 sizeof (icmp6_neighbor_discovery_mtu_option_t);
1735 if (vlib_buffer_add_data
1736 (vm, &bi0, (void *) &h,
1738 (icmp6_neighbor_discovery_mtu_option_t)))
1740 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1745 /* add advertised prefix options */
1746 ip6_radv_prefix_t *pr_info;
1749 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1751 if(pr_info->enabled &&
1752 (!pr_info->decrement_lifetime_flag
1753 || (pr_info->pref_lifetime_expires >0)))
1755 /* advertise this prefix */
1756 icmp6_neighbor_discovery_prefix_information_option_t h;
1758 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1759 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1761 h.dst_address_length = pr_info->prefix_len;
1763 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1764 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1766 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1768 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1769 h.preferred_time = 0;
1773 if(pr_info->decrement_lifetime_flag)
1775 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1776 (pr_info->valid_lifetime_expires - now) : 0;
1778 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1779 (pr_info->pref_lifetime_expires - now) : 0;
1782 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1783 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1787 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1789 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1791 if (vlib_buffer_add_data
1792 (vm, &bi0, (void *)&h,
1793 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
1795 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1803 /* add additional options before here */
1805 /* finish building the router advertisement... */
1806 if (!is_unspecified && radv_info->send_unicast)
1808 ip0->dst_address = ip0->src_address;
1812 /* target address is all-nodes mcast addr */
1813 ip6_set_reserved_multicast_address
1815 IP6_MULTICAST_SCOPE_link_local,
1816 IP6_MULTICAST_GROUP_ID_all_hosts);
1819 /* source address MUST be the link-local address */
1820 ip0->src_address = radv_info->link_local_address;
1822 ip0->hop_limit = 255;
1823 ip0->payload_length =
1824 clib_host_to_net_u16 (payload_length);
1826 icmp6_router_advertisement_header_t *rh0 =
1827 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1828 rh0->icmp.checksum =
1829 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1831 ASSERT (bogus_length == 0);
1833 /* setup output if and adjacency */
1834 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1835 vnet_main.local_interface_sw_if_index;
1837 if (is_solicitation)
1839 ethernet_header_t *eth0;
1840 /* Reuse current MAC header, copy SMAC to DMAC and
1841 * interface MAC to SMAC */
1842 vlib_buffer_reset (p0);
1843 eth0 = vlib_buffer_get_current (p0);
1844 clib_memcpy (eth0->dst_address, eth0->src_address,
1846 clib_memcpy (eth0->src_address, eth_if0->address,
1849 is_dropped ? next0 :
1850 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1851 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1856 adj_index0 = radv_info->mcast_adj_index;
1857 if (adj_index0 == 0)
1858 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1862 is_dropped ? next0 :
1863 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1864 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1868 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1870 radv_info->n_solicitations_dropped += is_dropped;
1871 radv_info->n_solicitations_rcvd += is_solicitation;
1873 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1875 radv_info->n_advertisements_sent++;
1876 n_advertisements_sent++;
1883 p0->error = error_node->errors[error0];
1885 if (error0 != ICMP6_ERROR_NONE)
1886 vlib_error_count (vm, error_node->node_index, error0, 1);
1888 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1889 to_next, n_left_to_next,
1894 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1897 /* Account for router advertisements sent. */
1898 vlib_error_count (vm, error_node->node_index,
1899 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1900 n_advertisements_sent);
1902 return frame->n_vectors;
1905 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1906 static_always_inline uword
1907 icmp6_router_advertisement (vlib_main_t * vm,
1908 vlib_node_runtime_t * node, vlib_frame_t * frame)
1910 vnet_main_t *vnm = vnet_get_main ();
1911 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1912 uword n_packets = frame->n_vectors;
1913 u32 *from, *to_next;
1914 u32 n_left_from, n_left_to_next, next_index;
1915 u32 n_advertisements_rcvd = 0;
1917 vlib_node_runtime_t *error_node =
1918 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1920 from = vlib_frame_vector_args (frame);
1921 n_left_from = n_packets;
1922 next_index = node->cached_next_index;
1924 if (node->flags & VLIB_NODE_FLAG_TRACE)
1925 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1927 sizeof (icmp6_input_trace_t));
1929 while (n_left_from > 0)
1931 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1933 while (n_left_from > 0 && n_left_to_next > 0)
1937 ip6_radv_t *radv_info = 0;
1938 icmp6_router_advertisement_header_t *h0;
1939 u32 bi0, options_len0, sw_if_index0, next0, error0;
1941 bi0 = to_next[0] = from[0];
1946 n_left_to_next -= 1;
1948 p0 = vlib_get_buffer (vm, bi0);
1949 ip0 = vlib_buffer_get_current (p0);
1950 h0 = ip6_next_header (ip0);
1952 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1954 error0 = ICMP6_ERROR_NONE;
1955 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1957 /* Check that source address is link-local */
1958 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1959 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1961 /* default is to drop */
1962 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1964 n_advertisements_rcvd++;
1966 if (error0 == ICMP6_ERROR_NONE)
1968 vnet_sw_interface_t *sw_if0;
1969 ethernet_interface_t *eth_if0;
1971 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1972 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1974 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1976 /* only support ethernet interface type for now */
1978 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1981 if (error0 == ICMP6_ERROR_NONE)
1985 /* look up the radv_t information for this interface */
1986 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1990 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1993 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1998 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
2001 if (error0 == ICMP6_ERROR_NONE)
2003 radv_info->keep_sending_rs = 0;
2007 r.sw_if_index = sw_if_index0;
2008 memcpy (&r.router_address, &ip0->src_address, 16);
2009 r.current_hop_limit = h0->current_hop_limit;
2010 r.flags = h0->flags;
2011 r.router_lifetime_in_sec =
2012 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2013 r.neighbor_reachable_time_in_msec =
2014 clib_net_to_host_u32
2015 (h0->neighbor_reachable_time_in_msec);
2016 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2019 /* validate advertised information */
2020 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2021 && (h0->current_hop_limit !=
2022 radv_info->curr_hop_limit))
2024 ip6_neighbor_syslog (vm, LOG_WARNING,
2025 "our AdvCurHopLimit on %U doesn't agree with %U",
2026 format_vnet_sw_if_index_name,
2033 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2034 != radv_info->adv_managed_flag)
2036 ip6_neighbor_syslog (vm, LOG_WARNING,
2037 "our AdvManagedFlag on %U doesn't agree with %U",
2038 format_vnet_sw_if_index_name,
2045 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2046 != radv_info->adv_other_flag)
2048 ip6_neighbor_syslog (vm, LOG_WARNING,
2049 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2050 format_vnet_sw_if_index_name,
2057 time_in_msec_between_retransmitted_neighbor_solicitations
2059 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2061 time_in_msec_between_retransmitted_neighbor_solicitations
2063 clib_host_to_net_u32 (radv_info->
2064 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
2066 ip6_neighbor_syslog (vm, LOG_WARNING,
2067 "our AdvRetransTimer on %U doesn't agree with %U",
2068 format_vnet_sw_if_index_name,
2074 if ((h0->neighbor_reachable_time_in_msec &&
2075 radv_info->adv_neighbor_reachable_time_in_msec) &&
2076 (h0->neighbor_reachable_time_in_msec !=
2077 clib_host_to_net_u32
2078 (radv_info->adv_neighbor_reachable_time_in_msec)))
2080 ip6_neighbor_syslog (vm, LOG_WARNING,
2081 "our AdvReachableTime on %U doesn't agree with %U",
2082 format_vnet_sw_if_index_name,
2088 /* check for MTU or prefix options or .. */
2089 u8 *opt_hdr = (u8 *) (h0 + 1);
2090 while (options_len0 > 0)
2092 icmp6_neighbor_discovery_option_header_t *o0 =
2093 (icmp6_neighbor_discovery_option_header_t *)
2095 int opt_len = o0->n_data_u64s << 3;
2096 icmp6_neighbor_discovery_option_type_t option_type =
2099 if (options_len0 < 2)
2101 ip6_neighbor_syslog (vm, LOG_ERR,
2102 "malformed RA packet on %U from %U",
2103 format_vnet_sw_if_index_name,
2112 ip6_neighbor_syslog (vm, LOG_ERR,
2113 " zero length option in RA on %U from %U",
2114 format_vnet_sw_if_index_name,
2120 else if (opt_len > options_len0)
2122 ip6_neighbor_syslog (vm, LOG_ERR,
2123 "option length in RA packet greater than total length on %U from %U",
2124 format_vnet_sw_if_index_name,
2131 options_len0 -= opt_len;
2134 switch (option_type)
2136 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2138 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2140 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2143 if (opt_len < sizeof (*h))
2146 memcpy (r.slla, h->ethernet_address, 6);
2150 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
2152 icmp6_neighbor_discovery_mtu_option_t *h =
2153 (icmp6_neighbor_discovery_mtu_option_t
2156 if (opt_len < sizeof (*h))
2159 r.mtu = clib_net_to_host_u32 (h->mtu);
2161 if ((h->mtu && radv_info->adv_link_mtu) &&
2163 clib_host_to_net_u32
2164 (radv_info->adv_link_mtu)))
2166 ip6_neighbor_syslog (vm, LOG_WARNING,
2167 "our AdvLinkMTU on %U doesn't agree with %U",
2168 format_vnet_sw_if_index_name,
2176 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2178 icmp6_neighbor_discovery_prefix_information_option_t
2180 (icmp6_neighbor_discovery_prefix_information_option_t
2183 /* validate advertised prefix options */
2184 ip6_radv_prefix_t *pr_info;
2185 u32 preferred, valid;
2187 if (opt_len < sizeof (*h))
2190 vec_validate (r.prefixes,
2191 vec_len (r.prefixes));
2192 ra_report_prefix_info_t *prefix =
2193 vec_elt_at_index (r.prefixes,
2194 vec_len (r.prefixes) - 1);
2197 clib_net_to_host_u32 (h->preferred_time);
2198 valid = clib_net_to_host_u32 (h->valid_time);
2200 prefix->preferred_time = preferred;
2201 prefix->valid_time = valid;
2202 prefix->flags = h->flags & 0xc0;
2203 prefix->prefix.fp_len = h->dst_address_length;
2204 prefix->prefix.fp_addr.ip6 = h->dst_address;
2205 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
2207 /* look for matching prefix - if we our advertising it, it better be consistant */
2209 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2213 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2215 if(pr_info->enabled &&
2216 (pr_info->prefix_len == h->dst_address_length) &&
2217 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2220 if(!pr_info->decrement_lifetime_flag &&
2221 valid != pr_info->adv_valid_lifetime_in_secs)
2223 ip6_neighbor_syslog(vm, LOG_WARNING,
2224 "our ADV validlifetime on %U for %U does not agree with %U",
2225 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2226 format_ip6_address, &h->dst_address);
2228 if(!pr_info->decrement_lifetime_flag &&
2229 preferred != pr_info->adv_pref_lifetime_in_secs)
2231 ip6_neighbor_syslog(vm, LOG_WARNING,
2232 "our ADV preferredlifetime 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);
2252 p0->error = error_node->errors[error0];
2254 if (error0 != ICMP6_ERROR_NONE)
2255 vlib_error_count (vm, error_node->node_index, error0, 1);
2257 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2258 to_next, n_left_to_next,
2262 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2265 /* Account for router advertisements received. */
2266 vlib_error_count (vm, error_node->node_index,
2267 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2268 n_advertisements_rcvd);
2270 return frame->n_vectors;
2274 random_f64_from_to (f64 from, f64 to)
2276 static u32 seed = 0;
2277 static u8 seed_set = 0;
2280 seed = random_default_seed ();
2283 return random_f64 (&seed) * (to - from) + from;
2287 get_mac_address (u32 sw_if_index, u8 * address)
2289 vnet_main_t *vnm = vnet_get_main ();
2290 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2291 if (!hw_if->hw_address)
2293 clib_memcpy (address, hw_if->hw_address, 6);
2297 static inline vlib_buffer_t *
2298 create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2302 icmp6_router_solicitation_header_t *rh;
2307 sw_if_index = radv_info->sw_if_index;
2309 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2311 clib_warning ("buffer allocation failure");
2315 p0 = vlib_get_buffer (vm, bi0);
2316 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2317 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2319 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2320 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2322 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2324 rh = vlib_buffer_get_current (p0);
2325 p0->current_length = sizeof (*rh);
2327 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2328 rh->neighbor.icmp.code = 0;
2329 rh->neighbor.icmp.checksum = 0;
2330 rh->neighbor.reserved_must_be_zero = 0;
2332 rh->link_layer_option.header.type =
2333 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2334 if (0 != get_mac_address (sw_if_index,
2335 rh->link_layer_option.ethernet_address))
2337 clib_warning ("interface with sw_if_index %u has no mac address",
2339 vlib_buffer_free (vm, &bi0, 1);
2342 rh->link_layer_option.header.n_data_u64s = 1;
2344 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2346 rh->ip.ip_version_traffic_class_and_flow_label =
2347 clib_host_to_net_u32 (0x6 << 28);
2348 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2349 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2350 rh->ip.hop_limit = 255;
2351 rh->ip.src_address = radv_info->link_local_address;
2352 /* set address ff02::2 */
2353 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
2354 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2356 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2364 stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2368 ra->keep_sending_rs = 0;
2371 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2372 vlib_buffer_free (vm, &bi0, 1);
2378 check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2388 icmp6_send_router_solicitation_params_t *params;
2390 if (!radv_info->keep_sending_rs)
2393 params = &radv_info->params;
2395 if (radv_info->due_time > current_time)
2397 *due_time = radv_info->due_time;
2401 p0 = radv_info->buffer;
2403 next_index = ip6_rewrite_mcast_node.index;
2405 c0 = vlib_buffer_copy (vm, p0);
2407 return radv_info->keep_sending_rs;
2409 ci0 = vlib_get_buffer_index (vm, c0);
2411 f = vlib_get_frame_to_node (vm, next_index);
2412 to_next = vlib_frame_vector_args (f);
2415 vlib_put_frame_to_node (vm, next_index, f);
2417 if (params->mrc != 0 && --radv_info->n_left == 0)
2418 stop_sending_rs (vm, radv_info);
2421 radv_info->sleep_interval =
2422 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2423 if (radv_info->sleep_interval > params->mrt)
2424 radv_info->sleep_interval =
2425 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2427 radv_info->due_time = current_time + radv_info->sleep_interval;
2429 if (params->mrd != 0
2430 && current_time > radv_info->start_time + params->mrd)
2431 stop_sending_rs (vm, radv_info);
2433 *due_time = radv_info->due_time;
2436 return radv_info->keep_sending_rs;
2440 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2443 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2444 ip6_radv_t *radv_info;
2445 uword *event_data = 0;
2446 f64 sleep_time = 1e9;
2453 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2454 vlib_process_get_events (vm, &event_data);
2455 vec_reset_length (event_data);
2457 current_time = vlib_time_now (vm);
2460 due_time = current_time + 1e9;
2462 pool_foreach (radv_info, nm->if_radv_pool,
2464 if (check_send_rs (vm, radv_info, current_time, &dt)
2469 current_time = vlib_time_now (vm);
2471 while (due_time < current_time);
2473 sleep_time = due_time - current_time;
2480 VLIB_REGISTER_NODE (send_rs_process_node) = {
2481 .function = send_rs_process,
2482 .type = VLIB_NODE_TYPE_PROCESS,
2483 .name = "send-rs-process",
2488 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2489 icmp6_send_router_solicitation_params_t *
2492 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2496 ASSERT (~0 != sw_if_index);
2498 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2499 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2501 stop_sending_rs (vm, ra);
2505 ra->keep_sending_rs = 1;
2506 ra->params = *params;
2507 ra->n_left = params->mrc;
2508 ra->start_time = vlib_time_now (vm);
2509 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2510 ra->due_time = 0; /* send first packet ASAP */
2511 ra->buffer = create_buffer_for_rs (vm, ra);
2513 ra->keep_sending_rs = 0;
2515 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2520 * @brief Add a multicast Address to the advertised MLD set
2523 ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2525 ip6_mldp_group_t *mcast_group_info;
2528 /* lookup mldp info for this interface */
2529 p = mhash_get (&radv_info->address_to_mldp_index, addr);
2531 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2534 if (!mcast_group_info)
2538 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2540 mi = mcast_group_info - radv_info->mldp_group_pool;
2541 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
2544 mcast_group_info->type = 4;
2545 mcast_group_info->mcast_source_address_pool = 0;
2546 mcast_group_info->num_sources = 0;
2547 clib_memcpy (&mcast_group_info->mcast_address, addr,
2548 sizeof (ip6_address_t));
2553 * @brief Delete a multicast Address from the advertised MLD set
2556 ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2558 ip6_mldp_group_t *mcast_group_info;
2561 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2563 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2565 if (mcast_group_info)
2567 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2569 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2574 * @brief Add a multicast Address to the advertised MLD set
2577 ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2578 ip6_multicast_address_scope_t scope,
2579 ip6_multicast_link_local_group_id_t group)
2583 ip6_set_reserved_multicast_address (&addr, scope, group);
2585 ip6_neighbor_add_mld_prefix (a, &addr);
2589 * @brief create and initialize router advertisement parameters with default
2590 * values for this intfc
2593 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
2594 u32 sw_if_index, u32 is_add)
2596 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2599 vnet_sw_interface_t *sw_if0;
2600 ethernet_interface_t *eth_if0 = 0;
2602 /* lookup radv container - ethernet interfaces only */
2603 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2604 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2605 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2610 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2612 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2616 a = pool_elt_at_index (nm->if_radv_pool, ri);
2620 ip6_radv_prefix_t *p;
2621 ip6_mldp_group_t *m;
2623 /* release the lock on the interface's mcast adj */
2624 adj_unlock (a->mcast_adj_index);
2626 /* clean up prefix and MDP pools */
2628 pool_flush(p, a->adv_prefixes_pool,
2630 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
2632 pool_flush (m, a->mldp_group_pool,
2634 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
2638 pool_free (a->mldp_group_pool);
2639 pool_free (a->adv_prefixes_pool);
2641 mhash_free (&a->address_to_prefix_index);
2642 mhash_free (&a->address_to_mldp_index);
2644 if (a->keep_sending_rs)
2645 a->keep_sending_rs = 0;
2647 pool_put (nm->if_radv_pool, a);
2648 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2656 pool_get (nm->if_radv_pool, a);
2658 ri = a - nm->if_radv_pool;
2659 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2661 /* initialize default values (most of which are zero) */
2662 clib_memset (a, 0, sizeof (a[0]));
2664 a->sw_if_index = sw_if_index;
2665 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2666 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2667 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2668 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2670 /* send ll address source address option */
2671 a->adv_link_layer_address = 1;
2673 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2674 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2675 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2676 a->seed = (u32) clib_cpu_time_now ();
2677 (void) random_u32 (&a->seed);
2678 a->randomizer = clib_cpu_time_now ();
2679 (void) random_u64 (&a->randomizer);
2681 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2682 a->initial_adverts_sent = a->initial_adverts_count - 1;
2683 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2685 /* deafult is to send */
2688 /* fill in radv_info for this interface that will be needed later */
2690 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
2692 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2694 /* fill in default link-local address (this may be overridden) */
2695 ip6_link_local_address_from_ethernet_mac_address
2696 (&a->link_local_address, eth_if0->address);
2698 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2699 sizeof (ip6_address_t));
2700 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2701 sizeof (ip6_address_t));
2703 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2707 a->keep_sending_rs = 0;
2709 /* add multicast groups we will always be reporting */
2710 ip6_neighbor_add_mld_grp (a,
2711 IP6_MULTICAST_SCOPE_link_local,
2712 IP6_MULTICAST_GROUP_ID_all_hosts);
2713 ip6_neighbor_add_mld_grp (a,
2714 IP6_MULTICAST_SCOPE_link_local,
2715 IP6_MULTICAST_GROUP_ID_all_routers);
2716 ip6_neighbor_add_mld_grp (a,
2717 IP6_MULTICAST_SCOPE_link_local,
2718 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2724 /* send an mldpv2 report */
2726 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2728 vnet_main_t *vnm = vnet_get_main ();
2729 vlib_main_t *vm = vnm->vlib_main;
2730 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2731 vnet_sw_interface_t *sw_if0;
2732 ethernet_interface_t *eth_if0;
2736 ip6_radv_t *radv_info;
2746 icmp6_multicast_listener_report_header_t *rh0;
2747 icmp6_multicast_listener_report_packet_t *rp0;
2749 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2750 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2751 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2753 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2756 /* look up the radv_t information for this interface */
2757 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2760 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2765 /* send report now - build a mldpv2 report packet */
2766 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
2767 if (PREDICT_FALSE (n_allocated == 0))
2770 clib_warning ("buffer allocation failure");
2774 b0 = vlib_get_buffer (vm, bo0);
2776 /* adjust the sizeof the buffer to just include the ipv6 header */
2777 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2779 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2781 b0->error = ICMP6_ERROR_NONE;
2783 rp0 = vlib_buffer_get_current (b0);
2784 ip0 = (ip6_header_t *) & rp0->ip;
2785 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2787 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2789 ip0->ip_version_traffic_class_and_flow_label =
2790 clib_host_to_net_u32 (0x6 << 28);
2792 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2793 /* for DEBUG - vnet driver won't seem to emit router alerts */
2794 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2797 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2799 /* source address MUST be the link-local address */
2800 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2801 ip0->src_address = radv_info->link_local_address;
2803 /* destination is all mldpv2 routers */
2804 ip6_set_reserved_multicast_address (&ip0->dst_address,
2805 IP6_MULTICAST_SCOPE_link_local,
2806 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2808 /* add reports here */
2809 ip6_mldp_group_t *m;
2810 int num_addr_records = 0;
2811 icmp6_multicast_address_record_t rr;
2813 /* fill in the hop-by-hop extension header (router alert) info */
2814 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2815 rh0->ext_hdr.n_data_u64s = 0;
2817 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2819 rh0->alert.value = 0;
2824 rh0->icmp.checksum = 0;
2827 pool_foreach (m, radv_info->mldp_group_pool,
2830 rr.aux_data_len_u32s = 0;
2831 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2832 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2836 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
2837 sizeof(icmp6_multicast_address_record_t)))
2839 vlib_buffer_free (vm, &bo0, 1);
2843 payload_length += sizeof( icmp6_multicast_address_record_t);
2848 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2850 /* update lengths */
2851 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2853 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2855 ASSERT (bogus_length == 0);
2858 * OK to override w/ no regard for actual FIB, because
2859 * ip6-rewrite only looks at the adjacency.
2861 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2862 vnet_main.local_interface_sw_if_index;
2864 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2865 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2867 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2869 f = vlib_get_frame_to_node (vm, node->index);
2870 to_next = vlib_frame_vector_args (f);
2874 vlib_put_frame_to_node (vm, node->index, f);
2879 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2881 .function = icmp6_router_solicitation,
2882 .name = "icmp6-router-solicitation",
2884 .vector_size = sizeof (u32),
2886 .format_trace = format_icmp6_input_trace,
2888 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2890 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
2891 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2892 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2897 /* send a RA or update the timer info etc.. */
2899 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2900 vlib_node_runtime_t * node,
2901 vlib_frame_t * frame)
2903 vnet_main_t *vnm = vnet_get_main ();
2904 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2905 ip6_radv_t *radv_info;
2906 vlib_frame_t *f = 0;
2907 u32 n_this_frame = 0;
2908 u32 n_left_to_next = 0;
2911 icmp6_router_solicitation_header_t *h0;
2913 f64 now = vlib_time_now (vm);
2915 /* Interface ip6 radv info list */
2917 pool_foreach (radv_info, nm->if_radv_pool,
2919 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2921 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2922 radv_info->next_multicast_time = now;
2923 radv_info->last_multicast_time = now;
2924 radv_info->last_radv_time = 0;
2925 radv_info->all_routers_mcast = 0;
2929 /* Make sure that we've joined the all-routers multicast group */
2930 if(!radv_info->all_routers_mcast)
2932 /* send MDLP_REPORT_EVENT message */
2933 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2934 radv_info->all_routers_mcast = 1;
2937 /* is it time to send a multicast RA on this interface? */
2938 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2943 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2944 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2946 /* multicast send - compute next multicast send time */
2947 if( radv_info->initial_adverts_sent > 0)
2949 radv_info->initial_adverts_sent--;
2950 if(rfn > radv_info-> initial_adverts_interval)
2951 rfn = radv_info-> initial_adverts_interval;
2953 /* check to see if we are ceasing to send */
2954 if( radv_info->initial_adverts_sent == 0)
2955 if(radv_info->cease_radv)
2956 radv_info->send_radv = 0;
2959 radv_info->next_multicast_time = rfn + now;
2960 radv_info->last_multicast_time = now;
2962 /* send advert now - build a "solicted" router advert with unspecified source address */
2963 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
2965 if (PREDICT_FALSE(n_allocated == 0))
2967 clib_warning ("buffer allocation failure");
2970 b0 = vlib_get_buffer (vm, bo0);
2971 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2972 b0->error = ICMP6_ERROR_NONE;
2973 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2975 h0 = vlib_buffer_get_current (b0);
2977 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2979 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2980 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2981 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2982 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2983 h0->ip.hop_limit = 255;
2985 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2986 h0->ip.src_address.as_u64[0] = 0;
2987 h0->ip.src_address.as_u64[1] = 0;
2989 h0->ip.dst_address.as_u64[0] = 0;
2990 h0->ip.dst_address.as_u64[1] = 0;
2992 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2994 if (PREDICT_FALSE(f == 0))
2996 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2997 to_next = vlib_frame_vector_args (f);
2998 n_left_to_next = VLIB_FRAME_SIZE;
3007 if (PREDICT_FALSE(n_left_to_next == 0))
3009 f->n_vectors = n_this_frame;
3010 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3019 ASSERT (n_this_frame);
3020 f->n_vectors = n_this_frame;
3021 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3027 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3028 vlib_node_runtime_t * node,
3029 vlib_frame_t * frame)
3032 ip6_icmp_neighbor_discovery_event_data_t *event_data;
3034 /* init code here */
3038 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
3040 event_data = vlib_process_get_event_data (vm, &event_type);
3044 /* No events found: timer expired. */
3045 /* process interface list and send RAs as appropriate, update timer info */
3046 ip6_neighbor_process_timer_event (vm, node, frame);
3053 case ICMP6_ND_EVENT_INIT:
3064 _vec_len (event_data) = 0;
3067 return frame->n_vectors;
3071 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3073 .function = icmp6_router_advertisement,
3074 .name = "icmp6-router-advertisement",
3076 .vector_size = sizeof (u32),
3078 .format_trace = format_icmp6_input_trace,
3087 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3089 .function = ip6_icmp_neighbor_discovery_event_process,
3090 .name = "ip6-icmp-neighbor-discovery-event-process",
3091 .type = VLIB_NODE_TYPE_PROCESS,
3095 icmp6_neighbor_solicitation (vlib_main_t * vm,
3096 vlib_node_runtime_t * node, vlib_frame_t * frame)
3098 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3099 /* is_solicitation */
3104 icmp6_neighbor_advertisement (vlib_main_t * vm,
3105 vlib_node_runtime_t * node,
3106 vlib_frame_t * frame)
3108 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3109 /* is_solicitation */
3114 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3116 .function = icmp6_neighbor_solicitation,
3117 .name = "icmp6-neighbor-solicitation",
3119 .vector_size = sizeof (u32),
3121 .format_trace = format_icmp6_input_trace,
3123 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3125 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
3126 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3132 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3134 .function = icmp6_neighbor_advertisement,
3135 .name = "icmp6-neighbor-advertisement",
3137 .vector_size = sizeof (u32),
3139 .format_trace = format_icmp6_input_trace,
3150 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3151 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3152 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3153 } ip6_discover_neighbor_next_t;
3157 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3158 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3159 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3160 } ip6_discover_neighbor_error_t;
3163 ip6_discover_neighbor_inline (vlib_main_t * vm,
3164 vlib_node_runtime_t * node,
3165 vlib_frame_t * frame, int is_glean)
3167 vnet_main_t *vnm = vnet_get_main ();
3168 ip6_main_t *im = &ip6_main;
3169 ip_lookup_main_t *lm = &im->lookup_main;
3170 u32 *from, *to_next_drop;
3171 uword n_left_from, n_left_to_next_drop;
3173 u32 thread_index = vm->thread_index;
3175 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3177 if (node->flags & VLIB_NODE_FLAG_TRACE)
3178 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3180 seed = throttle_seed (&im->nd_throttle, thread_index, vlib_time_now (vm));
3182 from = vlib_frame_vector_args (frame);
3183 n_left_from = frame->n_vectors;
3185 while (n_left_from > 0)
3187 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3188 to_next_drop, n_left_to_next_drop);
3190 while (n_left_from > 0 && n_left_to_next_drop > 0)
3192 u32 pi0, adj_index0, sw_if_index0, drop0, r0, next0;
3193 vnet_hw_interface_t *hw_if0;
3194 ip6_radv_t *radv_info;
3195 ip_adjacency_t *adj0;
3201 p0 = vlib_get_buffer (vm, pi0);
3203 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3205 ip0 = vlib_buffer_get_current (p0);
3207 adj0 = adj_get (adj_index0);
3211 ip0->dst_address.as_u64[0] =
3212 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3213 ip0->dst_address.as_u64[1] =
3214 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3217 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3218 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3220 /* combine the address and interface for a hash */
3221 r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;
3223 drop0 = throttle_check (&im->nd_throttle, thread_index, r0, seed);
3227 to_next_drop[0] = pi0;
3229 n_left_to_next_drop -= 1;
3231 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3233 /* If the interface is link-down, drop the pkt */
3234 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3237 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3239 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3242 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3250 * the adj has been updated to a rewrite but the node the DPO that got
3251 * us here hasn't - yet. no big deal. we'll drop while we wait.
3253 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3257 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3258 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3265 icmp6_neighbor_solicitation_header_t *h0;
3268 h0 = vlib_packet_template_get_packet
3269 (vm, &im->discover_neighbor_packet_template, &bi0);
3273 /* copy the persistent fields from the original */
3274 b0 = vlib_get_buffer (vm, bi0);
3275 clib_memcpy_fast (b0->opaque2, p0->opaque2, sizeof (p0->opaque2));
3278 * Build ethernet header.
3279 * Choose source address based on destination lookup
3282 if (!ip6_src_address_for_packet (lm,
3285 &h0->ip.src_address))
3287 /* There is no address on the interface */
3289 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3290 vlib_buffer_free (vm, &bi0, 1);
3295 * Destination address is a solicited node multicast address.
3296 * We need to fill in
3297 * the low 24 bits with low 24 bits of target's address.
3299 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3300 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3301 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3303 h0->neighbor.target_address = ip0->dst_address;
3305 clib_memcpy (h0->link_layer_option.ethernet_address,
3306 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3308 /* $$$$ appears we need this; why is the checksum non-zero? */
3309 h0->neighbor.icmp.checksum = 0;
3310 h0->neighbor.icmp.checksum =
3311 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3314 ASSERT (bogus_length == 0);
3316 vlib_buffer_copy_trace_flag (vm, p0, bi0);
3317 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3318 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3320 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3321 radv_info->mcast_adj_index;
3323 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3324 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3326 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3330 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3331 n_left_to_next_drop);
3334 return frame->n_vectors;
3338 ip6_discover_neighbor (vlib_main_t * vm,
3339 vlib_node_runtime_t * node, vlib_frame_t * frame)
3341 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3345 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3347 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3350 static char *ip6_discover_neighbor_error_strings[] = {
3351 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3352 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3353 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3354 = "no source address for ND solicitation",
3358 VLIB_REGISTER_NODE (ip6_glean_node) =
3360 .function = ip6_glean,
3361 .name = "ip6-glean",
3362 .vector_size = sizeof (u32),
3363 .format_trace = format_ip6_forward_next_trace,
3364 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3365 .error_strings = ip6_discover_neighbor_error_strings,
3366 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3369 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3370 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3373 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3375 .function = ip6_discover_neighbor,
3376 .name = "ip6-discover-neighbor",
3377 .vector_size = sizeof (u32),
3378 .format_trace = format_ip6_forward_next_trace,
3379 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3380 .error_strings = ip6_discover_neighbor_error_strings,
3381 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3384 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3385 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3390 /* API support functions */
3392 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3393 u8 suppress, u8 managed, u8 other,
3394 u8 ll_option, u8 send_unicast, u8 cease,
3395 u8 use_lifetime, u32 lifetime,
3396 u32 initial_count, u32 initial_interval,
3397 u32 max_interval, u32 min_interval, u8 is_no)
3399 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3403 /* look up the radv_t information for this interface */
3404 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3406 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3407 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3412 ip6_radv_t *radv_info;
3413 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3415 if ((max_interval != 0) && (min_interval == 0))
3416 min_interval = .75 * max_interval;
3420 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3421 radv_info->max_radv_interval;
3424 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3425 radv_info->min_radv_interval;
3428 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3429 radv_info->adv_router_lifetime_in_sec;
3433 if (lifetime > MAX_DEF_RTR_LIFETIME)
3434 lifetime = MAX_DEF_RTR_LIFETIME;
3436 if (lifetime <= max_interval)
3437 return VNET_API_ERROR_INVALID_VALUE;
3440 if (min_interval != 0)
3442 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3443 return VNET_API_ERROR_INVALID_VALUE;
3446 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3447 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3448 return VNET_API_ERROR_INVALID_VALUE;
3451 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3452 if "flag" is clear don't change corresponding value
3454 radv_info->send_radv =
3455 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3456 radv_info->adv_managed_flag =
3457 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3458 radv_info->adv_other_flag =
3459 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3460 radv_info->adv_link_layer_address =
3462 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3463 radv_info->send_unicast =
3464 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3465 radv_info->cease_radv =
3466 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3468 radv_info->min_radv_interval = min_interval;
3469 radv_info->max_radv_interval = max_interval;
3470 radv_info->adv_router_lifetime_in_sec = lifetime;
3472 radv_info->initial_adverts_count =
3474 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3475 radv_info->initial_adverts_count;
3476 radv_info->initial_adverts_interval =
3477 (initial_interval !=
3478 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3479 radv_info->initial_adverts_interval;
3482 if ((cease != 0) && (is_no))
3483 radv_info->send_radv = 1;
3485 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3486 radv_info->next_multicast_time = vlib_time_now (vm);
3487 radv_info->last_multicast_time = vlib_time_now (vm);
3488 radv_info->last_radv_time = 0;
3494 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3495 ip6_address_t * prefix_addr, u8 prefix_len,
3496 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3497 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3498 u8 no_onlink, u8 is_no)
3500 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3505 /* look up the radv_t information for this interface */
3506 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3509 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3511 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3515 f64 now = vlib_time_now (vm);
3516 ip6_radv_t *radv_info;
3517 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3519 /* prefix info add, delete or update */
3520 ip6_radv_prefix_t *prefix;
3522 /* lookup prefix info for this address on this interface */
3523 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3525 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3531 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3533 if (prefix->prefix_len != prefix_len)
3534 return VNET_API_ERROR_INVALID_VALUE_2;
3536 /* FIXME - Should the DP do this or the CP ? */
3537 /* do specific delete processing here before returning */
3538 /* try to remove from routing table */
3540 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3542 pool_put (radv_info->adv_prefixes_pool, prefix);
3544 radv_info->initial_adverts_sent =
3545 radv_info->initial_adverts_count - 1;
3546 radv_info->next_multicast_time = vlib_time_now (vm);
3547 radv_info->last_multicast_time = vlib_time_now (vm);
3548 radv_info->last_radv_time = 0;
3552 /* adding or changing */
3557 pool_get (radv_info->adv_prefixes_pool, prefix);
3558 pi = prefix - radv_info->adv_prefixes_pool;
3559 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3562 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3564 prefix->prefix_len = prefix_len;
3565 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3567 /* initialize default values */
3568 prefix->adv_on_link_flag = 1; /* L bit set */
3569 prefix->adv_autonomous_flag = 1; /* A bit set */
3570 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
3571 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3572 prefix->enabled = 1;
3573 prefix->decrement_lifetime_flag = 1;
3574 prefix->deprecated_prefix_flag = 1;
3578 /* FIXME - Should the DP do this or the CP ? */
3579 /* insert prefix into routing table as a connected prefix */
3588 if (prefix->prefix_len != prefix_len)
3589 return VNET_API_ERROR_INVALID_VALUE_2;
3593 /* FIXME - Should the DP do this or the CP ? */
3594 /* remove from routing table if already there */
3598 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
3600 prefix->adv_valid_lifetime_in_secs = ~0;
3601 prefix->adv_pref_lifetime_in_secs = ~0;
3602 prefix->decrement_lifetime_flag = 0;
3606 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3607 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
3610 /* copy remaining */
3611 prefix->enabled = !(no_advertise != 0);
3612 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3613 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3617 /* fill in the expiration times */
3618 prefix->valid_lifetime_expires =
3619 now + prefix->adv_valid_lifetime_in_secs;
3620 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
3622 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3623 radv_info->next_multicast_time = vlib_time_now (vm);
3624 radv_info->last_multicast_time = vlib_time_now (vm);
3625 radv_info->last_radv_time = 0;
3631 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3632 vlib_cli_command_t * cmd)
3634 vnet_main_t *vnm = vnet_get_main ();
3635 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3636 clib_error_t *error = 0;
3638 u8 suppress = 0, managed = 0, other = 0;
3639 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
3640 u8 use_lifetime = 0;
3641 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3642 0, ra_initial_interval = 0;
3643 u32 ra_max_interval = 0, ra_min_interval = 0;
3645 unformat_input_t _line_input, *line_input = &_line_input;
3646 vnet_sw_interface_t *sw_if0;
3648 int add_radv_info = 1;
3649 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
3650 ip6_address_t ip6_addr;
3654 /* Get a line of input. */
3655 if (!unformat_user (main_input, unformat_line_input, line_input))
3658 /* get basic radv info for this interface */
3659 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3662 if (unformat_user (line_input,
3663 unformat_vnet_sw_interface, vnm, &sw_if_index))
3666 ethernet_interface_t *eth_if0 = 0;
3668 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3669 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3671 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
3676 clib_error_return (0, "Interface must be of ethernet type");
3680 /* look up the radv_t information for this interface */
3681 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3684 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3688 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3692 error = clib_error_return (0, "unknown interface %U'",
3693 format_unformat_error, line_input);
3699 error = clib_error_return (0, "invalid interface name %U'",
3700 format_unformat_error, line_input);
3705 /* get the rest of the command */
3706 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3708 if (unformat (line_input, "no"))
3710 else if (unformat (line_input, "prefix %U/%d",
3711 unformat_ip6_address, &ip6_addr, &addr_len))
3715 else if (unformat (line_input, "ra-managed-config-flag"))
3719 else if (unformat (line_input, "ra-other-config-flag"))
3723 else if (unformat (line_input, "ra-suppress") ||
3724 unformat (line_input, "ra-surpress"))
3728 else if (unformat (line_input, "ra-suppress-link-layer") ||
3729 unformat (line_input, "ra-surpress-link-layer"))
3731 suppress_ll_option = 1;
3733 else if (unformat (line_input, "ra-send-unicast"))
3737 else if (unformat (line_input, "ra-lifetime"))
3739 if (!unformat (line_input, "%d", &ra_lifetime))
3741 error = unformat_parse_error (line_input);
3746 else if (unformat (line_input, "ra-initial"))
3749 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
3751 error = unformat_parse_error (line_input);
3755 else if (unformat (line_input, "ra-interval"))
3757 if (!unformat (line_input, "%d", &ra_max_interval))
3759 error = unformat_parse_error (line_input);
3763 if (!unformat (line_input, "%d", &ra_min_interval))
3764 ra_min_interval = 0;
3766 else if (unformat (line_input, "ra-cease"))
3772 error = unformat_parse_error (line_input);
3779 ip6_neighbor_ra_config (vm, sw_if_index,
3780 suppress, managed, other,
3781 suppress_ll_option, send_unicast, cease,
3782 use_lifetime, ra_lifetime,
3783 ra_initial_count, ra_initial_interval,
3784 ra_max_interval, ra_min_interval, is_no);
3788 u32 valid_lifetime_in_secs = 0;
3789 u32 pref_lifetime_in_secs = 0;
3790 u8 use_prefix_default_values = 0;
3791 u8 no_advertise = 0;
3793 u8 no_autoconfig = 0;
3796 /* get the rest of the command */
3797 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3799 if (unformat (line_input, "default"))
3801 use_prefix_default_values = 1;
3804 else if (unformat (line_input, "infinite"))
3806 valid_lifetime_in_secs = ~0;
3807 pref_lifetime_in_secs = ~0;
3810 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3811 &pref_lifetime_in_secs))
3818 /* get the rest of the command */
3819 while (!use_prefix_default_values &&
3820 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3822 if (unformat (line_input, "no-advertise"))
3824 else if (unformat (line_input, "off-link"))
3826 else if (unformat (line_input, "no-autoconfig"))
3828 else if (unformat (line_input, "no-onlink"))
3832 error = unformat_parse_error (line_input);
3837 ip6_neighbor_ra_prefix (vm, sw_if_index,
3838 &ip6_addr, addr_len,
3839 use_prefix_default_values,
3840 valid_lifetime_in_secs,
3841 pref_lifetime_in_secs,
3843 off_link, no_autoconfig, no_onlink, is_no);
3847 unformat_free (line_input);
3853 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3855 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3858 for (i = 0; i < vec_len (addrs); i++)
3860 ip_interface_address_t *a =
3861 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3862 ip6_address_t *address = ip_interface_address_get_address (lm, a);
3864 vlib_cli_output (vm, "\t\t%U/%d",
3865 format_ip6_address, address, a->address_length);
3869 static clib_error_t *
3870 show_ip6_interface_cmd (vlib_main_t * vm,
3871 unformat_input_t * input, vlib_cli_command_t * cmd)
3873 vnet_main_t *vnm = vnet_get_main ();
3874 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3875 clib_error_t *error = 0;
3880 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3884 /* look up the radv_t information for this interface */
3885 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3888 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3892 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3893 ip6_radv_t *radv_info;
3894 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3896 vlib_cli_output (vm, "%U is admin %s\n",
3897 format_vnet_sw_interface_name, vnm,
3898 vnet_get_sw_interface (vnm, sw_if_index),
3899 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3903 u32 *link_scope = 0, *global_scope = 0;
3904 u32 *local_scope = 0, *unknown_scope = 0;
3905 ip_interface_address_t *a;
3907 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3909 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3911 while (ai != (u32) ~ 0)
3913 a = pool_elt_at_index (lm->if_address_pool, ai);
3914 ip6_address_t *address =
3915 ip_interface_address_get_address (lm, a);
3917 if (ip6_address_is_link_local_unicast (address))
3918 vec_add1 (link_scope, ai);
3919 else if (ip6_address_is_global_unicast (address))
3920 vec_add1 (global_scope, ai);
3921 else if (ip6_address_is_local_unicast (address))
3922 vec_add1 (local_scope, ai);
3924 vec_add1 (unknown_scope, ai);
3926 ai = a->next_this_sw_interface;
3929 if (vec_len (link_scope))
3931 vlib_cli_output (vm, "\tLink-local address(es):\n");
3932 ip6_print_addrs (vm, link_scope);
3933 vec_free (link_scope);
3936 if (vec_len (local_scope))
3938 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3939 ip6_print_addrs (vm, local_scope);
3940 vec_free (local_scope);
3943 if (vec_len (global_scope))
3945 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3946 ip6_print_addrs (vm, global_scope);
3947 vec_free (global_scope);
3950 if (vec_len (unknown_scope))
3952 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3953 ip6_print_addrs (vm, unknown_scope);
3954 vec_free (unknown_scope);
3957 vlib_cli_output (vm, "\tLink-local address(es):\n");
3958 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3959 &radv_info->link_local_address);
3961 vlib_cli_output (vm, "\tJoined group address(es):\n");
3962 ip6_mldp_group_t *m;
3964 pool_foreach (m, radv_info->mldp_group_pool,
3966 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3971 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3972 ip6_radv_prefix_t *p;
3974 pool_foreach (p, radv_info->adv_prefixes_pool,
3976 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3977 format_ip6_address, &p->prefix, p->prefix_len);
3981 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
3982 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3983 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3984 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3985 vlib_cli_output (vm, "\tND DAD is disabled\n");
3986 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3987 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3988 radv_info->adv_neighbor_reachable_time_in_msec);
3989 vlib_cli_output (vm,
3990 "\tND advertised retransmit interval is %d (msec)\n",
3992 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
3994 u32 ra_interval = radv_info->max_radv_interval;
3995 u32 ra_interval_min = radv_info->min_radv_interval;
3996 vlib_cli_output (vm,
3997 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
3998 ra_interval, ra_interval_min);
3999 vlib_cli_output (vm,
4000 "\tND router advertisements live for %d seconds\n",
4001 radv_info->adv_router_lifetime_in_sec);
4002 vlib_cli_output (vm,
4003 "\tHosts %s stateless autoconfig for addresses\n",
4004 (radv_info->adv_managed_flag) ? "use" :
4006 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4007 radv_info->n_advertisements_sent);
4008 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4009 radv_info->n_solicitations_rcvd);
4010 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4011 radv_info->n_solicitations_dropped);
4015 error = clib_error_return (0, "IPv6 not enabled on interface",
4016 format_unformat_error, input);
4024 * This command is used to display various IPv6 attributes on a given
4028 * Example of how to display IPv6 settings:
4029 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4030 * GigabitEthernet2/0/0 is admin up
4031 * Link-local address(es):
4033 * Joined group address(es):
4038 * Advertised Prefixes:
4039 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4041 * ICMP error messages are unlimited
4042 * ICMP redirects are disabled
4043 * ICMP unreachables are not sent
4044 * ND DAD is disabled
4045 * ND advertised reachable time is 0
4046 * ND advertised retransmit interval is 0 (msec)
4047 * ND router advertisements are sent every 200 seconds (min interval is 150)
4048 * ND router advertisements live for 600 seconds
4049 * Hosts use stateless autoconfig for addresses
4050 * ND router advertisements sent 19336
4051 * ND router solicitations received 0
4052 * ND router solicitations dropped 0
4054 * Example of output if IPv6 is not enabled on the interface:
4055 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4056 * show ip6 interface: IPv6 not enabled on interface
4060 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4062 .path = "show ip6 interface",
4063 .function = show_ip6_interface_cmd,
4064 .short_help = "show ip6 interface <interface>",
4069 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4071 clib_error_t *error = 0;
4072 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4075 /* look up the radv_t information for this interface */
4076 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4078 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4080 /* if not created - do nothing */
4083 ip6_radv_t *radv_info;
4085 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4087 /* check radv_info ref count for other ip6 addresses on this interface */
4088 /* This implicitly excludes the link local address */
4089 if (radv_info->ref_count == 0)
4091 /* essentially "disables" ipv6 on this interface */
4092 ip6_ll_prefix_t ilp = {
4093 .ilp_addr = radv_info->link_local_address,
4094 .ilp_sw_if_index = sw_if_index,
4096 ip6_ll_table_entry_delete (&ilp);
4097 ip6_sw_interface_enable_disable (sw_if_index, 0);
4098 ip6_mfib_interface_enable_disable (sw_if_index, 0);
4099 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4107 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
4109 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4112 /* look up the radv_t information for this interface */
4113 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4116 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4122 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4124 clib_error_t *error = 0;
4125 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4129 /* look up the radv_t information for this interface */
4130 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4133 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4135 /* if not created yet */
4138 vnet_main_t *vnm = vnet_get_main ();
4139 vnet_sw_interface_t *sw_if0;
4141 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4142 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4144 ethernet_interface_t *eth_if0;
4147 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
4150 /* create radv_info. for this interface. This holds all the info needed for router adverts */
4152 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
4156 ip6_radv_t *radv_info;
4157 ip6_address_t link_local_address;
4159 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4161 ip6_link_local_address_from_ethernet_mac_address
4162 (&link_local_address, eth_if0->address);
4164 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
4165 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
4166 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
4167 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
4169 /* make up an interface id */
4170 link_local_address.as_u64[1] =
4171 random_u64 (&radv_info->randomizer);
4173 link_local_address.as_u64[0] =
4174 clib_host_to_net_u64 (0xFE80000000000000ULL);
4176 link_local_address.as_u8[8] &= 0xfd;
4179 ip6_ll_prefix_t ilp = {
4180 .ilp_addr = link_local_address,
4181 .ilp_sw_if_index = sw_if_index,
4184 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4187 /* essentially "enables" ipv6 on this interface */
4188 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4189 ip6_sw_interface_enable_disable (sw_if_index, 1);
4193 radv_info->link_local_address = link_local_address;
4203 ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4205 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4206 ip6_radv_t *radv_info;
4209 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
4212 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4217 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4218 *addr = radv_info->link_local_address;
4223 static clib_error_t *
4224 enable_ip6_interface_cmd (vlib_main_t * vm,
4225 unformat_input_t * input, vlib_cli_command_t * cmd)
4227 vnet_main_t *vnm = vnet_get_main ();
4228 clib_error_t *error = 0;
4233 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4235 enable_ip6_interface (vm, sw_if_index);
4239 error = clib_error_return (0, "unknown interface\n'",
4240 format_unformat_error, input);
4247 * This command is used to enable IPv6 on a given interface.
4250 * Example of how enable IPv6 on a given interface:
4251 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4254 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4256 .path = "enable ip6 interface",
4257 .function = enable_ip6_interface_cmd,
4258 .short_help = "enable ip6 interface <interface>",
4262 static clib_error_t *
4263 disable_ip6_interface_cmd (vlib_main_t * vm,
4264 unformat_input_t * input, vlib_cli_command_t * cmd)
4266 vnet_main_t *vnm = vnet_get_main ();
4267 clib_error_t *error = 0;
4272 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4274 error = disable_ip6_interface (vm, sw_if_index);
4278 error = clib_error_return (0, "unknown interface\n'",
4279 format_unformat_error, input);
4286 * This command is used to disable IPv6 on a given interface.
4289 * Example of how disable IPv6 on a given interface:
4290 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4293 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4295 .path = "disable ip6 interface",
4296 .function = disable_ip6_interface_cmd,
4297 .short_help = "disable ip6 interface <interface>",
4302 * This command is used to configure the neighbor discovery
4303 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4304 * command to display some of the current neighbor discovery parameters
4305 * on a given interface. This command has three formats:
4308 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4310 * '<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>'
4314 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4315 * router-advertisement messages to use stateful address
4316 * auto-configuration to obtain address information (sets the M-bit).
4317 * Default is the M-bit is not set and the '<em>no</em>' option
4318 * returns it to this default state.
4320 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4321 * router-advertisement messages that hosts use stateful auto
4322 * configuration to obtain nonaddress related information (sets
4323 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4324 * option returns it to this default state.
4326 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4327 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4328 * router-advertisement messages.
4330 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4331 * optional source link-layer address in the ICMPv6 router-advertisement
4332 * messages. Default is to include the optional source link-layer address
4333 * and the '<em>no</em>' option returns it to this default state.
4335 * <em>[no] ra-send-unicast</em> - Use the source address of the
4336 * router-solicitation message if availiable. The default is to use
4337 * multicast address of all nodes, and the '<em>no</em>' option returns
4338 * it to this default state.
4340 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4341 * default router in ICMPv6 router-advertisement messages. The range is
4342 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4343 * '<em><max-interval></em>'. The default value is 600 seconds and the
4344 * '<em>no</em>' option returns it to this default value.
4346 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4347 * router-advertisement messages sent and the interval between each
4348 * message. Range for count is 1 - 3 and default is 3. Range for interval
4349 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4350 * returns both to their default value.
4352 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4353 * interval between sending ICMPv6 router-advertisement messages. The
4354 * range for max-interval is from 4 to 200 seconds. min-interval can not
4355 * be more than 75% of max-interval. If not set, min-interval will be
4356 * set to 75% of max-interval. The range for min-interval is from 3 to
4357 * 150 seconds. The '<em>no</em>' option returns both to their default
4360 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4361 * The '<em>no</em>' options implies to start (or restart) sending
4362 * ICMPv6 router-advertisement messages.
4365 * <b>Format 2 - Prefix Options:</b>
4367 * '<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>'
4371 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4373 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4374 * length of time in seconds during what the prefix is valid for the purpose of
4375 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4376 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4377 * length of time in seconds during what addresses generated from the prefix remain
4378 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4380 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4381 * are inifinte, no timeout.
4383 * <em>no-advertise</em> - Do not send full router address in prefix
4384 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4386 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4387 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4388 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4390 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4391 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4393 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4394 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4395 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4399 * <b>Format 3: - Default of Prefix:</b>
4401 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4403 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4404 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4405 * is ignored and the prefix is deleted.
4409 * Example of how set a router advertisement option:
4410 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4411 * Example of how to add a prefix:
4412 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4413 * Example of how to delete a prefix:
4414 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4417 VLIB_CLI_COMMAND (ip6_nd_command, static) =
4420 .short_help = "ip6 nd <interface> ...",
4421 .function = ip6_neighbor_cmd,
4426 ip6_neighbor_set_link_local_address (vlib_main_t * vm, u32 sw_if_index,
4427 ip6_address_t * address)
4429 clib_error_t *error = 0;
4430 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4432 ip6_radv_t *radv_info;
4433 vnet_main_t *vnm = vnet_get_main ();
4434 ip6_ll_prefix_t pfx = { 0, };
4436 if (!ip6_address_is_link_local_unicast (address))
4437 return (error = clib_error_return (0, "address not link-local",
4438 format_unformat_error));
4440 /* call enable ipv6 */
4441 enable_ip6_interface (vm, sw_if_index);
4443 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4447 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4449 pfx.ilp_sw_if_index = sw_if_index;
4451 pfx.ilp_addr = radv_info->link_local_address;
4452 ip6_ll_table_entry_delete (&pfx);
4454 pfx.ilp_addr = *address;
4455 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_LOCAL);
4457 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4458 radv_info->link_local_address = *address;
4462 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4463 error = clib_error_return (0, "ip6 not enabled for interface",
4464 format_unformat_error);
4470 * @brief callback when an interface address is added or deleted
4473 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4476 ip6_address_t * address,
4478 u32 if_address_index, u32 is_delete)
4480 vnet_main_t *vnm = vnet_get_main ();
4481 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4483 vlib_main_t *vm = vnm->vlib_main;
4484 ip6_radv_t *radv_info;
4487 /* create solicited node multicast address for this interface address */
4488 ip6_set_solicited_node_multicast_address (&a, 0);
4490 a.as_u8[0xd] = address->as_u8[0xd];
4491 a.as_u8[0xe] = address->as_u8[0xe];
4492 a.as_u8[0xf] = address->as_u8[0xf];
4496 /* try to create radv_info - does nothing if ipv6 already enabled */
4497 enable_ip6_interface (vm, sw_if_index);
4499 /* look up the radv_t information for this interface */
4500 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4502 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4506 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4509 if (!ip6_address_is_link_local_unicast (address))
4510 radv_info->ref_count++;
4512 ip6_neighbor_add_mld_prefix (radv_info, &a);
4519 /* look up the radv_t information for this interface */
4520 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4522 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4527 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4529 ip6_neighbor_del_mld_prefix (radv_info, &a);
4531 /* if interface up send MLDP "report" */
4532 radv_info->all_routers_mcast = 0;
4535 if (!ip6_address_is_link_local_unicast (address))
4536 radv_info->ref_count--;
4538 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4539 disable_ip6_interface (vm, sw_if_index);
4544 ip6_set_neighbor_limit (u32 neighbor_limit)
4546 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4548 nm->limit_neighbor_cache_size = neighbor_limit;
4553 ip6_neighbor_table_bind (ip6_main_t * im,
4556 u32 new_fib_index, u32 old_fib_index)
4558 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4559 ip6_neighbor_t *n = NULL;
4560 u32 i, *to_re_add = 0;
4563 pool_foreach (n, nm->neighbor_pool,
4565 if (n->key.sw_if_index == sw_if_index)
4566 vec_add1 (to_re_add, n - nm->neighbor_pool);
4570 for (i = 0; i < vec_len (to_re_add); i++)
4572 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4573 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4574 ip6_neighbor_adj_fib_add (n, new_fib_index);
4576 vec_free (to_re_add);
4579 static clib_error_t *
4580 ip6_neighbor_init (vlib_main_t * vm)
4582 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4583 ip6_main_t *im = &ip6_main;
4585 mhash_init (&nm->neighbor_index_by_key,
4586 /* value size */ sizeof (uword),
4587 /* key size */ sizeof (ip6_neighbor_key_t));
4589 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4590 ip6_icmp_neighbor_solicitation_node.index);
4591 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4592 ip6_icmp_neighbor_advertisement_node.index);
4593 icmp6_register_type (vm, ICMP6_router_solicitation,
4594 ip6_icmp_router_solicitation_node.index);
4595 icmp6_register_type (vm, ICMP6_router_advertisement,
4596 ip6_icmp_router_advertisement_node.index);
4598 /* handler node for ip6 neighbor discovery events and timers */
4599 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4601 /* add call backs */
4602 ip6_add_del_interface_address_callback_t cb;
4603 clib_memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4605 /* when an interface address changes... */
4606 cb.function = ip6_neighbor_add_del_interface_address;
4607 cb.function_opaque = 0;
4608 vec_add1 (im->add_del_interface_address_callbacks, cb);
4610 ip6_table_bind_callback_t cbt;
4611 cbt.function = ip6_neighbor_table_bind;
4612 cbt.function_opaque = 0;
4613 vec_add1 (im->table_bind_callbacks, cbt);
4615 mhash_init (&nm->pending_resolutions_by_address,
4616 /* value size */ sizeof (uword),
4617 /* key size */ sizeof (ip6_address_t));
4619 mhash_init (&nm->mac_changes_by_address,
4620 /* value size */ sizeof (uword),
4621 /* key size */ sizeof (ip6_address_t));
4623 /* default, configurable */
4624 nm->limit_neighbor_cache_size = 50000;
4626 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4628 nm->ip6_ra_publisher_node = (uword) ~ 0;
4631 /* $$$$ Hack fix for today */
4632 vec_validate_init_empty
4633 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
4639 VLIB_INIT_FUNCTION (ip6_neighbor_init);
4643 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4646 uword type_opaque, uword data)
4648 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4649 ip6_address_t *address = address_arg;
4651 pending_resolution_t *pr;
4653 pool_get (nm->pending_resolutions, pr);
4655 pr->next_index = ~0;
4656 pr->node_index = node_index;
4657 pr->type_opaque = type_opaque;
4660 p = mhash_get (&nm->pending_resolutions_by_address, address);
4663 /* Insert new resolution at the head of the list */
4664 pr->next_index = p[0];
4665 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4668 mhash_set (&nm->pending_resolutions_by_address, address,
4669 pr - nm->pending_resolutions, 0 /* old value */ );
4673 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4674 ip6_nd_change_event_cb_t data_callback,
4678 uword type_opaque, uword data, int is_add)
4680 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4681 ip6_address_t *address = address_arg;
4683 /* Try to find an existing entry */
4684 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4686 pending_resolution_t *mc;
4687 while (p && *p != ~0)
4689 mc = pool_elt_at_index (nm->mac_changes, *p);
4690 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4693 p = &mc->next_index;
4696 int found = p && *p != ~0;
4700 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4702 pool_get (nm->mac_changes, mc);
4704 *mc = (pending_resolution_t)
4707 .node_index = node_index,
4708 .type_opaque = type_opaque,
4710 .data_callback = data_callback,
4715 /* Insert new resolution at the end of the list */
4716 u32 new_idx = mc - nm->mac_changes;
4720 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
4725 return VNET_API_ERROR_NO_SUCH_ENTRY;
4727 /* Clients may need to clean up pool entries, too */
4729 (data_callback) (mc->data, NULL /* no new mac addrs */ , 0, NULL);
4731 /* Remove the entry from the list and delete the entry */
4732 *p = mc->next_index;
4733 pool_put (nm->mac_changes, mc);
4735 /* Remove from hash if we deleted the last entry */
4736 if (*p == ~0 && p == first)
4737 mhash_unset (&nm->mac_changes_by_address, address, 0);
4743 vnet_ip6_nd_term (vlib_main_t * vm,
4744 vlib_node_runtime_t * node,
4746 ethernet_header_t * eth,
4747 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
4749 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4750 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4753 mac_address_from_bytes (&mac, eth->src_address);
4754 ndh = ip6_next_header (ip);
4755 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4756 ndh->icmp.type != ICMP6_neighbor_advertisement)
4759 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4760 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4762 u8 *t0 = vlib_add_trace (vm, node, p0,
4763 sizeof (icmp6_input_trace_t));
4764 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4767 /* Check if anyone want ND events for L2 BDs */
4769 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4770 && !ip6_address_is_link_local_unicast (&ip->src_address)))
4772 vnet_nd_wc_publish (sw_if_index, &mac, &ip->src_address);
4775 /* Check if MAC entry exsist for solicited target IP */
4776 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4778 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4779 l2_bridge_domain_t *bd_config;
4782 opt = (void *) (ndh + 1);
4783 if ((opt->header.type !=
4784 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4785 (opt->header.n_data_u64s != 1))
4786 return 0; /* source link layer address option not present */
4788 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4790 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4792 { /* found ip-mac entry, generate eighbor advertisement response */
4794 vlib_node_runtime_t *error_node =
4795 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4796 ip->dst_address = ip->src_address;
4797 ip->src_address = ndh->target_address;
4798 ip->hop_limit = 255;
4800 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4801 clib_memcpy (opt->ethernet_address, macp, 6);
4802 ndh->icmp.type = ICMP6_neighbor_advertisement;
4803 ndh->advertisement_flags = clib_host_to_net_u32
4804 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4805 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4806 ndh->icmp.checksum = 0;
4807 ndh->icmp.checksum =
4808 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4809 clib_memcpy (eth->dst_address, eth->src_address, 6);
4810 clib_memcpy (eth->src_address, macp, 6);
4811 vlib_error_count (vm, error_node->node_index,
4812 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4822 ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4826 fib_prefix_t pfx = {
4828 .fp_proto = FIB_PROTOCOL_IP6,
4833 ip46_address_t nh = {
4837 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4839 if (~0 == fib_index)
4840 return VNET_API_ERROR_NO_SUCH_FIB;
4844 fib_table_entry_path_remove (fib_index,
4846 FIB_SOURCE_IP6_ND_PROXY,
4850 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4851 /* flush the ND cache of this address if it's there */
4852 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
4856 fib_table_entry_path_add (fib_index,
4858 FIB_SOURCE_IP6_ND_PROXY,
4859 FIB_ENTRY_FLAG_NONE,
4863 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4868 static clib_error_t *
4869 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4870 unformat_input_t * input, vlib_cli_command_t * cmd)
4872 vnet_main_t *vnm = vnet_get_main ();
4873 clib_error_t *error = 0;
4878 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4880 /* get the rest of the command */
4881 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4883 if (unformat (input, "%U", unformat_ip6_address, &addr))
4885 else if (unformat (input, "delete") || unformat (input, "del"))
4888 return (unformat_parse_error (input));
4892 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4898 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4900 .path = "set ip6 nd proxy",
4901 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4902 .function = set_ip6_nd_proxy_cmd,
4907 ethernet_ndp_change_mac (u32 sw_if_index)
4909 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4914 pool_foreach (n, nm->neighbor_pool,
4916 if (n->key.sw_if_index == sw_if_index)
4918 adj_nbr_walk_nh6 (sw_if_index,
4919 &n->key.ip6_address,
4920 ip6_nd_mk_complete_walk, n);
4925 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4927 if (ADJ_INDEX_INVALID != ai)
4928 adj_glean_update_rewrite (ai);
4932 send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
4934 ip6_main_t *i6m = &ip6_main;
4935 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4937 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
4941 send_ip6_na_w_addr (vlib_main_t * vm,
4942 const ip6_address_t * ip6_addr, u32 sw_if_index)
4944 ip6_main_t *i6m = &ip6_main;
4945 vnet_main_t *vnm = vnet_get_main ();
4946 u8 *rewrite, rewrite_len;
4947 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4953 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4954 format_ip6_address, ip6_addr, sw_if_index);
4956 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4959 icmp6_neighbor_solicitation_header_t *h =
4960 vlib_packet_template_get_packet (vm,
4961 &i6m->discover_neighbor_packet_template,
4966 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4967 IP6_MULTICAST_SCOPE_link_local,
4968 IP6_MULTICAST_GROUP_ID_all_hosts);
4969 h->ip.src_address = ip6_addr[0];
4970 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4971 h->neighbor.target_address = ip6_addr[0];
4972 h->neighbor.advertisement_flags = clib_host_to_net_u32
4973 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4974 h->link_layer_option.header.type =
4975 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4976 clib_memcpy (h->link_layer_option.ethernet_address,
4977 hi->hw_address, vec_len (hi->hw_address));
4978 h->neighbor.icmp.checksum =
4979 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4980 ASSERT (bogus_length == 0);
4982 /* Setup MAC header with IP6 Etype and mcast DMAC */
4983 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
4984 ip6_multicast_ethernet_address (dst_address,
4985 IP6_MULTICAST_GROUP_ID_all_hosts);
4987 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
4988 rewrite_len = vec_len (rewrite);
4989 vlib_buffer_advance (b, -rewrite_len);
4990 ethernet_header_t *e = vlib_buffer_get_current (b);
4991 clib_memcpy (e->dst_address, rewrite, rewrite_len);
4994 /* Send unsolicited ND advertisement packet out the specified interface */
4995 vnet_buffer (b)->sw_if_index[VLIB_RX] =
4996 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
4997 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
4998 u32 *to_next = vlib_frame_vector_args (f);
5001 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5006 * fd.io coding-style-patch-verification: ON
5009 * eval: (c-set-style "gnu")