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