Debian Codegraph @

src/libsystemd/sd-netlink/netlink-internal.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <linux/netlink.h>
5 #include <sys/socket.h>
6
7 #include "sd-forward.h"
8 #include "list.h"
9 #include "netlink-types.h"
10
11 #define NETLINK_DEFAULT_TIMEOUT_USEC ((usec_t) (25 * USEC_PER_SEC))
12
13 #define NETLINK_RQUEUE_MAX 64*1024
14
15 #define NETLINK_CONTAINER_DEPTH 32
16
17 struct reply_callback {
18 sd_netlink_message_handler_t callback;
19 usec_t timeout;
20 uint32_t serial;
21 unsigned prioq_idx;
22 };
23
24 struct match_callback {
25 sd_netlink_message_handler_t callback;
26 uint32_t *groups;
27 size_t n_groups;
28 uint16_t type;
29 uint8_t cmd; /* used by genl */
30
31 LIST_FIELDS(struct match_callback, match_callbacks);
32 };
33
34 typedef enum NetlinkSlotType {
35 NETLINK_REPLY_CALLBACK,
36 NETLINK_MATCH_CALLBACK,
37 _NETLINK_SLOT_INVALID = -EINVAL,
38 } NetlinkSlotType;
39
40 typedef struct sd_netlink_slot {
41 unsigned n_ref;
42 NetlinkSlotType type:8;
43 bool floating;
44 sd_netlink *netlink;
45 void *userdata;
46 sd_netlink_destroy_t destroy_callback;
47
48 char *description;
49
50 LIST_FIELDS(sd_netlink_slot, slots);
51
52 union {
53 struct reply_callback reply_callback;
54 struct match_callback match_callback;
55 };
56 } sd_netlink_slot;
57
58 typedef struct NetlinkIgnoredSerial {
59 uint32_t serial;
60 usec_t timeout_usec; /* timestamp in CLOCK_MONOTONIC */
61 } NetlinkIgnoredSerial;
62
63 typedef struct sd_netlink {
64 unsigned n_ref;
65
66 int fd;
67
68 union {
69 struct sockaddr sa;
70 struct sockaddr_nl nl;
71 } sockaddr;
72
73 int protocol;
74
75 Hashmap *broadcast_group_refs;
76
77 OrderedSet *rqueue;
78 Hashmap *rqueue_by_serial;
79 Hashmap *rqueue_partial_by_serial;
80
81 struct nlmsghdr *rbuffer;
82
83 bool processing:1;
84
85 uint32_t serial;
86 Hashmap *ignored_serials;
87
88 struct Prioq *reply_callbacks_prioq;
89 Hashmap *reply_callbacks;
90
91 LIST_HEAD(struct match_callback, match_callbacks);
92
93 LIST_HEAD(sd_netlink_slot, slots);
94
95 pid_t original_pid;
96
97 sd_event_source *io_event_source;
98 sd_event_source *time_event_source;
99 sd_event_source *exit_event_source;
100 sd_event *event;
101
102 Hashmap *genl_family_by_name;
103 Hashmap *genl_family_by_id;
104 } sd_netlink;
105
106 struct netlink_attribute {
107 size_t offset; /* offset from hdr to attribute */
108 bool nested:1;
109 bool net_byteorder:1;
110 };
111
112 struct netlink_container {
113 const struct NLAPolicySet *policy_set; /* the policy set of the container */
114 size_t offset; /* offset from hdr to the start of the container */
115 struct netlink_attribute *attributes;
116 uint16_t max_attribute; /* the maximum attribute in container */
117 };
118
119 typedef struct sd_netlink_message {
120 unsigned n_ref;
121
122 int protocol;
123
124 struct nlmsghdr *hdr;
125 struct netlink_container containers[NETLINK_CONTAINER_DEPTH];
126 unsigned n_containers; /* number of containers */
127 uint32_t multicast_group;
128 bool sealed:1;
129
130 sd_netlink_message *next; /* next in a chain of multi-part messages */
131 } sd_netlink_message;
132
133 int message_new_empty(sd_netlink *nl, sd_netlink_message **ret);
134 int message_new_full(
135 sd_netlink *nl,
136 uint16_t nlmsg_type,
137 uint16_t nlmsg_flags,
138 const NLAPolicySet *policy_set,
139 size_t header_size,
140 sd_netlink_message **ret);
141 int message_new(sd_netlink *nl, sd_netlink_message **ret, uint16_t type, uint16_t flags);
142 int message_new_synthetic_error(sd_netlink *nl, int error, uint32_t serial, sd_netlink_message **ret);
143
144 static inline uint32_t message_get_serial(sd_netlink_message *m) {
145 assert(m);
146 return ASSERT_PTR(m->hdr)->nlmsg_seq;
147 }
148
149 void message_seal(sd_netlink_message *m);
150
151 int netlink_open_family(sd_netlink **ret, int family);
152 bool netlink_pid_changed(sd_netlink *nl);
153
154 int socket_bind(sd_netlink *nl);
155 int socket_broadcast_group_ref(sd_netlink *nl, unsigned group);
156 int socket_broadcast_group_unref(sd_netlink *nl, unsigned group);
157 int socket_write_message(sd_netlink *nl, sd_netlink_message *m);
158 int socket_read_message(sd_netlink *nl);
159
160 int netlink_add_match_internal(
161 sd_netlink *nl,
162 sd_netlink_slot **ret_slot,
163 const uint32_t *groups,
164 size_t n_groups,
165 uint16_t type,
166 uint8_t cmd,
167 sd_netlink_message_handler_t callback,
168 sd_netlink_destroy_t destroy_callback,
169 void *userdata,
170 const char *description);
171
172 /* Make sure callbacks don't destroy the netlink connection */
173 #define NETLINK_DONT_DESTROY(nl) \
174 _cleanup_(sd_netlink_unrefp) _unused_ sd_netlink *_dont_destroy_##nl = sd_netlink_ref(nl)
175
176 bool nfproto_is_valid(int nfproto);
177
178 /* nfnl */
179 /* TODO: to be exported later */
180 int sd_nfnl_socket_open(sd_netlink **ret);
181 int sd_nfnl_send_batch(
182 sd_netlink *nfnl,
183 sd_netlink_message **messages,
184 size_t n_messages,
185 uint32_t **ret_serials);
186 int sd_nfnl_call_batch(
187 sd_netlink *nfnl,
188 sd_netlink_message **messages,
189 size_t n_messages,
190 uint64_t usec);
191 int sd_nfnl_message_new(
192 sd_netlink *nfnl,
193 sd_netlink_message **ret,
194 int nfproto,
195 uint16_t subsys,
196 uint16_t msg_type,
197 uint16_t flags);
198 int sd_nfnl_nft_message_new_table(sd_netlink *nfnl, sd_netlink_message **ret,
199 int nfproto, const char *table);
200 int sd_nfnl_nft_message_new_basechain(sd_netlink *nfnl, sd_netlink_message **ret,
201 int nfproto, const char *table, const char *chain,
202 const char *type, uint8_t hook, int prio);
203 int sd_nfnl_nft_message_new_rule(sd_netlink *nfnl, sd_netlink_message **ret,
204 int nfproto, const char *table, const char *chain);
205 int sd_nfnl_nft_message_new_set(sd_netlink *nfnl, sd_netlink_message **ret,
206 int nfproto, const char *table, const char *set_name,
207 uint32_t set_id, uint32_t klen);
208 int sd_nfnl_nft_message_new_setelems(sd_netlink *nfnl, sd_netlink_message **ret,
209 int add, int nfproto, const char *table, const char *set_name);
210 int sd_nfnl_nft_message_append_setelem(sd_netlink_message *m,
211 uint32_t index,
212 const void *key, size_t key_len,
213 const void *data, size_t data_len,
214 uint32_t flags);