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
33 /* Router solicitation packet format for ethernet. */
34 typedef CLIB_PACKED (struct
37 icmp6_neighbor_discovery_header_t neighbor;
38 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
40 }) icmp6_router_solicitation_header_t;
42 /* router advertisement packet format for ethernet. */
43 typedef CLIB_PACKED (struct
46 icmp6_router_advertisement_header_t router;
47 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
49 icmp6_neighbor_discovery_mtu_option_t mtu_option;
50 icmp6_neighbor_discovery_prefix_information_option_t
52 }) icmp6_router_advertisement_packet_t;
54 #define DEF_MAX_RADV_INTERVAL 200
55 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
56 #define DEF_CURR_HOP_LIMIT 64
57 #define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
58 #define MAX_DEF_RTR_LIFETIME 9000
60 #define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
61 #define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
62 #define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
63 #define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
64 #define MAX_RA_DELAY_TIME .5 /* seconds */
66 static ip6_link_delegate_id_t ip6_ra_delegate_id;
67 static ip6_ra_t *ip6_ra_pool;
70 /* vector of registered RA report listeners */
71 static ip6_ra_report_notify_t *ip6_ra_listeners;
73 static int ip6_ra_publish (ip6_ra_report_t * r);
76 ip6_ra_report_register (ip6_ra_report_notify_t fn)
78 ip6_ra_report_notify_t *listener;
79 vec_foreach (listener, ip6_ra_listeners)
85 vec_add1 (ip6_ra_listeners, fn);
89 ip6_ra_report_unregister (ip6_ra_report_notify_t fn)
93 vec_foreach_index (ii, ip6_ra_listeners)
95 if (ip6_ra_listeners[ii] == fn)
97 vec_del1 (ip6_ra_listeners, ii);
104 ip6_ra_get_itf (u32 sw_if_index)
108 rai = ip6_link_delegate_get (sw_if_index, ip6_ra_delegate_id);
110 if (INDEX_INVALID != rai)
111 return (pool_elt_at_index (ip6_ra_pool, rai));
117 ip6_ra_adv_enabled (u32 sw_if_index)
121 ra = ip6_ra_get_itf (sw_if_index);
123 return ((ra != NULL) && (ra->send_radv != 0));
127 ip6_ra_itf_walk (ip6_ra_itf_walk_fn_t fn, void *ctx)
131 pool_foreach (radv_info, ip6_ra_pool)
133 if (WALK_STOP == fn (radv_info->sw_if_index, ctx))
138 /* for "syslogging" - use elog for now */
139 #define foreach_log_level \
141 _ (INFO, "INFORMATION") \
142 _ (NOTICE, "NOTICE") \
143 _ (WARNING, "WARNING") \
145 _ (CRIT, "CRITICAL") \
147 _ (EMERG, "EMERGENCY")
151 #define _(f,s) LOG_##f,
156 static char *log_level_strings[] = {
162 static int logmask = 1 << LOG_DEBUG;
165 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
167 /* just use elog for now */
171 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
177 what = va_format (0, fmt, &va);
179 ELOG_TYPE_DECLARE (e) =
181 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
186 ed = ELOG_DATA (vlib_get_elog_main (), e);
188 elog_string (vlib_get_elog_main (), log_level_strings[priority]);
189 ed->s[1] = elog_string (vlib_get_elog_main (), (char *) what);
195 /* ipv6 neighbor discovery - router advertisements */
198 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
199 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
200 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
201 ICMP6_ROUTER_SOLICITATION_N_NEXT,
202 } icmp6_router_solicitation_or_advertisement_next_t;
205 * Note: Both periodic RAs and solicited RS come through here.
207 static_always_inline uword
208 icmp6_router_solicitation (vlib_main_t * vm,
209 vlib_node_runtime_t * node, vlib_frame_t * frame)
211 vnet_main_t *vnm = vnet_get_main ();
212 ip6_main_t *im = &ip6_main;
213 uword n_packets = frame->n_vectors;
215 u32 n_left_from, n_left_to_next, next_index;
216 u32 n_advertisements_sent = 0;
219 icmp6_neighbor_discovery_option_type_t option_type;
221 vlib_node_runtime_t *error_node =
222 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
224 from = vlib_frame_vector_args (frame);
225 n_left_from = n_packets;
226 next_index = node->cached_next_index;
228 if (node->flags & VLIB_NODE_FLAG_TRACE)
229 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
231 sizeof (icmp6_input_trace_t));
233 /* source may append his LL address */
234 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
236 while (n_left_from > 0)
238 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
240 while (n_left_from > 0 && n_left_to_next > 0)
244 ip6_ra_t *radv_info = NULL;
246 icmp6_neighbor_discovery_header_t *h0;
247 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
249 u32 bi0, options_len0, sw_if_index0, next0, error0;
250 u32 is_solicitation = 1, is_dropped = 0;
251 u32 is_unspecified, is_link_local;
253 bi0 = to_next[0] = from[0];
260 p0 = vlib_get_buffer (vm, bi0);
261 ip0 = vlib_buffer_get_current (p0);
262 h0 = ip6_next_header (ip0);
264 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
265 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
267 ip6_address_is_link_local_unicast (&ip0->src_address);
269 error0 = ICMP6_ERROR_NONE;
270 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
272 /* check if solicitation (not from nd_timer node) */
273 if (ip6_address_is_unspecified (&ip0->dst_address))
276 /* Check that source address is unspecified, link-local or else on-link. */
277 if (!is_unspecified && !is_link_local)
279 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
281 if (ADJ_INDEX_INVALID != src_adj_index0)
283 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
285 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
287 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
292 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
296 /* check for source LL option and process */
297 o0 = (void *) (h0 + 1);
298 o0 = ((options_len0 == 8
299 && o0->header.type == option_type
300 && o0->header.n_data_u64s == 1) ? o0 : 0);
302 /* if src address unspecified IGNORE any options */
303 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
304 !is_unspecified && !is_link_local))
306 ip_neighbor_learn_t learn = {
307 .sw_if_index = sw_if_index0,
309 .ip.ip6 = ip0->src_address,
313 memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac));
314 ip_neighbor_learn_dp (&learn);
317 /* default is to drop */
318 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
320 if (error0 == ICMP6_ERROR_NONE)
322 vnet_sw_interface_t *sw_if0;
323 ethernet_interface_t *eth_if0;
326 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
327 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
329 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
331 /* only support ethernet interface type for now */
333 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
336 if (error0 == ICMP6_ERROR_NONE)
338 /* adjust the sizeof the buffer to just include the ipv6 header */
339 p0->current_length -=
341 sizeof (icmp6_neighbor_discovery_header_t));
343 radv_info = ip6_ra_get_itf (sw_if_index0);
345 error0 = ((!radv_info || 0 == radv_info->send_radv) ?
346 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
348 if (error0 == ICMP6_ERROR_NONE)
350 f64 now = vlib_time_now (vm);
352 /* for solicited adverts - need to rate limit */
355 if (0 != radv_info->last_radv_time &&
356 (now - radv_info->last_radv_time) <
357 MIN_DELAY_BETWEEN_RAS)
360 radv_info->last_radv_time = now;
364 icmp6_router_advertisement_header_t rh;
366 rh.icmp.type = ICMP6_router_advertisement;
368 rh.icmp.checksum = 0;
370 rh.current_hop_limit = radv_info->curr_hop_limit;
371 rh.router_lifetime_in_sec =
373 (radv_info->adv_router_lifetime_in_sec);
375 time_in_msec_between_retransmitted_neighbor_solicitations
377 clib_host_to_net_u32 (radv_info->
378 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
379 rh.neighbor_reachable_time_in_msec =
380 clib_host_to_net_u32 (radv_info->
381 adv_neighbor_reachable_time_in_msec);
384 (radv_info->adv_managed_flag) ?
385 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
388 ((radv_info->adv_other_flag) ?
389 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
394 sizeof (icmp6_router_advertisement_header_t);
396 if (vlib_buffer_add_data
397 (vm, &bi0, (void *) &rh,
398 sizeof (icmp6_router_advertisement_header_t)))
400 /* buffer allocation failed, drop the pkt */
401 error0 = ICMP6_ERROR_ALLOC_FAILURE;
405 if (radv_info->adv_link_layer_address)
407 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
411 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
412 h.header.n_data_u64s = 1;
414 /* copy ll address */
415 clib_memcpy (&h.ethernet_address[0],
416 ð_if0->address, 6);
418 if (vlib_buffer_add_data
419 (vm, &bi0, (void *) &h,
421 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
423 error0 = ICMP6_ERROR_ALLOC_FAILURE;
429 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
433 if (radv_info->adv_link_mtu)
435 icmp6_neighbor_discovery_mtu_option_t h;
439 clib_host_to_net_u32 (radv_info->adv_link_mtu);
440 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
441 h.header.n_data_u64s = 1;
444 sizeof (icmp6_neighbor_discovery_mtu_option_t);
446 if (vlib_buffer_add_data
447 (vm, &bi0, (void *) &h,
449 (icmp6_neighbor_discovery_mtu_option_t)))
451 error0 = ICMP6_ERROR_ALLOC_FAILURE;
456 /* add advertised prefix options */
457 ip6_radv_prefix_t *pr_info;
459 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
461 if(pr_info->enabled &&
462 (!pr_info->decrement_lifetime_flag
463 || (pr_info->pref_lifetime_expires >0)))
465 /* advertise this prefix */
466 icmp6_neighbor_discovery_prefix_information_option_t h;
468 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
469 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
471 h.dst_address_length = pr_info->prefix_len;
473 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
474 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
476 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
478 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
479 h.preferred_time = 0;
483 if(pr_info->decrement_lifetime_flag)
485 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
486 (pr_info->valid_lifetime_expires - now) : 0;
488 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
489 (pr_info->pref_lifetime_expires - now) : 0;
492 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
493 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
497 /* Handy for debugging, but too noisy... */
498 if (0 && CLIB_DEBUG > 0)
500 ("send RA for prefix %U/%d "
501 "sw_if_index %d valid %u preferred %u",
502 format_ip6_address, &pr_info->prefix,
503 pr_info->prefix_len, sw_if_index0,
504 clib_net_to_host_u32 (h.valid_time),
505 clib_net_to_host_u32 (h.preferred_time));
507 if (h.valid_time == 0)
508 clib_warning ("BUG: send RA with valid_time 0");
510 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
512 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
514 if (vlib_buffer_add_data
515 (vm, &bi0, (void *)&h,
516 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
518 error0 = ICMP6_ERROR_ALLOC_FAILURE;
525 /* add additional options before here */
527 /* finish building the router advertisement... */
528 if (!is_unspecified && radv_info->send_unicast)
530 ip0->dst_address = ip0->src_address;
534 /* target address is all-nodes mcast addr */
535 ip6_set_reserved_multicast_address
537 IP6_MULTICAST_SCOPE_link_local,
538 IP6_MULTICAST_GROUP_ID_all_hosts);
541 /* source address MUST be the link-local address */
542 ip6_address_copy (&ip0->src_address,
543 ip6_get_link_local_address
544 (radv_info->sw_if_index));
546 ip0->hop_limit = 255;
547 ip0->payload_length =
548 clib_host_to_net_u16 (payload_length);
550 icmp6_router_advertisement_header_t *rh0 =
551 (icmp6_router_advertisement_header_t *) (ip0 + 1);
553 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
555 ASSERT (bogus_length == 0);
557 /* setup output if and adjacency */
558 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
559 vnet_main.local_interface_sw_if_index;
563 ethernet_header_t *eth0;
564 /* Reuse current MAC header, copy SMAC to DMAC and
565 * interface MAC to SMAC */
566 vlib_buffer_reset (p0);
567 vlib_buffer_advance (
568 p0, vnet_buffer (p0)->l2_hdr_offset);
569 eth0 = vlib_buffer_get_current (p0);
570 clib_memcpy (eth0->dst_address, eth0->src_address,
572 clib_memcpy (eth0->src_address, ð_if0->address,
576 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
577 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
582 adj_index0 = ip6_link_get_mcast_adj (sw_if_index0);
583 if (adj_index0 == INDEX_INVALID)
584 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
589 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
590 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
594 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
596 radv_info->n_solicitations_dropped += is_dropped;
597 radv_info->n_solicitations_rcvd += is_solicitation;
599 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
601 radv_info->n_advertisements_sent++;
602 n_advertisements_sent++;
609 p0->error = error_node->errors[error0];
611 if (error0 != ICMP6_ERROR_NONE)
612 vlib_error_count (vm, error_node->node_index, error0, 1);
614 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
615 to_next, n_left_to_next,
620 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
623 /* Account for router advertisements sent. */
624 vlib_error_count (vm, error_node->node_index,
625 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
626 n_advertisements_sent);
628 return frame->n_vectors;
631 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
633 .function = icmp6_router_solicitation,
634 .name = "icmp6-router-solicitation",
636 .vector_size = sizeof (u32),
638 .format_trace = format_icmp6_input_trace,
640 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
642 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
643 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
644 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
648 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
649 static_always_inline uword
650 icmp6_router_advertisement (vlib_main_t * vm,
651 vlib_node_runtime_t * node, vlib_frame_t * frame)
653 vnet_main_t *vnm = vnet_get_main ();
654 uword n_packets = frame->n_vectors;
656 u32 n_left_from, n_left_to_next, next_index;
657 u32 n_advertisements_rcvd = 0;
659 vlib_node_runtime_t *error_node =
660 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
662 from = vlib_frame_vector_args (frame);
663 n_left_from = n_packets;
664 next_index = node->cached_next_index;
666 if (node->flags & VLIB_NODE_FLAG_TRACE)
667 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
669 sizeof (icmp6_input_trace_t));
671 while (n_left_from > 0)
673 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
675 while (n_left_from > 0 && n_left_to_next > 0)
679 ip6_ra_t *radv_info = 0;
680 icmp6_router_advertisement_header_t *h0;
681 u32 bi0, options_len0, sw_if_index0, next0, error0;
683 bi0 = to_next[0] = from[0];
690 p0 = vlib_get_buffer (vm, bi0);
691 ip0 = vlib_buffer_get_current (p0);
692 h0 = ip6_next_header (ip0);
694 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
696 error0 = ICMP6_ERROR_NONE;
697 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
699 /* Check that source address is link-local */
700 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
701 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
703 /* default is to drop */
704 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
706 n_advertisements_rcvd++;
708 if (error0 == ICMP6_ERROR_NONE)
710 vnet_sw_interface_t *sw_if0;
711 ethernet_interface_t *eth_if0;
713 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
714 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
716 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
718 /* only support ethernet interface type for now */
720 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
723 if (error0 == ICMP6_ERROR_NONE)
725 /* look up the radv_t information for this interface */
726 radv_info = ip6_ra_get_itf (sw_if_index0);
728 error0 = ((!radv_info) ?
729 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
732 if (error0 == ICMP6_ERROR_NONE)
734 radv_info->keep_sending_rs = 0;
738 r.sw_if_index = sw_if_index0;
739 memcpy (&r.router_address, &ip0->src_address, 16);
740 r.current_hop_limit = h0->current_hop_limit;
742 r.router_lifetime_in_sec =
743 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
744 r.neighbor_reachable_time_in_msec =
746 (h0->neighbor_reachable_time_in_msec);
747 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
750 /* validate advertised information */
751 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
752 && (h0->current_hop_limit !=
753 radv_info->curr_hop_limit))
755 ip6_neighbor_syslog (vm, LOG_WARNING,
756 "our AdvCurHopLimit on %U doesn't agree with %U",
757 format_vnet_sw_if_index_name,
764 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
765 != radv_info->adv_managed_flag)
767 ip6_neighbor_syslog (vm, LOG_WARNING,
768 "our AdvManagedFlag on %U doesn't agree with %U",
769 format_vnet_sw_if_index_name,
776 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
777 != radv_info->adv_other_flag)
779 ip6_neighbor_syslog (vm, LOG_WARNING,
780 "our AdvOtherConfigFlag on %U doesn't agree with %U",
781 format_vnet_sw_if_index_name,
788 time_in_msec_between_retransmitted_neighbor_solicitations
790 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
792 time_in_msec_between_retransmitted_neighbor_solicitations
794 clib_host_to_net_u32 (radv_info->
795 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
797 ip6_neighbor_syslog (vm, LOG_WARNING,
798 "our AdvRetransTimer on %U doesn't agree with %U",
799 format_vnet_sw_if_index_name,
805 if ((h0->neighbor_reachable_time_in_msec &&
806 radv_info->adv_neighbor_reachable_time_in_msec) &&
807 (h0->neighbor_reachable_time_in_msec !=
809 (radv_info->adv_neighbor_reachable_time_in_msec)))
811 ip6_neighbor_syslog (vm, LOG_WARNING,
812 "our AdvReachableTime on %U doesn't agree with %U",
813 format_vnet_sw_if_index_name,
819 /* check for MTU or prefix options or .. */
820 u8 *opt_hdr = (u8 *) (h0 + 1);
821 while (options_len0 > 0)
823 icmp6_neighbor_discovery_option_header_t *o0 =
824 (icmp6_neighbor_discovery_option_header_t *)
826 int opt_len = o0->n_data_u64s << 3;
827 icmp6_neighbor_discovery_option_type_t option_type =
830 if (options_len0 < 2)
832 ip6_neighbor_syslog (vm, LOG_ERR,
833 "malformed RA packet on %U from %U",
834 format_vnet_sw_if_index_name,
843 ip6_neighbor_syslog (vm, LOG_ERR,
844 " zero length option in RA on %U from %U",
845 format_vnet_sw_if_index_name,
851 else if (opt_len > options_len0)
853 ip6_neighbor_syslog (vm, LOG_ERR,
854 "option length in RA packet greater than total length on %U from %U",
855 format_vnet_sw_if_index_name,
862 options_len0 -= opt_len;
867 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
869 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
871 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
874 if (opt_len < sizeof (*h))
877 memcpy (r.slla, h->ethernet_address, 6);
881 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
883 icmp6_neighbor_discovery_mtu_option_t *h =
884 (icmp6_neighbor_discovery_mtu_option_t
887 if (opt_len < sizeof (*h))
890 r.mtu = clib_net_to_host_u32 (h->mtu);
892 if ((h->mtu && radv_info->adv_link_mtu) &&
895 (radv_info->adv_link_mtu)))
897 ip6_neighbor_syslog (vm, LOG_WARNING,
898 "our AdvLinkMTU on %U doesn't agree with %U",
899 format_vnet_sw_if_index_name,
907 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
909 icmp6_neighbor_discovery_prefix_information_option_t
911 (icmp6_neighbor_discovery_prefix_information_option_t
914 /* validate advertised prefix options */
915 ip6_radv_prefix_t *pr_info;
916 u32 preferred, valid;
918 if (opt_len < sizeof (*h))
921 vec_validate (r.prefixes,
922 vec_len (r.prefixes));
923 ra_report_prefix_info_t *prefix =
924 vec_elt_at_index (r.prefixes,
925 vec_len (r.prefixes) - 1);
928 clib_net_to_host_u32 (h->preferred_time);
929 valid = clib_net_to_host_u32 (h->valid_time);
931 prefix->preferred_time = preferred;
932 prefix->valid_time = valid;
933 prefix->flags = h->flags & 0xc0;
934 prefix->prefix.fp_len = h->dst_address_length;
935 prefix->prefix.fp_addr.ip6 = h->dst_address;
936 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
938 /* look for matching prefix - if we our advertising it, it better be consistant */
939 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
943 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
945 if(pr_info->enabled &&
946 (pr_info->prefix_len == h->dst_address_length) &&
947 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
950 if(!pr_info->decrement_lifetime_flag &&
951 valid != pr_info->adv_valid_lifetime_in_secs)
953 ip6_neighbor_syslog(vm, LOG_WARNING,
954 "our ADV validlifetime on %U for %U does not agree with %U",
955 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
956 format_ip6_address, &h->dst_address);
958 if(!pr_info->decrement_lifetime_flag &&
959 preferred != pr_info->adv_pref_lifetime_in_secs)
961 ip6_neighbor_syslog(vm, LOG_WARNING,
962 "our ADV preferredlifetime on %U for %U does not agree with %U",
963 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
964 format_ip6_address, &h->dst_address);
981 p0->error = error_node->errors[error0];
983 if (error0 != ICMP6_ERROR_NONE)
984 vlib_error_count (vm, error_node->node_index, error0, 1);
986 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
987 to_next, n_left_to_next,
991 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
994 /* Account for router advertisements received. */
995 vlib_error_count (vm, error_node->node_index,
996 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
997 n_advertisements_rcvd);
999 return frame->n_vectors;
1002 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
1004 .function = icmp6_router_advertisement,
1005 .name = "icmp6-router-advertisement",
1007 .vector_size = sizeof (u32),
1009 .format_trace = format_icmp6_input_trace,
1018 random_f64_from_to (f64 from, f64 to)
1020 static u32 seed = 0;
1021 static u8 seed_set = 0;
1024 seed = random_default_seed ();
1027 return random_f64 (&seed) * (to - from) + from;
1031 get_mac_address (u32 sw_if_index, u8 * address)
1033 vnet_main_t *vnm = vnet_get_main ();
1034 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1035 if (!hw_if->hw_address)
1037 clib_memcpy (address, hw_if->hw_address, 6);
1041 static inline vlib_buffer_t *
1042 create_buffer_for_rs (vlib_main_t * vm, ip6_ra_t * radv_info)
1046 icmp6_router_solicitation_header_t *rh;
1051 sw_if_index = radv_info->sw_if_index;
1053 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
1055 clib_warning ("buffer allocation failure");
1059 p0 = vlib_get_buffer (vm, bi0);
1060 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1062 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
1063 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
1065 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1066 ip6_link_get_mcast_adj (sw_if_index);
1068 rh = vlib_buffer_get_current (p0);
1069 p0->current_length = sizeof (*rh);
1071 rh->neighbor.icmp.type = ICMP6_router_solicitation;
1072 rh->neighbor.icmp.code = 0;
1073 rh->neighbor.icmp.checksum = 0;
1074 rh->neighbor.reserved_must_be_zero = 0;
1076 rh->link_layer_option.header.type =
1077 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1078 if (0 != get_mac_address (sw_if_index,
1079 rh->link_layer_option.ethernet_address))
1081 clib_warning ("interface with sw_if_index %u has no mac address",
1083 vlib_buffer_free (vm, &bi0, 1);
1086 rh->link_layer_option.header.n_data_u64s = 1;
1088 payload_length = sizeof (rh->neighbor) + sizeof (u64);
1090 rh->ip.ip_version_traffic_class_and_flow_label =
1091 clib_host_to_net_u32 (0x6 << 28);
1092 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
1093 rh->ip.protocol = IP_PROTOCOL_ICMP6;
1094 rh->ip.hop_limit = 255;
1095 ip6_address_copy (&rh->ip.src_address,
1096 ip6_get_link_local_address (radv_info->sw_if_index));
1097 /* set address ff02::2 */
1098 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
1099 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
1101 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
1109 stop_sending_rs (vlib_main_t * vm, ip6_ra_t * ra)
1113 ra->keep_sending_rs = 0;
1116 bi0 = vlib_get_buffer_index (vm, ra->buffer);
1117 vlib_buffer_free (vm, &bi0, 1);
1123 check_send_rs (vlib_main_t * vm, ip6_ra_t * radv_info, f64 current_time,
1133 icmp6_send_router_solicitation_params_t *params;
1135 if (!radv_info->keep_sending_rs)
1138 params = &radv_info->params;
1140 if (radv_info->due_time > current_time)
1142 *due_time = radv_info->due_time;
1146 p0 = radv_info->buffer;
1148 next_index = ip6_rewrite_mcast_node.index;
1150 c0 = vlib_buffer_copy (vm, p0);
1152 return radv_info->keep_sending_rs;
1154 ci0 = vlib_get_buffer_index (vm, c0);
1156 f = vlib_get_frame_to_node (vm, next_index);
1157 to_next = vlib_frame_vector_args (f);
1160 vlib_put_frame_to_node (vm, next_index, f);
1162 if (params->mrc != 0 && --radv_info->n_left == 0)
1163 stop_sending_rs (vm, radv_info);
1166 radv_info->sleep_interval =
1167 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
1168 if (radv_info->sleep_interval > params->mrt)
1169 radv_info->sleep_interval =
1170 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
1172 radv_info->due_time = current_time + radv_info->sleep_interval;
1174 if (params->mrd != 0
1175 && current_time > radv_info->start_time + params->mrd)
1176 stop_sending_rs (vm, radv_info);
1178 *due_time = radv_info->due_time;
1181 return radv_info->keep_sending_rs;
1185 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1188 uword *event_data = NULL;
1189 f64 sleep_time = 1e9;
1190 ip6_ra_t *radv_info;
1197 vlib_process_wait_for_event_or_clock (vm, sleep_time);
1198 vlib_process_get_events (vm, &event_data);
1199 vec_reset_length (event_data);
1201 current_time = vlib_time_now (vm);
1204 due_time = current_time + 1e9;
1205 pool_foreach (radv_info, ip6_ra_pool)
1207 if (check_send_rs (vm, radv_info, current_time, &dt)
1211 current_time = vlib_time_now (vm);
1213 while (due_time < current_time);
1215 sleep_time = due_time - current_time;
1221 VLIB_REGISTER_NODE (ip6_rs_process_node) = {
1222 .function = send_rs_process,
1223 .type = VLIB_NODE_TYPE_PROCESS,
1224 .name = "ip6-rs-process",
1228 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
1229 const icmp6_send_router_solicitation_params_t
1234 ASSERT (~0 != sw_if_index);
1236 ra = ip6_ra_get_itf (sw_if_index);
1241 stop_sending_rs (vm, ra);
1245 ra->keep_sending_rs = 1;
1246 ra->params = *params;
1247 ra->n_left = params->mrc;
1248 ra->start_time = vlib_time_now (vm);
1249 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
1250 ra->due_time = 0; /* send first packet ASAP */
1251 ra->buffer = create_buffer_for_rs (vm, ra);
1253 ra->keep_sending_rs = 0;
1255 vlib_process_signal_event (vm, ip6_rs_process_node.index, 1, 0);
1259 static const ethernet_interface_t *
1260 ip6_ra_get_eth_itf (u32 sw_if_index)
1262 const vnet_sw_interface_t *sw;
1264 /* lookup radv container - ethernet interfaces only */
1265 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
1266 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1267 return (ethernet_get_interface (ðernet_main, sw->hw_if_index));
1273 * @brief called when IP6 is enabled on an interface
1274 *create and initialize router advertisement parameters with default
1275 * values for this intfc
1278 ip6_ra_link_enable (u32 sw_if_index)
1280 const ethernet_interface_t *eth;
1281 ip6_ra_t *radv_info;
1283 eth = ip6_ra_get_eth_itf (sw_if_index);
1288 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
1289 ip6_ra_delegate_id));
1291 pool_get_zero (ip6_ra_pool, radv_info);
1293 radv_info->seed = (u32) clib_cpu_time_now ();
1294 random_u32 (&radv_info->seed);
1296 radv_info->sw_if_index = sw_if_index;
1297 radv_info->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1298 radv_info->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1299 radv_info->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1300 radv_info->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1302 /* send ll address source address option */
1303 radv_info->adv_link_layer_address = 1;
1305 radv_info->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1306 radv_info->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1307 radv_info->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1309 radv_info->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
1310 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1311 radv_info->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1313 /* fill in delegate for this interface that will be needed later */
1314 radv_info->adv_link_mtu =
1315 vnet_sw_interface_get_mtu (vnet_get_main (), sw_if_index, VNET_MTU_IP6);
1317 mhash_init (&radv_info->address_to_prefix_index, sizeof (uword),
1318 sizeof (ip6_address_t));
1320 ip6_link_delegate_update (sw_if_index, ip6_ra_delegate_id,
1321 radv_info - ip6_ra_pool);
1325 ip6_ra_delegate_disable (index_t rai)
1327 ip6_radv_prefix_t *p;
1328 ip6_ra_t *radv_info;
1330 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
1332 /* clean up prefix and MDP pools */
1333 pool_flush(p, radv_info->adv_prefixes_pool,
1335 mhash_unset (&radv_info->address_to_prefix_index, &p->prefix, 0);
1338 pool_free (radv_info->adv_prefixes_pool);
1340 mhash_free (&radv_info->address_to_prefix_index);
1342 pool_put (ip6_ra_pool, radv_info);
1346 ip6_ra_update_secondary_radv_info (ip6_address_t * address, u8 prefix_len,
1347 u32 primary_sw_if_index,
1348 u32 valid_time, u32 preferred_time)
1350 vlib_main_t *vm = vlib_get_main ();
1351 static u32 *radv_indices;
1352 ip6_ra_t *radv_info;
1355 ip6_address_mask_from_width (&mask, prefix_len);
1357 vec_reset_length (radv_indices);
1358 pool_foreach (radv_info, ip6_ra_pool)
1360 vec_add1 (radv_indices, radv_info - ip6_ra_pool);
1364 * If we have another customer for this prefix,
1365 * tell the RA code about it...
1367 for (i = 0; i < vec_len (radv_indices); i++)
1369 ip6_radv_prefix_t *this_prefix;
1370 radv_info = pool_elt_at_index (ip6_ra_pool, radv_indices[i]);
1372 /* We already took care of these timers... */
1373 if (radv_info->sw_if_index == primary_sw_if_index)
1376 pool_foreach (this_prefix, radv_info->adv_prefixes_pool)
1378 if (this_prefix->prefix_len == prefix_len
1379 && ip6_address_is_equal_masked (&this_prefix->prefix, address,
1382 int rv = ip6_ra_prefix (vm,
1383 radv_info->sw_if_index,
1386 0 /* use_default */,
1389 0 /* no_advertise */,
1391 0 /* no_autoconfig */,
1395 clib_warning ("ip6_neighbor_ra_prefix returned %d", rv);
1401 /* send a RA or update the timer info etc.. */
1403 ip6_ra_process_timer_event (vlib_main_t * vm,
1404 vlib_node_runtime_t * node, vlib_frame_t * frame)
1406 vnet_main_t *vnm = vnet_get_main ();
1407 ip6_ra_t *radv_info;
1408 vlib_frame_t *f = 0;
1409 u32 n_this_frame = 0;
1410 u32 n_left_to_next = 0;
1413 icmp6_router_solicitation_header_t *h0;
1415 f64 now = vlib_time_now (vm);
1417 /* Interface ip6 radv info list */
1418 pool_foreach (radv_info, ip6_ra_pool)
1420 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
1422 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
1423 radv_info->next_multicast_time = now;
1424 radv_info->last_multicast_time = now;
1425 radv_info->last_radv_time = 0;
1429 /* is it time to send a multicast RA on this interface? */
1430 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
1435 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
1436 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
1438 /* multicast send - compute next multicast send time */
1439 if( radv_info->initial_adverts_sent > 0)
1441 radv_info->initial_adverts_sent--;
1442 if(rfn > radv_info->initial_adverts_interval)
1443 rfn = radv_info->initial_adverts_interval;
1445 /* check to see if we are ceasing to send */
1446 if( radv_info->initial_adverts_sent == 0)
1447 if(radv_info->cease_radv)
1448 radv_info->send_radv = 0;
1451 radv_info->next_multicast_time = rfn + now;
1452 radv_info->last_multicast_time = now;
1454 /* send advert now - build a "solicted" router advert with unspecified source address */
1455 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
1457 if (PREDICT_FALSE(n_allocated == 0))
1459 clib_warning ("buffer allocation failure");
1462 b0 = vlib_get_buffer (vm, bo0);
1463 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
1464 b0->error = ICMP6_ERROR_NONE;
1465 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
1467 h0 = vlib_buffer_get_current (b0);
1469 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
1471 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1472 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
1473 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
1474 h0->ip.protocol = IP_PROTOCOL_ICMP6;
1475 h0->ip.hop_limit = 255;
1477 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
1478 h0->ip.src_address.as_u64[0] = 0;
1479 h0->ip.src_address.as_u64[1] = 0;
1481 h0->ip.dst_address.as_u64[0] = 0;
1482 h0->ip.dst_address.as_u64[1] = 0;
1484 h0->neighbor.icmp.type = ICMP6_router_solicitation;
1486 if (PREDICT_FALSE(f == 0))
1488 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
1489 to_next = vlib_frame_vector_args (f);
1490 n_left_to_next = VLIB_FRAME_SIZE;
1499 if (PREDICT_FALSE(n_left_to_next == 0))
1501 f->n_vectors = n_this_frame;
1502 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1510 ASSERT (n_this_frame);
1511 f->n_vectors = n_this_frame;
1512 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1518 ip6_ra_handle_report (ip6_ra_report_t * rap)
1522 vec_foreach_index (ii, ip6_ra_listeners) ip6_ra_listeners[ii] (rap);
1523 vec_free (rap->prefixes);
1524 clib_mem_free (rap);
1528 ip6_ra_event_process (vlib_main_t * vm,
1529 vlib_node_runtime_t * node, vlib_frame_t * frame)
1533 uword *event_data = 0;
1536 /* init code here */
1540 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
1542 event_type = vlib_process_get_events (vm, &event_data);
1544 if (event_type == ~0)
1546 /* No events found: timer expired. */
1547 /* process interface list and send RAs as appropriate, update timer info */
1548 ip6_ra_process_timer_event (vm, node, frame);
1552 for (i = 0; i < vec_len (event_data); i++)
1554 r = (void *) (event_data[i]);
1555 ip6_ra_handle_report (r);
1557 vec_reset_length (event_data);
1560 return frame->n_vectors;
1563 VLIB_REGISTER_NODE (ip6_ra_process_node) =
1565 .function = ip6_ra_event_process,
1566 .name = "ip6-ra-process",
1567 .type = VLIB_NODE_TYPE_PROCESS,
1571 ip6_ra_signal_report (ip6_ra_report_t * r)
1573 vlib_main_t *vm = vlib_get_main ();
1576 if (!vec_len (ip6_ra_listeners))
1579 q = clib_mem_alloc (sizeof (*q));
1582 vlib_process_signal_event (vm, ip6_ra_process_node.index, 0, (uword) q);
1586 ip6_ra_publish (ip6_ra_report_t * r)
1588 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
1589 vl_api_rpc_call_main_thread (ip6_ra_signal_report, (u8 *) r, sizeof *r);
1593 /* API support functions */
1595 ip6_ra_config (vlib_main_t * vm, u32 sw_if_index,
1596 u8 suppress, u8 managed, u8 other,
1597 u8 ll_option, u8 send_unicast, u8 cease,
1598 u8 use_lifetime, u32 lifetime,
1599 u32 initial_count, u32 initial_interval,
1600 u32 max_interval, u32 min_interval, u8 is_no)
1602 ip6_ra_t *radv_info;
1604 /* look up the radv_t information for this interface */
1605 radv_info = ip6_ra_get_itf (sw_if_index);
1608 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1610 /* Start off believing that we're going to send radv's */
1611 radv_info->send_radv = 1;
1613 if ((max_interval != 0) && (min_interval == 0))
1614 min_interval = .75 * max_interval;
1618 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
1619 radv_info->max_radv_interval;
1622 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
1623 radv_info->min_radv_interval;
1626 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
1627 radv_info->adv_router_lifetime_in_sec;
1631 if (lifetime > MAX_DEF_RTR_LIFETIME)
1632 lifetime = MAX_DEF_RTR_LIFETIME;
1634 if (lifetime <= max_interval)
1635 return VNET_API_ERROR_INVALID_VALUE;
1638 if (min_interval != 0)
1640 if ((min_interval > .75 * max_interval) || (min_interval < 3))
1641 return VNET_API_ERROR_INVALID_VALUE;
1644 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
1645 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
1646 return VNET_API_ERROR_INVALID_VALUE;
1649 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
1650 if "flag" is clear don't change corresponding value
1652 radv_info->send_radv =
1653 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
1654 radv_info->adv_managed_flag =
1655 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
1656 radv_info->adv_other_flag =
1657 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
1658 radv_info->adv_link_layer_address =
1659 (ll_option != 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
1660 radv_info->send_unicast =
1661 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
1662 radv_info->cease_radv =
1663 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
1665 radv_info->min_radv_interval = min_interval;
1666 radv_info->max_radv_interval = max_interval;
1667 radv_info->adv_router_lifetime_in_sec = lifetime;
1669 radv_info->initial_adverts_count =
1671 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
1672 radv_info->initial_adverts_count;
1673 radv_info->initial_adverts_interval =
1674 (initial_interval !=
1675 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
1676 radv_info->initial_adverts_interval;
1679 if ((cease != 0) && (is_no))
1680 radv_info->send_radv = 1;
1682 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1683 radv_info->next_multicast_time = vlib_time_now (vm);
1684 radv_info->last_multicast_time = vlib_time_now (vm);
1685 radv_info->last_radv_time = 0;
1692 ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
1693 ip6_address_t * prefix_addr, u8 prefix_len,
1694 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
1695 u8 no_advertise, u8 off_link, u8 no_autoconfig,
1696 u8 no_onlink, u8 is_no)
1698 ip6_ra_t *radv_info;
1700 /* look up the radv_t information for this interface */
1701 radv_info = ip6_ra_get_itf (sw_if_index);
1704 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1706 f64 now = vlib_time_now (vm);
1708 /* prefix info add, delete or update */
1709 ip6_radv_prefix_t *prefix;
1711 /* lookup prefix info for this address on this interface */
1712 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
1714 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
1720 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
1722 if (prefix->prefix_len != prefix_len)
1723 return VNET_API_ERROR_INVALID_VALUE_2;
1725 /* FIXME - Should the DP do this or the CP ? */
1726 /* do specific delete processing here before returning */
1727 /* try to remove from routing table */
1729 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
1731 pool_put (radv_info->adv_prefixes_pool, prefix);
1733 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1734 radv_info->next_multicast_time = vlib_time_now (vm);
1735 radv_info->last_multicast_time = vlib_time_now (vm);
1736 radv_info->last_radv_time = 0;
1740 /* adding or changing */
1745 pool_get_zero (radv_info->adv_prefixes_pool, prefix);
1746 pi = prefix - radv_info->adv_prefixes_pool;
1747 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
1750 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
1752 prefix->prefix_len = prefix_len;
1753 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
1755 /* initialize default values */
1756 prefix->adv_on_link_flag = 1; /* L bit set */
1757 prefix->adv_autonomous_flag = 1; /* A bit set */
1758 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
1759 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
1760 prefix->enabled = 1;
1761 prefix->decrement_lifetime_flag = 1;
1762 prefix->deprecated_prefix_flag = 1;
1766 /* FIXME - Should the DP do this or the CP ? */
1767 /* insert prefix into routing table as a connected prefix */
1776 if (prefix->prefix_len != prefix_len)
1777 return VNET_API_ERROR_INVALID_VALUE_2;
1781 /* FIXME - Should the DP do this or the CP ? */
1782 /* remove from routing table if already there */
1786 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
1788 prefix->adv_valid_lifetime_in_secs = ~0;
1789 prefix->adv_pref_lifetime_in_secs = ~0;
1790 prefix->decrement_lifetime_flag = 0;
1794 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
1795 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
1798 /* copy remaining */
1799 prefix->enabled = !(no_advertise != 0);
1800 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
1801 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
1805 /* fill in the expiration times */
1806 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
1807 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
1809 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1810 radv_info->next_multicast_time = vlib_time_now (vm);
1811 radv_info->last_multicast_time = vlib_time_now (vm);
1812 radv_info->last_radv_time = 0;
1818 ip6_ra_cmd (vlib_main_t * vm,
1819 unformat_input_t * main_input, vlib_cli_command_t * cmd)
1821 vnet_main_t *vnm = vnet_get_main ();
1822 clib_error_t *error = 0;
1824 u8 suppress = 0, managed = 0, other = 0;
1825 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
1826 u8 use_lifetime = 0;
1827 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
1828 0, ra_initial_interval = 0;
1829 u32 ra_max_interval = 0, ra_min_interval = 0;
1831 unformat_input_t _line_input, *line_input = &_line_input;
1833 int add_radv_info = 1;
1834 ip6_address_t ip6_addr;
1838 /* Get a line of input. */
1839 if (!unformat_user (main_input, unformat_line_input, line_input))
1842 /* get basic radv info for this interface */
1843 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1846 if (unformat_user (line_input,
1847 unformat_vnet_sw_interface, vnm, &sw_if_index))
1849 if (!ip6_ra_get_eth_itf (sw_if_index))
1852 clib_error_return (0, "Interface must be of ethernet type");
1856 if (!ip6_link_is_enabled (sw_if_index))
1858 error = clib_error_return (0, "IP6 nt enabler interface %U'",
1859 format_unformat_error, line_input);
1865 error = clib_error_return (0, "invalid interface name %U'",
1866 format_unformat_error, line_input);
1871 /* get the rest of the command */
1872 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1874 if (unformat (line_input, "no"))
1876 else if (unformat (line_input, "prefix %U/%d",
1877 unformat_ip6_address, &ip6_addr, &addr_len))
1881 else if (unformat (line_input, "ra-managed-config-flag"))
1885 else if (unformat (line_input, "ra-other-config-flag"))
1889 else if (unformat (line_input, "ra-suppress") ||
1890 unformat (line_input, "ra-surpress"))
1894 else if (unformat (line_input, "ra-suppress-link-layer") ||
1895 unformat (line_input, "ra-surpress-link-layer"))
1897 suppress_ll_option = 1;
1899 else if (unformat (line_input, "ra-send-unicast"))
1903 else if (unformat (line_input, "ra-lifetime"))
1905 if (!unformat (line_input, "%d", &ra_lifetime))
1907 error = unformat_parse_error (line_input);
1912 else if (unformat (line_input, "ra-initial"))
1915 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
1917 error = unformat_parse_error (line_input);
1921 else if (unformat (line_input, "ra-interval"))
1923 if (!unformat (line_input, "%d", &ra_max_interval))
1925 error = unformat_parse_error (line_input);
1929 if (!unformat (line_input, "%d", &ra_min_interval))
1930 ra_min_interval = 0;
1932 else if (unformat (line_input, "ra-cease"))
1944 ip6_ra_config (vm, sw_if_index,
1945 suppress, managed, other,
1946 suppress_ll_option, send_unicast, cease,
1947 use_lifetime, ra_lifetime,
1948 ra_initial_count, ra_initial_interval,
1949 ra_max_interval, ra_min_interval, is_no);
1953 u32 valid_lifetime_in_secs = 0;
1954 u32 pref_lifetime_in_secs = 0;
1955 u8 use_prefix_default_values = 0;
1956 u8 no_advertise = 0;
1958 u8 no_autoconfig = 0;
1961 /* get the rest of the command */
1962 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1964 if (unformat (line_input, "default"))
1966 use_prefix_default_values = 1;
1969 else if (unformat (line_input, "infinite"))
1971 valid_lifetime_in_secs = ~0;
1972 pref_lifetime_in_secs = ~0;
1975 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
1976 &pref_lifetime_in_secs))
1983 /* get the rest of the command */
1984 while (!use_prefix_default_values &&
1985 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1987 if (unformat (line_input, "no-advertise"))
1989 else if (unformat (line_input, "off-link"))
1991 else if (unformat (line_input, "no-autoconfig"))
1993 else if (unformat (line_input, "no-onlink"))
1997 error = unformat_parse_error (line_input);
2002 ip6_ra_prefix (vm, sw_if_index,
2003 &ip6_addr, addr_len,
2004 use_prefix_default_values,
2005 valid_lifetime_in_secs,
2006 pref_lifetime_in_secs,
2007 no_advertise, off_link, no_autoconfig, no_onlink, is_no);
2011 unformat_free (line_input);
2017 format_ip6_ra (u8 * s, va_list * args)
2019 index_t rai = va_arg (*args, index_t);
2020 u32 indent = va_arg (*args, u32);
2021 ip6_radv_prefix_t *p;
2022 ip6_ra_t *radv_info;
2024 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
2026 s = format (s, "%UAdvertised Prefixes:\n", format_white_space, indent);
2030 pool_foreach (p, radv_info->adv_prefixes_pool)
2032 s = format (s, "%Uprefix %U, length %d\n",
2033 format_white_space, indent+2,
2034 format_ip6_address, &p->prefix, p->prefix_len);
2037 s = format (s, "%UMTU is %d\n",
2038 format_white_space, indent, radv_info->adv_link_mtu);
2039 s = format (s, "%UICMP error messages are unlimited\n",
2040 format_white_space, indent);
2041 s = format (s, "%UICMP redirects are disabled\n",
2042 format_white_space, indent);
2043 s = format (s, "%UICMP unreachables are not sent\n",
2044 format_white_space, indent);
2045 s = format (s, "%UND DAD is disabled\n", format_white_space, indent);
2046 //s = format (s, "%UND reachable time is %d milliseconds\n",);
2047 s = format (s, "%UND advertised reachable time is %d\n",
2048 format_white_space, indent,
2049 radv_info->adv_neighbor_reachable_time_in_msec);
2051 "%UND advertised retransmit interval is %d (msec)\n",
2052 format_white_space, indent,
2054 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2057 "%UND router advertisements are sent every %0.1f seconds (min interval is %0.1f)\n",
2058 format_white_space, indent, radv_info->max_radv_interval,
2059 radv_info->min_radv_interval);
2061 format (s, "%UND router advertisements live for %d seconds\n",
2062 format_white_space, indent,
2063 radv_info->adv_router_lifetime_in_sec);
2065 format (s, "%UHosts %s stateless autoconfig for addresses\n",
2066 format_white_space, indent,
2067 (radv_info->adv_managed_flag) ? "use" : " don't use");
2069 format (s, "%UND router advertisements sent %d\n", format_white_space,
2070 indent, radv_info->n_advertisements_sent);
2072 format (s, "%UND router solicitations received %d\n", format_white_space,
2073 indent, radv_info->n_solicitations_rcvd);
2075 format (s, "%UND router solicitations dropped %d\n", format_white_space,
2076 indent, radv_info->n_solicitations_dropped);
2082 * This command is used to configure the neighbor discovery
2083 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
2084 * command to display some of the current neighbor discovery parameters
2085 * on a given interface. This command has three formats:
2088 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in
2092 * ip6 nd <interface> [no] [ra-managed-config-flag] |
2093 * [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] |
2094 * [ra-send-unicast] | [ra-lifetime <lifetime>] |
2095 * [ra-initial <cnt> <interval>] |
2096 * [ra-interval <max-interval> [<min-interval>]] | [ra-cease]
2101 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
2102 * router-advertisement messages to use stateful address
2103 * auto-configuration to obtain address information (sets the M-bit).
2104 * Default is the M-bit is not set and the '<em>no</em>' option
2105 * returns it to this default state.
2107 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
2108 * router-advertisement messages that hosts use stateful auto
2109 * configuration to obtain nonaddress related information (sets
2110 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
2111 * option returns it to this default state.
2113 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
2114 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
2115 * router-advertisement messages.
2117 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
2118 * optional source link-layer address in the ICMPv6 router-advertisement
2119 * messages. Default is to include the optional source link-layer address
2120 * and the '<em>no</em>' option returns it to this default state.
2122 * <em>[no] ra-send-unicast</em> - Use the source address of the
2123 * router-solicitation message if available. The default is to use
2124 * multicast address of all nodes, and the '<em>no</em>' option returns
2125 * it to this default state.
2127 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
2128 * default router in ICMPv6 router-advertisement messages. The range is
2129 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
2130 * '<em><max-interval></em>'. The default value is 600 seconds and the
2131 * '<em>no</em>' option returns it to this default value.
2133 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
2134 * router-advertisement messages sent and the interval between each
2135 * message. Range for count is 1 - 3 and default is 3. Range for interval
2136 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
2137 * returns both to their default value.
2139 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
2140 * interval between sending ICMPv6 router-advertisement messages. The
2141 * range for max-interval is from 4 to 200 seconds. min-interval can not
2142 * be more than 75% of max-interval. If not set, min-interval will be
2143 * set to 75% of max-interval. The range for min-interval is from 3 to
2144 * 150 seconds. The '<em>no</em>' option returns both to their default
2147 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
2148 * The '<em>no</em>' options implies to start (or restart) sending
2149 * ICMPv6 router-advertisement messages.
2152 * <b>Format 2 - Prefix Options:</b>
2155 * ip6 nd <interface> [no] prefix <ip6-address>/<width>
2156 * [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link]
2157 * [no-autoconfig] [no-onlink]
2162 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
2164 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is
2165 * the length of time in seconds during what the prefix is valid for the
2166 * purpose of on-link determination. Range is 7203 to 2592000 seconds and
2167 * default is 2592000 seconds (30 days). '<em><pref-lifetime></em>' is the
2168 * preferred-lifetime and is the length of time in seconds during what
2169 * addresses generated from the prefix remain preferred. Range is 0 to 604800
2170 * seconds and default is 604800 seconds (7 days).
2172 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and
2173 * '<em><pref-lifetime></em>' are infinite, no timeout.
2175 * <em>no-advertise</em> - Do not send full router address in prefix
2176 * advertisement. Default is to advertise (i.e. - This flag is off by default).
2178 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is
2179 * on-link (i.e. - This flag is off and L-bit in packet is set by default
2180 * and this prefix can be used for on-link determination). '<em>no-onlink</em>'
2181 * also controls the L-bit.
2183 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear
2184 * A-bit in packet. Default is autoconfig (i.e. - This flag is off and A-bit
2185 * in packet is set by default.
2187 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit
2188 * in packet. Default is on-link (i.e. - This flag is off and L-bit in packet
2189 * is set by default and this prefix can be used for on-link determination).
2190 * '<em>off-link</em>' also controls the L-bit.
2193 * <b>Format 3: - Default of Prefix:</b>
2195 * @cliexcmd{ip6 nd <interface> [no] prefix <ip6-address>/<width> default}
2197 * When a new prefix is added (or existing one is being overwritten)
2198 * <em>default</em> uses default values for the prefix. If <em>no</em> is
2199 * used, the <em>default</em> is ignored and the prefix is deleted.
2203 * Example of how set a router advertisement option:
2204 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
2205 * Example of how to add a prefix:
2206 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64
2207 * infinite no-advertise}
2208 * Example of how to delete a prefix:
2209 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
2211 VLIB_CLI_COMMAND (ip6_nd_command, static) =
2214 .short_help = "ip6 nd <interface> ...",
2215 .function = ip6_ra_cmd,
2219 * VFT for registering as a delegate to an IP6 link
2221 const static ip6_link_delegate_vft_t ip6_ra_delegate_vft = {
2222 .ildv_disable = ip6_ra_delegate_disable,
2223 .ildv_enable = ip6_ra_link_enable,
2224 .ildv_format = format_ip6_ra,
2227 static clib_error_t *
2228 ip6_ra_init (vlib_main_t * vm)
2230 vlib_call_init_function (vm, icmp6_init);
2232 icmp6_register_type (vm, ICMP6_router_solicitation,
2233 ip6_icmp_router_solicitation_node.index);
2234 icmp6_register_type (vm, ICMP6_router_advertisement,
2235 ip6_icmp_router_advertisement_node.index);
2237 ip6_ra_delegate_id = ip6_link_delegate_register (&ip6_ra_delegate_vft);
2242 VLIB_INIT_FUNCTION (ip6_ra_init) =
2244 .runs_after = VLIB_INITS("icmp6_init"),
2248 * fd.io coding-style-patch-verification: ON
2251 * eval: (c-set-style "gnu")