HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/basic/socket-util.c

61,797 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 79eabcf32077895d7ef592a5c0fc6d458522ab48

Lines 1–1000 of 1,923

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_infiniband.h>
#include <linux/pkt_sched.h>
#include <mqueue.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/ip.h>
#include <poll.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
 
#include "alloc-util.h"
#include "errno-util.h"
#include "escape.h"
#include "fd-util.h"
#include "format-ifname.h"
#include "format-util.h"
#include "in-addr-util.h"
#include "io-util.h"
#include "log.h"
#include "memory-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "pidref.h"
#include "process-util.h"
#include "random-util.h"
#include "socket-util.h"
#include "sparse-endian.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "sysctl-util.h"
#include "user-util.h"
#include "xattr-util.h"
 
#if ENABLE_IDN
#  define IDN_FLAGS NI_IDN
#else
#  define IDN_FLAGS 0
#endif
 
static const char* const socket_address_type_table[] = {
        [SOCK_STREAM] =    "Stream",
        [SOCK_DGRAM] =     "Datagram",
        [SOCK_RAW] =       "Raw",
        [SOCK_RDM] =       "ReliableDatagram",
        [SOCK_SEQPACKET] = "SequentialPacket",
        [SOCK_DCCP] =      "DatagramCongestionControl",
};
 
DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int);
 
int socket_address_verify(const SocketAddress *a, bool strict) {
        assert(a);
 
        /* With 'strict' we enforce additional sanity constraints which are not set by the standard,
         * but should only apply to sockets we create ourselves. */
 
        switch (socket_address_family(a)) {
 
        case AF_INET:
                if (a->size != sizeof(struct sockaddr_in))
                        return -EINVAL;
 
                if (a->sockaddr.in.sin_port == 0)
                        return -EINVAL;
 
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
                        return -EINVAL;
 
                return 0;
 
        case AF_INET6:
                if (a->size != sizeof(struct sockaddr_in6))
                        return -EINVAL;
 
                if (a->sockaddr.in6.sin6_port == 0)
                        return -EINVAL;
 
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
                        return -EINVAL;
 
                return 0;
 
        case AF_UNIX:
                if (a->size < offsetof(struct sockaddr_un, sun_path))
                        return -EINVAL;
                if (a->size > sizeof(struct sockaddr_un) + !strict)
                        /* If !strict, allow one extra byte, since getsockname() on Linux will append
                         * a NUL byte if we have path sockets that are above sun_path's full size. */
                        return -EINVAL;
 
                if (a->size > offsetof(struct sockaddr_un, sun_path) &&
                    a->sockaddr.un.sun_path[0] != 0 &&
                    strict) {
                        /* Only validate file system sockets here, and only in strict mode */
                        const char *e;
 
                        e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
                        if (e) {
                                /* If there's an embedded NUL byte, make sure the size of the socket address matches it */
                                if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
                                        return -EINVAL;
                        } else {
                                /* If there's no embedded NUL byte, then the size needs to match the whole
                                 * structure or the structure with one extra NUL byte suffixed. (Yeah, Linux is awful,
                                 * and considers both equivalent: getsockname() even extends sockaddr_un beyond its
                                 * size if the path is non NUL terminated.) */
                                if (!IN_SET(a->size, sizeof(a->sockaddr.un.sun_path), sizeof(a->sockaddr.un.sun_path)+1))
                                        return -EINVAL;
                        }
                }
 
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET))
                        return -EINVAL;
 
                return 0;
 
        case AF_NETLINK:
 
                if (a->size != sizeof(struct sockaddr_nl))
                        return -EINVAL;
 
                if (!IN_SET(a->type, 0, SOCK_RAW, SOCK_DGRAM))
                        return -EINVAL;
 
                return 0;
 
        case AF_VSOCK:
                if (a->size != sizeof(struct sockaddr_vm))
                        return -EINVAL;
 
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
                        return -EINVAL;
 
                return 0;
 
        default:
                return -EAFNOSUPPORT;
        }
}
 
