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