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>
31 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
33 * The files contains the API and CLI code for managing IPv6 neighbor
34 * adjacency tables and neighbor discovery logic.
37 /* can't use sizeof link_layer_address, that's 8 */
38 #define ETHER_MAC_ADDR_LEN 6
40 /* advertised prefix option */
43 /* basic advertised information */
47 int adv_autonomous_flag;
48 u32 adv_valid_lifetime_in_secs;
49 u32 adv_pref_lifetime_in_secs;
51 /* advertised values are computed from these times if decrementing */
52 f64 valid_lifetime_expires;
53 f64 pref_lifetime_expires;
55 /* local information */
57 int deprecated_prefix_flag;
58 int decrement_lifetime_flag;
60 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
61 #define DEF_ADV_VALID_LIFETIME 2592000
62 #define DEF_ADV_PREF_LIFETIME 604800
64 /* extensions are added here, mobile, DNS etc.. */
70 /* group information */
72 ip6_address_t mcast_address;
74 ip6_address_t *mcast_source_address_pool;
77 /* configured router advertisement information per ipv6 interface */
81 /* advertised config information, zero means unspecified */
85 u16 adv_router_lifetime_in_sec;
86 u32 adv_neighbor_reachable_time_in_msec;
87 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
92 /* source link layer option */
93 u8 link_layer_address[8];
94 u8 link_layer_addr_len;
97 ip6_radv_prefix_t *adv_prefixes_pool;
99 /* Hash table mapping address to index in interface advertised prefix pool. */
100 mhash_t address_to_prefix_index;
102 /* MLDP group information */
103 ip6_mldp_group_t *mldp_group_pool;
105 /* Hash table mapping address to index in mldp address pool. */
106 mhash_t address_to_mldp_index;
108 /* local information */
110 int send_radv; /* radv on/off on this interface - set by config */
111 int cease_radv; /* we are ceasing to send - set byf config */
113 int adv_link_layer_address;
115 int failed_device_check;
116 int all_routers_mcast;
120 adj_index_t mcast_adj_index;
122 /* timing information */
123 #define DEF_MAX_RADV_INTERVAL 200
124 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125 #define DEF_CURR_HOP_LIMIT 64
126 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
127 #define MAX_DEF_RTR_LIFETIME 9000
129 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
130 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
131 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
132 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
133 #define MAX_RA_DELAY_TIME .5 /* seconds */
135 f64 max_radv_interval;
136 f64 min_radv_interval;
137 f64 min_delay_between_radv;
138 f64 max_delay_between_radv;
139 f64 max_rtr_default_lifetime;
142 f64 last_multicast_time;
143 f64 next_multicast_time;
146 u32 initial_adverts_count;
147 f64 initial_adverts_interval;
148 u32 initial_adverts_sent;
151 u32 n_advertisements_sent;
152 u32 n_solicitations_rcvd;
153 u32 n_solicitations_dropped;
155 /* Link local address to use (defaults to underlying physical for logical interfaces */
156 ip6_address_t link_local_address;
158 /* router solicitations sending state */
159 u8 keep_sending_rs; /* when true then next fields are valid */
160 icmp6_send_router_solicitation_params_t params;
165 vlib_buffer_t *buffer;
174 /* Used for nd event notification only */
177 } pending_resolution_t;
182 /* Hash tables mapping name to opcode. */
183 uword *opcode_by_name;
185 /* lite beer "glean" adjacency handling */
186 mhash_t pending_resolutions_by_address;
187 pending_resolution_t *pending_resolutions;
189 /* Mac address change notification */
190 mhash_t mac_changes_by_address;
191 pending_resolution_t *mac_changes;
193 u32 *neighbor_input_next_index_by_hw_if_index;
195 ip6_neighbor_t *neighbor_pool;
197 mhash_t neighbor_index_by_key;
199 u32 *if_radv_pool_index_by_sw_if_index;
201 ip6_radv_t *if_radv_pool;
203 /* Neighbor attack mitigation */
204 u32 limit_neighbor_cache_size;
205 u32 neighbor_delete_rotor;
207 /* Wildcard nd report publisher */
208 uword wc_ip6_nd_publisher_node;
209 uword wc_ip6_nd_publisher_et;
211 /* Router advertisement report publisher */
212 uword ip6_ra_publisher_node;
213 uword ip6_ra_publisher_et;
214 } ip6_neighbor_main_t;
216 /* ipv6 neighbor discovery - timer/event types */
220 } ip6_icmp_neighbor_discovery_event_type_t;
230 } ip6_icmp_neighbor_discovery_event_data_t;
232 static ip6_neighbor_main_t ip6_neighbor_main;
233 ip6_neighbor_public_main_t ip6_neighbor_public_main;
234 static ip6_address_t ip6a_zero; /* ip6 address 0 */
236 static void wc_nd_signal_report (wc_nd_report_t * r);
237 static void ra_signal_report (ra_report_t * r);
240 * @brief publish wildcard arp event
241 * @param sw_if_index The interface on which the ARP entires are acted
244 vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
247 .sw_if_index = sw_if_index,
250 memcpy (r.mac, mac, sizeof r.mac);
252 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
253 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
258 wc_nd_signal_report (wc_nd_report_t * r)
260 vlib_main_t *vm = vlib_get_main ();
261 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
262 uword ni = nm->wc_ip6_nd_publisher_node;
263 uword et = nm->wc_ip6_nd_publisher_et;
265 if (ni == (uword) ~ 0)
268 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
274 wc_nd_set_publisher_node (uword node_index, uword event_type)
276 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
277 nm->wc_ip6_nd_publisher_node = node_index;
278 nm->wc_ip6_nd_publisher_et = event_type;
282 ra_publish (ra_report_t * r)
284 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
285 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
290 ra_signal_report (ra_report_t * r)
292 vlib_main_t *vm = vlib_get_main ();
293 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
294 uword ni = nm->ip6_ra_publisher_node;
295 uword et = nm->ip6_ra_publisher_et;
297 if (ni == (uword) ~ 0)
299 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
305 ra_set_publisher_node (uword node_index, uword event_type)
307 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
308 nm->ip6_ra_publisher_node = node_index;
309 nm->ip6_ra_publisher_et = event_type;
313 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
315 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
316 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
317 vnet_main_t *vnm = vnet_get_main ();
318 vnet_sw_interface_t *si;
322 return format (s, "%=12s%=25s%=6s%=20s%=40s", "Time", "Address", "Flags",
323 "Link layer", "Interface");
325 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
326 flags = format (flags, "D");
328 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
329 flags = format (flags, "S");
331 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
332 flags = format (flags, "N");
334 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
335 s = format (s, "%=12U%=25U%=6s%=20U%=40U",
336 format_vlib_time, vm, n->time_last_updated,
337 format_ip6_address, &n->key.ip6_address,
338 flags ? (char *) flags : "",
339 format_ethernet_address, n->link_layer_address,
340 format_vnet_sw_interface_name, vnm, si);
347 ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
349 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
351 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
353 ip6_ll_prefix_t pfx = {
354 .ilp_addr = n->key.ip6_address,
355 .ilp_sw_if_index = n->key.sw_if_index,
357 ip6_ll_table_entry_delete (&pfx);
363 .fp_proto = FIB_PROTOCOL_IP6,
364 .fp_addr.ip6 = n->key.ip6_address,
366 fib_table_entry_path_remove (fib_index,
371 n->key.sw_if_index, ~0,
372 1, FIB_ROUTE_PATH_FLAG_NONE);
382 u8 link_layer_address[6];
385 } ip6_neighbor_set_unset_rpc_args_t;
387 static void ip6_neighbor_set_unset_rpc_callback
388 (ip6_neighbor_set_unset_rpc_args_t * a);
390 static void set_unset_ip6_neighbor_rpc
393 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
396 ip6_neighbor_set_unset_rpc_args_t args;
397 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
399 args.sw_if_index = sw_if_index;
400 args.is_add = is_add;
401 args.is_static = is_static;
402 args.is_no_fib_entry = is_no_fib_entry;
403 clib_memcpy (&args.addr, a, sizeof (*a));
404 if (NULL != link_layer_address)
405 clib_memcpy (args.link_layer_address, link_layer_address, 6);
407 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
408 (u8 *) & args, sizeof (args));
412 ip6_nbr_probe (ip_adjacency_t * adj)
414 icmp6_neighbor_solicitation_header_t *h;
415 vnet_main_t *vnm = vnet_get_main ();
416 ip6_main_t *im = &ip6_main;
417 ip_interface_address_t *ia;
418 ip6_address_t *dst, *src;
419 vnet_hw_interface_t *hi;
420 vnet_sw_interface_t *si;
426 vm = vlib_get_main ();
428 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
429 dst = &adj->sub_type.nbr.next_hop.ip6;
431 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
435 src = ip6_interface_address_matching_destination (im, dst,
443 h = vlib_packet_template_get_packet (vm,
444 &im->discover_neighbor_packet_template,
447 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
449 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
450 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
451 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
452 h->ip.src_address = src[0];
453 h->neighbor.target_address = dst[0];
455 clib_memcpy (h->link_layer_option.ethernet_address,
456 hi->hw_address, vec_len (hi->hw_address));
458 h->neighbor.icmp.checksum =
459 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
460 ASSERT (bogus_length == 0);
462 b = vlib_get_buffer (vm, bi);
463 vnet_buffer (b)->sw_if_index[VLIB_RX] =
464 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
466 /* Add encapsulation string for software interface (e.g. ethernet header). */
467 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
468 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
471 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
472 u32 *to_next = vlib_frame_vector_args (f);
475 vlib_put_frame_to_node (vm, hi->output_node_index, f);
480 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
482 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
483 ethernet_build_rewrite (vnet_get_main (),
484 nbr->key.sw_if_index,
485 adj_get_link_type (ai),
486 nbr->link_layer_address));
490 ip6_nd_mk_incomplete (adj_index_t ai)
492 ip_adjacency_t *adj = adj_get (ai);
494 adj_nbr_update_rewrite (ai,
495 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
496 ethernet_build_rewrite (vnet_get_main (),
499 adj_get_link_type (ai),
500 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
503 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
505 k.sw_if_index = sw_if_index; \
506 k.ip6_address = *addr; \
510 static ip6_neighbor_t *
511 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
513 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
514 ip6_neighbor_t *n = NULL;
515 ip6_neighbor_key_t k;
518 IP6_NBR_MK_KEY (k, sw_if_index, addr);
520 p = mhash_get (&nm->neighbor_index_by_key, &k);
523 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
530 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
532 ip6_neighbor_t *nbr = ctx;
534 ip6_nd_mk_complete (ai, nbr);
536 return (ADJ_WALK_RC_CONTINUE);
540 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
542 ip6_nd_mk_incomplete (ai);
544 return (ADJ_WALK_RC_CONTINUE);
547 static clib_error_t *
548 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
549 u32 sw_if_index, u32 flags)
551 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
553 u32 i, *to_delete = 0;
556 pool_foreach (n, nm->neighbor_pool,
558 if (n->key.sw_if_index == sw_if_index)
559 vec_add1 (to_delete, n - nm->neighbor_pool);
563 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
565 for (i = 0; i < vec_len (to_delete); i++)
567 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
568 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
569 ip6_nd_mk_complete_walk, n);
574 for (i = 0; i < vec_len (to_delete); i++)
576 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
577 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
578 ip6_nd_mk_incomplete_walk, NULL);
579 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
581 ip6_neighbor_adj_fib_remove (n,
582 ip6_fib_table_get_index_for_sw_if_index
583 (n->key.sw_if_index));
584 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
585 pool_put (nm->neighbor_pool, n);
589 vec_free (to_delete);
593 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
596 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
603 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
605 switch (adj->lookup_next_index)
607 case IP_LOOKUP_NEXT_GLEAN:
608 adj_glean_update_rewrite (ai);
610 case IP_LOOKUP_NEXT_ARP:
613 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
614 ip6_nd_mk_complete_walk, nbr);
619 * no matching ND entry.
620 * construct the rewrite required to for an ND packet, and stick
621 * that in the adj's pipe to smoke.
623 adj_nbr_update_rewrite (ai,
624 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
625 ethernet_build_rewrite (vnm,
628 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
631 * since the FIB has added this adj for a route, it makes sense it may
632 * want to forward traffic sometime soon. Let's send a speculative ND.
633 * just one. If we were to do periodically that wouldn't be bad either,
634 * but that's more code than i'm prepared to write at this time for
635 * relatively little reward.
640 case IP_LOOKUP_NEXT_MCAST:
643 * Construct a partial rewrite from the known ethernet mcast dest MAC
648 rewrite = ethernet_build_rewrite (vnm,
651 ethernet_ip6_mcast_dst_addr ());
654 * Complete the remaining fields of the adj's rewrite to direct the
655 * complete of the rewrite at switch time by copying in the IP
656 * dst address's bytes.
657 * Ofset is 2 bytes into the desintation address.
659 offset = vec_len (rewrite) - 2;
660 adj_mcast_update_rewrite (ai, rewrite, offset);
664 case IP_LOOKUP_NEXT_DROP:
665 case IP_LOOKUP_NEXT_PUNT:
666 case IP_LOOKUP_NEXT_LOCAL:
667 case IP_LOOKUP_NEXT_REWRITE:
668 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
669 case IP_LOOKUP_NEXT_MIDCHAIN:
670 case IP_LOOKUP_NEXT_ICMP_ERROR:
671 case IP_LOOKUP_N_NEXT:
679 ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
681 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
683 ip6_ll_prefix_t pfx = {
684 .ilp_addr = n->key.ip6_address,
685 .ilp_sw_if_index = n->key.sw_if_index,
688 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
694 .fp_proto = FIB_PROTOCOL_IP6,
695 .fp_addr.ip6 = n->key.ip6_address,
699 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
700 FIB_ENTRY_FLAG_ATTACHED,
701 DPO_PROTO_IP6, &pfx.fp_addr,
702 n->key.sw_if_index, ~0, 1, NULL,
703 FIB_ROUTE_PATH_FLAG_NONE);
707 static ip6_neighbor_t *
708 force_reuse_neighbor_entry (void)
711 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
713 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
714 if (index == ~0) /* Try again from elt 0 */
715 index = pool_next_index (nm->neighbor_pool, index);
717 /* Find a non-static random entry to free up for reuse */
720 if ((count++ == 100) || (index == ~0))
721 return NULL; /* give up after 100 entries */
722 n = pool_elt_at_index (nm->neighbor_pool, index);
723 nm->neighbor_delete_rotor = index;
724 index = pool_next_index (nm->neighbor_pool, index);
726 while (n->flags & IP6_NEIGHBOR_FLAG_STATIC);
728 /* Remove ARP entry from its interface and update fib */
729 adj_nbr_walk_nh6 (n->key.sw_if_index,
730 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
731 ip6_neighbor_adj_fib_remove
732 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
733 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
739 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
742 u8 * link_layer_address,
743 uword n_bytes_link_layer_address,
744 int is_static, int is_no_fib_entry)
746 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
747 ip6_neighbor_key_t k;
748 ip6_neighbor_t *n = 0;
749 int make_new_nd_cache_entry = 1;
752 pending_resolution_t *pr, *mc;
754 if (vlib_get_thread_index ())
756 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
757 1 /* set new neighbor */ , is_static,
762 k.sw_if_index = sw_if_index;
763 k.ip6_address = a[0];
766 p = mhash_get (&nm->neighbor_index_by_key, &k);
769 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
770 /* Refuse to over-write static neighbor entry. */
771 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
773 make_new_nd_cache_entry = 0;
776 if (make_new_nd_cache_entry)
778 if (nm->limit_neighbor_cache_size &&
779 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
781 n = force_reuse_neighbor_entry ();
786 pool_get (nm->neighbor_pool, n);
788 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
791 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
793 clib_memcpy (n->link_layer_address,
794 link_layer_address, n_bytes_link_layer_address);
797 * create the adj-fib. the entry in the FIB table for and to the peer.
799 if (!is_no_fib_entry)
801 ip6_neighbor_adj_fib_add
802 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
806 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
812 * prevent a DoS attack from the data-plane that
813 * spams us with no-op updates to the MAC address
815 if (0 == memcmp (n->link_layer_address,
816 link_layer_address, n_bytes_link_layer_address))
818 n->time_last_updated = vlib_time_now (vm);
819 goto check_customers;
822 clib_memcpy (n->link_layer_address,
823 link_layer_address, n_bytes_link_layer_address);
826 /* Update time stamp and flags. */
827 n->time_last_updated = vlib_time_now (vm);
830 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
831 n->flags &= ~IP6_NEIGHBOR_FLAG_DYNAMIC;
835 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
836 n->flags &= ~IP6_NEIGHBOR_FLAG_STATIC;
839 adj_nbr_walk_nh6 (sw_if_index,
840 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
843 /* Customer(s) waiting for this address to be resolved? */
844 p = mhash_get (&nm->pending_resolutions_by_address, a);
849 while (next_index != (u32) ~ 0)
851 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
852 vlib_process_signal_event (vm, pr->node_index,
853 pr->type_opaque, pr->data);
854 next_index = pr->next_index;
855 pool_put (nm->pending_resolutions, pr);
858 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
861 /* Customer(s) requesting ND event for this address? */
862 p = mhash_get (&nm->mac_changes_by_address, a);
867 while (next_index != (u32) ~ 0)
869 int (*fp) (u32, u8 *, u32, ip6_address_t *);
871 mc = pool_elt_at_index (nm->mac_changes, next_index);
872 fp = mc->data_callback;
874 /* Call the user's data callback, return 1 to suppress dup events */
877 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
879 * Signal the resolver process, as long as the user
880 * says they want to be notified
883 vlib_process_signal_event (vm, mc->node_index,
884 mc->type_opaque, mc->data);
885 next_index = mc->next_index;
893 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
896 u8 * link_layer_address,
897 uword n_bytes_link_layer_address)
899 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
900 ip6_neighbor_key_t k;
905 if (vlib_get_thread_index ())
907 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
908 0 /* unset */ , 0, 0);
912 k.sw_if_index = sw_if_index;
913 k.ip6_address = a[0];
916 p = mhash_get (&nm->neighbor_index_by_key, &k);
923 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
925 adj_nbr_walk_nh6 (sw_if_index,
926 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
927 ip6_neighbor_adj_fib_remove
928 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
930 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
931 pool_put (nm->neighbor_pool, n);
937 static void ip6_neighbor_set_unset_rpc_callback
938 (ip6_neighbor_set_unset_rpc_args_t * a)
940 vlib_main_t *vm = vlib_get_main ();
942 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
943 a->link_layer_address, 6, a->is_static,
946 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
947 a->link_layer_address, 6);
951 ip6_neighbor_sort (void *a1, void *a2)
953 vnet_main_t *vnm = vnet_get_main ();
954 ip6_neighbor_t *n1 = a1, *n2 = a2;
956 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
957 n2->key.sw_if_index);
959 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
964 ip6_neighbors_pool (void)
966 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
967 return nm->neighbor_pool;
971 ip6_neighbors_entries (u32 sw_if_index)
973 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
974 ip6_neighbor_t *n, *ns = 0;
977 pool_foreach (n, nm->neighbor_pool,
979 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
986 vec_sort_with_function (ns, ip6_neighbor_sort);
990 static clib_error_t *
991 show_ip6_neighbors (vlib_main_t * vm,
992 unformat_input_t * input, vlib_cli_command_t * cmd)
994 vnet_main_t *vnm = vnet_get_main ();
995 ip6_neighbor_t *n, *ns;
996 clib_error_t *error = 0;
999 /* Filter entries by interface if given. */
1001 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1003 ns = ip6_neighbors_entries (sw_if_index);
1006 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
1009 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
1018 * This command is used to display the adjacent IPv6 hosts found via
1019 * neighbor discovery. Optionally, limit the output to the specified
1023 * Example of how to display the IPv6 neighbor adjacency table:
1024 * @cliexstart{show ip6 neighbors}
1025 * Time Address Flags Link layer Interface
1026 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1027 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1028 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1030 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1031 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1032 * Time Address Flags Link layer Interface
1033 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1034 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1038 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1039 .path = "show ip6 neighbors",
1040 .function = show_ip6_neighbors,
1041 .short_help = "show ip6 neighbors [<interface>]",
1045 static clib_error_t *
1046 set_ip6_neighbor (vlib_main_t * vm,
1047 unformat_input_t * input, vlib_cli_command_t * cmd)
1049 vnet_main_t *vnm = vnet_get_main ();
1055 int is_no_fib_entry = 0;
1058 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1060 /* intfc, ip6-address, mac-address */
1061 if (unformat (input, "%U %U %U",
1062 unformat_vnet_sw_interface, vnm, &sw_if_index,
1063 unformat_ip6_address, &addr,
1064 unformat_ethernet_address, mac_address))
1067 else if (unformat (input, "delete") || unformat (input, "del"))
1069 else if (unformat (input, "static"))
1071 else if (unformat (input, "no-fib-entry"))
1072 is_no_fib_entry = 1;
1078 return clib_error_return (0, "Missing interface, ip6 or hw address");
1081 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
1082 mac_address, sizeof (mac_address),
1083 is_static, is_no_fib_entry);
1085 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
1086 mac_address, sizeof (mac_address));
1091 * This command is used to manually add an entry to the IPv6 neighbor
1092 * adjacency table. Optionally, the entry can be added as static. It is
1093 * also used to remove an entry from the table. Use the '<em>show ip6
1094 * neighbors</em>' command to display all learned and manually entered entries.
1097 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1098 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1099 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1100 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1103 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1105 .path = "set ip6 neighbor",
1106 .function = set_ip6_neighbor,
1107 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
1113 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1114 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1115 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1116 } icmp6_neighbor_solicitation_or_advertisement_next_t;
1118 static_always_inline uword
1119 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1120 vlib_node_runtime_t * node,
1121 vlib_frame_t * frame,
1122 uword is_solicitation)
1124 vnet_main_t *vnm = vnet_get_main ();
1125 ip6_main_t *im = &ip6_main;
1126 uword n_packets = frame->n_vectors;
1127 u32 *from, *to_next;
1128 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1129 icmp6_neighbor_discovery_option_type_t option_type;
1130 vlib_node_runtime_t *error_node =
1131 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1134 from = vlib_frame_vector_args (frame);
1135 n_left_from = n_packets;
1136 next_index = node->cached_next_index;
1138 if (node->flags & VLIB_NODE_FLAG_TRACE)
1139 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1141 sizeof (icmp6_input_trace_t));
1145 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1146 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1147 n_advertisements_sent = 0;
1149 while (n_left_from > 0)
1151 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1153 while (n_left_from > 0 && n_left_to_next > 0)
1157 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1158 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1159 u32 bi0, options_len0, sw_if_index0, next0, error0;
1160 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1164 bi0 = to_next[0] = from[0];
1169 n_left_to_next -= 1;
1171 p0 = vlib_get_buffer (vm, bi0);
1172 ip0 = vlib_buffer_get_current (p0);
1173 h0 = ip6_next_header (ip0);
1175 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1177 error0 = ICMP6_ERROR_NONE;
1178 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1179 ip6_sadd_link_local =
1180 ip6_address_is_link_local_unicast (&ip0->src_address);
1181 ip6_sadd_unspecified =
1182 ip6_address_is_unspecified (&ip0->src_address);
1184 /* Check that source address is unspecified, link-local or else on-link. */
1185 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1187 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1189 if (ADJ_INDEX_INVALID != src_adj_index0)
1191 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1193 /* Allow all realistic-looking rewrite adjacencies to pass */
1194 ni0 = adj0->lookup_next_index;
1195 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1196 (ni0 < IP6_LOOKUP_N_NEXT);
1198 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1201 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1207 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1211 o0 = (void *) (h0 + 1);
1212 o0 = ((options_len0 == 8 && o0->header.type == option_type
1213 && o0->header.n_data_u64s == 1) ? o0 : 0);
1215 /* If src address unspecified or link local, donot learn neighbor MAC */
1216 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1217 !ip6_sadd_unspecified))
1219 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1222 &h0->target_address,
1223 o0->ethernet_address,
1224 sizeof (o0->ethernet_address),
1228 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1230 /* Check that target address is local to this router. */
1231 fib_node_index_t fei;
1235 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1237 if (~0 == fib_index)
1239 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1243 if (ip6_address_is_link_local_unicast (&h0->target_address))
1245 fei = ip6_fib_table_lookup_exact_match
1246 (ip6_ll_fib_get (sw_if_index0),
1247 &h0->target_address, 128);
1251 fei = ip6_fib_table_lookup_exact_match (fib_index,
1252 &h0->target_address,
1256 if (FIB_NODE_INDEX_INVALID == fei)
1258 /* The target address is not in the FIB */
1260 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1264 if (FIB_ENTRY_FLAG_LOCAL &
1265 fib_entry_get_flags_for_source (fei,
1266 FIB_SOURCE_INTERFACE))
1268 /* It's an address that belongs to one of our interfaces
1272 if (fib_entry_is_sourced
1273 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1274 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
1276 /* The address was added by IPv6 Proxy ND config.
1277 * We should only respond to these if the NS arrived on
1278 * the link that has a matching covering prefix */
1283 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1289 if (is_solicitation)
1290 next0 = (error0 != ICMP6_ERROR_NONE
1291 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1292 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1296 error0 = error0 == ICMP6_ERROR_NONE ?
1297 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1300 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1302 vnet_sw_interface_t *sw_if0;
1303 ethernet_interface_t *eth_if0;
1304 ethernet_header_t *eth0;
1306 /* dst address is either source address or the all-nodes mcast addr */
1307 if (!ip6_sadd_unspecified)
1308 ip0->dst_address = ip0->src_address;
1310 ip6_set_reserved_multicast_address (&ip0->dst_address,
1311 IP6_MULTICAST_SCOPE_link_local,
1312 IP6_MULTICAST_GROUP_ID_all_hosts);
1314 ip0->src_address = h0->target_address;
1315 ip0->hop_limit = 255;
1316 h0->icmp.type = ICMP6_neighbor_advertisement;
1318 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1319 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1321 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1324 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1326 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1329 h0->advertisement_flags = clib_host_to_net_u32
1330 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1331 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1333 h0->icmp.checksum = 0;
1335 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1337 ASSERT (bogus_length == 0);
1339 /* Reuse current MAC header, copy SMAC to DMAC and
1340 * interface MAC to SMAC */
1341 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1342 eth0 = vlib_buffer_get_current (p0);
1343 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1345 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1347 /* Setup input and output sw_if_index for packet */
1348 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1349 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1350 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1351 vnet_main.local_interface_sw_if_index;
1353 n_advertisements_sent++;
1356 p0->error = error_node->errors[error0];
1358 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1359 to_next, n_left_to_next,
1363 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1366 /* Account for advertisements sent. */
1367 vlib_error_count (vm, error_node->node_index,
1368 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1369 n_advertisements_sent);
1371 return frame->n_vectors;
1374 /* for "syslogging" - use elog for now */
1375 #define foreach_log_level \
1376 _ (DEBUG, "DEBUG") \
1377 _ (INFO, "INFORMATION") \
1378 _ (NOTICE, "NOTICE") \
1379 _ (WARNING, "WARNING") \
1381 _ (CRIT, "CRITICAL") \
1382 _ (ALERT, "ALERT") \
1383 _ (EMERG, "EMERGENCY")
1387 #define _(f,s) LOG_##f,
1392 static char *log_level_strings[] = {
1398 static int logmask = 1 << LOG_DEBUG;
1401 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1403 /* just use elog for now */
1407 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1413 what = va_format (0, fmt, &va);
1415 ELOG_TYPE_DECLARE (e) =
1417 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1422 ed = ELOG_DATA (&vm->elog_main, e);
1423 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1424 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1431 call_ip6_neighbor_callbacks (void *data,
1432 _vnet_ip6_neighbor_function_list_elt_t * elt)
1434 clib_error_t *error = 0;
1438 error = elt->fp (data);
1441 elt = elt->next_ip6_neighbor_function;
1447 /* ipv6 neighbor discovery - router advertisements */
1450 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1451 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1452 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1453 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1454 } icmp6_router_solicitation_or_advertisement_next_t;
1456 static_always_inline uword
1457 icmp6_router_solicitation (vlib_main_t * vm,
1458 vlib_node_runtime_t * node, vlib_frame_t * frame)
1460 vnet_main_t *vnm = vnet_get_main ();
1461 ip6_main_t *im = &ip6_main;
1462 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1463 uword n_packets = frame->n_vectors;
1464 u32 *from, *to_next;
1465 u32 n_left_from, n_left_to_next, next_index;
1466 u32 n_advertisements_sent = 0;
1469 icmp6_neighbor_discovery_option_type_t option_type;
1471 vlib_node_runtime_t *error_node =
1472 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1474 from = vlib_frame_vector_args (frame);
1475 n_left_from = n_packets;
1476 next_index = node->cached_next_index;
1478 if (node->flags & VLIB_NODE_FLAG_TRACE)
1479 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1481 sizeof (icmp6_input_trace_t));
1483 /* source may append his LL address */
1484 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1486 while (n_left_from > 0)
1488 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1490 while (n_left_from > 0 && n_left_to_next > 0)
1494 ip6_radv_t *radv_info = 0;
1496 icmp6_neighbor_discovery_header_t *h0;
1497 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1499 u32 bi0, options_len0, sw_if_index0, next0, error0;
1500 u32 is_solicitation = 1, is_dropped = 0;
1501 u32 is_unspecified, is_link_local;
1503 bi0 = to_next[0] = from[0];
1508 n_left_to_next -= 1;
1510 p0 = vlib_get_buffer (vm, bi0);
1511 ip0 = vlib_buffer_get_current (p0);
1512 h0 = ip6_next_header (ip0);
1514 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1515 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1517 ip6_address_is_link_local_unicast (&ip0->src_address);
1519 error0 = ICMP6_ERROR_NONE;
1520 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1522 /* check if solicitation (not from nd_timer node) */
1523 if (ip6_address_is_unspecified (&ip0->dst_address))
1524 is_solicitation = 0;
1526 /* Check that source address is unspecified, link-local or else on-link. */
1527 if (!is_unspecified && !is_link_local)
1529 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1531 if (ADJ_INDEX_INVALID != src_adj_index0)
1533 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1535 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1537 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1542 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1546 /* check for source LL option and process */
1547 o0 = (void *) (h0 + 1);
1548 o0 = ((options_len0 == 8
1549 && o0->header.type == option_type
1550 && o0->header.n_data_u64s == 1) ? o0 : 0);
1552 /* if src address unspecified IGNORE any options */
1553 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1554 !is_unspecified && !is_link_local))
1556 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1558 o0->ethernet_address,
1559 sizeof (o0->ethernet_address),
1563 /* default is to drop */
1564 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1566 if (error0 == ICMP6_ERROR_NONE)
1568 vnet_sw_interface_t *sw_if0;
1569 ethernet_interface_t *eth_if0;
1572 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1573 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1575 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1577 /* only support ethernet interface type for now */
1579 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1582 if (error0 == ICMP6_ERROR_NONE)
1586 /* adjust the sizeof the buffer to just include the ipv6 header */
1587 p0->current_length -=
1589 sizeof (icmp6_neighbor_discovery_header_t));
1591 /* look up the radv_t information for this interface */
1592 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1596 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1599 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1604 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1607 if (error0 == ICMP6_ERROR_NONE)
1609 f64 now = vlib_time_now (vm);
1611 /* for solicited adverts - need to rate limit */
1612 if (is_solicitation)
1614 if (0 != radv_info->last_radv_time &&
1615 (now - radv_info->last_radv_time) <
1616 MIN_DELAY_BETWEEN_RAS)
1619 radv_info->last_radv_time = now;
1623 icmp6_router_advertisement_header_t rh;
1625 rh.icmp.type = ICMP6_router_advertisement;
1627 rh.icmp.checksum = 0;
1629 rh.current_hop_limit = radv_info->curr_hop_limit;
1630 rh.router_lifetime_in_sec =
1631 clib_host_to_net_u16
1632 (radv_info->adv_router_lifetime_in_sec);
1634 time_in_msec_between_retransmitted_neighbor_solicitations
1636 clib_host_to_net_u32 (radv_info->
1637 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1638 rh.neighbor_reachable_time_in_msec =
1639 clib_host_to_net_u32 (radv_info->
1640 adv_neighbor_reachable_time_in_msec);
1643 (radv_info->adv_managed_flag) ?
1644 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1647 ((radv_info->adv_other_flag) ?
1648 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1652 u16 payload_length =
1653 sizeof (icmp6_router_advertisement_header_t);
1655 vlib_buffer_add_data (vm,
1656 vlib_buffer_get_free_list_index
1657 (p0), bi0, (void *) &rh,
1659 (icmp6_router_advertisement_header_t));
1661 if (radv_info->adv_link_layer_address)
1663 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1667 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1668 h.header.n_data_u64s = 1;
1670 /* copy ll address */
1671 clib_memcpy (&h.ethernet_address[0],
1672 eth_if0->address, 6);
1674 vlib_buffer_add_data (vm,
1675 vlib_buffer_get_free_list_index
1676 (p0), bi0, (void *) &h,
1678 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1682 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1685 /* add MTU option */
1686 if (radv_info->adv_link_mtu)
1688 icmp6_neighbor_discovery_mtu_option_t h;
1692 clib_host_to_net_u32 (radv_info->adv_link_mtu);
1693 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1694 h.header.n_data_u64s = 1;
1697 sizeof (icmp6_neighbor_discovery_mtu_option_t);
1699 vlib_buffer_add_data (vm,
1700 vlib_buffer_get_free_list_index
1701 (p0), bi0, (void *) &h,
1703 (icmp6_neighbor_discovery_mtu_option_t));
1706 /* add advertised prefix options */
1707 ip6_radv_prefix_t *pr_info;
1710 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1712 if(pr_info->enabled &&
1713 (!pr_info->decrement_lifetime_flag
1714 || (pr_info->pref_lifetime_expires >0)))
1716 /* advertise this prefix */
1717 icmp6_neighbor_discovery_prefix_information_option_t h;
1719 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1720 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1722 h.dst_address_length = pr_info->prefix_len;
1724 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1725 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1727 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1729 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1730 h.preferred_time = 0;
1734 if(pr_info->decrement_lifetime_flag)
1736 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1737 (pr_info->valid_lifetime_expires - now) : 0;
1739 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1740 (pr_info->pref_lifetime_expires - now) : 0;
1743 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1744 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1748 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1750 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1752 vlib_buffer_add_data (vm,
1753 vlib_buffer_get_free_list_index (p0),
1755 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1761 /* add additional options before here */
1763 /* finish building the router advertisement... */
1764 if (!is_unspecified && radv_info->send_unicast)
1766 ip0->dst_address = ip0->src_address;
1770 /* target address is all-nodes mcast addr */
1771 ip6_set_reserved_multicast_address
1773 IP6_MULTICAST_SCOPE_link_local,
1774 IP6_MULTICAST_GROUP_ID_all_hosts);
1777 /* source address MUST be the link-local address */
1778 ip0->src_address = radv_info->link_local_address;
1780 ip0->hop_limit = 255;
1781 ip0->payload_length =
1782 clib_host_to_net_u16 (payload_length);
1784 icmp6_router_advertisement_header_t *rh0 =
1785 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1786 rh0->icmp.checksum =
1787 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1789 ASSERT (bogus_length == 0);
1791 /* setup output if and adjacency */
1792 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1793 vnet_main.local_interface_sw_if_index;
1795 if (is_solicitation)
1797 ethernet_header_t *eth0;
1798 /* Reuse current MAC header, copy SMAC to DMAC and
1799 * interface MAC to SMAC */
1800 vlib_buffer_reset (p0);
1801 eth0 = vlib_buffer_get_current (p0);
1802 clib_memcpy (eth0->dst_address, eth0->src_address,
1804 clib_memcpy (eth0->src_address, eth_if0->address,
1807 is_dropped ? next0 :
1808 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1809 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1814 adj_index0 = radv_info->mcast_adj_index;
1815 if (adj_index0 == 0)
1816 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1820 is_dropped ? next0 :
1821 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1822 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1826 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1828 radv_info->n_solicitations_dropped += is_dropped;
1829 radv_info->n_solicitations_rcvd += is_solicitation;
1831 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1833 radv_info->n_advertisements_sent++;
1834 n_advertisements_sent++;
1840 p0->error = error_node->errors[error0];
1842 if (error0 != ICMP6_ERROR_NONE)
1843 vlib_error_count (vm, error_node->node_index, error0, 1);
1845 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1846 to_next, n_left_to_next,
1851 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1854 /* Account for router advertisements sent. */
1855 vlib_error_count (vm, error_node->node_index,
1856 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1857 n_advertisements_sent);
1859 return frame->n_vectors;
1862 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1863 static_always_inline uword
1864 icmp6_router_advertisement (vlib_main_t * vm,
1865 vlib_node_runtime_t * node, vlib_frame_t * frame)
1867 vnet_main_t *vnm = vnet_get_main ();
1868 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1869 uword n_packets = frame->n_vectors;
1870 u32 *from, *to_next;
1871 u32 n_left_from, n_left_to_next, next_index;
1872 u32 n_advertisements_rcvd = 0;
1874 vlib_node_runtime_t *error_node =
1875 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1877 from = vlib_frame_vector_args (frame);
1878 n_left_from = n_packets;
1879 next_index = node->cached_next_index;
1881 if (node->flags & VLIB_NODE_FLAG_TRACE)
1882 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1884 sizeof (icmp6_input_trace_t));
1886 while (n_left_from > 0)
1888 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1890 while (n_left_from > 0 && n_left_to_next > 0)
1894 ip6_radv_t *radv_info = 0;
1895 icmp6_router_advertisement_header_t *h0;
1896 u32 bi0, options_len0, sw_if_index0, next0, error0;
1898 bi0 = to_next[0] = from[0];
1903 n_left_to_next -= 1;
1905 p0 = vlib_get_buffer (vm, bi0);
1906 ip0 = vlib_buffer_get_current (p0);
1907 h0 = ip6_next_header (ip0);
1909 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1911 error0 = ICMP6_ERROR_NONE;
1912 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1914 /* Check that source address is link-local */
1915 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1916 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1918 /* default is to drop */
1919 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1921 n_advertisements_rcvd++;
1923 if (error0 == ICMP6_ERROR_NONE)
1925 vnet_sw_interface_t *sw_if0;
1926 ethernet_interface_t *eth_if0;
1928 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1929 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1931 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1933 /* only support ethernet interface type for now */
1935 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1938 if (error0 == ICMP6_ERROR_NONE)
1942 /* look up the radv_t information for this interface */
1943 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1947 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1950 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1955 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1958 if (error0 == ICMP6_ERROR_NONE)
1960 radv_info->keep_sending_rs = 0;
1964 r.sw_if_index = sw_if_index0;
1965 memcpy (r.router_address, &ip0->src_address, 16);
1966 r.current_hop_limit = h0->current_hop_limit;
1967 r.flags = h0->flags;
1968 r.router_lifetime_in_sec =
1969 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
1970 r.neighbor_reachable_time_in_msec =
1971 clib_net_to_host_u32
1972 (h0->neighbor_reachable_time_in_msec);
1973 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
1976 /* validate advertised information */
1977 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1978 && (h0->current_hop_limit !=
1979 radv_info->curr_hop_limit))
1981 ip6_neighbor_syslog (vm, LOG_WARNING,
1982 "our AdvCurHopLimit on %U doesn't agree with %U",
1983 format_vnet_sw_if_index_name,
1990 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1991 != radv_info->adv_managed_flag)
1993 ip6_neighbor_syslog (vm, LOG_WARNING,
1994 "our AdvManagedFlag on %U doesn't agree with %U",
1995 format_vnet_sw_if_index_name,
2002 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2003 != radv_info->adv_other_flag)
2005 ip6_neighbor_syslog (vm, LOG_WARNING,
2006 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2007 format_vnet_sw_if_index_name,
2014 time_in_msec_between_retransmitted_neighbor_solicitations
2016 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2018 time_in_msec_between_retransmitted_neighbor_solicitations
2020 clib_host_to_net_u32 (radv_info->
2021 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
2023 ip6_neighbor_syslog (vm, LOG_WARNING,
2024 "our AdvRetransTimer on %U doesn't agree with %U",
2025 format_vnet_sw_if_index_name,
2031 if ((h0->neighbor_reachable_time_in_msec &&
2032 radv_info->adv_neighbor_reachable_time_in_msec) &&
2033 (h0->neighbor_reachable_time_in_msec !=
2034 clib_host_to_net_u32
2035 (radv_info->adv_neighbor_reachable_time_in_msec)))
2037 ip6_neighbor_syslog (vm, LOG_WARNING,
2038 "our AdvReachableTime on %U doesn't agree with %U",
2039 format_vnet_sw_if_index_name,
2045 /* check for MTU or prefix options or .. */
2046 u8 *opt_hdr = (u8 *) (h0 + 1);
2047 while (options_len0 > 0)
2049 icmp6_neighbor_discovery_option_header_t *o0 =
2050 (icmp6_neighbor_discovery_option_header_t *)
2052 int opt_len = o0->n_data_u64s << 3;
2053 icmp6_neighbor_discovery_option_type_t option_type =
2056 if (options_len0 < 2)
2058 ip6_neighbor_syslog (vm, LOG_ERR,
2059 "malformed RA packet on %U from %U",
2060 format_vnet_sw_if_index_name,
2069 ip6_neighbor_syslog (vm, LOG_ERR,
2070 " zero length option in RA on %U from %U",
2071 format_vnet_sw_if_index_name,
2077 else if (opt_len > options_len0)
2079 ip6_neighbor_syslog (vm, LOG_ERR,
2080 "option length in RA packet greater than total length on %U from %U",
2081 format_vnet_sw_if_index_name,
2088 options_len0 -= opt_len;
2091 switch (option_type)
2093 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2095 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2097 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2100 if (opt_len < sizeof (*h))
2103 memcpy (r.slla, h->ethernet_address, 6);
2107 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
2109 icmp6_neighbor_discovery_mtu_option_t *h =
2110 (icmp6_neighbor_discovery_mtu_option_t
2113 if (opt_len < sizeof (*h))
2116 r.mtu = clib_net_to_host_u32 (h->mtu);
2118 if ((h->mtu && radv_info->adv_link_mtu) &&
2120 clib_host_to_net_u32
2121 (radv_info->adv_link_mtu)))
2123 ip6_neighbor_syslog (vm, LOG_WARNING,
2124 "our AdvLinkMTU on %U doesn't agree with %U",
2125 format_vnet_sw_if_index_name,
2133 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2135 icmp6_neighbor_discovery_prefix_information_option_t
2137 (icmp6_neighbor_discovery_prefix_information_option_t
2140 /* validate advertised prefix options */
2141 ip6_radv_prefix_t *pr_info;
2142 u32 preferred, valid;
2144 if (opt_len < sizeof (*h))
2147 vec_validate (r.prefixes,
2148 vec_len (r.prefixes));
2149 ra_report_prefix_info_t *prefix =
2150 vec_elt_at_index (r.prefixes,
2151 vec_len (r.prefixes) - 1);
2154 clib_net_to_host_u32 (h->preferred_time);
2155 valid = clib_net_to_host_u32 (h->valid_time);
2157 prefix->preferred_time = preferred;
2158 prefix->valid_time = valid;
2159 prefix->flags = h->flags & 0xc0;
2160 prefix->dst_address_length =
2161 h->dst_address_length;
2162 prefix->dst_address = h->dst_address;
2164 /* look for matching prefix - if we our advertising it, it better be consistant */
2166 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2170 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2172 if(pr_info->enabled &&
2173 (pr_info->prefix_len == h->dst_address_length) &&
2174 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2177 if(!pr_info->decrement_lifetime_flag &&
2178 valid != pr_info->adv_valid_lifetime_in_secs)
2180 ip6_neighbor_syslog(vm, LOG_WARNING,
2181 "our ADV validlifetime on %U for %U does not agree with %U",
2182 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2183 format_ip6_address, &h->dst_address);
2185 if(!pr_info->decrement_lifetime_flag &&
2186 preferred != pr_info->adv_pref_lifetime_in_secs)
2188 ip6_neighbor_syslog(vm, LOG_WARNING,
2189 "our ADV preferredlifetime on %U for %U does not agree with %U",
2190 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2191 format_ip6_address, &h->dst_address);
2209 p0->error = error_node->errors[error0];
2211 if (error0 != ICMP6_ERROR_NONE)
2212 vlib_error_count (vm, error_node->node_index, error0, 1);
2214 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2215 to_next, n_left_to_next,
2219 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2222 /* Account for router advertisements received. */
2223 vlib_error_count (vm, error_node->node_index,
2224 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2225 n_advertisements_rcvd);
2227 return frame->n_vectors;
2231 random_f64_from_to (f64 from, f64 to)
2233 static u32 seed = 0;
2234 static u8 seed_set = 0;
2237 seed = random_default_seed ();
2240 return random_f64 (&seed) * (to - from) + from;
2244 get_mac_address (u32 sw_if_index, u8 * address)
2246 vnet_main_t *vnm = vnet_get_main ();
2247 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2248 if (!hw_if->hw_address)
2250 clib_memcpy (address, hw_if->hw_address, 6);
2254 static inline vlib_buffer_t *
2255 create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2259 vlib_buffer_free_list_t *fl;
2260 icmp6_router_solicitation_header_t *rh;
2265 sw_if_index = radv_info->sw_if_index;
2267 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2269 clib_warning ("buffer allocation failure");
2273 p0 = vlib_get_buffer (vm, bi0);
2274 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2275 vlib_buffer_init_for_free_list (p0, fl);
2276 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2277 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2279 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2280 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2282 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2284 rh = vlib_buffer_get_current (p0);
2285 p0->current_length = sizeof (*rh);
2287 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2288 rh->neighbor.icmp.code = 0;
2289 rh->neighbor.icmp.checksum = 0;
2290 rh->neighbor.reserved_must_be_zero = 0;
2292 rh->link_layer_option.header.type =
2293 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2294 if (0 != get_mac_address (sw_if_index,
2295 rh->link_layer_option.ethernet_address))
2297 clib_warning ("interface with sw_if_index %u has no mac address",
2299 vlib_buffer_free (vm, &bi0, 1);
2302 rh->link_layer_option.header.n_data_u64s = 1;
2304 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2306 rh->ip.ip_version_traffic_class_and_flow_label =
2307 clib_host_to_net_u32 (0x6 << 28);
2308 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2309 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2310 rh->ip.hop_limit = 255;
2311 rh->ip.src_address = radv_info->link_local_address;
2312 /* set address ff02::2 */
2313 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02L << 48);
2314 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2316 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2324 stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2328 ra->keep_sending_rs = 0;
2331 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2332 vlib_buffer_free (vm, &bi0, 1);
2338 check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2348 icmp6_send_router_solicitation_params_t *params;
2350 if (!radv_info->keep_sending_rs)
2353 params = &radv_info->params;
2355 if (radv_info->due_time > current_time)
2357 *due_time = radv_info->due_time;
2361 p0 = radv_info->buffer;
2363 next_index = ip6_rewrite_mcast_node.index;
2365 c0 = vlib_buffer_copy (vm, p0);
2366 ci0 = vlib_get_buffer_index (vm, c0);
2368 f = vlib_get_frame_to_node (vm, next_index);
2369 to_next = vlib_frame_vector_args (f);
2372 vlib_put_frame_to_node (vm, next_index, f);
2374 if (params->mrc != 0 && --radv_info->n_left == 0)
2375 stop_sending_rs (vm, radv_info);
2378 radv_info->sleep_interval =
2379 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2380 if (radv_info->sleep_interval > params->mrt)
2381 radv_info->sleep_interval =
2382 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2384 radv_info->due_time = current_time + radv_info->sleep_interval;
2386 if (params->mrd != 0
2387 && current_time > radv_info->start_time + params->mrd)
2388 stop_sending_rs (vm, radv_info);
2390 *due_time = radv_info->due_time;
2393 return radv_info->keep_sending_rs;
2397 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2400 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2401 ip6_radv_t *radv_info;
2402 uword *event_data = 0;
2403 f64 sleep_time = 1e9;
2410 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2411 vlib_process_get_events (vm, &event_data);
2412 vec_reset_length (event_data);
2414 current_time = vlib_time_now (vm);
2417 due_time = current_time + 1e9;
2419 pool_foreach (radv_info, nm->if_radv_pool,
2421 if (check_send_rs (vm, radv_info, current_time, &dt)
2426 current_time = vlib_time_now (vm);
2428 while (due_time < current_time);
2430 sleep_time = due_time - current_time;
2437 VLIB_REGISTER_NODE (send_rs_process_node) = {
2438 .function = send_rs_process,
2439 .type = VLIB_NODE_TYPE_PROCESS,
2440 .name = "send-rs-process",
2445 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2446 icmp6_send_router_solicitation_params_t *
2449 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2453 ASSERT (~0 != sw_if_index);
2455 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2456 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2458 stop_sending_rs (vm, ra);
2462 ra->keep_sending_rs = 1;
2463 ra->params = *params;
2464 ra->n_left = params->mrc;
2465 ra->start_time = vlib_time_now (vm);
2466 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2467 ra->due_time = 0; /* send first packet ASAP */
2468 ra->buffer = create_buffer_for_rs (vm, ra);
2470 ra->keep_sending_rs = 0;
2472 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2477 * @brief Add a multicast Address to the advertised MLD set
2480 ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2482 ip6_mldp_group_t *mcast_group_info;
2485 /* lookup mldp info for this interface */
2486 p = mhash_get (&radv_info->address_to_mldp_index, addr);
2488 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2491 if (!mcast_group_info)
2495 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2497 mi = mcast_group_info - radv_info->mldp_group_pool;
2498 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
2501 mcast_group_info->type = 4;
2502 mcast_group_info->mcast_source_address_pool = 0;
2503 mcast_group_info->num_sources = 0;
2504 clib_memcpy (&mcast_group_info->mcast_address, addr,
2505 sizeof (ip6_address_t));
2510 * @brief Delete a multicast Address from the advertised MLD set
2513 ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2515 ip6_mldp_group_t *mcast_group_info;
2518 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2520 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2522 if (mcast_group_info)
2524 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2526 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2531 * @brief Add a multicast Address to the advertised MLD set
2534 ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2535 ip6_multicast_address_scope_t scope,
2536 ip6_multicast_link_local_group_id_t group)
2540 ip6_set_reserved_multicast_address (&addr, scope, group);
2542 ip6_neighbor_add_mld_prefix (a, &addr);
2546 * @brief create and initialize router advertisement parameters with default
2547 * values for this intfc
2550 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
2551 u32 sw_if_index, u32 is_add)
2553 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2556 vnet_sw_interface_t *sw_if0;
2557 ethernet_interface_t *eth_if0 = 0;
2559 /* lookup radv container - ethernet interfaces only */
2560 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2561 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2562 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2567 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2569 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2573 a = pool_elt_at_index (nm->if_radv_pool, ri);
2577 ip6_radv_prefix_t *p;
2578 ip6_mldp_group_t *m;
2580 /* release the lock on the interface's mcast adj */
2581 adj_unlock (a->mcast_adj_index);
2583 /* clean up prefix and MDP pools */
2585 pool_flush(p, a->adv_prefixes_pool,
2587 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
2589 pool_flush (m, a->mldp_group_pool,
2591 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
2595 pool_free (a->mldp_group_pool);
2596 pool_free (a->adv_prefixes_pool);
2598 mhash_free (&a->address_to_prefix_index);
2599 mhash_free (&a->address_to_mldp_index);
2601 if (a->keep_sending_rs)
2602 a->keep_sending_rs = 0;
2604 pool_put (nm->if_radv_pool, a);
2605 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2613 vnet_hw_interface_t *hw_if0;
2615 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2617 pool_get (nm->if_radv_pool, a);
2619 ri = a - nm->if_radv_pool;
2620 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2622 /* initialize default values (most of which are zero) */
2623 memset (a, 0, sizeof (a[0]));
2625 a->sw_if_index = sw_if_index;
2626 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2627 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2628 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2629 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2631 /* send ll address source address option */
2632 a->adv_link_layer_address = 1;
2634 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2635 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2636 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2637 a->seed = (u32) clib_cpu_time_now ();
2638 (void) random_u32 (&a->seed);
2639 a->randomizer = clib_cpu_time_now ();
2640 (void) random_u64 (&a->randomizer);
2642 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2643 a->initial_adverts_sent = a->initial_adverts_count - 1;
2644 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2646 /* deafult is to send */
2649 /* fill in radv_info for this interface that will be needed later */
2650 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2652 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2654 /* fill in default link-local address (this may be overridden) */
2655 ip6_link_local_address_from_ethernet_address
2656 (&a->link_local_address, eth_if0->address);
2658 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2659 sizeof (ip6_address_t));
2660 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2661 sizeof (ip6_address_t));
2663 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2667 a->keep_sending_rs = 0;
2669 /* add multicast groups we will always be reporting */
2670 ip6_neighbor_add_mld_grp (a,
2671 IP6_MULTICAST_SCOPE_link_local,
2672 IP6_MULTICAST_GROUP_ID_all_hosts);
2673 ip6_neighbor_add_mld_grp (a,
2674 IP6_MULTICAST_SCOPE_link_local,
2675 IP6_MULTICAST_GROUP_ID_all_routers);
2676 ip6_neighbor_add_mld_grp (a,
2677 IP6_MULTICAST_SCOPE_link_local,
2678 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2684 /* send an mldpv2 report */
2686 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2688 vnet_main_t *vnm = vnet_get_main ();
2689 vlib_main_t *vm = vnm->vlib_main;
2690 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2691 vnet_sw_interface_t *sw_if0;
2692 ethernet_interface_t *eth_if0;
2696 ip6_radv_t *radv_info;
2706 icmp6_multicast_listener_report_header_t *rh0;
2707 icmp6_multicast_listener_report_packet_t *rp0;
2709 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2710 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2711 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2713 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2716 /* look up the radv_t information for this interface */
2717 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2720 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2725 /* send report now - build a mldpv2 report packet */
2726 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2729 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2730 if (PREDICT_FALSE (n_allocated == 0))
2732 clib_warning ("buffer allocation failure");
2736 b0 = vlib_get_buffer (vm, bo0);
2738 /* adjust the sizeof the buffer to just include the ipv6 header */
2739 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2741 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2743 b0->error = ICMP6_ERROR_NONE;
2745 rp0 = vlib_buffer_get_current (b0);
2746 ip0 = (ip6_header_t *) & rp0->ip;
2747 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2749 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2751 ip0->ip_version_traffic_class_and_flow_label =
2752 clib_host_to_net_u32 (0x6 << 28);
2754 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2755 /* for DEBUG - vnet driver won't seem to emit router alerts */
2756 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2759 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2761 /* source address MUST be the link-local address */
2762 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2763 ip0->src_address = radv_info->link_local_address;
2765 /* destination is all mldpv2 routers */
2766 ip6_set_reserved_multicast_address (&ip0->dst_address,
2767 IP6_MULTICAST_SCOPE_link_local,
2768 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2770 /* add reports here */
2771 ip6_mldp_group_t *m;
2772 int num_addr_records = 0;
2773 icmp6_multicast_address_record_t rr;
2775 /* fill in the hop-by-hop extension header (router alert) info */
2776 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2777 rh0->ext_hdr.n_data_u64s = 0;
2779 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2781 rh0->alert.value = 0;
2786 rh0->icmp.checksum = 0;
2789 pool_foreach (m, radv_info->mldp_group_pool,
2792 rr.aux_data_len_u32s = 0;
2793 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2794 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2798 vlib_buffer_add_data
2799 (vm, vlib_buffer_get_free_list_index (b0), bo0,
2800 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
2802 payload_length += sizeof( icmp6_multicast_address_record_t);
2807 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2809 /* update lengths */
2810 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2812 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2814 ASSERT (bogus_length == 0);
2817 * OK to override w/ no regard for actual FIB, because
2818 * ip6-rewrite only looks at the adjacency.
2820 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2821 vnet_main.local_interface_sw_if_index;
2823 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2824 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2826 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2828 f = vlib_get_frame_to_node (vm, node->index);
2829 to_next = vlib_frame_vector_args (f);
2833 vlib_put_frame_to_node (vm, node->index, f);
2838 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2840 .function = icmp6_router_solicitation,
2841 .name = "icmp6-router-solicitation",
2843 .vector_size = sizeof (u32),
2845 .format_trace = format_icmp6_input_trace,
2847 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2849 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
2850 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2851 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2856 /* send a RA or update the timer info etc.. */
2858 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2859 vlib_node_runtime_t * node,
2860 vlib_frame_t * frame)
2862 vnet_main_t *vnm = vnet_get_main ();
2863 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2864 ip6_radv_t *radv_info;
2865 vlib_frame_t *f = 0;
2866 u32 n_this_frame = 0;
2867 u32 n_left_to_next = 0;
2870 icmp6_router_solicitation_header_t *h0;
2872 f64 now = vlib_time_now (vm);
2874 /* Interface ip6 radv info list */
2876 pool_foreach (radv_info, nm->if_radv_pool,
2878 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2880 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2881 radv_info->next_multicast_time = now;
2882 radv_info->last_multicast_time = now;
2883 radv_info->last_radv_time = 0;
2884 radv_info->all_routers_mcast = 0;
2888 /* Make sure that we've joined the all-routers multicast group */
2889 if(!radv_info->all_routers_mcast)
2891 /* send MDLP_REPORT_EVENT message */
2892 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2893 radv_info->all_routers_mcast = 1;
2896 /* is it time to send a multicast RA on this interface? */
2897 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2902 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2903 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2905 /* multicast send - compute next multicast send time */
2906 if( radv_info->initial_adverts_sent > 0)
2908 radv_info->initial_adverts_sent--;
2909 if(rfn > radv_info-> initial_adverts_interval)
2910 rfn = radv_info-> initial_adverts_interval;
2912 /* check to see if we are ceasing to send */
2913 if( radv_info->initial_adverts_sent == 0)
2914 if(radv_info->cease_radv)
2915 radv_info->send_radv = 0;
2918 radv_info->next_multicast_time = rfn + now;
2919 radv_info->last_multicast_time = now;
2921 /* send advert now - build a "solicted" router advert with unspecified source address */
2922 n_allocated = vlib_buffer_alloc_from_free_list
2923 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2925 if (PREDICT_FALSE(n_allocated == 0))
2927 clib_warning ("buffer allocation failure");
2930 b0 = vlib_get_buffer (vm, bo0);
2931 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2932 b0->error = ICMP6_ERROR_NONE;
2933 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2935 h0 = vlib_buffer_get_current (b0);
2937 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2939 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2940 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2941 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2942 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2943 h0->ip.hop_limit = 255;
2945 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2946 h0->ip.src_address.as_u64[0] = 0;
2947 h0->ip.src_address.as_u64[1] = 0;
2949 h0->ip.dst_address.as_u64[0] = 0;
2950 h0->ip.dst_address.as_u64[1] = 0;
2952 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2954 if (PREDICT_FALSE(f == 0))
2956 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2957 to_next = vlib_frame_vector_args (f);
2958 n_left_to_next = VLIB_FRAME_SIZE;
2967 if (PREDICT_FALSE(n_left_to_next == 0))
2969 f->n_vectors = n_this_frame;
2970 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2979 ASSERT (n_this_frame);
2980 f->n_vectors = n_this_frame;
2981 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2987 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2988 vlib_node_runtime_t * node,
2989 vlib_frame_t * frame)
2992 ip6_icmp_neighbor_discovery_event_data_t *event_data;
2994 /* init code here */
2998 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
3000 event_data = vlib_process_get_event_data (vm, &event_type);
3004 /* No events found: timer expired. */
3005 /* process interface list and send RAs as appropriate, update timer info */
3006 ip6_neighbor_process_timer_event (vm, node, frame);
3013 case ICMP6_ND_EVENT_INIT:
3024 _vec_len (event_data) = 0;
3027 return frame->n_vectors;
3031 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3033 .function = icmp6_router_advertisement,
3034 .name = "icmp6-router-advertisement",
3036 .vector_size = sizeof (u32),
3038 .format_trace = format_icmp6_input_trace,
3047 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3049 .function = ip6_icmp_neighbor_discovery_event_process,
3050 .name = "ip6-icmp-neighbor-discovery-event-process",
3051 .type = VLIB_NODE_TYPE_PROCESS,
3055 icmp6_neighbor_solicitation (vlib_main_t * vm,
3056 vlib_node_runtime_t * node, vlib_frame_t * frame)
3058 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3059 /* is_solicitation */
3064 icmp6_neighbor_advertisement (vlib_main_t * vm,
3065 vlib_node_runtime_t * node,
3066 vlib_frame_t * frame)
3068 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3069 /* is_solicitation */
3074 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3076 .function = icmp6_neighbor_solicitation,
3077 .name = "icmp6-neighbor-solicitation",
3079 .vector_size = sizeof (u32),
3081 .format_trace = format_icmp6_input_trace,
3083 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3085 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
3086 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3092 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3094 .function = icmp6_neighbor_advertisement,
3095 .name = "icmp6-neighbor-advertisement",
3097 .vector_size = sizeof (u32),
3099 .format_trace = format_icmp6_input_trace,
3110 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3111 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3112 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3113 } ip6_discover_neighbor_next_t;
3117 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3118 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3119 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3120 } ip6_discover_neighbor_error_t;
3123 ip6_discover_neighbor_inline (vlib_main_t * vm,
3124 vlib_node_runtime_t * node,
3125 vlib_frame_t * frame, int is_glean)
3127 vnet_main_t *vnm = vnet_get_main ();
3128 ip6_main_t *im = &ip6_main;
3129 ip_lookup_main_t *lm = &im->lookup_main;
3130 u32 *from, *to_next_drop;
3131 uword n_left_from, n_left_to_next_drop;
3132 static f64 time_last_seed_change = -1e100;
3133 static u32 hash_seeds[3];
3134 static uword hash_bitmap[256 / BITS (uword)];
3137 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3139 if (node->flags & VLIB_NODE_FLAG_TRACE)
3140 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3142 time_now = vlib_time_now (vm);
3143 if (time_now - time_last_seed_change > 1e-3)
3146 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
3147 sizeof (hash_seeds));
3148 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
3149 hash_seeds[i] = r[i];
3151 /* Mark all hash keys as been not-seen before. */
3152 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
3155 time_last_seed_change = time_now;
3158 from = vlib_frame_vector_args (frame);
3159 n_left_from = frame->n_vectors;
3161 while (n_left_from > 0)
3163 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3164 to_next_drop, n_left_to_next_drop);
3166 while (n_left_from > 0 && n_left_to_next_drop > 0)
3170 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
3172 ip_adjacency_t *adj0;
3173 vnet_hw_interface_t *hw_if0;
3174 ip6_radv_t *radv_info;
3179 p0 = vlib_get_buffer (vm, pi0);
3181 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3183 ip0 = vlib_buffer_get_current (p0);
3185 adj0 = adj_get (adj_index0);
3189 ip0->dst_address.as_u64[0] =
3190 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3191 ip0->dst_address.as_u64[1] =
3192 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3199 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3200 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3203 b0 ^= ip0->dst_address.as_u32[0];
3204 c0 ^= ip0->dst_address.as_u32[1];
3206 hash_v3_mix32 (a0, b0, c0);
3208 b0 ^= ip0->dst_address.as_u32[2];
3209 c0 ^= ip0->dst_address.as_u32[3];
3211 hash_v3_finalize32 (a0, b0, c0);
3213 c0 &= BITS (hash_bitmap) - 1;
3214 c0 = c0 / BITS (uword);
3215 m0 = (uword) 1 << (c0 % BITS (uword));
3217 bm0 = hash_bitmap[c0];
3218 drop0 = (bm0 & m0) != 0;
3220 /* Mark it as seen. */
3221 hash_bitmap[c0] = bm0 | m0;
3225 to_next_drop[0] = pi0;
3227 n_left_to_next_drop -= 1;
3229 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3231 /* If the interface is link-down, drop the pkt */
3232 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3235 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3237 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3240 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3248 * the adj has been updated to a rewrite but the node the DPO that got
3249 * us here hasn't - yet. no big deal. we'll drop while we wait.
3251 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3255 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3256 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3263 icmp6_neighbor_solicitation_header_t *h0;
3266 h0 = vlib_packet_template_get_packet
3267 (vm, &im->discover_neighbor_packet_template, &bi0);
3270 * Build ethernet header.
3271 * Choose source address based on destination lookup
3274 if (!ip6_src_address_for_packet (lm,
3277 &h0->ip.src_address))
3279 /* There is no address on the interface */
3281 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3282 vlib_buffer_free (vm, &bi0, 1);
3287 * Destination address is a solicited node multicast address.
3288 * We need to fill in
3289 * the low 24 bits with low 24 bits of target's address.
3291 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3292 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3293 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3295 h0->neighbor.target_address = ip0->dst_address;
3297 clib_memcpy (h0->link_layer_option.ethernet_address,
3298 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3300 /* $$$$ appears we need this; why is the checksum non-zero? */
3301 h0->neighbor.icmp.checksum = 0;
3302 h0->neighbor.icmp.checksum =
3303 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3306 ASSERT (bogus_length == 0);
3308 vlib_buffer_copy_trace_flag (vm, p0, bi0);
3309 b0 = vlib_get_buffer (vm, bi0);
3310 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3311 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3313 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3314 radv_info->mcast_adj_index;
3316 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3317 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3319 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3323 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3324 n_left_to_next_drop);
3327 return frame->n_vectors;
3331 ip6_discover_neighbor (vlib_main_t * vm,
3332 vlib_node_runtime_t * node, vlib_frame_t * frame)
3334 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3338 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3340 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3343 static char *ip6_discover_neighbor_error_strings[] = {
3344 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3345 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3346 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3347 = "no source address for ND solicitation",
3351 VLIB_REGISTER_NODE (ip6_glean_node) =
3353 .function = ip6_glean,
3354 .name = "ip6-glean",
3355 .vector_size = sizeof (u32),
3356 .format_trace = format_ip6_forward_next_trace,
3357 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3358 .error_strings = ip6_discover_neighbor_error_strings,
3359 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3362 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3363 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3366 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3368 .function = ip6_discover_neighbor,
3369 .name = "ip6-discover-neighbor",
3370 .vector_size = sizeof (u32),
3371 .format_trace = format_ip6_forward_next_trace,
3372 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3373 .error_strings = ip6_discover_neighbor_error_strings,
3374 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3377 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3378 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3383 /* API support functions */
3385 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3386 u8 suppress, u8 managed, u8 other,
3387 u8 ll_option, u8 send_unicast, u8 cease,
3388 u8 use_lifetime, u32 lifetime,
3389 u32 initial_count, u32 initial_interval,
3390 u32 max_interval, u32 min_interval, u8 is_no)
3392 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3396 /* look up the radv_t information for this interface */
3397 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3399 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3400 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3405 ip6_radv_t *radv_info;
3406 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3408 if ((max_interval != 0) && (min_interval == 0))
3409 min_interval = .75 * max_interval;
3413 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3414 radv_info->max_radv_interval;
3417 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3418 radv_info->min_radv_interval;
3421 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3422 radv_info->adv_router_lifetime_in_sec;
3426 if (lifetime > MAX_DEF_RTR_LIFETIME)
3427 lifetime = MAX_DEF_RTR_LIFETIME;
3429 if (lifetime <= max_interval)
3430 return VNET_API_ERROR_INVALID_VALUE;
3433 if (min_interval != 0)
3435 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3436 return VNET_API_ERROR_INVALID_VALUE;
3439 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3440 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3441 return VNET_API_ERROR_INVALID_VALUE;
3444 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3445 if "flag" is clear don't change corresponding value
3447 radv_info->send_radv =
3448 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3449 radv_info->adv_managed_flag =
3450 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3451 radv_info->adv_other_flag =
3452 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3453 radv_info->adv_link_layer_address =
3455 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3456 radv_info->send_unicast =
3457 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3458 radv_info->cease_radv =
3459 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3461 radv_info->min_radv_interval = min_interval;
3462 radv_info->max_radv_interval = max_interval;
3463 radv_info->adv_router_lifetime_in_sec = lifetime;
3465 radv_info->initial_adverts_count =
3467 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3468 radv_info->initial_adverts_count;
3469 radv_info->initial_adverts_interval =
3470 (initial_interval !=
3471 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3472 radv_info->initial_adverts_interval;
3475 if ((cease != 0) && (is_no))
3476 radv_info->send_radv = 1;
3478 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3479 radv_info->next_multicast_time = vlib_time_now (vm);
3480 radv_info->last_multicast_time = vlib_time_now (vm);
3481 radv_info->last_radv_time = 0;
3487 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3488 ip6_address_t * prefix_addr, u8 prefix_len,
3489 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3490 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3491 u8 no_onlink, u8 is_no)
3493 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3498 /* look up the radv_t information for this interface */
3499 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3502 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3504 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3508 f64 now = vlib_time_now (vm);
3509 ip6_radv_t *radv_info;
3510 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3512 /* prefix info add, delete or update */
3513 ip6_radv_prefix_t *prefix;
3515 /* lookup prefix info for this address on this interface */
3516 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3518 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3524 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3526 if (prefix->prefix_len != prefix_len)
3527 return VNET_API_ERROR_INVALID_VALUE_2;
3529 /* FIXME - Should the DP do this or the CP ? */
3530 /* do specific delete processing here before returning */
3531 /* try to remove from routing table */
3533 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3535 pool_put (radv_info->adv_prefixes_pool, prefix);
3537 radv_info->initial_adverts_sent =
3538 radv_info->initial_adverts_count - 1;
3539 radv_info->next_multicast_time = vlib_time_now (vm);
3540 radv_info->last_multicast_time = vlib_time_now (vm);
3541 radv_info->last_radv_time = 0;
3545 /* adding or changing */
3550 pool_get (radv_info->adv_prefixes_pool, prefix);
3551 pi = prefix - radv_info->adv_prefixes_pool;
3552 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3555 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3557 prefix->prefix_len = prefix_len;
3558 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3560 /* initialize default values */
3561 prefix->adv_on_link_flag = 1; /* L bit set */
3562 prefix->adv_autonomous_flag = 1; /* A bit set */
3563 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
3564 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3565 prefix->enabled = 1;
3566 prefix->decrement_lifetime_flag = 1;
3567 prefix->deprecated_prefix_flag = 1;
3571 /* FIXME - Should the DP do this or the CP ? */
3572 /* insert prefix into routing table as a connected prefix */
3581 if (prefix->prefix_len != prefix_len)
3582 return VNET_API_ERROR_INVALID_VALUE_2;
3586 /* FIXME - Should the DP do this or the CP ? */
3587 /* remove from routing table if already there */
3591 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
3593 prefix->adv_valid_lifetime_in_secs = ~0;
3594 prefix->adv_pref_lifetime_in_secs = ~0;
3595 prefix->decrement_lifetime_flag = 0;
3599 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3600 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
3603 /* copy remaining */
3604 prefix->enabled = !(no_advertise != 0);
3605 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3606 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3610 /* fill in the expiration times */
3611 prefix->valid_lifetime_expires =
3612 now + prefix->adv_valid_lifetime_in_secs;
3613 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
3615 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3616 radv_info->next_multicast_time = vlib_time_now (vm);
3617 radv_info->last_multicast_time = vlib_time_now (vm);
3618 radv_info->last_radv_time = 0;
3624 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3625 vlib_cli_command_t * cmd)
3627 vnet_main_t *vnm = vnet_get_main ();
3628 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3629 clib_error_t *error = 0;
3631 u8 suppress = 0, managed = 0, other = 0;
3632 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
3633 u8 use_lifetime = 0;
3634 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3635 0, ra_initial_interval = 0;
3636 u32 ra_max_interval = 0, ra_min_interval = 0;
3638 unformat_input_t _line_input, *line_input = &_line_input;
3639 vnet_sw_interface_t *sw_if0;
3641 int add_radv_info = 1;
3642 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
3643 ip6_address_t ip6_addr;
3647 /* Get a line of input. */
3648 if (!unformat_user (main_input, unformat_line_input, line_input))
3651 /* get basic radv info for this interface */
3652 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3655 if (unformat_user (line_input,
3656 unformat_vnet_sw_interface, vnm, &sw_if_index))
3659 ethernet_interface_t *eth_if0 = 0;
3661 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3662 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3664 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
3669 clib_error_return (0, "Interface must be of ethernet type");
3673 /* look up the radv_t information for this interface */
3674 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3677 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3681 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3685 error = clib_error_return (0, "unknown interface %U'",
3686 format_unformat_error, line_input);
3692 error = clib_error_return (0, "invalid interface name %U'",
3693 format_unformat_error, line_input);
3698 /* get the rest of the command */
3699 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3701 if (unformat (line_input, "no"))
3703 else if (unformat (line_input, "prefix %U/%d",
3704 unformat_ip6_address, &ip6_addr, &addr_len))
3709 else if (unformat (line_input, "ra-managed-config-flag"))
3714 else if (unformat (line_input, "ra-other-config-flag"))
3719 else if (unformat (line_input, "ra-suppress") ||
3720 unformat (line_input, "ra-surpress"))
3725 else if (unformat (line_input, "ra-suppress-link-layer") ||
3726 unformat (line_input, "ra-surpress-link-layer"))
3728 suppress_ll_option = 1;
3731 else if (unformat (line_input, "ra-send-unicast"))
3736 else if (unformat (line_input, "ra-lifetime"))
3738 if (!unformat (line_input, "%d", &ra_lifetime))
3740 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);
3756 else if (unformat (line_input, "ra-interval"))
3758 if (!unformat (line_input, "%d", &ra_max_interval))
3760 error = unformat_parse_error (line_input);
3764 if (!unformat (line_input, "%d", &ra_min_interval))
3765 ra_min_interval = 0;
3768 else if (unformat (line_input, "ra-cease"))
3775 error = unformat_parse_error (line_input);
3782 ip6_neighbor_ra_config (vm, sw_if_index,
3783 suppress, managed, other,
3784 suppress_ll_option, send_unicast, cease,
3785 use_lifetime, ra_lifetime,
3786 ra_initial_count, ra_initial_interval,
3787 ra_max_interval, ra_min_interval, is_no);
3791 u32 valid_lifetime_in_secs = 0;
3792 u32 pref_lifetime_in_secs = 0;
3793 u8 use_prefix_default_values = 0;
3794 u8 no_advertise = 0;
3796 u8 no_autoconfig = 0;
3799 /* get the rest of the command */
3800 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3802 if (unformat (line_input, "default"))
3804 use_prefix_default_values = 1;
3807 else if (unformat (line_input, "infinite"))
3809 valid_lifetime_in_secs = ~0;
3810 pref_lifetime_in_secs = ~0;
3813 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3814 &pref_lifetime_in_secs))
3821 /* get the rest of the command */
3822 while (!use_prefix_default_values &&
3823 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3825 if (unformat (line_input, "no-advertise"))
3827 else if (unformat (line_input, "off-link"))
3829 else if (unformat (line_input, "no-autoconfig"))
3831 else if (unformat (line_input, "no-onlink"))
3835 error = unformat_parse_error (line_input);
3840 ip6_neighbor_ra_prefix (vm, sw_if_index,
3841 &ip6_addr, addr_len,
3842 use_prefix_default_values,
3843 valid_lifetime_in_secs,
3844 pref_lifetime_in_secs,
3846 off_link, no_autoconfig, no_onlink, is_no);
3850 unformat_free (line_input);
3856 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3858 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3861 for (i = 0; i < vec_len (addrs); i++)
3863 ip_interface_address_t *a =
3864 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3865 ip6_address_t *address = ip_interface_address_get_address (lm, a);
3867 vlib_cli_output (vm, "\t\t%U/%d",
3868 format_ip6_address, address, a->address_length);
3872 static clib_error_t *
3873 show_ip6_interface_cmd (vlib_main_t * vm,
3874 unformat_input_t * input, vlib_cli_command_t * cmd)
3876 vnet_main_t *vnm = vnet_get_main ();
3877 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3878 clib_error_t *error = 0;
3883 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3887 /* look up the radv_t information for this interface */
3888 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3891 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3895 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3896 ip6_radv_t *radv_info;
3897 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3899 vlib_cli_output (vm, "%U is admin %s\n",
3900 format_vnet_sw_interface_name, vnm,
3901 vnet_get_sw_interface (vnm, sw_if_index),
3902 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3906 u32 *link_scope = 0, *global_scope = 0;
3907 u32 *local_scope = 0, *unknown_scope = 0;
3908 ip_interface_address_t *a;
3910 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3912 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3914 while (ai != (u32) ~ 0)
3916 a = pool_elt_at_index (lm->if_address_pool, ai);
3917 ip6_address_t *address =
3918 ip_interface_address_get_address (lm, a);
3920 if (ip6_address_is_link_local_unicast (address))
3921 vec_add1 (link_scope, ai);
3922 else if (ip6_address_is_global_unicast (address))
3923 vec_add1 (global_scope, ai);
3924 else if (ip6_address_is_local_unicast (address))
3925 vec_add1 (local_scope, ai);
3927 vec_add1 (unknown_scope, ai);
3929 ai = a->next_this_sw_interface;
3932 if (vec_len (link_scope))
3934 vlib_cli_output (vm, "\tLink-local address(es):\n");
3935 ip6_print_addrs (vm, link_scope);
3936 vec_free (link_scope);
3939 if (vec_len (local_scope))
3941 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3942 ip6_print_addrs (vm, local_scope);
3943 vec_free (local_scope);
3946 if (vec_len (global_scope))
3948 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3949 ip6_print_addrs (vm, global_scope);
3950 vec_free (global_scope);
3953 if (vec_len (unknown_scope))
3955 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3956 ip6_print_addrs (vm, unknown_scope);
3957 vec_free (unknown_scope);
3960 vlib_cli_output (vm, "\tLink-local address(es):\n");
3961 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3962 &radv_info->link_local_address);
3964 vlib_cli_output (vm, "\tJoined group address(es):\n");
3965 ip6_mldp_group_t *m;
3967 pool_foreach (m, radv_info->mldp_group_pool,
3969 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3974 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3975 ip6_radv_prefix_t *p;
3977 pool_foreach (p, radv_info->adv_prefixes_pool,
3979 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3980 format_ip6_address, &p->prefix, p->prefix_len);
3984 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
3985 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3986 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3987 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3988 vlib_cli_output (vm, "\tND DAD is disabled\n");
3989 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3990 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3991 radv_info->adv_neighbor_reachable_time_in_msec);
3992 vlib_cli_output (vm,
3993 "\tND advertised retransmit interval is %d (msec)\n",
3995 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
3997 u32 ra_interval = radv_info->max_radv_interval;
3998 u32 ra_interval_min = radv_info->min_radv_interval;
3999 vlib_cli_output (vm,
4000 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
4001 ra_interval, ra_interval_min);
4002 vlib_cli_output (vm,
4003 "\tND router advertisements live for %d seconds\n",
4004 radv_info->adv_router_lifetime_in_sec);
4005 vlib_cli_output (vm,
4006 "\tHosts %s stateless autoconfig for addresses\n",
4007 (radv_info->adv_managed_flag) ? "use" :
4009 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4010 radv_info->n_advertisements_sent);
4011 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4012 radv_info->n_solicitations_rcvd);
4013 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4014 radv_info->n_solicitations_dropped);
4018 error = clib_error_return (0, "IPv6 not enabled on interface",
4019 format_unformat_error, input);
4027 * This command is used to display various IPv6 attributes on a given
4031 * Example of how to display IPv6 settings:
4032 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4033 * GigabitEthernet2/0/0 is admin up
4034 * Link-local address(es):
4036 * Joined group address(es):
4041 * Advertised Prefixes:
4042 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4044 * ICMP error messages are unlimited
4045 * ICMP redirects are disabled
4046 * ICMP unreachables are not sent
4047 * ND DAD is disabled
4048 * ND advertised reachable time is 0
4049 * ND advertised retransmit interval is 0 (msec)
4050 * ND router advertisements are sent every 200 seconds (min interval is 150)
4051 * ND router advertisements live for 600 seconds
4052 * Hosts use stateless autoconfig for addresses
4053 * ND router advertisements sent 19336
4054 * ND router solicitations received 0
4055 * ND router solicitations dropped 0
4057 * Example of output if IPv6 is not enabled on the interface:
4058 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4059 * show ip6 interface: IPv6 not enabled on interface
4063 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4065 .path = "show ip6 interface",
4066 .function = show_ip6_interface_cmd,
4067 .short_help = "show ip6 interface <interface>",
4072 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4074 clib_error_t *error = 0;
4075 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4078 /* look up the radv_t information for this interface */
4079 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4081 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4083 /* if not created - do nothing */
4086 ip6_radv_t *radv_info;
4088 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4090 /* check radv_info ref count for other ip6 addresses on this interface */
4091 /* This implicitly excludes the link local address */
4092 if (radv_info->ref_count == 0)
4094 /* essentially "disables" ipv6 on this interface */
4095 ip6_ll_prefix_t ilp = {
4096 .ilp_addr = radv_info->link_local_address,
4097 .ilp_sw_if_index = sw_if_index,
4099 ip6_ll_table_entry_delete (&ilp);
4100 ip6_sw_interface_enable_disable (sw_if_index, 0);
4101 ip6_mfib_interface_enable_disable (sw_if_index, 0);
4102 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4110 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
4112 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4115 /* look up the radv_t information for this interface */
4116 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4119 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4125 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4127 clib_error_t *error = 0;
4128 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4132 /* look up the radv_t information for this interface */
4133 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4136 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4138 /* if not created yet */
4141 vnet_main_t *vnm = vnet_get_main ();
4142 vnet_sw_interface_t *sw_if0;
4144 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4145 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4147 ethernet_interface_t *eth_if0;
4150 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
4153 /* create radv_info. for this interface. This holds all the info needed for router adverts */
4155 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
4159 ip6_radv_t *radv_info;
4160 ip6_address_t link_local_address;
4162 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4164 ip6_link_local_address_from_ethernet_mac_address
4165 (&link_local_address, eth_if0->address);
4167 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
4168 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
4169 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
4171 /* make up an interface id */
4172 link_local_address.as_u64[1] =
4173 random_u64 (&radv_info->randomizer);
4175 link_local_address.as_u64[0] =
4176 clib_host_to_net_u64 (0xFE80000000000000ULL);
4178 link_local_address.as_u8[8] &= 0xfd;
4181 ip6_ll_prefix_t ilp = {
4182 .ilp_addr = link_local_address,
4183 .ilp_sw_if_index = sw_if_index,
4186 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4189 /* essentially "enables" ipv6 on this interface */
4190 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4191 ip6_sw_interface_enable_disable (sw_if_index, 1);
4195 radv_info->link_local_address = link_local_address;
4205 ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4207 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4208 ip6_radv_t *radv_info;
4211 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) < sw_if_index)
4214 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4219 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4220 *addr = radv_info->link_local_address;
4225 static clib_error_t *
4226 enable_ip6_interface_cmd (vlib_main_t * vm,
4227 unformat_input_t * input, vlib_cli_command_t * cmd)
4229 vnet_main_t *vnm = vnet_get_main ();
4230 clib_error_t *error = 0;
4235 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4237 enable_ip6_interface (vm, sw_if_index);
4241 error = clib_error_return (0, "unknown interface\n'",
4242 format_unformat_error, input);
4249 * This command is used to enable IPv6 on a given interface.
4252 * Example of how enable IPv6 on a given interface:
4253 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4256 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4258 .path = "enable ip6 interface",
4259 .function = enable_ip6_interface_cmd,
4260 .short_help = "enable ip6 interface <interface>",
4264 static clib_error_t *
4265 disable_ip6_interface_cmd (vlib_main_t * vm,
4266 unformat_input_t * input, vlib_cli_command_t * cmd)
4268 vnet_main_t *vnm = vnet_get_main ();
4269 clib_error_t *error = 0;
4274 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4276 error = disable_ip6_interface (vm, sw_if_index);
4280 error = clib_error_return (0, "unknown interface\n'",
4281 format_unformat_error, input);
4288 * This command is used to disable IPv6 on a given interface.
4291 * Example of how disable IPv6 on a given interface:
4292 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4295 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4297 .path = "disable ip6 interface",
4298 .function = disable_ip6_interface_cmd,
4299 .short_help = "disable ip6 interface <interface>",
4304 * This command is used to configure the neighbor discovery
4305 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4306 * command to display some of the current neighbor discovery parameters
4307 * on a given interface. This command has three formats:
4310 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4312 * '<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>'
4316 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4317 * router-advertisement messages to use stateful address
4318 * auto-configuration to obtain address information (sets the M-bit).
4319 * Default is the M-bit is not set and the '<em>no</em>' option
4320 * returns it to this default state.
4322 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4323 * router-advertisement messages that hosts use stateful auto
4324 * configuration to obtain nonaddress related information (sets
4325 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4326 * option returns it to this default state.
4328 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4329 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4330 * router-advertisement messages.
4332 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4333 * optional source link-layer address in the ICMPv6 router-advertisement
4334 * messages. Default is to include the optional source link-layer address
4335 * and the '<em>no</em>' option returns it to this default state.
4337 * <em>[no] ra-send-unicast</em> - Use the source address of the
4338 * router-solicitation message if availiable. The default is to use
4339 * multicast address of all nodes, and the '<em>no</em>' option returns
4340 * it to this default state.
4342 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4343 * default router in ICMPv6 router-advertisement messages. The range is
4344 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4345 * '<em><max-interval></em>'. The default value is 600 seconds and the
4346 * '<em>no</em>' option returns it to this default value.
4348 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4349 * router-advertisement messages sent and the interval between each
4350 * message. Range for count is 1 - 3 and default is 3. Range for interval
4351 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4352 * returns both to their default value.
4354 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4355 * interval between sending ICMPv6 router-advertisement messages. The
4356 * range for max-interval is from 4 to 200 seconds. min-interval can not
4357 * be more than 75% of max-interval. If not set, min-interval will be
4358 * set to 75% of max-interval. The range for min-interval is from 3 to
4359 * 150 seconds. The '<em>no</em>' option returns both to their default
4362 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4363 * The '<em>no</em>' options implies to start (or restart) sending
4364 * ICMPv6 router-advertisement messages.
4367 * <b>Format 2 - Prefix Options:</b>
4369 * '<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>'
4373 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4375 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4376 * length of time in seconds during what the prefix is valid for the purpose of
4377 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4378 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4379 * length of time in seconds during what addresses generated from the prefix remain
4380 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4382 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4383 * are inifinte, no timeout.
4385 * <em>no-advertise</em> - Do not send full router address in prefix
4386 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4388 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4389 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4390 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4392 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4393 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4395 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4396 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4397 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4401 * <b>Format 3: - Default of Prefix:</b>
4403 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4405 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4406 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4407 * is ignored and the prefix is deleted.
4411 * Example of how set a router advertisement option:
4412 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4413 * Example of how to add a prefix:
4414 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4415 * Example of how to delete a prefix:
4416 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4419 VLIB_CLI_COMMAND (ip6_nd_command, static) =
4422 .short_help = "ip6 nd <interface> ...",
4423 .function = ip6_neighbor_cmd,
4428 set_ip6_link_local_address (vlib_main_t * vm,
4429 u32 sw_if_index, ip6_address_t * address)
4431 clib_error_t *error = 0;
4432 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4434 ip6_radv_t *radv_info;
4435 vnet_main_t *vnm = vnet_get_main ();
4437 if (!ip6_address_is_link_local_unicast (address))
4439 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
4440 return (error = clib_error_return (0, "address not link-local",
4441 format_unformat_error));
4444 /* call enable ipv6 */
4445 enable_ip6_interface (vm, sw_if_index);
4447 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4451 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4453 /* save if link local address (overwrite default) */
4455 /* delete the old one */
4456 error = ip6_add_del_interface_address (vm, sw_if_index,
4457 &radv_info->link_local_address,
4458 128, 1 /* is_del */ );
4462 /* add the new one */
4463 error = ip6_add_del_interface_address (vm, sw_if_index,
4469 radv_info->link_local_address = *address;
4475 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4476 error = clib_error_return (0, "ip6 not enabled for interface",
4477 format_unformat_error);
4483 set_ip6_link_local_address_cmd (vlib_main_t * vm,
4484 unformat_input_t * input,
4485 vlib_cli_command_t * cmd)
4487 vnet_main_t *vnm = vnet_get_main ();
4488 clib_error_t *error = 0;
4490 ip6_address_t ip6_addr;
4492 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4494 /* get the rest of the command */
4495 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4497 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
4500 return (unformat_parse_error (input));
4503 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
4508 * This command is used to assign an IPv6 Link-local address to an
4509 * interface. This command will enable IPv6 on an interface if it
4510 * is not already enabled. Use the '<em>show ip6 interface</em>' command
4511 * to display the assigned Link-local address.
4514 * Example of how to assign an IPv6 Link-local address to an interface:
4515 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
4518 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
4520 .path = "set ip6 link-local address",
4521 .short_help = "set ip6 link-local address <interface> <ip6-address>",
4522 .function = set_ip6_link_local_address_cmd,
4527 * @brief callback when an interface address is added or deleted
4530 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4533 ip6_address_t * address,
4535 u32 if_address_index, u32 is_delete)
4537 vnet_main_t *vnm = vnet_get_main ();
4538 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4540 vlib_main_t *vm = vnm->vlib_main;
4541 ip6_radv_t *radv_info;
4544 /* create solicited node multicast address for this interface adddress */
4545 ip6_set_solicited_node_multicast_address (&a, 0);
4547 a.as_u8[0xd] = address->as_u8[0xd];
4548 a.as_u8[0xe] = address->as_u8[0xe];
4549 a.as_u8[0xf] = address->as_u8[0xf];
4553 /* try to create radv_info - does nothing if ipv6 already enabled */
4554 enable_ip6_interface (vm, sw_if_index);
4556 /* look up the radv_t information for this interface */
4557 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4559 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4563 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4566 if (!ip6_address_is_link_local_unicast (address))
4567 radv_info->ref_count++;
4569 ip6_neighbor_add_mld_prefix (radv_info, &a);
4576 /* look up the radv_t information for this interface */
4577 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4579 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4584 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4586 ip6_neighbor_del_mld_prefix (radv_info, &a);
4588 /* if interface up send MLDP "report" */
4589 radv_info->all_routers_mcast = 0;
4592 if (!ip6_address_is_link_local_unicast (address))
4593 radv_info->ref_count--;
4595 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4596 disable_ip6_interface (vm, sw_if_index);
4601 ip6_set_neighbor_limit (u32 neighbor_limit)
4603 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4605 nm->limit_neighbor_cache_size = neighbor_limit;
4610 ip6_neighbor_table_bind (ip6_main_t * im,
4613 u32 new_fib_index, u32 old_fib_index)
4615 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4616 ip6_neighbor_t *n = NULL;
4617 u32 i, *to_re_add = 0;
4620 pool_foreach (n, nm->neighbor_pool,
4622 if (n->key.sw_if_index == sw_if_index)
4623 vec_add1 (to_re_add, n - nm->neighbor_pool);
4627 for (i = 0; i < vec_len (to_re_add); i++)
4629 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4630 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4631 ip6_neighbor_adj_fib_add (n, new_fib_index);
4633 vec_free (to_re_add);
4636 static clib_error_t *
4637 ip6_neighbor_init (vlib_main_t * vm)
4639 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4640 ip6_main_t *im = &ip6_main;
4642 mhash_init (&nm->neighbor_index_by_key,
4643 /* value size */ sizeof (uword),
4644 /* key size */ sizeof (ip6_neighbor_key_t));
4646 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4647 ip6_icmp_neighbor_solicitation_node.index);
4648 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4649 ip6_icmp_neighbor_advertisement_node.index);
4650 icmp6_register_type (vm, ICMP6_router_solicitation,
4651 ip6_icmp_router_solicitation_node.index);
4652 icmp6_register_type (vm, ICMP6_router_advertisement,
4653 ip6_icmp_router_advertisement_node.index);
4655 /* handler node for ip6 neighbor discovery events and timers */
4656 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4658 /* add call backs */
4659 ip6_add_del_interface_address_callback_t cb;
4660 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4662 /* when an interface address changes... */
4663 cb.function = ip6_neighbor_add_del_interface_address;
4664 cb.function_opaque = 0;
4665 vec_add1 (im->add_del_interface_address_callbacks, cb);
4667 ip6_table_bind_callback_t cbt;
4668 cbt.function = ip6_neighbor_table_bind;
4669 cbt.function_opaque = 0;
4670 vec_add1 (im->table_bind_callbacks, cbt);
4672 mhash_init (&nm->pending_resolutions_by_address,
4673 /* value size */ sizeof (uword),
4674 /* key size */ sizeof (ip6_address_t));
4676 mhash_init (&nm->mac_changes_by_address,
4677 /* value size */ sizeof (uword),
4678 /* key size */ sizeof (ip6_address_t));
4680 /* default, configurable */
4681 nm->limit_neighbor_cache_size = 50000;
4683 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4685 nm->ip6_ra_publisher_node = (uword) ~ 0;
4688 /* $$$$ Hack fix for today */
4689 vec_validate_init_empty
4690 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
4696 VLIB_INIT_FUNCTION (ip6_neighbor_init);
4700 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4703 uword type_opaque, uword data)
4705 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4706 ip6_address_t *address = address_arg;
4708 pending_resolution_t *pr;
4710 pool_get (nm->pending_resolutions, pr);
4712 pr->next_index = ~0;
4713 pr->node_index = node_index;
4714 pr->type_opaque = type_opaque;
4717 p = mhash_get (&nm->pending_resolutions_by_address, address);
4720 /* Insert new resolution at the head of the list */
4721 pr->next_index = p[0];
4722 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4725 mhash_set (&nm->pending_resolutions_by_address, address,
4726 pr - nm->pending_resolutions, 0 /* old value */ );
4730 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4731 void *data_callback,
4735 uword type_opaque, uword data, int is_add)
4737 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4738 ip6_address_t *address = address_arg;
4740 /* Try to find an existing entry */
4741 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4743 pending_resolution_t *mc;
4744 while (p && *p != ~0)
4746 mc = pool_elt_at_index (nm->mac_changes, *p);
4747 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4750 p = &mc->next_index;
4753 int found = p && *p != ~0;
4757 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4759 pool_get (nm->mac_changes, mc);
4760 *mc = (pending_resolution_t)
4762 .next_index = ~0,.node_index = node_index,.type_opaque =
4763 type_opaque,.data = data,.data_callback = data_callback,.pid =
4766 /* Insert new resolution at the end of the list */
4767 u32 new_idx = mc - nm->mac_changes;
4771 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
4776 return VNET_API_ERROR_NO_SUCH_ENTRY;
4778 /* Clients may need to clean up pool entries, too */
4779 void (*fp) (u32, u8 *) = data_callback;
4781 (*fp) (mc->data, 0 /* no new mac addrs */ );
4783 /* Remove the entry from the list and delete the entry */
4784 *p = mc->next_index;
4785 pool_put (nm->mac_changes, mc);
4787 /* Remove from hash if we deleted the last entry */
4788 if (*p == ~0 && p == first)
4789 mhash_unset (&nm->mac_changes_by_address, address, 0);
4795 vnet_ip6_nd_term (vlib_main_t * vm,
4796 vlib_node_runtime_t * node,
4798 ethernet_header_t * eth,
4799 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
4801 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4802 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4804 ndh = ip6_next_header (ip);
4805 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4806 ndh->icmp.type != ICMP6_neighbor_advertisement)
4809 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4810 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4812 u8 *t0 = vlib_add_trace (vm, node, p0,
4813 sizeof (icmp6_input_trace_t));
4814 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4817 /* Check if anyone want ND events for L2 BDs */
4819 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4820 && !ip6_address_is_link_local_unicast (&ip->src_address)))
4822 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
4825 /* Check if MAC entry exsist for solicited target IP */
4826 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4828 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4829 l2_bridge_domain_t *bd_config;
4832 opt = (void *) (ndh + 1);
4833 if ((opt->header.type !=
4834 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4835 (opt->header.n_data_u64s != 1))
4836 return 0; /* source link layer address option not present */
4838 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4840 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4842 { /* found ip-mac entry, generate eighbor advertisement response */
4844 vlib_node_runtime_t *error_node =
4845 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4846 ip->dst_address = ip->src_address;
4847 ip->src_address = ndh->target_address;
4848 ip->hop_limit = 255;
4850 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4851 clib_memcpy (opt->ethernet_address, macp, 6);
4852 ndh->icmp.type = ICMP6_neighbor_advertisement;
4853 ndh->advertisement_flags = clib_host_to_net_u32
4854 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4855 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4856 ndh->icmp.checksum = 0;
4857 ndh->icmp.checksum =
4858 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4859 clib_memcpy (eth->dst_address, eth->src_address, 6);
4860 clib_memcpy (eth->src_address, macp, 6);
4861 vlib_error_count (vm, error_node->node_index,
4862 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4872 ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4876 fib_prefix_t pfx = {
4878 .fp_proto = FIB_PROTOCOL_IP6,
4883 ip46_address_t nh = {
4887 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4889 if (~0 == fib_index)
4890 return VNET_API_ERROR_NO_SUCH_FIB;
4894 fib_table_entry_path_remove (fib_index,
4896 FIB_SOURCE_IP6_ND_PROXY,
4900 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4901 /* flush the ND cache of this address if it's there */
4902 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4903 sw_if_index, addr, NULL, 0);
4907 fib_table_entry_path_add (fib_index,
4909 FIB_SOURCE_IP6_ND_PROXY,
4910 FIB_ENTRY_FLAG_NONE,
4914 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4919 static clib_error_t *
4920 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4921 unformat_input_t * input, vlib_cli_command_t * cmd)
4923 vnet_main_t *vnm = vnet_get_main ();
4924 clib_error_t *error = 0;
4929 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4931 /* get the rest of the command */
4932 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4934 if (unformat (input, "%U", unformat_ip6_address, &addr))
4936 else if (unformat (input, "delete") || unformat (input, "del"))
4939 return (unformat_parse_error (input));
4943 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4949 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4951 .path = "set ip6 nd proxy",
4952 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4953 .function = set_ip6_nd_proxy_cmd,
4958 ethernet_ndp_change_mac (u32 sw_if_index)
4960 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4965 pool_foreach (n, nm->neighbor_pool,
4967 if (n->key.sw_if_index == sw_if_index)
4969 adj_nbr_walk_nh6 (sw_if_index,
4970 &n->key.ip6_address,
4971 ip6_nd_mk_complete_walk, n);
4976 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4978 if (ADJ_INDEX_INVALID != ai)
4979 adj_glean_update_rewrite (ai);
4983 send_ip6_na (vlib_main_t * vm, const vnet_hw_interface_t * hi)
4985 ip6_main_t *i6m = &ip6_main;
4986 u32 sw_if_index = hi->sw_if_index;
4987 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4989 send_ip6_na_w_addr (vm, ip6_addr, hi);
4993 send_ip6_na_w_addr (vlib_main_t * vm,
4994 const ip6_address_t * ip6_addr,
4995 const vnet_hw_interface_t * hi)
4997 ip6_main_t *i6m = &ip6_main;
4998 u32 sw_if_index = hi->sw_if_index;
5003 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
5004 format_ip6_address, ip6_addr, sw_if_index);
5006 /* Form unsolicited neighbor advertisement packet from NS pkt template */
5009 icmp6_neighbor_solicitation_header_t *h =
5010 vlib_packet_template_get_packet (vm,
5011 &i6m->discover_neighbor_packet_template,
5013 ip6_set_reserved_multicast_address (&h->ip.dst_address,
5014 IP6_MULTICAST_SCOPE_link_local,
5015 IP6_MULTICAST_GROUP_ID_all_hosts);
5016 h->ip.src_address = ip6_addr[0];
5017 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
5018 h->neighbor.target_address = ip6_addr[0];
5019 h->neighbor.advertisement_flags = clib_host_to_net_u32
5020 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
5021 clib_memcpy (h->link_layer_option.ethernet_address,
5022 hi->hw_address, vec_len (hi->hw_address));
5023 h->neighbor.icmp.checksum =
5024 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
5025 ASSERT (bogus_length == 0);
5027 /* Setup MAC header with IP6 Etype and mcast DMAC */
5028 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
5029 vlib_buffer_advance (b, -sizeof (ethernet_header_t));
5030 ethernet_header_t *e = vlib_buffer_get_current (b);
5031 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_IP6);
5032 clib_memcpy (e->src_address, hi->hw_address, sizeof (e->src_address));
5033 ip6_multicast_ethernet_address (e->dst_address,
5034 IP6_MULTICAST_GROUP_ID_all_hosts);
5036 /* Send unsolicited ND advertisement packet out the specified interface */
5037 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5038 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5039 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5040 u32 *to_next = vlib_frame_vector_args (f);
5043 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5048 * fd.io coding-style-patch-verification: ON
5051 * eval: (c-set-style "gnu")