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