SMC911x: unbreak PXA builds
[safe/jmp/linux-2.6] / drivers / net / tun.c
index 20db308..6daea0c 100644 (file)
@@ -22,7 +22,7 @@
  *    Add TUNSETLINK ioctl to set the link encapsulation
  *
  *  Mark Smith <markzzzsmith@yahoo.com.au>
- *   Use random_ether_addr() for tap MAC address.
+ *    Use random_ether_addr() for tap MAC address.
  *
  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
  *    Fixes in packet dropping, queue length setting and queue wakeup.
@@ -44,6 +44,7 @@
 #include <linux/kernel.h>
 #include <linux/major.h>
 #include <linux/slab.h>
+#include <linux/smp_lock.h>
 #include <linux/poll.h>
 #include <linux/fcntl.h>
 #include <linux/init.h>
 #include <linux/if_ether.h>
 #include <linux/if_tun.h>
 #include <linux/crc32.h>
+#include <linux/nsproxy.h>
+#include <linux/virtio_net.h>
+#include <net/net_namespace.h>
+#include <net/netns/generic.h>
 
 #include <asm/system.h>
 #include <asm/uaccess.h>
 
+/* Uncomment to enable debugging */
+/* #define TUN_DEBUG 1 */
+
 #ifdef TUN_DEBUG
 static int debug;
+
+#define DBG  if(tun->debug)printk
+#define DBG1 if(debug==2)printk
+#else
+#define DBG( a... )
+#define DBG1( a... )
+#endif
+
+#define FLT_EXACT_COUNT 8
+struct tap_filter {
+       unsigned int    count;    /* Number of addrs. Zero means disabled */
+       u32             mask[2];  /* Mask of the hashed addrs */
+       unsigned char   addr[FLT_EXACT_COUNT][ETH_ALEN];
+};
+
+struct tun_struct {
+       struct list_head        list;
+       unsigned int            flags;
+       int                     attached;
+       uid_t                   owner;
+       gid_t                   group;
+
+       wait_queue_head_t       read_wait;
+       struct sk_buff_head     readq;
+
+       struct net_device       *dev;
+       struct fasync_struct    *fasync;
+
+       struct tap_filter       txflt;
+
+#ifdef TUN_DEBUG
+       int debug;
 #endif
+};
+
+/* TAP filterting */
+static void addr_hash_set(u32 *mask, const u8 *addr)
+{
+       int n = ether_crc(ETH_ALEN, addr) >> 26;
+       mask[n >> 5] |= (1 << (n & 31));
+}
+
+static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
+{
+       int n = ether_crc(ETH_ALEN, addr) >> 26;
+       return mask[n >> 5] & (1 << (n & 31));
+}
+
+static int update_filter(struct tap_filter *filter, void __user *arg)
+{
+       struct { u8 u[ETH_ALEN]; } *addr;
+       struct tun_filter uf;
+       int err, alen, n, nexact;
+
+       if (copy_from_user(&uf, arg, sizeof(uf)))
+               return -EFAULT;
+
+       if (!uf.count) {
+               /* Disabled */
+               filter->count = 0;
+               return 0;
+       }
+
+       alen = ETH_ALEN * uf.count;
+       addr = kmalloc(alen, GFP_KERNEL);
+       if (!addr)
+               return -ENOMEM;
+
+       if (copy_from_user(addr, arg + sizeof(uf), alen)) {
+               err = -EFAULT;
+               goto done;
+       }
+
+       /* The filter is updated without holding any locks. Which is
+        * perfectly safe. We disable it first and in the worst
+        * case we'll accept a few undesired packets. */
+       filter->count = 0;
+       wmb();
+
+       /* Use first set of addresses as an exact filter */
+       for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
+               memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
+
+       nexact = n;
+
+       /* The rest is hashed */
+       memset(filter->mask, 0, sizeof(filter->mask));
+       for (; n < uf.count; n++)
+               addr_hash_set(filter->mask, addr[n].u);
+
+       /* For ALLMULTI just set the mask to all ones.
+        * This overrides the mask populated above. */
+       if ((uf.flags & TUN_FLT_ALLMULTI))
+               memset(filter->mask, ~0, sizeof(filter->mask));
+
+       /* Now enable the filter */
+       wmb();
+       filter->count = nexact;
+
+       /* Return the number of exact filters */
+       err = nexact;
+
+done:
+       kfree(addr);
+       return err;
+}
+
+/* Returns: 0 - drop, !=0 - accept */
+static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
+{
+       /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
+        * at this point. */
+       struct ethhdr *eh = (struct ethhdr *) skb->data;
+       int i;
+
+       /* Exact match */
+       for (i = 0; i < filter->count; i++)
+               if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
+                       return 1;
+
+       /* Inexact match (multicast only) */
+       if (is_multicast_ether_addr(eh->h_dest))
+               return addr_hash_test(filter->mask, eh->h_dest);
+
+       return 0;
+}
+
+/*
+ * Checks whether the packet is accepted or not.
+ * Returns: 0 - drop, !=0 - accept
+ */
+static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
+{
+       if (!filter->count)
+               return 1;
+
+       return run_filter(filter, skb);
+}
 
 /* Network device part of the driver */
 
