]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-socket.c
basic: rename util.h to logarithm.h
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-socket.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <malloc.h>
4 #include <netinet/in.h>
5 #include <stdbool.h>
6 #include <unistd.h>
7
8 #include "sd-netlink.h"
9
10 #include "alloc-util.h"
11 #include "fd-util.h"
12 #include "format-util.h"
13 #include "io-util.h"
14 #include "netlink-internal.h"
15 #include "netlink-types.h"
16 #include "socket-util.h"
17
18 static int broadcast_groups_get(sd_netlink *nl) {
19 _cleanup_free_ uint32_t *groups = NULL;
20 socklen_t len = 0, old_len;
21 int r;
22
23 assert(nl);
24 assert(nl->fd >= 0);
25
26 if (getsockopt(nl->fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &len) < 0) {
27 if (errno != ENOPROTOOPT)
28 return -errno;
29
30 nl->broadcast_group_dont_leave = true;
31 return 0;
32 }
33
34 if (len == 0)
35 return 0;
36
37 groups = new0(uint32_t, len);
38 if (!groups)
39 return -ENOMEM;
40
41 old_len = len;
42
43 if (getsockopt(nl->fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, groups, &len) < 0)
44 return -errno;
45
46 if (old_len != len)
47 return -EIO;
48
49 for (unsigned i = 0; i < len; i++)
50 for (unsigned j = 0; j < sizeof(uint32_t) * 8; j++)
51 if (groups[i] & (1U << j)) {
52 unsigned group = i * sizeof(uint32_t) * 8 + j + 1;
53
54 r = hashmap_ensure_put(&nl->broadcast_group_refs, NULL, UINT_TO_PTR(group), UINT_TO_PTR(1));
55 if (r < 0)
56 return r;
57 }
58
59 return 0;
60 }
61
62 int socket_bind(sd_netlink *nl) {
63 socklen_t addrlen;
64 int r;
65
66 r = setsockopt_int(nl->fd, SOL_NETLINK, NETLINK_PKTINFO, true);
67 if (r < 0)
68 return r;
69
70 addrlen = sizeof(nl->sockaddr);
71
72 /* ignore EINVAL to allow binding an already bound socket */
73 if (bind(nl->fd, &nl->sockaddr.sa, addrlen) < 0 && errno != EINVAL)
74 return -errno;
75
76 if (getsockname(nl->fd, &nl->sockaddr.sa, &addrlen) < 0)
77 return -errno;
78
79 return broadcast_groups_get(nl);
80 }
81
82 static unsigned broadcast_group_get_ref(sd_netlink *nl, unsigned group) {
83 assert(nl);
84
85 return PTR_TO_UINT(hashmap_get(nl->broadcast_group_refs, UINT_TO_PTR(group)));
86 }
87
88 static int broadcast_group_set_ref(sd_netlink *nl, unsigned group, unsigned n_ref) {
89 int r;
90
91 assert(nl);
92
93 r = hashmap_ensure_allocated(&nl->broadcast_group_refs, NULL);
94 if (r < 0)
95 return r;
96
97 return hashmap_replace(nl->broadcast_group_refs, UINT_TO_PTR(group), UINT_TO_PTR(n_ref));
98 }
99
100 static int broadcast_group_join(sd_netlink *nl, unsigned group) {
101 assert(nl);
102 assert(nl->fd >= 0);
103 assert(group > 0);
104
105 /* group is "unsigned", but netlink(7) says the argument for NETLINK_ADD_MEMBERSHIP is "int" */
106 return setsockopt_int(nl->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, group);
107 }
108
109 int socket_broadcast_group_ref(sd_netlink *nl, unsigned group) {
110 unsigned n_ref;
111 int r;
112
113 assert(nl);
114
115 n_ref = broadcast_group_get_ref(nl, group);
116
117 n_ref++;
118
119 r = broadcast_group_set_ref(nl, group, n_ref);
120 if (r < 0)
121 return r;
122
123 if (n_ref > 1)
124 /* already in the group */
125 return 0;
126
127 return broadcast_group_join(nl, group);
128 }
129
130 static int broadcast_group_leave(sd_netlink *nl, unsigned group) {
131 assert(nl);
132 assert(nl->fd >= 0);
133 assert(group > 0);
134
135 if (nl->broadcast_group_dont_leave)
136 return 0;
137
138 /* group is "unsigned", but netlink(7) says the argument for NETLINK_DROP_MEMBERSHIP is "int" */
139 return setsockopt_int(nl->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, group);
140 }
141
142 int socket_broadcast_group_unref(sd_netlink *nl, unsigned group) {
143 unsigned n_ref;
144 int r;
145
146 assert(nl);
147
148 n_ref = broadcast_group_get_ref(nl, group);
149 if (n_ref == 0)
150 return 0;
151
152 n_ref--;
153
154 r = broadcast_group_set_ref(nl, group, n_ref);
155 if (r < 0)
156 return r;
157
158 if (n_ref > 0)
159 /* still refs left */
160 return 0;
161
162 return broadcast_group_leave(nl, group);
163 }
164
165 /* returns the number of bytes sent, or a negative error code */
166 int socket_write_message(sd_netlink *nl, sd_netlink_message *m) {
167 union sockaddr_union addr = {
168 .nl.nl_family = AF_NETLINK,
169 };
170 ssize_t k;
171
172 assert(nl);
173 assert(m);
174 assert(m->hdr);
175
176 k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len, 0, &addr.sa, sizeof(addr));
177 if (k < 0)
178 return -errno;
179
180 return k;
181 }
182
183 static int socket_recv_message(int fd, struct iovec *iov, uint32_t *ret_mcast_group, bool peek) {
184 union sockaddr_union sender;
185 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct nl_pktinfo))) control;
186 struct msghdr msg = {
187 .msg_iov = iov,
188 .msg_iovlen = 1,
189 .msg_name = &sender,
190 .msg_namelen = sizeof(sender),
191 .msg_control = &control,
192 .msg_controllen = sizeof(control),
193 };
194 ssize_t n;
195
196 assert(fd >= 0);
197 assert(iov);
198
199 n = recvmsg_safe(fd, &msg, MSG_TRUNC | (peek ? MSG_PEEK : 0));
200 if (n < 0) {
201 if (n == -ENOBUFS)
202 return log_debug_errno(n, "sd-netlink: kernel receive buffer overrun");
203 if (ERRNO_IS_TRANSIENT(n))
204 return 0;
205 return (int) n;
206 }
207
208 if (sender.nl.nl_pid != 0) {
209 /* not from the kernel, ignore */
210 log_debug("sd-netlink: ignoring message from PID %"PRIu32, sender.nl.nl_pid);
211
212 if (peek) {
213 /* drop the message */
214 n = recvmsg_safe(fd, &msg, 0);
215 if (n < 0)
216 return (int) n;
217 }
218
219 return 0;
220 }
221
222 if (ret_mcast_group) {
223 struct nl_pktinfo *pi;
224
225 pi = CMSG_FIND_DATA(&msg, SOL_NETLINK, NETLINK_PKTINFO, struct nl_pktinfo);
226 if (pi)
227 *ret_mcast_group = pi->group;
228 else
229 *ret_mcast_group = 0;
230 }
231
232 return (int) n;
233 }
234
235 /* On success, the number of bytes received is returned and *ret points to the received message
236 * which has a valid header and the correct size.
237 * If nothing useful was received 0 is returned.
238 * On failure, a negative error code is returned.
239 */
240 int socket_read_message(sd_netlink *nl) {
241 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *first = NULL;
242 bool multi_part = false, done = false;
243 size_t len, allocated;
244 struct iovec iov = {};
245 uint32_t group = 0;
246 unsigned i = 0;
247 int r;
248
249 assert(nl);
250 assert(nl->rbuffer);
251
252 /* read nothing, just get the pending message size */
253 r = socket_recv_message(nl->fd, &iov, NULL, true);
254 if (r <= 0)
255 return r;
256 else
257 len = (size_t) r;
258
259 /* make room for the pending message */
260 if (!greedy_realloc((void**) &nl->rbuffer, len, sizeof(uint8_t)))
261 return -ENOMEM;
262
263 allocated = MALLOC_SIZEOF_SAFE(nl->rbuffer);
264 iov = IOVEC_MAKE(nl->rbuffer, allocated);
265
266 /* read the pending message */
267 r = socket_recv_message(nl->fd, &iov, &group, false);
268 if (r <= 0)
269 return r;
270 else
271 len = (size_t) r;
272
273 if (len > allocated)
274 /* message did not fit in read buffer */
275 return -EIO;
276
277 if (NLMSG_OK(nl->rbuffer, len) && nl->rbuffer->nlmsg_flags & NLM_F_MULTI) {
278 multi_part = true;
279
280 for (i = 0; i < nl->rqueue_partial_size; i++)
281 if (message_get_serial(nl->rqueue_partial[i]) ==
282 nl->rbuffer->nlmsg_seq) {
283 first = nl->rqueue_partial[i];
284 break;
285 }
286 }
287
288 for (struct nlmsghdr *new_msg = nl->rbuffer; NLMSG_OK(new_msg, len) && !done; new_msg = NLMSG_NEXT(new_msg, len)) {
289 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
290 size_t size;
291
292 if (group == 0 && new_msg->nlmsg_pid != nl->sockaddr.nl.nl_pid)
293 /* not broadcast and not for us */
294 continue;
295
296 if (new_msg->nlmsg_type == NLMSG_NOOP)
297 /* silently drop noop messages */
298 continue;
299
300 if (new_msg->nlmsg_type == NLMSG_DONE) {
301 /* finished reading multi-part message */
302 done = true;
303
304 /* if first is not defined, put NLMSG_DONE into the receive queue. */
305 if (first)
306 continue;
307 }
308
309 /* check that we support this message type */
310 r = netlink_get_policy_set_and_header_size(nl, new_msg->nlmsg_type, NULL, &size);
311 if (r < 0) {
312 if (r == -EOPNOTSUPP)
313 log_debug("sd-netlink: ignored message with unknown type: %i",
314 new_msg->nlmsg_type);
315
316 continue;
317 }
318
319 /* check that the size matches the message type */
320 if (new_msg->nlmsg_len < NLMSG_LENGTH(size)) {
321 log_debug("sd-netlink: message is shorter than expected, dropping");
322 continue;
323 }
324
325 r = message_new_empty(nl, &m);
326 if (r < 0)
327 return r;
328
329 m->multicast_group = group;
330 m->hdr = memdup(new_msg, new_msg->nlmsg_len);
331 if (!m->hdr)
332 return -ENOMEM;
333
334 /* seal and parse the top-level message */
335 r = sd_netlink_message_rewind(m, nl);
336 if (r < 0)
337 return r;
338
339 /* push the message onto the multi-part message stack */
340 if (first)
341 m->next = first;
342 first = TAKE_PTR(m);
343 }
344
345 if (len > 0)
346 log_debug("sd-netlink: discarding %zu bytes of incoming message", len);
347
348 if (!first)
349 return 0;
350
351 if (!multi_part || done) {
352 /* we got a complete message, push it on the read queue */
353 r = netlink_rqueue_make_room(nl);
354 if (r < 0)
355 return r;
356
357 nl->rqueue[nl->rqueue_size++] = TAKE_PTR(first);
358
359 if (multi_part && (i < nl->rqueue_partial_size)) {
360 /* remove the message form the partial read queue */
361 memmove(nl->rqueue_partial + i, nl->rqueue_partial + i + 1,
362 sizeof(sd_netlink_message*) * (nl->rqueue_partial_size - i - 1));
363 nl->rqueue_partial_size--;
364 }
365
366 return 1;
367 } else {
368 /* we only got a partial multi-part message, push it on the
369 partial read queue */
370 if (i < nl->rqueue_partial_size)
371 nl->rqueue_partial[i] = TAKE_PTR(first);
372 else {
373 r = netlink_rqueue_partial_make_room(nl);
374 if (r < 0)
375 return r;
376
377 nl->rqueue_partial[nl->rqueue_partial_size++] = TAKE_PTR(first);
378 }
379
380 return 0;
381 }
382 }