]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ndisc.c
Merge pull request #5960 from keszybz/journald-memleak
[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 nd->nd_sent = 0;
135
136 return 0;
137 }
138
139 _public_ sd_ndisc *sd_ndisc_unref(sd_ndisc *nd) {
140
141 if (!nd)
142 return NULL;
143
144 assert(nd->n_ref > 0);
145 nd->n_ref--;
146
147 if (nd->n_ref > 0)
148 return NULL;
149
150 ndisc_reset(nd);
151 sd_ndisc_detach_event(nd);
152 return mfree(nd);
153 }
154
155 _public_ int sd_ndisc_new(sd_ndisc **ret) {
156 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
157
158 assert_return(ret, -EINVAL);
159
160 nd = new0(sd_ndisc, 1);
161 if (!nd)
162 return -ENOMEM;
163
164 nd->n_ref = 1;
165 nd->fd = -1;
166
167 *ret = nd;
168 nd = NULL;
169
170 return 0;
171 }
172
173 _public_ int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *mtu) {
174 assert_return(nd, -EINVAL);
175 assert_return(mtu, -EINVAL);
176
177 if (nd->mtu == 0)
178 return -ENODATA;
179
180 *mtu = nd->mtu;
181 return 0;
182 }
183
184 _public_ int sd_ndisc_get_hop_limit(sd_ndisc *nd, uint8_t *ret) {
185 assert_return(nd, -EINVAL);
186 assert_return(ret, -EINVAL);
187
188 if (nd->hop_limit == 0)
189 return -ENODATA;
190
191 *ret = nd->hop_limit;
192 return 0;
193 }
194
195 static int ndisc_handle_datagram(sd_ndisc *nd, sd_ndisc_router *rt) {
196 int r;
197
198 assert(nd);
199 assert(rt);
200
201 r = ndisc_router_parse(rt);
202 if (r == -EBADMSG) /* Bad packet */
203 return 0;
204 if (r < 0)
205 return 0;
206
207 /* Update global variables we keep */
208 if (rt->mtu > 0)
209 nd->mtu = rt->mtu;
210 if (rt->hop_limit > 0)
211 nd->hop_limit = rt->hop_limit;
212
213 log_ndisc("Received Router Advertisement: flags %s preference %s lifetime %" PRIu16 " sec",
214 rt->flags & ND_RA_FLAG_MANAGED ? "MANAGED" : rt->flags & ND_RA_FLAG_OTHER ? "OTHER" : "none",
215 rt->preference == SD_NDISC_PREFERENCE_HIGH ? "high" : rt->preference == SD_NDISC_PREFERENCE_LOW ? "low" : "medium",
216 rt->lifetime);
217
218 ndisc_callback(nd, SD_NDISC_EVENT_ROUTER, rt);
219 return 0;
220 }
221
222 static int ndisc_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
223 _cleanup_(sd_ndisc_router_unrefp) sd_ndisc_router *rt = NULL;
224 sd_ndisc *nd = userdata;
225 ssize_t buflen;
226 int r;
227 _cleanup_free_ char *addr = NULL;
228
229 assert(s);
230 assert(nd);
231 assert(nd->event);
232
233 buflen = next_datagram_size_fd(fd);
234 if (buflen < 0)
235 return log_ndisc_errno(buflen, "Failed to determine datagram size to read: %m");
236
237 rt = ndisc_router_new(buflen);
238 if (!rt)
239 return -ENOMEM;
240
241 r = icmp6_receive(fd, NDISC_ROUTER_RAW(rt), rt->raw_size, &rt->address,
242 &rt->timestamp);
243 if (r < 0) {
244 switch (r) {
245 case -EADDRNOTAVAIL:
246 (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &rt->address, &addr);
247 log_ndisc("Received RA from non-link-local address %s. Ignoring", addr);
248 break;
249
250 case -EMULTIHOP:
251 log_ndisc("Received RA with invalid hop limit. Ignoring.");
252 break;
253
254 case -EPFNOSUPPORT:
255 log_ndisc("Received invalid source address from ICMPv6 socket.");
256 break;
257 }
258
259 return 0;
260 }
261
262 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
263
264 return ndisc_handle_datagram(nd, rt);
265 }
266
267 static int ndisc_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
268 sd_ndisc *nd = userdata;
269 usec_t time_now, next_timeout;
270 int r;
271
272 assert(s);
273 assert(nd);
274 assert(nd->event);
275
276 if (nd->nd_sent >= NDISC_MAX_ROUTER_SOLICITATIONS) {
277 nd->timeout_event_source = sd_event_source_unref(nd->timeout_event_source);
278 ndisc_callback(nd, SD_NDISC_EVENT_TIMEOUT, NULL);
279 return 0;
280 }
281
282 r = icmp6_send_router_solicitation(nd->fd, &nd->mac_addr);
283 if (r < 0) {
284 log_ndisc_errno(r, "Error sending Router Solicitation: %m");
285 goto fail;
286 }
287
288 log_ndisc("Sent Router Solicitation");
289 nd->nd_sent++;
290
291 assert_se(sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
292 next_timeout = time_now + NDISC_ROUTER_SOLICITATION_INTERVAL;
293
294 r = sd_event_source_set_time(nd->timeout_event_source, next_timeout);
295 if (r < 0) {
296 log_ndisc_errno(r, "Error updating timer: %m");
297 goto fail;
298 }
299
300 r = sd_event_source_set_enabled(nd->timeout_event_source, SD_EVENT_ONESHOT);
301 if (r < 0) {
302 log_ndisc_errno(r, "Error reenabling timer: %m");
303 goto fail;
304 }
305
306 return 0;
307
308 fail:
309 sd_ndisc_stop(nd);
310 return 0;
311 }
312
313 _public_ int sd_ndisc_stop(sd_ndisc *nd) {
314 assert_return(nd, -EINVAL);
315
316 if (nd->fd < 0)
317 return 0;
318
319 log_ndisc("Stopping IPv6 Router Solicitation client");
320
321 ndisc_reset(nd);
322 return 1;
323 }
324
325 _public_ int sd_ndisc_start(sd_ndisc *nd) {
326 int r;
327
328 assert_return(nd, -EINVAL);
329 assert_return(nd->event, -EINVAL);
330 assert_return(nd->ifindex > 0, -EINVAL);
331
332 if (nd->fd >= 0)
333 return 0;
334
335 assert(!nd->recv_event_source);
336 assert(!nd->timeout_event_source);
337
338 nd->fd = icmp6_bind_router_solicitation(nd->ifindex);
339 if (nd->fd < 0)
340 return nd->fd;
341
342 r = sd_event_add_io(nd->event, &nd->recv_event_source, nd->fd, EPOLLIN, ndisc_recv, nd);
343 if (r < 0)
344 goto fail;
345
346 r = sd_event_source_set_priority(nd->recv_event_source, nd->event_priority);
347 if (r < 0)
348 goto fail;
349
350 (void) sd_event_source_set_description(nd->recv_event_source, "ndisc-receive-message");
351
352 r = sd_event_add_time(nd->event, &nd->timeout_event_source, clock_boottime_or_monotonic(), 0, 0, ndisc_timeout, nd);
353 if (r < 0)
354 goto fail;
355
356 r = sd_event_source_set_priority(nd->timeout_event_source, nd->event_priority);
357 if (r < 0)
358 goto fail;
359
360 (void) sd_event_source_set_description(nd->timeout_event_source, "ndisc-timeout");
361
362 log_ndisc("Started IPv6 Router Solicitation client");
363 return 1;
364
365 fail:
366 ndisc_reset(nd);
367 return r;
368 }