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 static ip6_link_delegate_id_t ip6_ra_delegate_id;
69 static ip6_ra_t *ip6_ra_pool;
72 /* vector of registered RA report listeners */
73 static ip6_ra_report_notify_t *ip6_ra_listeners;
75 static int ip6_ra_publish (ip6_ra_report_t * r);
78 ip6_ra_report_register (ip6_ra_report_notify_t fn)
80 ip6_ra_report_notify_t *listener;
81 vec_foreach (listener, ip6_ra_listeners)
87 vec_add1 (ip6_ra_listeners, fn);
91 ip6_ra_report_unregister (ip6_ra_report_notify_t fn)
95 vec_foreach_index (ii, ip6_ra_listeners)
97 if (ip6_ra_listeners[ii] == fn)
99 vec_del1 (ip6_ra_listeners, ii);
106 ip6_ra_get_itf (u32 sw_if_index)
110 rai = ip6_link_delegate_get (sw_if_index, ip6_ra_delegate_id);
112 if (INDEX_INVALID != rai)
113 return (pool_elt_at_index (ip6_ra_pool, rai));
119 ip6_ra_adv_enabled (u32 sw_if_index)
123 ra = ip6_ra_get_itf (sw_if_index);
125 return ((ra != NULL) && (ra->send_radv != 0));
129 ip6_ra_itf_walk (ip6_ra_itf_walk_fn_t fn, void *ctx)
133 pool_foreach (radv_info, ip6_ra_pool)
135 if (WALK_STOP == fn (radv_info->sw_if_index, ctx))
140 /* for "syslogging" - use elog for now */
141 #define foreach_log_level \
143 _ (INFO, "INFORMATION") \
144 _ (NOTICE, "NOTICE") \
145 _ (WARNING, "WARNING") \
147 _ (CRIT, "CRITICAL") \
149 _ (EMERG, "EMERGENCY")
153 #define _(f,s) LOG_##f,
158 static char *log_level_strings[] = {
164 static int logmask = 1 << LOG_DEBUG;
167 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
169 /* just use elog for now */
173 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
179 what = va_format (0, fmt, &va);
181 ELOG_TYPE_DECLARE (e) =
183 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
188 ed = ELOG_DATA (vlib_get_elog_main (), e);
190 elog_string (vlib_get_elog_main (), log_level_strings[priority]);
191 ed->s[1] = elog_string (vlib_get_elog_main (), (char *) what);
197 /* ipv6 neighbor discovery - router advertisements */
200 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
201 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
202 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
203 ICMP6_ROUTER_SOLICITATION_N_NEXT,
204 } icmp6_router_solicitation_or_advertisement_next_t;
207 * Note: Both periodic RAs and solicited RS come through here.
209 static_always_inline uword
210 icmp6_router_solicitation (vlib_main_t * vm,
211 vlib_node_runtime_t * node, vlib_frame_t * frame)
213 vnet_main_t *vnm = vnet_get_main ();
214 ip6_main_t *im = &ip6_main;
215 uword n_packets = frame->n_vectors;
217 u32 n_left_from, n_left_to_next, next_index;
218 u32 n_advertisements_sent = 0;
221 icmp6_neighbor_discovery_option_type_t option_type;
223 vlib_node_runtime_t *error_node =
224 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
226 from = vlib_frame_vector_args (frame);
227 n_left_from = n_packets;
228 next_index = node->cached_next_index;
230 if (node->flags & VLIB_NODE_FLAG_TRACE)
231 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
233 sizeof (icmp6_input_trace_t));
235 /* source may append his LL address */
236 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
238 while (n_left_from > 0)
240 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
242 while (n_left_from > 0 && n_left_to_next > 0)
246 ip6_ra_t *radv_info = NULL;
248 icmp6_neighbor_discovery_header_t *h0;
249 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
251 u32 bi0, options_len0, sw_if_index0, next0, error0;
252 u32 is_solicitation = 1, is_dropped = 0;
253 u32 is_unspecified, is_link_local;
255 bi0 = to_next[0] = from[0];
262 p0 = vlib_get_buffer (vm, bi0);
263 ip0 = vlib_buffer_get_current (p0);
264 h0 = ip6_next_header (ip0);
266 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
267 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
269 ip6_address_is_link_local_unicast (&ip0->src_address);
271 error0 = ICMP6_ERROR_NONE;
272 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
274 /* check if solicitation (not from nd_timer node) */
275 if (ip6_address_is_unspecified (&ip0->dst_address))
278 /* Check that source address is unspecified, link-local or else on-link. */
279 if (!is_unspecified && !is_link_local)
281 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
283 if (ADJ_INDEX_INVALID != src_adj_index0)
285 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
287 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
289 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
294 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
298 /* check for source LL option and process */
299 o0 = (void *) (h0 + 1);
300 o0 = ((options_len0 == 8
301 && o0->header.type == option_type
302 && o0->header.n_data_u64s == 1) ? o0 : 0);
304 /* if src address unspecified IGNORE any options */
305 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
306 !is_unspecified && !is_link_local))
309 ip_neighbor_learn_t learn = {
310 .sw_if_index = sw_if_index0,
312 .ip.ip6 = ip0->src_address,
317 memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac));
318 ip_neighbor_learn_dp (&learn);
321 /* default is to drop */
322 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
324 if (error0 == ICMP6_ERROR_NONE)
326 vnet_sw_interface_t *sw_if0;
327 ethernet_interface_t *eth_if0;
330 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
331 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
333 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
335 /* only support ethernet interface type for now */
337 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
340 if (error0 == ICMP6_ERROR_NONE)
342 /* adjust the sizeof the buffer to just include the ipv6 header */
343 p0->current_length -=
345 sizeof (icmp6_neighbor_discovery_header_t));
347 radv_info = ip6_ra_get_itf (sw_if_index0);
349 error0 = ((!radv_info || 0 == radv_info->send_radv) ?
350 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
352 if (error0 == ICMP6_ERROR_NONE)
354 f64 now = vlib_time_now (vm);
356 /* for solicited adverts - need to rate limit */
359 if (0 != radv_info->last_radv_time &&
360 (now - radv_info->last_radv_time) <
361 MIN_DELAY_BETWEEN_RAS)
364 radv_info->last_radv_time = now;
368 icmp6_router_advertisement_header_t rh;
370 rh.icmp.type = ICMP6_router_advertisement;
372 rh.icmp.checksum = 0;
374 rh.current_hop_limit = radv_info->curr_hop_limit;
375 rh.router_lifetime_in_sec =
377 (radv_info->adv_router_lifetime_in_sec);
379 time_in_msec_between_retransmitted_neighbor_solicitations
381 clib_host_to_net_u32 (radv_info->
382 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
383 rh.neighbor_reachable_time_in_msec =
384 clib_host_to_net_u32 (radv_info->
385 adv_neighbor_reachable_time_in_msec);
388 (radv_info->adv_managed_flag) ?
389 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
392 ((radv_info->adv_other_flag) ?
393 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
398 sizeof (icmp6_router_advertisement_header_t);
400 if (vlib_buffer_add_data
401 (vm, &bi0, (void *) &rh,
402 sizeof (icmp6_router_advertisement_header_t)))
404 /* buffer allocation failed, drop the pkt */
405 error0 = ICMP6_ERROR_ALLOC_FAILURE;
409 if (radv_info->adv_link_layer_address)
411 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
415 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
416 h.header.n_data_u64s = 1;
418 /* copy ll address */
419 clib_memcpy (&h.ethernet_address[0],
420 ð_if0->address, 6);
422 if (vlib_buffer_add_data
423 (vm, &bi0, (void *) &h,
425 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
427 error0 = ICMP6_ERROR_ALLOC_FAILURE;
433 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
437 if (radv_info->adv_link_mtu)
439 icmp6_neighbor_discovery_mtu_option_t h;
443 clib_host_to_net_u32 (radv_info->adv_link_mtu);
444 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
445 h.header.n_data_u64s = 1;
448 sizeof (icmp6_neighbor_discovery_mtu_option_t);
450 if (vlib_buffer_add_data
451 (vm, &bi0, (void *) &h,
453 (icmp6_neighbor_discovery_mtu_option_t)))
455 error0 = ICMP6_ERROR_ALLOC_FAILURE;
460 /* add advertised prefix options */
461 ip6_radv_prefix_t *pr_info;
464 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
466 if(pr_info->enabled &&
467 (!pr_info->decrement_lifetime_flag
468 || (pr_info->pref_lifetime_expires >0)))
470 /* advertise this prefix */
471 icmp6_neighbor_discovery_prefix_information_option_t h;
473 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
474 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
476 h.dst_address_length = pr_info->prefix_len;
478 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
479 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
481 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
483 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
484 h.preferred_time = 0;
488 if(pr_info->decrement_lifetime_flag)
490 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
491 (pr_info->valid_lifetime_expires - now) : 0;
493 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
494 (pr_info->pref_lifetime_expires - now) : 0;
497 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
498 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
502 /* Handy for debugging, but too noisy... */
503 if (0 && CLIB_DEBUG > 0)
505 ("send RA for prefix %U/%d "
506 "sw_if_index %d valid %u preferred %u",
507 format_ip6_address, &pr_info->prefix,
508 pr_info->prefix_len, sw_if_index0,
509 clib_net_to_host_u32 (h.valid_time),
510 clib_net_to_host_u32 (h.preferred_time));
512 if (h.valid_time == 0)
513 clib_warning ("BUG: send RA with valid_time 0");
515 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
517 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
519 if (vlib_buffer_add_data
520 (vm, &bi0, (void *)&h,
521 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
523 error0 = ICMP6_ERROR_ALLOC_FAILURE;
531 /* add additional options before here */
533 /* finish building the router advertisement... */
534 if (!is_unspecified && radv_info->send_unicast)
536 ip0->dst_address = ip0->src_address;
540 /* target address is all-nodes mcast addr */
541 ip6_set_reserved_multicast_address
543 IP6_MULTICAST_SCOPE_link_local,
544 IP6_MULTICAST_GROUP_ID_all_hosts);
547 /* source address MUST be the link-local address */
548 ip6_address_copy (&ip0->src_address,
549 ip6_get_link_local_address
550 (radv_info->sw_if_index));
552 ip0->hop_limit = 255;
553 ip0->payload_length =
554 clib_host_to_net_u16 (payload_length);
556 icmp6_router_advertisement_header_t *rh0 =
557 (icmp6_router_advertisement_header_t *) (ip0 + 1);
559 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
561 ASSERT (bogus_length == 0);
563 /* setup output if and adjacency */
564 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
565 vnet_main.local_interface_sw_if_index;
569 ethernet_header_t *eth0;
570 /* Reuse current MAC header, copy SMAC to DMAC and
571 * interface MAC to SMAC */
572 vlib_buffer_reset (p0);
573 vlib_buffer_advance (
574 p0, vnet_buffer (p0)->l2_hdr_offset);
575 eth0 = vlib_buffer_get_current (p0);
576 clib_memcpy (eth0->dst_address, eth0->src_address,
578 clib_memcpy (eth0->src_address, ð_if0->address,
582 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
583 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
588 adj_index0 = ip6_link_get_mcast_adj (sw_if_index0);
589 if (adj_index0 == INDEX_INVALID)
590 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
595 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
596 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
600 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
602 radv_info->n_solicitations_dropped += is_dropped;
603 radv_info->n_solicitations_rcvd += is_solicitation;
605 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
607 radv_info->n_advertisements_sent++;
608 n_advertisements_sent++;
615 p0->error = error_node->errors[error0];
617 if (error0 != ICMP6_ERROR_NONE)
618 vlib_error_count (vm, error_node->node_index, error0, 1);
620 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
621 to_next, n_left_to_next,
626 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
629 /* Account for router advertisements sent. */
630 vlib_error_count (vm, error_node->node_index,
631 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
632 n_advertisements_sent);
634 return frame->n_vectors;
638 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
640 .function = icmp6_router_solicitation,
641 .name = "icmp6-router-solicitation",
643 .vector_size = sizeof (u32),
645 .format_trace = format_icmp6_input_trace,
647 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
649 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
650 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
651 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
656 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
657 static_always_inline uword
658 icmp6_router_advertisement (vlib_main_t * vm,
659 vlib_node_runtime_t * node, vlib_frame_t * frame)
661 vnet_main_t *vnm = vnet_get_main ();
662 uword n_packets = frame->n_vectors;
664 u32 n_left_from, n_left_to_next, next_index;
665 u32 n_advertisements_rcvd = 0;
667 vlib_node_runtime_t *error_node =
668 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
670 from = vlib_frame_vector_args (frame);
671 n_left_from = n_packets;
672 next_index = node->cached_next_index;
674 if (node->flags & VLIB_NODE_FLAG_TRACE)
675 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
677 sizeof (icmp6_input_trace_t));
679 while (n_left_from > 0)
681 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
683 while (n_left_from > 0 && n_left_to_next > 0)
687 ip6_ra_t *radv_info = 0;
688 icmp6_router_advertisement_header_t *h0;
689 u32 bi0, options_len0, sw_if_index0, next0, error0;
691 bi0 = to_next[0] = from[0];
698 p0 = vlib_get_buffer (vm, bi0);
699 ip0 = vlib_buffer_get_current (p0);
700 h0 = ip6_next_header (ip0);
702 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
704 error0 = ICMP6_ERROR_NONE;
705 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
707 /* Check that source address is link-local */
708 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
709 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
711 /* default is to drop */
712 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
714 n_advertisements_rcvd++;
716 if (error0 == ICMP6_ERROR_NONE)
718 vnet_sw_interface_t *sw_if0;
719 ethernet_interface_t *eth_if0;
721 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
722 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
724 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
726 /* only support ethernet interface type for now */
728 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
731 if (error0 == ICMP6_ERROR_NONE)
733 /* look up the radv_t information for this interface */
734 radv_info = ip6_ra_get_itf (sw_if_index0);
736 error0 = ((!radv_info) ?
737 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
740 if (error0 == ICMP6_ERROR_NONE)
742 radv_info->keep_sending_rs = 0;
746 r.sw_if_index = sw_if_index0;
747 memcpy (&r.router_address, &ip0->src_address, 16);
748 r.current_hop_limit = h0->current_hop_limit;
750 r.router_lifetime_in_sec =
751 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
752 r.neighbor_reachable_time_in_msec =
754 (h0->neighbor_reachable_time_in_msec);
755 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
758 /* validate advertised information */
759 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
760 && (h0->current_hop_limit !=
761 radv_info->curr_hop_limit))
763 ip6_neighbor_syslog (vm, LOG_WARNING,
764 "our AdvCurHopLimit on %U doesn't agree with %U",
765 format_vnet_sw_if_index_name,
772 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
773 != radv_info->adv_managed_flag)
775 ip6_neighbor_syslog (vm, LOG_WARNING,
776 "our AdvManagedFlag on %U doesn't agree with %U",
777 format_vnet_sw_if_index_name,
784 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
785 != radv_info->adv_other_flag)
787 ip6_neighbor_syslog (vm, LOG_WARNING,
788 "our AdvOtherConfigFlag on %U doesn't agree with %U",
789 format_vnet_sw_if_index_name,
796 time_in_msec_between_retransmitted_neighbor_solicitations
798 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
800 time_in_msec_between_retransmitted_neighbor_solicitations
802 clib_host_to_net_u32 (radv_info->
803 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
805 ip6_neighbor_syslog (vm, LOG_WARNING,
806 "our AdvRetransTimer on %U doesn't agree with %U",
807 format_vnet_sw_if_index_name,
813 if ((h0->neighbor_reachable_time_in_msec &&
814 radv_info->adv_neighbor_reachable_time_in_msec) &&
815 (h0->neighbor_reachable_time_in_msec !=
817 (radv_info->adv_neighbor_reachable_time_in_msec)))
819 ip6_neighbor_syslog (vm, LOG_WARNING,
820 "our AdvReachableTime on %U doesn't agree with %U",
821 format_vnet_sw_if_index_name,
827 /* check for MTU or prefix options or .. */
828 u8 *opt_hdr = (u8 *) (h0 + 1);
829 while (options_len0 > 0)
831 icmp6_neighbor_discovery_option_header_t *o0 =
832 (icmp6_neighbor_discovery_option_header_t *)
834 int opt_len = o0->n_data_u64s << 3;
835 icmp6_neighbor_discovery_option_type_t option_type =
838 if (options_len0 < 2)
840 ip6_neighbor_syslog (vm, LOG_ERR,
841 "malformed RA packet on %U from %U",
842 format_vnet_sw_if_index_name,
851 ip6_neighbor_syslog (vm, LOG_ERR,
852 " zero length option in RA on %U from %U",
853 format_vnet_sw_if_index_name,
859 else if (opt_len > options_len0)
861 ip6_neighbor_syslog (vm, LOG_ERR,
862 "option length in RA packet greater than total length on %U from %U",
863 format_vnet_sw_if_index_name,
870 options_len0 -= opt_len;
875 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
877 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
879 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
882 if (opt_len < sizeof (*h))
885 memcpy (r.slla, h->ethernet_address, 6);
889 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
891 icmp6_neighbor_discovery_mtu_option_t *h =
892 (icmp6_neighbor_discovery_mtu_option_t
895 if (opt_len < sizeof (*h))
898 r.mtu = clib_net_to_host_u32 (h->mtu);
900 if ((h->mtu && radv_info->adv_link_mtu) &&
903 (radv_info->adv_link_mtu)))
905 ip6_neighbor_syslog (vm, LOG_WARNING,
906 "our AdvLinkMTU on %U doesn't agree with %U",
907 format_vnet_sw_if_index_name,
915 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
917 icmp6_neighbor_discovery_prefix_information_option_t
919 (icmp6_neighbor_discovery_prefix_information_option_t
922 /* validate advertised prefix options */
923 ip6_radv_prefix_t *pr_info;
924 u32 preferred, valid;
926 if (opt_len < sizeof (*h))
929 vec_validate (r.prefixes,
930 vec_len (r.prefixes));
931 ra_report_prefix_info_t *prefix =
932 vec_elt_at_index (r.prefixes,
933 vec_len (r.prefixes) - 1);
936 clib_net_to_host_u32 (h->preferred_time);
937 valid = clib_net_to_host_u32 (h->valid_time);
939 prefix->preferred_time = preferred;
940 prefix->valid_time = valid;
941 prefix->flags = h->flags & 0xc0;
942 prefix->prefix.fp_len = h->dst_address_length;
943 prefix->prefix.fp_addr.ip6 = h->dst_address;
944 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
946 /* look for matching prefix - if we our advertising it, it better be consistant */
948 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
952 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
954 if(pr_info->enabled &&
955 (pr_info->prefix_len == h->dst_address_length) &&
956 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
959 if(!pr_info->decrement_lifetime_flag &&
960 valid != pr_info->adv_valid_lifetime_in_secs)
962 ip6_neighbor_syslog(vm, LOG_WARNING,
963 "our ADV validlifetime on %U for %U does not agree with %U",
964 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
965 format_ip6_address, &h->dst_address);
967 if(!pr_info->decrement_lifetime_flag &&
968 preferred != pr_info->adv_pref_lifetime_in_secs)
970 ip6_neighbor_syslog(vm, LOG_WARNING,
971 "our ADV preferredlifetime on %U for %U does not agree with %U",
972 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
973 format_ip6_address, &h->dst_address);
991 p0->error = error_node->errors[error0];
993 if (error0 != ICMP6_ERROR_NONE)
994 vlib_error_count (vm, error_node->node_index, error0, 1);
996 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
997 to_next, n_left_to_next,
1001 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1004 /* Account for router advertisements received. */
1005 vlib_error_count (vm, error_node->node_index,
1006 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
1007 n_advertisements_rcvd);
1009 return frame->n_vectors;
1013 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
1015 .function = icmp6_router_advertisement,
1016 .name = "icmp6-router-advertisement",
1018 .vector_size = sizeof (u32),
1020 .format_trace = format_icmp6_input_trace,
1030 random_f64_from_to (f64 from, f64 to)
1032 static u32 seed = 0;
1033 static u8 seed_set = 0;
1036 seed = random_default_seed ();
1039 return random_f64 (&seed) * (to - from) + from;
1043 get_mac_address (u32 sw_if_index, u8 * address)
1045 vnet_main_t *vnm = vnet_get_main ();
1046 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1047 if (!hw_if->hw_address)
1049 clib_memcpy (address, hw_if->hw_address, 6);
1053 static inline vlib_buffer_t *
1054 create_buffer_for_rs (vlib_main_t * vm, ip6_ra_t * radv_info)
1058 icmp6_router_solicitation_header_t *rh;
1063 sw_if_index = radv_info->sw_if_index;
1065 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
1067 clib_warning ("buffer allocation failure");
1071 p0 = vlib_get_buffer (vm, bi0);
1072 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1074 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
1075 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
1077 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1078 ip6_link_get_mcast_adj (sw_if_index);
1080 rh = vlib_buffer_get_current (p0);
1081 p0->current_length = sizeof (*rh);
1083 rh->neighbor.icmp.type = ICMP6_router_solicitation;
1084 rh->neighbor.icmp.code = 0;
1085 rh->neighbor.icmp.checksum = 0;
1086 rh->neighbor.reserved_must_be_zero = 0;
1088 rh->link_layer_option.header.type =
1089 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1090 if (0 != get_mac_address (sw_if_index,
1091 rh->link_layer_option.ethernet_address))
1093 clib_warning ("interface with sw_if_index %u has no mac address",
1095 vlib_buffer_free (vm, &bi0, 1);
1098 rh->link_layer_option.header.n_data_u64s = 1;
1100 payload_length = sizeof (rh->neighbor) + sizeof (u64);
1102 rh->ip.ip_version_traffic_class_and_flow_label =
1103 clib_host_to_net_u32 (0x6 << 28);
1104 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
1105 rh->ip.protocol = IP_PROTOCOL_ICMP6;
1106 rh->ip.hop_limit = 255;
1107 ip6_address_copy (&rh->ip.src_address,
1108 ip6_get_link_local_address (radv_info->sw_if_index));
1109 /* set address ff02::2 */
1110 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
1111 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
1113 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
1121 stop_sending_rs (vlib_main_t * vm, ip6_ra_t * ra)
1125 ra->keep_sending_rs = 0;
1128 bi0 = vlib_get_buffer_index (vm, ra->buffer);
1129 vlib_buffer_free (vm, &bi0, 1);
1135 check_send_rs (vlib_main_t * vm, ip6_ra_t * radv_info, f64 current_time,
1145 icmp6_send_router_solicitation_params_t *params;
1147 if (!radv_info->keep_sending_rs)
1150 params = &radv_info->params;
1152 if (radv_info->due_time > current_time)
1154 *due_time = radv_info->due_time;
1158 p0 = radv_info->buffer;
1160 next_index = ip6_rewrite_mcast_node.index;
1162 c0 = vlib_buffer_copy (vm, p0);
1164 return radv_info->keep_sending_rs;
1166 ci0 = vlib_get_buffer_index (vm, c0);
1168 f = vlib_get_frame_to_node (vm, next_index);
1169 to_next = vlib_frame_vector_args (f);
1172 vlib_put_frame_to_node (vm, next_index, f);
1174 if (params->mrc != 0 && --radv_info->n_left == 0)
1175 stop_sending_rs (vm, radv_info);
1178 radv_info->sleep_interval =
1179 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
1180 if (radv_info->sleep_interval > params->mrt)
1181 radv_info->sleep_interval =
1182 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
1184 radv_info->due_time = current_time + radv_info->sleep_interval;
1186 if (params->mrd != 0
1187 && current_time > radv_info->start_time + params->mrd)
1188 stop_sending_rs (vm, radv_info);
1190 *due_time = radv_info->due_time;
1193 return radv_info->keep_sending_rs;
1197 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1200 uword *event_data = NULL;
1201 f64 sleep_time = 1e9;
1202 ip6_ra_t *radv_info;
1209 vlib_process_wait_for_event_or_clock (vm, sleep_time);
1210 vlib_process_get_events (vm, &event_data);
1211 vec_reset_length (event_data);
1213 current_time = vlib_time_now (vm);
1216 due_time = current_time + 1e9;
1218 pool_foreach (radv_info, ip6_ra_pool)
1220 if (check_send_rs (vm, radv_info, current_time, &dt)
1225 current_time = vlib_time_now (vm);
1227 while (due_time < current_time);
1229 sleep_time = due_time - current_time;
1236 VLIB_REGISTER_NODE (ip6_rs_process_node) = {
1237 .function = send_rs_process,
1238 .type = VLIB_NODE_TYPE_PROCESS,
1239 .name = "ip6-rs-process",
1244 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
1245 const icmp6_send_router_solicitation_params_t
1250 ASSERT (~0 != sw_if_index);
1252 ra = ip6_ra_get_itf (sw_if_index);
1257 stop_sending_rs (vm, ra);
1261 ra->keep_sending_rs = 1;
1262 ra->params = *params;
1263 ra->n_left = params->mrc;
1264 ra->start_time = vlib_time_now (vm);
1265 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
1266 ra->due_time = 0; /* send first packet ASAP */
1267 ra->buffer = create_buffer_for_rs (vm, ra);
1269 ra->keep_sending_rs = 0;
1271 vlib_process_signal_event (vm, ip6_rs_process_node.index, 1, 0);
1275 static const ethernet_interface_t *
1276 ip6_ra_get_eth_itf (u32 sw_if_index)
1278 const vnet_sw_interface_t *sw;
1280 /* lookup radv container - ethernet interfaces only */
1281 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
1282 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1283 return (ethernet_get_interface (ðernet_main, sw->hw_if_index));
1289 * @brief called when IP6 is enabled on an interface
1290 *create and initialize router advertisement parameters with default
1291 * values for this intfc
1294 ip6_ra_link_enable (u32 sw_if_index)
1296 const ethernet_interface_t *eth;
1297 ip6_ra_t *radv_info;
1299 eth = ip6_ra_get_eth_itf (sw_if_index);
1304 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
1305 ip6_ra_delegate_id));
1307 pool_get_zero (ip6_ra_pool, radv_info);
1309 radv_info->seed = (u32) clib_cpu_time_now ();
1310 random_u32 (&radv_info->seed);
1312 radv_info->sw_if_index = sw_if_index;
1313 radv_info->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1314 radv_info->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1315 radv_info->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1316 radv_info->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1318 /* send ll address source address option */
1319 radv_info->adv_link_layer_address = 1;
1321 radv_info->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1322 radv_info->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1323 radv_info->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1325 radv_info->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
1326 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1327 radv_info->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1329 /* fill in delegate for this interface that will be needed later */
1330 radv_info->adv_link_mtu =
1331 vnet_sw_interface_get_mtu (vnet_get_main (), sw_if_index, VNET_MTU_IP6);
1333 mhash_init (&radv_info->address_to_prefix_index, sizeof (uword),
1334 sizeof (ip6_address_t));
1336 ip6_link_delegate_update (sw_if_index, ip6_ra_delegate_id,
1337 radv_info - ip6_ra_pool);
1341 ip6_ra_delegate_disable (index_t rai)
1343 ip6_radv_prefix_t *p;
1344 ip6_ra_t *radv_info;
1346 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
1348 /* clean up prefix and MDP pools */
1350 pool_flush(p, radv_info->adv_prefixes_pool,
1352 mhash_unset (&radv_info->address_to_prefix_index, &p->prefix, 0);
1356 pool_free (radv_info->adv_prefixes_pool);
1358 mhash_free (&radv_info->address_to_prefix_index);
1360 pool_put (ip6_ra_pool, radv_info);
1364 ip6_ra_update_secondary_radv_info (ip6_address_t * address, u8 prefix_len,
1365 u32 primary_sw_if_index,
1366 u32 valid_time, u32 preferred_time)
1368 vlib_main_t *vm = vlib_get_main ();
1369 static u32 *radv_indices;
1370 ip6_ra_t *radv_info;
1373 ip6_address_mask_from_width (&mask, prefix_len);
1375 vec_reset_length (radv_indices);
1377 pool_foreach (radv_info, ip6_ra_pool)
1379 vec_add1 (radv_indices, radv_info - ip6_ra_pool);
1384 * If we have another customer for this prefix,
1385 * tell the RA code about it...
1387 for (i = 0; i < vec_len (radv_indices); i++)
1389 ip6_radv_prefix_t *this_prefix;
1390 radv_info = pool_elt_at_index (ip6_ra_pool, radv_indices[i]);
1392 /* We already took care of these timers... */
1393 if (radv_info->sw_if_index == primary_sw_if_index)
1397 pool_foreach (this_prefix, radv_info->adv_prefixes_pool)
1399 if (this_prefix->prefix_len == prefix_len
1400 && ip6_address_is_equal_masked (&this_prefix->prefix, address,
1403 int rv = ip6_ra_prefix (vm,
1404 radv_info->sw_if_index,
1407 0 /* use_default */,
1410 0 /* no_advertise */,
1412 0 /* no_autoconfig */,
1416 clib_warning ("ip6_neighbor_ra_prefix returned %d", rv);
1423 /* send a RA or update the timer info etc.. */
1425 ip6_ra_process_timer_event (vlib_main_t * vm,
1426 vlib_node_runtime_t * node, vlib_frame_t * frame)
1428 vnet_main_t *vnm = vnet_get_main ();
1429 ip6_ra_t *radv_info;
1430 vlib_frame_t *f = 0;
1431 u32 n_this_frame = 0;
1432 u32 n_left_to_next = 0;
1435 icmp6_router_solicitation_header_t *h0;
1437 f64 now = vlib_time_now (vm);
1439 /* Interface ip6 radv info list */
1441 pool_foreach (radv_info, ip6_ra_pool)
1443 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
1445 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
1446 radv_info->next_multicast_time = now;
1447 radv_info->last_multicast_time = now;
1448 radv_info->last_radv_time = 0;
1452 /* is it time to send a multicast RA on this interface? */
1453 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
1458 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
1459 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
1461 /* multicast send - compute next multicast send time */
1462 if( radv_info->initial_adverts_sent > 0)
1464 radv_info->initial_adverts_sent--;
1465 if(rfn > radv_info->initial_adverts_interval)
1466 rfn = radv_info->initial_adverts_interval;
1468 /* check to see if we are ceasing to send */
1469 if( radv_info->initial_adverts_sent == 0)
1470 if(radv_info->cease_radv)
1471 radv_info->send_radv = 0;
1474 radv_info->next_multicast_time = rfn + now;
1475 radv_info->last_multicast_time = now;
1477 /* send advert now - build a "solicted" router advert with unspecified source address */
1478 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
1480 if (PREDICT_FALSE(n_allocated == 0))
1482 clib_warning ("buffer allocation failure");
1485 b0 = vlib_get_buffer (vm, bo0);
1486 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
1487 b0->error = ICMP6_ERROR_NONE;
1488 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
1490 h0 = vlib_buffer_get_current (b0);
1492 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
1494 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1495 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
1496 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
1497 h0->ip.protocol = IP_PROTOCOL_ICMP6;
1498 h0->ip.hop_limit = 255;
1500 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
1501 h0->ip.src_address.as_u64[0] = 0;
1502 h0->ip.src_address.as_u64[1] = 0;
1504 h0->ip.dst_address.as_u64[0] = 0;
1505 h0->ip.dst_address.as_u64[1] = 0;
1507 h0->neighbor.icmp.type = ICMP6_router_solicitation;
1509 if (PREDICT_FALSE(f == 0))
1511 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
1512 to_next = vlib_frame_vector_args (f);
1513 n_left_to_next = VLIB_FRAME_SIZE;
1522 if (PREDICT_FALSE(n_left_to_next == 0))
1524 f->n_vectors = n_this_frame;
1525 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1534 ASSERT (n_this_frame);
1535 f->n_vectors = n_this_frame;
1536 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1542 ip6_ra_handle_report (ip6_ra_report_t * rap)
1546 vec_foreach_index (ii, ip6_ra_listeners) ip6_ra_listeners[ii] (rap);
1547 vec_free (rap->prefixes);
1548 clib_mem_free (rap);
1552 ip6_ra_event_process (vlib_main_t * vm,
1553 vlib_node_runtime_t * node, vlib_frame_t * frame)
1557 uword *event_data = 0;
1560 /* init code here */
1564 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
1566 event_type = vlib_process_get_events (vm, &event_data);
1568 if (event_type == ~0)
1570 /* No events found: timer expired. */
1571 /* process interface list and send RAs as appropriate, update timer info */
1572 ip6_ra_process_timer_event (vm, node, frame);
1576 for (i = 0; i < vec_len (event_data); i++)
1578 r = (void *) (event_data[i]);
1579 ip6_ra_handle_report (r);
1581 vec_reset_length (event_data);
1584 return frame->n_vectors;
1588 VLIB_REGISTER_NODE (ip6_ra_process_node) =
1590 .function = ip6_ra_event_process,
1591 .name = "ip6-ra-process",
1592 .type = VLIB_NODE_TYPE_PROCESS,
1597 ip6_ra_signal_report (ip6_ra_report_t * r)
1599 vlib_main_t *vm = vlib_get_main ();
1602 if (!vec_len (ip6_ra_listeners))
1605 q = clib_mem_alloc (sizeof (*q));
1608 vlib_process_signal_event (vm, ip6_ra_process_node.index, 0, (uword) q);
1612 ip6_ra_publish (ip6_ra_report_t * r)
1614 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
1615 vl_api_rpc_call_main_thread (ip6_ra_signal_report, (u8 *) r, sizeof *r);
1619 /* API support functions */
1621 ip6_ra_config (vlib_main_t * vm, u32 sw_if_index,
1622 u8 suppress, u8 managed, u8 other,
1623 u8 ll_option, u8 send_unicast, u8 cease,
1624 u8 use_lifetime, u32 lifetime,
1625 u32 initial_count, u32 initial_interval,
1626 u32 max_interval, u32 min_interval, u8 is_no)
1628 ip6_ra_t *radv_info;
1630 /* look up the radv_t information for this interface */
1631 radv_info = ip6_ra_get_itf (sw_if_index);
1634 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1636 /* Start off believing that we're going to send radv's */
1637 radv_info->send_radv = 1;
1639 if ((max_interval != 0) && (min_interval == 0))
1640 min_interval = .75 * max_interval;
1644 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
1645 radv_info->max_radv_interval;
1648 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
1649 radv_info->min_radv_interval;
1652 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
1653 radv_info->adv_router_lifetime_in_sec;
1657 if (lifetime > MAX_DEF_RTR_LIFETIME)
1658 lifetime = MAX_DEF_RTR_LIFETIME;
1660 if (lifetime <= max_interval)
1661 return VNET_API_ERROR_INVALID_VALUE;
1664 if (min_interval != 0)
1666 if ((min_interval > .75 * max_interval) || (min_interval < 3))
1667 return VNET_API_ERROR_INVALID_VALUE;
1670 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
1671 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
1672 return VNET_API_ERROR_INVALID_VALUE;
1675 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
1676 if "flag" is clear don't change corresponding value
1678 radv_info->send_radv =
1679 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
1680 radv_info->adv_managed_flag =
1681 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
1682 radv_info->adv_other_flag =
1683 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
1684 radv_info->adv_link_layer_address =
1685 (ll_option != 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
1686 radv_info->send_unicast =
1687 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
1688 radv_info->cease_radv =
1689 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
1691 radv_info->min_radv_interval = min_interval;
1692 radv_info->max_radv_interval = max_interval;
1693 radv_info->adv_router_lifetime_in_sec = lifetime;
1695 radv_info->initial_adverts_count =
1697 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
1698 radv_info->initial_adverts_count;
1699 radv_info->initial_adverts_interval =
1700 (initial_interval !=
1701 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
1702 radv_info->initial_adverts_interval;
1705 if ((cease != 0) && (is_no))
1706 radv_info->send_radv = 1;
1708 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1709 radv_info->next_multicast_time = vlib_time_now (vm);
1710 radv_info->last_multicast_time = vlib_time_now (vm);
1711 radv_info->last_radv_time = 0;
1718 ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
1719 ip6_address_t * prefix_addr, u8 prefix_len,
1720 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
1721 u8 no_advertise, u8 off_link, u8 no_autoconfig,
1722 u8 no_onlink, u8 is_no)
1724 ip6_ra_t *radv_info;
1726 /* look up the radv_t information for this interface */
1727 radv_info = ip6_ra_get_itf (sw_if_index);
1730 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1732 f64 now = vlib_time_now (vm);
1734 /* prefix info add, delete or update */
1735 ip6_radv_prefix_t *prefix;
1737 /* lookup prefix info for this address on this interface */
1738 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
1740 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
1746 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
1748 if (prefix->prefix_len != prefix_len)
1749 return VNET_API_ERROR_INVALID_VALUE_2;
1751 /* FIXME - Should the DP do this or the CP ? */
1752 /* do specific delete processing here before returning */
1753 /* try to remove from routing table */
1755 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
1757 pool_put (radv_info->adv_prefixes_pool, prefix);
1759 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1760 radv_info->next_multicast_time = vlib_time_now (vm);
1761 radv_info->last_multicast_time = vlib_time_now (vm);
1762 radv_info->last_radv_time = 0;
1766 /* adding or changing */
1771 pool_get_zero (radv_info->adv_prefixes_pool, prefix);
1772 pi = prefix - radv_info->adv_prefixes_pool;
1773 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
1776 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
1778 prefix->prefix_len = prefix_len;
1779 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
1781 /* initialize default values */
1782 prefix->adv_on_link_flag = 1; /* L bit set */
1783 prefix->adv_autonomous_flag = 1; /* A bit set */
1784 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
1785 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
1786 prefix->enabled = 1;
1787 prefix->decrement_lifetime_flag = 1;
1788 prefix->deprecated_prefix_flag = 1;
1792 /* FIXME - Should the DP do this or the CP ? */
1793 /* insert prefix into routing table as a connected prefix */
1802 if (prefix->prefix_len != prefix_len)
1803 return VNET_API_ERROR_INVALID_VALUE_2;
1807 /* FIXME - Should the DP do this or the CP ? */
1808 /* remove from routing table if already there */
1812 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
1814 prefix->adv_valid_lifetime_in_secs = ~0;
1815 prefix->adv_pref_lifetime_in_secs = ~0;
1816 prefix->decrement_lifetime_flag = 0;
1820 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
1821 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
1824 /* copy remaining */
1825 prefix->enabled = !(no_advertise != 0);
1826 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
1827 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
1831 /* fill in the expiration times */
1832 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
1833 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
1835 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1836 radv_info->next_multicast_time = vlib_time_now (vm);
1837 radv_info->last_multicast_time = vlib_time_now (vm);
1838 radv_info->last_radv_time = 0;
1844 ip6_ra_cmd (vlib_main_t * vm,
1845 unformat_input_t * main_input, vlib_cli_command_t * cmd)
1847 vnet_main_t *vnm = vnet_get_main ();
1848 clib_error_t *error = 0;
1850 u8 suppress = 0, managed = 0, other = 0;
1851 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
1852 u8 use_lifetime = 0;
1853 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
1854 0, ra_initial_interval = 0;
1855 u32 ra_max_interval = 0, ra_min_interval = 0;
1857 unformat_input_t _line_input, *line_input = &_line_input;
1859 int add_radv_info = 1;
1860 ip6_address_t ip6_addr;
1864 /* Get a line of input. */
1865 if (!unformat_user (main_input, unformat_line_input, line_input))
1868 /* get basic radv info for this interface */
1869 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1872 if (unformat_user (line_input,
1873 unformat_vnet_sw_interface, vnm, &sw_if_index))
1875 if (!ip6_ra_get_eth_itf (sw_if_index))
1878 clib_error_return (0, "Interface must be of ethernet type");
1882 if (!ip6_link_is_enabled (sw_if_index))
1884 error = clib_error_return (0, "IP6 nt enabler interface %U'",
1885 format_unformat_error, line_input);
1891 error = clib_error_return (0, "invalid interface name %U'",
1892 format_unformat_error, line_input);
1897 /* get the rest of the command */
1898 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1900 if (unformat (line_input, "no"))
1902 else if (unformat (line_input, "prefix %U/%d",
1903 unformat_ip6_address, &ip6_addr, &addr_len))
1907 else if (unformat (line_input, "ra-managed-config-flag"))
1911 else if (unformat (line_input, "ra-other-config-flag"))
1915 else if (unformat (line_input, "ra-suppress") ||
1916 unformat (line_input, "ra-surpress"))
1920 else if (unformat (line_input, "ra-suppress-link-layer") ||
1921 unformat (line_input, "ra-surpress-link-layer"))
1923 suppress_ll_option = 1;
1925 else if (unformat (line_input, "ra-send-unicast"))
1929 else if (unformat (line_input, "ra-lifetime"))
1931 if (!unformat (line_input, "%d", &ra_lifetime))
1933 error = unformat_parse_error (line_input);
1938 else if (unformat (line_input, "ra-initial"))
1941 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
1943 error = unformat_parse_error (line_input);
1947 else if (unformat (line_input, "ra-interval"))
1949 if (!unformat (line_input, "%d", &ra_max_interval))
1951 error = unformat_parse_error (line_input);
1955 if (!unformat (line_input, "%d", &ra_min_interval))
1956 ra_min_interval = 0;
1958 else if (unformat (line_input, "ra-cease"))
1970 ip6_ra_config (vm, sw_if_index,
1971 suppress, managed, other,
1972 suppress_ll_option, send_unicast, cease,
1973 use_lifetime, ra_lifetime,
1974 ra_initial_count, ra_initial_interval,
1975 ra_max_interval, ra_min_interval, is_no);
1979 u32 valid_lifetime_in_secs = 0;
1980 u32 pref_lifetime_in_secs = 0;
1981 u8 use_prefix_default_values = 0;
1982 u8 no_advertise = 0;
1984 u8 no_autoconfig = 0;
1987 /* get the rest of the command */
1988 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1990 if (unformat (line_input, "default"))
1992 use_prefix_default_values = 1;
1995 else if (unformat (line_input, "infinite"))
1997 valid_lifetime_in_secs = ~0;
1998 pref_lifetime_in_secs = ~0;
2001 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2002 &pref_lifetime_in_secs))
2009 /* get the rest of the command */
2010 while (!use_prefix_default_values &&
2011 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2013 if (unformat (line_input, "no-advertise"))
2015 else if (unformat (line_input, "off-link"))
2017 else if (unformat (line_input, "no-autoconfig"))
2019 else if (unformat (line_input, "no-onlink"))
2023 error = unformat_parse_error (line_input);
2028 ip6_ra_prefix (vm, sw_if_index,
2029 &ip6_addr, addr_len,
2030 use_prefix_default_values,
2031 valid_lifetime_in_secs,
2032 pref_lifetime_in_secs,
2033 no_advertise, off_link, no_autoconfig, no_onlink, is_no);
2037 unformat_free (line_input);
2043 format_ip6_ra (u8 * s, va_list * args)
2045 index_t rai = va_arg (*args, index_t);
2046 u32 indent = va_arg (*args, u32);
2047 ip6_radv_prefix_t *p;
2048 ip6_ra_t *radv_info;
2050 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
2052 s = format (s, "%UAdvertised Prefixes:\n", format_white_space, indent);
2057 pool_foreach (p, radv_info->adv_prefixes_pool)
2059 s = format (s, "%Uprefix %U, length %d\n",
2060 format_white_space, indent+2,
2061 format_ip6_address, &p->prefix, p->prefix_len);
2065 s = format (s, "%UMTU is %d\n",
2066 format_white_space, indent, radv_info->adv_link_mtu);
2067 s = format (s, "%UICMP error messages are unlimited\n",
2068 format_white_space, indent);
2069 s = format (s, "%UICMP redirects are disabled\n",
2070 format_white_space, indent);
2071 s = format (s, "%UICMP unreachables are not sent\n",
2072 format_white_space, indent);
2073 s = format (s, "%UND DAD is disabled\n", format_white_space, indent);
2074 //s = format (s, "%UND reachable time is %d milliseconds\n",);
2075 s = format (s, "%UND advertised reachable time is %d\n",
2076 format_white_space, indent,
2077 radv_info->adv_neighbor_reachable_time_in_msec);
2079 "%UND advertised retransmit interval is %d (msec)\n",
2080 format_white_space, indent,
2082 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2085 "%UND router advertisements are sent every %0.1f seconds (min interval is %0.1f)\n",
2086 format_white_space, indent, radv_info->max_radv_interval,
2087 radv_info->min_radv_interval);
2089 format (s, "%UND router advertisements live for %d seconds\n",
2090 format_white_space, indent,
2091 radv_info->adv_router_lifetime_in_sec);
2093 format (s, "%UHosts %s stateless autoconfig for addresses\n",
2094 format_white_space, indent,
2095 (radv_info->adv_managed_flag) ? "use" : " don't use");
2097 format (s, "%UND router advertisements sent %d\n", format_white_space,
2098 indent, radv_info->n_advertisements_sent);
2100 format (s, "%UND router solicitations received %d\n", format_white_space,
2101 indent, radv_info->n_solicitations_rcvd);
2103 format (s, "%UND router solicitations dropped %d\n", format_white_space,
2104 indent, radv_info->n_solicitations_dropped);
2110 * This command is used to configure the neighbor discovery
2111 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
2112 * command to display some of the current neighbor discovery parameters
2113 * on a given interface. This command has three formats:
2116 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in
2120 * ip6 nd <interface> [no] [ra-managed-config-flag] |
2121 * [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] |
2122 * [ra-send-unicast] | [ra-lifetime <lifetime>] |
2123 * [ra-initial <cnt> <interval>] |
2124 * [ra-interval <max-interval> [<min-interval>]] | [ra-cease]
2129 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
2130 * router-advertisement messages to use stateful address
2131 * auto-configuration to obtain address information (sets the M-bit).
2132 * Default is the M-bit is not set and the '<em>no</em>' option
2133 * returns it to this default state.
2135 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
2136 * router-advertisement messages that hosts use stateful auto
2137 * configuration to obtain nonaddress related information (sets
2138 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
2139 * option returns it to this default state.
2141 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
2142 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
2143 * router-advertisement messages.
2145 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
2146 * optional source link-layer address in the ICMPv6 router-advertisement
2147 * messages. Default is to include the optional source link-layer address
2148 * and the '<em>no</em>' option returns it to this default state.
2150 * <em>[no] ra-send-unicast</em> - Use the source address of the
2151 * router-solicitation message if available. The default is to use
2152 * multicast address of all nodes, and the '<em>no</em>' option returns
2153 * it to this default state.
2155 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
2156 * default router in ICMPv6 router-advertisement messages. The range is
2157 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
2158 * '<em><max-interval></em>'. The default value is 600 seconds and the
2159 * '<em>no</em>' option returns it to this default value.
2161 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
2162 * router-advertisement messages sent and the interval between each
2163 * message. Range for count is 1 - 3 and default is 3. Range for interval
2164 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
2165 * returns both to their default value.
2167 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
2168 * interval between sending ICMPv6 router-advertisement messages. The
2169 * range for max-interval is from 4 to 200 seconds. min-interval can not
2170 * be more than 75% of max-interval. If not set, min-interval will be
2171 * set to 75% of max-interval. The range for min-interval is from 3 to
2172 * 150 seconds. The '<em>no</em>' option returns both to their default
2175 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
2176 * The '<em>no</em>' options implies to start (or restart) sending
2177 * ICMPv6 router-advertisement messages.
2180 * <b>Format 2 - Prefix Options:</b>
2183 * ip6 nd <interface> [no] prefix <ip6-address>/<width>
2184 * [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link]
2185 * [no-autoconfig] [no-onlink]
2190 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
2192 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is
2193 * the length of time in seconds during what the prefix is valid for the
2194 * purpose of on-link determination. Range is 7203 to 2592000 seconds and
2195 * default is 2592000 seconds (30 days). '<em><pref-lifetime></em>' is the
2196 * preferred-lifetime and is the length of time in seconds during what
2197 * addresses generated from the prefix remain preferred. Range is 0 to 604800
2198 * seconds and default is 604800 seconds (7 days).
2200 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and
2201 * '<em><pref-lifetime></em>' are infinite, no timeout.
2203 * <em>no-advertise</em> - Do not send full router address in prefix
2204 * advertisement. Default is to advertise (i.e. - This flag is off by default).
2206 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is
2207 * on-link (i.e. - This flag is off and L-bit in packet is set by default
2208 * and this prefix can be used for on-link determination). '<em>no-onlink</em>'
2209 * also controls the L-bit.
2211 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear
2212 * A-bit in packet. Default is autoconfig (i.e. - This flag is off and A-bit
2213 * in packet is set by default.
2215 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit
2216 * in packet. Default is on-link (i.e. - This flag is off and L-bit in packet
2217 * is set by default and this prefix can be used for on-link determination).
2218 * '<em>off-link</em>' also controls the L-bit.
2221 * <b>Format 3: - Default of Prefix:</b>
2223 * @cliexcmd{ip6 nd <interface> [no] prefix <ip6-address>/<width> default}
2225 * When a new prefix is added (or existing one is being overwritten)
2226 * <em>default</em> uses default values for the prefix. If <em>no</em> is
2227 * used, the <em>default</em> is ignored and the prefix is deleted.
2231 * Example of how set a router advertisement option:
2232 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
2233 * Example of how to add a prefix:
2234 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64
2235 * infinite no-advertise}
2236 * Example of how to delete a prefix:
2237 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
2240 VLIB_CLI_COMMAND (ip6_nd_command, static) =
2243 .short_help = "ip6 nd <interface> ...",
2244 .function = ip6_ra_cmd,
2249 * VFT for registering as a delegate to an IP6 link
2251 const static ip6_link_delegate_vft_t ip6_ra_delegate_vft = {
2252 .ildv_disable = ip6_ra_delegate_disable,
2253 .ildv_enable = ip6_ra_link_enable,
2254 .ildv_format = format_ip6_ra,
2257 static clib_error_t *
2258 ip6_ra_init (vlib_main_t * vm)
2260 vlib_call_init_function (vm, icmp6_init);
2262 icmp6_register_type (vm, ICMP6_router_solicitation,
2263 ip6_icmp_router_solicitation_node.index);
2264 icmp6_register_type (vm, ICMP6_router_advertisement,
2265 ip6_icmp_router_advertisement_node.index);
2267 ip6_ra_delegate_id = ip6_link_delegate_register (&ip6_ra_delegate_vft);
2273 VLIB_INIT_FUNCTION (ip6_ra_init) =
2275 .runs_after = VLIB_INITS("icmp6_init"),
2280 * fd.io coding-style-patch-verification: ON
2283 * eval: (c-set-style "gnu")