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