1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2014 Intel Corporation. All rights reserved.
8 #include <netinet/icmp6.h>
9 #include <netinet/in.h>
13 #include "alloc-util.h"
15 #include "icmp6-util.h"
16 #include "in-addr-util.h"
17 #include "ndisc-internal.h"
18 #include "ndisc-router.h"
19 #include "random-util.h"
20 #include "socket-util.h"
21 #include "string-util.h"
24 #define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
26 static void ndisc_callback(sd_ndisc
*ndisc
, sd_ndisc_event event
, sd_ndisc_router
*rt
) {
29 log_ndisc("Invoking callback for '%c'.", event
);
34 ndisc
->callback(ndisc
, event
, rt
, ndisc
->userdata
);
37 _public_
int sd_ndisc_set_callback(
39 sd_ndisc_callback_t callback
,
42 assert_return(nd
, -EINVAL
);
44 nd
->callback
= callback
;
45 nd
->userdata
= userdata
;
50 _public_
int sd_ndisc_set_ifindex(sd_ndisc
*nd
, int ifindex
) {
51 assert_return(nd
, -EINVAL
);
52 assert_return(ifindex
> 0, -EINVAL
);
53 assert_return(nd
->fd
< 0, -EBUSY
);
55 nd
->ifindex
= ifindex
;
59 _public_
int sd_ndisc_set_mac(sd_ndisc
*nd
, const struct ether_addr
*mac_addr
) {
60 assert_return(nd
, -EINVAL
);
63 nd
->mac_addr
= *mac_addr
;
70 _public_
int sd_ndisc_attach_event(sd_ndisc
*nd
, sd_event
*event
, int64_t priority
) {
73 assert_return(nd
, -EINVAL
);
74 assert_return(nd
->fd
< 0, -EBUSY
);
75 assert_return(!nd
->event
, -EBUSY
);
78 nd
->event
= sd_event_ref(event
);
80 r
= sd_event_default(&nd
->event
);
85 nd
->event_priority
= priority
;
90 _public_
int sd_ndisc_detach_event(sd_ndisc
*nd
) {
92 assert_return(nd
, -EINVAL
);
93 assert_return(nd
->fd
< 0, -EBUSY
);
95 nd
->event
= sd_event_unref(nd
->event
);
99 _public_ sd_event
*sd_ndisc_get_event(sd_ndisc
*nd
) {
100 assert_return(nd
, NULL
);
105 _public_ sd_ndisc
*sd_ndisc_ref(sd_ndisc
*nd
) {
110 assert(nd
->n_ref
> 0);
116 static int ndisc_reset(sd_ndisc
*nd
) {
119 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
120 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
121 nd
->retransmit_time
= 0;
122 nd
->recv_event_source
= sd_event_source_unref(nd
->recv_event_source
);
123 nd
->fd
= safe_close(nd
->fd
);
128 _public_ sd_ndisc
*sd_ndisc_unref(sd_ndisc
*nd
) {
133 assert(nd
->n_ref
> 0);
140 sd_ndisc_detach_event(nd
);
144 _public_
int sd_ndisc_new(sd_ndisc
**ret
) {
145 _cleanup_(sd_ndisc_unrefp
) sd_ndisc
*nd
= NULL
;
147 assert_return(ret
, -EINVAL
);
149 nd
= new0(sd_ndisc
, 1);
161 _public_
int sd_ndisc_get_mtu(sd_ndisc
*nd
, uint32_t *mtu
) {
162 assert_return(nd
, -EINVAL
);
163 assert_return(mtu
, -EINVAL
);
172 _public_
int sd_ndisc_get_hop_limit(sd_ndisc
*nd
, uint8_t *ret
) {
173 assert_return(nd
, -EINVAL
);
174 assert_return(ret
, -EINVAL
);
176 if (nd
->hop_limit
== 0)
179 *ret
= nd
->hop_limit
;
183 static int ndisc_handle_datagram(sd_ndisc
*nd
, sd_ndisc_router
*rt
) {
189 r
= ndisc_router_parse(rt
);
190 if (r
== -EBADMSG
) /* Bad packet */
195 /* Update global variables we keep */
198 if (rt
->hop_limit
> 0)
199 nd
->hop_limit
= rt
->hop_limit
;
201 log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16
" sec",
202 rt
->flags
& ND_RA_FLAG_MANAGED
? "MANAGED" : rt
->flags
& ND_RA_FLAG_OTHER
? "OTHER" : "none",
203 rt
->preference
== SD_NDISC_PREFERENCE_HIGH
? "high" : rt
->preference
== SD_NDISC_PREFERENCE_LOW
? "low" : "medium",
206 ndisc_callback(nd
, SD_NDISC_EVENT_ROUTER
, rt
);
210 static int ndisc_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
211 _cleanup_(sd_ndisc_router_unrefp
) sd_ndisc_router
*rt
= NULL
;
212 sd_ndisc
*nd
= userdata
;
215 _cleanup_free_
char *addr
= NULL
;
221 buflen
= next_datagram_size_fd(fd
);
223 return log_ndisc_errno(buflen
, "Failed to determine datagram size to read: %m");
225 rt
= ndisc_router_new(buflen
);
229 r
= icmp6_receive(fd
, NDISC_ROUTER_RAW(rt
), rt
->raw_size
, &rt
->address
,
234 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &rt
->address
, &addr
);
235 log_ndisc("Received RA from non-link-local address %s. Ignoring", addr
);
239 log_ndisc("Received RA with invalid hop limit. Ignoring.");
243 log_ndisc("Received invalid source address from ICMPv6 socket.");
250 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
252 return 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
= userdata
;
265 char time_string
[FORMAT_TIMESPAN_MAX
];
271 assert_se(sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
273 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
275 if (!nd
->retransmit_time
)
276 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL
);
278 if (nd
->retransmit_time
> NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
/ 2)
279 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
);
281 nd
->retransmit_time
+= ndisc_timeout_compute_random(nd
->retransmit_time
);
284 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
,
285 clock_boottime_or_monotonic(),
286 time_now
+ nd
->retransmit_time
,
287 10 * USEC_PER_MSEC
, ndisc_timeout
, nd
);
291 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
295 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout-no-ra");
297 r
= sd_event_source_set_enabled(nd
->timeout_event_source
, SD_EVENT_ONESHOT
);
299 log_ndisc_errno(r
, "Error reenabling timer: %m");
303 r
= icmp6_send_router_solicitation(nd
->fd
, &nd
->mac_addr
);
305 log_ndisc_errno(r
, "Error sending Router Solicitation: %m");
309 log_ndisc("Sent Router Solicitation, next solicitation in %s",
310 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
,
311 nd
->retransmit_time
, USEC_PER_SEC
));
320 static int ndisc_timeout_no_ra(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
321 sd_ndisc
*nd
= userdata
;
326 log_ndisc("No RA received before link confirmation timeout");
328 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
329 ndisc_callback(nd
, SD_NDISC_EVENT_TIMEOUT
, NULL
);
334 _public_
int sd_ndisc_stop(sd_ndisc
*nd
) {
335 assert_return(nd
, -EINVAL
);
340 log_ndisc("Stopping IPv6 Router Solicitation client");
346 _public_
int sd_ndisc_start(sd_ndisc
*nd
) {
350 assert_return(nd
, -EINVAL
);
351 assert_return(nd
->event
, -EINVAL
);
352 assert_return(nd
->ifindex
> 0, -EINVAL
);
357 assert(!nd
->recv_event_source
);
358 assert(!nd
->timeout_event_source
);
360 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
364 nd
->fd
= icmp6_bind_router_solicitation(nd
->ifindex
);
368 r
= sd_event_add_io(nd
->event
, &nd
->recv_event_source
, nd
->fd
, EPOLLIN
, ndisc_recv
, nd
);
372 r
= sd_event_source_set_priority(nd
->recv_event_source
, nd
->event_priority
);
376 (void) sd_event_source_set_description(nd
->recv_event_source
, "ndisc-receive-message");
378 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
, clock_boottime_or_monotonic(), 0, 0, ndisc_timeout
, nd
);
382 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
386 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout");
388 r
= sd_event_add_time(nd
->event
, &nd
->timeout_no_ra
,
389 clock_boottime_or_monotonic(),
390 time_now
+ NDISC_TIMEOUT_NO_RA_USEC
,
391 10 * USEC_PER_MSEC
, ndisc_timeout_no_ra
, nd
);
395 r
= sd_event_source_set_priority(nd
->timeout_no_ra
, nd
->event_priority
);
399 (void) sd_event_source_set_description(nd
->timeout_no_ra
, "ndisc-timeout-no-ra");
401 log_ndisc("Started IPv6 Router Solicitation client");