Debian Codegraph @

src/shared/socket-netlink.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/net_namespace.h>
4 #include <linux/unix_diag.h>
5 #include <string.h>
6 #include <sys/stat.h>
7
8 #include "sd-netlink.h"
9
10 #include "alloc-util.h"
11 #include "extract-word.h"
12 #include "fd-util.h"
13 #include "log.h"
14 #include "namespace-util.h"
15 #include "netlink-sock-diag.h"
16 #include "netlink-util.h"
17 #include "parse-util.h"
18 #include "socket-label.h"
19 #include "socket-netlink.h"
20 #include "socket-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
23
24 int socket_address_parse(SocketAddress *a, const char *s) {
25 uint16_t port;
26 int r;
27
28 assert(a);
29 assert(s);
30
31 r = socket_address_parse_unix(a, s);
32 if (r == -EPROTO)
33 r = socket_address_parse_vsock(a, s);
34 if (r != -EPROTO)
35 return r;
36
37 r = parse_ip_port(s, &port);
38 if (r == -ERANGE)
39 return r; /* Valid port syntax, but the numerical value is wrong for a port. */
40 if (r >= 0) {
41 /* Just a port */
42 if (socket_ipv6_is_supported())
43 *a = (SocketAddress) {
44 .sockaddr.in6 = {
45 .sin6_family = AF_INET6,
46 .sin6_port = htobe16(port),
47 .sin6_addr = in6addr_any,
48 },
49 .size = sizeof(struct sockaddr_in6),
50 };
51 else
52 *a = (SocketAddress) {
53 .sockaddr.in = {
54 .sin_family = AF_INET,
55 .sin_port = htobe16(port),
56 .sin_addr.s_addr = INADDR_ANY,
57 },
58 .size = sizeof(struct sockaddr_in),
59 };
60
61 } else {
62 union in_addr_union address;
63 int family, ifindex;
64
65 r = in_addr_port_ifindex_name_from_string_auto(s, &family, &address, &port, &ifindex, NULL);
66 if (r < 0)
67 return r;
68
69 if (port == 0) /* No port, no go. */
70 return -EINVAL;
71
72 if (family == AF_INET)
73 *a = (SocketAddress) {
74 .sockaddr.in = {
75 .sin_family = AF_INET,
76 .sin_addr = address.in,
77 .sin_port = htobe16(port),
78 },
79 .size = sizeof(struct sockaddr_in),
80 };
81 else if (family == AF_INET6)
82 *a = (SocketAddress) {
83 .sockaddr.in6 = {
84 .sin6_family = AF_INET6,
85 .sin6_addr = address.in6,
86 .sin6_port = htobe16(port),
87 .sin6_scope_id = ifindex,
88 },
89 .size = sizeof(struct sockaddr_in6),
90 };
91 else
92 assert_not_reached();
93 }
94
95 return 0;
96 }
97
98 int socket_address_parse_and_warn(SocketAddress *a, const char *s) {
99 SocketAddress b;
100 int r;
101
102 assert(a);
103
104 /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */
105
106 r = socket_address_parse(&b, s);
107 if (r < 0)
108 return r;
109
110 if (!socket_ipv6_is_supported() && b.sockaddr.sa.sa_family == AF_INET6) {
111 log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
112 return -EAFNOSUPPORT;
113 }
114
115 *a = b;
116 return 0;
117 }
118
119 int socket_address_parse_netlink(SocketAddress *a, const char *s) {
120 _cleanup_free_ char *word = NULL;
121 unsigned group = 0;
122 int family, r;
123
124 assert(a);
125 assert(s);
126
127 r = extract_first_word(&s, &word, NULL, 0);
128 if (r < 0)
129 return r;
130 if (r == 0)
131 return -EINVAL;
132
133 family = netlink_family_from_string(word);
134 if (family < 0)
135 return -EINVAL;
136
137 if (!isempty(s)) {
138 r = safe_atou(s, &group);
139 if (r < 0)
140 return r;
141 }
142
143 *a = (SocketAddress) {
144 .type = SOCK_RAW,
145 .sockaddr.nl.nl_family = AF_NETLINK,
146 .sockaddr.nl.nl_groups = group,
147 .protocol = family,
148 .size = sizeof(struct sockaddr_nl),
149 };
150
151 return 0;
152 }
153
154 bool socket_address_is(const SocketAddress *a, const char *s, int type) {
155 struct SocketAddress b;
156
157 assert(a);
158 assert(s);
159
160 if (socket_address_parse(&b, s) < 0)
161 return false;
162
163 b.type = type;
164
165 return socket_address_equal(a, &b);
166 }
167
168 bool socket_address_is_netlink(const SocketAddress *a, const char *s) {
169 struct SocketAddress b;
170
171 assert(a);
172 assert(s);
173
174 if (socket_address_parse_netlink(&b, s) < 0)
175 return false;
176
177 return socket_address_equal(a, &b);
178 }
179
180 int make_socket_fd(int log_level, const char* address, int type, int flags) {
181 SocketAddress a;
182 int fd, r;
183
184 r = socket_address_parse(&a, address);
185 if (r < 0)
186 return log_error_errno(r, "Failed to parse socket address \"%s\": %m", address);
187
188 a.type = type;
189
190 fd = socket_address_listen(
191 &a,
192 type | flags,
193 SOMAXCONN_DELUXE, SOCKET_ADDRESS_DEFAULT,
194 /* bind_to_device= */ NULL,
195 /* reuse_port= */ false,
196 /* free_bind= */ false,
197 /* transparent= */ false,
198 0755,
199 0644,
200 /* selinux_label= */ NULL,
201 /* smack_label= */ NULL);
202 if (fd < 0 || log_get_max_level() >= log_level) {
203 _cleanup_free_ char *p = NULL;
204
205 r = socket_address_print(&a, &p);
206 if (r < 0)
207 return log_error_errno(r, "socket_address_print(): %m");
208
209 if (fd < 0)
210 log_error_errno(fd, "Failed to listen on %s: %m", p);
211 else
212 log_full(log_level, "Listening on %s", p);
213 }
214
215 return fd;
216 }
217
218 int in_addr_port_ifindex_name_from_string_auto(
219 const char *s,
220 int *ret_family,
221 union in_addr_union *ret_address,
222 uint16_t *ret_port,
223 int *ret_ifindex,
224 char **ret_server_name) {
225
226 _cleanup_free_ char *buf1 = NULL, *buf2 = NULL, *name = NULL;
227 int family, ifindex = 0, r;
228 union in_addr_union a;
229 uint16_t port = 0;
230 const char *m;
231
232 assert(s);
233
234 /* This accepts the following:
235 * 192.168.0.1:53#example.com
236 * [2001:4860:4860::8888]:53%eth0#example.com
237 *
238 * If ret_port is NULL, then the port cannot be specified.
239 * If ret_ifindex is NULL, then the interface index cannot be specified.
240 * If ret_server_name is NULL, then server_name cannot be specified.
241 *
242 * ret_family is always AF_INET or AF_INET6.
243 */
244
245 m = strchr(s, '#');
246 if (m) {
247 if (!ret_server_name)
248 return -EINVAL;
249
250 if (isempty(m + 1))
251 return -EINVAL;
252
253 name = strdup(m + 1);
254 if (!name)
255 return -ENOMEM;
256
257 s = buf1 = strndup(s, m - s);
258 if (!buf1)
259 return -ENOMEM;
260 }
261
262 m = strchr(s, '%');
263 if (m) {
264 if (!ret_ifindex)
265 return -EINVAL;
266
267 if (isempty(m + 1))
268 return -EINVAL;
269
270 if (!ifname_valid_full(m + 1, IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC))
271 return -EINVAL; /* We want to return -EINVAL for syntactically invalid names,
272 * and -ENODEV for valid but nonexistent interfaces. */
273
274 ifindex = rtnl_resolve_interface(NULL, m + 1);
275 if (ifindex < 0)
276 return ifindex;
277
278 s = buf2 = strndup(s, m - s);
279 if (!buf2)
280 return -ENOMEM;
281 }
282
283 m = strrchr(s, ':');
284 if (m) {
285 if (*s == '[') {
286 _cleanup_free_ char *ip_str = NULL;
287
288 if (!ret_port)
289 return -EINVAL;
290
291 if (*(m - 1) != ']')
292 return -EINVAL;
293
294 family = AF_INET6;
295
296 r = parse_ip_port(m + 1, &port);
297 if (r < 0)
298 return r;
299
300 ip_str = strndup(s + 1, m - s - 2);
301 if (!ip_str)
302 return -ENOMEM;
303
304 r = in_addr_from_string(family, ip_str, &a);
305 if (r < 0)
306 return r;
307 } else {
308 /* First try to parse the string as IPv6 address without port number */
309 r = in_addr_from_string(AF_INET6, s, &a);
310 if (r < 0) {
311 /* Then the input should be IPv4 address with port number */
312 _cleanup_free_ char *ip_str = NULL;
313
314 if (!ret_port)
315 return -EINVAL;
316
317 family = AF_INET;
318
319 ip_str = strndup(s, m - s);
320 if (!ip_str)
321 return -ENOMEM;
322
323 r = in_addr_from_string(family, ip_str, &a);
324 if (r < 0)
325 return r;
326
327 r = parse_ip_port(m + 1, &port);
328 if (r < 0)
329 return r;
330 } else
331 family = AF_INET6;
332 }
333 } else {
334 family = AF_INET;
335 r = in_addr_from_string(family, s, &a);
336 if (r < 0)
337 return r;
338 }
339
340 if (ret_family)
341 *ret_family = family;
342 if (ret_address)
343 *ret_address = a;
344 if (ret_port)
345 *ret_port = port;
346 if (ret_ifindex)
347 *ret_ifindex = ifindex;
348 if (ret_server_name)
349 *ret_server_name = TAKE_PTR(name);
350
351 return r;
352 }
353
354 struct in_addr_full* in_addr_full_free(struct in_addr_full *a) {
355 if (!a)
356 return NULL;
357
358 free(a->server_name);
359 free(a->cached_server_string);
360 return mfree(a);
361 }
362
363 DEFINE_POINTER_ARRAY_FREE_FUNC(struct in_addr_full*, in_addr_full_free);
364
365 int in_addr_full_new(
366 int family,
367 const union in_addr_union *a,
368 uint16_t port,
369 int ifindex,
370 const char *server_name,
371 struct in_addr_full **ret) {
372
373 _cleanup_free_ char *name = NULL;
374 struct in_addr_full *x;
375
376 assert(a);
377 assert(ret);
378
379 if (!isempty(server_name)) {
380 name = strdup(server_name);
381 if (!name)
382 return -ENOMEM;
383 }
384
385 x = new(struct in_addr_full, 1);
386 if (!x)
387 return -ENOMEM;
388
389 *x = (struct in_addr_full) {
390 .family = family,
391 .address = *a,
392 .port = port,
393 .ifindex = ifindex,
394 .server_name = TAKE_PTR(name),
395 };
396
397 *ret = x;
398 return 0;
399 }
400
401 int in_addr_full_new_from_string(const char *s, struct in_addr_full **ret) {
402 _cleanup_free_ char *server_name = NULL;
403 int family, ifindex, r;
404 union in_addr_union a;
405 uint16_t port;
406
407 assert(s);
408
409 r = in_addr_port_ifindex_name_from_string_auto(s, &family, &a, &port, &ifindex, &server_name);
410 if (r < 0)
411 return r;
412
413 return in_addr_full_new(family, &a, port, ifindex, server_name, ret);
414 }
415
416 const char* in_addr_full_to_string(struct in_addr_full *a) {
417 assert(a);
418
419 if (!a->cached_server_string)
420 (void) in_addr_port_ifindex_name_to_string(
421 a->family,
422 &a->address,
423 a->port,
424 a->ifindex,
425 a->server_name,
426 &a->cached_server_string);
427
428 return a->cached_server_string;
429 }
430
431 int netns_get_nsid(int netnsfd, uint32_t *ret) {
432 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
433 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
434 _cleanup_close_ int _netns_fd = -EBADF;
435 int r;
436
437 if (netnsfd < 0) {
438 _netns_fd = namespace_open_by_type(NAMESPACE_NET);
439 if (_netns_fd < 0)
440 return _netns_fd;
441
442 netnsfd = _netns_fd;
443 }
444
445 r = sd_netlink_open(&rtnl);
446 if (r < 0)
447 return r;
448
449 r = sd_rtnl_message_new_nsid(rtnl, &req, RTM_GETNSID);
450 if (r < 0)
451 return r;
452
453 r = sd_netlink_message_append_s32(req, NETNSA_FD, netnsfd);
454 if (r < 0)
455 return r;
456
457 r = sd_netlink_call(rtnl, req, 0, &reply);
458 if (r < 0)
459 return r;
460
461 for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
462 uint16_t type;
463
464 r = sd_netlink_message_get_errno(m);
465 if (r < 0)
466 return r;
467
468 r = sd_netlink_message_get_type(m, &type);
469 if (r < 0)
470 return r;
471 if (type != RTM_NEWNSID)
472 continue;
473
474 uint32_t u;
475 r = sd_netlink_message_read_u32(m, NETNSA_NSID, &u);
476 if (r < 0)
477 return r;
478
479 if (u == (uint32_t) NETNSA_NSID_NOT_ASSIGNED) /* no NSID assigned yet */
480 return -ENODATA;
481
482 if (ret)
483 *ret = u;
484
485 return 0;
486 }
487
488 return -ENXIO;
489 }
490
491 int af_unix_get_qlen(int fd, uint32_t *ret) {
492 int r;
493
494 assert(fd >= 0);
495 assert(ret);
496
497 /* Returns the current queue length for an AF_UNIX listening socket */
498
499 struct stat st;
500 if (fstat(fd, &st) < 0)
501 return -errno;
502 r = stat_verify_socket(&st);
503 if (r < 0)
504 return r;
505
506 _cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL;
507 r = sd_sock_diag_socket_open(&nl);
508 if (r < 0)
509 return r;
510
511 uint64_t cookie;
512 r = socket_get_cookie(fd, &cookie);
513 if (r < 0)
514 return r;
515
516 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
517 r = sd_sock_diag_message_new_unix(nl, &message, st.st_ino, cookie, UDIAG_SHOW_RQLEN);
518 if (r < 0)
519 return r;
520
521 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *reply = NULL;
522 r = sd_netlink_call(nl, message, /* timeout= */ 0, &reply);
523 if (r < 0)
524 return r;
525
526 for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
527 r = sd_netlink_message_get_errno(m);
528 if (r < 0)
529 return r;
530
531 _cleanup_free_ void *data = NULL;
532 size_t size = 0;
533
534 r = sd_netlink_message_read_data(m, UNIX_DIAG_RQLEN, &size, &data);
535 if (r == -ENODATA)
536 continue;
537 if (r < 0)
538 return r;
539
540 assert(size == sizeof(struct unix_diag_rqlen));
541 const struct unix_diag_rqlen *udrql = ASSERT_PTR(data);
542
543 *ret = udrql->udiag_rqueue;
544 return 0;
545 }
546
547 return -ENODATA;
548 }