From: Michael Tremer Date: Thu, 9 Feb 2023 19:46:25 +0000 (+0000) Subject: networkd: Connect to the kernel's netlink interface X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e62b300ef78484b882d2da8683ebe79896523a13;p=network.git networkd: Connect to the kernel's netlink interface Signed-off-by: Michael Tremer --- diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index 960fd96c..13e12571 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "bus.h" #include "config.h" @@ -45,6 +46,9 @@ struct nw_daemon { // Event Loop sd_event* loop; + // Netlink Connection + sd_netlink* rtnl; + // DBus Connection sd_bus* bus; @@ -185,6 +189,47 @@ static int nw_start_device_monitor(struct nw_daemon* daemon) { return 0; } +static int _rtnl_dummy(sd_netlink* rtnl, sd_netlink_message* message, void* data) { + DEBUG("_rtnl_dummy called\n"); + + return 0; +} + +static int nw_daemon_connect_rtnl(struct nw_daemon* daemon, int fd) { + int r; + + // Connect to Netlink + r = sd_netlink_open(&daemon->rtnl); + if (r < 0) { + ERROR("Could not connect to the kernel's netlink interface: %m\n"); + return 1; + } + + // Increase the receive buffer + r = sd_netlink_increase_rxbuf(daemon->rtnl, RCVBUF_SIZE); + if (r < 0) { + ERROR("Could not increase receive buffer for the netlink socket: %m\n"); + return 1; + } + + // Connect it to the event loop + r = sd_netlink_attach_event(daemon->rtnl, daemon->loop, 0); + if (r < 0) { + ERROR("Could not connect the netlink socket to the event loop: %m\n"); + return 1; + } + + // Register callback for new interfaces + r = sd_netlink_add_match(daemon->rtnl, NULL, RTM_NEWLINK, _rtnl_dummy, NULL, + daemon, "networkd-RTM_NEWLINK"); + if (r < 0) { + ERROR("Could not register RTM_NEWLINK: %m\n"); + return 1; + } + + return 0; +} + static int nw_daemon_setup(struct nw_daemon* daemon) { int r; @@ -198,6 +243,11 @@ static int nw_daemon_setup(struct nw_daemon* daemon) { if (r) return r; + // Connect to the kernel's netlink interface + r = nw_daemon_connect_rtnl(daemon, 0); + if (r) + return r; + // Connect to the system bus r = nw_bus_connect(daemon->bus, daemon->loop, daemon); if (r)