tun: add IFF_TUN_EXCL flag to avoid opening a persistent device.
[safe/jmp/linux-2.6] / drivers / net / tun.c
index 0476549..94622e5 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);
 
@@ -260,10 +257,16 @@ static int update_filter(struct tap_filter *filter, void __user *arg)
 
        nexact = n;
 
-       /* The rest is hashed */
+       /* Remaining multicast addresses are hashed,
+        * unicast will leave the filter disabled. */
        memset(filter->mask, 0, sizeof(filter->mask));
-       for (; n < uf.count; n++)
+       for (; n < uf.count; n++) {
+               if (!is_multicast_ether_addr(addr[n].u)) {
+                       err = 0; /* no filter */
+                       goto done;
+               }
                addr_hash_set(filter->mask, addr[n].u);
+       }
 
        /* For ALLMULTI just set the mask to all ones.
         * This overrides the mask populated above. */
@@ -327,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)
 {
@@ -387,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:
@@ -484,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;
@@ -512,7 +522,7 @@ static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
        int err;
 
        /* Under a page?  Don't bother with paged skb. */
-       if (prepad + len < PAGE_SIZE)
+       if (prepad + len < PAGE_SIZE || !linear)
                linear = len;
 
        skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
@@ -530,36 +540,40 @@ 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) {
                align = NET_IP_ALIGN;
-               if (unlikely(len < ETH_HLEN))
+               if (unlikely(len < ETH_HLEN ||
+                            (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
                        return -EINVAL;
        }
 
@@ -570,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;
@@ -654,7 +668,7 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
                              unsigned long count, loff_t pos)
 {
        struct file *file = iocb->ki_filp;
-       struct tun_struct *tun = file->private_data;
+       struct tun_struct *tun = tun_get(file);
        ssize_t result;
 
        if (!tun)
@@ -662,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);
@@ -672,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;
@@ -686,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);
        }
@@ -719,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++;
@@ -756,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;
 
@@ -781,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);
@@ -804,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
@@ -841,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 = {
@@ -855,11 +870,12 @@ 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)
@@ -913,13 +929,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;
@@ -935,11 +948,13 @@ 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;
+
+               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);
@@ -1259,7 +1274,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;
 }
@@ -1277,14 +1291,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);