]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-internal.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-internal.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2013 Tom Gundersen <teg@jklm.no>
8
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.
13
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.
18
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/>.
21 ***/
22
23 #include <linux/netlink.h>
24
25 #include "sd-netlink.h"
26
27 #include "list.h"
28 #include "netlink-types.h"
29 #include "prioq.h"
30 #include "refcnt.h"
31
32 #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
33
34 #define RTNL_WQUEUE_MAX 1024
35 #define RTNL_RQUEUE_MAX 64*1024
36
37 #define RTNL_CONTAINER_DEPTH 32
38
39 struct reply_callback {
40 sd_netlink_message_handler_t callback;
41 void *userdata;
42 usec_t timeout;
43 uint64_t serial;
44 unsigned prioq_idx;
45 };
46
47 struct match_callback {
48 sd_netlink_message_handler_t callback;
49 uint16_t type;
50 void *userdata;
51
52 LIST_FIELDS(struct match_callback, match_callbacks);
53 };
54
55 struct sd_netlink {
56 RefCount n_ref;
57
58 int fd;
59
60 union {
61 struct sockaddr sa;
62 struct sockaddr_nl nl;
63 } sockaddr;
64
65 Hashmap *broadcast_group_refs;
66 bool broadcast_group_dont_leave:1; /* until we can rely on 4.2 */
67
68 sd_netlink_message **rqueue;
69 unsigned rqueue_size;
70 size_t rqueue_allocated;
71
72 sd_netlink_message **rqueue_partial;
73 unsigned rqueue_partial_size;
74 size_t rqueue_partial_allocated;
75
76 struct nlmsghdr *rbuffer;
77 size_t rbuffer_allocated;
78
79 bool processing:1;
80
81 uint32_t serial;
82
83 struct Prioq *reply_callbacks_prioq;
84 Hashmap *reply_callbacks;
85
86 LIST_HEAD(struct match_callback, match_callbacks);
87
88 pid_t original_pid;
89
90 sd_event_source *io_event_source;
91 sd_event_source *time_event_source;
92 sd_event_source *exit_event_source;
93 sd_event *event;
94 };
95
96 struct netlink_attribute {
97 size_t offset; /* offset from hdr to attribute */
98 bool nested:1;
99 bool net_byteorder:1;
100 };
101
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 */
107 };
108
109 struct sd_netlink_message {
110 RefCount n_ref;
111
112 sd_netlink *rtnl;
113
114 struct nlmsghdr *hdr;
115 struct netlink_container containers[RTNL_CONTAINER_DEPTH];
116 unsigned n_containers; /* number of containers */
117 bool sealed:1;
118 bool broadcast:1;
119
120 sd_netlink_message *next; /* next in a chain of multi-part messages */
121 };
122
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);
125
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);
132
133 int rtnl_rqueue_make_room(sd_netlink *rtnl);
134 int rtnl_rqueue_partial_make_room(sd_netlink *rtnl);
135
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)