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