]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ndisc.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / libsystemd-network / sd-ndisc.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 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 "event-util.h"
13 #include "fd-util.h"
14 #include "icmp6-util.h"
15 #include "in-addr-util.h"
16 #include "ndisc-internal.h"
17 #include "ndisc-router.h"
18 #include "random-util.h"
19 #include "socket-util.h"
20 #include "string-table.h"
21 #include "string-util.h"
22 #include "util.h"
23
24 #define NDISC_TIMEOUT_NO_RA_USEC (NDISC_ROUTER_SOLICITATION_INTERVAL * NDISC_MAX_ROUTER_SOLICITATIONS)
25
26 static const char * const ndisc_event_table[_SD_NDISC_EVENT_MAX] = {
27 [SD_NDISC_EVENT_TIMEOUT] = "timeout",
28 [SD_NDISC_EVENT_ROUTER] = "router",
29 };
30
31 DEFINE_STRING_TABLE_LOOKUP(ndisc_event, sd_ndisc_event);
32
33 static void ndisc_callback(sd_ndisc *ndisc, sd_ndisc_event event, sd_ndisc_router *rt) {
34 assert(ndisc);
35 assert(event >= 0 && event < _SD_NDISC_EVENT_MAX);
36
37 if (!ndisc->callback) {
38 log_ndisc("Received '%s' event.", ndisc_event_to_string(event));
39 return;
40 }
41
42 log_ndisc("Invoking callback for '%s' event.", ndisc_event_to_string(event));
43 ndisc->callback(ndisc, event, rt, ndisc->userdata);
44 }
45
46 _public_ int sd_ndisc_set_callback(
47 sd_ndisc *nd,
48 sd_ndisc_callback_t callback,
49 void *userdata) {
50
51 assert_return(nd, -EINVAL);
52
53 nd->callback = callback;
54 nd->userdata = userdata;
55
56 return 0;
57 }
58
59 _public_ int sd_ndisc_set_ifindex(sd_ndisc *nd, int ifindex) {
60 assert_return(nd, -EINVAL);
61 assert_return(ifindex > 0, -EINVAL);
62 assert_return(nd->fd < 0, -EBUSY);
63
64 nd->ifindex = ifindex;
65 return 0;
66 }
67
68 _public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) {
69 assert_return(nd, -EINVAL);
70
71 if (mac_addr)
72 nd->mac_addr = *mac_addr;
73 else
74 zero(nd->mac_addr);
75
76 return 0;
77 }
78
79 _public_ int sd_ndisc_attach_event(sd_ndisc *nd, sd_event *event, int64_t priority) {
80 int r;
81
82 assert_return(nd, -EINVAL);
83 assert_return(nd->fd < 0, -EBUSY);
84 assert_return(!nd->event, -EBUSY);
85
86 if (event)
87 nd->event = sd_event_ref(event);
88 else {
89 r = sd_event_default(&nd->event);
90 if (r < 0)
91 return 0;
92 }
93
94 nd->event_priority = priority;
95
96 return 0;
97 }
98
99 _public_ int sd_ndisc_detach_event(sd_ndisc *nd) {
100
101 assert_return(nd, -EINVAL);
102 assert_return(nd->fd < 0, -EBUSY);
103
104 nd->event = sd_event_unref(nd->event);
105 return 0;
106 }
107
108 _public_ sd_event *sd_ndisc_get_event(sd_ndisc *nd) {
109 assert_return(nd, NULL);
110
111 return nd->event;
112 }
113
114 static void ndisc_reset(sd_ndisc *nd) {
115 assert(nd);
116
117 (void) event_source_disable(nd->timeout_event_source);
118 (void) event_source_disable(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
124 static sd_ndisc *ndisc_free(sd_ndisc *nd) {
125 assert(nd);
126
127 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
128 nd->timeout_no_ra = sd_event_source_unref(nd->timeout_no_ra);
129
130 ndisc_reset(nd);
131 sd_ndisc_detach_event(nd);
132 return mfree(nd);
133 }
134
135 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc, sd_ndisc, ndisc_free);
136
137 _public_ int sd_ndisc_new(sd_ndisc **ret) {
138 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
139
140 assert_return(ret, -EINVAL);
141
142 nd = new(sd_ndisc, 1);
143 if (!nd)
144 return -ENOMEM;
145
146 *nd = (sd_ndisc) {
147 .n_ref = 1,
148 .fd = -1,
149 };
150
151 *ret = TAKE_PTR(nd);
152
153 return 0;
154 }
155
156 _public_ int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *mtu) {
157 assert_return(nd, -EINVAL);
158 assert_return(mtu, -EINVAL);
159
160 if (nd->mtu == 0)
161 return -ENODATA;
162
163 *mtu = nd->mtu;
164 return 0;
165 }
166
167 _public_ int sd_ndisc_get_hop_limit(sd_ndisc *nd, uint8_t *ret) {
168 assert_return(nd, -EINVAL);
169 assert_return(ret, -EINVAL);
170
171 if (nd->hop_limit == 0)
172 return -ENODATA;
173
174 *ret = nd->hop_limit;
175 return 0;
176 }
177
178 static int ndisc_handle_datagram(sd_ndisc *nd, sd_ndisc_router *rt) {
179 int r;
180
181 assert(nd);
182 assert(rt);
183
184 r = ndisc_router_parse(rt);
185 if (r == -EBADMSG) /* Bad packet */
186 return 0;
187 if (r < 0)
188 return 0;
189
190 /* Update global variables we keep */
191 if (rt->mtu > 0)
192 nd->mtu = rt->mtu;
193 if (rt->hop_limit > 0)
194 nd->hop_limit = rt->hop_limit;
195
196 log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16 " sec",
197 rt->flags & ND_RA_FLAG_MANAGED ? "MANAGED" : rt->flags & ND_RA_FLAG_OTHER ? "OTHER" : "none",
198 rt->preference == SD_NDISC_PREFERENCE_HIGH ? "high" : rt->preference == SD_NDISC_PREFERENCE_LOW ? "low" : "medium",
199 rt->lifetime);
200
201 ndisc_callback(nd, SD_NDISC_EVENT_ROUTER, rt);
202 return 0;
203 }
204
205 static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
206 _cleanup_(sd_ndisc_router_unrefp) sd_ndisc_router *rt = NULL;
207 sd_ndisc *nd = userdata;
208 ssize_t buflen;
209 int r;
210 _cleanup_free_ char *addr = NULL;
211
212 assert(s);
213 assert(nd);
214 assert(nd->event);
215
216 buflen = next_datagram_size_fd(fd);
217 if (buflen < 0)
218 return log_ndisc_errno(buflen, "Failed to determine datagram size to read: %m");
219
220 rt = ndisc_router_new(buflen);
221 if (!rt)
222 return -ENOMEM;
223
224 r = icmp6_receive(fd, NDISC_ROUTER_RAW(rt), rt->raw_size, &rt->address,
225 &rt->timestamp);
226 if (r < 0) {
227 switch (r) {
228 case -EADDRNOTAVAIL:
229 (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &rt->address, &addr);
230 log_ndisc("Received RA from non-link-local address %s. Ignoring", addr);
231 break;
232
233 case -EMULTIHOP:
234 log_ndisc("Received RA with invalid hop limit. Ignoring.");
235 break;
236
237 case -EPFNOSUPPORT:
238 log_ndisc("Received invalid source address from ICMPv6 socket. Ignoring.");
239 break;
240
241 case -EAGAIN: /* ignore spurious wakeups */
242 break;
243
244 default:
245 log_ndisc_errno(r, "Unexpected error while reading from ICMPv6, ignoring: %m");
246 break;
247 }
248
249 return 0;
250 }
251
252 (void) event_source_disable(nd->timeout_event_source);
253
254 return ndisc_handle_datagram(nd, rt);
255 }
256
257 static usec_t ndisc_timeout_compute_random(usec_t val) {
258 /* compute a time that is random within ±10% of the given value */
259 return val - val / 10 +
260 (random_u64() % (2 * USEC_PER_SEC)) * val / 10 / USEC_PER_SEC;
261 }
262
263 static int ndisc_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
264 char time_string[FORMAT_TIMESPAN_MAX];
265 sd_ndisc *nd = userdata;
266 usec_t time_now;
267 int r;
268
269 assert(s);
270 assert(nd);
271 assert(nd->event);
272
273 assert_se(sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
274
275 if (!nd->retransmit_time)
276 nd->retransmit_time = ndisc_timeout_compute_random(NDISC_ROUTER_SOLICITATION_INTERVAL);
277 else {
278 if (nd->retransmit_time > NDISC_MAX_ROUTER_SOLICITATION_INTERVAL / 2)
279 nd->retransmit_time = ndisc_timeout_compute_random(NDISC_MAX_ROUTER_SOLICITATION_INTERVAL);
280 else
281 nd->retransmit_time += ndisc_timeout_compute_random(nd->retransmit_time);
282 }
283
284 r = event_reset_time(nd->event, &nd->timeout_event_source,
285 clock_boottime_or_monotonic(),
286 time_now + nd->retransmit_time, 10 * USEC_PER_MSEC,
287 ndisc_timeout, nd,
288 nd->event_priority, "ndisc-timeout-no-ra", true);
289 if (r < 0)
290 goto fail;
291
292 r = icmp6_send_router_solicitation(nd->fd, &nd->mac_addr);
293 if (r < 0) {
294 log_ndisc_errno(r, "Error sending Router Solicitation: %m");
295 goto fail;
296 }
297
298 log_ndisc("Sent Router Solicitation, next solicitation in %s",
299 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
300 nd->retransmit_time, USEC_PER_SEC));
301
302 return 0;
303
304 fail:
305 (void) sd_ndisc_stop(nd);
306 return 0;
307 }
308
309 static int ndisc_timeout_no_ra(sd_event_source *s, uint64_t usec, void *userdata) {
310 sd_ndisc *nd = userdata;
311
312 assert(s);
313 assert(nd);
314
315 log_ndisc("No RA received before link confirmation timeout");
316
317 (void) event_source_disable(nd->timeout_no_ra);
318 ndisc_callback(nd, SD_NDISC_EVENT_TIMEOUT, NULL);
319
320 return 0;
321 }
322
323 _public_ int sd_ndisc_stop(sd_ndisc *nd) {
324 assert_return(nd, -EINVAL);
325
326 if (nd->fd < 0)
327 return 0;
328
329 log_ndisc("Stopping IPv6 Router Solicitation client");
330
331 ndisc_reset(nd);
332 return 1;
333 }
334
335 _public_ int sd_ndisc_start(sd_ndisc *nd) {
336 int r;
337 usec_t time_now;
338
339 assert_return(nd, -EINVAL);
340 assert_return(nd->event, -EINVAL);
341 assert_return(nd->ifindex > 0, -EINVAL);
342
343 if (nd->fd >= 0)
344 return 0;
345
346 assert(!nd->recv_event_source);
347
348 r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
349 if (r < 0)
350 goto fail;
351
352 nd->fd = icmp6_bind_router_solicitation(nd->ifindex);
353 if (nd->fd < 0)
354 return nd->fd;
355
356 r = sd_event_add_io(nd->event, &nd->recv_event_source, nd->fd, EPOLLIN, ndisc_recv, nd);
357 if (r < 0)
358 goto fail;
359
360 r = sd_event_source_set_priority(nd->recv_event_source, nd->event_priority);
361 if (r < 0)
362 goto fail;
363
364 (void) sd_event_source_set_description(nd->recv_event_source, "ndisc-receive-message");
365
366 r = event_reset_time(nd->event, &nd->timeout_event_source,
367 clock_boottime_or_monotonic(),
368 0, 0,
369 ndisc_timeout, nd,
370 nd->event_priority, "ndisc-timeout", true);
371 if (r < 0)
372 goto fail;
373
374 r = event_reset_time(nd->event, &nd->timeout_no_ra,
375 clock_boottime_or_monotonic(),
376 time_now + NDISC_TIMEOUT_NO_RA_USEC, 10 * USEC_PER_MSEC,
377 ndisc_timeout_no_ra, nd,
378 nd->event_priority, "ndisc-timeout-no-ra", true);
379 if (r < 0)
380 goto fail;
381
382 log_ndisc("Started IPv6 Router Solicitation client");
383 return 1;
384
385 fail:
386 ndisc_reset(nd);
387 return r;
388 }