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