ath9k: serialize ath9k_ps_{wakeup,restore} calls
[safe/jmp/linux-2.6] / drivers / net / tun.c
index 95ae40a..a998b6a 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;
@@ -331,7 +330,7 @@ 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);
        }
@@ -398,13 +397,13 @@ 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);
-       return 0;
+       wake_up_interruptible(&tun->socket.wait);
+       return NETDEV_TX_OK;
 
 drop:
        dev->stats.tx_dropped++;
        kfree_skb(skb);
-       return 0;
+       return NETDEV_TX_OK;
 }
 
 static void tun_net_mclist(struct net_device *dev)
@@ -487,15 +486,17 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
 {
        struct tun_file *tfile = file->private_data;
        struct tun_struct *tun = __tun_get(tfile);
-       struct sock *sk = tun->sk;
+       struct sock *sk;
        unsigned int mask = 0;
 
        if (!tun)
                return POLLERR;
 
+       sk = tun->sk;
+
        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;
@@ -541,31 +542,38 @@ 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.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
+                   gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
+                       gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
+
                if (gso.hdr_len > len)
                        return -EINVAL;
+               offset += sizeof(gso);
        }
 
        if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
@@ -582,7 +590,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;
@@ -633,6 +641,9 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
                case VIRTIO_NET_HDR_GSO_TCPV6:
                        skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
                        break;
+               case VIRTIO_NET_HDR_GSO_UDP:
+                       skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
+                       break;
                default:
                        tun->dev->stats.rx_frame_errors++;
                        kfree_skb(skb);
@@ -674,7 +685,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);
@@ -684,7 +695,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;
@@ -698,7 +709,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);
        }
@@ -718,6 +729,8 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
                                gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
                        else if (sinfo->gso_type & SKB_GSO_TCPV6)
                                gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
+                       else if (sinfo->gso_type & SKB_GSO_UDP)
+                               gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
                        else
                                BUG();
                        if (sinfo->gso_type & SKB_GSO_TCP_ECN)
@@ -731,14 +744,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++;
@@ -768,7 +782,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;
 
@@ -793,13 +807,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);
@@ -841,12 +855,12 @@ static void tun_sock_write_space(struct sock *sk)
        if (!sock_writeable(sk))
                return;
 
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
-               wake_up_interruptible_sync(sk->sk_sleep);
-
        if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
                return;
 
+       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
+               wake_up_interruptible_sync(sk->sk_sleep);
+
        tun = container_of(sk, struct tun_sock, sk)->tun;
        kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
 }
@@ -862,16 +876,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)
@@ -925,10 +986,10 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                if (!sk)
                        goto err_free_dev;
 
+               init_waitqueue_head(&tun->socket.wait);
                sock_init_data(&tun->socket, sk);
                sk->sk_write_space = tun_sock_write_space;
                sk->sk_sndbuf = INT_MAX;
-               sk->sk_sleep = &tfile->read_wait;
 
                tun->sk = sk;
                container_of(sk, struct tun_sock, sk)->tun = tun;
@@ -941,11 +1002,15 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                                goto err_free_sk;
                }
 
-               err = -EINVAL;
                err = register_netdevice(tun->dev);
                if (err < 0)
                        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);
@@ -998,21 +1063,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;
@@ -1027,7 +1078,8 @@ static int set_offload(struct net_device *dev, unsigned long arg)
        old_features = dev->features;
        /* Unset features, set them as we chew on the arg. */
        features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
-                                   |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
+                                   |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6
+                                   |NETIF_F_UFO));
 
        if (arg & TUN_F_CSUM) {
                features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
@@ -1044,6 +1096,11 @@ static int set_offload(struct net_device *dev, unsigned long arg)
                                features |= NETIF_F_TSO6;
                        arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
                }
+
+               if (arg & TUN_F_UFO) {
+                       features |= NETIF_F_UFO;
+                       arg &= ~TUN_F_UFO;
+               }
        }
 
        /* This gives the user a way to test for new features in future by
@@ -1270,7 +1327,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;
 }
@@ -1278,20 +1334,23 @@ static int tun_chr_open(struct inode *inode, struct file * file)
 static int tun_chr_close(struct inode *inode, struct file *file)
 {
        struct tun_file *tfile = file->private_data;
-       struct tun_struct *tun = __tun_get(tfile);
-
+       struct tun_struct *tun;
 
+       tun = __tun_get(tfile);
        if (tun) {
-               DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
+               struct net_device *dev = tun->dev;
+
+               DBG(KERN_INFO "%s: tun_chr_close\n", dev->name);
 
-               rtnl_lock();
                __tun_detach(tun);
 
                /* If desireable, unregister the netdevice. */
-               if (!(tun->flags & TUN_PERSIST))
-                       unregister_netdevice(tun->dev);
-
-               rtnl_unlock();
+               if (!(tun->flags & TUN_PERSIST)) {
+                       rtnl_lock();
+                       if (dev->reg_state == NETREG_REGISTERED)
+                               unregister_netdevice(dev);
+                       rtnl_unlock();
+               }
        }
 
        tun = tfile->tun;
@@ -1321,6 +1380,7 @@ static const struct file_operations tun_fops = {
 static struct miscdevice tun_miscdev = {
        .minor = TUN_MINOR,
        .name = "tun",
+       .devnode = "net/tun",
        .fops = &tun_fops,
 };