-static LIST_HEAD(tun_dev_list);
-static struct ethtool_ops tun_ethtool_ops;
+static unsigned int tun_net_id;
+struct tun_net {
+       struct list_head dev_list;
+};
+
+static const struct ethtool_ops tun_ethtool_ops;
 
 /* Net device open. */
 static int tun_net_open(struct net_device *dev)
@@ -96,7 +245,12 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
        if (!tun->attached)
                goto drop;
 
-       /* Packet dropping */
+       /* Drop if the filter does not like it.
+        * This is a noop if the filter is disabled.
+        * Filter can be enabled only for the TAP devices. */
+       if (!check_filter(&tun->txflt, skb))
+               goto drop;
+
        if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
                if (!(tun->flags & TUN_ONE_QUEUE)) {
                        /* Normal queueing mode. */
@@ -105,7 +259,7 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
 
                        /* We won't see all dropped packets individually, so overrun
                         * error is more appropriate. */
-                       tun->stats.tx_fifo_errors++;
+                       dev->stats.tx_fifo_errors++;
                } else {
                        /* Single queue mode.
                         * Driver handles dropping of all packets itself. */
@@ -113,7 +267,7 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
                }
        }
 
-       /* Queue packet */
+       /* Enqueue packet */
        skb_queue_tail(&tun->readq, skb);
        dev->trans_start = jiffies;
 
@@ -124,53 +278,31 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
        return 0;
 
 drop:
-       tun->stats.tx_dropped++;
+       dev->stats.tx_dropped++;
        kfree_skb(skb);
        return 0;
 }
 
-/** Add the specified Ethernet address to this multicast filter. */
-static void
-add_multi(u32* filter, const u8* addr)
+static void tun_net_mclist(struct net_device *dev)
 {
-       int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
-       filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
+       /*
+        * This callback is supposed to deal with mc filter in
+        * _rx_ path and has nothing to do with the _tx_ path.
+        * In rx path we always accept everything userspace gives us.
+        */
+       return;
 }
 
-/** Remove the specified Ethernet addres from this multicast filter. */
-static void
-del_multi(u32* filter, const u8* addr)
-{
-       int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
-       filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
-}
+#define MIN_MTU 68
+#define MAX_MTU 65535
 
-/** Update the list of multicast groups to which the network device belongs.
- * This list is used to filter packets being sent from the character device to
- * the network device. */
-static void
-tun_net_mclist(struct net_device *dev)
+static int
+tun_net_change_mtu(struct net_device *dev, int new_mtu)
 {
-       struct tun_struct *tun = netdev_priv(dev);
-       const struct dev_mc_list *mclist;
-       int i;
-       DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
-                       dev->name, dev->mc_count);
-       memset(tun->chr_filter, 0, sizeof tun->chr_filter);
-       for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
-                       i++, mclist = mclist->next) {
-               add_multi(tun->net_filter, mclist->dmi_addr);
-               DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
-                               dev->name,
-                               mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
-                               mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
-       }
-}
-
-static struct net_device_stats *tun_net_stats(struct net_device *dev)
-{
-       struct tun_struct *tun = netdev_priv(dev);
-       return &tun->stats;
+       if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
+               return -EINVAL;
+       dev->mtu = new_mtu;
+       return 0;
 }
 
 /* Initialize net device. */
