net: wireguard: Allow binding to specific ifindex
Subsystems (3)
PATCH 1/1 net: wireguard: Allow binding to specific ifindex
From: Ben Greear <[email protected]>
Which allows us to bind to VRF.
---
NOTE: Modified user-space to utilize this may be found here:
https://github.com/greearb/wireguard-tools-ct
Only the 'wg' part has been tested with this new feature as of today.
drivers/net/wireguard/device.h | 1 + drivers/net/wireguard/netlink.c | 12 +++++++++++- drivers/net/wireguard/socket.c | 8 +++++++- include/uapi/linux/wireguard.h | 3 +++ 4 files changed, 22 insertions(+), 2 deletions(-)
drivers/net/wireguard/device.h +1 -0
@@ -53,6 +53,7 @@ struct wg_device {atomic_t handshake_queue_len;unsigned int num_peers, device_update_gen;u32 fwmark; +int lowerdev;/* ifindex of lower level device to bind UDP transport */ u16 incoming_port; };
drivers/net/wireguard/netlink.c +11 -1
@@ -27,7 +27,8 @@ static const struct nla_policy device_policy[WGDEVICE_A_MAX + 1] = { [WGDEVICE_A_FLAGS] = { .type = NLA_U32 }, [WGDEVICE_A_LISTEN_PORT] = { .type = NLA_U16 }, [WGDEVICE_A_FWMARK] = { .type = NLA_U32 }, - [WGDEVICE_A_PEERS ] = { .type =NLA_NESTED } + [WGDEVICE_A_PEERS] = { .type = NLA_NESTED }, + [WGDEVICE_A_LOWERDEV] = { .type = NLA_U32 }, };static const struct nla_policy peer_policy[WGPEER_A_MAX +1 ] = {
@@ -232,6 +233,7 @@ static int wg_get_device_dump(struct sk_buff *skb, struct netlink_callback *cb)if (nla_put_u16 (skb,WGDEVICE_A_LISTEN_PORT , wg->incoming_port) ||nla_put_u32 (skb,WGDEVICE_A_FWMARK , wg->fwmark) || +nla_put_u32 (skb,WGDEVICE_A_LOWERDEV , wg->lowerdev) ||nla_put_u32 (skb,WGDEVICE_A_IFINDEX , wg->dev->ifindex) ||nla_put_string (skb,WGDEVICE_A_IFNAME , wg->dev->name)) goto out;
@@ -530,6 +532,14 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)wg_socket_clear_peer_endpoint_src (peer); } +if (info->attrs[WGDEVICE_A_LOWERDEV ]) { +struct wg_peer *peer; + + wg->lowerdev =nla_get_u32 (info->attrs[WGDEVICE_A_LOWERDEV ]); +list_for_each_entry (peer, &wg->peer_list, peer_list) +wg_socket_clear_peer_endpoint_src (peer); + } +if (info->attrs[WGDEVICE_A_LISTEN_PORT ]) { ret =set_port (wg,nla_get_u16 (info->attrs[WGDEVICE_A_LISTEN_PORT ]));
drivers/net/wireguard/socket.c +7 -1
@@ -25,7 +25,8 @@ static int send4(struct wg_device *wg, struct sk_buff *skb, .daddr = endpoint->addr4.sin_addr.s_addr, .fl4_dport = endpoint->addr4.sin_port, .flowi4_mark = wg->fwmark, - .flowi4_proto = IPPROTO_UDP + .flowi4_proto = IPPROTO_UDP, + .flowi4_oif = wg->lowerdev, };struct rtable *rt =NULL ;struct sock *sock;
@@ -111,6 +112,9 @@ static int send6(struct wg_device *wg, struct sk_buff *skb,struct sock *sock;int ret =0 ; +if (wg->lowerdev) + fl.flowi6_oif = wg->lowerdev, +skb_mark_not_on_list (skb); skb->dev = wg->dev; skb->mark = wg->fwmark;
@@ -360,6 +364,7 @@ int wg_socket_init(struct wg_device *wg, u16 port) .family = AF_INET, .local_ip.s_addr = htonl(INADDR_ANY), .local_udp_port = htons(port), + .bind_ifindex = wg->lowerdev, .use_udp_checksums = true };#if IS_ENABLED (CONFIG_IPV6 )
@@ -369,6 +374,7 @@ int wg_socket_init(struct wg_device *wg, u16 port) .local_ip6 = IN6ADDR_ANY_INIT, .use_udp6_tx_checksums = true, .use_udp6_rx_checksums = true, + .bind_ifindex = wg->lowerdev, .ipv6_v6only = true };#endif
include/uapi/linux/wireguard.h +3 -0
@@ -29,6 +29,7 @@* WGDEVICE_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN * WGDEVICE_A_LISTEN_PORT: NLA_U16 * WGDEVICE_A_FWMARK: NLA_U32 +* WGDEVICE_A_LOWERDEV: NLA_U32 * WGDEVICE_A_PEERS: NLA_NESTED * 0: NLA_NESTED * WGPEER_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN
@@ -83,6 +84,7 @@* WGDEVICE_A_PRIVATE_KEY: len WG_KEY_LEN, all zeros to remove * WGDEVICE_A_LISTEN_PORT: NLA_U16, 0 to choose randomly * WGDEVICE_A_FWMARK: NLA_U32, 0 to disable +* WGDEVICE_A_LOWERDEV: NLA_U32, ifindex to bind lower transport, 0 to disable * WGDEVICE_A_PEERS: NLA_NESTED * 0: NLA_NESTED * WGPEER_A_PUBLIC_KEY: len WG_KEY_LEN
@@ -157,6 +159,7 @@ enum wgdevice_attribute {WGDEVICE_A_LISTEN_PORT ,WGDEVICE_A_FWMARK ,WGDEVICE_A_PEERS , +WGDEVICE_A_LOWERDEV , __WGDEVICE_A_LAST };#define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1)
Signature · 1 line
2.42.0
Simon Horman
reviewerQuoted 10 lines · On Mon, Nov 25, 2024 at 01:21:11PM -0800, [email protected] wrote:
From: Ben Greear <[email protected]>
Which allows us to bind to VRF.
Signed-off-by Ben Greear <[email protected]>---
NOTE: Modified user-space to utilize this may be found here:
https://github.com/greearb/wireguard-tools-ct
Only the 'wg' part has been tested with this new feature as of today.
...
Quoted 15 lines
drivers/net/wireguard/socket.c +2
-1@@ -25,7 +25,8 @@ static int send4(struct wg_device *wg, struct sk_buff *skb, .daddr = endpoint->addr4.sin_addr.s_addr, .fl4_dport = endpoint->addr4.sin_port, .flowi4_mark = wg->fwmark, - .flowi4_proto = IPPROTO_UDP + .flowi4_proto = IPPROTO_UDP, + .flowi4_oif = wg->lowerdev, };struct rtable *rt =NULL ;struct sock *sock;
drivers/net/wireguard/socket.c static int send6(struct wg_device *wg, struct sk_buff *skb, · line 116
Quoted 5 lines
drivers/net/wireguard/socket.c +2
-0@@ -111,6 +112,9 @@ static int send6(struct wg_device *wg, struct sk_buff *skb,struct sock *sock;int ret =0 ; +if (wg->lowerdev) + fl.flowi6_oif = wg->lowerdev,
Hi Ben,
I think that the trailing ',' on the line above should be a ';'. As written, with a ',', the call to skb_mark_not_on_list() below will be included in the conditional block above.
And this doesn't seem to be the intention of the code based on indentation.
Flagged by clang-19 with -Wcomma
Quoted 4 lines
drivers/net/wireguard/socket.c +1
-0+skb_mark_not_on_list (skb); skb->dev = wg->dev; skb->mark = wg->fwmark;
...
Quoted 37 lines · Simon Horman
Quoted 10 lines · On Mon, Nov 25, 2024 at 01:21:11PM -0800, [email protected] wrote:
From: Ben Greear <[email protected]>
Which allows us to bind to VRF.
Signed-off-by Ben Greear <[email protected]>---
NOTE: Modified user-space to utilize this may be found here:
https://github.com/greearb/wireguard-tools-ct
Only the 'wg' part has been tested with this new feature as of today....
Quoted 15 lines
drivers/net/wireguard/socket.c +2
-1@@ -25,7 +25,8 @@ static int send4(struct wg_device *wg, struct sk_buff *skb, .daddr = endpoint->addr4.sin_addr.s_addr, .fl4_dport = endpoint->addr4.sin_port, .flowi4_mark = wg->fwmark, - .flowi4_proto = IPPROTO_UDP + .flowi4_proto = IPPROTO_UDP, + .flowi4_oif = wg->lowerdev, };struct rtable *rt =NULL ;struct sock *sock;drivers/net/wireguard/socket.c static int send6(struct wg_device *wg, struct sk_buff *skb, · line 116
Quoted 5 lines
drivers/net/wireguard/socket.c +2
-0@@ -111,6 +112,9 @@ static int send6(struct wg_device *wg, struct sk_buff *skb,struct sock *sock;int ret =0 ; +if (wg->lowerdev) + fl.flowi6_oif = wg->lowerdev,Hi Ben,
Quoted 6 lines
I think that the trailing ',' on the line above should be a ';'. As written, with a ',', the call to
skb_mark_not_on_list()below will be included in the conditional block above.
And this doesn't seem to be the intention of the code based on indentation.Flagged by clang-19 with -Wcomma
Thank you for noticing that, it was bad copy paste bug on my part. I'll submit a v2.
Thanks,
Ben
Signature · 2 lines
Ben Greear <[email protected]> Candela Technologies Inc http://www.candelatech.com