]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/socket-util.c
Merge pull request #15940 from keszybz/names-set-optimization
[thirdparty/systemd.git] / src / basic / socket-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
93cc7779 24#include "log.h"
42f4e3c4 25#include "macro.h"
0a970718 26#include "memory-util.h"
f5947a5e 27#include "missing_socket.h"
6bedfcbb 28#include "parse-util.h"
9eb977db 29#include "path-util.h"
dccca82b 30#include "process-util.h"
2583fbea 31#include "socket-util.h"
8b43440b 32#include "string-table.h"
07630cea 33#include "string-util.h"
ef76dff2 34#include "strv.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
398ce0bc 44static const char* const socket_address_type_table[] = {
955bb7fa
ZJS
45 [SOCK_STREAM] = "Stream",
46 [SOCK_DGRAM] = "Datagram",
47 [SOCK_RAW] = "Raw",
48 [SOCK_RDM] = "ReliableDatagram",
398ce0bc 49 [SOCK_SEQPACKET] = "SequentialPacket",
955bb7fa 50 [SOCK_DCCP] = "DatagramCongestionControl",
398ce0bc
YW
51};
52
53DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int);
54
15dca371 55int socket_address_verify(const SocketAddress *a, bool strict) {
42f4e3c4
LP
56 assert(a);
57
15dca371
ZJS
58 /* With 'strict' we enforce additional sanity constraints which are not set by the standard,
59 * but should only apply to sockets we create ourselves. */
60
542563ba 61 switch (socket_address_family(a)) {
42f4e3c4 62
7a22745a
LP
63 case AF_INET:
64 if (a->size != sizeof(struct sockaddr_in))
65 return -EINVAL;
42f4e3c4 66
4d49b48c 67 if (a->sockaddr.in.sin_port == 0)
7a22745a 68 return -EINVAL;
42f4e3c4 69
ec2ce0c5 70 if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM))
7a22745a 71 return -EINVAL;
42f4e3c4 72
7a22745a
LP
73 return 0;
74
75 case AF_INET6:
76 if (a->size != sizeof(struct sockaddr_in6))
77 return -EINVAL;
42f4e3c4 78
7a22745a
LP
79 if (a->sockaddr.in6.sin6_port == 0)
80 return -EINVAL;
42f4e3c4 81
ec2ce0c5 82 if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM))
7a22745a 83 return -EINVAL;
42f4e3c4 84
7a22745a 85 return 0;
42f4e3c4 86
7a22745a
LP
87 case AF_UNIX:
88 if (a->size < offsetof(struct sockaddr_un, sun_path))
89 return -EINVAL;
15dca371
ZJS
90 if (a->size > sizeof(struct sockaddr_un) + !strict)
91 /* If !strict, allow one extra byte, since getsockname() on Linux will append
92 * a NUL byte if we have path sockets that are above sun_path's full size. */
8e8132c6 93 return -EINVAL;
42f4e3c4 94
8e8132c6 95 if (a->size > offsetof(struct sockaddr_un, sun_path) &&
15dca371
ZJS
96 a->sockaddr.un.sun_path[0] != 0 &&
97 strict) {
98 /* Only validate file system sockets here, and only in strict mode */
8e8132c6 99 const char *e;
7a22745a 100
8e8132c6
LP
101 e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
102 if (e) {
15dca371 103 /* If there's an embedded NUL byte, make sure the size of the socket address matches it */
7a22745a
LP
104 if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
105 return -EINVAL;
8e8132c6
LP
106 } else {
107 /* If there's no embedded NUL byte, then then the size needs to match the whole
108 * structure or the structure with one extra NUL byte suffixed. (Yeah, Linux is awful,
109 * and considers both equivalent: getsockname() even extends sockaddr_un beyond its
110 * size if the path is non NUL terminated.)*/
111 if (!IN_SET(a->size, sizeof(a->sockaddr.un.sun_path), sizeof(a->sockaddr.un.sun_path)+1))
112 return -EINVAL;
42f4e3c4 113 }
7a22745a 114 }
42f4e3c4 115
ec2ce0c5 116 if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET))
7a22745a 117 return -EINVAL;
42f4e3c4 118
7a22745a
LP
119 return 0;
120
121 case AF_NETLINK:
122
123 if (a->size != sizeof(struct sockaddr_nl))
124 return -EINVAL;
125
ec2ce0c5 126 if (!IN_SET(a->type, SOCK_RAW, SOCK_DGRAM))
7a22745a
LP
127 return -EINVAL;
128
129 return 0;
130
0fc0f14b
SH
131 case AF_VSOCK:
132 if (a->size != sizeof(struct sockaddr_vm))
133 return -EINVAL;
134
ec2ce0c5 135 if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM))
0fc0f14b
SH
136 return -EINVAL;
137
138 return 0;
139
7a22745a
LP
140 default:
141 return -EAFNOSUPPORT;
42f4e3c4
LP
142 }
143}
144
4d49b48c 145int socket_address_print(const SocketAddress *a, char **ret) {
42f4e3c4 146 int r;
4d49b48c 147
42f4e3c4 148 assert(a);
4d49b48c 149 assert(ret);
42f4e3c4 150
15dca371
ZJS
151 r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be
152 * able to pretty-print any socket the kernel considers
153 * valid. We still need to do validation to know if we
154 * can meaningfully print the address. */
4d49b48c 155 if (r < 0)
42f4e3c4
LP
156 return r;
157
4d49b48c 158 if (socket_address_family(a) == AF_NETLINK) {
7fd1b19b 159 _cleanup_free_ char *sfamily = NULL;
7a22745a 160
f8b69d1d 161 r = netlink_family_to_string_alloc(a->protocol, &sfamily);
7a22745a 162 if (r < 0)
f8b69d1d 163 return r;
4d49b48c
LP
164
165 r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
8520cfa5
MS
166 if (r < 0)
167 return -ENOMEM;
7a22745a
LP
168
169 return 0;
170 }
171
3b1c5241 172 return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
42f4e3c4
LP
173}
174
4f2d528d
LP
175bool socket_address_can_accept(const SocketAddress *a) {
176 assert(a);
177
178 return
3742095b 179 IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET);
4f2d528d 180}
a16e1123
LP
181
182bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
183 assert(a);
184 assert(b);
185
186 /* Invalid addresses are unequal to all */
15dca371
ZJS
187 if (socket_address_verify(a, false) < 0 ||
188 socket_address_verify(b, false) < 0)
a16e1123
LP
189 return false;
190
191 if (a->type != b->type)
192 return false;
193
a16e1123
LP
194 if (socket_address_family(a) != socket_address_family(b))
195 return false;
196
197 switch (socket_address_family(a)) {
198
199 case AF_INET:
4d49b48c 200 if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
a16e1123
LP
201 return false;
202
4d49b48c 203 if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
a16e1123
LP
204 return false;
205
206 break;
207
208 case AF_INET6:
209 if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
210 return false;
211
212 if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
213 return false;
214
215 break;
216
217 case AF_UNIX:
710708a5
MS
218 if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
219 b->size <= offsetof(struct sockaddr_un, sun_path))
220 return false;
221
a16e1123
LP
222 if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
223 return false;
224
225 if (a->sockaddr.un.sun_path[0]) {
e3f791a2 226 if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
a16e1123
LP
227 return false;
228 } else {
c78e47a6
MS
229 if (a->size != b->size)
230 return false;
231
b12c1e7c 232 if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
a16e1123
LP
233 return false;
234 }
235
236 break;
237
7a22745a 238 case AF_NETLINK:
7a22745a
LP
239 if (a->protocol != b->protocol)
240 return false;
241
242 if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
243 return false;
244
245 break;
246
0fc0f14b
SH
247 case AF_VSOCK:
248 if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid)
249 return false;
250
251 if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port)
252 return false;
253
254 break;
255
a16e1123
LP
256 default:
257 /* Cannot compare, so we assume the addresses are different */
258 return false;
259 }
260
261 return true;
262}
263
a57f7e2c 264const char* socket_address_get_path(const SocketAddress *a) {
6e2ef85b
LP
265 assert(a);
266
267 if (socket_address_family(a) != AF_UNIX)
a57f7e2c 268 return NULL;
6e2ef85b
LP
269
270 if (a->sockaddr.un.sun_path[0] == 0)
a57f7e2c 271 return NULL;
a16e1123 272
48e6a2dc
LP
273 /* Note that this is only safe because we know that there's an extra NUL byte after the sockaddr_un
274 * structure. On Linux AF_UNIX file system socket addresses don't have to be NUL terminated if they take up the
275 * full sun_path space. */
276 assert_cc(sizeof(union sockaddr_union) >= sizeof(struct sockaddr_un)+1);
a57f7e2c 277 return a->sockaddr.un.sun_path;
a16e1123 278}
c0120d99 279
5bfcc1c6 280bool socket_ipv6_is_supported(void) {
629abfc2 281 if (access("/proc/net/if_inet6", F_OK) != 0)
90ab5042 282 return false;
f89f1e8f 283
7377964d 284 return true;
5bfcc1c6
FF
285}
286
01e10de3 287bool socket_address_matches_fd(const SocketAddress *a, int fd) {
dbafedac
MS
288 SocketAddress b;
289 socklen_t solen;
01e10de3
LP
290
291 assert(a);
292 assert(fd >= 0);
293
dbafedac
MS
294 b.size = sizeof(b.sockaddr);
295 if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
01e10de3
LP
296 return false;
297
dbafedac 298 if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
01e10de3
LP
299 return false;
300
dbafedac
MS
301 solen = sizeof(b.type);
302 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
01e10de3
LP
303 return false;
304
dbafedac 305 if (b.type != a->type)
01e10de3
LP
306 return false;
307
308 if (a->protocol != 0) {
dbafedac
MS
309 solen = sizeof(b.protocol);
310 if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
01e10de3
LP
311 return false;
312
dbafedac 313 if (b.protocol != a->protocol)
01e10de3
LP
314 return false;
315 }
316
02233928 317 return socket_address_equal(a, &b);
01e10de3
LP
318}
319
f6aac5bf 320int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
3b1c5241
SL
321 union sockaddr_union *sa = (union sockaddr_union*) _sa;
322
f6aac5bf
LP
323 /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
324
3b1c5241
SL
325 assert(sa);
326
0fc0f14b 327 switch (sa->sa.sa_family) {
f6aac5bf 328
0fc0f14b 329 case AF_INET:
f6aac5bf 330 *ret_port = be16toh(sa->in.sin_port);
0fc0f14b 331 return 0;
3b1c5241 332
0fc0f14b 333 case AF_INET6:
f6aac5bf 334 *ret_port = be16toh(sa->in6.sin6_port);
0fc0f14b
SH
335 return 0;
336
337 case AF_VSOCK:
f6aac5bf 338 *ret_port = sa->vm.svm_port;
0fc0f14b
SH
339 return 0;
340
341 default:
342 return -EAFNOSUPPORT;
343 }
3b1c5241
SL
344}
345
836f9cfe
LP
346int sockaddr_pretty(
347 const struct sockaddr *_sa,
348 socklen_t salen,
349 bool translate_ipv6,
350 bool include_port,
351 char **ret) {
352
4d49b48c 353 union sockaddr_union *sa = (union sockaddr_union*) _sa;
8569a776 354 char *p;
fc25ad25 355 int r;
8569a776 356
4d49b48c
LP
357 assert(sa);
358 assert(salen >= sizeof(sa->sa.sa_family));
8569a776 359
4d49b48c 360 switch (sa->sa.sa_family) {
8569a776
LP
361
362 case AF_INET: {
363 uint32_t a;
364
8e38570e 365 a = be32toh(sa->in.sin_addr.s_addr);
8569a776 366
fc25ad25
ZJS
367 if (include_port)
368 r = asprintf(&p,
3b1c5241
SL
369 "%u.%u.%u.%u:%u",
370 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
8e38570e 371 be16toh(sa->in.sin_port));
fc25ad25
ZJS
372 else
373 r = asprintf(&p,
3b1c5241 374 "%u.%u.%u.%u",
fc25ad25
ZJS
375 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
376 if (r < 0)
377 return -ENOMEM;
8569a776
LP
378 break;
379 }
380
381 case AF_INET6: {
382 static const unsigned char ipv4_prefix[] = {
383 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
384 };
385
fc25ad25
ZJS
386 if (translate_ipv6 &&
387 memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
4d49b48c 388 const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
fc25ad25
ZJS
389 if (include_port)
390 r = asprintf(&p,
3b1c5241
SL
391 "%u.%u.%u.%u:%u",
392 a[0], a[1], a[2], a[3],
8e38570e 393 be16toh(sa->in6.sin6_port));
fc25ad25
ZJS
394 else
395 r = asprintf(&p,
3b1c5241 396 "%u.%u.%u.%u",
fc25ad25
ZJS
397 a[0], a[1], a[2], a[3]);
398 if (r < 0)
399 return -ENOMEM;
8569a776
LP
400 } else {
401 char a[INET6_ADDRSTRLEN];
402
3b1c5241
SL
403 inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a));
404
405 if (include_port) {
fc25ad25 406 r = asprintf(&p,
3b1c5241
SL
407 "[%s]:%u",
408 a,
8e38570e 409 be16toh(sa->in6.sin6_port));
fc25ad25 410 if (r < 0)
3b1c5241
SL
411 return -ENOMEM;
412 } else {
413 p = strdup(a);
414 if (!p)
415 return -ENOMEM;
416 }
8569a776
LP
417 }
418
419 break;
420 }
421
4d49b48c 422 case AF_UNIX:
15dca371 423 if (salen <= offsetof(struct sockaddr_un, sun_path) ||
994b9d4e 424 (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1))
15dca371 425 /* The name must have at least one character (and the leading NUL does not count) */
4d49b48c 426 p = strdup("<unnamed>");
994b9d4e 427 else {
085b39e9
LP
428 /* Note that we calculate the path pointer here through the .un_buffer[] field, in order to
429 * outtrick bounds checking tools such as ubsan, which are too smart for their own good: on
430 * Linux the kernel may return sun_path[] data one byte longer than the declared size of the
431 * field. */
432 char *path = (char*) sa->un_buffer + offsetof(struct sockaddr_un, sun_path);
15dca371 433 size_t path_len = salen - offsetof(struct sockaddr_un, sun_path);
8569a776 434
085b39e9 435 if (path[0] == 0) {
15dca371
ZJS
436 /* Abstract socket. When parsing address information from, we
437 * explicitly reject overly long paths and paths with embedded NULs.
438 * But we might get such a socket from the outside. Let's return
439 * something meaningful and printable in this case. */
4d49b48c 440
15dca371 441 _cleanup_free_ char *e = NULL;
4d49b48c 442
085b39e9 443 e = cescape_length(path + 1, path_len - 1);
15dca371
ZJS
444 if (!e)
445 return -ENOMEM;
4d49b48c 446
15dca371
ZJS
447 p = strjoin("@", e);
448 } else {
085b39e9 449 if (path[path_len - 1] == '\0')
15dca371
ZJS
450 /* We expect a terminating NUL and don't print it */
451 path_len --;
452
085b39e9 453 p = cescape_length(path, path_len);
15dca371 454 }
4d49b48c 455 }
994b9d4e
LP
456 if (!p)
457 return -ENOMEM;
8569a776
LP
458
459 break;
8569a776 460
0fc0f14b 461 case AF_VSOCK:
3a484991
ZJS
462 if (include_port) {
463 if (sa->vm.svm_cid == VMADDR_CID_ANY)
464 r = asprintf(&p, "vsock::%u", sa->vm.svm_port);
465 else
466 r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port);
467 } else
0fc0f14b
SH
468 r = asprintf(&p, "vsock:%u", sa->vm.svm_cid);
469 if (r < 0)
470 return -ENOMEM;
471 break;
472
8569a776 473 default:
15411c0c 474 return -EOPNOTSUPP;
8569a776
LP
475 }
476
477 *ret = p;
478 return 0;
479}
480
366b7db4 481int getpeername_pretty(int fd, bool include_port, char **ret) {
4d49b48c 482 union sockaddr_union sa;
b31f535c 483 socklen_t salen = sizeof(sa);
eff05270 484 int r;
4d49b48c
LP
485
486 assert(fd >= 0);
487 assert(ret);
488
4d49b48c
LP
489 if (getpeername(fd, &sa.sa, &salen) < 0)
490 return -errno;
491
492 if (sa.sa.sa_family == AF_UNIX) {
39883f62 493 struct ucred ucred = {};
4d49b48c
LP
494
495 /* UNIX connection sockets are anonymous, so let's use
496 * PID/UID as pretty credentials instead */
497
eff05270
LP
498 r = getpeercred(fd, &ucred);
499 if (r < 0)
500 return r;
4d49b48c 501
de0671ee 502 if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
4d49b48c
LP
503 return -ENOMEM;
504
505 return 0;
506 }
507
508 /* For remote sockets we translate IPv6 addresses back to IPv4
509 * if applicable, since that's nicer. */
510
366b7db4 511 return sockaddr_pretty(&sa.sa, salen, true, include_port, ret);
4d49b48c
LP
512}
513
514int getsockname_pretty(int fd, char **ret) {
515 union sockaddr_union sa;
b31f535c 516 socklen_t salen = sizeof(sa);
4d49b48c
LP
517
518 assert(fd >= 0);
519 assert(ret);
520
4d49b48c
LP
521 if (getsockname(fd, &sa.sa, &salen) < 0)
522 return -errno;
523
524 /* For local sockets we do not translate IPv6 addresses back
525 * to IPv6 if applicable, since this is usually used for
526 * listening sockets where the difference between IPv4 and
527 * IPv6 matters. */
528
3b1c5241 529 return sockaddr_pretty(&sa.sa, salen, false, true, ret);
4d49b48c
LP
530}
531
b31f535c
ZJS
532int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) {
533 int r;
534 char host[NI_MAXHOST], *ret;
535
536 assert(_ret);
537
6326a143 538 r = getnameinfo(&sa->sa, salen, host, sizeof(host), NULL, 0, IDN_FLAGS);
b31f535c 539 if (r != 0) {
b31f535c
ZJS
540 int saved_errno = errno;
541
3b1c5241 542 r = sockaddr_pretty(&sa->sa, salen, true, true, &ret);
f647962d 543 if (r < 0)
1938ac51 544 return r;
b31f535c 545
279d3c9c 546 log_debug_errno(saved_errno, "getnameinfo(%s) failed: %m", ret);
cb651834
ZJS
547 } else {
548 ret = strdup(host);
549 if (!ret)
1938ac51 550 return -ENOMEM;
cb651834 551 }
b31f535c
ZJS
552
553 *_ret = ret;
554 return 0;
555}
556
7a22745a
LP
557static const char* const netlink_family_table[] = {
558 [NETLINK_ROUTE] = "route",
559 [NETLINK_FIREWALL] = "firewall",
560 [NETLINK_INET_DIAG] = "inet-diag",
561 [NETLINK_NFLOG] = "nflog",
562 [NETLINK_XFRM] = "xfrm",
563 [NETLINK_SELINUX] = "selinux",
564 [NETLINK_ISCSI] = "iscsi",
565 [NETLINK_AUDIT] = "audit",
566 [NETLINK_FIB_LOOKUP] = "fib-lookup",
567 [NETLINK_CONNECTOR] = "connector",
568 [NETLINK_NETFILTER] = "netfilter",
569 [NETLINK_IP6_FW] = "ip6-fw",
570 [NETLINK_DNRTMSG] = "dnrtmsg",
571 [NETLINK_KOBJECT_UEVENT] = "kobject-uevent",
572 [NETLINK_GENERIC] = "generic",
573 [NETLINK_SCSITRANSPORT] = "scsitransport",
5570d7f9
JG
574 [NETLINK_ECRYPTFS] = "ecryptfs",
575 [NETLINK_RDMA] = "rdma",
7a22745a
LP
576};
577
f8b69d1d 578DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
7a22745a 579
c0120d99
LP
580static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
581 [SOCKET_ADDRESS_DEFAULT] = "default",
582 [SOCKET_ADDRESS_BOTH] = "both",
583 [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only"
584};
585
586DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only);
f01e5736 587
b54e98ef 588SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *n) {
6f90844f
YW
589 int r;
590
591 r = parse_boolean(n);
592 if (r > 0)
593 return SOCKET_ADDRESS_IPV6_ONLY;
594 if (r == 0)
595 return SOCKET_ADDRESS_BOTH;
596
597 return socket_address_bind_ipv6_only_from_string(n);
598}
599
f01e5736
LP
600bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
601 assert(a);
602 assert(b);
603
604 if (a->sa.sa_family != b->sa.sa_family)
605 return false;
606
607 if (a->sa.sa_family == AF_INET)
608 return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
609
610 if (a->sa.sa_family == AF_INET6)
611 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
612
0fc0f14b
SH
613 if (a->sa.sa_family == AF_VSOCK)
614 return a->vm.svm_cid == b->vm.svm_cid;
615
f01e5736
LP
616 return false;
617}
2583fbea
LP
618
619int fd_inc_sndbuf(int fd, size_t n) {
620 int r, value;
621 socklen_t l = sizeof(value);
622
623 r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
624 if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
625 return 0;
626
627 /* If we have the privileges we will ignore the kernel limit. */
628
9e5b6496
YW
629 if (setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n) < 0) {
630 r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n);
631 if (r < 0)
632 return r;
633 }
2583fbea
LP
634
635 return 1;
636}
637
638int fd_inc_rcvbuf(int fd, size_t n) {
639 int r, value;
640 socklen_t l = sizeof(value);
641
642 r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
643 if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
644 return 0;
645
646 /* If we have the privileges we will ignore the kernel limit. */
647
9e5b6496
YW
648 if (setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n) < 0) {
649 r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n);
650 if (r < 0)
651 return r;
652 }
653
2583fbea
LP
654 return 1;
655}
656
657static const char* const ip_tos_table[] = {
658 [IPTOS_LOWDELAY] = "low-delay",
659 [IPTOS_THROUGHPUT] = "throughput",
660 [IPTOS_RELIABILITY] = "reliability",
661 [IPTOS_LOWCOST] = "low-cost",
662};
663
664DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
665
4252696a 666bool ifname_valid_full(const char *p, bool alternative) {
ef76dff2
LP
667 bool numeric = true;
668
669 /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
670 * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
671 * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
672
673 if (isempty(p))
674 return false;
675
4252696a
YW
676 if (alternative) {
677 if (strlen(p) >= ALTIFNAMSIZ)
678 return false;
679 } else {
680 if (strlen(p) >= IFNAMSIZ)
681 return false;
682 }
ef76dff2 683
49bfc877 684 if (dot_or_dot_dot(p))
ef76dff2
LP
685 return false;
686
687 while (*p) {
688 if ((unsigned char) *p >= 127U)
689 return false;
690
691 if ((unsigned char) *p <= 32U)
692 return false;
693
4c701096 694 if (IN_SET(*p, ':', '/'))
ef76dff2
LP
695 return false;
696
697 numeric = numeric && (*p >= '0' && *p <= '9');
698 p++;
699 }
700
701 if (numeric)
702 return false;
703
704 return true;
705}
706
26808948
SS
707bool address_label_valid(const char *p) {
708
709 if (isempty(p))
710 return false;
711
712 if (strlen(p) >= IFNAMSIZ)
713 return false;
714
715 while (*p) {
716 if ((uint8_t) *p >= 127U)
717 return false;
718
719 if ((uint8_t) *p <= 31U)
720 return false;
721 p++;
722 }
723
724 return true;
725}
726
2583fbea
LP
727int getpeercred(int fd, struct ucred *ucred) {
728 socklen_t n = sizeof(struct ucred);
729 struct ucred u;
730 int r;
731
732 assert(fd >= 0);
733 assert(ucred);
734
735 r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n);
736 if (r < 0)
737 return -errno;
738
739 if (n != sizeof(struct ucred))
740 return -EIO;
741
bbcc701e
LP
742 /* Check if the data is actually useful and not suppressed due to namespacing issues */
743 if (!pid_is_valid(u.pid))
2583fbea
LP
744 return -ENODATA;
745
bbcc701e
LP
746 /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of
747 * receiving in "invalid" user/group we get the overflow UID/GID. */
748
2583fbea
LP
749 *ucred = u;
750 return 0;
751}
752
753int getpeersec(int fd, char **ret) {
217d8967 754 _cleanup_free_ char *s = NULL;
2583fbea 755 socklen_t n = 64;
2583fbea
LP
756
757 assert(fd >= 0);
758 assert(ret);
759
217d8967
LP
760 for (;;) {
761 s = new0(char, n+1);
762 if (!s)
763 return -ENOMEM;
2583fbea 764
217d8967
LP
765 if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0)
766 break;
2583fbea
LP
767
768 if (errno != ERANGE)
769 return -errno;
770
217d8967 771 s = mfree(s);
2583fbea
LP
772 }
773
217d8967 774 if (isempty(s))
2583fbea 775 return -EOPNOTSUPP;
2583fbea 776
ae2a15bc 777 *ret = TAKE_PTR(s);
217d8967 778
2583fbea
LP
779 return 0;
780}
781
43f2c88d
LP
782int getpeergroups(int fd, gid_t **ret) {
783 socklen_t n = sizeof(gid_t) * 64;
784 _cleanup_free_ gid_t *d = NULL;
785
75f40779 786 assert(fd >= 0);
43f2c88d
LP
787 assert(ret);
788
789 for (;;) {
790 d = malloc(n);
791 if (!d)
792 return -ENOMEM;
793
794 if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
795 break;
796
797 if (errno != ERANGE)
798 return -errno;
799
800 d = mfree(d);
801 }
802
803 assert_se(n % sizeof(gid_t) == 0);
804 n /= sizeof(gid_t);
805
806 if ((socklen_t) (int) n != n)
807 return -E2BIG;
808
1cc6c93a 809 *ret = TAKE_PTR(d);
43f2c88d
LP
810
811 return (int) n;
812}
813
d34673ec 814ssize_t send_one_fd_iov_sa(
726f4c47
ZJS
815 int transport_fd,
816 int fd,
d34673ec 817 struct iovec *iov, size_t iovlen,
726f4c47
ZJS
818 const struct sockaddr *sa, socklen_t len,
819 int flags) {
820
fb29cdbe 821 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control = {};
2583fbea 822 struct msghdr mh = {
726f4c47
ZJS
823 .msg_name = (struct sockaddr*) sa,
824 .msg_namelen = len,
d34673ec
FB
825 .msg_iov = iov,
826 .msg_iovlen = iovlen,
2583fbea 827 };
d34673ec 828 ssize_t k;
2583fbea
LP
829
830 assert(transport_fd >= 0);
2583fbea 831
d34673ec
FB
832 /*
833 * We need either an FD or data to send.
834 * If there's nothing, return an error.
835 */
836 if (fd < 0 && !iov)
837 return -EINVAL;
2583fbea 838
d34673ec
FB
839 if (fd >= 0) {
840 struct cmsghdr *cmsg;
2583fbea 841
d34673ec
FB
842 mh.msg_control = &control;
843 mh.msg_controllen = sizeof(control);
844
845 cmsg = CMSG_FIRSTHDR(&mh);
846 cmsg->cmsg_level = SOL_SOCKET;
847 cmsg->cmsg_type = SCM_RIGHTS;
848 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
849 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
d34673ec
FB
850 }
851 k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
852 if (k < 0)
853 return (ssize_t) -errno;
854
855 return k;
2583fbea
LP
856}
857
d34673ec
FB
858int send_one_fd_sa(
859 int transport_fd,
860 int fd,
861 const struct sockaddr *sa, socklen_t len,
862 int flags) {
863
864 assert(fd >= 0);
865
866 return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags);
867}
868
869ssize_t receive_one_fd_iov(
870 int transport_fd,
871 struct iovec *iov, size_t iovlen,
872 int flags,
873 int *ret_fd) {
874
fb29cdbe 875 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control;
2583fbea
LP
876 struct msghdr mh = {
877 .msg_control = &control,
878 .msg_controllen = sizeof(control),
d34673ec
FB
879 .msg_iov = iov,
880 .msg_iovlen = iovlen,
2583fbea 881 };
dac556fa 882 struct cmsghdr *found;
d34673ec 883 ssize_t k;
2583fbea
LP
884
885 assert(transport_fd >= 0);
d34673ec 886 assert(ret_fd);
2583fbea
LP
887
888 /*
889 * Receive a single FD via @transport_fd. We don't care for
890 * the transport-type. We retrieve a single FD at most, so for
891 * packet-based transports, the caller must ensure to send
892 * only a single FD per packet. This is best used in
893 * combination with send_one_fd().
894 */
895
3691bcf3 896 k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
d34673ec 897 if (k < 0)
3691bcf3 898 return k;
2583fbea 899
dac556fa 900 found = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int)));
3691bcf3 901 if (!found) {
2583fbea 902 cmsg_close_all(&mh);
d34673ec 903
3691bcf3
LP
904 /* If didn't receive an FD or any data, return an error. */
905 if (k == 0)
906 return -EIO;
907 }
2583fbea 908
d34673ec
FB
909 if (found)
910 *ret_fd = *(int*) CMSG_DATA(found);
911 else
912 *ret_fd = -1;
913
914 return k;
915}
916
917int receive_one_fd(int transport_fd, int flags) {
918 int fd;
919 ssize_t k;
920
921 k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd);
922 if (k == 0)
923 return fd;
924
925 /* k must be negative, since receive_one_fd_iov() only returns
926 * a positive value if data was received through the iov. */
927 assert(k < 0);
928 return (int) k;
2583fbea 929}
4edc2c9b
LP
930
931ssize_t next_datagram_size_fd(int fd) {
932 ssize_t l;
933 int k;
934
935 /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will
96d49011 936 * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't
4edc2c9b
LP
937 * do. This difference is actually of major importance as we need to be sure that the size returned here
938 * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of
939 * the wrong size. */
940
941 l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC);
942 if (l < 0) {
3742095b 943 if (IN_SET(errno, EOPNOTSUPP, EFAULT))
4edc2c9b
LP
944 goto fallback;
945
946 return -errno;
947 }
948 if (l == 0)
949 goto fallback;
950
951 return l;
952
953fallback:
954 k = 0;
955
956 /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD
957 * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */
958
959 if (ioctl(fd, FIONREAD, &k) < 0)
960 return -errno;
961
962 return (ssize_t) k;
963}
60d9771c 964
67962036
ZJS
965/* Put a limit on how many times will attempt to call accept4(). We loop
966 * only on "transient" errors, but let's make sure we don't loop forever. */
967#define MAX_FLUSH_ITERATIONS 1024
968
60d9771c
LP
969int flush_accept(int fd) {
970
971 struct pollfd pollfd = {
972 .fd = fd,
973 .events = POLLIN,
974 };
f3d75364
LP
975 int r, b;
976 socklen_t l = sizeof(b);
977
644cb4cc
ZJS
978 /* Similar to flush_fd() but flushes all incoming connections by accepting and immediately closing
979 * them. */
f3d75364
LP
980
981 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0)
982 return -errno;
60d9771c 983
f3d75364 984 assert(l == sizeof(b));
644cb4cc
ZJS
985 if (!b) /* Let's check if this socket accepts connections before calling accept(). accept4() can
986 * return EOPNOTSUPP if the fd is not a listening socket, which we should treat as a fatal
987 * error, or in case the incoming TCP connection triggered a network issue, which we want to
988 * treat as a transient error. Thus, let's rule out the first reason for EOPNOTSUPP early, so
989 * we can loop safely on transient errors below. */
f3d75364 990 return -ENOTTY;
60d9771c 991
67962036 992 for (unsigned iteration = 0;; iteration++) {
60d9771c
LP
993 int cfd;
994
995 r = poll(&pollfd, 1, 0);
996 if (r < 0) {
997 if (errno == EINTR)
998 continue;
999
1000 return -errno;
4ff9bc2e
LP
1001 }
1002 if (r == 0)
60d9771c
LP
1003 return 0;
1004
dad28bff
LP
1005 if (pollfd.revents & POLLNVAL)
1006 return -EBADF;
1007
67962036
ZJS
1008 if (iteration >= MAX_FLUSH_ITERATIONS)
1009 return log_debug_errno(SYNTHETIC_ERRNO(EBUSY),
1010 "Failed to flush connections within " STRINGIFY(MAX_FLUSH_ITERATIONS) " iterations.");
1011
60d9771c
LP
1012 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
1013 if (cfd < 0) {
60d9771c
LP
1014 if (errno == EAGAIN)
1015 return 0;
1016
4ff9bc2e
LP
1017 if (ERRNO_IS_ACCEPT_AGAIN(errno))
1018 continue;
1019
60d9771c
LP
1020 return -errno;
1021 }
1022
4ff9bc2e 1023 safe_close(cfd);
60d9771c
LP
1024 }
1025}
29206d46
LP
1026
1027struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) {
1028 struct cmsghdr *cmsg;
1029
1030 assert(mh);
1031
1032 CMSG_FOREACH(cmsg, mh)
1033 if (cmsg->cmsg_level == level &&
1034 cmsg->cmsg_type == type &&
1035 (length == (socklen_t) -1 || length == cmsg->cmsg_len))
1036 return cmsg;
1037
1038 return NULL;
1039}
429b4350
LP
1040
1041int socket_ioctl_fd(void) {
1042 int fd;
1043
1044 /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for
1045 * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not
1046 * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more
1047 * generic AF_NETLINK. */
1048
1049 fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1050 if (fd < 0)
1051 fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC);
1052 if (fd < 0)
1053 return -errno;
1054
1055 return fd;
1056}
9f20fc28
LP
1057
1058int sockaddr_un_unlink(const struct sockaddr_un *sa) {
1059 const char *p, * nul;
1060
1061 assert(sa);
1062
1063 if (sa->sun_family != AF_UNIX)
1064 return -EPROTOTYPE;
1065
1066 if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */
1067 return 0;
1068
1069 /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */
1070 nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path));
1071 if (nul)
1072 p = sa->sun_path;
1073 else
1074 p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path));
1075
1076 if (unlink(p) < 0)
1077 return -errno;
1078
1079 return 1;
1080}
5cf91ea9
LP
1081
1082int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path) {
1083 size_t l;
1084
1085 assert(ret);
1086 assert(path);
1087
1088 /* Initialize ret->sun_path from the specified argument. This will interpret paths starting with '@' as
1089 * abstract namespace sockets, and those starting with '/' as regular filesystem sockets. It won't accept
1090 * anything else (i.e. no relative paths), to avoid ambiguities. Note that this function cannot be used to
1091 * reference paths in the abstract namespace that include NUL bytes in the name. */
1092
1093 l = strlen(path);
1094 if (l == 0)
1095 return -EINVAL;
1096 if (!IN_SET(path[0], '/', '@'))
1097 return -EINVAL;
1098 if (path[1] == 0)
1099 return -EINVAL;
1100
1101 /* Don't allow paths larger than the space in sockaddr_un. Note that we are a tiny bit more restrictive than
1102 * the kernel is: we insist on NUL termination (both for abstract namespace and regular file system socket
1103 * addresses!), which the kernel doesn't. We do this to reduce chance of incompatibility with other apps that
1104 * do not expect non-NUL terminated file system path*/
1105 if (l+1 > sizeof(ret->sun_path))
1106 return -EINVAL;
1107
1108 *ret = (struct sockaddr_un) {
1109 .sun_family = AF_UNIX,
1110 };
1111
1112 if (path[0] == '@') {
1113 /* Abstract namespace socket */
1114 memcpy(ret->sun_path + 1, path + 1, l); /* copy *with* trailing NUL byte */
1115 return (int) (offsetof(struct sockaddr_un, sun_path) + l); /* 🔥 *don't* 🔥 include trailing NUL in size */
1116
1117 } else {
1118 assert(path[0] == '/');
1119
1120 /* File system socket */
1121 memcpy(ret->sun_path, path, l + 1); /* copy *with* trailing NUL byte */
1122 return (int) (offsetof(struct sockaddr_un, sun_path) + l + 1); /* include trailing NUL in size */
1123 }
1124}
5d594d01
LP
1125
1126int socket_bind_to_ifname(int fd, const char *ifname) {
1127 assert(fd >= 0);
1128
1129 /* Call with NULL to drop binding */
1130
1131 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname)) < 0)
1132 return -errno;
1133
1134 return 0;
1135}
1136
1137int socket_bind_to_ifindex(int fd, int ifindex) {
518a66ec 1138 char ifname[IF_NAMESIZE + 1];
5d594d01
LP
1139
1140 assert(fd >= 0);
1141
1142 if (ifindex <= 0) {
1143 /* Drop binding */
1144 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0) < 0)
1145 return -errno;
1146
1147 return 0;
1148 }
1149
1150 if (setsockopt(fd, SOL_SOCKET, SO_BINDTOIFINDEX, &ifindex, sizeof(ifindex)) >= 0)
1151 return 0;
1152 if (errno != ENOPROTOOPT)
1153 return -errno;
1154
1155 /* Fall back to SO_BINDTODEVICE on kernels < 5.0 which didn't have SO_BINDTOIFINDEX */
518a66ec 1156 if (!format_ifname(ifindex, ifname))
5d594d01
LP
1157 return -errno;
1158
1159 return socket_bind_to_ifname(fd, ifname);
1160}
47eae6ce
LP
1161
1162ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) {
1163 ssize_t n;
1164
1165 /* A wrapper around recvmsg() that checks for MSG_CTRUNC, and turns it into an error, in a reasonably
1166 * safe way, closing any SCM_RIGHTS fds in the error path.
1167 *
1168 * Note that unlike our usual coding style this might modify *msg on failure. */
1169
1170 n = recvmsg(sockfd, msg, flags);
1171 if (n < 0)
1172 return -errno;
1173
1174 if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC)) {
1175 cmsg_close_all(msg);
1176 return -EXFULL; /* a recognizable error code */
1177 }
1178
1179 return n;
35a3eb9b
LP
1180}
1181
1182int socket_pass_pktinfo(int fd, bool b) {
1183 int af;
1184 socklen_t sl = sizeof(af);
1185
1186 if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
1187 return -errno;
1188
1189 switch (af) {
1190
1191 case AF_INET:
1192 return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
47eae6ce 1193
35a3eb9b
LP
1194 case AF_INET6:
1195 return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
1196
1197 case AF_NETLINK:
1198 return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
1199
1200 default:
1201 return -EAFNOSUPPORT;
1202 }
47eae6ce 1203}