1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 #include <linux/netlink.h>
10 #include "sd-netlink.h"
13 #include "netlink-types.h"
17 #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
19 #define RTNL_WQUEUE_MAX 1024
20 #define RTNL_RQUEUE_MAX 64*1024
22 #define RTNL_CONTAINER_DEPTH 32
24 struct reply_callback
{
25 sd_netlink_message_handler_t callback
;
32 struct match_callback
{
33 sd_netlink_message_handler_t callback
;
37 LIST_FIELDS(struct match_callback
, match_callbacks
);
47 struct sockaddr_nl nl
;
52 Hashmap
*broadcast_group_refs
;
53 bool broadcast_group_dont_leave
:1; /* until we can rely on 4.2 */
55 sd_netlink_message
**rqueue
;
57 size_t rqueue_allocated
;
59 sd_netlink_message
**rqueue_partial
;
60 unsigned rqueue_partial_size
;
61 size_t rqueue_partial_allocated
;
63 struct nlmsghdr
*rbuffer
;
64 size_t rbuffer_allocated
;
70 struct Prioq
*reply_callbacks_prioq
;
71 Hashmap
*reply_callbacks
;
73 LIST_HEAD(struct match_callback
, match_callbacks
);
77 sd_event_source
*io_event_source
;
78 sd_event_source
*time_event_source
;
79 sd_event_source
*exit_event_source
;
83 struct netlink_attribute
{
84 size_t offset
; /* offset from hdr to attribute */
89 struct netlink_container
{
90 const struct NLTypeSystem
*type_system
; /* the type system of the container */
91 size_t offset
; /* offset from hdr to the start of the container */
92 struct netlink_attribute
*attributes
;
93 unsigned short n_attributes
; /* number of attributes in container */
96 struct sd_netlink_message
{
103 struct nlmsghdr
*hdr
;
104 struct netlink_container containers
[RTNL_CONTAINER_DEPTH
];
105 unsigned n_containers
; /* number of containers */
109 sd_netlink_message
*next
; /* next in a chain of multi-part messages */
112 int message_new(sd_netlink
*rtnl
, sd_netlink_message
**ret
, uint16_t type
);
113 int message_new_empty(sd_netlink
*rtnl
, sd_netlink_message
**ret
);
115 int netlink_open_family(sd_netlink
**ret
, int family
);
117 int socket_open(int family
);
118 int socket_bind(sd_netlink
*nl
);
119 int socket_broadcast_group_ref(sd_netlink
*nl
, unsigned group
);
120 int socket_broadcast_group_unref(sd_netlink
*nl
, unsigned group
);
121 int socket_write_message(sd_netlink
*nl
, sd_netlink_message
*m
);
122 int socket_read_message(sd_netlink
*nl
);
124 int rtnl_rqueue_make_room(sd_netlink
*rtnl
);
125 int rtnl_rqueue_partial_make_room(sd_netlink
*rtnl
);
127 /* Make sure callbacks don't destroy the rtnl connection */
128 #define NETLINK_DONT_DESTROY(rtnl) \
129 _cleanup_(sd_netlink_unrefp) _unused_ sd_netlink *_dont_destroy_##rtnl = sd_netlink_ref(rtnl)