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