2 * ip/ip6_neighbor.c: IP6 neighbor handling
4 * Copyright (c) 2010 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/ip/ip.h>
19 #include <vnet/ip/ip6_neighbor.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vppinfra/mhash.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/adj/adj_mcast.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/fib/ip6_fib.h>
26 #include <vnet/mfib/ip6_mfib.h>
27 #include <vnet/ip/ip6_ll_table.h>
28 #include <vnet/l2/l2_input.h>
29 #include <vlibmemory/api.h>
33 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
35 * The files contains the API and CLI code for managing IPv6 neighbor
36 * adjacency tables and neighbor discovery logic.
39 /* can't use sizeof link_layer_address, that's 8 */
40 #define ETHER_MAC_ADDR_LEN 6
42 /* advertised prefix option */
45 /* basic advertised information */
49 int adv_autonomous_flag;
50 u32 adv_valid_lifetime_in_secs;
51 u32 adv_pref_lifetime_in_secs;
53 /* advertised values are computed from these times if decrementing */
54 f64 valid_lifetime_expires;
55 f64 pref_lifetime_expires;
57 /* local information */
59 int deprecated_prefix_flag;
60 int decrement_lifetime_flag;
62 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
63 #define DEF_ADV_VALID_LIFETIME 2592000
64 #define DEF_ADV_PREF_LIFETIME 604800
66 /* extensions are added here, mobile, DNS etc.. */
72 /* group information */
74 ip6_address_t mcast_address;
76 ip6_address_t *mcast_source_address_pool;
79 /* configured router advertisement information per ipv6 interface */
83 /* advertised config information, zero means unspecified */
87 u16 adv_router_lifetime_in_sec;
88 u32 adv_neighbor_reachable_time_in_msec;
89 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
94 /* source link layer option */
95 u8 link_layer_address[8];
96 u8 link_layer_addr_len;
99 ip6_radv_prefix_t *adv_prefixes_pool;
101 /* Hash table mapping address to index in interface advertised prefix pool. */
102 mhash_t address_to_prefix_index;
104 /* MLDP group information */
105 ip6_mldp_group_t *mldp_group_pool;
107 /* Hash table mapping address to index in mldp address pool. */
108 mhash_t address_to_mldp_index;
110 /* local information */
112 int send_radv; /* radv on/off on this interface - set by config */
113 int cease_radv; /* we are ceasing to send - set byf config */
115 int adv_link_layer_address;
117 int failed_device_check;
118 int all_routers_mcast;
122 adj_index_t mcast_adj_index;
124 /* timing information */
125 #define DEF_MAX_RADV_INTERVAL 200
126 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
127 #define DEF_CURR_HOP_LIMIT 64
128 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
129 #define MAX_DEF_RTR_LIFETIME 9000
131 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
132 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
133 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
134 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
135 #define MAX_RA_DELAY_TIME .5 /* seconds */
137 f64 max_radv_interval;
138 f64 min_radv_interval;
139 f64 min_delay_between_radv;
140 f64 max_delay_between_radv;
141 f64 max_rtr_default_lifetime;
144 f64 last_multicast_time;
145 f64 next_multicast_time;
148 u32 initial_adverts_count;
149 f64 initial_adverts_interval;
150 u32 initial_adverts_sent;
153 u32 n_advertisements_sent;
154 u32 n_solicitations_rcvd;
155 u32 n_solicitations_dropped;
157 /* Link local address to use (defaults to underlying physical for logical interfaces */
158 ip6_address_t link_local_address;
160 /* router solicitations sending state */
161 u8 keep_sending_rs; /* when true then next fields are valid */
162 icmp6_send_router_solicitation_params_t params;
167 vlib_buffer_t *buffer;
176 /* Used for nd event notification only */
177 ip6_nd_change_event_cb_t data_callback;
179 } pending_resolution_t;
184 /* Hash tables mapping name to opcode. */
185 uword *opcode_by_name;
187 /* lite beer "glean" adjacency handling */
188 mhash_t pending_resolutions_by_address;
189 pending_resolution_t *pending_resolutions;
191 /* Mac address change notification */
192 mhash_t mac_changes_by_address;
193 pending_resolution_t *mac_changes;
195 u32 *neighbor_input_next_index_by_hw_if_index;
197 ip6_neighbor_t *neighbor_pool;
199 mhash_t neighbor_index_by_key;
201 u32 *if_radv_pool_index_by_sw_if_index;
203 ip6_radv_t *if_radv_pool;
205 /* Neighbor attack mitigation */
206 u32 limit_neighbor_cache_size;
207 u32 neighbor_delete_rotor;
209 /* Wildcard nd report publisher */
210 uword wc_ip6_nd_publisher_node;
211 uword wc_ip6_nd_publisher_et;
213 /* Router advertisement report publisher */
214 uword ip6_ra_publisher_node;
215 uword ip6_ra_publisher_et;
216 } ip6_neighbor_main_t;
218 /* ipv6 neighbor discovery - timer/event types */
222 } ip6_icmp_neighbor_discovery_event_type_t;
232 } ip6_icmp_neighbor_discovery_event_data_t;
234 static ip6_neighbor_main_t ip6_neighbor_main;
235 ip6_neighbor_public_main_t ip6_neighbor_public_main;
236 static ip6_address_t ip6a_zero; /* ip6 address 0 */
238 static void wc_nd_signal_report (wc_nd_report_t * r);
239 static void ra_signal_report (ra_report_t * r);
242 ip6_neighbor_get_link_local_address (u32 sw_if_index)
244 static ip6_address_t empty_address = { {0} };
245 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
246 ip6_radv_t *radv_info;
249 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index)
250 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
253 clib_warning ("IPv6 is not enabled for sw_if_index %d", sw_if_index);
254 return empty_address;
256 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
257 if (radv_info == NULL)
259 clib_warning ("Internal error");
260 return empty_address;
262 return radv_info->link_local_address;
266 * @brief publish wildcard arp event
267 * @param sw_if_index The interface on which the ARP entries are acted
270 vnet_nd_wc_publish (u32 sw_if_index,
271 const mac_address_t * mac, const ip6_address_t * ip6)
274 .sw_if_index = sw_if_index,
279 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
284 wc_nd_signal_report (wc_nd_report_t * r)
286 vlib_main_t *vm = vlib_get_main ();
287 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
288 uword ni = nm->wc_ip6_nd_publisher_node;
289 uword et = nm->wc_ip6_nd_publisher_et;
291 if (ni == (uword) ~ 0)
294 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
300 wc_nd_set_publisher_node (uword node_index, uword event_type)
302 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
303 nm->wc_ip6_nd_publisher_node = node_index;
304 nm->wc_ip6_nd_publisher_et = event_type;
308 ra_publish (ra_report_t * r)
310 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
311 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
316 ra_signal_report (ra_report_t * r)
318 vlib_main_t *vm = vlib_get_main ();
319 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
320 uword ni = nm->ip6_ra_publisher_node;
321 uword et = nm->ip6_ra_publisher_et;
323 if (ni == (uword) ~ 0)
325 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
331 ra_set_publisher_node (uword node_index, uword event_type)
333 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
334 nm->ip6_ra_publisher_node = node_index;
335 nm->ip6_ra_publisher_et = event_type;
339 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
341 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
342 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
343 vnet_main_t *vnm = vnet_get_main ();
344 vnet_sw_interface_t *si;
347 return format (s, "%=12s%=45s%=6s%=20s%=40s", "Time", "Address", "Flags",
348 "Link layer", "Interface");
350 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
352 return format (s, "%=12U%=45U%=6U%=20U%=40U",
353 format_vlib_time, vm, n->time_last_updated,
354 format_ip6_address, &n->key.ip6_address,
355 format_ip_neighbor_flags, n->flags,
356 format_mac_address_t, &n->mac,
357 format_vnet_sw_interface_name, vnm, si);
361 ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
363 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
365 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
367 ip6_ll_prefix_t pfx = {
368 .ilp_addr = n->key.ip6_address,
369 .ilp_sw_if_index = n->key.sw_if_index,
371 ip6_ll_table_entry_delete (&pfx);
377 .fp_proto = FIB_PROTOCOL_IP6,
378 .fp_addr.ip6 = n->key.ip6_address,
380 fib_table_entry_path_remove (fib_index,
385 n->key.sw_if_index, ~0,
386 1, FIB_ROUTE_PATH_FLAG_NONE);
394 ip_neighbor_flags_t flags;
398 } ip6_neighbor_set_unset_rpc_args_t;
400 static void ip6_neighbor_set_unset_rpc_callback
401 (ip6_neighbor_set_unset_rpc_args_t * a);
403 static void set_unset_ip6_neighbor_rpc
406 const ip6_address_t * a,
407 const mac_address_t * mac, int is_add, ip_neighbor_flags_t flags)
409 ip6_neighbor_set_unset_rpc_args_t args;
410 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
412 args.sw_if_index = sw_if_index;
413 args.is_add = is_add;
415 ip6_address_copy (&args.addr, a);
416 mac_address_copy (&args.mac, mac);
418 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
419 (u8 *) & args, sizeof (args));
423 ip6_nbr_probe (ip_adjacency_t * adj)
425 icmp6_neighbor_solicitation_header_t *h;
426 vnet_main_t *vnm = vnet_get_main ();
427 ip6_main_t *im = &ip6_main;
428 ip_interface_address_t *ia;
429 ip6_address_t *dst, *src;
430 vnet_hw_interface_t *hi;
431 vnet_sw_interface_t *si;
437 vm = vlib_get_main ();
439 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
440 dst = &adj->sub_type.nbr.next_hop.ip6;
442 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
446 src = ip6_interface_address_matching_destination (im, dst,
454 h = vlib_packet_template_get_packet (vm,
455 &im->discover_neighbor_packet_template,
460 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
462 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
463 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
464 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
465 h->ip.src_address = src[0];
466 h->neighbor.target_address = dst[0];
468 clib_memcpy (h->link_layer_option.ethernet_address,
469 hi->hw_address, vec_len (hi->hw_address));
471 h->neighbor.icmp.checksum =
472 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
473 ASSERT (bogus_length == 0);
475 b = vlib_get_buffer (vm, bi);
476 vnet_buffer (b)->sw_if_index[VLIB_RX] =
477 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
479 /* Add encapsulation string for software interface (e.g. ethernet header). */
480 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
481 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
484 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
485 u32 *to_next = vlib_frame_vector_args (f);
488 vlib_put_frame_to_node (vm, hi->output_node_index, f);
493 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
495 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
496 ethernet_build_rewrite (vnet_get_main (),
497 nbr->key.sw_if_index,
498 adj_get_link_type (ai),
503 ip6_nd_mk_incomplete (adj_index_t ai)
505 ip_adjacency_t *adj = adj_get (ai);
507 adj_nbr_update_rewrite (ai,
508 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
509 ethernet_build_rewrite (vnet_get_main (),
512 adj_get_link_type (ai),
513 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
516 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
518 k.sw_if_index = sw_if_index; \
519 k.ip6_address = *addr; \
523 static ip6_neighbor_t *
524 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
526 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
527 ip6_neighbor_t *n = NULL;
528 ip6_neighbor_key_t k;
531 IP6_NBR_MK_KEY (k, sw_if_index, addr);
533 p = mhash_get (&nm->neighbor_index_by_key, &k);
536 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
543 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
545 ip6_neighbor_t *nbr = ctx;
547 ip6_nd_mk_complete (ai, nbr);
549 return (ADJ_WALK_RC_CONTINUE);
553 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
555 ip6_nd_mk_incomplete (ai);
557 return (ADJ_WALK_RC_CONTINUE);
560 static clib_error_t *
561 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
562 u32 sw_if_index, u32 flags)
564 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
566 u32 i, *to_delete = 0;
569 pool_foreach (n, nm->neighbor_pool,
571 if (n->key.sw_if_index == sw_if_index)
572 vec_add1 (to_delete, n - nm->neighbor_pool);
576 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
578 for (i = 0; i < vec_len (to_delete); i++)
580 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
581 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
582 ip6_nd_mk_complete_walk, n);
587 for (i = 0; i < vec_len (to_delete); i++)
589 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
590 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
591 ip6_nd_mk_incomplete_walk, NULL);
592 if (n->flags & IP_NEIGHBOR_FLAG_STATIC)
594 ip6_neighbor_adj_fib_remove (n,
595 ip6_fib_table_get_index_for_sw_if_index
596 (n->key.sw_if_index));
597 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
598 pool_put (nm->neighbor_pool, n);
602 vec_free (to_delete);
606 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
609 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
616 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
618 switch (adj->lookup_next_index)
620 case IP_LOOKUP_NEXT_GLEAN:
621 adj_glean_update_rewrite (ai);
623 case IP_LOOKUP_NEXT_ARP:
626 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
627 ip6_nd_mk_complete_walk, nbr);
632 * no matching ND entry.
633 * construct the rewrite required to for an ND packet, and stick
634 * that in the adj's pipe to smoke.
636 adj_nbr_update_rewrite (ai,
637 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
638 ethernet_build_rewrite (vnm,
641 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
644 * since the FIB has added this adj for a route, it makes sense it may
645 * want to forward traffic sometime soon. Let's send a speculative ND.
646 * just one. If we were to do periodically that wouldn't be bad either,
647 * but that's more code than i'm prepared to write at this time for
648 * relatively little reward.
653 case IP_LOOKUP_NEXT_BCAST:
654 adj_nbr_update_rewrite (ai,
655 ADJ_NBR_REWRITE_FLAG_COMPLETE,
656 ethernet_build_rewrite (vnm,
659 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
661 case IP_LOOKUP_NEXT_MCAST:
664 * Construct a partial rewrite from the known ethernet mcast dest MAC
669 rewrite = ethernet_build_rewrite (vnm,
672 ethernet_ip6_mcast_dst_addr ());
675 * Complete the remaining fields of the adj's rewrite to direct the
676 * complete of the rewrite at switch time by copying in the IP
677 * dst address's bytes.
678 * Ofset is 2 bytes into the destintation address.
680 offset = vec_len (rewrite) - 2;
681 adj_mcast_update_rewrite (ai, rewrite, offset);
685 case IP_LOOKUP_NEXT_DROP:
686 case IP_LOOKUP_NEXT_PUNT:
687 case IP_LOOKUP_NEXT_LOCAL:
688 case IP_LOOKUP_NEXT_REWRITE:
689 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
690 case IP_LOOKUP_NEXT_MIDCHAIN:
691 case IP_LOOKUP_NEXT_ICMP_ERROR:
692 case IP_LOOKUP_N_NEXT:
700 ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
702 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
704 ip6_ll_prefix_t pfx = {
705 .ilp_addr = n->key.ip6_address,
706 .ilp_sw_if_index = n->key.sw_if_index,
709 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
715 .fp_proto = FIB_PROTOCOL_IP6,
716 .fp_addr.ip6 = n->key.ip6_address,
720 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
721 FIB_ENTRY_FLAG_ATTACHED,
722 DPO_PROTO_IP6, &pfx.fp_addr,
723 n->key.sw_if_index, ~0, 1, NULL,
724 FIB_ROUTE_PATH_FLAG_NONE);
728 static ip6_neighbor_t *
729 force_reuse_neighbor_entry (void)
732 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
734 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
735 if (index == ~0) /* Try again from elt 0 */
736 index = pool_next_index (nm->neighbor_pool, index);
738 /* Find a non-static random entry to free up for reuse */
741 if ((count++ == 100) || (index == ~0))
742 return NULL; /* give up after 100 entries */
743 n = pool_elt_at_index (nm->neighbor_pool, index);
744 nm->neighbor_delete_rotor = index;
745 index = pool_next_index (nm->neighbor_pool, index);
747 while (n->flags & IP_NEIGHBOR_FLAG_STATIC);
749 /* Remove ARP entry from its interface and update fib */
750 adj_nbr_walk_nh6 (n->key.sw_if_index,
751 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
752 ip6_neighbor_adj_fib_remove
753 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
754 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
760 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
762 const ip6_address_t * a,
763 const mac_address_t * mac,
764 ip_neighbor_flags_t flags)
766 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
767 ip6_neighbor_key_t k;
768 ip6_neighbor_t *n = 0;
769 int make_new_nd_cache_entry = 1;
772 pending_resolution_t *pr, *mc;
774 if (vlib_get_thread_index ())
776 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, mac, 1, flags);
780 k.sw_if_index = sw_if_index;
781 k.ip6_address = a[0];
784 p = mhash_get (&nm->neighbor_index_by_key, &k);
787 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
788 /* Refuse to over-write static neighbor entry. */
789 if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
790 (n->flags & IP_NEIGHBOR_FLAG_STATIC))
792 /* if MAC address match, still check to send event */
793 if (0 == mac_address_cmp (&n->mac, mac))
794 goto check_customers;
797 make_new_nd_cache_entry = 0;
800 if (make_new_nd_cache_entry)
802 if (nm->limit_neighbor_cache_size &&
803 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
805 n = force_reuse_neighbor_entry ();
811 pool_get (nm->neighbor_pool, n);
812 memset (n, 0, sizeof (*n));
815 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
818 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
820 mac_address_copy (&n->mac, mac);
823 * create the adj-fib. the entry in the FIB table for and to the peer.
825 if (!(flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY))
827 ip6_neighbor_adj_fib_add
828 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
832 n->flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
838 * prevent a DoS attack from the data-plane that
839 * spams us with no-op updates to the MAC address
841 if (0 == mac_address_cmp (&n->mac, mac))
843 n->time_last_updated = vlib_time_now (vm);
844 goto check_customers;
847 mac_address_copy (&n->mac, mac);
850 /* Update time stamp and flags. */
851 n->time_last_updated = vlib_time_now (vm);
852 if (flags & IP_NEIGHBOR_FLAG_STATIC)
854 n->flags |= IP_NEIGHBOR_FLAG_STATIC;
855 n->flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC;
859 n->flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
860 n->flags &= ~IP_NEIGHBOR_FLAG_STATIC;
863 adj_nbr_walk_nh6 (sw_if_index,
864 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
867 /* Customer(s) waiting for this address to be resolved? */
868 p = mhash_get (&nm->pending_resolutions_by_address, a);
873 while (next_index != (u32) ~ 0)
875 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
876 vlib_process_signal_event (vm, pr->node_index,
877 pr->type_opaque, pr->data);
878 next_index = pr->next_index;
879 pool_put (nm->pending_resolutions, pr);
882 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
885 /* Customer(s) requesting ND event for this address? */
886 p = mhash_get (&nm->mac_changes_by_address, a);
891 while (next_index != (u32) ~ 0)
895 mc = pool_elt_at_index (nm->mac_changes, next_index);
897 /* Call the user's data callback, return 1 to suppress dup events */
898 if (mc->data_callback)
899 rv = (mc->data_callback) (mc->data, mac, sw_if_index, &ip6a_zero);
901 * Signal the resolver process, as long as the user
902 * says they want to be notified
905 vlib_process_signal_event (vm, mc->node_index,
906 mc->type_opaque, mc->data);
907 next_index = mc->next_index;
915 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
916 u32 sw_if_index, const ip6_address_t * a)
918 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
919 ip6_neighbor_key_t k;
924 if (vlib_get_thread_index ())
926 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL, 0,
927 IP_NEIGHBOR_FLAG_NONE);
931 k.sw_if_index = sw_if_index;
932 k.ip6_address = a[0];
935 p = mhash_get (&nm->neighbor_index_by_key, &k);
942 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
944 adj_nbr_walk_nh6 (sw_if_index,
945 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
946 ip6_neighbor_adj_fib_remove
947 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
949 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
950 pool_put (nm->neighbor_pool, n);
956 static void ip6_neighbor_set_unset_rpc_callback
957 (ip6_neighbor_set_unset_rpc_args_t * a)
959 vlib_main_t *vm = vlib_get_main ();
961 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
964 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
968 ip6_neighbor_sort (void *a1, void *a2)
970 vnet_main_t *vnm = vnet_get_main ();
971 ip6_neighbor_t *n1 = a1, *n2 = a2;
973 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
974 n2->key.sw_if_index);
976 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
981 ip6_neighbors_pool (void)
983 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
984 return nm->neighbor_pool;
988 ip6_neighbors_entries (u32 sw_if_index)
990 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
991 ip6_neighbor_t *n, *ns = NULL;
994 pool_foreach (n, nm->neighbor_pool,
996 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
1003 vec_sort_with_function (ns, ip6_neighbor_sort);
1007 static clib_error_t *
1008 show_ip6_neighbors (vlib_main_t * vm,
1009 unformat_input_t * input, vlib_cli_command_t * cmd)
1011 vnet_main_t *vnm = vnet_get_main ();
1012 ip6_neighbor_t *n, *ns;
1013 clib_error_t *error = 0;
1017 if (unformat (input, "verbose"))
1020 /* Filter entries by interface if given. */
1022 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1024 ns = ip6_neighbors_entries (sw_if_index);
1028 * Show the entire table if it's not too big, otherwise just
1029 * show the size of the table.
1031 if (vec_len (ns) < 50)
1035 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
1038 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
1043 (vm, "There are %u ip6 neighbors, "
1044 "'show ip6 neighbors verbose' to display the entire table...",
1049 vlib_cli_output (vm, "No ip6 neighbors");
1055 * This command is used to display the adjacent IPv6 hosts found via
1056 * neighbor discovery. Optionally, limit the output to the specified
1060 * Example of how to display the IPv6 neighbor adjacency table:
1061 * @cliexstart{show ip6 neighbors}
1062 * Time Address Flags Link layer Interface
1063 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1064 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1065 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1067 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1068 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1069 * Time Address Flags Link layer Interface
1070 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1071 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1075 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1076 .path = "show ip6 neighbors",
1077 .function = show_ip6_neighbors,
1078 .short_help = "show ip6 neighbors [<interface>]",
1082 static clib_error_t *
1083 set_ip6_neighbor (vlib_main_t * vm,
1084 unformat_input_t * input, vlib_cli_command_t * cmd)
1086 ip_neighbor_flags_t flags = IP_NEIGHBOR_FLAG_NONE;
1087 vnet_main_t *vnm = vnet_get_main ();
1094 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1096 /* intfc, ip6-address, mac-address */
1097 if (unformat (input, "%U %U %U",
1098 unformat_vnet_sw_interface, vnm, &sw_if_index,
1099 unformat_ip6_address, &addr,
1100 unformat_mac_address_t, &mac))
1103 else if (unformat (input, "delete") || unformat (input, "del"))
1105 else if (unformat (input, "static"))
1106 flags |= IP_NEIGHBOR_FLAG_STATIC;
1107 else if (unformat (input, "no-fib-entry"))
1108 flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
1114 return clib_error_return (0, "Missing interface, ip6 or hw address");
1117 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr, &mac, flags);
1119 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
1124 * This command is used to manually add an entry to the IPv6 neighbor
1125 * adjacency table. Optionally, the entry can be added as static. It is
1126 * also used to remove an entry from the table. Use the '<em>show ip6
1127 * neighbors</em>' command to display all learned and manually entered entries.
1130 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1131 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1132 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1133 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1136 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1138 .path = "set ip6 neighbor",
1139 .function = set_ip6_neighbor,
1140 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
1146 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1147 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1148 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1149 } icmp6_neighbor_solicitation_or_advertisement_next_t;
1151 static_always_inline uword
1152 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1153 vlib_node_runtime_t * node,
1154 vlib_frame_t * frame,
1155 uword is_solicitation)
1157 vnet_main_t *vnm = vnet_get_main ();
1158 ip6_main_t *im = &ip6_main;
1159 uword n_packets = frame->n_vectors;
1160 u32 *from, *to_next;
1161 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1162 icmp6_neighbor_discovery_option_type_t option_type;
1163 vlib_node_runtime_t *error_node =
1164 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1167 from = vlib_frame_vector_args (frame);
1168 n_left_from = n_packets;
1169 next_index = node->cached_next_index;
1171 if (node->flags & VLIB_NODE_FLAG_TRACE)
1172 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1174 sizeof (icmp6_input_trace_t));
1178 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1179 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1180 n_advertisements_sent = 0;
1182 while (n_left_from > 0)
1184 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1186 while (n_left_from > 0 && n_left_to_next > 0)
1190 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1191 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1192 u32 bi0, options_len0, sw_if_index0, next0, error0;
1193 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1197 bi0 = to_next[0] = from[0];
1202 n_left_to_next -= 1;
1204 p0 = vlib_get_buffer (vm, bi0);
1205 ip0 = vlib_buffer_get_current (p0);
1206 h0 = ip6_next_header (ip0);
1208 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1210 error0 = ICMP6_ERROR_NONE;
1211 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1212 ip6_sadd_link_local =
1213 ip6_address_is_link_local_unicast (&ip0->src_address);
1214 ip6_sadd_unspecified =
1215 ip6_address_is_unspecified (&ip0->src_address);
1217 /* Check that source address is unspecified, link-local or else on-link. */
1218 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1220 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1222 if (ADJ_INDEX_INVALID != src_adj_index0)
1224 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1226 /* Allow all realistic-looking rewrite adjacencies to pass */
1227 ni0 = adj0->lookup_next_index;
1228 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1229 (ni0 < IP6_LOOKUP_N_NEXT);
1231 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1234 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1240 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1244 o0 = (void *) (h0 + 1);
1245 o0 = ((options_len0 == 8 && o0->header.type == option_type
1246 && o0->header.n_data_u64s == 1) ? o0 : 0);
1248 /* If src address unspecified or link local, donot learn neighbor MAC */
1249 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1250 !ip6_sadd_unspecified))
1252 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1255 &h0->target_address,
1257 o0->ethernet_address,
1258 IP_NEIGHBOR_FLAG_NONE);
1261 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1263 /* Check that target address is local to this router. */
1264 fib_node_index_t fei;
1268 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1270 if (~0 == fib_index)
1272 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1276 if (ip6_address_is_link_local_unicast (&h0->target_address))
1278 fei = ip6_fib_table_lookup_exact_match
1279 (ip6_ll_fib_get (sw_if_index0),
1280 &h0->target_address, 128);
1284 fei = ip6_fib_table_lookup_exact_match (fib_index,
1285 &h0->target_address,
1289 if (FIB_NODE_INDEX_INVALID == fei)
1291 /* The target address is not in the FIB */
1293 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1297 if (FIB_ENTRY_FLAG_LOCAL &
1298 fib_entry_get_flags_for_source (fei,
1299 FIB_SOURCE_INTERFACE))
1301 /* It's an address that belongs to one of our interfaces
1305 if (fib_entry_is_sourced
1306 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1307 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
1309 /* The address was added by IPv6 Proxy ND config.
1310 * We should only respond to these if the NS arrived on
1311 * the link that has a matching covering prefix */
1316 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1322 if (is_solicitation)
1323 next0 = (error0 != ICMP6_ERROR_NONE
1324 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1325 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1329 error0 = error0 == ICMP6_ERROR_NONE ?
1330 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1333 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1335 vnet_sw_interface_t *sw_if0;
1336 ethernet_interface_t *eth_if0;
1337 ethernet_header_t *eth0;
1339 /* dst address is either source address or the all-nodes mcast addr */
1340 if (!ip6_sadd_unspecified)
1341 ip0->dst_address = ip0->src_address;
1343 ip6_set_reserved_multicast_address (&ip0->dst_address,
1344 IP6_MULTICAST_SCOPE_link_local,
1345 IP6_MULTICAST_GROUP_ID_all_hosts);
1347 ip0->src_address = h0->target_address;
1348 ip0->hop_limit = 255;
1349 h0->icmp.type = ICMP6_neighbor_advertisement;
1351 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1352 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1354 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1357 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1359 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1362 h0->advertisement_flags = clib_host_to_net_u32
1363 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1364 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1366 h0->icmp.checksum = 0;
1368 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1370 ASSERT (bogus_length == 0);
1372 /* Reuse current MAC header, copy SMAC to DMAC and
1373 * interface MAC to SMAC */
1374 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1375 eth0 = vlib_buffer_get_current (p0);
1376 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1378 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1380 /* Setup input and output sw_if_index for packet */
1381 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1382 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1383 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1384 vnet_main.local_interface_sw_if_index;
1386 n_advertisements_sent++;
1389 p0->error = error_node->errors[error0];
1391 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1392 to_next, n_left_to_next,
1396 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1399 /* Account for advertisements sent. */
1400 vlib_error_count (vm, error_node->node_index,
1401 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1402 n_advertisements_sent);
1404 return frame->n_vectors;
1407 /* for "syslogging" - use elog for now */
1408 #define foreach_log_level \
1409 _ (DEBUG, "DEBUG") \
1410 _ (INFO, "INFORMATION") \
1411 _ (NOTICE, "NOTICE") \
1412 _ (WARNING, "WARNING") \
1414 _ (CRIT, "CRITICAL") \
1415 _ (ALERT, "ALERT") \
1416 _ (EMERG, "EMERGENCY")
1420 #define _(f,s) LOG_##f,
1425 static char *log_level_strings[] = {
1431 static int logmask = 1 << LOG_DEBUG;
1434 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1436 /* just use elog for now */
1440 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1446 what = va_format (0, fmt, &va);
1448 ELOG_TYPE_DECLARE (e) =
1450 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1455 ed = ELOG_DATA (&vm->elog_main, e);
1456 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1457 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1464 call_ip6_neighbor_callbacks (void *data,
1465 _vnet_ip6_neighbor_function_list_elt_t * elt)
1467 clib_error_t *error = 0;
1471 error = elt->fp (data);
1474 elt = elt->next_ip6_neighbor_function;
1480 /* ipv6 neighbor discovery - router advertisements */
1483 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1484 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1485 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1486 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1487 } icmp6_router_solicitation_or_advertisement_next_t;
1489 static_always_inline uword
1490 icmp6_router_solicitation (vlib_main_t * vm,
1491 vlib_node_runtime_t * node, vlib_frame_t * frame)
1493 vnet_main_t *vnm = vnet_get_main ();
1494 ip6_main_t *im = &ip6_main;
1495 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1496 uword n_packets = frame->n_vectors;
1497 u32 *from, *to_next;
1498 u32 n_left_from, n_left_to_next, next_index;
1499 u32 n_advertisements_sent = 0;
1502 icmp6_neighbor_discovery_option_type_t option_type;
1504 vlib_node_runtime_t *error_node =
1505 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1507 from = vlib_frame_vector_args (frame);
1508 n_left_from = n_packets;
1509 next_index = node->cached_next_index;
1511 if (node->flags & VLIB_NODE_FLAG_TRACE)
1512 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1514 sizeof (icmp6_input_trace_t));
1516 /* source may append his LL address */
1517 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1519 while (n_left_from > 0)
1521 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1523 while (n_left_from > 0 && n_left_to_next > 0)
1527 ip6_radv_t *radv_info = 0;
1529 icmp6_neighbor_discovery_header_t *h0;
1530 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1532 u32 bi0, options_len0, sw_if_index0, next0, error0;
1533 u32 is_solicitation = 1, is_dropped = 0;
1534 u32 is_unspecified, is_link_local;
1536 bi0 = to_next[0] = from[0];
1541 n_left_to_next -= 1;
1543 p0 = vlib_get_buffer (vm, bi0);
1544 ip0 = vlib_buffer_get_current (p0);
1545 h0 = ip6_next_header (ip0);
1547 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1548 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1550 ip6_address_is_link_local_unicast (&ip0->src_address);
1552 error0 = ICMP6_ERROR_NONE;
1553 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1555 /* check if solicitation (not from nd_timer node) */
1556 if (ip6_address_is_unspecified (&ip0->dst_address))
1557 is_solicitation = 0;
1559 /* Check that source address is unspecified, link-local or else on-link. */
1560 if (!is_unspecified && !is_link_local)
1562 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1564 if (ADJ_INDEX_INVALID != src_adj_index0)
1566 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1568 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1570 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1575 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1579 /* check for source LL option and process */
1580 o0 = (void *) (h0 + 1);
1581 o0 = ((options_len0 == 8
1582 && o0->header.type == option_type
1583 && o0->header.n_data_u64s == 1) ? o0 : 0);
1585 /* if src address unspecified IGNORE any options */
1586 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1587 !is_unspecified && !is_link_local))
1589 vnet_set_ip6_ethernet_neighbor
1592 (mac_address_t *) o0->ethernet_address,
1593 IP_NEIGHBOR_FLAG_NONE);
1596 /* default is to drop */
1597 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1599 if (error0 == ICMP6_ERROR_NONE)
1601 vnet_sw_interface_t *sw_if0;
1602 ethernet_interface_t *eth_if0;
1605 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1606 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1608 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1610 /* only support ethernet interface type for now */
1612 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1615 if (error0 == ICMP6_ERROR_NONE)
1619 /* adjust the sizeof the buffer to just include the ipv6 header */
1620 p0->current_length -=
1622 sizeof (icmp6_neighbor_discovery_header_t));
1624 /* look up the radv_t information for this interface */
1625 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1629 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1632 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1637 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1640 if (error0 == ICMP6_ERROR_NONE)
1642 f64 now = vlib_time_now (vm);
1644 /* for solicited adverts - need to rate limit */
1645 if (is_solicitation)
1647 if (0 != radv_info->last_radv_time &&
1648 (now - radv_info->last_radv_time) <
1649 MIN_DELAY_BETWEEN_RAS)
1652 radv_info->last_radv_time = now;
1656 icmp6_router_advertisement_header_t rh;
1658 rh.icmp.type = ICMP6_router_advertisement;
1660 rh.icmp.checksum = 0;
1662 rh.current_hop_limit = radv_info->curr_hop_limit;
1663 rh.router_lifetime_in_sec =
1664 clib_host_to_net_u16
1665 (radv_info->adv_router_lifetime_in_sec);
1667 time_in_msec_between_retransmitted_neighbor_solicitations
1669 clib_host_to_net_u32 (radv_info->
1670 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1671 rh.neighbor_reachable_time_in_msec =
1672 clib_host_to_net_u32 (radv_info->
1673 adv_neighbor_reachable_time_in_msec);
1676 (radv_info->adv_managed_flag) ?
1677 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1680 ((radv_info->adv_other_flag) ?
1681 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1685 u16 payload_length =
1686 sizeof (icmp6_router_advertisement_header_t);
1688 if (vlib_buffer_add_data
1689 (vm, &bi0, (void *) &rh,
1690 sizeof (icmp6_router_advertisement_header_t)))
1692 /* buffer allocation failed, drop the pkt */
1693 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1697 if (radv_info->adv_link_layer_address)
1699 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1703 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1704 h.header.n_data_u64s = 1;
1706 /* copy ll address */
1707 clib_memcpy (&h.ethernet_address[0],
1708 eth_if0->address, 6);
1710 if (vlib_buffer_add_data
1711 (vm, &bi0, (void *) &h,
1713 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
1715 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1721 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1724 /* add MTU option */
1725 if (radv_info->adv_link_mtu)
1727 icmp6_neighbor_discovery_mtu_option_t h;
1731 clib_host_to_net_u32 (radv_info->adv_link_mtu);
1732 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1733 h.header.n_data_u64s = 1;
1736 sizeof (icmp6_neighbor_discovery_mtu_option_t);
1738 if (vlib_buffer_add_data
1739 (vm, &bi0, (void *) &h,
1741 (icmp6_neighbor_discovery_mtu_option_t)))
1743 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1748 /* add advertised prefix options */
1749 ip6_radv_prefix_t *pr_info;
1752 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1754 if(pr_info->enabled &&
1755 (!pr_info->decrement_lifetime_flag
1756 || (pr_info->pref_lifetime_expires >0)))
1758 /* advertise this prefix */
1759 icmp6_neighbor_discovery_prefix_information_option_t h;
1761 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1762 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1764 h.dst_address_length = pr_info->prefix_len;
1766 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1767 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1769 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1771 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1772 h.preferred_time = 0;
1776 if(pr_info->decrement_lifetime_flag)
1778 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1779 (pr_info->valid_lifetime_expires - now) : 0;
1781 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1782 (pr_info->pref_lifetime_expires - now) : 0;
1785 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1786 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1790 clib_warning ("Prefix %U valid %u preferred %u",
1791 format_ip6_address, &pr_info->prefix,
1792 ntohl(h.valid_time),
1793 ntohl(h.preferred_time));
1795 if (h.valid_time == 0)
1796 clib_warning ("WARNING: valid_time 0!!!");
1798 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1800 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1802 if (vlib_buffer_add_data
1803 (vm, &bi0, (void *)&h,
1804 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
1806 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1814 /* add additional options before here */
1816 /* finish building the router advertisement... */
1817 if (!is_unspecified && radv_info->send_unicast)
1819 ip0->dst_address = ip0->src_address;
1823 /* target address is all-nodes mcast addr */
1824 ip6_set_reserved_multicast_address
1826 IP6_MULTICAST_SCOPE_link_local,
1827 IP6_MULTICAST_GROUP_ID_all_hosts);
1830 /* source address MUST be the link-local address */
1831 ip0->src_address = radv_info->link_local_address;
1833 ip0->hop_limit = 255;
1834 ip0->payload_length =
1835 clib_host_to_net_u16 (payload_length);
1837 icmp6_router_advertisement_header_t *rh0 =
1838 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1839 rh0->icmp.checksum =
1840 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1842 ASSERT (bogus_length == 0);
1844 /* setup output if and adjacency */
1845 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1846 vnet_main.local_interface_sw_if_index;
1848 if (is_solicitation)
1850 ethernet_header_t *eth0;
1851 /* Reuse current MAC header, copy SMAC to DMAC and
1852 * interface MAC to SMAC */
1853 vlib_buffer_reset (p0);
1854 eth0 = vlib_buffer_get_current (p0);
1855 clib_memcpy (eth0->dst_address, eth0->src_address,
1857 clib_memcpy (eth0->src_address, eth_if0->address,
1860 is_dropped ? next0 :
1861 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1862 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1867 adj_index0 = radv_info->mcast_adj_index;
1868 if (adj_index0 == ADJ_INDEX_INVALID)
1869 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1873 is_dropped ? next0 :
1874 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1875 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1879 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1881 radv_info->n_solicitations_dropped += is_dropped;
1882 radv_info->n_solicitations_rcvd += is_solicitation;
1884 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1886 radv_info->n_advertisements_sent++;
1887 n_advertisements_sent++;
1894 p0->error = error_node->errors[error0];
1896 if (error0 != ICMP6_ERROR_NONE)
1897 vlib_error_count (vm, error_node->node_index, error0, 1);
1899 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1900 to_next, n_left_to_next,
1905 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1908 /* Account for router advertisements sent. */
1909 vlib_error_count (vm, error_node->node_index,
1910 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1911 n_advertisements_sent);
1913 return frame->n_vectors;
1916 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1917 static_always_inline uword
1918 icmp6_router_advertisement (vlib_main_t * vm,
1919 vlib_node_runtime_t * node, vlib_frame_t * frame)
1921 vnet_main_t *vnm = vnet_get_main ();
1922 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1923 uword n_packets = frame->n_vectors;
1924 u32 *from, *to_next;
1925 u32 n_left_from, n_left_to_next, next_index;
1926 u32 n_advertisements_rcvd = 0;
1928 vlib_node_runtime_t *error_node =
1929 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1931 from = vlib_frame_vector_args (frame);
1932 n_left_from = n_packets;
1933 next_index = node->cached_next_index;
1935 if (node->flags & VLIB_NODE_FLAG_TRACE)
1936 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1938 sizeof (icmp6_input_trace_t));
1940 while (n_left_from > 0)
1942 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1944 while (n_left_from > 0 && n_left_to_next > 0)
1948 ip6_radv_t *radv_info = 0;
1949 icmp6_router_advertisement_header_t *h0;
1950 u32 bi0, options_len0, sw_if_index0, next0, error0;
1952 bi0 = to_next[0] = from[0];
1957 n_left_to_next -= 1;
1959 p0 = vlib_get_buffer (vm, bi0);
1960 ip0 = vlib_buffer_get_current (p0);
1961 h0 = ip6_next_header (ip0);
1963 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1965 error0 = ICMP6_ERROR_NONE;
1966 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1968 /* Check that source address is link-local */
1969 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1970 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1972 /* default is to drop */
1973 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1975 n_advertisements_rcvd++;
1977 if (error0 == ICMP6_ERROR_NONE)
1979 vnet_sw_interface_t *sw_if0;
1980 ethernet_interface_t *eth_if0;
1982 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1983 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1985 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1987 /* only support ethernet interface type for now */
1989 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1992 if (error0 == ICMP6_ERROR_NONE)
1996 /* look up the radv_t information for this interface */
1997 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
2001 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
2004 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2009 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
2012 if (error0 == ICMP6_ERROR_NONE)
2014 radv_info->keep_sending_rs = 0;
2018 r.sw_if_index = sw_if_index0;
2019 memcpy (&r.router_address, &ip0->src_address, 16);
2020 r.current_hop_limit = h0->current_hop_limit;
2021 r.flags = h0->flags;
2022 r.router_lifetime_in_sec =
2023 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2024 r.neighbor_reachable_time_in_msec =
2025 clib_net_to_host_u32
2026 (h0->neighbor_reachable_time_in_msec);
2027 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2030 /* validate advertised information */
2031 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2032 && (h0->current_hop_limit !=
2033 radv_info->curr_hop_limit))
2035 ip6_neighbor_syslog (vm, LOG_WARNING,
2036 "our AdvCurHopLimit on %U doesn't agree with %U",
2037 format_vnet_sw_if_index_name,
2044 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2045 != radv_info->adv_managed_flag)
2047 ip6_neighbor_syslog (vm, LOG_WARNING,
2048 "our AdvManagedFlag on %U doesn't agree with %U",
2049 format_vnet_sw_if_index_name,
2056 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2057 != radv_info->adv_other_flag)
2059 ip6_neighbor_syslog (vm, LOG_WARNING,
2060 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2061 format_vnet_sw_if_index_name,
2068 time_in_msec_between_retransmitted_neighbor_solicitations
2070 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2072 time_in_msec_between_retransmitted_neighbor_solicitations
2074 clib_host_to_net_u32 (radv_info->
2075 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
2077 ip6_neighbor_syslog (vm, LOG_WARNING,
2078 "our AdvRetransTimer on %U doesn't agree with %U",
2079 format_vnet_sw_if_index_name,
2085 if ((h0->neighbor_reachable_time_in_msec &&
2086 radv_info->adv_neighbor_reachable_time_in_msec) &&
2087 (h0->neighbor_reachable_time_in_msec !=
2088 clib_host_to_net_u32
2089 (radv_info->adv_neighbor_reachable_time_in_msec)))
2091 ip6_neighbor_syslog (vm, LOG_WARNING,
2092 "our AdvReachableTime on %U doesn't agree with %U",
2093 format_vnet_sw_if_index_name,
2099 /* check for MTU or prefix options or .. */
2100 u8 *opt_hdr = (u8 *) (h0 + 1);
2101 while (options_len0 > 0)
2103 icmp6_neighbor_discovery_option_header_t *o0 =
2104 (icmp6_neighbor_discovery_option_header_t *)
2106 int opt_len = o0->n_data_u64s << 3;
2107 icmp6_neighbor_discovery_option_type_t option_type =
2110 if (options_len0 < 2)
2112 ip6_neighbor_syslog (vm, LOG_ERR,
2113 "malformed RA packet on %U from %U",
2114 format_vnet_sw_if_index_name,
2123 ip6_neighbor_syslog (vm, LOG_ERR,
2124 " zero length option in RA on %U from %U",
2125 format_vnet_sw_if_index_name,
2131 else if (opt_len > options_len0)
2133 ip6_neighbor_syslog (vm, LOG_ERR,
2134 "option length in RA packet greater than total length on %U from %U",
2135 format_vnet_sw_if_index_name,
2142 options_len0 -= opt_len;
2145 switch (option_type)
2147 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2149 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2151 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2154 if (opt_len < sizeof (*h))
2157 memcpy (r.slla, h->ethernet_address, 6);
2161 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
2163 icmp6_neighbor_discovery_mtu_option_t *h =
2164 (icmp6_neighbor_discovery_mtu_option_t
2167 if (opt_len < sizeof (*h))
2170 r.mtu = clib_net_to_host_u32 (h->mtu);
2172 if ((h->mtu && radv_info->adv_link_mtu) &&
2174 clib_host_to_net_u32
2175 (radv_info->adv_link_mtu)))
2177 ip6_neighbor_syslog (vm, LOG_WARNING,
2178 "our AdvLinkMTU on %U doesn't agree with %U",
2179 format_vnet_sw_if_index_name,
2187 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2189 icmp6_neighbor_discovery_prefix_information_option_t
2191 (icmp6_neighbor_discovery_prefix_information_option_t
2194 /* validate advertised prefix options */
2195 ip6_radv_prefix_t *pr_info;
2196 u32 preferred, valid;
2198 if (opt_len < sizeof (*h))
2201 vec_validate (r.prefixes,
2202 vec_len (r.prefixes));
2203 ra_report_prefix_info_t *prefix =
2204 vec_elt_at_index (r.prefixes,
2205 vec_len (r.prefixes) - 1);
2208 clib_net_to_host_u32 (h->preferred_time);
2209 valid = clib_net_to_host_u32 (h->valid_time);
2211 prefix->preferred_time = preferred;
2212 prefix->valid_time = valid;
2213 prefix->flags = h->flags & 0xc0;
2214 prefix->prefix.fp_len = h->dst_address_length;
2215 prefix->prefix.fp_addr.ip6 = h->dst_address;
2216 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
2218 /* look for matching prefix - if we our advertising it, it better be consistant */
2220 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2224 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2226 if(pr_info->enabled &&
2227 (pr_info->prefix_len == h->dst_address_length) &&
2228 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2231 if(!pr_info->decrement_lifetime_flag &&
2232 valid != pr_info->adv_valid_lifetime_in_secs)
2234 ip6_neighbor_syslog(vm, LOG_WARNING,
2235 "our ADV validlifetime on %U for %U does not agree with %U",
2236 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2237 format_ip6_address, &h->dst_address);
2239 if(!pr_info->decrement_lifetime_flag &&
2240 preferred != pr_info->adv_pref_lifetime_in_secs)
2242 ip6_neighbor_syslog(vm, LOG_WARNING,
2243 "our ADV preferredlifetime on %U for %U does not agree with %U",
2244 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2245 format_ip6_address, &h->dst_address);
2263 p0->error = error_node->errors[error0];
2265 if (error0 != ICMP6_ERROR_NONE)
2266 vlib_error_count (vm, error_node->node_index, error0, 1);
2268 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2269 to_next, n_left_to_next,
2273 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2276 /* Account for router advertisements received. */
2277 vlib_error_count (vm, error_node->node_index,
2278 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2279 n_advertisements_rcvd);
2281 return frame->n_vectors;
2285 random_f64_from_to (f64 from, f64 to)
2287 static u32 seed = 0;
2288 static u8 seed_set = 0;
2291 seed = random_default_seed ();
2294 return random_f64 (&seed) * (to - from) + from;
2298 get_mac_address (u32 sw_if_index, u8 * address)
2300 vnet_main_t *vnm = vnet_get_main ();
2301 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2302 if (!hw_if->hw_address)
2304 clib_memcpy (address, hw_if->hw_address, 6);
2308 static inline vlib_buffer_t *
2309 create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2313 icmp6_router_solicitation_header_t *rh;
2318 sw_if_index = radv_info->sw_if_index;
2320 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2322 clib_warning ("buffer allocation failure");
2326 p0 = vlib_get_buffer (vm, bi0);
2327 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2328 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2330 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2331 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2333 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2335 rh = vlib_buffer_get_current (p0);
2336 p0->current_length = sizeof (*rh);
2338 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2339 rh->neighbor.icmp.code = 0;
2340 rh->neighbor.icmp.checksum = 0;
2341 rh->neighbor.reserved_must_be_zero = 0;
2343 rh->link_layer_option.header.type =
2344 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2345 if (0 != get_mac_address (sw_if_index,
2346 rh->link_layer_option.ethernet_address))
2348 clib_warning ("interface with sw_if_index %u has no mac address",
2350 vlib_buffer_free (vm, &bi0, 1);
2353 rh->link_layer_option.header.n_data_u64s = 1;
2355 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2357 rh->ip.ip_version_traffic_class_and_flow_label =
2358 clib_host_to_net_u32 (0x6 << 28);
2359 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2360 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2361 rh->ip.hop_limit = 255;
2362 rh->ip.src_address = radv_info->link_local_address;
2363 /* set address ff02::2 */
2364 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
2365 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2367 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2375 stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2379 ra->keep_sending_rs = 0;
2382 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2383 vlib_buffer_free (vm, &bi0, 1);
2389 check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2399 icmp6_send_router_solicitation_params_t *params;
2401 if (!radv_info->keep_sending_rs)
2404 params = &radv_info->params;
2406 if (radv_info->due_time > current_time)
2408 *due_time = radv_info->due_time;
2412 p0 = radv_info->buffer;
2414 next_index = ip6_rewrite_mcast_node.index;
2416 c0 = vlib_buffer_copy (vm, p0);
2417 ci0 = vlib_get_buffer_index (vm, c0);
2419 f = vlib_get_frame_to_node (vm, next_index);
2420 to_next = vlib_frame_vector_args (f);
2423 vlib_put_frame_to_node (vm, next_index, f);
2425 if (params->mrc != 0 && --radv_info->n_left == 0)
2426 stop_sending_rs (vm, radv_info);
2429 radv_info->sleep_interval =
2430 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2431 if (radv_info->sleep_interval > params->mrt)
2432 radv_info->sleep_interval =
2433 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2435 radv_info->due_time = current_time + radv_info->sleep_interval;
2437 if (params->mrd != 0
2438 && current_time > radv_info->start_time + params->mrd)
2439 stop_sending_rs (vm, radv_info);
2441 *due_time = radv_info->due_time;
2444 return radv_info->keep_sending_rs;
2448 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2451 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2452 ip6_radv_t *radv_info;
2453 uword *event_data = 0;
2454 f64 sleep_time = 1e9;
2461 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2462 vlib_process_get_events (vm, &event_data);
2463 vec_reset_length (event_data);
2465 current_time = vlib_time_now (vm);
2468 due_time = current_time + 1e9;
2470 pool_foreach (radv_info, nm->if_radv_pool,
2472 if (check_send_rs (vm, radv_info, current_time, &dt)
2477 current_time = vlib_time_now (vm);
2479 while (due_time < current_time);
2481 sleep_time = due_time - current_time;
2488 VLIB_REGISTER_NODE (send_rs_process_node) = {
2489 .function = send_rs_process,
2490 .type = VLIB_NODE_TYPE_PROCESS,
2491 .name = "send-rs-process",
2496 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2497 icmp6_send_router_solicitation_params_t *
2500 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2504 ASSERT (~0 != sw_if_index);
2506 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2507 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2509 stop_sending_rs (vm, ra);
2513 ra->keep_sending_rs = 1;
2514 ra->params = *params;
2515 ra->n_left = params->mrc;
2516 ra->start_time = vlib_time_now (vm);
2517 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2518 ra->due_time = 0; /* send first packet ASAP */
2519 ra->buffer = create_buffer_for_rs (vm, ra);
2521 ra->keep_sending_rs = 0;
2523 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2528 * @brief Add a multicast Address to the advertised MLD set
2531 ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2533 ip6_mldp_group_t *mcast_group_info;
2536 /* lookup mldp info for this interface */
2537 p = mhash_get (&radv_info->address_to_mldp_index, addr);
2539 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2542 if (!mcast_group_info)
2546 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2547 memset (mcast_group_info, 0, sizeof (*mcast_group_info));
2549 mi = mcast_group_info - radv_info->mldp_group_pool;
2550 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
2553 mcast_group_info->type = 4;
2554 mcast_group_info->mcast_source_address_pool = 0;
2555 mcast_group_info->num_sources = 0;
2556 clib_memcpy (&mcast_group_info->mcast_address, addr,
2557 sizeof (ip6_address_t));
2562 * @brief Delete a multicast Address from the advertised MLD set
2565 ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2567 ip6_mldp_group_t *mcast_group_info;
2570 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2572 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2574 if (mcast_group_info)
2576 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2578 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2583 * @brief Add a multicast Address to the advertised MLD set
2586 ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2587 ip6_multicast_address_scope_t scope,
2588 ip6_multicast_link_local_group_id_t group)
2592 ip6_set_reserved_multicast_address (&addr, scope, group);
2594 ip6_neighbor_add_mld_prefix (a, &addr);
2598 * @brief create and initialize router advertisement parameters with default
2599 * values for this intfc
2602 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
2603 u32 sw_if_index, u32 is_add)
2605 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2608 vnet_sw_interface_t *sw_if0;
2609 ethernet_interface_t *eth_if0 = 0;
2611 /* lookup radv container - ethernet interfaces only */
2612 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2613 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2614 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2619 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2621 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2625 a = pool_elt_at_index (nm->if_radv_pool, ri);
2629 ip6_radv_prefix_t *p;
2630 ip6_mldp_group_t *m;
2632 /* release the lock on the interface's mcast adj */
2633 adj_unlock (a->mcast_adj_index);
2635 /* clean up prefix and MDP pools */
2637 pool_flush(p, a->adv_prefixes_pool,
2639 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
2641 pool_flush (m, a->mldp_group_pool,
2643 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
2647 pool_free (a->mldp_group_pool);
2648 pool_free (a->adv_prefixes_pool);
2650 mhash_free (&a->address_to_prefix_index);
2651 mhash_free (&a->address_to_mldp_index);
2653 if (a->keep_sending_rs)
2654 a->keep_sending_rs = 0;
2656 pool_put (nm->if_radv_pool, a);
2657 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2665 pool_get (nm->if_radv_pool, a);
2666 memset (a, 0, sizeof (*a));
2668 ri = a - nm->if_radv_pool;
2669 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2671 /* initialize default values (most of which are zero) */
2672 clib_memset (a, 0, sizeof (a[0]));
2674 a->sw_if_index = sw_if_index;
2675 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2676 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2677 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2678 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2680 /* send ll address source address option */
2681 a->adv_link_layer_address = 1;
2683 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2684 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2685 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2686 a->seed = (u32) clib_cpu_time_now ();
2687 (void) random_u32 (&a->seed);
2688 a->randomizer = clib_cpu_time_now ();
2689 (void) random_u64 (&a->randomizer);
2691 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2692 a->initial_adverts_sent = a->initial_adverts_count - 1;
2693 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2695 /* deafult is to send */
2698 /* fill in radv_info for this interface that will be needed later */
2700 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
2702 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2704 /* fill in default link-local address (this may be overridden) */
2705 ip6_link_local_address_from_ethernet_mac_address
2706 (&a->link_local_address, eth_if0->address);
2708 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2709 sizeof (ip6_address_t));
2710 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2711 sizeof (ip6_address_t));
2713 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2717 a->keep_sending_rs = 0;
2719 /* add multicast groups we will always be reporting */
2720 ip6_neighbor_add_mld_grp (a,
2721 IP6_MULTICAST_SCOPE_link_local,
2722 IP6_MULTICAST_GROUP_ID_all_hosts);
2723 ip6_neighbor_add_mld_grp (a,
2724 IP6_MULTICAST_SCOPE_link_local,
2725 IP6_MULTICAST_GROUP_ID_all_routers);
2726 ip6_neighbor_add_mld_grp (a,
2727 IP6_MULTICAST_SCOPE_link_local,
2728 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2734 /* send an mldpv2 report */
2736 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2738 vnet_main_t *vnm = vnet_get_main ();
2739 vlib_main_t *vm = vnm->vlib_main;
2740 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2741 vnet_sw_interface_t *sw_if0;
2742 ethernet_interface_t *eth_if0;
2746 ip6_radv_t *radv_info;
2756 icmp6_multicast_listener_report_header_t *rh0;
2757 icmp6_multicast_listener_report_packet_t *rp0;
2759 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2760 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2761 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2763 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2766 /* look up the radv_t information for this interface */
2767 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2770 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2775 /* send report now - build a mldpv2 report packet */
2776 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
2777 if (PREDICT_FALSE (n_allocated == 0))
2780 clib_warning ("buffer allocation failure");
2784 b0 = vlib_get_buffer (vm, bo0);
2786 /* adjust the sizeof the buffer to just include the ipv6 header */
2787 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2789 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2791 b0->error = ICMP6_ERROR_NONE;
2793 rp0 = vlib_buffer_get_current (b0);
2794 ip0 = (ip6_header_t *) & rp0->ip;
2795 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2797 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2799 ip0->ip_version_traffic_class_and_flow_label =
2800 clib_host_to_net_u32 (0x6 << 28);
2802 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2803 /* for DEBUG - vnet driver won't seem to emit router alerts */
2804 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2807 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2809 /* source address MUST be the link-local address */
2810 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2811 ip0->src_address = radv_info->link_local_address;
2813 /* destination is all mldpv2 routers */
2814 ip6_set_reserved_multicast_address (&ip0->dst_address,
2815 IP6_MULTICAST_SCOPE_link_local,
2816 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2818 /* add reports here */
2819 ip6_mldp_group_t *m;
2820 int num_addr_records = 0;
2821 icmp6_multicast_address_record_t rr;
2823 /* fill in the hop-by-hop extension header (router alert) info */
2824 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2825 rh0->ext_hdr.n_data_u64s = 0;
2827 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2829 rh0->alert.value = 0;
2834 rh0->icmp.checksum = 0;
2837 pool_foreach (m, radv_info->mldp_group_pool,
2840 rr.aux_data_len_u32s = 0;
2841 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2842 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2846 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
2847 sizeof(icmp6_multicast_address_record_t)))
2849 vlib_buffer_free (vm, &bo0, 1);
2853 payload_length += sizeof( icmp6_multicast_address_record_t);
2858 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2860 /* update lengths */
2861 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2863 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2865 ASSERT (bogus_length == 0);
2868 * OK to override w/ no regard for actual FIB, because
2869 * ip6-rewrite only looks at the adjacency.
2871 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2872 vnet_main.local_interface_sw_if_index;
2874 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2875 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2877 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2879 f = vlib_get_frame_to_node (vm, node->index);
2880 to_next = vlib_frame_vector_args (f);
2884 vlib_put_frame_to_node (vm, node->index, f);
2889 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2891 .function = icmp6_router_solicitation,
2892 .name = "icmp6-router-solicitation",
2894 .vector_size = sizeof (u32),
2896 .format_trace = format_icmp6_input_trace,
2898 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2900 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
2901 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2902 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2907 /* send a RA or update the timer info etc.. */
2909 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2910 vlib_node_runtime_t * node,
2911 vlib_frame_t * frame)
2913 vnet_main_t *vnm = vnet_get_main ();
2914 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2915 ip6_radv_t *radv_info;
2916 vlib_frame_t *f = 0;
2917 u32 n_this_frame = 0;
2918 u32 n_left_to_next = 0;
2921 icmp6_router_solicitation_header_t *h0;
2923 f64 now = vlib_time_now (vm);
2925 /* Interface ip6 radv info list */
2927 pool_foreach (radv_info, nm->if_radv_pool,
2929 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2931 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2932 radv_info->next_multicast_time = now;
2933 radv_info->last_multicast_time = now;
2934 radv_info->last_radv_time = 0;
2935 radv_info->all_routers_mcast = 0;
2939 /* Make sure that we've joined the all-routers multicast group */
2940 if(!radv_info->all_routers_mcast)
2942 /* send MDLP_REPORT_EVENT message */
2943 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2944 radv_info->all_routers_mcast = 1;
2947 /* is it time to send a multicast RA on this interface? */
2948 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2953 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2954 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2956 /* multicast send - compute next multicast send time */
2957 if( radv_info->initial_adverts_sent > 0)
2959 radv_info->initial_adverts_sent--;
2960 if(rfn > radv_info-> initial_adverts_interval)
2961 rfn = radv_info-> initial_adverts_interval;
2963 /* check to see if we are ceasing to send */
2964 if( radv_info->initial_adverts_sent == 0)
2965 if(radv_info->cease_radv)
2966 radv_info->send_radv = 0;
2969 radv_info->next_multicast_time = rfn + now;
2970 radv_info->last_multicast_time = now;
2972 /* send advert now - build a "solicted" router advert with unspecified source address */
2973 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
2975 if (PREDICT_FALSE(n_allocated == 0))
2977 clib_warning ("buffer allocation failure");
2980 b0 = vlib_get_buffer (vm, bo0);
2981 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2982 b0->error = ICMP6_ERROR_NONE;
2983 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2985 h0 = vlib_buffer_get_current (b0);
2987 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2989 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2990 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2991 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2992 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2993 h0->ip.hop_limit = 255;
2995 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2996 h0->ip.src_address.as_u64[0] = 0;
2997 h0->ip.src_address.as_u64[1] = 0;
2999 h0->ip.dst_address.as_u64[0] = 0;
3000 h0->ip.dst_address.as_u64[1] = 0;
3002 h0->neighbor.icmp.type = ICMP6_router_solicitation;
3004 if (PREDICT_FALSE(f == 0))
3006 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
3007 to_next = vlib_frame_vector_args (f);
3008 n_left_to_next = VLIB_FRAME_SIZE;
3017 if (PREDICT_FALSE(n_left_to_next == 0))
3019 f->n_vectors = n_this_frame;
3020 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3029 ASSERT (n_this_frame);
3030 f->n_vectors = n_this_frame;
3031 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3037 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3038 vlib_node_runtime_t * node,
3039 vlib_frame_t * frame)
3042 ip6_icmp_neighbor_discovery_event_data_t *event_data;
3044 /* init code here */
3048 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
3050 event_data = vlib_process_get_event_data (vm, &event_type);
3054 /* No events found: timer expired. */
3055 /* process interface list and send RAs as appropriate, update timer info */
3056 ip6_neighbor_process_timer_event (vm, node, frame);
3063 case ICMP6_ND_EVENT_INIT:
3074 _vec_len (event_data) = 0;
3077 return frame->n_vectors;
3081 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3083 .function = icmp6_router_advertisement,
3084 .name = "icmp6-router-advertisement",
3086 .vector_size = sizeof (u32),
3088 .format_trace = format_icmp6_input_trace,
3097 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3099 .function = ip6_icmp_neighbor_discovery_event_process,
3100 .name = "ip6-icmp-neighbor-discovery-event-process",
3101 .type = VLIB_NODE_TYPE_PROCESS,
3105 icmp6_neighbor_solicitation (vlib_main_t * vm,
3106 vlib_node_runtime_t * node, vlib_frame_t * frame)
3108 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3109 /* is_solicitation */
3114 icmp6_neighbor_advertisement (vlib_main_t * vm,
3115 vlib_node_runtime_t * node,
3116 vlib_frame_t * frame)
3118 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3119 /* is_solicitation */
3124 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3126 .function = icmp6_neighbor_solicitation,
3127 .name = "icmp6-neighbor-solicitation",
3129 .vector_size = sizeof (u32),
3131 .format_trace = format_icmp6_input_trace,
3133 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3135 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
3136 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3142 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3144 .function = icmp6_neighbor_advertisement,
3145 .name = "icmp6-neighbor-advertisement",
3147 .vector_size = sizeof (u32),
3149 .format_trace = format_icmp6_input_trace,
3160 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3161 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3162 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3163 } ip6_discover_neighbor_next_t;
3167 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3168 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3169 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3170 } ip6_discover_neighbor_error_t;
3173 ip6_discover_neighbor_inline (vlib_main_t * vm,
3174 vlib_node_runtime_t * node,
3175 vlib_frame_t * frame, int is_glean)
3177 vnet_main_t *vnm = vnet_get_main ();
3178 ip6_main_t *im = &ip6_main;
3179 ip_lookup_main_t *lm = &im->lookup_main;
3180 u32 *from, *to_next_drop;
3181 uword n_left_from, n_left_to_next_drop;
3183 u32 thread_index = vm->thread_index;
3185 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3187 if (node->flags & VLIB_NODE_FLAG_TRACE)
3188 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3190 seed = throttle_seed (&im->nd_throttle, thread_index, vlib_time_now (vm));
3192 from = vlib_frame_vector_args (frame);
3193 n_left_from = frame->n_vectors;
3195 while (n_left_from > 0)
3197 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3198 to_next_drop, n_left_to_next_drop);
3200 while (n_left_from > 0 && n_left_to_next_drop > 0)
3202 u32 pi0, adj_index0, sw_if_index0, drop0, r0, next0;
3203 vnet_hw_interface_t *hw_if0;
3204 ip6_radv_t *radv_info;
3205 ip_adjacency_t *adj0;
3211 p0 = vlib_get_buffer (vm, pi0);
3213 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3215 ip0 = vlib_buffer_get_current (p0);
3217 adj0 = adj_get (adj_index0);
3221 ip0->dst_address.as_u64[0] =
3222 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3223 ip0->dst_address.as_u64[1] =
3224 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3227 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3228 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3230 /* combine the address and interface for a hash */
3231 r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;
3233 drop0 = throttle_check (&im->nd_throttle, thread_index, r0, seed);
3237 to_next_drop[0] = pi0;
3239 n_left_to_next_drop -= 1;
3241 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3243 /* If the interface is link-down, drop the pkt */
3244 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3247 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3249 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3252 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3260 * the adj has been updated to a rewrite but the node the DPO that got
3261 * us here hasn't - yet. no big deal. we'll drop while we wait.
3263 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3267 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3268 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3275 icmp6_neighbor_solicitation_header_t *h0;
3278 h0 = vlib_packet_template_get_packet
3279 (vm, &im->discover_neighbor_packet_template, &bi0);
3283 /* copy the persistent fields from the original */
3284 b0 = vlib_get_buffer (vm, bi0);
3285 clib_memcpy_fast (b0->opaque2, p0->opaque2, sizeof (p0->opaque2));
3288 * Build ethernet header.
3289 * Choose source address based on destination lookup
3292 if (!ip6_src_address_for_packet (lm,
3295 &h0->ip.src_address))
3297 /* There is no address on the interface */
3299 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3300 vlib_buffer_free (vm, &bi0, 1);
3305 * Destination address is a solicited node multicast address.
3306 * We need to fill in
3307 * the low 24 bits with low 24 bits of target's address.
3309 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3310 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3311 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3313 h0->neighbor.target_address = ip0->dst_address;
3315 clib_memcpy (h0->link_layer_option.ethernet_address,
3316 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3318 /* $$$$ appears we need this; why is the checksum non-zero? */
3319 h0->neighbor.icmp.checksum = 0;
3320 h0->neighbor.icmp.checksum =
3321 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3324 ASSERT (bogus_length == 0);
3326 vlib_buffer_copy_trace_flag (vm, p0, bi0);
3327 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3328 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3330 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3331 radv_info->mcast_adj_index;
3333 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3334 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3336 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3340 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3341 n_left_to_next_drop);
3344 return frame->n_vectors;
3348 ip6_discover_neighbor (vlib_main_t * vm,
3349 vlib_node_runtime_t * node, vlib_frame_t * frame)
3351 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3355 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3357 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3360 static char *ip6_discover_neighbor_error_strings[] = {
3361 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3362 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3363 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3364 = "no source address for ND solicitation",
3368 VLIB_REGISTER_NODE (ip6_glean_node) =
3370 .function = ip6_glean,
3371 .name = "ip6-glean",
3372 .vector_size = sizeof (u32),
3373 .format_trace = format_ip6_forward_next_trace,
3374 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3375 .error_strings = ip6_discover_neighbor_error_strings,
3376 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3379 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3380 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3383 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3385 .function = ip6_discover_neighbor,
3386 .name = "ip6-discover-neighbor",
3387 .vector_size = sizeof (u32),
3388 .format_trace = format_ip6_forward_next_trace,
3389 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3390 .error_strings = ip6_discover_neighbor_error_strings,
3391 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3394 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3395 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3400 /* API support functions */
3402 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3403 u8 suppress, u8 managed, u8 other,
3404 u8 ll_option, u8 send_unicast, u8 cease,
3405 u8 use_lifetime, u32 lifetime,
3406 u32 initial_count, u32 initial_interval,
3407 u32 max_interval, u32 min_interval, u8 is_no)
3409 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3413 /* look up the radv_t information for this interface */
3414 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3416 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3417 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3422 ip6_radv_t *radv_info;
3423 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3425 if ((max_interval != 0) && (min_interval == 0))
3426 min_interval = .75 * max_interval;
3430 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3431 radv_info->max_radv_interval;
3434 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3435 radv_info->min_radv_interval;
3438 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3439 radv_info->adv_router_lifetime_in_sec;
3443 if (lifetime > MAX_DEF_RTR_LIFETIME)
3444 lifetime = MAX_DEF_RTR_LIFETIME;
3446 if (lifetime <= max_interval)
3447 return VNET_API_ERROR_INVALID_VALUE;
3450 if (min_interval != 0)
3452 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3453 return VNET_API_ERROR_INVALID_VALUE;
3456 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3457 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3458 return VNET_API_ERROR_INVALID_VALUE;
3461 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3462 if "flag" is clear don't change corresponding value
3464 radv_info->send_radv =
3465 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3466 radv_info->adv_managed_flag =
3467 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3468 radv_info->adv_other_flag =
3469 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3470 radv_info->adv_link_layer_address =
3472 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3473 radv_info->send_unicast =
3474 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3475 radv_info->cease_radv =
3476 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3478 radv_info->min_radv_interval = min_interval;
3479 radv_info->max_radv_interval = max_interval;
3480 radv_info->adv_router_lifetime_in_sec = lifetime;
3482 radv_info->initial_adverts_count =
3484 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3485 radv_info->initial_adverts_count;
3486 radv_info->initial_adverts_interval =
3487 (initial_interval !=
3488 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3489 radv_info->initial_adverts_interval;
3492 if ((cease != 0) && (is_no))
3493 radv_info->send_radv = 1;
3495 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3496 radv_info->next_multicast_time = vlib_time_now (vm);
3497 radv_info->last_multicast_time = vlib_time_now (vm);
3498 radv_info->last_radv_time = 0;
3504 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3505 ip6_address_t * prefix_addr, u8 prefix_len,
3506 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3507 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3508 u8 no_onlink, u8 is_no)
3510 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3515 /* look up the radv_t information for this interface */
3516 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3519 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3521 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3525 f64 now = vlib_time_now (vm);
3526 ip6_radv_t *radv_info;
3527 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3529 /* prefix info add, delete or update */
3530 ip6_radv_prefix_t *prefix;
3532 /* lookup prefix info for this address on this interface */
3533 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3535 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3541 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3543 if (prefix->prefix_len != prefix_len)
3544 return VNET_API_ERROR_INVALID_VALUE_2;
3546 /* FIXME - Should the DP do this or the CP ? */
3547 /* do specific delete processing here before returning */
3548 /* try to remove from routing table */
3550 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3552 pool_put (radv_info->adv_prefixes_pool, prefix);
3554 radv_info->initial_adverts_sent =
3555 radv_info->initial_adverts_count - 1;
3556 radv_info->next_multicast_time = vlib_time_now (vm);
3557 radv_info->last_multicast_time = vlib_time_now (vm);
3558 radv_info->last_radv_time = 0;
3562 /* adding or changing */
3567 pool_get (radv_info->adv_prefixes_pool, prefix);
3568 memset (prefix, 0, sizeof (*prefix));
3569 pi = prefix - radv_info->adv_prefixes_pool;
3570 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3573 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3575 prefix->prefix_len = prefix_len;
3576 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3578 /* initialize default values */
3579 prefix->adv_on_link_flag = 1; /* L bit set */
3580 prefix->adv_autonomous_flag = 1; /* A bit set */
3581 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
3582 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3583 prefix->enabled = 1;
3584 prefix->decrement_lifetime_flag = 1;
3585 prefix->deprecated_prefix_flag = 1;
3589 /* FIXME - Should the DP do this or the CP ? */
3590 /* insert prefix into routing table as a connected prefix */
3599 if (prefix->prefix_len != prefix_len)
3600 return VNET_API_ERROR_INVALID_VALUE_2;
3604 /* FIXME - Should the DP do this or the CP ? */
3605 /* remove from routing table if already there */
3609 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
3611 prefix->adv_valid_lifetime_in_secs = ~0;
3612 prefix->adv_pref_lifetime_in_secs = ~0;
3613 prefix->decrement_lifetime_flag = 0;
3617 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3618 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
3621 /* copy remaining */
3622 prefix->enabled = !(no_advertise != 0);
3623 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3624 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3628 /* fill in the expiration times */
3629 prefix->valid_lifetime_expires =
3630 now + prefix->adv_valid_lifetime_in_secs;
3631 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
3633 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3634 radv_info->next_multicast_time = vlib_time_now (vm);
3635 radv_info->last_multicast_time = vlib_time_now (vm);
3636 radv_info->last_radv_time = 0;
3642 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3643 vlib_cli_command_t * cmd)
3645 vnet_main_t *vnm = vnet_get_main ();
3646 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3647 clib_error_t *error = 0;
3649 u8 suppress = 0, managed = 0, other = 0;
3650 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
3651 u8 use_lifetime = 0;
3652 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3653 0, ra_initial_interval = 0;
3654 u32 ra_max_interval = 0, ra_min_interval = 0;
3656 unformat_input_t _line_input, *line_input = &_line_input;
3657 vnet_sw_interface_t *sw_if0;
3659 int add_radv_info = 1;
3660 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
3661 ip6_address_t ip6_addr;
3665 /* Get a line of input. */
3666 if (!unformat_user (main_input, unformat_line_input, line_input))
3669 /* get basic radv info for this interface */
3670 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3673 if (unformat_user (line_input,
3674 unformat_vnet_sw_interface, vnm, &sw_if_index))
3677 ethernet_interface_t *eth_if0 = 0;
3679 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3680 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3682 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
3687 clib_error_return (0, "Interface must be of ethernet type");
3691 /* look up the radv_t information for this interface */
3692 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3695 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3699 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3703 error = clib_error_return (0, "unknown interface %U'",
3704 format_unformat_error, line_input);
3710 error = clib_error_return (0, "invalid interface name %U'",
3711 format_unformat_error, line_input);
3716 /* get the rest of the command */
3717 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3719 if (unformat (line_input, "no"))
3721 else if (unformat (line_input, "prefix %U/%d",
3722 unformat_ip6_address, &ip6_addr, &addr_len))
3727 else if (unformat (line_input, "ra-managed-config-flag"))
3732 else if (unformat (line_input, "ra-other-config-flag"))
3737 else if (unformat (line_input, "ra-suppress") ||
3738 unformat (line_input, "ra-surpress"))
3743 else if (unformat (line_input, "ra-suppress-link-layer") ||
3744 unformat (line_input, "ra-surpress-link-layer"))
3746 suppress_ll_option = 1;
3749 else if (unformat (line_input, "ra-send-unicast"))
3754 else if (unformat (line_input, "ra-lifetime"))
3756 if (!unformat (line_input, "%d", &ra_lifetime))
3758 error = unformat_parse_error (line_input);
3764 else if (unformat (line_input, "ra-initial"))
3767 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
3769 error = unformat_parse_error (line_input);
3774 else if (unformat (line_input, "ra-interval"))
3776 if (!unformat (line_input, "%d", &ra_max_interval))
3778 error = unformat_parse_error (line_input);
3782 if (!unformat (line_input, "%d", &ra_min_interval))
3783 ra_min_interval = 0;
3786 else if (unformat (line_input, "ra-cease"))
3793 error = unformat_parse_error (line_input);
3800 ip6_neighbor_ra_config (vm, sw_if_index,
3801 suppress, managed, other,
3802 suppress_ll_option, send_unicast, cease,
3803 use_lifetime, ra_lifetime,
3804 ra_initial_count, ra_initial_interval,
3805 ra_max_interval, ra_min_interval, is_no);
3809 u32 valid_lifetime_in_secs = 0;
3810 u32 pref_lifetime_in_secs = 0;
3811 u8 use_prefix_default_values = 0;
3812 u8 no_advertise = 0;
3814 u8 no_autoconfig = 0;
3817 /* get the rest of the command */
3818 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3820 if (unformat (line_input, "default"))
3822 use_prefix_default_values = 1;
3825 else if (unformat (line_input, "infinite"))
3827 valid_lifetime_in_secs = ~0;
3828 pref_lifetime_in_secs = ~0;
3831 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3832 &pref_lifetime_in_secs))
3839 /* get the rest of the command */
3840 while (!use_prefix_default_values &&
3841 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3843 if (unformat (line_input, "no-advertise"))
3845 else if (unformat (line_input, "off-link"))
3847 else if (unformat (line_input, "no-autoconfig"))
3849 else if (unformat (line_input, "no-onlink"))
3853 error = unformat_parse_error (line_input);
3858 ip6_neighbor_ra_prefix (vm, sw_if_index,
3859 &ip6_addr, addr_len,
3860 use_prefix_default_values,
3861 valid_lifetime_in_secs,
3862 pref_lifetime_in_secs,
3864 off_link, no_autoconfig, no_onlink, is_no);
3868 unformat_free (line_input);
3874 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3876 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3879 for (i = 0; i < vec_len (addrs); i++)
3881 ip_interface_address_t *a =
3882 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3883 ip6_address_t *address = ip_interface_address_get_address (lm, a);
3885 vlib_cli_output (vm, "\t\t%U/%d",
3886 format_ip6_address, address, a->address_length);
3890 static clib_error_t *
3891 show_ip6_interface_cmd (vlib_main_t * vm,
3892 unformat_input_t * input, vlib_cli_command_t * cmd)
3894 vnet_main_t *vnm = vnet_get_main ();
3895 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3896 clib_error_t *error = 0;
3901 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3905 /* look up the radv_t information for this interface */
3906 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3909 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3913 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3914 ip6_radv_t *radv_info;
3915 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3917 vlib_cli_output (vm, "%U is admin %s\n",
3918 format_vnet_sw_interface_name, vnm,
3919 vnet_get_sw_interface (vnm, sw_if_index),
3920 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3924 u32 *link_scope = 0, *global_scope = 0;
3925 u32 *local_scope = 0, *unknown_scope = 0;
3926 ip_interface_address_t *a;
3928 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3930 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3932 while (ai != (u32) ~ 0)
3934 a = pool_elt_at_index (lm->if_address_pool, ai);
3935 ip6_address_t *address =
3936 ip_interface_address_get_address (lm, a);
3938 if (ip6_address_is_link_local_unicast (address))
3939 vec_add1 (link_scope, ai);
3940 else if (ip6_address_is_global_unicast (address))
3941 vec_add1 (global_scope, ai);
3942 else if (ip6_address_is_local_unicast (address))
3943 vec_add1 (local_scope, ai);
3945 vec_add1 (unknown_scope, ai);
3947 ai = a->next_this_sw_interface;
3950 if (vec_len (link_scope))
3952 vlib_cli_output (vm, "\tLink-local address(es):\n");
3953 ip6_print_addrs (vm, link_scope);
3954 vec_free (link_scope);
3957 if (vec_len (local_scope))
3959 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3960 ip6_print_addrs (vm, local_scope);
3961 vec_free (local_scope);
3964 if (vec_len (global_scope))
3966 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3967 ip6_print_addrs (vm, global_scope);
3968 vec_free (global_scope);
3971 if (vec_len (unknown_scope))
3973 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3974 ip6_print_addrs (vm, unknown_scope);
3975 vec_free (unknown_scope);
3978 vlib_cli_output (vm, "\tLink-local address(es):\n");
3979 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3980 &radv_info->link_local_address);
3982 vlib_cli_output (vm, "\tJoined group address(es):\n");
3983 ip6_mldp_group_t *m;
3985 pool_foreach (m, radv_info->mldp_group_pool,
3987 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3992 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3993 ip6_radv_prefix_t *p;
3995 pool_foreach (p, radv_info->adv_prefixes_pool,
3997 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3998 format_ip6_address, &p->prefix, p->prefix_len);
4002 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
4003 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
4004 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
4005 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
4006 vlib_cli_output (vm, "\tND DAD is disabled\n");
4007 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
4008 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
4009 radv_info->adv_neighbor_reachable_time_in_msec);
4010 vlib_cli_output (vm,
4011 "\tND advertised retransmit interval is %d (msec)\n",
4013 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
4015 u32 ra_interval = radv_info->max_radv_interval;
4016 u32 ra_interval_min = radv_info->min_radv_interval;
4017 vlib_cli_output (vm,
4018 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
4019 ra_interval, ra_interval_min);
4020 vlib_cli_output (vm,
4021 "\tND router advertisements live for %d seconds\n",
4022 radv_info->adv_router_lifetime_in_sec);
4023 vlib_cli_output (vm,
4024 "\tHosts %s stateless autoconfig for addresses\n",
4025 (radv_info->adv_managed_flag) ? "use" :
4027 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4028 radv_info->n_advertisements_sent);
4029 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4030 radv_info->n_solicitations_rcvd);
4031 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4032 radv_info->n_solicitations_dropped);
4036 error = clib_error_return (0, "IPv6 not enabled on interface",
4037 format_unformat_error, input);
4045 * This command is used to display various IPv6 attributes on a given
4049 * Example of how to display IPv6 settings:
4050 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4051 * GigabitEthernet2/0/0 is admin up
4052 * Link-local address(es):
4054 * Joined group address(es):
4059 * Advertised Prefixes:
4060 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4062 * ICMP error messages are unlimited
4063 * ICMP redirects are disabled
4064 * ICMP unreachables are not sent
4065 * ND DAD is disabled
4066 * ND advertised reachable time is 0
4067 * ND advertised retransmit interval is 0 (msec)
4068 * ND router advertisements are sent every 200 seconds (min interval is 150)
4069 * ND router advertisements live for 600 seconds
4070 * Hosts use stateless autoconfig for addresses
4071 * ND router advertisements sent 19336
4072 * ND router solicitations received 0
4073 * ND router solicitations dropped 0
4075 * Example of output if IPv6 is not enabled on the interface:
4076 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4077 * show ip6 interface: IPv6 not enabled on interface
4081 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4083 .path = "show ip6 interface",
4084 .function = show_ip6_interface_cmd,
4085 .short_help = "show ip6 interface <interface>",
4090 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4092 clib_error_t *error = 0;
4093 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4096 /* look up the radv_t information for this interface */
4097 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4099 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4101 /* if not created - do nothing */
4104 ip6_radv_t *radv_info;
4106 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4108 /* check radv_info ref count for other ip6 addresses on this interface */
4109 /* This implicitly excludes the link local address */
4110 if (radv_info->ref_count == 0)
4112 /* essentially "disables" ipv6 on this interface */
4113 ip6_ll_prefix_t ilp = {
4114 .ilp_addr = radv_info->link_local_address,
4115 .ilp_sw_if_index = sw_if_index,
4117 ip6_ll_table_entry_delete (&ilp);
4118 ip6_sw_interface_enable_disable (sw_if_index, 0);
4119 ip6_mfib_interface_enable_disable (sw_if_index, 0);
4120 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4128 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
4130 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4133 /* look up the radv_t information for this interface */
4134 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4137 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4143 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4145 clib_error_t *error = 0;
4146 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4150 /* look up the radv_t information for this interface */
4151 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4154 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4156 /* if not created yet */
4159 vnet_main_t *vnm = vnet_get_main ();
4160 vnet_sw_interface_t *sw_if0;
4162 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4163 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4165 ethernet_interface_t *eth_if0;
4168 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
4171 /* create radv_info. for this interface. This holds all the info needed for router adverts */
4173 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
4177 ip6_radv_t *radv_info;
4178 ip6_address_t link_local_address;
4180 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4182 ip6_link_local_address_from_ethernet_mac_address
4183 (&link_local_address, eth_if0->address);
4185 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
4186 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
4187 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
4188 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
4190 /* make up an interface id */
4191 link_local_address.as_u64[1] =
4192 random_u64 (&radv_info->randomizer);
4194 link_local_address.as_u64[0] =
4195 clib_host_to_net_u64 (0xFE80000000000000ULL);
4197 link_local_address.as_u8[8] &= 0xfd;
4200 ip6_ll_prefix_t ilp = {
4201 .ilp_addr = link_local_address,
4202 .ilp_sw_if_index = sw_if_index,
4205 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4208 /* essentially "enables" ipv6 on this interface */
4209 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4210 ip6_sw_interface_enable_disable (sw_if_index, 1);
4214 radv_info->link_local_address = link_local_address;
4224 ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4226 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4227 ip6_radv_t *radv_info;
4230 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
4233 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4238 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4239 *addr = radv_info->link_local_address;
4244 static clib_error_t *
4245 enable_ip6_interface_cmd (vlib_main_t * vm,
4246 unformat_input_t * input, vlib_cli_command_t * cmd)
4248 vnet_main_t *vnm = vnet_get_main ();
4249 clib_error_t *error = 0;
4254 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4256 enable_ip6_interface (vm, sw_if_index);
4260 error = clib_error_return (0, "unknown interface\n'",
4261 format_unformat_error, input);
4268 * This command is used to enable IPv6 on a given interface.
4271 * Example of how enable IPv6 on a given interface:
4272 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4275 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4277 .path = "enable ip6 interface",
4278 .function = enable_ip6_interface_cmd,
4279 .short_help = "enable ip6 interface <interface>",
4283 static clib_error_t *
4284 disable_ip6_interface_cmd (vlib_main_t * vm,
4285 unformat_input_t * input, vlib_cli_command_t * cmd)
4287 vnet_main_t *vnm = vnet_get_main ();
4288 clib_error_t *error = 0;
4293 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4295 error = disable_ip6_interface (vm, sw_if_index);
4299 error = clib_error_return (0, "unknown interface\n'",
4300 format_unformat_error, input);
4307 * This command is used to disable IPv6 on a given interface.
4310 * Example of how disable IPv6 on a given interface:
4311 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4314 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4316 .path = "disable ip6 interface",
4317 .function = disable_ip6_interface_cmd,
4318 .short_help = "disable ip6 interface <interface>",
4323 * This command is used to configure the neighbor discovery
4324 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4325 * command to display some of the current neighbor discovery parameters
4326 * on a given interface. This command has three formats:
4329 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4331 * '<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>'
4335 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4336 * router-advertisement messages to use stateful address
4337 * auto-configuration to obtain address information (sets the M-bit).
4338 * Default is the M-bit is not set and the '<em>no</em>' option
4339 * returns it to this default state.
4341 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4342 * router-advertisement messages that hosts use stateful auto
4343 * configuration to obtain nonaddress related information (sets
4344 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4345 * option returns it to this default state.
4347 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4348 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4349 * router-advertisement messages.
4351 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4352 * optional source link-layer address in the ICMPv6 router-advertisement
4353 * messages. Default is to include the optional source link-layer address
4354 * and the '<em>no</em>' option returns it to this default state.
4356 * <em>[no] ra-send-unicast</em> - Use the source address of the
4357 * router-solicitation message if availiable. The default is to use
4358 * multicast address of all nodes, and the '<em>no</em>' option returns
4359 * it to this default state.
4361 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4362 * default router in ICMPv6 router-advertisement messages. The range is
4363 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4364 * '<em><max-interval></em>'. The default value is 600 seconds and the
4365 * '<em>no</em>' option returns it to this default value.
4367 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4368 * router-advertisement messages sent and the interval between each
4369 * message. Range for count is 1 - 3 and default is 3. Range for interval
4370 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4371 * returns both to their default value.
4373 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4374 * interval between sending ICMPv6 router-advertisement messages. The
4375 * range for max-interval is from 4 to 200 seconds. min-interval can not
4376 * be more than 75% of max-interval. If not set, min-interval will be
4377 * set to 75% of max-interval. The range for min-interval is from 3 to
4378 * 150 seconds. The '<em>no</em>' option returns both to their default
4381 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4382 * The '<em>no</em>' options implies to start (or restart) sending
4383 * ICMPv6 router-advertisement messages.
4386 * <b>Format 2 - Prefix Options:</b>
4388 * '<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>'
4392 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4394 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4395 * length of time in seconds during what the prefix is valid for the purpose of
4396 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4397 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4398 * length of time in seconds during what addresses generated from the prefix remain
4399 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4401 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4402 * are inifinte, no timeout.
4404 * <em>no-advertise</em> - Do not send full router address in prefix
4405 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4407 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4408 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4409 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4411 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4412 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4414 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4415 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4416 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4420 * <b>Format 3: - Default of Prefix:</b>
4422 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4424 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4425 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4426 * is ignored and the prefix is deleted.
4430 * Example of how set a router advertisement option:
4431 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4432 * Example of how to add a prefix:
4433 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4434 * Example of how to delete a prefix:
4435 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4438 VLIB_CLI_COMMAND (ip6_nd_command, static) =
4441 .short_help = "ip6 nd <interface> ...",
4442 .function = ip6_neighbor_cmd,
4447 ip6_neighbor_set_link_local_address (vlib_main_t * vm, u32 sw_if_index,
4448 ip6_address_t * address)
4450 clib_error_t *error = 0;
4451 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4453 ip6_radv_t *radv_info;
4454 vnet_main_t *vnm = vnet_get_main ();
4455 ip6_ll_prefix_t pfx = { 0, };
4457 if (!ip6_address_is_link_local_unicast (address))
4458 return (error = clib_error_return (0, "address not link-local",
4459 format_unformat_error));
4461 /* call enable ipv6 */
4462 enable_ip6_interface (vm, sw_if_index);
4464 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4468 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4470 pfx.ilp_sw_if_index = sw_if_index;
4472 pfx.ilp_addr = radv_info->link_local_address;
4473 ip6_ll_table_entry_delete (&pfx);
4475 pfx.ilp_addr = *address;
4476 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_LOCAL);
4478 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4479 radv_info->link_local_address = *address;
4483 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4484 error = clib_error_return (0, "ip6 not enabled for interface",
4485 format_unformat_error);
4491 * @brief callback when an interface address is added or deleted
4494 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4497 ip6_address_t * address,
4499 u32 if_address_index, u32 is_delete)
4501 vnet_main_t *vnm = vnet_get_main ();
4502 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4504 vlib_main_t *vm = vnm->vlib_main;
4505 ip6_radv_t *radv_info;
4508 /* create solicited node multicast address for this interface address */
4509 ip6_set_solicited_node_multicast_address (&a, 0);
4511 a.as_u8[0xd] = address->as_u8[0xd];
4512 a.as_u8[0xe] = address->as_u8[0xe];
4513 a.as_u8[0xf] = address->as_u8[0xf];
4517 /* try to create radv_info - does nothing if ipv6 already enabled */
4518 enable_ip6_interface (vm, sw_if_index);
4520 /* look up the radv_t information for this interface */
4521 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4523 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4527 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4530 if (!ip6_address_is_link_local_unicast (address))
4531 radv_info->ref_count++;
4533 ip6_neighbor_add_mld_prefix (radv_info, &a);
4540 /* look up the radv_t information for this interface */
4541 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4543 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4548 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4550 ip6_neighbor_del_mld_prefix (radv_info, &a);
4552 /* if interface up send MLDP "report" */
4553 radv_info->all_routers_mcast = 0;
4556 if (!ip6_address_is_link_local_unicast (address))
4557 radv_info->ref_count--;
4559 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4560 disable_ip6_interface (vm, sw_if_index);
4565 ip6_set_neighbor_limit (u32 neighbor_limit)
4567 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4569 nm->limit_neighbor_cache_size = neighbor_limit;
4574 ip6_neighbor_table_bind (ip6_main_t * im,
4577 u32 new_fib_index, u32 old_fib_index)
4579 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4580 ip6_neighbor_t *n = NULL;
4581 u32 i, *to_re_add = 0;
4584 pool_foreach (n, nm->neighbor_pool,
4586 if (n->key.sw_if_index == sw_if_index)
4587 vec_add1 (to_re_add, n - nm->neighbor_pool);
4591 for (i = 0; i < vec_len (to_re_add); i++)
4593 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4594 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4595 ip6_neighbor_adj_fib_add (n, new_fib_index);
4597 vec_free (to_re_add);
4600 static clib_error_t *
4601 ip6_neighbor_init (vlib_main_t * vm)
4603 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4604 ip6_main_t *im = &ip6_main;
4606 mhash_init (&nm->neighbor_index_by_key,
4607 /* value size */ sizeof (uword),
4608 /* key size */ sizeof (ip6_neighbor_key_t));
4610 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4611 ip6_icmp_neighbor_solicitation_node.index);
4612 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4613 ip6_icmp_neighbor_advertisement_node.index);
4614 icmp6_register_type (vm, ICMP6_router_solicitation,
4615 ip6_icmp_router_solicitation_node.index);
4616 icmp6_register_type (vm, ICMP6_router_advertisement,
4617 ip6_icmp_router_advertisement_node.index);
4619 /* handler node for ip6 neighbor discovery events and timers */
4620 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4622 /* add call backs */
4623 ip6_add_del_interface_address_callback_t cb;
4624 clib_memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4626 /* when an interface address changes... */
4627 cb.function = ip6_neighbor_add_del_interface_address;
4628 cb.function_opaque = 0;
4629 vec_add1 (im->add_del_interface_address_callbacks, cb);
4631 ip6_table_bind_callback_t cbt;
4632 cbt.function = ip6_neighbor_table_bind;
4633 cbt.function_opaque = 0;
4634 vec_add1 (im->table_bind_callbacks, cbt);
4636 mhash_init (&nm->pending_resolutions_by_address,
4637 /* value size */ sizeof (uword),
4638 /* key size */ sizeof (ip6_address_t));
4640 mhash_init (&nm->mac_changes_by_address,
4641 /* value size */ sizeof (uword),
4642 /* key size */ sizeof (ip6_address_t));
4644 /* default, configurable */
4645 nm->limit_neighbor_cache_size = 50000;
4647 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4649 nm->ip6_ra_publisher_node = (uword) ~ 0;
4652 /* $$$$ Hack fix for today */
4653 vec_validate_init_empty
4654 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
4660 VLIB_INIT_FUNCTION (ip6_neighbor_init);
4664 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4667 uword type_opaque, uword data)
4669 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4670 ip6_address_t *address = address_arg;
4672 pending_resolution_t *pr;
4674 pool_get (nm->pending_resolutions, pr);
4675 memset (pr, 0, sizeof (*pr));
4677 pr->next_index = ~0;
4678 pr->node_index = node_index;
4679 pr->type_opaque = type_opaque;
4682 p = mhash_get (&nm->pending_resolutions_by_address, address);
4685 /* Insert new resolution at the head of the list */
4686 pr->next_index = p[0];
4687 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4690 mhash_set (&nm->pending_resolutions_by_address, address,
4691 pr - nm->pending_resolutions, 0 /* old value */ );
4695 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4696 ip6_nd_change_event_cb_t data_callback,
4700 uword type_opaque, uword data, int is_add)
4702 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4703 ip6_address_t *address = address_arg;
4705 /* Try to find an existing entry */
4706 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4708 pending_resolution_t *mc;
4709 while (p && *p != ~0)
4711 mc = pool_elt_at_index (nm->mac_changes, *p);
4712 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4715 p = &mc->next_index;
4718 int found = p && *p != ~0;
4722 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4724 pool_get (nm->mac_changes, mc);
4725 memset (mc, 0, sizeof (*mc));
4727 *mc = (pending_resolution_t)
4730 .node_index = node_index,
4731 .type_opaque = type_opaque,
4733 .data_callback = data_callback,
4738 /* Insert new resolution at the end of the list */
4739 u32 new_idx = mc - nm->mac_changes;
4743 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
4748 return VNET_API_ERROR_NO_SUCH_ENTRY;
4750 /* Clients may need to clean up pool entries, too */
4752 (data_callback) (mc->data, NULL /* no new mac addrs */ , 0, NULL);
4754 /* Remove the entry from the list and delete the entry */
4755 *p = mc->next_index;
4756 pool_put (nm->mac_changes, mc);
4758 /* Remove from hash if we deleted the last entry */
4759 if (*p == ~0 && p == first)
4760 mhash_unset (&nm->mac_changes_by_address, address, 0);
4766 vnet_ip6_nd_term (vlib_main_t * vm,
4767 vlib_node_runtime_t * node,
4769 ethernet_header_t * eth,
4770 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
4772 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4773 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4776 mac_address_from_bytes (&mac, eth->src_address);
4777 ndh = ip6_next_header (ip);
4778 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4779 ndh->icmp.type != ICMP6_neighbor_advertisement)
4782 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4783 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4785 u8 *t0 = vlib_add_trace (vm, node, p0,
4786 sizeof (icmp6_input_trace_t));
4787 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4790 /* Check if anyone want ND events for L2 BDs */
4792 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4793 && !ip6_address_is_link_local_unicast (&ip->src_address)))
4795 vnet_nd_wc_publish (sw_if_index, &mac, &ip->src_address);
4798 /* Check if MAC entry exsist for solicited target IP */
4799 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4801 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4802 l2_bridge_domain_t *bd_config;
4805 opt = (void *) (ndh + 1);
4806 if ((opt->header.type !=
4807 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4808 (opt->header.n_data_u64s != 1))
4809 return 0; /* source link layer address option not present */
4811 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4813 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4815 { /* found ip-mac entry, generate eighbor advertisement response */
4817 vlib_node_runtime_t *error_node =
4818 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4819 ip->dst_address = ip->src_address;
4820 ip->src_address = ndh->target_address;
4821 ip->hop_limit = 255;
4823 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4824 clib_memcpy (opt->ethernet_address, macp, 6);
4825 ndh->icmp.type = ICMP6_neighbor_advertisement;
4826 ndh->advertisement_flags = clib_host_to_net_u32
4827 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4828 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4829 ndh->icmp.checksum = 0;
4830 ndh->icmp.checksum =
4831 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4832 clib_memcpy (eth->dst_address, eth->src_address, 6);
4833 clib_memcpy (eth->src_address, macp, 6);
4834 vlib_error_count (vm, error_node->node_index,
4835 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4845 ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4849 fib_prefix_t pfx = {
4851 .fp_proto = FIB_PROTOCOL_IP6,
4856 ip46_address_t nh = {
4860 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4862 if (~0 == fib_index)
4863 return VNET_API_ERROR_NO_SUCH_FIB;
4867 fib_table_entry_path_remove (fib_index,
4869 FIB_SOURCE_IP6_ND_PROXY,
4873 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4874 /* flush the ND cache of this address if it's there */
4875 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
4879 fib_table_entry_path_add (fib_index,
4881 FIB_SOURCE_IP6_ND_PROXY,
4882 FIB_ENTRY_FLAG_NONE,
4886 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4891 static clib_error_t *
4892 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4893 unformat_input_t * input, vlib_cli_command_t * cmd)
4895 vnet_main_t *vnm = vnet_get_main ();
4896 clib_error_t *error = 0;
4901 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4903 /* get the rest of the command */
4904 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4906 if (unformat (input, "%U", unformat_ip6_address, &addr))
4908 else if (unformat (input, "delete") || unformat (input, "del"))
4911 return (unformat_parse_error (input));
4915 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4921 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4923 .path = "set ip6 nd proxy",
4924 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4925 .function = set_ip6_nd_proxy_cmd,
4930 ethernet_ndp_change_mac (u32 sw_if_index)
4932 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4937 pool_foreach (n, nm->neighbor_pool,
4939 if (n->key.sw_if_index == sw_if_index)
4941 adj_nbr_walk_nh6 (sw_if_index,
4942 &n->key.ip6_address,
4943 ip6_nd_mk_complete_walk, n);
4948 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4950 if (ADJ_INDEX_INVALID != ai)
4951 adj_glean_update_rewrite (ai);
4955 send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
4957 ip6_main_t *i6m = &ip6_main;
4958 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4960 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
4964 send_ip6_na_w_addr (vlib_main_t * vm,
4965 const ip6_address_t * ip6_addr, u32 sw_if_index)
4967 ip6_main_t *i6m = &ip6_main;
4968 vnet_main_t *vnm = vnet_get_main ();
4969 u8 *rewrite, rewrite_len;
4970 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4976 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4977 format_ip6_address, ip6_addr, sw_if_index);
4979 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4982 icmp6_neighbor_solicitation_header_t *h =
4983 vlib_packet_template_get_packet (vm,
4984 &i6m->discover_neighbor_packet_template,
4989 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4990 IP6_MULTICAST_SCOPE_link_local,
4991 IP6_MULTICAST_GROUP_ID_all_hosts);
4992 h->ip.src_address = ip6_addr[0];
4993 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4994 h->neighbor.target_address = ip6_addr[0];
4995 h->neighbor.advertisement_flags = clib_host_to_net_u32
4996 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4997 h->link_layer_option.header.type =
4998 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4999 clib_memcpy (h->link_layer_option.ethernet_address,
5000 hi->hw_address, vec_len (hi->hw_address));
5001 h->neighbor.icmp.checksum =
5002 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
5003 ASSERT (bogus_length == 0);
5005 /* Setup MAC header with IP6 Etype and mcast DMAC */
5006 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
5007 ip6_multicast_ethernet_address (dst_address,
5008 IP6_MULTICAST_GROUP_ID_all_hosts);
5010 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
5011 rewrite_len = vec_len (rewrite);
5012 vlib_buffer_advance (b, -rewrite_len);
5013 ethernet_header_t *e = vlib_buffer_get_current (b);
5014 clib_memcpy (e->dst_address, rewrite, rewrite_len);
5017 /* Send unsolicited ND advertisement packet out the specified interface */
5018 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5019 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5020 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5021 u32 *to_next = vlib_frame_vector_args (f);
5024 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5029 * fd.io coding-style-patch-verification: ON
5032 * eval: (c-set-style "gnu")