@@ -184,6 +316,7 @@ static void tun_net_init(struct net_device *dev)
                dev->hard_header_len = 0;
                dev->addr_len = 0;
                dev->mtu = 1500;
+               dev->change_mtu = tun_net_change_mtu;
 
                /* Zero header length */
                dev->type = ARPHRD_NONE;
@@ -193,10 +326,12 @@ static void tun_net_init(struct net_device *dev)
 
        case TUN_TAP_DEV:
                /* Ethernet TAP Device */
+               ether_setup(dev);
+               dev->change_mtu         = tun_net_change_mtu;
                dev->set_multicast_list = tun_net_mclist;
 
-               ether_setup(dev);
                random_ether_addr(dev->dev_addr);
+
                dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
                break;
        }
@@ -223,12 +358,73 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
        return mask;
 }
 
+/* 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)
+{
+       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;
+       }
+
+       /* Under a page?  Don't bother with paged skb. */
+       if (prepad + len < PAGE_SIZE)
+               return NULL;
+
+       /* Start with a normal skb, and add pages. */
+       skb = alloc_skb(prepad + linear, gfp);
+       if (!skb)
+               return NULL;
+
+       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;
+       }
+
+       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)
 {
        struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
        struct sk_buff *skb;
        size_t len = count, align = 0;
+       struct virtio_net_hdr gso = { 0 };
 
        if (!(tun->flags & TUN_NO_PI)) {
                if ((len -= sizeof(pi)) > count)
@@ -238,76 +434,120 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv,
                        return -EFAULT;
        }
 
-       if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
+       if (tun->flags & TUN_VNET_HDR) {
+               if ((len -= sizeof(gso)) > count)
+                       return -EINVAL;
+
+               if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
+                       return -EFAULT;
+
+               if (gso.hdr_len > len)
+                       return -EINVAL;
+       }
+
+       if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
                align = NET_IP_ALIGN;
+               if (unlikely(len < ETH_HLEN))
+                       return -EINVAL;
+       }
 
-       if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
-               tun->stats.rx_dropped++;
+       if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
+               tun->dev->stats.rx_dropped++;
                return -ENOMEM;
        }
 
-       if (align)
-               skb_reserve(skb, align);
-       if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
-               tun->stats.rx_dropped++;
+       if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
+               tun->dev->stats.rx_dropped++;
                kfree_skb(skb);
                return -EFAULT;
        }
 
-       skb->dev = tun->dev;
+       if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
+               if (!skb_partial_csum_set(skb, gso.csum_start,
+                                         gso.csum_offset)) {
+                       tun->dev->stats.rx_frame_errors++;
+                       kfree_skb(skb);
+                       return -EINVAL;
+               }
+       } else if (tun->flags & TUN_NOCHECKSUM)
+               skb->ip_summed = CHECKSUM_UNNECESSARY;
+
        switch (tun->flags & TUN_TYPE_MASK) {
        case TUN_TUN_DEV:
-               skb->mac.raw = skb->data;
+               if (tun->flags & TUN_NO_PI) {
+                       switch (skb->data[0] & 0xf0) {
+                       case 0x40:
+                               pi.proto = htons(ETH_P_IP);
+                               break;
+                       case 0x60:
+                               pi.proto = htons(ETH_P_IPV6);
+                               break;
+                       default:
+                               tun->dev->stats.rx_dropped++;
+                               kfree_skb(skb);
+                               return -EINVAL;
+                       }
+               }
+
+               skb_reset_mac_header(skb);
                skb->protocol = pi.proto;
+               skb->dev = tun->dev;
                break;
        case TUN_TAP_DEV:
                skb->protocol = eth_type_trans(skb, tun->dev);
                break;
        };
 
-       if (tun->flags & TUN_NOCHECKSUM)
-               skb->ip_summed = CHECKSUM_UNNECESSARY;
+       if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
+               pr_debug("GSO!\n");
+               switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
+               case VIRTIO_NET_HDR_GSO_TCPV4:
+                       skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
+                       break;
+               case VIRTIO_NET_HDR_GSO_TCPV6:
+                       skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
+                       break;
+               default:
+                       tun->dev->stats.rx_frame_errors++;
+                       kfree_skb(skb);
+                       return -EINVAL;
+               }
 
