]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ndisc.c
07b0d7f70493909bb626de9486d65b90880bc725
[thirdparty/systemd.git] / src / libsystemd-network / sd-ndisc.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2014 Intel Corporation. All rights reserved.
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <netinet/icmp6.h>
21 #include <netinet/in.h>
22
23 #include "sd-ndisc.h"
24
25 #include "alloc-util.h"
26 #include "fd-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 "socket-util.h"
32 #include "string-util.h"
33 #include "util.h"
34
35 #define NDISC_ROUTER_SOLICITATION_INTERVAL (4U * USEC_PER_SEC)
36 #define NDISC_MAX_ROUTER_SOLICITATIONS 3U
37
38 static void ndisc_callback(sd_ndisc *ndisc, sd_ndisc_event event, sd_ndisc_router *rt) {
39 assert(ndisc);
40
41 log_ndisc("Invoking callback for '%c'.", event);
42
43 if (!ndisc->callback)
44 return;
45
46 ndisc->callback(ndisc, event, rt, ndisc->userdata);
47 }
48
49 _public_ int sd_ndisc_set_callback(
50 sd_ndisc *nd,
51 sd_ndisc_callback_t callback,
52 void *userdata) {
53
54 assert_return(nd, -EINVAL);
55
56 nd->callback = callback;
57 nd->userdata = userdata;
58
59 return 0;
60 }
61
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);
66
67 nd->ifindex = ifindex;
68 return 0;
69 }
70
71 _public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) {
72 assert_return(nd, -EINVAL);
73
74 if (mac_addr)
75 nd->mac_addr = *mac_addr;
76 else
77 zero(nd->mac_addr);
78
79 return 0;
80 }
81
82 _public_ int sd_ndisc_attach_event(sd_ndisc *nd, sd_event *event, int64_t priority) {
83 int r;
84
85 assert_return(nd, -EINVAL);
86 assert_return(nd->fd < 0, -EBUSY);
87 assert_return(!nd->event, -EBUSY);
88
89 if (event)
90 nd->event = sd_event_ref(event);
91 else {
92 r = sd_event_default(&nd->event);
93 if (r < 0)
94 return 0;
95 }
96
97 nd->event_priority = priority;
98
99 return 0;
100 }
101
102 _public_ int sd_ndisc_detach_event(sd_ndisc *nd) {
103
104 assert_return(nd, -EINVAL);
105 assert_return(nd->fd < 0, -EBUSY);
106
107 nd->event = sd_event_unref(nd->event);
108 return 0;
109 }
110
111 _public_ sd_event *sd_ndisc_get_event(sd_ndisc *nd) {
112 assert_return(nd, NULL);
113
114 return nd->event;
115 }
116
117 _public_ sd_ndisc *sd_ndisc_ref(sd_ndisc *nd) {
118
119 if (!nd)
120 return NULL;
121
122 assert(nd->n_ref > 0);
123 nd->n_ref++;
124
125 return nd;
126 }
127
128 static int ndisc_reset(sd_ndisc *nd) {
129 assert(nd);
130
131 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
132 nd->recv_event_source = sd_event_source_unref(nd->recv_event_source);
133 nd->fd = safe_close(nd->fd);
134
135 return 0;
136 }
137
138 _public_ sd_ndisc *sd_ndisc_unref(sd_ndisc *nd) {
139
140 if (!nd)
141 return NULL;
142
143 assert(nd->n_ref > 0);
144 nd->n_ref--;
145
146 if (nd->n_ref > 0)
147 return NULL;
148
149 ndisc_reset(nd);
150 sd_ndisc_detach_event(nd);
151 free(nd);
152
153 return NULL;
154 }
155
156 _public_ int sd_ndisc_new(sd_ndisc **ret) {
157 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
158
159 assert_return(ret, -EINVAL);
160
161 nd = new0(sd_ndisc, 1);
162 if (!nd)
163 return -ENOMEM;
164
165 nd->n_ref = 1;
166 nd->fd = -1;
167
168 *ret = nd;
169 nd = NULL;
170
171 return 0;
172 }
173
174 _public_ int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *mtu) {
175 assert_return(nd, -EINVAL);
176 assert_return(mtu, -EINVAL);
177
178 if (nd->mtu == 0)
179 return -ENODATA;
180
181 *mtu = nd->mtu;
182 return 0;
183 }
184
185 _public_ int sd_ndisc_get_hop_limit(sd_ndisc *nd, uint8_t *ret) {
186 assert_return(nd, -EINVAL);
187 assert_return(ret, -EINVAL);
188
189 if (nd->hop_limit == 0)
190 return -ENODATA;
191
192 *ret = nd->hop_limit;
193 return 0;
194 }
195
196 static int ndisc_handle_datagram(sd_ndisc *nd, sd_ndisc_router *rt) {
197 int r;
198
199 assert(nd);
200 assert(rt);
201
202 r = ndisc_router_parse(rt);
203 if (r == -EBADMSG) /* Bad packet */
204 return 0;
205 if (r < 0)
206 return 0;
207
208 /* Update global variables we keep */
209 if (rt->mtu > 0)
210 nd->mtu = rt->mtu;
211 if (rt->hop_limit > 0)
212 nd->hop_limit = rt->hop_limit;
213
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",
217 rt->lifetime);
218
219 ndisc_callback(nd, SD_NDISC_EVENT_ROUTER, rt);
220 return 0;
221 }
222
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;
226 union {
227 struct cmsghdr cmsghdr;
228 uint8_t buf[CMSG_SPACE(sizeof(int)) + /* ttl */
229 CMSG_SPACE(sizeof(struct timeval))];
230 } control = {};
231 struct iovec iov = {};
232 union sockaddr_union sa = {};
233 struct msghdr msg = {
234 .msg_name = &sa.sa,
235 .msg_namelen = sizeof(sa),
236 .msg_iov = &iov,
237 .msg_iovlen = 1,
238 .msg_control = &control,
239 .msg_controllen = sizeof(control),
240 };
241 struct cmsghdr *cmsg;
242 ssize_t len, buflen;
243
244 assert(s);
245 assert(nd);
246 assert(nd->event);
247
248 buflen = next_datagram_size_fd(fd);
249 if (buflen < 0)
250 return log_ndisc_errno(buflen, "Failed to determine datagram size to read: %m");
251
252 rt = ndisc_router_new(buflen);
253 if (!rt)
254 return -ENOMEM;
255
256 iov.iov_base = NDISC_ROUTER_RAW(rt);
257 iov.iov_len = rt->raw_size;
258
259 len = recvmsg(fd, &msg, MSG_DONTWAIT);
260 if (len < 0) {
261 if (errno == EAGAIN || errno == EINTR)
262 return 0;
263
264 return log_ndisc_errno(errno, "Could not receive message from ICMPv6 socket: %m");
265 }
266
267 if ((size_t) len != rt->raw_size) {
268 log_ndisc("Packet size mismatch.");
269 return -EINVAL;
270 }
271
272 if (msg.msg_namelen == sizeof(struct sockaddr_in6) &&
273 sa.in6.sin6_family == AF_INET6) {
274
275 if (in_addr_is_link_local(AF_INET6, (union in_addr_union*) &sa.in6.sin6_addr) <= 0) {
276 _cleanup_free_ char *addr = NULL;
277
278 (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &sa.in6.sin6_addr, &addr);
279 log_ndisc("Received RA from non-link-local address %s. Ignoring.", strna(addr));
280 return 0;
281 }
282
283 rt->address = sa.in6.sin6_addr;
284
285 } else if (msg.msg_namelen > 0) {
286 log_ndisc("Received invalid source address size from ICMPv6 socket: %zu bytes", (size_t) msg.msg_namelen);
287 return -EINVAL;
288 }
289
290 /* namelen == 0 only happens when running the test-suite over a socketpair */
291
292 assert(!(msg.msg_flags & MSG_CTRUNC));
293 assert(!(msg.msg_flags & MSG_TRUNC));
294
295 CMSG_FOREACH(cmsg, &msg) {
296 if (cmsg->cmsg_level == SOL_IPV6 &&
297 cmsg->cmsg_type == IPV6_HOPLIMIT &&
298 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
299 int hops = *(int*) CMSG_DATA(cmsg);
300
301 if (hops != 255) {
302 log_ndisc("Received RA with invalid hop limit %d. Ignoring.", hops);
303 return 0;
304 }
305 }
306
307 if (cmsg->cmsg_level == SOL_SOCKET &&
308 cmsg->cmsg_type == SO_TIMESTAMP &&
309 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
310 triple_timestamp_from_realtime(&rt->timestamp, timeval_load((struct timeval*) CMSG_DATA(cmsg)));
311 }
312
313 if (!triple_timestamp_is_set(&rt->timestamp))
314 triple_timestamp_get(&rt->timestamp);
315
316 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
317
318 return ndisc_handle_datagram(nd, rt);
319 }
320
321 static int ndisc_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
322 sd_ndisc *nd = userdata;
323 usec_t time_now, next_timeout;
324 int r;
325
326 assert(s);
327 assert(nd);
328 assert(nd->event);
329
330 if (nd->nd_sent >= NDISC_MAX_ROUTER_SOLICITATIONS) {
331 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
332 ndisc_callback(nd, SD_NDISC_EVENT_TIMEOUT, NULL);
333 return 0;
334 }
335
336 r = icmp6_send_router_solicitation(nd->fd, &nd->mac_addr);
337 if (r < 0) {
338 log_ndisc_errno(r, "Error sending Router Solicitation: %m");
339 goto fail;
340 }
341
342 log_ndisc("Sent Router Solicitation");
343 nd->nd_sent++;
344
345 assert_se(sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
346 next_timeout = time_now + NDISC_ROUTER_SOLICITATION_INTERVAL;
347
348 r = sd_event_source_set_time(nd->timeout_event_source, next_timeout);
349 if (r < 0) {
350 log_ndisc_errno(r, "Error updating timer: %m");
351 goto fail;
352 }
353
354 r = sd_event_source_set_enabled(nd->timeout_event_source, SD_EVENT_ONESHOT);
355 if (r < 0) {
356 log_ndisc_errno(r, "Error reenabling timer: %m");
357 goto fail;
358 }
359
360 return 0;
361
362 fail:
363 sd_ndisc_stop(nd);
364 return 0;
365 }
366
367 _public_ int sd_ndisc_stop(sd_ndisc *nd) {
368 assert_return(nd, -EINVAL);
369
370 if (nd->fd < 0)
371 return 0;
372
373 log_ndisc("Stopping IPv6 Router Solicitation client");
374
375 ndisc_reset(nd);
376 return 1;
377 }
378
379 _public_ int sd_ndisc_start(sd_ndisc *nd) {
380 int r;
381
382 assert_return(nd, -EINVAL);
383 assert_return(nd->event, -EINVAL);
384 assert_return(nd->ifindex > 0, -EINVAL);
385
386 if (nd->fd >= 0)
387 return 0;
388
389 assert(!nd->recv_event_source);
390 assert(!nd->timeout_event_source);
391
392 nd->fd = icmp6_bind_router_solicitation(nd->ifindex);
393 if (nd->fd < 0)
394 return nd->fd;
395
396 r = sd_event_add_io(nd->event, &nd->recv_event_source, nd->fd, EPOLLIN, ndisc_recv, nd);
397 if (r < 0)
398 goto fail;
399
400 r = sd_event_source_set_priority(nd->recv_event_source, nd->event_priority);
401 if (r < 0)
402 goto fail;
403
404 (void) sd_event_source_set_description(nd->recv_event_source, "ndisc-receive-message");
405
406 r = sd_event_add_time(nd->event, &nd->timeout_event_source, clock_boottime_or_monotonic(), 0, 0, ndisc_timeout, nd);
407 if (r < 0)
408 goto fail;
409
410 r = sd_event_source_set_priority(nd->timeout_event_source, nd->event_priority);
411 if (r < 0)
412 goto fail;
413
414 (void) sd_event_source_set_description(nd->timeout_event_source, "ndisc-timeout");
415
416 log_ndisc("Started IPv6 Router Solicitation client");
417 return 1;
418
419 fail:
420 ndisc_reset(nd);
421 return r;
422 }