1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "ndisc-internal.h"
17 #include "ndisc-router.h"
18 #include "random-util.h"
19 #include "socket-util.h"
20 #include "string-table.h"
21 #include "string-util.h"
24 #define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
26 static const char * const ndisc_event_table
[_SD_NDISC_EVENT_MAX
] = {
27 [SD_NDISC_EVENT_TIMEOUT
] = "timeout",
28 [SD_NDISC_EVENT_ROUTER
] = "router",
31 DEFINE_STRING_TABLE_LOOKUP(ndisc_event
, sd_ndisc_event
);
33 static void ndisc_callback(sd_ndisc
*ndisc
, sd_ndisc_event event
, sd_ndisc_router
*rt
) {
35 assert(event
>= 0 && event
< _SD_NDISC_EVENT_MAX
);
38 if (!ndisc
->callback
) {
39 log_ndisc("Received '%s' event.", ndisc_event_to_string(event
));
43 log_ndisc("Invoking callback for '%s' event.", ndisc_event_to_string(event
));
44 ndisc
->callback(ndisc
, event
, rt
, ndisc
->userdata
);
47 _public_
int sd_ndisc_set_callback(
49 sd_ndisc_callback_t callback
,
52 assert_return(nd
, -EINVAL
);
54 nd
->callback
= callback
;
55 nd
->userdata
= userdata
;
60 _public_
int sd_ndisc_set_ifindex(sd_ndisc
*nd
, int ifindex
) {
61 assert_return(nd
, -EINVAL
);
62 assert_return(ifindex
> 0, -EINVAL
);
63 assert_return(nd
->fd
< 0, -EBUSY
);
65 nd
->ifindex
= ifindex
;
69 _public_
int sd_ndisc_set_mac(sd_ndisc
*nd
, const struct ether_addr
*mac_addr
) {
70 assert_return(nd
, -EINVAL
);
73 nd
->mac_addr
= *mac_addr
;
80 _public_
int sd_ndisc_attach_event(sd_ndisc
*nd
, sd_event
*event
, int64_t priority
) {
83 assert_return(nd
, -EINVAL
);
84 assert_return(nd
->fd
< 0, -EBUSY
);
85 assert_return(!nd
->event
, -EBUSY
);
88 nd
->event
= sd_event_ref(event
);
90 r
= sd_event_default(&nd
->event
);
95 nd
->event_priority
= priority
;
100 _public_
int sd_ndisc_detach_event(sd_ndisc
*nd
) {
102 assert_return(nd
, -EINVAL
);
103 assert_return(nd
->fd
< 0, -EBUSY
);
105 nd
->event
= sd_event_unref(nd
->event
);
109 _public_ sd_event
*sd_ndisc_get_event(sd_ndisc
*nd
) {
110 assert_return(nd
, NULL
);
115 static void ndisc_reset(sd_ndisc
*nd
) {
118 (void) event_source_disable(nd
->timeout_event_source
);
119 (void) event_source_disable(nd
->timeout_no_ra
);
120 nd
->retransmit_time
= 0;
121 nd
->recv_event_source
= sd_event_source_unref(nd
->recv_event_source
);
122 nd
->fd
= safe_close(nd
->fd
);
125 static sd_ndisc
*ndisc_free(sd_ndisc
*nd
) {
128 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
129 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
132 sd_ndisc_detach_event(nd
);
136 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc
, sd_ndisc
, ndisc_free
);
138 _public_
int sd_ndisc_new(sd_ndisc
**ret
) {
139 _cleanup_(sd_ndisc_unrefp
) sd_ndisc
*nd
= NULL
;
141 assert_return(ret
, -EINVAL
);
143 nd
= new(sd_ndisc
, 1);
157 _public_
int sd_ndisc_get_mtu(sd_ndisc
*nd
, uint32_t *mtu
) {
158 assert_return(nd
, -EINVAL
);
159 assert_return(mtu
, -EINVAL
);
168 _public_
int sd_ndisc_get_hop_limit(sd_ndisc
*nd
, uint8_t *ret
) {
169 assert_return(nd
, -EINVAL
);
170 assert_return(ret
, -EINVAL
);
172 if (nd
->hop_limit
== 0)
175 *ret
= nd
->hop_limit
;
179 static int ndisc_handle_datagram(sd_ndisc
*nd
, sd_ndisc_router
*rt
) {
185 r
= ndisc_router_parse(rt
);
186 if (r
== -EBADMSG
) /* Bad packet */
191 /* Update global variables we keep */
194 if (rt
->hop_limit
> 0)
195 nd
->hop_limit
= rt
->hop_limit
;
197 log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16
" sec",
198 rt
->flags
& ND_RA_FLAG_MANAGED
? "MANAGED" : rt
->flags
& ND_RA_FLAG_OTHER
? "OTHER" : "none",
199 rt
->preference
== SD_NDISC_PREFERENCE_HIGH
? "high" : rt
->preference
== SD_NDISC_PREFERENCE_LOW
? "low" : "medium",
202 ndisc_callback(nd
, SD_NDISC_EVENT_ROUTER
, rt
);
206 static int ndisc_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
207 _cleanup_(sd_ndisc_router_unrefp
) sd_ndisc_router
*rt
= NULL
;
208 sd_ndisc
*nd
= userdata
;
211 _cleanup_free_
char *addr
= NULL
;
217 buflen
= next_datagram_size_fd(fd
);
219 return log_ndisc_errno(buflen
, "Failed to determine datagram size to read: %m");
221 rt
= ndisc_router_new(buflen
);
225 r
= icmp6_receive(fd
, NDISC_ROUTER_RAW(rt
), rt
->raw_size
, &rt
->address
,
230 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &rt
->address
, &addr
);
231 log_ndisc("Received RA from non-link-local address %s. Ignoring", addr
);
235 log_ndisc("Received RA with invalid hop limit. Ignoring.");
239 log_ndisc("Received invalid source address from ICMPv6 socket. Ignoring.");
242 case -EAGAIN
: /* ignore spurious wakeups */
246 log_ndisc_errno(r
, "Unexpected error while reading from ICMPv6, ignoring: %m");
253 (void) event_source_disable(nd
->timeout_event_source
);
255 return ndisc_handle_datagram(nd
, rt
);
258 static usec_t
ndisc_timeout_compute_random(usec_t val
) {
259 /* compute a time that is random within ±10% of the given value */
260 return val
- val
/ 10 +
261 (random_u64() % (2 * USEC_PER_SEC
)) * val
/ 10 / USEC_PER_SEC
;
264 static int ndisc_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
265 char time_string
[FORMAT_TIMESPAN_MAX
];
266 sd_ndisc
*nd
= userdata
;
274 assert_se(sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
276 if (!nd
->retransmit_time
)
277 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL
);
279 if (nd
->retransmit_time
> NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
/ 2)
280 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
);
282 nd
->retransmit_time
+= ndisc_timeout_compute_random(nd
->retransmit_time
);
285 r
= event_reset_time(nd
->event
, &nd
->timeout_event_source
,
286 clock_boottime_or_monotonic(),
287 time_now
+ nd
->retransmit_time
, 10 * USEC_PER_MSEC
,
289 nd
->event_priority
, "ndisc-timeout-no-ra", true);
293 r
= icmp6_send_router_solicitation(nd
->fd
, &nd
->mac_addr
);
295 log_ndisc_errno(r
, "Error sending Router Solicitation: %m");
299 log_ndisc("Sent Router Solicitation, next solicitation in %s",
300 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
,
301 nd
->retransmit_time
, USEC_PER_SEC
));
306 (void) sd_ndisc_stop(nd
);
310 static int ndisc_timeout_no_ra(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
311 sd_ndisc
*nd
= userdata
;
316 log_ndisc("No RA received before link confirmation timeout");
318 (void) event_source_disable(nd
->timeout_no_ra
);
319 ndisc_callback(nd
, SD_NDISC_EVENT_TIMEOUT
, NULL
);
324 _public_
int sd_ndisc_stop(sd_ndisc
*nd
) {
325 assert_return(nd
, -EINVAL
);
330 log_ndisc("Stopping IPv6 Router Solicitation client");
336 _public_
int sd_ndisc_start(sd_ndisc
*nd
) {
340 assert_return(nd
, -EINVAL
);
341 assert_return(nd
->event
, -EINVAL
);
342 assert_return(nd
->ifindex
> 0, -EINVAL
);
347 assert(!nd
->recv_event_source
);
349 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
353 nd
->fd
= icmp6_bind_router_solicitation(nd
->ifindex
);
357 r
= sd_event_add_io(nd
->event
, &nd
->recv_event_source
, nd
->fd
, EPOLLIN
, ndisc_recv
, nd
);
361 r
= sd_event_source_set_priority(nd
->recv_event_source
, nd
->event_priority
);
365 (void) sd_event_source_set_description(nd
->recv_event_source
, "ndisc-receive-message");
367 r
= event_reset_time(nd
->event
, &nd
->timeout_event_source
,
368 clock_boottime_or_monotonic(),
371 nd
->event_priority
, "ndisc-timeout", true);
375 r
= event_reset_time(nd
->event
, &nd
->timeout_no_ra
,
376 clock_boottime_or_monotonic(),
377 time_now
+ NDISC_TIMEOUT_NO_RA_USEC
, 10 * USEC_PER_MSEC
,
378 ndisc_timeout_no_ra
, nd
,
379 nd
->event_priority
, "ndisc-timeout-no-ra", true);
383 log_ndisc("Started IPv6 Router Solicitation client");