aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/tun.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/tun.c')
-rw-r--r--drivers/net/tun.c47
1 files changed, 25 insertions, 22 deletions
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 16716aef184c..735bf41c654a 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -93,7 +93,6 @@ struct tun_file {
93 atomic_t count; 93 atomic_t count;
94 struct tun_struct *tun; 94 struct tun_struct *tun;
95 struct net *net; 95 struct net *net;
96 wait_queue_head_t read_wait;
97}; 96};
98 97
99struct tun_sock; 98struct tun_sock;
@@ -156,6 +155,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
156 tfile->tun = tun; 155 tfile->tun = tun;
157 tun->tfile = tfile; 156 tun->tfile = tfile;
158 dev_hold(tun->dev); 157 dev_hold(tun->dev);
158 sock_hold(tun->sk);
159 atomic_inc(&tfile->count); 159 atomic_inc(&tfile->count);
160 160
161out: 161out:
@@ -165,11 +165,8 @@ out:
165 165
166static void __tun_detach(struct tun_struct *tun) 166static void __tun_detach(struct tun_struct *tun)
167{ 167{
168 struct tun_file *tfile = tun->tfile;
169
170 /* Detach from net device */ 168 /* Detach from net device */
171 netif_tx_lock_bh(tun->dev); 169 netif_tx_lock_bh(tun->dev);
172 tfile->tun = NULL;
173 tun->tfile = NULL; 170 tun->tfile = NULL;
174 netif_tx_unlock_bh(tun->dev); 171 netif_tx_unlock_bh(tun->dev);
175 172
@@ -333,12 +330,19 @@ static void tun_net_uninit(struct net_device *dev)
333 /* Inform the methods they need to stop using the dev. 330 /* Inform the methods they need to stop using the dev.
334 */ 331 */
335 if (tfile) { 332 if (tfile) {
336 wake_up_all(&tfile->read_wait); 333 wake_up_all(&tun->socket.wait);
337 if (atomic_dec_and_test(&tfile->count)) 334 if (atomic_dec_and_test(&tfile->count))
338 __tun_detach(tun); 335 __tun_detach(tun);
339 } 336 }
340} 337}
341 338
339static void tun_free_netdev(struct net_device *dev)
340{
341 struct tun_struct *tun = netdev_priv(dev);
342
343 sock_put(tun->sk);
344}
345
342/* Net device open. */ 346/* Net device open. */
343static int tun_net_open(struct net_device *dev) 347static int tun_net_open(struct net_device *dev)
344{ 348{
@@ -393,7 +397,7 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
393 /* Notify and wake up reader process */ 397 /* Notify and wake up reader process */
394 if (tun->flags & TUN_FASYNC) 398 if (tun->flags & TUN_FASYNC)
395 kill_fasync(&tun->fasync, SIGIO, POLL_IN); 399 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
396 wake_up_interruptible(&tun->tfile->read_wait); 400 wake_up_interruptible(&tun->socket.wait);
397 return 0; 401 return 0;
398 402
399drop: 403drop:
@@ -490,7 +494,7 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
490 494
491 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name); 495 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
492 496
493 poll_wait(file, &tfile->read_wait, wait); 497 poll_wait(file, &tun->socket.wait, wait);
494 498
495 if (!skb_queue_empty(&tun->readq)) 499 if (!skb_queue_empty(&tun->readq))
496 mask |= POLLIN | POLLRDNORM; 500 mask |= POLLIN | POLLRDNORM;
@@ -763,7 +767,7 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
763 goto out; 767 goto out;
764 } 768 }
765 769
766 add_wait_queue(&tfile->read_wait, &wait); 770 add_wait_queue(&tun->socket.wait, &wait);
767 while (len) { 771 while (len) {
768 current->state = TASK_INTERRUPTIBLE; 772 current->state = TASK_INTERRUPTIBLE;
769 773
@@ -794,7 +798,7 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
794 } 798 }
795 799
796 current->state = TASK_RUNNING; 800 current->state = TASK_RUNNING;
797 remove_wait_queue(&tfile->read_wait, &wait); 801 remove_wait_queue(&tun->socket.wait, &wait);
798 802
799out: 803out:
800 tun_put(tun); 804 tun_put(tun);
@@ -811,7 +815,7 @@ static void tun_setup(struct net_device *dev)
811 tun->group = -1; 815 tun->group = -1;
812 816
813 dev->ethtool_ops = &tun_ethtool_ops; 817 dev->ethtool_ops = &tun_ethtool_ops;
814 dev->destructor = free_netdev; 818 dev->destructor = tun_free_netdev;
815} 819}
816 820
817/* Trivial set of netlink ops to allow deleting tun or tap 821/* Trivial set of netlink ops to allow deleting tun or tap
@@ -848,7 +852,7 @@ static void tun_sock_write_space(struct sock *sk)
848 852
849static void tun_sock_destruct(struct sock *sk) 853static void tun_sock_destruct(struct sock *sk)
850{ 854{
851 dev_put(container_of(sk, struct tun_sock, sk)->tun->dev); 855 free_netdev(container_of(sk, struct tun_sock, sk)->tun->dev);
852} 856}
853 857
854static struct proto tun_proto = { 858static struct proto tun_proto = {
@@ -862,7 +866,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
862 struct sock *sk; 866 struct sock *sk;
863 struct tun_struct *tun; 867 struct tun_struct *tun;
864 struct net_device *dev; 868 struct net_device *dev;
865 struct tun_file *tfile = file->private_data;
866 int err; 869 int err;
867 870
868 dev = __dev_get_by_name(net, ifr->ifr_name); 871 dev = __dev_get_by_name(net, ifr->ifr_name);
@@ -920,13 +923,10 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
920 if (!sk) 923 if (!sk)
921 goto err_free_dev; 924 goto err_free_dev;
922 925
923 /* This ref count is for tun->sk. */ 926 init_waitqueue_head(&tun->socket.wait);
924 dev_hold(dev);
925 sock_init_data(&tun->socket, sk); 927 sock_init_data(&tun->socket, sk);
926 sk->sk_write_space = tun_sock_write_space; 928 sk->sk_write_space = tun_sock_write_space;
927 sk->sk_destruct = tun_sock_destruct;
928 sk->sk_sndbuf = INT_MAX; 929 sk->sk_sndbuf = INT_MAX;
929 sk->sk_sleep = &tfile->read_wait;
930 930
931 tun->sk = sk; 931 tun->sk = sk;
932 container_of(sk, struct tun_sock, sk)->tun = tun; 932 container_of(sk, struct tun_sock, sk)->tun = tun;
@@ -942,11 +942,13 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
942 err = -EINVAL; 942 err = -EINVAL;
943 err = register_netdevice(tun->dev); 943 err = register_netdevice(tun->dev);
944 if (err < 0) 944 if (err < 0)
945 goto err_free_dev; 945 goto err_free_sk;
946
947 sk->sk_destruct = tun_sock_destruct;
946 948
947 err = tun_attach(tun, file); 949 err = tun_attach(tun, file);
948 if (err < 0) 950 if (err < 0)
949 goto err_free_dev; 951 goto failed;
950 } 952 }
951 953
952 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name); 954 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
@@ -1266,7 +1268,6 @@ static int tun_chr_open(struct inode *inode, struct file * file)
1266 atomic_set(&tfile->count, 0); 1268 atomic_set(&tfile->count, 0);
1267 tfile->tun = NULL; 1269 tfile->tun = NULL;
1268 tfile->net = get_net(current->nsproxy->net_ns); 1270 tfile->net = get_net(current->nsproxy->net_ns);
1269 init_waitqueue_head(&tfile->read_wait);
1270 file->private_data = tfile; 1271 file->private_data = tfile;
1271 return 0; 1272 return 0;
1272} 1273}
@@ -1284,14 +1285,16 @@ static int tun_chr_close(struct inode *inode, struct file *file)
1284 __tun_detach(tun); 1285 __tun_detach(tun);
1285 1286
1286 /* If desireable, unregister the netdevice. */ 1287 /* If desireable, unregister the netdevice. */
1287 if (!(tun->flags & TUN_PERSIST)) { 1288 if (!(tun->flags & TUN_PERSIST))
1288 sock_put(tun->sk);
1289 unregister_netdevice(tun->dev); 1289 unregister_netdevice(tun->dev);
1290 }
1291 1290
1292 rtnl_unlock(); 1291 rtnl_unlock();
1293 } 1292 }
1294 1293
1294 tun = tfile->tun;
1295 if (tun)
1296 sock_put(tun->sk);
1297
1295 put_net(tfile->net); 1298 put_net(tfile->net);
1296 kfree(tfile); 1299 kfree(tfile);
1297 1300