1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <sys/socket.h>
6 #include "sd-netlink.h"
8 #include "alloc-util.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"
21 static int sd_netlink_new(sd_netlink
**ret
) {
22 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
24 assert_return(ret
, -EINVAL
);
26 rtnl
= new(sd_netlink
, 1);
30 *rtnl
= (sd_netlink
) {
33 .sockaddr
.nl
.nl_family
= AF_NETLINK
,
34 .original_pid
= getpid_cached(),
37 /* Change notification responses have sequence 0, so we must
38 * start our request sequence numbers at 1, or we may confuse our
39 * responses with notifications from the kernel */
44 /* We guarantee that the read buffer has at least space for
46 if (!greedy_realloc((void**)&rtnl
->rbuffer
, &rtnl
->rbuffer_allocated
,
47 sizeof(struct nlmsghdr
), sizeof(uint8_t)))
50 *ret
= TAKE_PTR(rtnl
);
55 int sd_netlink_new_from_netlink(sd_netlink
**ret
, int fd
) {
56 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
60 assert_return(ret
, -EINVAL
);
62 r
= sd_netlink_new(&rtnl
);
66 addrlen
= sizeof(rtnl
->sockaddr
);
68 r
= getsockname(fd
, &rtnl
->sockaddr
.sa
, &addrlen
);
72 if (rtnl
->sockaddr
.nl
.nl_family
!= AF_NETLINK
)
77 *ret
= TAKE_PTR(rtnl
);
82 static bool rtnl_pid_changed(sd_netlink
*rtnl
) {
85 /* We don't support people creating an rtnl connection and
86 * keeping it around over a fork(). Let's complain. */
88 return rtnl
->original_pid
!= getpid_cached();
91 int sd_netlink_open_fd(sd_netlink
**ret
, int fd
) {
92 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
97 assert_return(ret
, -EINVAL
);
98 assert_return(fd
>= 0, -EBADF
);
100 r
= sd_netlink_new(&rtnl
);
104 l
= sizeof(protocol
);
105 r
= getsockopt(fd
, SOL_SOCKET
, SO_PROTOCOL
, &protocol
, &l
);
110 rtnl
->protocol
= protocol
;
112 r
= socket_bind(rtnl
);
114 rtnl
->fd
= -1; /* on failure, the caller remains owner of the fd, hence don't close it here */
119 *ret
= TAKE_PTR(rtnl
);
124 int netlink_open_family(sd_netlink
**ret
, int family
) {
125 _cleanup_close_
int fd
= -1;
128 fd
= socket_open(family
);
132 r
= sd_netlink_open_fd(ret
, fd
);
141 int sd_netlink_open(sd_netlink
**ret
) {
142 return netlink_open_family(ret
, NETLINK_ROUTE
);
145 int sd_netlink_inc_rcvbuf(sd_netlink
*rtnl
, size_t size
) {
146 assert_return(rtnl
, -EINVAL
);
147 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
149 return fd_inc_rcvbuf(rtnl
->fd
, size
);
152 static sd_netlink
*netlink_free(sd_netlink
*rtnl
) {
158 for (i
= 0; i
< rtnl
->rqueue_size
; i
++)
159 sd_netlink_message_unref(rtnl
->rqueue
[i
]);
162 for (i
= 0; i
< rtnl
->rqueue_partial_size
; i
++)
163 sd_netlink_message_unref(rtnl
->rqueue_partial
[i
]);
164 free(rtnl
->rqueue_partial
);
168 while ((s
= rtnl
->slots
)) {
170 netlink_slot_disconnect(s
, true);
172 hashmap_free(rtnl
->reply_callbacks
);
173 prioq_free(rtnl
->reply_callbacks_prioq
);
175 sd_event_source_unref(rtnl
->io_event_source
);
176 sd_event_source_unref(rtnl
->time_event_source
);
177 sd_event_unref(rtnl
->event
);
179 hashmap_free(rtnl
->broadcast_group_refs
);
181 safe_close(rtnl
->fd
);
185 DEFINE_ATOMIC_REF_UNREF_FUNC(sd_netlink
, sd_netlink
, netlink_free
);
187 static void rtnl_seal_message(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
189 assert(!rtnl_pid_changed(rtnl
));
193 /* don't use seq == 0, as that is used for broadcasts, so we
194 would get confused by replies to such messages */
195 m
->hdr
->nlmsg_seq
= rtnl
->serial
++ ? : rtnl
->serial
++;
197 rtnl_message_seal(m
);
202 int sd_netlink_send(sd_netlink
*nl
,
203 sd_netlink_message
*message
,
207 assert_return(nl
, -EINVAL
);
208 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
209 assert_return(message
, -EINVAL
);
210 assert_return(!message
->sealed
, -EPERM
);
212 rtnl_seal_message(nl
, message
);
214 r
= socket_write_message(nl
, message
);
219 *serial
= rtnl_message_get_serial(message
);
224 int rtnl_rqueue_make_room(sd_netlink
*rtnl
) {
227 if (rtnl
->rqueue_size
>= RTNL_RQUEUE_MAX
)
228 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS
),
229 "rtnl: exhausted the read queue size (%d)",
232 if (!GREEDY_REALLOC(rtnl
->rqueue
, rtnl
->rqueue_allocated
, rtnl
->rqueue_size
+ 1))
238 int rtnl_rqueue_partial_make_room(sd_netlink
*rtnl
) {
241 if (rtnl
->rqueue_partial_size
>= RTNL_RQUEUE_MAX
)
242 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS
),
243 "rtnl: exhausted the partial read queue size (%d)",
246 if (!GREEDY_REALLOC(rtnl
->rqueue_partial
, rtnl
->rqueue_partial_allocated
,
247 rtnl
->rqueue_partial_size
+ 1))
253 static int dispatch_rqueue(sd_netlink
*rtnl
, sd_netlink_message
**message
) {
259 if (rtnl
->rqueue_size
<= 0) {
260 /* Try to read a new message */
261 r
= socket_read_message(rtnl
);
262 if (r
== -ENOBUFS
) { /* FIXME: ignore buffer overruns for now */
263 log_debug_errno(r
, "Got ENOBUFS from netlink socket, ignoring.");
270 /* Dispatch a queued message */
271 *message
= rtnl
->rqueue
[0];
273 memmove(rtnl
->rqueue
, rtnl
->rqueue
+ 1, sizeof(sd_netlink_message
*) * rtnl
->rqueue_size
);
278 static int process_timeout(sd_netlink
*rtnl
) {
279 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
280 struct reply_callback
*c
;
281 sd_netlink_slot
*slot
;
287 c
= prioq_peek(rtnl
->reply_callbacks_prioq
);
291 n
= now(CLOCK_MONOTONIC
);
295 r
= rtnl_message_new_synthetic_error(rtnl
, -ETIMEDOUT
, c
->serial
, &m
);
299 assert_se(prioq_pop(rtnl
->reply_callbacks_prioq
) == c
);
301 hashmap_remove(rtnl
->reply_callbacks
, &c
->serial
);
303 slot
= container_of(c
, sd_netlink_slot
, reply_callback
);
305 r
= c
->callback(rtnl
, m
, slot
->userdata
);
307 log_debug_errno(r
, "sd-netlink: timedout callback %s%s%sfailed: %m",
308 slot
->description
? "'" : "",
309 strempty(slot
->description
),
310 slot
->description
? "' " : "");
313 netlink_slot_disconnect(slot
, true);
318 static int process_reply(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
319 struct reply_callback
*c
;
320 sd_netlink_slot
*slot
;
328 serial
= rtnl_message_get_serial(m
);
329 c
= hashmap_remove(rtnl
->reply_callbacks
, &serial
);
333 if (c
->timeout
!= 0) {
334 prioq_remove(rtnl
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
338 r
= sd_netlink_message_get_type(m
, &type
);
342 if (type
== NLMSG_DONE
)
345 slot
= container_of(c
, sd_netlink_slot
, reply_callback
);
347 r
= c
->callback(rtnl
, m
, slot
->userdata
);
349 log_debug_errno(r
, "sd-netlink: reply callback %s%s%sfailed: %m",
350 slot
->description
? "'" : "",
351 strempty(slot
->description
),
352 slot
->description
? "' " : "");
355 netlink_slot_disconnect(slot
, true);
360 static int process_match(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
361 struct match_callback
*c
;
362 sd_netlink_slot
*slot
;
369 r
= sd_netlink_message_get_type(m
, &type
);
373 LIST_FOREACH(match_callbacks
, c
, rtnl
->match_callbacks
) {
374 if (type
== c
->type
) {
375 slot
= container_of(c
, sd_netlink_slot
, match_callback
);
377 r
= c
->callback(rtnl
, m
, slot
->userdata
);
380 log_debug_errno(r
, "sd-netlink: match callback %s%s%sfailed: %m",
381 slot
->description
? "'" : "",
382 strempty(slot
->description
),
383 slot
->description
? "' " : "");
393 static int process_running(sd_netlink
*rtnl
, sd_netlink_message
**ret
) {
394 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
399 r
= process_timeout(rtnl
);
403 r
= dispatch_rqueue(rtnl
, &m
);
409 if (sd_netlink_message_is_broadcast(m
)) {
410 r
= process_match(rtnl
, m
);
414 r
= process_reply(rtnl
, m
);
434 int sd_netlink_process(sd_netlink
*rtnl
, sd_netlink_message
**ret
) {
435 NETLINK_DONT_DESTROY(rtnl
);
438 assert_return(rtnl
, -EINVAL
);
439 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
440 assert_return(!rtnl
->processing
, -EBUSY
);
442 rtnl
->processing
= true;
443 r
= process_running(rtnl
, ret
);
444 rtnl
->processing
= false;
449 static usec_t
calc_elapse(uint64_t usec
) {
450 if (usec
== (uint64_t) -1)
454 usec
= RTNL_DEFAULT_TIMEOUT
;
456 return now(CLOCK_MONOTONIC
) + usec
;
459 static int rtnl_poll(sd_netlink
*rtnl
, bool need_more
, uint64_t timeout_usec
) {
460 struct pollfd p
[1] = {};
462 usec_t m
= USEC_INFINITY
;
467 e
= sd_netlink_get_events(rtnl
);
472 /* Caller wants more data, and doesn't care about
473 * what's been read or any other timeouts. */
477 /* Caller wants to process if there is something to
478 * process, but doesn't care otherwise */
480 r
= sd_netlink_get_timeout(rtnl
, &until
);
485 nw
= now(CLOCK_MONOTONIC
);
486 m
= until
> nw
? until
- nw
: 0;
490 if (timeout_usec
!= (uint64_t) -1 && (m
== (uint64_t) -1 || timeout_usec
< m
))
496 r
= ppoll(p
, 1, m
== (uint64_t) -1 ? NULL
: timespec_store(&ts
, m
), NULL
);
500 return r
> 0 ? 1 : 0;
503 int sd_netlink_wait(sd_netlink
*nl
, uint64_t timeout_usec
) {
504 assert_return(nl
, -EINVAL
);
505 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
507 if (nl
->rqueue_size
> 0)
510 return rtnl_poll(nl
, false, timeout_usec
);
513 static int timeout_compare(const void *a
, const void *b
) {
514 const struct reply_callback
*x
= a
, *y
= b
;
516 if (x
->timeout
!= 0 && y
->timeout
== 0)
519 if (x
->timeout
== 0 && y
->timeout
!= 0)
522 return CMP(x
->timeout
, y
->timeout
);
525 int sd_netlink_call_async(
527 sd_netlink_slot
**ret_slot
,
528 sd_netlink_message
*m
,
529 sd_netlink_message_handler_t callback
,
530 sd_netlink_destroy_t destroy_callback
,
533 const char *description
) {
534 _cleanup_free_ sd_netlink_slot
*slot
= NULL
;
538 assert_return(nl
, -EINVAL
);
539 assert_return(m
, -EINVAL
);
540 assert_return(callback
, -EINVAL
);
541 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
543 r
= hashmap_ensure_allocated(&nl
->reply_callbacks
, &uint64_hash_ops
);
547 if (usec
!= (uint64_t) -1) {
548 r
= prioq_ensure_allocated(&nl
->reply_callbacks_prioq
, timeout_compare
);
553 r
= netlink_slot_allocate(nl
, !ret_slot
, NETLINK_REPLY_CALLBACK
, sizeof(struct reply_callback
), userdata
, description
, &slot
);
557 slot
->reply_callback
.callback
= callback
;
558 slot
->reply_callback
.timeout
= calc_elapse(usec
);
560 k
= sd_netlink_send(nl
, m
, &s
);
564 slot
->reply_callback
.serial
= s
;
566 r
= hashmap_put(nl
->reply_callbacks
, &slot
->reply_callback
.serial
, &slot
->reply_callback
);
570 if (slot
->reply_callback
.timeout
!= 0) {
571 r
= prioq_put(nl
->reply_callbacks_prioq
, &slot
->reply_callback
, &slot
->reply_callback
.prioq_idx
);
573 (void) hashmap_remove(nl
->reply_callbacks
, &slot
->reply_callback
.serial
);
578 /* Set this at last. Otherwise, some failures in above call the destroy callback but some do not. */
579 slot
->destroy_callback
= destroy_callback
;
589 int sd_netlink_call(sd_netlink
*rtnl
,
590 sd_netlink_message
*message
,
592 sd_netlink_message
**ret
) {
597 assert_return(rtnl
, -EINVAL
);
598 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
599 assert_return(message
, -EINVAL
);
601 r
= sd_netlink_send(rtnl
, message
, &serial
);
605 timeout
= calc_elapse(usec
);
611 for (i
= 0; i
< rtnl
->rqueue_size
; i
++) {
612 uint32_t received_serial
;
614 received_serial
= rtnl_message_get_serial(rtnl
->rqueue
[i
]);
616 if (received_serial
== serial
) {
617 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*incoming
= NULL
;
620 incoming
= rtnl
->rqueue
[i
];
622 /* found a match, remove from rqueue and return it */
623 memmove(rtnl
->rqueue
+ i
,rtnl
->rqueue
+ i
+ 1,
624 sizeof(sd_netlink_message
*) * (rtnl
->rqueue_size
- i
- 1));
627 r
= sd_netlink_message_get_errno(incoming
);
631 r
= sd_netlink_message_get_type(incoming
, &type
);
635 if (type
== NLMSG_DONE
) {
641 *ret
= TAKE_PTR(incoming
);
647 r
= socket_read_message(rtnl
);
651 /* received message, so try to process straight away */
657 n
= now(CLOCK_MONOTONIC
);
663 left
= (uint64_t) -1;
665 r
= rtnl_poll(rtnl
, true, left
);
673 int sd_netlink_get_events(sd_netlink
*rtnl
) {
674 assert_return(rtnl
, -EINVAL
);
675 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
677 if (rtnl
->rqueue_size
== 0)
683 int sd_netlink_get_timeout(sd_netlink
*rtnl
, uint64_t *timeout_usec
) {
684 struct reply_callback
*c
;
686 assert_return(rtnl
, -EINVAL
);
687 assert_return(timeout_usec
, -EINVAL
);
688 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
690 if (rtnl
->rqueue_size
> 0) {
695 c
= prioq_peek(rtnl
->reply_callbacks_prioq
);
697 *timeout_usec
= (uint64_t) -1;
701 *timeout_usec
= c
->timeout
;
706 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
707 sd_netlink
*rtnl
= userdata
;
712 r
= sd_netlink_process(rtnl
, NULL
);
719 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
720 sd_netlink
*rtnl
= userdata
;
725 r
= sd_netlink_process(rtnl
, NULL
);
732 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
733 sd_netlink
*rtnl
= userdata
;
740 e
= sd_netlink_get_events(rtnl
);
744 r
= sd_event_source_set_io_events(rtnl
->io_event_source
, e
);
748 r
= sd_netlink_get_timeout(rtnl
, &until
);
754 j
= sd_event_source_set_time(rtnl
->time_event_source
, until
);
759 r
= sd_event_source_set_enabled(rtnl
->time_event_source
, r
> 0);
766 int sd_netlink_attach_event(sd_netlink
*rtnl
, sd_event
*event
, int64_t priority
) {
769 assert_return(rtnl
, -EINVAL
);
770 assert_return(!rtnl
->event
, -EBUSY
);
772 assert(!rtnl
->io_event_source
);
773 assert(!rtnl
->time_event_source
);
776 rtnl
->event
= sd_event_ref(event
);
778 r
= sd_event_default(&rtnl
->event
);
783 r
= sd_event_add_io(rtnl
->event
, &rtnl
->io_event_source
, rtnl
->fd
, 0, io_callback
, rtnl
);
787 r
= sd_event_source_set_priority(rtnl
->io_event_source
, priority
);
791 r
= sd_event_source_set_description(rtnl
->io_event_source
, "rtnl-receive-message");
795 r
= sd_event_source_set_prepare(rtnl
->io_event_source
, prepare_callback
);
799 r
= sd_event_add_time(rtnl
->event
, &rtnl
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, rtnl
);
803 r
= sd_event_source_set_priority(rtnl
->time_event_source
, priority
);
807 r
= sd_event_source_set_description(rtnl
->time_event_source
, "rtnl-timer");
814 sd_netlink_detach_event(rtnl
);
818 int sd_netlink_detach_event(sd_netlink
*rtnl
) {
819 assert_return(rtnl
, -EINVAL
);
820 assert_return(rtnl
->event
, -ENXIO
);
822 rtnl
->io_event_source
= sd_event_source_unref(rtnl
->io_event_source
);
824 rtnl
->time_event_source
= sd_event_source_unref(rtnl
->time_event_source
);
826 rtnl
->event
= sd_event_unref(rtnl
->event
);
831 int sd_netlink_add_match(
833 sd_netlink_slot
**ret_slot
,
835 sd_netlink_message_handler_t callback
,
836 sd_netlink_destroy_t destroy_callback
,
838 const char *description
) {
839 _cleanup_free_ sd_netlink_slot
*slot
= NULL
;
842 assert_return(rtnl
, -EINVAL
);
843 assert_return(callback
, -EINVAL
);
844 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
846 r
= netlink_slot_allocate(rtnl
, !ret_slot
, NETLINK_MATCH_CALLBACK
, sizeof(struct match_callback
), userdata
, description
, &slot
);
850 slot
->match_callback
.callback
= callback
;
851 slot
->match_callback
.type
= type
;
856 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_LINK
);
863 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_IFADDR
);
867 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_IFADDR
);
874 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_ROUTE
);
878 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_ROUTE
);
884 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_RULE
);
888 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_RULE
);
896 LIST_PREPEND(match_callbacks
, rtnl
->match_callbacks
, &slot
->match_callback
);
898 /* Set this at last. Otherwise, some failures in above call the destroy callback but some do not. */
899 slot
->destroy_callback
= destroy_callback
;