Skip to contentSkip to conversation

wireguard: Add netlink attrs for binding to address and netdev

PATCH New Daniel Gröber 1 participant 1 message Feb 19, 2024

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

PATCH v3 1/1 wireguard: Add netlink attrs for binding to address and netdev

Currently wg sockets are always bound to the wildcard address, this has two major downsides we care about:

1) It prevents re-using listen one port numbers on different ifaces/IPs.

Our use case lies in dynamically routed wg mesh networks. Here running one wg tunnel with AllowedIPs=::/0[1] per node *pair* is not unusual, but needing unique ports across the whole network actually represents a tangible scalability concern because as few as n=363 nodes will cause port exhaustion since n*(n-1)/2 > 2^16-1.

[1]: The need to do that comes from the lack of routing daemon support for
dynamically managing AllowedIPs which in turn is due to poor scalability of the current netlink interface.

2) It has been demonstrated that having tunnel underlay and overlay routes mixed into one routing table is fraught with peril[2] so it is desirable to allow wireguard sockets to bind to a VRF l3mdev to separate the two.

Technically ip-rules hacks could be applied to make this happen but since routing table separation is a serious security concern we should make doing it right easy.

[2]: Xue et. al. Bypassing Tunnels: Leaking VPN Client Traffic by Abusing
Routing Tables.
https://www.usenix.org/conference/usenixsecurity23/presentation/xue

The implementation is intentionally backwards compatible. Older wireguard-tools userspace will only show WGDEVICE_A_LISTEN_PORT even when we're listening on an IP or netdev and omitting the new listen attrs during set results in a wildcard bound socket as ever.

---
Changes in v3:
- Rewrite commit message

Userspace wireguard-tools patches are here:
https://lists.zx2c4.com/pipermail/wireguard/2023-October/thread.html#8222
wg: Support binding to specific addr and iface for multihomed hosts

 drivers/net/wireguard/device.c  |  4 +--
 drivers/net/wireguard/device.h  |  3 +-
 drivers/net/wireguard/netlink.c | 56 ++++++++++++++++++++++++++++-----
 drivers/net/wireguard/socket.c  | 41 +++++++++++++++---------
 drivers/net/wireguard/socket.h  |  3 +-
 include/uapi/linux/wireguard.h  |  6 ++++
 6 files changed, 88 insertions(+), 25 deletions(-)
drivers/net/wireguard/device.c +2 -2
index deb9636b0ecf..ec28f5021791 100644
@@ -48,7 +48,7 @@ static int wg_open(struct net_device *dev)
 		dev_v6->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_NONE;
 
 	mutex_lock(&wg->device_update_lock);
-	ret = wg_socket_init(wg, wg->incoming_port);
+	ret = wg_socket_init(wg, wg->port_cfg);
 	if (ret < 0)
 		goto out;
 	list_for_each_entry(peer, &wg->peer_list, peer_list) {
@@ -249,7 +249,7 @@ static void wg_destruct(struct net_device *dev)
 	rtnl_unlock();
 	mutex_lock(&wg->device_update_lock);
 	rcu_assign_pointer(wg->creating_net, NULL);
-	wg->incoming_port = 0;
+	memzero_explicit(&wg->port_cfg, sizeof(wg->port_cfg));
 	wg_socket_reinit(wg, NULL, NULL);
 	/* The final references are cleared in the below calls to destroy_workqueue. */
 	wg_peer_remove_all(wg);
drivers/net/wireguard/device.h +2 -1
index 43c7cebbf50b..ac4092d8c9d0 100644
@@ -17,6 +17,7 @@
 #include <linux/mutex.h>
 #include <linux/net.h>
 #include <linux/ptr_ring.h>
+#include <net/udp_tunnel.h>
 
 struct wg_device;
 
@@ -53,7 +54,7 @@ struct wg_device {
 	atomic_t handshake_queue_len;
 	unsigned int num_peers, device_update_gen;
 	u32 fwmark;
-	u16 incoming_port;
+	struct udp_port_cfg port_cfg;
 };
 
 int wg_device_init(void);
drivers/net/wireguard/netlink.c +49 -7

93 lines not on this page · raw message

drivers/net/wireguard/socket.c +27 -14

89 lines not on this page · raw message

drivers/net/wireguard/socket.h +2 -1
index bab5848efbcd..1532a263c518 100644
@@ -10,8 +10,9 @@
 #include <linux/udp.h>
 #include <linux/if_vlan.h>
 #include <linux/if_ether.h>
+#include <net/udp_tunnel.h>
 
-int wg_socket_init(struct wg_device *wg, u16 port);
+int wg_socket_init(struct wg_device *wg, struct udp_port_cfg port);
 void wg_socket_reinit(struct wg_device *wg, struct sock *new4,
 		      struct sock *new6);
 int wg_socket_send_buffer_to_peer(struct wg_peer *peer, void *data,
include/uapi/linux/wireguard.h +6 -0
index ae88be14c947..240d1c850dfd 100644
@@ -28,6 +28,8 @@
  *    WGDEVICE_A_PRIVATE_KEY: NLA_EXACT_LEN, len WG_KEY_LEN
  *    WGDEVICE_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN
  *    WGDEVICE_A_LISTEN_PORT: NLA_U16
+ *    WGDEVICE_A_LISTEN_ADDR : NLA_MIN_LEN(struct sockaddr), struct sockaddr_in or struct sockaddr_in6
+ *    WGDEVICE_A_LISTEN_IFINDEX : NLA_U32
  *    WGDEVICE_A_FWMARK: NLA_U32
  *    WGDEVICE_A_PEERS: NLA_NESTED
  *        0: NLA_NESTED
@@ -82,6 +84,8 @@
  *                      peers should be removed prior to adding the list below.
  *    WGDEVICE_A_PRIVATE_KEY: len WG_KEY_LEN, all zeros to remove
  *    WGDEVICE_A_LISTEN_PORT: NLA_U16, 0 to choose randomly
+ *    WGDEVICE_A_LISTEN_ADDR : struct sockaddr_in or struct sockaddr_in6.
+ *    WGDEVICE_A_LISTEN_IFINDEX : NLA_U32
  *    WGDEVICE_A_FWMARK: NLA_U32, 0 to disable
  *    WGDEVICE_A_PEERS: NLA_NESTED
  *        0: NLA_NESTED
@@ -157,6 +161,8 @@ enum wgdevice_attribute {
 	WGDEVICE_A_LISTEN_PORT,
 	WGDEVICE_A_FWMARK,
 	WGDEVICE_A_PEERS,
+	WGDEVICE_A_LISTEN_ADDR,
+	WGDEVICE_A_LISTEN_IFINDEX,
 	__WGDEVICE_A_LAST
 };
 #define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1)
Signature · 1 line
2.39.2