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