]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/sd-netlink.c
912efcf3ad9a97c9f615b3ea644d1fd8ebb745da
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / sd-netlink.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <poll.h>
4
5 #include "sd-netlink.h"
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "hashmap.h"
10 #include "io-util.h"
11 #include "macro.h"
12 #include "netlink-genl.h"
13 #include "netlink-internal.h"
14 #include "netlink-slot.h"
15 #include "netlink-util.h"
16 #include "process-util.h"
17 #include "socket-util.h"
18 #include "string-util.h"
19
20 /* Some really high limit, to catch programming errors */
21 #define REPLY_CALLBACKS_MAX UINT16_MAX
22
23 static int netlink_new(sd_netlink **ret) {
24 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL;
25
26 assert_return(ret, -EINVAL);
27
28 nl = new(sd_netlink, 1);
29 if (!nl)
30 return -ENOMEM;
31
32 *nl = (sd_netlink) {
33 .n_ref = 1,
34 .fd = -1,
35 .sockaddr.nl.nl_family = AF_NETLINK,
36 .original_pid = getpid_cached(),
37 .protocol = -1,
38
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,
62 };
63
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)))
66 return -ENOMEM;
67
68 *ret = TAKE_PTR(nl);
69 return 0;
70 }
71
72 _public_ int sd_netlink_open_fd(sd_netlink **ret, int fd) {
73 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL;
74 int r, protocol;
75
76 assert_return(ret, -EINVAL);
77 assert_return(fd >= 0, -EBADF);
78
79 r = netlink_new(&nl);
80 if (r < 0)
81 return r;
82
83 r = getsockopt_int(fd, SOL_SOCKET, SO_PROTOCOL, &protocol);
84 if (r < 0)
85 return r;
86
87 nl->fd = fd;
88 nl->protocol = protocol;
89
90 r = setsockopt_int(fd, SOL_NETLINK, NETLINK_EXT_ACK, true);
91 if (r < 0)
92 log_debug_errno(r, "sd-netlink: Failed to enable NETLINK_EXT_ACK option, ignoring: %m");
93
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
98 r = socket_bind(nl);
99 if (r < 0) {
100 nl->fd = -1; /* on failure, the caller remains owner of the fd, hence don't close it here */
101 nl->protocol = -1;
102 return r;
103 }
104
105 *ret = TAKE_PTR(nl);
106
107 return 0;
108 }
109
110 _public_ int sd_netlink_open(sd_netlink **ret) {
111 return netlink_open_family(ret, NETLINK_ROUTE);
112 }
113
114 _public_ int sd_netlink_increase_rxbuf(sd_netlink *nl, size_t size) {
115 assert_return(nl, -EINVAL);
116 assert_return(!netlink_pid_changed(nl), -ECHILD);
117
118 return fd_increase_rxbuf(nl->fd, size);
119 }
120
121 static sd_netlink *netlink_free(sd_netlink *nl) {
122 sd_netlink_slot *s;
123 unsigned i;
124
125 assert(nl);
126
127 for (i = 0; i < nl->rqueue_size; i++)
128 sd_netlink_message_unref(nl->rqueue[i]);
129 free(nl->rqueue);
130
131 for (i = 0; i < nl->rqueue_partial_size; i++)
132 sd_netlink_message_unref(nl->rqueue_partial[i]);
133 free(nl->rqueue_partial);
134
135 free(nl->rbuffer);
136
137 while ((s = nl->slots)) {
138 assert(s->floating);
139 netlink_slot_disconnect(s, true);
140 }
141 hashmap_free(nl->reply_callbacks);
142 prioq_free(nl->reply_callbacks_prioq);
143
144 sd_event_source_unref(nl->io_event_source);
145 sd_event_source_unref(nl->time_event_source);
146 sd_event_unref(nl->event);
147
148 hashmap_free(nl->broadcast_group_refs);
149
150 genl_clear_family(nl);
151
152 safe_close(nl->fd);
153 return mfree(nl);
154 }
155
156 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_netlink, sd_netlink, netlink_free);
157
158 _public_ int sd_netlink_send(
159 sd_netlink *nl,
160 sd_netlink_message *message,
161 uint32_t *serial) {
162
163 int r;
164
165 assert_return(nl, -EINVAL);
166 assert_return(!netlink_pid_changed(nl), -ECHILD);
167 assert_return(message, -EINVAL);
168 assert_return(!message->sealed, -EPERM);
169
170 netlink_seal_message(nl, message);
171
172 r = socket_write_message(nl, message);
173 if (r < 0)
174 return r;
175
176 if (serial)
177 *serial = message_get_serial(message);
178
179 return 1;
180 }
181
182 int netlink_rqueue_make_room(sd_netlink *nl) {
183 assert(nl);
184
185 if (nl->rqueue_size >= NETLINK_RQUEUE_MAX)
186 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
187 "sd-netlink: exhausted the read queue size (%d)",
188 NETLINK_RQUEUE_MAX);
189
190 if (!GREEDY_REALLOC(nl->rqueue, nl->rqueue_size + 1))
191 return -ENOMEM;
192
193 return 0;
194 }
195
196 int netlink_rqueue_partial_make_room(sd_netlink *nl) {
197 assert(nl);
198
199 if (nl->rqueue_partial_size >= NETLINK_RQUEUE_MAX)
200 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
201 "sd-netlink: exhausted the partial read queue size (%d)",
202 NETLINK_RQUEUE_MAX);
203
204 if (!GREEDY_REALLOC(nl->rqueue_partial, nl->rqueue_partial_size + 1))
205 return -ENOMEM;
206
207 return 0;
208 }
209
210 static int dispatch_rqueue(sd_netlink *nl, sd_netlink_message **message) {
211 int r;
212
213 assert(nl);
214 assert(message);
215
216 if (nl->rqueue_size <= 0) {
217 /* Try to read a new message */
218 r = socket_read_message(nl);
219 if (r == -ENOBUFS) { /* FIXME: ignore buffer overruns for now */
220 log_debug_errno(r, "sd-netlink: Got ENOBUFS from netlink socket, ignoring.");
221 return 1;
222 }
223 if (r <= 0)
224 return r;
225 }
226
227 /* Dispatch a queued message */
228 *message = nl->rqueue[0];
229 nl->rqueue_size--;
230 memmove(nl->rqueue, nl->rqueue + 1, sizeof(sd_netlink_message*) * nl->rqueue_size);
231
232 return 1;
233 }
234
235 static int process_timeout(sd_netlink *nl) {
236 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
237 struct reply_callback *c;
238 sd_netlink_slot *slot;
239 usec_t n;
240 int r;
241
242 assert(nl);
243
244 c = prioq_peek(nl->reply_callbacks_prioq);
245 if (!c)
246 return 0;
247
248 n = now(CLOCK_MONOTONIC);
249 if (c->timeout > n)
250 return 0;
251
252 r = message_new_synthetic_error(nl, -ETIMEDOUT, c->serial, &m);
253 if (r < 0)
254 return r;
255
256 assert_se(prioq_pop(nl->reply_callbacks_prioq) == c);
257 c->timeout = 0;
258 hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(c->serial));
259
260 slot = container_of(c, sd_netlink_slot, reply_callback);
261
262 r = c->callback(nl, m, slot->userdata);
263 if (r < 0)
264 log_debug_errno(r, "sd-netlink: timedout callback %s%s%sfailed: %m",
265 slot->description ? "'" : "",
266 strempty(slot->description),
267 slot->description ? "' " : "");
268
269 if (slot->floating)
270 netlink_slot_disconnect(slot, true);
271
272 return 1;
273 }
274
275 static int process_reply(sd_netlink *nl, sd_netlink_message *m) {
276 struct reply_callback *c;
277 sd_netlink_slot *slot;
278 uint32_t serial;
279 uint16_t type;
280 int r;
281
282 assert(nl);
283 assert(m);
284
285 serial = message_get_serial(m);
286 c = hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(serial));
287 if (!c)
288 return 0;
289
290 if (c->timeout != 0) {
291 prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx);
292 c->timeout = 0;
293 }
294
295 r = sd_netlink_message_get_type(m, &type);
296 if (r < 0)
297 return r;
298
299 if (type == NLMSG_DONE)
300 m = NULL;
301
302 slot = container_of(c, sd_netlink_slot, reply_callback);
303
304 r = c->callback(nl, m, slot->userdata);
305 if (r < 0)
306 log_debug_errno(r, "sd-netlink: reply callback %s%s%sfailed: %m",
307 slot->description ? "'" : "",
308 strempty(slot->description),
309 slot->description ? "' " : "");
310
311 if (slot->floating)
312 netlink_slot_disconnect(slot, true);
313
314 return 1;
315 }
316
317 static int process_match(sd_netlink *nl, sd_netlink_message *m) {
318 uint16_t type;
319 uint8_t cmd;
320 int r;
321
322 assert(nl);
323 assert(m);
324
325 r = sd_netlink_message_get_type(m, &type);
326 if (r < 0)
327 return r;
328
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
336 LIST_FOREACH(match_callbacks, c, nl->match_callbacks) {
337 sd_netlink_slot *slot;
338 bool found = false;
339
340 if (c->type != type)
341 continue;
342 if (c->cmd != 0 && c->cmd != cmd)
343 continue;
344
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
354 slot = container_of(c, sd_netlink_slot, match_callback);
355
356 r = c->callback(nl, m, slot->userdata);
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;
364 }
365
366 return 1;
367 }
368
369 static int process_running(sd_netlink *nl, sd_netlink_message **ret) {
370 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
371 int r;
372
373 assert(nl);
374
375 r = process_timeout(nl);
376 if (r != 0)
377 goto null_message;
378
379 r = dispatch_rqueue(nl, &m);
380 if (r < 0)
381 return r;
382 if (!m)
383 goto null_message;
384
385 if (sd_netlink_message_is_broadcast(m))
386 r = process_match(nl, m);
387 else
388 r = process_reply(nl, m);
389 if (r != 0)
390 goto null_message;
391
392 if (ret) {
393 *ret = TAKE_PTR(m);
394
395 return 1;
396 }
397
398 return 1;
399
400 null_message:
401 if (r >= 0 && ret)
402 *ret = NULL;
403
404 return r;
405 }
406
407 int sd_netlink_process(sd_netlink *nl, sd_netlink_message **ret) {
408 NETLINK_DONT_DESTROY(nl);
409 int r;
410
411 assert_return(nl, -EINVAL);
412 assert_return(!netlink_pid_changed(nl), -ECHILD);
413 assert_return(!nl->processing, -EBUSY);
414
415 nl->processing = true;
416 r = process_running(nl, ret);
417 nl->processing = false;
418
419 return r;
420 }
421
422 static usec_t calc_elapse(uint64_t usec) {
423 if (usec == UINT64_MAX)
424 return 0;
425
426 if (usec == 0)
427 usec = NETLINK_DEFAULT_TIMEOUT_USEC;
428
429 return usec_add(now(CLOCK_MONOTONIC), usec);
430 }
431
432 static int netlink_poll(sd_netlink *nl, bool need_more, usec_t timeout_usec) {
433 usec_t m = USEC_INFINITY;
434 int r, e;
435
436 assert(nl);
437
438 e = sd_netlink_get_events(nl);
439 if (e < 0)
440 return e;
441
442 if (need_more)
443 /* Caller wants more data, and doesn't care about
444 * what's been read or any other timeouts. */
445 e |= POLLIN;
446 else {
447 usec_t until;
448
449 /* Caller wants to process if there is something to
450 * process, but doesn't care otherwise */
451
452 r = sd_netlink_get_timeout(nl, &until);
453 if (r < 0)
454 return r;
455
456 m = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
457 }
458
459 r = fd_wait_for_event(nl->fd, e, MIN(m, timeout_usec));
460 if (r <= 0)
461 return r;
462
463 return 1;
464 }
465
466 int sd_netlink_wait(sd_netlink *nl, uint64_t timeout_usec) {
467 assert_return(nl, -EINVAL);
468 assert_return(!netlink_pid_changed(nl), -ECHILD);
469
470 if (nl->rqueue_size > 0)
471 return 0;
472
473 return netlink_poll(nl, false, timeout_usec);
474 }
475
476 static 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
485 return CMP(x->timeout, y->timeout);
486 }
487
488 _public_ int sd_netlink_call_async(
489 sd_netlink *nl,
490 sd_netlink_slot **ret_slot,
491 sd_netlink_message *m,
492 sd_netlink_message_handler_t callback,
493 sd_netlink_destroy_t destroy_callback,
494 void *userdata,
495 uint64_t usec,
496 const char *description) {
497
498 _cleanup_free_ sd_netlink_slot *slot = NULL;
499 int r, k;
500
501 assert_return(nl, -EINVAL);
502 assert_return(m, -EINVAL);
503 assert_return(callback, -EINVAL);
504 assert_return(!netlink_pid_changed(nl), -ECHILD);
505
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);
510 if (r < 0)
511 return r;
512
513 if (usec != UINT64_MAX) {
514 r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare);
515 if (r < 0)
516 return r;
517 }
518
519 r = netlink_slot_allocate(nl, !ret_slot, NETLINK_REPLY_CALLBACK, sizeof(struct reply_callback), userdata, description, &slot);
520 if (r < 0)
521 return r;
522
523 slot->reply_callback.callback = callback;
524 slot->reply_callback.timeout = calc_elapse(usec);
525
526 k = sd_netlink_send(nl, m, &slot->reply_callback.serial);
527 if (k < 0)
528 return k;
529
530 r = hashmap_put(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial), &slot->reply_callback);
531 if (r < 0)
532 return r;
533
534 if (slot->reply_callback.timeout != 0) {
535 r = prioq_put(nl->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);
536 if (r < 0) {
537 (void) hashmap_remove(nl->reply_callbacks, UINT32_TO_PTR(slot->reply_callback.serial));
538 return r;
539 }
540 }
541
542 /* Set this at last. Otherwise, some failures in above would call destroy_callback but some would not. */
543 slot->destroy_callback = destroy_callback;
544
545 if (ret_slot)
546 *ret_slot = slot;
547
548 TAKE_PTR(slot);
549
550 return k;
551 }
552
553 _public_ int sd_netlink_read(
554 sd_netlink *nl,
555 uint32_t serial,
556 uint64_t usec,
557 sd_netlink_message **ret) {
558
559 usec_t timeout;
560 int r;
561
562 assert_return(nl, -EINVAL);
563 assert_return(!netlink_pid_changed(nl), -ECHILD);
564
565 timeout = calc_elapse(usec);
566
567 for (;;) {
568 usec_t left;
569
570 for (unsigned i = 0; i < nl->rqueue_size; i++) {
571 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *incoming = NULL;
572 uint32_t received_serial;
573 uint16_t type;
574
575 received_serial = message_get_serial(nl->rqueue[i]);
576 if (received_serial != serial)
577 continue;
578
579 incoming = nl->rqueue[i];
580
581 /* found a match, remove from rqueue and return it */
582 memmove(nl->rqueue + i, nl->rqueue + i + 1,
583 sizeof(sd_netlink_message*) * (nl->rqueue_size - i - 1));
584 nl->rqueue_size--;
585
586 r = sd_netlink_message_get_errno(incoming);
587 if (r < 0)
588 return r;
589
590 r = sd_netlink_message_get_type(incoming, &type);
591 if (r < 0)
592 return r;
593
594 if (type == NLMSG_DONE) {
595 *ret = NULL;
596 return 0;
597 }
598
599 if (ret)
600 *ret = TAKE_PTR(incoming);
601 return 1;
602 }
603
604 r = socket_read_message(nl);
605 if (r < 0)
606 return r;
607 if (r > 0)
608 /* received message, so try to process straight away */
609 continue;
610
611 if (timeout > 0) {
612 usec_t n;
613
614 n = now(CLOCK_MONOTONIC);
615 if (n >= timeout)
616 return -ETIMEDOUT;
617
618 left = usec_sub_unsigned(timeout, n);
619 } else
620 left = USEC_INFINITY;
621
622 r = netlink_poll(nl, true, left);
623 if (r < 0)
624 return r;
625 if (r == 0)
626 return -ETIMEDOUT;
627 }
628 }
629
630 _public_ int sd_netlink_call(
631 sd_netlink *nl,
632 sd_netlink_message *message,
633 uint64_t usec,
634 sd_netlink_message **ret) {
635
636 uint32_t serial;
637 int r;
638
639 assert_return(nl, -EINVAL);
640 assert_return(!netlink_pid_changed(nl), -ECHILD);
641 assert_return(message, -EINVAL);
642
643 r = sd_netlink_send(nl, message, &serial);
644 if (r < 0)
645 return r;
646
647 return sd_netlink_read(nl, serial, usec, ret);
648 }
649
650 _public_ int sd_netlink_get_events(sd_netlink *nl) {
651 assert_return(nl, -EINVAL);
652 assert_return(!netlink_pid_changed(nl), -ECHILD);
653
654 return nl->rqueue_size == 0 ? POLLIN : 0;
655 }
656
657 _public_ int sd_netlink_get_timeout(sd_netlink *nl, uint64_t *timeout_usec) {
658 struct reply_callback *c;
659
660 assert_return(nl, -EINVAL);
661 assert_return(timeout_usec, -EINVAL);
662 assert_return(!netlink_pid_changed(nl), -ECHILD);
663
664 if (nl->rqueue_size > 0) {
665 *timeout_usec = 0;
666 return 1;
667 }
668
669 c = prioq_peek(nl->reply_callbacks_prioq);
670 if (!c) {
671 *timeout_usec = UINT64_MAX;
672 return 0;
673 }
674
675 *timeout_usec = c->timeout;
676
677 return 1;
678 }
679
680 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
681 sd_netlink *nl = userdata;
682 int r;
683
684 assert(nl);
685
686 r = sd_netlink_process(nl, NULL);
687 if (r < 0)
688 return r;
689
690 return 1;
691 }
692
693 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
694 sd_netlink *nl = userdata;
695 int r;
696
697 assert(nl);
698
699 r = sd_netlink_process(nl, NULL);
700 if (r < 0)
701 return r;
702
703 return 1;
704 }
705
706 static int prepare_callback(sd_event_source *s, void *userdata) {
707 sd_netlink *nl = userdata;
708 int r, enabled;
709 usec_t until;
710
711 assert(s);
712 assert(nl);
713
714 r = sd_netlink_get_events(nl);
715 if (r < 0)
716 return r;
717
718 r = sd_event_source_set_io_events(nl->io_event_source, r);
719 if (r < 0)
720 return r;
721
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;
729 }
730
731 r = sd_event_source_set_enabled(nl->time_event_source,
732 enabled > 0 ? SD_EVENT_ONESHOT : SD_EVENT_OFF);
733 if (r < 0)
734 return r;
735
736 return 1;
737 }
738
739 _public_ int sd_netlink_attach_event(sd_netlink *nl, sd_event *event, int64_t priority) {
740 int r;
741
742 assert_return(nl, -EINVAL);
743 assert_return(!nl->event, -EBUSY);
744
745 assert(!nl->io_event_source);
746 assert(!nl->time_event_source);
747
748 if (event)
749 nl->event = sd_event_ref(event);
750 else {
751 r = sd_event_default(&nl->event);
752 if (r < 0)
753 return r;
754 }
755
756 r = sd_event_add_io(nl->event, &nl->io_event_source, nl->fd, 0, io_callback, nl);
757 if (r < 0)
758 goto fail;
759
760 r = sd_event_source_set_priority(nl->io_event_source, priority);
761 if (r < 0)
762 goto fail;
763
764 r = sd_event_source_set_description(nl->io_event_source, "netlink-receive-message");
765 if (r < 0)
766 goto fail;
767
768 r = sd_event_source_set_prepare(nl->io_event_source, prepare_callback);
769 if (r < 0)
770 goto fail;
771
772 r = sd_event_add_time(nl->event, &nl->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, nl);
773 if (r < 0)
774 goto fail;
775
776 r = sd_event_source_set_priority(nl->time_event_source, priority);
777 if (r < 0)
778 goto fail;
779
780 r = sd_event_source_set_description(nl->time_event_source, "netlink-timer");
781 if (r < 0)
782 goto fail;
783
784 return 0;
785
786 fail:
787 sd_netlink_detach_event(nl);
788 return r;
789 }
790
791 _public_ int sd_netlink_detach_event(sd_netlink *nl) {
792 assert_return(nl, -EINVAL);
793 assert_return(nl->event, -ENXIO);
794
795 nl->io_event_source = sd_event_source_unref(nl->io_event_source);
796
797 nl->time_event_source = sd_event_source_unref(nl->time_event_source);
798
799 nl->event = sd_event_unref(nl->event);
800
801 return 0;
802 }
803
804 int netlink_add_match_internal(
805 sd_netlink *nl,
806 sd_netlink_slot **ret_slot,
807 const uint32_t *groups,
808 size_t n_groups,
809 uint16_t type,
810 uint8_t cmd,
811 sd_netlink_message_handler_t callback,
812 sd_netlink_destroy_t destroy_callback,
813 void *userdata,
814 const char *description) {
815
816 _cleanup_free_ sd_netlink_slot *slot = NULL;
817 int r;
818
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 }
827
828 r = netlink_slot_allocate(nl, !ret_slot, NETLINK_MATCH_CALLBACK, sizeof(struct match_callback),
829 userdata, description, &slot);
830 if (r < 0)
831 return r;
832
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;
838 slot->match_callback.callback = callback;
839 slot->match_callback.type = type;
840 slot->match_callback.cmd = cmd;
841
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
854 _public_ int sd_netlink_add_match(
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, },
869 rule_groups[] = { RTNLGRP_IPV4_RULE, RTNLGRP_IPV6_RULE, },
870 tc_groups[] = { RTNLGRP_TC };
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
878 switch (type) {
879 case RTM_NEWLINK:
880 case RTM_DELLINK:
881 groups = link_groups;
882 n_groups = ELEMENTSOF(link_groups);
883 break;
884 case RTM_NEWADDR:
885 case RTM_DELADDR:
886 groups = address_groups;
887 n_groups = ELEMENTSOF(address_groups);
888 break;
889 case RTM_NEWNEIGH:
890 case RTM_DELNEIGH:
891 groups = neighbor_groups;
892 n_groups = ELEMENTSOF(neighbor_groups);
893 break;
894 case RTM_NEWROUTE:
895 case RTM_DELROUTE:
896 groups = route_groups;
897 n_groups = ELEMENTSOF(route_groups);
898 break;
899 case RTM_NEWRULE:
900 case RTM_DELRULE:
901 groups = rule_groups;
902 n_groups = ELEMENTSOF(rule_groups);
903 break;
904 case RTM_NEWNEXTHOP:
905 case RTM_DELNEXTHOP:
906 groups = nexthop_groups;
907 n_groups = ELEMENTSOF(nexthop_groups);
908 break;
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;
916 default:
917 return -EOPNOTSUPP;
918 }
919
920 return netlink_add_match_internal(rtnl, ret_slot, groups, n_groups, type, 0, callback,
921 destroy_callback, userdata, description);
922 }
923
924 _public_ int sd_netlink_attach_filter(sd_netlink *nl, size_t len, const struct sock_filter *filter) {
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,
932 .filter = (struct sock_filter*) filter,
933 }, sizeof(struct sock_fprog)) < 0)
934 return -errno;
935
936 return 0;
937 }