1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright (C) 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 _public_ sd_ndisc
*sd_ndisc_ref(sd_ndisc
*nd
) {
108 assert(nd
->n_ref
> 0);
114 static int ndisc_reset(sd_ndisc
*nd
) {
117 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
118 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
119 nd
->retransmit_time
= 0;
120 nd
->recv_event_source
= sd_event_source_unref(nd
->recv_event_source
);
121 nd
->fd
= safe_close(nd
->fd
);
126 _public_ sd_ndisc
*sd_ndisc_unref(sd_ndisc
*nd
) {
131 assert(nd
->n_ref
> 0);
138 sd_ndisc_detach_event(nd
);
142 _public_
int sd_ndisc_new(sd_ndisc
**ret
) {
143 _cleanup_(sd_ndisc_unrefp
) sd_ndisc
*nd
= NULL
;
145 assert_return(ret
, -EINVAL
);
147 nd
= new0(sd_ndisc
, 1);
159 _public_
int sd_ndisc_get_mtu(sd_ndisc
*nd
, uint32_t *mtu
) {
160 assert_return(nd
, -EINVAL
);
161 assert_return(mtu
, -EINVAL
);
170 _public_
int sd_ndisc_get_hop_limit(sd_ndisc
*nd
, uint8_t *ret
) {
171 assert_return(nd
, -EINVAL
);
172 assert_return(ret
, -EINVAL
);
174 if (nd
->hop_limit
== 0)
177 *ret
= nd
->hop_limit
;
181 static int ndisc_handle_datagram(sd_ndisc
*nd
, sd_ndisc_router
*rt
) {
187 r
= ndisc_router_parse(rt
);
188 if (r
== -EBADMSG
) /* Bad packet */
193 /* Update global variables we keep */
196 if (rt
->hop_limit
> 0)
197 nd
->hop_limit
= rt
->hop_limit
;
199 log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16
" sec",
200 rt
->flags
& ND_RA_FLAG_MANAGED
? "MANAGED" : rt
->flags
& ND_RA_FLAG_OTHER
? "OTHER" : "none",
201 rt
->preference
== SD_NDISC_PREFERENCE_HIGH
? "high" : rt
->preference
== SD_NDISC_PREFERENCE_LOW
? "low" : "medium",
204 ndisc_callback(nd
, SD_NDISC_EVENT_ROUTER
, rt
);
208 static int ndisc_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
209 _cleanup_(sd_ndisc_router_unrefp
) sd_ndisc_router
*rt
= NULL
;
210 sd_ndisc
*nd
= userdata
;
213 _cleanup_free_
char *addr
= NULL
;
219 buflen
= next_datagram_size_fd(fd
);
221 return log_ndisc_errno(buflen
, "Failed to determine datagram size to read: %m");
223 rt
= ndisc_router_new(buflen
);
227 r
= icmp6_receive(fd
, NDISC_ROUTER_RAW(rt
), rt
->raw_size
, &rt
->address
,
232 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &rt
->address
, &addr
);
233 log_ndisc("Received RA from non-link-local address %s. Ignoring", addr
);
237 log_ndisc("Received RA with invalid hop limit. Ignoring.");
241 log_ndisc("Received invalid source address from ICMPv6 socket.");
248 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
250 return ndisc_handle_datagram(nd
, rt
);
253 static usec_t
ndisc_timeout_compute_random(usec_t val
) {
254 /* compute a time that is random within ±10% of the given value */
255 return val
- val
/ 10 +
256 (random_u64() % (2 * USEC_PER_SEC
)) * val
/ 10 / USEC_PER_SEC
;
259 static int ndisc_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
260 sd_ndisc
*nd
= userdata
;
263 char time_string
[FORMAT_TIMESPAN_MAX
];
269 assert_se(sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
271 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
273 if (!nd
->retransmit_time
)
274 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL
);
276 if (nd
->retransmit_time
> NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
/ 2)
277 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
);
279 nd
->retransmit_time
+= ndisc_timeout_compute_random(nd
->retransmit_time
);
282 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
,
283 clock_boottime_or_monotonic(),
284 time_now
+ nd
->retransmit_time
,
285 10 * USEC_PER_MSEC
, ndisc_timeout
, nd
);
289 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
293 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout-no-ra");
295 r
= sd_event_source_set_enabled(nd
->timeout_event_source
, SD_EVENT_ONESHOT
);
297 log_ndisc_errno(r
, "Error reenabling timer: %m");
301 r
= icmp6_send_router_solicitation(nd
->fd
, &nd
->mac_addr
);
303 log_ndisc_errno(r
, "Error sending Router Solicitation: %m");
307 log_ndisc("Sent Router Solicitation, next solicitation in %s",
308 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
,
309 nd
->retransmit_time
, USEC_PER_SEC
));
318 static int ndisc_timeout_no_ra(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
319 sd_ndisc
*nd
= userdata
;
324 log_ndisc("No RA received before link confirmation timeout");
326 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
327 ndisc_callback(nd
, SD_NDISC_EVENT_TIMEOUT
, NULL
);
332 _public_
int sd_ndisc_stop(sd_ndisc
*nd
) {
333 assert_return(nd
, -EINVAL
);
338 log_ndisc("Stopping IPv6 Router Solicitation client");
344 _public_
int sd_ndisc_start(sd_ndisc
*nd
) {
348 assert_return(nd
, -EINVAL
);
349 assert_return(nd
->event
, -EINVAL
);
350 assert_return(nd
->ifindex
> 0, -EINVAL
);
355 assert(!nd
->recv_event_source
);
356 assert(!nd
->timeout_event_source
);
358 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
362 nd
->fd
= icmp6_bind_router_solicitation(nd
->ifindex
);
366 r
= sd_event_add_io(nd
->event
, &nd
->recv_event_source
, nd
->fd
, EPOLLIN
, ndisc_recv
, nd
);
370 r
= sd_event_source_set_priority(nd
->recv_event_source
, nd
->event_priority
);
374 (void) sd_event_source_set_description(nd
->recv_event_source
, "ndisc-receive-message");
376 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
, clock_boottime_or_monotonic(), 0, 0, ndisc_timeout
, nd
);
380 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
384 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout");
386 r
= sd_event_add_time(nd
->event
, &nd
->timeout_no_ra
,
387 clock_boottime_or_monotonic(),
388 time_now
+ NDISC_TIMEOUT_NO_RA_USEC
,
389 10 * USEC_PER_MSEC
, ndisc_timeout_no_ra
, nd
);
393 r
= sd_event_source_set_priority(nd
->timeout_no_ra
, nd
->event_priority
);
397 (void) sd_event_source_set_description(nd
->timeout_no_ra
, "ndisc-timeout-no-ra");
399 log_ndisc("Started IPv6 Router Solicitation client");