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