summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2011-04-10 03:27:00 +0200
committerLennart Poettering <lennart@poettering.net>2011-04-10 03:27:00 +0200
commit7a22745ac3c267edf89a23a920a28d86df5d0f9a (patch)
tree111d4115e6c740bc6df2f5dbcca00415c18a8432 /src
parentb9a2a36b519ccd79c4198e7dda4e657d597a14ad (diff)
socket: support netlink sockets
Diffstat (limited to 'src')
-rw-r--r--src/load-fragment.c10
-rw-r--r--src/socket-util.c279
-rw-r--r--src/socket-util.h11
-rw-r--r--src/socket.c31
4 files changed, 245 insertions, 86 deletions
diff --git a/src/load-fragment.c b/src/load-fragment.c
index aac27b56ed..3440d9158f 100644
--- a/src/load-fragment.c
+++ b/src/load-fragment.c
@@ -220,6 +220,15 @@ static int config_parse_listen(
}
path_kill_slashes(p->path);
+ } else if (streq(lvalue, "ListenNetlink")) {
+ p->type = SOCKET_SOCKET;
+
+ if (socket_address_parse_netlink(&p->address, rvalue) < 0) {
+ log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
+ free(p);
+ return 0;
+ }
+
} else {
p->type = SOCKET_SOCKET;
@@ -1892,6 +1901,7 @@ static int load_from_path(Unit *u, const char *path) {
{ "ListenDatagram", config_parse_listen, 0, &u->socket, "Socket" },
{ "ListenSequentialPacket", config_parse_listen, 0, &u->socket, "Socket" },
{ "ListenFIFO", config_parse_listen, 0, &u->socket, "Socket" },
+ { "ListenNetlink", config_parse_listen, 0, &u->socket, "Socket" },
{ "BindIPv6Only", config_parse_socket_bind, 0, &u->socket, "Socket" },
{ "Backlog", config_parse_unsigned, 0, &u->socket.backlog, "Socket" },
{ "BindToDevice", config_parse_bindtodevice, 0, &u->socket, "Socket" },
diff --git a/src/socket-util.c b/src/socket-util.c
index 9b4a1b3b48..4c28946b71 100644
--- a/src/socket-util.c
+++ b/src/socket-util.c
@@ -189,50 +189,102 @@ int socket_address_parse(SocketAddress *a, const char *s) {
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;
}
}
@@ -245,74 +297,89 @@ int socket_address_print(const SocketAddress *a, char **p) {
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;
}
}
@@ -341,7 +408,7 @@ int socket_address_listen(
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();
@@ -356,14 +423,16 @@ int socket_address_listen(
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;
@@ -397,7 +466,7 @@ int socket_address_listen(
if (r < 0)
goto fail;
- if (a->type == SOCK_STREAM)
+ if (socket_address_can_accept(a))
if (listen(fd, backlog) < 0)
goto fail;
@@ -471,6 +540,16 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
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;
@@ -493,6 +572,18 @@ bool socket_address_is(const SocketAddress *a, const char *s, int type) {
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);
@@ -523,6 +614,28 @@ bool socket_ipv6_is_supported(void) {
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",
diff --git a/src/socket-util.h b/src/socket-util.h
index 4743c37686..0e891ec2b2 100644
--- a/src/socket-util.h
+++ b/src/socket-util.h
@@ -26,6 +26,8 @@
#include <netinet/in.h>
#include <sys/un.h>
#include <net/if.h>
+#include <asm/types.h>
+#include <linux/netlink.h>
#include "macro.h"
#include "util.h"
@@ -35,6 +37,7 @@ union sockaddr_union {
struct sockaddr_in in4;
struct sockaddr_in6 in6;
struct sockaddr_un un;
+ struct sockaddr_nl nl;
struct sockaddr_storage storage;
};
@@ -47,6 +50,9 @@ typedef struct SocketAddress {
/* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
int type;
+
+ /* Socket protocol, IPPROTO_xxx, usually 0, except for netlink */
+ int protocol;
} SocketAddress;
typedef enum SocketAddressBindIPv6Only {
@@ -60,6 +66,7 @@ typedef enum SocketAddressBindIPv6Only {
#define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
int socket_address_parse(SocketAddress *a, const char *s);
+int socket_address_parse_netlink(SocketAddress *a, const char *s);
int socket_address_print(const SocketAddress *a, char **p);
int socket_address_verify(const SocketAddress *a);
@@ -77,6 +84,7 @@ int socket_address_listen(
int *ret);
bool socket_address_is(const SocketAddress *a, const char *s, int type);
+bool socket_address_is_netlink(const SocketAddress *a, const char *s);
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b);
@@ -85,6 +93,9 @@ bool socket_address_needs_mount(const SocketAddress *a, const char *prefix);
const char* socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only b);
SocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string(const char *s);
+const char* netlink_family_to_string(int b);
+int netlink_family_from_string(const char *s);
+
bool socket_ipv6_is_supported(void);
#endif
diff --git a/src/socket.c b/src/socket.c
index beb328657f..80adf16890 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -366,7 +366,10 @@ static int socket_load(Unit *u) {
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";
@@ -477,7 +480,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
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);
@@ -1447,7 +1450,10 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
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);
@@ -1542,6 +1548,25 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
}
}
+ } 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);