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