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 <vppinfra/md5.h>
23 #include <vnet/adj/adj.h>
24 #include <vnet/adj/adj_mcast.h>
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/fib/ip6_fib.h>
27 #include <vnet/mfib/ip6_mfib.h>
31 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
33 * The files contains the API and CLI code for managing IPv6 neighbor
34 * adjacency tables and neighbor discovery logic.
37 /* can't use sizeof link_layer_address, that's 8 */
38 #define ETHER_MAC_ADDR_LEN 6
40 /* advertised prefix option */
43 /* basic advertised information */
47 int adv_autonomous_flag;
48 u32 adv_valid_lifetime_in_secs;
49 u32 adv_pref_lifetime_in_secs;
51 /* advertised values are computed from these times if decrementing */
52 f64 valid_lifetime_expires;
53 f64 pref_lifetime_expires;
55 /* local information */
57 int deprecated_prefix_flag;
58 int decrement_lifetime_flag;
60 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
61 #define DEF_ADV_VALID_LIFETIME 2592000
62 #define DEF_ADV_PREF_LIFETIME 604800
64 /* extensions are added here, mobile, DNS etc.. */
70 /* group information */
72 ip6_address_t mcast_address;
74 ip6_address_t *mcast_source_address_pool;
77 /* configured router advertisement information per ipv6 interface */
81 /* advertised config information, zero means unspecified */
85 u16 adv_router_lifetime_in_sec;
86 u32 adv_neighbor_reachable_time_in_msec;
87 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
92 /* source link layer option */
93 u8 link_layer_address[8];
94 u8 link_layer_addr_len;
97 ip6_radv_prefix_t *adv_prefixes_pool;
99 /* Hash table mapping address to index in interface advertised prefix pool. */
100 mhash_t address_to_prefix_index;
102 /* MLDP group information */
103 ip6_mldp_group_t *mldp_group_pool;
105 /* Hash table mapping address to index in mldp address pool. */
106 mhash_t address_to_mldp_index;
108 /* local information */
110 int send_radv; /* radv on/off on this interface - set by config */
111 int cease_radv; /* we are ceasing to send - set byf config */
113 int adv_link_layer_address;
115 int failed_device_check;
116 int all_routers_mcast;
120 adj_index_t mcast_adj_index;
122 /* timing information */
123 #define DEF_MAX_RADV_INTERVAL 200
124 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125 #define DEF_CURR_HOP_LIMIT 64
126 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
127 #define MAX_DEF_RTR_LIFETIME 9000
129 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
130 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
131 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
132 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
133 #define MAX_RA_DELAY_TIME .5 /* seconds */
135 f64 max_radv_interval;
136 f64 min_radv_interval;
137 f64 min_delay_between_radv;
138 f64 max_delay_between_radv;
139 f64 max_rtr_default_lifetime;
142 f64 last_multicast_time;
143 f64 next_multicast_time;
146 u32 initial_adverts_count;
147 f64 initial_adverts_interval;
148 u32 initial_adverts_sent;
151 u32 n_advertisements_sent;
152 u32 n_solicitations_rcvd;
153 u32 n_solicitations_dropped;
155 /* Link local address to use (defaults to underlying physical for logical interfaces */
156 ip6_address_t link_local_address;
165 /* Used for nd event notification only */
168 } pending_resolution_t;
173 /* Hash tables mapping name to opcode. */
174 uword *opcode_by_name;
176 /* lite beer "glean" adjacency handling */
177 mhash_t pending_resolutions_by_address;
178 pending_resolution_t *pending_resolutions;
180 /* Mac address change notification */
181 mhash_t mac_changes_by_address;
182 pending_resolution_t *mac_changes;
184 u32 *neighbor_input_next_index_by_hw_if_index;
186 ip6_neighbor_t *neighbor_pool;
188 mhash_t neighbor_index_by_key;
190 u32 *if_radv_pool_index_by_sw_if_index;
192 ip6_radv_t *if_radv_pool;
194 /* Neighbor attack mitigation */
195 u32 limit_neighbor_cache_size;
196 u32 neighbor_delete_rotor;
198 /* Wildcard nd report publisher */
199 uword wc_ip6_nd_publisher_node;
200 uword wc_ip6_nd_publisher_et;
201 } ip6_neighbor_main_t;
203 /* ipv6 neighbor discovery - timer/event types */
207 } ip6_icmp_neighbor_discovery_event_type_t;
217 } ip6_icmp_neighbor_discovery_event_data_t;
219 static ip6_neighbor_main_t ip6_neighbor_main;
220 static ip6_address_t ip6a_zero; /* ip6 address 0 */
222 static void wc_nd_signal_report (wc_nd_report_t * r);
225 * @brief publish wildcard arp event
226 * @param sw_if_index The interface on which the ARP entires are acted
229 vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
232 .sw_if_index = sw_if_index,
235 memcpy (r.mac, mac, sizeof r.mac);
237 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
238 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
243 wc_nd_signal_report (wc_nd_report_t * r)
245 vlib_main_t *vm = vlib_get_main ();
246 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
247 uword ni = nm->wc_ip6_nd_publisher_node;
248 uword et = nm->wc_ip6_nd_publisher_et;
250 if (ni == (uword) ~ 0)
253 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
259 wc_nd_set_publisher_node (uword node_index, uword event_type)
261 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
262 nm->wc_ip6_nd_publisher_node = node_index;
263 nm->wc_ip6_nd_publisher_et = event_type;
267 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
269 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
270 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
271 vnet_main_t *vnm = vnet_get_main ();
272 vnet_sw_interface_t *si;
276 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags",
277 "Link layer", "Interface");
279 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
280 flags = format (flags, "D");
282 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
283 flags = format (flags, "S");
285 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
286 flags = format (flags, "N");
288 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
289 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
290 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
291 format_ip6_address, &n->key.ip6_address,
292 flags ? (char *) flags : "",
293 format_ethernet_address, n->link_layer_address,
294 format_vnet_sw_interface_name, vnm, si);
301 ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
303 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
307 .fp_proto = FIB_PROTOCOL_IP6,
308 .fp_addr.ip6 = n->key.ip6_address,
310 fib_table_entry_path_remove (fib_index,
315 n->key.sw_if_index, ~0,
316 1, FIB_ROUTE_PATH_FLAG_NONE);
320 static clib_error_t *
321 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
322 u32 sw_if_index, u32 flags)
324 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
327 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
329 u32 i, *to_delete = 0;
332 pool_foreach (n, nm->neighbor_pool,
334 if (n->key.sw_if_index == sw_if_index)
335 vec_add1 (to_delete, n - nm->neighbor_pool);
339 for (i = 0; i < vec_len (to_delete); i++)
341 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
342 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
343 ip6_neighbor_adj_fib_remove (n,
344 ip6_fib_table_get_index_for_sw_if_index
345 (n->key.sw_if_index));
346 pool_put (nm->neighbor_pool, n);
348 vec_free (to_delete);
354 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
357 unset_random_neighbor_entry (void)
359 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
360 vnet_main_t *vnm = vnet_get_main ();
361 vlib_main_t *vm = vnm->vlib_main;
365 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
366 nm->neighbor_delete_rotor = index;
368 /* Try again from elt 0, could happen if an intfc goes down */
371 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
372 nm->neighbor_delete_rotor = index;
375 /* Nothing left in the pool */
379 e = pool_elt_at_index (nm->neighbor_pool, index);
381 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
383 e->link_layer_address,
392 u8 link_layer_address[6];
395 } ip6_neighbor_set_unset_rpc_args_t;
397 static void ip6_neighbor_set_unset_rpc_callback
398 (ip6_neighbor_set_unset_rpc_args_t * a);
400 static void set_unset_ip6_neighbor_rpc
403 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
406 ip6_neighbor_set_unset_rpc_args_t args;
407 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
409 args.sw_if_index = sw_if_index;
410 args.is_add = is_add;
411 args.is_static = is_static;
412 args.is_no_fib_entry = is_no_fib_entry;
413 clib_memcpy (&args.addr, a, sizeof (*a));
414 if (NULL != link_layer_address)
415 clib_memcpy (args.link_layer_address, link_layer_address, 6);
417 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
418 (u8 *) & args, sizeof (args));
422 ip6_nbr_probe (ip_adjacency_t * adj)
424 icmp6_neighbor_solicitation_header_t *h;
425 vnet_main_t *vnm = vnet_get_main ();
426 ip6_main_t *im = &ip6_main;
427 ip_interface_address_t *ia;
428 ip6_address_t *dst, *src;
429 vnet_hw_interface_t *hi;
430 vnet_sw_interface_t *si;
436 vm = vlib_get_main ();
438 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
439 dst = &adj->sub_type.nbr.next_hop.ip6;
441 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
445 src = ip6_interface_address_matching_destination (im, dst,
453 h = vlib_packet_template_get_packet (vm,
454 &im->discover_neighbor_packet_template,
457 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
459 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
460 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
461 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
462 h->ip.src_address = src[0];
463 h->neighbor.target_address = dst[0];
465 clib_memcpy (h->link_layer_option.ethernet_address,
466 hi->hw_address, vec_len (hi->hw_address));
468 h->neighbor.icmp.checksum =
469 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
470 ASSERT (bogus_length == 0);
472 b = vlib_get_buffer (vm, bi);
473 vnet_buffer (b)->sw_if_index[VLIB_RX] =
474 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
476 /* Add encapsulation string for software interface (e.g. ethernet header). */
477 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
478 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
481 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
482 u32 *to_next = vlib_frame_vector_args (f);
485 vlib_put_frame_to_node (vm, hi->output_node_index, f);
490 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
492 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
493 ethernet_build_rewrite (vnet_get_main (),
494 nbr->key.sw_if_index,
495 adj_get_link_type (ai),
496 nbr->link_layer_address));
500 ip6_nd_mk_incomplete (adj_index_t ai)
502 ip_adjacency_t *adj = adj_get (ai);
504 adj_nbr_update_rewrite (ai,
505 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
506 ethernet_build_rewrite (vnet_get_main (),
509 adj_get_link_type (ai),
510 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
513 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
515 k.sw_if_index = sw_if_index; \
516 k.ip6_address = *addr; \
520 static ip6_neighbor_t *
521 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
523 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
524 ip6_neighbor_t *n = NULL;
525 ip6_neighbor_key_t k;
528 IP6_NBR_MK_KEY (k, sw_if_index, addr);
530 p = mhash_get (&nm->neighbor_index_by_key, &k);
533 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
540 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
542 ip6_neighbor_t *nbr = ctx;
544 ip6_nd_mk_complete (ai, nbr);
546 return (ADJ_WALK_RC_CONTINUE);
550 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
552 ip6_nd_mk_incomplete (ai);
554 return (ADJ_WALK_RC_CONTINUE);
558 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
565 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
567 switch (adj->lookup_next_index)
569 case IP_LOOKUP_NEXT_GLEAN:
570 adj_glean_update_rewrite (ai);
572 case IP_LOOKUP_NEXT_ARP:
575 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
576 ip6_nd_mk_complete_walk, nbr);
581 * no matching ND entry.
582 * construct the rewrite required to for an ND packet, and stick
583 * that in the adj's pipe to smoke.
585 adj_nbr_update_rewrite (ai,
586 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
587 ethernet_build_rewrite (vnm,
590 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
593 * since the FIB has added this adj for a route, it makes sense it may
594 * want to forward traffic sometime soon. Let's send a speculative ND.
595 * just one. If we were to do periodically that wouldn't be bad either,
596 * but that's more code than i'm prepared to write at this time for
597 * relatively little reward.
602 case IP_LOOKUP_NEXT_MCAST:
605 * Construct a partial rewrite from the known ethernet mcast dest MAC
610 rewrite = ethernet_build_rewrite (vnm,
613 ethernet_ip6_mcast_dst_addr ());
616 * Complete the remaining fields of the adj's rewrite to direct the
617 * complete of the rewrite at switch time by copying in the IP
618 * dst address's bytes.
619 * Ofset is 2 bytes into the desintation address. And we write 4 bytes.
621 offset = vec_len (rewrite) - 2;
622 adj_mcast_update_rewrite (ai, rewrite, offset, 0xffffffff);
626 case IP_LOOKUP_NEXT_DROP:
627 case IP_LOOKUP_NEXT_PUNT:
628 case IP_LOOKUP_NEXT_LOCAL:
629 case IP_LOOKUP_NEXT_REWRITE:
630 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
631 case IP_LOOKUP_NEXT_MIDCHAIN:
632 case IP_LOOKUP_NEXT_ICMP_ERROR:
633 case IP_LOOKUP_N_NEXT:
641 ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
645 .fp_proto = FIB_PROTOCOL_IP6,
646 .fp_addr.ip6 = n->key.ip6_address,
650 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
651 FIB_ENTRY_FLAG_ATTACHED,
652 DPO_PROTO_IP6, &pfx.fp_addr,
653 n->key.sw_if_index, ~0, 1, NULL,
654 FIB_ROUTE_PATH_FLAG_NONE);
658 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
661 u8 * link_layer_address,
662 uword n_bytes_link_layer_address,
663 int is_static, int is_no_fib_entry)
665 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
666 ip6_neighbor_key_t k;
667 ip6_neighbor_t *n = 0;
668 int make_new_nd_cache_entry = 1;
671 pending_resolution_t *pr, *mc;
673 if (vlib_get_thread_index ())
675 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
676 1 /* set new neighbor */ , is_static,
681 k.sw_if_index = sw_if_index;
682 k.ip6_address = a[0];
685 p = mhash_get (&nm->neighbor_index_by_key, &k);
688 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
689 /* Refuse to over-write static neighbor entry. */
690 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
692 make_new_nd_cache_entry = 0;
695 if (make_new_nd_cache_entry)
697 pool_get (nm->neighbor_pool, n);
698 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
701 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
703 clib_memcpy (n->link_layer_address,
704 link_layer_address, n_bytes_link_layer_address);
707 * create the adj-fib. the entry in the FIB table for and to the peer.
709 if (!is_no_fib_entry)
711 ip6_neighbor_adj_fib_add (n,
712 ip6_fib_table_get_index_for_sw_if_index
713 (n->key.sw_if_index));
717 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
723 * prevent a DoS attack from the data-plane that
724 * spams us with no-op updates to the MAC address
726 if (0 == memcmp (n->link_layer_address,
727 link_layer_address, n_bytes_link_layer_address))
728 goto check_customers;
730 clib_memcpy (n->link_layer_address,
731 link_layer_address, n_bytes_link_layer_address);
734 /* Update time stamp and flags. */
735 n->cpu_time_last_updated = clib_cpu_time_now ();
737 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
739 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
741 adj_nbr_walk_nh6 (sw_if_index,
742 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
745 /* Customer(s) waiting for this address to be resolved? */
746 p = mhash_get (&nm->pending_resolutions_by_address, a);
751 while (next_index != (u32) ~ 0)
753 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
754 vlib_process_signal_event (vm, pr->node_index,
755 pr->type_opaque, pr->data);
756 next_index = pr->next_index;
757 pool_put (nm->pending_resolutions, pr);
760 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
763 /* Customer(s) requesting ND event for this address? */
764 p = mhash_get (&nm->mac_changes_by_address, a);
769 while (next_index != (u32) ~ 0)
771 int (*fp) (u32, u8 *, u32, ip6_address_t *);
773 mc = pool_elt_at_index (nm->mac_changes, next_index);
774 fp = mc->data_callback;
776 /* Call the user's data callback, return 1 to suppress dup events */
779 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
781 * Signal the resolver process, as long as the user
782 * says they want to be notified
785 vlib_process_signal_event (vm, mc->node_index,
786 mc->type_opaque, mc->data);
787 next_index = mc->next_index;
795 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
798 u8 * link_layer_address,
799 uword n_bytes_link_layer_address)
801 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
802 ip6_neighbor_key_t k;
807 if (vlib_get_thread_index ())
809 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
810 0 /* unset */ , 0, 0);
814 k.sw_if_index = sw_if_index;
815 k.ip6_address = a[0];
818 p = mhash_get (&nm->neighbor_index_by_key, &k);
825 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
826 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
828 adj_nbr_walk_nh6 (sw_if_index,
829 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
832 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
836 .fp_proto = FIB_PROTOCOL_IP6,
837 .fp_addr.ip6 = n->key.ip6_address,
839 fib_table_entry_path_remove
840 (ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index),
844 &pfx.fp_addr, n->key.sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
846 pool_put (nm->neighbor_pool, n);
852 static void ip6_neighbor_set_unset_rpc_callback
853 (ip6_neighbor_set_unset_rpc_args_t * a)
855 vlib_main_t *vm = vlib_get_main ();
857 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
858 a->link_layer_address, 6, a->is_static,
861 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
862 a->link_layer_address, 6);
866 ip6_neighbor_sort (void *a1, void *a2)
868 vnet_main_t *vnm = vnet_get_main ();
869 ip6_neighbor_t *n1 = a1, *n2 = a2;
871 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
872 n2->key.sw_if_index);
874 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
879 ip6_neighbors_entries (u32 sw_if_index)
881 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
882 ip6_neighbor_t *n, *ns = 0;
885 pool_foreach (n, nm->neighbor_pool,
887 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
894 vec_sort_with_function (ns, ip6_neighbor_sort);
898 static clib_error_t *
899 show_ip6_neighbors (vlib_main_t * vm,
900 unformat_input_t * input, vlib_cli_command_t * cmd)
902 vnet_main_t *vnm = vnet_get_main ();
903 ip6_neighbor_t *n, *ns;
904 clib_error_t *error = 0;
907 /* Filter entries by interface if given. */
909 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
911 ns = ip6_neighbors_entries (sw_if_index);
914 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
917 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
926 * This command is used to display the adjacent IPv6 hosts found via
927 * neighbor discovery. Optionally, limit the output to the specified
931 * Example of how to display the IPv6 neighbor adjacency table:
932 * @cliexstart{show ip6 neighbors}
933 * Time Address Flags Link layer Interface
934 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
935 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
936 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
938 * Example of how to display the IPv6 neighbor adjacency table for given interface:
939 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
940 * Time Address Flags Link layer Interface
941 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
942 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
946 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
947 .path = "show ip6 neighbors",
948 .function = show_ip6_neighbors,
949 .short_help = "show ip6 neighbors [<interface>]",
953 static clib_error_t *
954 set_ip6_neighbor (vlib_main_t * vm,
955 unformat_input_t * input, vlib_cli_command_t * cmd)
957 vnet_main_t *vnm = vnet_get_main ();
963 int is_no_fib_entry = 0;
966 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
968 /* intfc, ip6-address, mac-address */
969 if (unformat (input, "%U %U %U",
970 unformat_vnet_sw_interface, vnm, &sw_if_index,
971 unformat_ip6_address, &addr,
972 unformat_ethernet_address, mac_address))
975 else if (unformat (input, "delete") || unformat (input, "del"))
977 else if (unformat (input, "static"))
979 else if (unformat (input, "no-fib-entry"))
986 return clib_error_return (0, "Missing interface, ip6 or hw address");
989 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
990 mac_address, sizeof (mac_address),
991 is_static, is_no_fib_entry);
993 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
994 mac_address, sizeof (mac_address));
999 * This command is used to manually add an entry to the IPv6 neighbor
1000 * adjacency table. Optionally, the entry can be added as static. It is
1001 * also used to remove an entry from the table. Use the '<em>show ip6
1002 * neighbors</em>' command to display all learned and manually entered entries.
1005 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1006 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1007 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1008 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1011 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1013 .path = "set ip6 neighbor",
1014 .function = set_ip6_neighbor,
1015 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
1021 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1022 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1023 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1024 } icmp6_neighbor_solicitation_or_advertisement_next_t;
1026 static_always_inline uword
1027 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1028 vlib_node_runtime_t * node,
1029 vlib_frame_t * frame,
1030 uword is_solicitation)
1032 vnet_main_t *vnm = vnet_get_main ();
1033 ip6_main_t *im = &ip6_main;
1034 uword n_packets = frame->n_vectors;
1035 u32 *from, *to_next;
1036 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1037 icmp6_neighbor_discovery_option_type_t option_type;
1038 vlib_node_runtime_t *error_node =
1039 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1042 from = vlib_frame_vector_args (frame);
1043 n_left_from = n_packets;
1044 next_index = node->cached_next_index;
1046 if (node->flags & VLIB_NODE_FLAG_TRACE)
1047 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1049 sizeof (icmp6_input_trace_t));
1053 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1054 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1055 n_advertisements_sent = 0;
1057 while (n_left_from > 0)
1059 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1061 while (n_left_from > 0 && n_left_to_next > 0)
1065 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1066 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1067 u32 bi0, options_len0, sw_if_index0, next0, error0;
1068 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1072 bi0 = to_next[0] = from[0];
1077 n_left_to_next -= 1;
1079 p0 = vlib_get_buffer (vm, bi0);
1080 ip0 = vlib_buffer_get_current (p0);
1081 h0 = ip6_next_header (ip0);
1083 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1085 error0 = ICMP6_ERROR_NONE;
1086 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1087 ip6_sadd_link_local =
1088 ip6_address_is_link_local_unicast (&ip0->src_address);
1089 ip6_sadd_unspecified =
1090 ip6_address_is_unspecified (&ip0->src_address);
1092 /* Check that source address is unspecified, link-local or else on-link. */
1093 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1095 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1097 if (ADJ_INDEX_INVALID != src_adj_index0)
1099 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1101 /* Allow all realistic-looking rewrite adjacencies to pass */
1102 ni0 = adj0->lookup_next_index;
1103 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1104 (ni0 < IP6_LOOKUP_N_NEXT);
1106 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1109 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1115 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1119 o0 = (void *) (h0 + 1);
1120 o0 = ((options_len0 == 8 && o0->header.type == option_type
1121 && o0->header.n_data_u64s == 1) ? o0 : 0);
1123 /* If src address unspecified or link local, donot learn neighbor MAC */
1124 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1125 !ip6_sadd_unspecified))
1127 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1128 if (nm->limit_neighbor_cache_size &&
1129 pool_elts (nm->neighbor_pool) >=
1130 nm->limit_neighbor_cache_size)
1131 unset_random_neighbor_entry ();
1132 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1135 &h0->target_address,
1136 o0->ethernet_address,
1137 sizeof (o0->ethernet_address),
1138 0, ip6_sadd_link_local);
1141 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1143 /* Check that target address is local to this router. */
1144 fib_node_index_t fei;
1148 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1150 if (~0 == fib_index)
1152 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1156 fei = ip6_fib_table_lookup_exact_match (fib_index,
1157 &h0->target_address,
1160 if (FIB_NODE_INDEX_INVALID == fei)
1162 /* The target address is not in the FIB */
1164 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1168 if (FIB_ENTRY_FLAG_LOCAL &
1169 fib_entry_get_flags_for_source (fei,
1170 FIB_SOURCE_INTERFACE))
1172 /* It's an address that belongs to one of our interfaces
1176 if (fib_entry_is_sourced
1177 (fei, FIB_SOURCE_IP6_ND_PROXY))
1179 /* The address was added by IPv6 Proxy ND config.
1180 * We should only respond to these if the NS arrived on
1181 * the link that has a matching covering prefix */
1186 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1192 if (is_solicitation)
1193 next0 = (error0 != ICMP6_ERROR_NONE
1194 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1195 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1199 error0 = error0 == ICMP6_ERROR_NONE ?
1200 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1203 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1205 vnet_sw_interface_t *sw_if0;
1206 ethernet_interface_t *eth_if0;
1207 ethernet_header_t *eth0;
1209 /* dst address is either source address or the all-nodes mcast addr */
1210 if (!ip6_sadd_unspecified)
1211 ip0->dst_address = ip0->src_address;
1213 ip6_set_reserved_multicast_address (&ip0->dst_address,
1214 IP6_MULTICAST_SCOPE_link_local,
1215 IP6_MULTICAST_GROUP_ID_all_hosts);
1217 ip0->src_address = h0->target_address;
1218 ip0->hop_limit = 255;
1219 h0->icmp.type = ICMP6_neighbor_advertisement;
1221 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1222 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1224 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1227 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1229 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1232 h0->advertisement_flags = clib_host_to_net_u32
1233 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1234 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1236 h0->icmp.checksum = 0;
1238 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1240 ASSERT (bogus_length == 0);
1242 /* Reuse current MAC header, copy SMAC to DMAC and
1243 * interface MAC to SMAC */
1244 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1245 eth0 = vlib_buffer_get_current (p0);
1246 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1248 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1250 /* Setup input and output sw_if_index for packet */
1251 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1252 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1253 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1254 vnet_main.local_interface_sw_if_index;
1256 n_advertisements_sent++;
1259 p0->error = error_node->errors[error0];
1261 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1262 to_next, n_left_to_next,
1266 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1269 /* Account for advertisements sent. */
1270 vlib_error_count (vm, error_node->node_index,
1271 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1272 n_advertisements_sent);
1274 return frame->n_vectors;
1277 /* for "syslogging" - use elog for now */
1278 #define foreach_log_level \
1279 _ (DEBUG, "DEBUG") \
1280 _ (INFO, "INFORMATION") \
1281 _ (NOTICE, "NOTICE") \
1282 _ (WARNING, "WARNING") \
1284 _ (CRIT, "CRITICAL") \
1285 _ (ALERT, "ALERT") \
1286 _ (EMERG, "EMERGENCY")
1290 #define _(f,s) LOG_##f,
1295 static char *log_level_strings[] = {
1301 static int logmask = 1 << LOG_DEBUG;
1304 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1306 /* just use elog for now */
1310 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1316 what = va_format (0, fmt, &va);
1318 ELOG_TYPE_DECLARE (e) =
1320 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1325 ed = ELOG_DATA (&vm->elog_main, e);
1326 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1327 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1333 /* ipv6 neighbor discovery - router advertisements */
1336 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1337 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1338 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1339 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1340 } icmp6_router_solicitation_or_advertisement_next_t;
1342 static_always_inline uword
1343 icmp6_router_solicitation (vlib_main_t * vm,
1344 vlib_node_runtime_t * node, vlib_frame_t * frame)
1346 vnet_main_t *vnm = vnet_get_main ();
1347 ip6_main_t *im = &ip6_main;
1348 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1349 uword n_packets = frame->n_vectors;
1350 u32 *from, *to_next;
1351 u32 n_left_from, n_left_to_next, next_index;
1352 u32 n_advertisements_sent = 0;
1355 icmp6_neighbor_discovery_option_type_t option_type;
1357 vlib_node_runtime_t *error_node =
1358 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1360 from = vlib_frame_vector_args (frame);
1361 n_left_from = n_packets;
1362 next_index = node->cached_next_index;
1364 if (node->flags & VLIB_NODE_FLAG_TRACE)
1365 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1367 sizeof (icmp6_input_trace_t));
1369 /* source may append his LL address */
1370 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1372 while (n_left_from > 0)
1374 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1376 while (n_left_from > 0 && n_left_to_next > 0)
1380 ip6_radv_t *radv_info = 0;
1382 icmp6_neighbor_discovery_header_t *h0;
1383 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1385 u32 bi0, options_len0, sw_if_index0, next0, error0;
1386 u32 is_solicitation = 1, is_dropped = 0;
1387 u32 is_unspecified, is_link_local;
1389 bi0 = to_next[0] = from[0];
1394 n_left_to_next -= 1;
1396 p0 = vlib_get_buffer (vm, bi0);
1397 ip0 = vlib_buffer_get_current (p0);
1398 h0 = ip6_next_header (ip0);
1400 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1401 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1403 ip6_address_is_link_local_unicast (&ip0->src_address);
1405 error0 = ICMP6_ERROR_NONE;
1406 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1408 /* check if solicitation (not from nd_timer node) */
1409 if (ip6_address_is_unspecified (&ip0->dst_address))
1410 is_solicitation = 0;
1412 /* Check that source address is unspecified, link-local or else on-link. */
1413 if (!is_unspecified && !is_link_local)
1415 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1417 if (ADJ_INDEX_INVALID != src_adj_index0)
1419 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1421 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1423 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1428 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1432 /* check for source LL option and process */
1433 o0 = (void *) (h0 + 1);
1434 o0 = ((options_len0 == 8
1435 && o0->header.type == option_type
1436 && o0->header.n_data_u64s == 1) ? o0 : 0);
1438 /* if src address unspecified IGNORE any options */
1439 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1440 !is_unspecified && !is_link_local))
1442 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1443 if (nm->limit_neighbor_cache_size &&
1444 pool_elts (nm->neighbor_pool) >=
1445 nm->limit_neighbor_cache_size)
1446 unset_random_neighbor_entry ();
1448 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1450 o0->ethernet_address,
1451 sizeof (o0->ethernet_address),
1455 /* default is to drop */
1456 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1458 if (error0 == ICMP6_ERROR_NONE)
1460 vnet_sw_interface_t *sw_if0;
1461 ethernet_interface_t *eth_if0;
1464 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1465 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1467 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1469 /* only support ethernet interface type for now */
1471 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1474 if (error0 == ICMP6_ERROR_NONE)
1478 /* adjust the sizeof the buffer to just include the ipv6 header */
1479 p0->current_length -=
1481 sizeof (icmp6_neighbor_discovery_header_t));
1483 /* look up the radv_t information for this interface */
1484 vec_validate_init_empty
1485 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1487 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1490 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1494 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1497 if (error0 == ICMP6_ERROR_NONE)
1499 f64 now = vlib_time_now (vm);
1501 /* for solicited adverts - need to rate limit */
1502 if (is_solicitation)
1504 if (0 != radv_info->last_radv_time &&
1505 (now - radv_info->last_radv_time) <
1506 MIN_DELAY_BETWEEN_RAS)
1509 radv_info->last_radv_time = now;
1513 icmp6_router_advertisement_header_t rh;
1515 rh.icmp.type = ICMP6_router_advertisement;
1517 rh.icmp.checksum = 0;
1519 rh.current_hop_limit = radv_info->curr_hop_limit;
1520 rh.router_lifetime_in_sec =
1521 clib_host_to_net_u16
1522 (radv_info->adv_router_lifetime_in_sec);
1524 time_in_msec_between_retransmitted_neighbor_solicitations
1526 clib_host_to_net_u32 (radv_info->
1527 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1528 rh.neighbor_reachable_time_in_msec =
1529 clib_host_to_net_u32 (radv_info->
1530 adv_neighbor_reachable_time_in_msec);
1533 (radv_info->adv_managed_flag) ?
1534 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1537 ((radv_info->adv_other_flag) ?
1538 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1542 u16 payload_length =
1543 sizeof (icmp6_router_advertisement_header_t);
1545 vlib_buffer_add_data (vm,
1546 vlib_buffer_get_free_list_index
1547 (p0), bi0, (void *) &rh,
1549 (icmp6_router_advertisement_header_t));
1551 if (radv_info->adv_link_layer_address)
1553 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1557 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1558 h.header.n_data_u64s = 1;
1560 /* copy ll address */
1561 clib_memcpy (&h.ethernet_address[0],
1562 eth_if0->address, 6);
1564 vlib_buffer_add_data (vm,
1565 vlib_buffer_get_free_list_index
1566 (p0), bi0, (void *) &h,
1568 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1572 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1575 /* add MTU option */
1576 if (radv_info->adv_link_mtu)
1578 icmp6_neighbor_discovery_mtu_option_t h;
1582 clib_host_to_net_u32 (radv_info->adv_link_mtu);
1583 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1584 h.header.n_data_u64s = 1;
1587 sizeof (icmp6_neighbor_discovery_mtu_option_t);
1589 vlib_buffer_add_data (vm,
1590 vlib_buffer_get_free_list_index
1591 (p0), bi0, (void *) &h,
1593 (icmp6_neighbor_discovery_mtu_option_t));
1596 /* add advertised prefix options */
1597 ip6_radv_prefix_t *pr_info;
1600 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1602 if(pr_info->enabled &&
1603 (!pr_info->decrement_lifetime_flag
1604 || (pr_info->pref_lifetime_expires >0)))
1606 /* advertise this prefix */
1607 icmp6_neighbor_discovery_prefix_information_option_t h;
1609 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1610 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1612 h.dst_address_length = pr_info->prefix_len;
1614 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1615 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1617 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1619 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1620 h.preferred_time = 0;
1624 if(pr_info->decrement_lifetime_flag)
1626 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1627 (pr_info->valid_lifetime_expires - now) : 0;
1629 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1630 (pr_info->pref_lifetime_expires - now) : 0;
1633 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1634 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1638 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1640 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1642 vlib_buffer_add_data (vm,
1643 vlib_buffer_get_free_list_index (p0),
1645 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1651 /* add additional options before here */
1653 /* finish building the router advertisement... */
1654 if (!is_unspecified && radv_info->send_unicast)
1656 ip0->dst_address = ip0->src_address;
1660 /* target address is all-nodes mcast addr */
1661 ip6_set_reserved_multicast_address
1663 IP6_MULTICAST_SCOPE_link_local,
1664 IP6_MULTICAST_GROUP_ID_all_hosts);
1667 /* source address MUST be the link-local address */
1668 ip0->src_address = radv_info->link_local_address;
1670 ip0->hop_limit = 255;
1671 ip0->payload_length =
1672 clib_host_to_net_u16 (payload_length);
1674 icmp6_router_advertisement_header_t *rh0 =
1675 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1676 rh0->icmp.checksum =
1677 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1679 ASSERT (bogus_length == 0);
1681 /* setup output if and adjacency */
1682 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1683 vnet_main.local_interface_sw_if_index;
1685 if (is_solicitation)
1687 ethernet_header_t *eth0;
1688 /* Reuse current MAC header, copy SMAC to DMAC and
1689 * interface MAC to SMAC */
1690 vlib_buffer_reset (p0);
1691 eth0 = vlib_buffer_get_current (p0);
1692 clib_memcpy (eth0->dst_address, eth0->src_address,
1694 clib_memcpy (eth0->src_address, eth_if0->address,
1697 is_dropped ? next0 :
1698 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1699 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1704 adj_index0 = radv_info->mcast_adj_index;
1705 if (adj_index0 == 0)
1706 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1710 is_dropped ? next0 :
1711 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1712 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1716 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1718 radv_info->n_solicitations_dropped += is_dropped;
1719 radv_info->n_solicitations_rcvd += is_solicitation;
1721 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1723 radv_info->n_advertisements_sent++;
1724 n_advertisements_sent++;
1730 p0->error = error_node->errors[error0];
1732 if (error0 != ICMP6_ERROR_NONE)
1733 vlib_error_count (vm, error_node->node_index, error0, 1);
1735 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1736 to_next, n_left_to_next,
1741 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1744 /* Account for router advertisements sent. */
1745 vlib_error_count (vm, error_node->node_index,
1746 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1747 n_advertisements_sent);
1749 return frame->n_vectors;
1752 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1753 static_always_inline uword
1754 icmp6_router_advertisement (vlib_main_t * vm,
1755 vlib_node_runtime_t * node, vlib_frame_t * frame)
1757 vnet_main_t *vnm = vnet_get_main ();
1758 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1759 uword n_packets = frame->n_vectors;
1760 u32 *from, *to_next;
1761 u32 n_left_from, n_left_to_next, next_index;
1762 u32 n_advertisements_rcvd = 0;
1764 vlib_node_runtime_t *error_node =
1765 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1767 from = vlib_frame_vector_args (frame);
1768 n_left_from = n_packets;
1769 next_index = node->cached_next_index;
1771 if (node->flags & VLIB_NODE_FLAG_TRACE)
1772 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1774 sizeof (icmp6_input_trace_t));
1776 while (n_left_from > 0)
1778 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1780 while (n_left_from > 0 && n_left_to_next > 0)
1784 ip6_radv_t *radv_info = 0;
1785 icmp6_router_advertisement_header_t *h0;
1786 u32 bi0, options_len0, sw_if_index0, next0, error0;
1788 bi0 = to_next[0] = from[0];
1793 n_left_to_next -= 1;
1795 p0 = vlib_get_buffer (vm, bi0);
1796 ip0 = vlib_buffer_get_current (p0);
1797 h0 = ip6_next_header (ip0);
1799 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1801 error0 = ICMP6_ERROR_NONE;
1802 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1804 /* Check that source address is link-local */
1805 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1806 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1808 /* default is to drop */
1809 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1811 n_advertisements_rcvd++;
1813 if (error0 == ICMP6_ERROR_NONE)
1815 vnet_sw_interface_t *sw_if0;
1816 ethernet_interface_t *eth_if0;
1818 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1819 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1821 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1823 /* only support ethernet interface type for now */
1825 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1828 if (error0 == ICMP6_ERROR_NONE)
1832 /* look up the radv_t information for this interface */
1833 vec_validate_init_empty
1834 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1836 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1839 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1843 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1846 if (error0 == ICMP6_ERROR_NONE)
1848 /* validate advertised information */
1849 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1850 && (h0->current_hop_limit !=
1851 radv_info->curr_hop_limit))
1853 ip6_neighbor_syslog (vm, LOG_WARNING,
1854 "our AdvCurHopLimit on %U doesn't agree with %U",
1855 format_vnet_sw_if_index_name,
1862 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1863 != radv_info->adv_managed_flag)
1865 ip6_neighbor_syslog (vm, LOG_WARNING,
1866 "our AdvManagedFlag on %U doesn't agree with %U",
1867 format_vnet_sw_if_index_name,
1874 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1875 != radv_info->adv_other_flag)
1877 ip6_neighbor_syslog (vm, LOG_WARNING,
1878 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1879 format_vnet_sw_if_index_name,
1886 time_in_msec_between_retransmitted_neighbor_solicitations
1888 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1890 time_in_msec_between_retransmitted_neighbor_solicitations
1892 clib_host_to_net_u32 (radv_info->
1893 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1895 ip6_neighbor_syslog (vm, LOG_WARNING,
1896 "our AdvRetransTimer on %U doesn't agree with %U",
1897 format_vnet_sw_if_index_name,
1903 if ((h0->neighbor_reachable_time_in_msec &&
1904 radv_info->adv_neighbor_reachable_time_in_msec) &&
1905 (h0->neighbor_reachable_time_in_msec !=
1906 clib_host_to_net_u32
1907 (radv_info->adv_neighbor_reachable_time_in_msec)))
1909 ip6_neighbor_syslog (vm, LOG_WARNING,
1910 "our AdvReachableTime on %U doesn't agree with %U",
1911 format_vnet_sw_if_index_name,
1917 /* check for MTU or prefix options or .. */
1918 u8 *opt_hdr = (u8 *) (h0 + 1);
1919 while (options_len0 > 0)
1921 icmp6_neighbor_discovery_option_header_t *o0 =
1922 (icmp6_neighbor_discovery_option_header_t *)
1924 int opt_len = o0->n_data_u64s << 3;
1925 icmp6_neighbor_discovery_option_type_t option_type =
1928 if (options_len0 < 2)
1930 ip6_neighbor_syslog (vm, LOG_ERR,
1931 "malformed RA packet on %U from %U",
1932 format_vnet_sw_if_index_name,
1941 ip6_neighbor_syslog (vm, LOG_ERR,
1942 " zero length option in RA on %U from %U",
1943 format_vnet_sw_if_index_name,
1949 else if (opt_len > options_len0)
1951 ip6_neighbor_syslog (vm, LOG_ERR,
1952 "option length in RA packet greater than total length on %U from %U",
1953 format_vnet_sw_if_index_name,
1960 options_len0 -= opt_len;
1963 switch (option_type)
1965 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1967 icmp6_neighbor_discovery_mtu_option_t *h =
1968 (icmp6_neighbor_discovery_mtu_option_t
1971 if (opt_len < sizeof (*h))
1974 if ((h->mtu && radv_info->adv_link_mtu) &&
1976 clib_host_to_net_u32
1977 (radv_info->adv_link_mtu)))
1979 ip6_neighbor_syslog (vm, LOG_WARNING,
1980 "our AdvLinkMTU on %U doesn't agree with %U",
1981 format_vnet_sw_if_index_name,
1989 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1991 icmp6_neighbor_discovery_prefix_information_option_t
1993 (icmp6_neighbor_discovery_prefix_information_option_t
1996 /* validate advertised prefix options */
1997 ip6_radv_prefix_t *pr_info;
1998 u32 preferred, valid;
2000 if (opt_len < sizeof (*h))
2004 clib_net_to_host_u32 (h->preferred_time);
2005 valid = clib_net_to_host_u32 (h->valid_time);
2007 /* look for matching prefix - if we our advertising it, it better be consistant */
2009 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2013 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2015 if(pr_info->enabled &&
2016 (pr_info->prefix_len == h->dst_address_length) &&
2017 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2020 if(!pr_info->decrement_lifetime_flag &&
2021 valid != pr_info->adv_valid_lifetime_in_secs)
2023 ip6_neighbor_syslog(vm, LOG_WARNING,
2024 "our ADV validlifetime on %U for %U does not agree with %U",
2025 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2026 format_ip6_address, &h->dst_address);
2028 if(!pr_info->decrement_lifetime_flag &&
2029 preferred != pr_info->adv_pref_lifetime_in_secs)
2031 ip6_neighbor_syslog(vm, LOG_WARNING,
2032 "our ADV preferredlifetime on %U for %U does not agree with %U",
2033 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2034 format_ip6_address, &h->dst_address);
2051 p0->error = error_node->errors[error0];
2053 if (error0 != ICMP6_ERROR_NONE)
2054 vlib_error_count (vm, error_node->node_index, error0, 1);
2056 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2057 to_next, n_left_to_next,
2061 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2064 /* Account for router advertisements sent. */
2065 vlib_error_count (vm, error_node->node_index,
2066 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2067 n_advertisements_rcvd);
2069 return frame->n_vectors;
2073 * @brief Add a multicast Address to the advertised MLD set
2076 ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2078 ip6_mldp_group_t *mcast_group_info;
2081 /* lookup mldp info for this interface */
2082 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2084 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2087 if (!mcast_group_info)
2091 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2093 mi = mcast_group_info - radv_info->mldp_group_pool;
2094 mhash_set (&radv_info->address_to_mldp_index, &addr, mi, /* old_value */
2097 mcast_group_info->type = 4;
2098 mcast_group_info->mcast_source_address_pool = 0;
2099 mcast_group_info->num_sources = 0;
2100 clib_memcpy (&mcast_group_info->mcast_address, &addr,
2101 sizeof (ip6_address_t));
2106 * @brief Delete a multicast Address from the advertised MLD set
2109 ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2111 ip6_mldp_group_t *mcast_group_info;
2114 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2116 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2118 if (mcast_group_info)
2120 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2122 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2127 * @brief Add a multicast Address to the advertised MLD set
2130 ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2131 ip6_multicast_address_scope_t scope,
2132 ip6_multicast_link_local_group_id_t group)
2136 ip6_set_reserved_multicast_address (&addr, scope, group);
2138 ip6_neighbor_add_mld_prefix (a, &addr);
2142 * @brief create and initialize router advertisement parameters with default
2143 * values for this intfc
2146 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
2147 u32 sw_if_index, u32 is_add)
2149 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2152 vnet_sw_interface_t *sw_if0;
2153 ethernet_interface_t *eth_if0 = 0;
2155 /* lookup radv container - ethernet interfaces only */
2156 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2157 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2158 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2163 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2165 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2169 a = pool_elt_at_index (nm->if_radv_pool, ri);
2173 ip6_radv_prefix_t *p;
2174 ip6_mldp_group_t *m;
2176 /* release the lock on the interface's mcast adj */
2177 adj_unlock (a->mcast_adj_index);
2179 /* clean up prefix and MDP pools */
2181 pool_flush(p, a->adv_prefixes_pool,
2183 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
2185 pool_flush (m, a->mldp_group_pool,
2187 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
2191 pool_free (a->mldp_group_pool);
2192 pool_free (a->adv_prefixes_pool);
2194 mhash_free (&a->address_to_prefix_index);
2195 mhash_free (&a->address_to_mldp_index);
2197 pool_put (nm->if_radv_pool, a);
2198 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2206 vnet_hw_interface_t *hw_if0;
2208 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2210 pool_get (nm->if_radv_pool, a);
2212 ri = a - nm->if_radv_pool;
2213 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2215 /* initialize default values (most of which are zero) */
2216 memset (a, 0, sizeof (a[0]));
2218 a->sw_if_index = sw_if_index;
2219 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2220 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2221 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2222 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2224 /* send ll address source address option */
2225 a->adv_link_layer_address = 1;
2227 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2228 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2229 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2230 a->seed = (u32) clib_cpu_time_now ();
2231 (void) random_u32 (&a->seed);
2232 a->randomizer = clib_cpu_time_now ();
2233 (void) random_u64 (&a->randomizer);
2235 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2236 a->initial_adverts_sent = a->initial_adverts_count - 1;
2237 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2239 /* deafult is to send */
2242 /* fill in radv_info for this interface that will be needed later */
2243 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2245 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2247 /* fill in default link-local address (this may be overridden) */
2248 ip6_link_local_address_from_ethernet_address
2249 (&a->link_local_address, eth_if0->address);
2251 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2252 sizeof (ip6_address_t));
2253 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2254 sizeof (ip6_address_t));
2256 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2260 /* add multicast groups we will always be reporting */
2261 ip6_neighbor_add_mld_grp (a,
2262 IP6_MULTICAST_SCOPE_link_local,
2263 IP6_MULTICAST_GROUP_ID_all_hosts);
2264 ip6_neighbor_add_mld_grp (a,
2265 IP6_MULTICAST_SCOPE_link_local,
2266 IP6_MULTICAST_GROUP_ID_all_routers);
2267 ip6_neighbor_add_mld_grp (a,
2268 IP6_MULTICAST_SCOPE_link_local,
2269 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2275 /* send an mldpv2 report */
2277 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2279 vnet_main_t *vnm = vnet_get_main ();
2280 vlib_main_t *vm = vnm->vlib_main;
2281 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2282 vnet_sw_interface_t *sw_if0;
2283 ethernet_interface_t *eth_if0;
2287 ip6_radv_t *radv_info;
2297 icmp6_multicast_listener_report_header_t *rh0;
2298 icmp6_multicast_listener_report_packet_t *rp0;
2300 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2301 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2302 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2304 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2307 /* look up the radv_t information for this interface */
2308 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2311 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2316 /* send report now - build a mldpv2 report packet */
2317 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2320 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2321 if (PREDICT_FALSE (n_allocated == 0))
2323 clib_warning ("buffer allocation failure");
2327 b0 = vlib_get_buffer (vm, bo0);
2329 /* adjust the sizeof the buffer to just include the ipv6 header */
2330 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2332 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2334 b0->error = ICMP6_ERROR_NONE;
2336 rp0 = vlib_buffer_get_current (b0);
2337 ip0 = (ip6_header_t *) & rp0->ip;
2338 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2340 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2342 ip0->ip_version_traffic_class_and_flow_label =
2343 clib_host_to_net_u32 (0x6 << 28);
2345 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2346 /* for DEBUG - vnet driver won't seem to emit router alerts */
2347 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2350 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2352 /* source address MUST be the link-local address */
2353 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2354 ip0->src_address = radv_info->link_local_address;
2356 /* destination is all mldpv2 routers */
2357 ip6_set_reserved_multicast_address (&ip0->dst_address,
2358 IP6_MULTICAST_SCOPE_link_local,
2359 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2361 /* add reports here */
2362 ip6_mldp_group_t *m;
2363 int num_addr_records = 0;
2364 icmp6_multicast_address_record_t rr;
2366 /* fill in the hop-by-hop extension header (router alert) info */
2367 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2368 rh0->ext_hdr.n_data_u64s = 0;
2370 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2372 rh0->alert.value = 0;
2377 rh0->icmp.checksum = 0;
2380 pool_foreach (m, radv_info->mldp_group_pool,
2383 rr.aux_data_len_u32s = 0;
2384 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2385 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2389 vlib_buffer_add_data
2390 (vm, vlib_buffer_get_free_list_index (b0), bo0,
2391 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
2393 payload_length += sizeof( icmp6_multicast_address_record_t);
2398 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2400 /* update lengths */
2401 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2403 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2405 ASSERT (bogus_length == 0);
2408 * OK to override w/ no regard for actual FIB, because
2409 * ip6-rewrite only looks at the adjacency.
2411 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2412 vnet_main.local_interface_sw_if_index;
2414 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2415 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2417 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2419 f = vlib_get_frame_to_node (vm, node->index);
2420 to_next = vlib_frame_vector_args (f);
2424 vlib_put_frame_to_node (vm, node->index, f);
2429 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2431 .function = icmp6_router_solicitation,
2432 .name = "icmp6-router-solicitation",
2434 .vector_size = sizeof (u32),
2436 .format_trace = format_icmp6_input_trace,
2438 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2440 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
2441 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2442 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2447 /* send a RA or update the timer info etc.. */
2449 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2450 vlib_node_runtime_t * node,
2451 vlib_frame_t * frame)
2453 vnet_main_t *vnm = vnet_get_main ();
2454 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2455 ip6_radv_t *radv_info;
2456 vlib_frame_t *f = 0;
2457 u32 n_this_frame = 0;
2458 u32 n_left_to_next = 0;
2461 icmp6_router_solicitation_header_t *h0;
2463 f64 now = vlib_time_now (vm);
2465 /* Interface ip6 radv info list */
2467 pool_foreach (radv_info, nm->if_radv_pool,
2469 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2471 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2472 radv_info->next_multicast_time = now;
2473 radv_info->last_multicast_time = now;
2474 radv_info->last_radv_time = 0;
2475 radv_info->all_routers_mcast = 0;
2479 /* Make sure that we've joined the all-routers multicast group */
2480 if(!radv_info->all_routers_mcast)
2482 /* send MDLP_REPORT_EVENT message */
2483 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2484 radv_info->all_routers_mcast = 1;
2487 /* is it time to send a multicast RA on this interface? */
2488 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2493 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2494 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2496 /* multicast send - compute next multicast send time */
2497 if( radv_info->initial_adverts_sent > 0)
2499 radv_info->initial_adverts_sent--;
2500 if(rfn > radv_info-> initial_adverts_interval)
2501 rfn = radv_info-> initial_adverts_interval;
2503 /* check to see if we are ceasing to send */
2504 if( radv_info->initial_adverts_sent == 0)
2505 if(radv_info->cease_radv)
2506 radv_info->send_radv = 0;
2509 radv_info->next_multicast_time = rfn + now;
2510 radv_info->last_multicast_time = now;
2512 /* send advert now - build a "solicted" router advert with unspecified source address */
2513 n_allocated = vlib_buffer_alloc_from_free_list
2514 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2516 if (PREDICT_FALSE(n_allocated == 0))
2518 clib_warning ("buffer allocation failure");
2521 b0 = vlib_get_buffer (vm, bo0);
2522 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2523 b0->error = ICMP6_ERROR_NONE;
2524 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2526 h0 = vlib_buffer_get_current (b0);
2528 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2530 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2531 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2532 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2533 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2534 h0->ip.hop_limit = 255;
2536 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2537 h0->ip.src_address.as_u64[0] = 0;
2538 h0->ip.src_address.as_u64[1] = 0;
2540 h0->ip.dst_address.as_u64[0] = 0;
2541 h0->ip.dst_address.as_u64[1] = 0;
2543 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2545 if (PREDICT_FALSE(f == 0))
2547 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2548 to_next = vlib_frame_vector_args (f);
2549 n_left_to_next = VLIB_FRAME_SIZE;
2558 if (PREDICT_FALSE(n_left_to_next == 0))
2560 f->n_vectors = n_this_frame;
2561 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2570 ASSERT (n_this_frame);
2571 f->n_vectors = n_this_frame;
2572 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2578 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2579 vlib_node_runtime_t * node,
2580 vlib_frame_t * frame)
2583 ip6_icmp_neighbor_discovery_event_data_t *event_data;
2585 /* init code here */
2589 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
2591 event_data = vlib_process_get_event_data (vm, &event_type);
2595 /* No events found: timer expired. */
2596 /* process interface list and send RAs as appropriate, update timer info */
2597 ip6_neighbor_process_timer_event (vm, node, frame);
2604 case ICMP6_ND_EVENT_INIT:
2615 _vec_len (event_data) = 0;
2618 return frame->n_vectors;
2622 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
2624 .function = icmp6_router_advertisement,
2625 .name = "icmp6-router-advertisement",
2627 .vector_size = sizeof (u32),
2629 .format_trace = format_icmp6_input_trace,
2638 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2640 .function = ip6_icmp_neighbor_discovery_event_process,
2641 .name = "ip6-icmp-neighbor-discovery-event-process",
2642 .type = VLIB_NODE_TYPE_PROCESS,
2646 icmp6_neighbor_solicitation (vlib_main_t * vm,
2647 vlib_node_runtime_t * node, vlib_frame_t * frame)
2649 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2650 /* is_solicitation */
2655 icmp6_neighbor_advertisement (vlib_main_t * vm,
2656 vlib_node_runtime_t * node,
2657 vlib_frame_t * frame)
2659 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2660 /* is_solicitation */
2665 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
2667 .function = icmp6_neighbor_solicitation,
2668 .name = "icmp6-neighbor-solicitation",
2670 .vector_size = sizeof (u32),
2672 .format_trace = format_icmp6_input_trace,
2674 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2676 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
2677 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2683 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
2685 .function = icmp6_neighbor_advertisement,
2686 .name = "icmp6-neighbor-advertisement",
2688 .vector_size = sizeof (u32),
2690 .format_trace = format_icmp6_input_trace,
2699 /* API support functions */
2701 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
2702 u8 suppress, u8 managed, u8 other,
2703 u8 ll_option, u8 send_unicast, u8 cease,
2704 u8 use_lifetime, u32 lifetime,
2705 u32 initial_count, u32 initial_interval,
2706 u32 max_interval, u32 min_interval, u8 is_no)
2708 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2712 /* look up the radv_t information for this interface */
2713 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2715 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2716 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2721 ip6_radv_t *radv_info;
2722 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2724 if ((max_interval != 0) && (min_interval == 0))
2725 min_interval = .75 * max_interval;
2729 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
2730 radv_info->max_radv_interval;
2733 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
2734 radv_info->min_radv_interval;
2737 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
2738 radv_info->adv_router_lifetime_in_sec;
2742 if (lifetime > MAX_DEF_RTR_LIFETIME)
2743 lifetime = MAX_DEF_RTR_LIFETIME;
2745 if (lifetime <= max_interval)
2746 return VNET_API_ERROR_INVALID_VALUE;
2749 if (min_interval != 0)
2751 if ((min_interval > .75 * max_interval) || (min_interval < 3))
2752 return VNET_API_ERROR_INVALID_VALUE;
2755 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2756 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2757 return VNET_API_ERROR_INVALID_VALUE;
2760 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2761 if "flag" is clear don't change corresponding value
2763 radv_info->send_radv =
2764 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
2765 radv_info->adv_managed_flag =
2766 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2767 radv_info->adv_other_flag =
2768 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
2769 radv_info->adv_link_layer_address =
2771 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2772 radv_info->send_unicast =
2773 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
2774 radv_info->cease_radv =
2775 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
2777 radv_info->min_radv_interval = min_interval;
2778 radv_info->max_radv_interval = max_interval;
2779 radv_info->adv_router_lifetime_in_sec = lifetime;
2781 radv_info->initial_adverts_count =
2783 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
2784 radv_info->initial_adverts_count;
2785 radv_info->initial_adverts_interval =
2786 (initial_interval !=
2787 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
2788 radv_info->initial_adverts_interval;
2791 if ((cease != 0) && (is_no))
2792 radv_info->send_radv = 1;
2794 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2795 radv_info->next_multicast_time = vlib_time_now (vm);
2796 radv_info->last_multicast_time = vlib_time_now (vm);
2797 radv_info->last_radv_time = 0;
2803 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
2804 ip6_address_t * prefix_addr, u8 prefix_len,
2805 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2806 u8 no_advertise, u8 off_link, u8 no_autoconfig,
2807 u8 no_onlink, u8 is_no)
2809 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2814 /* look up the radv_t information for this interface */
2815 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2818 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2820 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2824 f64 now = vlib_time_now (vm);
2825 ip6_radv_t *radv_info;
2826 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2828 /* prefix info add, delete or update */
2829 ip6_radv_prefix_t *prefix;
2831 /* lookup prefix info for this address on this interface */
2832 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2834 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2840 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2842 if (prefix->prefix_len != prefix_len)
2843 return VNET_API_ERROR_INVALID_VALUE_2;
2845 /* FIXME - Should the DP do this or the CP ? */
2846 /* do specific delete processing here before returning */
2847 /* try to remove from routing table */
2849 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
2851 pool_put (radv_info->adv_prefixes_pool, prefix);
2853 radv_info->initial_adverts_sent =
2854 radv_info->initial_adverts_count - 1;
2855 radv_info->next_multicast_time = vlib_time_now (vm);
2856 radv_info->last_multicast_time = vlib_time_now (vm);
2857 radv_info->last_radv_time = 0;
2861 /* adding or changing */
2866 pool_get (radv_info->adv_prefixes_pool, prefix);
2867 pi = prefix - radv_info->adv_prefixes_pool;
2868 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
2871 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
2873 prefix->prefix_len = prefix_len;
2874 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
2876 /* initialize default values */
2877 prefix->adv_on_link_flag = 1; /* L bit set */
2878 prefix->adv_autonomous_flag = 1; /* A bit set */
2879 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2880 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2881 prefix->enabled = 1;
2882 prefix->decrement_lifetime_flag = 1;
2883 prefix->deprecated_prefix_flag = 1;
2887 /* FIXME - Should the DP do this or the CP ? */
2888 /* insert prefix into routing table as a connected prefix */
2897 if (prefix->prefix_len != prefix_len)
2898 return VNET_API_ERROR_INVALID_VALUE_2;
2902 /* FIXME - Should the DP do this or the CP ? */
2903 /* remove from routing table if already there */
2907 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
2909 prefix->adv_valid_lifetime_in_secs = ~0;
2910 prefix->adv_pref_lifetime_in_secs = ~0;
2911 prefix->decrement_lifetime_flag = 0;
2915 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2916 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2919 /* copy remaining */
2920 prefix->enabled = !(no_advertise != 0);
2921 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2922 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2926 /* fill in the expiration times */
2927 prefix->valid_lifetime_expires =
2928 now + prefix->adv_valid_lifetime_in_secs;
2929 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2931 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2932 radv_info->next_multicast_time = vlib_time_now (vm);
2933 radv_info->last_multicast_time = vlib_time_now (vm);
2934 radv_info->last_radv_time = 0;
2940 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
2941 vlib_cli_command_t * cmd)
2943 vnet_main_t *vnm = vnet_get_main ();
2944 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2945 clib_error_t *error = 0;
2947 u8 suppress = 0, managed = 0, other = 0;
2948 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
2949 u8 use_lifetime = 0;
2950 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
2951 0, ra_initial_interval = 0;
2952 u32 ra_max_interval = 0, ra_min_interval = 0;
2954 unformat_input_t _line_input, *line_input = &_line_input;
2955 vnet_sw_interface_t *sw_if0;
2957 int add_radv_info = 1;
2958 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
2959 ip6_address_t ip6_addr;
2963 /* Get a line of input. */
2964 if (!unformat_user (main_input, unformat_line_input, line_input))
2967 /* get basic radv info for this interface */
2968 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2971 if (unformat_user (line_input,
2972 unformat_vnet_sw_interface, vnm, &sw_if_index))
2975 ethernet_interface_t *eth_if0 = 0;
2977 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2978 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2980 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2985 clib_error_return (0, "Interface must be of ethernet type");
2989 /* look up the radv_t information for this interface */
2990 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
2993 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2997 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3001 error = clib_error_return (0, "unknown interface %U'",
3002 format_unformat_error, line_input);
3008 error = clib_error_return (0, "invalid interface name %U'",
3009 format_unformat_error, line_input);
3014 /* get the rest of the command */
3015 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3017 if (unformat (line_input, "no"))
3019 else if (unformat (line_input, "prefix %U/%d",
3020 unformat_ip6_address, &ip6_addr, &addr_len))
3025 else if (unformat (line_input, "ra-managed-config-flag"))
3030 else if (unformat (line_input, "ra-other-config-flag"))
3035 else if (unformat (line_input, "ra-suppress") ||
3036 unformat (line_input, "ra-surpress"))
3041 else if (unformat (line_input, "ra-suppress-link-layer") ||
3042 unformat (line_input, "ra-surpress-link-layer"))
3044 suppress_ll_option = 1;
3047 else if (unformat (line_input, "ra-send-unicast"))
3052 else if (unformat (line_input, "ra-lifetime"))
3054 if (!unformat (line_input, "%d", &ra_lifetime))
3056 error = unformat_parse_error (line_input);
3062 else if (unformat (line_input, "ra-initial"))
3065 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
3067 error = unformat_parse_error (line_input);
3072 else if (unformat (line_input, "ra-interval"))
3074 if (!unformat (line_input, "%d", &ra_max_interval))
3076 error = unformat_parse_error (line_input);
3080 if (!unformat (line_input, "%d", &ra_min_interval))
3081 ra_min_interval = 0;
3084 else if (unformat (line_input, "ra-cease"))
3091 error = unformat_parse_error (line_input);
3098 ip6_neighbor_ra_config (vm, sw_if_index,
3099 suppress, managed, other,
3100 suppress_ll_option, send_unicast, cease,
3101 use_lifetime, ra_lifetime,
3102 ra_initial_count, ra_initial_interval,
3103 ra_max_interval, ra_min_interval, is_no);
3107 u32 valid_lifetime_in_secs = 0;
3108 u32 pref_lifetime_in_secs = 0;
3109 u8 use_prefix_default_values = 0;
3110 u8 no_advertise = 0;
3112 u8 no_autoconfig = 0;
3115 /* get the rest of the command */
3116 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3118 if (unformat (line_input, "default"))
3120 use_prefix_default_values = 1;
3123 else if (unformat (line_input, "infinite"))
3125 valid_lifetime_in_secs = ~0;
3126 pref_lifetime_in_secs = ~0;
3129 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3130 &pref_lifetime_in_secs))
3137 /* get the rest of the command */
3138 while (!use_prefix_default_values &&
3139 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3141 if (unformat (line_input, "no-advertise"))
3143 else if (unformat (line_input, "off-link"))
3145 else if (unformat (line_input, "no-autoconfig"))
3147 else if (unformat (line_input, "no-onlink"))
3151 error = unformat_parse_error (line_input);
3156 ip6_neighbor_ra_prefix (vm, sw_if_index,
3157 &ip6_addr, addr_len,
3158 use_prefix_default_values,
3159 valid_lifetime_in_secs,
3160 pref_lifetime_in_secs,
3162 off_link, no_autoconfig, no_onlink, is_no);
3166 unformat_free (line_input);
3172 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3174 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3177 for (i = 0; i < vec_len (addrs); i++)
3179 ip_interface_address_t *a =
3180 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3181 ip6_address_t *address = ip_interface_address_get_address (lm, a);
3183 vlib_cli_output (vm, "\t\t%U/%d",
3184 format_ip6_address, address, a->address_length);
3188 static clib_error_t *
3189 show_ip6_interface_cmd (vlib_main_t * vm,
3190 unformat_input_t * input, vlib_cli_command_t * cmd)
3192 vnet_main_t *vnm = vnet_get_main ();
3193 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3194 clib_error_t *error = 0;
3199 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3203 /* look up the radv_t information for this interface */
3204 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3207 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3211 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3212 ip6_radv_t *radv_info;
3213 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3215 vlib_cli_output (vm, "%U is admin %s\n",
3216 format_vnet_sw_interface_name, vnm,
3217 vnet_get_sw_interface (vnm, sw_if_index),
3218 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3222 u32 *link_scope = 0, *global_scope = 0;
3223 u32 *local_scope = 0, *unknown_scope = 0;
3224 ip_interface_address_t *a;
3226 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3228 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3230 while (ai != (u32) ~ 0)
3232 a = pool_elt_at_index (lm->if_address_pool, ai);
3233 ip6_address_t *address =
3234 ip_interface_address_get_address (lm, a);
3236 if (ip6_address_is_link_local_unicast (address))
3237 vec_add1 (link_scope, ai);
3238 else if (ip6_address_is_global_unicast (address))
3239 vec_add1 (global_scope, ai);
3240 else if (ip6_address_is_local_unicast (address))
3241 vec_add1 (local_scope, ai);
3243 vec_add1 (unknown_scope, ai);
3245 ai = a->next_this_sw_interface;
3248 if (vec_len (link_scope))
3250 vlib_cli_output (vm, "\tLink-local address(es):\n");
3251 ip6_print_addrs (vm, link_scope);
3252 vec_free (link_scope);
3255 if (vec_len (local_scope))
3257 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3258 ip6_print_addrs (vm, local_scope);
3259 vec_free (local_scope);
3262 if (vec_len (global_scope))
3264 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3265 ip6_print_addrs (vm, global_scope);
3266 vec_free (global_scope);
3269 if (vec_len (unknown_scope))
3271 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3272 ip6_print_addrs (vm, unknown_scope);
3273 vec_free (unknown_scope);
3276 vlib_cli_output (vm, "\tJoined group address(es):\n");
3277 ip6_mldp_group_t *m;
3279 pool_foreach (m, radv_info->mldp_group_pool,
3281 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3286 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3287 ip6_radv_prefix_t *p;
3289 pool_foreach (p, radv_info->adv_prefixes_pool,
3291 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3292 format_ip6_address, &p->prefix, p->prefix_len);
3296 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
3297 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3298 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3299 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3300 vlib_cli_output (vm, "\tND DAD is disabled\n");
3301 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3302 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3303 radv_info->adv_neighbor_reachable_time_in_msec);
3304 vlib_cli_output (vm,
3305 "\tND advertised retransmit interval is %d (msec)\n",
3307 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
3309 u32 ra_interval = radv_info->max_radv_interval;
3310 u32 ra_interval_min = radv_info->min_radv_interval;
3311 vlib_cli_output (vm,
3312 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
3313 ra_interval, ra_interval_min);
3314 vlib_cli_output (vm,
3315 "\tND router advertisements live for %d seconds\n",
3316 radv_info->adv_router_lifetime_in_sec);
3317 vlib_cli_output (vm,
3318 "\tHosts %s stateless autoconfig for addresses\n",
3319 (radv_info->adv_managed_flag) ? "use" :
3321 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3322 radv_info->n_advertisements_sent);
3323 vlib_cli_output (vm, "\tND router solicitations received %d\n",
3324 radv_info->n_solicitations_rcvd);
3325 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3326 radv_info->n_solicitations_dropped);
3330 error = clib_error_return (0, "IPv6 not enabled on interface",
3331 format_unformat_error, input);
3339 * This command is used to display various IPv6 attributes on a given
3343 * Example of how to display IPv6 settings:
3344 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3345 * GigabitEthernet2/0/0 is admin up
3346 * Link-local address(es):
3348 * Joined group address(es):
3353 * Advertised Prefixes:
3354 * prefix fe80::fe:28ff:fe9c:75b3, length 64
3356 * ICMP error messages are unlimited
3357 * ICMP redirects are disabled
3358 * ICMP unreachables are not sent
3359 * ND DAD is disabled
3360 * ND advertised reachable time is 0
3361 * ND advertised retransmit interval is 0 (msec)
3362 * ND router advertisements are sent every 200 seconds (min interval is 150)
3363 * ND router advertisements live for 600 seconds
3364 * Hosts use stateless autoconfig for addresses
3365 * ND router advertisements sent 19336
3366 * ND router solicitations received 0
3367 * ND router solicitations dropped 0
3369 * Example of output if IPv6 is not enabled on the interface:
3370 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3371 * show ip6 interface: IPv6 not enabled on interface
3375 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
3377 .path = "show ip6 interface",
3378 .function = show_ip6_interface_cmd,
3379 .short_help = "show ip6 interface <interface>",
3384 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
3386 clib_error_t *error = 0;
3387 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3390 /* look up the radv_t information for this interface */
3391 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3393 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3395 /* if not created - do nothing */
3398 vnet_main_t *vnm = vnet_get_main ();
3399 ip6_radv_t *radv_info;
3401 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3403 /* check radv_info ref count for other ip6 addresses on this interface */
3404 /* This implicitly excludes the link local address */
3405 if (radv_info->ref_count == 0)
3407 /* essentially "disables" ipv6 on this interface */
3408 error = ip6_add_del_interface_address (vm, sw_if_index,
3410 link_local_address, 128,
3413 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3415 ip6_mfib_interface_enable_disable (sw_if_index, 0);
3422 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
3424 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3427 /* look up the radv_t information for this interface */
3428 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3431 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3437 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
3439 clib_error_t *error = 0;
3440 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3444 /* look up the radv_t information for this interface */
3445 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3448 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3450 /* if not created yet */
3453 vnet_main_t *vnm = vnet_get_main ();
3454 vnet_sw_interface_t *sw_if0;
3456 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3457 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3459 ethernet_interface_t *eth_if0;
3462 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
3465 /* create radv_info. for this interface. This holds all the info needed for router adverts */
3467 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
3471 ip6_radv_t *radv_info;
3472 ip6_address_t link_local_address;
3474 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3476 ip6_link_local_address_from_ethernet_mac_address
3477 (&link_local_address, eth_if0->address);
3479 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
3480 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
3481 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
3483 /* make up an interface id */
3487 link_local_address.as_u64[0] = radv_info->randomizer;
3490 md5_add (&m, &link_local_address, 16);
3491 md5_finish (&m, digest);
3493 clib_memcpy (&link_local_address, digest, 16);
3495 radv_info->randomizer = link_local_address.as_u64[0];
3497 link_local_address.as_u64[0] =
3498 clib_host_to_net_u64 (0xFE80000000000000ULL);
3500 link_local_address.as_u8[8] &= 0xfd;
3503 ip6_mfib_interface_enable_disable (sw_if_index, 1);
3505 /* essentially "enables" ipv6 on this interface */
3506 error = ip6_add_del_interface_address (vm, sw_if_index,
3507 &link_local_address,
3509 /* address width */ ,
3513 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3517 radv_info->link_local_address = link_local_address;
3526 static clib_error_t *
3527 enable_ip6_interface_cmd (vlib_main_t * vm,
3528 unformat_input_t * input, vlib_cli_command_t * cmd)
3530 vnet_main_t *vnm = vnet_get_main ();
3531 clib_error_t *error = 0;
3536 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3538 enable_ip6_interface (vm, sw_if_index);
3542 error = clib_error_return (0, "unknown interface\n'",
3543 format_unformat_error, input);
3550 * This command is used to enable IPv6 on a given interface.
3553 * Example of how enable IPv6 on a given interface:
3554 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
3557 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
3559 .path = "enable ip6 interface",
3560 .function = enable_ip6_interface_cmd,
3561 .short_help = "enable ip6 interface <interface>",
3565 static clib_error_t *
3566 disable_ip6_interface_cmd (vlib_main_t * vm,
3567 unformat_input_t * input, vlib_cli_command_t * cmd)
3569 vnet_main_t *vnm = vnet_get_main ();
3570 clib_error_t *error = 0;
3575 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3577 error = disable_ip6_interface (vm, sw_if_index);
3581 error = clib_error_return (0, "unknown interface\n'",
3582 format_unformat_error, input);
3589 * This command is used to disable IPv6 on a given interface.
3592 * Example of how disable IPv6 on a given interface:
3593 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
3596 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
3598 .path = "disable ip6 interface",
3599 .function = disable_ip6_interface_cmd,
3600 .short_help = "disable ip6 interface <interface>",
3605 * This command is used to configure the neighbor discovery
3606 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
3607 * command to display some of the current neighbor discovery parameters
3608 * on a given interface. This command has three formats:
3611 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
3613 * '<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>'
3617 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
3618 * router-advertisement messages to use stateful address
3619 * auto-configuration to obtain address information (sets the M-bit).
3620 * Default is the M-bit is not set and the '<em>no</em>' option
3621 * returns it to this default state.
3623 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
3624 * router-advertisement messages that hosts use stateful auto
3625 * configuration to obtain nonaddress related information (sets
3626 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
3627 * option returns it to this default state.
3629 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
3630 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
3631 * router-advertisement messages.
3633 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
3634 * optional source link-layer address in the ICMPv6 router-advertisement
3635 * messages. Default is to include the optional source link-layer address
3636 * and the '<em>no</em>' option returns it to this default state.
3638 * <em>[no] ra-send-unicast</em> - Use the source address of the
3639 * router-solicitation message if availiable. The default is to use
3640 * multicast address of all nodes, and the '<em>no</em>' option returns
3641 * it to this default state.
3643 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
3644 * default router in ICMPv6 router-advertisement messages. The range is
3645 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
3646 * '<em><max-interval></em>'. The default value is 600 seconds and the
3647 * '<em>no</em>' option returns it to this default value.
3649 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
3650 * router-advertisement messages sent and the interval between each
3651 * message. Range for count is 1 - 3 and default is 3. Range for interval
3652 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
3653 * returns both to their default value.
3655 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
3656 * interval between sending ICMPv6 router-advertisement messages. The
3657 * range for max-interval is from 4 to 200 seconds. min-interval can not
3658 * be more than 75% of max-interval. If not set, min-interval will be
3659 * set to 75% of max-interval. The range for min-interval is from 3 to
3660 * 150 seconds. The '<em>no</em>' option returns both to their default
3663 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
3664 * The '<em>no</em>' options implies to start (or restart) sending
3665 * ICMPv6 router-advertisement messages.
3668 * <b>Format 2 - Prefix Options:</b>
3670 * '<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>'
3674 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
3676 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
3677 * length of time in seconds during what the prefix is valid for the purpose of
3678 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
3679 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
3680 * length of time in seconds during what addresses generated from the prefix remain
3681 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
3683 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
3684 * are inifinte, no timeout.
3686 * <em>no-advertise</em> - Do not send full router address in prefix
3687 * advertisement. Default is to advertise (i.e. - This flag is off by default).
3689 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
3690 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
3691 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
3693 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
3694 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
3696 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
3697 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
3698 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
3702 * <b>Format 3: - Default of Prefix:</b>
3704 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
3706 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
3707 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
3708 * is ignored and the prefix is deleted.
3712 * Example of how set a router advertisement option:
3713 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
3714 * Example of how to add a prefix:
3715 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
3716 * Example of how to delete a prefix:
3717 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
3720 VLIB_CLI_COMMAND (ip6_nd_command, static) =
3723 .short_help = "ip6 nd <interface> ...",
3724 .function = ip6_neighbor_cmd,
3729 set_ip6_link_local_address (vlib_main_t * vm,
3730 u32 sw_if_index, ip6_address_t * address)
3732 clib_error_t *error = 0;
3733 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3735 ip6_radv_t *radv_info;
3736 vnet_main_t *vnm = vnet_get_main ();
3738 if (!ip6_address_is_link_local_unicast (address))
3740 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3741 return (error = clib_error_return (0, "address not link-local",
3742 format_unformat_error));
3745 /* call enable ipv6 */
3746 enable_ip6_interface (vm, sw_if_index);
3748 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3752 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3754 /* save if link local address (overwrite default) */
3756 /* delete the old one */
3757 error = ip6_add_del_interface_address (vm, sw_if_index,
3758 &radv_info->link_local_address,
3759 128, 1 /* is_del */ );
3763 /* add the new one */
3764 error = ip6_add_del_interface_address (vm, sw_if_index,
3770 radv_info->link_local_address = *address;
3776 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3777 error = clib_error_return (0, "ip6 not enabled for interface",
3778 format_unformat_error);
3784 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3785 unformat_input_t * input,
3786 vlib_cli_command_t * cmd)
3788 vnet_main_t *vnm = vnet_get_main ();
3789 clib_error_t *error = 0;
3791 ip6_address_t ip6_addr;
3793 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3795 /* get the rest of the command */
3796 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3798 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
3801 return (unformat_parse_error (input));
3804 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
3809 * This command is used to assign an IPv6 Link-local address to an
3810 * interface. This command will enable IPv6 on an interface if it
3811 * is not already enabled. Use the '<em>show ip6 interface</em>' command
3812 * to display the assigned Link-local address.
3815 * Example of how to assign an IPv6 Link-local address to an interface:
3816 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
3819 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
3821 .path = "set ip6 link-local address",
3822 .short_help = "set ip6 link-local address <interface> <ip6-address>",
3823 .function = set_ip6_link_local_address_cmd,
3828 * @brief callback when an interface address is added or deleted
3831 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3834 ip6_address_t * address,
3836 u32 if_address_index, u32 is_delete)
3838 vnet_main_t *vnm = vnet_get_main ();
3839 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3841 vlib_main_t *vm = vnm->vlib_main;
3842 ip6_radv_t *radv_info;
3845 /* create solicited node multicast address for this interface adddress */
3846 ip6_set_solicited_node_multicast_address (&a, 0);
3848 a.as_u8[0xd] = address->as_u8[0xd];
3849 a.as_u8[0xe] = address->as_u8[0xe];
3850 a.as_u8[0xf] = address->as_u8[0xf];
3854 /* try to create radv_info - does nothing if ipv6 already enabled */
3855 enable_ip6_interface (vm, sw_if_index);
3857 /* look up the radv_t information for this interface */
3858 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3860 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3864 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3867 if (!ip6_address_is_link_local_unicast (address))
3868 radv_info->ref_count++;
3870 ip6_neighbor_add_mld_prefix (radv_info, &a);
3877 /* look up the radv_t information for this interface */
3878 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3880 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3885 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3887 ip6_neighbor_del_mld_prefix (radv_info, &a);
3889 /* if interface up send MLDP "report" */
3890 radv_info->all_routers_mcast = 0;
3893 if (!ip6_address_is_link_local_unicast (address))
3894 radv_info->ref_count--;
3896 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
3897 disable_ip6_interface (vm, sw_if_index);
3902 ip6_set_neighbor_limit (u32 neighbor_limit)
3904 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3906 nm->limit_neighbor_cache_size = neighbor_limit;
3911 ip6_neighbor_table_bind (ip6_main_t * im,
3914 u32 new_fib_index, u32 old_fib_index)
3916 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3917 ip6_neighbor_t *n = NULL;
3918 u32 i, *to_re_add = 0;
3921 pool_foreach (n, nm->neighbor_pool,
3923 if (n->key.sw_if_index == sw_if_index)
3924 vec_add1 (to_re_add, n - nm->neighbor_pool);
3928 for (i = 0; i < vec_len (to_re_add); i++)
3930 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
3931 ip6_neighbor_adj_fib_remove (n, old_fib_index);
3932 ip6_neighbor_adj_fib_add (n, new_fib_index);
3934 vec_free (to_re_add);
3937 static clib_error_t *
3938 ip6_neighbor_init (vlib_main_t * vm)
3940 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3941 ip6_main_t *im = &ip6_main;
3943 mhash_init (&nm->neighbor_index_by_key,
3944 /* value size */ sizeof (uword),
3945 /* key size */ sizeof (ip6_neighbor_key_t));
3947 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
3948 ip6_icmp_neighbor_solicitation_node.index);
3949 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
3950 ip6_icmp_neighbor_advertisement_node.index);
3951 icmp6_register_type (vm, ICMP6_router_solicitation,
3952 ip6_icmp_router_solicitation_node.index);
3953 icmp6_register_type (vm, ICMP6_router_advertisement,
3954 ip6_icmp_router_advertisement_node.index);
3956 /* handler node for ip6 neighbor discovery events and timers */
3957 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3959 /* add call backs */
3960 ip6_add_del_interface_address_callback_t cb;
3961 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
3963 /* when an interface address changes... */
3964 cb.function = ip6_neighbor_add_del_interface_address;
3965 cb.function_opaque = 0;
3966 vec_add1 (im->add_del_interface_address_callbacks, cb);
3968 ip6_table_bind_callback_t cbt;
3969 cbt.function = ip6_neighbor_table_bind;
3970 cbt.function_opaque = 0;
3971 vec_add1 (im->table_bind_callbacks, cbt);
3973 mhash_init (&nm->pending_resolutions_by_address,
3974 /* value size */ sizeof (uword),
3975 /* key size */ sizeof (ip6_address_t));
3977 mhash_init (&nm->mac_changes_by_address,
3978 /* value size */ sizeof (uword),
3979 /* key size */ sizeof (ip6_address_t));
3981 /* default, configurable */
3982 nm->limit_neighbor_cache_size = 50000;
3984 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
3987 /* $$$$ Hack fix for today */
3988 vec_validate_init_empty
3989 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
3995 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3999 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4002 uword type_opaque, uword data)
4004 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4005 ip6_address_t *address = address_arg;
4007 pending_resolution_t *pr;
4009 pool_get (nm->pending_resolutions, pr);
4011 pr->next_index = ~0;
4012 pr->node_index = node_index;
4013 pr->type_opaque = type_opaque;
4016 p = mhash_get (&nm->pending_resolutions_by_address, address);
4019 /* Insert new resolution at the head of the list */
4020 pr->next_index = p[0];
4021 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4024 mhash_set (&nm->pending_resolutions_by_address, address,
4025 pr - nm->pending_resolutions, 0 /* old value */ );
4029 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4030 void *data_callback,
4034 uword type_opaque, uword data, int is_add)
4036 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4037 ip6_address_t *address = address_arg;
4039 /* Try to find an existing entry */
4040 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4042 pending_resolution_t *mc;
4043 while (p && *p != ~0)
4045 mc = pool_elt_at_index (nm->mac_changes, *p);
4046 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4049 p = &mc->next_index;
4052 int found = p && *p != ~0;
4056 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4058 pool_get (nm->mac_changes, mc);
4059 *mc = (pending_resolution_t)
4061 .next_index = ~0,.node_index = node_index,.type_opaque =
4062 type_opaque,.data = data,.data_callback = data_callback,.pid =
4065 /* Insert new resolution at the end of the list */
4066 u32 new_idx = mc - nm->mac_changes;
4070 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
4075 return VNET_API_ERROR_NO_SUCH_ENTRY;
4077 /* Clients may need to clean up pool entries, too */
4078 void (*fp) (u32, u8 *) = data_callback;
4080 (*fp) (mc->data, 0 /* no new mac addrs */ );
4082 /* Remove the entry from the list and delete the entry */
4083 *p = mc->next_index;
4084 pool_put (nm->mac_changes, mc);
4086 /* Remove from hash if we deleted the last entry */
4087 if (*p == ~0 && p == first)
4088 mhash_unset (&nm->mac_changes_by_address, address, 0);
4094 vnet_ip6_nd_term (vlib_main_t * vm,
4095 vlib_node_runtime_t * node,
4097 ethernet_header_t * eth,
4098 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
4100 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4101 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4103 ndh = ip6_next_header (ip);
4104 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4105 ndh->icmp.type != ICMP6_neighbor_advertisement)
4108 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4109 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4111 u8 *t0 = vlib_add_trace (vm, node, p0,
4112 sizeof (icmp6_input_trace_t));
4113 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4116 /* Check if anyone want ND events for L2 BDs */
4118 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4119 && !ip6_address_is_link_local_unicast (&ip->src_address)))
4121 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
4124 /* Check if MAC entry exsist for solicited target IP */
4125 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4127 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4128 l2_bridge_domain_t *bd_config;
4131 opt = (void *) (ndh + 1);
4132 if ((opt->header.type !=
4133 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4134 (opt->header.n_data_u64s != 1))
4135 return 0; /* source link layer address option not present */
4137 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4139 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4141 { /* found ip-mac entry, generate eighbor advertisement response */
4143 vlib_node_runtime_t *error_node =
4144 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4145 ip->dst_address = ip->src_address;
4146 ip->src_address = ndh->target_address;
4147 ip->hop_limit = 255;
4149 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4150 clib_memcpy (opt->ethernet_address, macp, 6);
4151 ndh->icmp.type = ICMP6_neighbor_advertisement;
4152 ndh->advertisement_flags = clib_host_to_net_u32
4153 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4154 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4155 ndh->icmp.checksum = 0;
4156 ndh->icmp.checksum =
4157 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4158 clib_memcpy (eth->dst_address, eth->src_address, 6);
4159 clib_memcpy (eth->src_address, macp, 6);
4160 vlib_error_count (vm, error_node->node_index,
4161 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4171 ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4175 fib_prefix_t pfx = {
4177 .fp_proto = FIB_PROTOCOL_IP6,
4182 ip46_address_t nh = {
4186 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4188 if (~0 == fib_index)
4189 return VNET_API_ERROR_NO_SUCH_FIB;
4193 fib_table_entry_path_remove (fib_index,
4195 FIB_SOURCE_IP6_ND_PROXY,
4199 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4200 /* flush the ND cache of this address if it's there */
4201 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4202 sw_if_index, addr, NULL, 0);
4206 fib_table_entry_path_add (fib_index,
4208 FIB_SOURCE_IP6_ND_PROXY,
4209 FIB_ENTRY_FLAG_NONE,
4213 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4218 static clib_error_t *
4219 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4220 unformat_input_t * input, vlib_cli_command_t * cmd)
4222 vnet_main_t *vnm = vnet_get_main ();
4223 clib_error_t *error = 0;
4228 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4230 /* get the rest of the command */
4231 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4233 if (unformat (input, "%U", unformat_ip6_address, &addr))
4235 else if (unformat (input, "delete") || unformat (input, "del"))
4238 return (unformat_parse_error (input));
4242 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4248 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4250 .path = "set ip6 nd proxy",
4251 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4252 .function = set_ip6_nd_proxy_cmd,
4257 ethernet_ndp_change_mac (u32 sw_if_index)
4259 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4264 pool_foreach (n, nm->neighbor_pool,
4266 if (n->key.sw_if_index == sw_if_index)
4268 adj_nbr_walk_nh6 (sw_if_index,
4269 &n->key.ip6_address,
4270 ip6_nd_mk_complete_walk, n);
4275 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4277 if (ADJ_INDEX_INVALID != ai)
4278 adj_glean_update_rewrite (ai);
4282 send_ip6_na (vlib_main_t * vm, vnet_hw_interface_t * hi)
4284 ip6_main_t *i6m = &ip6_main;
4285 u32 sw_if_index = hi->sw_if_index;
4286 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4290 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4291 format_ip6_address, ip6_addr, sw_if_index);
4293 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4296 icmp6_neighbor_solicitation_header_t *h =
4297 vlib_packet_template_get_packet (vm,
4298 &i6m->discover_neighbor_packet_template,
4300 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4301 IP6_MULTICAST_SCOPE_link_local,
4302 IP6_MULTICAST_GROUP_ID_all_hosts);
4303 h->ip.src_address = ip6_addr[0];
4304 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4305 h->neighbor.target_address = ip6_addr[0];
4306 h->neighbor.advertisement_flags = clib_host_to_net_u32
4307 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4308 clib_memcpy (h->link_layer_option.ethernet_address,
4309 hi->hw_address, vec_len (hi->hw_address));
4310 h->neighbor.icmp.checksum =
4311 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4312 ASSERT (bogus_length == 0);
4314 /* Setup MAC header with IP6 Etype and mcast DMAC */
4315 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
4316 vlib_buffer_advance (b, -sizeof (ethernet_header_t));
4317 ethernet_header_t *e = vlib_buffer_get_current (b);
4318 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_IP6);
4319 clib_memcpy (e->src_address, hi->hw_address, sizeof (e->src_address));
4320 ip6_multicast_ethernet_address (e->dst_address,
4321 IP6_MULTICAST_GROUP_ID_all_hosts);
4323 /* Send unsolicited ND advertisement packet out the specified interface */
4324 vnet_buffer (b)->sw_if_index[VLIB_RX] =
4325 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
4326 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
4327 u32 *to_next = vlib_frame_vector_args (f);
4330 vlib_put_frame_to_node (vm, hi->output_node_index, f);
4335 * fd.io coding-style-patch-verification: ON
4338 * eval: (c-set-style "gnu")