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