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