-       netif_rx_ni(skb);
-       tun->dev->last_rx = jiffies;
+               if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
+                       skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
 
-       tun->stats.rx_packets++;
-       tun->stats.rx_bytes += len;
+               skb_shinfo(skb)->gso_size = gso.gso_size;
+               if (skb_shinfo(skb)->gso_size == 0) {
+                       tun->dev->stats.rx_frame_errors++;
+                       kfree_skb(skb);
+                       return -EINVAL;
+               }
 
-       return count;
-}
+               /* Header must be checked, and gso_segs computed. */
+               skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
+               skb_shinfo(skb)->gso_segs = 0;
+       }
 
-static inline size_t iov_total(const struct iovec *iv, unsigned long count)
-{
-       unsigned long i;
-       size_t len;
+       netif_rx_ni(skb);
+       tun->dev->last_rx = jiffies;
 
-       for (i = 0, len = 0; i < count; i++)
-               len += iv[i].iov_len;
+       tun->dev->stats.rx_packets++;
+       tun->dev->stats.rx_bytes += len;
 
-       return len;
+       return count;
 }
 
-/* Writev */
-static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
-                             unsigned long count, loff_t *pos)
+static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
+                             unsigned long count, loff_t pos)
 {
-       struct tun_struct *tun = file->private_data;
+       struct tun_struct *tun = iocb->ki_filp->private_data;
 
        if (!tun)
                return -EBADFD;
 
        DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
 
-       return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
-}
-
-/* Write */
-static ssize_t tun_chr_write(struct file * file, const char __user * buf,
-                            size_t count, loff_t *pos)
-{
-       struct iovec iv = { (void __user *) buf, count };
-       return tun_chr_writev(file, &iv, 1, pos);
+       return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
 }
 
 /* Put packet to the user space buffer */
@@ -332,21 +572,54 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
                total += sizeof(pi);
        }
 
+       if (tun->flags & TUN_VNET_HDR) {
+               struct virtio_net_hdr gso = { 0 }; /* no info leak */
+               if ((len -= sizeof(gso)) < 0)
+                       return -EINVAL;
+
+               if (skb_is_gso(skb)) {
+                       struct skb_shared_info *sinfo = skb_shinfo(skb);
+
+                       /* This is a hint as to how much should be linear. */
+                       gso.hdr_len = skb_headlen(skb);
+                       gso.gso_size = sinfo->gso_size;
+                       if (sinfo->gso_type & SKB_GSO_TCPV4)
+                               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
+                               BUG();
+                       if (sinfo->gso_type & SKB_GSO_TCP_ECN)
+                               gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
+               } else
+                       gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
+
+               if (skb->ip_summed == CHECKSUM_PARTIAL) {
+                       gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
+                       gso.csum_start = skb->csum_start - skb_headroom(skb);
+                       gso.csum_offset = skb->csum_offset;
+               } /* else everything is zero */
+
+               if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
+                       return -EFAULT;
+               total += sizeof(gso);
+       }
+
        len = min_t(int, skb->len, len);
 
        skb_copy_datagram_iovec(skb, 0, iv, len);
        total += len;
 
-       tun->stats.tx_packets++;
-       tun->stats.tx_bytes += len;
+       tun->dev->stats.tx_packets++;
+       tun->dev->stats.tx_bytes += len;
 
        return total;
 }
 
