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