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