2 This file is part of systemd.
4 Copyright (C) 2014 Intel Corporation. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/icmp6.h>
21 #include <netinet/in.h>
25 #include "alloc-util.h"
27 #include "icmp6-util.h"
28 #include "in-addr-util.h"
29 #include "ndisc-internal.h"
30 #include "ndisc-router.h"
31 #include "random-util.h"
32 #include "socket-util.h"
33 #include "string-util.h"
36 #define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
38 static void ndisc_callback(sd_ndisc
*ndisc
, sd_ndisc_event event
, sd_ndisc_router
*rt
) {
41 log_ndisc("Invoking callback for '%c'.", event
);
46 ndisc
->callback(ndisc
, event
, rt
, ndisc
->userdata
);
49 _public_
int sd_ndisc_set_callback(
51 sd_ndisc_callback_t callback
,
54 assert_return(nd
, -EINVAL
);
56 nd
->callback
= callback
;
57 nd
->userdata
= userdata
;
62 _public_
int sd_ndisc_set_ifindex(sd_ndisc
*nd
, int ifindex
) {
63 assert_return(nd
, -EINVAL
);
64 assert_return(ifindex
> 0, -EINVAL
);
65 assert_return(nd
->fd
< 0, -EBUSY
);
67 nd
->ifindex
= ifindex
;
71 _public_
int sd_ndisc_set_mac(sd_ndisc
*nd
, const struct ether_addr
*mac_addr
) {
72 assert_return(nd
, -EINVAL
);
75 nd
->mac_addr
= *mac_addr
;
82 _public_
int sd_ndisc_attach_event(sd_ndisc
*nd
, sd_event
*event
, int64_t priority
) {
85 assert_return(nd
, -EINVAL
);
86 assert_return(nd
->fd
< 0, -EBUSY
);
87 assert_return(!nd
->event
, -EBUSY
);
90 nd
->event
= sd_event_ref(event
);
92 r
= sd_event_default(&nd
->event
);
97 nd
->event_priority
= priority
;
102 _public_
int sd_ndisc_detach_event(sd_ndisc
*nd
) {
104 assert_return(nd
, -EINVAL
);
105 assert_return(nd
->fd
< 0, -EBUSY
);
107 nd
->event
= sd_event_unref(nd
->event
);
111 _public_ sd_event
*sd_ndisc_get_event(sd_ndisc
*nd
) {
112 assert_return(nd
, NULL
);
117 _public_ sd_ndisc
*sd_ndisc_ref(sd_ndisc
*nd
) {
122 assert(nd
->n_ref
> 0);
128 static int ndisc_reset(sd_ndisc
*nd
) {
131 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
132 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
133 nd
->retransmit_time
= 0;
134 nd
->recv_event_source
= sd_event_source_unref(nd
->recv_event_source
);
135 nd
->fd
= safe_close(nd
->fd
);
140 _public_ sd_ndisc
*sd_ndisc_unref(sd_ndisc
*nd
) {
145 assert(nd
->n_ref
> 0);
152 sd_ndisc_detach_event(nd
);
156 _public_
int sd_ndisc_new(sd_ndisc
**ret
) {
157 _cleanup_(sd_ndisc_unrefp
) sd_ndisc
*nd
= NULL
;
159 assert_return(ret
, -EINVAL
);
161 nd
= new0(sd_ndisc
, 1);
174 _public_
int sd_ndisc_get_mtu(sd_ndisc
*nd
, uint32_t *mtu
) {
175 assert_return(nd
, -EINVAL
);
176 assert_return(mtu
, -EINVAL
);
185 _public_
int sd_ndisc_get_hop_limit(sd_ndisc
*nd
, uint8_t *ret
) {
186 assert_return(nd
, -EINVAL
);
187 assert_return(ret
, -EINVAL
);
189 if (nd
->hop_limit
== 0)
192 *ret
= nd
->hop_limit
;
196 static int ndisc_handle_datagram(sd_ndisc
*nd
, sd_ndisc_router
*rt
) {
202 r
= ndisc_router_parse(rt
);
203 if (r
== -EBADMSG
) /* Bad packet */
208 /* Update global variables we keep */
211 if (rt
->hop_limit
> 0)
212 nd
->hop_limit
= rt
->hop_limit
;
214 log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16
" sec",
215 rt
->flags
& ND_RA_FLAG_MANAGED
? "MANAGED" : rt
->flags
& ND_RA_FLAG_OTHER
? "OTHER" : "none",
216 rt
->preference
== SD_NDISC_PREFERENCE_HIGH
? "high" : rt
->preference
== SD_NDISC_PREFERENCE_LOW
? "low" : "medium",
219 ndisc_callback(nd
, SD_NDISC_EVENT_ROUTER
, rt
);
223 static int ndisc_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
224 _cleanup_(sd_ndisc_router_unrefp
) sd_ndisc_router
*rt
= NULL
;
225 sd_ndisc
*nd
= userdata
;
228 _cleanup_free_
char *addr
= NULL
;
234 buflen
= next_datagram_size_fd(fd
);
236 return log_ndisc_errno(buflen
, "Failed to determine datagram size to read: %m");
238 rt
= ndisc_router_new(buflen
);
242 r
= icmp6_receive(fd
, NDISC_ROUTER_RAW(rt
), rt
->raw_size
, &rt
->address
,
247 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &rt
->address
, &addr
);
248 log_ndisc("Received RA from non-link-local address %s. Ignoring", addr
);
252 log_ndisc("Received RA with invalid hop limit. Ignoring.");
256 log_ndisc("Received invalid source address from ICMPv6 socket.");
263 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
265 return ndisc_handle_datagram(nd
, rt
);
268 static usec_t
ndisc_timeout_compute_random(usec_t val
) {
269 /* compute a time that is random within ±10% of the given value */
270 return val
- val
/ 10 +
271 (random_u64() % (2 * USEC_PER_SEC
)) * val
/ 10 / USEC_PER_SEC
;
274 static int ndisc_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
275 sd_ndisc
*nd
= userdata
;
278 char time_string
[FORMAT_TIMESPAN_MAX
];
284 assert_se(sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
286 nd
->timeout_event_source
= sd_event_source_unref(nd
->timeout_event_source
);
288 if (!nd
->retransmit_time
)
289 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL
);
291 if (nd
->retransmit_time
> NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
/ 2)
292 nd
->retransmit_time
= ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL
);
294 nd
->retransmit_time
+= ndisc_timeout_compute_random(nd
->retransmit_time
);
297 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
,
298 clock_boottime_or_monotonic(),
299 time_now
+ nd
->retransmit_time
,
300 10 * USEC_PER_MSEC
, ndisc_timeout
, nd
);
304 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
308 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout-no-ra");
310 r
= sd_event_source_set_enabled(nd
->timeout_event_source
, SD_EVENT_ONESHOT
);
312 log_ndisc_errno(r
, "Error reenabling timer: %m");
316 r
= icmp6_send_router_solicitation(nd
->fd
, &nd
->mac_addr
);
318 log_ndisc_errno(r
, "Error sending Router Solicitation: %m");
322 log_ndisc("Sent Router Solicitation, next solicitation in %s",
323 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
,
324 nd
->retransmit_time
, USEC_PER_SEC
));
333 static int ndisc_timeout_no_ra(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
334 sd_ndisc
*nd
= userdata
;
339 log_ndisc("No RA received before link confirmation timeout");
341 nd
->timeout_no_ra
= sd_event_source_unref(nd
->timeout_no_ra
);
342 ndisc_callback(nd
, SD_NDISC_EVENT_TIMEOUT
, NULL
);
347 _public_
int sd_ndisc_stop(sd_ndisc
*nd
) {
348 assert_return(nd
, -EINVAL
);
353 log_ndisc("Stopping IPv6 Router Solicitation client");
359 _public_
int sd_ndisc_start(sd_ndisc
*nd
) {
363 assert_return(nd
, -EINVAL
);
364 assert_return(nd
->event
, -EINVAL
);
365 assert_return(nd
->ifindex
> 0, -EINVAL
);
370 assert(!nd
->recv_event_source
);
371 assert(!nd
->timeout_event_source
);
373 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
377 nd
->fd
= icmp6_bind_router_solicitation(nd
->ifindex
);
381 r
= sd_event_add_io(nd
->event
, &nd
->recv_event_source
, nd
->fd
, EPOLLIN
, ndisc_recv
, nd
);
385 r
= sd_event_source_set_priority(nd
->recv_event_source
, nd
->event_priority
);
389 (void) sd_event_source_set_description(nd
->recv_event_source
, "ndisc-receive-message");
391 r
= sd_event_add_time(nd
->event
, &nd
->timeout_event_source
, clock_boottime_or_monotonic(), 0, 0, ndisc_timeout
, nd
);
395 r
= sd_event_source_set_priority(nd
->timeout_event_source
, nd
->event_priority
);
399 (void) sd_event_source_set_description(nd
->timeout_event_source
, "ndisc-timeout");
401 r
= sd_event_add_time(nd
->event
, &nd
->timeout_no_ra
,
402 clock_boottime_or_monotonic(),
403 time_now
+ NDISC_TIMEOUT_NO_RA_USEC
,
404 10 * USEC_PER_MSEC
, ndisc_timeout_no_ra
, nd
);
408 r
= sd_event_source_set_priority(nd
->timeout_no_ra
, nd
->event_priority
);
412 (void) sd_event_source_set_description(nd
->timeout_no_ra
, "ndisc-timeout-no-ra");
414 log_ndisc("Started IPv6 Router Solicitation client");