ip6_ra.c revision cbe25aab
1/*
2 * ip/ip6_neighbor.c: IP6 neighbor handling
3 *
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:
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vnet/ip6-nd/ip6_ra.h>
19
20#include <vnet/ip/ip.h>
21#include <vnet/ip-neighbor/ip_neighbor_dp.h>
22
23#include <vnet/fib/ip6_fib.h>
24#include <vnet/ip/ip6_link.h>
25
26/**
27 * @file
28 * @brief IPv6 Router Advertisements.
29 *
30 * The files contains the API and CLI code for managing IPv6 RAs
31 */
32
33/* *INDENT-OFF* */
34/* Router solicitation packet format for ethernet. */
35typedef CLIB_PACKED (struct
36{
37    ip6_header_t ip;
38    icmp6_neighbor_discovery_header_t neighbor;
39    icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
40    link_layer_option;
41}) icmp6_router_solicitation_header_t;
42
43/* router advertisement packet format for ethernet. */
44typedef CLIB_PACKED (struct
45{
46  ip6_header_t ip;
47  icmp6_router_advertisement_header_t router;
48  icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
49  link_layer_option;
50  icmp6_neighbor_discovery_mtu_option_t mtu_option;
51  icmp6_neighbor_discovery_prefix_information_option_t
52  prefix[0];
53}) icmp6_router_advertisement_packet_t;
54/* *INDENT-ON* */
55
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
61
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 */
67
68/* advertised prefix option */
69typedef struct
70{
71  /* basic advertised information */
72  ip6_address_t prefix;
73  u8 prefix_len;
74  int adv_on_link_flag;
75  int adv_autonomous_flag;
76  u32 adv_valid_lifetime_in_secs;
77  u32 adv_pref_lifetime_in_secs;
78
79  /* advertised values are computed from these times if decrementing */
80  f64 valid_lifetime_expires;
81  f64 pref_lifetime_expires;
82
83  /* local information */
84  int enabled;
85  int deprecated_prefix_flag;
86  int decrement_lifetime_flag;
87
88#define MIN_ADV_VALID_LIFETIME 7203	/* seconds */
89#define DEF_ADV_VALID_LIFETIME  2592000
90#define DEF_ADV_PREF_LIFETIME 604800
91
92  /* extensions are added here, mobile, DNS etc.. */
93} ip6_radv_prefix_t;
94
95typedef struct ip6_ra_t_
96{
97  /* advertised config information, zero means unspecified  */
98  u8 curr_hop_limit;
99  int adv_managed_flag;
100  int adv_other_flag;
101  u16 adv_router_lifetime_in_sec;
102  u32 adv_neighbor_reachable_time_in_msec;
103  u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
104
105  /* mtu option */
106  u32 adv_link_mtu;
107
108  /* local information */
109  u32 sw_if_index;
110  int send_radv;		/* radv on/off on this interface -  set by config */
111  int cease_radv;		/* we are ceasing  to send  - set byf config */
112  int send_unicast;
113  int adv_link_layer_address;
114  int prefix_option;
115  int failed_device_check;
116  int ref_count;
117
118  /* prefix option */
119  ip6_radv_prefix_t *adv_prefixes_pool;
120
121  /* Hash table mapping address to index in interface advertised  prefix pool. */
122  mhash_t address_to_prefix_index;
123
124  f64 max_radv_interval;
125  f64 min_radv_interval;
126  f64 min_delay_between_radv;
127  f64 max_delay_between_radv;
128  f64 max_rtr_default_lifetime;
129
130  f64 last_radv_time;
131  f64 last_multicast_time;
132  f64 next_multicast_time;
133
134
135  u32 initial_adverts_count;
136  f64 initial_adverts_interval;
137  u32 initial_adverts_sent;
138
139  /* stats */
140  u32 n_advertisements_sent;
141  u32 n_solicitations_rcvd;
142  u32 n_solicitations_dropped;
143
144  /* router solicitations sending state */
145  u8 keep_sending_rs;		/* when true then next fields are valid */
146  icmp6_send_router_solicitation_params_t params;
147  f64 sleep_interval;
148  f64 due_time;
149  u32 n_left;
150  f64 start_time;
151  vlib_buffer_t *buffer;
152
153  u32 seed;
154
155} ip6_ra_t;
156
157static ip6_link_delegate_id_t ip6_ra_delegate_id;
158static ip6_ra_t *ip6_ra_pool;
159
160
161/* vector of registered RA report listeners */
162static ip6_ra_report_notify_t *ip6_ra_listeners;
163
164static int ip6_ra_publish (ip6_ra_report_t * r);
165
166void
167ip6_ra_report_register (ip6_ra_report_notify_t fn)
168{
169  ip6_ra_report_notify_t *listener;
170  vec_foreach (listener, ip6_ra_listeners)
171  {
172    if (*listener == fn)
173      return;
174  }
175
176  vec_add1 (ip6_ra_listeners, fn);
177}
178
179void
180ip6_ra_report_unregister (ip6_ra_report_notify_t fn)
181{
182  u32 ii;
183
184  vec_foreach_index (ii, ip6_ra_listeners)
185  {
186    if (ip6_ra_listeners[ii] == fn)
187      {
188	vec_del1 (ip6_ra_listeners, ii);
189	break;
190      }
191  }
192}
193
194static inline ip6_ra_t *
195ip6_ra_get_itf (u32 sw_if_index)
196{
197  index_t rai;
198
199  rai = ip6_link_delegate_get (sw_if_index, ip6_ra_delegate_id);
200
201  if (INDEX_INVALID != rai)
202    return (pool_elt_at_index (ip6_ra_pool, rai));
203
204  return (NULL);
205}
206
207/* for "syslogging" - use elog for now */
208#define foreach_log_level	     \
209  _ (DEBUG, "DEBUG")                 \
210  _ (INFO, "INFORMATION")	     \
211  _ (NOTICE, "NOTICE")		     \
212  _ (WARNING, "WARNING")             \
213  _ (ERR, "ERROR")	             \
214  _ (CRIT, "CRITICAL")               \
215  _ (ALERT, "ALERT")                 \
216  _ (EMERG,  "EMERGENCY")
217
218typedef enum
219{
220#define _(f,s) LOG_##f,
221  foreach_log_level
222#undef _
223} log_level_t;
224
225static char *log_level_strings[] = {
226#define _(f,s) s,
227  foreach_log_level
228#undef _
229};
230
231static int logmask = 1 << LOG_DEBUG;
232
233static void
234ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
235{
236  /* just use elog for now */
237  u8 *what;
238  va_list va;
239
240  if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
241    return;
242
243  va_start (va, fmt);
244  if (fmt)
245    {
246      what = va_format (0, fmt, &va);
247
248      ELOG_TYPE_DECLARE (e) =
249      {
250      .format = "ip6 nd:  (%s): %s",.format_args = "T4T4",};
251      struct
252      {
253	u32 s[2];
254      } *ed;
255      ed = ELOG_DATA (&vm->elog_main, e);
256      ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
257      ed->s[1] = elog_string (&vm->elog_main, (char *) what);
258    }
259  va_end (va);
260  return;
261}
262
263/* ipv6 neighbor discovery - router advertisements */
264typedef enum
265{
266  ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
267  ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
268  ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
269  ICMP6_ROUTER_SOLICITATION_N_NEXT,
270} icmp6_router_solicitation_or_advertisement_next_t;
271
272static_always_inline uword
273icmp6_router_solicitation (vlib_main_t * vm,
274			   vlib_node_runtime_t * node, vlib_frame_t * frame)
275{
276  vnet_main_t *vnm = vnet_get_main ();
277  ip6_main_t *im = &ip6_main;
278  uword n_packets = frame->n_vectors;
279  u32 *from, *to_next;
280  u32 n_left_from, n_left_to_next, next_index;
281  u32 n_advertisements_sent = 0;
282  int bogus_length;
283
284  icmp6_neighbor_discovery_option_type_t option_type;
285
286  vlib_node_runtime_t *error_node =
287    vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
288
289  from = vlib_frame_vector_args (frame);
290  n_left_from = n_packets;
291  next_index = node->cached_next_index;
292
293  if (node->flags & VLIB_NODE_FLAG_TRACE)
294    vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
295				   /* stride */ 1,
296				   sizeof (icmp6_input_trace_t));
297
298  /* source may append his LL address */
299  option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
300
301  while (n_left_from > 0)
302    {
303      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
304
305      while (n_left_from > 0 && n_left_to_next > 0)
306	{
307	  vlib_buffer_t *p0;
308	  ip6_header_t *ip0;
309	  ip6_ra_t *radv_info = NULL;
310
311	  icmp6_neighbor_discovery_header_t *h0;
312	  icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
313
314	  u32 bi0, options_len0, sw_if_index0, next0, error0;
315	  u32 is_solicitation = 1, is_dropped = 0;
316	  u32 is_unspecified, is_link_local;
317
318	  bi0 = to_next[0] = from[0];
319
320	  from += 1;
321	  to_next += 1;
322	  n_left_from -= 1;
323	  n_left_to_next -= 1;
324
325	  p0 = vlib_get_buffer (vm, bi0);
326	  ip0 = vlib_buffer_get_current (p0);
327	  h0 = ip6_next_header (ip0);
328	  options_len0 =
329	    clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
330	  is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
331	  is_link_local =
332	    ip6_address_is_link_local_unicast (&ip0->src_address);
333
334	  error0 = ICMP6_ERROR_NONE;
335	  sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
336
337	  /* check if solicitation  (not from nd_timer node) */
338	  if (ip6_address_is_unspecified (&ip0->dst_address))
339	    is_solicitation = 0;
340
341	  /* Check that source address is unspecified, link-local or else on-link. */
342	  if (!is_unspecified && !is_link_local)
343	    {
344	      u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
345
346	      if (ADJ_INDEX_INVALID != src_adj_index0)
347		{
348		  ip_adjacency_t *adj0 = adj_get (src_adj_index0);
349
350		  error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
351			    ?
352			    ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
353			    : error0);
354		}
355	      else
356		{
357		  error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
358		}
359	    }
360
361	  /* check for source LL option and process */
362	  o0 = (void *) (h0 + 1);
363	  o0 = ((options_len0 == 8
364		 && o0->header.type == option_type
365		 && o0->header.n_data_u64s == 1) ? o0 : 0);
366
367	  /* if src address unspecified IGNORE any options */
368	  if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
369			    !is_unspecified && !is_link_local))
370	    {
371	      ip_neighbor_learn_t learn = {
372		.type = IP46_TYPE_IP6,
373		.sw_if_index = sw_if_index0,
374		.ip.ip6 = ip0->src_address,
375	      };
376	      memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac));
377	      ip_neighbor_learn_dp (&learn);
378	    }
379
380	  /* default is to drop */
381	  next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
382
383	  if (error0 == ICMP6_ERROR_NONE)
384	    {
385	      vnet_sw_interface_t *sw_if0;
386	      ethernet_interface_t *eth_if0;
387	      u32 adj_index0;
388
389	      sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
390	      ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
391	      eth_if0 =
392		ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
393
394	      /* only support ethernet interface type for now */
395	      error0 =
396		(!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
397		: error0;
398
399	      if (error0 == ICMP6_ERROR_NONE)
400		{
401		  /* adjust the sizeof the buffer to just include the ipv6 header */
402		  p0->current_length -=
403		    (options_len0 +
404		     sizeof (icmp6_neighbor_discovery_header_t));
405
406		  radv_info = ip6_ra_get_itf (sw_if_index0);
407
408		  error0 = ((!radv_info) ?
409			    ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
410			    error0);
411
412		  if (error0 == ICMP6_ERROR_NONE)
413		    {
414		      f64 now = vlib_time_now (vm);
415
416		      /* for solicited adverts - need to rate limit */
417		      if (is_solicitation)
418			{
419			  if (0 != radv_info->last_radv_time &&
420			      (now - radv_info->last_radv_time) <
421			      MIN_DELAY_BETWEEN_RAS)
422			    is_dropped = 1;
423			  else
424			    radv_info->last_radv_time = now;
425			}
426
427		      /* send now  */
428		      icmp6_router_advertisement_header_t rh;
429
430		      rh.icmp.type = ICMP6_router_advertisement;
431		      rh.icmp.code = 0;
432		      rh.icmp.checksum = 0;
433
434		      rh.current_hop_limit = radv_info->curr_hop_limit;
435		      rh.router_lifetime_in_sec =
436			clib_host_to_net_u16
437			(radv_info->adv_router_lifetime_in_sec);
438		      rh.
439			time_in_msec_between_retransmitted_neighbor_solicitations
440			=
441			clib_host_to_net_u32 (radv_info->
442					      adv_time_in_msec_between_retransmitted_neighbor_solicitations);
443		      rh.neighbor_reachable_time_in_msec =
444			clib_host_to_net_u32 (radv_info->
445					      adv_neighbor_reachable_time_in_msec);
446
447		      rh.flags =
448			(radv_info->adv_managed_flag) ?
449			ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
450			0;
451		      rh.flags |=
452			((radv_info->adv_other_flag) ?
453			 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
454			 0);
455
456
457		      u16 payload_length =
458			sizeof (icmp6_router_advertisement_header_t);
459
460		      if (vlib_buffer_add_data
461			  (vm, &bi0, (void *) &rh,
462			   sizeof (icmp6_router_advertisement_header_t)))
463			{
464			  /* buffer allocation failed, drop the pkt */
465			  error0 = ICMP6_ERROR_ALLOC_FAILURE;
466			  goto drop0;
467			}
468
469		      if (radv_info->adv_link_layer_address)
470			{
471			  icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
472			    h;
473
474			  h.header.type =
475			    ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
476			  h.header.n_data_u64s = 1;
477
478			  /* copy ll address */
479			  clib_memcpy (&h.ethernet_address[0],
480				       eth_if0->address, 6);
481
482			  if (vlib_buffer_add_data
483			      (vm, &bi0, (void *) &h,
484			       sizeof
485			       (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
486			    {
487			      error0 = ICMP6_ERROR_ALLOC_FAILURE;
488			      goto drop0;
489			    }
490
491			  payload_length +=
492			    sizeof
493			    (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
494			}
495
496		      /* add MTU option */
497		      if (radv_info->adv_link_mtu)
498			{
499			  icmp6_neighbor_discovery_mtu_option_t h;
500
501			  h.unused = 0;
502			  h.mtu =
503			    clib_host_to_net_u32 (radv_info->adv_link_mtu);
504			  h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
505			  h.header.n_data_u64s = 1;
506
507			  payload_length +=
508			    sizeof (icmp6_neighbor_discovery_mtu_option_t);
509
510			  if (vlib_buffer_add_data
511			      (vm, &bi0, (void *) &h,
512			       sizeof
513			       (icmp6_neighbor_discovery_mtu_option_t)))
514			    {
515			      error0 = ICMP6_ERROR_ALLOC_FAILURE;
516			      goto drop0;
517			    }
518			}
519
520		      /* add advertised prefix options  */
521		      ip6_radv_prefix_t *pr_info;
522
523		      /* *INDENT-OFF* */
524		      pool_foreach (pr_info, radv_info->adv_prefixes_pool,
525                      ({
526                        if(pr_info->enabled &&
527                           (!pr_info->decrement_lifetime_flag
528                            || (pr_info->pref_lifetime_expires >0)))
529                          {
530                            /* advertise this prefix */
531                            icmp6_neighbor_discovery_prefix_information_option_t h;
532
533                            h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
534                            h.header.n_data_u64s  =  (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
535
536                            h.dst_address_length  = pr_info->prefix_len;
537
538                            h.flags  = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
539                            h.flags |= (pr_info->adv_autonomous_flag) ?  ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO :  0;
540
541                            if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
542                              {
543                                h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
544                                h.preferred_time  = 0;
545                              }
546                            else
547                              {
548                                if(pr_info->decrement_lifetime_flag)
549                                  {
550                                    pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires  > now)) ?
551                                      (pr_info->valid_lifetime_expires  - now) : 0;
552
553                                    pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires  > now)) ?
554                                      (pr_info->pref_lifetime_expires  - now) : 0;
555                                  }
556
557                                h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
558                                h.preferred_time  = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
559                              }
560                            h.unused  = 0;
561
562                            clib_warning ("Prefix %U valid %u preferred %u",
563                                          format_ip6_address, &pr_info->prefix,
564                                          clib_net_to_host_u32 (h.valid_time),
565                                          clib_net_to_host_u32 (h.preferred_time));
566
567                            if (h.valid_time == 0)
568                              clib_warning ("WARNING: valid_time 0!!!");
569
570                            clib_memcpy(&h.dst_address, &pr_info->prefix,  sizeof(ip6_address_t));
571
572                            payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
573
574                            if (vlib_buffer_add_data
575				(vm, &bi0, (void *)&h,
576				 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
577                              {
578                                error0 = ICMP6_ERROR_ALLOC_FAILURE;
579                                goto drop0;
580                              }
581
582                          }
583                      }));
584		      /* *INDENT-ON* */
585
586		      /* add additional options before here */
587
588		      /* finish building the router advertisement... */
589		      if (!is_unspecified && radv_info->send_unicast)
590			{
591			  ip0->dst_address = ip0->src_address;
592			}
593		      else
594			{
595			  /* target address is all-nodes mcast addr */
596			  ip6_set_reserved_multicast_address
597			    (&ip0->dst_address,
598			     IP6_MULTICAST_SCOPE_link_local,
599			     IP6_MULTICAST_GROUP_ID_all_hosts);
600			}
601
602		      /* source address MUST be the link-local address */
603		      ip6_address_copy (&ip0->src_address,
604					ip6_get_link_local_address
605					(radv_info->sw_if_index));
606
607		      ip0->hop_limit = 255;
608		      ip0->payload_length =
609			clib_host_to_net_u16 (payload_length);
610
611		      icmp6_router_advertisement_header_t *rh0 =
612			(icmp6_router_advertisement_header_t *) (ip0 + 1);
613		      rh0->icmp.checksum =
614			ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
615							   &bogus_length);
616		      ASSERT (bogus_length == 0);
617
618		      /* setup output if and adjacency */
619		      vnet_buffer (p0)->sw_if_index[VLIB_RX] =
620			vnet_main.local_interface_sw_if_index;
621
622		      if (is_solicitation)
623			{
624			  ethernet_header_t *eth0;
625			  /* Reuse current MAC header, copy SMAC to DMAC and
626			   * interface MAC to SMAC */
627			  vlib_buffer_reset (p0);
628			  eth0 = vlib_buffer_get_current (p0);
629			  clib_memcpy (eth0->dst_address, eth0->src_address,
630				       6);
631			  clib_memcpy (eth0->src_address, eth_if0->address,
632				       6);
633			  next0 =
634			    is_dropped ? next0 :
635			    ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
636			  vnet_buffer (p0)->sw_if_index[VLIB_TX] =
637			    sw_if_index0;
638			}
639		      else
640			{
641			  adj_index0 = ip6_link_get_mcast_adj (sw_if_index0);
642			  if (adj_index0 == INDEX_INVALID)
643			    error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
644			  else
645			    {
646			      next0 =
647				is_dropped ? next0 :
648				ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
649			      vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
650				adj_index0;
651			    }
652			}
653		      p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
654
655		      radv_info->n_solicitations_dropped += is_dropped;
656		      radv_info->n_solicitations_rcvd += is_solicitation;
657
658		      if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
659			{
660			  radv_info->n_advertisements_sent++;
661			  n_advertisements_sent++;
662			}
663		    }
664		}
665	    }
666
667	drop0:
668	  p0->error = error_node->errors[error0];
669
670	  if (error0 != ICMP6_ERROR_NONE)
671	    vlib_error_count (vm, error_node->node_index, error0, 1);
672
673	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
674					   to_next, n_left_to_next,
675					   bi0, next0);
676
677	}
678
679      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
680    }
681
682  /* Account for router advertisements sent. */
683  vlib_error_count (vm, error_node->node_index,
684		    ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
685		    n_advertisements_sent);
686
687  return frame->n_vectors;
688}
689
690/* *INDENT-OFF* */
691VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
692{
693  .function = icmp6_router_solicitation,
694  .name = "icmp6-router-solicitation",
695
696  .vector_size = sizeof (u32),
697
698  .format_trace = format_icmp6_input_trace,
699
700  .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
701  .next_nodes = {
702    [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
703    [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
704    [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
705  },
706};
707/* *INDENT-ON* */
708
709 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always  be dropped  */
710static_always_inline uword
711icmp6_router_advertisement (vlib_main_t * vm,
712			    vlib_node_runtime_t * node, vlib_frame_t * frame)
713{
714  vnet_main_t *vnm = vnet_get_main ();
715  uword n_packets = frame->n_vectors;
716  u32 *from, *to_next;
717  u32 n_left_from, n_left_to_next, next_index;
718  u32 n_advertisements_rcvd = 0;
719
720  vlib_node_runtime_t *error_node =
721    vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
722
723  from = vlib_frame_vector_args (frame);
724  n_left_from = n_packets;
725  next_index = node->cached_next_index;
726
727  if (node->flags & VLIB_NODE_FLAG_TRACE)
728    vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
729				   /* stride */ 1,
730				   sizeof (icmp6_input_trace_t));
731
732  while (n_left_from > 0)
733    {
734      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
735
736      while (n_left_from > 0 && n_left_to_next > 0)
737	{
738	  vlib_buffer_t *p0;
739	  ip6_header_t *ip0;
740	  ip6_ra_t *radv_info = 0;
741	  icmp6_router_advertisement_header_t *h0;
742	  u32 bi0, options_len0, sw_if_index0, next0, error0;
743
744	  bi0 = to_next[0] = from[0];
745
746	  from += 1;
747	  to_next += 1;
748	  n_left_from -= 1;
749	  n_left_to_next -= 1;
750
751	  p0 = vlib_get_buffer (vm, bi0);
752	  ip0 = vlib_buffer_get_current (p0);
753	  h0 = ip6_next_header (ip0);
754	  options_len0 =
755	    clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
756
757	  error0 = ICMP6_ERROR_NONE;
758	  sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
759
760	  /* Check that source address is link-local */
761	  error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
762	    ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
763
764	  /* default is to drop */
765	  next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
766
767	  n_advertisements_rcvd++;
768
769	  if (error0 == ICMP6_ERROR_NONE)
770	    {
771	      vnet_sw_interface_t *sw_if0;
772	      ethernet_interface_t *eth_if0;
773
774	      sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
775	      ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
776	      eth_if0 =
777		ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
778
779	      /* only support ethernet interface type for now */
780	      error0 =
781		(!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
782		: error0;
783
784	      if (error0 == ICMP6_ERROR_NONE)
785		{
786		  /* look up the radv_t information for this interface */
787		  radv_info = ip6_ra_get_itf (sw_if_index0);
788
789		  error0 = ((!radv_info) ?
790			    ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
791			    error0);
792
793		  if (error0 == ICMP6_ERROR_NONE)
794		    {
795		      radv_info->keep_sending_rs = 0;
796
797		      ip6_ra_report_t r;
798
799		      r.sw_if_index = sw_if_index0;
800		      memcpy (&r.router_address, &ip0->src_address, 16);
801		      r.current_hop_limit = h0->current_hop_limit;
802		      r.flags = h0->flags;
803		      r.router_lifetime_in_sec =
804			clib_net_to_host_u16 (h0->router_lifetime_in_sec);
805		      r.neighbor_reachable_time_in_msec =
806			clib_net_to_host_u32
807			(h0->neighbor_reachable_time_in_msec);
808		      r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
809		      r.prefixes = 0;
810
811		      /* validate advertised information */
812		      if ((h0->current_hop_limit && radv_info->curr_hop_limit)
813			  && (h0->current_hop_limit !=
814			      radv_info->curr_hop_limit))
815			{
816			  ip6_neighbor_syslog (vm, LOG_WARNING,
817					       "our AdvCurHopLimit on %U doesn't agree with %U",
818					       format_vnet_sw_if_index_name,
819					       vnm, sw_if_index0,
820					       format_ip6_address,
821					       &ip0->src_address);
822			}
823
824		      if ((h0->flags &
825			   ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
826			  != radv_info->adv_managed_flag)
827			{
828			  ip6_neighbor_syslog (vm, LOG_WARNING,
829					       "our AdvManagedFlag on %U doesn't agree with %U",
830					       format_vnet_sw_if_index_name,
831					       vnm, sw_if_index0,
832					       format_ip6_address,
833					       &ip0->src_address);
834			}
835
836		      if ((h0->flags &
837			   ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
838			  != radv_info->adv_other_flag)
839			{
840			  ip6_neighbor_syslog (vm, LOG_WARNING,
841					       "our AdvOtherConfigFlag on %U doesn't agree with %U",
842					       format_vnet_sw_if_index_name,
843					       vnm, sw_if_index0,
844					       format_ip6_address,
845					       &ip0->src_address);
846			}
847
848		      if ((h0->
849			   time_in_msec_between_retransmitted_neighbor_solicitations
850			   && radv_info->
851			   adv_time_in_msec_between_retransmitted_neighbor_solicitations)
852			  && (h0->
853			      time_in_msec_between_retransmitted_neighbor_solicitations
854			      !=
855			      clib_host_to_net_u32 (radv_info->
856						    adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
857			{
858			  ip6_neighbor_syslog (vm, LOG_WARNING,
859					       "our AdvRetransTimer on %U doesn't agree with %U",
860					       format_vnet_sw_if_index_name,
861					       vnm, sw_if_index0,
862					       format_ip6_address,
863					       &ip0->src_address);
864			}
865
866		      if ((h0->neighbor_reachable_time_in_msec &&
867			   radv_info->adv_neighbor_reachable_time_in_msec) &&
868			  (h0->neighbor_reachable_time_in_msec !=
869			   clib_host_to_net_u32
870			   (radv_info->adv_neighbor_reachable_time_in_msec)))
871			{
872			  ip6_neighbor_syslog (vm, LOG_WARNING,
873					       "our AdvReachableTime on %U doesn't agree with %U",
874					       format_vnet_sw_if_index_name,
875					       vnm, sw_if_index0,
876					       format_ip6_address,
877					       &ip0->src_address);
878			}
879
880		      /* check for MTU or prefix options or .. */
881		      u8 *opt_hdr = (u8 *) (h0 + 1);
882		      while (options_len0 > 0)
883			{
884			  icmp6_neighbor_discovery_option_header_t *o0 =
885			    (icmp6_neighbor_discovery_option_header_t *)
886			    opt_hdr;
887			  int opt_len = o0->n_data_u64s << 3;
888			  icmp6_neighbor_discovery_option_type_t option_type =
889			    o0->type;
890
891			  if (options_len0 < 2)
892			    {
893			      ip6_neighbor_syslog (vm, LOG_ERR,
894						   "malformed RA packet on %U from %U",
895						   format_vnet_sw_if_index_name,
896						   vnm, sw_if_index0,
897						   format_ip6_address,
898						   &ip0->src_address);
899			      break;
900			    }
901
902			  if (opt_len == 0)
903			    {
904			      ip6_neighbor_syslog (vm, LOG_ERR,
905						   " zero length option in RA on %U from %U",
906						   format_vnet_sw_if_index_name,
907						   vnm, sw_if_index0,
908						   format_ip6_address,
909						   &ip0->src_address);
910			      break;
911			    }
912			  else if (opt_len > options_len0)
913			    {
914			      ip6_neighbor_syslog (vm, LOG_ERR,
915						   "option length in RA packet  greater than total length on %U from %U",
916						   format_vnet_sw_if_index_name,
917						   vnm, sw_if_index0,
918						   format_ip6_address,
919						   &ip0->src_address);
920			      break;
921			    }
922
923			  options_len0 -= opt_len;
924			  opt_hdr += opt_len;
925
926			  switch (option_type)
927			    {
928			    case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
929			      {
930				icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
931				  * h =
932				  (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
933				   *) (o0);
934
935				if (opt_len < sizeof (*h))
936				  break;
937
938				memcpy (r.slla, h->ethernet_address, 6);
939			      }
940			      break;
941
942			    case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
943			      {
944				icmp6_neighbor_discovery_mtu_option_t *h =
945				  (icmp6_neighbor_discovery_mtu_option_t
946				   *) (o0);
947
948				if (opt_len < sizeof (*h))
949				  break;
950
951				r.mtu = clib_net_to_host_u32 (h->mtu);
952
953				if ((h->mtu && radv_info->adv_link_mtu) &&
954				    (h->mtu !=
955				     clib_host_to_net_u32
956				     (radv_info->adv_link_mtu)))
957				  {
958				    ip6_neighbor_syslog (vm, LOG_WARNING,
959							 "our AdvLinkMTU on %U doesn't agree with %U",
960							 format_vnet_sw_if_index_name,
961							 vnm, sw_if_index0,
962							 format_ip6_address,
963							 &ip0->src_address);
964				  }
965			      }
966			      break;
967
968			    case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
969			      {
970				icmp6_neighbor_discovery_prefix_information_option_t
971				  * h =
972				  (icmp6_neighbor_discovery_prefix_information_option_t
973				   *) (o0);
974
975				/* validate advertised prefix options  */
976				ip6_radv_prefix_t *pr_info;
977				u32 preferred, valid;
978
979				if (opt_len < sizeof (*h))
980				  break;
981
982				vec_validate (r.prefixes,
983					      vec_len (r.prefixes));
984				ra_report_prefix_info_t *prefix =
985				  vec_elt_at_index (r.prefixes,
986						    vec_len (r.prefixes) - 1);
987
988				preferred =
989				  clib_net_to_host_u32 (h->preferred_time);
990				valid = clib_net_to_host_u32 (h->valid_time);
991
992				prefix->preferred_time = preferred;
993				prefix->valid_time = valid;
994				prefix->flags = h->flags & 0xc0;
995				prefix->prefix.fp_len = h->dst_address_length;
996				prefix->prefix.fp_addr.ip6 = h->dst_address;
997				prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
998
999				/* look for matching prefix - if we our advertising it, it better be consistant */
1000				/* *INDENT-OFF* */
1001				pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1002                                ({
1003
1004                                  ip6_address_t mask;
1005                                  ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1006
1007                                  if(pr_info->enabled &&
1008                                     (pr_info->prefix_len == h->dst_address_length) &&
1009                                     ip6_address_is_equal_masked (&pr_info->prefix,  &h->dst_address, &mask))
1010                                    {
1011                                      /* found it */
1012                                      if(!pr_info->decrement_lifetime_flag &&
1013                                         valid != pr_info->adv_valid_lifetime_in_secs)
1014                                        {
1015                                          ip6_neighbor_syslog(vm,  LOG_WARNING,
1016                                                              "our ADV validlifetime on  %U for %U does not  agree with %U",
1017                                                              format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1018                                                              format_ip6_address, &h->dst_address);
1019                                        }
1020                                      if(!pr_info->decrement_lifetime_flag &&
1021                                         preferred != pr_info->adv_pref_lifetime_in_secs)
1022                                        {
1023                                          ip6_neighbor_syslog(vm,  LOG_WARNING,
1024                                                              "our ADV preferredlifetime on  %U for %U does not  agree with %U",
1025                                                              format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1026                                                              format_ip6_address, &h->dst_address);
1027                                        }
1028                                    }
1029                                  break;
1030                                }));
1031				/* *INDENT-ON* */
1032				break;
1033			      }
1034			    default:
1035			      /* skip this one */
1036			      break;
1037			    }
1038			}
1039		      ip6_ra_publish (&r);
1040		    }
1041		}
1042	    }
1043
1044	  p0->error = error_node->errors[error0];
1045
1046	  if (error0 != ICMP6_ERROR_NONE)
1047	    vlib_error_count (vm, error_node->node_index, error0, 1);
1048
1049	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1050					   to_next, n_left_to_next,
1051					   bi0, next0);
1052	}
1053
1054      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1055    }
1056
1057  /* Account for router advertisements received. */
1058  vlib_error_count (vm, error_node->node_index,
1059		    ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
1060		    n_advertisements_rcvd);
1061
1062  return frame->n_vectors;
1063}
1064
1065/* *INDENT-OFF* */
1066VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
1067{
1068  .function = icmp6_router_advertisement,
1069  .name = "icmp6-router-advertisement",
1070
1071  .vector_size = sizeof (u32),
1072
1073  .format_trace = format_icmp6_input_trace,
1074
1075  .n_next_nodes = 1,
1076  .next_nodes = {
1077    [0] = "ip6-drop",
1078  },
1079};
1080/* *INDENT-ON* */
1081
1082static inline f64
1083random_f64_from_to (f64 from, f64 to)
1084{
1085  static u32 seed = 0;
1086  static u8 seed_set = 0;
1087  if (!seed_set)
1088    {
1089      seed = random_default_seed ();
1090      seed_set = 1;
1091    }
1092  return random_f64 (&seed) * (to - from) + from;
1093}
1094
1095static inline u8
1096get_mac_address (u32 sw_if_index, u8 * address)
1097{
1098  vnet_main_t *vnm = vnet_get_main ();
1099  vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1100  if (!hw_if->hw_address)
1101    return 1;
1102  clib_memcpy (address, hw_if->hw_address, 6);
1103  return 0;
1104}
1105
1106static inline vlib_buffer_t *
1107create_buffer_for_rs (vlib_main_t * vm, ip6_ra_t * radv_info)
1108{
1109  u32 bi0;
1110  vlib_buffer_t *p0;
1111  icmp6_router_solicitation_header_t *rh;
1112  u16 payload_length;
1113  int bogus_length;
1114  u32 sw_if_index;
1115
1116  sw_if_index = radv_info->sw_if_index;
1117
1118  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
1119    {
1120      clib_warning ("buffer allocation failure");
1121      return 0;
1122    }
1123
1124  p0 = vlib_get_buffer (vm, bi0);
1125  VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
1126  p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1127
1128  vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
1129  vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
1130
1131  vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1132    ip6_link_get_mcast_adj (sw_if_index);
1133
1134  rh = vlib_buffer_get_current (p0);
1135  p0->current_length = sizeof (*rh);
1136
1137  rh->neighbor.icmp.type = ICMP6_router_solicitation;
1138  rh->neighbor.icmp.code = 0;
1139  rh->neighbor.icmp.checksum = 0;
1140  rh->neighbor.reserved_must_be_zero = 0;
1141
1142  rh->link_layer_option.header.type =
1143    ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1144  if (0 != get_mac_address (sw_if_index,
1145			    rh->link_layer_option.ethernet_address))
1146    {
1147      clib_warning ("interface with sw_if_index %u has no mac address",
1148		    sw_if_index);
1149      vlib_buffer_free (vm, &bi0, 1);
1150      return 0;
1151    }
1152  rh->link_layer_option.header.n_data_u64s = 1;
1153
1154  payload_length = sizeof (rh->neighbor) + sizeof (u64);
1155
1156  rh->ip.ip_version_traffic_class_and_flow_label =
1157    clib_host_to_net_u32 (0x6 << 28);
1158  rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
1159  rh->ip.protocol = IP_PROTOCOL_ICMP6;
1160  rh->ip.hop_limit = 255;
1161  ip6_address_copy (&rh->ip.src_address,
1162		    ip6_get_link_local_address (radv_info->sw_if_index));
1163  /* set address ff02::2 */
1164  rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
1165  rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
1166
1167  rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
1168								  &rh->ip,
1169								  &bogus_length);
1170
1171  return p0;
1172}
1173
1174static inline void
1175stop_sending_rs (vlib_main_t * vm, ip6_ra_t * ra)
1176{
1177  u32 bi0;
1178
1179  ra->keep_sending_rs = 0;
1180  if (ra->buffer)
1181    {
1182      bi0 = vlib_get_buffer_index (vm, ra->buffer);
1183      vlib_buffer_free (vm, &bi0, 1);
1184      ra->buffer = 0;
1185    }
1186}
1187
1188static inline bool
1189check_send_rs (vlib_main_t * vm, ip6_ra_t * radv_info, f64 current_time,
1190	       f64 * due_time)
1191{
1192  vlib_buffer_t *p0;
1193  vlib_frame_t *f;
1194  u32 *to_next;
1195  u32 next_index;
1196  vlib_buffer_t *c0;
1197  u32 ci0;
1198
1199  icmp6_send_router_solicitation_params_t *params;
1200
1201  if (!radv_info->keep_sending_rs)
1202    return false;
1203
1204  params = &radv_info->params;
1205
1206  if (radv_info->due_time > current_time)
1207    {
1208      *due_time = radv_info->due_time;
1209      return true;
1210    }
1211
1212  p0 = radv_info->buffer;
1213
1214  next_index = ip6_rewrite_mcast_node.index;
1215
1216  c0 = vlib_buffer_copy (vm, p0);
1217  ci0 = vlib_get_buffer_index (vm, c0);
1218
1219  f = vlib_get_frame_to_node (vm, next_index);
1220  to_next = vlib_frame_vector_args (f);
1221  to_next[0] = ci0;
1222  f->n_vectors = 1;
1223  vlib_put_frame_to_node (vm, next_index, f);
1224
1225  if (params->mrc != 0 && --radv_info->n_left == 0)
1226    stop_sending_rs (vm, radv_info);
1227  else
1228    {
1229      radv_info->sleep_interval =
1230	(2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
1231      if (radv_info->sleep_interval > params->mrt)
1232	radv_info->sleep_interval =
1233	  (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
1234
1235      radv_info->due_time = current_time + radv_info->sleep_interval;
1236
1237      if (params->mrd != 0
1238	  && current_time > radv_info->start_time + params->mrd)
1239	stop_sending_rs (vm, radv_info);
1240      else
1241	*due_time = radv_info->due_time;
1242    }
1243
1244  return radv_info->keep_sending_rs;
1245}
1246
1247static uword
1248send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1249		 vlib_frame_t * f0)
1250{
1251  uword *event_data = NULL;
1252  f64 sleep_time = 1e9;
1253  ip6_ra_t *radv_info;
1254  f64 current_time;
1255  f64 due_time;
1256  f64 dt = 0;
1257
1258  while (true)
1259    {
1260      vlib_process_wait_for_event_or_clock (vm, sleep_time);
1261      vlib_process_get_events (vm, &event_data);
1262      vec_reset_length (event_data);
1263
1264      current_time = vlib_time_now (vm);
1265      do
1266	{
1267	  due_time = current_time + 1e9;
1268        /* *INDENT-OFF* */
1269        pool_foreach (radv_info, ip6_ra_pool,
1270        ({
1271	    if (check_send_rs (vm, radv_info, current_time, &dt)
1272		&& (dt < due_time))
1273	      due_time = dt;
1274        }));
1275        /* *INDENT-ON* */
1276	  current_time = vlib_time_now (vm);
1277	}
1278      while (due_time < current_time);
1279
1280      sleep_time = due_time - current_time;
1281    }
1282
1283  return 0;
1284}
1285
1286/* *INDENT-OFF* */
1287VLIB_REGISTER_NODE (ip6_rs_process_node) = {
1288    .function = send_rs_process,
1289    .type = VLIB_NODE_TYPE_PROCESS,
1290    .name = "ip6-rs-process",
1291};
1292/* *INDENT-ON* */
1293
1294void
1295icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
1296				const icmp6_send_router_solicitation_params_t
1297				* params)
1298{
1299  ip6_ra_t *ra;
1300
1301  ASSERT (~0 != sw_if_index);
1302
1303  ra = ip6_ra_get_itf (sw_if_index);
1304
1305  if (!ra)
1306    return;
1307
1308  stop_sending_rs (vm, ra);
1309
1310  if (!stop)
1311    {
1312      ra->keep_sending_rs = 1;
1313      ra->params = *params;
1314      ra->n_left = params->mrc;
1315      ra->start_time = vlib_time_now (vm);
1316      ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
1317      ra->due_time = 0;		/* send first packet ASAP */
1318      ra->buffer = create_buffer_for_rs (vm, ra);
1319      if (!ra->buffer)
1320	ra->keep_sending_rs = 0;
1321      else
1322	vlib_process_signal_event (vm, ip6_rs_process_node.index, 1, 0);
1323    }
1324}
1325
1326static const ethernet_interface_t *
1327ip6_ra_get_eth_itf (u32 sw_if_index)
1328{
1329  const vnet_sw_interface_t *sw;
1330
1331  /* lookup radv container  - ethernet interfaces only */
1332  sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
1333  if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1334    return (ethernet_get_interface (&ethernet_main, sw->hw_if_index));
1335
1336  return (NULL);
1337}
1338
1339/**
1340 * @brief called when IP6 is enabled on an interface
1341 *create and initialize router advertisement parameters with default
1342 * values for this intfc
1343 */
1344static void
1345ip6_ra_link_enable (u32 sw_if_index)
1346{
1347  const ethernet_interface_t *eth;
1348  ip6_ra_t *radv_info;
1349
1350  eth = ip6_ra_get_eth_itf (sw_if_index);
1351
1352  if (NULL == eth)
1353    return;
1354
1355  ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
1356						  ip6_ra_delegate_id));
1357
1358  pool_get_zero (ip6_ra_pool, radv_info);
1359
1360  radv_info->seed = (u32) clib_cpu_time_now ();
1361  random_u32 (&radv_info->seed);
1362
1363  radv_info->sw_if_index = sw_if_index;
1364  radv_info->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1365  radv_info->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1366  radv_info->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1367  radv_info->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1368
1369  /* send ll address source address option */
1370  radv_info->adv_link_layer_address = 1;
1371
1372  radv_info->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1373  radv_info->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1374  radv_info->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1375
1376  radv_info->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
1377  radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1378  radv_info->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1379
1380  /* deafult is to send */
1381  radv_info->send_radv = 1;
1382
1383  /* fill in delegate for this interface that will be needed later */
1384  radv_info->adv_link_mtu =
1385    vnet_sw_interface_get_mtu (vnet_get_main (), sw_if_index, VNET_MTU_IP6);
1386
1387  mhash_init (&radv_info->address_to_prefix_index, sizeof (uword),
1388	      sizeof (ip6_address_t));
1389
1390  ip6_link_delegate_update (sw_if_index, ip6_ra_delegate_id,
1391			    radv_info - ip6_ra_pool);
1392}
1393
1394static void
1395ip6_ra_delegate_disable (index_t rai)
1396{
1397  ip6_radv_prefix_t *p;
1398  ip6_ra_t *radv_info;
1399
1400  radv_info = pool_elt_at_index (ip6_ra_pool, rai);
1401
1402  /* clean up prefix and MDP pools */
1403  /* *INDENT-OFF* */
1404  pool_flush(p, radv_info->adv_prefixes_pool,
1405  ({
1406    mhash_unset (&radv_info->address_to_prefix_index, &p->prefix, 0);
1407  }));
1408  /* *INDENT-ON* */
1409
1410  pool_free (radv_info->adv_prefixes_pool);
1411
1412  mhash_free (&radv_info->address_to_prefix_index);
1413
1414  pool_put (ip6_ra_pool, radv_info);
1415}
1416
1417/* send a RA or update the timer info etc.. */
1418static uword
1419ip6_ra_process_timer_event (vlib_main_t * vm,
1420			    vlib_node_runtime_t * node, vlib_frame_t * frame)
1421{
1422  vnet_main_t *vnm = vnet_get_main ();
1423  ip6_ra_t *radv_info;
1424  vlib_frame_t *f = 0;
1425  u32 n_this_frame = 0;
1426  u32 n_left_to_next = 0;
1427  u32 *to_next = 0;
1428  u32 bo0;
1429  icmp6_router_solicitation_header_t *h0;
1430  vlib_buffer_t *b0;
1431  f64 now = vlib_time_now (vm);
1432
1433  /* Interface ip6 radv info list */
1434  /* *INDENT-OFF* */
1435  pool_foreach (radv_info, ip6_ra_pool,
1436  ({
1437    if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
1438      {
1439        radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
1440        radv_info->next_multicast_time = now;
1441        radv_info->last_multicast_time = now;
1442        radv_info->last_radv_time = 0;
1443        continue;
1444      }
1445
1446    /* is it time to send a multicast  RA on this interface? */
1447    if(radv_info->send_radv && (now >=  radv_info->next_multicast_time))
1448      {
1449        u32 n_to_alloc = 1;
1450        u32 n_allocated;
1451
1452        f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
1453          random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
1454
1455        /* multicast send - compute next multicast send time */
1456        if( radv_info->initial_adverts_sent > 0)
1457          {
1458            radv_info->initial_adverts_sent--;
1459            if(rfn > radv_info->initial_adverts_interval)
1460              rfn =  radv_info->initial_adverts_interval;
1461
1462            /* check to see if we are ceasing to send */
1463            if( radv_info->initial_adverts_sent  == 0)
1464              if(radv_info->cease_radv)
1465                radv_info->send_radv = 0;
1466          }
1467
1468        radv_info->next_multicast_time =  rfn + now;
1469        radv_info->last_multicast_time = now;
1470
1471        /* send advert now - build a "solicted" router advert with unspecified source address */
1472        n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
1473
1474        if (PREDICT_FALSE(n_allocated == 0))
1475          {
1476            clib_warning ("buffer allocation failure");
1477            continue;
1478          }
1479        b0 = vlib_get_buffer (vm, bo0);
1480        b0->current_length = sizeof( icmp6_router_solicitation_header_t);
1481        b0->error = ICMP6_ERROR_NONE;
1482        vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
1483
1484        h0 =  vlib_buffer_get_current (b0);
1485
1486        clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
1487
1488        h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1489        h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
1490                                                      - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
1491        h0->ip.protocol = IP_PROTOCOL_ICMP6;
1492        h0->ip.hop_limit = 255;
1493
1494        /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
1495        h0->ip.src_address.as_u64[0] = 0;
1496        h0->ip.src_address.as_u64[1] = 0;
1497
1498        h0->ip.dst_address.as_u64[0] = 0;
1499        h0->ip.dst_address.as_u64[1] = 0;
1500
1501        h0->neighbor.icmp.type = ICMP6_router_solicitation;
1502
1503        if (PREDICT_FALSE(f == 0))
1504          {
1505            f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
1506            to_next = vlib_frame_vector_args (f);
1507            n_left_to_next = VLIB_FRAME_SIZE;
1508            n_this_frame = 0;
1509          }
1510
1511        n_this_frame++;
1512        n_left_to_next--;
1513        to_next[0] = bo0;
1514        to_next += 1;
1515
1516        if (PREDICT_FALSE(n_left_to_next == 0))
1517          {
1518            f->n_vectors = n_this_frame;
1519            vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1520            f = 0;
1521          }
1522      }
1523  }));
1524  /* *INDENT-ON* */
1525
1526  if (f)
1527    {
1528      ASSERT (n_this_frame);
1529      f->n_vectors = n_this_frame;
1530      vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1531    }
1532  return 0;
1533}
1534
1535static void
1536ip6_ra_handle_report (const ip6_ra_report_t * rap)
1537{
1538  u32 ii;
1539
1540  vec_foreach_index (ii, ip6_ra_listeners) ip6_ra_listeners[ii] (rap);
1541}
1542
1543static uword
1544ip6_ra_event_process (vlib_main_t * vm,
1545		      vlib_node_runtime_t * node, vlib_frame_t * frame)
1546{
1547  ip6_ra_report_t *r, *rs;
1548  uword event_type;
1549
1550  /* init code here */
1551
1552  while (1)
1553    {
1554      vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
1555
1556      rs = vlib_process_get_event_data (vm, &event_type);
1557
1558      if (NULL == rs)
1559	{
1560	  /* No events found: timer expired. */
1561	  /* process interface list and send RAs as appropriate, update timer info */
1562	  ip6_ra_process_timer_event (vm, node, frame);
1563	}
1564      else
1565	{
1566	  vec_foreach (r, rs) ip6_ra_handle_report (r);
1567	  vec_reset_length (rs);
1568	}
1569    }
1570  return frame->n_vectors;
1571}
1572
1573VLIB_REGISTER_NODE (ip6_ra_process_node) =
1574{
1575.function = ip6_ra_event_process,.name = "ip6-ra-process",.type =
1576    VLIB_NODE_TYPE_PROCESS,};
1577
1578static void
1579ip6_ra_signal_report (ip6_ra_report_t * r)
1580{
1581  vlib_main_t *vm = vlib_get_main ();
1582  ip6_ra_report_t *q;
1583
1584  if (!vec_len (ip6_ra_listeners))
1585    return;
1586
1587  q = vlib_process_signal_event_data (vm,
1588				      ip6_ra_process_node.index,
1589				      0, 1, sizeof *q);
1590  *q = *r;
1591}
1592
1593static int
1594ip6_ra_publish (ip6_ra_report_t * r)
1595{
1596  void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
1597  vl_api_rpc_call_main_thread (ip6_ra_signal_report, (u8 *) r, sizeof *r);
1598  return 0;
1599}
1600
1601/* API support functions */
1602int
1603ip6_ra_config (vlib_main_t * vm, u32 sw_if_index,
1604	       u8 suppress, u8 managed, u8 other,
1605	       u8 ll_option, u8 send_unicast, u8 cease,
1606	       u8 use_lifetime, u32 lifetime,
1607	       u32 initial_count, u32 initial_interval,
1608	       u32 max_interval, u32 min_interval, u8 is_no)
1609{
1610  ip6_ra_t *radv_info;
1611
1612  /* look up the radv_t  information for this interface */
1613  radv_info = ip6_ra_get_itf (sw_if_index);
1614
1615  if (!radv_info)
1616    return (VNET_API_ERROR_IP6_NOT_ENABLED);
1617
1618  if ((max_interval != 0) && (min_interval == 0))
1619    min_interval = .75 * max_interval;
1620
1621  max_interval =
1622    (max_interval !=
1623     0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
1624    radv_info->max_radv_interval;
1625  min_interval =
1626    (min_interval !=
1627     0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
1628    radv_info->min_radv_interval;
1629  lifetime =
1630    (use_lifetime !=
1631     0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
1632    radv_info->adv_router_lifetime_in_sec;
1633
1634  if (lifetime)
1635    {
1636      if (lifetime > MAX_DEF_RTR_LIFETIME)
1637	lifetime = MAX_DEF_RTR_LIFETIME;
1638
1639      if (lifetime <= max_interval)
1640	return VNET_API_ERROR_INVALID_VALUE;
1641    }
1642
1643  if (min_interval != 0)
1644    {
1645      if ((min_interval > .75 * max_interval) || (min_interval < 3))
1646	return VNET_API_ERROR_INVALID_VALUE;
1647    }
1648
1649  if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
1650      (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
1651    return VNET_API_ERROR_INVALID_VALUE;
1652
1653  /*
1654     if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
1655     if "flag" is clear  don't change corresponding value
1656   */
1657  radv_info->send_radv =
1658    (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
1659  radv_info->adv_managed_flag =
1660    (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
1661  radv_info->adv_other_flag =
1662    (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
1663  radv_info->adv_link_layer_address =
1664    (ll_option != 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
1665  radv_info->send_unicast =
1666    (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
1667  radv_info->cease_radv =
1668    (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
1669
1670  radv_info->min_radv_interval = min_interval;
1671  radv_info->max_radv_interval = max_interval;
1672  radv_info->adv_router_lifetime_in_sec = lifetime;
1673
1674  radv_info->initial_adverts_count =
1675    (initial_count !=
1676     0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
1677    radv_info->initial_adverts_count;
1678  radv_info->initial_adverts_interval =
1679    (initial_interval !=
1680     0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
1681    radv_info->initial_adverts_interval;
1682
1683  /* restart */
1684  if ((cease != 0) && (is_no))
1685    radv_info->send_radv = 1;
1686
1687  radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1688  radv_info->next_multicast_time = vlib_time_now (vm);
1689  radv_info->last_multicast_time = vlib_time_now (vm);
1690  radv_info->last_radv_time = 0;
1691
1692  return (0);
1693}
1694
1695
1696int
1697ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
1698	       ip6_address_t * prefix_addr, u8 prefix_len,
1699	       u8 use_default, u32 val_lifetime, u32 pref_lifetime,
1700	       u8 no_advertise, u8 off_link, u8 no_autoconfig,
1701	       u8 no_onlink, u8 is_no)
1702{
1703  ip6_ra_t *radv_info;
1704
1705  /* look up the radv_t  information for this interface */
1706  radv_info = ip6_ra_get_itf (sw_if_index);
1707
1708  if (!radv_info)
1709    return (VNET_API_ERROR_IP6_NOT_ENABLED);
1710
1711  f64 now = vlib_time_now (vm);
1712
1713  /* prefix info add, delete or update */
1714  ip6_radv_prefix_t *prefix;
1715
1716  /* lookup  prefix info for this  address on this interface */
1717  uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
1718
1719  prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
1720
1721  if (is_no)
1722    {
1723      /* delete */
1724      if (!prefix)
1725	return VNET_API_ERROR_INVALID_VALUE;	/* invalid prefix */
1726
1727      if (prefix->prefix_len != prefix_len)
1728	return VNET_API_ERROR_INVALID_VALUE_2;
1729
1730      /* FIXME - Should the DP do this or the CP ? */
1731      /* do specific delete processing here before returning */
1732      /* try to remove from routing table */
1733
1734      mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
1735		   /* old_value */ 0);
1736      pool_put (radv_info->adv_prefixes_pool, prefix);
1737
1738      radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1739      radv_info->next_multicast_time = vlib_time_now (vm);
1740      radv_info->last_multicast_time = vlib_time_now (vm);
1741      radv_info->last_radv_time = 0;
1742      return (0);
1743    }
1744
1745  /* adding or changing */
1746  if (!prefix)
1747    {
1748      /* add */
1749      u32 pi;
1750      pool_get_zero (radv_info->adv_prefixes_pool, prefix);
1751      pi = prefix - radv_info->adv_prefixes_pool;
1752      mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
1753		 /* old_value */ 0);
1754
1755      clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
1756
1757      prefix->prefix_len = prefix_len;
1758      clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
1759
1760      /* initialize default values */
1761      prefix->adv_on_link_flag = 1;	/* L bit set */
1762      prefix->adv_autonomous_flag = 1;	/* A bit set */
1763      prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
1764      prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
1765      prefix->enabled = 1;
1766      prefix->decrement_lifetime_flag = 1;
1767      prefix->deprecated_prefix_flag = 1;
1768
1769      if (off_link == 0)
1770	{
1771	  /* FIXME - Should the DP do this or the CP ? */
1772	  /* insert prefix into routing table as a connected prefix */
1773	}
1774
1775      if (use_default)
1776	goto restart;
1777    }
1778  else
1779    {
1780
1781      if (prefix->prefix_len != prefix_len)
1782	return VNET_API_ERROR_INVALID_VALUE_2;
1783
1784      if (off_link != 0)
1785	{
1786	  /* FIXME - Should the DP do this or the CP ? */
1787	  /* remove from routing table if already there */
1788	}
1789    }
1790
1791  if ((val_lifetime == ~0) || (pref_lifetime == ~0))
1792    {
1793      prefix->adv_valid_lifetime_in_secs = ~0;
1794      prefix->adv_pref_lifetime_in_secs = ~0;
1795      prefix->decrement_lifetime_flag = 0;
1796    }
1797  else
1798    {
1799      prefix->adv_valid_lifetime_in_secs = val_lifetime;;
1800      prefix->adv_pref_lifetime_in_secs = pref_lifetime;
1801    }
1802
1803  /* copy  remaining */
1804  prefix->enabled = !(no_advertise != 0);
1805  prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
1806  prefix->adv_autonomous_flag = !(no_autoconfig != 0);
1807
1808restart:
1809  /* restart */
1810  /* fill in the expiration times  */
1811  prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
1812  prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
1813
1814  radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1815  radv_info->next_multicast_time = vlib_time_now (vm);
1816  radv_info->last_multicast_time = vlib_time_now (vm);
1817  radv_info->last_radv_time = 0;
1818
1819  return (0);
1820}
1821
1822clib_error_t *
1823ip6_ra_cmd (vlib_main_t * vm,
1824	    unformat_input_t * main_input, vlib_cli_command_t * cmd)
1825{
1826  vnet_main_t *vnm = vnet_get_main ();
1827  clib_error_t *error = 0;
1828  u8 is_no = 0;
1829  u8 suppress = 0, managed = 0, other = 0;
1830  u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
1831  u8 use_lifetime = 0;
1832  u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
1833    0, ra_initial_interval = 0;
1834  u32 ra_max_interval = 0, ra_min_interval = 0;
1835
1836  unformat_input_t _line_input, *line_input = &_line_input;
1837
1838  int add_radv_info = 1;
1839  ip6_address_t ip6_addr;
1840  u32 addr_len;
1841
1842
1843  /* Get a line of input. */
1844  if (!unformat_user (main_input, unformat_line_input, line_input))
1845    return 0;
1846
1847  /* get basic radv info for this interface */
1848  if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1849    {
1850
1851      if (unformat_user (line_input,
1852			 unformat_vnet_sw_interface, vnm, &sw_if_index))
1853	{
1854	  if (!ip6_ra_get_eth_itf (sw_if_index))
1855	    {
1856	      error =
1857		clib_error_return (0, "Interface must be of ethernet type");
1858	      goto done;
1859	    }
1860
1861	  if (!ip6_link_is_enabled (sw_if_index))
1862	    {
1863	      error = clib_error_return (0, "IP6 nt enabler interface %U'",
1864					 format_unformat_error, line_input);
1865	      goto done;
1866	    }
1867	}
1868      else
1869	{
1870	  error = clib_error_return (0, "invalid interface name %U'",
1871				     format_unformat_error, line_input);
1872	  goto done;
1873	}
1874    }
1875
1876  /* get the rest of the command */
1877  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1878    {
1879      if (unformat (line_input, "no"))
1880	is_no = 1;
1881      else if (unformat (line_input, "prefix %U/%d",
1882			 unformat_ip6_address, &ip6_addr, &addr_len))
1883	{
1884	  add_radv_info = 0;
1885	  break;
1886	}
1887      else if (unformat (line_input, "ra-managed-config-flag"))
1888	{
1889	  managed = 1;
1890	  break;
1891	}
1892      else if (unformat (line_input, "ra-other-config-flag"))
1893	{
1894	  other = 1;
1895	  break;
1896	}
1897      else if (unformat (line_input, "ra-suppress") ||
1898	       unformat (line_input, "ra-surpress"))
1899	{
1900	  suppress = 1;
1901	  break;
1902	}
1903      else if (unformat (line_input, "ra-suppress-link-layer") ||
1904	       unformat (line_input, "ra-surpress-link-layer"))
1905	{
1906	  suppress_ll_option = 1;
1907	  break;
1908	}
1909      else if (unformat (line_input, "ra-send-unicast"))
1910	{
1911	  send_unicast = 1;
1912	  break;
1913	}
1914      else if (unformat (line_input, "ra-lifetime"))
1915	{
1916	  if (!unformat (line_input, "%d", &ra_lifetime))
1917	    {
1918	      error = unformat_parse_error (line_input);
1919	      goto done;
1920	    }
1921	  use_lifetime = 1;
1922	  break;
1923	}
1924      else if (unformat (line_input, "ra-initial"))
1925	{
1926	  if (!unformat
1927	      (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
1928	    {
1929	      error = unformat_parse_error (line_input);
1930	      goto done;
1931	    }
1932	  break;
1933	}
1934      else if (unformat (line_input, "ra-interval"))
1935	{
1936	  if (!unformat (line_input, "%d", &ra_max_interval))
1937	    {
1938	      error = unformat_parse_error (line_input);
1939	      goto done;
1940	    }
1941
1942	  if (!unformat (line_input, "%d", &ra_min_interval))
1943	    ra_min_interval = 0;
1944	  break;
1945	}
1946      else if (unformat (line_input, "ra-cease"))
1947	{
1948	  cease = 1;
1949	  break;
1950	}
1951      else
1952	{
1953	  error = unformat_parse_error (line_input);
1954	  goto done;
1955	}
1956    }
1957
1958  if (add_radv_info)
1959    {
1960      ip6_ra_config (vm, sw_if_index,
1961		     suppress, managed, other,
1962		     suppress_ll_option, send_unicast, cease,
1963		     use_lifetime, ra_lifetime,
1964		     ra_initial_count, ra_initial_interval,
1965		     ra_max_interval, ra_min_interval, is_no);
1966    }
1967  else
1968    {
1969      u32 valid_lifetime_in_secs = 0;
1970      u32 pref_lifetime_in_secs = 0;
1971      u8 use_prefix_default_values = 0;
1972      u8 no_advertise = 0;
1973      u8 off_link = 0;
1974      u8 no_autoconfig = 0;
1975      u8 no_onlink = 0;
1976
1977      /* get the rest of the command */
1978      while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1979	{
1980	  if (unformat (line_input, "default"))
1981	    {
1982	      use_prefix_default_values = 1;
1983	      break;
1984	    }
1985	  else if (unformat (line_input, "infinite"))
1986	    {
1987	      valid_lifetime_in_secs = ~0;
1988	      pref_lifetime_in_secs = ~0;
1989	      break;
1990	    }
1991	  else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
1992			     &pref_lifetime_in_secs))
1993	    break;
1994	  else
1995	    break;
1996	}
1997
1998
1999      /* get the rest of the command */
2000      while (!use_prefix_default_values &&
2001	     unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2002	{
2003	  if (unformat (line_input, "no-advertise"))
2004	    no_advertise = 1;
2005	  else if (unformat (line_input, "off-link"))
2006	    off_link = 1;
2007	  else if (unformat (line_input, "no-autoconfig"))
2008	    no_autoconfig = 1;
2009	  else if (unformat (line_input, "no-onlink"))
2010	    no_onlink = 1;
2011	  else
2012	    {
2013	      error = unformat_parse_error (line_input);
2014	      goto done;
2015	    }
2016	}
2017
2018      ip6_ra_prefix (vm, sw_if_index,
2019		     &ip6_addr, addr_len,
2020		     use_prefix_default_values,
2021		     valid_lifetime_in_secs,
2022		     pref_lifetime_in_secs,
2023		     no_advertise, off_link, no_autoconfig, no_onlink, is_no);
2024    }
2025
2026done:
2027  unformat_free (line_input);
2028
2029  return error;
2030}
2031
2032static u8 *
2033format_ip6_ra (u8 * s, va_list * args)
2034{
2035  index_t rai = va_arg (*args, index_t);
2036  u32 indent = va_arg (*args, u32);
2037  ip6_radv_prefix_t *p;
2038  ip6_ra_t *radv_info;
2039
2040  radv_info = pool_elt_at_index (ip6_ra_pool, rai);
2041
2042  s = format (s, "%UAdvertised Prefixes:\n", format_white_space, indent);
2043
2044  indent += 2;
2045
2046  /* *INDENT-OFF* */
2047  pool_foreach (p, radv_info->adv_prefixes_pool,
2048  ({
2049    s = format (s, "%Uprefix %U, length %d\n",
2050                format_white_space, indent+2,
2051                format_ip6_address, &p->prefix, p->prefix_len);
2052  }));
2053  /* *INDENT-ON* */
2054
2055  s = format (s, "%UMTU is %d\n",
2056	      format_white_space, indent, radv_info->adv_link_mtu);
2057  s = format (s, "%UICMP error messages are unlimited\n",
2058	      format_white_space, indent);
2059  s = format (s, "%UICMP redirects are disabled\n",
2060	      format_white_space, indent);
2061  s = format (s, "%UICMP unreachables are not sent\n",
2062	      format_white_space, indent);
2063  s = format (s, "%UND DAD is disabled\n", format_white_space, indent);
2064  //s = format (s, "%UND reachable time is %d milliseconds\n",);
2065  s = format (s, "%UND advertised reachable time is %d\n",
2066	      format_white_space, indent,
2067	      radv_info->adv_neighbor_reachable_time_in_msec);
2068  s = format (s,
2069	      "%UND advertised retransmit interval is %d (msec)\n",
2070	      format_white_space, indent,
2071	      radv_info->
2072	      adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2073  s =
2074    format (s,
2075	    "%UND router advertisements are sent every %0.1f seconds (min interval is %0.1f)\n",
2076	    format_white_space, indent, radv_info->max_radv_interval,
2077	    radv_info->min_radv_interval);
2078  s =
2079    format (s, "%UND router advertisements live for %d seconds\n",
2080	    format_white_space, indent,
2081	    radv_info->adv_router_lifetime_in_sec);
2082  s =
2083    format (s, "%UHosts %s stateless autoconfig for addresses\n",
2084	    format_white_space, indent,
2085	    (radv_info->adv_managed_flag) ? "use" : " don't use");
2086  s =
2087    format (s, "%UND router advertisements sent %d\n", format_white_space,
2088	    indent, radv_info->n_advertisements_sent);
2089  s =
2090    format (s, "%UND router solicitations received %d\n", format_white_space,
2091	    indent, radv_info->n_solicitations_rcvd);
2092  s =
2093    format (s, "%UND router solicitations dropped %d\n", format_white_space,
2094	    indent, radv_info->n_solicitations_dropped);
2095
2096  return (s);
2097}
2098
2099
2100/*?
2101 * This command is used to configure the neighbor discovery
2102 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
2103 * command to display some of the current neighbor discovery parameters
2104 * on a given interface. This command has three formats:
2105 *
2106 *
2107 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
2108 *
2109 * '<em><b>ip6 nd <interface> [no] [ra-managed-config-flag] | [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] | [ra-send-unicast] | [ra-lifetime <lifetime>] | [ra-initial <cnt> <interval>] | [ra-interval <max-interval> [<min-interval>]] | [ra-cease]</b></em>'
2110 *
2111 * Where:
2112 *
2113 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
2114 * router-advertisement messages to use stateful address
2115 * auto-configuration to obtain address information (sets the M-bit).
2116 * Default is the M-bit is not set and the '<em>no</em>' option
2117 * returns it to this default state.
2118 *
2119 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
2120 * router-advertisement messages that hosts use stateful auto
2121 * configuration to obtain nonaddress related information (sets
2122 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
2123 * option returns it to this default state.
2124 *
2125 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
2126 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
2127 * router-advertisement messages.
2128 *
2129 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
2130 * optional source link-layer address in the ICMPv6 router-advertisement
2131 * messages. Default is to include the optional source link-layer address
2132 * and the '<em>no</em>' option returns it to this default state.
2133 *
2134 * <em>[no] ra-send-unicast</em> - Use the source address of the
2135 * router-solicitation message if availiable. The default is to use
2136 * multicast address of all nodes, and the '<em>no</em>' option returns
2137 * it to this default state.
2138 *
2139 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
2140 * default router in ICMPv6 router-advertisement messages. The range is
2141 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
2142 * '<em><max-interval></em>'. The default value is 600 seconds and the
2143 * '<em>no</em>' option returns it to this default value.
2144 *
2145 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
2146 * router-advertisement messages sent and the interval between each
2147 * message. Range for count is 1 - 3 and default is 3. Range for interval
2148 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
2149 * returns both to their default value.
2150 *
2151 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
2152 * interval between sending ICMPv6 router-advertisement messages. The
2153 * range for max-interval is from 4 to 200 seconds. min-interval can not
2154 * be more than 75% of max-interval. If not set, min-interval will be
2155 * set to 75% of max-interval. The range for min-interval is from 3 to
2156 * 150 seconds.  The '<em>no</em>' option returns both to their default
2157 * value.
2158 *
2159 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
2160 * The '<em>no</em>' options implies to start (or restart) sending
2161 * ICMPv6 router-advertisement messages.
2162 *
2163 *
2164 * <b>Format 2 - Prefix Options:</b>
2165 *
2166 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link] [no-autoconfig] [no-onlink]</b></em>'
2167 *
2168 * Where:
2169 *
2170 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
2171 *
2172 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
2173 * length of time in seconds during what the prefix is valid for the purpose of
2174 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
2175 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
2176 * length of time in seconds during what addresses generated from the prefix remain
2177 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
2178 *
2179 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
2180 * are inifinte, no timeout.
2181 *
2182 * <em>no-advertise</em> - Do not send full router address in prefix
2183 * advertisement. Default is to advertise (i.e. - This flag is off by default).
2184 *
2185 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
2186 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
2187 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
2188 *
2189 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
2190 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
2191 *
2192 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
2193 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
2194 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
2195 * the L-bit.
2196 *
2197 *
2198 * <b>Format 3: - Default of Prefix:</b>
2199 *
2200 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
2201 *
2202 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
2203 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
2204 * is ignored and the prefix is deleted.
2205 *
2206 *
2207 * @cliexpar
2208 * Example of how set a router advertisement option:
2209 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
2210 * Example of how to add a prefix:
2211 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
2212 * Example of how to delete a prefix:
2213 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
2214?*/
2215/* *INDENT-OFF* */
2216VLIB_CLI_COMMAND (ip6_nd_command, static) =
2217{
2218  .path = "ip6 nd",
2219  .short_help = "ip6 nd <interface> ...",
2220  .function = ip6_ra_cmd,
2221};
2222/* *INDENT-ON* */
2223
2224/**
2225 * VFT for registering as a delegate to an IP6 link
2226 */
2227const static ip6_link_delegate_vft_t ip6_ra_delegate_vft = {
2228  .ildv_disable = ip6_ra_delegate_disable,
2229  .ildv_enable = ip6_ra_link_enable,
2230  .ildv_format = format_ip6_ra,
2231};
2232
2233static clib_error_t *
2234ip6_ra_init (vlib_main_t * vm)
2235{
2236  vlib_call_init_function (vm, icmp6_init);
2237
2238  icmp6_register_type (vm, ICMP6_router_solicitation,
2239		       ip6_icmp_router_solicitation_node.index);
2240  icmp6_register_type (vm, ICMP6_router_advertisement,
2241		       ip6_icmp_router_advertisement_node.index);
2242
2243  ip6_ra_delegate_id = ip6_link_delegate_register (&ip6_ra_delegate_vft);
2244
2245  return (NULL);
2246}
2247
2248/* *INDENT-OFF* */
2249VLIB_INIT_FUNCTION (ip6_ra_init) =
2250{
2251  .runs_after = VLIB_INITS("icmp6_init"),
2252};
2253/* *INDENT-ON* */
2254
2255/*
2256 * fd.io coding-style-patch-verification: ON
2257 *
2258 * Local Variables:
2259 * eval: (c-set-style "gnu")
2260 * End:
2261 */
2262