-/* Readv */
-static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
-                           unsigned long count, loff_t *pos)
+static ssize_t tun_chr_aio_read(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;
        DECLARE_WAITQUEUE(wait, current);
        struct sk_buff *skb;
@@ -357,16 +630,12 @@ static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
 
        DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
 
-       len = iov_total(iv, count);
+       len = iov_length(iv, count);
        if (len < 0)
                return -EINVAL;
 
        add_wait_queue(&tun->read_wait, &wait);
        while (len) {
-               const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-               u8 addr[ ETH_ALEN];
-               int bit_nr;
-
                current->state = TASK_INTERRUPTIBLE;
 
                /* Read frames from the queue */
@@ -386,38 +655,9 @@ static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
                }
                netif_wake_queue(tun->dev);
 
-               /** Decide whether to accept this packet. This code is designed to
-                * behave identically to an Ethernet interface. Accept the packet if
-                * - we are promiscuous.
-                * - the packet is addressed to us.
-                * - the packet is broadcast.
-                * - the packet is multicast and
-                *   - we are multicast promiscous.
-                *   - we belong to the multicast group.
-                */
-               memcpy(addr, skb->data,
-                      min_t(size_t, sizeof addr, skb->len));
-               bit_nr = ether_crc(sizeof addr, addr) >> 26;
-               if ((tun->if_flags & IFF_PROMISC) ||
-                               memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
-                               memcmp(addr, ones, sizeof addr) == 0 ||
-                               (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
-                                 (addr[0] == 0x33 && addr[1] == 0x33)) &&
-                                ((tun->if_flags & IFF_ALLMULTI) ||
-                                 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
-                       DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
-                                       tun->dev->name, addr[0], addr[1], addr[2],
-                                       addr[3], addr[4], addr[5]);
-                       ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
-                       kfree_skb(skb);
-                       break;
-               } else {
-                       DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
-                                       tun->dev->name, addr[0], addr[1], addr[2],
-                                       addr[3], addr[4], addr[5]);
-                       kfree_skb(skb);
-                       continue;
-               }
+               ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
+               kfree_skb(skb);
+               break;
        }
 
        current->state = TASK_RUNNING;
@@ -426,14 +666,6 @@ static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
        return ret;
 }
 
-/* Read */
-static ssize_t tun_chr_read(struct file * file, char __user * buf,
-                           size_t count, loff_t *pos)
-{
-       struct iovec iv = { buf, count };
-       return tun_chr_readv(file, &iv, 1, pos);
-}
-
 static void tun_setup(struct net_device *dev)
 {
        struct tun_struct *tun = netdev_priv(dev);
@@ -442,22 +674,22 @@ static void tun_setup(struct net_device *dev)
        init_waitqueue_head(&tun->read_wait);
 
        tun->owner = -1;
+       tun->group = -1;
 
-       SET_MODULE_OWNER(dev);
        dev->open = tun_net_open;
        dev->hard_start_xmit = tun_net_xmit;
        dev->stop = tun_net_close;
-       dev->get_stats = tun_net_stats;
        dev->ethtool_ops = &tun_ethtool_ops;
        dev->destructor = free_netdev;
+       dev->features |= NETIF_F_NETNS_LOCAL;
 }
 
