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