2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <arpa/inet.h>
25 #include <netinet/ip.h>
33 #include "alloc-util.h"
36 #include "format-util.h"
40 #include "parse-util.h"
41 #include "path-util.h"
42 #include "socket-util.h"
43 #include "string-table.h"
44 #include "string-util.h"
46 #include "user-util.h"
49 #if 0 /// UNNEEDED by elogind
50 int socket_address_parse(SocketAddress *a, const char *s) {
59 a->type = SOCK_STREAM;
62 /* IPv6 in [x:.....:z]:p notation */
68 n = strndupa(s+1, e-s-1);
71 if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0)
72 return errno > 0 ? -errno : -EINVAL;
83 if (u <= 0 || u > 0xFFFF)
86 a->sockaddr.in6.sin6_family = AF_INET6;
87 a->sockaddr.in6.sin6_port = htobe16((uint16_t)u);
88 a->size = sizeof(struct sockaddr_in6);
90 } else if (*s == '/') {
96 if (l >= sizeof(a->sockaddr.un.sun_path))
99 a->sockaddr.un.sun_family = AF_UNIX;
100 memcpy(a->sockaddr.un.sun_path, s, l);
101 a->size = offsetof(struct sockaddr_un, sun_path) + l + 1;
103 } else if (*s == '@') {
104 /* Abstract AF_UNIX socket */
108 if (l >= sizeof(a->sockaddr.un.sun_path) - 1)
111 a->sockaddr.un.sun_family = AF_UNIX;
112 memcpy(a->sockaddr.un.sun_path+1, s+1, l);
113 a->size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
115 } else if (startswith(s, "vsock:")) {
116 /* AF_VSOCK socket in vsock:cid:port notation */
117 const char *cid_start = s + strlen("vsock:");
119 e = strchr(cid_start, ':');
123 r = safe_atou(e+1, &u);
127 n = strndupa(cid_start, e - cid_start);
129 r = safe_atou(n, &a->sockaddr.vm.svm_cid);
133 a->sockaddr.vm.svm_cid = VMADDR_CID_ANY;
135 a->sockaddr.vm.svm_family = AF_VSOCK;
136 a->sockaddr.vm.svm_port = u;
137 a->size = sizeof(struct sockaddr_vm);
142 r = safe_atou(e+1, &u);
146 if (u <= 0 || u > 0xFFFF)
149 n = strndupa(s, e-s);
151 /* IPv4 in w.x.y.z:p notation? */
152 r = inet_pton(AF_INET, n, &a->sockaddr.in.sin_addr);
157 /* Gotcha, it's a traditional IPv4 address */
158 a->sockaddr.in.sin_family = AF_INET;
159 a->sockaddr.in.sin_port = htobe16((uint16_t)u);
160 a->size = sizeof(struct sockaddr_in);
164 if (strlen(n) > IF_NAMESIZE-1)
167 /* Uh, our last resort, an interface name */
168 idx = if_nametoindex(n);
172 a->sockaddr.in6.sin6_family = AF_INET6;
173 a->sockaddr.in6.sin6_port = htobe16((uint16_t)u);
174 a->sockaddr.in6.sin6_scope_id = idx;
175 a->sockaddr.in6.sin6_addr = in6addr_any;
176 a->size = sizeof(struct sockaddr_in6);
181 r = safe_atou(s, &u);
185 if (u <= 0 || u > 0xFFFF)
188 if (socket_ipv6_is_supported()) {
189 a->sockaddr.in6.sin6_family = AF_INET6;
190 a->sockaddr.in6.sin6_port = htobe16((uint16_t)u);
191 a->sockaddr.in6.sin6_addr = in6addr_any;
192 a->size = sizeof(struct sockaddr_in6);
194 a->sockaddr.in.sin_family = AF_INET;
195 a->sockaddr.in.sin_port = htobe16((uint16_t)u);
196 a->sockaddr.in.sin_addr.s_addr = INADDR_ANY;
197 a->size = sizeof(struct sockaddr_in);
205 int socket_address_parse_and_warn(SocketAddress *a, const char *s) {
209 /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */
211 r = socket_address_parse(&b, s);
215 if (!socket_ipv6_is_supported() && b.sockaddr.sa.sa_family == AF_INET6) {
216 log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
217 return -EAFNOSUPPORT;
224 int socket_address_parse_netlink(SocketAddress *a, const char *s) {
227 _cleanup_free_ char *sfamily = NULL;
235 if (sscanf(s, "%ms %u", &sfamily, &group) < 1)
236 return errno > 0 ? -errno : -EINVAL;
238 family = netlink_family_from_string(sfamily);
242 a->sockaddr.nl.nl_family = AF_NETLINK;
243 a->sockaddr.nl.nl_groups = group;
246 a->size = sizeof(struct sockaddr_nl);
247 a->protocol = family;
252 int socket_address_verify(const SocketAddress *a) {
255 switch (socket_address_family(a)) {
258 if (a->size != sizeof(struct sockaddr_in))
261 if (a->sockaddr.in.sin_port == 0)
264 if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
270 if (a->size != sizeof(struct sockaddr_in6))
273 if (a->sockaddr.in6.sin6_port == 0)
276 if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
282 if (a->size < offsetof(struct sockaddr_un, sun_path))
285 if (a->size > offsetof(struct sockaddr_un, sun_path)) {
287 if (a->sockaddr.un.sun_path[0] != 0) {
291 e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
295 if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
300 if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM && a->type != SOCK_SEQPACKET)
307 if (a->size != sizeof(struct sockaddr_nl))
310 if (a->type != SOCK_RAW && a->type != SOCK_DGRAM)
316 if (a->size != sizeof(struct sockaddr_vm))
319 if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
325 return -EAFNOSUPPORT;
329 int socket_address_print(const SocketAddress *a, char **ret) {
335 r = socket_address_verify(a);
339 if (socket_address_family(a) == AF_NETLINK) {
340 _cleanup_free_ char *sfamily = NULL;
342 r = netlink_family_to_string_alloc(a->protocol, &sfamily);
346 r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
353 return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
356 bool socket_address_can_accept(const SocketAddress *a) {
360 a->type == SOCK_STREAM ||
361 a->type == SOCK_SEQPACKET;
364 bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
368 /* Invalid addresses are unequal to all */
369 if (socket_address_verify(a) < 0 ||
370 socket_address_verify(b) < 0)
373 if (a->type != b->type)
376 if (socket_address_family(a) != socket_address_family(b))
379 switch (socket_address_family(a)) {
382 if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
385 if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
391 if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
394 if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
400 if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
401 b->size <= offsetof(struct sockaddr_un, sun_path))
404 if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
407 if (a->sockaddr.un.sun_path[0]) {
408 if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path))
411 if (a->size != b->size)
414 if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
421 if (a->protocol != b->protocol)
424 if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
430 if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid)
433 if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port)
439 /* Cannot compare, so we assume the addresses are different */
446 bool socket_address_is(const SocketAddress *a, const char *s, int type) {
447 struct SocketAddress b;
452 if (socket_address_parse(&b, s) < 0)
457 return socket_address_equal(a, &b);
460 bool socket_address_is_netlink(const SocketAddress *a, const char *s) {
461 struct SocketAddress b;
466 if (socket_address_parse_netlink(&b, s) < 0)
469 return socket_address_equal(a, &b);
472 const char* socket_address_get_path(const SocketAddress *a) {
475 if (socket_address_family(a) != AF_UNIX)
478 if (a->sockaddr.un.sun_path[0] == 0)
481 return a->sockaddr.un.sun_path;
485 bool socket_ipv6_is_supported(void) {
486 if (access("/proc/net/if_inet6", F_OK) != 0)
492 #if 0 /// UNNEEDED by elogind
493 bool socket_address_matches_fd(const SocketAddress *a, int fd) {
500 b.size = sizeof(b.sockaddr);
501 if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
504 if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
507 solen = sizeof(b.type);
508 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
511 if (b.type != a->type)
514 if (a->protocol != 0) {
515 solen = sizeof(b.protocol);
516 if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
519 if (b.protocol != a->protocol)
523 return socket_address_equal(a, &b);
526 int sockaddr_port(const struct sockaddr *_sa, unsigned *port) {
527 union sockaddr_union *sa = (union sockaddr_union*) _sa;
531 switch (sa->sa.sa_family) {
533 *port = be16toh(sa->in.sin_port);
537 *port = be16toh(sa->in6.sin6_port);
541 *port = sa->vm.svm_port;
545 return -EAFNOSUPPORT;
549 int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret) {
550 union sockaddr_union *sa = (union sockaddr_union*) _sa;
555 assert(salen >= sizeof(sa->sa.sa_family));
557 switch (sa->sa.sa_family) {
562 a = be32toh(sa->in.sin_addr.s_addr);
567 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
568 be16toh(sa->in.sin_port));
572 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
579 static const unsigned char ipv4_prefix[] = {
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
583 if (translate_ipv6 &&
584 memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
585 const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
589 a[0], a[1], a[2], a[3],
590 be16toh(sa->in6.sin6_port));
594 a[0], a[1], a[2], a[3]);
598 char a[INET6_ADDRSTRLEN];
600 inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a));
606 be16toh(sa->in6.sin6_port));
620 if (salen <= offsetof(struct sockaddr_un, sun_path)) {
621 p = strdup("<unnamed>");
625 } else if (sa->un.sun_path[0] == 0) {
628 /* FIXME: We assume we can print the
629 * socket path here and that it hasn't
630 * more than one NUL byte. That is
631 * actually an invalid assumption */
633 p = new(char, sizeof(sa->un.sun_path)+1);
638 memcpy(p+1, sa->un.sun_path+1, sizeof(sa->un.sun_path)-1);
639 p[sizeof(sa->un.sun_path)] = 0;
642 p = strndup(sa->un.sun_path, sizeof(sa->un.sun_path));
656 r = asprintf(&p, "vsock:%u", sa->vm.svm_cid);
670 int getpeername_pretty(int fd, bool include_port, char **ret) {
671 union sockaddr_union sa;
672 socklen_t salen = sizeof(sa);
678 if (getpeername(fd, &sa.sa, &salen) < 0)
681 if (sa.sa.sa_family == AF_UNIX) {
682 struct ucred ucred = {};
684 /* UNIX connection sockets are anonymous, so let's use
685 * PID/UID as pretty credentials instead */
687 r = getpeercred(fd, &ucred);
691 if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
697 /* For remote sockets we translate IPv6 addresses back to IPv4
698 * if applicable, since that's nicer. */
700 return sockaddr_pretty(&sa.sa, salen, true, include_port, ret);
703 int getsockname_pretty(int fd, char **ret) {
704 union sockaddr_union sa;
705 socklen_t salen = sizeof(sa);
710 if (getsockname(fd, &sa.sa, &salen) < 0)
713 /* For local sockets we do not translate IPv6 addresses back
714 * to IPv6 if applicable, since this is usually used for
715 * listening sockets where the difference between IPv4 and
718 return sockaddr_pretty(&sa.sa, salen, false, true, ret);
721 int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) {
723 char host[NI_MAXHOST], *ret;
727 r = getnameinfo(&sa->sa, salen, host, sizeof(host), NULL, 0,
728 NI_IDN|NI_IDN_USE_STD3_ASCII_RULES);
730 int saved_errno = errno;
732 r = sockaddr_pretty(&sa->sa, salen, true, true, &ret);
736 log_debug_errno(saved_errno, "getnameinfo(%s) failed: %m", ret);
747 int getnameinfo_pretty(int fd, char **ret) {
748 union sockaddr_union sa;
749 socklen_t salen = sizeof(sa);
754 if (getsockname(fd, &sa.sa, &salen) < 0)
757 return socknameinfo_pretty(&sa, salen, ret);
760 int socket_address_unlink(SocketAddress *a) {
763 if (socket_address_family(a) != AF_UNIX)
766 if (a->sockaddr.un.sun_path[0] == 0)
769 if (unlink(a->sockaddr.un.sun_path) < 0)
775 static const char* const netlink_family_table[] = {
776 [NETLINK_ROUTE] = "route",
777 [NETLINK_FIREWALL] = "firewall",
778 [NETLINK_INET_DIAG] = "inet-diag",
779 [NETLINK_NFLOG] = "nflog",
780 [NETLINK_XFRM] = "xfrm",
781 [NETLINK_SELINUX] = "selinux",
782 [NETLINK_ISCSI] = "iscsi",
783 [NETLINK_AUDIT] = "audit",
784 [NETLINK_FIB_LOOKUP] = "fib-lookup",
785 [NETLINK_CONNECTOR] = "connector",
786 [NETLINK_NETFILTER] = "netfilter",
787 [NETLINK_IP6_FW] = "ip6-fw",
788 [NETLINK_DNRTMSG] = "dnrtmsg",
789 [NETLINK_KOBJECT_UEVENT] = "kobject-uevent",
790 [NETLINK_GENERIC] = "generic",
791 [NETLINK_SCSITRANSPORT] = "scsitransport",
792 [NETLINK_ECRYPTFS] = "ecryptfs"
795 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
797 static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
798 [SOCKET_ADDRESS_DEFAULT] = "default",
799 [SOCKET_ADDRESS_BOTH] = "both",
800 [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only"
803 DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only);
805 bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
809 if (a->sa.sa_family != b->sa.sa_family)
812 if (a->sa.sa_family == AF_INET)
813 return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
815 if (a->sa.sa_family == AF_INET6)
816 return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
818 if (a->sa.sa_family == AF_VSOCK)
819 return a->vm.svm_cid == b->vm.svm_cid;
825 int fd_inc_sndbuf(int fd, size_t n) {
827 socklen_t l = sizeof(value);
829 r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
830 if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
833 /* If we have the privileges we will ignore the kernel limit. */
836 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0)
837 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0)
843 int fd_inc_rcvbuf(int fd, size_t n) {
845 socklen_t l = sizeof(value);
847 r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
848 if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
851 /* If we have the privileges we will ignore the kernel limit. */
854 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0)
855 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0)
860 #if 0 /// UNNEEDED by elogind
861 static const char* const ip_tos_table[] = {
862 [IPTOS_LOWDELAY] = "low-delay",
863 [IPTOS_THROUGHPUT] = "throughput",
864 [IPTOS_RELIABILITY] = "reliability",
865 [IPTOS_LOWCOST] = "low-cost",
868 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
871 bool ifname_valid(const char *p) {
874 /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
875 * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
876 * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
881 if (strlen(p) >= IFNAMSIZ)
884 if (dot_or_dot_dot(p))
888 if ((unsigned char) *p >= 127U)
891 if ((unsigned char) *p <= 32U)
894 if (*p == ':' || *p == '/')
897 numeric = numeric && (*p >= '0' && *p <= '9');
907 int getpeercred(int fd, struct ucred *ucred) {
908 socklen_t n = sizeof(struct ucred);
915 r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n);
919 if (n != sizeof(struct ucred))
922 /* Check if the data is actually useful and not suppressed due
923 * to namespacing issues */
926 if (u.uid == UID_INVALID)
928 if (u.gid == GID_INVALID)
935 int getpeersec(int fd, char **ret) {
947 r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
958 r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
977 const struct sockaddr *sa, socklen_t len,
981 struct cmsghdr cmsghdr;
982 uint8_t buf[CMSG_SPACE(sizeof(int))];
985 .msg_name = (struct sockaddr*) sa,
987 .msg_control = &control,
988 .msg_controllen = sizeof(control),
990 struct cmsghdr *cmsg;
992 assert(transport_fd >= 0);
995 cmsg = CMSG_FIRSTHDR(&mh);
996 cmsg->cmsg_level = SOL_SOCKET;
997 cmsg->cmsg_type = SCM_RIGHTS;
998 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
999 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
1001 mh.msg_controllen = CMSG_SPACE(sizeof(int));
1002 if (sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags) < 0)
1008 #if 0 /// UNNEEDED by elogind
1009 int receive_one_fd(int transport_fd, int flags) {
1011 struct cmsghdr cmsghdr;
1012 uint8_t buf[CMSG_SPACE(sizeof(int))];
1014 struct msghdr mh = {
1015 .msg_control = &control,
1016 .msg_controllen = sizeof(control),
1018 struct cmsghdr *cmsg, *found = NULL;
1020 assert(transport_fd >= 0);
1023 * Receive a single FD via @transport_fd. We don't care for
1024 * the transport-type. We retrieve a single FD at most, so for
1025 * packet-based transports, the caller must ensure to send
1026 * only a single FD per packet. This is best used in
1027 * combination with send_one_fd().
1030 if (recvmsg(transport_fd, &mh, MSG_NOSIGNAL | MSG_CMSG_CLOEXEC | flags) < 0)
1033 CMSG_FOREACH(cmsg, &mh) {
1034 if (cmsg->cmsg_level == SOL_SOCKET &&
1035 cmsg->cmsg_type == SCM_RIGHTS &&
1036 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
1044 cmsg_close_all(&mh);
1048 return *(int*) CMSG_DATA(found);
1051 ssize_t next_datagram_size_fd(int fd) {
1055 /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will
1056 * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't
1057 * do. This difference is actually of major importance as we need to be sure that the size returned here
1058 * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of
1059 * the wrong size. */
1061 l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC);
1063 if (errno == EOPNOTSUPP || errno == EFAULT)
1076 /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD
1077 * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */
1079 if (ioctl(fd, FIONREAD, &k) < 0)
1085 int flush_accept(int fd) {
1087 struct pollfd pollfd = {
1094 /* Similar to flush_fd() but flushes all incoming connection by accepting them and immediately closing them. */
1099 r = poll(&pollfd, 1, 0);
1109 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
1114 if (errno == EAGAIN)
1125 struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) {
1126 struct cmsghdr *cmsg;
1130 CMSG_FOREACH(cmsg, mh)
1131 if (cmsg->cmsg_level == level &&
1132 cmsg->cmsg_type == type &&
1133 (length == (socklen_t) -1 || length == cmsg->cmsg_len))
1139 #if 0 /// UNNEEDED by elogind
1140 int socket_ioctl_fd(void) {
1143 /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for
1144 * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not
1145 * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more
1146 * generic AF_NETLINK. */
1148 fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1150 fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC);