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