Skip to content

Commit 0c3e0e3

Browse files
Kirill Tkhaidavem330
authored andcommitted
tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device
In commit f2780d6 "tun: Add ioctl() SIOCGSKNS cmd to allow obtaining net ns of tun device" it was missed that tun may change its net ns, while net ns of socket remains the same as it was created initially. SIOCGSKNS returns net ns of socket, so it is not suitable for obtaining net ns of device. We may have two tun devices with the same names in two net ns, and in this case it's not possible to determ, which of them fd refers to (TUNGETIFF will return the same name). This patch adds new ioctl() cmd for obtaining net ns of a device. Reported-by: Harald Albrecht <[email protected]> Signed-off-by: Kirill Tkhai <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 28b18b3 commit 0c3e0e3

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

drivers/net/tun.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,6 +3102,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
31023102

31033103
tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
31043104

3105+
net = dev_net(tun->dev);
31053106
ret = 0;
31063107
switch (cmd) {
31073108
case TUNGETIFF:
@@ -3327,6 +3328,13 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
33273328
ret = tun_net_change_carrier(tun->dev, (bool)carrier);
33283329
break;
33293330

3331+
case TUNGETDEVNETNS:
3332+
ret = -EPERM;
3333+
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3334+
goto unlock;
3335+
ret = open_related_ns(&net->ns, get_net_ns);
3336+
break;
3337+
33303338
default:
33313339
ret = -EINVAL;
33323340
break;

include/uapi/linux/if_tun.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define TUNSETSTEERINGEBPF _IOR('T', 224, int)
6161
#define TUNSETFILTEREBPF _IOR('T', 225, int)
6262
#define TUNSETCARRIER _IOW('T', 226, int)
63+
#define TUNGETDEVNETNS _IO('T', 227)
6364

6465
/* TUNSETIFF ifr flags */
6566
#define IFF_TUN 0x0001

0 commit comments

Comments
 (0)