Skip to content

Commit da5095d

Browse files
Alexis Bauvindavem330
authored andcommitted
udp_tunnel: add config option to bind to a device
UDP tunnel sockets are always opened unbound to a specific device. This patch allow the socket to be bound on a custom device, which incidentally makes UDP tunnels VRF-aware if binding to an l3mdev. Signed-off-by: Alexis Bauvin <abauvin@scaleway.com> Reviewed-by: Amine Kherbouche <akherbouche@scaleway.com> Tested-by: Amine Kherbouche <akherbouche@scaleway.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e3dd762 commit da5095d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

include/net/udp_tunnel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct udp_port_cfg {
3030

3131
__be16 local_udp_port;
3232
__be16 peer_udp_port;
33+
int bind_ifindex;
3334
unsigned int use_udp_checksums:1,
3435
use_udp6_tx_checksums:1,
3536
use_udp6_rx_checksums:1,

net/ipv4/udp_tunnel.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
2020
if (err < 0)
2121
goto error;
2222

23+
if (cfg->bind_ifindex) {
24+
struct net_device *dev;
25+
26+
dev = dev_get_by_index(net, cfg->bind_ifindex);
27+
if (!dev) {
28+
err = -ENODEV;
29+
goto error;
30+
}
31+
32+
err = kernel_setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
33+
dev->name, strlen(dev->name) + 1);
34+
dev_put(dev);
35+
36+
if (err < 0)
37+
goto error;
38+
}
39+
2340
udp_addr.sin_family = AF_INET;
2441
udp_addr.sin_addr = cfg->local_ip;
2542
udp_addr.sin_port = cfg->local_udp_port;

net/ipv6/ip6_udp_tunnel.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
3131
if (err < 0)
3232
goto error;
3333
}
34+
if (cfg->bind_ifindex) {
35+
struct net_device *dev;
36+
37+
dev = dev_get_by_index(net, cfg->bind_ifindex);
38+
if (!dev) {
39+
err = -ENODEV;
40+
goto error;
41+
}
42+
43+
err = kernel_setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
44+
dev->name, strlen(dev->name) + 1);
45+
dev_put(dev);
46+
47+
if (err < 0)
48+
goto error;
49+
}
3450

3551
udp6_addr.sin6_family = AF_INET6;
3652
memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,

0 commit comments

Comments
 (0)