Debian Codegraph @

src/libsystemd/sd-netlink/netlink-socket.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-netlink.h"
4
5 #include "alloc-util.h"
6 #include "errno-util.h"
7 #include "hashmap.h"
8 #include "iovec-util.h"
9 #include "log.h"
10 #include "netlink-internal.h"
11 #include "netlink-types.h"
12 #include "ordered-set.h"
13 #include "socket-util.h"
14
15 static int broadcast_groups_get(sd_netlink *nl) {
16 _cleanup_free_ uint32_t *groups = NULL;
17 size_t len;
18 int r;
19
20 assert(nl);
21 assert(nl->fd >= 0);
22
23 r = netlink_socket_get_multicast_groups(nl->fd, &len, &groups);
24 if (r < 0)
25 return r;
26
27 for (size_t i = 0; i < len; i++)
28 for (unsigned j = 0; j < sizeof(uint32_t) * 8; j++)
29 if (groups[i] & (1U << j)) {
30 unsigned group = i * sizeof(uint32_t) * 8 + j + 1;
31
32 r = hashmap_ensure_put(&nl->broadcast_group_refs, NULL, UINT_TO_PTR(group), UINT_TO_PTR(1));
33 if (r < 0)
34 return r;
35 }
36
37 return 0;
38 }
39
40 int socket_bind(sd_netlink *nl) {
41 socklen_t addrlen;
42 int r;
43
44 r = setsockopt_int(nl->fd, SOL_NETLINK, NETLINK_PKTINFO, true);
45 if (r < 0)
46 return r;
47
48 addrlen = sizeof(nl->sockaddr);
49
50 /* ignore EINVAL to allow binding an already bound socket */
51 if (bind(nl->fd, &nl->sockaddr.sa, addrlen) < 0 && errno != EINVAL)
52 return -errno;
53
54 if (getsockname(nl->fd, &nl->sockaddr.sa, &addrlen) < 0)
55 return -errno;
56
57 return broadcast_groups_get(nl);
58 }
59
60 static unsigned broadcast_group_get_ref(sd_netlink *nl, unsigned group) {
61 assert(nl);
62
63 return PTR_TO_UINT(hashmap_get(nl->broadcast_group_refs, UINT_TO_PTR(group)));
64 }
65
66 static int broadcast_group_set_ref(sd_netlink *nl, unsigned group, unsigned n_ref) {
67 assert(nl);
68
69 return hashmap_ensure_replace(&nl->broadcast_group_refs, NULL, UINT_TO_PTR(group), UINT_TO_PTR(n_ref));
70 }
71
72 static int broadcast_group_join(sd_netlink *nl, unsigned group) {
73 assert(nl);
74 assert(nl->fd >= 0);
75 assert(group > 0);
76
77 /* group is "unsigned", but netlink(7) says the argument for NETLINK_ADD_MEMBERSHIP is "int" */
78 return setsockopt_int(nl->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, group);
79 }
80
81 int socket_broadcast_group_ref(sd_netlink *nl, unsigned group) {
82 unsigned n_ref;
83 int r;
84
85 assert(nl);
86
87 n_ref = broadcast_group_get_ref(nl, group);
88
89 n_ref++;
90
91 r = broadcast_group_set_ref(nl, group, n_ref);
92 if (r < 0)
93 return r;
94
95 if (n_ref > 1)
96 /* already in the group */
97 return 0;
98
99 return broadcast_group_join(nl, group);
100 }
101
102 int socket_broadcast_group_unref(sd_netlink *nl, unsigned group) {
103 unsigned n_ref;
104 int r;
105
106 assert(nl);
107
108 n_ref = broadcast_group_get_ref(nl, group);
109 if (n_ref == 0)
110 return 0;
111
112 n_ref--;
113
114 r = broadcast_group_set_ref(nl, group, n_ref);
115 if (r < 0)
116 return r;
117
118 if (n_ref > 0)
119 /* still refs left */
120 return 0;
121
122 /* group is "unsigned", but netlink(7) says the argument for NETLINK_DROP_MEMBERSHIP is "int" */
123 return setsockopt_int(nl->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, group);
124 }
125
126 /* returns the number of bytes sent, or a negative error code */
127 int socket_write_message(sd_netlink *nl, sd_netlink_message *m) {
128 union sockaddr_union addr = {
129 .nl.nl_family = AF_NETLINK,
130 };
131 ssize_t k;
132
133 assert(nl);
134 assert(m);
135 assert(m->hdr);
136
137 k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len, 0, &addr.sa, sizeof(addr));
138 if (k < 0)
139 return -errno;
140
141 return k;
142 }
143
144 static int socket_recv_message(int fd, void *buf, size_t buf_size, uint32_t *ret_mcast_group, bool peek) {
145 struct iovec iov = IOVEC_MAKE(buf, buf_size);
146 union sockaddr_union sender;
147 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct nl_pktinfo))) control;
148 struct msghdr msg = {
149 .msg_iov = &iov,
150 .msg_iovlen = 1,
151 .msg_name = &sender,
152 .msg_namelen = sizeof(sender),
153 .msg_control = &control,
154 .msg_controllen = sizeof(control),
155 };
156 ssize_t n;
157
158 assert(fd >= 0);
159 assert(peek || (buf && buf_size > 0));
160
161 /* Note: this might return successfully, but with a zero size under some transient conditions, such
162 * as the reception of a non-kernel message. In such a case the passed buffer might or might not be
163 * modified. Caller must treat a zero return as "no message, but also not an error". */
164
165 n = recvmsg_safe(fd, &msg, peek ? (MSG_PEEK|MSG_TRUNC) : 0);
166 if (ERRNO_IS_NEG_TRANSIENT(n))
167 goto transient;
168 if (n == -ENOBUFS)
169 return log_debug_errno(n, "sd-netlink: kernel receive buffer overrun");
170 if (n == -ECHRNG)
171 return log_debug_errno(n, "sd-netlink: got truncated control message");
172 if (n == -EXFULL)
173 return log_debug_errno(n, "sd-netlink: got truncated payload message");
174 if (n < 0)
175 return (int) n;
176
177 if (sender.nl.nl_pid != 0) {
178 /* not from the kernel, ignore */
179 log_debug("sd-netlink: ignoring message from PID %"PRIu32, sender.nl.nl_pid);
180
181 if (peek) {
182 /* Drop the message. Note that we ignore ECHRNG/EXFULL errors here, which
183 * recvmsg_safe() returns in case the payload or cdata is truncated. Given we just
184 * want to drop the message we also don't care if its payload or cdata was
185 * truncated. */
186 n = recvmsg_safe(fd, &msg, 0);
187 if (n < 0 && !IN_SET(n, -ECHRNG, -EXFULL))
188 return (int) n;
189 }
190
191 goto transient;
192 }
193
194 if (ret_mcast_group) {
195 struct nl_pktinfo *pi;
196
197 pi = CMSG_FIND_DATA(&msg, SOL_NETLINK, NETLINK_PKTINFO, struct nl_pktinfo);
198 if (pi)
199 *ret_mcast_group = pi->group;
200 else
201 *ret_mcast_group = 0;
202 }
203
204 return (int) n;
205
206 transient:
207 if (ret_mcast_group)
208 *ret_mcast_group = 0;
209
210 return 0;
211 }
212
213 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
214 netlink_message_hash_ops,
215 void, trivial_hash_func, trivial_compare_func,
216 sd_netlink_message, sd_netlink_message_unref);
217
218 static int netlink_queue_received_message(sd_netlink *nl, sd_netlink_message *m) {
219 uint32_t serial;
220 int r;
221
222 assert(nl);
223 assert(m);
224
225 serial = message_get_serial(m);
226 if (serial != 0) {
227 NetlinkIgnoredSerial *s = hashmap_remove(nl->ignored_serials, UINT32_TO_PTR(serial));
228 if (s) {
229 /* We are not interested in the message anymore. */
230 free(s);
231 return 0;
232 }
233 }
234
235 if (ordered_set_size(nl->rqueue) >= NETLINK_RQUEUE_MAX)
236 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
237 "sd-netlink: exhausted the read queue size (%d)", NETLINK_RQUEUE_MAX);
238
239 r = ordered_set_ensure_put(&nl->rqueue, &netlink_message_hash_ops, m);
240 if (r < 0)
241 return r;
242
243 sd_netlink_message_ref(m);
244
245 if (sd_netlink_message_is_broadcast(m))
246 return 0;
247
248 if (serial == 0)
249 return 0;
250
251 if (sd_netlink_message_get_errno(m) < 0) {
252 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *old = NULL;
253
254 old = hashmap_remove(nl->rqueue_by_serial, UINT32_TO_PTR(serial));
255 if (old)
256 log_debug("sd-netlink: received error message with serial %"PRIu32", but another message with "
257 "the same serial is already stored in the read queue, replacing.", serial);
258 }
259
260 r = hashmap_ensure_put(&nl->rqueue_by_serial, &netlink_message_hash_ops, UINT32_TO_PTR(serial), m);
261 if (r == -EEXIST) {
262 if (!sd_netlink_message_is_error(m))
263 log_debug("sd-netlink: received message with serial %"PRIu32", but another message with "
264 "the same serial is already stored in the read queue, ignoring.", serial);
265 return 0;
266 }
267 if (r < 0) {
268 sd_netlink_message_unref(ordered_set_remove(nl->rqueue, m));
269 return r;
270 }
271
272 sd_netlink_message_ref(m);
273 return 0;
274 }
275
276 static int netlink_queue_partially_received_message(sd_netlink *nl, sd_netlink_message *m) {
277 uint32_t serial;
278 int r;
279
280 assert(nl);
281 assert(m);
282 assert(m->hdr->nlmsg_flags & NLM_F_MULTI);
283
284 if (hashmap_size(nl->rqueue_partial_by_serial) >= NETLINK_RQUEUE_MAX)
285 return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
286 "sd-netlink: exhausted the partial read queue size (%d)", NETLINK_RQUEUE_MAX);
287
288 serial = message_get_serial(m);
289 r = hashmap_ensure_put(&nl->rqueue_partial_by_serial, &netlink_message_hash_ops, UINT32_TO_PTR(serial), m);
290 if (r < 0)
291 return r;
292
293 sd_netlink_message_ref(m);
294 return 0;
295 }
296
297 static int parse_message_one(sd_netlink *nl, uint32_t group, const struct nlmsghdr *hdr, sd_netlink_message **ret) {
298 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
299 size_t size;
300 int r;
301
302 assert(nl);
303 assert(hdr);
304 assert(ret);
305
306 /* not broadcast and not for us */
307 if (group == 0 && hdr->nlmsg_pid != nl->sockaddr.nl.nl_pid)
308 goto finalize;
309
310 /* silently drop noop messages */
311 if (hdr->nlmsg_type == NLMSG_NOOP)
312 goto finalize;
313
314 /* check that we support this message type */
315 r = netlink_get_policy_set_and_header_size(nl, hdr->nlmsg_type, hdr->nlmsg_flags, NULL, &size);
316 if (r == -EOPNOTSUPP) {
317 log_debug("sd-netlink: ignored message with unknown type: %i", hdr->nlmsg_type);
318 goto finalize;
319 }
320 if (r < 0)
321 return r;
322
323 /* check that the size matches the message type */
324 if (hdr->nlmsg_len < NLMSG_LENGTH(size)) {
325 log_debug("sd-netlink: message is shorter than expected, dropping.");
326 goto finalize;
327 }
328
329 r = message_new_empty(nl, &m);
330 if (r < 0)
331 return r;
332
333 m->multicast_group = group;
334 m->hdr = memdup(hdr, hdr->nlmsg_len);
335 if (!m->hdr)
336 return -ENOMEM;
337
338 /* seal and parse the top-level message */
339 r = sd_netlink_message_rewind(m, nl);
340 if (r < 0)
341 return r;
342
343 *ret = TAKE_PTR(m);
344 return 1;
345
346 finalize:
347 *ret = NULL;
348 return 0;
349 }
350
351 /* On success, the number of bytes received is returned and *ret points to the received message
352 * which has a valid header and the correct size.
353 * If nothing useful was received 0 is returned.
354 * On failure, a negative error code is returned.
355 */
356 int socket_read_message(sd_netlink *nl) {
357 bool done = false;
358 uint32_t group;
359 size_t len;
360 int r;
361
362 assert(nl);
363
364 /* read nothing, just get the pending message size */
365 r = socket_recv_message(nl->fd, NULL, 0, NULL, true);
366 if (r <= 0)
367 return r;
368 len = (size_t) r;
369
370 /* make room for the pending message */
371 if (!greedy_realloc((void**) &nl->rbuffer, len, sizeof(uint8_t)))
372 return -ENOMEM;
373
374 /* read the pending message */
375 r = socket_recv_message(nl->fd, nl->rbuffer, MALLOC_SIZEOF_SAFE(nl->rbuffer), &group, false);
376 if (r <= 0)
377 return r;
378 len = (size_t) r;
379
380 if (!NLMSG_OK(nl->rbuffer, len)) {
381 log_debug("sd-netlink: received invalid message, discarding %zu bytes of incoming message", len);
382 return 0;
383 }
384
385 for (struct nlmsghdr *hdr = nl->rbuffer; NLMSG_OK(hdr, len); hdr = NLMSG_NEXT(hdr, len)) {
386 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
387
388 r = parse_message_one(nl, group, hdr, &m);
389 if (r < 0)
390 return r;
391 if (r == 0)
392 continue;
393
394 if (hdr->nlmsg_flags & NLM_F_MULTI) {
395 if (hdr->nlmsg_type == NLMSG_DONE) {
396 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *existing = NULL;
397
398 /* finished reading multi-part message */
399 existing = hashmap_remove(nl->rqueue_partial_by_serial, UINT32_TO_PTR(hdr->nlmsg_seq));
400
401 /* if we receive only NLMSG_DONE, put it into the receive queue. */
402 r = netlink_queue_received_message(nl, existing ?: m);
403 if (r < 0)
404 return r;
405
406 done = true;
407 } else {
408 sd_netlink_message *existing;
409
410 existing = hashmap_get(nl->rqueue_partial_by_serial, UINT32_TO_PTR(hdr->nlmsg_seq));
411 if (existing) {
412 /* This is the continuation of the previously read messages.
413 * Let's append this message at the end. */
414 while (existing->next)
415 existing = existing->next;
416 existing->next = TAKE_PTR(m);
417 } else {
418 /* This is the first message. Put it into the queue for partially
419 * received messages. */
420 r = netlink_queue_partially_received_message(nl, m);
421 if (r < 0)
422 return r;
423 }
424 }
425
426 } else {
427 r = netlink_queue_received_message(nl, m);
428 if (r < 0)
429 return r;
430
431 done = true;
432 }
433 }
434
435 if (len > 0)
436 log_debug("sd-netlink: discarding trailing %zu bytes of incoming message", len);
437
438 return done;
439 }