1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Tom Gundersen <teg@jklm.no>
7 #include <sys/socket.h>
9 #include "sd-netlink.h"
11 #include "alloc-util.h"
16 #include "netlink-internal.h"
17 #include "netlink-util.h"
18 #include "process-util.h"
19 #include "socket-util.h"
22 static int sd_netlink_new(sd_netlink
**ret
) {
23 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
25 assert_return(ret
, -EINVAL
);
27 rtnl
= new0(sd_netlink
, 1);
31 rtnl
->n_ref
= REFCNT_INIT
;
33 rtnl
->sockaddr
.nl
.nl_family
= AF_NETLINK
;
34 rtnl
->original_pid
= getpid_cached();
37 LIST_HEAD_INIT(rtnl
->match_callbacks
);
39 /* We guarantee that the read buffer has at least space for
41 if (!greedy_realloc((void**)&rtnl
->rbuffer
, &rtnl
->rbuffer_allocated
,
42 sizeof(struct nlmsghdr
), sizeof(uint8_t)))
45 /* Change notification responses have sequence 0, so we must
46 * start our request sequence numbers at 1, or we may confuse our
47 * responses with notifications from the kernel */
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 sd_netlink
*sd_netlink_ref(sd_netlink
*rtnl
) {
153 assert_return(rtnl
, NULL
);
154 assert_return(!rtnl_pid_changed(rtnl
), NULL
);
157 assert_se(REFCNT_INC(rtnl
->n_ref
) >= 2);
162 sd_netlink
*sd_netlink_unref(sd_netlink
*rtnl
) {
166 assert_return(!rtnl_pid_changed(rtnl
), NULL
);
168 if (REFCNT_DEC(rtnl
->n_ref
) == 0) {
169 struct match_callback
*f
;
172 for (i
= 0; i
< rtnl
->rqueue_size
; i
++)
173 sd_netlink_message_unref(rtnl
->rqueue
[i
]);
176 for (i
= 0; i
< rtnl
->rqueue_partial_size
; i
++)
177 sd_netlink_message_unref(rtnl
->rqueue_partial
[i
]);
178 free(rtnl
->rqueue_partial
);
182 hashmap_free_free(rtnl
->reply_callbacks
);
183 prioq_free(rtnl
->reply_callbacks_prioq
);
185 sd_event_source_unref(rtnl
->io_event_source
);
186 sd_event_source_unref(rtnl
->time_event_source
);
187 sd_event_unref(rtnl
->event
);
189 while ((f
= rtnl
->match_callbacks
)) {
190 sd_netlink_remove_match(rtnl
, f
->type
, f
->callback
, f
->userdata
);
193 hashmap_free(rtnl
->broadcast_group_refs
);
195 safe_close(rtnl
->fd
);
202 static void rtnl_seal_message(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
204 assert(!rtnl_pid_changed(rtnl
));
208 /* don't use seq == 0, as that is used for broadcasts, so we
209 would get confused by replies to such messages */
210 m
->hdr
->nlmsg_seq
= rtnl
->serial
++ ? : rtnl
->serial
++;
212 rtnl_message_seal(m
);
217 int sd_netlink_send(sd_netlink
*nl
,
218 sd_netlink_message
*message
,
222 assert_return(nl
, -EINVAL
);
223 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
224 assert_return(message
, -EINVAL
);
225 assert_return(!message
->sealed
, -EPERM
);
227 rtnl_seal_message(nl
, message
);
229 r
= socket_write_message(nl
, message
);
234 *serial
= rtnl_message_get_serial(message
);
239 int rtnl_rqueue_make_room(sd_netlink
*rtnl
) {
242 if (rtnl
->rqueue_size
>= RTNL_RQUEUE_MAX
) {
243 log_debug("rtnl: exhausted the read queue size (%d)", RTNL_RQUEUE_MAX
);
247 if (!GREEDY_REALLOC(rtnl
->rqueue
, rtnl
->rqueue_allocated
, rtnl
->rqueue_size
+ 1))
253 int rtnl_rqueue_partial_make_room(sd_netlink
*rtnl
) {
256 if (rtnl
->rqueue_partial_size
>= RTNL_RQUEUE_MAX
) {
257 log_debug("rtnl: exhausted the partial read queue size (%d)", RTNL_RQUEUE_MAX
);
261 if (!GREEDY_REALLOC(rtnl
->rqueue_partial
, rtnl
->rqueue_partial_allocated
,
262 rtnl
->rqueue_partial_size
+ 1))
268 static int dispatch_rqueue(sd_netlink
*rtnl
, sd_netlink_message
**message
) {
274 if (rtnl
->rqueue_size
<= 0) {
275 /* Try to read a new message */
276 r
= socket_read_message(rtnl
);
277 if (r
== -ENOBUFS
) { /* FIXME: ignore buffer overruns for now */
278 log_debug_errno(r
, "Got ENOBUFS from netlink socket, ignoring.");
285 /* Dispatch a queued message */
286 *message
= rtnl
->rqueue
[0];
288 memmove(rtnl
->rqueue
, rtnl
->rqueue
+ 1, sizeof(sd_netlink_message
*) * rtnl
->rqueue_size
);
293 static int process_timeout(sd_netlink
*rtnl
) {
294 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
295 struct reply_callback
*c
;
301 c
= prioq_peek(rtnl
->reply_callbacks_prioq
);
305 n
= now(CLOCK_MONOTONIC
);
309 r
= rtnl_message_new_synthetic_error(rtnl
, -ETIMEDOUT
, c
->serial
, &m
);
313 assert_se(prioq_pop(rtnl
->reply_callbacks_prioq
) == c
);
314 hashmap_remove(rtnl
->reply_callbacks
, &c
->serial
);
316 r
= c
->callback(rtnl
, m
, c
->userdata
);
318 log_debug_errno(r
, "sd-netlink: timedout callback failed: %m");
325 static int process_reply(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
326 _cleanup_free_
struct reply_callback
*c
= NULL
;
334 serial
= rtnl_message_get_serial(m
);
335 c
= hashmap_remove(rtnl
->reply_callbacks
, &serial
);
340 prioq_remove(rtnl
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
342 r
= sd_netlink_message_get_type(m
, &type
);
346 if (type
== NLMSG_DONE
)
349 r
= c
->callback(rtnl
, m
, c
->userdata
);
351 log_debug_errno(r
, "sd-netlink: callback failed: %m");
356 static int process_match(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
357 struct match_callback
*c
;
364 r
= sd_netlink_message_get_type(m
, &type
);
368 LIST_FOREACH(match_callbacks
, c
, rtnl
->match_callbacks
) {
369 if (type
== c
->type
) {
370 r
= c
->callback(rtnl
, m
, c
->userdata
);
373 log_debug_errno(r
, "sd-netlink: match callback failed: %m");
383 static int process_running(sd_netlink
*rtnl
, sd_netlink_message
**ret
) {
384 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
389 r
= process_timeout(rtnl
);
393 r
= dispatch_rqueue(rtnl
, &m
);
399 if (sd_netlink_message_is_broadcast(m
)) {
400 r
= process_match(rtnl
, m
);
404 r
= process_reply(rtnl
, m
);
424 int sd_netlink_process(sd_netlink
*rtnl
, sd_netlink_message
**ret
) {
425 NETLINK_DONT_DESTROY(rtnl
);
428 assert_return(rtnl
, -EINVAL
);
429 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
430 assert_return(!rtnl
->processing
, -EBUSY
);
432 rtnl
->processing
= true;
433 r
= process_running(rtnl
, ret
);
434 rtnl
->processing
= false;
439 static usec_t
calc_elapse(uint64_t usec
) {
440 if (usec
== (uint64_t) -1)
444 usec
= RTNL_DEFAULT_TIMEOUT
;
446 return now(CLOCK_MONOTONIC
) + usec
;
449 static int rtnl_poll(sd_netlink
*rtnl
, bool need_more
, uint64_t timeout_usec
) {
450 struct pollfd p
[1] = {};
452 usec_t m
= USEC_INFINITY
;
457 e
= sd_netlink_get_events(rtnl
);
462 /* Caller wants more data, and doesn't care about
463 * what's been read or any other timeouts. */
467 /* Caller wants to process if there is something to
468 * process, but doesn't care otherwise */
470 r
= sd_netlink_get_timeout(rtnl
, &until
);
475 nw
= now(CLOCK_MONOTONIC
);
476 m
= until
> nw
? until
- nw
: 0;
480 if (timeout_usec
!= (uint64_t) -1 && (m
== (uint64_t) -1 || timeout_usec
< m
))
486 r
= ppoll(p
, 1, m
== (uint64_t) -1 ? NULL
: timespec_store(&ts
, m
), NULL
);
490 return r
> 0 ? 1 : 0;
493 int sd_netlink_wait(sd_netlink
*nl
, uint64_t timeout_usec
) {
494 assert_return(nl
, -EINVAL
);
495 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
497 if (nl
->rqueue_size
> 0)
500 return rtnl_poll(nl
, false, timeout_usec
);
503 static int timeout_compare(const void *a
, const void *b
) {
504 const struct reply_callback
*x
= a
, *y
= b
;
506 if (x
->timeout
!= 0 && y
->timeout
== 0)
509 if (x
->timeout
== 0 && y
->timeout
!= 0)
512 if (x
->timeout
< y
->timeout
)
515 if (x
->timeout
> y
->timeout
)
521 int sd_netlink_call_async(sd_netlink
*nl
,
522 sd_netlink_message
*m
,
523 sd_netlink_message_handler_t callback
,
527 struct reply_callback
*c
;
531 assert_return(nl
, -EINVAL
);
532 assert_return(m
, -EINVAL
);
533 assert_return(callback
, -EINVAL
);
534 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
536 r
= hashmap_ensure_allocated(&nl
->reply_callbacks
, &uint64_hash_ops
);
540 if (usec
!= (uint64_t) -1) {
541 r
= prioq_ensure_allocated(&nl
->reply_callbacks_prioq
, timeout_compare
);
546 c
= new0(struct reply_callback
, 1);
550 c
->callback
= callback
;
551 c
->userdata
= userdata
;
552 c
->timeout
= calc_elapse(usec
);
554 k
= sd_netlink_send(nl
, m
, &s
);
562 r
= hashmap_put(nl
->reply_callbacks
, &c
->serial
, c
);
568 if (c
->timeout
!= 0) {
569 r
= prioq_put(nl
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
572 sd_netlink_call_async_cancel(nl
, c
->serial
);
583 int sd_netlink_call_async_cancel(sd_netlink
*nl
, uint32_t serial
) {
584 struct reply_callback
*c
;
587 assert_return(nl
, -EINVAL
);
588 assert_return(serial
!= 0, -EINVAL
);
589 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
591 c
= hashmap_remove(nl
->reply_callbacks
, &s
);
596 prioq_remove(nl
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
602 int sd_netlink_call(sd_netlink
*rtnl
,
603 sd_netlink_message
*message
,
605 sd_netlink_message
**ret
) {
610 assert_return(rtnl
, -EINVAL
);
611 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
612 assert_return(message
, -EINVAL
);
614 r
= sd_netlink_send(rtnl
, message
, &serial
);
618 timeout
= calc_elapse(usec
);
624 for (i
= 0; i
< rtnl
->rqueue_size
; i
++) {
625 uint32_t received_serial
;
627 received_serial
= rtnl_message_get_serial(rtnl
->rqueue
[i
]);
629 if (received_serial
== serial
) {
630 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*incoming
= NULL
;
633 incoming
= rtnl
->rqueue
[i
];
635 /* found a match, remove from rqueue and return it */
636 memmove(rtnl
->rqueue
+ i
,rtnl
->rqueue
+ i
+ 1,
637 sizeof(sd_netlink_message
*) * (rtnl
->rqueue_size
- i
- 1));
640 r
= sd_netlink_message_get_errno(incoming
);
644 r
= sd_netlink_message_get_type(incoming
, &type
);
648 if (type
== NLMSG_DONE
) {
654 *ret
= TAKE_PTR(incoming
);
660 r
= socket_read_message(rtnl
);
664 /* received message, so try to process straight away */
670 n
= now(CLOCK_MONOTONIC
);
676 left
= (uint64_t) -1;
678 r
= rtnl_poll(rtnl
, true, left
);
686 int sd_netlink_get_events(sd_netlink
*rtnl
) {
687 assert_return(rtnl
, -EINVAL
);
688 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
690 if (rtnl
->rqueue_size
== 0)
696 int sd_netlink_get_timeout(sd_netlink
*rtnl
, uint64_t *timeout_usec
) {
697 struct reply_callback
*c
;
699 assert_return(rtnl
, -EINVAL
);
700 assert_return(timeout_usec
, -EINVAL
);
701 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
703 if (rtnl
->rqueue_size
> 0) {
708 c
= prioq_peek(rtnl
->reply_callbacks_prioq
);
710 *timeout_usec
= (uint64_t) -1;
714 *timeout_usec
= c
->timeout
;
719 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
720 sd_netlink
*rtnl
= userdata
;
725 r
= sd_netlink_process(rtnl
, NULL
);
732 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
733 sd_netlink
*rtnl
= userdata
;
738 r
= sd_netlink_process(rtnl
, NULL
);
745 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
746 sd_netlink
*rtnl
= userdata
;
753 e
= sd_netlink_get_events(rtnl
);
757 r
= sd_event_source_set_io_events(rtnl
->io_event_source
, e
);
761 r
= sd_netlink_get_timeout(rtnl
, &until
);
767 j
= sd_event_source_set_time(rtnl
->time_event_source
, until
);
772 r
= sd_event_source_set_enabled(rtnl
->time_event_source
, r
> 0);
779 int sd_netlink_attach_event(sd_netlink
*rtnl
, sd_event
*event
, int64_t priority
) {
782 assert_return(rtnl
, -EINVAL
);
783 assert_return(!rtnl
->event
, -EBUSY
);
785 assert(!rtnl
->io_event_source
);
786 assert(!rtnl
->time_event_source
);
789 rtnl
->event
= sd_event_ref(event
);
791 r
= sd_event_default(&rtnl
->event
);
796 r
= sd_event_add_io(rtnl
->event
, &rtnl
->io_event_source
, rtnl
->fd
, 0, io_callback
, rtnl
);
800 r
= sd_event_source_set_priority(rtnl
->io_event_source
, priority
);
804 r
= sd_event_source_set_description(rtnl
->io_event_source
, "rtnl-receive-message");
808 r
= sd_event_source_set_prepare(rtnl
->io_event_source
, prepare_callback
);
812 r
= sd_event_add_time(rtnl
->event
, &rtnl
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, rtnl
);
816 r
= sd_event_source_set_priority(rtnl
->time_event_source
, priority
);
820 r
= sd_event_source_set_description(rtnl
->time_event_source
, "rtnl-timer");
827 sd_netlink_detach_event(rtnl
);
831 int sd_netlink_detach_event(sd_netlink
*rtnl
) {
832 assert_return(rtnl
, -EINVAL
);
833 assert_return(rtnl
->event
, -ENXIO
);
835 rtnl
->io_event_source
= sd_event_source_unref(rtnl
->io_event_source
);
837 rtnl
->time_event_source
= sd_event_source_unref(rtnl
->time_event_source
);
839 rtnl
->event
= sd_event_unref(rtnl
->event
);
844 int sd_netlink_add_match(sd_netlink
*rtnl
,
846 sd_netlink_message_handler_t callback
,
848 _cleanup_free_
struct match_callback
*c
= NULL
;
851 assert_return(rtnl
, -EINVAL
);
852 assert_return(callback
, -EINVAL
);
853 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
855 c
= new0(struct match_callback
, 1);
859 c
->callback
= callback
;
861 c
->userdata
= userdata
;
866 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_LINK
);
873 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_IFADDR
);
877 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_IFADDR
);
884 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_ROUTE
);
888 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_ROUTE
);
894 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_RULE
);
898 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_RULE
);
906 LIST_PREPEND(match_callbacks
, rtnl
->match_callbacks
, c
);
913 int sd_netlink_remove_match(sd_netlink
*rtnl
,
915 sd_netlink_message_handler_t callback
,
917 struct match_callback
*c
;
920 assert_return(rtnl
, -EINVAL
);
921 assert_return(callback
, -EINVAL
);
922 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
924 LIST_FOREACH(match_callbacks
, c
, rtnl
->match_callbacks
)
925 if (c
->callback
== callback
&& c
->type
== type
&& c
->userdata
== userdata
) {
926 LIST_REMOVE(match_callbacks
, rtnl
->match_callbacks
, c
);
932 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_LINK
);
939 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV4_IFADDR
);
943 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV6_IFADDR
);
950 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV4_ROUTE
);
954 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV6_ROUTE
);