]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/socket-util.c
Fix assertion in socknameinfo_pretty()
[thirdparty/systemd.git] / src / basic / socket-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
42f4e3c4 3#include <arpa/inet.h>
07630cea 4#include <errno.h>
11c3a366 5#include <limits.h>
542563ba 6#include <net/if.h>
b31f535c 7#include <netdb.h>
2583fbea 8#include <netinet/ip.h>
60d9771c 9#include <poll.h>
07630cea 10#include <stddef.h>
11c3a366 11#include <stdint.h>
07630cea 12#include <stdio.h>
11c3a366 13#include <stdlib.h>
f5947a5e 14#include <sys/ioctl.h>
07630cea 15#include <unistd.h>
4252696a 16#include <linux/if.h>
42f4e3c4 17
b5efdb8a 18#include "alloc-util.h"
4ff9bc2e 19#include "errno-util.h"
15dca371 20#include "escape.h"
2583fbea 21#include "fd-util.h"
07630cea 22#include "fileio.h"
f97b34a6 23#include "format-util.h"
0f2d351f 24#include "io-util.h"
93cc7779 25#include "log.h"
0a970718 26#include "memory-util.h"
6bedfcbb 27#include "parse-util.h"
9eb977db 28#include "path-util.h"
dccca82b 29#include "process-util.h"
2583fbea 30#include "socket-util.h"
8b43440b 31#include "string-table.h"
07630cea 32#include "string-util.h"
ef76dff2 33#include "strv.h"
83e03c4f 34#include "sysctl-util.h"
ee104e11 35#include "user-util.h"
ef76dff2 36#include "utf8.h"
42f4e3c4 37
349cc4a5 38#if ENABLE_IDN
cadc80b8 39# define IDN_FLAGS NI_IDN
6326a143
WB
40#else
41# define IDN_FLAGS 0
42#endif
43
598d2428
LB
44/* From the kernel's include/net/scm.h */
45#ifndef SCM_MAX_FD
46# define SCM_MAX_FD 253
47#endif
48
398ce0bc 49static const char* const socket_address_type_table[] = {
955bb7fa
ZJS
50 [SOCK_STREAM] = "Stream",
51 [SOCK_DGRAM] = "Datagram",
52 [SOCK_RAW] = "Raw",
53 [SOCK_RDM] = "ReliableDatagram",
398ce0bc 54 [SOCK_SEQPACKET] = "SequentialPacket",
955bb7fa 55 [SOCK_DCCP] = "DatagramCongestionControl",
398ce0bc
YW
56};
57
58DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int);
59
15dca371 60int socket_address_verify(const SocketAddress *a, bool strict) {
42f4e3c4
LP
61 assert(a);
62
15dca371
ZJS
63 /* With 'strict' we enforce additional sanity constraints which are not set by the standard,
64 * but should only apply to sockets we create ourselves. */
65
542563ba 66 switch (socket_address_family(a)) {
42f4e3c4 67
7a22745a
LP
68 case AF_INET:
69 if (a->size != sizeof(struct sockaddr_in))
70 return -EINVAL;
42f4e3c4 71
4d49b48c 72 if (a->sockaddr.in.sin_port == 0)
7a22745a 73 return -EINVAL;
42f4e3c4 74
44ab2347 75 if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
7a22745a 76 return -EINVAL;
42f4e3c4 77
7a22745a
LP
78 return 0;
79
80 case AF_INET6:
81 if (a->size != sizeof(struct sockaddr_in6))
82 return -EINVAL;
42f4e3c4 83
7a22745a
LP
84 if (a->sockaddr.in6.sin6_port == 0)
85 return -EINVAL;
42f4e3c4 86
44ab2347 87 if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
7a22745a 88 return -EINVAL;
42f4e3c4 89
7a22745a 90 return 0;
42f4e3c4 91
7a22745a
LP
92 case AF_UNIX:
93 if (a->size < offsetof(struct sockaddr_un, sun_path))
94 return -EINVAL;
15dca371
ZJS
95 if (a->size > sizeof(struct sockaddr_un) + !strict)
96 /* If !strict, allow one extra byte, since getsockname() on Linux will append
97 * a NUL byte if we have path sockets that are above sun_path's full size. */
8e8132c6 98 return -EINVAL;
42f4e3c4 99
8e8132c6 100 if (a->size > offsetof(struct sockaddr_un, sun_path) &&
15dca371
ZJS
101 a->sockaddr.un.sun_path[0] != 0 &&
102 strict) {
103 /* Only validate file system sockets here, and only in strict mode */
8e8132c6 104 const char *e;
7a22745a 105
8e8132c6
LP
106 e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
107 if (e) {
15dca371 108 /* If there's an embedded NUL byte, make sure the size of the socket address matches it */
7a22745a
LP
109 if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
110 return -EINVAL;
8e8132c6 111 } else {
d7b34e38 112 /* If there's no embedded NUL byte, then the size needs to match the whole
8e8132c6
LP
113 * structure or the structure with one extra NUL byte suffixed. (Yeah, Linux is awful,
114 * and considers both equivalent: getsockname() even extends sockaddr_un beyond its
7802194a 115 * size if the path is non NUL terminated.) */
8e8132c6
LP
116 if (!IN_SET(a->size, sizeof(a->sockaddr.un.sun_path), sizeof(a->sockaddr.un.sun_path)+1))
117 return -EINVAL;
42f4e3c4 118 }
7a22745a 119 }
42f4e3c4 120
44ab2347 121 if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET))
7a22745a 122 return -EINVAL;
42f4e3c4 123
7a22745a
LP
124 return 0;
125
126 case AF_NETLINK:
127
128 if (a->size != sizeof(struct sockaddr_nl))
129 return -EINVAL;
130
44ab2347 131 if (!IN_SET(a->type, 0, SOCK_RAW, SOCK_DGRAM))
7a22745a
LP
132 return -EINVAL;
133
134 return 0;
135
0fc0f14b
SH
136 case AF_VSOCK:
137 if (a->size != sizeof(struct sockaddr_vm))
138 return -EINVAL;
139
44ab2347 140 if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
0fc0f14b
SH
141 return -EINVAL;
142
143 return 0;
144
7a22745a
LP
145 default:
146 return -EAFNOSUPPORT;
42f4e3c4
LP
147 }
148}
149
4d49b48c 150int socket_address_print(const SocketAddress *a, char **ret) {
42f4e3c4 151 int r;
4d49b48c 152
42f4e3c4 153 assert(a);
4d49b48c 154 assert(ret);
42f4e3c4 155
15dca371
ZJS
156 r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be
157 * able to pretty-print any socket the kernel considers
158 * valid. We still need to do validation to know if we
159 * can meaningfully print the address. */
4d49b48c 160 if (r < 0)
42f4e3c4
LP
161 return r;
162
4d49b48c 163 if (socket_address_family(a) == AF_NETLINK) {
7fd1b19b 164 _cleanup_free_ char *sfamily = NULL;
7a22745a 165
f8b69d1d 166 r = netlink_family_to_string_alloc(a->protocol, &sfamily);
7a22745a 167 if (r < 0)
f8b69d1d 168 return r;
4d49b48c
LP
169
170 r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
8520cfa5
MS
171 if (r < 0)
172 return -ENOMEM;
7a22745a
LP
173
174 return 0;
175 }
176
3b1c5241 177 return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
42f4e3c4
LP
178}
179
4f2d528d
LP
180bool socket_address_can_accept(const SocketAddress *a) {
181 assert(a);
182
183 return
3742095b 184 IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET);
4f2d528d 185}
a16e1123
LP
186
187bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
188 assert(a);
189 assert(b);
190
191 /* Invalid addresses are unequal to all */
15dca371
ZJS
192 if (socket_address_verify(a, false) < 0 ||
193 socket_address_verify(b, false) < 0)
a16e1123
LP
194 return false;
195
196 if (a->type != b->type)
197 return false;
198
a16e1123
LP
199 if (socket_address_family(a) != socket_address_family(b))
200 return false;
201
202 switch (socket_address_family(a)) {
203
204 case AF_INET:
4d49b48c 205 if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
a16e1123
LP
206 return false;
207
4d49b48c 208 if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
a16e1123
LP
209 return false;
210
211 break;
212
213 case AF_INET6:
214 if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
215 return false;
216
217 if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
218 return false;
219
220 break;
221
222 case AF_UNIX:
710708a5
MS
223 if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
224 b->size <= offsetof(struct sockaddr_un, sun_path))
225 return false;
226
a16e1123
LP
227 if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
228 return false;
229
230 if (a->sockaddr.un.sun_path[0]) {
563e6846 231 if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
a16e1123
LP
232 return false;
233 } else {
c78e47a6
MS
234 if (a->size != b->size)
235 return false;
236
b12c1e7c 237 if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
a16e1123
LP
238 return false;
239 }
240
241 break;
242
7a22745a 243 case AF_NETLINK:
7a22745a
LP
244 if (a->protocol != b->protocol)
245 return false;
246
247 if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
248 return false;
249
250 break;
251
0fc0f14b
SH
252 case AF_VSOCK:
253 if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid)
254 return false;
255
256 if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port)
257 return false;
258
259 break;
260
a16e1123
LP
261 default:
262 /* Cannot compare, so we assume the addresses are different */
263 return false;
264 }
265
266 return true;
267}
268
a57f7e2c 269const char* socket_address_get_path(const SocketAddress *a) {
6e2ef85b
LP
270 assert(a);
271
272 if (socket_address_family(a) != AF_UNIX)
a57f7e2c 273 return NULL;
6e2ef85b
LP
274
275 if (a->sockaddr.un.sun_path[0] == 0)
a57f7e2c 276 return NULL;
a16e1123 277
48e6a2dc
LP
278 /* Note that this is only safe because we know that there's an extra NUL byte after the sockaddr_un
279 * structure. On Linux AF_UNIX file system socket addresses don't have to be NUL terminated if they take up the
280 * full sun_path space. */
281 assert_cc(sizeof(union sockaddr_union) >= sizeof(struct sockaddr_un)+1);
a57f7e2c 282 return a->sockaddr.un.sun_path;
a16e1123 283}
c0120d99 284
5bfcc1c6 285bool socket_ipv6_is_supported(void) {
571ec995 286 static int cached = -1;
f89f1e8f 287
571ec995
LP
288 if (cached < 0) {
289
290 if (access("/proc/net/if_inet6", F_OK) < 0) {
291
292 if (errno != ENOENT) {
293 log_debug_errno(errno, "Unexpected error when checking whether /proc/net/if_inet6 exists: %m");
294 return false;
295 }
296
297 cached = false;
298 } else
299 cached = true;
300 }
301
302 return cached;
5bfcc1c6
FF
303}
304
83e03c4f 305bool socket_ipv6_is_enabled(void) {
f96f5d54 306 _cleanup_free_ char *v = NULL;
83e03c4f
LP
307 int r;
308
309 /* Much like socket_ipv6_is_supported(), but also checks that the sysctl that disables IPv6 on all
310 * interfaces isn't turned on */
311
312 if (!socket_ipv6_is_supported())
90ab5042 313 return false;
f89f1e8f 314
83e03c4f
LP
315 r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v);
316 if (r < 0) {
317 log_debug_errno(r, "Unexpected error reading 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
318 return true;
319 }
320
321 r = parse_boolean(v);
322 if (r < 0) {
323 log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
324 return true;
325 }
326
327 return !r;
5bfcc1c6
FF
328}
329
01e10de3 330bool socket_address_matches_fd(const SocketAddress *a, int fd) {
dbafedac
MS
331 SocketAddress b;
332 socklen_t solen;
01e10de3
LP
333
334 assert(a);
335 assert(fd >= 0);
336
dbafedac
MS
337 b.size = sizeof(b.sockaddr);
338 if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
01e10de3
LP
339 return false;
340
dbafedac 341 if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
01e10de3
LP
342 return false;
343
dbafedac
MS
344 solen = sizeof(b.type);
345 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
01e10de3
LP
346 return false;
347
dbafedac 348 if (b.type != a->type)
01e10de3
LP
349 return false;
350
351 if (a->protocol != 0) {
dbafedac
MS
352 solen = sizeof(b.protocol);
353 if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
01e10de3
LP
354 return false;
355
dbafedac 356 if (b.protocol != a->protocol)
01e10de3
LP
357 return false;
358 }
359
02233928 360 return socket_address_equal(a, &b);
01e10de3
LP
361}
362
f6aac5bf 363int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
31325971 364 const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
3b1c5241 365
f6aac5bf
LP
366 /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
367
3b1c5241
SL
368 assert(sa);
369
0fc0f14b 370 switch (sa->sa.sa_family) {
f6aac5bf 371
0fc0f14b 372 case AF_INET:
f6aac5bf 373 *ret_port = be16toh(sa->in.sin_port);
0fc0f14b 374 return 0;
3b1c5241 375
0fc0f14b 376 case AF_INET6:
f6aac5bf 377 *ret_port = be16toh(sa->in6.sin6_port);
0fc0f14b
SH
378 return 0;
379
380 case AF_VSOCK:
f6aac5bf 381 *ret_port = sa->vm.svm_port;
0fc0f14b
SH
382 return 0;
383
384 default:
385 return -EAFNOSUPPORT;
386 }
3b1c5241
SL
387}
388
31325971
LP
389const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) {
390 const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
391
392 if (!sa)
393 return NULL;
394
395 switch (sa->sa.sa_family) {
396
397 case AF_INET:
398 return (const union in_addr_union*) &sa->in.sin_addr;
399
400 case AF_INET6:
401 return (const union in_addr_union*) &sa->in6.sin6_addr;
402
403 default:
404 return NULL;
405 }
406}
407
c1b91f06
LP
408int sockaddr_set_in_addr(
409 union sockaddr_union *u,
410 int family,
411 const union in_addr_union *a,
412 uint16_t port) {
413
414 assert(u);
415 assert(a);
416
417 switch (family) {
418
419 case AF_INET:
420 u->in = (struct sockaddr_in) {
421 .sin_family = AF_INET,
422 .sin_addr = a->in,
423 .sin_port = htobe16(port),
424 };
425
426 return 0;
427
428 case AF_INET6:
429 u->in6 = (struct sockaddr_in6) {
430 .sin6_family = AF_INET6,
431 .sin6_addr = a->in6,
432 .sin6_port = htobe16(port),
433 };
434
435 return 0;
436
437 default:
438 return -EAFNOSUPPORT;
439
440 }
441}
442
836f9cfe
LP
443int sockaddr_pretty(
444 const struct sockaddr *_sa,
445 socklen_t salen,
446 bool translate_ipv6,
447 bool include_port,
448 char **ret) {
449
4d49b48c 450 union sockaddr_union *sa = (union sockaddr_union*) _sa;
8569a776 451 char *p;
fc25ad25 452 int r;
8569a776 453
4d49b48c
LP
454 assert(sa);
455 assert(salen >= sizeof(sa->sa.sa_family));
4eb3ec63 456 assert(ret);
8569a776 457
4d49b48c 458 switch (sa->sa.sa_family) {
8569a776
LP
459
460 case AF_INET: {
461 uint32_t a;
462
8e38570e 463 a = be32toh(sa->in.sin_addr.s_addr);
8569a776 464
fc25ad25
ZJS
465 if (include_port)
466 r = asprintf(&p,
3b1c5241
SL
467 "%u.%u.%u.%u:%u",
468 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
8e38570e 469 be16toh(sa->in.sin_port));
fc25ad25
ZJS
470 else
471 r = asprintf(&p,
3b1c5241 472 "%u.%u.%u.%u",
fc25ad25
ZJS
473 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
474 if (r < 0)
475 return -ENOMEM;
8569a776
LP
476 break;
477 }
478
479 case AF_INET6: {
480 static const unsigned char ipv4_prefix[] = {
481 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
482 };
483
fc25ad25
ZJS
484 if (translate_ipv6 &&
485 memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
4d49b48c 486 const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
fc25ad25
ZJS
487 if (include_port)
488 r = asprintf(&p,
3b1c5241
SL
489 "%u.%u.%u.%u:%u",
490 a[0], a[1], a[2], a[3],
8e38570e 491 be16toh(sa->in6.sin6_port));
fc25ad25
ZJS
492 else
493 r = asprintf(&p,
3b1c5241 494 "%u.%u.%u.%u",
fc25ad25
ZJS
495 a[0], a[1], a[2], a[3]);
496 if (r < 0)
497 return -ENOMEM;
8569a776 498 } else {
071e522e 499 const char *a = IN6_ADDR_TO_STRING(&sa->in6.sin6_addr);
3b1c5241
SL
500
501 if (include_port) {
01afd0f7 502 if (asprintf(&p,
b16d17a6 503 "[%s]:%u%s%s",
3b1c5241 504 a,
b16d17a6
ZJS
505 be16toh(sa->in6.sin6_port),
506 sa->in6.sin6_scope_id != 0 ? "%" : "",
01afd0f7 507 FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)) < 0)
3b1c5241
SL
508 return -ENOMEM;
509 } else {
01afd0f7
YW
510 if (sa->in6.sin6_scope_id != 0)
511 p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX));
512 else
513 p = strdup(a);
3b1c5241
SL
514 if (!p)
515 return -ENOMEM;
516 }
8569a776
LP
517 }
518
519 break;
520 }
521
4d49b48c 522 case AF_UNIX:
15dca371 523 if (salen <= offsetof(struct sockaddr_un, sun_path) ||
994b9d4e 524 (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1))
15dca371 525 /* The name must have at least one character (and the leading NUL does not count) */
4d49b48c 526 p = strdup("<unnamed>");
994b9d4e 527 else {
085b39e9
LP
528 /* Note that we calculate the path pointer here through the .un_buffer[] field, in order to
529 * outtrick bounds checking tools such as ubsan, which are too smart for their own good: on
530 * Linux the kernel may return sun_path[] data one byte longer than the declared size of the
531 * field. */
532 char *path = (char*) sa->un_buffer + offsetof(struct sockaddr_un, sun_path);
15dca371 533 size_t path_len = salen - offsetof(struct sockaddr_un, sun_path);
8569a776 534
085b39e9 535 if (path[0] == 0) {
15dca371
ZJS
536 /* Abstract socket. When parsing address information from, we
537 * explicitly reject overly long paths and paths with embedded NULs.
538 * But we might get such a socket from the outside. Let's return
539 * something meaningful and printable in this case. */
4d49b48c 540
15dca371 541 _cleanup_free_ char *e = NULL;
4d49b48c 542
085b39e9 543 e = cescape_length(path + 1, path_len - 1);
15dca371
ZJS
544 if (!e)
545 return -ENOMEM;
4d49b48c 546
15dca371
ZJS
547 p = strjoin("@", e);
548 } else {
085b39e9 549 if (path[path_len - 1] == '\0')
15dca371 550 /* We expect a terminating NUL and don't print it */
b3a9d980 551 path_len--;
15dca371 552
085b39e9 553 p = cescape_length(path, path_len);
15dca371 554 }
4d49b48c 555 }
994b9d4e
LP
556 if (!p)
557 return -ENOMEM;
8569a776
LP
558
559 break;
8569a776 560
0fc0f14b 561 case AF_VSOCK:
3a484991
ZJS
562 if (include_port) {
563 if (sa->vm.svm_cid == VMADDR_CID_ANY)
564 r = asprintf(&p, "vsock::%u", sa->vm.svm_port);
565 else
566 r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port);
567 } else
0fc0f14b
SH
568 r = asprintf(&p, "vsock:%u", sa->vm.svm_cid);
569 if (r < 0)
570 return -ENOMEM;
571 break;
572
8569a776 573 default:
15411c0c 574 return -EOPNOTSUPP;
8569a776
LP
575 }
576
577 *ret = p;
578 return 0;
579}
580
366b7db4 581int getpeername_pretty(int fd, bool include_port, char **ret) {
4d49b48c 582 union sockaddr_union sa;
b31f535c 583 socklen_t salen = sizeof(sa);
eff05270 584 int r;
4d49b48c
LP
585
586 assert(fd >= 0);
587 assert(ret);
588
4d49b48c
LP
589 if (getpeername(fd, &sa.sa, &salen) < 0)
590 return -errno;
591
592 if (sa.sa.sa_family == AF_UNIX) {
a995ce47 593 struct ucred ucred = UCRED_INVALID;
4d49b48c
LP
594
595 /* UNIX connection sockets are anonymous, so let's use
596 * PID/UID as pretty credentials instead */
597
eff05270
LP
598 r = getpeercred(fd, &ucred);
599 if (r < 0)
600 return r;
4d49b48c 601
de0671ee 602 if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
4d49b48c
LP
603 return -ENOMEM;
604
605 return 0;
606 }
607
608 /* For remote sockets we translate IPv6 addresses back to IPv4
609 * if applicable, since that's nicer. */
610
366b7db4 611 return sockaddr_pretty(&sa.sa, salen, true, include_port, ret);
4d49b48c
LP
612}
613
614int getsockname_pretty(int fd, char **ret) {
615 union sockaddr_union sa;
b31f535c 616 socklen_t salen = sizeof(sa);
4d49b48c
LP
617
618 assert(fd >= 0);
619 assert(ret);
620
4d49b48c
LP
621 if (getsockname(fd, &sa.sa, &salen) < 0)
622 return -errno;
623
624 /* For local sockets we do not translate IPv6 addresses back
625 * to IPv6 if applicable, since this is usually used for
626 * listening sockets where the difference between IPv4 and
627 * IPv6 matters. */
628
3b1c5241 629 return sockaddr_pretty(&sa.sa, salen, false, true, ret);
4d49b48c
LP
630}
631
fc1f05eb
LP
632int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **ret) {
633 char host[NI_MAXHOST];
b31f535c 634 int r;
b31f535c 635
fc1f05eb 636 assert(sa);
cde08933 637 assert(salen >= sizeof(sa_family_t));
4eb3ec63 638 assert(ret);
b31f535c 639
fc1f05eb 640 r = getnameinfo(sa, salen, host, sizeof(host), /* service= */ NULL, /* service_len= */ 0, IDN_FLAGS);
b31f535c 641 if (r != 0) {
fc1f05eb
LP
642 if (r == EAI_MEMORY)
643 return log_oom_debug();
644 if (r == EAI_SYSTEM)
645 log_debug_errno(errno, "getnameinfo() failed, ignoring: %m");
646 else
647 log_debug("getnameinfo() failed, ignoring: %s", gai_strerror(r));
b31f535c 648
fc1f05eb
LP
649 return sockaddr_pretty(sa, salen, /* translate_ipv6= */ true, /* include_port= */ true, ret);
650 }
b31f535c 651
4eb3ec63 652 return strdup_to(ret, host);
b31f535c
ZJS
653}
654
7a22745a 655static const char* const netlink_family_table[] = {
0d7e34e3
ZJS
656 [NETLINK_ROUTE] = "route",
657 [NETLINK_FIREWALL] = "firewall",
658 [NETLINK_INET_DIAG] = "inet-diag",
659 [NETLINK_NFLOG] = "nflog",
660 [NETLINK_XFRM] = "xfrm",
661 [NETLINK_SELINUX] = "selinux",
662 [NETLINK_ISCSI] = "iscsi",
663 [NETLINK_AUDIT] = "audit",
664 [NETLINK_FIB_LOOKUP] = "fib-lookup",
665 [NETLINK_CONNECTOR] = "connector",
666 [NETLINK_NETFILTER] = "netfilter",
667 [NETLINK_IP6_FW] = "ip6-fw",
668 [NETLINK_DNRTMSG] = "dnrtmsg",
7a22745a 669 [NETLINK_KOBJECT_UEVENT] = "kobject-uevent",
0d7e34e3
ZJS
670 [NETLINK_GENERIC] = "generic",
671 [NETLINK_SCSITRANSPORT] = "scsitransport",
672 [NETLINK_ECRYPTFS] = "ecryptfs",
673 [NETLINK_RDMA] = "rdma",
7a22745a
LP
674};
675
f8b69d1d 676DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
7a22745a 677
c0120d99
LP
678static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
679 [SOCKET_ADDRESS_DEFAULT] = "default",
680 [SOCKET_ADDRESS_BOTH] = "both",
681 [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only"
682};
683
684DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only);
f01e5736 685
b54e98ef 686SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *n) {
6f90844f
YW
687 int r;
688
689 r = parse_boolean(n);
690 if (r > 0)
691 return SOCKET_ADDRESS_IPV6_ONLY;
692 if (r == 0)
693 return SOCKET_ADDRESS_BOTH;
694
695 return socket_address_bind_ipv6_only_from_string(n);
696}
697
f01e5736
LP
698bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
699 assert(a);
700 assert(b);
701
702 if (a->sa.sa_family != b->sa.sa_family)
703 return false;
704
705 if (a->sa.sa_family == AF_INET)
706 return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
707
708 if (a->sa.sa_family == AF_INET6)
709 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
710
0fc0f14b
SH
711 if (a->sa.sa_family == AF_VSOCK)
712 return a->vm.svm_cid == b->vm.svm_cid;
713
f01e5736
LP
714 return false;
715}
2583fbea 716
d9d9b2a0 717int fd_set_sndbuf(int fd, size_t n, bool increase) {
2583fbea
LP
718 int r, value;
719 socklen_t l = sizeof(value);
720
1263c85e
YW
721 if (n > INT_MAX)
722 return -ERANGE;
723
2583fbea 724 r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
d9d9b2a0 725 if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
2583fbea
LP
726 return 0;
727
b92f3507
YW
728 /* First, try to set the buffer size with SO_SNDBUF. */
729 r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n);
730 if (r < 0)
731 return r;
2583fbea 732
b92f3507
YW
733 /* SO_SNDBUF above may set to the kernel limit, instead of the requested size.
734 * So, we need to check the actual buffer size here. */
67f5ae2d 735 l = sizeof(value);
b92f3507 736 r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
d9d9b2a0 737 if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
b92f3507
YW
738 return 1;
739
740 /* If we have the privileges we will ignore the kernel limit. */
741 r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n);
742 if (r < 0)
743 return r;
2583fbea
LP
744
745 return 1;
746}
747
d9d9b2a0 748int fd_set_rcvbuf(int fd, size_t n, bool increase) {
2583fbea
LP
749 int r, value;
750 socklen_t l = sizeof(value);
751
1263c85e
YW
752 if (n > INT_MAX)
753 return -ERANGE;
754
2583fbea 755 r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
d9d9b2a0 756 if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
2583fbea
LP
757 return 0;
758
b92f3507
YW
759 /* First, try to set the buffer size with SO_RCVBUF. */
760 r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n);
761 if (r < 0)
762 return r;
2583fbea 763
b92f3507
YW
764 /* SO_RCVBUF above may set to the kernel limit, instead of the requested size.
765 * So, we need to check the actual buffer size here. */
67f5ae2d 766 l = sizeof(value);
b92f3507 767 r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
d9d9b2a0 768 if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
b92f3507
YW
769 return 1;
770
771 /* If we have the privileges we will ignore the kernel limit. */
772 r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n);
773 if (r < 0)
774 return r;
9e5b6496 775
2583fbea
LP
776 return 1;
777}
778
779static const char* const ip_tos_table[] = {
0d7e34e3
ZJS
780 [IPTOS_LOWDELAY] = "low-delay",
781 [IPTOS_THROUGHPUT] = "throughput",
2583fbea 782 [IPTOS_RELIABILITY] = "reliability",
0d7e34e3 783 [IPTOS_LOWCOST] = "low-cost",
2583fbea
LP
784};
785
786DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
787
5a3586db
YW
788bool ifname_valid_char(char a) {
789 if ((unsigned char) a >= 127U)
790 return false;
791
792 if ((unsigned char) a <= 32U)
793 return false;
794
795 if (IN_SET(a,
796 ':', /* colons are used by the legacy "alias" interface logic */
797 '/', /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */
798 '%')) /* %d is used in the kernel's weird foo%d format string naming feature which we really really don't want to ever run into by accident */
799 return false;
800
801 return true;
802}
803
2313524a 804bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
ef76dff2
LP
805 bool numeric = true;
806
807 /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
808 * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
809 * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
810
2313524a
ZJS
811 assert(!(flags & ~_IFNAME_VALID_ALL));
812
ef76dff2
LP
813 if (isempty(p))
814 return false;
815
ee29bd5d
LP
816 /* A valid ifindex? If so, it's valid iff IFNAME_VALID_NUMERIC is set */
817 if (parse_ifindex(p) >= 0)
818 return flags & IFNAME_VALID_NUMERIC;
819
2313524a 820 if (flags & IFNAME_VALID_ALTERNATIVE) {
4252696a
YW
821 if (strlen(p) >= ALTIFNAMSIZ)
822 return false;
823 } else {
824 if (strlen(p) >= IFNAMSIZ)
825 return false;
826 }
ef76dff2 827
49bfc877 828 if (dot_or_dot_dot(p))
ef76dff2
LP
829 return false;
830
07a7441a
LP
831 /* Let's refuse "all" and "default" as interface name, to avoid collisions with the special sysctl
832 * directories /proc/sys/net/{ipv4,ipv6}/conf/{all,default} */
6aebfec3 833 if (!FLAGS_SET(flags, IFNAME_VALID_SPECIAL) && STR_IN_SET(p, "all", "default"))
07a7441a
LP
834 return false;
835
2313524a 836 for (const char *t = p; *t; t++) {
5a3586db 837 if (!ifname_valid_char(*t))
ef76dff2
LP
838 return false;
839
ff25d338 840 numeric = numeric && ascii_isdigit(*t);
ef76dff2
LP
841 }
842
ee29bd5d
LP
843 /* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or
844 * so, let's refuse that. */
845 if (numeric)
846 return false;
ef76dff2
LP
847
848 return true;
849}
850
26808948
SS
851bool address_label_valid(const char *p) {
852
853 if (isempty(p))
854 return false;
855
856 if (strlen(p) >= IFNAMSIZ)
857 return false;
858
859 while (*p) {
860 if ((uint8_t) *p >= 127U)
861 return false;
862
863 if ((uint8_t) *p <= 31U)
864 return false;
865 p++;
866 }
867
868 return true;
869}
870
2583fbea
LP
871int getpeercred(int fd, struct ucred *ucred) {
872 socklen_t n = sizeof(struct ucred);
873 struct ucred u;
2583fbea
LP
874
875 assert(fd >= 0);
876 assert(ucred);
877
fccad706 878 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n) < 0)
2583fbea
LP
879 return -errno;
880
881 if (n != sizeof(struct ucred))
882 return -EIO;
883
bbcc701e
LP
884 /* Check if the data is actually useful and not suppressed due to namespacing issues */
885 if (!pid_is_valid(u.pid))
2583fbea
LP
886 return -ENODATA;
887
bbcc701e
LP
888 /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of
889 * receiving in "invalid" user/group we get the overflow UID/GID. */
890
2583fbea
LP
891 *ucred = u;
892 return 0;
893}
894
895int getpeersec(int fd, char **ret) {
217d8967 896 _cleanup_free_ char *s = NULL;
2583fbea 897 socklen_t n = 64;
2583fbea
LP
898
899 assert(fd >= 0);
900 assert(ret);
901
217d8967
LP
902 for (;;) {
903 s = new0(char, n+1);
904 if (!s)
905 return -ENOMEM;
2583fbea 906
989740eb
LP
907 if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0) {
908 s[n] = 0;
217d8967 909 break;
989740eb 910 }
2583fbea
LP
911
912 if (errno != ERANGE)
913 return -errno;
914
217d8967 915 s = mfree(s);
2583fbea
LP
916 }
917
217d8967 918 if (isempty(s))
2583fbea 919 return -EOPNOTSUPP;
2583fbea 920
ae2a15bc 921 *ret = TAKE_PTR(s);
217d8967 922
2583fbea
LP
923 return 0;
924}
925
43f2c88d 926int getpeergroups(int fd, gid_t **ret) {
9c41e4eb 927 socklen_t n = sizeof(gid_t) * 64U;
43f2c88d
LP
928 _cleanup_free_ gid_t *d = NULL;
929
75f40779 930 assert(fd >= 0);
43f2c88d
LP
931 assert(ret);
932
7e8aa5c2 933 long ngroups_max = sysconf(_SC_NGROUPS_MAX);
9c41e4eb
LB
934 if (ngroups_max > 0)
935 n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max);
7e8aa5c2 936
43f2c88d
LP
937 for (;;) {
938 d = malloc(n);
939 if (!d)
940 return -ENOMEM;
941
942 if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
943 break;
944
945 if (errno != ERANGE)
946 return -errno;
947
948 d = mfree(d);
949 }
950
951 assert_se(n % sizeof(gid_t) == 0);
952 n /= sizeof(gid_t);
953
7e8aa5c2 954 if (n > INT_MAX)
43f2c88d
LP
955 return -E2BIG;
956
1cc6c93a 957 *ret = TAKE_PTR(d);
43f2c88d
LP
958
959 return (int) n;
960}
961
da5e0c44
LP
962int getpeerpidfd(int fd) {
963 socklen_t n = sizeof(int);
964 int pidfd = -EBADF;
965
966 assert(fd >= 0);
967
968 if (getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &n) < 0)
969 return -errno;
970
971 if (n != sizeof(int))
972 return -EIO;
973
974 return pidfd;
975}
976
598d2428
LB
977ssize_t send_many_fds_iov_sa(
978 int transport_fd,
979 int *fds_array, size_t n_fds_array,
980 const struct iovec *iov, size_t iovlen,
981 const struct sockaddr *sa, socklen_t len,
982 int flags) {
983
984 _cleanup_free_ struct cmsghdr *cmsg = NULL;
985 struct msghdr mh = {
986 .msg_name = (struct sockaddr*) sa,
987 .msg_namelen = len,
988 .msg_iov = (struct iovec *)iov,
989 .msg_iovlen = iovlen,
990 };
991 ssize_t k;
992
993 assert(transport_fd >= 0);
994 assert(fds_array || n_fds_array == 0);
995
996 /* The kernel will reject sending more than SCM_MAX_FD FDs at once */
997 if (n_fds_array > SCM_MAX_FD)
998 return -E2BIG;
999
1000 /* We need either an FD array or data to send. If there's nothing, return an error. */
1001 if (n_fds_array == 0 && !iov)
1002 return -EINVAL;
1003
1004 if (n_fds_array > 0) {
1005 mh.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds_array);
1006 mh.msg_control = cmsg = malloc(mh.msg_controllen);
1007 if (!cmsg)
1008 return -ENOMEM;
1009
1010 *cmsg = (struct cmsghdr) {
1011 .cmsg_len = CMSG_LEN(sizeof(int) * n_fds_array),
1012 .cmsg_level = SOL_SOCKET,
1013 .cmsg_type = SCM_RIGHTS,
1014 };
1015 memcpy(CMSG_DATA(cmsg), fds_array, sizeof(int) * n_fds_array);
1016 }
1017 k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
1018 if (k < 0)
1019 return (ssize_t) -errno;
1020
1021 return k;
1022}
1023
d34673ec 1024ssize_t send_one_fd_iov_sa(
726f4c47
ZJS
1025 int transport_fd,
1026 int fd,
f621b8d7 1027 const struct iovec *iov, size_t iovlen,
726f4c47
ZJS
1028 const struct sockaddr *sa, socklen_t len,
1029 int flags) {
1030
fb29cdbe 1031 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control = {};
2583fbea 1032 struct msghdr mh = {
726f4c47
ZJS
1033 .msg_name = (struct sockaddr*) sa,
1034 .msg_namelen = len,
f621b8d7 1035 .msg_iov = (struct iovec *)iov,
d34673ec 1036 .msg_iovlen = iovlen,
2583fbea 1037 };
d34673ec 1038 ssize_t k;
2583fbea
LP
1039
1040 assert(transport_fd >= 0);
2583fbea 1041
d34673ec
FB
1042 /*
1043 * We need either an FD or data to send.
1044 * If there's nothing, return an error.
1045 */
1046 if (fd < 0 && !iov)
1047 return -EINVAL;
2583fbea 1048
d34673ec
FB
1049 if (fd >= 0) {
1050 struct cmsghdr *cmsg;
2583fbea 1051
d34673ec
FB
1052 mh.msg_control = &control;
1053 mh.msg_controllen = sizeof(control);
1054
1055 cmsg = CMSG_FIRSTHDR(&mh);
1056 cmsg->cmsg_level = SOL_SOCKET;
1057 cmsg->cmsg_type = SCM_RIGHTS;
1058 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
1059 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
d34673ec
FB
1060 }
1061 k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
1062 if (k < 0)
1063 return (ssize_t) -errno;
1064
1065 return k;
2583fbea
LP
1066}
1067
d34673ec
FB
1068int send_one_fd_sa(
1069 int transport_fd,
1070 int fd,
1071 const struct sockaddr *sa, socklen_t len,
1072 int flags) {
1073
1074 assert(fd >= 0);
1075
1076 return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags);
1077}
1078
598d2428
LB
1079ssize_t receive_many_fds_iov(
1080 int transport_fd,
1081 struct iovec *iov, size_t iovlen,
1082 int **ret_fds_array, size_t *ret_n_fds_array,
1083 int flags) {
1084
1085 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * SCM_MAX_FD)) control;
1086 struct msghdr mh = {
1087 .msg_control = &control,
1088 .msg_controllen = sizeof(control),
1089 .msg_iov = iov,
1090 .msg_iovlen = iovlen,
1091 };
1092 _cleanup_free_ int *fds_array = NULL;
1093 size_t n_fds_array = 0;
1094 struct cmsghdr *cmsg;
1095 ssize_t k;
1096
1097 assert(transport_fd >= 0);
1098 assert(ret_fds_array);
1099 assert(ret_n_fds_array);
1100
1101 /*
1102 * Receive many FDs via @transport_fd. We don't care for the transport-type. We retrieve all the FDs
1103 * at once. This is best used in combination with send_many_fds().
1104 */
1105
1106 k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
1107 if (k < 0)
1108 return k;
1109
1110 CMSG_FOREACH(cmsg, &mh)
1111 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1112 size_t n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
1113
1114 fds_array = GREEDY_REALLOC(fds_array, n_fds_array + n);
1115 if (!fds_array) {
1116 cmsg_close_all(&mh);
1117 return -ENOMEM;
1118 }
1119
1120 memcpy(fds_array + n_fds_array, CMSG_TYPED_DATA(cmsg, int), sizeof(int) * n);
1121 n_fds_array += n;
1122 }
1123
1124 if (n_fds_array == 0) {
1125 cmsg_close_all(&mh);
1126
1127 /* If didn't receive an FD or any data, return an error. */
1128 if (k == 0)
1129 return -EIO;
1130 }
1131
1132 *ret_fds_array = TAKE_PTR(fds_array);
1133 *ret_n_fds_array = n_fds_array;
1134
1135 return k;
1136}
1137
1138int receive_many_fds(int transport_fd, int **ret_fds_array, size_t *ret_n_fds_array, int flags) {
1139 ssize_t k;
1140
1141 k = receive_many_fds_iov(transport_fd, NULL, 0, ret_fds_array, ret_n_fds_array, flags);
1142 if (k == 0)
1143 return 0;
1144
1145 /* k must be negative, since receive_many_fds_iov() only returns a positive value if data was received
1146 * through the iov. */
1147 assert(k < 0);
1148 return (int) k;
1149}
1150
d34673ec
FB
1151ssize_t receive_one_fd_iov(
1152 int transport_fd,
1153 struct iovec *iov, size_t iovlen,
1154 int flags,
1155 int *ret_fd) {
1156
fb29cdbe 1157 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control;
2583fbea
LP
1158 struct msghdr mh = {
1159 .msg_control = &control,
1160 .msg_controllen = sizeof(control),
d34673ec
FB
1161 .msg_iov = iov,
1162 .msg_iovlen = iovlen,
2583fbea 1163 };
dac556fa 1164 struct cmsghdr *found;
d34673ec 1165 ssize_t k;
2583fbea
LP
1166
1167 assert(transport_fd >= 0);
d34673ec 1168 assert(ret_fd);
2583fbea
LP
1169
1170 /*
1171 * Receive a single FD via @transport_fd. We don't care for
1172 * the transport-type. We retrieve a single FD at most, so for
1173 * packet-based transports, the caller must ensure to send
1174 * only a single FD per packet. This is best used in
1175 * combination with send_one_fd().
1176 */
1177
3691bcf3 1178 k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
d34673ec 1179 if (k < 0)
3691bcf3 1180 return k;
2583fbea 1181
dac556fa 1182 found = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int)));
3691bcf3 1183 if (!found) {
2583fbea 1184 cmsg_close_all(&mh);
d34673ec 1185
3691bcf3
LP
1186 /* If didn't receive an FD or any data, return an error. */
1187 if (k == 0)
1188 return -EIO;
1189 }
2583fbea 1190
d34673ec 1191 if (found)
b1d02191 1192 *ret_fd = *CMSG_TYPED_DATA(found, int);
d34673ec 1193 else
254d1313 1194 *ret_fd = -EBADF;
d34673ec
FB
1195
1196 return k;
1197}
1198
1199int receive_one_fd(int transport_fd, int flags) {
1200 int fd;
1201 ssize_t k;
1202
1203 k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd);
1204 if (k == 0)
1205 return fd;
1206
1207 /* k must be negative, since receive_one_fd_iov() only returns
1208 * a positive value if data was received through the iov. */
1209 assert(k < 0);
1210 return (int) k;
2583fbea 1211}
4edc2c9b
LP
1212
1213ssize_t next_datagram_size_fd(int fd) {
1214 ssize_t l;
1215 int k;
1216
1217 /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will
96d49011 1218 * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't
4edc2c9b
LP
1219 * do. This difference is actually of major importance as we need to be sure that the size returned here
1220 * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of
1221 * the wrong size. */
1222
1223 l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC);
1224 if (l < 0) {
3742095b 1225 if (IN_SET(errno, EOPNOTSUPP, EFAULT))
4edc2c9b
LP
1226 goto fallback;
1227
1228 return -errno;
1229 }
1230 if (l == 0)
1231 goto fallback;
1232
1233 return l;
1234
1235fallback:
1236 k = 0;
1237
1238 /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD
1239 * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */
1240
1241 if (ioctl(fd, FIONREAD, &k) < 0)
1242 return -errno;
1243
1244 return (ssize_t) k;
1245}
60d9771c 1246
67962036
ZJS
1247/* Put a limit on how many times will attempt to call accept4(). We loop
1248 * only on "transient" errors, but let's make sure we don't loop forever. */
1249#define MAX_FLUSH_ITERATIONS 1024
1250
60d9771c
LP
1251int flush_accept(int fd) {
1252
f3d75364
LP
1253 int r, b;
1254 socklen_t l = sizeof(b);
1255
644cb4cc
ZJS
1256 /* Similar to flush_fd() but flushes all incoming connections by accepting and immediately closing
1257 * them. */
f3d75364
LP
1258
1259 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0)
1260 return -errno;
60d9771c 1261
f3d75364 1262 assert(l == sizeof(b));
644cb4cc
ZJS
1263 if (!b) /* Let's check if this socket accepts connections before calling accept(). accept4() can
1264 * return EOPNOTSUPP if the fd is not a listening socket, which we should treat as a fatal
1265 * error, or in case the incoming TCP connection triggered a network issue, which we want to
1266 * treat as a transient error. Thus, let's rule out the first reason for EOPNOTSUPP early, so
1267 * we can loop safely on transient errors below. */
f3d75364 1268 return -ENOTTY;
60d9771c 1269
67962036 1270 for (unsigned iteration = 0;; iteration++) {
60d9771c
LP
1271 int cfd;
1272
0f2d351f 1273 r = fd_wait_for_event(fd, POLLIN, 0);
60d9771c 1274 if (r < 0) {
0f2d351f 1275 if (r == -EINTR)
60d9771c
LP
1276 continue;
1277
0f2d351f 1278 return r;
4ff9bc2e
LP
1279 }
1280 if (r == 0)
60d9771c
LP
1281 return 0;
1282
67962036
ZJS
1283 if (iteration >= MAX_FLUSH_ITERATIONS)
1284 return log_debug_errno(SYNTHETIC_ERRNO(EBUSY),
1285 "Failed to flush connections within " STRINGIFY(MAX_FLUSH_ITERATIONS) " iterations.");
1286
60d9771c
LP
1287 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
1288 if (cfd < 0) {
60d9771c
LP
1289 if (errno == EAGAIN)
1290 return 0;
1291
4ff9bc2e
LP
1292 if (ERRNO_IS_ACCEPT_AGAIN(errno))
1293 continue;
1294
60d9771c
LP
1295 return -errno;
1296 }
1297
4ff9bc2e 1298 safe_close(cfd);
60d9771c
LP
1299 }
1300}
29206d46
LP
1301
1302struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) {
1303 struct cmsghdr *cmsg;
1304
1305 assert(mh);
1306
1307 CMSG_FOREACH(cmsg, mh)
1308 if (cmsg->cmsg_level == level &&
1309 cmsg->cmsg_type == type &&
1310 (length == (socklen_t) -1 || length == cmsg->cmsg_len))
1311 return cmsg;
1312
1313 return NULL;
1314}
429b4350 1315
4836f4c6
YW
1316void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len) {
1317 struct cmsghdr *cmsg;
1318
1319 assert(mh);
1320 assert(buf);
1321 assert(buf_len > 0);
1322
1323 /* This is similar to cmsg_find_data(), but copy the found data to buf. This should be typically used
da890466 1324 * when reading possibly unaligned data such as timestamp, as time_t is 64-bit and size_t is 32-bit on
4836f4c6
YW
1325 * RISCV32. See issue #27241. */
1326
1327 cmsg = cmsg_find(mh, level, type, CMSG_LEN(buf_len));
1328 if (!cmsg)
1329 return NULL;
1330
1331 return memcpy_safe(buf, CMSG_DATA(cmsg), buf_len);
1332}
1333
429b4350
LP
1334int socket_ioctl_fd(void) {
1335 int fd;
1336
1337 /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for
1338 * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not
1339 * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more
1340 * generic AF_NETLINK. */
1341
1342 fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1343 if (fd < 0)
1344 fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC);
1345 if (fd < 0)
1346 return -errno;
1347
1348 return fd;
1349}
9f20fc28
LP
1350
1351int sockaddr_un_unlink(const struct sockaddr_un *sa) {
1352 const char *p, * nul;
1353
1354 assert(sa);
1355
1356 if (sa->sun_family != AF_UNIX)
1357 return -EPROTOTYPE;
1358
1359 if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */
1360 return 0;
1361
1362 /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */
1363 nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path));
1364 if (nul)
1365 p = sa->sun_path;
1366 else
1367 p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path));
1368
1369 if (unlink(p) < 0)
1370 return -errno;
1371
1372 return 1;
1373}
5cf91ea9
LP
1374
1375int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path) {
1376 size_t l;
1377
1378 assert(ret);
1379 assert(path);
1380
1381 /* Initialize ret->sun_path from the specified argument. This will interpret paths starting with '@' as
1382 * abstract namespace sockets, and those starting with '/' as regular filesystem sockets. It won't accept
1383 * anything else (i.e. no relative paths), to avoid ambiguities. Note that this function cannot be used to
1384 * reference paths in the abstract namespace that include NUL bytes in the name. */
1385
1386 l = strlen(path);
c097bf1f 1387 if (l < 2)
5cf91ea9
LP
1388 return -EINVAL;
1389 if (!IN_SET(path[0], '/', '@'))
1390 return -EINVAL;
5cf91ea9
LP
1391
1392 /* Don't allow paths larger than the space in sockaddr_un. Note that we are a tiny bit more restrictive than
1393 * the kernel is: we insist on NUL termination (both for abstract namespace and regular file system socket
1394 * addresses!), which the kernel doesn't. We do this to reduce chance of incompatibility with other apps that
7802194a 1395 * do not expect non-NUL terminated file system path. */
5cf91ea9 1396 if (l+1 > sizeof(ret->sun_path))
dfa2b389
LP
1397 return path[0] == '@' ? -EINVAL : -ENAMETOOLONG; /* return a recognizable error if this is
1398 * too long to fit into a sockaddr_un, but
1399 * is a file system path, and thus might be
1400 * connectible via O_PATH indirection. */
5cf91ea9
LP
1401
1402 *ret = (struct sockaddr_un) {
1403 .sun_family = AF_UNIX,
1404 };
1405
1406 if (path[0] == '@') {
1407 /* Abstract namespace socket */
1408 memcpy(ret->sun_path + 1, path + 1, l); /* copy *with* trailing NUL byte */
1409 return (int) (offsetof(struct sockaddr_un, sun_path) + l); /* 🔥 *don't* 🔥 include trailing NUL in size */
1410
1411 } else {
1412 assert(path[0] == '/');
1413
1414 /* File system socket */
1415 memcpy(ret->sun_path, path, l + 1); /* copy *with* trailing NUL byte */
1416 return (int) (offsetof(struct sockaddr_un, sun_path) + l + 1); /* include trailing NUL in size */
1417 }
1418}
5d594d01
LP
1419
1420int socket_bind_to_ifname(int fd, const char *ifname) {
1421 assert(fd >= 0);
1422
1423 /* Call with NULL to drop binding */
1424
7c248223 1425 return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname)));
5d594d01
LP
1426}
1427
1428int socket_bind_to_ifindex(int fd, int ifindex) {
01afd0f7 1429 char ifname[IF_NAMESIZE];
5e958e1d 1430 int r;
5d594d01
LP
1431
1432 assert(fd >= 0);
1433
7c248223 1434 if (ifindex <= 0)
5d594d01 1435 /* Drop binding */
7c248223 1436 return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0));
5d594d01 1437
5e958e1d
YW
1438 r = setsockopt_int(fd, SOL_SOCKET, SO_BINDTOIFINDEX, ifindex);
1439 if (r != -ENOPROTOOPT)
1440 return r;
5d594d01
LP
1441
1442 /* Fall back to SO_BINDTODEVICE on kernels < 5.0 which didn't have SO_BINDTOIFINDEX */
01afd0f7
YW
1443 r = format_ifname(ifindex, ifname);
1444 if (r < 0)
1445 return r;
5d594d01
LP
1446
1447 return socket_bind_to_ifname(fd, ifname);
1448}
47eae6ce
LP
1449
1450ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) {
1451 ssize_t n;
1452
1453 /* A wrapper around recvmsg() that checks for MSG_CTRUNC, and turns it into an error, in a reasonably
1454 * safe way, closing any SCM_RIGHTS fds in the error path.
1455 *
1456 * Note that unlike our usual coding style this might modify *msg on failure. */
1457
1458 n = recvmsg(sockfd, msg, flags);
1459 if (n < 0)
1460 return -errno;
1461
1462 if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC)) {
1463 cmsg_close_all(msg);
1464 return -EXFULL; /* a recognizable error code */
1465 }
1466
1467 return n;
35a3eb9b
LP
1468}
1469
5f64d2bf 1470int socket_get_family(int fd) {
35a3eb9b
LP
1471 int af;
1472 socklen_t sl = sizeof(af);
1473
1474 if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
1475 return -errno;
1476
5d0fe423
LP
1477 if (sl != sizeof(af))
1478 return -EINVAL;
1479
1480 return af;
1481}
1482
1483int socket_set_recvpktinfo(int fd, int af, bool b) {
5d0fe423
LP
1484
1485 if (af == AF_UNSPEC) {
5f64d2bf
LP
1486 af = socket_get_family(fd);
1487 if (af < 0)
1488 return af;
5d0fe423
LP
1489 }
1490
35a3eb9b
LP
1491 switch (af) {
1492
1493 case AF_INET:
1494 return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
47eae6ce 1495
35a3eb9b
LP
1496 case AF_INET6:
1497 return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
1498
1499 case AF_NETLINK:
1500 return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
1501
2d6d4136
LP
1502 case AF_PACKET:
1503 return setsockopt_int(fd, SOL_PACKET, PACKET_AUXDATA, b);
1504
35a3eb9b
LP
1505 default:
1506 return -EAFNOSUPPORT;
1507 }
47eae6ce 1508}
5d0fe423 1509
5d0fe423
LP
1510int socket_set_unicast_if(int fd, int af, int ifi) {
1511 be32_t ifindex_be = htobe32(ifi);
5d0fe423
LP
1512
1513 if (af == AF_UNSPEC) {
5f64d2bf
LP
1514 af = socket_get_family(fd);
1515 if (af < 0)
1516 return af;
5d0fe423
LP
1517 }
1518
1519 switch (af) {
1520
1521 case AF_INET:
7c248223 1522 return RET_NERRNO(setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
5d0fe423
LP
1523
1524 case AF_INET6:
7c248223 1525 return RET_NERRNO(setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
5d0fe423
LP
1526
1527 default:
1528 return -EAFNOSUPPORT;
1529 }
1530}
1531
402506ce 1532int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val) {
5d0fe423 1533 if (af == AF_UNSPEC) {
5f64d2bf
LP
1534 af = socket_get_family(fd);
1535 if (af < 0)
1536 return af;
5d0fe423
LP
1537 }
1538
1539 switch (af) {
1540
1541 case AF_INET:
402506ce 1542 return setsockopt_int(fd, IPPROTO_IP, opt_ipv4, val);
5d0fe423
LP
1543
1544 case AF_INET6:
402506ce 1545 return setsockopt_int(fd, IPPROTO_IPV6, opt_ipv6, val);
5d0fe423
LP
1546
1547 default:
1548 return -EAFNOSUPPORT;
1549 }
1550}
52975f86
LP
1551
1552int socket_get_mtu(int fd, int af, size_t *ret) {
1553 int mtu, r;
1554
1555 if (af == AF_UNSPEC) {
5f64d2bf
LP
1556 af = socket_get_family(fd);
1557 if (af < 0)
1558 return af;
52975f86
LP
1559 }
1560
1561 switch (af) {
1562
1563 case AF_INET:
1564 r = getsockopt_int(fd, IPPROTO_IP, IP_MTU, &mtu);
1565 break;
1566
1567 case AF_INET6:
1568 r = getsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, &mtu);
1569 break;
1570
1571 default:
1572 return -EAFNOSUPPORT;
1573 }
1574
1575 if (r < 0)
1576 return r;
1577 if (mtu <= 0)
1578 return -EINVAL;
1579
1580 *ret = (size_t) mtu;
1581 return 0;
1582}
2679aee4 1583
9a603dc2 1584static int connect_unix_path_simple(int fd, const char *path) {
2679aee4
LP
1585 union sockaddr_union sa = {
1586 .un.sun_family = AF_UNIX,
1587 };
9a603dc2 1588 size_t l;
2679aee4
LP
1589
1590 assert(fd >= 0);
2679aee4
LP
1591 assert(path);
1592
9a603dc2
LP
1593 l = strlen(path);
1594 assert(l > 0);
1595 assert(l < sizeof(sa.un.sun_path));
1596
1597 memcpy(sa.un.sun_path, path, l + 1);
1598 return RET_NERRNO(connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + l + 1));
1599}
1600
1601static int connect_unix_inode(int fd, int inode_fd) {
1602 assert(fd >= 0);
1603 assert(inode_fd >= 0);
1604
1605 return connect_unix_path_simple(fd, FORMAT_PROC_FD_PATH(inode_fd));
1606}
1607
1608int connect_unix_path(int fd, int dir_fd, const char *path) {
1609 _cleanup_close_ int inode_fd = -EBADF;
1610
1611 assert(fd >= 0);
1612 assert(dir_fd == AT_FDCWD || dir_fd >= 0);
1613
2679aee4
LP
1614 /* Connects to the specified AF_UNIX socket in the file system. Works around the 108 byte size limit
1615 * in sockaddr_un, by going via O_PATH if needed. This hence works for any kind of path. */
1616
9a603dc2
LP
1617 if (!path)
1618 return connect_unix_inode(fd, dir_fd); /* If no path is specified, then dir_fd refers to the socket inode to connect to. */
2679aee4
LP
1619
1620 /* Refuse zero length path early, to make sure AF_UNIX stack won't mistake this for an abstract
1621 * namespace path, since first char is NUL */
9a603dc2 1622 if (isempty(path))
2679aee4
LP
1623 return -EINVAL;
1624
9a603dc2
LP
1625 /* Shortcut for the simple case */
1626 if (dir_fd == AT_FDCWD && strlen(path) < sizeof_field(struct sockaddr_un, sun_path))
1627 return connect_unix_path_simple(fd, path);
2679aee4 1628
9a603dc2
LP
1629 /* If dir_fd is specified, then we need to go the indirect O_PATH route, because connectat() does not
1630 * exist. If the path is too long, we also need to take the indirect route, since we can't fit this
1631 * into a sockaddr_un directly. */
2679aee4 1632
9a603dc2
LP
1633 inode_fd = openat(dir_fd, path, O_PATH|O_CLOEXEC);
1634 if (inode_fd < 0)
1635 return -errno;
2679aee4 1636
9a603dc2 1637 return connect_unix_inode(fd, inode_fd);
2679aee4 1638}
747b5d96
LB
1639
1640int socket_address_parse_unix(SocketAddress *ret_address, const char *s) {
1641 struct sockaddr_un un;
1642 int r;
1643
1644 assert(ret_address);
1645 assert(s);
1646
1647 if (!IN_SET(*s, '/', '@'))
1648 return -EPROTO;
1649
1650 r = sockaddr_un_set_path(&un, s);
1651 if (r < 0)
1652 return r;
1653
1654 *ret_address = (SocketAddress) {
1655 .sockaddr.un = un,
1656 .size = r,
1657 };
1658
1659 return 0;
1660}
1661
8e471c6a
LP
1662int vsock_parse_port(const char *s, unsigned *ret) {
1663 int r;
1664
1665 assert(ret);
1666
1667 if (!s)
1668 return -EINVAL;
1669
1670 unsigned u;
1671 r = safe_atou(s, &u);
1672 if (r < 0)
1673 return r;
1674
1675 /* Port 0 is apparently valid and not special in AF_VSOCK (unlike on IP). But VMADDR_PORT_ANY
1676 * (UINT32_MAX) is. Hence refuse that. */
1677
1678 if (u == VMADDR_PORT_ANY)
1679 return -EINVAL;
1680
1681 *ret = u;
1682 return 0;
1683}
1684
1685int vsock_parse_cid(const char *s, unsigned *ret) {
1686 assert(ret);
1687
1688 if (!s)
1689 return -EINVAL;
1690
1691 /* Parsed an AF_VSOCK "CID". This is a 32bit entity, and the usual type is "unsigned". We recognize
1692 * the three special CIDs as strings, and otherwise parse the numeric CIDs. */
1693
1694 if (streq(s, "hypervisor"))
1695 *ret = VMADDR_CID_HYPERVISOR;
1696 else if (streq(s, "local"))
1697 *ret = VMADDR_CID_LOCAL;
1698 else if (streq(s, "host"))
1699 *ret = VMADDR_CID_HOST;
1700 else
1701 return safe_atou(s, ret);
1702
1703 return 0;
1704}
1705
747b5d96
LB
1706int socket_address_parse_vsock(SocketAddress *ret_address, const char *s) {
1707 /* AF_VSOCK socket in vsock:cid:port notation */
1708 _cleanup_free_ char *n = NULL;
1709 char *e, *cid_start;
1710 unsigned port, cid;
c31984e3 1711 int type, r;
747b5d96
LB
1712
1713 assert(ret_address);
1714 assert(s);
1715
c31984e3
DDM
1716 if ((cid_start = startswith(s, "vsock:")))
1717 type = 0;
1718 else if ((cid_start = startswith(s, "vsock-dgram:")))
1719 type = SOCK_DGRAM;
1720 else if ((cid_start = startswith(s, "vsock-seqpacket:")))
1721 type = SOCK_SEQPACKET;
1722 else if ((cid_start = startswith(s, "vsock-stream:")))
1723 type = SOCK_STREAM;
1724 else
747b5d96
LB
1725 return -EPROTO;
1726
1727 e = strchr(cid_start, ':');
1728 if (!e)
1729 return -EINVAL;
1730
8e471c6a 1731 r = vsock_parse_port(e+1, &port);
747b5d96
LB
1732 if (r < 0)
1733 return r;
1734
1735 n = strndup(cid_start, e - cid_start);
1736 if (!n)
1737 return -ENOMEM;
1738
1739 if (isempty(n))
1740 cid = VMADDR_CID_ANY;
1741 else {
8e471c6a 1742 r = vsock_parse_cid(n, &cid);
747b5d96
LB
1743 if (r < 0)
1744 return r;
1745 }
1746
1747 *ret_address = (SocketAddress) {
1748 .sockaddr.vm = {
747b5d96 1749 .svm_family = AF_VSOCK,
8e471c6a 1750 .svm_cid = cid,
747b5d96
LB
1751 .svm_port = port,
1752 },
c31984e3 1753 .type = type,
747b5d96
LB
1754 .size = sizeof(struct sockaddr_vm),
1755 };
1756
1757 return 0;
1758}
d3109d8d
LP
1759
1760int vsock_get_local_cid(unsigned *ret) {
1761 _cleanup_close_ int vsock_fd = -EBADF;
1762
1763 assert(ret);
1764
1765 vsock_fd = open("/dev/vsock", O_RDONLY|O_CLOEXEC);
1766 if (vsock_fd < 0)
1767 return log_debug_errno(errno, "Failed to open /dev/vsock: %m");
1768
1769 if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, ret) < 0)
1770 return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m");
1771
1772 return 0;
1773}