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 } ip6_neighbor_main_t;
200 /* ipv6 neighbor discovery - timer/event types */
204 } ip6_icmp_neighbor_discovery_event_type_t;
214 } ip6_icmp_neighbor_discovery_event_data_t;
216 static ip6_neighbor_main_t ip6_neighbor_main;
217 static ip6_address_t ip6a_zero; /* ip6 address 0 */
220 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
222 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
223 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
224 vnet_main_t *vnm = vnet_get_main ();
225 vnet_sw_interface_t *si;
229 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags",
230 "Link layer", "Interface");
232 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
233 flags = format (flags, "D");
235 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
236 flags = format (flags, "S");
238 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
239 flags = format (flags, "N");
241 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
242 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
243 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
244 format_ip6_address, &n->key.ip6_address,
245 flags ? (char *) flags : "",
246 format_ethernet_address, n->link_layer_address,
247 format_vnet_sw_interface_name, vnm, si);
254 ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
256 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
260 .fp_proto = FIB_PROTOCOL_IP6,
261 .fp_addr.ip6 = n->key.ip6_address,
263 fib_table_entry_path_remove (fib_index,
268 n->key.sw_if_index, ~0,
269 1, FIB_ROUTE_PATH_FLAG_NONE);
273 static clib_error_t *
274 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
275 u32 sw_if_index, u32 flags)
277 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
280 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
282 u32 i, *to_delete = 0;
285 pool_foreach (n, nm->neighbor_pool,
287 if (n->key.sw_if_index == sw_if_index)
288 vec_add1 (to_delete, n - nm->neighbor_pool);
292 for (i = 0; i < vec_len (to_delete); i++)
294 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
295 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
296 ip6_neighbor_adj_fib_remove (n,
297 ip6_fib_table_get_index_for_sw_if_index
298 (n->key.sw_if_index));
299 pool_put (nm->neighbor_pool, n);
301 vec_free (to_delete);
307 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
310 unset_random_neighbor_entry (void)
312 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
313 vnet_main_t *vnm = vnet_get_main ();
314 vlib_main_t *vm = vnm->vlib_main;
318 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
319 nm->neighbor_delete_rotor = index;
321 /* Try again from elt 0, could happen if an intfc goes down */
324 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
325 nm->neighbor_delete_rotor = index;
328 /* Nothing left in the pool */
332 e = pool_elt_at_index (nm->neighbor_pool, index);
334 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
336 e->link_layer_address,
345 u8 link_layer_address[6];
348 } ip6_neighbor_set_unset_rpc_args_t;
350 static void ip6_neighbor_set_unset_rpc_callback
351 (ip6_neighbor_set_unset_rpc_args_t * a);
353 static void set_unset_ip6_neighbor_rpc
356 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
359 ip6_neighbor_set_unset_rpc_args_t args;
360 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
362 args.sw_if_index = sw_if_index;
363 args.is_add = is_add;
364 args.is_static = is_static;
365 args.is_no_fib_entry = is_no_fib_entry;
366 clib_memcpy (&args.addr, a, sizeof (*a));
367 if (NULL != link_layer_address)
368 clib_memcpy (args.link_layer_address, link_layer_address, 6);
370 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
371 (u8 *) & args, sizeof (args));
375 ip6_nbr_probe (ip_adjacency_t * adj)
377 icmp6_neighbor_solicitation_header_t *h;
378 vnet_main_t *vnm = vnet_get_main ();
379 ip6_main_t *im = &ip6_main;
380 ip_interface_address_t *ia;
381 ip6_address_t *dst, *src;
382 vnet_hw_interface_t *hi;
383 vnet_sw_interface_t *si;
389 vm = vlib_get_main ();
391 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
392 dst = &adj->sub_type.nbr.next_hop.ip6;
394 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
398 src = ip6_interface_address_matching_destination (im, dst,
406 h = vlib_packet_template_get_packet (vm,
407 &im->discover_neighbor_packet_template,
410 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
412 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
413 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
414 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
415 h->ip.src_address = src[0];
416 h->neighbor.target_address = dst[0];
418 clib_memcpy (h->link_layer_option.ethernet_address,
419 hi->hw_address, vec_len (hi->hw_address));
421 h->neighbor.icmp.checksum =
422 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
423 ASSERT (bogus_length == 0);
425 b = vlib_get_buffer (vm, bi);
426 vnet_buffer (b)->sw_if_index[VLIB_RX] =
427 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
429 /* Add encapsulation string for software interface (e.g. ethernet header). */
430 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
431 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
434 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
435 u32 *to_next = vlib_frame_vector_args (f);
438 vlib_put_frame_to_node (vm, hi->output_node_index, f);
443 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
445 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
446 ethernet_build_rewrite (vnet_get_main (),
447 nbr->key.sw_if_index,
448 adj_get_link_type (ai),
449 nbr->link_layer_address));
453 ip6_nd_mk_incomplete (adj_index_t ai)
455 ip_adjacency_t *adj = adj_get (ai);
457 adj_nbr_update_rewrite (ai,
458 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
459 ethernet_build_rewrite (vnet_get_main (),
462 adj_get_link_type (ai),
463 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
466 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
468 k.sw_if_index = sw_if_index; \
469 k.ip6_address = *addr; \
473 static ip6_neighbor_t *
474 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
476 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
477 ip6_neighbor_t *n = NULL;
478 ip6_neighbor_key_t k;
481 IP6_NBR_MK_KEY (k, sw_if_index, addr);
483 p = mhash_get (&nm->neighbor_index_by_key, &k);
486 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
493 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
495 ip6_neighbor_t *nbr = ctx;
497 ip6_nd_mk_complete (ai, nbr);
499 return (ADJ_WALK_RC_CONTINUE);
503 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
505 ip6_nd_mk_incomplete (ai);
507 return (ADJ_WALK_RC_CONTINUE);
511 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
518 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
520 switch (adj->lookup_next_index)
522 case IP_LOOKUP_NEXT_ARP:
523 case IP_LOOKUP_NEXT_GLEAN:
526 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
527 ip6_nd_mk_complete_walk, nbr);
532 * no matching ND entry.
533 * construct the rewrite required to for an ND packet, and stick
534 * that in the adj's pipe to smoke.
536 adj_nbr_update_rewrite (ai,
537 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
538 ethernet_build_rewrite (vnm,
541 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
544 * since the FIB has added this adj for a route, it makes sense it may
545 * want to forward traffic sometime soon. Let's send a speculative ND.
546 * just one. If we were to do periodically that wouldn't be bad either,
547 * but that's more code than i'm prepared to write at this time for
548 * relatively little reward.
553 case IP_LOOKUP_NEXT_MCAST:
556 * Construct a partial rewrite from the known ethernet mcast dest MAC
561 rewrite = ethernet_build_rewrite (vnm,
564 ethernet_ip6_mcast_dst_addr ());
567 * Complete the remaining fields of the adj's rewrite to direct the
568 * complete of the rewrite at switch time by copying in the IP
569 * dst address's bytes.
570 * Ofset is 2 bytes into the desintation address. And we write 4 bytes.
572 offset = vec_len (rewrite) - 2;
573 adj_mcast_update_rewrite (ai, rewrite, offset, 0xffffffff);
577 case IP_LOOKUP_NEXT_DROP:
578 case IP_LOOKUP_NEXT_PUNT:
579 case IP_LOOKUP_NEXT_LOCAL:
580 case IP_LOOKUP_NEXT_REWRITE:
581 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
582 case IP_LOOKUP_NEXT_MIDCHAIN:
583 case IP_LOOKUP_NEXT_ICMP_ERROR:
584 case IP_LOOKUP_N_NEXT:
592 ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
596 .fp_proto = FIB_PROTOCOL_IP6,
597 .fp_addr.ip6 = n->key.ip6_address,
601 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
602 FIB_ENTRY_FLAG_ATTACHED,
603 DPO_PROTO_IP6, &pfx.fp_addr,
604 n->key.sw_if_index, ~0, 1, NULL,
605 FIB_ROUTE_PATH_FLAG_NONE);
609 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
612 u8 * link_layer_address,
613 uword n_bytes_link_layer_address,
614 int is_static, int is_no_fib_entry)
616 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
617 ip6_neighbor_key_t k;
618 ip6_neighbor_t *n = 0;
619 int make_new_nd_cache_entry = 1;
622 pending_resolution_t *pr, *mc;
624 if (vlib_get_thread_index ())
626 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
627 1 /* set new neighbor */ , is_static,
632 k.sw_if_index = sw_if_index;
633 k.ip6_address = a[0];
636 p = mhash_get (&nm->neighbor_index_by_key, &k);
639 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
640 /* Refuse to over-write static neighbor entry. */
641 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
643 make_new_nd_cache_entry = 0;
646 if (make_new_nd_cache_entry)
648 pool_get (nm->neighbor_pool, n);
649 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
652 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
654 clib_memcpy (n->link_layer_address,
655 link_layer_address, n_bytes_link_layer_address);
658 * create the adj-fib. the entry in the FIB table for and to the peer.
660 if (!is_no_fib_entry)
662 ip6_neighbor_adj_fib_add (n,
663 ip6_fib_table_get_index_for_sw_if_index
664 (n->key.sw_if_index));
668 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
674 * prevent a DoS attack from the data-plane that
675 * spams us with no-op updates to the MAC address
677 if (0 == memcmp (n->link_layer_address,
678 link_layer_address, n_bytes_link_layer_address))
681 clib_memcpy (n->link_layer_address,
682 link_layer_address, n_bytes_link_layer_address);
685 /* Update time stamp and flags. */
686 n->cpu_time_last_updated = clib_cpu_time_now ();
688 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
690 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
692 adj_nbr_walk_nh6 (sw_if_index,
693 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
695 /* Customer(s) waiting for this address to be resolved? */
696 p = mhash_get (&nm->pending_resolutions_by_address, a);
701 while (next_index != (u32) ~ 0)
703 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
704 vlib_process_signal_event (vm, pr->node_index,
705 pr->type_opaque, pr->data);
706 next_index = pr->next_index;
707 pool_put (nm->pending_resolutions, pr);
710 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
713 /* Customer(s) requesting ND event for this address? */
714 p = mhash_get (&nm->mac_changes_by_address, a);
719 while (next_index != (u32) ~ 0)
721 int (*fp) (u32, u8 *, u32, ip6_address_t *);
723 mc = pool_elt_at_index (nm->mac_changes, next_index);
724 fp = mc->data_callback;
726 /* Call the user's data callback, return 1 to suppress dup events */
729 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
731 * Signal the resolver process, as long as the user
732 * says they want to be notified
735 vlib_process_signal_event (vm, mc->node_index,
736 mc->type_opaque, mc->data);
737 next_index = mc->next_index;
745 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
748 u8 * link_layer_address,
749 uword n_bytes_link_layer_address)
751 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
752 ip6_neighbor_key_t k;
757 if (vlib_get_thread_index ())
759 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
760 0 /* unset */ , 0, 0);
764 k.sw_if_index = sw_if_index;
765 k.ip6_address = a[0];
768 p = mhash_get (&nm->neighbor_index_by_key, &k);
775 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
776 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
778 adj_nbr_walk_nh6 (sw_if_index,
779 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
782 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
786 .fp_proto = FIB_PROTOCOL_IP6,
787 .fp_addr.ip6 = n->key.ip6_address,
789 fib_table_entry_path_remove
790 (ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index),
794 &pfx.fp_addr, n->key.sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
796 pool_put (nm->neighbor_pool, n);
802 static void ip6_neighbor_set_unset_rpc_callback
803 (ip6_neighbor_set_unset_rpc_args_t * a)
805 vlib_main_t *vm = vlib_get_main ();
807 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
808 a->link_layer_address, 6, a->is_static,
811 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
812 a->link_layer_address, 6);
816 ip6_neighbor_sort (void *a1, void *a2)
818 vnet_main_t *vnm = vnet_get_main ();
819 ip6_neighbor_t *n1 = a1, *n2 = a2;
821 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
822 n2->key.sw_if_index);
824 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
829 ip6_neighbors_entries (u32 sw_if_index)
831 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
832 ip6_neighbor_t *n, *ns = 0;
835 pool_foreach (n, nm->neighbor_pool,
837 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
844 vec_sort_with_function (ns, ip6_neighbor_sort);
848 static clib_error_t *
849 show_ip6_neighbors (vlib_main_t * vm,
850 unformat_input_t * input, vlib_cli_command_t * cmd)
852 vnet_main_t *vnm = vnet_get_main ();
853 ip6_neighbor_t *n, *ns;
854 clib_error_t *error = 0;
857 /* Filter entries by interface if given. */
859 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
861 ns = ip6_neighbors_entries (sw_if_index);
864 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
867 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
876 * This command is used to display the adjacent IPv6 hosts found via
877 * neighbor discovery. Optionally, limit the output to the specified
881 * Example of how to display the IPv6 neighbor adjacency table:
882 * @cliexstart{show ip6 neighbors}
883 * Time Address Flags Link layer Interface
884 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
885 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
886 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
888 * Example of how to display the IPv6 neighbor adjacency table for given interface:
889 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
890 * Time Address Flags Link layer Interface
891 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
892 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
896 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
897 .path = "show ip6 neighbors",
898 .function = show_ip6_neighbors,
899 .short_help = "show ip6 neighbors [<interface>]",
903 static clib_error_t *
904 set_ip6_neighbor (vlib_main_t * vm,
905 unformat_input_t * input, vlib_cli_command_t * cmd)
907 vnet_main_t *vnm = vnet_get_main ();
913 int is_no_fib_entry = 0;
916 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
918 /* intfc, ip6-address, mac-address */
919 if (unformat (input, "%U %U %U",
920 unformat_vnet_sw_interface, vnm, &sw_if_index,
921 unformat_ip6_address, &addr,
922 unformat_ethernet_address, mac_address))
925 else if (unformat (input, "delete") || unformat (input, "del"))
927 else if (unformat (input, "static"))
929 else if (unformat (input, "no-fib-entry"))
936 return clib_error_return (0, "Missing interface, ip6 or hw address");
939 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
940 mac_address, sizeof (mac_address),
941 is_static, is_no_fib_entry);
943 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
944 mac_address, sizeof (mac_address));
949 * This command is used to manually add an entry to the IPv6 neighbor
950 * adjacency table. Optionally, the entry can be added as static. It is
951 * also used to remove an entry from the table. Use the '<em>show ip6
952 * neighbors</em>' command to display all learned and manually entered entries.
955 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
956 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
957 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
958 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
961 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
963 .path = "set ip6 neighbor",
964 .function = set_ip6_neighbor,
965 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
971 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
972 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
973 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
974 } icmp6_neighbor_solicitation_or_advertisement_next_t;
976 static_always_inline uword
977 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
978 vlib_node_runtime_t * node,
979 vlib_frame_t * frame,
980 uword is_solicitation)
982 vnet_main_t *vnm = vnet_get_main ();
983 ip6_main_t *im = &ip6_main;
984 uword n_packets = frame->n_vectors;
986 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
987 icmp6_neighbor_discovery_option_type_t option_type;
988 vlib_node_runtime_t *error_node =
989 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
992 from = vlib_frame_vector_args (frame);
993 n_left_from = n_packets;
994 next_index = node->cached_next_index;
996 if (node->flags & VLIB_NODE_FLAG_TRACE)
997 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
999 sizeof (icmp6_input_trace_t));
1003 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1004 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1005 n_advertisements_sent = 0;
1007 while (n_left_from > 0)
1009 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1011 while (n_left_from > 0 && n_left_to_next > 0)
1015 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1016 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1017 u32 bi0, options_len0, sw_if_index0, next0, error0;
1018 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1022 bi0 = to_next[0] = from[0];
1027 n_left_to_next -= 1;
1029 p0 = vlib_get_buffer (vm, bi0);
1030 ip0 = vlib_buffer_get_current (p0);
1031 h0 = ip6_next_header (ip0);
1033 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1035 error0 = ICMP6_ERROR_NONE;
1036 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1037 ip6_sadd_link_local =
1038 ip6_address_is_link_local_unicast (&ip0->src_address);
1039 ip6_sadd_unspecified =
1040 ip6_address_is_unspecified (&ip0->src_address);
1042 /* Check that source address is unspecified, link-local or else on-link. */
1043 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1045 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1047 if (ADJ_INDEX_INVALID != src_adj_index0)
1049 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1051 /* Allow all realistic-looking rewrite adjacencies to pass */
1052 ni0 = adj0->lookup_next_index;
1053 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1054 (ni0 < IP6_LOOKUP_N_NEXT);
1056 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1059 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1065 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1069 o0 = (void *) (h0 + 1);
1070 o0 = ((options_len0 == 8 && o0->header.type == option_type
1071 && o0->header.n_data_u64s == 1) ? o0 : 0);
1073 /* If src address unspecified or link local, donot learn neighbor MAC */
1074 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1075 !ip6_sadd_unspecified))
1077 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1078 if (nm->limit_neighbor_cache_size &&
1079 pool_elts (nm->neighbor_pool) >=
1080 nm->limit_neighbor_cache_size)
1081 unset_random_neighbor_entry ();
1082 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1085 &h0->target_address,
1086 o0->ethernet_address,
1087 sizeof (o0->ethernet_address),
1088 0, ip6_sadd_link_local);
1091 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1093 /* Check that target address is local to this router. */
1094 fib_node_index_t fei;
1098 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1100 if (~0 == fib_index)
1102 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1106 fei = ip6_fib_table_lookup_exact_match (fib_index,
1107 &h0->target_address,
1110 if (FIB_NODE_INDEX_INVALID == fei)
1112 /* The target address is not in the FIB */
1114 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1118 if (FIB_ENTRY_FLAG_LOCAL &
1119 fib_entry_get_flags_for_source (fei,
1120 FIB_SOURCE_INTERFACE))
1122 /* It's an address that belongs to one of our interfaces
1126 if (fib_entry_is_sourced
1127 (fei, FIB_SOURCE_IP6_ND_PROXY))
1129 /* The address was added by IPv6 Proxy ND config.
1130 * We should only respond to these if the NS arrived on
1131 * the link that has a matching covering prefix */
1136 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1142 if (is_solicitation)
1143 next0 = (error0 != ICMP6_ERROR_NONE
1144 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1145 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1149 error0 = error0 == ICMP6_ERROR_NONE ?
1150 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1153 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1155 vnet_sw_interface_t *sw_if0;
1156 ethernet_interface_t *eth_if0;
1157 ethernet_header_t *eth0;
1159 /* dst address is either source address or the all-nodes mcast addr */
1160 if (!ip6_sadd_unspecified)
1161 ip0->dst_address = ip0->src_address;
1163 ip6_set_reserved_multicast_address (&ip0->dst_address,
1164 IP6_MULTICAST_SCOPE_link_local,
1165 IP6_MULTICAST_GROUP_ID_all_hosts);
1167 ip0->src_address = h0->target_address;
1168 ip0->hop_limit = 255;
1169 h0->icmp.type = ICMP6_neighbor_advertisement;
1171 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1172 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1174 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1177 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1179 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1182 h0->advertisement_flags = clib_host_to_net_u32
1183 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1184 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1186 h0->icmp.checksum = 0;
1188 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1190 ASSERT (bogus_length == 0);
1192 /* Reuse current MAC header, copy SMAC to DMAC and
1193 * interface MAC to SMAC */
1194 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1195 eth0 = vlib_buffer_get_current (p0);
1196 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1198 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1200 /* Setup input and output sw_if_index for packet */
1201 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1202 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1203 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1204 vnet_main.local_interface_sw_if_index;
1206 n_advertisements_sent++;
1209 p0->error = error_node->errors[error0];
1211 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1212 to_next, n_left_to_next,
1216 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1219 /* Account for advertisements sent. */
1220 vlib_error_count (vm, error_node->node_index,
1221 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1222 n_advertisements_sent);
1224 return frame->n_vectors;
1227 /* for "syslogging" - use elog for now */
1228 #define foreach_log_level \
1229 _ (DEBUG, "DEBUG") \
1230 _ (INFO, "INFORMATION") \
1231 _ (NOTICE, "NOTICE") \
1232 _ (WARNING, "WARNING") \
1234 _ (CRIT, "CRITICAL") \
1235 _ (ALERT, "ALERT") \
1236 _ (EMERG, "EMERGENCY")
1240 #define _(f,s) LOG_##f,
1245 static char *log_level_strings[] = {
1251 static int logmask = 1 << LOG_DEBUG;
1254 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1256 /* just use elog for now */
1260 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1266 what = va_format (0, fmt, &va);
1268 ELOG_TYPE_DECLARE (e) =
1270 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1275 ed = ELOG_DATA (&vm->elog_main, e);
1276 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1277 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1283 /* ipv6 neighbor discovery - router advertisements */
1286 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1287 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1288 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1289 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1290 } icmp6_router_solicitation_or_advertisement_next_t;
1292 static_always_inline uword
1293 icmp6_router_solicitation (vlib_main_t * vm,
1294 vlib_node_runtime_t * node, vlib_frame_t * frame)
1296 vnet_main_t *vnm = vnet_get_main ();
1297 ip6_main_t *im = &ip6_main;
1298 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1299 uword n_packets = frame->n_vectors;
1300 u32 *from, *to_next;
1301 u32 n_left_from, n_left_to_next, next_index;
1302 u32 n_advertisements_sent = 0;
1305 icmp6_neighbor_discovery_option_type_t option_type;
1307 vlib_node_runtime_t *error_node =
1308 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1310 from = vlib_frame_vector_args (frame);
1311 n_left_from = n_packets;
1312 next_index = node->cached_next_index;
1314 if (node->flags & VLIB_NODE_FLAG_TRACE)
1315 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1317 sizeof (icmp6_input_trace_t));
1319 /* source may append his LL address */
1320 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1322 while (n_left_from > 0)
1324 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1326 while (n_left_from > 0 && n_left_to_next > 0)
1330 ip6_radv_t *radv_info = 0;
1332 icmp6_neighbor_discovery_header_t *h0;
1333 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1335 u32 bi0, options_len0, sw_if_index0, next0, error0;
1336 u32 is_solicitation = 1, is_dropped = 0;
1337 u32 is_unspecified, is_link_local;
1339 bi0 = to_next[0] = from[0];
1344 n_left_to_next -= 1;
1346 p0 = vlib_get_buffer (vm, bi0);
1347 ip0 = vlib_buffer_get_current (p0);
1348 h0 = ip6_next_header (ip0);
1350 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1351 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1353 ip6_address_is_link_local_unicast (&ip0->src_address);
1355 error0 = ICMP6_ERROR_NONE;
1356 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1358 /* check if solicitation (not from nd_timer node) */
1359 if (ip6_address_is_unspecified (&ip0->dst_address))
1360 is_solicitation = 0;
1362 /* Check that source address is unspecified, link-local or else on-link. */
1363 if (!is_unspecified && !is_link_local)
1365 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1367 if (ADJ_INDEX_INVALID != src_adj_index0)
1369 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1371 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1373 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1378 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1382 /* check for source LL option and process */
1383 o0 = (void *) (h0 + 1);
1384 o0 = ((options_len0 == 8
1385 && o0->header.type == option_type
1386 && o0->header.n_data_u64s == 1) ? o0 : 0);
1388 /* if src address unspecified IGNORE any options */
1389 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1390 !is_unspecified && !is_link_local))
1392 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1393 if (nm->limit_neighbor_cache_size &&
1394 pool_elts (nm->neighbor_pool) >=
1395 nm->limit_neighbor_cache_size)
1396 unset_random_neighbor_entry ();
1398 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1400 o0->ethernet_address,
1401 sizeof (o0->ethernet_address),
1405 /* default is to drop */
1406 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1408 if (error0 == ICMP6_ERROR_NONE)
1410 vnet_sw_interface_t *sw_if0;
1411 ethernet_interface_t *eth_if0;
1414 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1415 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1417 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1419 /* only support ethernet interface type for now */
1421 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1424 if (error0 == ICMP6_ERROR_NONE)
1428 /* adjust the sizeof the buffer to just include the ipv6 header */
1429 p0->current_length -=
1431 sizeof (icmp6_neighbor_discovery_header_t));
1433 /* look up the radv_t information for this interface */
1434 vec_validate_init_empty
1435 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1437 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1440 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1444 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1447 if (error0 == ICMP6_ERROR_NONE)
1449 f64 now = vlib_time_now (vm);
1451 /* for solicited adverts - need to rate limit */
1452 if (is_solicitation)
1454 if (0 != radv_info->last_radv_time &&
1455 (now - radv_info->last_radv_time) <
1456 MIN_DELAY_BETWEEN_RAS)
1459 radv_info->last_radv_time = now;
1463 icmp6_router_advertisement_header_t rh;
1465 rh.icmp.type = ICMP6_router_advertisement;
1467 rh.icmp.checksum = 0;
1469 rh.current_hop_limit = radv_info->curr_hop_limit;
1470 rh.router_lifetime_in_sec =
1471 clib_host_to_net_u16
1472 (radv_info->adv_router_lifetime_in_sec);
1474 time_in_msec_between_retransmitted_neighbor_solicitations
1476 clib_host_to_net_u32 (radv_info->
1477 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1478 rh.neighbor_reachable_time_in_msec =
1479 clib_host_to_net_u32 (radv_info->
1480 adv_neighbor_reachable_time_in_msec);
1483 (radv_info->adv_managed_flag) ?
1484 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1487 ((radv_info->adv_other_flag) ?
1488 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1492 u16 payload_length =
1493 sizeof (icmp6_router_advertisement_header_t);
1495 vlib_buffer_add_data (vm,
1496 vlib_buffer_get_free_list_index
1497 (p0), bi0, (void *) &rh,
1499 (icmp6_router_advertisement_header_t));
1501 if (radv_info->adv_link_layer_address)
1503 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1507 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1508 h.header.n_data_u64s = 1;
1510 /* copy ll address */
1511 clib_memcpy (&h.ethernet_address[0],
1512 eth_if0->address, 6);
1514 vlib_buffer_add_data (vm,
1515 vlib_buffer_get_free_list_index
1516 (p0), bi0, (void *) &h,
1518 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1522 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1525 /* add MTU option */
1526 if (radv_info->adv_link_mtu)
1528 icmp6_neighbor_discovery_mtu_option_t h;
1532 clib_host_to_net_u32 (radv_info->adv_link_mtu);
1533 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1534 h.header.n_data_u64s = 1;
1537 sizeof (icmp6_neighbor_discovery_mtu_option_t);
1539 vlib_buffer_add_data (vm,
1540 vlib_buffer_get_free_list_index
1541 (p0), bi0, (void *) &h,
1543 (icmp6_neighbor_discovery_mtu_option_t));
1546 /* add advertised prefix options */
1547 ip6_radv_prefix_t *pr_info;
1550 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1552 if(pr_info->enabled &&
1553 (!pr_info->decrement_lifetime_flag
1554 || (pr_info->pref_lifetime_expires >0)))
1556 /* advertise this prefix */
1557 icmp6_neighbor_discovery_prefix_information_option_t h;
1559 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1560 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1562 h.dst_address_length = pr_info->prefix_len;
1564 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1565 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
1567 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1569 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1570 h.preferred_time = 0;
1574 if(pr_info->decrement_lifetime_flag)
1576 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1577 (pr_info->valid_lifetime_expires - now) : 0;
1579 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1580 (pr_info->pref_lifetime_expires - now) : 0;
1583 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1584 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1588 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1590 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1592 vlib_buffer_add_data (vm,
1593 vlib_buffer_get_free_list_index (p0),
1595 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1601 /* add additional options before here */
1603 /* finish building the router advertisement... */
1604 if (!is_unspecified && radv_info->send_unicast)
1606 ip0->dst_address = ip0->src_address;
1610 /* target address is all-nodes mcast addr */
1611 ip6_set_reserved_multicast_address
1613 IP6_MULTICAST_SCOPE_link_local,
1614 IP6_MULTICAST_GROUP_ID_all_hosts);
1617 /* source address MUST be the link-local address */
1618 ip0->src_address = radv_info->link_local_address;
1620 ip0->hop_limit = 255;
1621 ip0->payload_length =
1622 clib_host_to_net_u16 (payload_length);
1624 icmp6_router_advertisement_header_t *rh0 =
1625 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1626 rh0->icmp.checksum =
1627 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1629 ASSERT (bogus_length == 0);
1631 /* setup output if and adjacency */
1632 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1633 vnet_main.local_interface_sw_if_index;
1635 if (is_solicitation)
1637 ethernet_header_t *eth0;
1638 /* Reuse current MAC header, copy SMAC to DMAC and
1639 * interface MAC to SMAC */
1640 vlib_buffer_reset (p0);
1641 eth0 = vlib_buffer_get_current (p0);
1642 clib_memcpy (eth0->dst_address, eth0->src_address,
1644 clib_memcpy (eth0->src_address, eth_if0->address,
1647 is_dropped ? next0 :
1648 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1649 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1654 adj_index0 = radv_info->mcast_adj_index;
1655 if (adj_index0 == 0)
1656 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1660 is_dropped ? next0 :
1661 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1662 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1666 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1668 radv_info->n_solicitations_dropped += is_dropped;
1669 radv_info->n_solicitations_rcvd += is_solicitation;
1671 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1673 radv_info->n_advertisements_sent++;
1674 n_advertisements_sent++;
1680 p0->error = error_node->errors[error0];
1682 if (error0 != ICMP6_ERROR_NONE)
1683 vlib_error_count (vm, error_node->node_index, error0, 1);
1685 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1686 to_next, n_left_to_next,
1691 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1694 /* Account for router advertisements sent. */
1695 vlib_error_count (vm, error_node->node_index,
1696 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1697 n_advertisements_sent);
1699 return frame->n_vectors;
1702 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1703 static_always_inline uword
1704 icmp6_router_advertisement (vlib_main_t * vm,
1705 vlib_node_runtime_t * node, vlib_frame_t * frame)
1707 vnet_main_t *vnm = vnet_get_main ();
1708 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1709 uword n_packets = frame->n_vectors;
1710 u32 *from, *to_next;
1711 u32 n_left_from, n_left_to_next, next_index;
1712 u32 n_advertisements_rcvd = 0;
1714 vlib_node_runtime_t *error_node =
1715 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1717 from = vlib_frame_vector_args (frame);
1718 n_left_from = n_packets;
1719 next_index = node->cached_next_index;
1721 if (node->flags & VLIB_NODE_FLAG_TRACE)
1722 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1724 sizeof (icmp6_input_trace_t));
1726 while (n_left_from > 0)
1728 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1730 while (n_left_from > 0 && n_left_to_next > 0)
1734 ip6_radv_t *radv_info = 0;
1735 icmp6_router_advertisement_header_t *h0;
1736 u32 bi0, options_len0, sw_if_index0, next0, error0;
1738 bi0 = to_next[0] = from[0];
1743 n_left_to_next -= 1;
1745 p0 = vlib_get_buffer (vm, bi0);
1746 ip0 = vlib_buffer_get_current (p0);
1747 h0 = ip6_next_header (ip0);
1749 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1751 error0 = ICMP6_ERROR_NONE;
1752 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1754 /* Check that source address is link-local */
1755 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1756 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1758 /* default is to drop */
1759 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1761 n_advertisements_rcvd++;
1763 if (error0 == ICMP6_ERROR_NONE)
1765 vnet_sw_interface_t *sw_if0;
1766 ethernet_interface_t *eth_if0;
1768 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1769 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1771 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
1773 /* only support ethernet interface type for now */
1775 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1778 if (error0 == ICMP6_ERROR_NONE)
1782 /* look up the radv_t information for this interface */
1783 vec_validate_init_empty
1784 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1786 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1789 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1793 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1796 if (error0 == ICMP6_ERROR_NONE)
1798 /* validate advertised information */
1799 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1800 && (h0->current_hop_limit !=
1801 radv_info->curr_hop_limit))
1803 ip6_neighbor_syslog (vm, LOG_WARNING,
1804 "our AdvCurHopLimit on %U doesn't agree with %U",
1805 format_vnet_sw_if_index_name,
1812 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1813 != radv_info->adv_managed_flag)
1815 ip6_neighbor_syslog (vm, LOG_WARNING,
1816 "our AdvManagedFlag on %U doesn't agree with %U",
1817 format_vnet_sw_if_index_name,
1824 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1825 != radv_info->adv_other_flag)
1827 ip6_neighbor_syslog (vm, LOG_WARNING,
1828 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1829 format_vnet_sw_if_index_name,
1836 time_in_msec_between_retransmitted_neighbor_solicitations
1838 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1840 time_in_msec_between_retransmitted_neighbor_solicitations
1842 clib_host_to_net_u32 (radv_info->
1843 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1845 ip6_neighbor_syslog (vm, LOG_WARNING,
1846 "our AdvRetransTimer on %U doesn't agree with %U",
1847 format_vnet_sw_if_index_name,
1853 if ((h0->neighbor_reachable_time_in_msec &&
1854 radv_info->adv_neighbor_reachable_time_in_msec) &&
1855 (h0->neighbor_reachable_time_in_msec !=
1856 clib_host_to_net_u32
1857 (radv_info->adv_neighbor_reachable_time_in_msec)))
1859 ip6_neighbor_syslog (vm, LOG_WARNING,
1860 "our AdvReachableTime on %U doesn't agree with %U",
1861 format_vnet_sw_if_index_name,
1867 /* check for MTU or prefix options or .. */
1868 u8 *opt_hdr = (u8 *) (h0 + 1);
1869 while (options_len0 > 0)
1871 icmp6_neighbor_discovery_option_header_t *o0 =
1872 (icmp6_neighbor_discovery_option_header_t *)
1874 int opt_len = o0->n_data_u64s << 3;
1875 icmp6_neighbor_discovery_option_type_t option_type =
1878 if (options_len0 < 2)
1880 ip6_neighbor_syslog (vm, LOG_ERR,
1881 "malformed RA packet on %U from %U",
1882 format_vnet_sw_if_index_name,
1891 ip6_neighbor_syslog (vm, LOG_ERR,
1892 " zero length option in RA on %U from %U",
1893 format_vnet_sw_if_index_name,
1899 else if (opt_len > options_len0)
1901 ip6_neighbor_syslog (vm, LOG_ERR,
1902 "option length in RA packet greater than total length on %U from %U",
1903 format_vnet_sw_if_index_name,
1910 options_len0 -= opt_len;
1913 switch (option_type)
1915 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1917 icmp6_neighbor_discovery_mtu_option_t *h =
1918 (icmp6_neighbor_discovery_mtu_option_t
1921 if (opt_len < sizeof (*h))
1924 if ((h->mtu && radv_info->adv_link_mtu) &&
1926 clib_host_to_net_u32
1927 (radv_info->adv_link_mtu)))
1929 ip6_neighbor_syslog (vm, LOG_WARNING,
1930 "our AdvLinkMTU on %U doesn't agree with %U",
1931 format_vnet_sw_if_index_name,
1939 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1941 icmp6_neighbor_discovery_prefix_information_option_t
1943 (icmp6_neighbor_discovery_prefix_information_option_t
1946 /* validate advertised prefix options */
1947 ip6_radv_prefix_t *pr_info;
1948 u32 preferred, valid;
1950 if (opt_len < sizeof (*h))
1954 clib_net_to_host_u32 (h->preferred_time);
1955 valid = clib_net_to_host_u32 (h->valid_time);
1957 /* look for matching prefix - if we our advertising it, it better be consistant */
1959 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1963 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1965 if(pr_info->enabled &&
1966 (pr_info->prefix_len == h->dst_address_length) &&
1967 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1970 if(!pr_info->decrement_lifetime_flag &&
1971 valid != pr_info->adv_valid_lifetime_in_secs)
1973 ip6_neighbor_syslog(vm, LOG_WARNING,
1974 "our ADV validlifetime on %U for %U does not agree with %U",
1975 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1976 format_ip6_address, &h->dst_address);
1978 if(!pr_info->decrement_lifetime_flag &&
1979 preferred != pr_info->adv_pref_lifetime_in_secs)
1981 ip6_neighbor_syslog(vm, LOG_WARNING,
1982 "our ADV preferredlifetime on %U for %U does not agree with %U",
1983 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1984 format_ip6_address, &h->dst_address);
2001 p0->error = error_node->errors[error0];
2003 if (error0 != ICMP6_ERROR_NONE)
2004 vlib_error_count (vm, error_node->node_index, error0, 1);
2006 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2007 to_next, n_left_to_next,
2011 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2014 /* Account for router advertisements sent. */
2015 vlib_error_count (vm, error_node->node_index,
2016 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2017 n_advertisements_rcvd);
2019 return frame->n_vectors;
2023 * @brief Add a multicast Address to the advertised MLD set
2026 ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2028 ip6_mldp_group_t *mcast_group_info;
2031 /* lookup mldp info for this interface */
2032 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2034 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2037 if (!mcast_group_info)
2041 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2043 mi = mcast_group_info - radv_info->mldp_group_pool;
2044 mhash_set (&radv_info->address_to_mldp_index, &addr, mi, /* old_value */
2047 mcast_group_info->type = 4;
2048 mcast_group_info->mcast_source_address_pool = 0;
2049 mcast_group_info->num_sources = 0;
2050 clib_memcpy (&mcast_group_info->mcast_address, &addr,
2051 sizeof (ip6_address_t));
2056 * @brief Delete a multicast Address from the advertised MLD set
2059 ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2061 ip6_mldp_group_t *mcast_group_info;
2064 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2066 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2068 if (mcast_group_info)
2070 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2072 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2077 * @brief Add a multicast Address to the advertised MLD set
2080 ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2081 ip6_multicast_address_scope_t scope,
2082 ip6_multicast_link_local_group_id_t group)
2086 ip6_set_reserved_multicast_address (&addr, scope, group);
2088 ip6_neighbor_add_mld_prefix (a, &addr);
2092 * @brief create and initialize router advertisement parameters with default
2093 * values for this intfc
2096 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
2097 u32 sw_if_index, u32 is_add)
2099 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2102 vnet_sw_interface_t *sw_if0;
2103 ethernet_interface_t *eth_if0 = 0;
2105 /* lookup radv container - ethernet interfaces only */
2106 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2107 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2108 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2113 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2115 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2119 a = pool_elt_at_index (nm->if_radv_pool, ri);
2123 ip6_radv_prefix_t *p;
2124 ip6_mldp_group_t *m;
2126 /* release the lock on the interface's mcast adj */
2127 adj_unlock (a->mcast_adj_index);
2129 /* clean up prefix and MDP pools */
2131 pool_flush(p, a->adv_prefixes_pool,
2133 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
2135 pool_flush (m, a->mldp_group_pool,
2137 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
2141 pool_free (a->mldp_group_pool);
2142 pool_free (a->adv_prefixes_pool);
2144 mhash_free (&a->address_to_prefix_index);
2145 mhash_free (&a->address_to_mldp_index);
2147 pool_put (nm->if_radv_pool, a);
2148 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2156 vnet_hw_interface_t *hw_if0;
2158 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2160 pool_get (nm->if_radv_pool, a);
2162 ri = a - nm->if_radv_pool;
2163 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2165 /* initialize default values (most of which are zero) */
2166 memset (a, 0, sizeof (a[0]));
2168 a->sw_if_index = sw_if_index;
2169 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2170 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2171 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2172 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2174 /* send ll address source address option */
2175 a->adv_link_layer_address = 1;
2177 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2178 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2179 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2180 a->seed = (u32) clib_cpu_time_now ();
2181 (void) random_u32 (&a->seed);
2182 a->randomizer = clib_cpu_time_now ();
2183 (void) random_u64 (&a->randomizer);
2185 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2186 a->initial_adverts_sent = a->initial_adverts_count - 1;
2187 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2189 /* deafult is to send */
2192 /* fill in radv_info for this interface that will be needed later */
2193 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2195 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2197 /* fill in default link-local address (this may be overridden) */
2198 ip6_link_local_address_from_ethernet_address
2199 (&a->link_local_address, eth_if0->address);
2201 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2202 sizeof (ip6_address_t));
2203 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2204 sizeof (ip6_address_t));
2206 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2210 /* add multicast groups we will always be reporting */
2211 ip6_neighbor_add_mld_grp (a,
2212 IP6_MULTICAST_SCOPE_link_local,
2213 IP6_MULTICAST_GROUP_ID_all_hosts);
2214 ip6_neighbor_add_mld_grp (a,
2215 IP6_MULTICAST_SCOPE_link_local,
2216 IP6_MULTICAST_GROUP_ID_all_routers);
2217 ip6_neighbor_add_mld_grp (a,
2218 IP6_MULTICAST_SCOPE_link_local,
2219 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2225 /* send an mldpv2 report */
2227 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2229 vnet_main_t *vnm = vnet_get_main ();
2230 vlib_main_t *vm = vnm->vlib_main;
2231 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2232 vnet_sw_interface_t *sw_if0;
2233 ethernet_interface_t *eth_if0;
2237 ip6_radv_t *radv_info;
2247 icmp6_multicast_listener_report_header_t *rh0;
2248 icmp6_multicast_listener_report_packet_t *rp0;
2250 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2251 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2252 eth_if0 = ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2254 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2257 /* look up the radv_t information for this interface */
2258 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2261 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2266 /* send report now - build a mldpv2 report packet */
2267 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2270 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2271 if (PREDICT_FALSE (n_allocated == 0))
2273 clib_warning ("buffer allocation failure");
2277 b0 = vlib_get_buffer (vm, bo0);
2279 /* adjust the sizeof the buffer to just include the ipv6 header */
2280 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2282 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2284 b0->error = ICMP6_ERROR_NONE;
2286 rp0 = vlib_buffer_get_current (b0);
2287 ip0 = (ip6_header_t *) & rp0->ip;
2288 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2290 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2292 ip0->ip_version_traffic_class_and_flow_label =
2293 clib_host_to_net_u32 (0x6 << 28);
2295 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2296 /* for DEBUG - vnet driver won't seem to emit router alerts */
2297 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2300 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2302 /* source address MUST be the link-local address */
2303 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2304 ip0->src_address = radv_info->link_local_address;
2306 /* destination is all mldpv2 routers */
2307 ip6_set_reserved_multicast_address (&ip0->dst_address,
2308 IP6_MULTICAST_SCOPE_link_local,
2309 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2311 /* add reports here */
2312 ip6_mldp_group_t *m;
2313 int num_addr_records = 0;
2314 icmp6_multicast_address_record_t rr;
2316 /* fill in the hop-by-hop extension header (router alert) info */
2317 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2318 rh0->ext_hdr.n_data_u64s = 0;
2320 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2322 rh0->alert.value = 0;
2327 rh0->icmp.checksum = 0;
2330 pool_foreach (m, radv_info->mldp_group_pool,
2333 rr.aux_data_len_u32s = 0;
2334 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2335 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2339 vlib_buffer_add_data
2340 (vm, vlib_buffer_get_free_list_index (b0), bo0,
2341 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
2343 payload_length += sizeof( icmp6_multicast_address_record_t);
2348 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2350 /* update lengths */
2351 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2353 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2355 ASSERT (bogus_length == 0);
2358 * OK to override w/ no regard for actual FIB, because
2359 * ip6-rewrite only looks at the adjacency.
2361 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2362 vnet_main.local_interface_sw_if_index;
2364 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2365 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2367 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2369 f = vlib_get_frame_to_node (vm, node->index);
2370 to_next = vlib_frame_vector_args (f);
2374 vlib_put_frame_to_node (vm, node->index, f);
2379 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2381 .function = icmp6_router_solicitation,
2382 .name = "icmp6-router-solicitation",
2384 .vector_size = sizeof (u32),
2386 .format_trace = format_icmp6_input_trace,
2388 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2390 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
2391 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2392 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2397 /* send a RA or update the timer info etc.. */
2399 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2400 vlib_node_runtime_t * node,
2401 vlib_frame_t * frame)
2403 vnet_main_t *vnm = vnet_get_main ();
2404 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2405 ip6_radv_t *radv_info;
2406 vlib_frame_t *f = 0;
2407 u32 n_this_frame = 0;
2408 u32 n_left_to_next = 0;
2411 icmp6_router_solicitation_header_t *h0;
2413 f64 now = vlib_time_now (vm);
2415 /* Interface ip6 radv info list */
2417 pool_foreach (radv_info, nm->if_radv_pool,
2419 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2421 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2422 radv_info->next_multicast_time = now;
2423 radv_info->last_multicast_time = now;
2424 radv_info->last_radv_time = 0;
2425 radv_info->all_routers_mcast = 0;
2429 /* Make sure that we've joined the all-routers multicast group */
2430 if(!radv_info->all_routers_mcast)
2432 /* send MDLP_REPORT_EVENT message */
2433 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2434 radv_info->all_routers_mcast = 1;
2437 /* is it time to send a multicast RA on this interface? */
2438 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2443 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2444 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2446 /* multicast send - compute next multicast send time */
2447 if( radv_info->initial_adverts_sent > 0)
2449 radv_info->initial_adverts_sent--;
2450 if(rfn > radv_info-> initial_adverts_interval)
2451 rfn = radv_info-> initial_adverts_interval;
2453 /* check to see if we are ceasing to send */
2454 if( radv_info->initial_adverts_sent == 0)
2455 if(radv_info->cease_radv)
2456 radv_info->send_radv = 0;
2459 radv_info->next_multicast_time = rfn + now;
2460 radv_info->last_multicast_time = now;
2462 /* send advert now - build a "solicted" router advert with unspecified source address */
2463 n_allocated = vlib_buffer_alloc_from_free_list
2464 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2466 if (PREDICT_FALSE(n_allocated == 0))
2468 clib_warning ("buffer allocation failure");
2471 b0 = vlib_get_buffer (vm, bo0);
2472 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2473 b0->error = ICMP6_ERROR_NONE;
2474 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2476 h0 = vlib_buffer_get_current (b0);
2478 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2480 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2481 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2482 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2483 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2484 h0->ip.hop_limit = 255;
2486 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2487 h0->ip.src_address.as_u64[0] = 0;
2488 h0->ip.src_address.as_u64[1] = 0;
2490 h0->ip.dst_address.as_u64[0] = 0;
2491 h0->ip.dst_address.as_u64[1] = 0;
2493 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2495 if (PREDICT_FALSE(f == 0))
2497 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2498 to_next = vlib_frame_vector_args (f);
2499 n_left_to_next = VLIB_FRAME_SIZE;
2508 if (PREDICT_FALSE(n_left_to_next == 0))
2510 f->n_vectors = n_this_frame;
2511 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2520 ASSERT (n_this_frame);
2521 f->n_vectors = n_this_frame;
2522 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2528 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2529 vlib_node_runtime_t * node,
2530 vlib_frame_t * frame)
2533 ip6_icmp_neighbor_discovery_event_data_t *event_data;
2535 /* init code here */
2539 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
2541 event_data = vlib_process_get_event_data (vm, &event_type);
2545 /* No events found: timer expired. */
2546 /* process interface list and send RAs as appropriate, update timer info */
2547 ip6_neighbor_process_timer_event (vm, node, frame);
2554 case ICMP6_ND_EVENT_INIT:
2565 _vec_len (event_data) = 0;
2568 return frame->n_vectors;
2572 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
2574 .function = icmp6_router_advertisement,
2575 .name = "icmp6-router-advertisement",
2577 .vector_size = sizeof (u32),
2579 .format_trace = format_icmp6_input_trace,
2588 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2590 .function = ip6_icmp_neighbor_discovery_event_process,
2591 .name = "ip6-icmp-neighbor-discovery-event-process",
2592 .type = VLIB_NODE_TYPE_PROCESS,
2596 icmp6_neighbor_solicitation (vlib_main_t * vm,
2597 vlib_node_runtime_t * node, vlib_frame_t * frame)
2599 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2600 /* is_solicitation */
2605 icmp6_neighbor_advertisement (vlib_main_t * vm,
2606 vlib_node_runtime_t * node,
2607 vlib_frame_t * frame)
2609 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2610 /* is_solicitation */
2615 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
2617 .function = icmp6_neighbor_solicitation,
2618 .name = "icmp6-neighbor-solicitation",
2620 .vector_size = sizeof (u32),
2622 .format_trace = format_icmp6_input_trace,
2624 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2626 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2627 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2633 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
2635 .function = icmp6_neighbor_advertisement,
2636 .name = "icmp6-neighbor-advertisement",
2638 .vector_size = sizeof (u32),
2640 .format_trace = format_icmp6_input_trace,
2649 /* API support functions */
2651 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
2652 u8 suppress, u8 managed, u8 other,
2653 u8 ll_option, u8 send_unicast, u8 cease,
2654 u8 use_lifetime, u32 lifetime,
2655 u32 initial_count, u32 initial_interval,
2656 u32 max_interval, u32 min_interval, u8 is_no)
2658 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2662 /* look up the radv_t information for this interface */
2663 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2665 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2666 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2671 ip6_radv_t *radv_info;
2672 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2674 if ((max_interval != 0) && (min_interval == 0))
2675 min_interval = .75 * max_interval;
2679 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
2680 radv_info->max_radv_interval;
2683 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
2684 radv_info->min_radv_interval;
2687 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
2688 radv_info->adv_router_lifetime_in_sec;
2692 if (lifetime > MAX_DEF_RTR_LIFETIME)
2693 lifetime = MAX_DEF_RTR_LIFETIME;
2695 if (lifetime <= max_interval)
2696 return VNET_API_ERROR_INVALID_VALUE;
2699 if (min_interval != 0)
2701 if ((min_interval > .75 * max_interval) || (min_interval < 3))
2702 return VNET_API_ERROR_INVALID_VALUE;
2705 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2706 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2707 return VNET_API_ERROR_INVALID_VALUE;
2710 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2711 if "flag" is clear don't change corresponding value
2713 radv_info->send_radv =
2714 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
2715 radv_info->adv_managed_flag =
2716 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2717 radv_info->adv_other_flag =
2718 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
2719 radv_info->adv_link_layer_address =
2721 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2722 radv_info->send_unicast =
2723 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
2724 radv_info->cease_radv =
2725 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
2727 radv_info->min_radv_interval = min_interval;
2728 radv_info->max_radv_interval = max_interval;
2729 radv_info->adv_router_lifetime_in_sec = lifetime;
2731 radv_info->initial_adverts_count =
2733 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
2734 radv_info->initial_adverts_count;
2735 radv_info->initial_adverts_interval =
2736 (initial_interval !=
2737 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
2738 radv_info->initial_adverts_interval;
2741 if ((cease != 0) && (is_no))
2742 radv_info->send_radv = 1;
2744 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2745 radv_info->next_multicast_time = vlib_time_now (vm);
2746 radv_info->last_multicast_time = vlib_time_now (vm);
2747 radv_info->last_radv_time = 0;
2753 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
2754 ip6_address_t * prefix_addr, u8 prefix_len,
2755 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2756 u8 no_advertise, u8 off_link, u8 no_autoconfig,
2757 u8 no_onlink, u8 is_no)
2759 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2764 /* look up the radv_t information for this interface */
2765 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2768 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2770 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2774 f64 now = vlib_time_now (vm);
2775 ip6_radv_t *radv_info;
2776 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2778 /* prefix info add, delete or update */
2779 ip6_radv_prefix_t *prefix;
2781 /* lookup prefix info for this address on this interface */
2782 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2784 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2790 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2792 if (prefix->prefix_len != prefix_len)
2793 return VNET_API_ERROR_INVALID_VALUE_2;
2795 /* FIXME - Should the DP do this or the CP ? */
2796 /* do specific delete processing here before returning */
2797 /* try to remove from routing table */
2799 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
2801 pool_put (radv_info->adv_prefixes_pool, prefix);
2803 radv_info->initial_adverts_sent =
2804 radv_info->initial_adverts_count - 1;
2805 radv_info->next_multicast_time = vlib_time_now (vm);
2806 radv_info->last_multicast_time = vlib_time_now (vm);
2807 radv_info->last_radv_time = 0;
2811 /* adding or changing */
2816 pool_get (radv_info->adv_prefixes_pool, prefix);
2817 pi = prefix - radv_info->adv_prefixes_pool;
2818 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
2821 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
2823 prefix->prefix_len = prefix_len;
2824 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
2826 /* initialize default values */
2827 prefix->adv_on_link_flag = 1; /* L bit set */
2828 prefix->adv_autonomous_flag = 1; /* A bit set */
2829 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2830 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2831 prefix->enabled = 1;
2832 prefix->decrement_lifetime_flag = 1;
2833 prefix->deprecated_prefix_flag = 1;
2837 /* FIXME - Should the DP do this or the CP ? */
2838 /* insert prefix into routing table as a connected prefix */
2847 if (prefix->prefix_len != prefix_len)
2848 return VNET_API_ERROR_INVALID_VALUE_2;
2852 /* FIXME - Should the DP do this or the CP ? */
2853 /* remove from routing table if already there */
2857 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
2859 prefix->adv_valid_lifetime_in_secs = ~0;
2860 prefix->adv_pref_lifetime_in_secs = ~0;
2861 prefix->decrement_lifetime_flag = 0;
2865 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2866 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2869 /* copy remaining */
2870 prefix->enabled = !(no_advertise != 0);
2871 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2872 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2876 /* fill in the expiration times */
2877 prefix->valid_lifetime_expires =
2878 now + prefix->adv_valid_lifetime_in_secs;
2879 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2881 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2882 radv_info->next_multicast_time = vlib_time_now (vm);
2883 radv_info->last_multicast_time = vlib_time_now (vm);
2884 radv_info->last_radv_time = 0;
2890 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
2891 vlib_cli_command_t * cmd)
2893 vnet_main_t *vnm = vnet_get_main ();
2894 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2895 clib_error_t *error = 0;
2897 u8 suppress = 0, managed = 0, other = 0;
2898 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
2899 u8 use_lifetime = 0;
2900 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
2901 0, ra_initial_interval = 0;
2902 u32 ra_max_interval = 0, ra_min_interval = 0;
2904 unformat_input_t _line_input, *line_input = &_line_input;
2905 vnet_sw_interface_t *sw_if0;
2907 int add_radv_info = 1;
2908 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
2909 ip6_address_t ip6_addr;
2913 /* Get a line of input. */
2914 if (!unformat_user (main_input, unformat_line_input, line_input))
2917 /* get basic radv info for this interface */
2918 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2921 if (unformat_user (line_input,
2922 unformat_vnet_sw_interface, vnm, &sw_if_index))
2925 ethernet_interface_t *eth_if0 = 0;
2927 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2928 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2930 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
2935 clib_error_return (0, "Interface must be of ethernet type");
2939 /* look up the radv_t information for this interface */
2940 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
2943 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2947 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2951 error = clib_error_return (0, "unknown interface %U'",
2952 format_unformat_error, line_input);
2958 error = clib_error_return (0, "invalid interface name %U'",
2959 format_unformat_error, line_input);
2964 /* get the rest of the command */
2965 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2967 if (unformat (line_input, "no"))
2969 else if (unformat (line_input, "prefix %U/%d",
2970 unformat_ip6_address, &ip6_addr, &addr_len))
2975 else if (unformat (line_input, "ra-managed-config-flag"))
2980 else if (unformat (line_input, "ra-other-config-flag"))
2985 else if (unformat (line_input, "ra-suppress") ||
2986 unformat (line_input, "ra-surpress"))
2991 else if (unformat (line_input, "ra-suppress-link-layer") ||
2992 unformat (line_input, "ra-surpress-link-layer"))
2994 suppress_ll_option = 1;
2997 else if (unformat (line_input, "ra-send-unicast"))
3002 else if (unformat (line_input, "ra-lifetime"))
3004 if (!unformat (line_input, "%d", &ra_lifetime))
3006 error = unformat_parse_error (line_input);
3012 else if (unformat (line_input, "ra-initial"))
3015 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
3017 error = unformat_parse_error (line_input);
3022 else if (unformat (line_input, "ra-interval"))
3024 if (!unformat (line_input, "%d", &ra_max_interval))
3026 error = unformat_parse_error (line_input);
3030 if (!unformat (line_input, "%d", &ra_min_interval))
3031 ra_min_interval = 0;
3034 else if (unformat (line_input, "ra-cease"))
3041 error = unformat_parse_error (line_input);
3048 ip6_neighbor_ra_config (vm, sw_if_index,
3049 suppress, managed, other,
3050 suppress_ll_option, send_unicast, cease,
3051 use_lifetime, ra_lifetime,
3052 ra_initial_count, ra_initial_interval,
3053 ra_max_interval, ra_min_interval, is_no);
3057 u32 valid_lifetime_in_secs = 0;
3058 u32 pref_lifetime_in_secs = 0;
3059 u8 use_prefix_default_values = 0;
3060 u8 no_advertise = 0;
3062 u8 no_autoconfig = 0;
3065 /* get the rest of the command */
3066 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3068 if (unformat (line_input, "default"))
3070 use_prefix_default_values = 1;
3073 else if (unformat (line_input, "infinite"))
3075 valid_lifetime_in_secs = ~0;
3076 pref_lifetime_in_secs = ~0;
3079 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3080 &pref_lifetime_in_secs))
3087 /* get the rest of the command */
3088 while (!use_prefix_default_values &&
3089 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3091 if (unformat (line_input, "no-advertise"))
3093 else if (unformat (line_input, "off-link"))
3095 else if (unformat (line_input, "no-autoconfig"))
3097 else if (unformat (line_input, "no-onlink"))
3101 error = unformat_parse_error (line_input);
3106 ip6_neighbor_ra_prefix (vm, sw_if_index,
3107 &ip6_addr, addr_len,
3108 use_prefix_default_values,
3109 valid_lifetime_in_secs,
3110 pref_lifetime_in_secs,
3112 off_link, no_autoconfig, no_onlink, is_no);
3116 unformat_free (line_input);
3122 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3124 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3127 for (i = 0; i < vec_len (addrs); i++)
3129 ip_interface_address_t *a =
3130 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3131 ip6_address_t *address = ip_interface_address_get_address (lm, a);
3133 vlib_cli_output (vm, "\t\t%U/%d",
3134 format_ip6_address, address, a->address_length);
3138 static clib_error_t *
3139 show_ip6_interface_cmd (vlib_main_t * vm,
3140 unformat_input_t * input, vlib_cli_command_t * cmd)
3142 vnet_main_t *vnm = vnet_get_main ();
3143 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3144 clib_error_t *error = 0;
3149 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3153 /* look up the radv_t information for this interface */
3154 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3157 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3161 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3162 ip6_radv_t *radv_info;
3163 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3165 vlib_cli_output (vm, "%U is admin %s\n",
3166 format_vnet_sw_interface_name, vnm,
3167 vnet_get_sw_interface (vnm, sw_if_index),
3168 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3172 u32 *link_scope = 0, *global_scope = 0;
3173 u32 *local_scope = 0, *unknown_scope = 0;
3174 ip_interface_address_t *a;
3176 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3178 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3180 while (ai != (u32) ~ 0)
3182 a = pool_elt_at_index (lm->if_address_pool, ai);
3183 ip6_address_t *address =
3184 ip_interface_address_get_address (lm, a);
3186 if (ip6_address_is_link_local_unicast (address))
3187 vec_add1 (link_scope, ai);
3188 else if (ip6_address_is_global_unicast (address))
3189 vec_add1 (global_scope, ai);
3190 else if (ip6_address_is_local_unicast (address))
3191 vec_add1 (local_scope, ai);
3193 vec_add1 (unknown_scope, ai);
3195 ai = a->next_this_sw_interface;
3198 if (vec_len (link_scope))
3200 vlib_cli_output (vm, "\tLink-local address(es):\n");
3201 ip6_print_addrs (vm, link_scope);
3202 vec_free (link_scope);
3205 if (vec_len (local_scope))
3207 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3208 ip6_print_addrs (vm, local_scope);
3209 vec_free (local_scope);
3212 if (vec_len (global_scope))
3214 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3215 ip6_print_addrs (vm, global_scope);
3216 vec_free (global_scope);
3219 if (vec_len (unknown_scope))
3221 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3222 ip6_print_addrs (vm, unknown_scope);
3223 vec_free (unknown_scope);
3226 vlib_cli_output (vm, "\tJoined group address(es):\n");
3227 ip6_mldp_group_t *m;
3229 pool_foreach (m, radv_info->mldp_group_pool,
3231 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3236 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3237 ip6_radv_prefix_t *p;
3239 pool_foreach (p, radv_info->adv_prefixes_pool,
3241 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3242 format_ip6_address, &p->prefix, p->prefix_len);
3246 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
3247 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3248 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3249 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3250 vlib_cli_output (vm, "\tND DAD is disabled\n");
3251 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3252 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3253 radv_info->adv_neighbor_reachable_time_in_msec);
3254 vlib_cli_output (vm,
3255 "\tND advertised retransmit interval is %d (msec)\n",
3257 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
3259 u32 ra_interval = radv_info->max_radv_interval;
3260 u32 ra_interval_min = radv_info->min_radv_interval;
3261 vlib_cli_output (vm,
3262 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
3263 ra_interval, ra_interval_min);
3264 vlib_cli_output (vm,
3265 "\tND router advertisements live for %d seconds\n",
3266 radv_info->adv_router_lifetime_in_sec);
3267 vlib_cli_output (vm,
3268 "\tHosts %s stateless autoconfig for addresses\n",
3269 (radv_info->adv_managed_flag) ? "use" :
3271 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3272 radv_info->n_advertisements_sent);
3273 vlib_cli_output (vm, "\tND router solicitations received %d\n",
3274 radv_info->n_solicitations_rcvd);
3275 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3276 radv_info->n_solicitations_dropped);
3280 error = clib_error_return (0, "IPv6 not enabled on interface",
3281 format_unformat_error, input);
3289 * This command is used to display various IPv6 attributes on a given
3293 * Example of how to display IPv6 settings:
3294 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3295 * GigabitEthernet2/0/0 is admin up
3296 * Link-local address(es):
3298 * Joined group address(es):
3303 * Advertised Prefixes:
3304 * prefix fe80::fe:28ff:fe9c:75b3, length 64
3306 * ICMP error messages are unlimited
3307 * ICMP redirects are disabled
3308 * ICMP unreachables are not sent
3309 * ND DAD is disabled
3310 * ND advertised reachable time is 0
3311 * ND advertised retransmit interval is 0 (msec)
3312 * ND router advertisements are sent every 200 seconds (min interval is 150)
3313 * ND router advertisements live for 600 seconds
3314 * Hosts use stateless autoconfig for addresses
3315 * ND router advertisements sent 19336
3316 * ND router solicitations received 0
3317 * ND router solicitations dropped 0
3319 * Example of output if IPv6 is not enabled on the interface:
3320 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3321 * show ip6 interface: IPv6 not enabled on interface
3325 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
3327 .path = "show ip6 interface",
3328 .function = show_ip6_interface_cmd,
3329 .short_help = "show ip6 interface <interface>",
3334 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
3336 clib_error_t *error = 0;
3337 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3340 /* look up the radv_t information for this interface */
3341 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3343 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3345 /* if not created - do nothing */
3348 vnet_main_t *vnm = vnet_get_main ();
3349 ip6_radv_t *radv_info;
3351 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3353 /* check radv_info ref count for other ip6 addresses on this interface */
3354 /* This implicitly excludes the link local address */
3355 if (radv_info->ref_count == 0)
3357 /* essentially "disables" ipv6 on this interface */
3358 error = ip6_add_del_interface_address (vm, sw_if_index,
3360 link_local_address, 128,
3363 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3365 ip6_mfib_interface_enable_disable (sw_if_index, 0);
3372 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
3374 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3377 /* look up the radv_t information for this interface */
3378 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3381 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3387 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
3389 clib_error_t *error = 0;
3390 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3394 /* look up the radv_t information for this interface */
3395 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3398 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3400 /* if not created yet */
3403 vnet_main_t *vnm = vnet_get_main ();
3404 vnet_sw_interface_t *sw_if0;
3406 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3407 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3409 ethernet_interface_t *eth_if0;
3412 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
3415 /* create radv_info. for this interface. This holds all the info needed for router adverts */
3417 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
3421 ip6_radv_t *radv_info;
3422 ip6_address_t link_local_address;
3424 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3426 ip6_link_local_address_from_ethernet_mac_address
3427 (&link_local_address, eth_if0->address);
3429 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
3430 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
3431 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
3433 /* make up an interface id */
3437 link_local_address.as_u64[0] = radv_info->randomizer;
3440 md5_add (&m, &link_local_address, 16);
3441 md5_finish (&m, digest);
3443 clib_memcpy (&link_local_address, digest, 16);
3445 radv_info->randomizer = link_local_address.as_u64[0];
3447 link_local_address.as_u64[0] =
3448 clib_host_to_net_u64 (0xFE80000000000000ULL);
3450 link_local_address.as_u8[8] &= 0xfd;
3453 ip6_mfib_interface_enable_disable (sw_if_index, 1);
3455 /* essentially "enables" ipv6 on this interface */
3456 error = ip6_add_del_interface_address (vm, sw_if_index,
3457 &link_local_address,
3459 /* address width */ ,
3463 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3467 radv_info->link_local_address = link_local_address;
3476 static clib_error_t *
3477 enable_ip6_interface_cmd (vlib_main_t * vm,
3478 unformat_input_t * input, vlib_cli_command_t * cmd)
3480 vnet_main_t *vnm = vnet_get_main ();
3481 clib_error_t *error = 0;
3486 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3488 enable_ip6_interface (vm, sw_if_index);
3492 error = clib_error_return (0, "unknown interface\n'",
3493 format_unformat_error, input);
3500 * This command is used to enable IPv6 on a given interface.
3503 * Example of how enable IPv6 on a given interface:
3504 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
3507 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
3509 .path = "enable ip6 interface",
3510 .function = enable_ip6_interface_cmd,
3511 .short_help = "enable ip6 interface <interface>",
3515 static clib_error_t *
3516 disable_ip6_interface_cmd (vlib_main_t * vm,
3517 unformat_input_t * input, vlib_cli_command_t * cmd)
3519 vnet_main_t *vnm = vnet_get_main ();
3520 clib_error_t *error = 0;
3525 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3527 error = disable_ip6_interface (vm, sw_if_index);
3531 error = clib_error_return (0, "unknown interface\n'",
3532 format_unformat_error, input);
3539 * This command is used to disable IPv6 on a given interface.
3542 * Example of how disable IPv6 on a given interface:
3543 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
3546 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
3548 .path = "disable ip6 interface",
3549 .function = disable_ip6_interface_cmd,
3550 .short_help = "disable ip6 interface <interface>",
3555 * This command is used to configure the neighbor discovery
3556 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
3557 * command to display some of the current neighbor discovery parameters
3558 * on a given interface. This command has three formats:
3561 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
3563 * '<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>'
3567 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
3568 * router-advertisement messages to use stateful address
3569 * auto-configuration to obtain address information (sets the M-bit).
3570 * Default is the M-bit is not set and the '<em>no</em>' option
3571 * returns it to this default state.
3573 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
3574 * router-advertisement messages that hosts use stateful auto
3575 * configuration to obtain nonaddress related information (sets
3576 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
3577 * option returns it to this default state.
3579 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
3580 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
3581 * router-advertisement messages.
3583 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
3584 * optional source link-layer address in the ICMPv6 router-advertisement
3585 * messages. Default is to include the optional source link-layer address
3586 * and the '<em>no</em>' option returns it to this default state.
3588 * <em>[no] ra-send-unicast</em> - Use the source address of the
3589 * router-solicitation message if availiable. The default is to use
3590 * multicast address of all nodes, and the '<em>no</em>' option returns
3591 * it to this default state.
3593 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
3594 * default router in ICMPv6 router-advertisement messages. The range is
3595 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
3596 * '<em><max-interval></em>'. The default value is 600 seconds and the
3597 * '<em>no</em>' option returns it to this default value.
3599 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
3600 * router-advertisement messages sent and the interval between each
3601 * message. Range for count is 1 - 3 and default is 3. Range for interval
3602 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
3603 * returns both to their default value.
3605 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
3606 * interval between sending ICMPv6 router-advertisement messages. The
3607 * range for max-interval is from 4 to 200 seconds. min-interval can not
3608 * be more than 75% of max-interval. If not set, min-interval will be
3609 * set to 75% of max-interval. The range for min-interval is from 3 to
3610 * 150 seconds. The '<em>no</em>' option returns both to their default
3613 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
3614 * The '<em>no</em>' options implies to start (or restart) sending
3615 * ICMPv6 router-advertisement messages.
3618 * <b>Format 2 - Prefix Options:</b>
3620 * '<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>'
3624 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
3626 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
3627 * length of time in seconds during what the prefix is valid for the purpose of
3628 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
3629 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
3630 * length of time in seconds during what addresses generated from the prefix remain
3631 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
3633 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
3634 * are inifinte, no timeout.
3636 * <em>no-advertise</em> - Do not send full router address in prefix
3637 * advertisement. Default is to advertise (i.e. - This flag is off by default).
3639 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
3640 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
3641 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
3643 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
3644 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
3646 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
3647 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
3648 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
3652 * <b>Format 3: - Default of Prefix:</b>
3654 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
3656 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
3657 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
3658 * is ignored and the prefix is deleted.
3662 * Example of how set a router advertisement option:
3663 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
3664 * Example of how to add a prefix:
3665 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
3666 * Example of how to delete a prefix:
3667 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
3670 VLIB_CLI_COMMAND (ip6_nd_command, static) =
3673 .short_help = "ip6 nd <interface> ...",
3674 .function = ip6_neighbor_cmd,
3679 set_ip6_link_local_address (vlib_main_t * vm,
3680 u32 sw_if_index, ip6_address_t * address)
3682 clib_error_t *error = 0;
3683 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3685 ip6_radv_t *radv_info;
3686 vnet_main_t *vnm = vnet_get_main ();
3688 if (!ip6_address_is_link_local_unicast (address))
3690 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3691 return (error = clib_error_return (0, "address not link-local",
3692 format_unformat_error));
3695 /* call enable ipv6 */
3696 enable_ip6_interface (vm, sw_if_index);
3698 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3702 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3704 /* save if link local address (overwrite default) */
3706 /* delete the old one */
3707 error = ip6_add_del_interface_address (vm, sw_if_index,
3708 &radv_info->link_local_address,
3709 128, 1 /* is_del */ );
3713 /* add the new one */
3714 error = ip6_add_del_interface_address (vm, sw_if_index,
3720 radv_info->link_local_address = *address;
3726 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3727 error = clib_error_return (0, "ip6 not enabled for interface",
3728 format_unformat_error);
3734 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3735 unformat_input_t * input,
3736 vlib_cli_command_t * cmd)
3738 vnet_main_t *vnm = vnet_get_main ();
3739 clib_error_t *error = 0;
3741 ip6_address_t ip6_addr;
3743 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3745 /* get the rest of the command */
3746 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3748 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
3751 return (unformat_parse_error (input));
3754 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
3759 * This command is used to assign an IPv6 Link-local address to an
3760 * interface. This command will enable IPv6 on an interface if it
3761 * is not already enabled. Use the '<em>show ip6 interface</em>' command
3762 * to display the assigned Link-local address.
3765 * Example of how to assign an IPv6 Link-local address to an interface:
3766 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
3769 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
3771 .path = "set ip6 link-local address",
3772 .short_help = "set ip6 link-local address <interface> <ip6-address>",
3773 .function = set_ip6_link_local_address_cmd,
3778 * @brief callback when an interface address is added or deleted
3781 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3784 ip6_address_t * address,
3786 u32 if_address_index, u32 is_delete)
3788 vnet_main_t *vnm = vnet_get_main ();
3789 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3791 vlib_main_t *vm = vnm->vlib_main;
3792 ip6_radv_t *radv_info;
3795 /* create solicited node multicast address for this interface adddress */
3796 ip6_set_solicited_node_multicast_address (&a, 0);
3798 a.as_u8[0xd] = address->as_u8[0xd];
3799 a.as_u8[0xe] = address->as_u8[0xe];
3800 a.as_u8[0xf] = address->as_u8[0xf];
3804 /* try to create radv_info - does nothing if ipv6 already enabled */
3805 enable_ip6_interface (vm, sw_if_index);
3807 /* look up the radv_t information for this interface */
3808 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3810 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3814 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3817 if (!ip6_address_is_link_local_unicast (address))
3818 radv_info->ref_count++;
3820 ip6_neighbor_add_mld_prefix (radv_info, &a);
3827 /* look up the radv_t information for this interface */
3828 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3830 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3835 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3837 ip6_neighbor_del_mld_prefix (radv_info, &a);
3839 /* if interface up send MLDP "report" */
3840 radv_info->all_routers_mcast = 0;
3843 if (!ip6_address_is_link_local_unicast (address))
3844 radv_info->ref_count--;
3846 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
3847 disable_ip6_interface (vm, sw_if_index);
3852 ip6_set_neighbor_limit (u32 neighbor_limit)
3854 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3856 nm->limit_neighbor_cache_size = neighbor_limit;
3861 ip6_neighbor_table_bind (ip6_main_t * im,
3864 u32 new_fib_index, u32 old_fib_index)
3866 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3867 ip6_neighbor_t *n = NULL;
3868 u32 i, *to_re_add = 0;
3871 pool_foreach (n, nm->neighbor_pool,
3873 if (n->key.sw_if_index == sw_if_index)
3874 vec_add1 (to_re_add, n - nm->neighbor_pool);
3878 for (i = 0; i < vec_len (to_re_add); i++)
3880 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
3881 ip6_neighbor_adj_fib_remove (n, old_fib_index);
3882 ip6_neighbor_adj_fib_add (n, new_fib_index);
3884 vec_free (to_re_add);
3887 static clib_error_t *
3888 ip6_neighbor_init (vlib_main_t * vm)
3890 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3891 ip6_main_t *im = &ip6_main;
3893 mhash_init (&nm->neighbor_index_by_key,
3894 /* value size */ sizeof (uword),
3895 /* key size */ sizeof (ip6_neighbor_key_t));
3897 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
3898 ip6_icmp_neighbor_solicitation_node.index);
3899 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
3900 ip6_icmp_neighbor_advertisement_node.index);
3901 icmp6_register_type (vm, ICMP6_router_solicitation,
3902 ip6_icmp_router_solicitation_node.index);
3903 icmp6_register_type (vm, ICMP6_router_advertisement,
3904 ip6_icmp_router_advertisement_node.index);
3906 /* handler node for ip6 neighbor discovery events and timers */
3907 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3909 /* add call backs */
3910 ip6_add_del_interface_address_callback_t cb;
3911 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
3913 /* when an interface address changes... */
3914 cb.function = ip6_neighbor_add_del_interface_address;
3915 cb.function_opaque = 0;
3916 vec_add1 (im->add_del_interface_address_callbacks, cb);
3918 ip6_table_bind_callback_t cbt;
3919 cbt.function = ip6_neighbor_table_bind;
3920 cbt.function_opaque = 0;
3921 vec_add1 (im->table_bind_callbacks, cbt);
3923 mhash_init (&nm->pending_resolutions_by_address,
3924 /* value size */ sizeof (uword),
3925 /* key size */ sizeof (ip6_address_t));
3927 mhash_init (&nm->mac_changes_by_address,
3928 /* value size */ sizeof (uword),
3929 /* key size */ sizeof (ip6_address_t));
3931 /* default, configurable */
3932 nm->limit_neighbor_cache_size = 50000;
3935 /* $$$$ Hack fix for today */
3936 vec_validate_init_empty
3937 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
3943 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3947 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3950 uword type_opaque, uword data)
3952 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3953 ip6_address_t *address = address_arg;
3955 pending_resolution_t *pr;
3957 pool_get (nm->pending_resolutions, pr);
3959 pr->next_index = ~0;
3960 pr->node_index = node_index;
3961 pr->type_opaque = type_opaque;
3964 p = mhash_get (&nm->pending_resolutions_by_address, address);
3967 /* Insert new resolution at the head of the list */
3968 pr->next_index = p[0];
3969 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3972 mhash_set (&nm->pending_resolutions_by_address, address,
3973 pr - nm->pending_resolutions, 0 /* old value */ );
3977 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3978 void *data_callback,
3982 uword type_opaque, uword data, int is_add)
3984 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3985 ip6_address_t *address = address_arg;
3987 /* Try to find an existing entry */
3988 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
3990 pending_resolution_t *mc;
3991 while (p && *p != ~0)
3993 mc = pool_elt_at_index (nm->mac_changes, *p);
3994 if (mc->node_index == node_index && mc->type_opaque == type_opaque
3997 p = &mc->next_index;
4000 int found = p && *p != ~0;
4004 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4006 pool_get (nm->mac_changes, mc);
4007 *mc = (pending_resolution_t)
4009 .next_index = ~0,.node_index = node_index,.type_opaque =
4010 type_opaque,.data = data,.data_callback = data_callback,.pid =
4013 /* Insert new resolution at the end of the list */
4014 u32 new_idx = mc - nm->mac_changes;
4018 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
4023 return VNET_API_ERROR_NO_SUCH_ENTRY;
4025 /* Clients may need to clean up pool entries, too */
4026 void (*fp) (u32, u8 *) = data_callback;
4028 (*fp) (mc->data, 0 /* no new mac addrs */ );
4030 /* Remove the entry from the list and delete the entry */
4031 *p = mc->next_index;
4032 pool_put (nm->mac_changes, mc);
4034 /* Remove from hash if we deleted the last entry */
4035 if (*p == ~0 && p == first)
4036 mhash_unset (&nm->mac_changes_by_address, address, 0);
4042 vnet_ip6_nd_term (vlib_main_t * vm,
4043 vlib_node_runtime_t * node,
4045 ethernet_header_t * eth,
4046 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
4048 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4049 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4050 pending_resolution_t *mc;
4052 ndh = ip6_next_header (ip);
4053 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4054 ndh->icmp.type != ICMP6_neighbor_advertisement)
4057 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4058 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4060 u8 *t0 = vlib_add_trace (vm, node, p0,
4061 sizeof (icmp6_input_trace_t));
4062 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4065 /* Check if anyone want ND events for L2 BDs */
4066 uword *p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
4067 if (p && !ip6_address_is_link_local_unicast (&ip->src_address))
4069 u32 next_index = p[0];
4070 while (next_index != (u32) ~ 0)
4072 int (*fp) (u32, u8 *, u32, ip6_address_t *);
4074 mc = pool_elt_at_index (nm->mac_changes, next_index);
4075 fp = mc->data_callback;
4076 /* Call the callback, return 1 to suppress dup events */
4078 rv = (*fp) (mc->data,
4079 eth->src_address, sw_if_index, &ip->src_address);
4080 /* Signal the resolver process */
4082 vlib_process_signal_event (vm, mc->node_index,
4083 mc->type_opaque, mc->data);
4084 next_index = mc->next_index;
4088 /* Check if MAC entry exsist for solicited target IP */
4089 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4091 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4092 l2_bridge_domain_t *bd_config;
4095 opt = (void *) (ndh + 1);
4096 if ((opt->header.type !=
4097 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4098 (opt->header.n_data_u64s != 1))
4099 return 0; /* source link layer address option not present */
4101 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4103 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4105 { /* found ip-mac entry, generate eighbor advertisement response */
4107 vlib_node_runtime_t *error_node =
4108 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4109 ip->dst_address = ip->src_address;
4110 ip->src_address = ndh->target_address;
4111 ip->hop_limit = 255;
4113 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4114 clib_memcpy (opt->ethernet_address, macp, 6);
4115 ndh->icmp.type = ICMP6_neighbor_advertisement;
4116 ndh->advertisement_flags = clib_host_to_net_u32
4117 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4118 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4119 ndh->icmp.checksum = 0;
4120 ndh->icmp.checksum =
4121 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4122 clib_memcpy (eth->dst_address, eth->src_address, 6);
4123 clib_memcpy (eth->src_address, macp, 6);
4124 vlib_error_count (vm, error_node->node_index,
4125 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4135 ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4139 fib_prefix_t pfx = {
4141 .fp_proto = FIB_PROTOCOL_IP6,
4146 ip46_address_t nh = {
4150 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4152 if (~0 == fib_index)
4153 return VNET_API_ERROR_NO_SUCH_FIB;
4157 fib_table_entry_path_remove (fib_index,
4159 FIB_SOURCE_IP6_ND_PROXY,
4163 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4164 /* flush the ND cache of this address if it's there */
4165 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4166 sw_if_index, addr, NULL, 0);
4170 fib_table_entry_path_add (fib_index,
4172 FIB_SOURCE_IP6_ND_PROXY,
4173 FIB_ENTRY_FLAG_NONE,
4177 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4182 static clib_error_t *
4183 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4184 unformat_input_t * input, vlib_cli_command_t * cmd)
4186 vnet_main_t *vnm = vnet_get_main ();
4187 clib_error_t *error = 0;
4192 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4194 /* get the rest of the command */
4195 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4197 if (unformat (input, "%U", unformat_ip6_address, &addr))
4199 else if (unformat (input, "delete") || unformat (input, "del"))
4202 return (unformat_parse_error (input));
4206 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4212 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4214 .path = "set ip6 nd proxy",
4215 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4216 .function = set_ip6_nd_proxy_cmd,
4221 ethernet_ndp_change_mac (u32 sw_if_index)
4223 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4227 pool_foreach (n, nm->neighbor_pool,
4229 if (n->key.sw_if_index == sw_if_index)
4231 adj_nbr_walk_nh6 (sw_if_index,
4232 &n->key.ip6_address,
4233 ip6_nd_mk_complete_walk, n);
4240 send_ip6_na (vlib_main_t * vm, vnet_hw_interface_t * hi)
4242 ip6_main_t *i6m = &ip6_main;
4243 u32 sw_if_index = hi->sw_if_index;
4244 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4248 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4249 format_ip6_address, ip6_addr, sw_if_index);
4251 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4254 icmp6_neighbor_solicitation_header_t *h =
4255 vlib_packet_template_get_packet (vm,
4256 &i6m->discover_neighbor_packet_template,
4258 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4259 IP6_MULTICAST_SCOPE_link_local,
4260 IP6_MULTICAST_GROUP_ID_all_hosts);
4261 h->ip.src_address = ip6_addr[0];
4262 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4263 h->neighbor.target_address = ip6_addr[0];
4264 h->neighbor.advertisement_flags = clib_host_to_net_u32
4265 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4266 clib_memcpy (h->link_layer_option.ethernet_address,
4267 hi->hw_address, vec_len (hi->hw_address));
4268 h->neighbor.icmp.checksum =
4269 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4270 ASSERT (bogus_length == 0);
4272 /* Setup MAC header with IP6 Etype and mcast DMAC */
4273 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
4274 vlib_buffer_advance (b, -sizeof (ethernet_header_t));
4275 ethernet_header_t *e = vlib_buffer_get_current (b);
4276 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_IP6);
4277 clib_memcpy (e->src_address, hi->hw_address, sizeof (e->src_address));
4278 ip6_multicast_ethernet_address (e->dst_address,
4279 IP6_MULTICAST_GROUP_ID_all_hosts);
4281 /* Send unsolicited ND advertisement packet out the specified interface */
4282 vnet_buffer (b)->sw_if_index[VLIB_RX] =
4283 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
4284 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
4285 u32 *to_next = vlib_frame_vector_args (f);
4288 vlib_put_frame_to_node (vm, hi->output_node_index, f);
4293 * fd.io coding-style-patch-verification: ON
4296 * eval: (c-set-style "gnu")