1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2013 Tom Gundersen <teg@jklm.no>
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <linux/netlink.h>
26 #include "sd-netlink.h"
29 #include "netlink-types.h"
33 #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
35 #define RTNL_WQUEUE_MAX 1024
36 #define RTNL_RQUEUE_MAX 64*1024
38 #define RTNL_CONTAINER_DEPTH 32
40 struct reply_callback
{
41 sd_netlink_message_handler_t callback
;
48 struct match_callback
{
49 sd_netlink_message_handler_t callback
;
53 LIST_FIELDS(struct match_callback
, match_callbacks
);
63 struct sockaddr_nl nl
;
66 Hashmap
*broadcast_group_refs
;
67 bool broadcast_group_dont_leave
:1; /* until we can rely on 4.2 */
69 sd_netlink_message
**rqueue
;
71 size_t rqueue_allocated
;
73 sd_netlink_message
**rqueue_partial
;
74 unsigned rqueue_partial_size
;
75 size_t rqueue_partial_allocated
;
77 struct nlmsghdr
*rbuffer
;
78 size_t rbuffer_allocated
;
84 struct Prioq
*reply_callbacks_prioq
;
85 Hashmap
*reply_callbacks
;
87 LIST_HEAD(struct match_callback
, match_callbacks
);
91 sd_event_source
*io_event_source
;
92 sd_event_source
*time_event_source
;
93 sd_event_source
*exit_event_source
;
97 struct netlink_attribute
{
98 size_t offset
; /* offset from hdr to attribute */
100 bool net_byteorder
:1;
103 struct netlink_container
{
104 const struct NLTypeSystem
*type_system
; /* the type system of the container */
105 size_t offset
; /* offset from hdr to the start of the container */
106 struct netlink_attribute
*attributes
;
107 unsigned short n_attributes
; /* number of attributes in container */
110 struct sd_netlink_message
{
115 struct nlmsghdr
*hdr
;
116 struct netlink_container containers
[RTNL_CONTAINER_DEPTH
];
117 unsigned n_containers
; /* number of containers */
121 sd_netlink_message
*next
; /* next in a chain of multi-part messages */
124 int message_new(sd_netlink
*rtnl
, sd_netlink_message
**ret
, uint16_t type
);
125 int message_new_empty(sd_netlink
*rtnl
, sd_netlink_message
**ret
);
127 int socket_open(int family
);
128 int socket_bind(sd_netlink
*nl
);
129 int socket_broadcast_group_ref(sd_netlink
*nl
, unsigned group
);
130 int socket_broadcast_group_unref(sd_netlink
*nl
, unsigned group
);
131 int socket_write_message(sd_netlink
*nl
, sd_netlink_message
*m
);
132 int socket_read_message(sd_netlink
*nl
);
134 int rtnl_rqueue_make_room(sd_netlink
*rtnl
);
135 int rtnl_rqueue_partial_make_room(sd_netlink
*rtnl
);
137 /* Make sure callbacks don't destroy the rtnl connection */
138 #define RTNL_DONT_DESTROY(rtnl) \
139 _cleanup_netlink_unref_ _unused_ sd_netlink *_dont_destroy_##rtnl = sd_netlink_ref(rtnl)