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