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