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