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"
13 #include "icmp6-util.h"
14 #include "in-addr-util.h"
15 #include "ndisc-internal.h"
16 #include "ndisc-router.h"
17 #include "random-util.h"
18 #include "socket-util.h"
19 #include "string-util.h"
22 #define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
24 static void ndisc_callback(sd_ndisc
*ndisc
, sd_ndisc_event event
, sd_ndisc_router
*rt
) {
27 log_ndisc("Invoking callback for '%c'.", event
);
32 ndisc
->callback(ndisc
, event
, rt
, ndisc
->userdata
);
35 _public_
int sd_ndisc_set_callback(
37 sd_ndisc_callback_t callback
,
40 assert_return(nd
, -EINVAL
);
42 nd
->callback
= callback
;
43 nd
->userdata
= userdata
;
48 _public_
int sd_ndisc_set_ifindex(sd_ndisc
*nd
, int ifindex
) {
49 assert_return(nd
, -EINVAL
);
50 assert_return(ifindex
> 0, -EINVAL
);
51 assert_return(nd
->fd
< 0, -EBUSY
);
53 nd
->ifindex
= ifindex
;
57 _public_
int sd_ndisc_set_mac(sd_ndisc
*nd
, const struct ether_addr
*mac_addr
) {
58 assert_return(nd
, -EINVAL
);
61 nd
->mac_addr
= *mac_addr
;
68 _public_
int sd_ndisc_attach_event(sd_ndisc
*nd
, sd_event
*event
, int64_t priority
) {
71 assert_return(nd
, -EINVAL
);
72 assert_return(nd
->fd
< 0, -EBUSY
);
73 assert_return(!nd
->event
, -EBUSY
);
76 nd
->event
= sd_event_ref(event
);
78 r
= sd_event_default(&nd
->event
);
83 nd
->event_priority
= priority
;
88 _public_
int sd_ndisc_detach_event(sd_ndisc
*nd
) {
90 assert_return(nd
, -EINVAL
);
91 assert_return(nd
->fd
< 0, -EBUSY
);
93 nd
->event
= sd_event_unref(nd
->event
);
97 _public_ sd_event
*sd_ndisc_get_event(sd_ndisc
*nd
) {
98 assert_return(nd
, NULL
);
103 static int ndisc_reset(sd_ndisc
*nd
) {
106 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
107 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
108 nd
->retransmit_time
= 0;
109 nd
->recv_event_source
= sd_event_source_unref(nd
->recv_event_source
);
110 nd
->fd
= safe_close(nd
->fd
);
115 static sd_ndisc
*ndisc_free(sd_ndisc
*nd
) {
119 sd_ndisc_detach_event(nd
);
123 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc
, sd_ndisc
, ndisc_free
);
125 _public_
int sd_ndisc_new(sd_ndisc
**ret
) {
126 _cleanup_(sd_ndisc_unrefp
) sd_ndisc
*nd
= NULL
;
128 assert_return(ret
, -EINVAL
);
130 nd
= new0(sd_ndisc
, 1);
142 _public_
int sd_ndisc_get_mtu(sd_ndisc
*nd
, uint32_t *mtu
) {
143 assert_return(nd
, -EINVAL
);
144 assert_return(mtu
, -EINVAL
);
153 _public_
int sd_ndisc_get_hop_limit(sd_ndisc
*nd
, uint8_t *ret
) {
154 assert_return(nd
, -EINVAL
);
155 assert_return(ret
, -EINVAL
);
157 if (nd
->hop_limit
== 0)
160 *ret
= nd
->hop_limit
;
164 static int ndisc_handle_datagram(sd_ndisc
*nd
, sd_ndisc_router
*rt
) {
170 r
= ndisc_router_parse(rt
);
171 if (r
== -EBADMSG
) /* Bad packet */
176 /* Update global variables we keep */
179 if (rt
->hop_limit
> 0)
180 nd
->hop_limit
= rt
->hop_limit
;
182 log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16
" sec",
183 rt
->flags
& ND_RA_FLAG_MANAGED
? "MANAGED" : rt
->flags
& ND_RA_FLAG_OTHER
? "OTHER" : "none",
184 rt
->preference
== SD_NDISC_PREFERENCE_HIGH
? "high" : rt
->preference
== SD_NDISC_PREFERENCE_LOW
? "low" : "medium",
187 ndisc_callback(nd
, SD_NDISC_EVENT_ROUTER
, rt
);
191 static int ndisc_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
192 _cleanup_(sd_ndisc_router_unrefp
) sd_ndisc_router
*rt
= NULL
;
193 sd_ndisc
*nd
= userdata
;
196 _cleanup_free_
char *addr
= NULL
;
202 buflen
= next_datagram_size_fd(fd
);
204 return log_ndisc_errno(buflen
, "Failed to determine datagram size to read: %m");
206 rt
= ndisc_router_new(buflen
);
210 r
= icmp6_receive(fd
, NDISC_ROUTER_RAW(rt
), rt
->raw_size
, &rt
->address
,
215 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &rt
->address
, &addr
);
216 log_ndisc("Received RA from non-link-local address %s. Ignoring", addr
);
220 log_ndisc("Received RA with invalid hop limit. Ignoring.");
224 log_ndisc("Received invalid source address from ICMPv6 socket. Ignoring.");
227 case -EAGAIN
: /* ignore spurious wakeups */
231 log_ndisc_errno(r
, "Unexpected error while reading from ICMPv6, ignoring: %m");
238 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
240 return ndisc_handle_datagram(nd
, rt
);
243 static usec_t
ndisc_timeout_compute_random(usec_t val
) {
244 /* compute a time that is random within ±10% of the given value */
245 return val
- val
/ 10 +
246 (random_u64() % (2 * USEC_PER_SEC
)) * val
/ 10 / USEC_PER_SEC
;
249 static int ndisc_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
250 sd_ndisc
*nd
= userdata
;
253 char time_string
[FORMAT_TIMESPAN_MAX
];
259 assert_se(sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
261 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
263 if (!nd
->retransmit_time
)
264 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL
);
266 if (nd
->retransmit_time
> NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
/ 2)
267 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
);
269 nd
->retransmit_time
+= ndisc_timeout_compute_random(nd
->retransmit_time
);
272 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
,
273 clock_boottime_or_monotonic(),
274 time_now
+ nd
->retransmit_time
,
275 10 * USEC_PER_MSEC
, ndisc_timeout
, nd
);
279 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
283 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout-no-ra");
285 r
= sd_event_source_set_enabled(nd
->timeout_event_source
, SD_EVENT_ONESHOT
);
287 log_ndisc_errno(r
, "Error reenabling timer: %m");
291 r
= icmp6_send_router_solicitation(nd
->fd
, &nd
->mac_addr
);
293 log_ndisc_errno(r
, "Error sending Router Solicitation: %m");
297 log_ndisc("Sent Router Solicitation, next solicitation in %s",
298 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
,
299 nd
->retransmit_time
, USEC_PER_SEC
));
308 static int ndisc_timeout_no_ra(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
309 sd_ndisc
*nd
= userdata
;
314 log_ndisc("No RA received before link confirmation timeout");
316 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
317 ndisc_callback(nd
, SD_NDISC_EVENT_TIMEOUT
, NULL
);
322 _public_
int sd_ndisc_stop(sd_ndisc
*nd
) {
323 assert_return(nd
, -EINVAL
);
328 log_ndisc("Stopping IPv6 Router Solicitation client");
334 _public_
int sd_ndisc_start(sd_ndisc
*nd
) {
338 assert_return(nd
, -EINVAL
);
339 assert_return(nd
->event
, -EINVAL
);
340 assert_return(nd
->ifindex
> 0, -EINVAL
);
345 assert(!nd
->recv_event_source
);
346 assert(!nd
->timeout_event_source
);
348 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
352 nd
->fd
= icmp6_bind_router_solicitation(nd
->ifindex
);
356 r
= sd_event_add_io(nd
->event
, &nd
->recv_event_source
, nd
->fd
, EPOLLIN
, ndisc_recv
, nd
);
360 r
= sd_event_source_set_priority(nd
->recv_event_source
, nd
->event_priority
);
364 (void) sd_event_source_set_description(nd
->recv_event_source
, "ndisc-receive-message");
366 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
, clock_boottime_or_monotonic(), 0, 0, ndisc_timeout
, nd
);
370 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
374 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout");
376 r
= sd_event_add_time(nd
->event
, &nd
->timeout_no_ra
,
377 clock_boottime_or_monotonic(),
378 time_now
+ NDISC_TIMEOUT_NO_RA_USEC
,
379 10 * USEC_PER_MSEC
, ndisc_timeout_no_ra
, nd
);
383 r
= sd_event_source_set_priority(nd
->timeout_no_ra
, nd
->event_priority
);
387 (void) sd_event_source_set_description(nd
->timeout_no_ra
, "ndisc-timeout-no-ra");
389 log_ndisc("Started IPv6 Router Solicitation client");