All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device
@ 2019-03-20  9:16 Kirill Tkhai
  2019-03-20  9:16 ` [PATCH 2/2] tun: Remove unused first parameter of tun_get_iff() Kirill Tkhai
  2019-03-21 20:19 ` [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device David Miller
  0 siblings, 2 replies; 4+ messages in thread
From: Kirill Tkhai @ 2019-03-20  9:16 UTC (permalink / raw)
  To: netdev
  Cc: davem, harald.albrecht, jasowang, brouer, edumazet, mst,
	nicolas.dichtel, wangli39, ktkhai

In commit f2780d6d7475 "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 <harald.albrecht@gmx.net>
Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 drivers/net/tun.c           |    8 ++++++++
 include/uapi/linux/if_tun.h |    1 +
 2 files changed, 9 insertions(+)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index e9ca1c088d0b..b7137edff624 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -3103,6 +3103,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 
 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
 
+	net = dev_net(tun->dev);
 	ret = 0;
 	switch (cmd) {
 	case TUNGETIFF:
@@ -3328,6 +3329,13 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		ret = tun_net_change_carrier(tun->dev, (bool)carrier);
 		break;
 
+	case TUNGETDEVNETNS:
+		ret = -EPERM;
+		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
+			goto unlock;
+		ret = open_related_ns(&net->ns, get_net_ns);
+		break;
+
 	default:
 		ret = -EINVAL;
 		break;
diff --git a/include/uapi/linux/if_tun.h b/include/uapi/linux/if_tun.h
index 23a6753b37df..454ae31b93c7 100644
--- a/include/uapi/linux/if_tun.h
+++ b/include/uapi/linux/if_tun.h
@@ -60,6 +60,7 @@
 #define TUNSETSTEERINGEBPF _IOR('T', 224, int)
 #define TUNSETFILTEREBPF _IOR('T', 225, int)
 #define TUNSETCARRIER _IOW('T', 226, int)
+#define TUNGETDEVNETNS _IO('T', 227)
 
 /* TUNSETIFF ifr flags */
 #define IFF_TUN		0x0001


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] tun: Remove unused first parameter of tun_get_iff()
  2019-03-20  9:16 [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device Kirill Tkhai
@ 2019-03-20  9:16 ` Kirill Tkhai
  2019-03-21 20:19   ` David Miller
  2019-03-21 20:19 ` [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device David Miller
  1 sibling, 1 reply; 4+ messages in thread
From: Kirill Tkhai @ 2019-03-20  9:16 UTC (permalink / raw)
  To: netdev
  Cc: davem, harald.albrecht, jasowang, brouer, edumazet, mst,
	nicolas.dichtel, wangli39, ktkhai

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 drivers/net/tun.c |    7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index b7137edff624..b834b0d168f9 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -2873,8 +2873,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 	return err;
 }
 
-static void tun_get_iff(struct net *net, struct tun_struct *tun,
-		       struct ifreq *ifr)
+static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
 {
 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
 
@@ -3107,7 +3106,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 	ret = 0;
 	switch (cmd) {
 	case TUNGETIFF:
-		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
+		tun_get_iff(tun, &ifr);
 
 		if (tfile->detached)
 			ifr.ifr_flags |= IFF_DETACH_QUEUE;
@@ -3465,7 +3464,7 @@ static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
 	rtnl_lock();
 	tun = tun_get(tfile);
 	if (tun)
-		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
+		tun_get_iff(tun, &ifr);
 	rtnl_unlock();
 
 	if (tun)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device
  2019-03-20  9:16 [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device Kirill Tkhai
  2019-03-20  9:16 ` [PATCH 2/2] tun: Remove unused first parameter of tun_get_iff() Kirill Tkhai
@ 2019-03-21 20:19 ` David Miller
  1 sibling, 0 replies; 4+ messages in thread
From: David Miller @ 2019-03-21 20:19 UTC (permalink / raw)
  To: ktkhai
  Cc: netdev, harald.albrecht, jasowang, brouer, edumazet, mst,
	nicolas.dichtel, wangli39

From: Kirill Tkhai <ktkhai@virtuozzo.com>
Date: Wed, 20 Mar 2019 12:16:42 +0300

> In commit f2780d6d7475 "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 <harald.albrecht@gmx.net>
> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>

Applied.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] tun: Remove unused first parameter of tun_get_iff()
  2019-03-20  9:16 ` [PATCH 2/2] tun: Remove unused first parameter of tun_get_iff() Kirill Tkhai
@ 2019-03-21 20:19   ` David Miller
  0 siblings, 0 replies; 4+ messages in thread
From: David Miller @ 2019-03-21 20:19 UTC (permalink / raw)
  To: ktkhai
  Cc: netdev, harald.albrecht, jasowang, brouer, edumazet, mst,
	nicolas.dichtel, wangli39

From: Kirill Tkhai <ktkhai@virtuozzo.com>
Date: Wed, 20 Mar 2019 12:16:53 +0300

> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>

Applied.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-03-21 20:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-20  9:16 [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device Kirill Tkhai
2019-03-20  9:16 ` [PATCH 2/2] tun: Remove unused first parameter of tun_get_iff() Kirill Tkhai
2019-03-21 20:19   ` David Miller
2019-03-21 20:19 ` [PATCH 1/2] tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.