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