public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: ebiederm@xmission•com (Eric W. Biederman)
To: David Miller <davem@davemloft•net>
Cc: <netdev@vger•kernel.org>, Max Krasnyansky <maxk@qualcomm•com>,
	Pavel Emelyanov <xemul@openvz•org>
Subject: [PATCH 05/10] tun: Grab the netns in open.
Date: Tue, 20 Jan 2009 13:01:48 -0800	[thread overview]
Message-ID: <m17i4plmir.fsf_-_@fess.ebiederm.org> (raw)
In-Reply-To: <m1bpu1lmkn.fsf_-_@fess.ebiederm.org> (Eric W. Biederman's message of "Tue\, 20 Jan 2009 13\:00\:40 -0800")


Grabbing namespaces in open, and putting them in close always seems to
be the cleanest approach with the fewest surprises.

So now that we have tun_file so we have somepleace to put the network
namespace, let's grab the network namespace on file open and put on
file close.

Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks•com>
---
 drivers/net/tun.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index d3a665d..dfbf586 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -89,6 +89,7 @@ struct tap_filter {
 
 struct tun_file {
 	struct tun_struct *tun;
+	struct net *net;
 };
 
 struct tun_struct {
@@ -131,7 +132,6 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
 
 	tfile->tun = tun;
 	tun->tfile = tfile;
-	get_net(dev_net(tun->dev));
 
 	return 0;
 }
@@ -143,7 +143,6 @@ static void __tun_detach(struct tun_struct *tun)
 	/* Detach from net device */
 	tfile->tun = NULL;
 	tun->tfile = NULL;
-	put_net(dev_net(tun->dev));
 
 	/* Drop read queue */
 	skb_queue_purge(&tun->readq);
@@ -936,6 +935,7 @@ static int set_offload(struct net_device *dev, unsigned long arg)
 static int tun_chr_ioctl(struct inode *inode, struct file *file,
 			 unsigned int cmd, unsigned long arg)
 {
+	struct tun_file *tfile = file->private_data;
 	struct tun_struct *tun;
 	void __user* argp = (void __user*)arg;
 	struct ifreq ifr;
@@ -954,14 +954,14 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
 				(unsigned int __user*)argp);
 	}
 
-	tun = tun_get(file);
+	tun = __tun_get(tfile);
 	if (cmd == TUNSETIFF && !tun) {
 		int err;
 
 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
 
 		rtnl_lock();
-		err = tun_set_iff(current->nsproxy->net_ns, file, &ifr);
+		err = tun_set_iff(tfile->net, file, &ifr);
 		rtnl_unlock();
 
 		if (err)
@@ -1125,6 +1125,7 @@ static int tun_chr_open(struct inode *inode, struct file * file)
 	if (!tfile)
 		return -ENOMEM;
 	tfile->tun = NULL;
+	tfile->net = get_net(current->nsproxy->net_ns);
 	file->private_data = tfile;
 	return 0;
 }
@@ -1148,6 +1149,7 @@ static int tun_chr_close(struct inode *inode, struct file *file)
 		rtnl_unlock();
 	}
 
+	put_net(tfile->net);
 	kfree(tfile);
 
 	return 0;
-- 
1.5.6.3

  reply	other threads:[~2009-01-21  2:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-20 20:53 [PATCH 0/10] Tun fixes and netns migration Eric W. Biederman
2009-01-20 20:56 ` [PATCH 01/10] tun: Remove unnecessary tun_get_by_name Eric W. Biederman
2009-01-20 20:57   ` [PATCH 02/10] tun: Fix races in tun_set_iff Eric W. Biederman
2009-01-20 20:59     ` [PATCH 03/10] tun: Use POLLERR not EBADF in tun_chr_poll Eric W. Biederman
2009-01-20 21:00       ` [PATCH 04/10] tun: Introduce tun_file Eric W. Biederman
2009-01-20 21:01         ` Eric W. Biederman [this message]
2009-01-20 21:02           ` [PATCH 06/10] tun: Make tun_net_xmit atomic wrt tun_attach && tun_detach Eric W. Biederman
2009-01-20 21:03             ` [PATCH 07/10] tun: Move read_wait into tun_file Eric W. Biederman
2009-01-20 21:07               ` [PATCH 08/10] tun: Fix races between tun_net_close and free_netdev Eric W. Biederman
2009-01-20 21:08                 ` [PATCH 09/10] tun: There is no longer any need to deny changing network namespaces Eric W. Biederman
2009-01-22  0:03 ` [PATCH 0/10] Tun fixes and netns migration David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m17i4plmir.fsf_-_@fess.ebiederm.org \
    --to=ebiederm@xmission$(echo .)com \
    --cc=davem@davemloft$(echo .)net \
    --cc=maxk@qualcomm$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=xemul@openvz$(echo .)org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox