struct tun_file {
        struct tun_struct *tun;
+       struct net *net;
 };
 
 struct tun_struct {
 
        tfile->tun = tun;
        tun->tfile = tfile;
-       get_net(dev_net(tun->dev));
 
        return 0;
 }
        /* Detach from net device */
        tfile->tun = NULL;
        tun->tfile = NULL;
-       put_net(dev_net(tun->dev));
 
        /* Drop read queue */
        skb_queue_purge(&tun->readq);
 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;
                                (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)
        if (!tfile)
                return -ENOMEM;
        tfile->tun = NULL;
+       tfile->net = get_net(current->nsproxy->net_ns);
        file->private_data = tfile;
        return 0;
 }
                rtnl_unlock();
        }
 
+       put_net(tfile->net);
        kfree(tfile);
 
        return 0;