1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2014 Intel Corporation. All rights reserved.
6 #include <netinet/icmp6.h>
7 #include <netinet/in.h>
11 #include "alloc-util.h"
12 #include "event-util.h"
14 #include "icmp6-util.h"
15 #include "in-addr-util.h"
16 #include "memory-util.h"
17 #include "ndisc-internal.h"
18 #include "ndisc-router.h"
19 #include "network-common.h"
20 #include "random-util.h"
21 #include "socket-util.h"
22 #include "string-table.h"
23 #include "string-util.h"
25 #define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
27 static const char * const ndisc_event_table
[_SD_NDISC_EVENT_MAX
] = {
28 [SD_NDISC_EVENT_TIMEOUT
] = "timeout",
29 [SD_NDISC_EVENT_ROUTER
] = "router",
32 DEFINE_STRING_TABLE_LOOKUP(ndisc_event
, sd_ndisc_event_t
);
34 static void ndisc_callback(sd_ndisc
*ndisc
, sd_ndisc_event_t event
, sd_ndisc_router
*rt
) {
36 assert(event
>= 0 && event
< _SD_NDISC_EVENT_MAX
);
39 return (void) log_ndisc(ndisc
, "Received '%s' event.", ndisc_event_to_string(event
));
41 log_ndisc(ndisc
, "Invoking callback for '%s' event.", ndisc_event_to_string(event
));
42 ndisc
->callback(ndisc
, event
, rt
, ndisc
->userdata
);
45 int sd_ndisc_set_callback(
47 sd_ndisc_callback_t callback
,
50 assert_return(nd
, -EINVAL
);
52 nd
->callback
= callback
;
53 nd
->userdata
= userdata
;
58 int sd_ndisc_set_ifindex(sd_ndisc
*nd
, int ifindex
) {
59 assert_return(nd
, -EINVAL
);
60 assert_return(ifindex
> 0, -EINVAL
);
61 assert_return(nd
->fd
< 0, -EBUSY
);
63 nd
->ifindex
= ifindex
;
67 int sd_ndisc_set_ifname(sd_ndisc
*nd
, const char *ifname
) {
68 assert_return(nd
, -EINVAL
);
69 assert_return(ifname
, -EINVAL
);
71 if (!ifname_valid_full(ifname
, IFNAME_VALID_ALTERNATIVE
))
74 return free_and_strdup(&nd
->ifname
, ifname
);
77 int sd_ndisc_get_ifname(sd_ndisc
*nd
, const char **ret
) {
80 assert_return(nd
, -EINVAL
);
82 r
= get_ifname(nd
->ifindex
, &nd
->ifname
);
92 int sd_ndisc_set_mac(sd_ndisc
*nd
, const struct ether_addr
*mac_addr
) {
93 assert_return(nd
, -EINVAL
);
96 nd
->mac_addr
= *mac_addr
;
103 int sd_ndisc_attach_event(sd_ndisc
*nd
, sd_event
*event
, int64_t priority
) {
106 assert_return(nd
, -EINVAL
);
107 assert_return(nd
->fd
< 0, -EBUSY
);
108 assert_return(!nd
->event
, -EBUSY
);
111 nd
->event
= sd_event_ref(event
);
113 r
= sd_event_default(&nd
->event
);
118 nd
->event_priority
= priority
;
123 int sd_ndisc_detach_event(sd_ndisc
*nd
) {
125 assert_return(nd
, -EINVAL
);
126 assert_return(nd
->fd
< 0, -EBUSY
);
128 nd
->event
= sd_event_unref(nd
->event
);
132 sd_event
*sd_ndisc_get_event(sd_ndisc
*nd
) {
133 assert_return(nd
, NULL
);
138 static void ndisc_reset(sd_ndisc
*nd
) {
141 (void) event_source_disable(nd
->timeout_event_source
);
142 (void) event_source_disable(nd
->timeout_no_ra
);
143 nd
->retransmit_time
= 0;
144 nd
->recv_event_source
= sd_event_source_disable_unref(nd
->recv_event_source
);
145 nd
->fd
= safe_close(nd
->fd
);
148 static sd_ndisc
*ndisc_free(sd_ndisc
*nd
) {
153 sd_event_source_unref(nd
->timeout_event_source
);
154 sd_event_source_unref(nd
->timeout_no_ra
);
155 sd_ndisc_detach_event(nd
);
161 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc
, sd_ndisc
, ndisc_free
);
163 int sd_ndisc_new(sd_ndisc
**ret
) {
164 _cleanup_(sd_ndisc_unrefp
) sd_ndisc
*nd
= NULL
;
166 assert_return(ret
, -EINVAL
);
168 nd
= new(sd_ndisc
, 1);
182 static int ndisc_handle_datagram(sd_ndisc
*nd
, sd_ndisc_router
*rt
) {
188 r
= ndisc_router_parse(nd
, rt
);
192 log_ndisc(nd
, "Received Router Advertisement: flags %s preference %s lifetime %" PRIu16
" sec",
193 rt
->flags
& ND_RA_FLAG_MANAGED
? "MANAGED" : rt
->flags
& ND_RA_FLAG_OTHER
? "OTHER" : "none",
194 rt
->preference
== SD_NDISC_PREFERENCE_HIGH
? "high" : rt
->preference
== SD_NDISC_PREFERENCE_LOW
? "low" : "medium",
197 ndisc_callback(nd
, SD_NDISC_EVENT_ROUTER
, rt
);
201 static int ndisc_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
202 _cleanup_(sd_ndisc_router_unrefp
) sd_ndisc_router
*rt
= NULL
;
203 sd_ndisc
*nd
= ASSERT_PTR(userdata
);
210 buflen
= next_datagram_size_fd(fd
);
212 if (ERRNO_IS_TRANSIENT(buflen
) || ERRNO_IS_DISCONNECT(buflen
))
215 log_ndisc_errno(nd
, buflen
, "Failed to determine datagram size to read, ignoring: %m");
219 rt
= ndisc_router_new(buflen
);
223 r
= icmp6_receive(fd
, NDISC_ROUTER_RAW(rt
), rt
->raw_size
, &rt
->address
, &rt
->timestamp
);
225 if (ERRNO_IS_TRANSIENT(r
) || ERRNO_IS_DISCONNECT(r
))
230 log_ndisc(nd
, "Received RA from non-link-local address %s. Ignoring.",
231 IN6_ADDR_TO_STRING(&rt
->address
));
235 log_ndisc(nd
, "Received RA with invalid hop limit. Ignoring.");
239 log_ndisc(nd
, "Received invalid source address from ICMPv6 socket. Ignoring.");
243 log_ndisc_errno(nd
, r
, "Unexpected error while reading from ICMPv6, ignoring: %m");
250 (void) event_source_disable(nd
->timeout_event_source
);
251 (void) ndisc_handle_datagram(nd
, rt
);
255 static usec_t
ndisc_timeout_compute_random(usec_t val
) {
256 /* compute a time that is random within ±10% of the given value */
257 return val
- val
/ 10 +
258 (random_u64() % (2 * USEC_PER_SEC
)) * val
/ 10 / USEC_PER_SEC
;
261 static int ndisc_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
262 sd_ndisc
*nd
= ASSERT_PTR(userdata
);
269 assert_se(sd_event_now(nd
->event
, CLOCK_BOOTTIME
, &time_now
) >= 0);
271 if (!nd
->retransmit_time
)
272 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL
);
274 if (nd
->retransmit_time
> NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
/ 2)
275 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
);
277 nd
->retransmit_time
+= ndisc_timeout_compute_random(nd
->retransmit_time
);
280 r
= event_reset_time(nd
->event
, &nd
->timeout_event_source
,
282 time_now
+ nd
->retransmit_time
, 10 * USEC_PER_MSEC
,
284 nd
->event_priority
, "ndisc-timeout-no-ra", true);
288 r
= icmp6_send_router_solicitation(nd
->fd
, &nd
->mac_addr
);
290 log_ndisc_errno(nd
, r
, "Failed to send Router Solicitation, next solicitation in %s, ignoring: %m",
291 FORMAT_TIMESPAN(nd
->retransmit_time
, USEC_PER_SEC
));
293 log_ndisc(nd
, "Sent Router Solicitation, next solicitation in %s",
294 FORMAT_TIMESPAN(nd
->retransmit_time
, USEC_PER_SEC
));
299 (void) sd_ndisc_stop(nd
);
303 static int ndisc_timeout_no_ra(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
304 sd_ndisc
*nd
= ASSERT_PTR(userdata
);
308 log_ndisc(nd
, "No RA received before link confirmation timeout");
310 (void) event_source_disable(nd
->timeout_no_ra
);
311 ndisc_callback(nd
, SD_NDISC_EVENT_TIMEOUT
, NULL
);
316 int sd_ndisc_stop(sd_ndisc
*nd
) {
323 log_ndisc(nd
, "Stopping IPv6 Router Solicitation client");
329 int sd_ndisc_start(sd_ndisc
*nd
) {
333 assert_return(nd
, -EINVAL
);
334 assert_return(nd
->event
, -EINVAL
);
335 assert_return(nd
->ifindex
> 0, -EINVAL
);
340 assert(!nd
->recv_event_source
);
342 r
= sd_event_now(nd
->event
, CLOCK_BOOTTIME
, &time_now
);
346 nd
->fd
= icmp6_bind_router_solicitation(nd
->ifindex
);
350 r
= sd_event_add_io(nd
->event
, &nd
->recv_event_source
, nd
->fd
, EPOLLIN
, ndisc_recv
, nd
);
354 r
= sd_event_source_set_priority(nd
->recv_event_source
, nd
->event_priority
);
358 (void) sd_event_source_set_description(nd
->recv_event_source
, "ndisc-receive-message");
360 r
= event_reset_time(nd
->event
, &nd
->timeout_event_source
,
362 time_now
+ USEC_PER_SEC
/ 2, 1 * USEC_PER_SEC
, /* See RFC 8415 sec. 18.2.1 */
364 nd
->event_priority
, "ndisc-timeout", true);
368 r
= event_reset_time(nd
->event
, &nd
->timeout_no_ra
,
370 time_now
+ NDISC_TIMEOUT_NO_RA_USEC
, 10 * USEC_PER_MSEC
,
371 ndisc_timeout_no_ra
, nd
,
372 nd
->event_priority
, "ndisc-timeout-no-ra", true);
376 log_ndisc(nd
, "Started IPv6 Router Solicitation client");