1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2013 Tom Gundersen <teg@jklm.no>
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/netlink.h>
25 #include "sd-netlink.h"
28 #include "netlink-types.h"
32 #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
34 #define RTNL_WQUEUE_MAX 1024
35 #define RTNL_RQUEUE_MAX 64*1024
37 #define RTNL_CONTAINER_DEPTH 32
39 struct reply_callback
{
40 sd_netlink_message_handler_t callback
;
47 struct match_callback
{
48 sd_netlink_message_handler_t callback
;
52 LIST_FIELDS(struct match_callback
, match_callbacks
);
62 struct sockaddr_nl nl
;
65 Hashmap
*broadcast_group_refs
;
66 bool broadcast_group_dont_leave
:1; /* until we can rely on 4.2 */
68 sd_netlink_message
**rqueue
;
70 size_t rqueue_allocated
;
72 sd_netlink_message
**rqueue_partial
;
73 unsigned rqueue_partial_size
;
74 size_t rqueue_partial_allocated
;
76 struct nlmsghdr
*rbuffer
;
77 size_t rbuffer_allocated
;
83 struct Prioq
*reply_callbacks_prioq
;
84 Hashmap
*reply_callbacks
;
86 LIST_HEAD(struct match_callback
, match_callbacks
);
90 sd_event_source
*io_event_source
;
91 sd_event_source
*time_event_source
;
92 sd_event_source
*exit_event_source
;
96 struct netlink_attribute
{
97 size_t offset
; /* offset from hdr to attribute */
102 struct netlink_container
{
103 const struct NLTypeSystem
*type_system
; /* the type system of the container */
104 size_t offset
; /* offset from hdr to the start of the container */
105 struct netlink_attribute
*attributes
;
106 unsigned short n_attributes
; /* number of attributes in container */
109 struct sd_netlink_message
{
114 struct nlmsghdr
*hdr
;
115 struct netlink_container containers
[RTNL_CONTAINER_DEPTH
];
116 unsigned n_containers
; /* number of containers */
120 sd_netlink_message
*next
; /* next in a chain of multi-part messages */
123 int message_new(sd_netlink
*rtnl
, sd_netlink_message
**ret
, uint16_t type
);
124 int message_new_empty(sd_netlink
*rtnl
, sd_netlink_message
**ret
);
126 int socket_open(int family
);
127 int socket_bind(sd_netlink
*nl
);
128 int socket_broadcast_group_ref(sd_netlink
*nl
, unsigned group
);
129 int socket_broadcast_group_unref(sd_netlink
*nl
, unsigned group
);
130 int socket_write_message(sd_netlink
*nl
, sd_netlink_message
*m
);
131 int socket_read_message(sd_netlink
*nl
);
133 int rtnl_rqueue_make_room(sd_netlink
*rtnl
);
134 int rtnl_rqueue_partial_make_room(sd_netlink
*rtnl
);
136 /* Make sure callbacks don't destroy the rtnl connection */
137 #define NETLINK_DONT_DESTROY(rtnl) \
138 _cleanup_(sd_netlink_unrefp) _unused_ sd_netlink *_dont_destroy_##rtnl = sd_netlink_ref(rtnl)