Skip to contentSkip to conversation

wireguard-linux: add netlink multicast group for notifications on peer change

PATCH In review Raphael Catolino 2 participants 2 messages Dec 20, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wireguard-linux: add netlink multicast group for notifications on peer change

From: Linus Karl <[email protected]>

Add a multicast group to the wireguard netlink family, and three message types to notify subscribers when a peer has changed .

---
 drivers/net/wireguard/device.h  |   2 +
 drivers/net/wireguard/netlink.c | 231 +++++++++++++++++++++++++++++++-
 drivers/net/wireguard/netlink.h |   6 +
 drivers/net/wireguard/peer.c    |   7 +-
 drivers/net/wireguard/socket.c  |   5 +
 include/uapi/linux/wireguard.h  |  69 ++++++++++
 6 files changed, 316 insertions(+), 4 deletions(-)
drivers/net/wireguard/device.h +2 -0
index 43c7cebbf50b..137b1b517815 100644
@@ -54,6 +54,8 @@ struct wg_device {
 	unsigned int num_peers, device_update_gen;
 	u32 fwmark;
 	u16 incoming_port;
+	bool endpoint_monitor;
+	bool peers_monitor;
 };
 
 int wg_device_init(void);
drivers/net/wireguard/netlink.c +228 -3

270 lines not on this page · raw message

drivers/net/wireguard/netlink.h +6 -0
index 15100d92e2e3..28e2e307c838 100644
@@ -6,6 +6,12 @@
 #ifndef _WG_NETLINK_H
 #define _WG_NETLINK_H
 
+#include "peer.h"
+
+int wg_genl_mcast_peer_endpoint_change(struct wg_peer *peer);
+int wg_genl_mcast_peer_remove(struct wg_peer *peer);
+int wg_genl_mcast_peer_set(struct wg_peer *peer);
+
 int wg_genetlink_init(void);
 void wg_genetlink_uninit(void);
 
drivers/net/wireguard/peer.c +6 -1
index 1cb502a932e0..97407304152d 100644
@@ -9,6 +9,7 @@
 #include "timers.h"
 #include "peerlookup.h"
 #include "noise.h"
+#include "netlink.h"
 
 #include <linux/kref.h>
 #include <linux/lockdep.h>
@@ -157,8 +158,12 @@ void wg_peer_remove(struct wg_peer *peer)
 {
 	if (unlikely(!peer))
 		return;
-	lockdep_assert_held(&peer->device->device_update_lock);
 
+	if (peer->device->peers_monitor) {
+		wg_genl_mcast_peer_remove(peer);
+	}
+
+	lockdep_assert_held(&peer->device->device_update_lock);
 	peer_make_dead(peer);
 	synchronize_net();
 	peer_remove_after_dead(peer);
drivers/net/wireguard/socket.c +5 -0
index 0414d7a6ce74..33e4da2a37ee 100644
@@ -8,6 +8,7 @@
 #include "socket.h"
 #include "queueing.h"
 #include "messages.h"
+#include "netlink.h"
 
 #include <linux/ctype.h>
 #include <linux/net.h>
@@ -294,6 +295,10 @@ void wg_socket_set_peer_endpoint(struct wg_peer *peer,
 	dst_cache_reset(&peer->endpoint_cache);
 out:
 	write_unlock_bh(&peer->endpoint_lock);
+
+	if (peer->device->endpoint_monitor) {
+		wg_genl_mcast_peer_endpoint_change(peer);
+	}
 }
 
 void wg_socket_set_peer_endpoint_from_skb(struct wg_peer *peer,
include/uapi/linux/wireguard.h +69 -0

105 lines not on this page · raw message

base-commit: 0cf9deb3005f552a3d125436fc8ccedd31a925a9

Signature · 1 line
2.42.0
#1 lore raw

Hi Raphael, Linus,

Interesting patch, I've been meaning to get around to adding some change notifications as well :)

I have some notes:

- It seem to me some more sharing with existing code constructing nlmsgs ought to be possible your new code seems quite verbose.

- Why is the endpoint_monitor flag necessary? Other genlmsg_multicast callers don't seem to do anything like this.

Do you have corresponding wireguard-tools userspace patches for this yet?

--Daniel

PS: If you intend for this to get applied you may want to fix scripts/checkpatch.pl warnings and add people get_maintainers.pl spits out to Cc.