int socket_address_print(const SocketAddress *a, char **ret) {
        int r;
 
        assert(a);
        assert(ret);
 
        r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be
                                              * able to pretty-print any socket the kernel considers
                                              * valid. We still need to do validation to know if we
                                              * can meaningfully print the address. */
        if (r < 0)
                return r;
 
        if (socket_address_family(a) == AF_NETLINK) {
                _cleanup_free_ char *sfamily = NULL;
 
                r = netlink_family_to_string_alloc(a->protocol, &sfamily);
                if (r < 0)
                        return r;
 
                r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
                if (r < 0)
                        return -ENOMEM;
 
                return 0;
        }
 
        return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
}
 
bool socket_address_can_accept(const SocketAddress *a) {
        assert(a);
 
        return
                IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET);
}
 
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
        assert(a);
        assert(b);
 
        /* Invalid addresses are unequal to all */
        if (socket_address_verify(a, false) < 0 ||
            socket_address_verify(b, false) < 0)
                return false;
 
        if (a->type != b->type)
                return false;
 
        if (socket_address_family(a) != socket_address_family(b))
                return false;
 
        switch (socket_address_family(a)) {
 
        case AF_INET:
                if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
                        return false;
 
                if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
                        return false;
 
                break;
 
        case AF_INET6:
                if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
                        return false;
 
                if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
                        return false;
 
                break;
 
        case AF_UNIX:
                if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
                    b->size <= offsetof(struct sockaddr_un, sun_path))
                        return false;
 
                if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
                        return false;
 
                if (a->sockaddr.un.sun_path[0]) {
                        if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
                                return false;
                } else {
                        if (a->size != b->size)
                                return false;
 
                        if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
                                return false;
                }
 
                break;
 
        case AF_NETLINK:
                if (a->protocol != b->protocol)
                        return false;
 
                if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
                        return false;
 
                break;
 
        case AF_VSOCK:
                if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid)
                        return false;
 
                if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port)
                        return false;
 
                break;
 
        default:
                /* Cannot compare, so we assume the addresses are different */
                return false;
        }
 
        return true;
}
 
const char* socket_address_get_path(const SocketAddress *a) {
        assert(a);
 
        if (socket_address_family(a) != AF_UNIX)
                return NULL;
 
        if (a->sockaddr.un.sun_path[0] == 0)
                return NULL;
 
        /* Note that this is only safe because we know that there's an extra NUL byte after the sockaddr_un
         * structure. On Linux AF_UNIX file system socket addresses don't have to be NUL terminated if they take up the
         * full sun_path space. */
        assert_cc(sizeof(union sockaddr_union) >= sizeof(struct sockaddr_un)+1);
        return a->sockaddr.un.sun_path;
}
 
bool socket_ipv6_is_supported(void) {
        static int cached = -1;
 
        if (cached < 0) {
 
                if (access("/proc/net/if_inet6", F_OK) < 0) {
 
                        if (errno != ENOENT) {
                                log_debug_errno(errno, "Unexpected error when checking whether /proc/net/if_inet6 exists: %m");
                                return false;
                        }
 
                        cached = false;
                } else
                        cached = true;
        }
 
        return cached;
}
 
bool socket_ipv6_is_enabled(void) {
        _cleanup_free_ char *v = NULL;
        int r;
 
        /* Much like socket_ipv6_is_supported(), but also checks that the sysctl that disables IPv6 on all
         * interfaces isn't turned on */
 
        if (!socket_ipv6_is_supported())
                return false;
 
        r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v);
        if (r < 0) {
                log_debug_errno(r, "Unexpected error reading 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
                return true;
        }
 
        r = parse_boolean(v);
        if (r < 0) {
                log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
                return true;
        }
 
        return !r;
}
 
bool socket_address_matches_fd(const SocketAddress *a, int fd) {
        SocketAddress b;
        socklen_t solen;
 
        assert(a);
        assert(fd >= 0);
 
        b.size = sizeof(b.sockaddr);
        if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
                return false;
 
        if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
                return false;
 
        solen = sizeof(b.type);
        if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
                return false;
 
        if (b.type != a->type)
                return false;
 
        if (a->protocol != 0)  {
                solen = sizeof(b.protocol);
                if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
                        return false;
 
                if (b.protocol != a->protocol)
                        return false;
        }
 
        return socket_address_equal(a, &b);
}
 
int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
 
        /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
 
        assert(sa);
        assert(ret_port);
 
        switch (sa->sa.sa_family) {
 
        case AF_INET:
                *ret_port = be16toh(sa->in.sin_port);
                return 0;
 
        case AF_INET6:
                *ret_port = be16toh(sa->in6.sin6_port);
                return 0;
 
        case AF_VSOCK:
                *ret_port = sa->vm.svm_port;
                return 0;
 
        default:
                return -EAFNOSUPPORT;
        }
}
 
const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) {
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
 
        if (!sa)
                return NULL;
 
        switch (sa->sa.sa_family) {
 
        case AF_INET:
                return (const union in_addr_union*) &sa->in.sin_addr;
 
        case AF_INET6:
                return (const union in_addr_union*) &sa->in6.sin6_addr;
 
        default:
                return NULL;
        }
}
 
int sockaddr_set_in_addr(
                union sockaddr_union *u,
                int family,
                const union in_addr_union *a,
                uint16_t port) {
 
        assert(u);
        assert(a);
 
        switch (family) {
 
        case AF_INET:
                u->in = (struct sockaddr_in) {
                        .sin_family = AF_INET,
                        .sin_addr = a->in,
                        .sin_port = htobe16(port),
                };
 
                return 0;
 
        case AF_INET6:
                u->in6 = (struct sockaddr_in6) {
                        .sin6_family = AF_INET6,
                        .sin6_addr = a->in6,
                        .sin6_port = htobe16(port),
                };
 
                return 0;
 
        default:
                return -EAFNOSUPPORT;
 
        }
}
 
int sockaddr_pretty(
                const struct sockaddr *_sa,
                socklen_t salen,
                bool translate_ipv6,
                bool include_port,
                char **ret) {
 
        union sockaddr_union *sa = (union sockaddr_union*) _sa;
        char *p;
        int r;
 
        assert(sa);
        assert(salen >= sizeof(sa->sa.sa_family));
        assert(ret);
 
        switch (sa->sa.sa_family) {
 
        case AF_INET: {
                uint32_t a;
 
                a = be32toh(sa->in.sin_addr.s_addr);
 
                if (include_port)
                        r = asprintf(&p,
                                     "%u.%u.%u.%u:%u",
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
                                     be16toh(sa->in.sin_port));
                else
                        r = asprintf(&p,
                                     "%u.%u.%u.%u",
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
                if (r < 0)
                        return -ENOMEM;
                break;
        }
 
        case AF_INET6: {
                static const unsigned char ipv4_prefix[] = {
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
                };
 
                if (translate_ipv6 &&
                    memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
                        const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
                        if (include_port)
                                r = asprintf(&p,
                                             "%u.%u.%u.%u:%u",
                                             a[0], a[1], a[2], a[3],
                                             be16toh(sa->in6.sin6_port));
                        else
                                r = asprintf(&p,
                                             "%u.%u.%u.%u",
                                             a[0], a[1], a[2], a[3]);
                        if (r < 0)
                                return -ENOMEM;
                } else {
                        const char *a = IN6_ADDR_TO_STRING(&sa->in6.sin6_addr);
 
                        if (include_port) {
                                if (asprintf(&p,
                                             "[%s]:%u%s%s",
                                             a,
                                             be16toh(sa->in6.sin6_port),
                                             sa->in6.sin6_scope_id != 0 ? "%" : "",
                                             FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)) < 0)
                                        return -ENOMEM;
                        } else {
                                if (sa->in6.sin6_scope_id != 0)
                                        p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX));
                                else
                                        p = strdup(a);
                                if (!p)
                                        return -ENOMEM;
                        }
                }
 
                break;
        }
 
        case AF_UNIX:
                if (salen <= offsetof(struct sockaddr_un, sun_path) ||
                    (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1))
                        /* The name must have at least one character (and the leading NUL does not count) */
                        p = strdup("<unnamed>");
                else {
                        /* Note that we calculate the path pointer here through the .un_buffer[] field, in order to
                         * outtrick bounds checking tools such as ubsan, which are too smart for their own good: on
                         * Linux the kernel may return sun_path[] data one byte longer than the declared size of the
                         * field. */
                        char *path = (char*) sa->un_buffer + offsetof(struct sockaddr_un, sun_path);
                        size_t path_len = salen - offsetof(struct sockaddr_un, sun_path);
 
                        if (path[0] == 0) {
                                /* Abstract socket. When parsing address information from, we
                                 * explicitly reject overly long paths and paths with embedded NULs.
                                 * But we might get such a socket from the outside. Let's return
                                 * something meaningful and printable in this case. */
 
                                _cleanup_free_ char *e = NULL;
 
                                e = cescape_length(path + 1, path_len - 1);
                                if (!e)
                                        return -ENOMEM;
 
                                p = strjoin("@", e);
                        } else {
                                if (path[path_len - 1] == '\0')
                                        /* We expect a terminating NUL and don't print it */
                                        path_len--;
 
                                p = cescape_length(path, path_len);
                        }
                }
                if (!p)
                        return -ENOMEM;
 
                break;
 
        case AF_VSOCK:
                if (include_port) {
                        if (sa->vm.svm_cid == VMADDR_CID_ANY)
                                r = asprintf(&p, "vsock::%u", sa->vm.svm_port);
                        else
                                r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port);
                } else
                        r = asprintf(&p, "vsock:%u", sa->vm.svm_cid);
                if (r < 0)
                        return -ENOMEM;
                break;
 
        default:
                return -EOPNOTSUPP;
        }
 
        *ret = p;
        return 0;
}
 
int getpeername_pretty(int fd, bool include_port, char **ret) {
        union sockaddr_union sa;
        socklen_t salen = sizeof(sa);
        int r;
 
        assert(fd >= 0);
        assert(ret);
 
        if (getpeername(fd, &sa.sa, &salen) < 0)
                return -errno;
 
        if (sa.sa.sa_family == AF_UNIX) {
                struct ucred ucred = UCRED_INVALID;
 
                /* UNIX connection sockets are anonymous, so let's use
                 * PID/UID as pretty credentials instead */
 
                r = getpeercred(fd, &ucred);
                if (r < 0)
                        return r;
 
                if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
                        return -ENOMEM;
 
                return 0;
        }
 
        /* For remote sockets we translate IPv6 addresses back to IPv4
         * if applicable, since that's nicer. */
 
        return sockaddr_pretty(&sa.sa, salen, true, include_port, ret);
}
 
int getsockname_pretty(int fd, char **ret) {
        union sockaddr_union sa;
        socklen_t salen = sizeof(sa);
 
        assert(fd >= 0);
        assert(ret);
 
        if (getsockname(fd, &sa.sa, &salen) < 0)
                return -errno;
 
        /* For local sockets we do not translate IPv6 addresses back
         * to IPv6 if applicable, since this is usually used for
         * listening sockets where the difference between IPv4 and
         * IPv6 matters. */
 
        return sockaddr_pretty(&sa.sa, salen, false, true, ret);
}
 
int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **ret) {
        char host[NI_MAXHOST];
        int r;
 
        assert(sa);
        assert(salen >= sizeof(sa_family_t));
        assert(ret);
 
        r = getnameinfo(sa, salen, host, sizeof(host), /* service= */ NULL, /* service_len= */ 0, IDN_FLAGS);
        if (r != 0) {
                if (r == EAI_MEMORY)
                        return log_oom_debug();
                if (r == EAI_SYSTEM)
                        log_debug_errno(errno, "getnameinfo() failed, ignoring: %m");
                else
                        log_debug("getnameinfo() failed, ignoring: %s", gai_strerror(r));
 
                return sockaddr_pretty(sa, salen, /* translate_ipv6= */ true, /* include_port= */ true, ret);
        }
 
        return strdup_to(ret, host);
}
 