-static struct tun_struct *tun_get_by_name(const char *name)
+static struct tun_struct *tun_get_by_name(struct tun_net *tn, const char *name)
 {
        struct tun_struct *tun;
 
        ASSERT_RTNL();
-       list_for_each_entry(tun, &tun_dev_list, list) {
+       list_for_each_entry(tun, &tn->dev_list, list) {
                if (!strncmp(tun->dev->name, name, IFNAMSIZ))
                    return tun;
        }
@@ -465,23 +697,28 @@ static struct tun_struct *tun_get_by_name(const char *name)
        return NULL;
 }
 
-static int tun_set_iff(struct file *file, struct ifreq *ifr)
+static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 {
+       struct tun_net *tn;
        struct tun_struct *tun;
        struct net_device *dev;
        int err;
 
-       tun = tun_get_by_name(ifr->ifr_name);
+       tn = net_generic(net, tun_net_id);
+       tun = tun_get_by_name(tn, ifr->ifr_name);
        if (tun) {
                if (tun->attached)
                        return -EBUSY;
 
                /* Check permissions */
-               if (tun->owner != -1 &&
-                   current->euid != tun->owner && !capable(CAP_NET_ADMIN))
+               if (((tun->owner != -1 &&
+                     current->euid != tun->owner) ||
+                    (tun->group != -1 &&
+                     current->egid != tun->group)) &&
+                    !capable(CAP_NET_ADMIN))
                        return -EPERM;
        }
-       else if (__dev_get_by_name(ifr->ifr_name))
+       else if (__dev_get_by_name(net, ifr->ifr_name))
                return -EINVAL;
        else {
                char *name;
@@ -512,15 +749,11 @@ static int tun_set_iff(struct file *file, struct ifreq *ifr)
                if (!dev)
                        return -ENOMEM;
 
+               dev_net_set(dev, net);
                tun = netdev_priv(dev);
                tun->dev = dev;
                tun->flags = flags;
-               /* Be promiscuous by default to maintain previous behaviour. */
-               tun->if_flags = IFF_PROMISC;
-               /* Generate random Ethernet address. */
-               *(u16 *)tun->dev_addr = htons(0x00FF);
-               get_random_bytes(tun->dev_addr + sizeof(u16), 4);
-               memset(tun->chr_filter, 0, sizeof tun->chr_filter);
+               tun->txflt.count = 0;
 
                tun_net_init(dev);
 
@@ -534,19 +767,35 @@ static int tun_set_iff(struct file *file, struct ifreq *ifr)
                if (err < 0)
                        goto err_free_dev;
 
-               list_add(&tun->list, &tun_dev_list);
+               list_add(&tun->list, &tn->dev_list);
        }
 
        DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
 
        if (ifr->ifr_flags & IFF_NO_PI)
                tun->flags |= TUN_NO_PI;
+       else
+               tun->flags &= ~TUN_NO_PI;
 
        if (ifr->ifr_flags & IFF_ONE_QUEUE)
                tun->flags |= TUN_ONE_QUEUE;
+       else
+               tun->flags &= ~TUN_ONE_QUEUE;
+
+       if (ifr->ifr_flags & IFF_VNET_HDR)
+               tun->flags |= TUN_VNET_HDR;
+       else
+               tun->flags &= ~TUN_VNET_HDR;
 
        file->private_data = tun;
        tun->attached = 1;
+       get_net(dev_net(tun->dev));
+
+       /* Make sure persistent devices do not get stuck in
+        * xoff state.
+        */
+       if (netif_running(tun->dev))
+               netif_wake_queue(tun->dev);
 
        strcpy(ifr->ifr_name, tun->dev->name);
        return 0;
@@ -557,12 +806,84 @@ static int tun_set_iff(struct file *file, struct ifreq *ifr)
        return err;
 }
 
+static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
+{
+       struct tun_struct *tun = file->private_data;
+
+       if (!tun)
+               return -EBADFD;
+
+       DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
+
+       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;
+
+       return 0;
+}
+
+/* This is like a cut-down ethtool ops, except done via tun fd so no
+ * privs required. */
+static int set_offload(struct net_device *dev, unsigned long arg)
+{
+       unsigned int old_features, features;
+
+       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));
+
+       if (arg & TUN_F_CSUM) {
+               features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
+               arg &= ~TUN_F_CSUM;
+
+               if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
+                       if (arg & TUN_F_TSO_ECN) {
+                               features |= NETIF_F_TSO_ECN;
+                               arg &= ~TUN_F_TSO_ECN;
+                       }
+                       if (arg & TUN_F_TSO4)
+                               features |= NETIF_F_TSO;
+                       if (arg & TUN_F_TSO6)
+                               features |= NETIF_F_TSO6;
+                       arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
+               }
+       }
+
+       /* This gives the user a way to test for new features in future by
+        * trying to set them. */
+       if (arg)
+               return -EINVAL;
+
+       dev->features = features;
+       if (old_features != dev->features)
+               netdev_features_change(dev);
+
+       return 0;
+}
+
 static int tun_chr_ioctl(struct inode *inode, struct file *file,
                         unsigned int cmd, unsigned long arg)
 {
        struct tun_struct *tun = file->private_data;
        void __user* argp = (void __user*)arg;
        struct ifreq ifr;
+       int ret;
+       DECLARE_MAC_BUF(mac);
 
        if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
                if (copy_from_user(&ifr, argp, sizeof ifr))
@@ -574,7 +895,7 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
                ifr.ifr_name[IFNAMSIZ-1] = '\0';
 
                rtnl_lock();
-               err = tun_set_iff(file, &ifr);
+               err = tun_set_iff(current->nsproxy->net_ns, file, &ifr);
                rtnl_unlock();
 
                if (err)
@@ -585,12 +906,30 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
                return 0;
        }
 
+       if (cmd == TUNGETFEATURES) {
+               /* Currently this just means: "what IFF flags are valid?".
+                * This is needed because we never checked for invalid flags on
+                * TUNSETIFF. */
+               return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
+                               IFF_VNET_HDR,
+                               (unsigned int __user*)argp);
+       }
+
        if (!tun)
                return -EBADFD;
 
        DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
 
        switch (cmd) {
+       case TUNGETIFF:
+               ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
+               if (ret)
+                       return ret;
+
+               if (copy_to_user(argp, &ifr, sizeof(ifr)))
+                       return -EFAULT;
+               break;
+
        case TUNSETNOCSUM:
                /* Disable/Enable checksum */
                if (arg)
@@ -610,7 +949,7 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
                        tun->flags &= ~TUN_PERSIST;
 
                DBG(KERN_INFO "%s: persist %s\n",
-                   tun->dev->name, arg ? "disabled" : "enabled");
+                   tun->dev->name, arg ? "enabled" : "disabled");
                break;
 
        case TUNSETOWNER:
@@ -620,78 +959,65 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
                DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
                break;
 
+       case TUNSETGROUP:
+               /* Set group of the device */
+               tun->group= (gid_t) arg;
+
+               DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
+               break;
+
        case TUNSETLINK:
                /* Only allow setting the type when the interface is down */
+               rtnl_lock();
                if (tun->dev->flags & IFF_UP) {
                        DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
                                tun->dev->name);
-                       return -EBUSY;
+                       ret = -EBUSY;
                } else {
                        tun->dev->type = (int) arg;
                        DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
+                       ret = 0;
                }
-               break;
+               rtnl_unlock();
+               return ret;
 
 #ifdef TUN_DEBUG
        case TUNSETDEBUG:
                tun->debug = arg;
                break;
 #endif
+       case TUNSETOFFLOAD:
+               rtnl_lock();
+               ret = set_offload(tun->dev, arg);
+               rtnl_unlock();
+               return ret;
 
-       case SIOCGIFFLAGS:
-               ifr.ifr_flags = tun->if_flags;
-               if (copy_to_user( argp, &ifr, sizeof ifr))
-                       return -EFAULT;
-               return 0;
-
-       case SIOCSIFFLAGS:
-               /** Set the character device's interface flags. Currently only
-                * IFF_PROMISC and IFF_ALLMULTI are used. */
-               tun->if_flags = ifr.ifr_flags;
-               DBG(KERN_INFO "%s: interface flags 0x%lx\n",
-                               tun->dev->name, tun->if_flags);
-               return 0;
+       case TUNSETTXFILTER:
+               /* Can be set only for TAPs */
+               if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
+                       return -EINVAL;
+               rtnl_lock();
+               ret = update_filter(&tun->txflt, (void __user *)arg);
+               rtnl_unlock();
+               return ret;
 
        case SIOCGIFHWADDR:
-               memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
-                               min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
-               if (copy_to_user( argp, &ifr, sizeof ifr))
+               /* Get hw addres */
+               memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
+               ifr.ifr_hwaddr.sa_family = tun->dev->type;
+               if (copy_to_user(argp, &ifr, sizeof ifr))
                        return -EFAULT;
                return 0;
 
        case SIOCSIFHWADDR:
