1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2017 Intel Corporation. All rights reserved.
8 #include <netinet/icmp6.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
15 #include "alloc-util.h"
16 #include "dns-domain.h"
18 #include "icmp6-util.h"
19 #include "in-addr-util.h"
20 #include "radv-internal.h"
21 #include "socket-util.h"
22 #include "string-util.h"
25 #include "random-util.h"
27 _public_
int sd_radv_new(sd_radv
**ret
) {
28 _cleanup_(sd_radv_unrefp
) sd_radv
*ra
= NULL
;
30 assert_return(ret
, -EINVAL
);
32 ra
= new0(sd_radv
, 1);
39 LIST_HEAD_INIT(ra
->prefixes
);
46 _public_
int sd_radv_attach_event(sd_radv
*ra
, sd_event
*event
, int64_t priority
) {
49 assert_return(ra
, -EINVAL
);
50 assert_return(!ra
->event
, -EBUSY
);
53 ra
->event
= sd_event_ref(event
);
55 r
= sd_event_default(&ra
->event
);
60 ra
->event_priority
= priority
;
65 _public_
int sd_radv_detach_event(sd_radv
*ra
) {
67 assert_return(ra
, -EINVAL
);
69 ra
->event
= sd_event_unref(ra
->event
);
73 _public_ sd_event
*sd_radv_get_event(sd_radv
*ra
) {
74 assert_return(ra
, NULL
);
79 static void radv_reset(sd_radv
*ra
) {
82 ra
->timeout_event_source
=
83 sd_event_source_unref(ra
->timeout_event_source
);
85 ra
->recv_event_source
=
86 sd_event_source_unref(ra
->recv_event_source
);
91 _public_ sd_radv
*sd_radv_ref(sd_radv
*ra
) {
95 assert(ra
->n_ref
> 0);
101 _public_ sd_radv
*sd_radv_unref(sd_radv
*ra
) {
105 assert(ra
->n_ref
> 0);
111 while (ra
->prefixes
) {
112 sd_radv_prefix
*p
= ra
->prefixes
;
114 LIST_REMOVE(prefix
, ra
->prefixes
, p
);
115 sd_radv_prefix_unref(p
);
123 sd_radv_detach_event(ra
);
125 ra
->fd
= safe_close(ra
->fd
);
130 static int radv_send(sd_radv
*ra
, const struct in6_addr
*dst
,
131 const uint32_t router_lifetime
) {
132 static const struct ether_addr mac_zero
= {};
134 struct sockaddr_in6 dst_addr
= {
135 .sin6_family
= AF_INET6
,
136 .sin6_addr
= IN6ADDR_ALL_NODES_MULTICAST_INIT
,
138 struct nd_router_advert adv
= {};
140 struct nd_opt_hdr opthdr
;
141 struct ether_addr slladdr
;
142 } _packed_ opt_mac
= {
144 .nd_opt_type
= ND_OPT_SOURCE_LINKADDR
,
145 .nd_opt_len
= (sizeof(struct nd_opt_hdr
) +
146 sizeof(struct ether_addr
) - 1) /8 + 1,
149 struct nd_opt_mtu opt_mtu
= {
150 .nd_opt_mtu_type
= ND_OPT_MTU
,
153 /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, RDNSS
155 struct iovec iov
[5 + ra
->n_prefixes
];
156 struct msghdr msg
= {
157 .msg_name
= &dst_addr
,
158 .msg_namelen
= sizeof(dst_addr
),
164 r
= sd_event_now(ra
->event
, clock_boottime_or_monotonic(), &time_now
);
168 if (dst
&& !in_addr_is_null(AF_INET6
, (union in_addr_union
*) dst
))
169 dst_addr
.sin6_addr
= *dst
;
171 adv
.nd_ra_type
= ND_ROUTER_ADVERT
;
172 adv
.nd_ra_curhoplimit
= ra
->hop_limit
;
173 adv
.nd_ra_flags_reserved
= ra
->flags
;
174 adv
.nd_ra_router_lifetime
= htobe16(router_lifetime
);
175 iov
[msg
.msg_iovlen
].iov_base
= &adv
;
176 iov
[msg
.msg_iovlen
].iov_len
= sizeof(adv
);
179 /* MAC address is optional, either because the link does not use L2
180 addresses or load sharing is desired. See RFC 4861, Section 4.2 */
181 if (memcmp(&mac_zero
, &ra
->mac_addr
, sizeof(mac_zero
))) {
182 opt_mac
.slladdr
= ra
->mac_addr
;
183 iov
[msg
.msg_iovlen
].iov_base
= &opt_mac
;
184 iov
[msg
.msg_iovlen
].iov_len
= sizeof(opt_mac
);
189 opt_mtu
.nd_opt_mtu_mtu
= htobe32(ra
->mtu
);
190 iov
[msg
.msg_iovlen
].iov_base
= &opt_mtu
;
191 iov
[msg
.msg_iovlen
].iov_len
= sizeof(opt_mtu
);
195 LIST_FOREACH(prefix
, p
, ra
->prefixes
) {
196 if (p
->valid_until
) {
198 if (time_now
> p
->valid_until
)
199 p
->opt
.valid_lifetime
= 0;
201 p
->opt
.valid_lifetime
= htobe32((p
->valid_until
- time_now
) / USEC_PER_SEC
);
203 if (time_now
> p
->preferred_until
)
204 p
->opt
.preferred_lifetime
= 0;
206 p
->opt
.preferred_lifetime
= htobe32((p
->preferred_until
- time_now
) / USEC_PER_SEC
);
208 iov
[msg
.msg_iovlen
].iov_base
= &p
->opt
;
209 iov
[msg
.msg_iovlen
].iov_len
= sizeof(p
->opt
);
214 iov
[msg
.msg_iovlen
].iov_base
= ra
->rdnss
;
215 iov
[msg
.msg_iovlen
].iov_len
= ra
->rdnss
->length
* 8;
220 iov
[msg
.msg_iovlen
].iov_base
= ra
->dnssl
;
221 iov
[msg
.msg_iovlen
].iov_len
= ra
->dnssl
->length
* 8;
225 if (sendmsg(ra
->fd
, &msg
, 0) < 0)
231 static int radv_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
232 sd_radv
*ra
= userdata
;
233 _cleanup_free_
char *addr
= NULL
;
235 triple_timestamp timestamp
;
238 _cleanup_free_
char *buf
= NULL
;
244 buflen
= next_datagram_size_fd(fd
);
246 if ((unsigned) buflen
< sizeof(struct nd_router_solicit
))
247 return log_radv("Too short packet received");
249 buf
= new0(char, buflen
);
253 r
= icmp6_receive(fd
, buf
, buflen
, &src
, ×tamp
);
257 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &src
, &addr
);
258 log_radv("Received RS from non-link-local address %s. Ignoring", addr
);
262 log_radv("Received RS with invalid hop limit. Ignoring.");
266 log_radv("Received invalid source address from ICMPv6 socket. Ignoring.");
270 log_radv_warning_errno(r
, "Error receiving from ICMPv6 socket: %m");
277 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &src
, &addr
);
279 r
= radv_send(ra
, &src
, ra
->lifetime
);
281 log_radv_warning_errno(r
, "Unable to send solicited Router Advertisment to %s: %m", addr
);
283 log_radv("Sent solicited Router Advertisement to %s", addr
);
288 static usec_t
radv_compute_timeout(usec_t min
, usec_t max
) {
289 assert_return(min
<= max
, SD_RADV_DEFAULT_MIN_TIMEOUT_USEC
);
291 return min
+ (random_u32() % (max
- min
));
294 static int radv_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
296 sd_radv
*ra
= userdata
;
297 usec_t min_timeout
= SD_RADV_DEFAULT_MIN_TIMEOUT_USEC
;
298 usec_t max_timeout
= SD_RADV_DEFAULT_MAX_TIMEOUT_USEC
;
299 usec_t time_now
, timeout
;
300 char time_string
[FORMAT_TIMESPAN_MAX
];
306 ra
->timeout_event_source
= sd_event_source_unref(ra
->timeout_event_source
);
308 r
= sd_event_now(ra
->event
, clock_boottime_or_monotonic(), &time_now
);
312 r
= radv_send(ra
, NULL
, ra
->lifetime
);
314 log_radv_warning_errno(r
, "Unable to send Router Advertisement: %m");
316 /* RFC 4861, Section 6.2.4, sending initial Router Advertisements */
317 if (ra
->ra_sent
< SD_RADV_MAX_INITIAL_RTR_ADVERTISEMENTS
) {
318 max_timeout
= SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC
;
319 min_timeout
= SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC
/ 3;
322 timeout
= radv_compute_timeout(min_timeout
, max_timeout
);
324 log_radv("Next Router Advertisement in %s",
325 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
,
326 timeout
, USEC_PER_SEC
));
328 r
= sd_event_add_time(ra
->event
, &ra
->timeout_event_source
,
329 clock_boottime_or_monotonic(),
330 time_now
+ timeout
, MSEC_PER_SEC
,
335 r
= sd_event_source_set_priority(ra
->timeout_event_source
,
340 r
= sd_event_source_set_description(ra
->timeout_event_source
,
354 _public_
int sd_radv_stop(sd_radv
*ra
) {
357 assert_return(ra
, -EINVAL
);
359 if (ra
->state
== SD_RADV_STATE_IDLE
)
362 log_radv("Stopping IPv6 Router Advertisement daemon");
364 /* RFC 4861, Section 6.2.5, send at least one Router Advertisement
365 with zero lifetime */
366 r
= radv_send(ra
, NULL
, 0);
368 log_radv_warning_errno(r
, "Unable to send last Router Advertisement with router lifetime set to zero: %m");
371 ra
->fd
= safe_close(ra
->fd
);
372 ra
->state
= SD_RADV_STATE_IDLE
;
377 _public_
int sd_radv_start(sd_radv
*ra
) {
380 assert_return(ra
, -EINVAL
);
381 assert_return(ra
->event
, -EINVAL
);
382 assert_return(ra
->ifindex
> 0, -EINVAL
);
384 if (ra
->state
!= SD_RADV_STATE_IDLE
)
387 r
= sd_event_add_time(ra
->event
, &ra
->timeout_event_source
,
388 clock_boottime_or_monotonic(), 0, 0,
393 r
= sd_event_source_set_priority(ra
->timeout_event_source
,
398 (void) sd_event_source_set_description(ra
->timeout_event_source
,
401 r
= icmp6_bind_router_advertisement(ra
->ifindex
);
407 r
= sd_event_add_io(ra
->event
, &ra
->recv_event_source
, ra
->fd
, EPOLLIN
, radv_recv
, ra
);
411 r
= sd_event_source_set_priority(ra
->recv_event_source
, ra
->event_priority
);
415 (void) sd_event_source_set_description(ra
->recv_event_source
, "radv-receive-message");
417 ra
->state
= SD_RADV_STATE_ADVERTISING
;
419 log_radv("Started IPv6 Router Advertisement daemon");
429 _public_
int sd_radv_set_ifindex(sd_radv
*ra
, int ifindex
) {
430 assert_return(ra
, -EINVAL
);
431 assert_return(ifindex
>= -1, -EINVAL
);
433 if (ra
->state
!= SD_RADV_STATE_IDLE
)
436 ra
->ifindex
= ifindex
;
441 _public_
int sd_radv_set_mac(sd_radv
*ra
, const struct ether_addr
*mac_addr
) {
442 assert_return(ra
, -EINVAL
);
444 if (ra
->state
!= SD_RADV_STATE_IDLE
)
448 ra
->mac_addr
= *mac_addr
;
455 _public_
int sd_radv_set_mtu(sd_radv
*ra
, uint32_t mtu
) {
456 assert_return(ra
, -EINVAL
);
457 assert_return(mtu
>= 1280, -EINVAL
);
464 _public_
int sd_radv_set_hop_limit(sd_radv
*ra
, uint8_t hop_limit
) {
465 assert_return(ra
, -EINVAL
);
467 if (ra
->state
!= SD_RADV_STATE_IDLE
)
470 ra
->hop_limit
= hop_limit
;
475 _public_
int sd_radv_set_router_lifetime(sd_radv
*ra
, uint32_t router_lifetime
) {
476 assert_return(ra
, -EINVAL
);
478 if (ra
->state
!= SD_RADV_STATE_IDLE
)
481 /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the
482 preference value MUST be set to (00) by the sender..." */
483 if (router_lifetime
== 0 &&
484 (ra
->flags
& (0x3 << 3)) != (SD_NDISC_PREFERENCE_MEDIUM
<< 3))
487 ra
->lifetime
= router_lifetime
;
492 _public_
int sd_radv_set_managed_information(sd_radv
*ra
, int managed
) {
493 assert_return(ra
, -EINVAL
);
495 if (ra
->state
!= SD_RADV_STATE_IDLE
)
498 SET_FLAG(ra
->flags
, ND_RA_FLAG_MANAGED
, managed
);
503 _public_
int sd_radv_set_other_information(sd_radv
*ra
, int other
) {
504 assert_return(ra
, -EINVAL
);
506 if (ra
->state
!= SD_RADV_STATE_IDLE
)
509 SET_FLAG(ra
->flags
, ND_RA_FLAG_OTHER
, other
);
514 _public_
int sd_radv_set_preference(sd_radv
*ra
, unsigned preference
) {
517 assert_return(ra
, -EINVAL
);
518 assert_return(IN_SET(preference
,
519 SD_NDISC_PREFERENCE_LOW
,
520 SD_NDISC_PREFERENCE_MEDIUM
,
521 SD_NDISC_PREFERENCE_HIGH
), -EINVAL
);
523 ra
->flags
= (ra
->flags
& ~(0x3 << 3)) | (preference
<< 3);
528 _public_
int sd_radv_add_prefix(sd_radv
*ra
, sd_radv_prefix
*p
, bool dynamic
) {
531 _cleanup_free_
char *addr_p
= NULL
;
532 char time_string_preferred
[FORMAT_TIMESPAN_MAX
];
533 char time_string_valid
[FORMAT_TIMESPAN_MAX
];
534 usec_t time_now
, valid
, preferred
, valid_until
, preferred_until
;
536 assert_return(ra
, -EINVAL
);
541 LIST_FOREACH(prefix
, cur
, ra
->prefixes
) {
543 r
= in_addr_prefix_intersect(AF_INET6
,
544 (union in_addr_union
*) &cur
->opt
.in6_addr
,
546 (union in_addr_union
*) &p
->opt
.in6_addr
,
549 _cleanup_free_
char *addr_cur
= NULL
;
551 (void) in_addr_to_string(AF_INET6
,
552 (union in_addr_union
*) &p
->opt
.in6_addr
,
555 if (dynamic
&& cur
->opt
.prefixlen
== p
->opt
.prefixlen
)
558 (void) in_addr_to_string(AF_INET6
,
559 (union in_addr_union
*) &cur
->opt
.in6_addr
,
561 log_radv("IPv6 prefix %s/%u already configured, ignoring %s/%u",
562 addr_cur
, cur
->opt
.prefixlen
,
563 addr_p
, p
->opt
.prefixlen
);
569 p
= sd_radv_prefix_ref(p
);
571 LIST_APPEND(prefix
, ra
->prefixes
, p
);
575 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &p
->opt
.in6_addr
, &addr_p
);
578 log_radv("Added prefix %s/%d", addr_p
, p
->opt
.prefixlen
);
585 r
= sd_event_now(ra
->event
, clock_boottime_or_monotonic(), &time_now
);
589 valid
= be32toh(p
->opt
.valid_lifetime
) * USEC_PER_SEC
;
590 valid_until
= usec_add(valid
, time_now
);
591 if (valid_until
== USEC_INFINITY
)
594 preferred
= be32toh(p
->opt
.preferred_lifetime
) * USEC_PER_SEC
;
595 preferred_until
= usec_add(preferred
, time_now
);
596 if (preferred_until
== USEC_INFINITY
)
599 cur
->valid_until
= valid_until
;
600 cur
->preferred_until
= preferred_until
;
602 log_radv("%s prefix %s/%u preferred %s valid %s",
603 cur
? "Updated": "Added",
604 addr_p
, p
->opt
.prefixlen
,
605 format_timespan(time_string_preferred
, FORMAT_TIMESPAN_MAX
,
606 preferred
, USEC_PER_SEC
),
607 format_timespan(time_string_valid
, FORMAT_TIMESPAN_MAX
,
608 valid
, USEC_PER_SEC
));
613 _public_ sd_radv_prefix
*sd_radv_remove_prefix(sd_radv
*ra
,
614 const struct in6_addr
*prefix
,
615 unsigned char prefixlen
) {
616 sd_radv_prefix
*cur
, *next
;
618 assert_return(ra
, NULL
);
619 assert_return(prefix
, NULL
);
621 LIST_FOREACH_SAFE(prefix
, cur
, next
, ra
->prefixes
) {
622 if (prefixlen
!= cur
->opt
.prefixlen
)
625 if (!in_addr_equal(AF_INET6
,
626 (union in_addr_union
*)prefix
,
627 (union in_addr_union
*)&cur
->opt
.in6_addr
))
630 LIST_REMOVE(prefix
, ra
->prefixes
, cur
);
639 _public_
int sd_radv_set_rdnss(sd_radv
*ra
, uint32_t lifetime
,
640 const struct in6_addr
*dns
, size_t n_dns
) {
641 _cleanup_free_
struct sd_radv_opt_dns
*opt_rdnss
= NULL
;
644 assert_return(ra
, -EINVAL
);
645 assert_return(n_dns
< 128, -EINVAL
);
647 if (!dns
|| n_dns
== 0) {
648 ra
->rdnss
= mfree(ra
->rdnss
);
654 len
= sizeof(struct sd_radv_opt_dns
) + sizeof(struct in6_addr
) * n_dns
;
656 opt_rdnss
= malloc0(len
);
660 opt_rdnss
->type
= SD_RADV_OPT_RDNSS
;
661 opt_rdnss
->length
= len
/ 8;
662 opt_rdnss
->lifetime
= htobe32(lifetime
);
664 memcpy(opt_rdnss
+ 1, dns
, n_dns
* sizeof(struct in6_addr
));
666 free_and_replace(ra
->rdnss
, opt_rdnss
);
673 _public_
int sd_radv_set_dnssl(sd_radv
*ra
, uint32_t lifetime
,
674 char **search_list
) {
675 _cleanup_free_
struct sd_radv_opt_dns
*opt_dnssl
= NULL
;
680 assert_return(ra
, -EINVAL
);
682 if (!search_list
|| *search_list
== NULL
) {
683 ra
->dnssl
= mfree(ra
->dnssl
);
688 STRV_FOREACH(s
, search_list
)
689 len
+= strlen(*s
) + 2;
691 len
= (sizeof(struct sd_radv_opt_dns
) + len
+ 7) & ~0x7;
693 opt_dnssl
= malloc0(len
);
697 opt_dnssl
->type
= SD_RADV_OPT_DNSSL
;
698 opt_dnssl
->length
= len
/ 8;
699 opt_dnssl
->lifetime
= htobe32(lifetime
);
701 p
= (uint8_t *)(opt_dnssl
+ 1);
702 len
-= sizeof(struct sd_radv_opt_dns
);
704 STRV_FOREACH(s
, search_list
) {
707 r
= dns_name_to_wire_format(*s
, p
, len
, false);
718 free_and_replace(ra
->dnssl
, opt_dnssl
);
723 _public_
int sd_radv_prefix_new(sd_radv_prefix
**ret
) {
724 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
726 assert_return(ret
, -EINVAL
);
728 p
= new0(sd_radv_prefix
, 1);
734 p
->opt
.type
= ND_OPT_PREFIX_INFORMATION
;
735 p
->opt
.length
= (sizeof(p
->opt
) - 1) /8 + 1;
737 p
->opt
.prefixlen
= 64;
739 /* RFC 4861, Section 6.2.1 */
740 SET_FLAG(p
->opt
.flags
, ND_OPT_PI_FLAG_ONLINK
, true);
741 SET_FLAG(p
->opt
.flags
, ND_OPT_PI_FLAG_AUTO
, true);
742 p
->opt
.preferred_lifetime
= htobe32(604800);
743 p
->opt
.valid_lifetime
= htobe32(2592000);
745 LIST_INIT(prefix
, p
);
752 _public_ sd_radv_prefix
*sd_radv_prefix_ref(sd_radv_prefix
*p
) {
756 assert(p
->n_ref
> 0);
762 _public_ sd_radv_prefix
*sd_radv_prefix_unref(sd_radv_prefix
*p
) {
766 assert(p
->n_ref
> 0);
775 _public_
int sd_radv_prefix_set_prefix(sd_radv_prefix
*p
, const struct in6_addr
*in6_addr
,
776 unsigned char prefixlen
) {
777 assert_return(p
, -EINVAL
);
778 assert_return(in6_addr
, -EINVAL
);
780 if (prefixlen
< 3 || prefixlen
> 128)
784 /* unusual but allowed, log it */
785 log_radv("Unusual prefix length %d greater than 64", prefixlen
);
787 p
->opt
.in6_addr
= *in6_addr
;
788 p
->opt
.prefixlen
= prefixlen
;
793 _public_
int sd_radv_prefix_set_onlink(sd_radv_prefix
*p
, int onlink
) {
794 assert_return(p
, -EINVAL
);
796 SET_FLAG(p
->opt
.flags
, ND_OPT_PI_FLAG_ONLINK
, onlink
);
801 _public_
int sd_radv_prefix_set_address_autoconfiguration(sd_radv_prefix
*p
,
802 int address_autoconfiguration
) {
803 assert_return(p
, -EINVAL
);
805 SET_FLAG(p
->opt
.flags
, ND_OPT_PI_FLAG_AUTO
, address_autoconfiguration
);
810 _public_
int sd_radv_prefix_set_valid_lifetime(sd_radv_prefix
*p
,
811 uint32_t valid_lifetime
) {
812 assert_return(p
, -EINVAL
);
814 p
->opt
.valid_lifetime
= htobe32(valid_lifetime
);
819 _public_
int sd_radv_prefix_set_preferred_lifetime(sd_radv_prefix
*p
,
820 uint32_t preferred_lifetime
) {
821 assert_return(p
, -EINVAL
);
823 p
->opt
.preferred_lifetime
= htobe32(preferred_lifetime
);