static const char* const netlink_family_table[] = {
        [NETLINK_ROUTE]          = "route",
        [NETLINK_FIREWALL]       = "firewall",
        [NETLINK_INET_DIAG]      = "inet-diag",
        [NETLINK_NFLOG]          = "nflog",
        [NETLINK_XFRM]           = "xfrm",
        [NETLINK_SELINUX]        = "selinux",
        [NETLINK_ISCSI]          = "iscsi",
        [NETLINK_AUDIT]          = "audit",
        [NETLINK_FIB_LOOKUP]     = "fib-lookup",
        [NETLINK_CONNECTOR]      = "connector",
        [NETLINK_NETFILTER]      = "netfilter",
        [NETLINK_IP6_FW]         = "ip6-fw",
        [NETLINK_DNRTMSG]        = "dnrtmsg",
        [NETLINK_KOBJECT_UEVENT] = "kobject-uevent",
        [NETLINK_GENERIC]        = "generic",
        [NETLINK_SCSITRANSPORT]  = "scsitransport",
        [NETLINK_ECRYPTFS]       = "ecryptfs",
        [NETLINK_RDMA]           = "rdma",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
 
bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
        assert(a);
        assert(b);
 
        if (a->sa.sa_family != b->sa.sa_family)
                return false;
 
        if (a->sa.sa_family == AF_INET)
                return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
 
        if (a->sa.sa_family == AF_INET6)
                return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
 
        if (a->sa.sa_family == AF_VSOCK)
                return a->vm.svm_cid == b->vm.svm_cid;
 
        return false;
}
 
int fd_set_sndbuf(int fd, size_t n, bool increase) {
        int r, value;
        socklen_t l = sizeof(value);
 
        if (n > INT_MAX)
                return -ERANGE;
 
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                return 0;
 
        /* First, try to set the buffer size with SO_SNDBUF. */
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n);
        if (r < 0)
                return r;
 
        /* SO_SNDBUF above may set to the kernel limit, instead of the requested size.
         * So, we need to check the actual buffer size here. */
        l = sizeof(value);
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                return 1;
 
        /* If we have the privileges we will ignore the kernel limit. */
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n);
        if (r < 0)
                return r;
 
        return 1;
}
 
int fd_set_rcvbuf(int fd, size_t n, bool increase) {
        int r, value;
        socklen_t l = sizeof(value);
 
        if (n > INT_MAX)
                return -ERANGE;
 
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                return 0;
 
        /* First, try to set the buffer size with SO_RCVBUF. */
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n);
        if (r < 0)
                return r;
 
        /* SO_RCVBUF above may set to the kernel limit, instead of the requested size.
         * So, we need to check the actual buffer size here. */
        l = sizeof(value);
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                return 1;
 
        /* If we have the privileges we will ignore the kernel limit. */
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n);
        if (r < 0)
                return r;
 
        return 1;
}
 
static const char* const ip_tos_table[] = {
        [IPTOS_LOWDELAY]    = "low-delay",
        [IPTOS_THROUGHPUT]  = "throughput",
        [IPTOS_RELIABILITY] = "reliability",
        [IPTOS_LOWCOST]     = "low-cost",
};
 
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
 
bool ifname_valid_char(char a) {
        if ((unsigned char) a >= 127U)
                return false;
 
        if ((unsigned char) a <= 32U)
                return false;
 
        if (IN_SET(a,
                   ':',  /* colons are used by the legacy "alias" interface logic */
                   '/',  /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */
                   '%')) /* %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 */
                return false;
 
        return true;
}
 
bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
        bool numeric = true;
 
        /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
         * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
         * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
 
        assert(!(flags & ~_IFNAME_VALID_ALL));
 
        if (isempty(p))
                return false;
 
        /* A valid ifindex? If so, it's valid iff IFNAME_VALID_NUMERIC is set */
        if (parse_ifindex(p) >= 0)
                return flags & IFNAME_VALID_NUMERIC;
 
        if (flags & IFNAME_VALID_ALTERNATIVE) {
                if (strlen(p) >= ALTIFNAMSIZ)
                        return false;
        } else {
                if (strlen(p) >= IFNAMSIZ)
                        return false;
        }
 
        if (dot_or_dot_dot(p))
                return false;
 
        /* Let's refuse "all" and "default" as interface name, to avoid collisions with the special sysctl
         * directories /proc/sys/net/{ipv4,ipv6}/conf/{all,default} */
        if (!FLAGS_SET(flags, IFNAME_VALID_SPECIAL) && STR_IN_SET(p, "all", "default"))
                return false;
 
        for (const char *t = p; *t; t++) {
                if (!ifname_valid_char(*t))
                        return false;
 
                numeric = numeric && ascii_isdigit(*t);
        }
 
        /* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or
         * so, let's refuse that. */
        if (numeric)
                return false;
 
        return true;
}
 
