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