]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-netlink/sd-netlink.c
sd-netlink: fix possible use-after-free
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / sd-netlink.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
65f568bb 2
65f568bb 3#include <poll.h>
65f568bb 4
1c4baffc 5#include "sd-netlink.h"
07630cea 6
b5efdb8a 7#include "alloc-util.h"
3ffd4af2 8#include "fd-util.h"
07630cea 9#include "hashmap.h"
0f2d351f 10#include "io-util.h"
07630cea 11#include "macro.h"
56fdc16d 12#include "netlink-genl.h"
1c4baffc 13#include "netlink-internal.h"
ee38400b 14#include "netlink-slot.h"
84e10015 15#include "netlink-util.h"
dccca82b 16#include "process-util.h"
2583fbea 17#include "socket-util.h"
8190a388 18#include "string-util.h"
65f568bb 19
b522c4b9
LP
20/* Some really high limit, to catch programming errors */
21#define REPLY_CALLBACKS_MAX UINT16_MAX
22
409856d3
YW
23static int netlink_new(sd_netlink **ret) {
24 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL;
65f568bb
TG
25
26 assert_return(ret, -EINVAL);
27
409856d3
YW
28 nl = new(sd_netlink, 1);
29 if (!nl)
65f568bb
TG
30 return -ENOMEM;
31
409856d3 32 *nl = (sd_netlink) {
f23ab4dc 33 .n_ref = 1,
2fea6090
YW
34 .fd = -1,
35 .sockaddr.nl.nl_family = AF_NETLINK,
36 .original_pid = getpid_cached(),
37 .protocol = -1,
adf412b9 38
ac3bc1b8
LP
39 /* Kernel change notification messages have sequence number 0. We want to avoid that with our
40 * own serials, in order not to get confused when matching up kernel replies to our earlier
41 * requests.
42 *
43 * Moreover, when using netlink socket activation (i.e. where PID 1 binds an AF_NETLINK
44 * socket for us and passes it to us across execve()) and we get restarted multiple times
45 * while the socket sticks around we might get confused by replies from earlier runs coming
46 * in late — which is pretty likely if we'd start our sequence numbers always from 1. Hence,
47 * let's start with a value based on the system clock. This should make collisions much less
48 * likely (though still theoretically possible). We use a 32 bit µs counter starting at boot
49 * for this (and explicitly exclude the zero, see above). This counter will wrap around after
50 * a bit more than 1h, but that's hopefully OK as the kernel shouldn't take that long to
51 * reply to our requests.
52 *
53 * We only pick the initial start value this way. For each message we simply increase the
54 * sequence number by 1. This means we could enqueue 1 netlink message per µs without risking
55 * collisions, which should be OK.
56 *
57 * Note this means the serials will be in the range 1…UINT32_MAX here.
58 *
59 * (In an ideal world we'd attach the current serial counter to the netlink socket itself
60 * somehow, to avoid all this, but I couldn't come up with a nice way to do this) */
61 .serial = (uint32_t) (now(CLOCK_MONOTONIC) % UINT32_MAX) + 1,
2fea6090 62 };
8cec01b9 63
409856d3 64 *ret = TAKE_PTR(nl);
65f568bb
TG
65 return 0;
66}
67
dd35a61c 68int sd_netlink_open_fd(sd_netlink **ret, int fd) {
409856d3 69 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL;
13ec9f10 70 int r, protocol;
65f568bb 71
9d0db178 72 assert_return(ret, -EINVAL);
8ac43fee 73 assert_return(fd >= 0, -EBADF);
9d0db178 74
409856d3 75 r = netlink_new(&nl);
65f568bb
TG
76 if (r < 0)
77 return r;
78
13ec9f10 79 r = getsockopt_int(fd, SOL_SOCKET, SO_PROTOCOL, &protocol);
05d0c2e3
JT
80 if (r < 0)
81 return r;
82
409856d3
YW
83 nl->fd = fd;
84 nl->protocol = protocol;
65f568bb 85
d7418b3b 86 r = setsockopt_int(fd, SOL_NETLINK, NETLINK_EXT_ACK, true);
e4a1e68d
YW
87 if (r < 0)
88 log_debug_errno(r, "sd-netlink: Failed to enable NETLINK_EXT_ACK option, ignoring: %m");
89
d7418b3b
YW
90 r = setsockopt_int(fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK, true);
91 if (r < 0)
92 log_debug_errno(r, "sd-netlink: Failed to enable NETLINK_GET_STRICT_CHK option, ignoring: %m");
93
409856d3 94 r = socket_bind(nl);
5c60db87 95 if (r < 0) {
409856d3
YW
96 nl->fd = -1; /* on failure, the caller remains owner of the fd, hence don't close it here */
97 nl->protocol = -1;
b95cc756 98 return r;
5c60db87 99 }
6d0b55c2 100
409856d3 101 *ret = TAKE_PTR(nl);
65f568bb
TG
102
103 return 0;
104}
105
dd35a61c 106int sd_netlink_open(sd_netlink **ret) {
05d0c2e3
JT
107 return netlink_open_family(ret, NETLINK_ROUTE);
108}
109
dd35a61c 110int sd_netlink_increase_rxbuf(sd_netlink *nl, size_t size) {
409856d3
YW
111 assert_return(nl, -EINVAL);
112 assert_return(!netlink_pid_changed(nl), -ECHILD);
75f8a779 113
28e7e934 114 return fd_increase_rxbuf(nl->fd, size);
be660c37
AR
115}
116
409856d3 117static sd_netlink *netlink_free(sd_netlink *nl) {
ee38400b 118 sd_netlink_slot *s;
1c71f7f3 119 unsigned i;
8c578303 120
409856d3 121 assert(nl);
4555ec72 122
409856d3
YW
123 for (i = 0; i < nl->rqueue_size; i++)
124 sd_netlink_message_unref(nl->rqueue[i]);
125 free(nl->rqueue);
8cec01b9 126
409856d3
YW
127 for (i = 0; i < nl->rqueue_partial_size; i++)
128 sd_netlink_message_unref(nl->rqueue_partial[i]);
129 free(nl->rqueue_partial);
4e996881 130
409856d3 131 free(nl->rbuffer);
a88f77c4 132
409856d3 133 while ((s = nl->slots)) {
ee38400b
YW
134 assert(s->floating);
135 netlink_slot_disconnect(s, true);
545bab1f 136 }
409856d3
YW
137 hashmap_free(nl->reply_callbacks);
138 prioq_free(nl->reply_callbacks_prioq);
22fdeadc 139
409856d3
YW
140 sd_event_source_unref(nl->io_event_source);
141 sd_event_source_unref(nl->time_event_source);
142 sd_event_unref(nl->event);
22fdeadc 143
409856d3 144 hashmap_free(nl->broadcast_group_refs);
9c5a882b 145
56fdc16d 146 genl_clear_family(nl);
4e8f0ef9 147
409856d3
YW
148 safe_close(nl->fd);
149 return mfree(nl);
65f568bb
TG
150}
151
f23ab4dc 152DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_netlink, sd_netlink, netlink_free);
1c71f7f3 153
dd35a61c 154int sd_netlink_send(
409856d3
YW
155 sd_netlink *nl,
156 sd_netlink_message *message,
157 uint32_t *serial) {
158
4555ec72 159 int r;
65f568bb
TG
160
161 assert_return(nl, -EINVAL);
409856d3 162 assert_return(!netlink_pid_changed(nl), -ECHILD);
65f568bb 163 assert_return(message, -EINVAL);
3dd215e0 164 assert_return(!message->sealed, -EPERM);
65f568bb 165
409856d3 166 netlink_seal_message(nl, message);
65f568bb 167
bbe181b4
TG
168 r = socket_write_message(nl, message);
169 if (r < 0)
170 return r;
65f568bb 171
4555ec72 172 if (serial)
409856d3 173 *serial = message_get_serial(message);
65f568bb 174
4555ec72
TG
175 return 1;
176}
65f568bb 177
409856d3
YW
178int netlink_rqueue_make_room(sd_netlink *nl) {
179 assert(nl);
1b89cf56 180
409856d3 181 if (nl->rqueue_size >= NETLINK_RQUEUE_MAX)
baaa35ad 182 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
409856d3
YW
183 "sd-netlink: exhausted the read queue size (%d)",
184 NETLINK_RQUEUE_MAX);
1b89cf56 185
409856d3 186 if (!GREEDY_REALLOC(nl->rqueue, nl->rqueue_size + 1))
1b89cf56
TG
187 return -ENOMEM;
188
189 return 0;
190}
191
409856d3
YW
192int netlink_rqueue_partial_make_room(sd_netlink *nl) {
193 assert(nl);
4e996881 194
409856d3 195 if (nl->rqueue_partial_size >= NETLINK_RQUEUE_MAX)
baaa35ad 196 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
409856d3
YW
197 "sd-netlink: exhausted the partial read queue size (%d)",
198 NETLINK_RQUEUE_MAX);
4e996881 199
409856d3 200 if (!GREEDY_REALLOC(nl->rqueue_partial, nl->rqueue_partial_size + 1))
4e996881
TG
201 return -ENOMEM;
202
203 return 0;
204}
205
409856d3 206static int dispatch_rqueue(sd_netlink *nl, sd_netlink_message **message) {
4555ec72 207 int r;
65f568bb 208
409856d3 209 assert(nl);
4555ec72
TG
210 assert(message);
211
409856d3 212 if (nl->rqueue_size <= 0) {
1b89cf56 213 /* Try to read a new message */
409856d3 214 r = socket_read_message(nl);
71994cff 215 if (r == -ENOBUFS) { /* FIXME: ignore buffer overruns for now */
409856d3 216 log_debug_errno(r, "sd-netlink: Got ENOBUFS from netlink socket, ignoring.");
71994cff
LP
217 return 1;
218 }
1b89cf56
TG
219 if (r <= 0)
220 return r;
4555ec72
TG
221 }
222
1b89cf56 223 /* Dispatch a queued message */
409856d3
YW
224 *message = nl->rqueue[0];
225 nl->rqueue_size--;
226 memmove(nl->rqueue, nl->rqueue + 1, sizeof(sd_netlink_message*) * nl->rqueue_size);
4555ec72
TG
227
228 return 1;
229}
230
409856d3 231static int process_timeout(sd_netlink *nl) {
4afd3348 232 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
e16bcf98 233 struct reply_callback *c;
ee38400b 234 sd_netlink_slot *slot;
e16bcf98
TG
235 usec_t n;
236 int r;
237
409856d3 238 assert(nl);
e16bcf98 239
409856d3 240 c = prioq_peek(nl->reply_callbacks_prioq);
e16bcf98
TG
241 if (!c)
242 return 0;
243
244 n = now(CLOCK_MONOTONIC);
245 if (c->timeout > n)
246 return 0;
247
409856d3 248 r = message_new_synthetic_error(nl, -ETIMEDOUT, c->serial, &m);
e16bcf98
TG
249 if (r < 0)
250 return r;
251
409856d3 252 assert_se(prioq_pop(nl->reply_callbacks_prioq) == c);
ee38400b 253 c->timeout = 0;
409856d3 254 hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(c->serial));
e16bcf98 255
ee38400b
YW
256 slot = container_of(c, sd_netlink_slot, reply_callback);
257
409856d3 258 r = c->callback(nl, m, slot->userdata);
233ba5c3 259 if (r < 0)
8190a388
YW
260 log_debug_errno(r, "sd-netlink: timedout callback %s%s%sfailed: %m",
261 slot->description ? "'" : "",
262 strempty(slot->description),
263 slot->description ? "' " : "");
233ba5c3 264
ee38400b
YW
265 if (slot->floating)
266 netlink_slot_disconnect(slot, true);
e16bcf98 267
233ba5c3 268 return 1;
e16bcf98
TG
269}
270
409856d3 271static int process_reply(sd_netlink *nl, sd_netlink_message *m) {
ee38400b
YW
272 struct reply_callback *c;
273 sd_netlink_slot *slot;
b522c4b9 274 uint32_t serial;
ea342a99 275 uint16_t type;
e16bcf98
TG
276 int r;
277
409856d3 278 assert(nl);
e16bcf98
TG
279 assert(m);
280
409856d3
YW
281 serial = message_get_serial(m);
282 c = hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(serial));
e16bcf98
TG
283 if (!c)
284 return 0;
285
ee38400b 286 if (c->timeout != 0) {
409856d3 287 prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx);
ee38400b
YW
288 c->timeout = 0;
289 }
e16bcf98 290
1c4baffc 291 r = sd_netlink_message_get_type(m, &type);
ea342a99 292 if (r < 0)
ee38400b 293 return r;
ea342a99
AR
294
295 if (type == NLMSG_DONE)
296 m = NULL;
297
ee38400b
YW
298 slot = container_of(c, sd_netlink_slot, reply_callback);
299
409856d3 300 r = c->callback(nl, m, slot->userdata);
233ba5c3 301 if (r < 0)
8190a388
YW
302 log_debug_errno(r, "sd-netlink: reply callback %s%s%sfailed: %m",
303 slot->description ? "'" : "",
304 strempty(slot->description),
305 slot->description ? "' " : "");
233ba5c3 306
ee38400b
YW
307 if (slot->floating)
308 netlink_slot_disconnect(slot, true);
545bab1f 309
233ba5c3 310 return 1;
e16bcf98
TG
311}
312
409856d3 313static int process_match(sd_netlink *nl, sd_netlink_message *m) {
8cec01b9 314 uint16_t type;
e1578f60 315 uint8_t cmd;
8cec01b9
TG
316 int r;
317
409856d3 318 assert(nl);
8cec01b9
TG
319 assert(m);
320
1c4baffc 321 r = sd_netlink_message_get_type(m, &type);
8cec01b9
TG
322 if (r < 0)
323 return r;
324
e1578f60
YW
325 if (m->protocol == NETLINK_GENERIC) {
326 r = sd_genl_message_get_command(nl, m, &cmd);
327 if (r < 0)
328 return r;
329 } else
330 cmd = 0;
331
409856d3 332 LIST_FOREACH(match_callbacks, c, nl->match_callbacks) {
e1578f60 333 sd_netlink_slot *slot;
4d4d898a 334 bool found = false;
e1578f60
YW
335
336 if (c->type != type)
337 continue;
338 if (c->cmd != 0 && c->cmd != cmd)
baf78f1a
LP
339 continue;
340
4d4d898a
YW
341 for (size_t i = 0; i < c->n_groups; i++)
342 if (c->groups[i] == m->multicast_group) {
343 found = true;
344 break;
345 }
346
347 if (!found)
348 continue;
349
baf78f1a
LP
350 slot = container_of(c, sd_netlink_slot, match_callback);
351
409856d3 352 r = c->callback(nl, m, slot->userdata);
baf78f1a
LP
353 if (r < 0)
354 log_debug_errno(r, "sd-netlink: match callback %s%s%sfailed: %m",
355 slot->description ? "'" : "",
356 strempty(slot->description),
357 slot->description ? "' " : "");
358 if (r != 0)
359 break;
8cec01b9
TG
360 }
361
233ba5c3 362 return 1;
8cec01b9
TG
363}
364
409856d3 365static int process_running(sd_netlink *nl, sd_netlink_message **ret) {
4afd3348 366 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
4555ec72
TG
367 int r;
368
409856d3 369 assert(nl);
9d0db178 370
409856d3 371 r = process_timeout(nl);
e16bcf98
TG
372 if (r != 0)
373 goto null_message;
374
409856d3 375 r = dispatch_rqueue(nl, &m);
4555ec72
TG
376 if (r < 0)
377 return r;
378 if (!m)
379 goto null_message;
380
52888279 381 if (sd_netlink_message_is_broadcast(m))
409856d3 382 r = process_match(nl, m);
52888279 383 else
409856d3 384 r = process_reply(nl, m);
52888279
YW
385 if (r != 0)
386 goto null_message;
8cec01b9 387
4555ec72 388 if (ret) {
1cc6c93a 389 *ret = TAKE_PTR(m);
4555ec72
TG
390
391 return 1;
392 }
393
394 return 1;
395
396null_message:
397 if (r >= 0 && ret)
398 *ret = NULL;
399
400 return r;
401}
e16bcf98 402
409856d3
YW
403int sd_netlink_process(sd_netlink *nl, sd_netlink_message **ret) {
404 NETLINK_DONT_DESTROY(nl);
4555ec72
TG
405 int r;
406
409856d3
YW
407 assert_return(nl, -EINVAL);
408 assert_return(!netlink_pid_changed(nl), -ECHILD);
409 assert_return(!nl->processing, -EBUSY);
4555ec72 410
409856d3
YW
411 nl->processing = true;
412 r = process_running(nl, ret);
413 nl->processing = false;
4555ec72
TG
414
415 return r;
416}
417
418static usec_t calc_elapse(uint64_t usec) {
f5fbe71d 419 if (usec == UINT64_MAX)
4555ec72
TG
420 return 0;
421
422 if (usec == 0)
409856d3 423 usec = NETLINK_DEFAULT_TIMEOUT_USEC;
4555ec72 424
496db330 425 return usec_add(now(CLOCK_MONOTONIC), usec);
4555ec72
TG
426}
427
409856d3 428static int netlink_poll(sd_netlink *nl, bool need_more, usec_t timeout_usec) {
3a43da28 429 usec_t m = USEC_INFINITY;
b4f2a5b1
TG
430 int r, e;
431
409856d3 432 assert(nl);
4555ec72 433
409856d3 434 e = sd_netlink_get_events(nl);
b4f2a5b1
TG
435 if (e < 0)
436 return e;
4555ec72 437
b4f2a5b1
TG
438 if (need_more)
439 /* Caller wants more data, and doesn't care about
440 * what's been read or any other timeouts. */
f55dc7c9 441 e |= POLLIN;
b4f2a5b1
TG
442 else {
443 usec_t until;
11537375 444
b4f2a5b1
TG
445 /* Caller wants to process if there is something to
446 * process, but doesn't care otherwise */
447
409856d3 448 r = sd_netlink_get_timeout(nl, &until);
b4f2a5b1
TG
449 if (r < 0)
450 return r;
65f568bb 451
11537375
YW
452 m = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
453 }
b4f2a5b1 454
409856d3 455 r = fd_wait_for_event(nl->fd, e, MIN(m, timeout_usec));
8d16f29b 456 if (r <= 0)
0f2d351f 457 return r;
4555ec72 458
dad28bff 459 return 1;
4555ec72
TG
460}
461
1c4baffc 462int sd_netlink_wait(sd_netlink *nl, uint64_t timeout_usec) {
69858785
LP
463 int r;
464
4555ec72 465 assert_return(nl, -EINVAL);
409856d3 466 assert_return(!netlink_pid_changed(nl), -ECHILD);
4555ec72
TG
467
468 if (nl->rqueue_size > 0)
469 return 0;
470
69858785
LP
471 r = netlink_poll(nl, false, timeout_usec);
472 if (r < 0 && ERRNO_IS_TRANSIENT(r)) /* Convert EINTR to "something happened" and give user a chance to run some code before calling back into us */
473 return 1;
474 return r;
4555ec72
TG
475}
476
e16bcf98
TG
477static int timeout_compare(const void *a, const void *b) {
478 const struct reply_callback *x = a, *y = b;
479
480 if (x->timeout != 0 && y->timeout == 0)
481 return -1;
482
483 if (x->timeout == 0 && y->timeout != 0)
484 return 1;
485
9c57a73b 486 return CMP(x->timeout, y->timeout);
e16bcf98
TG
487}
488
dd35a61c 489int sd_netlink_call_async(
545bab1f 490 sd_netlink *nl,
ee38400b 491 sd_netlink_slot **ret_slot,
545bab1f
YW
492 sd_netlink_message *m,
493 sd_netlink_message_handler_t callback,
494 sd_netlink_destroy_t destroy_callback,
495 void *userdata,
8190a388
YW
496 uint64_t usec,
497 const char *description) {
2b012288 498
ee38400b 499 _cleanup_free_ sd_netlink_slot *slot = NULL;
e16bcf98
TG
500 int r, k;
501
502 assert_return(nl, -EINVAL);
503 assert_return(m, -EINVAL);
504 assert_return(callback, -EINVAL);
409856d3 505 assert_return(!netlink_pid_changed(nl), -ECHILD);
e16bcf98 506
b522c4b9
LP
507 if (hashmap_size(nl->reply_callbacks) >= REPLY_CALLBACKS_MAX)
508 return -ERANGE;
509
510 r = hashmap_ensure_allocated(&nl->reply_callbacks, &trivial_hash_ops);
e16bcf98
TG
511 if (r < 0)
512 return r;
513
f5fbe71d 514 if (usec != UINT64_MAX) {
e16bcf98
TG
515 r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare);
516 if (r < 0)
517 return r;
518 }
519
5cd67116 520 r = netlink_slot_allocate(nl, !ret_slot, NETLINK_REPLY_CALLBACK, sizeof(struct reply_callback), userdata, description, &slot);
8190a388
YW
521 if (r < 0)
522 return r;
e16bcf98 523
ee38400b
YW
524 slot->reply_callback.callback = callback;
525 slot->reply_callback.timeout = calc_elapse(usec);
e16bcf98 526
b522c4b9 527 k = sd_netlink_send(nl, m, &slot->reply_callback.serial);
545bab1f 528 if (k < 0)
e16bcf98 529 return k;
e16bcf98 530
b522c4b9 531 r = hashmap_put(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial), &slot->reply_callback);
545bab1f 532 if (r < 0)
e16bcf98 533 return r;
e16bcf98 534
ee38400b
YW
535 if (slot->reply_callback.timeout != 0) {
536 r = prioq_put(nl->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);
f6bdbd9e 537 if (r < 0) {
b522c4b9 538 (void) hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial));
e16bcf98
TG
539 return r;
540 }
541 }
542
40c864af 543 /* Set this at last. Otherwise, some failures in above would call destroy_callback but some would not. */
5cd67116
YW
544 slot->destroy_callback = destroy_callback;
545
ee38400b
YW
546 if (ret_slot)
547 *ret_slot = slot;
e16bcf98 548
ee38400b 549 TAKE_PTR(slot);
545bab1f 550
e16bcf98
TG
551 return k;
552}
553
dd35a61c 554int sd_netlink_read(
409856d3 555 sd_netlink *nl,
2b012288
YW
556 uint32_t serial,
557 uint64_t usec,
558 sd_netlink_message **ret) {
559
4555ec72 560 usec_t timeout;
4555ec72
TG
561 int r;
562
409856d3
YW
563 assert_return(nl, -EINVAL);
564 assert_return(!netlink_pid_changed(nl), -ECHILD);
4555ec72
TG
565
566 timeout = calc_elapse(usec);
567
65f568bb 568 for (;;) {
4555ec72 569 usec_t left;
65f568bb 570
409856d3 571 for (unsigned i = 0; i < nl->rqueue_size; i++) {
2b012288 572 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *incoming = NULL;
1b89cf56 573 uint32_t received_serial;
2b012288 574 uint16_t type;
65f568bb 575
409856d3 576 received_serial = message_get_serial(nl->rqueue[i]);
2b012288
YW
577 if (received_serial != serial)
578 continue;
65f568bb 579
409856d3 580 incoming = nl->rqueue[i];
ea342a99 581
2b012288 582 /* found a match, remove from rqueue and return it */
409856d3
YW
583 memmove(nl->rqueue + i, nl->rqueue + i + 1,
584 sizeof(sd_netlink_message*) * (nl->rqueue_size - i - 1));
585 nl->rqueue_size--;
ea342a99 586
2b012288
YW
587 r = sd_netlink_message_get_errno(incoming);
588 if (r < 0)
589 return r;
1b89cf56 590
2b012288
YW
591 r = sd_netlink_message_get_type(incoming, &type);
592 if (r < 0)
593 return r;
ea342a99 594
2b012288
YW
595 if (type == NLMSG_DONE) {
596 *ret = NULL;
597 return 0;
65f568bb 598 }
2b012288
YW
599
600 if (ret)
601 *ret = TAKE_PTR(incoming);
602 return 1;
65f568bb 603 }
1b89cf56 604
409856d3 605 r = socket_read_message(nl);
1b89cf56
TG
606 if (r < 0)
607 return r;
608 if (r > 0)
6ff8806e 609 /* received message, so try to process straight away */
4555ec72 610 continue;
65f568bb 611
4555ec72
TG
612 if (timeout > 0) {
613 usec_t n;
614
615 n = now(CLOCK_MONOTONIC);
616 if (n >= timeout)
617 return -ETIMEDOUT;
618
11537375 619 left = usec_sub_unsigned(timeout, n);
4555ec72 620 } else
11537375 621 left = USEC_INFINITY;
4555ec72 622
409856d3 623 r = netlink_poll(nl, true, left);
4555ec72
TG
624 if (r < 0)
625 return r;
2b012288 626 if (r == 0)
b551ddd3 627 return -ETIMEDOUT;
b4f2a5b1
TG
628 }
629}
630
dd35a61c 631int sd_netlink_call(
409856d3 632 sd_netlink *nl,
4df42cd9
FW
633 sd_netlink_message *message,
634 uint64_t usec,
635 sd_netlink_message **ret) {
2b012288 636
4df42cd9
FW
637 uint32_t serial;
638 int r;
639
409856d3
YW
640 assert_return(nl, -EINVAL);
641 assert_return(!netlink_pid_changed(nl), -ECHILD);
4df42cd9
FW
642 assert_return(message, -EINVAL);
643
409856d3 644 r = sd_netlink_send(nl, message, &serial);
4df42cd9
FW
645 if (r < 0)
646 return r;
647
409856d3 648 return sd_netlink_read(nl, serial, usec, ret);
4df42cd9
FW
649}
650
dd35a61c 651int sd_netlink_get_events(sd_netlink *nl) {
409856d3
YW
652 assert_return(nl, -EINVAL);
653 assert_return(!netlink_pid_changed(nl), -ECHILD);
b4f2a5b1 654
409856d3 655 return nl->rqueue_size == 0 ? POLLIN : 0;
b4f2a5b1
TG
656}
657
dd35a61c 658int sd_netlink_get_timeout(sd_netlink *nl, uint64_t *timeout_usec) {
b4f2a5b1
TG
659 struct reply_callback *c;
660
409856d3 661 assert_return(nl, -EINVAL);
b4f2a5b1 662 assert_return(timeout_usec, -EINVAL);
409856d3 663 assert_return(!netlink_pid_changed(nl), -ECHILD);
b4f2a5b1 664
409856d3 665 if (nl->rqueue_size > 0) {
b4f2a5b1
TG
666 *timeout_usec = 0;
667 return 1;
668 }
669
409856d3 670 c = prioq_peek(nl->reply_callbacks_prioq);
b4f2a5b1 671 if (!c) {
f5fbe71d 672 *timeout_usec = UINT64_MAX;
b4f2a5b1
TG
673 return 0;
674 }
675
676 *timeout_usec = c->timeout;
677
678 return 1;
679}
680
681static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
99534007 682 sd_netlink *nl = ASSERT_PTR(userdata);
b4f2a5b1
TG
683 int r;
684
409856d3 685 r = sd_netlink_process(nl, NULL);
b4f2a5b1
TG
686 if (r < 0)
687 return r;
688
689 return 1;
690}
691
692static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
99534007 693 sd_netlink *nl = ASSERT_PTR(userdata);
b4f2a5b1
TG
694 int r;
695
409856d3 696 r = sd_netlink_process(nl, NULL);
b4f2a5b1
TG
697 if (r < 0)
698 return r;
699
700 return 1;
701}
702
703static int prepare_callback(sd_event_source *s, void *userdata) {
99534007 704 sd_netlink *nl = ASSERT_PTR(userdata);
40c864af 705 int r, enabled;
b4f2a5b1
TG
706 usec_t until;
707
708 assert(s);
b4f2a5b1 709
40c864af 710 r = sd_netlink_get_events(nl);
b4f2a5b1
TG
711 if (r < 0)
712 return r;
713
40c864af 714 r = sd_event_source_set_io_events(nl->io_event_source, r);
b4f2a5b1
TG
715 if (r < 0)
716 return r;
b4f2a5b1 717
40c864af
ZJS
718 enabled = sd_netlink_get_timeout(nl, &until);
719 if (enabled < 0)
720 return enabled;
721 if (enabled > 0) {
722 r = sd_event_source_set_time(nl->time_event_source, until);
723 if (r < 0)
724 return r;
b4f2a5b1
TG
725 }
726
93c0a5ec
ZJS
727 r = sd_event_source_set_enabled(nl->time_event_source,
728 enabled > 0 ? SD_EVENT_ONESHOT : SD_EVENT_OFF);
b4f2a5b1
TG
729 if (r < 0)
730 return r;
731
732 return 1;
733}
734
dd35a61c 735int sd_netlink_attach_event(sd_netlink *nl, sd_event *event, int64_t priority) {
b4f2a5b1
TG
736 int r;
737
409856d3
YW
738 assert_return(nl, -EINVAL);
739 assert_return(!nl->event, -EBUSY);
b4f2a5b1 740
409856d3
YW
741 assert(!nl->io_event_source);
742 assert(!nl->time_event_source);
b4f2a5b1
TG
743
744 if (event)
409856d3 745 nl->event = sd_event_ref(event);
b4f2a5b1 746 else {
409856d3 747 r = sd_event_default(&nl->event);
b4f2a5b1
TG
748 if (r < 0)
749 return r;
750 }
751
409856d3 752 r = sd_event_add_io(nl->event, &nl->io_event_source, nl->fd, 0, io_callback, nl);
b4f2a5b1
TG
753 if (r < 0)
754 goto fail;
755
409856d3 756 r = sd_event_source_set_priority(nl->io_event_source, priority);
b4f2a5b1
TG
757 if (r < 0)
758 goto fail;
759
409856d3 760 r = sd_event_source_set_description(nl->io_event_source, "netlink-receive-message");
9021bb9f
TG
761 if (r < 0)
762 goto fail;
763
409856d3 764 r = sd_event_source_set_prepare(nl->io_event_source, prepare_callback);
b4f2a5b1
TG
765 if (r < 0)
766 goto fail;
767
409856d3 768 r = sd_event_add_time(nl->event, &nl->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, nl);
b4f2a5b1
TG
769 if (r < 0)
770 goto fail;
771
409856d3 772 r = sd_event_source_set_priority(nl->time_event_source, priority);
b4f2a5b1
TG
773 if (r < 0)
774 goto fail;
775
409856d3 776 r = sd_event_source_set_description(nl->time_event_source, "netlink-timer");
9021bb9f
TG
777 if (r < 0)
778 goto fail;
779
b4f2a5b1
TG
780 return 0;
781
782fail:
409856d3 783 sd_netlink_detach_event(nl);
b4f2a5b1
TG
784 return r;
785}
786
dd35a61c 787int sd_netlink_detach_event(sd_netlink *nl) {
409856d3
YW
788 assert_return(nl, -EINVAL);
789 assert_return(nl->event, -ENXIO);
b4f2a5b1 790
409856d3 791 nl->io_event_source = sd_event_source_unref(nl->io_event_source);
b4f2a5b1 792
409856d3 793 nl->time_event_source = sd_event_source_unref(nl->time_event_source);
b4f2a5b1 794
409856d3 795 nl->event = sd_event_unref(nl->event);
b4f2a5b1
TG
796
797 return 0;
798}
8cec01b9 799
3f60e448
YW
800int netlink_add_match_internal(
801 sd_netlink *nl,
ee38400b 802 sd_netlink_slot **ret_slot,
3f60e448
YW
803 const uint32_t *groups,
804 size_t n_groups,
ee38400b 805 uint16_t type,
e1578f60 806 uint8_t cmd,
ee38400b
YW
807 sd_netlink_message_handler_t callback,
808 sd_netlink_destroy_t destroy_callback,
8190a388
YW
809 void *userdata,
810 const char *description) {
409856d3 811
ee38400b 812 _cleanup_free_ sd_netlink_slot *slot = NULL;
31710be5 813 int r;
8cec01b9 814
3f60e448
YW
815 assert(groups);
816 assert(n_groups > 0);
817
818 for (size_t i = 0; i < n_groups; i++) {
819 r = socket_broadcast_group_ref(nl, groups[i]);
820 if (r < 0)
821 return r;
822 }
8cec01b9 823
3f60e448
YW
824 r = netlink_slot_allocate(nl, !ret_slot, NETLINK_MATCH_CALLBACK, sizeof(struct match_callback),
825 userdata, description, &slot);
8190a388
YW
826 if (r < 0)
827 return r;
8cec01b9 828
3f60e448
YW
829 slot->match_callback.groups = newdup(uint32_t, groups, n_groups);
830 if (!slot->match_callback.groups)
831 return -ENOMEM;
832
833 slot->match_callback.n_groups = n_groups;
ee38400b
YW
834 slot->match_callback.callback = callback;
835 slot->match_callback.type = type;
e1578f60 836 slot->match_callback.cmd = cmd;
8cec01b9 837
3f60e448
YW
838 LIST_PREPEND(match_callbacks, nl->match_callbacks, &slot->match_callback);
839
840 /* Set this at last. Otherwise, some failures in above call the destroy callback but some do not. */
841 slot->destroy_callback = destroy_callback;
842
843 if (ret_slot)
844 *ret_slot = slot;
845
846 TAKE_PTR(slot);
847 return 0;
848}
849
dd35a61c 850int sd_netlink_add_match(
3f60e448
YW
851 sd_netlink *rtnl,
852 sd_netlink_slot **ret_slot,
853 uint16_t type,
854 sd_netlink_message_handler_t callback,
855 sd_netlink_destroy_t destroy_callback,
856 void *userdata,
857 const char *description) {
858
859 static const uint32_t
860 address_groups[] = { RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR, },
861 link_groups[] = { RTNLGRP_LINK, },
862 neighbor_groups[] = { RTNLGRP_NEIGH, },
863 nexthop_groups[] = { RTNLGRP_NEXTHOP, },
864 route_groups[] = { RTNLGRP_IPV4_ROUTE, RTNLGRP_IPV6_ROUTE, },
27e93a4b
YW
865 rule_groups[] = { RTNLGRP_IPV4_RULE, RTNLGRP_IPV6_RULE, },
866 tc_groups[] = { RTNLGRP_TC };
3f60e448
YW
867 const uint32_t *groups;
868 size_t n_groups;
869
870 assert_return(rtnl, -EINVAL);
871 assert_return(callback, -EINVAL);
872 assert_return(!netlink_pid_changed(rtnl), -ECHILD);
873
31710be5
TG
874 switch (type) {
875 case RTM_NEWLINK:
31710be5 876 case RTM_DELLINK:
3f60e448
YW
877 groups = link_groups;
878 n_groups = ELEMENTSOF(link_groups);
31710be5
TG
879 break;
880 case RTM_NEWADDR:
31710be5 881 case RTM_DELADDR:
3f60e448
YW
882 groups = address_groups;
883 n_groups = ELEMENTSOF(address_groups);
d1bdafd2
WKI
884 break;
885 case RTM_NEWNEIGH:
886 case RTM_DELNEIGH:
3f60e448
YW
887 groups = neighbor_groups;
888 n_groups = ELEMENTSOF(neighbor_groups);
31710be5 889 break;
87e4c847
TG
890 case RTM_NEWROUTE:
891 case RTM_DELROUTE:
3f60e448
YW
892 groups = route_groups;
893 n_groups = ELEMENTSOF(route_groups);
87e4c847 894 break;
bce67bbe
SS
895 case RTM_NEWRULE:
896 case RTM_DELRULE:
3f60e448
YW
897 groups = rule_groups;
898 n_groups = ELEMENTSOF(rule_groups);
bce67bbe 899 break;
c16c7808
SS
900 case RTM_NEWNEXTHOP:
901 case RTM_DELNEXTHOP:
3f60e448
YW
902 groups = nexthop_groups;
903 n_groups = ELEMENTSOF(nexthop_groups);
904 break;
27e93a4b
YW
905 case RTM_NEWQDISC:
906 case RTM_DELQDISC:
907 case RTM_NEWTCLASS:
908 case RTM_DELTCLASS:
909 groups = tc_groups;
910 n_groups = ELEMENTSOF(tc_groups);
911 break;
31710be5
TG
912 default:
913 return -EOPNOTSUPP;
914 }
915
e1578f60 916 return netlink_add_match_internal(rtnl, ret_slot, groups, n_groups, type, 0, callback,
3f60e448 917 destroy_callback, userdata, description);
8cec01b9 918}
dc317a9a 919
dd35a61c 920int sd_netlink_attach_filter(sd_netlink *nl, size_t len, const struct sock_filter *filter) {
dc317a9a
YW
921 assert_return(nl, -EINVAL);
922 assert_return(len == 0 || filter, -EINVAL);
923
924 if (setsockopt(nl->fd, SOL_SOCKET,
925 len == 0 ? SO_DETACH_FILTER : SO_ATTACH_FILTER,
926 &(struct sock_fprog) {
927 .len = len,
f4f81a6b 928 .filter = (struct sock_filter*) filter,
dc317a9a
YW
929 }, sizeof(struct sock_fprog)) < 0)
930 return -errno;
931
932 return 0;
933}