-               /** Set the character device's hardware address. This is used when
-                * filtering packets being sent from the network device to the character
-                * device. */
-               memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
-                               min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
-               DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
-                               tun->dev->name,
-                               tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
-                               tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
-               return 0;
-
-       case SIOCADDMULTI:
-               /** Add the specified group to the character device's multicast filter
-                * list. */
-               add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
-               DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
-                               tun->dev->name,
-                               (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
-                               (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
-                               (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
-               return 0;
+               /* Set hw address */
+               DBG(KERN_DEBUG "%s: set hw address: %s\n",
+                       tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
 
-       case SIOCDELMULTI:
-               /** Remove the specified group from the character device's multicast
-                * filter list. */
-               del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
-               DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
-                               tun->dev->name,
-                               (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
-                               (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
-                               (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
-               return 0;
+               rtnl_lock();
+               ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
+               rtnl_unlock();
+               return ret;
 
        default:
                return -EINVAL;
@@ -710,22 +1036,26 @@ static int tun_chr_fasync(int fd, struct file *file, int on)
 
        DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
 
+       lock_kernel();
        if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
-               return ret;
+               goto out;
 
        if (on) {
-               ret = f_setown(file, current->pid, 0);
+               ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
                if (ret)
-                       return ret;
+                       goto out;
                tun->flags |= TUN_FASYNC;
        } else
                tun->flags &= ~TUN_FASYNC;
-
-       return 0;
+       ret = 0;
+out:
+       unlock_kernel();
+       return ret;
 }
 
 static int tun_chr_open(struct inode *inode, struct file * file)
 {
+       cycle_kernel_lock();
        DBG1(KERN_INFO "tunX: tun_chr_open\n");
        file->private_data = NULL;
        return 0;
@@ -747,6 +1077,7 @@ static int tun_chr_close(struct inode *inode, struct file *file)
        /* Detach from net device */
        file->private_data = NULL;
        tun->attached = 0;
+       put_net(dev_net(tun->dev));
 
        /* Drop read queue */
        skb_queue_purge(&tun->readq);
@@ -761,13 +1092,13 @@ static int tun_chr_close(struct inode *inode, struct file *file)
        return 0;
 }
 
-static struct file_operations tun_fops = {
+static const struct file_operations tun_fops = {
        .owner  = THIS_MODULE,
        .llseek = no_llseek,
-       .read   = tun_chr_read,
-       .readv  = tun_chr_readv,
-       .write  = tun_chr_write,
-       .writev = tun_chr_writev,
+       .read  = do_sync_read,
+       .aio_read  = tun_chr_aio_read,
+       .write = do_sync_write,
+       .aio_write = tun_chr_aio_write,
        .poll   = tun_chr_poll,
        .ioctl  = tun_chr_ioctl,
        .open   = tun_chr_open,
@@ -856,7 +1187,7 @@ static int tun_set_rx_csum(struct net_device *dev, u32 data)
        return 0;
 }
 
-static struct ethtool_ops tun_ethtool_ops = {
+static const struct ethtool_ops tun_ethtool_ops = {
        .get_settings   = tun_get_settings,
        .get_drvinfo    = tun_get_drvinfo,
        .get_msglevel   = tun_get_msglevel,
@@ -866,32 +1197,76 @@ static struct ethtool_ops tun_ethtool_ops = {
        .set_rx_csum    = tun_set_rx_csum
 };
 
-static int __init tun_init(void)
+static int tun_init_net(struct net *net)
 {
-       int ret = 0;
+       struct tun_net *tn;
 
-       printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
-       printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
+       tn = kmalloc(sizeof(*tn), GFP_KERNEL);
+       if (tn == NULL)
+               return -ENOMEM;
 
-       ret = misc_register(&tun_miscdev);
-       if (ret)
-               printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
-       return ret;
+       INIT_LIST_HEAD(&tn->dev_list);
+
+       if (net_assign_generic(net, tun_net_id, tn)) {
+               kfree(tn);
+               return -ENOMEM;
+       }
+
+       return 0;
 }
 
-static void tun_cleanup(void)
+static void tun_exit_net(struct net *net)
 {
+       struct tun_net *tn;
        struct tun_struct *tun, *nxt;
 
-       misc_deregister(&tun_miscdev);
+       tn = net_generic(net, tun_net_id);
 
        rtnl_lock();
-       list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
+       list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
                DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
                unregister_netdevice(tun->dev);
        }
        rtnl_unlock();
 
+       kfree(tn);
+}
+
+static struct pernet_operations tun_net_ops = {
+       .init = tun_init_net,
+       .exit = tun_exit_net,
+};
+
+static int __init tun_init(void)
+{
+       int ret = 0;
+
+       printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
+       printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
+
+       ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
+       if (ret) {
+               printk(KERN_ERR "tun: Can't register pernet ops\n");
+               goto err_pernet;
+       }
+
+       ret = misc_register(&tun_miscdev);
+       if (ret) {
+               printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
+               goto err_misc;
+       }
+       return 0;
+
+err_misc:
+       unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
+err_pernet:
+       return ret;
+}
+
+static void tun_cleanup(void)
+{
+       misc_deregister(&tun_miscdev);
+       unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
 }
 
 module_init(tun_init);