Commit 7a22745a authored by Lennart Poettering's avatar Lennart Poettering

socket: support netlink sockets

parent b9a2a36b
......@@ -218,6 +218,22 @@
directive above.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>ListenNetlink=</varname></term>
<listitem><para>Specifies a Netlink
family to create a socket for to
listen on. This expects a short string
referring to the AF_NETLINK family
name (such as <varname>audit</varname>
or <varname>kobject-uevent</varname>)
as argument, optionally suffixed by a
whitespace followed by a multicast
group integer. Behaviour otherwise is
very similar to the
<varname>ListenDatagram=</varname>
directive above.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>BindIPv6Only=</varname></term>
<listitem><para>Takes a one of
......
......@@ -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" },
......
This diff is collapsed.
......@@ -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
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment