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