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 (vlib_get_elog_main (), e);
257 elog_string (vlib_get_elog_main (), log_level_strings[priority]);
258 ed->s[1] = elog_string (vlib_get_elog_main (), (char *) what);
264 /* ipv6 neighbor discovery - router advertisements */
267 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
268 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
269 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
270 ICMP6_ROUTER_SOLICITATION_N_NEXT,
271 } icmp6_router_solicitation_or_advertisement_next_t;
274 * Note: Both periodic RAs and solicited RS come through here.
276 static_always_inline uword
277 icmp6_router_solicitation (vlib_main_t * vm,
278 vlib_node_runtime_t * node, vlib_frame_t * frame)
280 vnet_main_t *vnm = vnet_get_main ();
281 ip6_main_t *im = &ip6_main;
282 uword n_packets = frame->n_vectors;
284 u32 n_left_from, n_left_to_next, next_index;
285 u32 n_advertisements_sent = 0;
288 icmp6_neighbor_discovery_option_type_t option_type;
290 vlib_node_runtime_t *error_node =
291 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
293 from = vlib_frame_vector_args (frame);
294 n_left_from = n_packets;
295 next_index = node->cached_next_index;
297 if (node->flags & VLIB_NODE_FLAG_TRACE)
298 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
300 sizeof (icmp6_input_trace_t));
302 /* source may append his LL address */
303 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
305 while (n_left_from > 0)
307 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
309 while (n_left_from > 0 && n_left_to_next > 0)
313 ip6_ra_t *radv_info = NULL;
315 icmp6_neighbor_discovery_header_t *h0;
316 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
318 u32 bi0, options_len0, sw_if_index0, next0, error0;
319 u32 is_solicitation = 1, is_dropped = 0;
320 u32 is_unspecified, is_link_local;
322 bi0 = to_next[0] = from[0];
329 p0 = vlib_get_buffer (vm, bi0);
330 ip0 = vlib_buffer_get_current (p0);
331 h0 = ip6_next_header (ip0);
333 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
334 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
336 ip6_address_is_link_local_unicast (&ip0->src_address);
338 error0 = ICMP6_ERROR_NONE;
339 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
341 /* check if solicitation (not from nd_timer node) */
342 if (ip6_address_is_unspecified (&ip0->dst_address))
345 /* Check that source address is unspecified, link-local or else on-link. */
346 if (!is_unspecified && !is_link_local)
348 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
350 if (ADJ_INDEX_INVALID != src_adj_index0)
352 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
354 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
356 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
361 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
365 /* check for source LL option and process */
366 o0 = (void *) (h0 + 1);
367 o0 = ((options_len0 == 8
368 && o0->header.type == option_type
369 && o0->header.n_data_u64s == 1) ? o0 : 0);
371 /* if src address unspecified IGNORE any options */
372 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
373 !is_unspecified && !is_link_local))
376 ip_neighbor_learn_t learn = {
377 .sw_if_index = sw_if_index0,
379 .ip.ip6 = ip0->src_address,
384 memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac));
385 ip_neighbor_learn_dp (&learn);
388 /* default is to drop */
389 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
391 if (error0 == ICMP6_ERROR_NONE)
393 vnet_sw_interface_t *sw_if0;
394 ethernet_interface_t *eth_if0;
397 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
398 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
400 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
402 /* only support ethernet interface type for now */
404 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
407 if (error0 == ICMP6_ERROR_NONE)
409 /* adjust the sizeof the buffer to just include the ipv6 header */
410 p0->current_length -=
412 sizeof (icmp6_neighbor_discovery_header_t));
414 radv_info = ip6_ra_get_itf (sw_if_index0);
416 error0 = ((!radv_info || 0 == radv_info->send_radv) ?
417 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
419 if (error0 == ICMP6_ERROR_NONE)
421 f64 now = vlib_time_now (vm);
423 /* for solicited adverts - need to rate limit */
426 if (0 != radv_info->last_radv_time &&
427 (now - radv_info->last_radv_time) <
428 MIN_DELAY_BETWEEN_RAS)
431 radv_info->last_radv_time = now;
435 icmp6_router_advertisement_header_t rh;
437 rh.icmp.type = ICMP6_router_advertisement;
439 rh.icmp.checksum = 0;
441 rh.current_hop_limit = radv_info->curr_hop_limit;
442 rh.router_lifetime_in_sec =
444 (radv_info->adv_router_lifetime_in_sec);
446 time_in_msec_between_retransmitted_neighbor_solicitations
448 clib_host_to_net_u32 (radv_info->
449 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
450 rh.neighbor_reachable_time_in_msec =
451 clib_host_to_net_u32 (radv_info->
452 adv_neighbor_reachable_time_in_msec);
455 (radv_info->adv_managed_flag) ?
456 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
459 ((radv_info->adv_other_flag) ?
460 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
465 sizeof (icmp6_router_advertisement_header_t);
467 if (vlib_buffer_add_data
468 (vm, &bi0, (void *) &rh,
469 sizeof (icmp6_router_advertisement_header_t)))
471 /* buffer allocation failed, drop the pkt */
472 error0 = ICMP6_ERROR_ALLOC_FAILURE;
476 if (radv_info->adv_link_layer_address)
478 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
482 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
483 h.header.n_data_u64s = 1;
485 /* copy ll address */
486 clib_memcpy (&h.ethernet_address[0],
487 ð_if0->address, 6);
489 if (vlib_buffer_add_data
490 (vm, &bi0, (void *) &h,
492 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
494 error0 = ICMP6_ERROR_ALLOC_FAILURE;
500 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
504 if (radv_info->adv_link_mtu)
506 icmp6_neighbor_discovery_mtu_option_t h;
510 clib_host_to_net_u32 (radv_info->adv_link_mtu);
511 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
512 h.header.n_data_u64s = 1;
515 sizeof (icmp6_neighbor_discovery_mtu_option_t);
517 if (vlib_buffer_add_data
518 (vm, &bi0, (void *) &h,
520 (icmp6_neighbor_discovery_mtu_option_t)))
522 error0 = ICMP6_ERROR_ALLOC_FAILURE;
527 /* add advertised prefix options */
528 ip6_radv_prefix_t *pr_info;
531 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
533 if(pr_info->enabled &&
534 (!pr_info->decrement_lifetime_flag
535 || (pr_info->pref_lifetime_expires >0)))
537 /* advertise this prefix */
538 icmp6_neighbor_discovery_prefix_information_option_t h;
540 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
541 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
543 h.dst_address_length = pr_info->prefix_len;
545 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
546 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
548 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
550 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
551 h.preferred_time = 0;
555 if(pr_info->decrement_lifetime_flag)
557 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
558 (pr_info->valid_lifetime_expires - now) : 0;
560 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
561 (pr_info->pref_lifetime_expires - now) : 0;
564 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
565 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
569 /* Handy for debugging, but too noisy... */
570 if (0 && CLIB_DEBUG > 0)
572 ("send RA for prefix %U/%d "
573 "sw_if_index %d valid %u preferred %u",
574 format_ip6_address, &pr_info->prefix,
575 pr_info->prefix_len, sw_if_index0,
576 clib_net_to_host_u32 (h.valid_time),
577 clib_net_to_host_u32 (h.preferred_time));
579 if (h.valid_time == 0)
580 clib_warning ("BUG: send RA with valid_time 0");
582 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
584 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
586 if (vlib_buffer_add_data
587 (vm, &bi0, (void *)&h,
588 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
590 error0 = ICMP6_ERROR_ALLOC_FAILURE;
598 /* add additional options before here */
600 /* finish building the router advertisement... */
601 if (!is_unspecified && radv_info->send_unicast)
603 ip0->dst_address = ip0->src_address;
607 /* target address is all-nodes mcast addr */
608 ip6_set_reserved_multicast_address
610 IP6_MULTICAST_SCOPE_link_local,
611 IP6_MULTICAST_GROUP_ID_all_hosts);
614 /* source address MUST be the link-local address */
615 ip6_address_copy (&ip0->src_address,
616 ip6_get_link_local_address
617 (radv_info->sw_if_index));
619 ip0->hop_limit = 255;
620 ip0->payload_length =
621 clib_host_to_net_u16 (payload_length);
623 icmp6_router_advertisement_header_t *rh0 =
624 (icmp6_router_advertisement_header_t *) (ip0 + 1);
626 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
628 ASSERT (bogus_length == 0);
630 /* setup output if and adjacency */
631 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
632 vnet_main.local_interface_sw_if_index;
636 ethernet_header_t *eth0;
637 /* Reuse current MAC header, copy SMAC to DMAC and
638 * interface MAC to SMAC */
639 vlib_buffer_reset (p0);
640 vlib_buffer_advance (
641 p0, vnet_buffer (p0)->l2_hdr_offset);
642 eth0 = vlib_buffer_get_current (p0);
643 clib_memcpy (eth0->dst_address, eth0->src_address,
645 clib_memcpy (eth0->src_address, ð_if0->address,
649 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
650 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
655 adj_index0 = ip6_link_get_mcast_adj (sw_if_index0);
656 if (adj_index0 == INDEX_INVALID)
657 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
662 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
663 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
667 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
669 radv_info->n_solicitations_dropped += is_dropped;
670 radv_info->n_solicitations_rcvd += is_solicitation;
672 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
674 radv_info->n_advertisements_sent++;
675 n_advertisements_sent++;
682 p0->error = error_node->errors[error0];
684 if (error0 != ICMP6_ERROR_NONE)
685 vlib_error_count (vm, error_node->node_index, error0, 1);
687 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
688 to_next, n_left_to_next,
693 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
696 /* Account for router advertisements sent. */
697 vlib_error_count (vm, error_node->node_index,
698 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
699 n_advertisements_sent);
701 return frame->n_vectors;
705 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
707 .function = icmp6_router_solicitation,
708 .name = "icmp6-router-solicitation",
710 .vector_size = sizeof (u32),
712 .format_trace = format_icmp6_input_trace,
714 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
716 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
717 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
718 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
723 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
724 static_always_inline uword
725 icmp6_router_advertisement (vlib_main_t * vm,
726 vlib_node_runtime_t * node, vlib_frame_t * frame)
728 vnet_main_t *vnm = vnet_get_main ();
729 uword n_packets = frame->n_vectors;
731 u32 n_left_from, n_left_to_next, next_index;
732 u32 n_advertisements_rcvd = 0;
734 vlib_node_runtime_t *error_node =
735 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
737 from = vlib_frame_vector_args (frame);
738 n_left_from = n_packets;
739 next_index = node->cached_next_index;
741 if (node->flags & VLIB_NODE_FLAG_TRACE)
742 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
744 sizeof (icmp6_input_trace_t));
746 while (n_left_from > 0)
748 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
750 while (n_left_from > 0 && n_left_to_next > 0)
754 ip6_ra_t *radv_info = 0;
755 icmp6_router_advertisement_header_t *h0;
756 u32 bi0, options_len0, sw_if_index0, next0, error0;
758 bi0 = to_next[0] = from[0];
765 p0 = vlib_get_buffer (vm, bi0);
766 ip0 = vlib_buffer_get_current (p0);
767 h0 = ip6_next_header (ip0);
769 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
771 error0 = ICMP6_ERROR_NONE;
772 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
774 /* Check that source address is link-local */
775 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
776 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
778 /* default is to drop */
779 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
781 n_advertisements_rcvd++;
783 if (error0 == ICMP6_ERROR_NONE)
785 vnet_sw_interface_t *sw_if0;
786 ethernet_interface_t *eth_if0;
788 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
789 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
791 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
793 /* only support ethernet interface type for now */
795 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
798 if (error0 == ICMP6_ERROR_NONE)
800 /* look up the radv_t information for this interface */
801 radv_info = ip6_ra_get_itf (sw_if_index0);
803 error0 = ((!radv_info) ?
804 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
807 if (error0 == ICMP6_ERROR_NONE)
809 radv_info->keep_sending_rs = 0;
813 r.sw_if_index = sw_if_index0;
814 memcpy (&r.router_address, &ip0->src_address, 16);
815 r.current_hop_limit = h0->current_hop_limit;
817 r.router_lifetime_in_sec =
818 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
819 r.neighbor_reachable_time_in_msec =
821 (h0->neighbor_reachable_time_in_msec);
822 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
825 /* validate advertised information */
826 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
827 && (h0->current_hop_limit !=
828 radv_info->curr_hop_limit))
830 ip6_neighbor_syslog (vm, LOG_WARNING,
831 "our AdvCurHopLimit on %U doesn't agree with %U",
832 format_vnet_sw_if_index_name,
839 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
840 != radv_info->adv_managed_flag)
842 ip6_neighbor_syslog (vm, LOG_WARNING,
843 "our AdvManagedFlag on %U doesn't agree with %U",
844 format_vnet_sw_if_index_name,
851 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
852 != radv_info->adv_other_flag)
854 ip6_neighbor_syslog (vm, LOG_WARNING,
855 "our AdvOtherConfigFlag on %U doesn't agree with %U",
856 format_vnet_sw_if_index_name,
863 time_in_msec_between_retransmitted_neighbor_solicitations
865 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
867 time_in_msec_between_retransmitted_neighbor_solicitations
869 clib_host_to_net_u32 (radv_info->
870 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
872 ip6_neighbor_syslog (vm, LOG_WARNING,
873 "our AdvRetransTimer on %U doesn't agree with %U",
874 format_vnet_sw_if_index_name,
880 if ((h0->neighbor_reachable_time_in_msec &&
881 radv_info->adv_neighbor_reachable_time_in_msec) &&
882 (h0->neighbor_reachable_time_in_msec !=
884 (radv_info->adv_neighbor_reachable_time_in_msec)))
886 ip6_neighbor_syslog (vm, LOG_WARNING,
887 "our AdvReachableTime on %U doesn't agree with %U",
888 format_vnet_sw_if_index_name,
894 /* check for MTU or prefix options or .. */
895 u8 *opt_hdr = (u8 *) (h0 + 1);
896 while (options_len0 > 0)
898 icmp6_neighbor_discovery_option_header_t *o0 =
899 (icmp6_neighbor_discovery_option_header_t *)
901 int opt_len = o0->n_data_u64s << 3;
902 icmp6_neighbor_discovery_option_type_t option_type =
905 if (options_len0 < 2)
907 ip6_neighbor_syslog (vm, LOG_ERR,
908 "malformed RA packet on %U from %U",
909 format_vnet_sw_if_index_name,
918 ip6_neighbor_syslog (vm, LOG_ERR,
919 " zero length option in RA on %U from %U",
920 format_vnet_sw_if_index_name,
926 else if (opt_len > options_len0)
928 ip6_neighbor_syslog (vm, LOG_ERR,
929 "option length in RA packet greater than total length on %U from %U",
930 format_vnet_sw_if_index_name,
937 options_len0 -= opt_len;
942 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
944 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
946 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
949 if (opt_len < sizeof (*h))
952 memcpy (r.slla, h->ethernet_address, 6);
956 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
958 icmp6_neighbor_discovery_mtu_option_t *h =
959 (icmp6_neighbor_discovery_mtu_option_t
962 if (opt_len < sizeof (*h))
965 r.mtu = clib_net_to_host_u32 (h->mtu);
967 if ((h->mtu && radv_info->adv_link_mtu) &&
970 (radv_info->adv_link_mtu)))
972 ip6_neighbor_syslog (vm, LOG_WARNING,
973 "our AdvLinkMTU on %U doesn't agree with %U",
974 format_vnet_sw_if_index_name,
982 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
984 icmp6_neighbor_discovery_prefix_information_option_t
986 (icmp6_neighbor_discovery_prefix_information_option_t
989 /* validate advertised prefix options */
990 ip6_radv_prefix_t *pr_info;
991 u32 preferred, valid;
993 if (opt_len < sizeof (*h))
996 vec_validate (r.prefixes,
997 vec_len (r.prefixes));
998 ra_report_prefix_info_t *prefix =
999 vec_elt_at_index (r.prefixes,
1000 vec_len (r.prefixes) - 1);
1003 clib_net_to_host_u32 (h->preferred_time);
1004 valid = clib_net_to_host_u32 (h->valid_time);
1006 prefix->preferred_time = preferred;
1007 prefix->valid_time = valid;
1008 prefix->flags = h->flags & 0xc0;
1009 prefix->prefix.fp_len = h->dst_address_length;
1010 prefix->prefix.fp_addr.ip6 = h->dst_address;
1011 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
1013 /* look for matching prefix - if we our advertising it, it better be consistant */
1015 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
1019 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1021 if(pr_info->enabled &&
1022 (pr_info->prefix_len == h->dst_address_length) &&
1023 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1026 if(!pr_info->decrement_lifetime_flag &&
1027 valid != pr_info->adv_valid_lifetime_in_secs)
1029 ip6_neighbor_syslog(vm, LOG_WARNING,
1030 "our ADV validlifetime on %U for %U does not agree with %U",
1031 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1032 format_ip6_address, &h->dst_address);
1034 if(!pr_info->decrement_lifetime_flag &&
1035 preferred != pr_info->adv_pref_lifetime_in_secs)
1037 ip6_neighbor_syslog(vm, LOG_WARNING,
1038 "our ADV preferredlifetime on %U for %U does not agree with %U",
1039 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1040 format_ip6_address, &h->dst_address);
1053 ip6_ra_publish (&r);
1058 p0->error = error_node->errors[error0];
1060 if (error0 != ICMP6_ERROR_NONE)
1061 vlib_error_count (vm, error_node->node_index, error0, 1);
1063 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1064 to_next, n_left_to_next,
1068 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1071 /* Account for router advertisements received. */
1072 vlib_error_count (vm, error_node->node_index,
1073 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
1074 n_advertisements_rcvd);
1076 return frame->n_vectors;
1080 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
1082 .function = icmp6_router_advertisement,
1083 .name = "icmp6-router-advertisement",
1085 .vector_size = sizeof (u32),
1087 .format_trace = format_icmp6_input_trace,
1097 random_f64_from_to (f64 from, f64 to)
1099 static u32 seed = 0;
1100 static u8 seed_set = 0;
1103 seed = random_default_seed ();
1106 return random_f64 (&seed) * (to - from) + from;
1110 get_mac_address (u32 sw_if_index, u8 * address)
1112 vnet_main_t *vnm = vnet_get_main ();
1113 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1114 if (!hw_if->hw_address)
1116 clib_memcpy (address, hw_if->hw_address, 6);
1120 static inline vlib_buffer_t *
1121 create_buffer_for_rs (vlib_main_t * vm, ip6_ra_t * radv_info)
1125 icmp6_router_solicitation_header_t *rh;
1130 sw_if_index = radv_info->sw_if_index;
1132 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
1134 clib_warning ("buffer allocation failure");
1138 p0 = vlib_get_buffer (vm, bi0);
1139 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1141 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
1142 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
1144 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1145 ip6_link_get_mcast_adj (sw_if_index);
1147 rh = vlib_buffer_get_current (p0);
1148 p0->current_length = sizeof (*rh);
1150 rh->neighbor.icmp.type = ICMP6_router_solicitation;
1151 rh->neighbor.icmp.code = 0;
1152 rh->neighbor.icmp.checksum = 0;
1153 rh->neighbor.reserved_must_be_zero = 0;
1155 rh->link_layer_option.header.type =
1156 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1157 if (0 != get_mac_address (sw_if_index,
1158 rh->link_layer_option.ethernet_address))
1160 clib_warning ("interface with sw_if_index %u has no mac address",
1162 vlib_buffer_free (vm, &bi0, 1);
1165 rh->link_layer_option.header.n_data_u64s = 1;
1167 payload_length = sizeof (rh->neighbor) + sizeof (u64);
1169 rh->ip.ip_version_traffic_class_and_flow_label =
1170 clib_host_to_net_u32 (0x6 << 28);
1171 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
1172 rh->ip.protocol = IP_PROTOCOL_ICMP6;
1173 rh->ip.hop_limit = 255;
1174 ip6_address_copy (&rh->ip.src_address,
1175 ip6_get_link_local_address (radv_info->sw_if_index));
1176 /* set address ff02::2 */
1177 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
1178 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
1180 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
1188 stop_sending_rs (vlib_main_t * vm, ip6_ra_t * ra)
1192 ra->keep_sending_rs = 0;
1195 bi0 = vlib_get_buffer_index (vm, ra->buffer);
1196 vlib_buffer_free (vm, &bi0, 1);
1202 check_send_rs (vlib_main_t * vm, ip6_ra_t * radv_info, f64 current_time,
1212 icmp6_send_router_solicitation_params_t *params;
1214 if (!radv_info->keep_sending_rs)
1217 params = &radv_info->params;
1219 if (radv_info->due_time > current_time)
1221 *due_time = radv_info->due_time;
1225 p0 = radv_info->buffer;
1227 next_index = ip6_rewrite_mcast_node.index;
1229 c0 = vlib_buffer_copy (vm, p0);
1231 return radv_info->keep_sending_rs;
1233 ci0 = vlib_get_buffer_index (vm, c0);
1235 f = vlib_get_frame_to_node (vm, next_index);
1236 to_next = vlib_frame_vector_args (f);
1239 vlib_put_frame_to_node (vm, next_index, f);
1241 if (params->mrc != 0 && --radv_info->n_left == 0)
1242 stop_sending_rs (vm, radv_info);
1245 radv_info->sleep_interval =
1246 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
1247 if (radv_info->sleep_interval > params->mrt)
1248 radv_info->sleep_interval =
1249 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
1251 radv_info->due_time = current_time + radv_info->sleep_interval;
1253 if (params->mrd != 0
1254 && current_time > radv_info->start_time + params->mrd)
1255 stop_sending_rs (vm, radv_info);
1257 *due_time = radv_info->due_time;
1260 return radv_info->keep_sending_rs;
1264 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1267 uword *event_data = NULL;
1268 f64 sleep_time = 1e9;
1269 ip6_ra_t *radv_info;
1276 vlib_process_wait_for_event_or_clock (vm, sleep_time);
1277 vlib_process_get_events (vm, &event_data);
1278 vec_reset_length (event_data);
1280 current_time = vlib_time_now (vm);
1283 due_time = current_time + 1e9;
1285 pool_foreach (radv_info, ip6_ra_pool)
1287 if (check_send_rs (vm, radv_info, current_time, &dt)
1292 current_time = vlib_time_now (vm);
1294 while (due_time < current_time);
1296 sleep_time = due_time - current_time;
1303 VLIB_REGISTER_NODE (ip6_rs_process_node) = {
1304 .function = send_rs_process,
1305 .type = VLIB_NODE_TYPE_PROCESS,
1306 .name = "ip6-rs-process",
1311 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
1312 const icmp6_send_router_solicitation_params_t
1317 ASSERT (~0 != sw_if_index);
1319 ra = ip6_ra_get_itf (sw_if_index);
1324 stop_sending_rs (vm, ra);
1328 ra->keep_sending_rs = 1;
1329 ra->params = *params;
1330 ra->n_left = params->mrc;
1331 ra->start_time = vlib_time_now (vm);
1332 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
1333 ra->due_time = 0; /* send first packet ASAP */
1334 ra->buffer = create_buffer_for_rs (vm, ra);
1336 ra->keep_sending_rs = 0;
1338 vlib_process_signal_event (vm, ip6_rs_process_node.index, 1, 0);
1342 static const ethernet_interface_t *
1343 ip6_ra_get_eth_itf (u32 sw_if_index)
1345 const vnet_sw_interface_t *sw;
1347 /* lookup radv container - ethernet interfaces only */
1348 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
1349 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1350 return (ethernet_get_interface (ðernet_main, sw->hw_if_index));
1356 * @brief called when IP6 is enabled on an interface
1357 *create and initialize router advertisement parameters with default
1358 * values for this intfc
1361 ip6_ra_link_enable (u32 sw_if_index)
1363 const ethernet_interface_t *eth;
1364 ip6_ra_t *radv_info;
1366 eth = ip6_ra_get_eth_itf (sw_if_index);
1371 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
1372 ip6_ra_delegate_id));
1374 pool_get_zero (ip6_ra_pool, radv_info);
1376 radv_info->seed = (u32) clib_cpu_time_now ();
1377 random_u32 (&radv_info->seed);
1379 radv_info->sw_if_index = sw_if_index;
1380 radv_info->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1381 radv_info->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1382 radv_info->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1383 radv_info->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1385 /* send ll address source address option */
1386 radv_info->adv_link_layer_address = 1;
1388 radv_info->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1389 radv_info->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1390 radv_info->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1392 radv_info->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
1393 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1394 radv_info->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1396 /* fill in delegate for this interface that will be needed later */
1397 radv_info->adv_link_mtu =
1398 vnet_sw_interface_get_mtu (vnet_get_main (), sw_if_index, VNET_MTU_IP6);
1400 mhash_init (&radv_info->address_to_prefix_index, sizeof (uword),
1401 sizeof (ip6_address_t));
1403 ip6_link_delegate_update (sw_if_index, ip6_ra_delegate_id,
1404 radv_info - ip6_ra_pool);
1408 ip6_ra_delegate_disable (index_t rai)
1410 ip6_radv_prefix_t *p;
1411 ip6_ra_t *radv_info;
1413 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
1415 /* clean up prefix and MDP pools */
1417 pool_flush(p, radv_info->adv_prefixes_pool,
1419 mhash_unset (&radv_info->address_to_prefix_index, &p->prefix, 0);
1423 pool_free (radv_info->adv_prefixes_pool);
1425 mhash_free (&radv_info->address_to_prefix_index);
1427 pool_put (ip6_ra_pool, radv_info);
1431 ip6_ra_update_secondary_radv_info (ip6_address_t * address, u8 prefix_len,
1432 u32 primary_sw_if_index,
1433 u32 valid_time, u32 preferred_time)
1435 vlib_main_t *vm = vlib_get_main ();
1436 static u32 *radv_indices;
1437 ip6_ra_t *radv_info;
1440 ip6_address_mask_from_width (&mask, prefix_len);
1442 vec_reset_length (radv_indices);
1444 pool_foreach (radv_info, ip6_ra_pool)
1446 vec_add1 (radv_indices, radv_info - ip6_ra_pool);
1451 * If we have another customer for this prefix,
1452 * tell the RA code about it...
1454 for (i = 0; i < vec_len (radv_indices); i++)
1456 ip6_radv_prefix_t *this_prefix;
1457 radv_info = pool_elt_at_index (ip6_ra_pool, radv_indices[i]);
1459 /* We already took care of these timers... */
1460 if (radv_info->sw_if_index == primary_sw_if_index)
1464 pool_foreach (this_prefix, radv_info->adv_prefixes_pool)
1466 if (this_prefix->prefix_len == prefix_len
1467 && ip6_address_is_equal_masked (&this_prefix->prefix, address,
1470 int rv = ip6_ra_prefix (vm,
1471 radv_info->sw_if_index,
1474 0 /* use_default */,
1477 0 /* no_advertise */,
1479 0 /* no_autoconfig */,
1483 clib_warning ("ip6_neighbor_ra_prefix returned %d", rv);
1490 /* send a RA or update the timer info etc.. */
1492 ip6_ra_process_timer_event (vlib_main_t * vm,
1493 vlib_node_runtime_t * node, vlib_frame_t * frame)
1495 vnet_main_t *vnm = vnet_get_main ();
1496 ip6_ra_t *radv_info;
1497 vlib_frame_t *f = 0;
1498 u32 n_this_frame = 0;
1499 u32 n_left_to_next = 0;
1502 icmp6_router_solicitation_header_t *h0;
1504 f64 now = vlib_time_now (vm);
1506 /* Interface ip6 radv info list */
1508 pool_foreach (radv_info, ip6_ra_pool)
1510 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
1512 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
1513 radv_info->next_multicast_time = now;
1514 radv_info->last_multicast_time = now;
1515 radv_info->last_radv_time = 0;
1519 /* is it time to send a multicast RA on this interface? */
1520 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
1525 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
1526 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
1528 /* multicast send - compute next multicast send time */
1529 if( radv_info->initial_adverts_sent > 0)
1531 radv_info->initial_adverts_sent--;
1532 if(rfn > radv_info->initial_adverts_interval)
1533 rfn = radv_info->initial_adverts_interval;
1535 /* check to see if we are ceasing to send */
1536 if( radv_info->initial_adverts_sent == 0)
1537 if(radv_info->cease_radv)
1538 radv_info->send_radv = 0;
1541 radv_info->next_multicast_time = rfn + now;
1542 radv_info->last_multicast_time = now;
1544 /* send advert now - build a "solicted" router advert with unspecified source address */
1545 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
1547 if (PREDICT_FALSE(n_allocated == 0))
1549 clib_warning ("buffer allocation failure");
1552 b0 = vlib_get_buffer (vm, bo0);
1553 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
1554 b0->error = ICMP6_ERROR_NONE;
1555 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
1557 h0 = vlib_buffer_get_current (b0);
1559 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
1561 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1562 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
1563 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
1564 h0->ip.protocol = IP_PROTOCOL_ICMP6;
1565 h0->ip.hop_limit = 255;
1567 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
1568 h0->ip.src_address.as_u64[0] = 0;
1569 h0->ip.src_address.as_u64[1] = 0;
1571 h0->ip.dst_address.as_u64[0] = 0;
1572 h0->ip.dst_address.as_u64[1] = 0;
1574 h0->neighbor.icmp.type = ICMP6_router_solicitation;
1576 if (PREDICT_FALSE(f == 0))
1578 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
1579 to_next = vlib_frame_vector_args (f);
1580 n_left_to_next = VLIB_FRAME_SIZE;
1589 if (PREDICT_FALSE(n_left_to_next == 0))
1591 f->n_vectors = n_this_frame;
1592 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1601 ASSERT (n_this_frame);
1602 f->n_vectors = n_this_frame;
1603 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1609 ip6_ra_handle_report (ip6_ra_report_t * rap)
1613 vec_foreach_index (ii, ip6_ra_listeners) ip6_ra_listeners[ii] (rap);
1614 vec_free (rap->prefixes);
1615 clib_mem_free (rap);
1619 ip6_ra_event_process (vlib_main_t * vm,
1620 vlib_node_runtime_t * node, vlib_frame_t * frame)
1624 uword *event_data = 0;
1627 /* init code here */
1631 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
1633 event_type = vlib_process_get_events (vm, &event_data);
1635 if (event_type == ~0)
1637 /* No events found: timer expired. */
1638 /* process interface list and send RAs as appropriate, update timer info */
1639 ip6_ra_process_timer_event (vm, node, frame);
1643 for (i = 0; i < vec_len (event_data); i++)
1645 r = (void *) (event_data[i]);
1646 ip6_ra_handle_report (r);
1648 vec_reset_length (event_data);
1651 return frame->n_vectors;
1655 VLIB_REGISTER_NODE (ip6_ra_process_node) =
1657 .function = ip6_ra_event_process,
1658 .name = "ip6-ra-process",
1659 .type = VLIB_NODE_TYPE_PROCESS,
1664 ip6_ra_signal_report (ip6_ra_report_t * r)
1666 vlib_main_t *vm = vlib_get_main ();
1669 if (!vec_len (ip6_ra_listeners))
1672 q = clib_mem_alloc (sizeof (*q));
1675 vlib_process_signal_event (vm, ip6_ra_process_node.index, 0, (uword) q);
1679 ip6_ra_publish (ip6_ra_report_t * r)
1681 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
1682 vl_api_rpc_call_main_thread (ip6_ra_signal_report, (u8 *) r, sizeof *r);
1686 /* API support functions */
1688 ip6_ra_config (vlib_main_t * vm, u32 sw_if_index,
1689 u8 suppress, u8 managed, u8 other,
1690 u8 ll_option, u8 send_unicast, u8 cease,
1691 u8 use_lifetime, u32 lifetime,
1692 u32 initial_count, u32 initial_interval,
1693 u32 max_interval, u32 min_interval, u8 is_no)
1695 ip6_ra_t *radv_info;
1697 /* look up the radv_t information for this interface */
1698 radv_info = ip6_ra_get_itf (sw_if_index);
1701 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1703 /* Start off believing that we're going to send radv's */
1704 radv_info->send_radv = 1;
1706 if ((max_interval != 0) && (min_interval == 0))
1707 min_interval = .75 * max_interval;
1711 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
1712 radv_info->max_radv_interval;
1715 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
1716 radv_info->min_radv_interval;
1719 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
1720 radv_info->adv_router_lifetime_in_sec;
1724 if (lifetime > MAX_DEF_RTR_LIFETIME)
1725 lifetime = MAX_DEF_RTR_LIFETIME;
1727 if (lifetime <= max_interval)
1728 return VNET_API_ERROR_INVALID_VALUE;
1731 if (min_interval != 0)
1733 if ((min_interval > .75 * max_interval) || (min_interval < 3))
1734 return VNET_API_ERROR_INVALID_VALUE;
1737 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
1738 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
1739 return VNET_API_ERROR_INVALID_VALUE;
1742 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
1743 if "flag" is clear don't change corresponding value
1745 radv_info->send_radv =
1746 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
1747 radv_info->adv_managed_flag =
1748 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
1749 radv_info->adv_other_flag =
1750 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
1751 radv_info->adv_link_layer_address =
1752 (ll_option != 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
1753 radv_info->send_unicast =
1754 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
1755 radv_info->cease_radv =
1756 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
1758 radv_info->min_radv_interval = min_interval;
1759 radv_info->max_radv_interval = max_interval;
1760 radv_info->adv_router_lifetime_in_sec = lifetime;
1762 radv_info->initial_adverts_count =
1764 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
1765 radv_info->initial_adverts_count;
1766 radv_info->initial_adverts_interval =
1767 (initial_interval !=
1768 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
1769 radv_info->initial_adverts_interval;
1772 if ((cease != 0) && (is_no))
1773 radv_info->send_radv = 1;
1775 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1776 radv_info->next_multicast_time = vlib_time_now (vm);
1777 radv_info->last_multicast_time = vlib_time_now (vm);
1778 radv_info->last_radv_time = 0;
1785 ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
1786 ip6_address_t * prefix_addr, u8 prefix_len,
1787 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
1788 u8 no_advertise, u8 off_link, u8 no_autoconfig,
1789 u8 no_onlink, u8 is_no)
1791 ip6_ra_t *radv_info;
1793 /* look up the radv_t information for this interface */
1794 radv_info = ip6_ra_get_itf (sw_if_index);
1797 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1799 f64 now = vlib_time_now (vm);
1801 /* prefix info add, delete or update */
1802 ip6_radv_prefix_t *prefix;
1804 /* lookup prefix info for this address on this interface */
1805 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
1807 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
1813 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
1815 if (prefix->prefix_len != prefix_len)
1816 return VNET_API_ERROR_INVALID_VALUE_2;
1818 /* FIXME - Should the DP do this or the CP ? */
1819 /* do specific delete processing here before returning */
1820 /* try to remove from routing table */
1822 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
1824 pool_put (radv_info->adv_prefixes_pool, prefix);
1826 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1827 radv_info->next_multicast_time = vlib_time_now (vm);
1828 radv_info->last_multicast_time = vlib_time_now (vm);
1829 radv_info->last_radv_time = 0;
1833 /* adding or changing */
1838 pool_get_zero (radv_info->adv_prefixes_pool, prefix);
1839 pi = prefix - radv_info->adv_prefixes_pool;
1840 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
1843 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
1845 prefix->prefix_len = prefix_len;
1846 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
1848 /* initialize default values */
1849 prefix->adv_on_link_flag = 1; /* L bit set */
1850 prefix->adv_autonomous_flag = 1; /* A bit set */
1851 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
1852 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
1853 prefix->enabled = 1;
1854 prefix->decrement_lifetime_flag = 1;
1855 prefix->deprecated_prefix_flag = 1;
1859 /* FIXME - Should the DP do this or the CP ? */
1860 /* insert prefix into routing table as a connected prefix */
1869 if (prefix->prefix_len != prefix_len)
1870 return VNET_API_ERROR_INVALID_VALUE_2;
1874 /* FIXME - Should the DP do this or the CP ? */
1875 /* remove from routing table if already there */
1879 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
1881 prefix->adv_valid_lifetime_in_secs = ~0;
1882 prefix->adv_pref_lifetime_in_secs = ~0;
1883 prefix->decrement_lifetime_flag = 0;
1887 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
1888 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
1891 /* copy remaining */
1892 prefix->enabled = !(no_advertise != 0);
1893 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
1894 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
1898 /* fill in the expiration times */
1899 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
1900 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
1902 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1903 radv_info->next_multicast_time = vlib_time_now (vm);
1904 radv_info->last_multicast_time = vlib_time_now (vm);
1905 radv_info->last_radv_time = 0;
1911 ip6_ra_cmd (vlib_main_t * vm,
1912 unformat_input_t * main_input, vlib_cli_command_t * cmd)
1914 vnet_main_t *vnm = vnet_get_main ();
1915 clib_error_t *error = 0;
1917 u8 suppress = 0, managed = 0, other = 0;
1918 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
1919 u8 use_lifetime = 0;
1920 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
1921 0, ra_initial_interval = 0;
1922 u32 ra_max_interval = 0, ra_min_interval = 0;
1924 unformat_input_t _line_input, *line_input = &_line_input;
1926 int add_radv_info = 1;
1927 ip6_address_t ip6_addr;
1931 /* Get a line of input. */
1932 if (!unformat_user (main_input, unformat_line_input, line_input))
1935 /* get basic radv info for this interface */
1936 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1939 if (unformat_user (line_input,
1940 unformat_vnet_sw_interface, vnm, &sw_if_index))
1942 if (!ip6_ra_get_eth_itf (sw_if_index))
1945 clib_error_return (0, "Interface must be of ethernet type");
1949 if (!ip6_link_is_enabled (sw_if_index))
1951 error = clib_error_return (0, "IP6 nt enabler interface %U'",
1952 format_unformat_error, line_input);
1958 error = clib_error_return (0, "invalid interface name %U'",
1959 format_unformat_error, line_input);
1964 /* get the rest of the command */
1965 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1967 if (unformat (line_input, "no"))
1969 else if (unformat (line_input, "prefix %U/%d",
1970 unformat_ip6_address, &ip6_addr, &addr_len))
1974 else if (unformat (line_input, "ra-managed-config-flag"))
1978 else if (unformat (line_input, "ra-other-config-flag"))
1982 else if (unformat (line_input, "ra-suppress") ||
1983 unformat (line_input, "ra-surpress"))
1987 else if (unformat (line_input, "ra-suppress-link-layer") ||
1988 unformat (line_input, "ra-surpress-link-layer"))
1990 suppress_ll_option = 1;
1992 else if (unformat (line_input, "ra-send-unicast"))
1996 else if (unformat (line_input, "ra-lifetime"))
1998 if (!unformat (line_input, "%d", &ra_lifetime))
2000 error = unformat_parse_error (line_input);
2005 else if (unformat (line_input, "ra-initial"))
2008 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2010 error = unformat_parse_error (line_input);
2014 else if (unformat (line_input, "ra-interval"))
2016 if (!unformat (line_input, "%d", &ra_max_interval))
2018 error = unformat_parse_error (line_input);
2022 if (!unformat (line_input, "%d", &ra_min_interval))
2023 ra_min_interval = 0;
2025 else if (unformat (line_input, "ra-cease"))
2037 ip6_ra_config (vm, sw_if_index,
2038 suppress, managed, other,
2039 suppress_ll_option, send_unicast, cease,
2040 use_lifetime, ra_lifetime,
2041 ra_initial_count, ra_initial_interval,
2042 ra_max_interval, ra_min_interval, is_no);
2046 u32 valid_lifetime_in_secs = 0;
2047 u32 pref_lifetime_in_secs = 0;
2048 u8 use_prefix_default_values = 0;
2049 u8 no_advertise = 0;
2051 u8 no_autoconfig = 0;
2054 /* get the rest of the command */
2055 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2057 if (unformat (line_input, "default"))
2059 use_prefix_default_values = 1;
2062 else if (unformat (line_input, "infinite"))
2064 valid_lifetime_in_secs = ~0;
2065 pref_lifetime_in_secs = ~0;
2068 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2069 &pref_lifetime_in_secs))
2076 /* get the rest of the command */
2077 while (!use_prefix_default_values &&
2078 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2080 if (unformat (line_input, "no-advertise"))
2082 else if (unformat (line_input, "off-link"))
2084 else if (unformat (line_input, "no-autoconfig"))
2086 else if (unformat (line_input, "no-onlink"))
2090 error = unformat_parse_error (line_input);
2095 ip6_ra_prefix (vm, sw_if_index,
2096 &ip6_addr, addr_len,
2097 use_prefix_default_values,
2098 valid_lifetime_in_secs,
2099 pref_lifetime_in_secs,
2100 no_advertise, off_link, no_autoconfig, no_onlink, is_no);
2104 unformat_free (line_input);
2110 format_ip6_ra (u8 * s, va_list * args)
2112 index_t rai = va_arg (*args, index_t);
2113 u32 indent = va_arg (*args, u32);
2114 ip6_radv_prefix_t *p;
2115 ip6_ra_t *radv_info;
2117 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
2119 s = format (s, "%UAdvertised Prefixes:\n", format_white_space, indent);
2124 pool_foreach (p, radv_info->adv_prefixes_pool)
2126 s = format (s, "%Uprefix %U, length %d\n",
2127 format_white_space, indent+2,
2128 format_ip6_address, &p->prefix, p->prefix_len);
2132 s = format (s, "%UMTU is %d\n",
2133 format_white_space, indent, radv_info->adv_link_mtu);
2134 s = format (s, "%UICMP error messages are unlimited\n",
2135 format_white_space, indent);
2136 s = format (s, "%UICMP redirects are disabled\n",
2137 format_white_space, indent);
2138 s = format (s, "%UICMP unreachables are not sent\n",
2139 format_white_space, indent);
2140 s = format (s, "%UND DAD is disabled\n", format_white_space, indent);
2141 //s = format (s, "%UND reachable time is %d milliseconds\n",);
2142 s = format (s, "%UND advertised reachable time is %d\n",
2143 format_white_space, indent,
2144 radv_info->adv_neighbor_reachable_time_in_msec);
2146 "%UND advertised retransmit interval is %d (msec)\n",
2147 format_white_space, indent,
2149 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2152 "%UND router advertisements are sent every %0.1f seconds (min interval is %0.1f)\n",
2153 format_white_space, indent, radv_info->max_radv_interval,
2154 radv_info->min_radv_interval);
2156 format (s, "%UND router advertisements live for %d seconds\n",
2157 format_white_space, indent,
2158 radv_info->adv_router_lifetime_in_sec);
2160 format (s, "%UHosts %s stateless autoconfig for addresses\n",
2161 format_white_space, indent,
2162 (radv_info->adv_managed_flag) ? "use" : " don't use");
2164 format (s, "%UND router advertisements sent %d\n", format_white_space,
2165 indent, radv_info->n_advertisements_sent);
2167 format (s, "%UND router solicitations received %d\n", format_white_space,
2168 indent, radv_info->n_solicitations_rcvd);
2170 format (s, "%UND router solicitations dropped %d\n", format_white_space,
2171 indent, radv_info->n_solicitations_dropped);
2177 * This command is used to configure the neighbor discovery
2178 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
2179 * command to display some of the current neighbor discovery parameters
2180 * on a given interface. This command has three formats:
2183 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in
2187 * ip6 nd <interface> [no] [ra-managed-config-flag] |
2188 * [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] |
2189 * [ra-send-unicast] | [ra-lifetime <lifetime>] |
2190 * [ra-initial <cnt> <interval>] |
2191 * [ra-interval <max-interval> [<min-interval>]] | [ra-cease]
2196 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
2197 * router-advertisement messages to use stateful address
2198 * auto-configuration to obtain address information (sets the M-bit).
2199 * Default is the M-bit is not set and the '<em>no</em>' option
2200 * returns it to this default state.
2202 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
2203 * router-advertisement messages that hosts use stateful auto
2204 * configuration to obtain nonaddress related information (sets
2205 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
2206 * option returns it to this default state.
2208 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
2209 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
2210 * router-advertisement messages.
2212 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
2213 * optional source link-layer address in the ICMPv6 router-advertisement
2214 * messages. Default is to include the optional source link-layer address
2215 * and the '<em>no</em>' option returns it to this default state.
2217 * <em>[no] ra-send-unicast</em> - Use the source address of the
2218 * router-solicitation message if available. The default is to use
2219 * multicast address of all nodes, and the '<em>no</em>' option returns
2220 * it to this default state.
2222 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
2223 * default router in ICMPv6 router-advertisement messages. The range is
2224 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
2225 * '<em><max-interval></em>'. The default value is 600 seconds and the
2226 * '<em>no</em>' option returns it to this default value.
2228 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
2229 * router-advertisement messages sent and the interval between each
2230 * message. Range for count is 1 - 3 and default is 3. Range for interval
2231 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
2232 * returns both to their default value.
2234 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
2235 * interval between sending ICMPv6 router-advertisement messages. The
2236 * range for max-interval is from 4 to 200 seconds. min-interval can not
2237 * be more than 75% of max-interval. If not set, min-interval will be
2238 * set to 75% of max-interval. The range for min-interval is from 3 to
2239 * 150 seconds. The '<em>no</em>' option returns both to their default
2242 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
2243 * The '<em>no</em>' options implies to start (or restart) sending
2244 * ICMPv6 router-advertisement messages.
2247 * <b>Format 2 - Prefix Options:</b>
2250 * ip6 nd <interface> [no] prefix <ip6-address>/<width>
2251 * [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link]
2252 * [no-autoconfig] [no-onlink]
2257 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
2259 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is
2260 * the length of time in seconds during what the prefix is valid for the
2261 * purpose of on-link determination. Range is 7203 to 2592000 seconds and
2262 * default is 2592000 seconds (30 days). '<em><pref-lifetime></em>' is the
2263 * preferred-lifetime and is the length of time in seconds during what
2264 * addresses generated from the prefix remain preferred. Range is 0 to 604800
2265 * seconds and default is 604800 seconds (7 days).
2267 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and
2268 * '<em><pref-lifetime></em>' are infinite, no timeout.
2270 * <em>no-advertise</em> - Do not send full router address in prefix
2271 * advertisement. Default is to advertise (i.e. - This flag is off by default).
2273 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is
2274 * on-link (i.e. - This flag is off and L-bit in packet is set by default
2275 * and this prefix can be used for on-link determination). '<em>no-onlink</em>'
2276 * also controls the L-bit.
2278 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear
2279 * A-bit in packet. Default is autoconfig (i.e. - This flag is off and A-bit
2280 * in packet is set by default.
2282 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit
2283 * in packet. Default is on-link (i.e. - This flag is off and L-bit in packet
2284 * is set by default and this prefix can be used for on-link determination).
2285 * '<em>off-link</em>' also controls the L-bit.
2288 * <b>Format 3: - Default of Prefix:</b>
2290 * @cliexcmd{ip6 nd <interface> [no] prefix <ip6-address>/<width> default}
2292 * When a new prefix is added (or existing one is being overwritten)
2293 * <em>default</em> uses default values for the prefix. If <em>no</em> is
2294 * used, the <em>default</em> is ignored and the prefix is deleted.
2298 * Example of how set a router advertisement option:
2299 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
2300 * Example of how to add a prefix:
2301 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64
2302 * infinite no-advertise}
2303 * Example of how to delete a prefix:
2304 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
2307 VLIB_CLI_COMMAND (ip6_nd_command, static) =
2310 .short_help = "ip6 nd <interface> ...",
2311 .function = ip6_ra_cmd,
2316 * VFT for registering as a delegate to an IP6 link
2318 const static ip6_link_delegate_vft_t ip6_ra_delegate_vft = {
2319 .ildv_disable = ip6_ra_delegate_disable,
2320 .ildv_enable = ip6_ra_link_enable,
2321 .ildv_format = format_ip6_ra,
2324 static clib_error_t *
2325 ip6_ra_init (vlib_main_t * vm)
2327 vlib_call_init_function (vm, icmp6_init);
2329 icmp6_register_type (vm, ICMP6_router_solicitation,
2330 ip6_icmp_router_solicitation_node.index);
2331 icmp6_register_type (vm, ICMP6_router_advertisement,
2332 ip6_icmp_router_advertisement_node.index);
2334 ip6_ra_delegate_id = ip6_link_delegate_register (&ip6_ra_delegate_vft);
2340 VLIB_INIT_FUNCTION (ip6_ra_init) =
2342 .runs_after = VLIB_INITS("icmp6_init"),
2347 * fd.io coding-style-patch-verification: ON
2350 * eval: (c-set-style "gnu")