unet-cli: add DHT support
[project/unetd.git] / wg-linux.c
index 2f578c6cd04a947485f89e433ea72d226e390a9f..9eaa9c4ec622b2b3e2bc76093243cf28951495c2 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-License-Identifier: GPL-2.0+
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  *
 #include <sys/un.h>
 #include <arpa/inet.h>
 #include <linux/rtnetlink.h>
-#include <linux/wireguard.h>
 #include <netlink/msg.h>
 #include <netlink/attr.h>
 #include <netlink/socket.h>
 #include <unl.h>
 
+#include "linux/wireguard.h"
 #include "unetd.h"
 
 struct timespec64 {
@@ -102,7 +102,7 @@ wg_linux_init_local(struct network *net, struct network_peer *peer)
        struct nl_msg *msg;
 
        msg = wg_genl_msg(net, true);
-       nla_put_u16(msg, WGDEVICE_A_LISTEN_PORT, peer->port);
+       nla_put_u16(msg, WGDEVICE_A_LISTEN_PORT, peer ? peer->port : 0);
 
        return wg_genl_call(msg);
 }
@@ -147,25 +147,12 @@ wg_linux_peer_req_done(struct wg_linux_peer_req *req)
        return wg_genl_call(req->msg);
 }
 
-static int
-wg_linux_peer_update(struct network *net, struct network_peer *peer, enum wg_update_cmd cmd)
+static void
+wg_linux_peer_msg_add_allowed_ip(struct nl_msg *msg, struct network_peer *peer)
 {
-       struct wg_linux_peer_req req;
        struct blob_attr *cur;
-       struct nl_msg *msg;
-       struct nlattr *ips;
        int rem;
 
-       msg = wg_linux_peer_req_init(net, peer, &req);
-
-       if (cmd == WG_PEER_DELETE) {
-               nla_put_u32(msg, WGPEER_A_FLAGS, WGPEER_F_REMOVE_ME);
-               goto out;
-       }
-
-       nla_put_u32(msg, WGPEER_A_FLAGS, WGPEER_F_REPLACE_ALLOWEDIPS);
-
-       ips = nla_nest_start(msg, WGPEER_A_ALLOWEDIPS);
        wg_linux_msg_add_ip(msg, AF_INET6, &peer->local_addr.in6, 128);
 
        blobmsg_for_each_attr(cur, peer->ipaddr, rem) {
@@ -200,6 +187,31 @@ wg_linux_peer_update(struct network *net, struct network_peer *peer, enum wg_upd
                wg_linux_msg_add_ip(msg, af, &addr, mask);
        }
 
+}
+
+static int
+wg_linux_peer_update(struct network *net, struct network_peer *peer, enum wg_update_cmd cmd)
+{
+       struct wg_linux_peer_req req;
+       struct network_host *host;
+       struct nl_msg *msg;
+       struct nlattr *ips;
+
+       msg = wg_linux_peer_req_init(net, peer, &req);
+
+       if (cmd == WG_PEER_DELETE) {
+               nla_put_u32(msg, WGPEER_A_FLAGS, WGPEER_F_REMOVE_ME);
+               goto out;
+       }
+
+       nla_put_u32(msg, WGPEER_A_FLAGS, WGPEER_F_REPLACE_ALLOWEDIPS);
+
+       ips = nla_nest_start(msg, WGPEER_A_ALLOWEDIPS);
+
+       wg_linux_peer_msg_add_allowed_ip(msg, peer);
+       for_each_routed_host(host, net, peer)
+               wg_linux_peer_msg_add_allowed_ip(msg, &host->peer);
+
        nla_nest_end(msg, ips);
 
 out: