IPv6 NS/RS; do not vec_validate global structs in the DP
[vpp.git] / src / 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/ip/ip6_neighbor.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vppinfra/mhash.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/adj/adj_mcast.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/fib/ip6_fib.h>
26 #include <vnet/mfib/ip6_mfib.h>
27 #include <vnet/ip/ip6_ll_table.h>
28
29 /**
30  * @file
31  * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
32  *
33  * The files contains the API and CLI code for managing IPv6 neighbor
34  * adjacency tables and neighbor discovery logic.
35  */
36
37 /* can't use sizeof link_layer_address, that's 8 */
38 #define ETHER_MAC_ADDR_LEN 6
39
40 /* advertised prefix option */
41 typedef struct
42 {
43   /* basic advertised information */
44   ip6_address_t prefix;
45   u8 prefix_len;
46   int adv_on_link_flag;
47   int adv_autonomous_flag;
48   u32 adv_valid_lifetime_in_secs;
49   u32 adv_pref_lifetime_in_secs;
50
51   /* advertised values are computed from these times if decrementing */
52   f64 valid_lifetime_expires;
53   f64 pref_lifetime_expires;
54
55   /* local information */
56   int enabled;
57   int deprecated_prefix_flag;
58   int decrement_lifetime_flag;
59
60 #define MIN_ADV_VALID_LIFETIME 7203     /* seconds */
61 #define DEF_ADV_VALID_LIFETIME  2592000
62 #define DEF_ADV_PREF_LIFETIME 604800
63
64   /* extensions are added here, mobile, DNS etc.. */
65 } ip6_radv_prefix_t;
66
67
68 typedef struct
69 {
70   /* group information */
71   u8 type;
72   ip6_address_t mcast_address;
73   u16 num_sources;
74   ip6_address_t *mcast_source_address_pool;
75 } ip6_mldp_group_t;
76
77 /* configured router advertisement information per ipv6 interface */
78 typedef struct
79 {
80
81   /* advertised config information, zero means unspecified  */
82   u8 curr_hop_limit;
83   int adv_managed_flag;
84   int adv_other_flag;
85   u16 adv_router_lifetime_in_sec;
86   u32 adv_neighbor_reachable_time_in_msec;
87   u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
88
89   /* mtu option */
90   u32 adv_link_mtu;
91
92   /* source link layer option */
93   u8 link_layer_address[8];
94   u8 link_layer_addr_len;
95
96   /* prefix option */
97   ip6_radv_prefix_t *adv_prefixes_pool;
98
99   /* Hash table mapping address to index in interface advertised  prefix pool. */
100   mhash_t address_to_prefix_index;
101
102   /* MLDP  group information */
103   ip6_mldp_group_t *mldp_group_pool;
104
105   /* Hash table mapping address to index in mldp address pool. */
106   mhash_t address_to_mldp_index;
107
108   /* local information */
109   u32 sw_if_index;
110   int send_radv;                /* radv on/off on this interface -  set by config */
111   int cease_radv;               /* we are ceasing  to send  - set byf config */
112   int send_unicast;
113   int adv_link_layer_address;
114   int prefix_option;
115   int failed_device_check;
116   int all_routers_mcast;
117   u32 seed;
118   u64 randomizer;
119   int ref_count;
120   adj_index_t mcast_adj_index;
121
122   /* timing information */
123 #define DEF_MAX_RADV_INTERVAL 200
124 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125 #define DEF_CURR_HOP_LIMIT  64
126 #define DEF_DEF_RTR_LIFETIME   3 * DEF_MAX_RADV_INTERVAL
127 #define MAX_DEF_RTR_LIFETIME   9000
128
129 #define MAX_INITIAL_RTR_ADVERT_INTERVAL   16    /* seconds */
130 #define MAX_INITIAL_RTR_ADVERTISEMENTS        3 /*transmissions */
131 #define MIN_DELAY_BETWEEN_RAS                              3    /* seconds */
132 #define MAX_DELAY_BETWEEN_RAS                    1800   /* seconds */
133 #define MAX_RA_DELAY_TIME                                          .5   /* seconds */
134
135   f64 max_radv_interval;
136   f64 min_radv_interval;
137   f64 min_delay_between_radv;
138   f64 max_delay_between_radv;
139   f64 max_rtr_default_lifetime;
140
141   f64 last_radv_time;
142   f64 last_multicast_time;
143   f64 next_multicast_time;
144
145
146   u32 initial_adverts_count;
147   f64 initial_adverts_interval;
148   u32 initial_adverts_sent;
149
150   /* stats */
151   u32 n_advertisements_sent;
152   u32 n_solicitations_rcvd;
153   u32 n_solicitations_dropped;
154
155   /* Link local address to use (defaults to underlying physical for logical interfaces */
156   ip6_address_t link_local_address;
157
158   /* router solicitations sending state */
159   u8 keep_sending_rs;           /* when true then next fields are valid */
160   icmp6_send_router_solicitation_params_t params;
161   f64 sleep_interval;
162   f64 due_time;
163   u32 n_left;
164   f64 start_time;
165   vlib_buffer_t *buffer;
166 } ip6_radv_t;
167
168 typedef struct
169 {
170   u32 next_index;
171   uword node_index;
172   uword type_opaque;
173   uword data;
174   /* Used for nd event notification only */
175   void *data_callback;
176   u32 pid;
177 } pending_resolution_t;
178
179
180 typedef struct
181 {
182   /* Hash tables mapping name to opcode. */
183   uword *opcode_by_name;
184
185   /* lite beer "glean" adjacency handling */
186   mhash_t pending_resolutions_by_address;
187   pending_resolution_t *pending_resolutions;
188
189   /* Mac address change notification */
190   mhash_t mac_changes_by_address;
191   pending_resolution_t *mac_changes;
192
193   u32 *neighbor_input_next_index_by_hw_if_index;
194
195   ip6_neighbor_t *neighbor_pool;
196
197   mhash_t neighbor_index_by_key;
198
199   u32 *if_radv_pool_index_by_sw_if_index;
200
201   ip6_radv_t *if_radv_pool;
202
203   /* Neighbor attack mitigation */
204   u32 limit_neighbor_cache_size;
205   u32 neighbor_delete_rotor;
206
207   /* Wildcard nd report publisher */
208   uword wc_ip6_nd_publisher_node;
209   uword wc_ip6_nd_publisher_et;
210
211   /* Router advertisement report publisher */
212   uword ip6_ra_publisher_node;
213   uword ip6_ra_publisher_et;
214 } ip6_neighbor_main_t;
215
216 /* ipv6 neighbor discovery - timer/event types */
217 typedef enum
218 {
219   ICMP6_ND_EVENT_INIT,
220 } ip6_icmp_neighbor_discovery_event_type_t;
221
222 typedef union
223 {
224   u32 add_del_swindex;
225   struct
226   {
227     u32 up_down_swindex;
228     u32 fib_index;
229   } up_down_event;
230 } ip6_icmp_neighbor_discovery_event_data_t;
231
232 static ip6_neighbor_main_t ip6_neighbor_main;
233 static ip6_address_t ip6a_zero; /* ip6 address 0 */
234
235 static void wc_nd_signal_report (wc_nd_report_t * r);
236 static void ra_signal_report (ra_report_t * r);
237
238 /**
239  * @brief publish wildcard arp event
240  * @param sw_if_index The interface on which the ARP entires are acted
241  */
242 static int
243 vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
244 {
245   wc_nd_report_t r = {
246     .sw_if_index = sw_if_index,
247     .ip6 = *ip6,
248   };
249   memcpy (r.mac, mac, sizeof r.mac);
250
251   void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
252   vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
253   return 0;
254 }
255
256 static void
257 wc_nd_signal_report (wc_nd_report_t * r)
258 {
259   vlib_main_t *vm = vlib_get_main ();
260   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
261   uword ni = nm->wc_ip6_nd_publisher_node;
262   uword et = nm->wc_ip6_nd_publisher_et;
263
264   if (ni == (uword) ~ 0)
265     return;
266   wc_nd_report_t *q =
267     vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
268
269   *q = *r;
270 }
271
272 void
273 wc_nd_set_publisher_node (uword node_index, uword event_type)
274 {
275   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
276   nm->wc_ip6_nd_publisher_node = node_index;
277   nm->wc_ip6_nd_publisher_et = event_type;
278 }
279
280 static int
281 ra_publish (ra_report_t * r)
282 {
283   void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
284   vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
285   return 0;
286 }
287
288 static void
289 ra_signal_report (ra_report_t * r)
290 {
291   vlib_main_t *vm = vlib_get_main ();
292   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
293   uword ni = nm->ip6_ra_publisher_node;
294   uword et = nm->ip6_ra_publisher_et;
295
296   if (ni == (uword) ~ 0)
297     return;
298   ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
299
300   *q = *r;
301 }
302
303 void
304 ra_set_publisher_node (uword node_index, uword event_type)
305 {
306   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
307   nm->ip6_ra_publisher_node = node_index;
308   nm->ip6_ra_publisher_et = event_type;
309 }
310
311 static u8 *
312 format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
313 {
314   vlib_main_t *vm = va_arg (*va, vlib_main_t *);
315   ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
316   vnet_main_t *vnm = vnet_get_main ();
317   vnet_sw_interface_t *si;
318   u8 *flags = 0;
319
320   if (!n)
321     return format (s, "%=12s%=25s%=6s%=20s%=40s", "Time", "Address", "Flags",
322                    "Link layer", "Interface");
323
324   if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
325     flags = format (flags, "D");
326
327   if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
328     flags = format (flags, "S");
329
330   if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
331     flags = format (flags, "N");
332
333   si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
334   s = format (s, "%=12U%=25U%=6s%=20U%=40U",
335               format_vlib_cpu_time, vm, n->cpu_time_last_updated,
336               format_ip6_address, &n->key.ip6_address,
337               flags ? (char *) flags : "",
338               format_ethernet_address, n->link_layer_address,
339               format_vnet_sw_interface_name, vnm, si);
340
341   vec_free (flags);
342   return s;
343 }
344
345 static void
346 ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
347 {
348   if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
349     {
350       if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
351         {
352           ip6_ll_prefix_t pfx = {
353             .ilp_addr = n->key.ip6_address,
354             .ilp_sw_if_index = n->key.sw_if_index,
355           };
356           ip6_ll_table_entry_delete (&pfx);
357         }
358       else
359         {
360           fib_prefix_t pfx = {
361             .fp_len = 128,
362             .fp_proto = FIB_PROTOCOL_IP6,
363             .fp_addr.ip6 = n->key.ip6_address,
364           };
365           fib_table_entry_path_remove (fib_index,
366                                        &pfx,
367                                        FIB_SOURCE_ADJ,
368                                        DPO_PROTO_IP6,
369                                        &pfx.fp_addr,
370                                        n->key.sw_if_index, ~0,
371                                        1, FIB_ROUTE_PATH_FLAG_NONE);
372         }
373     }
374 }
375
376 static clib_error_t *
377 ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
378                                    u32 sw_if_index, u32 flags)
379 {
380   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
381   ip6_neighbor_t *n;
382
383   if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
384     {
385       u32 i, *to_delete = 0;
386
387       /* *INDENT-OFF* */
388       pool_foreach (n, nm->neighbor_pool,
389       ({
390         if (n->key.sw_if_index == sw_if_index)
391           vec_add1 (to_delete, n - nm->neighbor_pool);
392       }));
393       /* *INDENT-ON* */
394
395       for (i = 0; i < vec_len (to_delete); i++)
396         {
397           n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
398           mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
399           ip6_neighbor_adj_fib_remove (n,
400                                        ip6_fib_table_get_index_for_sw_if_index
401                                        (n->key.sw_if_index));
402           pool_put (nm->neighbor_pool, n);
403         }
404       vec_free (to_delete);
405     }
406
407   return 0;
408 }
409
410 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
411
412 static void
413 unset_random_neighbor_entry (void)
414 {
415   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
416   vnet_main_t *vnm = vnet_get_main ();
417   vlib_main_t *vm = vnm->vlib_main;
418   ip6_neighbor_t *e;
419   u32 index;
420
421   index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
422   nm->neighbor_delete_rotor = index;
423
424   /* Try again from elt 0, could happen if an intfc goes down */
425   if (index == ~0)
426     {
427       index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
428       nm->neighbor_delete_rotor = index;
429     }
430
431   /* Nothing left in the pool */
432   if (index == ~0)
433     return;
434
435   e = pool_elt_at_index (nm->neighbor_pool, index);
436
437   vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
438                                     &e->key.ip6_address,
439                                     e->link_layer_address,
440                                     ETHER_MAC_ADDR_LEN);
441 }
442
443 typedef struct
444 {
445   u8 is_add;
446   u8 is_static;
447   u8 is_no_fib_entry;
448   u8 link_layer_address[6];
449   u32 sw_if_index;
450   ip6_address_t addr;
451 } ip6_neighbor_set_unset_rpc_args_t;
452
453 static void ip6_neighbor_set_unset_rpc_callback
454   (ip6_neighbor_set_unset_rpc_args_t * a);
455
456 static void set_unset_ip6_neighbor_rpc
457   (vlib_main_t * vm,
458    u32 sw_if_index,
459    ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
460    int is_no_fib_entry)
461 {
462   ip6_neighbor_set_unset_rpc_args_t args;
463   void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
464
465   args.sw_if_index = sw_if_index;
466   args.is_add = is_add;
467   args.is_static = is_static;
468   args.is_no_fib_entry = is_no_fib_entry;
469   clib_memcpy (&args.addr, a, sizeof (*a));
470   if (NULL != link_layer_address)
471     clib_memcpy (args.link_layer_address, link_layer_address, 6);
472
473   vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
474                                (u8 *) & args, sizeof (args));
475 }
476
477 static void
478 ip6_nbr_probe (ip_adjacency_t * adj)
479 {
480   icmp6_neighbor_solicitation_header_t *h;
481   vnet_main_t *vnm = vnet_get_main ();
482   ip6_main_t *im = &ip6_main;
483   ip_interface_address_t *ia;
484   ip6_address_t *dst, *src;
485   vnet_hw_interface_t *hi;
486   vnet_sw_interface_t *si;
487   vlib_buffer_t *b;
488   int bogus_length;
489   vlib_main_t *vm;
490   u32 bi = 0;
491
492   vm = vlib_get_main ();
493
494   si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
495   dst = &adj->sub_type.nbr.next_hop.ip6;
496
497   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
498     {
499       return;
500     }
501   src = ip6_interface_address_matching_destination (im, dst,
502                                                     adj->rewrite_header.
503                                                     sw_if_index, &ia);
504   if (!src)
505     {
506       return;
507     }
508
509   h = vlib_packet_template_get_packet (vm,
510                                        &im->discover_neighbor_packet_template,
511                                        &bi);
512
513   hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
514
515   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
516   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
517   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
518   h->ip.src_address = src[0];
519   h->neighbor.target_address = dst[0];
520
521   clib_memcpy (h->link_layer_option.ethernet_address,
522                hi->hw_address, vec_len (hi->hw_address));
523
524   h->neighbor.icmp.checksum =
525     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
526   ASSERT (bogus_length == 0);
527
528   b = vlib_get_buffer (vm, bi);
529   vnet_buffer (b)->sw_if_index[VLIB_RX] =
530     vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
531
532   /* Add encapsulation string for software interface (e.g. ethernet header). */
533   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
534   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
535
536   {
537     vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
538     u32 *to_next = vlib_frame_vector_args (f);
539     to_next[0] = bi;
540     f->n_vectors = 1;
541     vlib_put_frame_to_node (vm, hi->output_node_index, f);
542   }
543 }
544
545 static void
546 ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
547 {
548   adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
549                           ethernet_build_rewrite (vnet_get_main (),
550                                                   nbr->key.sw_if_index,
551                                                   adj_get_link_type (ai),
552                                                   nbr->link_layer_address));
553 }
554
555 static void
556 ip6_nd_mk_incomplete (adj_index_t ai)
557 {
558   ip_adjacency_t *adj = adj_get (ai);
559
560   adj_nbr_update_rewrite (ai,
561                           ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
562                           ethernet_build_rewrite (vnet_get_main (),
563                                                   adj->rewrite_header.
564                                                   sw_if_index,
565                                                   adj_get_link_type (ai),
566                                                   VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
567 }
568
569 #define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
570 {                                            \
571     k.sw_if_index = sw_if_index;             \
572     k.ip6_address = *addr;                   \
573     k.pad = 0;                               \
574 }
575
576 static ip6_neighbor_t *
577 ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
578 {
579   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
580   ip6_neighbor_t *n = NULL;
581   ip6_neighbor_key_t k;
582   uword *p;
583
584   IP6_NBR_MK_KEY (k, sw_if_index, addr);
585
586   p = mhash_get (&nm->neighbor_index_by_key, &k);
587   if (p)
588     {
589       n = pool_elt_at_index (nm->neighbor_pool, p[0]);
590     }
591
592   return (n);
593 }
594
595 static adj_walk_rc_t
596 ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
597 {
598   ip6_neighbor_t *nbr = ctx;
599
600   ip6_nd_mk_complete (ai, nbr);
601
602   return (ADJ_WALK_RC_CONTINUE);
603 }
604
605 static adj_walk_rc_t
606 ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
607 {
608   ip6_nd_mk_incomplete (ai);
609
610   return (ADJ_WALK_RC_CONTINUE);
611 }
612
613 void
614 ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
615 {
616   ip6_neighbor_t *nbr;
617   ip_adjacency_t *adj;
618
619   adj = adj_get (ai);
620
621   nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
622
623   switch (adj->lookup_next_index)
624     {
625     case IP_LOOKUP_NEXT_GLEAN:
626       adj_glean_update_rewrite (ai);
627       break;
628     case IP_LOOKUP_NEXT_ARP:
629       if (NULL != nbr)
630         {
631           adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
632                             ip6_nd_mk_complete_walk, nbr);
633         }
634       else
635         {
636           /*
637            * no matching ND entry.
638            * construct the rewrite required to for an ND packet, and stick
639            * that in the adj's pipe to smoke.
640            */
641           adj_nbr_update_rewrite (ai,
642                                   ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
643                                   ethernet_build_rewrite (vnm,
644                                                           sw_if_index,
645                                                           VNET_LINK_IP6,
646                                                           VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
647
648           /*
649            * since the FIB has added this adj for a route, it makes sense it may
650            * want to forward traffic sometime soon. Let's send a speculative ND.
651            * just one. If we were to do periodically that wouldn't be bad either,
652            * but that's more code than i'm prepared to write at this time for
653            * relatively little reward.
654            */
655           ip6_nbr_probe (adj);
656         }
657       break;
658     case IP_LOOKUP_NEXT_MCAST:
659       {
660         /*
661          * Construct a partial rewrite from the known ethernet mcast dest MAC
662          */
663         u8 *rewrite;
664         u8 offset;
665
666         rewrite = ethernet_build_rewrite (vnm,
667                                           sw_if_index,
668                                           adj->ia_link,
669                                           ethernet_ip6_mcast_dst_addr ());
670
671         /*
672          * Complete the remaining fields of the adj's rewrite to direct the
673          * complete of the rewrite at switch time by copying in the IP
674          * dst address's bytes.
675          * Ofset is 2 bytes into the desintation address.
676          */
677         offset = vec_len (rewrite) - 2;
678         adj_mcast_update_rewrite (ai, rewrite, offset);
679
680         break;
681       }
682     case IP_LOOKUP_NEXT_DROP:
683     case IP_LOOKUP_NEXT_PUNT:
684     case IP_LOOKUP_NEXT_LOCAL:
685     case IP_LOOKUP_NEXT_REWRITE:
686     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
687     case IP_LOOKUP_NEXT_MIDCHAIN:
688     case IP_LOOKUP_NEXT_ICMP_ERROR:
689     case IP_LOOKUP_N_NEXT:
690       ASSERT (0);
691       break;
692     }
693 }
694
695
696 static void
697 ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
698 {
699   if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
700     {
701       ip6_ll_prefix_t pfx = {
702         .ilp_addr = n->key.ip6_address,
703         .ilp_sw_if_index = n->key.sw_if_index,
704       };
705       n->fib_entry_index =
706         ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
707     }
708   else
709     {
710       fib_prefix_t pfx = {
711         .fp_len = 128,
712         .fp_proto = FIB_PROTOCOL_IP6,
713         .fp_addr.ip6 = n->key.ip6_address,
714       };
715
716       n->fib_entry_index =
717         fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
718                                   FIB_ENTRY_FLAG_ATTACHED,
719                                   DPO_PROTO_IP6, &pfx.fp_addr,
720                                   n->key.sw_if_index, ~0, 1, NULL,
721                                   FIB_ROUTE_PATH_FLAG_NONE);
722     }
723 }
724
725 int
726 vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
727                                 u32 sw_if_index,
728                                 ip6_address_t * a,
729                                 u8 * link_layer_address,
730                                 uword n_bytes_link_layer_address,
731                                 int is_static, int is_no_fib_entry)
732 {
733   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
734   ip6_neighbor_key_t k;
735   ip6_neighbor_t *n = 0;
736   int make_new_nd_cache_entry = 1;
737   uword *p;
738   u32 next_index;
739   pending_resolution_t *pr, *mc;
740
741   if (vlib_get_thread_index ())
742     {
743       set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
744                                   1 /* set new neighbor */ , is_static,
745                                   is_no_fib_entry);
746       return 0;
747     }
748
749   k.sw_if_index = sw_if_index;
750   k.ip6_address = a[0];
751   k.pad = 0;
752
753   p = mhash_get (&nm->neighbor_index_by_key, &k);
754   if (p)
755     {
756       n = pool_elt_at_index (nm->neighbor_pool, p[0]);
757       /* Refuse to over-write static neighbor entry. */
758       if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
759         return -2;
760       make_new_nd_cache_entry = 0;
761     }
762
763   if (make_new_nd_cache_entry)
764     {
765       pool_get (nm->neighbor_pool, n);
766       mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
767                  /* old value */ 0);
768       n->key = k;
769       n->fib_entry_index = FIB_NODE_INDEX_INVALID;
770
771       clib_memcpy (n->link_layer_address,
772                    link_layer_address, n_bytes_link_layer_address);
773
774       /*
775        * create the adj-fib. the entry in the FIB table for and to the peer.
776        */
777       if (!is_no_fib_entry)
778         {
779           ip6_neighbor_adj_fib_add
780             (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
781         }
782       else
783         {
784           n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
785         }
786     }
787   else
788     {
789       /*
790        * prevent a DoS attack from the data-plane that
791        * spams us with no-op updates to the MAC address
792        */
793       if (0 == memcmp (n->link_layer_address,
794                        link_layer_address, n_bytes_link_layer_address))
795         goto check_customers;
796
797       clib_memcpy (n->link_layer_address,
798                    link_layer_address, n_bytes_link_layer_address);
799     }
800
801   /* Update time stamp and flags. */
802   n->cpu_time_last_updated = clib_cpu_time_now ();
803   if (is_static)
804     n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
805   else
806     n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
807
808   adj_nbr_walk_nh6 (sw_if_index,
809                     &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
810
811 check_customers:
812   /* Customer(s) waiting for this address to be resolved? */
813   p = mhash_get (&nm->pending_resolutions_by_address, a);
814   if (p)
815     {
816       next_index = p[0];
817
818       while (next_index != (u32) ~ 0)
819         {
820           pr = pool_elt_at_index (nm->pending_resolutions, next_index);
821           vlib_process_signal_event (vm, pr->node_index,
822                                      pr->type_opaque, pr->data);
823           next_index = pr->next_index;
824           pool_put (nm->pending_resolutions, pr);
825         }
826
827       mhash_unset (&nm->pending_resolutions_by_address, a, 0);
828     }
829
830   /* Customer(s) requesting ND event for this address? */
831   p = mhash_get (&nm->mac_changes_by_address, a);
832   if (p)
833     {
834       next_index = p[0];
835
836       while (next_index != (u32) ~ 0)
837         {
838           int (*fp) (u32, u8 *, u32, ip6_address_t *);
839           int rv = 1;
840           mc = pool_elt_at_index (nm->mac_changes, next_index);
841           fp = mc->data_callback;
842
843           /* Call the user's data callback, return 1 to suppress dup events */
844           if (fp)
845             rv =
846               (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
847           /*
848            * Signal the resolver process, as long as the user
849            * says they want to be notified
850            */
851           if (rv == 0)
852             vlib_process_signal_event (vm, mc->node_index,
853                                        mc->type_opaque, mc->data);
854           next_index = mc->next_index;
855         }
856     }
857
858   return 0;
859 }
860
861 int
862 vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
863                                   u32 sw_if_index,
864                                   ip6_address_t * a,
865                                   u8 * link_layer_address,
866                                   uword n_bytes_link_layer_address)
867 {
868   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
869   ip6_neighbor_key_t k;
870   ip6_neighbor_t *n;
871   uword *p;
872   int rv = 0;
873
874   if (vlib_get_thread_index ())
875     {
876       set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
877                                   0 /* unset */ , 0, 0);
878       return 0;
879     }
880
881   k.sw_if_index = sw_if_index;
882   k.ip6_address = a[0];
883   k.pad = 0;
884
885   p = mhash_get (&nm->neighbor_index_by_key, &k);
886   if (p == 0)
887     {
888       rv = -1;
889       goto out;
890     }
891
892   n = pool_elt_at_index (nm->neighbor_pool, p[0]);
893   mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
894
895   adj_nbr_walk_nh6 (sw_if_index,
896                     &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
897
898
899   if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
900     {
901       fib_prefix_t pfx = {
902         .fp_len = 128,
903         .fp_proto = FIB_PROTOCOL_IP6,
904         .fp_addr.ip6 = n->key.ip6_address,
905       };
906       fib_table_entry_path_remove
907         (ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index),
908          &pfx,
909          FIB_SOURCE_ADJ,
910          DPO_PROTO_IP6,
911          &pfx.fp_addr, n->key.sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
912     }
913   pool_put (nm->neighbor_pool, n);
914
915 out:
916   return rv;
917 }
918
919 static void ip6_neighbor_set_unset_rpc_callback
920   (ip6_neighbor_set_unset_rpc_args_t * a)
921 {
922   vlib_main_t *vm = vlib_get_main ();
923   if (a->is_add)
924     vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
925                                     a->link_layer_address, 6, a->is_static,
926                                     a->is_no_fib_entry);
927   else
928     vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
929                                       a->link_layer_address, 6);
930 }
931
932 static int
933 ip6_neighbor_sort (void *a1, void *a2)
934 {
935   vnet_main_t *vnm = vnet_get_main ();
936   ip6_neighbor_t *n1 = a1, *n2 = a2;
937   int cmp;
938   cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
939                                    n2->key.sw_if_index);
940   if (!cmp)
941     cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
942   return cmp;
943 }
944
945 ip6_neighbor_t *
946 ip6_neighbors_entries (u32 sw_if_index)
947 {
948   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
949   ip6_neighbor_t *n, *ns = 0;
950
951   /* *INDENT-OFF* */
952   pool_foreach (n, nm->neighbor_pool,
953   ({
954     if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
955       continue;
956     vec_add1 (ns, n[0]);
957   }));
958   /* *INDENT-ON* */
959
960   if (ns)
961     vec_sort_with_function (ns, ip6_neighbor_sort);
962   return ns;
963 }
964
965 static clib_error_t *
966 show_ip6_neighbors (vlib_main_t * vm,
967                     unformat_input_t * input, vlib_cli_command_t * cmd)
968 {
969   vnet_main_t *vnm = vnet_get_main ();
970   ip6_neighbor_t *n, *ns;
971   clib_error_t *error = 0;
972   u32 sw_if_index;
973
974   /* Filter entries by interface if given. */
975   sw_if_index = ~0;
976   (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
977
978   ns = ip6_neighbors_entries (sw_if_index);
979   if (ns)
980     {
981       vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
982       vec_foreach (n, ns)
983       {
984         vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
985       }
986       vec_free (ns);
987     }
988
989   return error;
990 }
991
992 /*?
993  * This command is used to display the adjacent IPv6 hosts found via
994  * neighbor discovery. Optionally, limit the output to the specified
995  * interface.
996  *
997  * @cliexpar
998  * Example of how to display the IPv6 neighbor adjacency table:
999  * @cliexstart{show ip6 neighbors}
1000  *     Time           Address       Flags     Link layer                     Interface
1001  *      34.0910     ::a:1:1:0:7            02:fe:6a:07:39:6f                GigabitEthernet2/0/0
1002  *     173.2916     ::b:5:1:c:2            02:fe:50:62:3a:94                GigabitEthernet2/0/0
1003  *     886.6654     ::1:1:c:0:9       S    02:fe:e4:45:27:5b                GigabitEthernet3/0/0
1004  * @cliexend
1005  * Example of how to display the IPv6 neighbor adjacency table for given interface:
1006  * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1007  *     Time           Address       Flags     Link layer                     Interface
1008  *      34.0910     ::a:1:1:0:7            02:fe:6a:07:39:6f                GigabitEthernet2/0/0
1009  *     173.2916     ::b:5:1:c:2            02:fe:50:62:3a:94                GigabitEthernet2/0/0
1010  * @cliexend
1011 ?*/
1012 /* *INDENT-OFF* */
1013 VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1014   .path = "show ip6 neighbors",
1015   .function = show_ip6_neighbors,
1016   .short_help = "show ip6 neighbors [<interface>]",
1017 };
1018 /* *INDENT-ON* */
1019
1020 static clib_error_t *
1021 set_ip6_neighbor (vlib_main_t * vm,
1022                   unformat_input_t * input, vlib_cli_command_t * cmd)
1023 {
1024   vnet_main_t *vnm = vnet_get_main ();
1025   ip6_address_t addr;
1026   u8 mac_address[6];
1027   int addr_valid = 0;
1028   int is_del = 0;
1029   int is_static = 0;
1030   int is_no_fib_entry = 0;
1031   u32 sw_if_index;
1032
1033   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1034     {
1035       /* intfc, ip6-address, mac-address */
1036       if (unformat (input, "%U %U %U",
1037                     unformat_vnet_sw_interface, vnm, &sw_if_index,
1038                     unformat_ip6_address, &addr,
1039                     unformat_ethernet_address, mac_address))
1040         addr_valid = 1;
1041
1042       else if (unformat (input, "delete") || unformat (input, "del"))
1043         is_del = 1;
1044       else if (unformat (input, "static"))
1045         is_static = 1;
1046       else if (unformat (input, "no-fib-entry"))
1047         is_no_fib_entry = 1;
1048       else
1049         break;
1050     }
1051
1052   if (!addr_valid)
1053     return clib_error_return (0, "Missing interface, ip6 or hw address");
1054
1055   if (!is_del)
1056     vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
1057                                     mac_address, sizeof (mac_address),
1058                                     is_static, is_no_fib_entry);
1059   else
1060     vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
1061                                       mac_address, sizeof (mac_address));
1062   return 0;
1063 }
1064
1065 /*?
1066  * This command is used to manually add an entry to the IPv6 neighbor
1067  * adjacency table. Optionally, the entry can be added as static. It is
1068  * also used to remove an entry from the table. Use the '<em>show ip6
1069  * neighbors</em>' command to display all learned and manually entered entries.
1070  *
1071  * @cliexpar
1072  * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1073  * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1074  * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1075  * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1076 ?*/
1077 /* *INDENT-OFF* */
1078 VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1079 {
1080   .path = "set ip6 neighbor",
1081   .function = set_ip6_neighbor,
1082   .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
1083 };
1084 /* *INDENT-ON* */
1085
1086 typedef enum
1087 {
1088   ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1089   ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1090   ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1091 } icmp6_neighbor_solicitation_or_advertisement_next_t;
1092
1093 static_always_inline uword
1094 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1095                                               vlib_node_runtime_t * node,
1096                                               vlib_frame_t * frame,
1097                                               uword is_solicitation)
1098 {
1099   vnet_main_t *vnm = vnet_get_main ();
1100   ip6_main_t *im = &ip6_main;
1101   uword n_packets = frame->n_vectors;
1102   u32 *from, *to_next;
1103   u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1104   icmp6_neighbor_discovery_option_type_t option_type;
1105   vlib_node_runtime_t *error_node =
1106     vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1107   int bogus_length;
1108
1109   from = vlib_frame_vector_args (frame);
1110   n_left_from = n_packets;
1111   next_index = node->cached_next_index;
1112
1113   if (node->flags & VLIB_NODE_FLAG_TRACE)
1114     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1115                                    /* stride */ 1,
1116                                    sizeof (icmp6_input_trace_t));
1117
1118   option_type =
1119     (is_solicitation
1120      ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1121      : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1122   n_advertisements_sent = 0;
1123
1124   while (n_left_from > 0)
1125     {
1126       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1127
1128       while (n_left_from > 0 && n_left_to_next > 0)
1129         {
1130           vlib_buffer_t *p0;
1131           ip6_header_t *ip0;
1132           icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1133           icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1134           u32 bi0, options_len0, sw_if_index0, next0, error0;
1135           u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1136           int is_rewrite0;
1137           u32 ni0;
1138
1139           bi0 = to_next[0] = from[0];
1140
1141           from += 1;
1142           to_next += 1;
1143           n_left_from -= 1;
1144           n_left_to_next -= 1;
1145
1146           p0 = vlib_get_buffer (vm, bi0);
1147           ip0 = vlib_buffer_get_current (p0);
1148           h0 = ip6_next_header (ip0);
1149           options_len0 =
1150             clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1151
1152           error0 = ICMP6_ERROR_NONE;
1153           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1154           ip6_sadd_link_local =
1155             ip6_address_is_link_local_unicast (&ip0->src_address);
1156           ip6_sadd_unspecified =
1157             ip6_address_is_unspecified (&ip0->src_address);
1158
1159           /* Check that source address is unspecified, link-local or else on-link. */
1160           if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1161             {
1162               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1163
1164               if (ADJ_INDEX_INVALID != src_adj_index0)
1165                 {
1166                   ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1167
1168                   /* Allow all realistic-looking rewrite adjacencies to pass */
1169                   ni0 = adj0->lookup_next_index;
1170                   is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1171                     (ni0 < IP6_LOOKUP_N_NEXT);
1172
1173                   error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1174                              || !is_rewrite0)
1175                             ?
1176                             ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1177                             : error0);
1178                 }
1179               else
1180                 {
1181                   error0 =
1182                     ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1183                 }
1184             }
1185
1186           o0 = (void *) (h0 + 1);
1187           o0 = ((options_len0 == 8 && o0->header.type == option_type
1188                  && o0->header.n_data_u64s == 1) ? o0 : 0);
1189
1190           /* If src address unspecified or link local, donot learn neighbor MAC */
1191           if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1192                             !ip6_sadd_unspecified))
1193             {
1194               ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1195               if (nm->limit_neighbor_cache_size &&
1196                   pool_elts (nm->neighbor_pool) >=
1197                   nm->limit_neighbor_cache_size)
1198                 unset_random_neighbor_entry ();
1199               vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1200                                               is_solicitation ?
1201                                               &ip0->src_address :
1202                                               &h0->target_address,
1203                                               o0->ethernet_address,
1204                                               sizeof (o0->ethernet_address),
1205                                               0, 0);
1206             }
1207
1208           if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1209             {
1210               /* Check that target address is local to this router. */
1211               fib_node_index_t fei;
1212               u32 fib_index;
1213
1214               fib_index =
1215                 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
1216
1217               if (~0 == fib_index)
1218                 {
1219                   error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1220                 }
1221               else
1222                 {
1223                   if (ip6_address_is_link_local_unicast (&h0->target_address))
1224                     {
1225                       fei = ip6_fib_table_lookup_exact_match
1226                         (ip6_ll_fib_get (sw_if_index0),
1227                          &h0->target_address, 128);
1228                     }
1229                   else
1230                     {
1231                       fei = ip6_fib_table_lookup_exact_match (fib_index,
1232                                                               &h0->target_address,
1233                                                               128);
1234                     }
1235
1236                   if (FIB_NODE_INDEX_INVALID == fei)
1237                     {
1238                       /* The target address is not in the FIB */
1239                       error0 =
1240                         ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1241                     }
1242                   else
1243                     {
1244                       if (FIB_ENTRY_FLAG_LOCAL &
1245                           fib_entry_get_flags_for_source (fei,
1246                                                           FIB_SOURCE_INTERFACE))
1247                         {
1248                           /* It's an address that belongs to one of our interfaces
1249                            * that's good. */
1250                         }
1251                       else
1252                         if (fib_entry_is_sourced
1253                             (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1254                             fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
1255                         {
1256                           /* The address was added by IPv6 Proxy ND config.
1257                            * We should only respond to these if the NS arrived on
1258                            * the link that has a matching covering prefix */
1259                         }
1260                       else
1261                         {
1262                           error0 =
1263                             ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1264                         }
1265                     }
1266                 }
1267             }
1268
1269           if (is_solicitation)
1270             next0 = (error0 != ICMP6_ERROR_NONE
1271                      ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1272                      : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
1273           else
1274             {
1275               next0 = 0;
1276               error0 = error0 == ICMP6_ERROR_NONE ?
1277                 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
1278             }
1279
1280           if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1281             {
1282               vnet_sw_interface_t *sw_if0;
1283               ethernet_interface_t *eth_if0;
1284               ethernet_header_t *eth0;
1285
1286               /* dst address is either source address or the all-nodes mcast addr */
1287               if (!ip6_sadd_unspecified)
1288                 ip0->dst_address = ip0->src_address;
1289               else
1290                 ip6_set_reserved_multicast_address (&ip0->dst_address,
1291                                                     IP6_MULTICAST_SCOPE_link_local,
1292                                                     IP6_MULTICAST_GROUP_ID_all_hosts);
1293
1294               ip0->src_address = h0->target_address;
1295               ip0->hop_limit = 255;
1296               h0->icmp.type = ICMP6_neighbor_advertisement;
1297
1298               sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1299               ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1300               eth_if0 =
1301                 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
1302               if (eth_if0 && o0)
1303                 {
1304                   clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1305                   o0->header.type =
1306                     ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
1307                 }
1308
1309               h0->advertisement_flags = clib_host_to_net_u32
1310                 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1311                  | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1312
1313               h0->icmp.checksum = 0;
1314               h0->icmp.checksum =
1315                 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1316                                                    &bogus_length);
1317               ASSERT (bogus_length == 0);
1318
1319               /* Reuse current MAC header, copy SMAC to DMAC and
1320                * interface MAC to SMAC */
1321               vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1322               eth0 = vlib_buffer_get_current (p0);
1323               clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1324               if (eth_if0)
1325                 clib_memcpy (eth0->src_address, eth_if0->address, 6);
1326
1327               /* Setup input and output sw_if_index for packet */
1328               ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1329               vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1330               vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1331                 vnet_main.local_interface_sw_if_index;
1332
1333               n_advertisements_sent++;
1334             }
1335
1336           p0->error = error_node->errors[error0];
1337
1338           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1339                                            to_next, n_left_to_next,
1340                                            bi0, next0);
1341         }
1342
1343       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1344     }
1345
1346   /* Account for advertisements sent. */
1347   vlib_error_count (vm, error_node->node_index,
1348                     ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1349                     n_advertisements_sent);
1350
1351   return frame->n_vectors;
1352 }
1353
1354 /* for "syslogging" - use elog for now */
1355 #define foreach_log_level            \
1356   _ (DEBUG, "DEBUG")                         \
1357   _ (INFO, "INFORMATION")            \
1358   _ (NOTICE, "NOTICE")               \
1359   _ (WARNING, "WARNING")             \
1360   _ (ERR, "ERROR")                                    \
1361   _ (CRIT, "CRITICAL")                        \
1362   _ (ALERT, "ALERT")                          \
1363   _ (EMERG,  "EMERGENCY")
1364
1365 typedef enum
1366 {
1367 #define _(f,s) LOG_##f,
1368   foreach_log_level
1369 #undef _
1370 } log_level_t;
1371
1372 static char *log_level_strings[] = {
1373 #define _(f,s) s,
1374   foreach_log_level
1375 #undef _
1376 };
1377
1378 static int logmask = 1 << LOG_DEBUG;
1379
1380 static void
1381 ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
1382 {
1383   /* just use elog for now */
1384   u8 *what;
1385   va_list va;
1386
1387   if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1388     return;
1389
1390   va_start (va, fmt);
1391   if (fmt)
1392     {
1393       what = va_format (0, fmt, &va);
1394
1395       ELOG_TYPE_DECLARE (e) =
1396       {
1397       .format = "ip6 nd:  (%s): %s",.format_args = "T4T4",};
1398       struct
1399       {
1400         u32 s[2];
1401       } *ed;
1402       ed = ELOG_DATA (&vm->elog_main, e);
1403       ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1404       ed->s[1] = elog_string (&vm->elog_main, (char *) what);
1405     }
1406   va_end (va);
1407   return;
1408 }
1409
1410 /* ipv6 neighbor discovery - router advertisements */
1411 typedef enum
1412 {
1413   ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1414   ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1415   ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1416   ICMP6_ROUTER_SOLICITATION_N_NEXT,
1417 } icmp6_router_solicitation_or_advertisement_next_t;
1418
1419 static_always_inline uword
1420 icmp6_router_solicitation (vlib_main_t * vm,
1421                            vlib_node_runtime_t * node, vlib_frame_t * frame)
1422 {
1423   vnet_main_t *vnm = vnet_get_main ();
1424   ip6_main_t *im = &ip6_main;
1425   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1426   uword n_packets = frame->n_vectors;
1427   u32 *from, *to_next;
1428   u32 n_left_from, n_left_to_next, next_index;
1429   u32 n_advertisements_sent = 0;
1430   int bogus_length;
1431
1432   icmp6_neighbor_discovery_option_type_t option_type;
1433
1434   vlib_node_runtime_t *error_node =
1435     vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1436
1437   from = vlib_frame_vector_args (frame);
1438   n_left_from = n_packets;
1439   next_index = node->cached_next_index;
1440
1441   if (node->flags & VLIB_NODE_FLAG_TRACE)
1442     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1443                                    /* stride */ 1,
1444                                    sizeof (icmp6_input_trace_t));
1445
1446   /* source may append his LL address */
1447   option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1448
1449   while (n_left_from > 0)
1450     {
1451       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1452
1453       while (n_left_from > 0 && n_left_to_next > 0)
1454         {
1455           vlib_buffer_t *p0;
1456           ip6_header_t *ip0;
1457           ip6_radv_t *radv_info = 0;
1458
1459           icmp6_neighbor_discovery_header_t *h0;
1460           icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1461
1462           u32 bi0, options_len0, sw_if_index0, next0, error0;
1463           u32 is_solicitation = 1, is_dropped = 0;
1464           u32 is_unspecified, is_link_local;
1465
1466           bi0 = to_next[0] = from[0];
1467
1468           from += 1;
1469           to_next += 1;
1470           n_left_from -= 1;
1471           n_left_to_next -= 1;
1472
1473           p0 = vlib_get_buffer (vm, bi0);
1474           ip0 = vlib_buffer_get_current (p0);
1475           h0 = ip6_next_header (ip0);
1476           options_len0 =
1477             clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1478           is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1479           is_link_local =
1480             ip6_address_is_link_local_unicast (&ip0->src_address);
1481
1482           error0 = ICMP6_ERROR_NONE;
1483           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1484
1485           /* check if solicitation  (not from nd_timer node) */
1486           if (ip6_address_is_unspecified (&ip0->dst_address))
1487             is_solicitation = 0;
1488
1489           /* Check that source address is unspecified, link-local or else on-link. */
1490           if (!is_unspecified && !is_link_local)
1491             {
1492               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1493
1494               if (ADJ_INDEX_INVALID != src_adj_index0)
1495                 {
1496                   ip_adjacency_t *adj0 = adj_get (src_adj_index0);
1497
1498                   error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1499                             ?
1500                             ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1501                             : error0);
1502                 }
1503               else
1504                 {
1505                   error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1506                 }
1507             }
1508
1509           /* check for source LL option and process */
1510           o0 = (void *) (h0 + 1);
1511           o0 = ((options_len0 == 8
1512                  && o0->header.type == option_type
1513                  && o0->header.n_data_u64s == 1) ? o0 : 0);
1514
1515           /* if src address unspecified IGNORE any options */
1516           if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1517                             !is_unspecified && !is_link_local))
1518             {
1519               ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1520               if (nm->limit_neighbor_cache_size &&
1521                   pool_elts (nm->neighbor_pool) >=
1522                   nm->limit_neighbor_cache_size)
1523                 unset_random_neighbor_entry ();
1524
1525               vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1526                                               &ip0->src_address,
1527                                               o0->ethernet_address,
1528                                               sizeof (o0->ethernet_address),
1529                                               0, 0);
1530             }
1531
1532           /* default is to drop */
1533           next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1534
1535           if (error0 == ICMP6_ERROR_NONE)
1536             {
1537               vnet_sw_interface_t *sw_if0;
1538               ethernet_interface_t *eth_if0;
1539               u32 adj_index0;
1540
1541               sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1542               ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1543               eth_if0 =
1544                 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
1545
1546               /* only support ethernet interface type for now */
1547               error0 =
1548                 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1549                 : error0;
1550
1551               if (error0 == ICMP6_ERROR_NONE)
1552                 {
1553                   u32 ri;
1554
1555                   /* adjust the sizeof the buffer to just include the ipv6 header */
1556                   p0->current_length -=
1557                     (options_len0 +
1558                      sizeof (icmp6_neighbor_discovery_header_t));
1559
1560                   /* look up the radv_t information for this interface */
1561                   if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1562                       sw_if_index0)
1563                     {
1564                       ri =
1565                         nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1566
1567                       if (ri != ~0)
1568                         radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1569                     }
1570
1571                   error0 =
1572                     ((!radv_info) ?
1573                      ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1574                      error0);
1575
1576                   if (error0 == ICMP6_ERROR_NONE)
1577                     {
1578                       f64 now = vlib_time_now (vm);
1579
1580                       /* for solicited adverts - need to rate limit */
1581                       if (is_solicitation)
1582                         {
1583                           if (0 != radv_info->last_radv_time &&
1584                               (now - radv_info->last_radv_time) <
1585                               MIN_DELAY_BETWEEN_RAS)
1586                             is_dropped = 1;
1587                           else
1588                             radv_info->last_radv_time = now;
1589                         }
1590
1591                       /* send now  */
1592                       icmp6_router_advertisement_header_t rh;
1593
1594                       rh.icmp.type = ICMP6_router_advertisement;
1595                       rh.icmp.code = 0;
1596                       rh.icmp.checksum = 0;
1597
1598                       rh.current_hop_limit = radv_info->curr_hop_limit;
1599                       rh.router_lifetime_in_sec =
1600                         clib_host_to_net_u16
1601                         (radv_info->adv_router_lifetime_in_sec);
1602                       rh.
1603                         time_in_msec_between_retransmitted_neighbor_solicitations
1604                         =
1605                         clib_host_to_net_u32 (radv_info->
1606                                               adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1607                       rh.neighbor_reachable_time_in_msec =
1608                         clib_host_to_net_u32 (radv_info->
1609                                               adv_neighbor_reachable_time_in_msec);
1610
1611                       rh.flags =
1612                         (radv_info->adv_managed_flag) ?
1613                         ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1614                         0;
1615                       rh.flags |=
1616                         ((radv_info->adv_other_flag) ?
1617                          ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1618                          0);
1619
1620
1621                       u16 payload_length =
1622                         sizeof (icmp6_router_advertisement_header_t);
1623
1624                       vlib_buffer_add_data (vm,
1625                                             vlib_buffer_get_free_list_index
1626                                             (p0), bi0, (void *) &rh,
1627                                             sizeof
1628                                             (icmp6_router_advertisement_header_t));
1629
1630                       if (radv_info->adv_link_layer_address)
1631                         {
1632                           icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1633                             h;
1634
1635                           h.header.type =
1636                             ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1637                           h.header.n_data_u64s = 1;
1638
1639                           /* copy ll address */
1640                           clib_memcpy (&h.ethernet_address[0],
1641                                        eth_if0->address, 6);
1642
1643                           vlib_buffer_add_data (vm,
1644                                                 vlib_buffer_get_free_list_index
1645                                                 (p0), bi0, (void *) &h,
1646                                                 sizeof
1647                                                 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
1648
1649                           payload_length +=
1650                             sizeof
1651                             (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
1652                         }
1653
1654                       /* add MTU option */
1655                       if (radv_info->adv_link_mtu)
1656                         {
1657                           icmp6_neighbor_discovery_mtu_option_t h;
1658
1659                           h.unused = 0;
1660                           h.mtu =
1661                             clib_host_to_net_u32 (radv_info->adv_link_mtu);
1662                           h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1663                           h.header.n_data_u64s = 1;
1664
1665                           payload_length +=
1666                             sizeof (icmp6_neighbor_discovery_mtu_option_t);
1667
1668                           vlib_buffer_add_data (vm,
1669                                                 vlib_buffer_get_free_list_index
1670                                                 (p0), bi0, (void *) &h,
1671                                                 sizeof
1672                                                 (icmp6_neighbor_discovery_mtu_option_t));
1673                         }
1674
1675                       /* add advertised prefix options  */
1676                       ip6_radv_prefix_t *pr_info;
1677
1678                       /* *INDENT-OFF* */
1679                       pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1680                       ({
1681                         if(pr_info->enabled &&
1682                            (!pr_info->decrement_lifetime_flag
1683                             || (pr_info->pref_lifetime_expires >0)))
1684                           {
1685                             /* advertise this prefix */
1686                             icmp6_neighbor_discovery_prefix_information_option_t h;
1687
1688                             h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1689                             h.header.n_data_u64s  =  (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
1690
1691                             h.dst_address_length  = pr_info->prefix_len;
1692
1693                             h.flags  = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1694                             h.flags |= (pr_info->adv_autonomous_flag) ?  ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO :  0;
1695
1696                             if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1697                               {
1698                                 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1699                                 h.preferred_time  = 0;
1700                               }
1701                             else
1702                               {
1703                                 if(pr_info->decrement_lifetime_flag)
1704                                   {
1705                                     pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires  > now)) ?
1706                                       (pr_info->valid_lifetime_expires  - now) : 0;
1707
1708                                     pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires  > now)) ?
1709                                       (pr_info->pref_lifetime_expires  - now) : 0;
1710                                   }
1711
1712                                 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1713                                 h.preferred_time  = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1714                               }
1715                             h.unused  = 0;
1716
1717                             clib_memcpy(&h.dst_address, &pr_info->prefix,  sizeof(ip6_address_t));
1718
1719                             payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1720
1721                             vlib_buffer_add_data (vm,
1722                                             vlib_buffer_get_free_list_index (p0),
1723                                                   bi0,
1724                                                   (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1725
1726                           }
1727                       }));
1728                       /* *INDENT-ON* */
1729
1730                       /* add additional options before here */
1731
1732                       /* finish building the router advertisement... */
1733                       if (!is_unspecified && radv_info->send_unicast)
1734                         {
1735                           ip0->dst_address = ip0->src_address;
1736                         }
1737                       else
1738                         {
1739                           /* target address is all-nodes mcast addr */
1740                           ip6_set_reserved_multicast_address
1741                             (&ip0->dst_address,
1742                              IP6_MULTICAST_SCOPE_link_local,
1743                              IP6_MULTICAST_GROUP_ID_all_hosts);
1744                         }
1745
1746                       /* source address MUST be the link-local address */
1747                       ip0->src_address = radv_info->link_local_address;
1748
1749                       ip0->hop_limit = 255;
1750                       ip0->payload_length =
1751                         clib_host_to_net_u16 (payload_length);
1752
1753                       icmp6_router_advertisement_header_t *rh0 =
1754                         (icmp6_router_advertisement_header_t *) (ip0 + 1);
1755                       rh0->icmp.checksum =
1756                         ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1757                                                            &bogus_length);
1758                       ASSERT (bogus_length == 0);
1759
1760                       /* setup output if and adjacency */
1761                       vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1762                         vnet_main.local_interface_sw_if_index;
1763
1764                       if (is_solicitation)
1765                         {
1766                           ethernet_header_t *eth0;
1767                           /* Reuse current MAC header, copy SMAC to DMAC and
1768                            * interface MAC to SMAC */
1769                           vlib_buffer_reset (p0);
1770                           eth0 = vlib_buffer_get_current (p0);
1771                           clib_memcpy (eth0->dst_address, eth0->src_address,
1772                                        6);
1773                           clib_memcpy (eth0->src_address, eth_if0->address,
1774                                        6);
1775                           next0 =
1776                             is_dropped ? next0 :
1777                             ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1778                           vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1779                             sw_if_index0;
1780                         }
1781                       else
1782                         {
1783                           adj_index0 = radv_info->mcast_adj_index;
1784                           if (adj_index0 == 0)
1785                             error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1786                           else
1787                             {
1788                               next0 =
1789                                 is_dropped ? next0 :
1790                                 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1791                               vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1792                                 adj_index0;
1793                             }
1794                         }
1795                       p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1796
1797                       radv_info->n_solicitations_dropped += is_dropped;
1798                       radv_info->n_solicitations_rcvd += is_solicitation;
1799
1800                       if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
1801                         {
1802                           radv_info->n_advertisements_sent++;
1803                           n_advertisements_sent++;
1804                         }
1805                     }
1806                 }
1807             }
1808
1809           p0->error = error_node->errors[error0];
1810
1811           if (error0 != ICMP6_ERROR_NONE)
1812             vlib_error_count (vm, error_node->node_index, error0, 1);
1813
1814           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1815                                            to_next, n_left_to_next,
1816                                            bi0, next0);
1817
1818         }
1819
1820       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1821     }
1822
1823   /* Account for router advertisements sent. */
1824   vlib_error_count (vm, error_node->node_index,
1825                     ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1826                     n_advertisements_sent);
1827
1828   return frame->n_vectors;
1829 }
1830
1831  /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always  be dropped  */
1832 static_always_inline uword
1833 icmp6_router_advertisement (vlib_main_t * vm,
1834                             vlib_node_runtime_t * node, vlib_frame_t * frame)
1835 {
1836   vnet_main_t *vnm = vnet_get_main ();
1837   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1838   uword n_packets = frame->n_vectors;
1839   u32 *from, *to_next;
1840   u32 n_left_from, n_left_to_next, next_index;
1841   u32 n_advertisements_rcvd = 0;
1842
1843   vlib_node_runtime_t *error_node =
1844     vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
1845
1846   from = vlib_frame_vector_args (frame);
1847   n_left_from = n_packets;
1848   next_index = node->cached_next_index;
1849
1850   if (node->flags & VLIB_NODE_FLAG_TRACE)
1851     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1852                                    /* stride */ 1,
1853                                    sizeof (icmp6_input_trace_t));
1854
1855   while (n_left_from > 0)
1856     {
1857       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1858
1859       while (n_left_from > 0 && n_left_to_next > 0)
1860         {
1861           vlib_buffer_t *p0;
1862           ip6_header_t *ip0;
1863           ip6_radv_t *radv_info = 0;
1864           icmp6_router_advertisement_header_t *h0;
1865           u32 bi0, options_len0, sw_if_index0, next0, error0;
1866
1867           bi0 = to_next[0] = from[0];
1868
1869           from += 1;
1870           to_next += 1;
1871           n_left_from -= 1;
1872           n_left_to_next -= 1;
1873
1874           p0 = vlib_get_buffer (vm, bi0);
1875           ip0 = vlib_buffer_get_current (p0);
1876           h0 = ip6_next_header (ip0);
1877           options_len0 =
1878             clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1879
1880           error0 = ICMP6_ERROR_NONE;
1881           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1882
1883           /* Check that source address is link-local */
1884           error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
1885             ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1886
1887           /* default is to drop */
1888           next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
1889
1890           n_advertisements_rcvd++;
1891
1892           if (error0 == ICMP6_ERROR_NONE)
1893             {
1894               vnet_sw_interface_t *sw_if0;
1895               ethernet_interface_t *eth_if0;
1896
1897               sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1898               ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
1899               eth_if0 =
1900                 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
1901
1902               /* only support ethernet interface type for now */
1903               error0 =
1904                 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1905                 : error0;
1906
1907               if (error0 == ICMP6_ERROR_NONE)
1908                 {
1909                   u32 ri;
1910
1911                   /* look up the radv_t information for this interface */
1912                   if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1913                       sw_if_index0)
1914                     {
1915                       ri =
1916                         nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1917
1918                       if (ri != ~0)
1919                         radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1920                     }
1921
1922                   error0 =
1923                     ((!radv_info) ?
1924                      ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1925                      error0);
1926
1927                   if (error0 == ICMP6_ERROR_NONE)
1928                     {
1929                       radv_info->keep_sending_rs = 0;
1930
1931                       ra_report_t r;
1932
1933                       r.sw_if_index = sw_if_index0;
1934                       memcpy (r.router_address, &ip0->src_address, 16);
1935                       r.current_hop_limit = h0->current_hop_limit;
1936                       r.flags = h0->flags;
1937                       r.router_lifetime_in_sec =
1938                         clib_net_to_host_u16 (h0->router_lifetime_in_sec);
1939                       r.neighbor_reachable_time_in_msec =
1940                         clib_net_to_host_u32
1941                         (h0->neighbor_reachable_time_in_msec);
1942                       r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
1943                       r.prefixes = 0;
1944
1945                       /* validate advertised information */
1946                       if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1947                           && (h0->current_hop_limit !=
1948                               radv_info->curr_hop_limit))
1949                         {
1950                           ip6_neighbor_syslog (vm, LOG_WARNING,
1951                                                "our AdvCurHopLimit on %U doesn't agree with %U",
1952                                                format_vnet_sw_if_index_name,
1953                                                vnm, sw_if_index0,
1954                                                format_ip6_address,
1955                                                &ip0->src_address);
1956                         }
1957
1958                       if ((h0->flags &
1959                            ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1960                           != radv_info->adv_managed_flag)
1961                         {
1962                           ip6_neighbor_syslog (vm, LOG_WARNING,
1963                                                "our AdvManagedFlag on %U doesn't agree with %U",
1964                                                format_vnet_sw_if_index_name,
1965                                                vnm, sw_if_index0,
1966                                                format_ip6_address,
1967                                                &ip0->src_address);
1968                         }
1969
1970                       if ((h0->flags &
1971                            ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1972                           != radv_info->adv_other_flag)
1973                         {
1974                           ip6_neighbor_syslog (vm, LOG_WARNING,
1975                                                "our AdvOtherConfigFlag on %U doesn't agree with %U",
1976                                                format_vnet_sw_if_index_name,
1977                                                vnm, sw_if_index0,
1978                                                format_ip6_address,
1979                                                &ip0->src_address);
1980                         }
1981
1982                       if ((h0->
1983                            time_in_msec_between_retransmitted_neighbor_solicitations
1984                            && radv_info->
1985                            adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1986                           && (h0->
1987                               time_in_msec_between_retransmitted_neighbor_solicitations
1988                               !=
1989                               clib_host_to_net_u32 (radv_info->
1990                                                     adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
1991                         {
1992                           ip6_neighbor_syslog (vm, LOG_WARNING,
1993                                                "our AdvRetransTimer on %U doesn't agree with %U",
1994                                                format_vnet_sw_if_index_name,
1995                                                vnm, sw_if_index0,
1996                                                format_ip6_address,
1997                                                &ip0->src_address);
1998                         }
1999
2000                       if ((h0->neighbor_reachable_time_in_msec &&
2001                            radv_info->adv_neighbor_reachable_time_in_msec) &&
2002                           (h0->neighbor_reachable_time_in_msec !=
2003                            clib_host_to_net_u32
2004                            (radv_info->adv_neighbor_reachable_time_in_msec)))
2005                         {
2006                           ip6_neighbor_syslog (vm, LOG_WARNING,
2007                                                "our AdvReachableTime on %U doesn't agree with %U",
2008                                                format_vnet_sw_if_index_name,
2009                                                vnm, sw_if_index0,
2010                                                format_ip6_address,
2011                                                &ip0->src_address);
2012                         }
2013
2014                       /* check for MTU or prefix options or .. */
2015                       u8 *opt_hdr = (u8 *) (h0 + 1);
2016                       while (options_len0 > 0)
2017                         {
2018                           icmp6_neighbor_discovery_option_header_t *o0 =
2019                             (icmp6_neighbor_discovery_option_header_t *)
2020                             opt_hdr;
2021                           int opt_len = o0->n_data_u64s << 3;
2022                           icmp6_neighbor_discovery_option_type_t option_type =
2023                             o0->type;
2024
2025                           if (options_len0 < 2)
2026                             {
2027                               ip6_neighbor_syslog (vm, LOG_ERR,
2028                                                    "malformed RA packet on %U from %U",
2029                                                    format_vnet_sw_if_index_name,
2030                                                    vnm, sw_if_index0,
2031                                                    format_ip6_address,
2032                                                    &ip0->src_address);
2033                               break;
2034                             }
2035
2036                           if (opt_len == 0)
2037                             {
2038                               ip6_neighbor_syslog (vm, LOG_ERR,
2039                                                    " zero length option in RA on %U from %U",
2040                                                    format_vnet_sw_if_index_name,
2041                                                    vnm, sw_if_index0,
2042                                                    format_ip6_address,
2043                                                    &ip0->src_address);
2044                               break;
2045                             }
2046                           else if (opt_len > options_len0)
2047                             {
2048                               ip6_neighbor_syslog (vm, LOG_ERR,
2049                                                    "option length in RA packet  greater than total length on %U from %U",
2050                                                    format_vnet_sw_if_index_name,
2051                                                    vnm, sw_if_index0,
2052                                                    format_ip6_address,
2053                                                    &ip0->src_address);
2054                               break;
2055                             }
2056
2057                           options_len0 -= opt_len;
2058                           opt_hdr += opt_len;
2059
2060                           switch (option_type)
2061                             {
2062                             case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2063                               {
2064                                 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2065                                   * h =
2066                                   (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2067                                    *) (o0);
2068
2069                                 if (opt_len < sizeof (*h))
2070                                   break;
2071
2072                                 memcpy (r.slla, h->ethernet_address, 6);
2073                               }
2074                               break;
2075
2076                             case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
2077                               {
2078                                 icmp6_neighbor_discovery_mtu_option_t *h =
2079                                   (icmp6_neighbor_discovery_mtu_option_t
2080                                    *) (o0);
2081
2082                                 if (opt_len < sizeof (*h))
2083                                   break;
2084
2085                                 r.mtu = clib_net_to_host_u32 (h->mtu);
2086
2087                                 if ((h->mtu && radv_info->adv_link_mtu) &&
2088                                     (h->mtu !=
2089                                      clib_host_to_net_u32
2090                                      (radv_info->adv_link_mtu)))
2091                                   {
2092                                     ip6_neighbor_syslog (vm, LOG_WARNING,
2093                                                          "our AdvLinkMTU on %U doesn't agree with %U",
2094                                                          format_vnet_sw_if_index_name,
2095                                                          vnm, sw_if_index0,
2096                                                          format_ip6_address,
2097                                                          &ip0->src_address);
2098                                   }
2099                               }
2100                               break;
2101
2102                             case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2103                               {
2104                                 icmp6_neighbor_discovery_prefix_information_option_t
2105                                   * h =
2106                                   (icmp6_neighbor_discovery_prefix_information_option_t
2107                                    *) (o0);
2108
2109                                 /* validate advertised prefix options  */
2110                                 ip6_radv_prefix_t *pr_info;
2111                                 u32 preferred, valid;
2112
2113                                 if (opt_len < sizeof (*h))
2114                                   break;
2115
2116                                 vec_validate (r.prefixes,
2117                                               vec_len (r.prefixes));
2118                                 ra_report_prefix_info_t *prefix =
2119                                   vec_elt_at_index (r.prefixes,
2120                                                     vec_len (r.prefixes) - 1);
2121
2122                                 preferred =
2123                                   clib_net_to_host_u32 (h->preferred_time);
2124                                 valid = clib_net_to_host_u32 (h->valid_time);
2125
2126                                 prefix->preferred_time = preferred;
2127                                 prefix->valid_time = valid;
2128                                 prefix->flags = h->flags & 0xc0;
2129                                 prefix->dst_address_length =
2130                                   h->dst_address_length;
2131                                 prefix->dst_address = h->dst_address;
2132
2133                                 /* look for matching prefix - if we our advertising it, it better be consistant */
2134                                 /* *INDENT-OFF* */
2135                                 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2136                                 ({
2137
2138                                   ip6_address_t mask;
2139                                   ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2140
2141                                   if(pr_info->enabled &&
2142                                      (pr_info->prefix_len == h->dst_address_length) &&
2143                                      ip6_address_is_equal_masked (&pr_info->prefix,  &h->dst_address, &mask))
2144                                     {
2145                                       /* found it */
2146                                       if(!pr_info->decrement_lifetime_flag &&
2147                                          valid != pr_info->adv_valid_lifetime_in_secs)
2148                                         {
2149                                           ip6_neighbor_syslog(vm,  LOG_WARNING,
2150                                                               "our ADV validlifetime on  %U for %U does not  agree with %U",
2151                                                               format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2152                                                               format_ip6_address, &h->dst_address);
2153                                         }
2154                                       if(!pr_info->decrement_lifetime_flag &&
2155                                          preferred != pr_info->adv_pref_lifetime_in_secs)
2156                                         {
2157                                           ip6_neighbor_syslog(vm,  LOG_WARNING,
2158                                                               "our ADV preferredlifetime on  %U for %U does not  agree with %U",
2159                                                               format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2160                                                               format_ip6_address, &h->dst_address);
2161                                         }
2162                                     }
2163                                   break;
2164                                 }));
2165                                 /* *INDENT-ON* */
2166                                 break;
2167                               }
2168                             default:
2169                               /* skip this one */
2170                               break;
2171                             }
2172                         }
2173                       ra_publish (&r);
2174                     }
2175                 }
2176             }
2177
2178           p0->error = error_node->errors[error0];
2179
2180           if (error0 != ICMP6_ERROR_NONE)
2181             vlib_error_count (vm, error_node->node_index, error0, 1);
2182
2183           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2184                                            to_next, n_left_to_next,
2185                                            bi0, next0);
2186         }
2187
2188       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2189     }
2190
2191   /* Account for router advertisements received. */
2192   vlib_error_count (vm, error_node->node_index,
2193                     ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2194                     n_advertisements_rcvd);
2195
2196   return frame->n_vectors;
2197 }
2198
2199 static inline f64
2200 random_f64_from_to (f64 from, f64 to)
2201 {
2202   static u32 seed = 0;
2203   static u8 seed_set = 0;
2204   if (!seed_set)
2205     {
2206       seed = random_default_seed ();
2207       seed_set = 1;
2208     }
2209   return random_f64 (&seed) * (to - from) + from;
2210 }
2211
2212 static inline u8
2213 get_mac_address (u32 sw_if_index, u8 * address)
2214 {
2215   vnet_main_t *vnm = vnet_get_main ();
2216   vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2217   if (!hw_if->hw_address)
2218     return 1;
2219   clib_memcpy (address, hw_if->hw_address, 6);
2220   return 0;
2221 }
2222
2223 static inline vlib_buffer_t *
2224 create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2225 {
2226   u32 bi0;
2227   vlib_buffer_t *p0;
2228   vlib_buffer_free_list_t *fl;
2229   icmp6_router_solicitation_header_t *rh;
2230   u16 payload_length;
2231   int bogus_length;
2232   u32 sw_if_index;
2233
2234   sw_if_index = radv_info->sw_if_index;
2235
2236   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2237     {
2238       clib_warning ("buffer allocation failure");
2239       return 0;
2240     }
2241
2242   p0 = vlib_get_buffer (vm, bi0);
2243   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2244   vlib_buffer_init_for_free_list (p0, fl);
2245   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2246   p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2247
2248   vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2249   vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2250
2251   vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2252
2253   rh = vlib_buffer_get_current (p0);
2254   p0->current_length = sizeof (*rh);
2255
2256   rh->neighbor.icmp.type = ICMP6_router_solicitation;
2257   rh->neighbor.icmp.code = 0;
2258   rh->neighbor.icmp.checksum = 0;
2259   rh->neighbor.reserved_must_be_zero = 0;
2260
2261   rh->link_layer_option.header.type =
2262     ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2263   if (0 != get_mac_address (sw_if_index,
2264                             rh->link_layer_option.ethernet_address))
2265     {
2266       clib_warning ("interface with sw_if_index %u has no mac address",
2267                     sw_if_index);
2268       vlib_buffer_free (vm, &bi0, 1);
2269       return 0;
2270     }
2271   rh->link_layer_option.header.n_data_u64s = 1;
2272
2273   payload_length = sizeof (rh->neighbor) + sizeof (u64);
2274
2275   rh->ip.ip_version_traffic_class_and_flow_label =
2276     clib_host_to_net_u32 (0x6 << 28);
2277   rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2278   rh->ip.protocol = IP_PROTOCOL_ICMP6;
2279   rh->ip.hop_limit = 255;
2280   rh->ip.src_address = radv_info->link_local_address;
2281   /* set address ff02::2 */
2282   rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02L << 48);
2283   rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2284
2285   rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2286                                                                   &rh->ip,
2287                                                                   &bogus_length);
2288
2289   return p0;
2290 }
2291
2292 static inline void
2293 stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2294 {
2295   u32 bi0;
2296
2297   ra->keep_sending_rs = 0;
2298   if (ra->buffer)
2299     {
2300       bi0 = vlib_get_buffer_index (vm, ra->buffer);
2301       vlib_buffer_free (vm, &bi0, 1);
2302       ra->buffer = 0;
2303     }
2304 }
2305
2306 static inline bool
2307 check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2308                f64 * due_time)
2309 {
2310   vlib_buffer_t *p0;
2311   vlib_frame_t *f;
2312   u32 *to_next;
2313   u32 next_index;
2314   vlib_buffer_t *c0;
2315   u32 ci0;
2316
2317   icmp6_send_router_solicitation_params_t *params;
2318
2319   if (!radv_info->keep_sending_rs)
2320     return false;
2321
2322   params = &radv_info->params;
2323
2324   if (radv_info->due_time > current_time)
2325     {
2326       *due_time = radv_info->due_time;
2327       return true;
2328     }
2329
2330   p0 = radv_info->buffer;
2331
2332   next_index = ip6_rewrite_mcast_node.index;
2333
2334   c0 = vlib_buffer_copy (vm, p0);
2335   ci0 = vlib_get_buffer_index (vm, c0);
2336
2337   f = vlib_get_frame_to_node (vm, next_index);
2338   to_next = vlib_frame_vector_args (f);
2339   to_next[0] = ci0;
2340   f->n_vectors = 1;
2341   vlib_put_frame_to_node (vm, next_index, f);
2342
2343   if (params->mrc != 0 && --radv_info->n_left == 0)
2344     stop_sending_rs (vm, radv_info);
2345   else
2346     {
2347       radv_info->sleep_interval =
2348         (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2349       if (radv_info->sleep_interval > params->mrt)
2350         radv_info->sleep_interval =
2351           (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2352
2353       radv_info->due_time = current_time + radv_info->sleep_interval;
2354
2355       if (params->mrd != 0
2356           && current_time > radv_info->start_time + params->mrd)
2357         stop_sending_rs (vm, radv_info);
2358       else
2359         *due_time = radv_info->due_time;
2360     }
2361
2362   return radv_info->keep_sending_rs;
2363 }
2364
2365 static uword
2366 send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2367                  vlib_frame_t * f0)
2368 {
2369   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2370   ip6_radv_t *radv_info;
2371   uword *event_data = 0;
2372   f64 sleep_time = 1e9;
2373   f64 current_time;
2374   f64 due_time;
2375   f64 dt = 0;
2376
2377   while (true)
2378     {
2379       vlib_process_wait_for_event_or_clock (vm, sleep_time);
2380       vlib_process_get_events (vm, &event_data);
2381       vec_reset_length (event_data);
2382
2383       current_time = vlib_time_now (vm);
2384       do
2385         {
2386           due_time = current_time + 1e9;
2387         /* *INDENT-OFF* */
2388         pool_foreach (radv_info, nm->if_radv_pool,
2389         ({
2390             if (check_send_rs (vm, radv_info, current_time, &dt)
2391                 && (dt < due_time))
2392               due_time = dt;
2393         }));
2394         /* *INDENT-ON* */
2395           current_time = vlib_time_now (vm);
2396         }
2397       while (due_time < current_time);
2398
2399       sleep_time = due_time - current_time;
2400     }
2401
2402   return 0;
2403 }
2404
2405 /* *INDENT-OFF* */
2406 VLIB_REGISTER_NODE (send_rs_process_node) = {
2407     .function = send_rs_process,
2408     .type = VLIB_NODE_TYPE_PROCESS,
2409     .name = "send-rs-process",
2410 };
2411 /* *INDENT-ON* */
2412
2413 void
2414 icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2415                                 icmp6_send_router_solicitation_params_t *
2416                                 params)
2417 {
2418   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2419   u32 rai;
2420   ip6_radv_t *ra = 0;
2421
2422   ASSERT (~0 != sw_if_index);
2423
2424   rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2425   ra = pool_elt_at_index (nm->if_radv_pool, rai);
2426
2427   if (stop)
2428     stop_sending_rs (vm, ra);
2429   else
2430     {
2431       ra->keep_sending_rs = 1;
2432       ra->params = *params;
2433       ra->n_left = params->mrc;
2434       ra->start_time = vlib_time_now (vm);
2435       ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2436       ra->due_time = 0;         /* send first packet ASAP */
2437       ra->buffer = create_buffer_for_rs (vm, ra);
2438       if (!ra->buffer)
2439         ra->keep_sending_rs = 0;
2440       else
2441         vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2442     }
2443 }
2444
2445 /**
2446  * @brief Add a multicast Address to the advertised MLD set
2447  */
2448 static void
2449 ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2450 {
2451   ip6_mldp_group_t *mcast_group_info;
2452   uword *p;
2453
2454   /* lookup  mldp info for this interface */
2455   p = mhash_get (&radv_info->address_to_mldp_index, addr);
2456   mcast_group_info =
2457     p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2458
2459   /* add address */
2460   if (!mcast_group_info)
2461     {
2462       /* add */
2463       u32 mi;
2464       pool_get (radv_info->mldp_group_pool, mcast_group_info);
2465
2466       mi = mcast_group_info - radv_info->mldp_group_pool;
2467       mhash_set (&radv_info->address_to_mldp_index, addr, mi,   /* old_value */
2468                  0);
2469
2470       mcast_group_info->type = 4;
2471       mcast_group_info->mcast_source_address_pool = 0;
2472       mcast_group_info->num_sources = 0;
2473       clib_memcpy (&mcast_group_info->mcast_address, addr,
2474                    sizeof (ip6_address_t));
2475     }
2476 }
2477
2478 /**
2479  * @brief Delete a multicast Address from the advertised MLD set
2480  */
2481 static void
2482 ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2483 {
2484   ip6_mldp_group_t *mcast_group_info;
2485   uword *p;
2486
2487   p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2488   mcast_group_info =
2489     p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2490
2491   if (mcast_group_info)
2492     {
2493       mhash_unset (&radv_info->address_to_mldp_index, &addr,
2494                    /* old_value */ 0);
2495       pool_put (radv_info->mldp_group_pool, mcast_group_info);
2496     }
2497 }
2498
2499 /**
2500  * @brief Add a multicast Address to the advertised MLD set
2501  */
2502 static void
2503 ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2504                           ip6_multicast_address_scope_t scope,
2505                           ip6_multicast_link_local_group_id_t group)
2506 {
2507   ip6_address_t addr;
2508
2509   ip6_set_reserved_multicast_address (&addr, scope, group);
2510
2511   ip6_neighbor_add_mld_prefix (a, &addr);
2512 }
2513
2514 /**
2515  * @brief create and initialize router advertisement parameters with default
2516  * values for this intfc
2517  */
2518 u32
2519 ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
2520                                    u32 sw_if_index, u32 is_add)
2521 {
2522   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2523   ip6_radv_t *a = 0;
2524   u32 ri = ~0;
2525   vnet_sw_interface_t *sw_if0;
2526   ethernet_interface_t *eth_if0 = 0;
2527
2528   /* lookup radv container  - ethernet interfaces only */
2529   sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2530   if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2531     eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2532
2533   if (!eth_if0)
2534     return ri;
2535
2536   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2537                            ~0);
2538   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2539
2540   if (ri != ~0)
2541     {
2542       a = pool_elt_at_index (nm->if_radv_pool, ri);
2543
2544       if (!is_add)
2545         {
2546           ip6_radv_prefix_t *p;
2547           ip6_mldp_group_t *m;
2548
2549           /* release the lock on the interface's mcast adj */
2550           adj_unlock (a->mcast_adj_index);
2551
2552           /* clean up prefix and MDP pools */
2553           /* *INDENT-OFF* */
2554           pool_flush(p, a->adv_prefixes_pool,
2555           ({
2556               mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
2557           }));
2558           pool_flush (m, a->mldp_group_pool,
2559           ({
2560               mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
2561           }));
2562           /* *INDENT-ON* */
2563
2564           pool_free (a->mldp_group_pool);
2565           pool_free (a->adv_prefixes_pool);
2566
2567           mhash_free (&a->address_to_prefix_index);
2568           mhash_free (&a->address_to_mldp_index);
2569
2570           if (a->keep_sending_rs)
2571             a->keep_sending_rs = 0;
2572
2573           pool_put (nm->if_radv_pool, a);
2574           nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2575           ri = ~0;
2576         }
2577     }
2578   else
2579     {
2580       if (is_add)
2581         {
2582           vnet_hw_interface_t *hw_if0;
2583
2584           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2585
2586           pool_get (nm->if_radv_pool, a);
2587
2588           ri = a - nm->if_radv_pool;
2589           nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2590
2591           /* initialize default values (most of which are zero) */
2592           memset (a, 0, sizeof (a[0]));
2593
2594           a->sw_if_index = sw_if_index;
2595           a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2596           a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2597           a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2598           a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2599
2600           /* send ll address source address option */
2601           a->adv_link_layer_address = 1;
2602
2603           a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2604           a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2605           a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2606           a->seed = (u32) clib_cpu_time_now ();
2607           (void) random_u32 (&a->seed);
2608           a->randomizer = clib_cpu_time_now ();
2609           (void) random_u64 (&a->randomizer);
2610
2611           a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2612           a->initial_adverts_sent = a->initial_adverts_count - 1;
2613           a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2614
2615           /* deafult is to send */
2616           a->send_radv = 1;
2617
2618           /* fill in radv_info for this interface that will be needed later */
2619           a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2620
2621           clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2622
2623           /* fill in default link-local address  (this may be overridden) */
2624           ip6_link_local_address_from_ethernet_address
2625             (&a->link_local_address, eth_if0->address);
2626
2627           mhash_init (&a->address_to_prefix_index, sizeof (uword),
2628                       sizeof (ip6_address_t));
2629           mhash_init (&a->address_to_mldp_index, sizeof (uword),
2630                       sizeof (ip6_address_t));
2631
2632           a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2633                                                       VNET_LINK_IP6,
2634                                                       sw_if_index);
2635
2636           a->keep_sending_rs = 0;
2637
2638           /* add multicast groups we will always be reporting  */
2639           ip6_neighbor_add_mld_grp (a,
2640                                     IP6_MULTICAST_SCOPE_link_local,
2641                                     IP6_MULTICAST_GROUP_ID_all_hosts);
2642           ip6_neighbor_add_mld_grp (a,
2643                                     IP6_MULTICAST_SCOPE_link_local,
2644                                     IP6_MULTICAST_GROUP_ID_all_routers);
2645           ip6_neighbor_add_mld_grp (a,
2646                                     IP6_MULTICAST_SCOPE_link_local,
2647                                     IP6_MULTICAST_GROUP_ID_mldv2_routers);
2648         }
2649     }
2650   return ri;
2651 }
2652
2653 /* send an mldpv2 report  */
2654 static void
2655 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
2656 {
2657   vnet_main_t *vnm = vnet_get_main ();
2658   vlib_main_t *vm = vnm->vlib_main;
2659   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2660   vnet_sw_interface_t *sw_if0;
2661   ethernet_interface_t *eth_if0;
2662   u32 ri;
2663   int bogus_length;
2664
2665   ip6_radv_t *radv_info;
2666   u16 payload_length;
2667   vlib_buffer_t *b0;
2668   ip6_header_t *ip0;
2669   u32 *to_next;
2670   vlib_frame_t *f;
2671   u32 bo0;
2672   u32 n_to_alloc = 1;
2673   u32 n_allocated;
2674
2675   icmp6_multicast_listener_report_header_t *rh0;
2676   icmp6_multicast_listener_report_packet_t *rp0;
2677
2678   sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2679   ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2680   eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2681
2682   if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2683     return;
2684
2685   /* look up the radv_t  information for this interface */
2686   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2687                            ~0);
2688
2689   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2690
2691   if (ri == ~0)
2692     return;
2693
2694   /* send report now - build a mldpv2 report packet  */
2695   n_allocated = vlib_buffer_alloc_from_free_list (vm,
2696                                                   &bo0,
2697                                                   n_to_alloc,
2698                                                   VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2699   if (PREDICT_FALSE (n_allocated == 0))
2700     {
2701       clib_warning ("buffer allocation failure");
2702       return;
2703     }
2704
2705   b0 = vlib_get_buffer (vm, bo0);
2706
2707   /* adjust the sizeof the buffer to just include the ipv6 header */
2708   b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
2709
2710   payload_length = sizeof (icmp6_multicast_listener_report_header_t);
2711
2712   b0->error = ICMP6_ERROR_NONE;
2713
2714   rp0 = vlib_buffer_get_current (b0);
2715   ip0 = (ip6_header_t *) & rp0->ip;
2716   rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
2717
2718   memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2719
2720   ip0->ip_version_traffic_class_and_flow_label =
2721     clib_host_to_net_u32 (0x6 << 28);
2722
2723   ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
2724   /* for DEBUG - vnet driver won't seem to emit router alerts */
2725   /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2726   ip0->hop_limit = 1;
2727
2728   rh0->icmp.type = ICMP6_multicast_listener_report_v2;
2729
2730   /* source address MUST be the link-local address */
2731   radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2732   ip0->src_address = radv_info->link_local_address;
2733
2734   /* destination is all mldpv2 routers */
2735   ip6_set_reserved_multicast_address (&ip0->dst_address,
2736                                       IP6_MULTICAST_SCOPE_link_local,
2737                                       IP6_MULTICAST_GROUP_ID_mldv2_routers);
2738
2739   /* add reports here */
2740   ip6_mldp_group_t *m;
2741   int num_addr_records = 0;
2742   icmp6_multicast_address_record_t rr;
2743
2744   /* fill in the hop-by-hop extension header (router alert) info */
2745   rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2746   rh0->ext_hdr.n_data_u64s = 0;
2747
2748   rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2749   rh0->alert.len = 2;
2750   rh0->alert.value = 0;
2751
2752   rh0->pad.type = 1;
2753   rh0->pad.len = 0;
2754
2755   rh0->icmp.checksum = 0;
2756
2757   /* *INDENT-OFF* */
2758   pool_foreach (m, radv_info->mldp_group_pool,
2759   ({
2760     rr.type = m->type;
2761     rr.aux_data_len_u32s = 0;
2762     rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2763     clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
2764
2765     num_addr_records++;
2766
2767     vlib_buffer_add_data
2768       (vm, vlib_buffer_get_free_list_index (b0), bo0,
2769        (void *)&rr, sizeof(icmp6_multicast_address_record_t));
2770
2771     payload_length += sizeof( icmp6_multicast_address_record_t);
2772   }));
2773   /* *INDENT-ON* */
2774
2775   rh0->rsvd = 0;
2776   rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2777
2778   /* update lengths */
2779   ip0->payload_length = clib_host_to_net_u16 (payload_length);
2780
2781   rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2782                                                           &bogus_length);
2783   ASSERT (bogus_length == 0);
2784
2785   /*
2786    * OK to override w/ no regard for actual FIB, because
2787    * ip6-rewrite only looks at the adjacency.
2788    */
2789   vnet_buffer (b0)->sw_if_index[VLIB_RX] =
2790     vnet_main.local_interface_sw_if_index;
2791
2792   vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2793   b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2794
2795   vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
2796
2797   f = vlib_get_frame_to_node (vm, node->index);
2798   to_next = vlib_frame_vector_args (f);
2799   to_next[0] = bo0;
2800   f->n_vectors = 1;
2801
2802   vlib_put_frame_to_node (vm, node->index, f);
2803   return;
2804 }
2805
2806 /* *INDENT-OFF* */
2807 VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2808 {
2809   .function = icmp6_router_solicitation,
2810   .name = "icmp6-router-solicitation",
2811
2812   .vector_size = sizeof (u32),
2813
2814   .format_trace = format_icmp6_input_trace,
2815
2816   .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2817   .next_nodes = {
2818     [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
2819     [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
2820     [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2821   },
2822 };
2823 /* *INDENT-ON* */
2824
2825 /* send a RA or update the timer info etc.. */
2826 static uword
2827 ip6_neighbor_process_timer_event (vlib_main_t * vm,
2828                                   vlib_node_runtime_t * node,
2829                                   vlib_frame_t * frame)
2830 {
2831   vnet_main_t *vnm = vnet_get_main ();
2832   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2833   ip6_radv_t *radv_info;
2834   vlib_frame_t *f = 0;
2835   u32 n_this_frame = 0;
2836   u32 n_left_to_next = 0;
2837   u32 *to_next = 0;
2838   u32 bo0;
2839   icmp6_router_solicitation_header_t *h0;
2840   vlib_buffer_t *b0;
2841   f64 now = vlib_time_now (vm);
2842
2843   /* Interface ip6 radv info list */
2844   /* *INDENT-OFF* */
2845   pool_foreach (radv_info, nm->if_radv_pool,
2846   ({
2847     if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2848       {
2849         radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2850         radv_info->next_multicast_time = now;
2851         radv_info->last_multicast_time = now;
2852         radv_info->last_radv_time = 0;
2853         radv_info->all_routers_mcast = 0;
2854         continue;
2855       }
2856
2857     /* Make sure that we've joined the all-routers multicast group */
2858     if(!radv_info->all_routers_mcast)
2859       {
2860         /* send MDLP_REPORT_EVENT message */
2861         ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2862         radv_info->all_routers_mcast = 1;
2863       }
2864
2865     /* is it time to send a multicast  RA on this interface? */
2866     if(radv_info->send_radv && (now >=  radv_info->next_multicast_time))
2867       {
2868         u32 n_to_alloc = 1;
2869         u32 n_allocated;
2870
2871         f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2872           random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
2873
2874         /* multicast send - compute next multicast send time */
2875         if( radv_info->initial_adverts_sent > 0)
2876           {
2877             radv_info->initial_adverts_sent--;
2878             if(rfn > radv_info-> initial_adverts_interval)
2879               rfn =  radv_info-> initial_adverts_interval;
2880
2881             /* check to see if we are ceasing to send */
2882             if( radv_info->initial_adverts_sent  == 0)
2883               if(radv_info->cease_radv)
2884                 radv_info->send_radv = 0;
2885           }
2886
2887         radv_info->next_multicast_time =  rfn + now;
2888         radv_info->last_multicast_time = now;
2889
2890         /* send advert now - build a "solicted" router advert with unspecified source address */
2891         n_allocated = vlib_buffer_alloc_from_free_list
2892           (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2893
2894         if (PREDICT_FALSE(n_allocated == 0))
2895           {
2896             clib_warning ("buffer allocation failure");
2897             continue;
2898           }
2899         b0 = vlib_get_buffer (vm, bo0);
2900         b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2901         b0->error = ICMP6_ERROR_NONE;
2902         vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2903
2904         h0 =  vlib_buffer_get_current (b0);
2905
2906         memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2907
2908         h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2909         h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2910                                                       - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2911         h0->ip.protocol = IP_PROTOCOL_ICMP6;
2912         h0->ip.hop_limit = 255;
2913
2914         /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2915         h0->ip.src_address.as_u64[0] = 0;
2916         h0->ip.src_address.as_u64[1] = 0;
2917
2918         h0->ip.dst_address.as_u64[0] = 0;
2919         h0->ip.dst_address.as_u64[1] = 0;
2920
2921         h0->neighbor.icmp.type = ICMP6_router_solicitation;
2922
2923         if (PREDICT_FALSE(f == 0))
2924           {
2925             f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2926             to_next = vlib_frame_vector_args (f);
2927             n_left_to_next = VLIB_FRAME_SIZE;
2928             n_this_frame = 0;
2929           }
2930
2931         n_this_frame++;
2932         n_left_to_next--;
2933         to_next[0] = bo0;
2934         to_next += 1;
2935
2936         if (PREDICT_FALSE(n_left_to_next == 0))
2937           {
2938             f->n_vectors = n_this_frame;
2939             vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2940             f = 0;
2941           }
2942       }
2943   }));
2944   /* *INDENT-ON* */
2945
2946   if (f)
2947     {
2948       ASSERT (n_this_frame);
2949       f->n_vectors = n_this_frame;
2950       vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2951     }
2952   return 0;
2953 }
2954
2955 static uword
2956 ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2957                                            vlib_node_runtime_t * node,
2958                                            vlib_frame_t * frame)
2959 {
2960   uword event_type;
2961   ip6_icmp_neighbor_discovery_event_data_t *event_data;
2962
2963   /* init code here */
2964
2965   while (1)
2966     {
2967       vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
2968
2969       event_data = vlib_process_get_event_data (vm, &event_type);
2970
2971       if (!event_data)
2972         {
2973           /* No events found: timer expired. */
2974           /* process interface list and send RAs as appropriate, update timer info */
2975           ip6_neighbor_process_timer_event (vm, node, frame);
2976         }
2977       else
2978         {
2979           switch (event_type)
2980             {
2981
2982             case ICMP6_ND_EVENT_INIT:
2983               break;
2984
2985             case ~0:
2986               break;
2987
2988             default:
2989               ASSERT (0);
2990             }
2991
2992           if (event_data)
2993             _vec_len (event_data) = 0;
2994         }
2995     }
2996   return frame->n_vectors;
2997 }
2998
2999 /* *INDENT-OFF* */
3000 VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3001 {
3002   .function = icmp6_router_advertisement,
3003   .name = "icmp6-router-advertisement",
3004
3005   .vector_size = sizeof (u32),
3006
3007   .format_trace = format_icmp6_input_trace,
3008
3009   .n_next_nodes = 1,
3010   .next_nodes = {
3011     [0] = "ip6-drop",
3012   },
3013 };
3014 /* *INDENT-ON* */
3015
3016 vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3017
3018   .function = ip6_icmp_neighbor_discovery_event_process,
3019   .name = "ip6-icmp-neighbor-discovery-event-process",
3020   .type = VLIB_NODE_TYPE_PROCESS,
3021 };
3022
3023 static uword
3024 icmp6_neighbor_solicitation (vlib_main_t * vm,
3025                              vlib_node_runtime_t * node, vlib_frame_t * frame)
3026 {
3027   return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3028                                                        /* is_solicitation */
3029                                                        1);
3030 }
3031
3032 static uword
3033 icmp6_neighbor_advertisement (vlib_main_t * vm,
3034                               vlib_node_runtime_t * node,
3035                               vlib_frame_t * frame)
3036 {
3037   return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3038                                                        /* is_solicitation */
3039                                                        0);
3040 }
3041
3042 /* *INDENT-OFF* */
3043 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3044 {
3045   .function = icmp6_neighbor_solicitation,
3046   .name = "icmp6-neighbor-solicitation",
3047
3048   .vector_size = sizeof (u32),
3049
3050   .format_trace = format_icmp6_input_trace,
3051
3052   .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3053   .next_nodes = {
3054     [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
3055     [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3056   },
3057 };
3058 /* *INDENT-ON* */
3059
3060 /* *INDENT-OFF* */
3061 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3062 {
3063   .function = icmp6_neighbor_advertisement,
3064   .name = "icmp6-neighbor-advertisement",
3065
3066   .vector_size = sizeof (u32),
3067
3068   .format_trace = format_icmp6_input_trace,
3069
3070   .n_next_nodes = 1,
3071   .next_nodes = {
3072     [0] = "ip6-drop",
3073   },
3074 };
3075 /* *INDENT-ON* */
3076
3077 typedef enum
3078 {
3079   IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3080   IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3081   IP6_DISCOVER_NEIGHBOR_N_NEXT,
3082 } ip6_discover_neighbor_next_t;
3083
3084 typedef enum
3085 {
3086   IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3087   IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3088   IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3089 } ip6_discover_neighbor_error_t;
3090
3091 static uword
3092 ip6_discover_neighbor_inline (vlib_main_t * vm,
3093                               vlib_node_runtime_t * node,
3094                               vlib_frame_t * frame, int is_glean)
3095 {
3096   vnet_main_t *vnm = vnet_get_main ();
3097   ip6_main_t *im = &ip6_main;
3098   ip_lookup_main_t *lm = &im->lookup_main;
3099   u32 *from, *to_next_drop;
3100   uword n_left_from, n_left_to_next_drop;
3101   static f64 time_last_seed_change = -1e100;
3102   static u32 hash_seeds[3];
3103   static uword hash_bitmap[256 / BITS (uword)];
3104   f64 time_now;
3105   int bogus_length;
3106   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3107
3108   if (node->flags & VLIB_NODE_FLAG_TRACE)
3109     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3110
3111   time_now = vlib_time_now (vm);
3112   if (time_now - time_last_seed_change > 1e-3)
3113     {
3114       uword i;
3115       u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
3116                                             sizeof (hash_seeds));
3117       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
3118         hash_seeds[i] = r[i];
3119
3120       /* Mark all hash keys as been not-seen before. */
3121       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
3122         hash_bitmap[i] = 0;
3123
3124       time_last_seed_change = time_now;
3125     }
3126
3127   from = vlib_frame_vector_args (frame);
3128   n_left_from = frame->n_vectors;
3129
3130   while (n_left_from > 0)
3131     {
3132       vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3133                            to_next_drop, n_left_to_next_drop);
3134
3135       while (n_left_from > 0 && n_left_to_next_drop > 0)
3136         {
3137           vlib_buffer_t *p0;
3138           ip6_header_t *ip0;
3139           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
3140           uword bm0;
3141           ip_adjacency_t *adj0;
3142           vnet_hw_interface_t *hw_if0;
3143           ip6_radv_t *radv_info;
3144           u32 next0;
3145
3146           pi0 = from[0];
3147
3148           p0 = vlib_get_buffer (vm, pi0);
3149
3150           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3151
3152           ip0 = vlib_buffer_get_current (p0);
3153
3154           adj0 = adj_get (adj_index0);
3155
3156           if (!is_glean)
3157             {
3158               ip0->dst_address.as_u64[0] =
3159                 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3160               ip0->dst_address.as_u64[1] =
3161                 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3162             }
3163
3164           a0 = hash_seeds[0];
3165           b0 = hash_seeds[1];
3166           c0 = hash_seeds[2];
3167
3168           sw_if_index0 = adj0->rewrite_header.sw_if_index;
3169           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3170
3171           a0 ^= sw_if_index0;
3172           b0 ^= ip0->dst_address.as_u32[0];
3173           c0 ^= ip0->dst_address.as_u32[1];
3174
3175           hash_v3_mix32 (a0, b0, c0);
3176
3177           b0 ^= ip0->dst_address.as_u32[2];
3178           c0 ^= ip0->dst_address.as_u32[3];
3179
3180           hash_v3_finalize32 (a0, b0, c0);
3181
3182           c0 &= BITS (hash_bitmap) - 1;
3183           c0 = c0 / BITS (uword);
3184           m0 = (uword) 1 << (c0 % BITS (uword));
3185
3186           bm0 = hash_bitmap[c0];
3187           drop0 = (bm0 & m0) != 0;
3188
3189           /* Mark it as seen. */
3190           hash_bitmap[c0] = bm0 | m0;
3191
3192           from += 1;
3193           n_left_from -= 1;
3194           to_next_drop[0] = pi0;
3195           to_next_drop += 1;
3196           n_left_to_next_drop -= 1;
3197
3198           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3199
3200           /* If the interface is link-down, drop the pkt */
3201           if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3202             drop0 = 1;
3203
3204           if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3205             {
3206               u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3207
3208               if (ri != ~0)
3209                 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3210               else
3211                 drop0 = 1;
3212             }
3213           else
3214             drop0 = 1;
3215
3216           /*
3217            * the adj has been updated to a rewrite but the node the DPO that got
3218            * us here hasn't - yet. no big deal. we'll drop while we wait.
3219            */
3220           if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3221             drop0 = 1;
3222
3223           p0->error =
3224             node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3225                          : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3226
3227           if (drop0)
3228             continue;
3229
3230           {
3231             u32 bi0 = 0;
3232             icmp6_neighbor_solicitation_header_t *h0;
3233             vlib_buffer_t *b0;
3234
3235             h0 = vlib_packet_template_get_packet
3236               (vm, &im->discover_neighbor_packet_template, &bi0);
3237
3238             /*
3239              * Build ethernet header.
3240              * Choose source address based on destination lookup
3241              * adjacency.
3242              */
3243             if (!ip6_src_address_for_packet (lm,
3244                                              sw_if_index0,
3245                                              &ip0->dst_address,
3246                                              &h0->ip.src_address))
3247               {
3248                 /* There is no address on the interface */
3249                 p0->error =
3250                   node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3251                 vlib_buffer_free (vm, &bi0, 1);
3252                 continue;
3253               }
3254
3255             /*
3256              * Destination address is a solicited node multicast address.
3257              * We need to fill in
3258              * the low 24 bits with low 24 bits of target's address.
3259              */
3260             h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3261             h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3262             h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3263
3264             h0->neighbor.target_address = ip0->dst_address;
3265
3266             clib_memcpy (h0->link_layer_option.ethernet_address,
3267                          hw_if0->hw_address, vec_len (hw_if0->hw_address));
3268
3269             /* $$$$ appears we need this; why is the checksum non-zero? */
3270             h0->neighbor.icmp.checksum = 0;
3271             h0->neighbor.icmp.checksum =
3272               ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3273                                                  &bogus_length);
3274
3275             ASSERT (bogus_length == 0);
3276
3277             vlib_buffer_copy_trace_flag (vm, p0, bi0);
3278             b0 = vlib_get_buffer (vm, bi0);
3279             vnet_buffer (b0)->sw_if_index[VLIB_TX]
3280               = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3281
3282             vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3283               radv_info->mcast_adj_index;
3284
3285             b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3286             next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3287
3288             vlib_set_next_frame_buffer (vm, node, next0, bi0);
3289           }
3290         }
3291
3292       vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3293                            n_left_to_next_drop);
3294     }
3295
3296   return frame->n_vectors;
3297 }
3298
3299 static uword
3300 ip6_discover_neighbor (vlib_main_t * vm,
3301                        vlib_node_runtime_t * node, vlib_frame_t * frame)
3302 {
3303   return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3304 }
3305
3306 static uword
3307 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3308 {
3309   return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3310 }
3311
3312 static char *ip6_discover_neighbor_error_strings[] = {
3313   [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3314   [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3315   [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3316     = "no source address for ND solicitation",
3317 };
3318
3319 /* *INDENT-OFF* */
3320 VLIB_REGISTER_NODE (ip6_glean_node) =
3321 {
3322   .function = ip6_glean,
3323   .name = "ip6-glean",
3324   .vector_size = sizeof (u32),
3325   .format_trace = format_ip6_forward_next_trace,
3326   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3327   .error_strings = ip6_discover_neighbor_error_strings,
3328   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3329   .next_nodes =
3330   {
3331     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3332     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3333   },
3334 };
3335 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3336 {
3337   .function = ip6_discover_neighbor,
3338   .name = "ip6-discover-neighbor",
3339   .vector_size = sizeof (u32),
3340   .format_trace = format_ip6_forward_next_trace,
3341   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3342   .error_strings = ip6_discover_neighbor_error_strings,
3343   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3344   .next_nodes =
3345   {
3346     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3347     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3348   },
3349 };
3350 /* *INDENT-ON* */
3351
3352 /* API support functions */
3353 int
3354 ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3355                         u8 suppress, u8 managed, u8 other,
3356                         u8 ll_option, u8 send_unicast, u8 cease,
3357                         u8 use_lifetime, u32 lifetime,
3358                         u32 initial_count, u32 initial_interval,
3359                         u32 max_interval, u32 min_interval, u8 is_no)
3360 {
3361   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3362   int error;
3363   u32 ri;
3364
3365   /* look up the radv_t  information for this interface */
3366   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3367                            ~0);
3368   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3369   error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3370
3371   if (!error)
3372     {
3373
3374       ip6_radv_t *radv_info;
3375       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3376
3377       if ((max_interval != 0) && (min_interval == 0))
3378         min_interval = .75 * max_interval;
3379
3380       max_interval =
3381         (max_interval !=
3382          0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3383         radv_info->max_radv_interval;
3384       min_interval =
3385         (min_interval !=
3386          0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3387         radv_info->min_radv_interval;
3388       lifetime =
3389         (use_lifetime !=
3390          0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3391         radv_info->adv_router_lifetime_in_sec;
3392
3393       if (lifetime)
3394         {
3395           if (lifetime > MAX_DEF_RTR_LIFETIME)
3396             lifetime = MAX_DEF_RTR_LIFETIME;
3397
3398           if (lifetime <= max_interval)
3399             return VNET_API_ERROR_INVALID_VALUE;
3400         }
3401
3402       if (min_interval != 0)
3403         {
3404           if ((min_interval > .75 * max_interval) || (min_interval < 3))
3405             return VNET_API_ERROR_INVALID_VALUE;
3406         }
3407
3408       if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3409           (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3410         return VNET_API_ERROR_INVALID_VALUE;
3411
3412       /*
3413          if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3414          if "flag" is clear  don't change corresponding value
3415        */
3416       radv_info->send_radv =
3417         (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3418       radv_info->adv_managed_flag =
3419         (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3420       radv_info->adv_other_flag =
3421         (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3422       radv_info->adv_link_layer_address =
3423         (ll_option !=
3424          0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3425       radv_info->send_unicast =
3426         (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3427       radv_info->cease_radv =
3428         (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3429
3430       radv_info->min_radv_interval = min_interval;
3431       radv_info->max_radv_interval = max_interval;
3432       radv_info->adv_router_lifetime_in_sec = lifetime;
3433
3434       radv_info->initial_adverts_count =
3435         (initial_count !=
3436          0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3437         radv_info->initial_adverts_count;
3438       radv_info->initial_adverts_interval =
3439         (initial_interval !=
3440          0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3441         radv_info->initial_adverts_interval;
3442
3443       /* restart */
3444       if ((cease != 0) && (is_no))
3445         radv_info->send_radv = 1;
3446
3447       radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3448       radv_info->next_multicast_time = vlib_time_now (vm);
3449       radv_info->last_multicast_time = vlib_time_now (vm);
3450       radv_info->last_radv_time = 0;
3451     }
3452   return (error);
3453 }
3454
3455 int
3456 ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3457                         ip6_address_t * prefix_addr, u8 prefix_len,
3458                         u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3459                         u8 no_advertise, u8 off_link, u8 no_autoconfig,
3460                         u8 no_onlink, u8 is_no)
3461 {
3462   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3463   int error;
3464
3465   u32 ri;
3466
3467   /* look up the radv_t  information for this interface */
3468   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3469                            ~0);
3470
3471   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3472
3473   error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
3474
3475   if (!error)
3476     {
3477       f64 now = vlib_time_now (vm);
3478       ip6_radv_t *radv_info;
3479       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3480
3481       /* prefix info add, delete or update */
3482       ip6_radv_prefix_t *prefix;
3483
3484       /* lookup  prefix info for this  address on this interface */
3485       uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3486
3487       prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3488
3489       if (is_no)
3490         {
3491           /* delete */
3492           if (!prefix)
3493             return VNET_API_ERROR_INVALID_VALUE;        /* invalid prefix */
3494
3495           if (prefix->prefix_len != prefix_len)
3496             return VNET_API_ERROR_INVALID_VALUE_2;
3497
3498           /* FIXME - Should the DP do this or the CP ? */
3499           /* do specific delete processing here before returning */
3500           /* try to remove from routing table */
3501
3502           mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3503                        /* old_value */ 0);
3504           pool_put (radv_info->adv_prefixes_pool, prefix);
3505
3506           radv_info->initial_adverts_sent =
3507             radv_info->initial_adverts_count - 1;
3508           radv_info->next_multicast_time = vlib_time_now (vm);
3509           radv_info->last_multicast_time = vlib_time_now (vm);
3510           radv_info->last_radv_time = 0;
3511           return (error);
3512         }
3513
3514       /* adding or changing */
3515       if (!prefix)
3516         {
3517           /* add */
3518           u32 pi;
3519           pool_get (radv_info->adv_prefixes_pool, prefix);
3520           pi = prefix - radv_info->adv_prefixes_pool;
3521           mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3522                      /* old_value */ 0);
3523
3524           memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3525
3526           prefix->prefix_len = prefix_len;
3527           clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3528
3529           /* initialize default values */
3530           prefix->adv_on_link_flag = 1; /* L bit set */
3531           prefix->adv_autonomous_flag = 1;      /* A bit set */
3532           prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
3533           prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3534           prefix->enabled = 1;
3535           prefix->decrement_lifetime_flag = 1;
3536           prefix->deprecated_prefix_flag = 1;
3537
3538           if (off_link == 0)
3539             {
3540               /* FIXME - Should the DP do this or the CP ? */
3541               /* insert prefix into routing table as a connected prefix */
3542             }
3543
3544           if (use_default)
3545             goto restart;
3546         }
3547       else
3548         {
3549
3550           if (prefix->prefix_len != prefix_len)
3551             return VNET_API_ERROR_INVALID_VALUE_2;
3552
3553           if (off_link != 0)
3554             {
3555               /* FIXME - Should the DP do this or the CP ? */
3556               /* remove from routing table if already there */
3557             }
3558         }
3559
3560       if ((val_lifetime == ~0) || (pref_lifetime == ~0))
3561         {
3562           prefix->adv_valid_lifetime_in_secs = ~0;
3563           prefix->adv_pref_lifetime_in_secs = ~0;
3564           prefix->decrement_lifetime_flag = 0;
3565         }
3566       else
3567         {
3568           prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3569           prefix->adv_pref_lifetime_in_secs = pref_lifetime;
3570         }
3571
3572       /* copy  remaining */
3573       prefix->enabled = !(no_advertise != 0);
3574       prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3575       prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3576
3577     restart:
3578       /* restart */
3579       /* fill in the expiration times  */
3580       prefix->valid_lifetime_expires =
3581         now + prefix->adv_valid_lifetime_in_secs;
3582       prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
3583
3584       radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3585       radv_info->next_multicast_time = vlib_time_now (vm);
3586       radv_info->last_multicast_time = vlib_time_now (vm);
3587       radv_info->last_radv_time = 0;
3588     }
3589   return (error);
3590 }
3591
3592 clib_error_t *
3593 ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3594                   vlib_cli_command_t * cmd)
3595 {
3596   vnet_main_t *vnm = vnet_get_main ();
3597   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3598   clib_error_t *error = 0;
3599   u8 is_no = 0;
3600   u8 suppress = 0, managed = 0, other = 0;
3601   u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
3602   u8 use_lifetime = 0;
3603   u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3604     0, ra_initial_interval = 0;
3605   u32 ra_max_interval = 0, ra_min_interval = 0;
3606
3607   unformat_input_t _line_input, *line_input = &_line_input;
3608   vnet_sw_interface_t *sw_if0;
3609
3610   int add_radv_info = 1;
3611   __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
3612   ip6_address_t ip6_addr;
3613   u32 addr_len;
3614
3615
3616   /* Get a line of input. */
3617   if (!unformat_user (main_input, unformat_line_input, line_input))
3618     return 0;
3619
3620   /* get basic radv info for this interface */
3621   if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3622     {
3623
3624       if (unformat_user (line_input,
3625                          unformat_vnet_sw_interface, vnm, &sw_if_index))
3626         {
3627           u32 ri;
3628           ethernet_interface_t *eth_if0 = 0;
3629
3630           sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3631           if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3632             eth_if0 =
3633               ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3634
3635           if (!eth_if0)
3636             {
3637               error =
3638                 clib_error_return (0, "Interface must be of ethernet type");
3639               goto done;
3640             }
3641
3642           /* look up the radv_t  information for this interface */
3643           vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3644                                    sw_if_index, ~0);
3645
3646           ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3647
3648           if (ri != ~0)
3649             {
3650               radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3651             }
3652           else
3653             {
3654               error = clib_error_return (0, "unknown interface %U'",
3655                                          format_unformat_error, line_input);
3656               goto done;
3657             }
3658         }
3659       else
3660         {
3661           error = clib_error_return (0, "invalid interface name %U'",
3662                                      format_unformat_error, line_input);
3663           goto done;
3664         }
3665     }
3666
3667   /* get the rest of the command */
3668   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3669     {
3670       if (unformat (line_input, "no"))
3671         is_no = 1;
3672       else if (unformat (line_input, "prefix %U/%d",
3673                          unformat_ip6_address, &ip6_addr, &addr_len))
3674         {
3675           add_radv_info = 0;
3676           break;
3677         }
3678       else if (unformat (line_input, "ra-managed-config-flag"))
3679         {
3680           managed = 1;
3681           break;
3682         }
3683       else if (unformat (line_input, "ra-other-config-flag"))
3684         {
3685           other = 1;
3686           break;
3687         }
3688       else if (unformat (line_input, "ra-suppress") ||
3689                unformat (line_input, "ra-surpress"))
3690         {
3691           suppress = 1;
3692           break;
3693         }
3694       else if (unformat (line_input, "ra-suppress-link-layer") ||
3695                unformat (line_input, "ra-surpress-link-layer"))
3696         {
3697           suppress_ll_option = 1;
3698           break;
3699         }
3700       else if (unformat (line_input, "ra-send-unicast"))
3701         {
3702           send_unicast = 1;
3703           break;
3704         }
3705       else if (unformat (line_input, "ra-lifetime"))
3706         {
3707           if (!unformat (line_input, "%d", &ra_lifetime))
3708             {
3709               error = unformat_parse_error (line_input);
3710               goto done;
3711             }
3712           use_lifetime = 1;
3713           break;
3714         }
3715       else if (unformat (line_input, "ra-initial"))
3716         {
3717           if (!unformat
3718               (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
3719             {
3720               error = unformat_parse_error (line_input);
3721               goto done;
3722             }
3723           break;
3724         }
3725       else if (unformat (line_input, "ra-interval"))
3726         {
3727           if (!unformat (line_input, "%d", &ra_max_interval))
3728             {
3729               error = unformat_parse_error (line_input);
3730               goto done;
3731             }
3732
3733           if (!unformat (line_input, "%d", &ra_min_interval))
3734             ra_min_interval = 0;
3735           break;
3736         }
3737       else if (unformat (line_input, "ra-cease"))
3738         {
3739           cease = 1;
3740           break;
3741         }
3742       else
3743         {
3744           error = unformat_parse_error (line_input);
3745           goto done;
3746         }
3747     }
3748
3749   if (add_radv_info)
3750     {
3751       ip6_neighbor_ra_config (vm, sw_if_index,
3752                               suppress, managed, other,
3753                               suppress_ll_option, send_unicast, cease,
3754                               use_lifetime, ra_lifetime,
3755                               ra_initial_count, ra_initial_interval,
3756                               ra_max_interval, ra_min_interval, is_no);
3757     }
3758   else
3759     {
3760       u32 valid_lifetime_in_secs = 0;
3761       u32 pref_lifetime_in_secs = 0;
3762       u8 use_prefix_default_values = 0;
3763       u8 no_advertise = 0;
3764       u8 off_link = 0;
3765       u8 no_autoconfig = 0;
3766       u8 no_onlink = 0;
3767
3768       /* get the rest of the command */
3769       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3770         {
3771           if (unformat (line_input, "default"))
3772             {
3773               use_prefix_default_values = 1;
3774               break;
3775             }
3776           else if (unformat (line_input, "infinite"))
3777             {
3778               valid_lifetime_in_secs = ~0;
3779               pref_lifetime_in_secs = ~0;
3780               break;
3781             }
3782           else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3783                              &pref_lifetime_in_secs))
3784             break;
3785           else
3786             break;
3787         }
3788
3789
3790       /* get the rest of the command */
3791       while (!use_prefix_default_values &&
3792              unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3793         {
3794           if (unformat (line_input, "no-advertise"))
3795             no_advertise = 1;
3796           else if (unformat (line_input, "off-link"))
3797             off_link = 1;
3798           else if (unformat (line_input, "no-autoconfig"))
3799             no_autoconfig = 1;
3800           else if (unformat (line_input, "no-onlink"))
3801             no_onlink = 1;
3802           else
3803             {
3804               error = unformat_parse_error (line_input);
3805               goto done;
3806             }
3807         }
3808
3809       ip6_neighbor_ra_prefix (vm, sw_if_index,
3810                               &ip6_addr, addr_len,
3811                               use_prefix_default_values,
3812                               valid_lifetime_in_secs,
3813                               pref_lifetime_in_secs,
3814                               no_advertise,
3815                               off_link, no_autoconfig, no_onlink, is_no);
3816     }
3817
3818 done:
3819   unformat_free (line_input);
3820
3821   return error;
3822 }
3823
3824 static void
3825 ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
3826 {
3827   ip_lookup_main_t *lm = &ip6_main.lookup_main;
3828   u32 i;
3829
3830   for (i = 0; i < vec_len (addrs); i++)
3831     {
3832       ip_interface_address_t *a =
3833         pool_elt_at_index (lm->if_address_pool, addrs[i]);
3834       ip6_address_t *address = ip_interface_address_get_address (lm, a);
3835
3836       vlib_cli_output (vm, "\t\t%U/%d",
3837                        format_ip6_address, address, a->address_length);
3838     }
3839 }
3840
3841 static clib_error_t *
3842 show_ip6_interface_cmd (vlib_main_t * vm,
3843                         unformat_input_t * input, vlib_cli_command_t * cmd)
3844 {
3845   vnet_main_t *vnm = vnet_get_main ();
3846   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3847   clib_error_t *error = 0;
3848   u32 sw_if_index;
3849
3850   sw_if_index = ~0;
3851
3852   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
3853     {
3854       u32 ri;
3855
3856       /* look up the radv_t  information for this interface */
3857       vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3858                                sw_if_index, ~0);
3859
3860       ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3861
3862       if (ri != ~0)
3863         {
3864           ip_lookup_main_t *lm = &ip6_main.lookup_main;
3865           ip6_radv_t *radv_info;
3866           radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3867
3868           vlib_cli_output (vm, "%U is admin %s\n",
3869                            format_vnet_sw_interface_name, vnm,
3870                            vnet_get_sw_interface (vnm, sw_if_index),
3871                            (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3872                             "up" : "down"));
3873
3874           u32 ai;
3875           u32 *link_scope = 0, *global_scope = 0;
3876           u32 *local_scope = 0, *unknown_scope = 0;
3877           ip_interface_address_t *a;
3878
3879           vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3880                                    sw_if_index, ~0);
3881           ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3882
3883           while (ai != (u32) ~ 0)
3884             {
3885               a = pool_elt_at_index (lm->if_address_pool, ai);
3886               ip6_address_t *address =
3887                 ip_interface_address_get_address (lm, a);
3888
3889               if (ip6_address_is_link_local_unicast (address))
3890                 vec_add1 (link_scope, ai);
3891               else if (ip6_address_is_global_unicast (address))
3892                 vec_add1 (global_scope, ai);
3893               else if (ip6_address_is_local_unicast (address))
3894                 vec_add1 (local_scope, ai);
3895               else
3896                 vec_add1 (unknown_scope, ai);
3897
3898               ai = a->next_this_sw_interface;
3899             }
3900
3901           if (vec_len (link_scope))
3902             {
3903               vlib_cli_output (vm, "\tLink-local address(es):\n");
3904               ip6_print_addrs (vm, link_scope);
3905               vec_free (link_scope);
3906             }
3907
3908           if (vec_len (local_scope))
3909             {
3910               vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3911               ip6_print_addrs (vm, local_scope);
3912               vec_free (local_scope);
3913             }
3914
3915           if (vec_len (global_scope))
3916             {
3917               vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3918               ip6_print_addrs (vm, global_scope);
3919               vec_free (global_scope);
3920             }
3921
3922           if (vec_len (unknown_scope))
3923             {
3924               vlib_cli_output (vm, "\tOther-scope address(es):\n");
3925               ip6_print_addrs (vm, unknown_scope);
3926               vec_free (unknown_scope);
3927             }
3928
3929           vlib_cli_output (vm, "\tLink-local address(es):\n");
3930           vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3931                            &radv_info->link_local_address);
3932
3933           vlib_cli_output (vm, "\tJoined group address(es):\n");
3934           ip6_mldp_group_t *m;
3935           /* *INDENT-OFF* */
3936           pool_foreach (m, radv_info->mldp_group_pool,
3937           ({
3938             vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3939                              &m->mcast_address);
3940           }));
3941           /* *INDENT-ON* */
3942
3943           vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
3944           ip6_radv_prefix_t *p;
3945           /* *INDENT-OFF* */
3946           pool_foreach (p, radv_info->adv_prefixes_pool,
3947           ({
3948             vlib_cli_output (vm, "\t\tprefix %U,  length %d\n",
3949                              format_ip6_address, &p->prefix, p->prefix_len);
3950           }));
3951           /* *INDENT-ON* */
3952
3953           vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
3954           vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3955           vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3956           vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3957           vlib_cli_output (vm, "\tND DAD is disabled\n");
3958           //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3959           vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3960                            radv_info->adv_neighbor_reachable_time_in_msec);
3961           vlib_cli_output (vm,
3962                            "\tND advertised retransmit interval is %d (msec)\n",
3963                            radv_info->
3964                            adv_time_in_msec_between_retransmitted_neighbor_solicitations);
3965
3966           u32 ra_interval = radv_info->max_radv_interval;
3967           u32 ra_interval_min = radv_info->min_radv_interval;
3968           vlib_cli_output (vm,
3969                            "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
3970                            ra_interval, ra_interval_min);
3971           vlib_cli_output (vm,
3972                            "\tND router advertisements live for %d seconds\n",
3973                            radv_info->adv_router_lifetime_in_sec);
3974           vlib_cli_output (vm,
3975                            "\tHosts %s stateless autoconfig for addresses\n",
3976                            (radv_info->adv_managed_flag) ? "use" :
3977                            " don't use");
3978           vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3979                            radv_info->n_advertisements_sent);
3980           vlib_cli_output (vm, "\tND router solicitations received %d\n",
3981                            radv_info->n_solicitations_rcvd);
3982           vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3983                            radv_info->n_solicitations_dropped);
3984         }
3985       else
3986         {
3987           error = clib_error_return (0, "IPv6 not enabled on interface",
3988                                      format_unformat_error, input);
3989
3990         }
3991     }
3992   return error;
3993 }
3994
3995 /*?
3996  * This command is used to display various IPv6 attributes on a given
3997  * interface.
3998  *
3999  * @cliexpar
4000  * Example of how to display IPv6 settings:
4001  * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4002  * GigabitEthernet2/0/0 is admin up
4003  *         Link-local address(es):
4004  *                 fe80::ab8/64
4005  *         Joined group address(es):
4006  *                 ff02::1
4007  *                 ff02::2
4008  *                 ff02::16
4009  *                 ff02::1:ff00:ab8
4010  *         Advertised Prefixes:
4011  *                 prefix fe80::fe:28ff:fe9c:75b3,  length 64
4012  *         MTU is 1500
4013  *         ICMP error messages are unlimited
4014  *         ICMP redirects are disabled
4015  *         ICMP unreachables are not sent
4016  *         ND DAD is disabled
4017  *         ND advertised reachable time is 0
4018  *         ND advertised retransmit interval is 0 (msec)
4019  *         ND router advertisements are sent every 200 seconds (min interval is 150)
4020  *         ND router advertisements live for 600 seconds
4021  *         Hosts use stateless autoconfig for addresses
4022  *         ND router advertisements sent 19336
4023  *         ND router solicitations received 0
4024  *         ND router solicitations dropped 0
4025  * @cliexend
4026  * Example of output if IPv6 is not enabled on the interface:
4027  * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4028  * show ip6 interface: IPv6 not enabled on interface
4029  * @cliexend
4030 ?*/
4031 /* *INDENT-OFF* */
4032 VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4033 {
4034   .path = "show ip6 interface",
4035   .function = show_ip6_interface_cmd,
4036   .short_help = "show ip6 interface <interface>",
4037 };
4038 /* *INDENT-ON* */
4039
4040 clib_error_t *
4041 disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4042 {
4043   clib_error_t *error = 0;
4044   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4045   u32 ri;
4046
4047   /* look up the radv_t  information for this interface */
4048   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4049                            ~0);
4050   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4051
4052   /* if not created - do nothing */
4053   if (ri != ~0)
4054     {
4055       ip6_radv_t *radv_info;
4056
4057       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4058
4059       /* check radv_info ref count for other ip6 addresses on this interface */
4060       /* This implicitly excludes the link local address */
4061       if (radv_info->ref_count == 0)
4062         {
4063           /* essentially "disables" ipv6 on this interface */
4064           ip6_ll_prefix_t ilp = {
4065             .ilp_addr = radv_info->link_local_address,
4066             .ilp_sw_if_index = sw_if_index,
4067           };
4068           ip6_ll_table_entry_delete (&ilp);
4069           ip6_sw_interface_enable_disable (sw_if_index, 0);
4070           ip6_mfib_interface_enable_disable (sw_if_index, 0);
4071           ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4072                                              0);
4073         }
4074     }
4075   return error;
4076 }
4077
4078 int
4079 ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
4080 {
4081   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4082   u32 ri = ~0;
4083
4084   /* look up the radv_t  information for this interface */
4085   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4086                            ~0);
4087
4088   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4089
4090   return ri != ~0;
4091 }
4092
4093 clib_error_t *
4094 enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
4095 {
4096   clib_error_t *error = 0;
4097   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4098   u32 ri;
4099   int is_add = 1;
4100
4101   /* look up the radv_t  information for this interface */
4102   vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4103                            ~0);
4104
4105   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4106
4107   /* if not created yet */
4108   if (ri == ~0)
4109     {
4110       vnet_main_t *vnm = vnet_get_main ();
4111       vnet_sw_interface_t *sw_if0;
4112
4113       sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4114       if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4115         {
4116           ethernet_interface_t *eth_if0;
4117
4118           eth_if0 =
4119             ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4120           if (eth_if0)
4121             {
4122               /* create radv_info. for this interface.  This holds all the info needed for router adverts */
4123               ri =
4124                 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
4125
4126               if (ri != ~0)
4127                 {
4128                   ip6_radv_t *radv_info;
4129                   ip6_address_t link_local_address;
4130
4131                   radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4132
4133                   ip6_link_local_address_from_ethernet_mac_address
4134                     (&link_local_address, eth_if0->address);
4135
4136                   sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
4137                   if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
4138                       sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
4139                     {
4140                       /* make up  an interface id */
4141                       link_local_address.as_u64[1] =
4142                         random_u64 (&radv_info->randomizer);
4143
4144                       link_local_address.as_u64[0] =
4145                         clib_host_to_net_u64 (0xFE80000000000000ULL);
4146                       /* clear u bit */
4147                       link_local_address.as_u8[8] &= 0xfd;
4148                     }
4149                   {
4150                     ip6_ll_prefix_t ilp = {
4151                       .ilp_addr = link_local_address,
4152                       .ilp_sw_if_index = sw_if_index,
4153                     };
4154
4155                     ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4156                   }
4157
4158                   /* essentially "enables" ipv6 on this interface */
4159                   ip6_mfib_interface_enable_disable (sw_if_index, 1);
4160                   ip6_sw_interface_enable_disable (sw_if_index, 1);
4161
4162                   if (!error)
4163                     {
4164                       radv_info->link_local_address = link_local_address;
4165                     }
4166                 }
4167             }
4168         }
4169     }
4170   return error;
4171 }
4172
4173 int
4174 ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4175 {
4176   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4177   ip6_radv_t *radv_info;
4178   u32 ri;
4179
4180   if (vec_len (nm->if_radv_pool_index_by_sw_if_index) < sw_if_index)
4181     return 0;
4182
4183   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4184
4185   if (ri == ~0)
4186     return 0;
4187
4188   radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4189   *addr = radv_info->link_local_address;
4190
4191   return (!0);
4192 }
4193
4194 static clib_error_t *
4195 enable_ip6_interface_cmd (vlib_main_t * vm,
4196                           unformat_input_t * input, vlib_cli_command_t * cmd)
4197 {
4198   vnet_main_t *vnm = vnet_get_main ();
4199   clib_error_t *error = 0;
4200   u32 sw_if_index;
4201
4202   sw_if_index = ~0;
4203
4204   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4205     {
4206       enable_ip6_interface (vm, sw_if_index);
4207     }
4208   else
4209     {
4210       error = clib_error_return (0, "unknown interface\n'",
4211                                  format_unformat_error, input);
4212
4213     }
4214   return error;
4215 }
4216
4217 /*?
4218  * This command is used to enable IPv6 on a given interface.
4219  *
4220  * @cliexpar
4221  * Example of how enable IPv6 on a given interface:
4222  * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4223 ?*/
4224 /* *INDENT-OFF* */
4225 VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4226 {
4227   .path = "enable ip6 interface",
4228   .function = enable_ip6_interface_cmd,
4229   .short_help = "enable ip6 interface <interface>",
4230 };
4231 /* *INDENT-ON* */
4232
4233 static clib_error_t *
4234 disable_ip6_interface_cmd (vlib_main_t * vm,
4235                            unformat_input_t * input, vlib_cli_command_t * cmd)
4236 {
4237   vnet_main_t *vnm = vnet_get_main ();
4238   clib_error_t *error = 0;
4239   u32 sw_if_index;
4240
4241   sw_if_index = ~0;
4242
4243   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4244     {
4245       error = disable_ip6_interface (vm, sw_if_index);
4246     }
4247   else
4248     {
4249       error = clib_error_return (0, "unknown interface\n'",
4250                                  format_unformat_error, input);
4251
4252     }
4253   return error;
4254 }
4255
4256 /*?
4257  * This command is used to disable IPv6 on a given interface.
4258  *
4259  * @cliexpar
4260  * Example of how disable IPv6 on a given interface:
4261  * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4262 ?*/
4263 /* *INDENT-OFF* */
4264 VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4265 {
4266   .path = "disable ip6 interface",
4267   .function = disable_ip6_interface_cmd,
4268   .short_help = "disable ip6 interface <interface>",
4269 };
4270 /* *INDENT-ON* */
4271
4272 /*?
4273  * This command is used to configure the neighbor discovery
4274  * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4275  * command to display some of the current neighbor discovery parameters
4276  * on a given interface. This command has three formats:
4277  *
4278  *
4279  * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4280  *
4281  * '<em><b>ip6 nd <interface> [no] [ra-managed-config-flag] | [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] | [ra-send-unicast] | [ra-lifetime <lifetime>] | [ra-initial <cnt> <interval>] | [ra-interval <max-interval> [<min-interval>]] | [ra-cease]</b></em>'
4282  *
4283  * Where:
4284  *
4285  * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4286  * router-advertisement messages to use stateful address
4287  * auto-configuration to obtain address information (sets the M-bit).
4288  * Default is the M-bit is not set and the '<em>no</em>' option
4289  * returns it to this default state.
4290  *
4291  * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4292  * router-advertisement messages that hosts use stateful auto
4293  * configuration to obtain nonaddress related information (sets
4294  * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4295  * option returns it to this default state.
4296  *
4297  * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4298  * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4299  * router-advertisement messages.
4300  *
4301  * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4302  * optional source link-layer address in the ICMPv6 router-advertisement
4303  * messages. Default is to include the optional source link-layer address
4304  * and the '<em>no</em>' option returns it to this default state.
4305  *
4306  * <em>[no] ra-send-unicast</em> - Use the source address of the
4307  * router-solicitation message if availiable. The default is to use
4308  * multicast address of all nodes, and the '<em>no</em>' option returns
4309  * it to this default state.
4310  *
4311  * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4312  * default router in ICMPv6 router-advertisement messages. The range is
4313  * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4314  * '<em><max-interval></em>'. The default value is 600 seconds and the
4315  * '<em>no</em>' option returns it to this default value.
4316  *
4317  * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4318  * router-advertisement messages sent and the interval between each
4319  * message. Range for count is 1 - 3 and default is 3. Range for interval
4320  * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4321  * returns both to their default value.
4322  *
4323  * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4324  * interval between sending ICMPv6 router-advertisement messages. The
4325  * range for max-interval is from 4 to 200 seconds. min-interval can not
4326  * be more than 75% of max-interval. If not set, min-interval will be
4327  * set to 75% of max-interval. The range for min-interval is from 3 to
4328  * 150 seconds.  The '<em>no</em>' option returns both to their default
4329  * value.
4330  *
4331  * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4332  * The '<em>no</em>' options implies to start (or restart) sending
4333  * ICMPv6 router-advertisement messages.
4334  *
4335  *
4336  * <b>Format 2 - Prefix Options:</b>
4337  *
4338  * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link] [no-autoconfig] [no-onlink]</b></em>'
4339  *
4340  * Where:
4341  *
4342  * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4343  *
4344  * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4345  * length of time in seconds during what the prefix is valid for the purpose of
4346  * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4347  * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4348  * length of time in seconds during what addresses generated from the prefix remain
4349  * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4350  *
4351  * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4352  * are inifinte, no timeout.
4353  *
4354  * <em>no-advertise</em> - Do not send full router address in prefix
4355  * advertisement. Default is to advertise (i.e. - This flag is off by default).
4356  *
4357  * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4358  * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4359  * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4360  *
4361  * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4362  * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4363  *
4364  * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4365  * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4366  * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4367  * the L-bit.
4368  *
4369  *
4370  * <b>Format 3: - Default of Prefix:</b>
4371  *
4372  * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4373  *
4374  * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4375  * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4376  * is ignored and the prefix is deleted.
4377  *
4378  *
4379  * @cliexpar
4380  * Example of how set a router advertisement option:
4381  * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4382  * Example of how to add a prefix:
4383  * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4384  * Example of how to delete a prefix:
4385  * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4386 ?*/
4387 /* *INDENT-OFF* */
4388 VLIB_CLI_COMMAND (ip6_nd_command, static) =
4389 {
4390   .path = "ip6 nd",
4391   .short_help = "ip6 nd <interface> ...",
4392   .function = ip6_neighbor_cmd,
4393 };
4394 /* *INDENT-ON* */
4395
4396 clib_error_t *
4397 set_ip6_link_local_address (vlib_main_t * vm,
4398                             u32 sw_if_index, ip6_address_t * address)
4399 {
4400   clib_error_t *error = 0;
4401   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4402   u32 ri;
4403   ip6_radv_t *radv_info;
4404   vnet_main_t *vnm = vnet_get_main ();
4405
4406   if (!ip6_address_is_link_local_unicast (address))
4407     {
4408       vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
4409       return (error = clib_error_return (0, "address not link-local",
4410                                          format_unformat_error));
4411     }
4412
4413   /* call enable ipv6  */
4414   enable_ip6_interface (vm, sw_if_index);
4415
4416   ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4417
4418   if (ri != ~0)
4419     {
4420       radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4421
4422       /* save if link local address (overwrite default) */
4423
4424       /* delete the old one */
4425       error = ip6_add_del_interface_address (vm, sw_if_index,
4426                                              &radv_info->link_local_address,
4427                                              128, 1 /* is_del */ );
4428
4429       if (!error)
4430         {
4431           /* add the new one */
4432           error = ip6_add_del_interface_address (vm, sw_if_index,
4433                                                  address, 128,
4434                                                  0 /* is_del */ );
4435
4436           if (!error)
4437             {
4438               radv_info->link_local_address = *address;
4439             }
4440         }
4441     }
4442   else
4443     {
4444       vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4445       error = clib_error_return (0, "ip6 not enabled for interface",
4446                                  format_unformat_error);
4447     }
4448   return error;
4449 }
4450
4451 clib_error_t *
4452 set_ip6_link_local_address_cmd (vlib_main_t * vm,
4453                                 unformat_input_t * input,
4454                                 vlib_cli_command_t * cmd)
4455 {
4456   vnet_main_t *vnm = vnet_get_main ();
4457   clib_error_t *error = 0;
4458   u32 sw_if_index;
4459   ip6_address_t ip6_addr;
4460
4461   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4462     {
4463       /* get the rest of the command */
4464       while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4465         {
4466           if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
4467             break;
4468           else
4469             return (unformat_parse_error (input));
4470         }
4471     }
4472   error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
4473   return error;
4474 }
4475
4476 /*?
4477  * This command is used to assign an IPv6 Link-local address to an
4478  * interface. This command will enable IPv6 on an interface if it
4479  * is not already enabled. Use the '<em>show ip6 interface</em>' command
4480  * to display the assigned Link-local address.
4481  *
4482  * @cliexpar
4483  * Example of how to assign an IPv6 Link-local address to an interface:
4484  * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
4485 ?*/
4486 /* *INDENT-OFF* */
4487 VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
4488 {
4489   .path = "set ip6 link-local address",
4490   .short_help = "set ip6 link-local address <interface> <ip6-address>",
4491   .function = set_ip6_link_local_address_cmd,
4492 };
4493 /* *INDENT-ON* */
4494
4495 /**
4496  * @brief callback when an interface address is added or deleted
4497  */
4498 static void
4499 ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4500                                         uword opaque,
4501                                         u32 sw_if_index,
4502                                         ip6_address_t * address,
4503                                         u32 address_length,
4504                                         u32 if_address_index, u32 is_delete)
4505 {
4506   vnet_main_t *vnm = vnet_get_main ();
4507   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4508   u32 ri;
4509   vlib_main_t *vm = vnm->vlib_main;
4510   ip6_radv_t *radv_info;
4511   ip6_address_t a;
4512
4513   /* create solicited node multicast address for this interface adddress */
4514   ip6_set_solicited_node_multicast_address (&a, 0);
4515
4516   a.as_u8[0xd] = address->as_u8[0xd];
4517   a.as_u8[0xe] = address->as_u8[0xe];
4518   a.as_u8[0xf] = address->as_u8[0xf];
4519
4520   if (!is_delete)
4521     {
4522       /* try to  create radv_info - does nothing if ipv6 already enabled */
4523       enable_ip6_interface (vm, sw_if_index);
4524
4525       /* look up the radv_t  information for this interface */
4526       vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4527                                sw_if_index, ~0);
4528       ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4529       if (ri != ~0)
4530         {
4531           /* get radv_info */
4532           radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4533
4534           /* add address */
4535           if (!ip6_address_is_link_local_unicast (address))
4536             radv_info->ref_count++;
4537
4538           ip6_neighbor_add_mld_prefix (radv_info, &a);
4539         }
4540     }
4541   else
4542     {
4543
4544       /* delete */
4545       /* look up the radv_t  information for this interface */
4546       vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4547                                sw_if_index, ~0);
4548       ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4549
4550       if (ri != ~0)
4551         {
4552           /* get radv_info */
4553           radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4554
4555           ip6_neighbor_del_mld_prefix (radv_info, &a);
4556
4557           /* if interface up send MLDP "report" */
4558           radv_info->all_routers_mcast = 0;
4559
4560           /* add address */
4561           if (!ip6_address_is_link_local_unicast (address))
4562             radv_info->ref_count--;
4563         }
4564       /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4565       disable_ip6_interface (vm, sw_if_index);
4566     }
4567 }
4568
4569 clib_error_t *
4570 ip6_set_neighbor_limit (u32 neighbor_limit)
4571 {
4572   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4573
4574   nm->limit_neighbor_cache_size = neighbor_limit;
4575   return 0;
4576 }
4577
4578 static void
4579 ip6_neighbor_table_bind (ip6_main_t * im,
4580                          uword opaque,
4581                          u32 sw_if_index,
4582                          u32 new_fib_index, u32 old_fib_index)
4583 {
4584   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4585   ip6_neighbor_t *n = NULL;
4586   u32 i, *to_re_add = 0;
4587
4588   /* *INDENT-OFF* */
4589   pool_foreach (n, nm->neighbor_pool,
4590   ({
4591     if (n->key.sw_if_index == sw_if_index)
4592       vec_add1 (to_re_add, n - nm->neighbor_pool);
4593   }));
4594   /* *INDENT-ON* */
4595
4596   for (i = 0; i < vec_len (to_re_add); i++)
4597     {
4598       n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4599       ip6_neighbor_adj_fib_remove (n, old_fib_index);
4600       ip6_neighbor_adj_fib_add (n, new_fib_index);
4601     }
4602   vec_free (to_re_add);
4603 }
4604
4605 static clib_error_t *
4606 ip6_neighbor_init (vlib_main_t * vm)
4607 {
4608   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4609   ip6_main_t *im = &ip6_main;
4610
4611   mhash_init (&nm->neighbor_index_by_key,
4612               /* value size */ sizeof (uword),
4613               /* key size */ sizeof (ip6_neighbor_key_t));
4614
4615   icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4616                        ip6_icmp_neighbor_solicitation_node.index);
4617   icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4618                        ip6_icmp_neighbor_advertisement_node.index);
4619   icmp6_register_type (vm, ICMP6_router_solicitation,
4620                        ip6_icmp_router_solicitation_node.index);
4621   icmp6_register_type (vm, ICMP6_router_advertisement,
4622                        ip6_icmp_router_advertisement_node.index);
4623
4624   /* handler node for ip6 neighbor discovery events and timers */
4625   vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4626
4627   /* add call backs */
4628   ip6_add_del_interface_address_callback_t cb;
4629   memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4630
4631   /* when an interface address changes... */
4632   cb.function = ip6_neighbor_add_del_interface_address;
4633   cb.function_opaque = 0;
4634   vec_add1 (im->add_del_interface_address_callbacks, cb);
4635
4636   ip6_table_bind_callback_t cbt;
4637   cbt.function = ip6_neighbor_table_bind;
4638   cbt.function_opaque = 0;
4639   vec_add1 (im->table_bind_callbacks, cbt);
4640
4641   mhash_init (&nm->pending_resolutions_by_address,
4642               /* value size */ sizeof (uword),
4643               /* key size */ sizeof (ip6_address_t));
4644
4645   mhash_init (&nm->mac_changes_by_address,
4646               /* value size */ sizeof (uword),
4647               /* key size */ sizeof (ip6_address_t));
4648
4649   /* default, configurable */
4650   nm->limit_neighbor_cache_size = 50000;
4651
4652   nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4653
4654   nm->ip6_ra_publisher_node = (uword) ~ 0;
4655
4656 #if 0
4657   /* $$$$ Hack fix for today */
4658   vec_validate_init_empty
4659     (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
4660 #endif
4661
4662   return 0;
4663 }
4664
4665 VLIB_INIT_FUNCTION (ip6_neighbor_init);
4666
4667
4668 void
4669 vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4670                                              void *address_arg,
4671                                              uword node_index,
4672                                              uword type_opaque, uword data)
4673 {
4674   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4675   ip6_address_t *address = address_arg;
4676   uword *p;
4677   pending_resolution_t *pr;
4678
4679   pool_get (nm->pending_resolutions, pr);
4680
4681   pr->next_index = ~0;
4682   pr->node_index = node_index;
4683   pr->type_opaque = type_opaque;
4684   pr->data = data;
4685
4686   p = mhash_get (&nm->pending_resolutions_by_address, address);
4687   if (p)
4688     {
4689       /* Insert new resolution at the head of the list */
4690       pr->next_index = p[0];
4691       mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4692     }
4693
4694   mhash_set (&nm->pending_resolutions_by_address, address,
4695              pr - nm->pending_resolutions, 0 /* old value */ );
4696 }
4697
4698 int
4699 vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4700                                   void *data_callback,
4701                                   u32 pid,
4702                                   void *address_arg,
4703                                   uword node_index,
4704                                   uword type_opaque, uword data, int is_add)
4705 {
4706   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4707   ip6_address_t *address = address_arg;
4708
4709   /* Try to find an existing entry */
4710   u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4711   u32 *p = first;
4712   pending_resolution_t *mc;
4713   while (p && *p != ~0)
4714     {
4715       mc = pool_elt_at_index (nm->mac_changes, *p);
4716       if (mc->node_index == node_index && mc->type_opaque == type_opaque
4717           && mc->pid == pid)
4718         break;
4719       p = &mc->next_index;
4720     }
4721
4722   int found = p && *p != ~0;
4723   if (is_add)
4724     {
4725       if (found)
4726         return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4727
4728       pool_get (nm->mac_changes, mc);
4729       *mc = (pending_resolution_t)
4730       {
4731       .next_index = ~0,.node_index = node_index,.type_opaque =
4732           type_opaque,.data = data,.data_callback = data_callback,.pid =
4733           pid,};
4734
4735       /* Insert new resolution at the end of the list */
4736       u32 new_idx = mc - nm->mac_changes;
4737       if (p)
4738         p[0] = new_idx;
4739       else
4740         mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
4741     }
4742   else
4743     {
4744       if (!found)
4745         return VNET_API_ERROR_NO_SUCH_ENTRY;
4746
4747       /* Clients may need to clean up pool entries, too */
4748       void (*fp) (u32, u8 *) = data_callback;
4749       if (fp)
4750         (*fp) (mc->data, 0 /* no new mac addrs */ );
4751
4752       /* Remove the entry from the list and delete the entry */
4753       *p = mc->next_index;
4754       pool_put (nm->mac_changes, mc);
4755
4756       /* Remove from hash if we deleted the last entry */
4757       if (*p == ~0 && p == first)
4758         mhash_unset (&nm->mac_changes_by_address, address, 0);
4759     }
4760   return 0;
4761 }
4762
4763 int
4764 vnet_ip6_nd_term (vlib_main_t * vm,
4765                   vlib_node_runtime_t * node,
4766                   vlib_buffer_t * p0,
4767                   ethernet_header_t * eth,
4768                   ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
4769 {
4770   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4771   icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4772
4773   ndh = ip6_next_header (ip);
4774   if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4775       ndh->icmp.type != ICMP6_neighbor_advertisement)
4776     return 0;
4777
4778   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4779                      (p0->flags & VLIB_BUFFER_IS_TRACED)))
4780     {
4781       u8 *t0 = vlib_add_trace (vm, node, p0,
4782                                sizeof (icmp6_input_trace_t));
4783       clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4784     }
4785
4786   /* Check if anyone want ND events for L2 BDs */
4787   if (PREDICT_FALSE
4788       (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4789        && !ip6_address_is_link_local_unicast (&ip->src_address)))
4790     {
4791       vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
4792     }
4793
4794   /* Check if MAC entry exsist for solicited target IP */
4795   if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4796     {
4797       icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
4798       l2_bridge_domain_t *bd_config;
4799       u8 *macp;
4800
4801       opt = (void *) (ndh + 1);
4802       if ((opt->header.type !=
4803            ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4804           (opt->header.n_data_u64s != 1))
4805         return 0;               /* source link layer address option not present */
4806
4807       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
4808       macp =
4809         (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
4810       if (macp)
4811         {                       /* found ip-mac entry, generate eighbor advertisement response */
4812           int bogus_length;
4813           vlib_node_runtime_t *error_node =
4814             vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
4815           ip->dst_address = ip->src_address;
4816           ip->src_address = ndh->target_address;
4817           ip->hop_limit = 255;
4818           opt->header.type =
4819             ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
4820           clib_memcpy (opt->ethernet_address, macp, 6);
4821           ndh->icmp.type = ICMP6_neighbor_advertisement;
4822           ndh->advertisement_flags = clib_host_to_net_u32
4823             (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4824              ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4825           ndh->icmp.checksum = 0;
4826           ndh->icmp.checksum =
4827             ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4828           clib_memcpy (eth->dst_address, eth->src_address, 6);
4829           clib_memcpy (eth->src_address, macp, 6);
4830           vlib_error_count (vm, error_node->node_index,
4831                             ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
4832           return 1;
4833         }
4834     }
4835
4836   return 0;
4837
4838 }
4839
4840 int
4841 ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4842 {
4843   u32 fib_index;
4844
4845   fib_prefix_t pfx = {
4846     .fp_len = 128,
4847     .fp_proto = FIB_PROTOCOL_IP6,
4848     .fp_addr = {
4849                 .ip6 = *addr,
4850                 },
4851   };
4852   ip46_address_t nh = {
4853     .ip6 = *addr,
4854   };
4855
4856   fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4857
4858   if (~0 == fib_index)
4859     return VNET_API_ERROR_NO_SUCH_FIB;
4860
4861   if (is_del)
4862     {
4863       fib_table_entry_path_remove (fib_index,
4864                                    &pfx,
4865                                    FIB_SOURCE_IP6_ND_PROXY,
4866                                    DPO_PROTO_IP6,
4867                                    &nh,
4868                                    sw_if_index,
4869                                    ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4870       /* flush the ND cache of this address if it's there */
4871       vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4872                                         sw_if_index, addr, NULL, 0);
4873     }
4874   else
4875     {
4876       fib_table_entry_path_add (fib_index,
4877                                 &pfx,
4878                                 FIB_SOURCE_IP6_ND_PROXY,
4879                                 FIB_ENTRY_FLAG_NONE,
4880                                 DPO_PROTO_IP6,
4881                                 &nh,
4882                                 sw_if_index,
4883                                 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4884     }
4885   return (0);
4886 }
4887
4888 static clib_error_t *
4889 set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4890                       unformat_input_t * input, vlib_cli_command_t * cmd)
4891 {
4892   vnet_main_t *vnm = vnet_get_main ();
4893   clib_error_t *error = 0;
4894   ip6_address_t addr;
4895   u32 sw_if_index;
4896   u8 is_del = 0;
4897
4898   if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4899     {
4900       /* get the rest of the command */
4901       while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4902         {
4903           if (unformat (input, "%U", unformat_ip6_address, &addr))
4904             break;
4905           else if (unformat (input, "delete") || unformat (input, "del"))
4906             is_del = 1;
4907           else
4908             return (unformat_parse_error (input));
4909         }
4910     }
4911
4912   ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4913
4914   return error;
4915 }
4916
4917 /* *INDENT-OFF* */
4918 VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4919 {
4920   .path = "set ip6 nd proxy",
4921   .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4922   .function = set_ip6_nd_proxy_cmd,
4923 };
4924 /* *INDENT-ON* */
4925
4926 void
4927 ethernet_ndp_change_mac (u32 sw_if_index)
4928 {
4929   ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4930   ip6_neighbor_t *n;
4931   adj_index_t ai;
4932
4933   /* *INDENT-OFF* */
4934   pool_foreach (n, nm->neighbor_pool,
4935   ({
4936     if (n->key.sw_if_index == sw_if_index)
4937       {
4938         adj_nbr_walk_nh6 (sw_if_index,
4939                           &n->key.ip6_address,
4940                           ip6_nd_mk_complete_walk, n);
4941       }
4942   }));
4943   /* *INDENT-ON* */
4944
4945   ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4946
4947   if (ADJ_INDEX_INVALID != ai)
4948     adj_glean_update_rewrite (ai);
4949 }
4950
4951 void
4952 send_ip6_na (vlib_main_t * vm, const vnet_hw_interface_t * hi)
4953 {
4954   ip6_main_t *i6m = &ip6_main;
4955   u32 sw_if_index = hi->sw_if_index;
4956   ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4957
4958   send_ip6_na_w_addr (vm, ip6_addr, hi);
4959 }
4960
4961 void
4962 send_ip6_na_w_addr (vlib_main_t * vm,
4963                     const ip6_address_t * ip6_addr,
4964                     const vnet_hw_interface_t * hi)
4965 {
4966   ip6_main_t *i6m = &ip6_main;
4967   u32 sw_if_index = hi->sw_if_index;
4968
4969   if (ip6_addr)
4970     {
4971       clib_warning
4972         ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4973          format_ip6_address, ip6_addr, sw_if_index);
4974
4975       /* Form unsolicited neighbor advertisement packet from NS pkt template */
4976       int bogus_length;
4977       u32 bi = 0;
4978       icmp6_neighbor_solicitation_header_t *h =
4979         vlib_packet_template_get_packet (vm,
4980                                          &i6m->discover_neighbor_packet_template,
4981                                          &bi);
4982       ip6_set_reserved_multicast_address (&h->ip.dst_address,
4983                                           IP6_MULTICAST_SCOPE_link_local,
4984                                           IP6_MULTICAST_GROUP_ID_all_hosts);
4985       h->ip.src_address = ip6_addr[0];
4986       h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4987       h->neighbor.target_address = ip6_addr[0];
4988       h->neighbor.advertisement_flags = clib_host_to_net_u32
4989         (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4990       clib_memcpy (h->link_layer_option.ethernet_address,
4991                    hi->hw_address, vec_len (hi->hw_address));
4992       h->neighbor.icmp.checksum =
4993         ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4994       ASSERT (bogus_length == 0);
4995
4996       /* Setup MAC header with IP6 Etype and mcast DMAC */
4997       vlib_buffer_t *b = vlib_get_buffer (vm, bi);
4998       vlib_buffer_advance (b, -sizeof (ethernet_header_t));
4999       ethernet_header_t *e = vlib_buffer_get_current (b);
5000       e->type = clib_host_to_net_u16 (ETHERNET_TYPE_IP6);
5001       clib_memcpy (e->src_address, hi->hw_address, sizeof (e->src_address));
5002       ip6_multicast_ethernet_address (e->dst_address,
5003                                       IP6_MULTICAST_GROUP_ID_all_hosts);
5004
5005       /* Send unsolicited ND advertisement packet out the specified interface */
5006       vnet_buffer (b)->sw_if_index[VLIB_RX] =
5007         vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5008       vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5009       u32 *to_next = vlib_frame_vector_args (f);
5010       to_next[0] = bi;
5011       f->n_vectors = 1;
5012       vlib_put_frame_to_node (vm, hi->output_node_index, f);
5013     }
5014 }
5015
5016 /*
5017  * fd.io coding-style-patch-verification: ON
5018  *
5019  * Local Variables:
5020  * eval: (c-set-style "gnu")
5021  * End:
5022  */