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