Merge branch 'net-next' of git://git.kernel.org/pub/scm/linux/kernel/git/vxy/lksctp-dev
[deliverable/linux.git] / drivers / net / tun.c
index 16716aef184c993e5138deeaf84f968ed06212eb..4cda69b6b28cf1ffeb286066eea906409609df24 100644 (file)
@@ -93,7 +93,6 @@ struct tun_file {
        atomic_t count;
        struct tun_struct *tun;
        struct net *net;
-       wait_queue_head_t       read_wait;
 };
 
 struct tun_sock;
@@ -156,6 +155,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
        tfile->tun = tun;
        tun->tfile = tfile;
        dev_hold(tun->dev);
+       sock_hold(tun->sk);
        atomic_inc(&tfile->count);
 
 out:
@@ -165,11 +165,8 @@ out:
 
 static void __tun_detach(struct tun_struct *tun)
 {
-       struct tun_file *tfile = tun->tfile;
-
        /* Detach from net device */
        netif_tx_lock_bh(tun->dev);
-       tfile->tun = NULL;
        tun->tfile = NULL;
        netif_tx_unlock_bh(tun->dev);
 
@@ -333,12 +330,19 @@ static void tun_net_uninit(struct net_device *dev)
        /* Inform the methods they need to stop using the dev.
         */
        if (tfile) {
-               wake_up_all(&tfile->read_wait);
+               wake_up_all(&tun->socket.wait);
                if (atomic_dec_and_test(&tfile->count))
                        __tun_detach(tun);
        }
 }
 
+static void tun_free_netdev(struct net_device *dev)
+{
+       struct tun_struct *tun = netdev_priv(dev);
+
+       sock_put(tun->sk);
+}
+
 /* Net device open. */
 static int tun_net_open(struct net_device *dev)
 {
@@ -393,7 +397,7 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
        /* Notify and wake up reader process */
        if (tun->flags & TUN_FASYNC)
                kill_fasync(&tun->fasync, SIGIO, POLL_IN);
-       wake_up_interruptible(&tun->tfile->read_wait);
+       wake_up_interruptible(&tun->socket.wait);
        return 0;
 
 drop:
@@ -490,7 +494,7 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
 
        DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
 
-       poll_wait(file, &tfile->read_wait, wait);
+       poll_wait(file, &tun->socket.wait, wait);
 
        if (!skb_queue_empty(&tun->readq))
                mask |= POLLIN | POLLRDNORM;
@@ -536,31 +540,34 @@ static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
 
 /* Get packet from user space buffer */
 static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
-                                      struct iovec *iv, size_t count,
+                                      const struct iovec *iv, size_t count,
                                       int noblock)
 {
        struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
        struct sk_buff *skb;
        size_t len = count, align = 0;
        struct virtio_net_hdr gso = { 0 };
+       int offset = 0;
 
        if (!(tun->flags & TUN_NO_PI)) {
                if ((len -= sizeof(pi)) > count)
                        return -EINVAL;
 
-               if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
+               if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
                        return -EFAULT;
+               offset += sizeof(pi);
        }
 
        if (tun->flags & TUN_VNET_HDR) {
                if ((len -= sizeof(gso)) > count)
                        return -EINVAL;
 
-               if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
+               if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
                        return -EFAULT;
 
                if (gso.hdr_len > len)
                        return -EINVAL;
+               offset += sizeof(pi);
        }
 
        if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
@@ -577,7 +584,7 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
                return PTR_ERR(skb);
        }
 
-       if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
+       if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
                tun->dev->stats.rx_dropped++;
                kfree_skb(skb);
                return -EFAULT;
@@ -669,7 +676,7 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
 
        DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
 
-       result = tun_get_user(tun, (struct iovec *)iv, iov_length(iv, count),
+       result = tun_get_user(tun, iv, iov_length(iv, count),
                              file->f_flags & O_NONBLOCK);
 
        tun_put(tun);
@@ -679,7 +686,7 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
 /* Put packet to the user space buffer */
 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
                                       struct sk_buff *skb,
-                                      struct iovec *iv, int len)
+                                      const struct iovec *iv, int len)
 {
        struct tun_pi pi = { 0, skb->protocol };
        ssize_t total = 0;
@@ -693,7 +700,7 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
                        pi.flags |= TUN_PKT_STRIP;
                }
 
-               if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
+               if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
                        return -EFAULT;
                total += sizeof(pi);
        }
@@ -726,14 +733,15 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
                        gso.csum_offset = skb->csum_offset;
                } /* else everything is zero */
 
-               if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
+               if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
+                                              sizeof(gso))))
                        return -EFAULT;
                total += sizeof(gso);
        }
 
        len = min_t(int, skb->len, len);
 
-       skb_copy_datagram_iovec(skb, 0, iv, len);
+       skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
        total += len;
 
        tun->dev->stats.tx_packets++;
@@ -763,7 +771,7 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
                goto out;
        }
 
-       add_wait_queue(&tfile->read_wait, &wait);
+       add_wait_queue(&tun->socket.wait, &wait);
        while (len) {
                current->state = TASK_INTERRUPTIBLE;
 
@@ -788,13 +796,13 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
                }
                netif_wake_queue(tun->dev);
 
-               ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
+               ret = tun_put_user(tun, skb, iv, len);
                kfree_skb(skb);
                break;
        }
 
        current->state = TASK_RUNNING;
-       remove_wait_queue(&tfile->read_wait, &wait);
+       remove_wait_queue(&tun->socket.wait, &wait);
 
 out:
        tun_put(tun);
