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/ip6-nd/ip6_ra.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ip-neighbor/ip_neighbor_dp.h>
23 #include <vnet/fib/ip6_fib.h>
24 #include <vnet/ip/ip6_link.h>
28 * @brief IPv6 Router Advertisements.
30 * The files contains the API and CLI code for managing IPv6 RAs
34 /* Router solicitation packet format for ethernet. */
35 typedef CLIB_PACKED (struct
38 icmp6_neighbor_discovery_header_t neighbor;
39 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
41 }) icmp6_router_solicitation_header_t;
43 /* router advertisement packet format for ethernet. */
44 typedef CLIB_PACKED (struct
47 icmp6_router_advertisement_header_t router;
48 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
50 icmp6_neighbor_discovery_mtu_option_t mtu_option;
51 icmp6_neighbor_discovery_prefix_information_option_t
53 }) icmp6_router_advertisement_packet_t;
56 #define DEF_MAX_RADV_INTERVAL 200
57 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
58 #define DEF_CURR_HOP_LIMIT 64
59 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
60 #define MAX_DEF_RTR_LIFETIME 9000
62 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
63 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
64 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
65 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
66 #define MAX_RA_DELAY_TIME .5 /* seconds */
68 /* advertised prefix option */
71 /* basic advertised information */
75 int adv_autonomous_flag;
76 u32 adv_valid_lifetime_in_secs;
77 u32 adv_pref_lifetime_in_secs;
79 /* advertised values are computed from these times if decrementing */
80 f64 valid_lifetime_expires;
81 f64 pref_lifetime_expires;
83 /* local information */
85 int deprecated_prefix_flag;
86 int decrement_lifetime_flag;
88 #define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
89 #define DEF_ADV_VALID_LIFETIME 2592000
90 #define DEF_ADV_PREF_LIFETIME 604800
92 /* extensions are added here, mobile, DNS etc.. */
95 typedef struct ip6_ra_t_
97 /* advertised config information, zero means unspecified */
101 u16 adv_router_lifetime_in_sec;
102 u32 adv_neighbor_reachable_time_in_msec;
103 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
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;
119 ip6_radv_prefix_t *adv_prefixes_pool;
121 /* Hash table mapping address to index in interface advertised prefix pool. */
122 mhash_t address_to_prefix_index;
124 f64 max_radv_interval;
125 f64 min_radv_interval;
126 f64 min_delay_between_radv;
127 f64 max_delay_between_radv;
128 f64 max_rtr_default_lifetime;
131 f64 last_multicast_time;
132 f64 next_multicast_time;
135 u32 initial_adverts_count;
136 f64 initial_adverts_interval;
137 u32 initial_adverts_sent;
140 u32 n_advertisements_sent;
141 u32 n_solicitations_rcvd;
142 u32 n_solicitations_dropped;
144 /* router solicitations sending state */
145 u8 keep_sending_rs; /* when true then next fields are valid */
146 icmp6_send_router_solicitation_params_t params;
151 vlib_buffer_t *buffer;
157 static ip6_link_delegate_id_t ip6_ra_delegate_id;
158 static ip6_ra_t *ip6_ra_pool;
161 /* vector of registered RA report listeners */
162 static ip6_ra_report_notify_t *ip6_ra_listeners;
164 static int ip6_ra_publish (ip6_ra_report_t * r);
167 ip6_ra_report_register (ip6_ra_report_notify_t fn)
169 ip6_ra_report_notify_t *listener;
170 vec_foreach (listener, ip6_ra_listeners)
176 vec_add1 (ip6_ra_listeners, fn);
180 ip6_ra_report_unregister (ip6_ra_report_notify_t fn)
184 vec_foreach_index (ii, ip6_ra_listeners)
186 if (ip6_ra_listeners[ii] == fn)
188 vec_del1 (ip6_ra_listeners, ii);
194 static inline ip6_ra_t *
195 ip6_ra_get_itf (u32 sw_if_index)
199 rai = ip6_link_delegate_get (sw_if_index, ip6_ra_delegate_id);
201 if (INDEX_INVALID != rai)
202 return (pool_elt_at_index (ip6_ra_pool, rai));
207 /* for "syslogging" - use elog for now */
208 #define foreach_log_level \
210 _ (INFO, "INFORMATION") \
211 _ (NOTICE, "NOTICE") \
212 _ (WARNING, "WARNING") \
214 _ (CRIT, "CRITICAL") \
216 _ (EMERG, "EMERGENCY")
220 #define _(f,s) LOG_##f,
225 static char *log_level_strings[] = {
231 static int logmask = 1 << LOG_DEBUG;
234 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
236 /* just use elog for now */
240 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
246 what = va_format (0, fmt, &va);
248 ELOG_TYPE_DECLARE (e) =
250 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
255 ed = ELOG_DATA (&vm->elog_main, e);
256 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
257 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
263 /* ipv6 neighbor discovery - router advertisements */
266 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
267 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
268 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
269 ICMP6_ROUTER_SOLICITATION_N_NEXT,
270 } icmp6_router_solicitation_or_advertisement_next_t;
272 static_always_inline uword
273 icmp6_router_solicitation (vlib_main_t * vm,
274 vlib_node_runtime_t * node, vlib_frame_t * frame)
276 vnet_main_t *vnm = vnet_get_main ();
277 ip6_main_t *im = &ip6_main;
278 uword n_packets = frame->n_vectors;
280 u32 n_left_from, n_left_to_next, next_index;
281 u32 n_advertisements_sent = 0;
284 icmp6_neighbor_discovery_option_type_t option_type;
286 vlib_node_runtime_t *error_node =
287 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
289 from = vlib_frame_vector_args (frame);
290 n_left_from = n_packets;
291 next_index = node->cached_next_index;
293 if (node->flags & VLIB_NODE_FLAG_TRACE)
294 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
296 sizeof (icmp6_input_trace_t));
298 /* source may append his LL address */
299 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
301 while (n_left_from > 0)
303 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
305 while (n_left_from > 0 && n_left_to_next > 0)
309 ip6_ra_t *radv_info = NULL;
311 icmp6_neighbor_discovery_header_t *h0;
312 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
314 u32 bi0, options_len0, sw_if_index0, next0, error0;
315 u32 is_solicitation = 1, is_dropped = 0;
316 u32 is_unspecified, is_link_local;
318 bi0 = to_next[0] = from[0];
325 p0 = vlib_get_buffer (vm, bi0);
326 ip0 = vlib_buffer_get_current (p0);
327 h0 = ip6_next_header (ip0);
329 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
330 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
332 ip6_address_is_link_local_unicast (&ip0->src_address);
334 error0 = ICMP6_ERROR_NONE;
335 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
337 /* check if solicitation (not from nd_timer node) */
338 if (ip6_address_is_unspecified (&ip0->dst_address))
341 /* Check that source address is unspecified, link-local or else on-link. */
342 if (!is_unspecified && !is_link_local)
344 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
346 if (ADJ_INDEX_INVALID != src_adj_index0)
348 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
350 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
352 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
357 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
361 /* check for source LL option and process */
362 o0 = (void *) (h0 + 1);
363 o0 = ((options_len0 == 8
364 && o0->header.type == option_type
365 && o0->header.n_data_u64s == 1) ? o0 : 0);
367 /* if src address unspecified IGNORE any options */
368 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
369 !is_unspecified && !is_link_local))
371 ip_neighbor_learn_t learn = {
372 .type = IP46_TYPE_IP6,
373 .sw_if_index = sw_if_index0,
374 .ip.ip6 = ip0->src_address,
376 memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac));
377 ip_neighbor_learn_dp (&learn);
380 /* default is to drop */
381 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
383 if (error0 == ICMP6_ERROR_NONE)
385 vnet_sw_interface_t *sw_if0;
386 ethernet_interface_t *eth_if0;
389 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
390 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
392 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
394 /* only support ethernet interface type for now */
396 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
399 if (error0 == ICMP6_ERROR_NONE)
401 /* adjust the sizeof the buffer to just include the ipv6 header */
402 p0->current_length -=
404 sizeof (icmp6_neighbor_discovery_header_t));
406 radv_info = ip6_ra_get_itf (sw_if_index0);
408 error0 = ((!radv_info) ?
409 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
412 if (error0 == ICMP6_ERROR_NONE)
414 f64 now = vlib_time_now (vm);
416 /* for solicited adverts - need to rate limit */
419 if (0 != radv_info->last_radv_time &&
420 (now - radv_info->last_radv_time) <
421 MIN_DELAY_BETWEEN_RAS)
424 radv_info->last_radv_time = now;
428 icmp6_router_advertisement_header_t rh;
430 rh.icmp.type = ICMP6_router_advertisement;
432 rh.icmp.checksum = 0;
434 rh.current_hop_limit = radv_info->curr_hop_limit;
435 rh.router_lifetime_in_sec =
437 (radv_info->adv_router_lifetime_in_sec);
439 time_in_msec_between_retransmitted_neighbor_solicitations
441 clib_host_to_net_u32 (radv_info->
442 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
443 rh.neighbor_reachable_time_in_msec =
444 clib_host_to_net_u32 (radv_info->
445 adv_neighbor_reachable_time_in_msec);
448 (radv_info->adv_managed_flag) ?
449 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
452 ((radv_info->adv_other_flag) ?
453 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
458 sizeof (icmp6_router_advertisement_header_t);
460 if (vlib_buffer_add_data
461 (vm, &bi0, (void *) &rh,
462 sizeof (icmp6_router_advertisement_header_t)))
464 /* buffer allocation failed, drop the pkt */
465 error0 = ICMP6_ERROR_ALLOC_FAILURE;
469 if (radv_info->adv_link_layer_address)
471 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
475 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
476 h.header.n_data_u64s = 1;
478 /* copy ll address */
479 clib_memcpy (&h.ethernet_address[0],
480 eth_if0->address, 6);
482 if (vlib_buffer_add_data
483 (vm, &bi0, (void *) &h,
485 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
487 error0 = ICMP6_ERROR_ALLOC_FAILURE;
493 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
497 if (radv_info->adv_link_mtu)
499 icmp6_neighbor_discovery_mtu_option_t h;
503 clib_host_to_net_u32 (radv_info->adv_link_mtu);
504 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
505 h.header.n_data_u64s = 1;
508 sizeof (icmp6_neighbor_discovery_mtu_option_t);
510 if (vlib_buffer_add_data
511 (vm, &bi0, (void *) &h,
513 (icmp6_neighbor_discovery_mtu_option_t)))
515 error0 = ICMP6_ERROR_ALLOC_FAILURE;
520 /* add advertised prefix options */
521 ip6_radv_prefix_t *pr_info;
524 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
526 if(pr_info->enabled &&
527 (!pr_info->decrement_lifetime_flag
528 || (pr_info->pref_lifetime_expires >0)))
530 /* advertise this prefix */
531 icmp6_neighbor_discovery_prefix_information_option_t h;
533 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
534 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
536 h.dst_address_length = pr_info->prefix_len;
538 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
539 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
541 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
543 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
544 h.preferred_time = 0;
548 if(pr_info->decrement_lifetime_flag)
550 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
551 (pr_info->valid_lifetime_expires - now) : 0;
553 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
554 (pr_info->pref_lifetime_expires - now) : 0;
557 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
558 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
562 /* Handy for debugging, but too noisy... */
563 if (0 && CLIB_DEBUG > 0)
565 ("send RA for prefix %U/%d "
566 "sw_if_index %d valid %u preferred %u",
567 format_ip6_address, &pr_info->prefix,
568 pr_info->prefix_len, sw_if_index0,
569 clib_net_to_host_u32 (h.valid_time),
570 clib_net_to_host_u32 (h.preferred_time));
572 if (h.valid_time == 0)
573 clib_warning ("BUG: send RA with valid_time 0");
575 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
577 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
579 if (vlib_buffer_add_data
580 (vm, &bi0, (void *)&h,
581 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
583 error0 = ICMP6_ERROR_ALLOC_FAILURE;
591 /* add additional options before here */
593 /* finish building the router advertisement... */
594 if (!is_unspecified && radv_info->send_unicast)
596 ip0->dst_address = ip0->src_address;
600 /* target address is all-nodes mcast addr */
601 ip6_set_reserved_multicast_address
603 IP6_MULTICAST_SCOPE_link_local,
604 IP6_MULTICAST_GROUP_ID_all_hosts);
607 /* source address MUST be the link-local address */
608 ip6_address_copy (&ip0->src_address,
609 ip6_get_link_local_address
610 (radv_info->sw_if_index));
612 ip0->hop_limit = 255;
613 ip0->payload_length =
614 clib_host_to_net_u16 (payload_length);
616 icmp6_router_advertisement_header_t *rh0 =
617 (icmp6_router_advertisement_header_t *) (ip0 + 1);
619 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
621 ASSERT (bogus_length == 0);
623 /* setup output if and adjacency */
624 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
625 vnet_main.local_interface_sw_if_index;
629 ethernet_header_t *eth0;
630 /* Reuse current MAC header, copy SMAC to DMAC and
631 * interface MAC to SMAC */
632 vlib_buffer_reset (p0);
633 eth0 = vlib_buffer_get_current (p0);
634 clib_memcpy (eth0->dst_address, eth0->src_address,
636 clib_memcpy (eth0->src_address, eth_if0->address,
640 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
641 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
646 adj_index0 = ip6_link_get_mcast_adj (sw_if_index0);
647 if (adj_index0 == INDEX_INVALID)
648 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
653 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
654 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
658 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
660 radv_info->n_solicitations_dropped += is_dropped;
661 radv_info->n_solicitations_rcvd += is_solicitation;
663 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
665 radv_info->n_advertisements_sent++;
666 n_advertisements_sent++;
673 p0->error = error_node->errors[error0];
675 if (error0 != ICMP6_ERROR_NONE)
676 vlib_error_count (vm, error_node->node_index, error0, 1);
678 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
679 to_next, n_left_to_next,
684 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
687 /* Account for router advertisements sent. */
688 vlib_error_count (vm, error_node->node_index,
689 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
690 n_advertisements_sent);
692 return frame->n_vectors;
696 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
698 .function = icmp6_router_solicitation,
699 .name = "icmp6-router-solicitation",
701 .vector_size = sizeof (u32),
703 .format_trace = format_icmp6_input_trace,
705 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
707 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
708 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
709 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
714 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
715 static_always_inline uword
716 icmp6_router_advertisement (vlib_main_t * vm,
717 vlib_node_runtime_t * node, vlib_frame_t * frame)
719 vnet_main_t *vnm = vnet_get_main ();
720 uword n_packets = frame->n_vectors;
722 u32 n_left_from, n_left_to_next, next_index;
723 u32 n_advertisements_rcvd = 0;
725 vlib_node_runtime_t *error_node =
726 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
728 from = vlib_frame_vector_args (frame);
729 n_left_from = n_packets;
730 next_index = node->cached_next_index;
732 if (node->flags & VLIB_NODE_FLAG_TRACE)
733 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
735 sizeof (icmp6_input_trace_t));
737 while (n_left_from > 0)
739 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
741 while (n_left_from > 0 && n_left_to_next > 0)
745 ip6_ra_t *radv_info = 0;
746 icmp6_router_advertisement_header_t *h0;
747 u32 bi0, options_len0, sw_if_index0, next0, error0;
749 bi0 = to_next[0] = from[0];
756 p0 = vlib_get_buffer (vm, bi0);
757 ip0 = vlib_buffer_get_current (p0);
758 h0 = ip6_next_header (ip0);
760 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
762 error0 = ICMP6_ERROR_NONE;
763 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
765 /* Check that source address is link-local */
766 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
767 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
769 /* default is to drop */
770 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
772 n_advertisements_rcvd++;
774 if (error0 == ICMP6_ERROR_NONE)
776 vnet_sw_interface_t *sw_if0;
777 ethernet_interface_t *eth_if0;
779 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
780 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
782 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
784 /* only support ethernet interface type for now */
786 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
789 if (error0 == ICMP6_ERROR_NONE)
791 /* look up the radv_t information for this interface */
792 radv_info = ip6_ra_get_itf (sw_if_index0);
794 error0 = ((!radv_info) ?
795 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
798 if (error0 == ICMP6_ERROR_NONE)
800 radv_info->keep_sending_rs = 0;
804 r.sw_if_index = sw_if_index0;
805 memcpy (&r.router_address, &ip0->src_address, 16);
806 r.current_hop_limit = h0->current_hop_limit;
808 r.router_lifetime_in_sec =
809 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
810 r.neighbor_reachable_time_in_msec =
812 (h0->neighbor_reachable_time_in_msec);
813 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
816 /* validate advertised information */
817 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
818 && (h0->current_hop_limit !=
819 radv_info->curr_hop_limit))
821 ip6_neighbor_syslog (vm, LOG_WARNING,
822 "our AdvCurHopLimit on %U doesn't agree with %U",
823 format_vnet_sw_if_index_name,
830 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
831 != radv_info->adv_managed_flag)
833 ip6_neighbor_syslog (vm, LOG_WARNING,
834 "our AdvManagedFlag on %U doesn't agree with %U",
835 format_vnet_sw_if_index_name,
842 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
843 != radv_info->adv_other_flag)
845 ip6_neighbor_syslog (vm, LOG_WARNING,
846 "our AdvOtherConfigFlag on %U doesn't agree with %U",
847 format_vnet_sw_if_index_name,
854 time_in_msec_between_retransmitted_neighbor_solicitations
856 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
858 time_in_msec_between_retransmitted_neighbor_solicitations
860 clib_host_to_net_u32 (radv_info->
861 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
863 ip6_neighbor_syslog (vm, LOG_WARNING,
864 "our AdvRetransTimer on %U doesn't agree with %U",
865 format_vnet_sw_if_index_name,
871 if ((h0->neighbor_reachable_time_in_msec &&
872 radv_info->adv_neighbor_reachable_time_in_msec) &&
873 (h0->neighbor_reachable_time_in_msec !=
875 (radv_info->adv_neighbor_reachable_time_in_msec)))
877 ip6_neighbor_syslog (vm, LOG_WARNING,
878 "our AdvReachableTime on %U doesn't agree with %U",
879 format_vnet_sw_if_index_name,
885 /* check for MTU or prefix options or .. */
886 u8 *opt_hdr = (u8 *) (h0 + 1);
887 while (options_len0 > 0)
889 icmp6_neighbor_discovery_option_header_t *o0 =
890 (icmp6_neighbor_discovery_option_header_t *)
892 int opt_len = o0->n_data_u64s << 3;
893 icmp6_neighbor_discovery_option_type_t option_type =
896 if (options_len0 < 2)
898 ip6_neighbor_syslog (vm, LOG_ERR,
899 "malformed RA packet on %U from %U",
900 format_vnet_sw_if_index_name,
909 ip6_neighbor_syslog (vm, LOG_ERR,
910 " zero length option in RA on %U from %U",
911 format_vnet_sw_if_index_name,
917 else if (opt_len > options_len0)
919 ip6_neighbor_syslog (vm, LOG_ERR,
920 "option length in RA packet greater than total length on %U from %U",
921 format_vnet_sw_if_index_name,
928 options_len0 -= opt_len;
933 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
935 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
937 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
940 if (opt_len < sizeof (*h))
943 memcpy (r.slla, h->ethernet_address, 6);
947 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
949 icmp6_neighbor_discovery_mtu_option_t *h =
950 (icmp6_neighbor_discovery_mtu_option_t
953 if (opt_len < sizeof (*h))
956 r.mtu = clib_net_to_host_u32 (h->mtu);
958 if ((h->mtu && radv_info->adv_link_mtu) &&
961 (radv_info->adv_link_mtu)))
963 ip6_neighbor_syslog (vm, LOG_WARNING,
964 "our AdvLinkMTU on %U doesn't agree with %U",
965 format_vnet_sw_if_index_name,
973 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
975 icmp6_neighbor_discovery_prefix_information_option_t
977 (icmp6_neighbor_discovery_prefix_information_option_t
980 /* validate advertised prefix options */
981 ip6_radv_prefix_t *pr_info;
982 u32 preferred, valid;
984 if (opt_len < sizeof (*h))
987 vec_validate (r.prefixes,
988 vec_len (r.prefixes));
989 ra_report_prefix_info_t *prefix =
990 vec_elt_at_index (r.prefixes,
991 vec_len (r.prefixes) - 1);
994 clib_net_to_host_u32 (h->preferred_time);
995 valid = clib_net_to_host_u32 (h->valid_time);
997 prefix->preferred_time = preferred;
998 prefix->valid_time = valid;
999 prefix->flags = h->flags & 0xc0;
1000 prefix->prefix.fp_len = h->dst_address_length;
1001 prefix->prefix.fp_addr.ip6 = h->dst_address;
1002 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
1004 /* look for matching prefix - if we our advertising it, it better be consistant */
1006 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1010 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1012 if(pr_info->enabled &&
1013 (pr_info->prefix_len == h->dst_address_length) &&
1014 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1017 if(!pr_info->decrement_lifetime_flag &&
1018 valid != pr_info->adv_valid_lifetime_in_secs)
1020 ip6_neighbor_syslog(vm, LOG_WARNING,
1021 "our ADV validlifetime on %U for %U does not agree with %U",
1022 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1023 format_ip6_address, &h->dst_address);
1025 if(!pr_info->decrement_lifetime_flag &&
1026 preferred != pr_info->adv_pref_lifetime_in_secs)
1028 ip6_neighbor_syslog(vm, LOG_WARNING,
1029 "our ADV preferredlifetime on %U for %U does not agree with %U",
1030 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1031 format_ip6_address, &h->dst_address);
1044 ip6_ra_publish (&r);
1049 p0->error = error_node->errors[error0];
1051 if (error0 != ICMP6_ERROR_NONE)
1052 vlib_error_count (vm, error_node->node_index, error0, 1);
1054 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1055 to_next, n_left_to_next,
1059 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1062 /* Account for router advertisements received. */
1063 vlib_error_count (vm, error_node->node_index,
1064 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
1065 n_advertisements_rcvd);
1067 return frame->n_vectors;
1071 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
1073 .function = icmp6_router_advertisement,
1074 .name = "icmp6-router-advertisement",
1076 .vector_size = sizeof (u32),
1078 .format_trace = format_icmp6_input_trace,
1088 random_f64_from_to (f64 from, f64 to)
1090 static u32 seed = 0;
1091 static u8 seed_set = 0;
1094 seed = random_default_seed ();
1097 return random_f64 (&seed) * (to - from) + from;
1101 get_mac_address (u32 sw_if_index, u8 * address)
1103 vnet_main_t *vnm = vnet_get_main ();
1104 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1105 if (!hw_if->hw_address)
1107 clib_memcpy (address, hw_if->hw_address, 6);
1111 static inline vlib_buffer_t *
1112 create_buffer_for_rs (vlib_main_t * vm, ip6_ra_t * radv_info)
1116 icmp6_router_solicitation_header_t *rh;
1121 sw_if_index = radv_info->sw_if_index;
1123 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
1125 clib_warning ("buffer allocation failure");
1129 p0 = vlib_get_buffer (vm, bi0);
1130 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
1131 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1133 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
1134 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
1136 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1137 ip6_link_get_mcast_adj (sw_if_index);
1139 rh = vlib_buffer_get_current (p0);
1140 p0->current_length = sizeof (*rh);
1142 rh->neighbor.icmp.type = ICMP6_router_solicitation;
1143 rh->neighbor.icmp.code = 0;
1144 rh->neighbor.icmp.checksum = 0;
1145 rh->neighbor.reserved_must_be_zero = 0;
1147 rh->link_layer_option.header.type =
1148 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1149 if (0 != get_mac_address (sw_if_index,
1150 rh->link_layer_option.ethernet_address))
1152 clib_warning ("interface with sw_if_index %u has no mac address",
1154 vlib_buffer_free (vm, &bi0, 1);
1157 rh->link_layer_option.header.n_data_u64s = 1;
1159 payload_length = sizeof (rh->neighbor) + sizeof (u64);
1161 rh->ip.ip_version_traffic_class_and_flow_label =
1162 clib_host_to_net_u32 (0x6 << 28);
1163 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
1164 rh->ip.protocol = IP_PROTOCOL_ICMP6;
1165 rh->ip.hop_limit = 255;
1166 ip6_address_copy (&rh->ip.src_address,
1167 ip6_get_link_local_address (radv_info->sw_if_index));
1168 /* set address ff02::2 */
1169 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
1170 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
1172 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
1180 stop_sending_rs (vlib_main_t * vm, ip6_ra_t * ra)
1184 ra->keep_sending_rs = 0;
1187 bi0 = vlib_get_buffer_index (vm, ra->buffer);
1188 vlib_buffer_free (vm, &bi0, 1);
1194 check_send_rs (vlib_main_t * vm, ip6_ra_t * radv_info, f64 current_time,
1204 icmp6_send_router_solicitation_params_t *params;
1206 if (!radv_info->keep_sending_rs)
1209 params = &radv_info->params;
1211 if (radv_info->due_time > current_time)
1213 *due_time = radv_info->due_time;
1217 p0 = radv_info->buffer;
1219 next_index = ip6_rewrite_mcast_node.index;
1221 c0 = vlib_buffer_copy (vm, p0);
1222 ci0 = vlib_get_buffer_index (vm, c0);
1224 f = vlib_get_frame_to_node (vm, next_index);
1225 to_next = vlib_frame_vector_args (f);
1228 vlib_put_frame_to_node (vm, next_index, f);
1230 if (params->mrc != 0 && --radv_info->n_left == 0)
1231 stop_sending_rs (vm, radv_info);
1234 radv_info->sleep_interval =
1235 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
1236 if (radv_info->sleep_interval > params->mrt)
1237 radv_info->sleep_interval =
1238 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
1240 radv_info->due_time = current_time + radv_info->sleep_interval;
1242 if (params->mrd != 0
1243 && current_time > radv_info->start_time + params->mrd)
1244 stop_sending_rs (vm, radv_info);
1246 *due_time = radv_info->due_time;
1249 return radv_info->keep_sending_rs;
1253 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1256 uword *event_data = NULL;
1257 f64 sleep_time = 1e9;
1258 ip6_ra_t *radv_info;
1265 vlib_process_wait_for_event_or_clock (vm, sleep_time);
1266 vlib_process_get_events (vm, &event_data);
1267 vec_reset_length (event_data);
1269 current_time = vlib_time_now (vm);
1272 due_time = current_time + 1e9;
1274 pool_foreach (radv_info, ip6_ra_pool,
1276 if (check_send_rs (vm, radv_info, current_time, &dt)
1281 current_time = vlib_time_now (vm);
1283 while (due_time < current_time);
1285 sleep_time = due_time - current_time;
1292 VLIB_REGISTER_NODE (ip6_rs_process_node) = {
1293 .function = send_rs_process,
1294 .type = VLIB_NODE_TYPE_PROCESS,
1295 .name = "ip6-rs-process",
1300 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
1301 const icmp6_send_router_solicitation_params_t
1306 ASSERT (~0 != sw_if_index);
1308 ra = ip6_ra_get_itf (sw_if_index);
1313 stop_sending_rs (vm, ra);
1317 ra->keep_sending_rs = 1;
1318 ra->params = *params;
1319 ra->n_left = params->mrc;
1320 ra->start_time = vlib_time_now (vm);
1321 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
1322 ra->due_time = 0; /* send first packet ASAP */
1323 ra->buffer = create_buffer_for_rs (vm, ra);
1325 ra->keep_sending_rs = 0;
1327 vlib_process_signal_event (vm, ip6_rs_process_node.index, 1, 0);
1331 static const ethernet_interface_t *
1332 ip6_ra_get_eth_itf (u32 sw_if_index)
1334 const vnet_sw_interface_t *sw;
1336 /* lookup radv container - ethernet interfaces only */
1337 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
1338 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1339 return (ethernet_get_interface (ðernet_main, sw->hw_if_index));
1345 * @brief called when IP6 is enabled on an interface
1346 *create and initialize router advertisement parameters with default
1347 * values for this intfc
1350 ip6_ra_link_enable (u32 sw_if_index)
1352 const ethernet_interface_t *eth;
1353 ip6_ra_t *radv_info;
1355 eth = ip6_ra_get_eth_itf (sw_if_index);
1360 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
1361 ip6_ra_delegate_id));
1363 pool_get_zero (ip6_ra_pool, radv_info);
1365 radv_info->seed = (u32) clib_cpu_time_now ();
1366 random_u32 (&radv_info->seed);
1368 radv_info->sw_if_index = sw_if_index;
1369 radv_info->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1370 radv_info->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1371 radv_info->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1372 radv_info->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1374 /* send ll address source address option */
1375 radv_info->adv_link_layer_address = 1;
1377 radv_info->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1378 radv_info->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1379 radv_info->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1381 radv_info->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
1382 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1383 radv_info->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1385 /* deafult is to send */
1386 radv_info->send_radv = 1;
1388 /* fill in delegate for this interface that will be needed later */
1389 radv_info->adv_link_mtu =
1390 vnet_sw_interface_get_mtu (vnet_get_main (), sw_if_index, VNET_MTU_IP6);
1392 mhash_init (&radv_info->address_to_prefix_index, sizeof (uword),
1393 sizeof (ip6_address_t));
1395 ip6_link_delegate_update (sw_if_index, ip6_ra_delegate_id,
1396 radv_info - ip6_ra_pool);
1400 ip6_ra_delegate_disable (index_t rai)
1402 ip6_radv_prefix_t *p;
1403 ip6_ra_t *radv_info;
1405 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
1407 /* clean up prefix and MDP pools */
1409 pool_flush(p, radv_info->adv_prefixes_pool,
1411 mhash_unset (&radv_info->address_to_prefix_index, &p->prefix, 0);
1415 pool_free (radv_info->adv_prefixes_pool);
1417 mhash_free (&radv_info->address_to_prefix_index);
1419 pool_put (ip6_ra_pool, radv_info);
1423 ip6_ra_update_secondary_radv_info (ip6_address_t * address, u8 prefix_len,
1424 u32 primary_sw_if_index,
1425 u32 valid_time, u32 preferred_time)
1427 vlib_main_t *vm = vlib_get_main ();
1428 static u32 *radv_indices;
1429 ip6_ra_t *radv_info;
1432 ip6_address_mask_from_width (&mask, prefix_len);
1434 vec_reset_length (radv_indices);
1436 pool_foreach (radv_info, ip6_ra_pool,
1438 vec_add1 (radv_indices, radv_info - ip6_ra_pool);
1443 * If we have another customer for this prefix,
1444 * tell the RA code about it...
1446 for (i = 0; i < vec_len (radv_indices); i++)
1448 ip6_radv_prefix_t *this_prefix;
1449 radv_info = pool_elt_at_index (ip6_ra_pool, radv_indices[i]);
1451 /* We already took care of these timers... */
1452 if (radv_info->sw_if_index == primary_sw_if_index)
1456 pool_foreach (this_prefix, radv_info->adv_prefixes_pool,
1458 if (this_prefix->prefix_len == prefix_len
1459 && ip6_address_is_equal_masked (&this_prefix->prefix, address,
1462 int rv = ip6_ra_prefix (vm,
1463 radv_info->sw_if_index,
1466 0 /* use_default */,
1469 0 /* no_advertise */,
1471 0 /* no_autoconfig */,
1475 clib_warning ("ip6_neighbor_ra_prefix returned %d", rv);
1482 /* send a RA or update the timer info etc.. */
1484 ip6_ra_process_timer_event (vlib_main_t * vm,
1485 vlib_node_runtime_t * node, vlib_frame_t * frame)
1487 vnet_main_t *vnm = vnet_get_main ();
1488 ip6_ra_t *radv_info;
1489 vlib_frame_t *f = 0;
1490 u32 n_this_frame = 0;
1491 u32 n_left_to_next = 0;
1494 icmp6_router_solicitation_header_t *h0;
1496 f64 now = vlib_time_now (vm);
1498 /* Interface ip6 radv info list */
1500 pool_foreach (radv_info, ip6_ra_pool,
1502 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
1504 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
1505 radv_info->next_multicast_time = now;
1506 radv_info->last_multicast_time = now;
1507 radv_info->last_radv_time = 0;
1511 /* is it time to send a multicast RA on this interface? */
1512 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
1517 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
1518 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
1520 /* multicast send - compute next multicast send time */
1521 if( radv_info->initial_adverts_sent > 0)
1523 radv_info->initial_adverts_sent--;
1524 if(rfn > radv_info->initial_adverts_interval)
1525 rfn = radv_info->initial_adverts_interval;
1527 /* check to see if we are ceasing to send */
1528 if( radv_info->initial_adverts_sent == 0)
1529 if(radv_info->cease_radv)
1530 radv_info->send_radv = 0;
1533 radv_info->next_multicast_time = rfn + now;
1534 radv_info->last_multicast_time = now;
1536 /* send advert now - build a "solicted" router advert with unspecified source address */
1537 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
1539 if (PREDICT_FALSE(n_allocated == 0))
1541 clib_warning ("buffer allocation failure");
1544 b0 = vlib_get_buffer (vm, bo0);
1545 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
1546 b0->error = ICMP6_ERROR_NONE;
1547 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
1549 h0 = vlib_buffer_get_current (b0);
1551 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
1553 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1554 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
1555 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
1556 h0->ip.protocol = IP_PROTOCOL_ICMP6;
1557 h0->ip.hop_limit = 255;
1559 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
1560 h0->ip.src_address.as_u64[0] = 0;
1561 h0->ip.src_address.as_u64[1] = 0;
1563 h0->ip.dst_address.as_u64[0] = 0;
1564 h0->ip.dst_address.as_u64[1] = 0;
1566 h0->neighbor.icmp.type = ICMP6_router_solicitation;
1568 if (PREDICT_FALSE(f == 0))
1570 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
1571 to_next = vlib_frame_vector_args (f);
1572 n_left_to_next = VLIB_FRAME_SIZE;
1581 if (PREDICT_FALSE(n_left_to_next == 0))
1583 f->n_vectors = n_this_frame;
1584 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1593 ASSERT (n_this_frame);
1594 f->n_vectors = n_this_frame;
1595 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1601 ip6_ra_handle_report (const ip6_ra_report_t * rap)
1605 vec_foreach_index (ii, ip6_ra_listeners) ip6_ra_listeners[ii] (rap);
1609 ip6_ra_event_process (vlib_main_t * vm,
1610 vlib_node_runtime_t * node, vlib_frame_t * frame)
1612 ip6_ra_report_t *r, *rs;
1615 /* init code here */
1619 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
1621 rs = vlib_process_get_event_data (vm, &event_type);
1625 /* No events found: timer expired. */
1626 /* process interface list and send RAs as appropriate, update timer info */
1627 ip6_ra_process_timer_event (vm, node, frame);
1631 vec_foreach (r, rs) ip6_ra_handle_report (r);
1632 vec_reset_length (rs);
1635 return frame->n_vectors;
1638 VLIB_REGISTER_NODE (ip6_ra_process_node) =
1640 .function = ip6_ra_event_process,.name = "ip6-ra-process",.type =
1641 VLIB_NODE_TYPE_PROCESS,};
1644 ip6_ra_signal_report (ip6_ra_report_t * r)
1646 vlib_main_t *vm = vlib_get_main ();
1649 if (!vec_len (ip6_ra_listeners))
1652 q = vlib_process_signal_event_data (vm,
1653 ip6_ra_process_node.index,
1659 ip6_ra_publish (ip6_ra_report_t * r)
1661 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
1662 vl_api_rpc_call_main_thread (ip6_ra_signal_report, (u8 *) r, sizeof *r);
1666 /* API support functions */
1668 ip6_ra_config (vlib_main_t * vm, u32 sw_if_index,
1669 u8 suppress, u8 managed, u8 other,
1670 u8 ll_option, u8 send_unicast, u8 cease,
1671 u8 use_lifetime, u32 lifetime,
1672 u32 initial_count, u32 initial_interval,
1673 u32 max_interval, u32 min_interval, u8 is_no)
1675 ip6_ra_t *radv_info;
1677 /* look up the radv_t information for this interface */
1678 radv_info = ip6_ra_get_itf (sw_if_index);
1681 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1683 if ((max_interval != 0) && (min_interval == 0))
1684 min_interval = .75 * max_interval;
1688 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
1689 radv_info->max_radv_interval;
1692 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
1693 radv_info->min_radv_interval;
1696 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
1697 radv_info->adv_router_lifetime_in_sec;
1701 if (lifetime > MAX_DEF_RTR_LIFETIME)
1702 lifetime = MAX_DEF_RTR_LIFETIME;
1704 if (lifetime <= max_interval)
1705 return VNET_API_ERROR_INVALID_VALUE;
1708 if (min_interval != 0)
1710 if ((min_interval > .75 * max_interval) || (min_interval < 3))
1711 return VNET_API_ERROR_INVALID_VALUE;
1714 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
1715 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
1716 return VNET_API_ERROR_INVALID_VALUE;
1719 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
1720 if "flag" is clear don't change corresponding value
1722 radv_info->send_radv =
1723 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
1724 radv_info->adv_managed_flag =
1725 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
1726 radv_info->adv_other_flag =
1727 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
1728 radv_info->adv_link_layer_address =
1729 (ll_option != 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
1730 radv_info->send_unicast =
1731 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
1732 radv_info->cease_radv =
1733 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
1735 radv_info->min_radv_interval = min_interval;
1736 radv_info->max_radv_interval = max_interval;
1737 radv_info->adv_router_lifetime_in_sec = lifetime;
1739 radv_info->initial_adverts_count =
1741 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
1742 radv_info->initial_adverts_count;
1743 radv_info->initial_adverts_interval =
1744 (initial_interval !=
1745 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
1746 radv_info->initial_adverts_interval;
1749 if ((cease != 0) && (is_no))
1750 radv_info->send_radv = 1;
1752 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1753 radv_info->next_multicast_time = vlib_time_now (vm);
1754 radv_info->last_multicast_time = vlib_time_now (vm);
1755 radv_info->last_radv_time = 0;
1762 ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
1763 ip6_address_t * prefix_addr, u8 prefix_len,
1764 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
1765 u8 no_advertise, u8 off_link, u8 no_autoconfig,
1766 u8 no_onlink, u8 is_no)
1768 ip6_ra_t *radv_info;
1770 /* look up the radv_t information for this interface */
1771 radv_info = ip6_ra_get_itf (sw_if_index);
1774 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1776 f64 now = vlib_time_now (vm);
1778 /* prefix info add, delete or update */
1779 ip6_radv_prefix_t *prefix;
1781 /* lookup prefix info for this address on this interface */
1782 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
1784 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
1790 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
1792 if (prefix->prefix_len != prefix_len)
1793 return VNET_API_ERROR_INVALID_VALUE_2;
1795 /* FIXME - Should the DP do this or the CP ? */
1796 /* do specific delete processing here before returning */
1797 /* try to remove from routing table */
1799 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
1801 pool_put (radv_info->adv_prefixes_pool, prefix);
1803 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1804 radv_info->next_multicast_time = vlib_time_now (vm);
1805 radv_info->last_multicast_time = vlib_time_now (vm);
1806 radv_info->last_radv_time = 0;
1810 /* adding or changing */
1815 pool_get_zero (radv_info->adv_prefixes_pool, prefix);
1816 pi = prefix - radv_info->adv_prefixes_pool;
1817 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
1820 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
1822 prefix->prefix_len = prefix_len;
1823 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
1825 /* initialize default values */
1826 prefix->adv_on_link_flag = 1; /* L bit set */
1827 prefix->adv_autonomous_flag = 1; /* A bit set */
1828 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
1829 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
1830 prefix->enabled = 1;
1831 prefix->decrement_lifetime_flag = 1;
1832 prefix->deprecated_prefix_flag = 1;
1836 /* FIXME - Should the DP do this or the CP ? */
1837 /* insert prefix into routing table as a connected prefix */
1846 if (prefix->prefix_len != prefix_len)
1847 return VNET_API_ERROR_INVALID_VALUE_2;
1851 /* FIXME - Should the DP do this or the CP ? */
1852 /* remove from routing table if already there */
1856 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
1858 prefix->adv_valid_lifetime_in_secs = ~0;
1859 prefix->adv_pref_lifetime_in_secs = ~0;
1860 prefix->decrement_lifetime_flag = 0;
1864 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
1865 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
1868 /* copy remaining */
1869 prefix->enabled = !(no_advertise != 0);
1870 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
1871 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
1875 /* fill in the expiration times */
1876 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
1877 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
1879 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1880 radv_info->next_multicast_time = vlib_time_now (vm);
1881 radv_info->last_multicast_time = vlib_time_now (vm);
1882 radv_info->last_radv_time = 0;
1888 ip6_ra_cmd (vlib_main_t * vm,
1889 unformat_input_t * main_input, vlib_cli_command_t * cmd)
1891 vnet_main_t *vnm = vnet_get_main ();
1892 clib_error_t *error = 0;
1894 u8 suppress = 0, managed = 0, other = 0;
1895 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
1896 u8 use_lifetime = 0;
1897 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
1898 0, ra_initial_interval = 0;
1899 u32 ra_max_interval = 0, ra_min_interval = 0;
1901 unformat_input_t _line_input, *line_input = &_line_input;
1903 int add_radv_info = 1;
1904 ip6_address_t ip6_addr;
1908 /* Get a line of input. */
1909 if (!unformat_user (main_input, unformat_line_input, line_input))
1912 /* get basic radv info for this interface */
1913 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1916 if (unformat_user (line_input,
1917 unformat_vnet_sw_interface, vnm, &sw_if_index))
1919 if (!ip6_ra_get_eth_itf (sw_if_index))
1922 clib_error_return (0, "Interface must be of ethernet type");
1926 if (!ip6_link_is_enabled (sw_if_index))
1928 error = clib_error_return (0, "IP6 nt enabler interface %U'",
1929 format_unformat_error, line_input);
1935 error = clib_error_return (0, "invalid interface name %U'",
1936 format_unformat_error, line_input);
1941 /* get the rest of the command */
1942 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1944 if (unformat (line_input, "no"))
1946 else if (unformat (line_input, "prefix %U/%d",
1947 unformat_ip6_address, &ip6_addr, &addr_len))
1952 else if (unformat (line_input, "ra-managed-config-flag"))
1957 else if (unformat (line_input, "ra-other-config-flag"))
1962 else if (unformat (line_input, "ra-suppress") ||
1963 unformat (line_input, "ra-surpress"))
1968 else if (unformat (line_input, "ra-suppress-link-layer") ||
1969 unformat (line_input, "ra-surpress-link-layer"))
1971 suppress_ll_option = 1;
1974 else if (unformat (line_input, "ra-send-unicast"))
1979 else if (unformat (line_input, "ra-lifetime"))
1981 if (!unformat (line_input, "%d", &ra_lifetime))
1983 error = unformat_parse_error (line_input);
1989 else if (unformat (line_input, "ra-initial"))
1992 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
1994 error = unformat_parse_error (line_input);
1999 else if (unformat (line_input, "ra-interval"))
2001 if (!unformat (line_input, "%d", &ra_max_interval))
2003 error = unformat_parse_error (line_input);
2007 if (!unformat (line_input, "%d", &ra_min_interval))
2008 ra_min_interval = 0;
2011 else if (unformat (line_input, "ra-cease"))
2018 error = unformat_parse_error (line_input);
2025 ip6_ra_config (vm, sw_if_index,
2026 suppress, managed, other,
2027 suppress_ll_option, send_unicast, cease,
2028 use_lifetime, ra_lifetime,
2029 ra_initial_count, ra_initial_interval,
2030 ra_max_interval, ra_min_interval, is_no);
2034 u32 valid_lifetime_in_secs = 0;
2035 u32 pref_lifetime_in_secs = 0;
2036 u8 use_prefix_default_values = 0;
2037 u8 no_advertise = 0;
2039 u8 no_autoconfig = 0;
2042 /* get the rest of the command */
2043 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2045 if (unformat (line_input, "default"))
2047 use_prefix_default_values = 1;
2050 else if (unformat (line_input, "infinite"))
2052 valid_lifetime_in_secs = ~0;
2053 pref_lifetime_in_secs = ~0;
2056 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2057 &pref_lifetime_in_secs))
2064 /* get the rest of the command */
2065 while (!use_prefix_default_values &&
2066 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2068 if (unformat (line_input, "no-advertise"))
2070 else if (unformat (line_input, "off-link"))
2072 else if (unformat (line_input, "no-autoconfig"))
2074 else if (unformat (line_input, "no-onlink"))
2078 error = unformat_parse_error (line_input);
2083 ip6_ra_prefix (vm, sw_if_index,
2084 &ip6_addr, addr_len,
2085 use_prefix_default_values,
2086 valid_lifetime_in_secs,
2087 pref_lifetime_in_secs,
2088 no_advertise, off_link, no_autoconfig, no_onlink, is_no);
2092 unformat_free (line_input);
2098 format_ip6_ra (u8 * s, va_list * args)
2100 index_t rai = va_arg (*args, index_t);
2101 u32 indent = va_arg (*args, u32);
2102 ip6_radv_prefix_t *p;
2103 ip6_ra_t *radv_info;
2105 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
2107 s = format (s, "%UAdvertised Prefixes:\n", format_white_space, indent);
2112 pool_foreach (p, radv_info->adv_prefixes_pool,
2114 s = format (s, "%Uprefix %U, length %d\n",
2115 format_white_space, indent+2,
2116 format_ip6_address, &p->prefix, p->prefix_len);
2120 s = format (s, "%UMTU is %d\n",
2121 format_white_space, indent, radv_info->adv_link_mtu);
2122 s = format (s, "%UICMP error messages are unlimited\n",
2123 format_white_space, indent);
2124 s = format (s, "%UICMP redirects are disabled\n",
2125 format_white_space, indent);
2126 s = format (s, "%UICMP unreachables are not sent\n",
2127 format_white_space, indent);
2128 s = format (s, "%UND DAD is disabled\n", format_white_space, indent);
2129 //s = format (s, "%UND reachable time is %d milliseconds\n",);
2130 s = format (s, "%UND advertised reachable time is %d\n",
2131 format_white_space, indent,
2132 radv_info->adv_neighbor_reachable_time_in_msec);
2134 "%UND advertised retransmit interval is %d (msec)\n",
2135 format_white_space, indent,
2137 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2140 "%UND router advertisements are sent every %0.1f seconds (min interval is %0.1f)\n",
2141 format_white_space, indent, radv_info->max_radv_interval,
2142 radv_info->min_radv_interval);
2144 format (s, "%UND router advertisements live for %d seconds\n",
2145 format_white_space, indent,
2146 radv_info->adv_router_lifetime_in_sec);
2148 format (s, "%UHosts %s stateless autoconfig for addresses\n",
2149 format_white_space, indent,
2150 (radv_info->adv_managed_flag) ? "use" : " don't use");
2152 format (s, "%UND router advertisements sent %d\n", format_white_space,
2153 indent, radv_info->n_advertisements_sent);
2155 format (s, "%UND router solicitations received %d\n", format_white_space,
2156 indent, radv_info->n_solicitations_rcvd);
2158 format (s, "%UND router solicitations dropped %d\n", format_white_space,
2159 indent, radv_info->n_solicitations_dropped);
2166 * This command is used to configure the neighbor discovery
2167 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
2168 * command to display some of the current neighbor discovery parameters
2169 * on a given interface. This command has three formats:
2172 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
2174 * '<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>'
2178 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
2179 * router-advertisement messages to use stateful address
2180 * auto-configuration to obtain address information (sets the M-bit).
2181 * Default is the M-bit is not set and the '<em>no</em>' option
2182 * returns it to this default state.
2184 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
2185 * router-advertisement messages that hosts use stateful auto
2186 * configuration to obtain nonaddress related information (sets
2187 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
2188 * option returns it to this default state.
2190 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
2191 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
2192 * router-advertisement messages.
2194 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
2195 * optional source link-layer address in the ICMPv6 router-advertisement
2196 * messages. Default is to include the optional source link-layer address
2197 * and the '<em>no</em>' option returns it to this default state.
2199 * <em>[no] ra-send-unicast</em> - Use the source address of the
2200 * router-solicitation message if availiable. The default is to use
2201 * multicast address of all nodes, and the '<em>no</em>' option returns
2202 * it to this default state.
2204 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
2205 * default router in ICMPv6 router-advertisement messages. The range is
2206 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
2207 * '<em><max-interval></em>'. The default value is 600 seconds and the
2208 * '<em>no</em>' option returns it to this default value.
2210 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
2211 * router-advertisement messages sent and the interval between each
2212 * message. Range for count is 1 - 3 and default is 3. Range for interval
2213 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
2214 * returns both to their default value.
2216 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
2217 * interval between sending ICMPv6 router-advertisement messages. The
2218 * range for max-interval is from 4 to 200 seconds. min-interval can not
2219 * be more than 75% of max-interval. If not set, min-interval will be
2220 * set to 75% of max-interval. The range for min-interval is from 3 to
2221 * 150 seconds. The '<em>no</em>' option returns both to their default
2224 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
2225 * The '<em>no</em>' options implies to start (or restart) sending
2226 * ICMPv6 router-advertisement messages.
2229 * <b>Format 2 - Prefix Options:</b>
2231 * '<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>'
2235 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
2237 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
2238 * length of time in seconds during what the prefix is valid for the purpose of
2239 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
2240 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
2241 * length of time in seconds during what addresses generated from the prefix remain
2242 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
2244 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
2245 * are inifinte, no timeout.
2247 * <em>no-advertise</em> - Do not send full router address in prefix
2248 * advertisement. Default is to advertise (i.e. - This flag is off by default).
2250 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
2251 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
2252 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
2254 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
2255 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
2257 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
2258 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
2259 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
2263 * <b>Format 3: - Default of Prefix:</b>
2265 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
2267 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
2268 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
2269 * is ignored and the prefix is deleted.
2273 * Example of how set a router advertisement option:
2274 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
2275 * Example of how to add a prefix:
2276 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
2277 * Example of how to delete a prefix:
2278 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
2281 VLIB_CLI_COMMAND (ip6_nd_command, static) =
2284 .short_help = "ip6 nd <interface> ...",
2285 .function = ip6_ra_cmd,
2290 * VFT for registering as a delegate to an IP6 link
2292 const static ip6_link_delegate_vft_t ip6_ra_delegate_vft = {
2293 .ildv_disable = ip6_ra_delegate_disable,
2294 .ildv_enable = ip6_ra_link_enable,
2295 .ildv_format = format_ip6_ra,
2298 static clib_error_t *
2299 ip6_ra_init (vlib_main_t * vm)
2301 vlib_call_init_function (vm, icmp6_init);
2303 icmp6_register_type (vm, ICMP6_router_solicitation,
2304 ip6_icmp_router_solicitation_node.index);
2305 icmp6_register_type (vm, ICMP6_router_advertisement,
2306 ip6_icmp_router_advertisement_node.index);
2308 ip6_ra_delegate_id = ip6_link_delegate_register (&ip6_ra_delegate_vft);
2314 VLIB_INIT_FUNCTION (ip6_ra_init) =
2316 .runs_after = VLIB_INITS("icmp6_init"),
2321 * fd.io coding-style-patch-verification: ON
2324 * eval: (c-set-style "gnu")