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