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