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