VPP-635: CLI Memory leak with invalid parameter
[vpp.git] / src / vnet / ip / ip6_neighbor.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/ip/ip.h>
19 #include <vnet/ip/ip6_neighbor.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vppinfra/mhash.h>
22 #include <vppinfra/md5.h>
23 #include <vnet/adj/adj.h>
24 #include <vnet/adj/adj_mcast.h>
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/fib/ip6_fib.h>
27
28 /**
29  * @file
30  * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
31  *
32  * The files contains the API and CLI code for managing IPv6 neighbor
33  * adjacency tables and neighbor discovery logic.
34  */
35
36 /* can't use sizeof link_layer_address, that's 8 */
37 #define ETHER_MAC_ADDR_LEN 6
38
39 /* advertised prefix option */
40 typedef struct
41 {
42   /* basic advertised information */
43   ip6_address_t prefix;
44   u8 prefix_len;
45   int adv_on_link_flag;
46   int adv_autonomous_flag;
47   u32 adv_valid_lifetime_in_secs;
48   u32 adv_pref_lifetime_in_secs;
49
50   /* advertised values are computed from these times if decrementing */
51   f64 valid_lifetime_expires;
52   f64 pref_lifetime_expires;
53
54   /* local information */
55   int enabled;
56   int deprecated_prefix_flag;
57   int decrement_lifetime_flag;
58
59 #define MIN_ADV_VALID_LIFETIME 7203     /* seconds */
60 #define DEF_ADV_VALID_LIFETIME  2592000
61 #define DEF_ADV_PREF_LIFETIME 604800
62
63   /* extensions are added here, mobile, DNS etc.. */
64 } ip6_radv_prefix_t;
65
66
67 typedef struct
68 {
69   /* group information */
70   u8 type;
71   ip6_address_t mcast_address;
72   u16 num_sources;
73   ip6_address_t *mcast_source_address_pool;
74 } ip6_mldp_group_t;
75
76 /* configured router advertisement information per ipv6 interface */
77 typedef struct
78 {
79
80   /* advertised config information, zero means unspecified  */
81   u8 curr_hop_limit;
82   int adv_managed_flag;
83   int adv_other_flag;
84   u16 adv_router_lifetime_in_sec;
85   u32 adv_neighbor_reachable_time_in_msec;
86   u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
87
88   /* mtu option */
89   u32 adv_link_mtu;
90
91   /* source link layer option */
92   u8 link_layer_address[8];
93   u8 link_layer_addr_len;
94
95   /* prefix option */
96   ip6_radv_prefix_t *adv_prefixes_pool;
97
98   /* Hash table mapping address to index in interface advertised  prefix pool. */
99   mhash_t address_to_prefix_index;
100
101   /* MLDP  group information */
102   ip6_mldp_group_t *mldp_group_pool;
103
104   /* Hash table mapping address to index in mldp address pool. */
105   mhash_t address_to_mldp_index;
106
107   /* local information */
108   u32 sw_if_index;
109   u32 fib_index;
110   int send_radv;                /* radv on/off on this interface -  set by config */
111   int cease_radv;               /* we are ceasing  to send  - set byf config */
112   int send_unicast;
113   int adv_link_layer_address;
114   int prefix_option;
115   int failed_device_check;
116   int all_routers_mcast;
117   u32 seed;
118   u64 randomizer;
119   int ref_count;
120   adj_index_t mcast_adj_index;
121
122   /* timing information */
123 #define DEF_MAX_RADV_INTERVAL 200
124 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125 #define DEF_CURR_HOP_LIMIT  64
126 #define DEF_DEF_RTR_LIFETIME   3 * DEF_MAX_RADV_INTERVAL
127 #define MAX_DEF_RTR_LIFETIME   9000
128
129 #define MAX_INITIAL_RTR_ADVERT_INTERVAL   16    /* seconds */
130 #define MAX_INITIAL_RTR_ADVERTISEMENTS        3 /*transmissions */
131 #define MIN_DELAY_BETWEEN_RAS                              3    /* seconds */
132 #define MAX_DELAY_BETWEEN_RAS                    1800   /* seconds */
133 #define MAX_RA_DELAY_TIME                                          .5   /* seconds */
134
135   f64 max_radv_interval;
136   f64 min_radv_interval;
137   f64 min_delay_between_radv;
138   f64 max_delay_between_radv;
139   f64 max_rtr_default_lifetime;
140
141   f64 last_radv_time;
142   f64 last_multicast_time;
143   f64 next_multicast_time;
144
145
146   u32 initial_adverts_count;
147   f64 initial_adverts_interval;
148   u32 initial_adverts_sent;
149
150   /* stats */
151   u32 n_advertisements_sent;
152   u32 n_solicitations_rcvd;
153   u32 n_solicitations_dropped;
154
155   /* Link local address to use (defaults to underlying physical for logical interfaces */
156   ip6_address_t link_local_address;
157 } ip6_radv_t;
158
159 typedef struct
160 {
161   u32 next_index;
162   uword node_index;
163   uword type_opaque;
164   uword data;
165   /* Used for nd event notification only */
166   void *data_callback;
167   u32 pid;
168 } pending_resolution_t;
169
170
171 typedef struct
172 {
173   /* Hash tables mapping name to opcode. */
174   uword *opcode_by_name;
175
176   /* lite beer "glean" adjacency handling */
177   mhash_t pending_resolutions_by_address;
178   pending_resolution_t *pending_resolutions;
179
180   /* Mac address change notification */
181   mhash_t mac_changes_by_address;
182   pending_resolution_t *mac_changes;
183
184   u32 *neighbor_input_next_index_by_hw_if_index;
185
186   ip6_neighbor_t *neighbor_pool;
187
188   mhash_t neighbor_index_by_key;
189
190   u32 *if_radv_pool_index_by_sw_if_index;
191
192   ip6_radv_t *if_radv_pool;
193
194   /* Neighbor attack mitigation */
195   u32 limit_neighbor_cache_size;
196   u32 neighbor_delete_rotor;
197
198 } ip6_neighbor_main_t;
199
200 static ip6_neighbor_main_t ip6_neighbor_main;
201 static ip6_address_t ip6a_zero; /* ip6 address 0 */
202
203 static u8 *
204 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
205 {
206   vlib_main_t *vm = va_arg (*va, vlib_main_t *);
207   ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
208   vnet_main_t *vnm = vnet_get_main ();
209   vnet_sw_interface_t *si;
210   u8 *flags = 0;
211
212   if (!n)
213     return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags",
214                    "Link layer", "Interface");
215
216   if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
217     flags = format (flags, "D");
218
219   if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
220     flags = format (flags, "S");
221
222   si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
223   s = format (s, "%=12U%=20U%=6s%=20U%=40U",
224               format_vlib_cpu_time, vm, n->cpu_time_last_updated,
225               format_ip6_address, &n->key.ip6_address,
226               flags ? (char *) flags : "",
227               format_ethernet_address, n->link_layer_address,
228               format_vnet_sw_interface_name, vnm, si);
229
230   vec_free (flags);
231   return s;
232 }
233
234 static clib_error_t *
235 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
236                                    u32 sw_if_index, u32 flags)
237 {
238   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
239   ip6_neighbor_t *n;
240
241   if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
242     {
243       u32 i, *to_delete = 0;
244
245       /* *INDENT-OFF* */
246       pool_foreach (n, nm->neighbor_pool,
247       ({
248         if (n->key.sw_if_index == sw_if_index)
249           vec_add1 (to_delete, n - nm->neighbor_pool);
250       }));
251       /* *INDENT-ON* */
252
253       for (i = 0; i < vec_len (to_delete); i++)
254         {
255           n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
256           mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
257           fib_table_entry_delete_index (n->fib_entry_index, FIB_SOURCE_ADJ);
258           pool_put (nm->neighbor_pool, n);
259         }
260
261       vec_free (to_delete);
262     }
263
264   return 0;
265 }
266
267 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
268
269 static void
270 unset_random_neighbor_entry (void)
271 {
272   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
273   vnet_main_t *vnm = vnet_get_main ();
274   vlib_main_t *vm = vnm->vlib_main;
275   ip6_neighbor_t *e;
276   u32 index;
277
278   index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
279   nm->neighbor_delete_rotor = index;
280
281   /* Try again from elt 0, could happen if an intfc goes down */
282   if (index == ~0)
283     {
284       index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
285       nm->neighbor_delete_rotor = index;
286     }
287
288   /* Nothing left in the pool */
289   if (index == ~0)
290     return;
291
292   e = pool_elt_at_index (nm->neighbor_pool, index);
293
294   vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
295                                     &e->key.ip6_address,
296                                     e->link_layer_address,
297                                     ETHER_MAC_ADDR_LEN);
298 }
299
300 typedef struct
301 {
302   u8 is_add;
303   u8 is_static;
304   u8 link_layer_address[6];
305   u32 sw_if_index;
306   ip6_address_t addr;
307 } ip6_neighbor_set_unset_rpc_args_t;
308
309 static void ip6_neighbor_set_unset_rpc_callback
310   (ip6_neighbor_set_unset_rpc_args_t * a);
311
312 static void set_unset_ip6_neighbor_rpc
313   (vlib_main_t * vm,
314    u32 sw_if_index,
315    ip6_address_t * a, u8 * link_layer_addreess, int is_add, int is_static)
316 {
317   ip6_neighbor_set_unset_rpc_args_t args;
318   void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
319
320   args.sw_if_index = sw_if_index;
321   args.is_add = is_add;
322   args.is_static = is_static;
323   clib_memcpy (&args.addr, a, sizeof (*a));
324   clib_memcpy (args.link_layer_address, link_layer_addreess, 6);
325
326   vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
327                                (u8 *) & args, sizeof (args));
328 }
329
330 static void
331 ip6_nbr_probe (ip_adjacency_t * adj)
332 {
333   icmp6_neighbor_solicitation_header_t *h;
334   vnet_main_t *vnm = vnet_get_main ();
335   ip6_main_t *im = &ip6_main;
336   ip_interface_address_t *ia;
337   ip6_address_t *dst, *src;
338   vnet_hw_interface_t *hi;
339   vnet_sw_interface_t *si;
340   vlib_buffer_t *b;
341   int bogus_length;
342   vlib_main_t *vm;
343   u32 bi = 0;
344
345   vm = vlib_get_main ();
346
347   si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
348   dst = &adj->sub_type.nbr.next_hop.ip6;
349
350   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
351     {
352       return;
353     }
354   src = ip6_interface_address_matching_destination (im, dst,
355                                                     adj->rewrite_header.
356                                                     sw_if_index, &ia);
357   if (!src)
358     {
359       return;
360     }
361
362   h = vlib_packet_template_get_packet (vm,
363                                        &im->discover_neighbor_packet_template,
364                                        &bi);
365
366   hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
367
368   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
369   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
370   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
371   h->ip.src_address = src[0];
372   h->neighbor.target_address = dst[0];
373
374   clib_memcpy (h->link_layer_option.ethernet_address,
375                hi->hw_address, vec_len (hi->hw_address));
376
377   h->neighbor.icmp.checksum =
378     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
379   ASSERT (bogus_length == 0);
380
381   b = vlib_get_buffer (vm, bi);
382   vnet_buffer (b)->sw_if_index[VLIB_RX] =
383     vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
384
385   /* Add encapsulation string for software interface (e.g. ethernet header). */
386   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
387   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
388
389   {
390     vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
391     u32 *to_next = vlib_frame_vector_args (f);
392     to_next[0] = bi;
393     f->n_vectors = 1;
394     vlib_put_frame_to_node (vm, hi->output_node_index, f);
395   }
396 }
397
398 static void
399 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
400 {
401   adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
402                           ethernet_build_rewrite (vnet_get_main (),
403                                                   nbr->key.sw_if_index,
404                                                   adj_get_link_type (ai),
405                                                   nbr->link_layer_address));
406 }
407
408 static void
409 ip6_nd_mk_incomplete (adj_index_t ai)
410 {
411   ip_adjacency_t *adj = adj_get (ai);
412
413   adj_nbr_update_rewrite (ai,
414                           ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
415                           ethernet_build_rewrite (vnet_get_main (),
416                                                   adj->rewrite_header.
417                                                   sw_if_index,
418                                                   adj_get_link_type (ai),
419                                                   VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
420 }
421
422 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
423 {                                            \
424     k.sw_if_index = sw_if_index;             \
425     k.ip6_address = *addr;                   \
426     k.pad = 0;                               \
427 }
428
429 static ip6_neighbor_t *
430 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
431 {
432   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
433   ip6_neighbor_t *n = NULL;
434   ip6_neighbor_key_t k;
435   uword *p;
436
437   IP6_NBR_MK_KEY (k, sw_if_index, addr);
438
439   p = mhash_get (&nm->neighbor_index_by_key, &k);
440   if (p)
441     {
442       n = pool_elt_at_index (nm->neighbor_pool, p[0]);
443     }
444
445   return (n);
446 }
447
448 static adj_walk_rc_t
449 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
450 {
451   ip6_neighbor_t *nbr = ctx;
452
453   ip6_nd_mk_complete (ai, nbr);
454
455   return (ADJ_WALK_RC_CONTINUE);
456 }
457
458 static adj_walk_rc_t
459 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
460 {
461   ip6_nd_mk_incomplete (ai);
462
463   return (ADJ_WALK_RC_CONTINUE);
464 }
465
466 void
467 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
468 {
469   ip6_neighbor_t *nbr;
470   ip_adjacency_t *adj;
471
472   adj = adj_get (ai);
473
474   nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
475
476   switch (adj->lookup_next_index)
477     {
478     case IP_LOOKUP_NEXT_ARP:
479     case IP_LOOKUP_NEXT_GLEAN:
480       if (NULL != nbr)
481         {
482           adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
483                             ip6_nd_mk_complete_walk, nbr);
484         }
485       else
486         {
487           /*
488            * no matching ND entry.
489            * construct the rewrite required to for an ND packet, and stick
490            * that in the adj's pipe to smoke.
491            */
492           adj_nbr_update_rewrite (ai,
493                                   ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
494                                   ethernet_build_rewrite (vnm,
495                                                           sw_if_index,
496                                                           VNET_LINK_IP6,
497                                                           VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
498
499           /*
500            * since the FIB has added this adj for a route, it makes sense it may
501            * want to forward traffic sometime soon. Let's send a speculative ND.
502            * just one. If we were to do periodically that wouldn't be bad either,
503            * but that's more code than i'm prepared to write at this time for
504            * relatively little reward.
505            */
506           ip6_nbr_probe (adj);
507         }
508       break;
509     case IP_LOOKUP_NEXT_MCAST:
510       /*
511        * Construct a partial rewrite from the known ethernet mcast dest MAC
512        */
513       adj_mcast_update_rewrite
514         (ai,
515          ethernet_build_rewrite (vnm,
516                                  sw_if_index,
517                                  adj->ia_link,
518                                  ethernet_ip6_mcast_dst_addr ()));
519
520       /*
521        * Complete the remaining fields of the adj's rewrite to direct the
522        * complete of the rewrite at switch time by copying in the IP
523        * dst address's bytes.
524        * Ofset is 12 bytes from the end of the MAC header - which is 2
525        * bytes into the desintation address. And we write 4 bytes.
526        */
527       adj->rewrite_header.dst_mcast_offset = 12;
528       adj->rewrite_header.dst_mcast_n_bytes = 4;
529
530       break;
531
532     case IP_LOOKUP_NEXT_DROP:
533     case IP_LOOKUP_NEXT_PUNT:
534     case IP_LOOKUP_NEXT_LOCAL:
535     case IP_LOOKUP_NEXT_REWRITE:
536     case IP_LOOKUP_NEXT_LOAD_BALANCE:
537     case IP_LOOKUP_NEXT_MIDCHAIN:
538     case IP_LOOKUP_NEXT_ICMP_ERROR:
539     case IP_LOOKUP_N_NEXT:
540       ASSERT (0);
541       break;
542     }
543 }
544
545 int
546 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
547                                 u32 sw_if_index,
548                                 ip6_address_t * a,
549                                 u8 * link_layer_address,
550                                 uword n_bytes_link_layer_address,
551                                 int is_static)
552 {
553   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
554   ip6_neighbor_key_t k;
555   ip6_neighbor_t *n = 0;
556   int make_new_nd_cache_entry = 1;
557   uword *p;
558   u32 next_index;
559   pending_resolution_t *pr, *mc;
560
561   if (os_get_cpu_number ())
562     {
563       set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
564                                   1 /* set new neighbor */ , is_static);
565       return 0;
566     }
567
568   k.sw_if_index = sw_if_index;
569   k.ip6_address = a[0];
570   k.pad = 0;
571
572   p = mhash_get (&nm->neighbor_index_by_key, &k);
573   if (p)
574     {
575       n = pool_elt_at_index (nm->neighbor_pool, p[0]);
576       /* Refuse to over-write static neighbor entry. */
577       if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
578         return -2;
579       make_new_nd_cache_entry = 0;
580     }
581
582   if (make_new_nd_cache_entry)
583     {
584       fib_prefix_t pfx = {
585         .fp_len = 128,
586         .fp_proto = FIB_PROTOCOL_IP6,
587         .fp_addr = {
588                     .ip6 = k.ip6_address,
589                     }
590         ,
591       };
592       u32 fib_index;
593
594       pool_get (nm->neighbor_pool, n);
595       mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
596                  /* old value */ 0);
597       n->key = k;
598
599       clib_memcpy (n->link_layer_address,
600                    link_layer_address, n_bytes_link_layer_address);
601
602       /*
603        * create the adj-fib. the entry in the FIB table for and to the peer.
604        */
605       fib_index = ip6_main.fib_index_by_sw_if_index[n->key.sw_if_index];
606       n->fib_entry_index = fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_ADJ, FIB_ENTRY_FLAG_NONE, FIB_PROTOCOL_IP6, &pfx.fp_addr, n->key.sw_if_index, ~0, 1, NULL,      // no label stack
607                                                             FIB_ROUTE_PATH_FLAG_NONE);
608     }
609   else
610     {
611       /*
612        * prevent a DoS attack from the data-plane that
613        * spams us with no-op updates to the MAC address
614        */
615       if (0 == memcmp (n->link_layer_address,
616                        link_layer_address, n_bytes_link_layer_address))
617         return -1;
618
619       clib_memcpy (n->link_layer_address,
620                    link_layer_address, n_bytes_link_layer_address);
621     }
622
623   /* Update time stamp and flags. */
624   n->cpu_time_last_updated = clib_cpu_time_now ();
625   if (is_static)
626     n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
627   else
628     n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
629
630   adj_nbr_walk_nh6 (sw_if_index,
631                     &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
632
633   /* Customer(s) waiting for this address to be resolved? */
634   p = mhash_get (&nm->pending_resolutions_by_address, a);
635   if (p)
636     {
637       next_index = p[0];
638
639       while (next_index != (u32) ~ 0)
640         {
641           pr = pool_elt_at_index (nm->pending_resolutions, next_index);
642           vlib_process_signal_event (vm, pr->node_index,
643                                      pr->type_opaque, pr->data);
644           next_index = pr->next_index;
645           pool_put (nm->pending_resolutions, pr);
646         }
647
648       mhash_unset (&nm->pending_resolutions_by_address, a, 0);
649     }
650
651   /* Customer(s) requesting ND event for this address? */
652   p = mhash_get (&nm->mac_changes_by_address, a);
653   if (p)
654     {
655       next_index = p[0];
656
657       while (next_index != (u32) ~ 0)
658         {
659           int (*fp) (u32, u8 *, u32, ip6_address_t *);
660           int rv = 1;
661           mc = pool_elt_at_index (nm->mac_changes, next_index);
662           fp = mc->data_callback;
663
664           /* Call the user's data callback, return 1 to suppress dup events */
665           if (fp)
666             rv =
667               (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
668           /*
669            * Signal the resolver process, as long as the user
670            * says they want to be notified
671            */
672           if (rv == 0)
673             vlib_process_signal_event (vm, mc->node_index,
674                                        mc->type_opaque, mc->data);
675           next_index = mc->next_index;
676         }
677     }
678
679   return 0;
680 }
681
682 int
683 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
684                                   u32 sw_if_index,
685                                   ip6_address_t * a,
686                                   u8 * link_layer_address,
687                                   uword n_bytes_link_layer_address)
688 {
689   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
690   ip6_neighbor_key_t k;
691   ip6_neighbor_t *n;
692   uword *p;
693   int rv = 0;
694
695   if (os_get_cpu_number ())
696     {
697       set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
698                                   0 /* unset */ , 0);
699       return 0;
700     }
701
702   k.sw_if_index = sw_if_index;
703   k.ip6_address = a[0];
704   k.pad = 0;
705
706   p = mhash_get (&nm->neighbor_index_by_key, &k);
707   if (p == 0)
708     {
709       rv = -1;
710       goto out;
711     }
712
713   n = pool_elt_at_index (nm->neighbor_pool, p[0]);
714   mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
715
716   adj_nbr_walk_nh6 (sw_if_index,
717                     &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
718
719   fib_table_entry_delete_index (n->fib_entry_index, FIB_SOURCE_ADJ);
720   pool_put (nm->neighbor_pool, n);
721
722 out:
723   return rv;
724 }
725
726 static void ip6_neighbor_set_unset_rpc_callback
727   (ip6_neighbor_set_unset_rpc_args_t * a)
728 {
729   vlib_main_t *vm = vlib_get_main ();
730   if (a->is_add)
731     vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
732                                     a->link_layer_address, 6, a->is_static);
733   else
734     vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
735                                       a->link_layer_address, 6);
736 }
737
738 static int
739 ip6_neighbor_sort (void *a1, void *a2)
740 {
741   vnet_main_t *vnm = vnet_get_main ();
742   ip6_neighbor_t *n1 = a1, *n2 = a2;
743   int cmp;
744   cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
745                                    n2->key.sw_if_index);
746   if (!cmp)
747     cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
748   return cmp;
749 }
750
751 ip6_neighbor_t *
752 ip6_neighbors_entries (u32 sw_if_index)
753 {
754   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
755   ip6_neighbor_t *n, *ns = 0;
756
757   /* *INDENT-OFF* */
758   pool_foreach (n, nm->neighbor_pool,
759   ({
760     if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
761       continue;
762     vec_add1 (ns, n[0]);
763   }));
764   /* *INDENT-ON* */
765
766   if (ns)
767     vec_sort_with_function (ns, ip6_neighbor_sort);
768   return ns;
769 }
770
771 static clib_error_t *
772 show_ip6_neighbors (vlib_main_t * vm,
773                     unformat_input_t * input, vlib_cli_command_t * cmd)
774 {
775   vnet_main_t *vnm = vnet_get_main ();
776   ip6_neighbor_t *n, *ns;
777   clib_error_t *error = 0;
778   u32 sw_if_index;
779
780   /* Filter entries by interface if given. */
781   sw_if_index = ~0;
782   (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
783
784   ns = ip6_neighbors_entries (sw_if_index);
785   if (ns)
786     {
787       vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
788       vec_foreach (n, ns)
789       {
790         vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
791       }
792       vec_free (ns);
793     }
794
795   return error;
796 }
797
798 /*?
799  * This command is used to display the adjacent IPv6 hosts found via
800  * neighbor discovery. Optionally, limit the output to the specified
801  * interface.
802  *
803  * @cliexpar
804  * Example of how to display the IPv6 neighbor adjacency table:
805  * @cliexstart{show ip6 neighbors}
806  *     Time           Address       Flags     Link layer                     Interface
807  *      34.0910     ::a:1:1:0:7            02:fe:6a:07:39:6f                GigabitEthernet2/0/0
808  *     173.2916     ::b:5:1:c:2            02:fe:50:62:3a:94                GigabitEthernet2/0/0
809  *     886.6654     ::1:1:c:0:9       S    02:fe:e4:45:27:5b                GigabitEthernet3/0/0
810  * @cliexend
811  * Example of how to display the IPv6 neighbor adjacency table for given interface:
812  * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
813  *     Time           Address       Flags     Link layer                     Interface
814  *      34.0910     ::a:1:1:0:7            02:fe:6a:07:39:6f                GigabitEthernet2/0/0
815  *     173.2916     ::b:5:1:c:2            02:fe:50:62:3a:94                GigabitEthernet2/0/0
816  * @cliexend
817 ?*/
818 /* *INDENT-OFF* */
819 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
820   .path = "show ip6 neighbors",
821   .function = show_ip6_neighbors,
822   .short_help = "show ip6 neighbors [<interface>]",
823 };
824 /* *INDENT-ON* */
825
826 static clib_error_t *
827 set_ip6_neighbor (vlib_main_t * vm,
828                   unformat_input_t * input, vlib_cli_command_t * cmd)
829 {
830   vnet_main_t *vnm = vnet_get_main ();
831   ip6_address_t addr;
832   u8 mac_address[6];
833   int addr_valid = 0;
834   int is_del = 0;
835   int is_static = 0;
836   u32 sw_if_index;
837
838   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
839     {
840       /* intfc, ip6-address, mac-address */
841       if (unformat (input, "%U %U %U",
842                     unformat_vnet_sw_interface, vnm, &sw_if_index,
843                     unformat_ip6_address, &addr,
844                     unformat_ethernet_address, mac_address))
845         addr_valid = 1;
846
847       else if (unformat (input, "delete") || unformat (input, "del"))
848         is_del = 1;
849       else if (unformat (input, "static"))
850         is_static = 1;
851       else
852         break;
853     }
854
855   if (!addr_valid)
856     return clib_error_return (0, "Missing interface, ip6 or hw address");
857
858   if (!is_del)
859     vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
860                                     mac_address, sizeof (mac_address),
861                                     is_static);
862   else
863     vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
864                                       mac_address, sizeof (mac_address));
865   return 0;
866 }
867
868 /*?
869  * This command is used to manually add an entry to the IPv6 neighbor
870  * adjacency table. Optionally, the entry can be added as static. It is
871  * also used to remove an entry from the table. Use the '<em>show ip6
872  * neighbors</em>' command to display all learned and manually entered entries.
873  *
874  * @cliexpar
875  * Example of how to add a static entry to the IPv6 neighbor adjacency table:
876  * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
877  * Example of how to delete an entry from the IPv6 neighbor adjacency table:
878  * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
879 ?*/
880 /* *INDENT-OFF* */
881 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
882 {
883   .path = "set ip6 neighbor",
884   .function = set_ip6_neighbor,
885   .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
886 };
887 /* *INDENT-ON* */
888
889 typedef enum
890 {
891   ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
892   ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
893   ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
894 } icmp6_neighbor_solicitation_or_advertisement_next_t;
895
896 static_always_inline uword
897 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
898                                               vlib_node_runtime_t * node,
899                                               vlib_frame_t * frame,
900                                               uword is_solicitation)
901 {
902   vnet_main_t *vnm = vnet_get_main ();
903   ip6_main_t *im = &ip6_main;
904   uword n_packets = frame->n_vectors;
905   u32 *from, *to_next;
906   u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
907   icmp6_neighbor_discovery_option_type_t option_type;
908   vlib_node_runtime_t *error_node =
909     vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
910   int bogus_length;
911
912   from = vlib_frame_vector_args (frame);
913   n_left_from = n_packets;
914   next_index = node->cached_next_index;
915
916   if (node->flags & VLIB_NODE_FLAG_TRACE)
917     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
918                                    /* stride */ 1,
919                                    sizeof (icmp6_input_trace_t));
920
921   option_type =
922     (is_solicitation
923      ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
924      : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
925   n_advertisements_sent = 0;
926
927   while (n_left_from > 0)
928     {
929       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
930
931       while (n_left_from > 0 && n_left_to_next > 0)
932         {
933           vlib_buffer_t *p0;
934           ip6_header_t *ip0;
935           icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
936           icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
937           u32 bi0, options_len0, sw_if_index0, next0, error0;
938           u32 ip6_sadd_link_local, ip6_sadd_unspecified;
939           int is_rewrite0;
940           u32 ni0;
941
942           bi0 = to_next[0] = from[0];
943
944           from += 1;
945           to_next += 1;
946           n_left_from -= 1;
947           n_left_to_next -= 1;
948
949           p0 = vlib_get_buffer (vm, bi0);
950           ip0 = vlib_buffer_get_current (p0);
951           h0 = ip6_next_header (ip0);
952           options_len0 =
953             clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
954
955           error0 = ICMP6_ERROR_NONE;
956           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
957           ip6_sadd_link_local =
958             ip6_address_is_link_local_unicast (&ip0->src_address);
959           ip6_sadd_unspecified =
960             ip6_address_is_unspecified (&ip0->src_address);
961
962           /* Check that source address is unspecified, link-local or else on-link. */
963           if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
964             {
965               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
966
967               if (ADJ_INDEX_INVALID != src_adj_index0)
968                 {
969                   ip_adjacency_t *adj0 =
970                     ip_get_adjacency (&im->lookup_main, src_adj_index0);
971
972                   /* Allow all realistic-looking rewrite adjacencies to pass */
973                   ni0 = adj0->lookup_next_index;
974                   is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
975                     (ni0 < IP6_LOOKUP_N_NEXT);
976
977                   error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
978                              || !is_rewrite0)
979                             ?
980                             ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
981                             : error0);
982                 }
983               else
984                 {
985                   error0 =
986                     ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
987                 }
988             }
989
990           o0 = (void *) (h0 + 1);
991           o0 = ((options_len0 == 8 && o0->header.type == option_type
992                  && o0->header.n_data_u64s == 1) ? o0 : 0);
993
994           /* If src address unspecified or link local, donot learn neighbor MAC */
995           if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
996                             !ip6_sadd_unspecified && !ip6_sadd_link_local))
997             {
998               ip6_neighbor_main_t *nm = &ip6_neighbor_main;
999               if (nm->limit_neighbor_cache_size &&
1000                   pool_elts (nm->neighbor_pool) >=
1001                   nm->limit_neighbor_cache_size)
1002                 unset_random_neighbor_entry ();
1003               vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1004                                               is_solicitation ?
1005                                               &ip0->src_address :
1006                                               &h0->target_address,
1007                                               o0->ethernet_address,
1008                                               sizeof (o0->ethernet_address),
1009                                               0);
1010             }
1011
1012           if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1013             {
1014               /* Check that target address is local to this router. */
1015               fib_node_index_t fei;
1016               u32 fib_index;
1017
1018               fib_index =
1019                 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1020
1021               if (~0 == fib_index)
1022                 {
1023                   error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1024                 }
1025               else
1026                 {
1027                   fei = ip6_fib_table_lookup_exact_match (fib_index,
1028                                                           &h0->target_address,
1029                                                           128);
1030
1031                   if (FIB_NODE_INDEX_INVALID == fei ||
1032                       !(FIB_ENTRY_FLAG_LOCAL &
1033                         fib_entry_get_flags_for_source (fei,
1034                                                         FIB_SOURCE_INTERFACE)))
1035                     {
1036                       error0 =
1037                         ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1038                     }
1039                 }
1040             }
1041
1042           if (is_solicitation)
1043             next0 = (error0 != ICMP6_ERROR_NONE
1044                      ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1045                      : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1046           else
1047             {
1048               next0 = 0;
1049               error0 = error0 == ICMP6_ERROR_NONE ?
1050                 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1051             }
1052
1053           if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1054             {
1055               vnet_sw_interface_t *sw_if0;
1056               ethernet_interface_t *eth_if0;
1057               ethernet_header_t *eth0;
1058
1059               /* dst address is either source address or the all-nodes mcast addr */
1060               if (!ip6_sadd_unspecified)
1061                 ip0->dst_address = ip0->src_address;
1062               else
1063                 ip6_set_reserved_multicast_address (&ip0->dst_address,
1064                                                     IP6_MULTICAST_SCOPE_link_local,
1065                                                     IP6_MULTICAST_GROUP_ID_all_hosts);
1066
1067               ip0->src_address = h0->target_address;
1068               ip0->hop_limit = 255;
1069               h0->icmp.type = ICMP6_neighbor_advertisement;
1070
1071               sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1072               ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1073               eth_if0 =
1074                 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
1075               if (eth_if0 && o0)
1076                 {
1077                   clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1078                   o0->header.type =
1079                     ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1080                 }
1081
1082               h0->advertisement_flags = clib_host_to_net_u32
1083                 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1084                  | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1085
1086               h0->icmp.checksum = 0;
1087               h0->icmp.checksum =
1088                 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1089                                                    &bogus_length);
1090               ASSERT (bogus_length == 0);
1091
1092               /* Reuse current MAC header, copy SMAC to DMAC and
1093                * interface MAC to SMAC */
1094               vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1095               eth0 = vlib_buffer_get_current (p0);
1096               clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1097               if (eth_if0)
1098                 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1099
1100               /* Setup input and output sw_if_index for packet */
1101               ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1102               vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1103               vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1104                 vnet_main.local_interface_sw_if_index;
1105
1106               n_advertisements_sent++;
1107             }
1108
1109           p0->error = error_node->errors[error0];
1110
1111           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1112                                            to_next, n_left_to_next,
1113                                            bi0, next0);
1114         }
1115
1116       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1117     }
1118
1119   /* Account for advertisements sent. */
1120   vlib_error_count (vm, error_node->node_index,
1121                     ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1122                     n_advertisements_sent);
1123
1124   return frame->n_vectors;
1125 }
1126
1127 /* for "syslogging" - use elog for now */
1128 #define foreach_log_level            \
1129   _ (DEBUG, "DEBUG")                         \
1130   _ (INFO, "INFORMATION")            \
1131   _ (NOTICE, "NOTICE")               \
1132   _ (WARNING, "WARNING")             \
1133   _ (ERR, "ERROR")                                    \
1134   _ (CRIT, "CRITICAL")                        \
1135   _ (ALERT, "ALERT")                          \
1136   _ (EMERG,  "EMERGENCY")
1137
1138 typedef enum
1139 {
1140 #define _(f,s) LOG_##f,
1141   foreach_log_level
1142 #undef _
1143 } log_level_t;
1144
1145 static char *log_level_strings[] = {
1146 #define _(f,s) s,
1147   foreach_log_level
1148 #undef _
1149 };
1150
1151 static int logmask = 1 << LOG_DEBUG;
1152
1153 static void
1154 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1155 {
1156   /* just use elog for now */
1157   u8 *what;
1158   va_list va;
1159
1160   if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1161     return;
1162
1163   va_start (va, fmt);
1164   if (fmt)
1165     {
1166       what = va_format (0, fmt, &va);
1167
1168       ELOG_TYPE_DECLARE (e) =
1169       {
1170       .format = "ip6 nd:  (%s): %s",.format_args = "T4T4",};
1171       struct
1172       {
1173         u32 s[2];
1174       } *ed;
1175       ed = ELOG_DATA (&vm->elog_main, e);
1176       ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1177       ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1178     }
1179   va_end (va);
1180   return;
1181 }
1182
1183 /* ipv6 neighbor discovery - router advertisements */
1184 typedef enum
1185 {
1186   ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1187   ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1188   ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1189   ICMP6_ROUTER_SOLICITATION_N_NEXT,
1190 } icmp6_router_solicitation_or_advertisement_next_t;
1191
1192 static_always_inline uword
1193 icmp6_router_solicitation (vlib_main_t * vm,
1194                            vlib_node_runtime_t * node, vlib_frame_t * frame)
1195 {
1196   vnet_main_t *vnm = vnet_get_main ();
1197   ip6_main_t *im = &ip6_main;
1198   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1199   uword n_packets = frame->n_vectors;
1200   u32 *from, *to_next;
1201   u32 n_left_from, n_left_to_next, next_index;
1202   u32 n_advertisements_sent = 0;
1203   int bogus_length;
1204
1205   icmp6_neighbor_discovery_option_type_t option_type;
1206
1207   vlib_node_runtime_t *error_node =
1208     vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1209
1210   from = vlib_frame_vector_args (frame);
1211   n_left_from = n_packets;
1212   next_index = node->cached_next_index;
1213
1214   if (node->flags & VLIB_NODE_FLAG_TRACE)
1215     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1216                                    /* stride */ 1,
1217                                    sizeof (icmp6_input_trace_t));
1218
1219   /* source may append his LL address */
1220   option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1221
1222   while (n_left_from > 0)
1223     {
1224       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1225
1226       while (n_left_from > 0 && n_left_to_next > 0)
1227         {
1228           vlib_buffer_t *p0;
1229           ip6_header_t *ip0;
1230           ip6_radv_t *radv_info = 0;
1231
1232           icmp6_neighbor_discovery_header_t *h0;
1233           icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1234
1235           u32 bi0, options_len0, sw_if_index0, next0, error0;
1236           u32 is_solicitation = 1, is_dropped = 0;
1237           u32 is_unspecified, is_link_local;
1238
1239           bi0 = to_next[0] = from[0];
1240
1241           from += 1;
1242           to_next += 1;
1243           n_left_from -= 1;
1244           n_left_to_next -= 1;
1245
1246           p0 = vlib_get_buffer (vm, bi0);
1247           ip0 = vlib_buffer_get_current (p0);
1248           h0 = ip6_next_header (ip0);
1249           options_len0 =
1250             clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1251           is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1252           is_link_local =
1253             ip6_address_is_link_local_unicast (&ip0->src_address);
1254
1255           error0 = ICMP6_ERROR_NONE;
1256           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1257
1258           /* check if solicitation  (not from nd_timer node) */
1259           if (ip6_address_is_unspecified (&ip0->dst_address))
1260             is_solicitation = 0;
1261
1262           /* Check that source address is unspecified, link-local or else on-link. */
1263           if (!is_unspecified && !is_link_local)
1264             {
1265               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1266
1267               if (ADJ_INDEX_INVALID != src_adj_index0)
1268                 {
1269                   ip_adjacency_t *adj0 = ip_get_adjacency (&im->lookup_main,
1270                                                            src_adj_index0);
1271
1272                   error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1273                             ?
1274                             ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1275                             : error0);
1276                 }
1277               else
1278                 {
1279                   error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1280                 }
1281             }
1282
1283           /* check for source LL option and process */
1284           o0 = (void *) (h0 + 1);
1285           o0 = ((options_len0 == 8
1286                  && o0->header.type == option_type
1287                  && o0->header.n_data_u64s == 1) ? o0 : 0);
1288
1289           /* if src address unspecified IGNORE any options */
1290           if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1291                             !is_unspecified && !is_link_local))
1292             {
1293               ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1294               if (nm->limit_neighbor_cache_size &&
1295                   pool_elts (nm->neighbor_pool) >=
1296                   nm->limit_neighbor_cache_size)
1297                 unset_random_neighbor_entry ();
1298
1299               vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1300                                               &ip0->src_address,
1301                                               o0->ethernet_address,
1302                                               sizeof (o0->ethernet_address),
1303                                               0);
1304             }
1305
1306           /* default is to drop */
1307           next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1308
1309           if (error0 == ICMP6_ERROR_NONE)
1310             {
1311               vnet_sw_interface_t *sw_if0;
1312               ethernet_interface_t *eth_if0;
1313               u32 adj_index0;
1314
1315               sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1316               ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1317               eth_if0 =
1318                 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
1319
1320               /* only support ethernet interface type for now */
1321               error0 =
1322                 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1323                 : error0;
1324
1325               if (error0 == ICMP6_ERROR_NONE)
1326                 {
1327                   u32 ri;
1328
1329                   /* adjust the sizeof the buffer to just include the ipv6 header */
1330                   p0->current_length -=
1331                     (options_len0 +
1332                      sizeof (icmp6_neighbor_discovery_header_t));
1333
1334                   /* look up the radv_t information for this interface */
1335                   vec_validate_init_empty
1336                     (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1337
1338                   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1339
1340                   if (ri != ~0)
1341                     radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1342
1343                   error0 =
1344                     ((!radv_info) ?
1345                      ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1346                      error0);
1347
1348                   if (error0 == ICMP6_ERROR_NONE)
1349                     {
1350                       f64 now = vlib_time_now (vm);
1351
1352                       /* for solicited adverts - need to rate limit */
1353                       if (is_solicitation)
1354                         {
1355                           if (0 != radv_info->last_radv_time &&
1356                               (now - radv_info->last_radv_time) <
1357                               MIN_DELAY_BETWEEN_RAS)
1358                             is_dropped = 1;
1359                           else
1360                             radv_info->last_radv_time = now;
1361                         }
1362
1363                       /* send now  */
1364                       icmp6_router_advertisement_header_t rh;
1365
1366                       rh.icmp.type = ICMP6_router_advertisement;
1367                       rh.icmp.code = 0;
1368                       rh.icmp.checksum = 0;
1369
1370                       rh.current_hop_limit = radv_info->curr_hop_limit;
1371                       rh.router_lifetime_in_sec =
1372                         clib_host_to_net_u16
1373                         (radv_info->adv_router_lifetime_in_sec);
1374                       rh.
1375                         time_in_msec_between_retransmitted_neighbor_solicitations
1376                         =
1377                         clib_host_to_net_u32 (radv_info->
1378                                               adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1379                       rh.neighbor_reachable_time_in_msec =
1380                         clib_host_to_net_u32 (radv_info->
1381                                               adv_neighbor_reachable_time_in_msec);
1382
1383                       rh.flags =
1384                         (radv_info->adv_managed_flag) ?
1385                         ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1386                         0;
1387                       rh.flags |=
1388                         ((radv_info->adv_other_flag) ?
1389                          ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1390                          0);
1391
1392
1393                       u16 payload_length =
1394                         sizeof (icmp6_router_advertisement_header_t);
1395
1396                       vlib_buffer_add_data (vm,
1397                                             p0->free_list_index,
1398                                             bi0,
1399                                             (void *) &rh,
1400                                             sizeof
1401                                             (icmp6_router_advertisement_header_t));
1402
1403                       if (radv_info->adv_link_layer_address)
1404                         {
1405                           icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1406                             h;
1407
1408                           h.header.type =
1409                             ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1410                           h.header.n_data_u64s = 1;
1411
1412                           /* copy ll address */
1413                           clib_memcpy (&h.ethernet_address[0],
1414                                        eth_if0->address, 6);
1415
1416                           vlib_buffer_add_data (vm,
1417                                                 p0->free_list_index,
1418                                                 bi0,
1419                                                 (void *) &h,
1420                                                 sizeof
1421                                                 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1422
1423                           payload_length +=
1424                             sizeof
1425                             (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1426                         }
1427
1428                       /* add MTU option */
1429                       if (radv_info->adv_link_mtu)
1430                         {
1431                           icmp6_neighbor_discovery_mtu_option_t h;
1432
1433                           h.unused = 0;
1434                           h.mtu =
1435                             clib_host_to_net_u32 (radv_info->adv_link_mtu);
1436                           h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1437                           h.header.n_data_u64s = 1;
1438
1439                           payload_length +=
1440                             sizeof (icmp6_neighbor_discovery_mtu_option_t);
1441
1442                           vlib_buffer_add_data (vm,
1443                                                 p0->free_list_index,
1444                                                 bi0,
1445                                                 (void *) &h,
1446                                                 sizeof
1447                                                 (icmp6_neighbor_discovery_mtu_option_t));
1448                         }
1449
1450                       /* add advertised prefix options  */
1451                       ip6_radv_prefix_t *pr_info;
1452
1453                       /* *INDENT-OFF* */
1454                       pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1455                       ({
1456                         if(pr_info->enabled &&
1457                            (!pr_info->decrement_lifetime_flag
1458                             || (pr_info->pref_lifetime_expires >0)))
1459                           {
1460                             /* advertise this prefix */
1461                             icmp6_neighbor_discovery_prefix_information_option_t h;
1462
1463                             h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1464                             h.header.n_data_u64s  =  (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1465
1466                             h.dst_address_length  = pr_info->prefix_len;
1467
1468                             h.flags  = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1469                             h.flags |= (pr_info->adv_autonomous_flag) ?  ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO :  0;
1470
1471                             if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1472                               {
1473                                 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1474                                 h.preferred_time  = 0;
1475                               }
1476                             else
1477                               {
1478                                 if(pr_info->decrement_lifetime_flag)
1479                                   {
1480                                     pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires  > now)) ?
1481                                       (pr_info->valid_lifetime_expires  - now) : 0;
1482
1483                                     pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires  > now)) ?
1484                                       (pr_info->pref_lifetime_expires  - now) : 0;
1485                                   }
1486
1487                                 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1488                                 h.preferred_time  = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1489                               }
1490                             h.unused  = 0;
1491
1492                             clib_memcpy(&h.dst_address, &pr_info->prefix,  sizeof(ip6_address_t));
1493
1494                             payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1495
1496                             vlib_buffer_add_data (vm,
1497                                                   p0->free_list_index,
1498                                                   bi0,
1499                                                   (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1500
1501                           }
1502                       }));
1503                       /* *INDENT-ON* */
1504
1505                       /* add additional options before here */
1506
1507                       /* finish building the router advertisement... */
1508                       if (!is_unspecified && radv_info->send_unicast)
1509                         {
1510                           ip0->dst_address = ip0->src_address;
1511                         }
1512                       else
1513                         {
1514                           /* target address is all-nodes mcast addr */
1515                           ip6_set_reserved_multicast_address
1516                             (&ip0->dst_address,
1517                              IP6_MULTICAST_SCOPE_link_local,
1518                              IP6_MULTICAST_GROUP_ID_all_hosts);
1519                         }
1520
1521                       /* source address MUST be the link-local address */
1522                       ip0->src_address = radv_info->link_local_address;
1523
1524                       ip0->hop_limit = 255;
1525                       ip0->payload_length =
1526                         clib_host_to_net_u16 (payload_length);
1527
1528                       icmp6_router_advertisement_header_t *rh0 =
1529                         (icmp6_router_advertisement_header_t *) (ip0 + 1);
1530                       rh0->icmp.checksum =
1531                         ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1532                                                            &bogus_length);
1533                       ASSERT (bogus_length == 0);
1534
1535                       /* setup output if and adjacency */
1536                       vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1537                         vnet_main.local_interface_sw_if_index;
1538
1539                       if (is_solicitation)
1540                         {
1541                           ethernet_header_t *eth0;
1542                           /* Reuse current MAC header, copy SMAC to DMAC and
1543                            * interface MAC to SMAC */
1544                           vlib_buffer_reset (p0);
1545                           eth0 = vlib_buffer_get_current (p0);
1546                           clib_memcpy (eth0->dst_address, eth0->src_address,
1547                                        6);
1548                           clib_memcpy (eth0->src_address, eth_if0->address,
1549                                        6);
1550                           next0 =
1551                             is_dropped ? next0 :
1552                             ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1553                           vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1554                             sw_if_index0;
1555                         }
1556                       else
1557                         {
1558                           adj_index0 = radv_info->mcast_adj_index;
1559                           if (adj_index0 == 0)
1560                             error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1561                           else
1562                             {
1563                               next0 =
1564                                 is_dropped ? next0 :
1565                                 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1566                               vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1567                                 adj_index0;
1568                             }
1569                         }
1570                       p0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
1571
1572                       radv_info->n_solicitations_dropped += is_dropped;
1573                       radv_info->n_solicitations_rcvd += is_solicitation;
1574
1575                       if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1576                         {
1577                           radv_info->n_advertisements_sent++;
1578                           n_advertisements_sent++;
1579                         }
1580                     }
1581                 }
1582             }
1583
1584           p0->error = error_node->errors[error0];
1585
1586           if (error0 != ICMP6_ERROR_NONE)
1587             vlib_error_count (vm, error_node->node_index, error0, 1);
1588
1589           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1590                                            to_next, n_left_to_next,
1591                                            bi0, next0);
1592
1593         }
1594
1595       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1596     }
1597
1598   /* Account for router advertisements sent. */
1599   vlib_error_count (vm, error_node->node_index,
1600                     ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1601                     n_advertisements_sent);
1602
1603   return frame->n_vectors;
1604 }
1605
1606  /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always  be dropped  */
1607 static_always_inline uword
1608 icmp6_router_advertisement (vlib_main_t * vm,
1609                             vlib_node_runtime_t * node, vlib_frame_t * frame)
1610 {
1611   vnet_main_t *vnm = vnet_get_main ();
1612   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1613   uword n_packets = frame->n_vectors;
1614   u32 *from, *to_next;
1615   u32 n_left_from, n_left_to_next, next_index;
1616   u32 n_advertisements_rcvd = 0;
1617
1618   vlib_node_runtime_t *error_node =
1619     vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1620
1621   from = vlib_frame_vector_args (frame);
1622   n_left_from = n_packets;
1623   next_index = node->cached_next_index;
1624
1625   if (node->flags & VLIB_NODE_FLAG_TRACE)
1626     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1627                                    /* stride */ 1,
1628                                    sizeof (icmp6_input_trace_t));
1629
1630   while (n_left_from > 0)
1631     {
1632       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1633
1634       while (n_left_from > 0 && n_left_to_next > 0)
1635         {
1636           vlib_buffer_t *p0;
1637           ip6_header_t *ip0;
1638           ip6_radv_t *radv_info = 0;
1639           icmp6_router_advertisement_header_t *h0;
1640           u32 bi0, options_len0, sw_if_index0, next0, error0;
1641
1642           bi0 = to_next[0] = from[0];
1643
1644           from += 1;
1645           to_next += 1;
1646           n_left_from -= 1;
1647           n_left_to_next -= 1;
1648
1649           p0 = vlib_get_buffer (vm, bi0);
1650           ip0 = vlib_buffer_get_current (p0);
1651           h0 = ip6_next_header (ip0);
1652           options_len0 =
1653             clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1654
1655           error0 = ICMP6_ERROR_NONE;
1656           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1657
1658           /* Check that source address is link-local */
1659           error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1660             ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1661
1662           /* default is to drop */
1663           next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1664
1665           n_advertisements_rcvd++;
1666
1667           if (error0 == ICMP6_ERROR_NONE)
1668             {
1669               vnet_sw_interface_t *sw_if0;
1670               ethernet_interface_t *eth_if0;
1671
1672               sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1673               ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1674               eth_if0 =
1675                 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
1676
1677               /* only support ethernet interface type for now */
1678               error0 =
1679                 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1680                 : error0;
1681
1682               if (error0 == ICMP6_ERROR_NONE)
1683                 {
1684                   u32 ri;
1685
1686                   /* look up the radv_t information for this interface */
1687                   vec_validate_init_empty
1688                     (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
1689
1690                   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1691
1692                   if (ri != ~0)
1693                     radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1694
1695                   error0 =
1696                     ((!radv_info) ?
1697                      ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1698                      error0);
1699
1700                   if (error0 == ICMP6_ERROR_NONE)
1701                     {
1702                       /* validate advertised information */
1703                       if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1704                           && (h0->current_hop_limit !=
1705                               radv_info->curr_hop_limit))
1706                         {
1707                           ip6_neighbor_syslog (vm, LOG_WARNING,
1708                                                "our AdvCurHopLimit on %U doesn't agree with %U",
1709                                                format_vnet_sw_if_index_name,
1710                                                vnm, sw_if_index0,
1711                                                format_ip6_address,
1712                                                &ip0->src_address);
1713                         }
1714
1715                       if ((h0->flags &
1716                            ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1717                           != radv_info->adv_managed_flag)
1718                         {
1719                           ip6_neighbor_syslog (vm, LOG_WARNING,
1720                                                "our AdvManagedFlag on %U doesn't agree with %U",
1721                                                format_vnet_sw_if_index_name,
1722                                                vnm, sw_if_index0,
1723                                                format_ip6_address,
1724                                                &ip0->src_address);
1725                         }
1726
1727                       if ((h0->flags &
1728                            ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1729                           != radv_info->adv_other_flag)
1730                         {
1731                           ip6_neighbor_syslog (vm, LOG_WARNING,
1732                                                "our AdvOtherConfigFlag on %U doesn't agree with %U",
1733                                                format_vnet_sw_if_index_name,
1734                                                vnm, sw_if_index0,
1735                                                format_ip6_address,
1736                                                &ip0->src_address);
1737                         }
1738
1739                       if ((h0->
1740                            time_in_msec_between_retransmitted_neighbor_solicitations
1741                            && radv_info->
1742                            adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1743                           && (h0->
1744                               time_in_msec_between_retransmitted_neighbor_solicitations
1745                               !=
1746                               clib_host_to_net_u32 (radv_info->
1747                                                     adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1748                         {
1749                           ip6_neighbor_syslog (vm, LOG_WARNING,
1750                                                "our AdvRetransTimer on %U doesn't agree with %U",
1751                                                format_vnet_sw_if_index_name,
1752                                                vnm, sw_if_index0,
1753                                                format_ip6_address,
1754                                                &ip0->src_address);
1755                         }
1756
1757                       if ((h0->neighbor_reachable_time_in_msec &&
1758                            radv_info->adv_neighbor_reachable_time_in_msec) &&
1759                           (h0->neighbor_reachable_time_in_msec !=
1760                            clib_host_to_net_u32
1761                            (radv_info->adv_neighbor_reachable_time_in_msec)))
1762                         {
1763                           ip6_neighbor_syslog (vm, LOG_WARNING,
1764                                                "our AdvReachableTime on %U doesn't agree with %U",
1765                                                format_vnet_sw_if_index_name,
1766                                                vnm, sw_if_index0,
1767                                                format_ip6_address,
1768                                                &ip0->src_address);
1769                         }
1770
1771                       /* check for MTU or prefix options or .. */
1772                       u8 *opt_hdr = (u8 *) (h0 + 1);
1773                       while (options_len0 > 0)
1774                         {
1775                           icmp6_neighbor_discovery_option_header_t *o0 =
1776                             (icmp6_neighbor_discovery_option_header_t *)
1777                             opt_hdr;
1778                           int opt_len = o0->n_data_u64s << 3;
1779                           icmp6_neighbor_discovery_option_type_t option_type =
1780                             o0->type;
1781
1782                           if (options_len0 < 2)
1783                             {
1784                               ip6_neighbor_syslog (vm, LOG_ERR,
1785                                                    "malformed RA packet on %U from %U",
1786                                                    format_vnet_sw_if_index_name,
1787                                                    vnm, sw_if_index0,
1788                                                    format_ip6_address,
1789                                                    &ip0->src_address);
1790                               break;
1791                             }
1792
1793                           if (opt_len == 0)
1794                             {
1795                               ip6_neighbor_syslog (vm, LOG_ERR,
1796                                                    " zero length option in RA on %U from %U",
1797                                                    format_vnet_sw_if_index_name,
1798                                                    vnm, sw_if_index0,
1799                                                    format_ip6_address,
1800                                                    &ip0->src_address);
1801                               break;
1802                             }
1803                           else if (opt_len > options_len0)
1804                             {
1805                               ip6_neighbor_syslog (vm, LOG_ERR,
1806                                                    "option length in RA packet  greater than total length on %U from %U",
1807                                                    format_vnet_sw_if_index_name,
1808                                                    vnm, sw_if_index0,
1809                                                    format_ip6_address,
1810                                                    &ip0->src_address);
1811                               break;
1812                             }
1813
1814                           options_len0 -= opt_len;
1815                           opt_hdr += opt_len;
1816
1817                           switch (option_type)
1818                             {
1819                             case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
1820                               {
1821                                 icmp6_neighbor_discovery_mtu_option_t *h =
1822                                   (icmp6_neighbor_discovery_mtu_option_t
1823                                    *) (o0);
1824
1825                                 if (opt_len < sizeof (*h))
1826                                   break;
1827
1828                                 if ((h->mtu && radv_info->adv_link_mtu) &&
1829                                     (h->mtu !=
1830                                      clib_host_to_net_u32
1831                                      (radv_info->adv_link_mtu)))
1832                                   {
1833                                     ip6_neighbor_syslog (vm, LOG_WARNING,
1834                                                          "our AdvLinkMTU on %U doesn't agree with %U",
1835                                                          format_vnet_sw_if_index_name,
1836                                                          vnm, sw_if_index0,
1837                                                          format_ip6_address,
1838                                                          &ip0->src_address);
1839                                   }
1840                               }
1841                               break;
1842
1843                             case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1844                               {
1845                                 icmp6_neighbor_discovery_prefix_information_option_t
1846                                   * h =
1847                                   (icmp6_neighbor_discovery_prefix_information_option_t
1848                                    *) (o0);
1849
1850                                 /* validate advertised prefix options  */
1851                                 ip6_radv_prefix_t *pr_info;
1852                                 u32 preferred, valid;
1853
1854                                 if (opt_len < sizeof (*h))
1855                                   break;
1856
1857                                 preferred =
1858                                   clib_net_to_host_u32 (h->preferred_time);
1859                                 valid = clib_net_to_host_u32 (h->valid_time);
1860
1861                                 /* look for matching prefix - if we our advertising it, it better be consistant */
1862                                 /* *INDENT-OFF* */
1863                                 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1864                                 ({
1865
1866                                   ip6_address_t mask;
1867                                   ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1868
1869                                   if(pr_info->enabled &&
1870                                      (pr_info->prefix_len == h->dst_address_length) &&
1871                                      ip6_address_is_equal_masked (&pr_info->prefix,  &h->dst_address, &mask))
1872                                     {
1873                                       /* found it */
1874                                       if(!pr_info->decrement_lifetime_flag &&
1875                                          valid != pr_info->adv_valid_lifetime_in_secs)
1876                                         {
1877                                           ip6_neighbor_syslog(vm,  LOG_WARNING,
1878                                                               "our ADV validlifetime on  %U for %U does not  agree with %U",
1879                                                               format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1880                                                               format_ip6_address, &h->dst_address);
1881                                         }
1882                                       if(!pr_info->decrement_lifetime_flag &&
1883                                          preferred != pr_info->adv_pref_lifetime_in_secs)
1884                                         {
1885                                           ip6_neighbor_syslog(vm,  LOG_WARNING,
1886                                                               "our ADV preferredlifetime on  %U for %U does not  agree with %U",
1887                                                               format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1888                                                               format_ip6_address, &h->dst_address);
1889                                         }
1890                                     }
1891                                   break;
1892                                 }));
1893                                 /* *INDENT-ON* */
1894                                 break;
1895                               }
1896                             default:
1897                               /* skip this one */
1898                               break;
1899                             }
1900                         }
1901                     }
1902                 }
1903             }
1904
1905           p0->error = error_node->errors[error0];
1906
1907           if (error0 != ICMP6_ERROR_NONE)
1908             vlib_error_count (vm, error_node->node_index, error0, 1);
1909
1910           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1911                                            to_next, n_left_to_next,
1912                                            bi0, next0);
1913         }
1914
1915       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1916     }
1917
1918   /* Account for router advertisements sent. */
1919   vlib_error_count (vm, error_node->node_index,
1920                     ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
1921                     n_advertisements_rcvd);
1922
1923   return frame->n_vectors;
1924 }
1925
1926 /* create and initialize router advertisement parameters with default values for this intfc */
1927 static u32
1928 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
1929                                    u32 sw_if_index, u32 is_add)
1930 {
1931   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1932   ip6_radv_t *a = 0;
1933   u32 ri = ~0;
1934   vnet_sw_interface_t *sw_if0;
1935   ethernet_interface_t *eth_if0 = 0;
1936
1937   /* lookup radv container  - ethernet interfaces only */
1938   sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
1939   if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1940     eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
1941
1942   if (!eth_if0)
1943     return ri;
1944
1945   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
1946                            ~0);
1947   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
1948
1949   if (ri != ~0)
1950     {
1951       a = pool_elt_at_index (nm->if_radv_pool, ri);
1952
1953       if (!is_add)
1954         {
1955           u32 i, *to_delete = 0;
1956           ip6_radv_prefix_t *p;
1957           ip6_mldp_group_t *m;
1958
1959           /* release the lock on the interface's mcast adj */
1960           adj_unlock (a->mcast_adj_index);
1961
1962           /* clean up prefix_pool */
1963           /* *INDENT-OFF* */
1964           pool_foreach (p, a->adv_prefixes_pool,
1965           ({
1966             vec_add1 (to_delete, p  -  a->adv_prefixes_pool);
1967           }));
1968           /* *INDENT-ON* */
1969
1970           for (i = 0; i < vec_len (to_delete); i++)
1971             {
1972               p = pool_elt_at_index (a->adv_prefixes_pool, to_delete[i]);
1973               mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
1974               pool_put (a->adv_prefixes_pool, p);
1975             }
1976
1977           vec_free (to_delete);
1978           to_delete = 0;
1979
1980           /* clean up mldp group pool */
1981           /* *INDENT-OFF* */
1982           pool_foreach (m, a->mldp_group_pool,
1983           ({
1984             vec_add1 (to_delete, m  -  a->mldp_group_pool);
1985           }));
1986           /* *INDENT-ON* */
1987
1988           for (i = 0; i < vec_len (to_delete); i++)
1989             {
1990               m = pool_elt_at_index (a->mldp_group_pool, to_delete[i]);
1991               mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
1992               pool_put (a->mldp_group_pool, m);
1993             }
1994
1995           vec_free (to_delete);
1996
1997           pool_put (nm->if_radv_pool, a);
1998           nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
1999           ri = ~0;
2000         }
2001     }
2002   else
2003     {
2004       if (is_add)
2005         {
2006           vnet_hw_interface_t *hw_if0;
2007
2008           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2009
2010           pool_get (nm->if_radv_pool, a);
2011
2012           ri = a - nm->if_radv_pool;
2013           nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2014
2015           /* initialize default values (most of which are zero) */
2016           memset (a, 0, sizeof (a[0]));
2017
2018           a->sw_if_index = sw_if_index;
2019           a->fib_index = ~0;
2020           a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2021           a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2022           a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2023           a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2024
2025           a->adv_link_layer_address = 1;        /* send ll address source address option */
2026
2027           a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2028           a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2029           a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2030           a->seed = (u32) clib_cpu_time_now ();
2031           (void) random_u32 (&a->seed);
2032           a->randomizer = clib_cpu_time_now ();
2033           (void) random_u64 (&a->randomizer);
2034
2035           a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2036           a->initial_adverts_sent = a->initial_adverts_count - 1;
2037           a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2038
2039           /* deafult is to send */
2040           a->send_radv = 1;
2041
2042           /* fill in radv_info for this interface that will be needed later */
2043           a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2044
2045           clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2046
2047           /* fill in default link-local address  (this may be overridden) */
2048           ip6_link_local_address_from_ethernet_address
2049             (&a->link_local_address, eth_if0->address);
2050
2051           mhash_init (&a->address_to_prefix_index, sizeof (uword),
2052                       sizeof (ip6_address_t));
2053           mhash_init (&a->address_to_mldp_index, sizeof (uword),
2054                       sizeof (ip6_address_t));
2055
2056           a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2057                                                       VNET_LINK_IP6,
2058                                                       sw_if_index);
2059
2060           /* add multicast groups we will always be reporting  */
2061           ip6_address_t addr;
2062           ip6_mldp_group_t *mcast_group_info;
2063
2064           ip6_set_reserved_multicast_address (&addr,
2065                                               IP6_MULTICAST_SCOPE_link_local,
2066                                               IP6_MULTICAST_GROUP_ID_all_hosts);
2067
2068           /* lookup  mldp info for this interface */
2069
2070           uword *p = mhash_get (&a->address_to_mldp_index, &addr);
2071           mcast_group_info =
2072             p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
2073
2074           /* add address */
2075           if (!mcast_group_info)
2076             {
2077               /* add */
2078               u32 mi;
2079               pool_get (a->mldp_group_pool, mcast_group_info);
2080
2081               mi = mcast_group_info - a->mldp_group_pool;
2082               mhash_set (&a->address_to_mldp_index, &addr, mi,  /* old_value */
2083                          0);
2084
2085               mcast_group_info->type = 4;
2086               mcast_group_info->mcast_source_address_pool = 0;
2087               mcast_group_info->num_sources = 0;
2088               clib_memcpy (&mcast_group_info->mcast_address, &addr,
2089                            sizeof (ip6_address_t));
2090             }
2091
2092           ip6_set_reserved_multicast_address (&addr,
2093                                               IP6_MULTICAST_SCOPE_link_local,
2094                                               IP6_MULTICAST_GROUP_ID_all_routers);
2095
2096           p = mhash_get (&a->address_to_mldp_index, &addr);
2097           mcast_group_info =
2098             p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
2099
2100           if (!mcast_group_info)
2101             {
2102               /* add */
2103               u32 mi;
2104               pool_get (a->mldp_group_pool, mcast_group_info);
2105
2106               mi = mcast_group_info - a->mldp_group_pool;
2107               mhash_set (&a->address_to_mldp_index, &addr, mi,  /* old_value */
2108                          0);
2109
2110               mcast_group_info->type = 4;
2111               mcast_group_info->mcast_source_address_pool = 0;
2112               mcast_group_info->num_sources = 0;
2113               clib_memcpy (&mcast_group_info->mcast_address, &addr,
2114                            sizeof (ip6_address_t));
2115             }
2116
2117           ip6_set_reserved_multicast_address (&addr,
2118                                               IP6_MULTICAST_SCOPE_link_local,
2119                                               IP6_MULTICAST_GROUP_ID_mldv2_routers);
2120
2121           p = mhash_get (&a->address_to_mldp_index, &addr);
2122           mcast_group_info =
2123             p ? pool_elt_at_index (a->mldp_group_pool, p[0]) : 0;
2124
2125           if (!mcast_group_info)
2126             {
2127               /* add */
2128               u32 mi;
2129               pool_get (a->mldp_group_pool, mcast_group_info);
2130
2131               mi = mcast_group_info - a->mldp_group_pool;
2132               mhash_set (&a->address_to_mldp_index, &addr, mi,  /* old_value */
2133                          0);
2134
2135               mcast_group_info->type = 4;
2136               mcast_group_info->mcast_source_address_pool = 0;
2137               mcast_group_info->num_sources = 0;
2138               clib_memcpy (&mcast_group_info->mcast_address, &addr,
2139                            sizeof (ip6_address_t));
2140             }
2141         }
2142     }
2143   return ri;
2144 }
2145
2146 /* send an mldpv2 report  */
2147 static void
2148 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2149 {
2150   vnet_main_t *vnm = vnet_get_main ();
2151   vlib_main_t *vm = vnm->vlib_main;
2152   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2153   vnet_sw_interface_t *sw_if0;
2154   ethernet_interface_t *eth_if0;
2155   u32 ri;
2156   int bogus_length;
2157
2158   ip6_radv_t *radv_info;
2159   u16 payload_length;
2160   vlib_buffer_t *b0;
2161   ip6_header_t *ip0;
2162   u32 *to_next;
2163   vlib_frame_t *f;
2164   u32 bo0;
2165   u32 n_to_alloc = 1;
2166   u32 n_allocated;
2167
2168   icmp6_multicast_listener_report_header_t *rh0;
2169   icmp6_multicast_listener_report_packet_t *rp0;
2170
2171   sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2172   ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2173   eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2174
2175   if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2176     return;
2177
2178   /* look up the radv_t  information for this interface */
2179   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2180                            ~0);
2181
2182   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2183
2184   if (ri == ~0)
2185     return;
2186
2187   /* send report now - build a mldpv2 report packet  */
2188   n_allocated = vlib_buffer_alloc_from_free_list (vm,
2189                                                   &bo0,
2190                                                   n_to_alloc,
2191                                                   VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2192   if (PREDICT_FALSE (n_allocated == 0))
2193     {
2194       clib_warning ("buffer allocation failure");
2195       return;
2196     }
2197
2198   b0 = vlib_get_buffer (vm, bo0);
2199
2200   /* adjust the sizeof the buffer to just include the ipv6 header */
2201   b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2202
2203   payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2204
2205   b0->error = ICMP6_ERROR_NONE;
2206
2207   rp0 = vlib_buffer_get_current (b0);
2208   ip0 = (ip6_header_t *) & rp0->ip;
2209   rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2210
2211   memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2212
2213   ip0->ip_version_traffic_class_and_flow_label =
2214     clib_host_to_net_u32 (0x6 << 28);
2215
2216   ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2217   /* for DEBUG - vnet driver won't seem to emit router alerts */
2218   /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2219   ip0->hop_limit = 1;
2220
2221   rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2222
2223   /* source address MUST be the link-local address */
2224   radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2225   ip0->src_address = radv_info->link_local_address;
2226
2227   /* destination is all mldpv2 routers */
2228   ip6_set_reserved_multicast_address (&ip0->dst_address,
2229                                       IP6_MULTICAST_SCOPE_link_local,
2230                                       IP6_MULTICAST_GROUP_ID_mldv2_routers);
2231
2232   /* add reports here */
2233   ip6_mldp_group_t *m;
2234   int num_addr_records = 0;
2235   icmp6_multicast_address_record_t rr;
2236
2237   /* fill in the hop-by-hop extension header (router alert) info */
2238   rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2239   rh0->ext_hdr.n_data_u64s = 0;
2240
2241   rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2242   rh0->alert.len = 2;
2243   rh0->alert.value = 0;
2244
2245   rh0->pad.type = 1;
2246   rh0->pad.len = 0;
2247
2248   rh0->icmp.checksum = 0;
2249
2250   /* *INDENT-OFF* */
2251   pool_foreach (m, radv_info->mldp_group_pool,
2252   ({
2253     rr.type = m->type;
2254     rr.aux_data_len_u32s = 0;
2255     rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2256     clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2257
2258     num_addr_records++;
2259
2260     vlib_buffer_add_data
2261       (vm, b0->free_list_index, bo0,
2262        (void *)&rr, sizeof(icmp6_multicast_address_record_t));
2263
2264     payload_length += sizeof( icmp6_multicast_address_record_t);
2265   }));
2266   /* *INDENT-ON* */
2267
2268   rh0->rsvd = 0;
2269   rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2270
2271   /* update lengths */
2272   ip0->payload_length = clib_host_to_net_u16 (payload_length);
2273
2274   rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2275                                                           &bogus_length);
2276   ASSERT (bogus_length == 0);
2277
2278   /*
2279    * OK to override w/ no regard for actual FIB, because
2280    * ip6-rewrite only looks at the adjacency.
2281    */
2282   vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2283     vnet_main.local_interface_sw_if_index;
2284
2285   vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2286   b0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
2287
2288   vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2289
2290   f = vlib_get_frame_to_node (vm, node->index);
2291   to_next = vlib_frame_vector_args (f);
2292   to_next[0] = bo0;
2293   f->n_vectors = 1;
2294
2295   vlib_put_frame_to_node (vm, node->index, f);
2296   return;
2297 }
2298
2299 /* *INDENT-OFF* */
2300 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2301 {
2302   .function = icmp6_router_solicitation,
2303   .name = "icmp6-router-solicitation",
2304
2305   .vector_size = sizeof (u32),
2306
2307   .format_trace = format_icmp6_input_trace,
2308
2309   .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2310   .next_nodes = {
2311     [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
2312     [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2313     [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2314   },
2315 };
2316 /* *INDENT-ON* */
2317
2318 /* send a RA or update the timer info etc.. */
2319 static uword
2320 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2321                                   vlib_node_runtime_t * node,
2322                                   vlib_frame_t * frame)
2323 {
2324   vnet_main_t *vnm = vnet_get_main ();
2325   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2326   ip6_radv_t *radv_info;
2327   vlib_frame_t *f = 0;
2328   u32 n_this_frame = 0;
2329   u32 n_left_to_next = 0;
2330   u32 *to_next = 0;
2331   u32 bo0;
2332   icmp6_router_solicitation_header_t *h0;
2333   vlib_buffer_t *b0;
2334   f64 now = vlib_time_now (vm);
2335
2336   /* Interface ip6 radv info list */
2337   /* *INDENT-OFF* */
2338   pool_foreach (radv_info, nm->if_radv_pool,
2339   ({
2340     if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2341       {
2342         radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2343         radv_info->next_multicast_time = now;
2344         radv_info->last_multicast_time = now;
2345         radv_info->last_radv_time = 0;
2346         radv_info->all_routers_mcast = 0;
2347         continue;
2348       }
2349
2350     /* Make sure that we've joined the all-routers multicast group */
2351     if(!radv_info->all_routers_mcast)
2352       {
2353         /* send MDLP_REPORT_EVENT message */
2354         ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2355         radv_info->all_routers_mcast = 1;
2356       }
2357
2358     /* is it time to send a multicast  RA on this interface? */
2359     if(radv_info->send_radv && (now >=  radv_info->next_multicast_time))
2360       {
2361         u32 n_to_alloc = 1;
2362         u32 n_allocated;
2363
2364         f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2365           random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2366
2367         /* multicast send - compute next multicast send time */
2368         if( radv_info->initial_adverts_sent > 0)
2369           {
2370             radv_info->initial_adverts_sent--;
2371             if(rfn > radv_info-> initial_adverts_interval)
2372               rfn =  radv_info-> initial_adverts_interval;
2373
2374             /* check to see if we are ceasing to send */
2375             if( radv_info->initial_adverts_sent  == 0)
2376               if(radv_info->cease_radv)
2377                 radv_info->send_radv = 0;
2378           }
2379
2380         radv_info->next_multicast_time =  rfn + now;
2381         radv_info->last_multicast_time = now;
2382
2383         /* send advert now - build a "solicted" router advert with unspecified source address */
2384         n_allocated = vlib_buffer_alloc_from_free_list
2385           (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2386
2387         if (PREDICT_FALSE(n_allocated == 0))
2388           {
2389             clib_warning ("buffer allocation failure");
2390             continue;
2391           }
2392         b0 = vlib_get_buffer (vm, bo0);
2393         b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2394         b0->error = ICMP6_ERROR_NONE;
2395         vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2396
2397         h0 =  vlib_buffer_get_current (b0);
2398
2399         memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2400
2401         h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2402         h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2403                                                       - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2404         h0->ip.protocol = IP_PROTOCOL_ICMP6;
2405         h0->ip.hop_limit = 255;
2406
2407         /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2408         h0->ip.src_address.as_u64[0] = 0;
2409         h0->ip.src_address.as_u64[1] = 0;
2410
2411         h0->ip.dst_address.as_u64[0] = 0;
2412         h0->ip.dst_address.as_u64[1] = 0;
2413
2414         h0->neighbor.icmp.type = ICMP6_router_solicitation;
2415
2416         if (PREDICT_FALSE(f == 0))
2417           {
2418             f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2419             to_next = vlib_frame_vector_args (f);
2420             n_left_to_next = VLIB_FRAME_SIZE;
2421             n_this_frame = 0;
2422           }
2423
2424         n_this_frame++;
2425         n_left_to_next--;
2426         to_next[0] = bo0;
2427         to_next += 1;
2428
2429         if (PREDICT_FALSE(n_left_to_next == 0))
2430           {
2431             f->n_vectors = n_this_frame;
2432             vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2433             f = 0;
2434           }
2435       }
2436   }));
2437   /* *INDENT-ON* */
2438
2439   if (f)
2440     {
2441       ASSERT (n_this_frame);
2442       f->n_vectors = n_this_frame;
2443       vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2444     }
2445   return 0;
2446 }
2447
2448 static uword
2449 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2450                                            vlib_node_runtime_t * node,
2451                                            vlib_frame_t * frame)
2452 {
2453   uword event_type;
2454   ip6_icmp_neighbor_discovery_event_data_t *event_data;
2455
2456   /* init code here */
2457
2458   while (1)
2459     {
2460       vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
2461
2462       event_data = vlib_process_get_event_data (vm, &event_type);
2463
2464       if (!event_data)
2465         {
2466           /* No events found: timer expired. */
2467           /* process interface list and send RAs as appropriate, update timer info */
2468           ip6_neighbor_process_timer_event (vm, node, frame);
2469         }
2470       else
2471         {
2472           switch (event_type)
2473             {
2474
2475             case ICMP6_ND_EVENT_INIT:
2476               break;
2477
2478             case ~0:
2479               break;
2480
2481             default:
2482               ASSERT (0);
2483             }
2484
2485           if (event_data)
2486             _vec_len (event_data) = 0;
2487         }
2488     }
2489   return frame->n_vectors;
2490 }
2491
2492 /* *INDENT-OFF* */
2493 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
2494 {
2495   .function = icmp6_router_advertisement,
2496   .name = "icmp6-router-advertisement",
2497
2498   .vector_size = sizeof (u32),
2499
2500   .format_trace = format_icmp6_input_trace,
2501
2502   .n_next_nodes = 1,
2503   .next_nodes = {
2504     [0] = "error-drop",
2505   },
2506 };
2507 /* *INDENT-ON* */
2508
2509 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2510
2511   .function = ip6_icmp_neighbor_discovery_event_process,
2512   .name = "ip6-icmp-neighbor-discovery-event-process",
2513   .type = VLIB_NODE_TYPE_PROCESS,
2514 };
2515
2516 static uword
2517 icmp6_neighbor_solicitation (vlib_main_t * vm,
2518                              vlib_node_runtime_t * node, vlib_frame_t * frame)
2519 {
2520   return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2521                                                        /* is_solicitation */
2522                                                        1);
2523 }
2524
2525 static uword
2526 icmp6_neighbor_advertisement (vlib_main_t * vm,
2527                               vlib_node_runtime_t * node,
2528                               vlib_frame_t * frame)
2529 {
2530   return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2531                                                        /* is_solicitation */
2532                                                        0);
2533 }
2534
2535 /* *INDENT-OFF* */
2536 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
2537 {
2538   .function = icmp6_neighbor_solicitation,
2539   .name = "icmp6-neighbor-solicitation",
2540
2541   .vector_size = sizeof (u32),
2542
2543   .format_trace = format_icmp6_input_trace,
2544
2545   .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2546   .next_nodes = {
2547     [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2548     [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2549   },
2550 };
2551 /* *INDENT-ON* */
2552
2553 /* *INDENT-OFF* */
2554 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
2555 {
2556   .function = icmp6_neighbor_advertisement,
2557   .name = "icmp6-neighbor-advertisement",
2558
2559   .vector_size = sizeof (u32),
2560
2561   .format_trace = format_icmp6_input_trace,
2562
2563   .n_next_nodes = 1,
2564   .next_nodes = {
2565     [0] = "error-drop",
2566   },
2567 };
2568 /* *INDENT-ON* */
2569
2570 /* API support functions */
2571 int
2572 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
2573                         u8 suppress, u8 managed, u8 other,
2574                         u8 ll_option, u8 send_unicast, u8 cease,
2575                         u8 use_lifetime, u32 lifetime,
2576                         u32 initial_count, u32 initial_interval,
2577                         u32 max_interval, u32 min_interval, u8 is_no)
2578 {
2579   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2580   int error;
2581   u32 ri;
2582
2583   /* look up the radv_t  information for this interface */
2584   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2585                            ~0);
2586   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2587   error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2588
2589   if (!error)
2590     {
2591
2592       ip6_radv_t *radv_info;
2593       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2594
2595       if ((max_interval != 0) && (min_interval == 0))
2596         min_interval = .75 * max_interval;
2597
2598       max_interval =
2599         (max_interval !=
2600          0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
2601         radv_info->max_radv_interval;
2602       min_interval =
2603         (min_interval !=
2604          0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
2605         radv_info->min_radv_interval;
2606       lifetime =
2607         (use_lifetime !=
2608          0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
2609         radv_info->adv_router_lifetime_in_sec;
2610
2611       if (lifetime)
2612         {
2613           if (lifetime > MAX_DEF_RTR_LIFETIME)
2614             lifetime = MAX_DEF_RTR_LIFETIME;
2615
2616           if (lifetime <= max_interval)
2617             return VNET_API_ERROR_INVALID_VALUE;
2618         }
2619
2620       if (min_interval != 0)
2621         {
2622           if ((min_interval > .75 * max_interval) || (min_interval < 3))
2623             return VNET_API_ERROR_INVALID_VALUE;
2624         }
2625
2626       if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2627           (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2628         return VNET_API_ERROR_INVALID_VALUE;
2629
2630       /*
2631          if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2632          if "flag" is clear  don't change corresponding value
2633        */
2634       radv_info->send_radv =
2635         (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
2636       radv_info->adv_managed_flag =
2637         (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2638       radv_info->adv_other_flag =
2639         (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
2640       radv_info->adv_link_layer_address =
2641         (ll_option !=
2642          0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2643       radv_info->send_unicast =
2644         (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
2645       radv_info->cease_radv =
2646         (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
2647
2648       radv_info->min_radv_interval = min_interval;
2649       radv_info->max_radv_interval = max_interval;
2650       radv_info->adv_router_lifetime_in_sec = lifetime;
2651
2652       radv_info->initial_adverts_count =
2653         (initial_count !=
2654          0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
2655         radv_info->initial_adverts_count;
2656       radv_info->initial_adverts_interval =
2657         (initial_interval !=
2658          0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
2659         radv_info->initial_adverts_interval;
2660
2661       /* restart */
2662       if ((cease != 0) && (is_no))
2663         radv_info->send_radv = 1;
2664
2665       radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2666       radv_info->next_multicast_time = vlib_time_now (vm);
2667       radv_info->last_multicast_time = vlib_time_now (vm);
2668       radv_info->last_radv_time = 0;
2669     }
2670   return (error);
2671 }
2672
2673 int
2674 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
2675                         ip6_address_t * prefix_addr, u8 prefix_len,
2676                         u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2677                         u8 no_advertise, u8 off_link, u8 no_autoconfig,
2678                         u8 no_onlink, u8 is_no)
2679 {
2680   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2681   int error;
2682
2683   u32 ri;
2684
2685   /* look up the radv_t  information for this interface */
2686   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2687                            ~0);
2688
2689   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2690
2691   error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
2692
2693   if (!error)
2694     {
2695       f64 now = vlib_time_now (vm);
2696       ip6_radv_t *radv_info;
2697       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2698
2699       /* prefix info add, delete or update */
2700       ip6_radv_prefix_t *prefix;
2701
2702       /* lookup  prefix info for this  address on this interface */
2703       uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2704
2705       prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2706
2707       if (is_no)
2708         {
2709           /* delete */
2710           if (!prefix)
2711             return VNET_API_ERROR_INVALID_VALUE;        /* invalid prefix */
2712
2713           if (prefix->prefix_len != prefix_len)
2714             return VNET_API_ERROR_INVALID_VALUE_2;
2715
2716           /* FIXME - Should the DP do this or the CP ? */
2717           /* do specific delete processing here before returning */
2718           /* try to remove from routing table */
2719
2720           mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
2721                        /* old_value */ 0);
2722           pool_put (radv_info->adv_prefixes_pool, prefix);
2723
2724           radv_info->initial_adverts_sent =
2725             radv_info->initial_adverts_count - 1;
2726           radv_info->next_multicast_time = vlib_time_now (vm);
2727           radv_info->last_multicast_time = vlib_time_now (vm);
2728           radv_info->last_radv_time = 0;
2729           return (error);
2730         }
2731
2732       /* adding or changing */
2733       if (!prefix)
2734         {
2735           /* add */
2736           u32 pi;
2737           pool_get (radv_info->adv_prefixes_pool, prefix);
2738           pi = prefix - radv_info->adv_prefixes_pool;
2739           mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
2740                      /* old_value */ 0);
2741
2742           memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
2743
2744           prefix->prefix_len = prefix_len;
2745           clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
2746
2747           /* initialize default values */
2748           prefix->adv_on_link_flag = 1; /* L bit set */
2749           prefix->adv_autonomous_flag = 1;      /* A bit set */
2750           prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
2751           prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2752           prefix->enabled = 1;
2753           prefix->decrement_lifetime_flag = 1;
2754           prefix->deprecated_prefix_flag = 1;
2755
2756           if (off_link == 0)
2757             {
2758               /* FIXME - Should the DP do this or the CP ? */
2759               /* insert prefix into routing table as a connected prefix */
2760             }
2761
2762           if (use_default)
2763             goto restart;
2764         }
2765       else
2766         {
2767
2768           if (prefix->prefix_len != prefix_len)
2769             return VNET_API_ERROR_INVALID_VALUE_2;
2770
2771           if (off_link != 0)
2772             {
2773               /* FIXME - Should the DP do this or the CP ? */
2774               /* remove from routing table if already there */
2775             }
2776         }
2777
2778       if ((val_lifetime == ~0) || (pref_lifetime == ~0))
2779         {
2780           prefix->adv_valid_lifetime_in_secs = ~0;
2781           prefix->adv_pref_lifetime_in_secs = ~0;
2782           prefix->decrement_lifetime_flag = 0;
2783         }
2784       else
2785         {
2786           prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2787           prefix->adv_pref_lifetime_in_secs = pref_lifetime;
2788         }
2789
2790       /* copy  remaining */
2791       prefix->enabled = !(no_advertise != 0);
2792       prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2793       prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2794
2795     restart:
2796       /* restart */
2797       /* fill in the expiration times  */
2798       prefix->valid_lifetime_expires =
2799         now + prefix->adv_valid_lifetime_in_secs;
2800       prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
2801
2802       radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2803       radv_info->next_multicast_time = vlib_time_now (vm);
2804       radv_info->last_multicast_time = vlib_time_now (vm);
2805       radv_info->last_radv_time = 0;
2806     }
2807   return (error);
2808 }
2809
2810 clib_error_t *
2811 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
2812                   vlib_cli_command_t * cmd)
2813 {
2814   vnet_main_t *vnm = vnet_get_main ();
2815   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2816   clib_error_t *error = 0;
2817   u8 is_no = 0;
2818   u8 suppress = 0, managed = 0, other = 0;
2819   u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
2820   u8 use_lifetime = 0;
2821   u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
2822     0, ra_initial_interval = 0;
2823   u32 ra_max_interval = 0, ra_min_interval = 0;
2824
2825   unformat_input_t _line_input, *line_input = &_line_input;
2826   vnet_sw_interface_t *sw_if0;
2827
2828   int add_radv_info = 1;
2829   __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
2830   ip6_address_t ip6_addr;
2831   u32 addr_len;
2832
2833
2834   /* Get a line of input. */
2835   if (!unformat_user (main_input, unformat_line_input, line_input))
2836     return 0;
2837
2838   /* get basic radv info for this interface */
2839   if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2840     {
2841
2842       if (unformat_user (line_input,
2843                          unformat_vnet_sw_interface, vnm, &sw_if_index))
2844         {
2845           u32 ri;
2846           ethernet_interface_t *eth_if0 = 0;
2847
2848           sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2849           if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2850             eth_if0 =
2851               ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2852
2853           if (!eth_if0)
2854             {
2855               error =
2856                 clib_error_return (0, "Interface must be of ethernet type");
2857               goto done;
2858             }
2859
2860           /* look up the radv_t  information for this interface */
2861           vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
2862                                    sw_if_index, ~0);
2863
2864           ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2865
2866           if (ri != ~0)
2867             {
2868               radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2869             }
2870           else
2871             {
2872               error = clib_error_return (0, "unknown interface %U'",
2873                                          format_unformat_error, line_input);
2874               goto done;
2875             }
2876         }
2877       else
2878         {
2879           error = clib_error_return (0, "invalid interface name %U'",
2880                                      format_unformat_error, line_input);
2881           goto done;
2882         }
2883     }
2884
2885   /* get the rest of the command */
2886   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2887     {
2888       if (unformat (line_input, "no"))
2889         is_no = 1;
2890       else if (unformat (line_input, "prefix %U/%d",
2891                          unformat_ip6_address, &ip6_addr, &addr_len))
2892         {
2893           add_radv_info = 0;
2894           break;
2895         }
2896       else if (unformat (line_input, "ra-managed-config-flag"))
2897         {
2898           managed = 1;
2899           break;
2900         }
2901       else if (unformat (line_input, "ra-other-config-flag"))
2902         {
2903           other = 1;
2904           break;
2905         }
2906       else if (unformat (line_input, "ra-suppress") ||
2907                unformat (line_input, "ra-surpress"))
2908         {
2909           suppress = 1;
2910           break;
2911         }
2912       else if (unformat (line_input, "ra-suppress-link-layer") ||
2913                unformat (line_input, "ra-surpress-link-layer"))
2914         {
2915           suppress_ll_option = 1;
2916           break;
2917         }
2918       else if (unformat (line_input, "ra-send-unicast"))
2919         {
2920           send_unicast = 1;
2921           break;
2922         }
2923       else if (unformat (line_input, "ra-lifetime"))
2924         {
2925           if (!unformat (line_input, "%d", &ra_lifetime))
2926             {
2927               error = unformat_parse_error (line_input);
2928               goto done;
2929             }
2930           use_lifetime = 1;
2931           break;
2932         }
2933       else if (unformat (line_input, "ra-initial"))
2934         {
2935           if (!unformat
2936               (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2937             {
2938               error = unformat_parse_error (line_input);
2939               goto done;
2940             }
2941           break;
2942         }
2943       else if (unformat (line_input, "ra-interval"))
2944         {
2945           if (!unformat (line_input, "%d", &ra_max_interval))
2946             {
2947               error = unformat_parse_error (line_input);
2948               goto done;
2949             }
2950
2951           if (!unformat (line_input, "%d", &ra_min_interval))
2952             ra_min_interval = 0;
2953           break;
2954         }
2955       else if (unformat (line_input, "ra-cease"))
2956         {
2957           cease = 1;
2958           break;
2959         }
2960       else
2961         {
2962           error = unformat_parse_error (line_input);
2963           goto done;
2964         }
2965     }
2966
2967   if (add_radv_info)
2968     {
2969       ip6_neighbor_ra_config (vm, sw_if_index,
2970                               suppress, managed, other,
2971                               suppress_ll_option, send_unicast, cease,
2972                               use_lifetime, ra_lifetime,
2973                               ra_initial_count, ra_initial_interval,
2974                               ra_max_interval, ra_min_interval, is_no);
2975     }
2976   else
2977     {
2978       u32 valid_lifetime_in_secs = 0;
2979       u32 pref_lifetime_in_secs = 0;
2980       u8 use_prefix_default_values = 0;
2981       u8 no_advertise = 0;
2982       u8 off_link = 0;
2983       u8 no_autoconfig = 0;
2984       u8 no_onlink = 0;
2985
2986       /* get the rest of the command */
2987       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2988         {
2989           if (unformat (line_input, "default"))
2990             {
2991               use_prefix_default_values = 1;
2992               break;
2993             }
2994           else if (unformat (line_input, "infinite"))
2995             {
2996               valid_lifetime_in_secs = ~0;
2997               pref_lifetime_in_secs = ~0;
2998               break;
2999             }
3000           else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3001                              &pref_lifetime_in_secs))
3002             break;
3003           else
3004             break;
3005         }
3006
3007
3008       /* get the rest of the command */
3009       while (!use_prefix_default_values &&
3010              unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3011         {
3012           if (unformat (line_input, "no-advertise"))
3013             no_advertise = 1;
3014           else if (unformat (line_input, "off-link"))
3015             off_link = 1;
3016           else if (unformat (line_input, "no-autoconfig"))
3017             no_autoconfig = 1;
3018           else if (unformat (line_input, "no-onlink"))
3019             no_onlink = 1;
3020           else
3021             {
3022               error = unformat_parse_error (line_input);
3023               goto done;
3024             }
3025         }
3026
3027       ip6_neighbor_ra_prefix (vm, sw_if_index,
3028                               &ip6_addr, addr_len,
3029                               use_prefix_default_values,
3030                               valid_lifetime_in_secs,
3031                               pref_lifetime_in_secs,
3032                               no_advertise,
3033                               off_link, no_autoconfig, no_onlink, is_no);
3034     }
3035
3036 done:
3037   unformat_free (line_input);
3038
3039   return error;
3040 }
3041
3042 static void
3043 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3044 {
3045   ip_lookup_main_t *lm = &ip6_main.lookup_main;
3046   u32 i;
3047
3048   for (i = 0; i < vec_len (addrs); i++)
3049     {
3050       ip_interface_address_t *a =
3051         pool_elt_at_index (lm->if_address_pool, addrs[i]);
3052       ip6_address_t *address = ip_interface_address_get_address (lm, a);
3053
3054       vlib_cli_output (vm, "\t\t%U/%d",
3055                        format_ip6_address, address, a->address_length);
3056     }
3057 }
3058
3059 static clib_error_t *
3060 show_ip6_interface_cmd (vlib_main_t * vm,
3061                         unformat_input_t * input, vlib_cli_command_t * cmd)
3062 {
3063   vnet_main_t *vnm = vnet_get_main ();
3064   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3065   clib_error_t *error = 0;
3066   u32 sw_if_index;
3067
3068   sw_if_index = ~0;
3069
3070   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3071     {
3072       u32 ri;
3073
3074       /* look up the radv_t  information for this interface */
3075       vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3076                                sw_if_index, ~0);
3077
3078       ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3079
3080       if (ri != ~0)
3081         {
3082           ip_lookup_main_t *lm = &ip6_main.lookup_main;
3083           ip6_radv_t *radv_info;
3084           radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3085
3086           vlib_cli_output (vm, "%U is admin %s\n",
3087                            format_vnet_sw_interface_name, vnm,
3088                            vnet_get_sw_interface (vnm, sw_if_index),
3089                            (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3090                             "up" : "down"));
3091
3092           u32 ai;
3093           u32 *link_scope = 0, *global_scope = 0;
3094           u32 *local_scope = 0, *unknown_scope = 0;
3095           ip_interface_address_t *a;
3096
3097           vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3098                                    sw_if_index, ~0);
3099           ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3100
3101           while (ai != (u32) ~ 0)
3102             {
3103               a = pool_elt_at_index (lm->if_address_pool, ai);
3104               ip6_address_t *address =
3105                 ip_interface_address_get_address (lm, a);
3106
3107               if (ip6_address_is_link_local_unicast (address))
3108                 vec_add1 (link_scope, ai);
3109               else if (ip6_address_is_global_unicast (address))
3110                 vec_add1 (global_scope, ai);
3111               else if (ip6_address_is_local_unicast (address))
3112                 vec_add1 (local_scope, ai);
3113               else
3114                 vec_add1 (unknown_scope, ai);
3115
3116               ai = a->next_this_sw_interface;
3117             }
3118
3119           if (vec_len (link_scope))
3120             {
3121               vlib_cli_output (vm, "\tLink-local address(es):\n");
3122               ip6_print_addrs (vm, link_scope);
3123               vec_free (link_scope);
3124             }
3125
3126           if (vec_len (local_scope))
3127             {
3128               vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3129               ip6_print_addrs (vm, local_scope);
3130               vec_free (local_scope);
3131             }
3132
3133           if (vec_len (global_scope))
3134             {
3135               vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3136               ip6_print_addrs (vm, global_scope);
3137               vec_free (global_scope);
3138             }
3139
3140           if (vec_len (unknown_scope))
3141             {
3142               vlib_cli_output (vm, "\tOther-scope address(es):\n");
3143               ip6_print_addrs (vm, unknown_scope);
3144               vec_free (unknown_scope);
3145             }
3146
3147           vlib_cli_output (vm, "\tJoined group address(es):\n");
3148           ip6_mldp_group_t *m;
3149           /* *INDENT-OFF* */
3150           pool_foreach (m, radv_info->mldp_group_pool,
3151           ({
3152             vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3153                              &m->mcast_address);
3154           }));
3155           /* *INDENT-ON* */
3156
3157           vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3158           ip6_radv_prefix_t *p;
3159           /* *INDENT-OFF* */
3160           pool_foreach (p, radv_info->adv_prefixes_pool,
3161           ({
3162             vlib_cli_output (vm, "\t\tprefix %U,  length %d\n",
3163                              format_ip6_address, &p->prefix, p->prefix_len);
3164           }));
3165           /* *INDENT-ON* */
3166
3167           vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
3168           vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3169           vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3170           vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3171           vlib_cli_output (vm, "\tND DAD is disabled\n");
3172           //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3173           vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3174                            radv_info->adv_neighbor_reachable_time_in_msec);
3175           vlib_cli_output (vm,
3176                            "\tND advertised retransmit interval is %d (msec)\n",
3177                            radv_info->
3178                            adv_time_in_msec_between_retransmitted_neighbor_solicitations);
3179
3180           u32 ra_interval = radv_info->max_radv_interval;
3181           u32 ra_interval_min = radv_info->min_radv_interval;
3182           vlib_cli_output (vm,
3183                            "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
3184                            ra_interval, ra_interval_min);
3185           vlib_cli_output (vm,
3186                            "\tND router advertisements live for %d seconds\n",
3187                            radv_info->adv_router_lifetime_in_sec);
3188           vlib_cli_output (vm,
3189                            "\tHosts %s stateless autoconfig for addresses\n",
3190                            (radv_info->adv_managed_flag) ? "use" :
3191                            " don't use");
3192           vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3193                            radv_info->n_advertisements_sent);
3194           vlib_cli_output (vm, "\tND router solicitations received %d\n",
3195                            radv_info->n_solicitations_rcvd);
3196           vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3197                            radv_info->n_solicitations_dropped);
3198         }
3199       else
3200         {
3201           error = clib_error_return (0, "IPv6 not enabled on interface",
3202                                      format_unformat_error, input);
3203
3204         }
3205     }
3206   return error;
3207 }
3208
3209 /*?
3210  * This command is used to display various IPv6 attributes on a given
3211  * interface.
3212  *
3213  * @cliexpar
3214  * Example of how to display IPv6 settings:
3215  * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3216  * GigabitEthernet2/0/0 is admin up
3217  *         Link-local address(es):
3218  *                 fe80::ab8/64
3219  *         Joined group address(es):
3220  *                 ff02::1
3221  *                 ff02::2
3222  *                 ff02::16
3223  *                 ff02::1:ff00:ab8
3224  *         Advertised Prefixes:
3225  *                 prefix fe80::fe:28ff:fe9c:75b3,  length 64
3226  *         MTU is 1500
3227  *         ICMP error messages are unlimited
3228  *         ICMP redirects are disabled
3229  *         ICMP unreachables are not sent
3230  *         ND DAD is disabled
3231  *         ND advertised reachable time is 0
3232  *         ND advertised retransmit interval is 0 (msec)
3233  *         ND router advertisements are sent every 200 seconds (min interval is 150)
3234  *         ND router advertisements live for 600 seconds
3235  *         Hosts use stateless autoconfig for addresses
3236  *         ND router advertisements sent 19336
3237  *         ND router solicitations received 0
3238  *         ND router solicitations dropped 0
3239  * @cliexend
3240  * Example of output if IPv6 is not enabled on the interface:
3241  * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3242  * show ip6 interface: IPv6 not enabled on interface
3243  * @cliexend
3244 ?*/
3245 /* *INDENT-OFF* */
3246 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
3247 {
3248   .path = "show ip6 interface",
3249   .function = show_ip6_interface_cmd,
3250   .short_help = "show ip6 interface <interface>",
3251 };
3252 /* *INDENT-ON* */
3253
3254 clib_error_t *
3255 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
3256 {
3257   clib_error_t *error = 0;
3258   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3259   u32 ri;
3260
3261   /* look up the radv_t  information for this interface */
3262   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3263                            ~0);
3264   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3265
3266   /* if not created - do nothing */
3267   if (ri != ~0)
3268     {
3269       vnet_main_t *vnm = vnet_get_main ();
3270       ip6_radv_t *radv_info;
3271
3272       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3273
3274       /* check radv_info ref count for other ip6 addresses on this interface */
3275       /* This implicitly excludes the link local address */
3276       if (radv_info->ref_count == 0)
3277         {
3278           /* essentially "disables" ipv6 on this interface */
3279           error = ip6_add_del_interface_address (vm, sw_if_index,
3280                                                  &radv_info->
3281                                                  link_local_address, 128,
3282                                                  1 /* is_del */ );
3283
3284           ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3285                                              0 /* is_add */ );
3286         }
3287     }
3288   return error;
3289 }
3290
3291 int
3292 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
3293 {
3294   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3295   u32 ri = ~0;
3296
3297   /* look up the radv_t  information for this interface */
3298   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3299                            ~0);
3300
3301   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3302
3303   return ri != ~0;
3304 }
3305
3306 clib_error_t *
3307 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
3308 {
3309   clib_error_t *error = 0;
3310   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3311   u32 ri;
3312   int is_add = 1;
3313
3314   /* look up the radv_t  information for this interface */
3315   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3316                            ~0);
3317
3318   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3319
3320   /* if not created yet */
3321   if (ri == ~0)
3322     {
3323       vnet_main_t *vnm = vnet_get_main ();
3324       vnet_sw_interface_t *sw_if0;
3325
3326       sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3327       if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3328         {
3329           ethernet_interface_t *eth_if0;
3330
3331           eth_if0 =
3332             ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3333           if (eth_if0)
3334             {
3335               /* create radv_info. for this interface.  This holds all the info needed for router adverts */
3336               ri =
3337                 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
3338
3339               if (ri != ~0)
3340                 {
3341                   ip6_radv_t *radv_info;
3342                   ip6_address_t link_local_address;
3343
3344                   radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3345
3346                   ip6_link_local_address_from_ethernet_mac_address
3347                     (&link_local_address, eth_if0->address);
3348
3349                   sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
3350                   if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
3351                     {
3352                       /* make up  an interface id */
3353                       md5_context_t m;
3354                       u8 digest[16];
3355
3356                       link_local_address.as_u64[0] = radv_info->randomizer;
3357
3358                       md5_init (&m);
3359                       md5_add (&m, &link_local_address, 16);
3360                       md5_finish (&m, digest);
3361
3362                       clib_memcpy (&link_local_address, digest, 16);
3363
3364                       radv_info->randomizer = link_local_address.as_u64[0];
3365
3366                       link_local_address.as_u64[0] =
3367                         clib_host_to_net_u64 (0xFE80000000000000ULL);
3368                       /* clear u bit */
3369                       link_local_address.as_u8[8] &= 0xfd;
3370                     }
3371
3372                   /* essentially "enables" ipv6 on this interface */
3373                   error = ip6_add_del_interface_address (vm, sw_if_index,
3374                                                          &link_local_address,
3375                                                          128
3376                                                          /* address width */ ,
3377                                                          0 /* is_del */ );
3378
3379                   if (error)
3380                     ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3381                                                        !is_add);
3382                   else
3383                     {
3384                       radv_info->link_local_address = link_local_address;
3385                     }
3386                 }
3387             }
3388         }
3389     }
3390   return error;
3391 }
3392
3393 static clib_error_t *
3394 enable_ip6_interface_cmd (vlib_main_t * vm,
3395                           unformat_input_t * input, vlib_cli_command_t * cmd)
3396 {
3397   vnet_main_t *vnm = vnet_get_main ();
3398   clib_error_t *error = 0;
3399   u32 sw_if_index;
3400
3401   sw_if_index = ~0;
3402
3403   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3404     {
3405       enable_ip6_interface (vm, sw_if_index);
3406     }
3407   else
3408     {
3409       error = clib_error_return (0, "unknown interface\n'",
3410                                  format_unformat_error, input);
3411
3412     }
3413   return error;
3414 }
3415
3416 /*?
3417  * This command is used to enable IPv6 on a given interface.
3418  *
3419  * @cliexpar
3420  * Example of how enable IPv6 on a given interface:
3421  * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
3422 ?*/
3423 /* *INDENT-OFF* */
3424 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
3425 {
3426   .path = "enable ip6 interface",
3427   .function = enable_ip6_interface_cmd,
3428   .short_help = "enable ip6 interface <interface>",
3429 };
3430 /* *INDENT-ON* */
3431
3432 static clib_error_t *
3433 disable_ip6_interface_cmd (vlib_main_t * vm,
3434                            unformat_input_t * input, vlib_cli_command_t * cmd)
3435 {
3436   vnet_main_t *vnm = vnet_get_main ();
3437   clib_error_t *error = 0;
3438   u32 sw_if_index;
3439
3440   sw_if_index = ~0;
3441
3442   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3443     {
3444       error = disable_ip6_interface (vm, sw_if_index);
3445     }
3446   else
3447     {
3448       error = clib_error_return (0, "unknown interface\n'",
3449                                  format_unformat_error, input);
3450
3451     }
3452   return error;
3453 }
3454
3455 /*?
3456  * This command is used to disable IPv6 on a given interface.
3457  *
3458  * @cliexpar
3459  * Example of how disable IPv6 on a given interface:
3460  * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
3461 ?*/
3462 /* *INDENT-OFF* */
3463 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
3464 {
3465   .path = "disable ip6 interface",
3466   .function = disable_ip6_interface_cmd,
3467   .short_help = "disable ip6 interface <interface>",
3468 };
3469 /* *INDENT-ON* */
3470
3471 /*?
3472  * This command is used to configure the neighbor discovery
3473  * parameters on a given interface. Use the '<em>show ip6 interface</em>'
3474  * command to display some of the current neighbor discovery parameters
3475  * on a given interface. This command has three formats:
3476  *
3477  *
3478  * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
3479  *
3480  * '<em><b>ip6 nd <interface> [no] [ra-managed-config-flag] | [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] | [ra-send-unicast] | [ra-lifetime <lifetime>] | [ra-initial <cnt> <interval>] | [ra-interval <max-interval> [<min-interval>]] | [ra-cease]</b></em>'
3481  *
3482  * Where:
3483  *
3484  * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
3485  * router-advertisement messages to use stateful address
3486  * auto-configuration to obtain address information (sets the M-bit).
3487  * Default is the M-bit is not set and the '<em>no</em>' option
3488  * returns it to this default state.
3489  *
3490  * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
3491  * router-advertisement messages that hosts use stateful auto
3492  * configuration to obtain nonaddress related information (sets
3493  * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
3494  * option returns it to this default state.
3495  *
3496  * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
3497  * messages. The '<em>no</em>' option implies to enable sending ICMPv6
3498  * router-advertisement messages.
3499  *
3500  * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
3501  * optional source link-layer address in the ICMPv6 router-advertisement
3502  * messages. Default is to include the optional source link-layer address
3503  * and the '<em>no</em>' option returns it to this default state.
3504  *
3505  * <em>[no] ra-send-unicast</em> - Use the source address of the
3506  * router-solicitation message if availiable. The default is to use
3507  * multicast address of all nodes, and the '<em>no</em>' option returns
3508  * it to this default state.
3509  *
3510  * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
3511  * default router in ICMPv6 router-advertisement messages. The range is
3512  * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
3513  * '<em><max-interval></em>'. The default value is 600 seconds and the
3514  * '<em>no</em>' option returns it to this default value.
3515  *
3516  * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
3517  * router-advertisement messages sent and the interval between each
3518  * message. Range for count is 1 - 3 and default is 3. Range for interval
3519  * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
3520  * returns both to their default value.
3521  *
3522  * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
3523  * interval between sending ICMPv6 router-advertisement messages. The
3524  * range for max-interval is from 4 to 200 seconds. min-interval can not
3525  * be more than 75% of max-interval. If not set, min-interval will be
3526  * set to 75% of max-interval. The range for min-interval is from 3 to
3527  * 150 seconds.  The '<em>no</em>' option returns both to their default
3528  * value.
3529  *
3530  * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
3531  * The '<em>no</em>' options implies to start (or restart) sending
3532  * ICMPv6 router-advertisement messages.
3533  *
3534  *
3535  * <b>Format 2 - Prefix Options:</b>
3536  *
3537  * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link] [no-autoconfig] [no-onlink]</b></em>'
3538  *
3539  * Where:
3540  *
3541  * <em>no</em> - All additional flags are ignored and the prefix is deleted.
3542  *
3543  * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
3544  * length of time in seconds during what the prefix is valid for the purpose of
3545  * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
3546  * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
3547  * length of time in seconds during what addresses generated from the prefix remain
3548  * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
3549  *
3550  * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
3551  * are inifinte, no timeout.
3552  *
3553  * <em>no-advertise</em> - Do not send full router address in prefix
3554  * advertisement. Default is to advertise (i.e. - This flag is off by default).
3555  *
3556  * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
3557  * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
3558  * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
3559  *
3560  * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
3561  * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
3562  *
3563  * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
3564  * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
3565  * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
3566  * the L-bit.
3567  *
3568  *
3569  * <b>Format 3: - Default of Prefix:</b>
3570  *
3571  * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
3572  *
3573  * When a new prefix is added (or existing one is being overwritten) <em>default</em>
3574  * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
3575  * is ignored and the prefix is deleted.
3576  *
3577  *
3578  * @cliexpar
3579  * Example of how set a router advertisement option:
3580  * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
3581  * Example of how to add a prefix:
3582  * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
3583  * Example of how to delete a prefix:
3584  * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
3585 ?*/
3586 /* *INDENT-OFF* */
3587 VLIB_CLI_COMMAND (ip6_nd_command, static) =
3588 {
3589   .path = "ip6 nd",
3590   .short_help = "ip6 nd <interface> ...",
3591   .function = ip6_neighbor_cmd,
3592 };
3593 /* *INDENT-ON* */
3594
3595 clib_error_t *
3596 set_ip6_link_local_address (vlib_main_t * vm,
3597                             u32 sw_if_index, ip6_address_t * address)
3598 {
3599   clib_error_t *error = 0;
3600   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3601   u32 ri;
3602   ip6_radv_t *radv_info;
3603   vnet_main_t *vnm = vnet_get_main ();
3604
3605   if (!ip6_address_is_link_local_unicast (address))
3606     {
3607       vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
3608       return (error = clib_error_return (0, "address not link-local",
3609                                          format_unformat_error));
3610     }
3611
3612   /* call enable ipv6  */
3613   enable_ip6_interface (vm, sw_if_index);
3614
3615   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3616
3617   if (ri != ~0)
3618     {
3619       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3620
3621       /* save if link local address (overwrite default) */
3622
3623       /* delete the old one */
3624       error = ip6_add_del_interface_address (vm, sw_if_index,
3625                                              &radv_info->link_local_address,
3626                                              128, 1 /* is_del */ );
3627
3628       if (!error)
3629         {
3630           /* add the new one */
3631           error = ip6_add_del_interface_address (vm, sw_if_index,
3632                                                  address, 128,
3633                                                  0 /* is_del */ );
3634
3635           if (!error)
3636             {
3637               radv_info->link_local_address = *address;
3638             }
3639         }
3640     }
3641   else
3642     {
3643       vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3644       error = clib_error_return (0, "ip6 not enabled for interface",
3645                                  format_unformat_error);
3646     }
3647   return error;
3648 }
3649
3650 clib_error_t *
3651 set_ip6_link_local_address_cmd (vlib_main_t * vm,
3652                                 unformat_input_t * input,
3653                                 vlib_cli_command_t * cmd)
3654 {
3655   vnet_main_t *vnm = vnet_get_main ();
3656   clib_error_t *error = 0;
3657   u32 sw_if_index;
3658   ip6_address_t ip6_addr;
3659
3660   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3661     {
3662       /* get the rest of the command */
3663       while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3664         {
3665           if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
3666             break;
3667           else
3668             return (unformat_parse_error (input));
3669         }
3670     }
3671   error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
3672   return error;
3673 }
3674
3675 /*?
3676  * This command is used to assign an IPv6 Link-local address to an
3677  * interface. This command will enable IPv6 on an interface if it
3678  * is not already enabled. Use the '<em>show ip6 interface</em>' command
3679  * to display the assigned Link-local address.
3680  *
3681  * @cliexpar
3682  * Example of how to assign an IPv6 Link-local address to an interface:
3683  * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
3684 ?*/
3685 /* *INDENT-OFF* */
3686 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
3687 {
3688   .path = "set ip6 link-local address",
3689   .short_help = "set ip6 link-local address <interface> <ip6-address>",
3690   .function = set_ip6_link_local_address_cmd,
3691 };
3692 /* *INDENT-ON* */
3693
3694 /* callback when an interface address is added or deleted */
3695 static void
3696 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3697                                         uword opaque,
3698                                         u32 sw_if_index,
3699                                         ip6_address_t * address,
3700                                         u32 address_length,
3701                                         u32 if_address_index, u32 is_delete)
3702 {
3703   vnet_main_t *vnm = vnet_get_main ();
3704   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3705   u32 ri;
3706   vlib_main_t *vm = vnm->vlib_main;
3707   ip6_radv_t *radv_info;
3708   ip6_address_t a;
3709   ip6_mldp_group_t *mcast_group_info;
3710
3711   /* create solicited node multicast address for this interface adddress */
3712   ip6_set_solicited_node_multicast_address (&a, 0);
3713
3714   a.as_u8[0xd] = address->as_u8[0xd];
3715   a.as_u8[0xe] = address->as_u8[0xe];
3716   a.as_u8[0xf] = address->as_u8[0xf];
3717
3718   if (!is_delete)
3719     {
3720       /* try to  create radv_info - does nothing if ipv6 already enabled */
3721       enable_ip6_interface (vm, sw_if_index);
3722
3723       /* look up the radv_t  information for this interface */
3724       vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3725                                sw_if_index, ~0);
3726       ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3727       if (ri != ~0)
3728         {
3729           /* get radv_info */
3730           radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3731
3732           /* add address */
3733           if (!ip6_address_is_link_local_unicast (address))
3734             radv_info->ref_count++;
3735
3736           /* lookup  prefix info for this  address on this interface */
3737           uword *p = mhash_get (&radv_info->address_to_mldp_index, &a);
3738           mcast_group_info =
3739             p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3740
3741           /* add -solicted node multicast address  */
3742           if (!mcast_group_info)
3743             {
3744               /* add */
3745               u32 mi;
3746               pool_get (radv_info->mldp_group_pool, mcast_group_info);
3747
3748               mi = mcast_group_info - radv_info->mldp_group_pool;
3749               mhash_set (&radv_info->address_to_mldp_index, &a, mi,
3750                          /* old_value */ 0);
3751
3752               mcast_group_info->type = 4;
3753               mcast_group_info->mcast_source_address_pool = 0;
3754               mcast_group_info->num_sources = 0;
3755               clib_memcpy (&mcast_group_info->mcast_address, &a,
3756                            sizeof (ip6_address_t));
3757             }
3758         }
3759     }
3760   else
3761     {
3762
3763       /* delete */
3764       /* look up the radv_t  information for this interface */
3765       vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3766                                sw_if_index, ~0);
3767       ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3768
3769       if (ri != ~0)
3770         {
3771           /* get radv_info */
3772           radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3773
3774           /* lookup  prefix info for this  address on this interface */
3775           uword *p = mhash_get (&radv_info->address_to_mldp_index, &a);
3776           mcast_group_info =
3777             p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
3778
3779           if (mcast_group_info)
3780             {
3781               mhash_unset (&radv_info->address_to_mldp_index, &a,
3782                            /* old_value */ 0);
3783               pool_put (radv_info->mldp_group_pool, mcast_group_info);
3784             }
3785
3786           /* if interface up send MLDP "report" */
3787           radv_info->all_routers_mcast = 0;
3788
3789           /* add address */
3790           if (!ip6_address_is_link_local_unicast (address))
3791             radv_info->ref_count--;
3792         }
3793       /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
3794       disable_ip6_interface (vm, sw_if_index);
3795     }
3796 }
3797
3798 clib_error_t *
3799 ip6_set_neighbor_limit (u32 neighbor_limit)
3800 {
3801   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3802
3803   nm->limit_neighbor_cache_size = neighbor_limit;
3804   return 0;
3805 }
3806
3807 static clib_error_t *
3808 ip6_neighbor_init (vlib_main_t * vm)
3809 {
3810   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3811   ip6_main_t *im = &ip6_main;
3812
3813   mhash_init (&nm->neighbor_index_by_key,
3814               /* value size */ sizeof (uword),
3815               /* key size */ sizeof (ip6_neighbor_key_t));
3816
3817   icmp6_register_type (vm, ICMP6_neighbor_solicitation,
3818                        ip6_icmp_neighbor_solicitation_node.index);
3819   icmp6_register_type (vm, ICMP6_neighbor_advertisement,
3820                        ip6_icmp_neighbor_advertisement_node.index);
3821   icmp6_register_type (vm, ICMP6_router_solicitation,
3822                        ip6_icmp_router_solicitation_node.index);
3823   icmp6_register_type (vm, ICMP6_router_advertisement,
3824                        ip6_icmp_router_advertisement_node.index);
3825
3826   /* handler node for ip6 neighbor discovery events and timers */
3827   vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3828
3829   /* add call backs */
3830   ip6_add_del_interface_address_callback_t cb;
3831   memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
3832
3833   /* when an interface address changes... */
3834   cb.function = ip6_neighbor_add_del_interface_address;
3835   cb.function_opaque = 0;
3836   vec_add1 (im->add_del_interface_address_callbacks, cb);
3837
3838   mhash_init (&nm->pending_resolutions_by_address,
3839               /* value size */ sizeof (uword),
3840               /* key size */ sizeof (ip6_address_t));
3841
3842   mhash_init (&nm->mac_changes_by_address,
3843               /* value size */ sizeof (uword),
3844               /* key size */ sizeof (ip6_address_t));
3845
3846   /* default, configurable */
3847   nm->limit_neighbor_cache_size = 50000;
3848
3849 #if 0
3850   /* $$$$ Hack fix for today */
3851   vec_validate_init_empty
3852     (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
3853 #endif
3854
3855   return 0;
3856 }
3857
3858 VLIB_INIT_FUNCTION (ip6_neighbor_init);
3859
3860
3861 void
3862 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3863                                              void *address_arg,
3864                                              uword node_index,
3865                                              uword type_opaque, uword data)
3866 {
3867   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3868   ip6_address_t *address = address_arg;
3869   uword *p;
3870   pending_resolution_t *pr;
3871
3872   pool_get (nm->pending_resolutions, pr);
3873
3874   pr->next_index = ~0;
3875   pr->node_index = node_index;
3876   pr->type_opaque = type_opaque;
3877   pr->data = data;
3878
3879   p = mhash_get (&nm->pending_resolutions_by_address, address);
3880   if (p)
3881     {
3882       /* Insert new resolution at the head of the list */
3883       pr->next_index = p[0];
3884       mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3885     }
3886
3887   mhash_set (&nm->pending_resolutions_by_address, address,
3888              pr - nm->pending_resolutions, 0 /* old value */ );
3889 }
3890
3891 int
3892 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3893                                   void *data_callback,
3894                                   u32 pid,
3895                                   void *address_arg,
3896                                   uword node_index,
3897                                   uword type_opaque, uword data, int is_add)
3898 {
3899   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3900   ip6_address_t *address = address_arg;
3901   uword *p;
3902   pending_resolution_t *mc;
3903   void (*fp) (u32, u8 *) = data_callback;
3904
3905   if (is_add)
3906     {
3907       pool_get (nm->mac_changes, mc);
3908
3909       mc->next_index = ~0;
3910       mc->node_index = node_index;
3911       mc->type_opaque = type_opaque;
3912       mc->data = data;
3913       mc->data_callback = data_callback;
3914       mc->pid = pid;
3915
3916       p = mhash_get (&nm->mac_changes_by_address, address);
3917       if (p)
3918         {
3919           /* Insert new resolution at the head of the list */
3920           mc->next_index = p[0];
3921           mhash_unset (&nm->mac_changes_by_address, address, 0);
3922         }
3923
3924       mhash_set (&nm->mac_changes_by_address, address,
3925                  mc - nm->mac_changes, 0);
3926       return 0;
3927     }
3928   else
3929     {
3930       u32 index;
3931       pending_resolution_t *mc_last = 0;
3932
3933       p = mhash_get (&nm->mac_changes_by_address, address);
3934       if (p == 0)
3935         return VNET_API_ERROR_NO_SUCH_ENTRY;
3936
3937       index = p[0];
3938
3939       while (index != (u32) ~ 0)
3940         {
3941           mc = pool_elt_at_index (nm->mac_changes, index);
3942           if (mc->node_index == node_index &&
3943               mc->type_opaque == type_opaque && mc->pid == pid)
3944             {
3945               /* Clients may need to clean up pool entries, too */
3946               if (fp)
3947                 (*fp) (mc->data, 0 /* no new mac addrs */ );
3948               if (index == p[0])
3949                 {
3950                   mhash_unset (&nm->mac_changes_by_address, address, 0);
3951                   if (mc->next_index != ~0)
3952                     mhash_set (&nm->mac_changes_by_address, address,
3953                                mc->next_index, 0);
3954                   pool_put (nm->mac_changes, mc);
3955                   return 0;
3956                 }
3957               else
3958                 {
3959                   ASSERT (mc_last);
3960                   mc_last->next_index = mc->next_index;
3961                   pool_put (nm->mac_changes, mc);
3962                   return 0;
3963                 }
3964             }
3965           mc_last = mc;
3966           index = mc->next_index;
3967         }
3968
3969       return VNET_API_ERROR_NO_SUCH_ENTRY;
3970     }
3971 }
3972
3973 int
3974 vnet_ip6_nd_term (vlib_main_t * vm,
3975                   vlib_node_runtime_t * node,
3976                   vlib_buffer_t * p0,
3977                   ethernet_header_t * eth,
3978                   ip6_header_t * ip, u32 sw_if_index, u16 bd_index, u8 shg)
3979 {
3980   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3981   icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
3982   pending_resolution_t *mc;
3983   uword *p;
3984
3985   ndh = ip6_next_header (ip);
3986   if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
3987       ndh->icmp.type != ICMP6_neighbor_advertisement)
3988     return 0;
3989
3990   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3991                      (p0->flags & VLIB_BUFFER_IS_TRACED)))
3992     {
3993       u8 *t0 = vlib_add_trace (vm, node, p0,
3994                                sizeof (icmp6_input_trace_t));
3995       clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
3996     }
3997
3998   /* Check if anyone want ND events for L2 BDs */
3999   p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
4000   if (p && shg == 0 &&          /* Only SHG 0 interface which is more likely local */
4001       !ip6_address_is_link_local_unicast (&ip->src_address))
4002     {
4003       u32 next_index = p[0];
4004       while (next_index != (u32) ~ 0)
4005         {
4006           int (*fp) (u32, u8 *, u32, ip6_address_t *);
4007           int rv = 1;
4008           mc = pool_elt_at_index (nm->mac_changes, next_index);
4009           fp = mc->data_callback;
4010           /* Call the callback, return 1 to suppress dup events */
4011           if (fp)
4012             rv = (*fp) (mc->data,
4013                         eth->src_address, sw_if_index, &ip->src_address);
4014           /* Signal the resolver process */
4015           if (rv == 0)
4016             vlib_process_signal_event (vm, mc->node_index,
4017                                        mc->type_opaque, mc->data);
4018           next_index = mc->next_index;
4019         }
4020     }
4021
4022   /* Check if MAC entry exsist for solicited target IP */
4023   if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4024     {
4025       icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4026       l2_bridge_domain_t *bd_config;
4027       u8 *macp;
4028
4029       opt = (void *) (ndh + 1);
4030       if ((opt->header.type !=
4031            ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4032           (opt->header.n_data_u64s != 1))
4033         return 0;               /* source link layer address option not present */
4034
4035       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4036       macp =
4037         (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4038       if (macp)
4039         {                       /* found ip-mac entry, generate eighbor advertisement response */
4040           int bogus_length;
4041           vlib_node_runtime_t *error_node =
4042             vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4043           ip->dst_address = ip->src_address;
4044           ip->src_address = ndh->target_address;
4045           ip->hop_limit = 255;
4046           opt->header.type =
4047             ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4048           clib_memcpy (opt->ethernet_address, macp, 6);
4049           ndh->icmp.type = ICMP6_neighbor_advertisement;
4050           ndh->advertisement_flags = clib_host_to_net_u32
4051             (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4052              ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4053           ndh->icmp.checksum = 0;
4054           ndh->icmp.checksum =
4055             ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4056           clib_memcpy (eth->dst_address, eth->src_address, 6);
4057           clib_memcpy (eth->src_address, macp, 6);
4058           vlib_error_count (vm, error_node->node_index,
4059                             ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4060           return 1;
4061         }
4062     }
4063
4064   return 0;
4065
4066 }
4067
4068 void
4069 ethernet_ndp_change_mac (u32 sw_if_index)
4070 {
4071   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4072   ip6_neighbor_t *n;
4073
4074   /* *INDENT-OFF* */
4075   pool_foreach (n, nm->neighbor_pool,
4076   ({
4077     if (n->key.sw_if_index == sw_if_index)
4078       {
4079         adj_nbr_walk_nh6 (sw_if_index,
4080                           &n->key.ip6_address,
4081                           ip6_nd_mk_complete_walk, n);
4082       }
4083   }));
4084   /* *INDENT-ON* */
4085 }
4086
4087 /*
4088  * fd.io coding-style-patch-verification: ON
4089  *
4090  * Local Variables:
4091  * eval: (c-set-style "gnu")
4092  * End:
4093  */