]> git.ipfire.org Git - network.git/commitdiff
networkd: Connect to the kernel's netlink interface
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Feb 2023 19:46:25 +0000 (19:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Feb 2023 19:46:25 +0000 (19:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c

index 960fd96cb25f807e25f04d8b80ace527c58bda51..13e12571ac08b8ca2a70caec4e987038efacd070 100644 (file)
@@ -25,6 +25,7 @@
 #include <systemd/sd-daemon.h>
 #include <systemd/sd-device.h>
 #include <systemd/sd-event.h>
+#include <systemd/sd-netlink.h>
 
 #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)