]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ndisc.c
d95200c90daf881d7643c94b855c55ac2c71fd91
[thirdparty/systemd.git] / src / libsystemd-network / sd-ndisc.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright (C) 2014 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/icmp6.h>
7 #include <netinet/in.h>
8
9 #include "sd-ndisc.h"
10
11 #include "alloc-util.h"
12 #include "fd-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"
20 #include "util.h"
21
22 #define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
23
24 static void ndisc_callback(sd_ndisc *ndisc, sd_ndisc_event event, sd_ndisc_router *rt) {
25 assert(ndisc);
26
27 log_ndisc("Invoking callback for '%c'.", event);
28
29 if (!ndisc->callback)
30 return;
31
32 ndisc->callback(ndisc, event, rt, ndisc->userdata);
33 }
34
35 _public_ int sd_ndisc_set_callback(
36 sd_ndisc *nd,
37 sd_ndisc_callback_t callback,
38 void *userdata) {
39
40 assert_return(nd, -EINVAL);
41
42 nd->callback = callback;
43 nd->userdata = userdata;
44
45 return 0;
46 }
47
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);
52
53 nd->ifindex = ifindex;
54 return 0;
55 }
56
57 _public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) {
58 assert_return(nd, -EINVAL);
59
60 if (mac_addr)
61 nd->mac_addr = *mac_addr;
62 else
63 zero(nd->mac_addr);
64
65 return 0;
66 }
67
68 _public_ int sd_ndisc_attach_event(sd_ndisc *nd, sd_event *event, int64_t priority) {
69 int r;
70
71 assert_return(nd, -EINVAL);
72 assert_return(nd->fd < 0, -EBUSY);
73 assert_return(!nd->event, -EBUSY);
74
75 if (event)
76 nd->event = sd_event_ref(event);
77 else {
78 r = sd_event_default(&nd->event);
79 if (r < 0)
80 return 0;
81 }
82
83 nd->event_priority = priority;
84
85 return 0;
86 }
87
88 _public_ int sd_ndisc_detach_event(sd_ndisc *nd) {
89
90 assert_return(nd, -EINVAL);
91 assert_return(nd->fd < 0, -EBUSY);
92
93 nd->event = sd_event_unref(nd->event);
94 return 0;
95 }
96
97 _public_ sd_event *sd_ndisc_get_event(sd_ndisc *nd) {
98 assert_return(nd, NULL);
99
100 return nd->event;
101 }
102
103 _public_ sd_ndisc *sd_ndisc_ref(sd_ndisc *nd) {
104
105 if (!nd)
106 return NULL;
107
108 assert(nd->n_ref > 0);
109 nd->n_ref++;
110
111 return nd;
112 }
113
114 static int ndisc_reset(sd_ndisc *nd) {
115 assert(nd);
116
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);
122
123 return 0;
124 }
125
126 _public_ sd_ndisc *sd_ndisc_unref(sd_ndisc *nd) {
127
128 if (!nd)
129 return NULL;
130
131 assert(nd->n_ref > 0);
132 nd->n_ref--;
133
134 if (nd->n_ref > 0)
135 return NULL;
136
137 ndisc_reset(nd);
138 sd_ndisc_detach_event(nd);
139 return mfree(nd);
140 }
141
142 _public_ int sd_ndisc_new(sd_ndisc **ret) {
143 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
144
145 assert_return(ret, -EINVAL);
146
147 nd = new0(sd_ndisc, 1);
148 if (!nd)
149 return -ENOMEM;
150
151 nd->n_ref = 1;
152 nd->fd = -1;
153
154 *ret = TAKE_PTR(nd);
155
156 return 0;
157 }
158
159 _public_ int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *mtu) {
160 assert_return(nd, -EINVAL);
161 assert_return(mtu, -EINVAL);
162
163 if (nd->mtu == 0)
164 return -ENODATA;
165
166 *mtu = nd->mtu;
167 return 0;
168 }
169
170 _public_ int sd_ndisc_get_hop_limit(sd_ndisc *nd, uint8_t *ret) {
171 assert_return(nd, -EINVAL);
172 assert_return(ret, -EINVAL);
173
174 if (nd->hop_limit == 0)
175 return -ENODATA;
176
177 *ret = nd->hop_limit;
178 return 0;
179 }
180
181 static int ndisc_handle_datagram(sd_ndisc *nd, sd_ndisc_router *rt) {
182 int r;
183
184 assert(nd);
185 assert(rt);
186
187 r = ndisc_router_parse(rt);
188 if (r == -EBADMSG) /* Bad packet */
189 return 0;
190 if (r < 0)
191 return 0;
192
193 /* Update global variables we keep */
194 if (rt->mtu > 0)
195 nd->mtu = rt->mtu;
196 if (rt->hop_limit > 0)
197 nd->hop_limit = rt->hop_limit;
198
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",
202 rt->lifetime);
203
204 ndisc_callback(nd, SD_NDISC_EVENT_ROUTER, rt);
205 return 0;
206 }
207
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;
211 ssize_t buflen;
212 int r;
213 _cleanup_free_ char *addr = NULL;
214
215 assert(s);
216 assert(nd);
217 assert(nd->event);
218
219 buflen = next_datagram_size_fd(fd);
220 if (buflen < 0)
221 return log_ndisc_errno(buflen, "Failed to determine datagram size to read: %m");
222
223 rt = ndisc_router_new(buflen);
224 if (!rt)
225 return -ENOMEM;
226
227 r = icmp6_receive(fd, NDISC_ROUTER_RAW(rt), rt->raw_size, &rt->address,
228 &rt->timestamp);
229 if (r < 0) {
230 switch (r) {
231 case -EADDRNOTAVAIL:
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);
234 break;
235
236 case -EMULTIHOP:
237 log_ndisc("Received RA with invalid hop limit. Ignoring.");
238 break;
239
240 case -EPFNOSUPPORT:
241 log_ndisc("Received invalid source address from ICMPv6 socket.");
242 break;
243 }
244
245 return 0;
246 }
247
248 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
249
250 return ndisc_handle_datagram(nd, rt);
251 }
252
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;
257 }
258
259 static int ndisc_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
260 sd_ndisc *nd = userdata;
261 usec_t time_now;
262 int r;
263 char time_string[FORMAT_TIMESPAN_MAX];
264
265 assert(s);
266 assert(nd);
267 assert(nd->event);
268
269 assert_se(sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
270
271 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
272
273 if (!nd->retransmit_time)
274 nd->retransmit_time = ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL);
275 else {
276 if (nd->retransmit_time > NDISC_MAX_ROUTER_SOLICITATION_INTERVAL / 2)
277 nd->retransmit_time = ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL);
278 else
279 nd->retransmit_time += ndisc_timeout_compute_random(nd->retransmit_time);
280 }
281
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);
286 if (r < 0)
287 goto fail;
288
289 r = sd_event_source_set_priority(nd->timeout_event_source, nd->event_priority);
290 if (r < 0)
291 goto fail;
292
293 (void) sd_event_source_set_description(nd->timeout_event_source, "ndisc-timeout-no-ra");
294
295 r = sd_event_source_set_enabled(nd->timeout_event_source, SD_EVENT_ONESHOT);
296 if (r < 0) {
297 log_ndisc_errno(r, "Error reenabling timer: %m");
298 goto fail;
299 }
300
301 r = icmp6_send_router_solicitation(nd->fd, &nd->mac_addr);
302 if (r < 0) {
303 log_ndisc_errno(r, "Error sending Router Solicitation: %m");
304 goto fail;
305 }
306
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));
310
311 return 0;
312
313 fail:
314 sd_ndisc_stop(nd);
315 return 0;
316 }
317
318 static int ndisc_timeout_no_ra(sd_event_source *s, uint64_t usec, void *userdata) {
319 sd_ndisc *nd = userdata;
320
321 assert(s);
322 assert(nd);
323
324 log_ndisc("No RA received before link confirmation timeout");
325
326 nd->timeout_no_ra = sd_event_source_unref(nd->timeout_no_ra);
327 ndisc_callback(nd, SD_NDISC_EVENT_TIMEOUT, NULL);
328
329 return 0;
330 }
331
332 _public_ int sd_ndisc_stop(sd_ndisc *nd) {
333 assert_return(nd, -EINVAL);
334
335 if (nd->fd < 0)
336 return 0;
337
338 log_ndisc("Stopping IPv6 Router Solicitation client");
339
340 ndisc_reset(nd);
341 return 1;
342 }
343
344 _public_ int sd_ndisc_start(sd_ndisc *nd) {
345 int r;
346 usec_t time_now;
347
348 assert_return(nd, -EINVAL);
349 assert_return(nd->event, -EINVAL);
350 assert_return(nd->ifindex > 0, -EINVAL);
351
352 if (nd->fd >= 0)
353 return 0;
354
355 assert(!nd->recv_event_source);
356 assert(!nd->timeout_event_source);
357
358 r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
359 if (r < 0)
360 goto fail;
361
362 nd->fd = icmp6_bind_router_solicitation(nd->ifindex);
363 if (nd->fd < 0)
364 return nd->fd;
365
366 r = sd_event_add_io(nd->event, &nd->recv_event_source, nd->fd, EPOLLIN, ndisc_recv, nd);
367 if (r < 0)
368 goto fail;
369
370 r = sd_event_source_set_priority(nd->recv_event_source, nd->event_priority);
371 if (r < 0)
372 goto fail;
373
374 (void) sd_event_source_set_description(nd->recv_event_source, "ndisc-receive-message");
375
376 r = sd_event_add_time(nd->event, &nd->timeout_event_source, clock_boottime_or_monotonic(), 0, 0, ndisc_timeout, nd);
377 if (r < 0)
378 goto fail;
379
380 r = sd_event_source_set_priority(nd->timeout_event_source, nd->event_priority);
381 if (r < 0)
382 goto fail;
383
384 (void) sd_event_source_set_description(nd->timeout_event_source, "ndisc-timeout");
385
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);
390 if (r < 0)
391 goto fail;
392
393 r = sd_event_source_set_priority(nd->timeout_no_ra, nd->event_priority);
394 if (r < 0)
395 goto fail;
396
397 (void) sd_event_source_set_description(nd->timeout_no_ra, "ndisc-timeout-no-ra");
398
399 log_ndisc("Started IPv6 Router Solicitation client");
400 return 1;
401
402 fail:
403 ndisc_reset(nd);
404 return r;
405 }