bool address_label_valid(const char *p) {
 
        POINTER_MAY_BE_NULL(p);
 
        if (isempty(p))
                return false;
 
        if (strlen(p) >= IFNAMSIZ)
                return false;
 
        while (*p) {
                if ((uint8_t) *p >= 127U)
                        return false;
 
                if ((uint8_t) *p <= 31U)
                        return false;
                p++;
        }
 
        return true;
}
 
int getpeercred(int fd, struct ucred *ucred) {
        socklen_t n = sizeof(struct ucred);
        struct ucred u;
 
        assert(fd >= 0);
        assert(ucred);
 
        if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n) < 0)
                return -errno;
 
        if (n != sizeof(struct ucred))
                return -EIO;
 
        /* Check if the data is actually useful and not suppressed due to namespacing issues */
        if (!pid_is_valid(u.pid))
                return -ENODATA;
 
        /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of
         * receiving in "invalid" user/group we get the overflow UID/GID. */
 
        *ucred = u;
        return 0;
}
 
int getpeersec(int fd, char **ret) {
        _cleanup_free_ char *s = NULL;
        socklen_t n = 64;
 
        assert(fd >= 0);
        assert(ret);
 
        for (;;) {
                s = new0(char, n+1);
                if (!s)
                        return -ENOMEM;
 
                if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0) {
                        s[n] = 0;
                        break;
                }
 
                if (errno != ERANGE)
                        return -errno;
 
                s = mfree(s);
        }
 
        if (isempty(s))
                return -EOPNOTSUPP;
 
        *ret = TAKE_PTR(s);
 
        return 0;
}
 
int getpeergroups(int fd, gid_t **ret) {
        socklen_t n = sizeof(gid_t) * 64U;
        _cleanup_free_ gid_t *d = NULL;
 
        assert(fd >= 0);
        assert(ret);
 
        int ngroups_max = sysconf_ngroups_max();
        if (ngroups_max > 0)
                n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max);
 
        for (;;) {
                d = malloc(n);
                if (!d)
                        return -ENOMEM;
 
                if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
                        break;
 
                if (errno != ERANGE)
                        return -errno;
 
                d = mfree(d);
        }
 
        assert_se(n % sizeof(gid_t) == 0);
        n /= sizeof(gid_t);
 
        if (n > INT_MAX)
                return -E2BIG;
 
        *ret = TAKE_PTR(d);
 
        return (int) n;
}
 
int getpeerpidfd(int fd) {
        socklen_t n = sizeof(int);
        int pidfd = -EBADF;
 
        assert(fd >= 0);
 
        if (getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &n) < 0)
                return -errno;
 
        if (n != sizeof(int))
                return -EIO;
 
        return pidfd;
}
 
int getpeerpidref(int fd, PidRef *ret) {
        int r;
 
        assert(fd >= 0);
        assert(ret);
 
        int pidfd = getpeerpidfd(fd);
        if (pidfd < 0) {
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(pidfd))
                        return pidfd;
 
                struct ucred ucred;
                r = getpeercred(fd, &ucred);
                if (r < 0)
                        return r;
 
                return pidref_set_pid(ret, ucred.pid);
        }
 
        return pidref_set_pidfd_consume(ret, pidfd);
}
 
ssize_t send_one_fd_iov_sa(
                int transport_fd,
                int fd,
                const struct iovec *iov, size_t iovlen,
                const struct sockaddr *sa, socklen_t len,
                int flags) {
 
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control = {};
        struct msghdr mh = {
                .msg_name = (struct sockaddr*) sa,
                .msg_namelen = len,
                .msg_iov = (struct iovec *)iov,
                .msg_iovlen = iovlen,
        };
        ssize_t k;
 
        assert(transport_fd >= 0);
 
        /*