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