return 0;
}
+int socket_address_parse_netlink(SocketAddress *a, const char *s) {
+ int family;
+ unsigned group = 0;
+ char* sfamily = NULL;
+ assert(a);
+ assert(s);
+
+ zero(*a);
+ a->type = SOCK_RAW;
+
+ errno = 0;
+ if (sscanf(s, "%ms %u", &sfamily, &group) < 1)
+ return errno ? -errno : -EINVAL;
+
+ if ((family = netlink_family_from_string(sfamily)) < 0)
+ if (safe_atoi(sfamily, &family) < 0) {
+ free(sfamily);
+ return -EINVAL;
+ }
+
+ free(sfamily);
+
+ a->sockaddr.nl.nl_family = AF_NETLINK;
+ a->sockaddr.nl.nl_groups = group;
+
+ a->type = SOCK_RAW;
+ a->size = sizeof(struct sockaddr_nl);
+ a->protocol = family;
+
+ return 0;
+}
+
int socket_address_verify(const SocketAddress *a) {
assert(a);
switch (socket_address_family(a)) {
- case AF_INET:
- if (a->size != sizeof(struct sockaddr_in))
- return -EINVAL;
- if (a->sockaddr.in4.sin_port == 0)
- return -EINVAL;
+ case AF_INET:
+ if (a->size != sizeof(struct sockaddr_in))
+ return -EINVAL;
- return 0;
+ if (a->sockaddr.in4.sin_port == 0)
+ return -EINVAL;
- case AF_INET6:
- if (a->size != sizeof(struct sockaddr_in6))
- return -EINVAL;
+ if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
+ return -EINVAL;
- if (a->sockaddr.in6.sin6_port == 0)
- return -EINVAL;
+ return 0;
+
+ case AF_INET6:
+ if (a->size != sizeof(struct sockaddr_in6))
+ return -EINVAL;
- return 0;
+ if (a->sockaddr.in6.sin6_port == 0)
+ return -EINVAL;
- case AF_UNIX:
- if (a->size < offsetof(struct sockaddr_un, sun_path))
- return -EINVAL;
+ if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM)
+ return -EINVAL;
- if (a->size > offsetof(struct sockaddr_un, sun_path)) {
+ return 0;
- if (a->sockaddr.un.sun_path[0] != 0) {
- char *e;
+ case AF_UNIX:
+ if (a->size < offsetof(struct sockaddr_un, sun_path))
+ return -EINVAL;
- /* path */
- if (!(e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path))))
- return -EINVAL;
+ if (a->size > offsetof(struct sockaddr_un, sun_path)) {
- if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
- return -EINVAL;
- }
+ if (a->sockaddr.un.sun_path[0] != 0) {
+ char *e;
+
+ /* path */
+ if (!(e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path))))
+ return -EINVAL;
+
+ if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
+ return -EINVAL;
}
+ }
- return 0;
+ if (a->type != SOCK_STREAM && a->type != SOCK_DGRAM && a->type == SOCK_SEQPACKET)
+ return -EINVAL;
- default:
- return -EAFNOSUPPORT;
+ return 0;
+
+ case AF_NETLINK:
+
+ if (a->size != sizeof(struct sockaddr_nl))
+ return -EINVAL;
+
+ if (a->type != SOCK_RAW && a->type != SOCK_DGRAM)
+ return -EINVAL;
+
+ return 0;
+
+ default:
+ return -EAFNOSUPPORT;
}
}
return r;
switch (socket_address_family(a)) {
- case AF_INET: {
- char *ret;
- if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1)))
- return -ENOMEM;
+ case AF_INET: {
+ char *ret;
- if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) {
- free(ret);
- return -errno;
- }
+ if (!(ret = new(char, INET_ADDRSTRLEN+1+5+1)))
+ return -ENOMEM;
- sprintf(strchr(ret, 0), ":%u", ntohs(a->sockaddr.in4.sin_port));
- *p = ret;
- return 0;
+ if (!inet_ntop(AF_INET, &a->sockaddr.in4.sin_addr, ret, INET_ADDRSTRLEN)) {
+ free(ret);
+ return -errno;
}
- case AF_INET6: {
- char *ret;
+ sprintf(strchr(ret, 0), ":%u", ntohs(a->sockaddr.in4.sin_port));
+ *p = ret;
+ return 0;
+ }
- if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1)))
- return -ENOMEM;
+ case AF_INET6: {
+ char *ret;
- ret[0] = '[';
- if (!inet_ntop(AF_INET6, &a->sockaddr.in6.sin6_addr, ret+1, INET6_ADDRSTRLEN)) {
- free(ret);
- return -errno;
- }
+ if (!(ret = new(char, 1+INET6_ADDRSTRLEN+2+5+1)))
+ return -ENOMEM;
- sprintf(strchr(ret, 0), "]:%u", ntohs(a->sockaddr.in6.sin6_port));
- *p = ret;
- return 0;
+ ret[0] = '[';
+ if (!inet_ntop(AF_INET6, &a->sockaddr.in6.sin6_addr, ret+1, INET6_ADDRSTRLEN)) {
+ free(ret);
+ return -errno;
}
- case AF_UNIX: {
- char *ret;
+ sprintf(strchr(ret, 0), "]:%u", ntohs(a->sockaddr.in6.sin6_port));
+ *p = ret;
+ return 0;
+ }
- if (a->size <= offsetof(struct sockaddr_un, sun_path)) {
+ case AF_UNIX: {
+ char *ret;
- if (!(ret = strdup("<unamed>")))
- return -ENOMEM;
+ if (a->size <= offsetof(struct sockaddr_un, sun_path)) {
- } else if (a->sockaddr.un.sun_path[0] == 0) {
- /* abstract */
+ if (!(ret = strdup("<unnamed>")))
+ return -ENOMEM;
- /* FIXME: We assume we can print the
- * socket path here and that it hasn't
- * more than one NUL byte. That is
- * actually an invalid assumption */
+ } else if (a->sockaddr.un.sun_path[0] == 0) {
+ /* abstract */
- if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1)))
- return -ENOMEM;
+ /* FIXME: We assume we can print the
+ * socket path here and that it hasn't
+ * more than one NUL byte. That is
+ * actually an invalid assumption */
- ret[0] = '@';
- memcpy(ret+1, a->sockaddr.un.sun_path+1, sizeof(a->sockaddr.un.sun_path)-1);
- ret[sizeof(a->sockaddr.un.sun_path)] = 0;
+ if (!(ret = new(char, sizeof(a->sockaddr.un.sun_path)+1)))
+ return -ENOMEM;
- } else {
+ ret[0] = '@';
+ memcpy(ret+1, a->sockaddr.un.sun_path+1, sizeof(a->sockaddr.un.sun_path)-1);
+ ret[sizeof(a->sockaddr.un.sun_path)] = 0;
- if (!(ret = strdup(a->sockaddr.un.sun_path)))
- return -ENOMEM;
- }
+ } else {
- *p = ret;
- return 0;
+ if (!(ret = strdup(a->sockaddr.un.sun_path)))
+ return -ENOMEM;
}
- default:
- return -EINVAL;
+ *p = ret;
+ return 0;
+ }
+
+ case AF_NETLINK: {
+ const char *sfamily;
+
+ if ((sfamily = netlink_family_to_string(a->protocol)))
+ r = asprintf(p, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
+ else
+ r = asprintf(p, "%i %u", a->protocol, a->sockaddr.nl.nl_groups);
+
+ if (r < 0)
+ return -ENOMEM;
+
+ return 0;
+ }
+
+ default:
+ return -EINVAL;
}
}
if (r < 0)
return r;
- fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
+ fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, a->protocol);
r = fd < 0 ? -errno : 0;
label_socket_clear();
goto fail;
}
- if (bind_to_device)
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
- goto fail;
+ if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
+ if (bind_to_device)
+ if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
+ goto fail;
- if (free_bind) {
- one = 1;
- if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
- log_warning("IP_FREEBIND failed: %m");
+ if (free_bind) {
+ one = 1;
+ if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
+ log_warning("IP_FREEBIND failed: %m");
+ }
}
one = 1;
if (r < 0)
goto fail;
- if (a->type == SOCK_STREAM)
+ if (socket_address_can_accept(a))
if (listen(fd, backlog) < 0)
goto fail;
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;
+
default:
/* Cannot compare, so we assume the addresses are different */
return false;
return socket_address_equal(a, &b);
}
+bool socket_address_is_netlink(const SocketAddress *a, const char *s) {
+ struct SocketAddress b;
+
+ assert(a);
+ assert(s);
+
+ if (socket_address_parse_netlink(&b, s) < 0)
+ return false;
+
+ return socket_address_equal(a, &b);
+}
+
bool socket_address_needs_mount(const SocketAddress *a, const char *prefix) {
assert(a);
return enabled;
}
+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"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(netlink_family, int);
+
static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
[SOCKET_ADDRESS_DEFAULT] = "default",
[SOCKET_ADDRESS_BOTH] = "both",
return socket_verify(s);
}
-static const char* listen_lookup(int type) {
+static const char* listen_lookup(int family, int type) {
+
+ if (family == AF_NETLINK)
+ return "ListenNetlink";
if (type == SOCK_STREAM)
return "ListenStream";
else
t = k;
- fprintf(f, "%s%s: %s\n", prefix, listen_lookup(p->address.type), t);
+ fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
free(k);
} else
fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
if ((r = socket_address_print(&p->address, &t)) < 0)
return r;
- unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
+ if (socket_address_family(&p->address) == AF_NETLINK)
+ unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t);
+ else
+ unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
free(t);
} else {
assert(p->type == SOCKET_FIFO);
}
}
+ } else if (streq(key, "netlink")) {
+ int fd, skip = 0;
+ SocketPort *p;
+
+ if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
+ log_debug("Failed to parse socket value %s", value);
+ else {
+
+ LIST_FOREACH(port, p, s->ports)
+ if (socket_address_is_netlink(&p->address, value+skip))
+ break;
+
+ if (p) {
+ if (p->fd >= 0)
+ close_nointr_nofail(p->fd);
+ p->fd = fdset_remove(fds, fd);
+ }
+ }
+
} else
log_debug("Unknown serialization key '%s'", key);