blob: a3d95eee9fbfe828b89bbae3e549522b58053fe0 [file] [log] [blame]
Neale Rannscbe25aa2019-09-30 10:53:31 +00001/*
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/ip6-nd/ip6_ra.h>
19
20#include <vnet/ip/ip.h>
21#include <vnet/ip-neighbor/ip_neighbor_dp.h>
22
23#include <vnet/fib/ip6_fib.h>
24#include <vnet/ip/ip6_link.h>
25
26/**
27 * @file
28 * @brief IPv6 Router Advertisements.
29 *
30 * The files contains the API and CLI code for managing IPv6 RAs
31 */
32
33/* *INDENT-OFF* */
34/* Router solicitation packet format for ethernet. */
35typedef CLIB_PACKED (struct
36{
37 ip6_header_t ip;
38 icmp6_neighbor_discovery_header_t neighbor;
39 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
40 link_layer_option;
41}) icmp6_router_solicitation_header_t;
42
43/* router advertisement packet format for ethernet. */
44typedef CLIB_PACKED (struct
45{
46 ip6_header_t ip;
47 icmp6_router_advertisement_header_t router;
48 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
49 link_layer_option;
50 icmp6_neighbor_discovery_mtu_option_t mtu_option;
51 icmp6_neighbor_discovery_prefix_information_option_t
52 prefix[0];
53}) icmp6_router_advertisement_packet_t;
54/* *INDENT-ON* */
55
56#define DEF_MAX_RADV_INTERVAL 200
57#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
58#define DEF_CURR_HOP_LIMIT 64
59#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
60#define MAX_DEF_RTR_LIFETIME 9000
61
62#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
63#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
64#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
65#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
66#define MAX_RA_DELAY_TIME .5 /* seconds */
67
68/* advertised prefix option */
69typedef struct
70{
71 /* basic advertised information */
72 ip6_address_t prefix;
73 u8 prefix_len;
74 int adv_on_link_flag;
75 int adv_autonomous_flag;
76 u32 adv_valid_lifetime_in_secs;
77 u32 adv_pref_lifetime_in_secs;
78
79 /* advertised values are computed from these times if decrementing */
80 f64 valid_lifetime_expires;
81 f64 pref_lifetime_expires;
82
83 /* local information */
84 int enabled;
85 int deprecated_prefix_flag;
86 int decrement_lifetime_flag;
87
88#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
89#define DEF_ADV_VALID_LIFETIME 2592000
90#define DEF_ADV_PREF_LIFETIME 604800
91
92 /* extensions are added here, mobile, DNS etc.. */
93} ip6_radv_prefix_t;
94
95typedef struct ip6_ra_t_
96{
97 /* advertised config information, zero means unspecified */
98 u8 curr_hop_limit;
99 int adv_managed_flag;
100 int adv_other_flag;
101 u16 adv_router_lifetime_in_sec;
102 u32 adv_neighbor_reachable_time_in_msec;
103 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
104
105 /* mtu option */
106 u32 adv_link_mtu;
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 ref_count;
117
118 /* prefix option */
119 ip6_radv_prefix_t *adv_prefixes_pool;
120
121 /* Hash table mapping address to index in interface advertised prefix pool. */
122 mhash_t address_to_prefix_index;
123
124 f64 max_radv_interval;
125 f64 min_radv_interval;
126 f64 min_delay_between_radv;
127 f64 max_delay_between_radv;
128 f64 max_rtr_default_lifetime;
129
130 f64 last_radv_time;
131 f64 last_multicast_time;
132 f64 next_multicast_time;
133
134
135 u32 initial_adverts_count;
136 f64 initial_adverts_interval;
137 u32 initial_adverts_sent;
138
139 /* stats */
140 u32 n_advertisements_sent;
141 u32 n_solicitations_rcvd;
142 u32 n_solicitations_dropped;
143
144 /* router solicitations sending state */
145 u8 keep_sending_rs; /* when true then next fields are valid */
146 icmp6_send_router_solicitation_params_t params;
147 f64 sleep_interval;
148 f64 due_time;
149 u32 n_left;
150 f64 start_time;
151 vlib_buffer_t *buffer;
152
153 u32 seed;
154
155} ip6_ra_t;
156
157static ip6_link_delegate_id_t ip6_ra_delegate_id;
158static ip6_ra_t *ip6_ra_pool;
159
160
161/* vector of registered RA report listeners */
162static ip6_ra_report_notify_t *ip6_ra_listeners;
163
164static int ip6_ra_publish (ip6_ra_report_t * r);
165
166void
167ip6_ra_report_register (ip6_ra_report_notify_t fn)
168{
169 ip6_ra_report_notify_t *listener;
170 vec_foreach (listener, ip6_ra_listeners)
171 {
172 if (*listener == fn)
173 return;
174 }
175
176 vec_add1 (ip6_ra_listeners, fn);
177}
178
179void
180ip6_ra_report_unregister (ip6_ra_report_notify_t fn)
181{
182 u32 ii;
183
184 vec_foreach_index (ii, ip6_ra_listeners)
185 {
186 if (ip6_ra_listeners[ii] == fn)
187 {
188 vec_del1 (ip6_ra_listeners, ii);
189 break;
190 }
191 }
192}
193
194static inline ip6_ra_t *
195ip6_ra_get_itf (u32 sw_if_index)
196{
197 index_t rai;
198
199 rai = ip6_link_delegate_get (sw_if_index, ip6_ra_delegate_id);
200
201 if (INDEX_INVALID != rai)
202 return (pool_elt_at_index (ip6_ra_pool, rai));
203
204 return (NULL);
205}
206
207/* for "syslogging" - use elog for now */
208#define foreach_log_level \
209 _ (DEBUG, "DEBUG") \
210 _ (INFO, "INFORMATION") \
211 _ (NOTICE, "NOTICE") \
212 _ (WARNING, "WARNING") \
213 _ (ERR, "ERROR") \
214 _ (CRIT, "CRITICAL") \
215 _ (ALERT, "ALERT") \
216 _ (EMERG, "EMERGENCY")
217
218typedef enum
219{
220#define _(f,s) LOG_##f,
221 foreach_log_level
222#undef _
223} log_level_t;
224
225static char *log_level_strings[] = {
226#define _(f,s) s,
227 foreach_log_level
228#undef _
229};
230
231static int logmask = 1 << LOG_DEBUG;
232
233static void
234ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
235{
236 /* just use elog for now */
237 u8 *what;
238 va_list va;
239
240 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
241 return;
242
243 va_start (va, fmt);
244 if (fmt)
245 {
246 what = va_format (0, fmt, &va);
247
248 ELOG_TYPE_DECLARE (e) =
249 {
250 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
251 struct
252 {
253 u32 s[2];
254 } *ed;
255 ed = ELOG_DATA (&vm->elog_main, e);
256 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
257 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
258 }
259 va_end (va);
260 return;
261}
262
263/* ipv6 neighbor discovery - router advertisements */
264typedef enum
265{
266 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
267 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
268 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
269 ICMP6_ROUTER_SOLICITATION_N_NEXT,
270} icmp6_router_solicitation_or_advertisement_next_t;
271
272static_always_inline uword
273icmp6_router_solicitation (vlib_main_t * vm,
274 vlib_node_runtime_t * node, vlib_frame_t * frame)
275{
276 vnet_main_t *vnm = vnet_get_main ();
277 ip6_main_t *im = &ip6_main;
278 uword n_packets = frame->n_vectors;
279 u32 *from, *to_next;
280 u32 n_left_from, n_left_to_next, next_index;
281 u32 n_advertisements_sent = 0;
282 int bogus_length;
283
284 icmp6_neighbor_discovery_option_type_t option_type;
285
286 vlib_node_runtime_t *error_node =
287 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
288
289 from = vlib_frame_vector_args (frame);
290 n_left_from = n_packets;
291 next_index = node->cached_next_index;
292
293 if (node->flags & VLIB_NODE_FLAG_TRACE)
294 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
295 /* stride */ 1,
296 sizeof (icmp6_input_trace_t));
297
298 /* source may append his LL address */
299 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
300
301 while (n_left_from > 0)
302 {
303 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
304
305 while (n_left_from > 0 && n_left_to_next > 0)
306 {
307 vlib_buffer_t *p0;
308 ip6_header_t *ip0;
309 ip6_ra_t *radv_info = NULL;
310
311 icmp6_neighbor_discovery_header_t *h0;
312 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
313
314 u32 bi0, options_len0, sw_if_index0, next0, error0;
315 u32 is_solicitation = 1, is_dropped = 0;
316 u32 is_unspecified, is_link_local;
317
318 bi0 = to_next[0] = from[0];
319
320 from += 1;
321 to_next += 1;
322 n_left_from -= 1;
323 n_left_to_next -= 1;
324
325 p0 = vlib_get_buffer (vm, bi0);
326 ip0 = vlib_buffer_get_current (p0);
327 h0 = ip6_next_header (ip0);
328 options_len0 =
329 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
330 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
331 is_link_local =
332 ip6_address_is_link_local_unicast (&ip0->src_address);
333
334 error0 = ICMP6_ERROR_NONE;
335 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
336
337 /* check if solicitation (not from nd_timer node) */
338 if (ip6_address_is_unspecified (&ip0->dst_address))
339 is_solicitation = 0;
340
341 /* Check that source address is unspecified, link-local or else on-link. */
342 if (!is_unspecified && !is_link_local)
343 {
344 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
345
346 if (ADJ_INDEX_INVALID != src_adj_index0)
347 {
348 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
349
350 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
351 ?
352 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
353 : error0);
354 }
355 else
356 {
357 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
358 }
359 }
360
361 /* check for source LL option and process */
362 o0 = (void *) (h0 + 1);
363 o0 = ((options_len0 == 8
364 && o0->header.type == option_type
365 && o0->header.n_data_u64s == 1) ? o0 : 0);
366
367 /* if src address unspecified IGNORE any options */
368 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
369 !is_unspecified && !is_link_local))
370 {
Neale Rannsdc617b82020-08-20 08:22:56 +0000371 /* *INDENT-OFF* */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000372 ip_neighbor_learn_t learn = {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000373 .sw_if_index = sw_if_index0,
Neale Rannsdc617b82020-08-20 08:22:56 +0000374 .ip = {
375 .ip.ip6 = ip0->src_address,
376 .version = AF_IP6,
377 },
Neale Rannscbe25aa2019-09-30 10:53:31 +0000378 };
Neale Rannsdc617b82020-08-20 08:22:56 +0000379 /* *INDENT-ON* */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000380 memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac));
381 ip_neighbor_learn_dp (&learn);
382 }
383
384 /* default is to drop */
385 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
386
387 if (error0 == ICMP6_ERROR_NONE)
388 {
389 vnet_sw_interface_t *sw_if0;
390 ethernet_interface_t *eth_if0;
391 u32 adj_index0;
392
393 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
394 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
395 eth_if0 =
396 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
397
398 /* only support ethernet interface type for now */
399 error0 =
400 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
401 : error0;
402
403 if (error0 == ICMP6_ERROR_NONE)
404 {
405 /* adjust the sizeof the buffer to just include the ipv6 header */
406 p0->current_length -=
407 (options_len0 +
408 sizeof (icmp6_neighbor_discovery_header_t));
409
410 radv_info = ip6_ra_get_itf (sw_if_index0);
411
412 error0 = ((!radv_info) ?
413 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
414 error0);
415
416 if (error0 == ICMP6_ERROR_NONE)
417 {
418 f64 now = vlib_time_now (vm);
419
420 /* for solicited adverts - need to rate limit */
421 if (is_solicitation)
422 {
423 if (0 != radv_info->last_radv_time &&
424 (now - radv_info->last_radv_time) <
425 MIN_DELAY_BETWEEN_RAS)
426 is_dropped = 1;
427 else
428 radv_info->last_radv_time = now;
429 }
430
431 /* send now */
432 icmp6_router_advertisement_header_t rh;
433
434 rh.icmp.type = ICMP6_router_advertisement;
435 rh.icmp.code = 0;
436 rh.icmp.checksum = 0;
437
438 rh.current_hop_limit = radv_info->curr_hop_limit;
439 rh.router_lifetime_in_sec =
440 clib_host_to_net_u16
441 (radv_info->adv_router_lifetime_in_sec);
442 rh.
443 time_in_msec_between_retransmitted_neighbor_solicitations
444 =
445 clib_host_to_net_u32 (radv_info->
446 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
447 rh.neighbor_reachable_time_in_msec =
448 clib_host_to_net_u32 (radv_info->
449 adv_neighbor_reachable_time_in_msec);
450
451 rh.flags =
452 (radv_info->adv_managed_flag) ?
453 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
454 0;
455 rh.flags |=
456 ((radv_info->adv_other_flag) ?
457 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
458 0);
459
460
461 u16 payload_length =
462 sizeof (icmp6_router_advertisement_header_t);
463
464 if (vlib_buffer_add_data
465 (vm, &bi0, (void *) &rh,
466 sizeof (icmp6_router_advertisement_header_t)))
467 {
468 /* buffer allocation failed, drop the pkt */
469 error0 = ICMP6_ERROR_ALLOC_FAILURE;
470 goto drop0;
471 }
472
473 if (radv_info->adv_link_layer_address)
474 {
475 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
476 h;
477
478 h.header.type =
479 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
480 h.header.n_data_u64s = 1;
481
482 /* copy ll address */
483 clib_memcpy (&h.ethernet_address[0],
Benoît Ganneb44c77d2020-10-20 16:24:17 +0200484 &eth_if0->address, 6);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000485
486 if (vlib_buffer_add_data
487 (vm, &bi0, (void *) &h,
488 sizeof
489 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
490 {
491 error0 = ICMP6_ERROR_ALLOC_FAILURE;
492 goto drop0;
493 }
494
495 payload_length +=
496 sizeof
497 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
498 }
499
500 /* add MTU option */
501 if (radv_info->adv_link_mtu)
502 {
503 icmp6_neighbor_discovery_mtu_option_t h;
504
505 h.unused = 0;
506 h.mtu =
507 clib_host_to_net_u32 (radv_info->adv_link_mtu);
508 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
509 h.header.n_data_u64s = 1;
510
511 payload_length +=
512 sizeof (icmp6_neighbor_discovery_mtu_option_t);
513
514 if (vlib_buffer_add_data
515 (vm, &bi0, (void *) &h,
516 sizeof
517 (icmp6_neighbor_discovery_mtu_option_t)))
518 {
519 error0 = ICMP6_ERROR_ALLOC_FAILURE;
520 goto drop0;
521 }
522 }
523
524 /* add advertised prefix options */
525 ip6_radv_prefix_t *pr_info;
526
527 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100528 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
529 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000530 if(pr_info->enabled &&
531 (!pr_info->decrement_lifetime_flag
532 || (pr_info->pref_lifetime_expires >0)))
533 {
534 /* advertise this prefix */
535 icmp6_neighbor_discovery_prefix_information_option_t h;
536
537 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
538 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
539
540 h.dst_address_length = pr_info->prefix_len;
541
542 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
543 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
544
545 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
546 {
547 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
548 h.preferred_time = 0;
549 }
550 else
551 {
552 if(pr_info->decrement_lifetime_flag)
553 {
554 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
555 (pr_info->valid_lifetime_expires - now) : 0;
556
557 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
558 (pr_info->pref_lifetime_expires - now) : 0;
559 }
560
561 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
562 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
563 }
564 h.unused = 0;
565
Dave Barachd1586962020-02-20 16:17:58 -0500566 /* Handy for debugging, but too noisy... */
567 if (0 && CLIB_DEBUG > 0)
568 clib_warning
569 ("send RA for prefix %U/%d "
570 "sw_if_index %d valid %u preferred %u",
571 format_ip6_address, &pr_info->prefix,
572 pr_info->prefix_len, sw_if_index0,
573 clib_net_to_host_u32 (h.valid_time),
574 clib_net_to_host_u32 (h.preferred_time));
Neale Rannscbe25aa2019-09-30 10:53:31 +0000575
576 if (h.valid_time == 0)
Dave Barachd1586962020-02-20 16:17:58 -0500577 clib_warning ("BUG: send RA with valid_time 0");
Neale Rannscbe25aa2019-09-30 10:53:31 +0000578
579 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
580
581 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
582
583 if (vlib_buffer_add_data
584 (vm, &bi0, (void *)&h,
585 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
586 {
587 error0 = ICMP6_ERROR_ALLOC_FAILURE;
588 goto drop0;
589 }
590
591 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100592 }
Neale Rannscbe25aa2019-09-30 10:53:31 +0000593 /* *INDENT-ON* */
594
595 /* add additional options before here */
596
597 /* finish building the router advertisement... */
598 if (!is_unspecified && radv_info->send_unicast)
599 {
600 ip0->dst_address = ip0->src_address;
601 }
602 else
603 {
604 /* target address is all-nodes mcast addr */
605 ip6_set_reserved_multicast_address
606 (&ip0->dst_address,
607 IP6_MULTICAST_SCOPE_link_local,
608 IP6_MULTICAST_GROUP_ID_all_hosts);
609 }
610
611 /* source address MUST be the link-local address */
612 ip6_address_copy (&ip0->src_address,
613 ip6_get_link_local_address
614 (radv_info->sw_if_index));
615
616 ip0->hop_limit = 255;
617 ip0->payload_length =
618 clib_host_to_net_u16 (payload_length);
619
620 icmp6_router_advertisement_header_t *rh0 =
621 (icmp6_router_advertisement_header_t *) (ip0 + 1);
622 rh0->icmp.checksum =
623 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
624 &bogus_length);
625 ASSERT (bogus_length == 0);
626
627 /* setup output if and adjacency */
628 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
629 vnet_main.local_interface_sw_if_index;
630
631 if (is_solicitation)
632 {
633 ethernet_header_t *eth0;
634 /* Reuse current MAC header, copy SMAC to DMAC and
635 * interface MAC to SMAC */
636 vlib_buffer_reset (p0);
637 eth0 = vlib_buffer_get_current (p0);
638 clib_memcpy (eth0->dst_address, eth0->src_address,
639 6);
Benoît Ganneb44c77d2020-10-20 16:24:17 +0200640 clib_memcpy (eth0->src_address, &eth_if0->address,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000641 6);
642 next0 =
643 is_dropped ? next0 :
644 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
645 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
646 sw_if_index0;
647 }
648 else
649 {
650 adj_index0 = ip6_link_get_mcast_adj (sw_if_index0);
651 if (adj_index0 == INDEX_INVALID)
652 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
653 else
654 {
655 next0 =
656 is_dropped ? next0 :
657 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
658 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
659 adj_index0;
660 }
661 }
662 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
663
664 radv_info->n_solicitations_dropped += is_dropped;
665 radv_info->n_solicitations_rcvd += is_solicitation;
666
667 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
668 {
669 radv_info->n_advertisements_sent++;
670 n_advertisements_sent++;
671 }
672 }
673 }
674 }
675
676 drop0:
677 p0->error = error_node->errors[error0];
678
679 if (error0 != ICMP6_ERROR_NONE)
680 vlib_error_count (vm, error_node->node_index, error0, 1);
681
682 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
683 to_next, n_left_to_next,
684 bi0, next0);
685
686 }
687
688 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
689 }
690
691 /* Account for router advertisements sent. */
692 vlib_error_count (vm, error_node->node_index,
693 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
694 n_advertisements_sent);
695
696 return frame->n_vectors;
697}
698
699/* *INDENT-OFF* */
700VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
701{
702 .function = icmp6_router_solicitation,
703 .name = "icmp6-router-solicitation",
704
705 .vector_size = sizeof (u32),
706
707 .format_trace = format_icmp6_input_trace,
708
709 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
710 .next_nodes = {
711 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
712 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
713 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
714 },
715};
716/* *INDENT-ON* */
717
718 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
719static_always_inline uword
720icmp6_router_advertisement (vlib_main_t * vm,
721 vlib_node_runtime_t * node, vlib_frame_t * frame)
722{
723 vnet_main_t *vnm = vnet_get_main ();
724 uword n_packets = frame->n_vectors;
725 u32 *from, *to_next;
726 u32 n_left_from, n_left_to_next, next_index;
727 u32 n_advertisements_rcvd = 0;
728
729 vlib_node_runtime_t *error_node =
730 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
731
732 from = vlib_frame_vector_args (frame);
733 n_left_from = n_packets;
734 next_index = node->cached_next_index;
735
736 if (node->flags & VLIB_NODE_FLAG_TRACE)
737 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
738 /* stride */ 1,
739 sizeof (icmp6_input_trace_t));
740
741 while (n_left_from > 0)
742 {
743 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
744
745 while (n_left_from > 0 && n_left_to_next > 0)
746 {
747 vlib_buffer_t *p0;
748 ip6_header_t *ip0;
749 ip6_ra_t *radv_info = 0;
750 icmp6_router_advertisement_header_t *h0;
751 u32 bi0, options_len0, sw_if_index0, next0, error0;
752
753 bi0 = to_next[0] = from[0];
754
755 from += 1;
756 to_next += 1;
757 n_left_from -= 1;
758 n_left_to_next -= 1;
759
760 p0 = vlib_get_buffer (vm, bi0);
761 ip0 = vlib_buffer_get_current (p0);
762 h0 = ip6_next_header (ip0);
763 options_len0 =
764 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
765
766 error0 = ICMP6_ERROR_NONE;
767 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
768
769 /* Check that source address is link-local */
770 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
771 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
772
773 /* default is to drop */
774 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
775
776 n_advertisements_rcvd++;
777
778 if (error0 == ICMP6_ERROR_NONE)
779 {
780 vnet_sw_interface_t *sw_if0;
781 ethernet_interface_t *eth_if0;
782
783 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
784 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
785 eth_if0 =
786 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
787
788 /* only support ethernet interface type for now */
789 error0 =
790 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
791 : error0;
792
793 if (error0 == ICMP6_ERROR_NONE)
794 {
795 /* look up the radv_t information for this interface */
796 radv_info = ip6_ra_get_itf (sw_if_index0);
797
798 error0 = ((!radv_info) ?
799 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
800 error0);
801
802 if (error0 == ICMP6_ERROR_NONE)
803 {
804 radv_info->keep_sending_rs = 0;
805
806 ip6_ra_report_t r;
807
808 r.sw_if_index = sw_if_index0;
809 memcpy (&r.router_address, &ip0->src_address, 16);
810 r.current_hop_limit = h0->current_hop_limit;
811 r.flags = h0->flags;
812 r.router_lifetime_in_sec =
813 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
814 r.neighbor_reachable_time_in_msec =
815 clib_net_to_host_u32
816 (h0->neighbor_reachable_time_in_msec);
817 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
818 r.prefixes = 0;
819
820 /* validate advertised information */
821 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
822 && (h0->current_hop_limit !=
823 radv_info->curr_hop_limit))
824 {
825 ip6_neighbor_syslog (vm, LOG_WARNING,
826 "our AdvCurHopLimit on %U doesn't agree with %U",
827 format_vnet_sw_if_index_name,
828 vnm, sw_if_index0,
829 format_ip6_address,
830 &ip0->src_address);
831 }
832
833 if ((h0->flags &
834 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
835 != radv_info->adv_managed_flag)
836 {
837 ip6_neighbor_syslog (vm, LOG_WARNING,
838 "our AdvManagedFlag on %U doesn't agree with %U",
839 format_vnet_sw_if_index_name,
840 vnm, sw_if_index0,
841 format_ip6_address,
842 &ip0->src_address);
843 }
844
845 if ((h0->flags &
846 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
847 != radv_info->adv_other_flag)
848 {
849 ip6_neighbor_syslog (vm, LOG_WARNING,
850 "our AdvOtherConfigFlag on %U doesn't agree with %U",
851 format_vnet_sw_if_index_name,
852 vnm, sw_if_index0,
853 format_ip6_address,
854 &ip0->src_address);
855 }
856
857 if ((h0->
858 time_in_msec_between_retransmitted_neighbor_solicitations
859 && radv_info->
860 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
861 && (h0->
862 time_in_msec_between_retransmitted_neighbor_solicitations
863 !=
864 clib_host_to_net_u32 (radv_info->
865 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
866 {
867 ip6_neighbor_syslog (vm, LOG_WARNING,
868 "our AdvRetransTimer on %U doesn't agree with %U",
869 format_vnet_sw_if_index_name,
870 vnm, sw_if_index0,
871 format_ip6_address,
872 &ip0->src_address);
873 }
874
875 if ((h0->neighbor_reachable_time_in_msec &&
876 radv_info->adv_neighbor_reachable_time_in_msec) &&
877 (h0->neighbor_reachable_time_in_msec !=
878 clib_host_to_net_u32
879 (radv_info->adv_neighbor_reachable_time_in_msec)))
880 {
881 ip6_neighbor_syslog (vm, LOG_WARNING,
882 "our AdvReachableTime on %U doesn't agree with %U",
883 format_vnet_sw_if_index_name,
884 vnm, sw_if_index0,
885 format_ip6_address,
886 &ip0->src_address);
887 }
888
889 /* check for MTU or prefix options or .. */
890 u8 *opt_hdr = (u8 *) (h0 + 1);
891 while (options_len0 > 0)
892 {
893 icmp6_neighbor_discovery_option_header_t *o0 =
894 (icmp6_neighbor_discovery_option_header_t *)
895 opt_hdr;
896 int opt_len = o0->n_data_u64s << 3;
897 icmp6_neighbor_discovery_option_type_t option_type =
898 o0->type;
899
900 if (options_len0 < 2)
901 {
902 ip6_neighbor_syslog (vm, LOG_ERR,
903 "malformed RA packet on %U from %U",
904 format_vnet_sw_if_index_name,
905 vnm, sw_if_index0,
906 format_ip6_address,
907 &ip0->src_address);
908 break;
909 }
910
911 if (opt_len == 0)
912 {
913 ip6_neighbor_syslog (vm, LOG_ERR,
914 " zero length option in RA on %U from %U",
915 format_vnet_sw_if_index_name,
916 vnm, sw_if_index0,
917 format_ip6_address,
918 &ip0->src_address);
919 break;
920 }
921 else if (opt_len > options_len0)
922 {
923 ip6_neighbor_syslog (vm, LOG_ERR,
924 "option length in RA packet greater than total length on %U from %U",
925 format_vnet_sw_if_index_name,
926 vnm, sw_if_index0,
927 format_ip6_address,
928 &ip0->src_address);
929 break;
930 }
931
932 options_len0 -= opt_len;
933 opt_hdr += opt_len;
934
935 switch (option_type)
936 {
937 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
938 {
939 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
940 * h =
941 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
942 *) (o0);
943
944 if (opt_len < sizeof (*h))
945 break;
946
947 memcpy (r.slla, h->ethernet_address, 6);
948 }
949 break;
950
951 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
952 {
953 icmp6_neighbor_discovery_mtu_option_t *h =
954 (icmp6_neighbor_discovery_mtu_option_t
955 *) (o0);
956
957 if (opt_len < sizeof (*h))
958 break;
959
960 r.mtu = clib_net_to_host_u32 (h->mtu);
961
962 if ((h->mtu && radv_info->adv_link_mtu) &&
963 (h->mtu !=
964 clib_host_to_net_u32
965 (radv_info->adv_link_mtu)))
966 {
967 ip6_neighbor_syslog (vm, LOG_WARNING,
968 "our AdvLinkMTU on %U doesn't agree with %U",
969 format_vnet_sw_if_index_name,
970 vnm, sw_if_index0,
971 format_ip6_address,
972 &ip0->src_address);
973 }
974 }
975 break;
976
977 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
978 {
979 icmp6_neighbor_discovery_prefix_information_option_t
980 * h =
981 (icmp6_neighbor_discovery_prefix_information_option_t
982 *) (o0);
983
984 /* validate advertised prefix options */
985 ip6_radv_prefix_t *pr_info;
986 u32 preferred, valid;
987
988 if (opt_len < sizeof (*h))
989 break;
990
991 vec_validate (r.prefixes,
992 vec_len (r.prefixes));
993 ra_report_prefix_info_t *prefix =
994 vec_elt_at_index (r.prefixes,
995 vec_len (r.prefixes) - 1);
996
997 preferred =
998 clib_net_to_host_u32 (h->preferred_time);
999 valid = clib_net_to_host_u32 (h->valid_time);
1000
1001 prefix->preferred_time = preferred;
1002 prefix->valid_time = valid;
1003 prefix->flags = h->flags & 0xc0;
1004 prefix->prefix.fp_len = h->dst_address_length;
1005 prefix->prefix.fp_addr.ip6 = h->dst_address;
1006 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
1007
1008 /* look for matching prefix - if we our advertising it, it better be consistant */
1009 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001010 pool_foreach (pr_info, radv_info->adv_prefixes_pool)
1011 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001012
1013 ip6_address_t mask;
1014 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1015
1016 if(pr_info->enabled &&
1017 (pr_info->prefix_len == h->dst_address_length) &&
1018 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1019 {
1020 /* found it */
1021 if(!pr_info->decrement_lifetime_flag &&
1022 valid != pr_info->adv_valid_lifetime_in_secs)
1023 {
1024 ip6_neighbor_syslog(vm, LOG_WARNING,
1025 "our ADV validlifetime on %U for %U does not agree with %U",
1026 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1027 format_ip6_address, &h->dst_address);
1028 }
1029 if(!pr_info->decrement_lifetime_flag &&
1030 preferred != pr_info->adv_pref_lifetime_in_secs)
1031 {
1032 ip6_neighbor_syslog(vm, LOG_WARNING,
1033 "our ADV preferredlifetime on %U for %U does not agree with %U",
1034 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1035 format_ip6_address, &h->dst_address);
1036 }
1037 }
1038 break;
Damjan Marionb2c31b62020-12-13 21:47:40 +01001039 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001040 /* *INDENT-ON* */
1041 break;
1042 }
1043 default:
1044 /* skip this one */
1045 break;
1046 }
1047 }
1048 ip6_ra_publish (&r);
1049 }
1050 }
1051 }
1052
1053 p0->error = error_node->errors[error0];
1054
1055 if (error0 != ICMP6_ERROR_NONE)
1056 vlib_error_count (vm, error_node->node_index, error0, 1);
1057
1058 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1059 to_next, n_left_to_next,
1060 bi0, next0);
1061 }
1062
1063 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1064 }
1065
1066 /* Account for router advertisements received. */
1067 vlib_error_count (vm, error_node->node_index,
1068 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
1069 n_advertisements_rcvd);
1070
1071 return frame->n_vectors;
1072}
1073
1074/* *INDENT-OFF* */
1075VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
1076{
1077 .function = icmp6_router_advertisement,
1078 .name = "icmp6-router-advertisement",
1079
1080 .vector_size = sizeof (u32),
1081
1082 .format_trace = format_icmp6_input_trace,
1083
1084 .n_next_nodes = 1,
1085 .next_nodes = {
1086 [0] = "ip6-drop",
1087 },
1088};
1089/* *INDENT-ON* */
1090
1091static inline f64
1092random_f64_from_to (f64 from, f64 to)
1093{
1094 static u32 seed = 0;
1095 static u8 seed_set = 0;
1096 if (!seed_set)
1097 {
1098 seed = random_default_seed ();
1099 seed_set = 1;
1100 }
1101 return random_f64 (&seed) * (to - from) + from;
1102}
1103
1104static inline u8
1105get_mac_address (u32 sw_if_index, u8 * address)
1106{
1107 vnet_main_t *vnm = vnet_get_main ();
1108 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
1109 if (!hw_if->hw_address)
1110 return 1;
1111 clib_memcpy (address, hw_if->hw_address, 6);
1112 return 0;
1113}
1114
1115static inline vlib_buffer_t *
1116create_buffer_for_rs (vlib_main_t * vm, ip6_ra_t * radv_info)
1117{
1118 u32 bi0;
1119 vlib_buffer_t *p0;
1120 icmp6_router_solicitation_header_t *rh;
1121 u16 payload_length;
1122 int bogus_length;
1123 u32 sw_if_index;
1124
1125 sw_if_index = radv_info->sw_if_index;
1126
1127 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
1128 {
1129 clib_warning ("buffer allocation failure");
1130 return 0;
1131 }
1132
1133 p0 = vlib_get_buffer (vm, bi0);
1134 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
1135 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1136
1137 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
1138 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
1139
1140 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1141 ip6_link_get_mcast_adj (sw_if_index);
1142
1143 rh = vlib_buffer_get_current (p0);
1144 p0->current_length = sizeof (*rh);
1145
1146 rh->neighbor.icmp.type = ICMP6_router_solicitation;
1147 rh->neighbor.icmp.code = 0;
1148 rh->neighbor.icmp.checksum = 0;
1149 rh->neighbor.reserved_must_be_zero = 0;
1150
1151 rh->link_layer_option.header.type =
1152 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1153 if (0 != get_mac_address (sw_if_index,
1154 rh->link_layer_option.ethernet_address))
1155 {
1156 clib_warning ("interface with sw_if_index %u has no mac address",
1157 sw_if_index);
1158 vlib_buffer_free (vm, &bi0, 1);
1159 return 0;
1160 }
1161 rh->link_layer_option.header.n_data_u64s = 1;
1162
1163 payload_length = sizeof (rh->neighbor) + sizeof (u64);
1164
1165 rh->ip.ip_version_traffic_class_and_flow_label =
1166 clib_host_to_net_u32 (0x6 << 28);
1167 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
1168 rh->ip.protocol = IP_PROTOCOL_ICMP6;
1169 rh->ip.hop_limit = 255;
1170 ip6_address_copy (&rh->ip.src_address,
1171 ip6_get_link_local_address (radv_info->sw_if_index));
1172 /* set address ff02::2 */
1173 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
1174 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
1175
1176 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
1177 &rh->ip,
1178 &bogus_length);
1179
1180 return p0;
1181}
1182
1183static inline void
1184stop_sending_rs (vlib_main_t * vm, ip6_ra_t * ra)
1185{
1186 u32 bi0;
1187
1188 ra->keep_sending_rs = 0;
1189 if (ra->buffer)
1190 {
1191 bi0 = vlib_get_buffer_index (vm, ra->buffer);
1192 vlib_buffer_free (vm, &bi0, 1);
1193 ra->buffer = 0;
1194 }
1195}
1196
1197static inline bool
1198check_send_rs (vlib_main_t * vm, ip6_ra_t * radv_info, f64 current_time,
1199 f64 * due_time)
1200{
1201 vlib_buffer_t *p0;
1202 vlib_frame_t *f;
1203 u32 *to_next;
1204 u32 next_index;
1205 vlib_buffer_t *c0;
1206 u32 ci0;
1207
1208 icmp6_send_router_solicitation_params_t *params;
1209
1210 if (!radv_info->keep_sending_rs)
1211 return false;
1212
1213 params = &radv_info->params;
1214
1215 if (radv_info->due_time > current_time)
1216 {
1217 *due_time = radv_info->due_time;
1218 return true;
1219 }
1220
1221 p0 = radv_info->buffer;
1222
1223 next_index = ip6_rewrite_mcast_node.index;
1224
1225 c0 = vlib_buffer_copy (vm, p0);
Dave Barach954c7072020-04-08 08:14:57 -04001226 if (c0 == NULL)
1227 return radv_info->keep_sending_rs;
1228
Neale Rannscbe25aa2019-09-30 10:53:31 +00001229 ci0 = vlib_get_buffer_index (vm, c0);
1230
1231 f = vlib_get_frame_to_node (vm, next_index);
1232 to_next = vlib_frame_vector_args (f);
1233 to_next[0] = ci0;
1234 f->n_vectors = 1;
1235 vlib_put_frame_to_node (vm, next_index, f);
1236
1237 if (params->mrc != 0 && --radv_info->n_left == 0)
1238 stop_sending_rs (vm, radv_info);
1239 else
1240 {
1241 radv_info->sleep_interval =
1242 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
1243 if (radv_info->sleep_interval > params->mrt)
1244 radv_info->sleep_interval =
1245 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
1246
1247 radv_info->due_time = current_time + radv_info->sleep_interval;
1248
1249 if (params->mrd != 0
1250 && current_time > radv_info->start_time + params->mrd)
1251 stop_sending_rs (vm, radv_info);
1252 else
1253 *due_time = radv_info->due_time;
1254 }
1255
1256 return radv_info->keep_sending_rs;
1257}
1258
1259static uword
1260send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1261 vlib_frame_t * f0)
1262{
1263 uword *event_data = NULL;
1264 f64 sleep_time = 1e9;
1265 ip6_ra_t *radv_info;
1266 f64 current_time;
1267 f64 due_time;
1268 f64 dt = 0;
1269
1270 while (true)
1271 {
1272 vlib_process_wait_for_event_or_clock (vm, sleep_time);
1273 vlib_process_get_events (vm, &event_data);
1274 vec_reset_length (event_data);
1275
1276 current_time = vlib_time_now (vm);
1277 do
1278 {
1279 due_time = current_time + 1e9;
1280 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001281 pool_foreach (radv_info, ip6_ra_pool)
1282 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001283 if (check_send_rs (vm, radv_info, current_time, &dt)
1284 && (dt < due_time))
1285 due_time = dt;
Damjan Marionb2c31b62020-12-13 21:47:40 +01001286 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001287 /* *INDENT-ON* */
1288 current_time = vlib_time_now (vm);
1289 }
1290 while (due_time < current_time);
1291
1292 sleep_time = due_time - current_time;
1293 }
1294
1295 return 0;
1296}
1297
1298/* *INDENT-OFF* */
1299VLIB_REGISTER_NODE (ip6_rs_process_node) = {
1300 .function = send_rs_process,
1301 .type = VLIB_NODE_TYPE_PROCESS,
1302 .name = "ip6-rs-process",
1303};
1304/* *INDENT-ON* */
1305
1306void
1307icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
1308 const icmp6_send_router_solicitation_params_t
1309 * params)
1310{
1311 ip6_ra_t *ra;
1312
1313 ASSERT (~0 != sw_if_index);
1314
1315 ra = ip6_ra_get_itf (sw_if_index);
1316
1317 if (!ra)
1318 return;
1319
1320 stop_sending_rs (vm, ra);
1321
1322 if (!stop)
1323 {
1324 ra->keep_sending_rs = 1;
1325 ra->params = *params;
1326 ra->n_left = params->mrc;
1327 ra->start_time = vlib_time_now (vm);
1328 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
1329 ra->due_time = 0; /* send first packet ASAP */
1330 ra->buffer = create_buffer_for_rs (vm, ra);
1331 if (!ra->buffer)
1332 ra->keep_sending_rs = 0;
1333 else
1334 vlib_process_signal_event (vm, ip6_rs_process_node.index, 1, 0);
1335 }
1336}
1337
1338static const ethernet_interface_t *
1339ip6_ra_get_eth_itf (u32 sw_if_index)
1340{
1341 const vnet_sw_interface_t *sw;
1342
1343 /* lookup radv container - ethernet interfaces only */
1344 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
1345 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
1346 return (ethernet_get_interface (&ethernet_main, sw->hw_if_index));
1347
1348 return (NULL);
1349}
1350
1351/**
1352 * @brief called when IP6 is enabled on an interface
1353 *create and initialize router advertisement parameters with default
1354 * values for this intfc
1355 */
1356static void
1357ip6_ra_link_enable (u32 sw_if_index)
1358{
1359 const ethernet_interface_t *eth;
1360 ip6_ra_t *radv_info;
1361
1362 eth = ip6_ra_get_eth_itf (sw_if_index);
1363
1364 if (NULL == eth)
1365 return;
1366
1367 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
1368 ip6_ra_delegate_id));
1369
1370 pool_get_zero (ip6_ra_pool, radv_info);
1371
1372 radv_info->seed = (u32) clib_cpu_time_now ();
1373 random_u32 (&radv_info->seed);
1374
1375 radv_info->sw_if_index = sw_if_index;
1376 radv_info->max_radv_interval = DEF_MAX_RADV_INTERVAL;
1377 radv_info->min_radv_interval = DEF_MIN_RADV_INTERVAL;
1378 radv_info->curr_hop_limit = DEF_CURR_HOP_LIMIT;
1379 radv_info->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
1380
1381 /* send ll address source address option */
1382 radv_info->adv_link_layer_address = 1;
1383
1384 radv_info->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
1385 radv_info->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
1386 radv_info->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
1387
1388 radv_info->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
1389 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1390 radv_info->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1391
1392 /* deafult is to send */
1393 radv_info->send_radv = 1;
1394
1395 /* fill in delegate for this interface that will be needed later */
1396 radv_info->adv_link_mtu =
1397 vnet_sw_interface_get_mtu (vnet_get_main (), sw_if_index, VNET_MTU_IP6);
1398
1399 mhash_init (&radv_info->address_to_prefix_index, sizeof (uword),
1400 sizeof (ip6_address_t));
1401
1402 ip6_link_delegate_update (sw_if_index, ip6_ra_delegate_id,
1403 radv_info - ip6_ra_pool);
1404}
1405
1406static void
1407ip6_ra_delegate_disable (index_t rai)
1408{
1409 ip6_radv_prefix_t *p;
1410 ip6_ra_t *radv_info;
1411
1412 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
1413
1414 /* clean up prefix and MDP pools */
1415 /* *INDENT-OFF* */
1416 pool_flush(p, radv_info->adv_prefixes_pool,
1417 ({
1418 mhash_unset (&radv_info->address_to_prefix_index, &p->prefix, 0);
1419 }));
1420 /* *INDENT-ON* */
1421
1422 pool_free (radv_info->adv_prefixes_pool);
1423
1424 mhash_free (&radv_info->address_to_prefix_index);
1425
1426 pool_put (ip6_ra_pool, radv_info);
1427}
1428
Dave Barachd1586962020-02-20 16:17:58 -05001429void
1430ip6_ra_update_secondary_radv_info (ip6_address_t * address, u8 prefix_len,
1431 u32 primary_sw_if_index,
1432 u32 valid_time, u32 preferred_time)
1433{
1434 vlib_main_t *vm = vlib_get_main ();
1435 static u32 *radv_indices;
1436 ip6_ra_t *radv_info;
1437 int i;
1438 ip6_address_t mask;
1439 ip6_address_mask_from_width (&mask, prefix_len);
1440
1441 vec_reset_length (radv_indices);
1442 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001443 pool_foreach (radv_info, ip6_ra_pool)
1444 {
Dave Barachd1586962020-02-20 16:17:58 -05001445 vec_add1 (radv_indices, radv_info - ip6_ra_pool);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001446 }
Dave Barachd1586962020-02-20 16:17:58 -05001447 /* *INDENT-ON* */
1448
1449 /*
1450 * If we have another customer for this prefix,
1451 * tell the RA code about it...
1452 */
1453 for (i = 0; i < vec_len (radv_indices); i++)
1454 {
1455 ip6_radv_prefix_t *this_prefix;
1456 radv_info = pool_elt_at_index (ip6_ra_pool, radv_indices[i]);
1457
1458 /* We already took care of these timers... */
1459 if (radv_info->sw_if_index == primary_sw_if_index)
1460 continue;
1461
1462 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001463 pool_foreach (this_prefix, radv_info->adv_prefixes_pool)
1464 {
Dave Barachd1586962020-02-20 16:17:58 -05001465 if (this_prefix->prefix_len == prefix_len
1466 && ip6_address_is_equal_masked (&this_prefix->prefix, address,
1467 &mask))
1468 {
1469 int rv = ip6_ra_prefix (vm,
1470 radv_info->sw_if_index,
1471 address,
1472 prefix_len,
1473 0 /* use_default */,
1474 valid_time,
1475 preferred_time,
1476 0 /* no_advertise */,
1477 0 /* off_link */,
1478 0 /* no_autoconfig */,
1479 0 /* no_onlink */,
1480 0 /* is_no */);
1481 if (rv != 0)
1482 clib_warning ("ip6_neighbor_ra_prefix returned %d", rv);
1483 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001484 }
Dave Barachd1586962020-02-20 16:17:58 -05001485 /* *INDENT-ON*/
1486 }
1487}
1488
Neale Rannscbe25aa2019-09-30 10:53:31 +00001489/* send a RA or update the timer info etc.. */
1490static uword
1491ip6_ra_process_timer_event (vlib_main_t * vm,
1492 vlib_node_runtime_t * node, vlib_frame_t * frame)
1493{
1494 vnet_main_t *vnm = vnet_get_main ();
1495 ip6_ra_t *radv_info;
1496 vlib_frame_t *f = 0;
1497 u32 n_this_frame = 0;
1498 u32 n_left_to_next = 0;
1499 u32 *to_next = 0;
1500 u32 bo0;
1501 icmp6_router_solicitation_header_t *h0;
1502 vlib_buffer_t *b0;
1503 f64 now = vlib_time_now (vm);
1504
1505 /* Interface ip6 radv info list */
1506 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001507 pool_foreach (radv_info, ip6_ra_pool)
1508 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001509 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
1510 {
1511 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
1512 radv_info->next_multicast_time = now;
1513 radv_info->last_multicast_time = now;
1514 radv_info->last_radv_time = 0;
1515 continue;
1516 }
1517
1518 /* is it time to send a multicast RA on this interface? */
1519 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
1520 {
1521 u32 n_to_alloc = 1;
1522 u32 n_allocated;
1523
1524 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
1525 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
1526
1527 /* multicast send - compute next multicast send time */
1528 if( radv_info->initial_adverts_sent > 0)
1529 {
1530 radv_info->initial_adverts_sent--;
1531 if(rfn > radv_info->initial_adverts_interval)
1532 rfn = radv_info->initial_adverts_interval;
1533
1534 /* check to see if we are ceasing to send */
1535 if( radv_info->initial_adverts_sent == 0)
1536 if(radv_info->cease_radv)
1537 radv_info->send_radv = 0;
1538 }
1539
1540 radv_info->next_multicast_time = rfn + now;
1541 radv_info->last_multicast_time = now;
1542
1543 /* send advert now - build a "solicted" router advert with unspecified source address */
1544 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
1545
1546 if (PREDICT_FALSE(n_allocated == 0))
1547 {
1548 clib_warning ("buffer allocation failure");
1549 continue;
1550 }
1551 b0 = vlib_get_buffer (vm, bo0);
1552 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
1553 b0->error = ICMP6_ERROR_NONE;
1554 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
1555
1556 h0 = vlib_buffer_get_current (b0);
1557
1558 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
1559
1560 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
1561 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
1562 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
1563 h0->ip.protocol = IP_PROTOCOL_ICMP6;
1564 h0->ip.hop_limit = 255;
1565
1566 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
1567 h0->ip.src_address.as_u64[0] = 0;
1568 h0->ip.src_address.as_u64[1] = 0;
1569
1570 h0->ip.dst_address.as_u64[0] = 0;
1571 h0->ip.dst_address.as_u64[1] = 0;
1572
1573 h0->neighbor.icmp.type = ICMP6_router_solicitation;
1574
1575 if (PREDICT_FALSE(f == 0))
1576 {
1577 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
1578 to_next = vlib_frame_vector_args (f);
1579 n_left_to_next = VLIB_FRAME_SIZE;
1580 n_this_frame = 0;
1581 }
1582
1583 n_this_frame++;
1584 n_left_to_next--;
1585 to_next[0] = bo0;
1586 to_next += 1;
1587
1588 if (PREDICT_FALSE(n_left_to_next == 0))
1589 {
1590 f->n_vectors = n_this_frame;
1591 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1592 f = 0;
1593 }
1594 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001595 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001596 /* *INDENT-ON* */
1597
1598 if (f)
1599 {
1600 ASSERT (n_this_frame);
1601 f->n_vectors = n_this_frame;
1602 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
1603 }
1604 return 0;
1605}
1606
1607static void
Dave Barach5f09efe2020-11-09 17:11:57 -05001608ip6_ra_handle_report (ip6_ra_report_t * rap)
Neale Rannscbe25aa2019-09-30 10:53:31 +00001609{
1610 u32 ii;
1611
1612 vec_foreach_index (ii, ip6_ra_listeners) ip6_ra_listeners[ii] (rap);
Dave Barach5f09efe2020-11-09 17:11:57 -05001613 vec_free (rap->prefixes);
1614 clib_mem_free (rap);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001615}
1616
1617static uword
1618ip6_ra_event_process (vlib_main_t * vm,
1619 vlib_node_runtime_t * node, vlib_frame_t * frame)
1620{
Dave Barach5f09efe2020-11-09 17:11:57 -05001621 ip6_ra_report_t *r;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001622 uword event_type;
Dave Barach5f09efe2020-11-09 17:11:57 -05001623 uword *event_data = 0;
1624 int i;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001625
1626 /* init code here */
1627
1628 while (1)
1629 {
1630 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
1631
Dave Barach5f09efe2020-11-09 17:11:57 -05001632 event_type = vlib_process_get_events (vm, &event_data);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001633
Dave Barach5f09efe2020-11-09 17:11:57 -05001634 if (event_type == ~0)
Neale Rannscbe25aa2019-09-30 10:53:31 +00001635 {
1636 /* No events found: timer expired. */
1637 /* process interface list and send RAs as appropriate, update timer info */
1638 ip6_ra_process_timer_event (vm, node, frame);
1639 }
1640 else
1641 {
Dave Barach5f09efe2020-11-09 17:11:57 -05001642 for (i = 0; i < vec_len (event_data); i++)
1643 {
1644 r = (void *) (event_data[i]);
1645 ip6_ra_handle_report (r);
1646 }
1647 vec_reset_length (event_data);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001648 }
1649 }
1650 return frame->n_vectors;
1651}
1652
Dave Barach5f09efe2020-11-09 17:11:57 -05001653/* *INDENT-OFF* */
Neale Rannscbe25aa2019-09-30 10:53:31 +00001654VLIB_REGISTER_NODE (ip6_ra_process_node) =
1655{
Dave Barach5f09efe2020-11-09 17:11:57 -05001656 .function = ip6_ra_event_process,
1657 .name = "ip6-ra-process",
1658 .type = VLIB_NODE_TYPE_PROCESS,
1659};
1660/* *INDENT-ON* */
Neale Rannscbe25aa2019-09-30 10:53:31 +00001661
1662static void
1663ip6_ra_signal_report (ip6_ra_report_t * r)
1664{
1665 vlib_main_t *vm = vlib_get_main ();
1666 ip6_ra_report_t *q;
1667
1668 if (!vec_len (ip6_ra_listeners))
1669 return;
1670
Dave Barach5f09efe2020-11-09 17:11:57 -05001671 q = clib_mem_alloc (sizeof (*q));
Neale Rannscbe25aa2019-09-30 10:53:31 +00001672 *q = *r;
Dave Barach5f09efe2020-11-09 17:11:57 -05001673
1674 vlib_process_signal_event (vm, ip6_ra_process_node.index, 0, (uword) q);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001675}
1676
1677static int
1678ip6_ra_publish (ip6_ra_report_t * r)
1679{
1680 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
1681 vl_api_rpc_call_main_thread (ip6_ra_signal_report, (u8 *) r, sizeof *r);
1682 return 0;
1683}
1684
1685/* API support functions */
1686int
1687ip6_ra_config (vlib_main_t * vm, u32 sw_if_index,
1688 u8 suppress, u8 managed, u8 other,
1689 u8 ll_option, u8 send_unicast, u8 cease,
1690 u8 use_lifetime, u32 lifetime,
1691 u32 initial_count, u32 initial_interval,
1692 u32 max_interval, u32 min_interval, u8 is_no)
1693{
1694 ip6_ra_t *radv_info;
1695
1696 /* look up the radv_t information for this interface */
1697 radv_info = ip6_ra_get_itf (sw_if_index);
1698
1699 if (!radv_info)
1700 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1701
1702 if ((max_interval != 0) && (min_interval == 0))
1703 min_interval = .75 * max_interval;
1704
1705 max_interval =
1706 (max_interval !=
1707 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
1708 radv_info->max_radv_interval;
1709 min_interval =
1710 (min_interval !=
1711 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
1712 radv_info->min_radv_interval;
1713 lifetime =
1714 (use_lifetime !=
1715 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
1716 radv_info->adv_router_lifetime_in_sec;
1717
1718 if (lifetime)
1719 {
1720 if (lifetime > MAX_DEF_RTR_LIFETIME)
1721 lifetime = MAX_DEF_RTR_LIFETIME;
1722
1723 if (lifetime <= max_interval)
1724 return VNET_API_ERROR_INVALID_VALUE;
1725 }
1726
1727 if (min_interval != 0)
1728 {
1729 if ((min_interval > .75 * max_interval) || (min_interval < 3))
1730 return VNET_API_ERROR_INVALID_VALUE;
1731 }
1732
1733 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
1734 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
1735 return VNET_API_ERROR_INVALID_VALUE;
1736
1737 /*
1738 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
1739 if "flag" is clear don't change corresponding value
1740 */
1741 radv_info->send_radv =
1742 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
1743 radv_info->adv_managed_flag =
1744 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
1745 radv_info->adv_other_flag =
1746 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
1747 radv_info->adv_link_layer_address =
1748 (ll_option != 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
1749 radv_info->send_unicast =
1750 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
1751 radv_info->cease_radv =
1752 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
1753
1754 radv_info->min_radv_interval = min_interval;
1755 radv_info->max_radv_interval = max_interval;
1756 radv_info->adv_router_lifetime_in_sec = lifetime;
1757
1758 radv_info->initial_adverts_count =
1759 (initial_count !=
1760 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
1761 radv_info->initial_adverts_count;
1762 radv_info->initial_adverts_interval =
1763 (initial_interval !=
1764 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
1765 radv_info->initial_adverts_interval;
1766
1767 /* restart */
1768 if ((cease != 0) && (is_no))
1769 radv_info->send_radv = 1;
1770
1771 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1772 radv_info->next_multicast_time = vlib_time_now (vm);
1773 radv_info->last_multicast_time = vlib_time_now (vm);
1774 radv_info->last_radv_time = 0;
1775
1776 return (0);
1777}
1778
1779
1780int
1781ip6_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
1782 ip6_address_t * prefix_addr, u8 prefix_len,
1783 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
1784 u8 no_advertise, u8 off_link, u8 no_autoconfig,
1785 u8 no_onlink, u8 is_no)
1786{
1787 ip6_ra_t *radv_info;
1788
1789 /* look up the radv_t information for this interface */
1790 radv_info = ip6_ra_get_itf (sw_if_index);
1791
1792 if (!radv_info)
1793 return (VNET_API_ERROR_IP6_NOT_ENABLED);
1794
1795 f64 now = vlib_time_now (vm);
1796
1797 /* prefix info add, delete or update */
1798 ip6_radv_prefix_t *prefix;
1799
1800 /* lookup prefix info for this address on this interface */
1801 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
1802
1803 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
1804
1805 if (is_no)
1806 {
1807 /* delete */
1808 if (!prefix)
1809 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
1810
1811 if (prefix->prefix_len != prefix_len)
1812 return VNET_API_ERROR_INVALID_VALUE_2;
1813
1814 /* FIXME - Should the DP do this or the CP ? */
1815 /* do specific delete processing here before returning */
1816 /* try to remove from routing table */
1817
1818 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
1819 /* old_value */ 0);
1820 pool_put (radv_info->adv_prefixes_pool, prefix);
1821
1822 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1823 radv_info->next_multicast_time = vlib_time_now (vm);
1824 radv_info->last_multicast_time = vlib_time_now (vm);
1825 radv_info->last_radv_time = 0;
1826 return (0);
1827 }
1828
1829 /* adding or changing */
1830 if (!prefix)
1831 {
1832 /* add */
1833 u32 pi;
1834 pool_get_zero (radv_info->adv_prefixes_pool, prefix);
1835 pi = prefix - radv_info->adv_prefixes_pool;
1836 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
1837 /* old_value */ 0);
1838
1839 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
1840
1841 prefix->prefix_len = prefix_len;
1842 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
1843
1844 /* initialize default values */
1845 prefix->adv_on_link_flag = 1; /* L bit set */
1846 prefix->adv_autonomous_flag = 1; /* A bit set */
1847 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
1848 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
1849 prefix->enabled = 1;
1850 prefix->decrement_lifetime_flag = 1;
1851 prefix->deprecated_prefix_flag = 1;
1852
1853 if (off_link == 0)
1854 {
1855 /* FIXME - Should the DP do this or the CP ? */
1856 /* insert prefix into routing table as a connected prefix */
1857 }
1858
1859 if (use_default)
1860 goto restart;
1861 }
1862 else
1863 {
1864
1865 if (prefix->prefix_len != prefix_len)
1866 return VNET_API_ERROR_INVALID_VALUE_2;
1867
1868 if (off_link != 0)
1869 {
1870 /* FIXME - Should the DP do this or the CP ? */
1871 /* remove from routing table if already there */
1872 }
1873 }
1874
1875 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
1876 {
1877 prefix->adv_valid_lifetime_in_secs = ~0;
1878 prefix->adv_pref_lifetime_in_secs = ~0;
1879 prefix->decrement_lifetime_flag = 0;
1880 }
1881 else
1882 {
1883 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
1884 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
1885 }
1886
1887 /* copy remaining */
1888 prefix->enabled = !(no_advertise != 0);
1889 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
1890 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
1891
1892restart:
1893 /* restart */
1894 /* fill in the expiration times */
1895 prefix->valid_lifetime_expires = now + prefix->adv_valid_lifetime_in_secs;
1896 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
1897
1898 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
1899 radv_info->next_multicast_time = vlib_time_now (vm);
1900 radv_info->last_multicast_time = vlib_time_now (vm);
1901 radv_info->last_radv_time = 0;
1902
1903 return (0);
1904}
1905
1906clib_error_t *
1907ip6_ra_cmd (vlib_main_t * vm,
1908 unformat_input_t * main_input, vlib_cli_command_t * cmd)
1909{
1910 vnet_main_t *vnm = vnet_get_main ();
1911 clib_error_t *error = 0;
1912 u8 is_no = 0;
1913 u8 suppress = 0, managed = 0, other = 0;
1914 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
1915 u8 use_lifetime = 0;
1916 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
1917 0, ra_initial_interval = 0;
1918 u32 ra_max_interval = 0, ra_min_interval = 0;
1919
1920 unformat_input_t _line_input, *line_input = &_line_input;
1921
1922 int add_radv_info = 1;
1923 ip6_address_t ip6_addr;
1924 u32 addr_len;
1925
1926
1927 /* Get a line of input. */
1928 if (!unformat_user (main_input, unformat_line_input, line_input))
1929 return 0;
1930
1931 /* get basic radv info for this interface */
1932 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1933 {
1934
1935 if (unformat_user (line_input,
1936 unformat_vnet_sw_interface, vnm, &sw_if_index))
1937 {
1938 if (!ip6_ra_get_eth_itf (sw_if_index))
1939 {
1940 error =
1941 clib_error_return (0, "Interface must be of ethernet type");
1942 goto done;
1943 }
1944
1945 if (!ip6_link_is_enabled (sw_if_index))
1946 {
1947 error = clib_error_return (0, "IP6 nt enabler interface %U'",
1948 format_unformat_error, line_input);
1949 goto done;
1950 }
1951 }
1952 else
1953 {
1954 error = clib_error_return (0, "invalid interface name %U'",
1955 format_unformat_error, line_input);
1956 goto done;
1957 }
1958 }
1959
1960 /* get the rest of the command */
1961 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1962 {
1963 if (unformat (line_input, "no"))
1964 is_no = 1;
1965 else if (unformat (line_input, "prefix %U/%d",
1966 unformat_ip6_address, &ip6_addr, &addr_len))
1967 {
1968 add_radv_info = 0;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001969 }
1970 else if (unformat (line_input, "ra-managed-config-flag"))
1971 {
1972 managed = 1;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001973 }
1974 else if (unformat (line_input, "ra-other-config-flag"))
1975 {
1976 other = 1;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001977 }
1978 else if (unformat (line_input, "ra-suppress") ||
1979 unformat (line_input, "ra-surpress"))
1980 {
1981 suppress = 1;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001982 }
1983 else if (unformat (line_input, "ra-suppress-link-layer") ||
1984 unformat (line_input, "ra-surpress-link-layer"))
1985 {
1986 suppress_ll_option = 1;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001987 }
1988 else if (unformat (line_input, "ra-send-unicast"))
1989 {
1990 send_unicast = 1;
Neale Rannscbe25aa2019-09-30 10:53:31 +00001991 }
1992 else if (unformat (line_input, "ra-lifetime"))
1993 {
1994 if (!unformat (line_input, "%d", &ra_lifetime))
1995 {
1996 error = unformat_parse_error (line_input);
1997 goto done;
1998 }
1999 use_lifetime = 1;
Neale Rannscbe25aa2019-09-30 10:53:31 +00002000 }
2001 else if (unformat (line_input, "ra-initial"))
2002 {
2003 if (!unformat
2004 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
2005 {
2006 error = unformat_parse_error (line_input);
2007 goto done;
2008 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00002009 }
2010 else if (unformat (line_input, "ra-interval"))
2011 {
2012 if (!unformat (line_input, "%d", &ra_max_interval))
2013 {
2014 error = unformat_parse_error (line_input);
2015 goto done;
2016 }
2017
2018 if (!unformat (line_input, "%d", &ra_min_interval))
2019 ra_min_interval = 0;
Neale Rannscbe25aa2019-09-30 10:53:31 +00002020 }
2021 else if (unformat (line_input, "ra-cease"))
2022 {
2023 cease = 1;
Neale Rannscbe25aa2019-09-30 10:53:31 +00002024 }
2025 else
2026 {
2027 error = unformat_parse_error (line_input);
2028 goto done;
2029 }
2030 }
2031
2032 if (add_radv_info)
2033 {
2034 ip6_ra_config (vm, sw_if_index,
2035 suppress, managed, other,
2036 suppress_ll_option, send_unicast, cease,
2037 use_lifetime, ra_lifetime,
2038 ra_initial_count, ra_initial_interval,
2039 ra_max_interval, ra_min_interval, is_no);
2040 }
2041 else
2042 {
2043 u32 valid_lifetime_in_secs = 0;
2044 u32 pref_lifetime_in_secs = 0;
2045 u8 use_prefix_default_values = 0;
2046 u8 no_advertise = 0;
2047 u8 off_link = 0;
2048 u8 no_autoconfig = 0;
2049 u8 no_onlink = 0;
2050
2051 /* get the rest of the command */
2052 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2053 {
2054 if (unformat (line_input, "default"))
2055 {
2056 use_prefix_default_values = 1;
2057 break;
2058 }
2059 else if (unformat (line_input, "infinite"))
2060 {
2061 valid_lifetime_in_secs = ~0;
2062 pref_lifetime_in_secs = ~0;
2063 break;
2064 }
2065 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
2066 &pref_lifetime_in_secs))
2067 break;
2068 else
2069 break;
2070 }
2071
2072
2073 /* get the rest of the command */
2074 while (!use_prefix_default_values &&
2075 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2076 {
2077 if (unformat (line_input, "no-advertise"))
2078 no_advertise = 1;
2079 else if (unformat (line_input, "off-link"))
2080 off_link = 1;
2081 else if (unformat (line_input, "no-autoconfig"))
2082 no_autoconfig = 1;
2083 else if (unformat (line_input, "no-onlink"))
2084 no_onlink = 1;
2085 else
2086 {
2087 error = unformat_parse_error (line_input);
2088 goto done;
2089 }
2090 }
2091
2092 ip6_ra_prefix (vm, sw_if_index,
2093 &ip6_addr, addr_len,
2094 use_prefix_default_values,
2095 valid_lifetime_in_secs,
2096 pref_lifetime_in_secs,
2097 no_advertise, off_link, no_autoconfig, no_onlink, is_no);
2098 }
2099
2100done:
2101 unformat_free (line_input);
2102
2103 return error;
2104}
2105
2106static u8 *
2107format_ip6_ra (u8 * s, va_list * args)
2108{
2109 index_t rai = va_arg (*args, index_t);
2110 u32 indent = va_arg (*args, u32);
2111 ip6_radv_prefix_t *p;
2112 ip6_ra_t *radv_info;
2113
2114 radv_info = pool_elt_at_index (ip6_ra_pool, rai);
2115
2116 s = format (s, "%UAdvertised Prefixes:\n", format_white_space, indent);
2117
2118 indent += 2;
2119
2120 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01002121 pool_foreach (p, radv_info->adv_prefixes_pool)
2122 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00002123 s = format (s, "%Uprefix %U, length %d\n",
2124 format_white_space, indent+2,
2125 format_ip6_address, &p->prefix, p->prefix_len);
Damjan Marionb2c31b62020-12-13 21:47:40 +01002126 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00002127 /* *INDENT-ON* */
2128
2129 s = format (s, "%UMTU is %d\n",
2130 format_white_space, indent, radv_info->adv_link_mtu);
2131 s = format (s, "%UICMP error messages are unlimited\n",
2132 format_white_space, indent);
2133 s = format (s, "%UICMP redirects are disabled\n",
2134 format_white_space, indent);
2135 s = format (s, "%UICMP unreachables are not sent\n",
2136 format_white_space, indent);
2137 s = format (s, "%UND DAD is disabled\n", format_white_space, indent);
2138 //s = format (s, "%UND reachable time is %d milliseconds\n",);
2139 s = format (s, "%UND advertised reachable time is %d\n",
2140 format_white_space, indent,
2141 radv_info->adv_neighbor_reachable_time_in_msec);
2142 s = format (s,
2143 "%UND advertised retransmit interval is %d (msec)\n",
2144 format_white_space, indent,
2145 radv_info->
2146 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
2147 s =
2148 format (s,
2149 "%UND router advertisements are sent every %0.1f seconds (min interval is %0.1f)\n",
2150 format_white_space, indent, radv_info->max_radv_interval,
2151 radv_info->min_radv_interval);
2152 s =
2153 format (s, "%UND router advertisements live for %d seconds\n",
2154 format_white_space, indent,
2155 radv_info->adv_router_lifetime_in_sec);
2156 s =
2157 format (s, "%UHosts %s stateless autoconfig for addresses\n",
2158 format_white_space, indent,
2159 (radv_info->adv_managed_flag) ? "use" : " don't use");
2160 s =
2161 format (s, "%UND router advertisements sent %d\n", format_white_space,
2162 indent, radv_info->n_advertisements_sent);
2163 s =
2164 format (s, "%UND router solicitations received %d\n", format_white_space,
2165 indent, radv_info->n_solicitations_rcvd);
2166 s =
2167 format (s, "%UND router solicitations dropped %d\n", format_white_space,
2168 indent, radv_info->n_solicitations_dropped);
2169
2170 return (s);
2171}
2172
2173
2174/*?
2175 * This command is used to configure the neighbor discovery
2176 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
2177 * command to display some of the current neighbor discovery parameters
2178 * on a given interface. This command has three formats:
2179 *
2180 *
2181 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
2182 *
2183 * '<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>'
2184 *
2185 * Where:
2186 *
2187 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
2188 * router-advertisement messages to use stateful address
2189 * auto-configuration to obtain address information (sets the M-bit).
2190 * Default is the M-bit is not set and the '<em>no</em>' option
2191 * returns it to this default state.
2192 *
2193 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
2194 * router-advertisement messages that hosts use stateful auto
2195 * configuration to obtain nonaddress related information (sets
2196 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
2197 * option returns it to this default state.
2198 *
2199 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
2200 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
2201 * router-advertisement messages.
2202 *
2203 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
2204 * optional source link-layer address in the ICMPv6 router-advertisement
2205 * messages. Default is to include the optional source link-layer address
2206 * and the '<em>no</em>' option returns it to this default state.
2207 *
2208 * <em>[no] ra-send-unicast</em> - Use the source address of the
2209 * router-solicitation message if availiable. The default is to use
2210 * multicast address of all nodes, and the '<em>no</em>' option returns
2211 * it to this default state.
2212 *
2213 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
2214 * default router in ICMPv6 router-advertisement messages. The range is
2215 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
2216 * '<em><max-interval></em>'. The default value is 600 seconds and the
2217 * '<em>no</em>' option returns it to this default value.
2218 *
2219 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
2220 * router-advertisement messages sent and the interval between each
2221 * message. Range for count is 1 - 3 and default is 3. Range for interval
2222 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
2223 * returns both to their default value.
2224 *
2225 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
2226 * interval between sending ICMPv6 router-advertisement messages. The
2227 * range for max-interval is from 4 to 200 seconds. min-interval can not
2228 * be more than 75% of max-interval. If not set, min-interval will be
2229 * set to 75% of max-interval. The range for min-interval is from 3 to
2230 * 150 seconds. The '<em>no</em>' option returns both to their default
2231 * value.
2232 *
2233 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
2234 * The '<em>no</em>' options implies to start (or restart) sending
2235 * ICMPv6 router-advertisement messages.
2236 *
2237 *
2238 * <b>Format 2 - Prefix Options:</b>
2239 *
2240 * '<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>'
2241 *
2242 * Where:
2243 *
2244 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
2245 *
2246 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
2247 * length of time in seconds during what the prefix is valid for the purpose of
2248 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
2249 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
2250 * length of time in seconds during what addresses generated from the prefix remain
2251 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
2252 *
2253 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
2254 * are inifinte, no timeout.
2255 *
2256 * <em>no-advertise</em> - Do not send full router address in prefix
2257 * advertisement. Default is to advertise (i.e. - This flag is off by default).
2258 *
2259 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
2260 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
2261 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
2262 *
2263 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
2264 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
2265 *
2266 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
2267 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
2268 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
2269 * the L-bit.
2270 *
2271 *
2272 * <b>Format 3: - Default of Prefix:</b>
2273 *
2274 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
2275 *
2276 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
2277 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
2278 * is ignored and the prefix is deleted.
2279 *
2280 *
2281 * @cliexpar
2282 * Example of how set a router advertisement option:
2283 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
2284 * Example of how to add a prefix:
2285 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
2286 * Example of how to delete a prefix:
2287 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
2288?*/
2289/* *INDENT-OFF* */
2290VLIB_CLI_COMMAND (ip6_nd_command, static) =
2291{
2292 .path = "ip6 nd",
2293 .short_help = "ip6 nd <interface> ...",
2294 .function = ip6_ra_cmd,
2295};
2296/* *INDENT-ON* */
2297
2298/**
2299 * VFT for registering as a delegate to an IP6 link
2300 */
2301const static ip6_link_delegate_vft_t ip6_ra_delegate_vft = {
2302 .ildv_disable = ip6_ra_delegate_disable,
2303 .ildv_enable = ip6_ra_link_enable,
2304 .ildv_format = format_ip6_ra,
2305};
2306
2307static clib_error_t *
2308ip6_ra_init (vlib_main_t * vm)
2309{
2310 vlib_call_init_function (vm, icmp6_init);
2311
2312 icmp6_register_type (vm, ICMP6_router_solicitation,
2313 ip6_icmp_router_solicitation_node.index);
2314 icmp6_register_type (vm, ICMP6_router_advertisement,
2315 ip6_icmp_router_advertisement_node.index);
2316
2317 ip6_ra_delegate_id = ip6_link_delegate_register (&ip6_ra_delegate_vft);
2318
2319 return (NULL);
2320}
2321
2322/* *INDENT-OFF* */
2323VLIB_INIT_FUNCTION (ip6_ra_init) =
2324{
2325 .runs_after = VLIB_INITS("icmp6_init"),
2326};
2327/* *INDENT-ON* */
2328
2329/*
2330 * fd.io coding-style-patch-verification: ON
2331 *
2332 * Local Variables:
2333 * eval: (c-set-style "gnu")
2334 * End:
2335 */