ipmi: fix statistics counting issues
[safe/jmp/linux-2.6] / drivers / net / tun.c
index 97b0500..16716ae 100644 (file)
@@ -63,6 +63,8 @@
 #include <linux/virtio_net.h>
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
+#include <net/rtnetlink.h>
+#include <net/sock.h>
 
 #include <asm/system.h>
 #include <asm/uaccess.h>
@@ -94,6 +96,8 @@ struct tun_file {
        wait_queue_head_t       read_wait;
 };
 
+struct tun_sock;
+
 struct tun_struct {
        struct tun_file         *tfile;
        unsigned int            flags;
@@ -106,12 +110,24 @@ struct tun_struct {
        struct fasync_struct    *fasync;
 
        struct tap_filter       txflt;
+       struct sock             *sk;
+       struct socket           socket;
 
 #ifdef TUN_DEBUG
        int debug;
 #endif
 };
 
+struct tun_sock {
+       struct sock             sk;
+       struct tun_struct       *tun;
+};
+
+static inline struct tun_sock *tun_sk(struct sock *sk)
+{
+       return container_of(sk, struct tun_sock, sk);
+}
+
 static int tun_attach(struct tun_struct *tun, struct file *file)
 {
        struct tun_file *tfile = file->private_data;
@@ -122,7 +138,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
 
        /* Check permissions */
        if (((tun->owner != -1 && cred->euid != tun->owner) ||
-            (tun->group != -1 && cred->egid != tun->group)) &&
+            (tun->group != -1 && !in_egroup_p(tun->group))) &&
                !capable(CAP_NET_ADMIN))
                return -EPERM;
 
@@ -244,10 +260,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. */
@@ -460,7 +482,8 @@ 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);
-       unsigned int mask = POLLOUT | POLLWRNORM;
+       struct sock *sk = tun->sk;
+       unsigned int mask = 0;
 
        if (!tun)
                return POLLERR;
@@ -472,6 +495,11 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
        if (!skb_queue_empty(&tun->readq))
                mask |= POLLIN | POLLRDNORM;
 
+       if (sock_writeable(sk) ||
+           (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
+            sock_writeable(sk)))
+               mask |= POLLOUT | POLLWRNORM;
+
        if (tun->dev->reg_state != NETREG_REGISTERED)
                mask = POLLERR;
 
@@ -481,68 +509,37 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
 
 /* prepad is the amount to reserve at front.  len is length after that.
  * linear is a hint as to how much to copy (usually headers). */
-static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear,
-                                    gfp_t gfp)
+static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
+                                           size_t prepad, size_t len,
+                                           size_t linear, int noblock)
 {
+       struct sock *sk = tun->sk;
        struct sk_buff *skb;
-       unsigned int i;
-
-       skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
-       if (skb) {
-               skb_reserve(skb, prepad);
-               skb_put(skb, len);
-               return skb;
-       }
+       int err;
 
        /* Under a page?  Don't bother with paged skb. */
-       if (prepad + len < PAGE_SIZE)
-               return NULL;
+       if (prepad + len < PAGE_SIZE || !linear)
+               linear = len;
 
-       /* Start with a normal skb, and add pages. */
-       skb = alloc_skb(prepad + linear, gfp);
+       skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
+                                  &err);
        if (!skb)
-               return NULL;
+               return ERR_PTR(err);
 
        skb_reserve(skb, prepad);
        skb_put(skb, linear);
-
-       len -= linear;
-
-       for (i = 0; i < MAX_SKB_FRAGS; i++) {
-               skb_frag_t *f = &skb_shinfo(skb)->frags[i];
-
-               f->page = alloc_page(gfp|__GFP_ZERO);
-               if (!f->page)
-                       break;
-
-               f->page_offset = 0;
-               f->size = PAGE_SIZE;
-
-               skb->data_len += PAGE_SIZE;
-               skb->len += PAGE_SIZE;
-               skb->truesize += PAGE_SIZE;
-               skb_shinfo(skb)->nr_frags++;
-
-               if (len < PAGE_SIZE) {
-                       len = 0;
-                       break;
-               }
-               len -= PAGE_SIZE;
-       }
-
-       /* Too large, or alloc fail? */
-       if (unlikely(len)) {
-               kfree_skb(skb);
-               skb = NULL;
-       }
+       skb->data_len = len - linear;
+       skb->len += len - linear;
 
        return skb;
 }
 
 /* Get packet from user space buffer */
-static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
+static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
+                                      struct iovec *iv, size_t count,
+                                      int noblock)
 {
-       struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
+       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 };
@@ -568,13 +565,16 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv,
 
        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;
        }
 
-       if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
-               tun->dev->stats.rx_dropped++;
-               return -ENOMEM;
+       skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
+       if (IS_ERR(skb)) {
+               if (PTR_ERR(skb) != -EAGAIN)
+                       tun->dev->stats.rx_dropped++;
+               return PTR_ERR(skb);
        }
 
        if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
@@ -660,7 +660,8 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv,
 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
                              unsigned long count, loff_t pos)
 {
-       struct tun_struct *tun = tun_get(iocb->ki_filp);
+       struct file *file = iocb->ki_filp;
+       struct tun_struct *tun = tun_get(file);
        ssize_t result;
 
        if (!tun)
@@ -668,7 +669,8 @@ 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, (struct iovec *)iv, iov_length(iv, count),
+                             file->f_flags & O_NONBLOCK);
 
        tun_put(tun);
        return result;