@@ -811,7 +819,7 @@ static void tun_setup(struct net_device *dev)
        tun->group = -1;
 
        dev->ethtool_ops = &tun_ethtool_ops;
-       dev->destructor = free_netdev;
+       dev->destructor = tun_free_netdev;
 }
 
 /* Trivial set of netlink ops to allow deleting tun or tap
@@ -848,7 +856,7 @@ static void tun_sock_write_space(struct sock *sk)
 
 static void tun_sock_destruct(struct sock *sk)
 {
-       dev_put(container_of(sk, struct tun_sock, sk)->tun->dev);
+       free_netdev(container_of(sk, struct tun_sock, sk)->tun->dev);
 }
 
 static struct proto tun_proto = {
@@ -857,16 +865,63 @@ static struct proto tun_proto = {
        .obj_size       = sizeof(struct tun_sock),
 };
 
+static int tun_flags(struct tun_struct *tun)
+{
+       int flags = 0;
+
+       if (tun->flags & TUN_TUN_DEV)
+               flags |= IFF_TUN;
+       else
+               flags |= IFF_TAP;
+
+       if (tun->flags & TUN_NO_PI)
+               flags |= IFF_NO_PI;
+
+       if (tun->flags & TUN_ONE_QUEUE)
+               flags |= IFF_ONE_QUEUE;
+
+       if (tun->flags & TUN_VNET_HDR)
+               flags |= IFF_VNET_HDR;
+
+       return flags;
+}
+
+static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
+                             char *buf)
+{
+       struct tun_struct *tun = netdev_priv(to_net_dev(dev));
+       return sprintf(buf, "0x%x\n", tun_flags(tun));
+}
+
+static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
+                             char *buf)
+{
+       struct tun_struct *tun = netdev_priv(to_net_dev(dev));
+       return sprintf(buf, "%d\n", tun->owner);
+}
+
+static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
+                             char *buf)
+{
+       struct tun_struct *tun = netdev_priv(to_net_dev(dev));
+       return sprintf(buf, "%d\n", tun->group);
+}
+
+static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
+static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
+static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
+
 static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 {
        struct sock *sk;
        struct tun_struct *tun;
        struct net_device *dev;
-       struct tun_file *tfile = file->private_data;
        int err;
 
        dev = __dev_get_by_name(net, ifr->ifr_name);
        if (dev) {
+               if (ifr->ifr_flags & IFF_TUN_EXCL)
+                       return -EBUSY;
                if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
                        tun = netdev_priv(dev);
                else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
@@ -920,13 +975,10 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                if (!sk)
                        goto err_free_dev;
 
-               /* This ref count is for tun->sk. */
-               dev_hold(dev);
+               init_waitqueue_head(&tun->socket.wait);
                sock_init_data(&tun->socket, sk);
                sk->sk_write_space = tun_sock_write_space;
-               sk->sk_destruct = tun_sock_destruct;
                sk->sk_sndbuf = INT_MAX;
-               sk->sk_sleep = &tfile->read_wait;
 
                tun->sk = sk;
                container_of(sk, struct tun_sock, sk)->tun = tun;
@@ -942,11 +994,18 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                err = -EINVAL;
                err = register_netdevice(tun->dev);
                if (err < 0)
-                       goto err_free_dev;
+                       goto err_free_sk;
+
+               if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
+                   device_create_file(&tun->dev->dev, &dev_attr_owner) ||
+                   device_create_file(&tun->dev->dev, &dev_attr_group))
+                       printk(KERN_ERR "Failed to create tun sysfs files\n");
+
+               sk->sk_destruct = tun_sock_destruct;
 
                err = tun_attach(tun, file);
                if (err < 0)
-                       goto err_free_dev;
+                       goto failed;
        }
 
        DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
@@ -994,21 +1053,7 @@ static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
        strcpy(ifr->ifr_name, tun->dev->name);
 
-       ifr->ifr_flags = 0;
-
-       if (ifr->ifr_flags & TUN_TUN_DEV)
-               ifr->ifr_flags |= IFF_TUN;
-       else
-               ifr->ifr_flags |= IFF_TAP;
-
-       if (tun->flags & TUN_NO_PI)
-               ifr->ifr_flags |= IFF_NO_PI;
-
-       if (tun->flags & TUN_ONE_QUEUE)
-               ifr->ifr_flags |= IFF_ONE_QUEUE;
-
-       if (tun->flags & TUN_VNET_HDR)
-               ifr->ifr_flags |= IFF_VNET_HDR;
+       ifr->ifr_flags = tun_flags(tun);
 
        tun_put(tun);
        return 0;
@@ -1266,7 +1311,6 @@ static int tun_chr_open(struct inode *inode, struct file * file)
        atomic_set(&tfile->count, 0);
        tfile->tun = NULL;
        tfile->net = get_net(current->nsproxy->net_ns);
-       init_waitqueue_head(&tfile->read_wait);
        file->private_data = tfile;
        return 0;
 }
@@ -1284,14 +1328,16 @@ static int tun_chr_close(struct inode *inode, struct file *file)
                __tun_detach(tun);
 
                /* If desireable, unregister the netdevice. */
-               if (!(tun->flags & TUN_PERSIST)) {
-                       sock_put(tun->sk);
+               if (!(tun->flags & TUN_PERSIST))
                        unregister_netdevice(tun->dev);
-               }
 
                rtnl_unlock();
        }
 
+       tun = tfile->tun;
+       if (tun)
+               sock_put(tun->sk);
+
        put_net(tfile->net);
        kfree(tfile);
 
This page took 0.031177 seconds and 5 git commands to generate.