]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-netlink/sd-netlink.c
sd-netlink: add call to query sd_event object an sd_netlink object is attached to
[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,
254d1313 34 .fd = -EBADF,
2fea6090
YW
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
e503019b 48 * likely (though still theoretically possible). We use a 32 bit μs counter starting at boot
ac3bc1b8
LP
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
e503019b 54 * sequence number by 1. This means we could enqueue 1 netlink message per μs without risking
ac3bc1b8
LP
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) {
254d1313 96 nl->fd = -EBADF; /* on failure, the caller remains owner of the fd, hence don't close it here */
409856d3 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;
8c578303 119
409856d3 120 assert(nl);
4555ec72 121
e417c4ac 122 ordered_set_free(nl->rqueue);
7b34bae3 123 hashmap_free(nl->rqueue_by_serial);
e417c4ac 124 hashmap_free(nl->rqueue_partial_by_serial);
409856d3 125 free(nl->rbuffer);
a88f77c4 126
409856d3 127 while ((s = nl->slots)) {
ee38400b
YW
128 assert(s->floating);
129 netlink_slot_disconnect(s, true);
545bab1f 130 }
409856d3
YW
131 hashmap_free(nl->reply_callbacks);
132 prioq_free(nl->reply_callbacks_prioq);
22fdeadc 133
409856d3
YW
134 sd_event_source_unref(nl->io_event_source);
135 sd_event_source_unref(nl->time_event_source);
136 sd_event_unref(nl->event);
22fdeadc 137
409856d3 138 hashmap_free(nl->broadcast_group_refs);
9c5a882b 139
56fdc16d 140 genl_clear_family(nl);
4e8f0ef9 141
409856d3
YW
142 safe_close(nl->fd);
143 return mfree(nl);
65f568bb
TG
144}
145
f23ab4dc 146DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_netlink, sd_netlink, netlink_free);
1c71f7f3 147
dd35a61c 148int sd_netlink_send(
409856d3
YW
149 sd_netlink *nl,
150 sd_netlink_message *message,
151 uint32_t *serial) {
152
4555ec72 153 int r;
65f568bb
TG
154
155 assert_return(nl, -EINVAL);
409856d3 156 assert_return(!netlink_pid_changed(nl), -ECHILD);
65f568bb 157 assert_return(message, -EINVAL);
3dd215e0 158 assert_return(!message->sealed, -EPERM);
65f568bb 159
409856d3 160 netlink_seal_message(nl, message);
65f568bb 161
bbe181b4
TG
162 r = socket_write_message(nl, message);
163 if (r < 0)
164 return r;
65f568bb 165
4555ec72 166 if (serial)
409856d3 167 *serial = message_get_serial(message);
65f568bb 168
4555ec72
TG
169 return 1;
170}
65f568bb 171
e417c4ac
YW
172static int dispatch_rqueue(sd_netlink *nl, sd_netlink_message **ret) {
173 sd_netlink_message *m;
4555ec72 174 int r;
65f568bb 175
409856d3 176 assert(nl);
e417c4ac 177 assert(ret);
4555ec72 178
e417c4ac 179 if (ordered_set_size(nl->rqueue) <= 0) {
1b89cf56 180 /* Try to read a new message */
409856d3 181 r = socket_read_message(nl);
e417c4ac 182 if (r == -ENOBUFS) /* FIXME: ignore buffer overruns for now */
409856d3 183 log_debug_errno(r, "sd-netlink: Got ENOBUFS from netlink socket, ignoring.");
e417c4ac 184 else if (r < 0)
1b89cf56 185 return r;
4555ec72
TG
186 }
187
1b89cf56 188 /* Dispatch a queued message */
e417c4ac 189 m = ordered_set_steal_first(nl->rqueue);
6b15f2ef
YW
190 if (m)
191 sd_netlink_message_unref(hashmap_remove_value(nl->rqueue_by_serial, UINT32_TO_PTR(message_get_serial(m)), m));
e417c4ac
YW
192 *ret = m;
193 return !!m;
4555ec72
TG
194}
195
409856d3 196static int process_timeout(sd_netlink *nl) {
4afd3348 197 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
e16bcf98 198 struct reply_callback *c;
ee38400b 199 sd_netlink_slot *slot;
e16bcf98
TG
200 usec_t n;
201 int r;
202
409856d3 203 assert(nl);
e16bcf98 204
409856d3 205 c = prioq_peek(nl->reply_callbacks_prioq);
e16bcf98
TG
206 if (!c)
207 return 0;
208
209 n = now(CLOCK_MONOTONIC);
210 if (c->timeout > n)
211 return 0;
212
409856d3 213 r = message_new_synthetic_error(nl, -ETIMEDOUT, c->serial, &m);
e16bcf98
TG
214 if (r < 0)
215 return r;
216
409856d3 217 assert_se(prioq_pop(nl->reply_callbacks_prioq) == c);
409856d3 218 hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(c->serial));
e16bcf98 219
ee38400b
YW
220 slot = container_of(c, sd_netlink_slot, reply_callback);
221
409856d3 222 r = c->callback(nl, m, slot->userdata);
233ba5c3 223 if (r < 0)
8190a388
YW
224 log_debug_errno(r, "sd-netlink: timedout callback %s%s%sfailed: %m",
225 slot->description ? "'" : "",
226 strempty(slot->description),
227 slot->description ? "' " : "");
233ba5c3 228
ee38400b
YW
229 if (slot->floating)
230 netlink_slot_disconnect(slot, true);
e16bcf98 231
233ba5c3 232 return 1;
e16bcf98
TG
233}
234
409856d3 235static int process_reply(sd_netlink *nl, sd_netlink_message *m) {
ee38400b
YW
236 struct reply_callback *c;
237 sd_netlink_slot *slot;
b522c4b9 238 uint32_t serial;
ea342a99 239 uint16_t type;
e16bcf98
TG
240 int r;
241
409856d3 242 assert(nl);
e16bcf98
TG
243 assert(m);
244
409856d3
YW
245 serial = message_get_serial(m);
246 c = hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(serial));
e16bcf98
TG
247 if (!c)
248 return 0;
249
f5c61588 250 if (c->timeout != USEC_INFINITY)
409856d3 251 prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx);
e16bcf98 252
1c4baffc 253 r = sd_netlink_message_get_type(m, &type);
ea342a99 254 if (r < 0)
ee38400b 255 return r;
ea342a99
AR
256
257 if (type == NLMSG_DONE)
258 m = NULL;
259
ee38400b
YW
260 slot = container_of(c, sd_netlink_slot, reply_callback);
261
409856d3 262 r = c->callback(nl, m, slot->userdata);
233ba5c3 263 if (r < 0)
8190a388
YW
264 log_debug_errno(r, "sd-netlink: reply callback %s%s%sfailed: %m",
265 slot->description ? "'" : "",
266 strempty(slot->description),
267 slot->description ? "' " : "");
233ba5c3 268
ee38400b
YW
269 if (slot->floating)
270 netlink_slot_disconnect(slot, true);
545bab1f 271
233ba5c3 272 return 1;
e16bcf98
TG
273}
274
409856d3 275static int process_match(sd_netlink *nl, sd_netlink_message *m) {
8cec01b9 276 uint16_t type;
e1578f60 277 uint8_t cmd;
8cec01b9
TG
278 int r;
279
409856d3 280 assert(nl);
8cec01b9
TG
281 assert(m);
282
1c4baffc 283 r = sd_netlink_message_get_type(m, &type);
8cec01b9
TG
284 if (r < 0)
285 return r;
286
e1578f60
YW
287 if (m->protocol == NETLINK_GENERIC) {
288 r = sd_genl_message_get_command(nl, m, &cmd);
289 if (r < 0)
290 return r;
291 } else
292 cmd = 0;
293
409856d3 294 LIST_FOREACH(match_callbacks, c, nl->match_callbacks) {
e1578f60 295 sd_netlink_slot *slot;
4d4d898a 296 bool found = false;
e1578f60
YW
297
298 if (c->type != type)
299 continue;
300 if (c->cmd != 0 && c->cmd != cmd)
baf78f1a
LP
301 continue;
302
4d4d898a
YW
303 for (size_t i = 0; i < c->n_groups; i++)
304 if (c->groups[i] == m->multicast_group) {
305 found = true;
306 break;
307 }
308
309 if (!found)
310 continue;
311
baf78f1a
LP
312 slot = container_of(c, sd_netlink_slot, match_callback);
313
409856d3 314 r = c->callback(nl, m, slot->userdata);
baf78f1a
LP
315 if (r < 0)
316 log_debug_errno(r, "sd-netlink: match callback %s%s%sfailed: %m",
317 slot->description ? "'" : "",
318 strempty(slot->description),
319 slot->description ? "' " : "");
320 if (r != 0)
321 break;
8cec01b9
TG
322 }
323
233ba5c3 324 return 1;
8cec01b9
TG
325}
326
409856d3 327static int process_running(sd_netlink *nl, sd_netlink_message **ret) {
4afd3348 328 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
4555ec72
TG
329 int r;
330
409856d3 331 assert(nl);
9d0db178 332
409856d3 333 r = process_timeout(nl);
e16bcf98
TG
334 if (r != 0)
335 goto null_message;
336
409856d3 337 r = dispatch_rqueue(nl, &m);
4555ec72
TG
338 if (r < 0)
339 return r;
340 if (!m)
341 goto null_message;
342
52888279 343 if (sd_netlink_message_is_broadcast(m))
409856d3 344 r = process_match(nl, m);
52888279 345 else
409856d3 346 r = process_reply(nl, m);
52888279
YW
347 if (r != 0)
348 goto null_message;
8cec01b9 349
4555ec72 350 if (ret) {
1cc6c93a 351 *ret = TAKE_PTR(m);
4555ec72
TG
352
353 return 1;
354 }
355
356 return 1;
357
358null_message:
359 if (r >= 0 && ret)
360 *ret = NULL;
361
362 return r;
363}
e16bcf98 364
409856d3
YW
365int sd_netlink_process(sd_netlink *nl, sd_netlink_message **ret) {
366 NETLINK_DONT_DESTROY(nl);
4555ec72
TG
367 int r;
368
409856d3
YW
369 assert_return(nl, -EINVAL);
370 assert_return(!netlink_pid_changed(nl), -ECHILD);
371 assert_return(!nl->processing, -EBUSY);
4555ec72 372
409856d3
YW
373 nl->processing = true;
374 r = process_running(nl, ret);
375 nl->processing = false;
4555ec72
TG
376
377 return r;
378}
379
f5c61588 380static usec_t timespan_to_timestamp(usec_t usec) {
52afaee7
YW
381 static bool default_timeout_set = false;
382 static usec_t default_timeout;
383 int r;
384
385 if (usec == 0) {
386 if (!default_timeout_set) {
387 const char *e;
388
389 default_timeout_set = true;
390 default_timeout = NETLINK_DEFAULT_TIMEOUT_USEC;
391
0558c86c 392 e = secure_getenv("SYSTEMD_NETLINK_DEFAULT_TIMEOUT");
52afaee7
YW
393 if (e) {
394 r = parse_sec(e, &default_timeout);
395 if (r < 0)
396 log_debug_errno(r, "sd-netlink: Failed to parse $SYSTEMD_NETLINK_DEFAULT_TIMEOUT environment variable, ignoring: %m");
397 }
398 }
399
400 usec = default_timeout;
401 }
4555ec72 402
496db330 403 return usec_add(now(CLOCK_MONOTONIC), usec);
4555ec72
TG
404}
405
409856d3 406static int netlink_poll(sd_netlink *nl, bool need_more, usec_t timeout_usec) {
3a43da28 407 usec_t m = USEC_INFINITY;
b4f2a5b1
TG
408 int r, e;
409
409856d3 410 assert(nl);
4555ec72 411
409856d3 412 e = sd_netlink_get_events(nl);
b4f2a5b1
TG
413 if (e < 0)
414 return e;
4555ec72 415
b4f2a5b1
TG
416 if (need_more)
417 /* Caller wants more data, and doesn't care about
418 * what's been read or any other timeouts. */
f55dc7c9 419 e |= POLLIN;
b4f2a5b1
TG
420 else {
421 usec_t until;
11537375 422
b4f2a5b1
TG
423 /* Caller wants to process if there is something to
424 * process, but doesn't care otherwise */
425
409856d3 426 r = sd_netlink_get_timeout(nl, &until);
b4f2a5b1
TG
427 if (r < 0)
428 return r;
65f568bb 429
11537375
YW
430 m = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
431 }
b4f2a5b1 432
409856d3 433 r = fd_wait_for_event(nl->fd, e, MIN(m, timeout_usec));
8d16f29b 434 if (r <= 0)
0f2d351f 435 return r;
4555ec72 436
dad28bff 437 return 1;
4555ec72
TG
438}
439
1c4baffc 440int sd_netlink_wait(sd_netlink *nl, uint64_t timeout_usec) {
69858785
LP
441 int r;
442
4555ec72 443 assert_return(nl, -EINVAL);
409856d3 444 assert_return(!netlink_pid_changed(nl), -ECHILD);
4555ec72 445
e417c4ac 446 if (ordered_set_size(nl->rqueue) > 0)
4555ec72
TG
447 return 0;
448
69858785 449 r = netlink_poll(nl, false, timeout_usec);
13d84288 450 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
451 return 1;
452 return r;
4555ec72
TG
453}
454
e16bcf98
TG
455static int timeout_compare(const void *a, const void *b) {
456 const struct reply_callback *x = a, *y = b;
457
9c57a73b 458 return CMP(x->timeout, y->timeout);
e16bcf98
TG
459}
460
dd35a61c 461int sd_netlink_call_async(
545bab1f 462 sd_netlink *nl,
ee38400b 463 sd_netlink_slot **ret_slot,
545bab1f
YW
464 sd_netlink_message *m,
465 sd_netlink_message_handler_t callback,
466 sd_netlink_destroy_t destroy_callback,
467 void *userdata,
8190a388
YW
468 uint64_t usec,
469 const char *description) {
2b012288 470
ee38400b 471 _cleanup_free_ sd_netlink_slot *slot = NULL;
e16bcf98
TG
472 int r, k;
473
474 assert_return(nl, -EINVAL);
475 assert_return(m, -EINVAL);
476 assert_return(callback, -EINVAL);
409856d3 477 assert_return(!netlink_pid_changed(nl), -ECHILD);
e16bcf98 478
b522c4b9
LP
479 if (hashmap_size(nl->reply_callbacks) >= REPLY_CALLBACKS_MAX)
480 return -ERANGE;
481
482 r = hashmap_ensure_allocated(&nl->reply_callbacks, &trivial_hash_ops);
e16bcf98
TG
483 if (r < 0)
484 return r;
485
f5fbe71d 486 if (usec != UINT64_MAX) {
e16bcf98
TG
487 r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare);
488 if (r < 0)
489 return r;
490 }
491
5cd67116 492 r = netlink_slot_allocate(nl, !ret_slot, NETLINK_REPLY_CALLBACK, sizeof(struct reply_callback), userdata, description, &slot);
8190a388
YW
493 if (r < 0)
494 return r;
e16bcf98 495
ee38400b 496 slot->reply_callback.callback = callback;
f5c61588 497 slot->reply_callback.timeout = timespan_to_timestamp(usec);
e16bcf98 498
b522c4b9 499 k = sd_netlink_send(nl, m, &slot->reply_callback.serial);
545bab1f 500 if (k < 0)
e16bcf98 501 return k;
e16bcf98 502
b522c4b9 503 r = hashmap_put(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial), &slot->reply_callback);
545bab1f 504 if (r < 0)
e16bcf98 505 return r;
e16bcf98 506
f5c61588 507 if (slot->reply_callback.timeout != USEC_INFINITY) {
ee38400b 508 r = prioq_put(nl->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);
f6bdbd9e 509 if (r < 0) {
b522c4b9 510 (void) hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial));
e16bcf98
TG
511 return r;
512 }
513 }
514
40c864af 515 /* Set this at last. Otherwise, some failures in above would call destroy_callback but some would not. */
5cd67116
YW
516 slot->destroy_callback = destroy_callback;
517
ee38400b
YW
518 if (ret_slot)
519 *ret_slot = slot;
e16bcf98 520
ee38400b 521 TAKE_PTR(slot);
545bab1f 522
e16bcf98
TG
523 return k;
524}
525
dd35a61c 526int sd_netlink_read(
409856d3 527 sd_netlink *nl,
2b012288
YW
528 uint32_t serial,
529 uint64_t usec,
530 sd_netlink_message **ret) {
531
4555ec72 532 usec_t timeout;
4555ec72
TG
533 int r;
534
409856d3
YW
535 assert_return(nl, -EINVAL);
536 assert_return(!netlink_pid_changed(nl), -ECHILD);
4555ec72 537
f5c61588 538 timeout = timespan_to_timestamp(usec);
4555ec72 539
65f568bb 540 for (;;) {
7b34bae3 541 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
4555ec72 542 usec_t left;
65f568bb 543
7b34bae3
YW
544 m = hashmap_remove(nl->rqueue_by_serial, UINT32_TO_PTR(serial));
545 if (m) {
2b012288 546 uint16_t type;
65f568bb 547
2b012288 548 /* found a match, remove from rqueue and return it */
7b34bae3 549 sd_netlink_message_unref(ordered_set_remove(nl->rqueue, m));
ea342a99 550
7b34bae3 551 r = sd_netlink_message_get_errno(m);
2b012288
YW
552 if (r < 0)
553 return r;
1b89cf56 554
7b34bae3 555 r = sd_netlink_message_get_type(m, &type);
2b012288
YW
556 if (r < 0)
557 return r;
ea342a99 558
2b012288 559 if (type == NLMSG_DONE) {
766417bd
YW
560 if (ret)
561 *ret = NULL;
2b012288 562 return 0;
65f568bb 563 }
2b012288
YW
564
565 if (ret)
7b34bae3 566 *ret = TAKE_PTR(m);
2b012288 567 return 1;
65f568bb 568 }
1b89cf56 569
409856d3 570 r = socket_read_message(nl);
1b89cf56
TG
571 if (r < 0)
572 return r;
573 if (r > 0)
6ff8806e 574 /* received message, so try to process straight away */
4555ec72 575 continue;
65f568bb 576
f5c61588 577 if (timeout != USEC_INFINITY) {
4555ec72
TG
578 usec_t n;
579
580 n = now(CLOCK_MONOTONIC);
581 if (n >= timeout)
582 return -ETIMEDOUT;
583
11537375 584 left = usec_sub_unsigned(timeout, n);
4555ec72 585 } else
11537375 586 left = USEC_INFINITY;
4555ec72 587
409856d3 588 r = netlink_poll(nl, true, left);
4555ec72
TG
589 if (r < 0)
590 return r;
2b012288 591 if (r == 0)
b551ddd3 592 return -ETIMEDOUT;
b4f2a5b1
TG
593 }
594}
595
dd35a61c 596int sd_netlink_call(
409856d3 597 sd_netlink *nl,
4df42cd9
FW
598 sd_netlink_message *message,
599 uint64_t usec,
600 sd_netlink_message **ret) {
2b012288 601
4df42cd9
FW
602 uint32_t serial;
603 int r;
604
409856d3
YW
605 assert_return(nl, -EINVAL);
606 assert_return(!netlink_pid_changed(nl), -ECHILD);
4df42cd9
FW
607 assert_return(message, -EINVAL);
608
409856d3 609 r = sd_netlink_send(nl, message, &serial);
4df42cd9
FW
610 if (r < 0)
611 return r;
612
409856d3 613 return sd_netlink_read(nl, serial, usec, ret);
4df42cd9
FW
614}
615
dd35a61c 616int sd_netlink_get_events(sd_netlink *nl) {
409856d3
YW
617 assert_return(nl, -EINVAL);
618 assert_return(!netlink_pid_changed(nl), -ECHILD);
b4f2a5b1 619
e417c4ac 620 return ordered_set_size(nl->rqueue) == 0 ? POLLIN : 0;
b4f2a5b1
TG
621}
622
dd35a61c 623int sd_netlink_get_timeout(sd_netlink *nl, uint64_t *timeout_usec) {
b4f2a5b1
TG
624 struct reply_callback *c;
625
409856d3 626 assert_return(nl, -EINVAL);
b4f2a5b1 627 assert_return(timeout_usec, -EINVAL);
409856d3 628 assert_return(!netlink_pid_changed(nl), -ECHILD);
b4f2a5b1 629
e417c4ac 630 if (ordered_set_size(nl->rqueue) > 0) {
b4f2a5b1
TG
631 *timeout_usec = 0;
632 return 1;
633 }
634
409856d3 635 c = prioq_peek(nl->reply_callbacks_prioq);
b4f2a5b1 636 if (!c) {
f5fbe71d 637 *timeout_usec = UINT64_MAX;
b4f2a5b1
TG
638 return 0;
639 }
640
641 *timeout_usec = c->timeout;
b4f2a5b1
TG
642 return 1;
643}
644
645static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
99534007 646 sd_netlink *nl = ASSERT_PTR(userdata);
b4f2a5b1
TG
647 int r;
648
409856d3 649 r = sd_netlink_process(nl, NULL);
b4f2a5b1
TG
650 if (r < 0)
651 return r;
652
653 return 1;
654}
655
656static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
99534007 657 sd_netlink *nl = ASSERT_PTR(userdata);
b4f2a5b1
TG
658 int r;
659
409856d3 660 r = sd_netlink_process(nl, NULL);
b4f2a5b1
TG
661 if (r < 0)
662 return r;
663
664 return 1;
665}
666
667static int prepare_callback(sd_event_source *s, void *userdata) {
99534007 668 sd_netlink *nl = ASSERT_PTR(userdata);
40c864af 669 int r, enabled;
b4f2a5b1
TG
670 usec_t until;
671
672 assert(s);
b4f2a5b1 673
40c864af 674 r = sd_netlink_get_events(nl);
b4f2a5b1
TG
675 if (r < 0)
676 return r;
677
40c864af 678 r = sd_event_source_set_io_events(nl->io_event_source, r);
b4f2a5b1
TG
679 if (r < 0)
680 return r;
b4f2a5b1 681
40c864af
ZJS
682 enabled = sd_netlink_get_timeout(nl, &until);
683 if (enabled < 0)
684 return enabled;
685 if (enabled > 0) {
686 r = sd_event_source_set_time(nl->time_event_source, until);
687 if (r < 0)
688 return r;
b4f2a5b1
TG
689 }
690
93c0a5ec
ZJS
691 r = sd_event_source_set_enabled(nl->time_event_source,
692 enabled > 0 ? SD_EVENT_ONESHOT : SD_EVENT_OFF);
b4f2a5b1
TG
693 if (r < 0)
694 return r;
695
696 return 1;
697}
698
dd35a61c 699int sd_netlink_attach_event(sd_netlink *nl, sd_event *event, int64_t priority) {
b4f2a5b1
TG
700 int r;
701
409856d3
YW
702 assert_return(nl, -EINVAL);
703 assert_return(!nl->event, -EBUSY);
b4f2a5b1 704
409856d3
YW
705 assert(!nl->io_event_source);
706 assert(!nl->time_event_source);
b4f2a5b1
TG
707
708 if (event)
409856d3 709 nl->event = sd_event_ref(event);
b4f2a5b1 710 else {
409856d3 711 r = sd_event_default(&nl->event);
b4f2a5b1
TG
712 if (r < 0)
713 return r;
714 }
715
409856d3 716 r = sd_event_add_io(nl->event, &nl->io_event_source, nl->fd, 0, io_callback, nl);
b4f2a5b1
TG
717 if (r < 0)
718 goto fail;
719
409856d3 720 r = sd_event_source_set_priority(nl->io_event_source, priority);
b4f2a5b1
TG
721 if (r < 0)
722 goto fail;
723
409856d3 724 r = sd_event_source_set_description(nl->io_event_source, "netlink-receive-message");
9021bb9f
TG
725 if (r < 0)
726 goto fail;
727
409856d3 728 r = sd_event_source_set_prepare(nl->io_event_source, prepare_callback);
b4f2a5b1
TG
729 if (r < 0)
730 goto fail;
731
409856d3 732 r = sd_event_add_time(nl->event, &nl->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, nl);
b4f2a5b1
TG
733 if (r < 0)
734 goto fail;
735
409856d3 736 r = sd_event_source_set_priority(nl->time_event_source, priority);
b4f2a5b1
TG
737 if (r < 0)
738 goto fail;
739
409856d3 740 r = sd_event_source_set_description(nl->time_event_source, "netlink-timer");
9021bb9f
TG
741 if (r < 0)
742 goto fail;
743
b4f2a5b1
TG
744 return 0;
745
746fail:
409856d3 747 sd_netlink_detach_event(nl);
b4f2a5b1
TG
748 return r;
749}
750
dd35a61c 751int sd_netlink_detach_event(sd_netlink *nl) {
409856d3
YW
752 assert_return(nl, -EINVAL);
753 assert_return(nl->event, -ENXIO);
b4f2a5b1 754
409856d3 755 nl->io_event_source = sd_event_source_unref(nl->io_event_source);
b4f2a5b1 756
409856d3 757 nl->time_event_source = sd_event_source_unref(nl->time_event_source);
b4f2a5b1 758
409856d3 759 nl->event = sd_event_unref(nl->event);
b4f2a5b1
TG
760
761 return 0;
762}
8cec01b9 763
0adb58c3
LP
764sd_event* sd_netlink_get_event(sd_netlink *nl) {
765 assert_return(nl, NULL);
766
767 return nl->event;
768}
769
3f60e448
YW
770int netlink_add_match_internal(
771 sd_netlink *nl,
ee38400b 772 sd_netlink_slot **ret_slot,
3f60e448
YW
773 const uint32_t *groups,
774 size_t n_groups,
ee38400b 775 uint16_t type,
e1578f60 776 uint8_t cmd,
ee38400b
YW
777 sd_netlink_message_handler_t callback,
778 sd_netlink_destroy_t destroy_callback,
8190a388
YW
779 void *userdata,
780 const char *description) {
409856d3 781
ee38400b 782 _cleanup_free_ sd_netlink_slot *slot = NULL;
31710be5 783 int r;
8cec01b9 784
3f60e448
YW
785 assert(groups);
786 assert(n_groups > 0);
787
788 for (size_t i = 0; i < n_groups; i++) {
789 r = socket_broadcast_group_ref(nl, groups[i]);
790 if (r < 0)
791 return r;
792 }
8cec01b9 793
3f60e448
YW
794 r = netlink_slot_allocate(nl, !ret_slot, NETLINK_MATCH_CALLBACK, sizeof(struct match_callback),
795 userdata, description, &slot);
8190a388
YW
796 if (r < 0)
797 return r;
8cec01b9 798
3f60e448
YW
799 slot->match_callback.groups = newdup(uint32_t, groups, n_groups);
800 if (!slot->match_callback.groups)
801 return -ENOMEM;
802
803 slot->match_callback.n_groups = n_groups;
ee38400b
YW
804 slot->match_callback.callback = callback;
805 slot->match_callback.type = type;
e1578f60 806 slot->match_callback.cmd = cmd;
8cec01b9 807
3f60e448
YW
808 LIST_PREPEND(match_callbacks, nl->match_callbacks, &slot->match_callback);
809
810 /* Set this at last. Otherwise, some failures in above call the destroy callback but some do not. */
811 slot->destroy_callback = destroy_callback;
812
813 if (ret_slot)
814 *ret_slot = slot;
815
816 TAKE_PTR(slot);
817 return 0;
818}
819
dd35a61c 820int sd_netlink_add_match(
3f60e448
YW
821 sd_netlink *rtnl,
822 sd_netlink_slot **ret_slot,
823 uint16_t type,
824 sd_netlink_message_handler_t callback,
825 sd_netlink_destroy_t destroy_callback,
826 void *userdata,
827 const char *description) {
828
829 static const uint32_t
830 address_groups[] = { RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR, },
831 link_groups[] = { RTNLGRP_LINK, },
832 neighbor_groups[] = { RTNLGRP_NEIGH, },
833 nexthop_groups[] = { RTNLGRP_NEXTHOP, },
834 route_groups[] = { RTNLGRP_IPV4_ROUTE, RTNLGRP_IPV6_ROUTE, },
27e93a4b
YW
835 rule_groups[] = { RTNLGRP_IPV4_RULE, RTNLGRP_IPV6_RULE, },
836 tc_groups[] = { RTNLGRP_TC };
3f60e448
YW
837 const uint32_t *groups;
838 size_t n_groups;
839
840 assert_return(rtnl, -EINVAL);
841 assert_return(callback, -EINVAL);
842 assert_return(!netlink_pid_changed(rtnl), -ECHILD);
843
31710be5
TG
844 switch (type) {
845 case RTM_NEWLINK:
31710be5 846 case RTM_DELLINK:
3f60e448
YW
847 groups = link_groups;
848 n_groups = ELEMENTSOF(link_groups);
31710be5
TG
849 break;
850 case RTM_NEWADDR:
31710be5 851 case RTM_DELADDR:
3f60e448
YW
852 groups = address_groups;
853 n_groups = ELEMENTSOF(address_groups);
d1bdafd2
WKI
854 break;
855 case RTM_NEWNEIGH:
856 case RTM_DELNEIGH:
3f60e448
YW
857 groups = neighbor_groups;
858 n_groups = ELEMENTSOF(neighbor_groups);
31710be5 859 break;
87e4c847
TG
860 case RTM_NEWROUTE:
861 case RTM_DELROUTE:
3f60e448
YW
862 groups = route_groups;
863 n_groups = ELEMENTSOF(route_groups);
87e4c847 864 break;
bce67bbe
SS
865 case RTM_NEWRULE:
866 case RTM_DELRULE:
3f60e448
YW
867 groups = rule_groups;
868 n_groups = ELEMENTSOF(rule_groups);
bce67bbe 869 break;
c16c7808
SS
870 case RTM_NEWNEXTHOP:
871 case RTM_DELNEXTHOP:
3f60e448
YW
872 groups = nexthop_groups;
873 n_groups = ELEMENTSOF(nexthop_groups);
874 break;
27e93a4b
YW
875 case RTM_NEWQDISC:
876 case RTM_DELQDISC:
877 case RTM_NEWTCLASS:
878 case RTM_DELTCLASS:
879 groups = tc_groups;
880 n_groups = ELEMENTSOF(tc_groups);
881 break;
31710be5
TG
882 default:
883 return -EOPNOTSUPP;
884 }
885
e1578f60 886 return netlink_add_match_internal(rtnl, ret_slot, groups, n_groups, type, 0, callback,
3f60e448 887 destroy_callback, userdata, description);
8cec01b9 888}
dc317a9a 889
dd35a61c 890int sd_netlink_attach_filter(sd_netlink *nl, size_t len, const struct sock_filter *filter) {
dc317a9a
YW
891 assert_return(nl, -EINVAL);
892 assert_return(len == 0 || filter, -EINVAL);
893
894 if (setsockopt(nl->fd, SOL_SOCKET,
895 len == 0 ? SO_DETACH_FILTER : SO_ATTACH_FILTER,
896 &(struct sock_fprog) {
897 .len = len,
f4f81a6b 898 .filter = (struct sock_filter*) filter,
dc317a9a
YW
899 }, sizeof(struct sock_fprog)) < 0)
900 return -errno;
901
902 return 0;
903}