@@ -812,10 +814,55 @@ static void tun_setup(struct net_device *dev)
        dev->destructor = free_netdev;
 }
 
+/* Trivial set of netlink ops to allow deleting tun or tap
+ * device with netlink.
+ */
+static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
+{
+       return -EINVAL;
+}
+
+static struct rtnl_link_ops tun_link_ops __read_mostly = {
+       .kind           = DRV_NAME,
+       .priv_size      = sizeof(struct tun_struct),
+       .setup          = tun_setup,
+       .validate       = tun_validate,
+};
+
+static void tun_sock_write_space(struct sock *sk)
+{
+       struct tun_struct *tun;
+
+       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;
+
+       tun = container_of(sk, struct tun_sock, sk)->tun;
+       kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
+}
+
+static void tun_sock_destruct(struct sock *sk)
+{
+       dev_put(container_of(sk, struct tun_sock, sk)->tun->dev);
+}
+
+static struct proto tun_proto = {
+       .name           = "tun",
+       .owner          = THIS_MODULE,
+       .obj_size       = sizeof(struct tun_sock),
+};
+
 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);
@@ -861,20 +908,38 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                        return -ENOMEM;
 
                dev_net_set(dev, net);
+               dev->rtnl_link_ops = &tun_link_ops;
 
                tun = netdev_priv(dev);
                tun->dev = dev;
                tun->flags = flags;
                tun->txflt.count = 0;
 
+               err = -ENOMEM;
+               sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
+               if (!sk)
+                       goto err_free_dev;
+
+               /* This ref count is for tun->sk. */
+               dev_hold(dev);
+               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;
+
                tun_net_init(dev);
 
                if (strchr(dev->name, '%')) {
                        err = dev_alloc_name(dev, dev->name);
                        if (err < 0)
-                               goto err_free_dev;
+                               goto err_free_sk;
                }
 
+               err = -EINVAL;
                err = register_netdevice(tun->dev);
                if (err < 0)
                        goto err_free_dev;
@@ -910,6 +975,8 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
        strcpy(ifr->ifr_name, tun->dev->name);
        return 0;
 
+ err_free_sk:
+       sock_put(sk);
  err_free_dev:
        free_netdev(dev);
  failed:
@@ -994,6 +1061,7 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
        struct tun_struct *tun;
        void __user* argp = (void __user*)arg;
        struct ifreq ifr;
+       int sndbuf;
        int ret;
 
        if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
@@ -1133,6 +1201,22 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
                ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
                rtnl_unlock();
                break;
+
+       case TUNGETSNDBUF:
+               sndbuf = tun->sk->sk_sndbuf;
+               if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
+                       ret = -EFAULT;
+               break;
+
+       case TUNSETSNDBUF:
+               if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
+                       ret = -EFAULT;
+                       break;
+               }
+
+               tun->sk->sk_sndbuf = sndbuf;
+               break;
+
        default:
                ret = -EINVAL;
                break;
@@ -1200,8 +1284,10 @@ static int tun_chr_close(struct inode *inode, struct file *file)
                __tun_detach(tun);
 
                /* If desireable, unregister the netdevice. */
-               if (!(tun->flags & TUN_PERSIST))
+               if (!(tun->flags & TUN_PERSIST)) {
+                       sock_put(tun->sk);
                        unregister_netdevice(tun->dev);
+               }
 
                rtnl_unlock();
        }
@@ -1317,29 +1403,6 @@ static const struct ethtool_ops tun_ethtool_ops = {
        .set_rx_csum    = tun_set_rx_csum
 };
 
-static int tun_init_net(struct net *net)
-{
-       return 0;
-}
-
-static void tun_exit_net(struct net *net)
-{
-       struct net_device *dev, *next;
-
-       rtnl_lock();
-       for_each_netdev_safe(net, dev, next) {
-               if (dev->ethtool_ops != &tun_ethtool_ops)
-                       continue;
-               DBG(KERN_INFO "%s cleaned up\n", dev->name);
-               unregister_netdevice(dev);
-       }
-       rtnl_unlock();
-}
-
-static struct pernet_operations tun_net_ops = {
-       .init = tun_init_net,
-       .exit = tun_exit_net,
-};
 
 static int __init tun_init(void)
 {
@@ -1348,10 +1411,10 @@ static int __init tun_init(void)
        printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
        printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
 
-       ret = register_pernet_device(&tun_net_ops);
+       ret = rtnl_link_register(&tun_link_ops);
        if (ret) {
-               printk(KERN_ERR "tun: Can't register pernet ops\n");
-               goto err_pernet;
+               printk(KERN_ERR "tun: Can't register link_ops\n");
+               goto err_linkops;
        }
 
        ret = misc_register(&tun_miscdev);
@@ -1359,18 +1422,17 @@ static int __init tun_init(void)
                printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
                goto err_misc;
        }
-       return 0;
-
+       return  0;
 err_misc:
-       unregister_pernet_device(&tun_net_ops);
-err_pernet:
+       rtnl_link_unregister(&tun_link_ops);
+err_linkops:
        return ret;
 }
 
 static void tun_cleanup(void)
 {
        misc_deregister(&tun_miscdev);
-       unregister_pernet_device(&tun_net_ops);
+       rtnl_link_unregister(&tun_link_ops);
 }
 
 module_init(tun_init);