2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/socket.h>
23 #include "sd-netlink.h"
25 #include "alloc-util.h"
30 #include "netlink-internal.h"
31 #include "netlink-util.h"
32 #include "socket-util.h"
35 static int sd_netlink_new(sd_netlink
**ret
) {
36 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
38 assert_return(ret
, -EINVAL
);
40 rtnl
= new0(sd_netlink
, 1);
44 rtnl
->n_ref
= REFCNT_INIT
;
46 rtnl
->sockaddr
.nl
.nl_family
= AF_NETLINK
;
47 rtnl
->original_pid
= getpid();
49 LIST_HEAD_INIT(rtnl
->match_callbacks
);
51 /* We guarantee that the read buffer has at least space for
53 if (!greedy_realloc((void**)&rtnl
->rbuffer
, &rtnl
->rbuffer_allocated
,
54 sizeof(struct nlmsghdr
), sizeof(uint8_t)))
57 /* Change notification responses have sequence 0, so we must
58 * start our request sequence numbers at 1, or we may confuse our
59 * responses with notifications from the kernel */
68 int sd_netlink_new_from_netlink(sd_netlink
**ret
, int fd
) {
69 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
73 assert_return(ret
, -EINVAL
);
75 r
= sd_netlink_new(&rtnl
);
79 addrlen
= sizeof(rtnl
->sockaddr
);
81 r
= getsockname(fd
, &rtnl
->sockaddr
.sa
, &addrlen
);
85 if (rtnl
->sockaddr
.nl
.nl_family
!= AF_NETLINK
)
96 static bool rtnl_pid_changed(sd_netlink
*rtnl
) {
99 /* We don't support people creating an rtnl connection and
100 * keeping it around over a fork(). Let's complain. */
102 return rtnl
->original_pid
!= getpid();
105 int sd_netlink_open_fd(sd_netlink
**ret
, int fd
) {
106 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
109 assert_return(ret
, -EINVAL
);
110 assert_return(fd
>= 0, -EBADF
);
112 r
= sd_netlink_new(&rtnl
);
118 r
= socket_bind(rtnl
);
120 rtnl
->fd
= -1; /* on failure, the caller remains owner of the fd, hence don't close it here */
130 int sd_netlink_open(sd_netlink
**ret
) {
131 _cleanup_close_
int fd
= -1;
134 fd
= socket_open(NETLINK_ROUTE
);
138 r
= sd_netlink_open_fd(ret
, fd
);
147 int sd_netlink_inc_rcvbuf(sd_netlink
*rtnl
, size_t size
) {
148 assert_return(rtnl
, -EINVAL
);
149 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
151 return fd_inc_rcvbuf(rtnl
->fd
, size
);
154 sd_netlink
*sd_netlink_ref(sd_netlink
*rtnl
) {
155 assert_return(rtnl
, NULL
);
156 assert_return(!rtnl_pid_changed(rtnl
), NULL
);
159 assert_se(REFCNT_INC(rtnl
->n_ref
) >= 2);
164 sd_netlink
*sd_netlink_unref(sd_netlink
*rtnl
) {
168 assert_return(!rtnl_pid_changed(rtnl
), NULL
);
170 if (REFCNT_DEC(rtnl
->n_ref
) == 0) {
171 struct match_callback
*f
;
174 for (i
= 0; i
< rtnl
->rqueue_size
; i
++)
175 sd_netlink_message_unref(rtnl
->rqueue
[i
]);
178 for (i
= 0; i
< rtnl
->rqueue_partial_size
; i
++)
179 sd_netlink_message_unref(rtnl
->rqueue_partial
[i
]);
180 free(rtnl
->rqueue_partial
);
184 hashmap_free_free(rtnl
->reply_callbacks
);
185 prioq_free(rtnl
->reply_callbacks_prioq
);
187 sd_event_source_unref(rtnl
->io_event_source
);
188 sd_event_source_unref(rtnl
->time_event_source
);
189 sd_event_unref(rtnl
->event
);
191 while ((f
= rtnl
->match_callbacks
)) {
192 sd_netlink_remove_match(rtnl
, f
->type
, f
->callback
, f
->userdata
);
195 hashmap_free(rtnl
->broadcast_group_refs
);
197 safe_close(rtnl
->fd
);
204 static void rtnl_seal_message(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
206 assert(!rtnl_pid_changed(rtnl
));
210 /* don't use seq == 0, as that is used for broadcasts, so we
211 would get confused by replies to such messages */
212 m
->hdr
->nlmsg_seq
= rtnl
->serial
++ ? : rtnl
->serial
++;
214 rtnl_message_seal(m
);
219 int sd_netlink_send(sd_netlink
*nl
,
220 sd_netlink_message
*message
,
224 assert_return(nl
, -EINVAL
);
225 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
226 assert_return(message
, -EINVAL
);
227 assert_return(!message
->sealed
, -EPERM
);
229 rtnl_seal_message(nl
, message
);
231 r
= socket_write_message(nl
, message
);
236 *serial
= rtnl_message_get_serial(message
);
241 int rtnl_rqueue_make_room(sd_netlink
*rtnl
) {
244 if (rtnl
->rqueue_size
>= RTNL_RQUEUE_MAX
) {
245 log_debug("rtnl: exhausted the read queue size (%d)", RTNL_RQUEUE_MAX
);
249 if (!GREEDY_REALLOC(rtnl
->rqueue
, rtnl
->rqueue_allocated
, rtnl
->rqueue_size
+ 1))
255 int rtnl_rqueue_partial_make_room(sd_netlink
*rtnl
) {
258 if (rtnl
->rqueue_partial_size
>= RTNL_RQUEUE_MAX
) {
259 log_debug("rtnl: exhausted the partial read queue size (%d)", RTNL_RQUEUE_MAX
);
263 if (!GREEDY_REALLOC(rtnl
->rqueue_partial
, rtnl
->rqueue_partial_allocated
,
264 rtnl
->rqueue_partial_size
+ 1))
270 static int dispatch_rqueue(sd_netlink
*rtnl
, sd_netlink_message
**message
) {
276 if (rtnl
->rqueue_size
<= 0) {
277 /* Try to read a new message */
278 r
= socket_read_message(rtnl
);
279 if (r
== -ENOBUFS
) { /* FIXME: ignore buffer overruns for now */
280 log_debug_errno(r
, "Got ENOBUFS from netlink socket, ignoring.");
287 /* Dispatch a queued message */
288 *message
= rtnl
->rqueue
[0];
290 memmove(rtnl
->rqueue
, rtnl
->rqueue
+ 1, sizeof(sd_netlink_message
*) * rtnl
->rqueue_size
);
295 static int process_timeout(sd_netlink
*rtnl
) {
296 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
297 struct reply_callback
*c
;
303 c
= prioq_peek(rtnl
->reply_callbacks_prioq
);
307 n
= now(CLOCK_MONOTONIC
);
311 r
= rtnl_message_new_synthetic_error(-ETIMEDOUT
, c
->serial
, &m
);
315 assert_se(prioq_pop(rtnl
->reply_callbacks_prioq
) == c
);
316 hashmap_remove(rtnl
->reply_callbacks
, &c
->serial
);
318 r
= c
->callback(rtnl
, m
, c
->userdata
);
320 log_debug_errno(r
, "sd-netlink: timedout callback failed: %m");
327 static int process_reply(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
328 _cleanup_free_
struct reply_callback
*c
= NULL
;
336 serial
= rtnl_message_get_serial(m
);
337 c
= hashmap_remove(rtnl
->reply_callbacks
, &serial
);
342 prioq_remove(rtnl
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
344 r
= sd_netlink_message_get_type(m
, &type
);
348 if (type
== NLMSG_DONE
)
351 r
= c
->callback(rtnl
, m
, c
->userdata
);
353 log_debug_errno(r
, "sd-netlink: callback failed: %m");
358 static int process_match(sd_netlink
*rtnl
, sd_netlink_message
*m
) {
359 struct match_callback
*c
;
366 r
= sd_netlink_message_get_type(m
, &type
);
370 LIST_FOREACH(match_callbacks
, c
, rtnl
->match_callbacks
) {
371 if (type
== c
->type
) {
372 r
= c
->callback(rtnl
, m
, c
->userdata
);
375 log_debug_errno(r
, "sd-netlink: match callback failed: %m");
385 static int process_running(sd_netlink
*rtnl
, sd_netlink_message
**ret
) {
386 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
391 r
= process_timeout(rtnl
);
395 r
= dispatch_rqueue(rtnl
, &m
);
401 if (sd_netlink_message_is_broadcast(m
)) {
402 r
= process_match(rtnl
, m
);
406 r
= process_reply(rtnl
, m
);
427 int sd_netlink_process(sd_netlink
*rtnl
, sd_netlink_message
**ret
) {
428 NETLINK_DONT_DESTROY(rtnl
);
431 assert_return(rtnl
, -EINVAL
);
432 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
433 assert_return(!rtnl
->processing
, -EBUSY
);
435 rtnl
->processing
= true;
436 r
= process_running(rtnl
, ret
);
437 rtnl
->processing
= false;
442 static usec_t
calc_elapse(uint64_t usec
) {
443 if (usec
== (uint64_t) -1)
447 usec
= RTNL_DEFAULT_TIMEOUT
;
449 return now(CLOCK_MONOTONIC
) + usec
;
452 static int rtnl_poll(sd_netlink
*rtnl
, bool need_more
, uint64_t timeout_usec
) {
453 struct pollfd p
[1] = {};
455 usec_t m
= USEC_INFINITY
;
460 e
= sd_netlink_get_events(rtnl
);
465 /* Caller wants more data, and doesn't care about
466 * what's been read or any other timeouts. */
470 /* Caller wants to process if there is something to
471 * process, but doesn't care otherwise */
473 r
= sd_netlink_get_timeout(rtnl
, &until
);
478 nw
= now(CLOCK_MONOTONIC
);
479 m
= until
> nw
? until
- nw
: 0;
483 if (timeout_usec
!= (uint64_t) -1 && (m
== (uint64_t) -1 || timeout_usec
< m
))
489 r
= ppoll(p
, 1, m
== (uint64_t) -1 ? NULL
: timespec_store(&ts
, m
), NULL
);
493 return r
> 0 ? 1 : 0;
496 int sd_netlink_wait(sd_netlink
*nl
, uint64_t timeout_usec
) {
497 assert_return(nl
, -EINVAL
);
498 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
500 if (nl
->rqueue_size
> 0)
503 return rtnl_poll(nl
, false, timeout_usec
);
506 static int timeout_compare(const void *a
, const void *b
) {
507 const struct reply_callback
*x
= a
, *y
= b
;
509 if (x
->timeout
!= 0 && y
->timeout
== 0)
512 if (x
->timeout
== 0 && y
->timeout
!= 0)
515 if (x
->timeout
< y
->timeout
)
518 if (x
->timeout
> y
->timeout
)
524 int sd_netlink_call_async(sd_netlink
*nl
,
525 sd_netlink_message
*m
,
526 sd_netlink_message_handler_t callback
,
530 struct reply_callback
*c
;
534 assert_return(nl
, -EINVAL
);
535 assert_return(m
, -EINVAL
);
536 assert_return(callback
, -EINVAL
);
537 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
539 r
= hashmap_ensure_allocated(&nl
->reply_callbacks
, &uint64_hash_ops
);
543 if (usec
!= (uint64_t) -1) {
544 r
= prioq_ensure_allocated(&nl
->reply_callbacks_prioq
, timeout_compare
);
549 c
= new0(struct reply_callback
, 1);
553 c
->callback
= callback
;
554 c
->userdata
= userdata
;
555 c
->timeout
= calc_elapse(usec
);
557 k
= sd_netlink_send(nl
, m
, &s
);
565 r
= hashmap_put(nl
->reply_callbacks
, &c
->serial
, c
);
571 if (c
->timeout
!= 0) {
572 r
= prioq_put(nl
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
575 sd_netlink_call_async_cancel(nl
, c
->serial
);
586 int sd_netlink_call_async_cancel(sd_netlink
*nl
, uint32_t serial
) {
587 struct reply_callback
*c
;
590 assert_return(nl
, -EINVAL
);
591 assert_return(serial
!= 0, -EINVAL
);
592 assert_return(!rtnl_pid_changed(nl
), -ECHILD
);
594 c
= hashmap_remove(nl
->reply_callbacks
, &s
);
599 prioq_remove(nl
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
605 int sd_netlink_call(sd_netlink
*rtnl
,
606 sd_netlink_message
*message
,
608 sd_netlink_message
**ret
) {
613 assert_return(rtnl
, -EINVAL
);
614 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
615 assert_return(message
, -EINVAL
);
617 r
= sd_netlink_send(rtnl
, message
, &serial
);
621 timeout
= calc_elapse(usec
);
627 for (i
= 0; i
< rtnl
->rqueue_size
; i
++) {
628 uint32_t received_serial
;
630 received_serial
= rtnl_message_get_serial(rtnl
->rqueue
[i
]);
632 if (received_serial
== serial
) {
633 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*incoming
= NULL
;
636 incoming
= rtnl
->rqueue
[i
];
638 /* found a match, remove from rqueue and return it */
639 memmove(rtnl
->rqueue
+ i
,rtnl
->rqueue
+ i
+ 1,
640 sizeof(sd_netlink_message
*) * (rtnl
->rqueue_size
- i
- 1));
643 r
= sd_netlink_message_get_errno(incoming
);
647 r
= sd_netlink_message_get_type(incoming
, &type
);
651 if (type
== NLMSG_DONE
) {
665 r
= socket_read_message(rtnl
);
669 /* received message, so try to process straight away */
675 n
= now(CLOCK_MONOTONIC
);
681 left
= (uint64_t) -1;
683 r
= rtnl_poll(rtnl
, true, left
);
691 int sd_netlink_get_events(sd_netlink
*rtnl
) {
692 assert_return(rtnl
, -EINVAL
);
693 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
695 if (rtnl
->rqueue_size
== 0)
701 int sd_netlink_get_timeout(sd_netlink
*rtnl
, uint64_t *timeout_usec
) {
702 struct reply_callback
*c
;
704 assert_return(rtnl
, -EINVAL
);
705 assert_return(timeout_usec
, -EINVAL
);
706 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
708 if (rtnl
->rqueue_size
> 0) {
713 c
= prioq_peek(rtnl
->reply_callbacks_prioq
);
715 *timeout_usec
= (uint64_t) -1;
719 *timeout_usec
= c
->timeout
;
724 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
725 sd_netlink
*rtnl
= userdata
;
730 r
= sd_netlink_process(rtnl
, NULL
);
737 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
738 sd_netlink
*rtnl
= userdata
;
743 r
= sd_netlink_process(rtnl
, NULL
);
750 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
751 sd_netlink
*rtnl
= userdata
;
758 e
= sd_netlink_get_events(rtnl
);
762 r
= sd_event_source_set_io_events(rtnl
->io_event_source
, e
);
766 r
= sd_netlink_get_timeout(rtnl
, &until
);
772 j
= sd_event_source_set_time(rtnl
->time_event_source
, until
);
777 r
= sd_event_source_set_enabled(rtnl
->time_event_source
, r
> 0);
784 int sd_netlink_attach_event(sd_netlink
*rtnl
, sd_event
*event
, int64_t priority
) {
787 assert_return(rtnl
, -EINVAL
);
788 assert_return(!rtnl
->event
, -EBUSY
);
790 assert(!rtnl
->io_event_source
);
791 assert(!rtnl
->time_event_source
);
794 rtnl
->event
= sd_event_ref(event
);
796 r
= sd_event_default(&rtnl
->event
);
801 r
= sd_event_add_io(rtnl
->event
, &rtnl
->io_event_source
, rtnl
->fd
, 0, io_callback
, rtnl
);
805 r
= sd_event_source_set_priority(rtnl
->io_event_source
, priority
);
809 r
= sd_event_source_set_description(rtnl
->io_event_source
, "rtnl-receive-message");
813 r
= sd_event_source_set_prepare(rtnl
->io_event_source
, prepare_callback
);
817 r
= sd_event_add_time(rtnl
->event
, &rtnl
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, rtnl
);
821 r
= sd_event_source_set_priority(rtnl
->time_event_source
, priority
);
825 r
= sd_event_source_set_description(rtnl
->time_event_source
, "rtnl-timer");
832 sd_netlink_detach_event(rtnl
);
836 int sd_netlink_detach_event(sd_netlink
*rtnl
) {
837 assert_return(rtnl
, -EINVAL
);
838 assert_return(rtnl
->event
, -ENXIO
);
840 rtnl
->io_event_source
= sd_event_source_unref(rtnl
->io_event_source
);
842 rtnl
->time_event_source
= sd_event_source_unref(rtnl
->time_event_source
);
844 rtnl
->event
= sd_event_unref(rtnl
->event
);
849 int sd_netlink_add_match(sd_netlink
*rtnl
,
851 sd_netlink_message_handler_t callback
,
853 _cleanup_free_
struct match_callback
*c
= NULL
;
856 assert_return(rtnl
, -EINVAL
);
857 assert_return(callback
, -EINVAL
);
858 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
860 c
= new0(struct match_callback
, 1);
864 c
->callback
= callback
;
866 c
->userdata
= userdata
;
871 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_LINK
);
878 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_IFADDR
);
882 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_IFADDR
);
889 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV4_ROUTE
);
893 r
= socket_broadcast_group_ref(rtnl
, RTNLGRP_IPV6_ROUTE
);
901 LIST_PREPEND(match_callbacks
, rtnl
->match_callbacks
, c
);
908 int sd_netlink_remove_match(sd_netlink
*rtnl
,
910 sd_netlink_message_handler_t callback
,
912 struct match_callback
*c
;
915 assert_return(rtnl
, -EINVAL
);
916 assert_return(callback
, -EINVAL
);
917 assert_return(!rtnl_pid_changed(rtnl
), -ECHILD
);
919 LIST_FOREACH(match_callbacks
, c
, rtnl
->match_callbacks
)
920 if (c
->callback
== callback
&& c
->type
== type
&& c
->userdata
== userdata
) {
921 LIST_REMOVE(match_callbacks
, rtnl
->match_callbacks
, c
);
927 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_LINK
);
934 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV4_IFADDR
);
938 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV6_IFADDR
);
945 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV4_ROUTE
);
949 r
= socket_broadcast_group_unref(rtnl
, RTNLGRP_IPV6_ROUTE
);