tuntap: dont use skb after netif_rx_ni(skb)
[linux-block.git] / drivers / net / tun.c
index b44d7b79cddcef3562341c5c8ef78e14479d48fe..40b426edc9e6cbab7babc20ee4cb7a9dd2f0bbc6 100644 (file)
@@ -121,11 +121,11 @@ struct tap_filter {
  * also contains all socket related strctures (except sock_fprog and tap_filter)
  * to serve as one transmit queue for tuntap device. The sock_fprog and
  * tap_filter were kept in tun_struct since they were used for filtering for the
- * netdevice not for a specific queue (at least I didn't see the reqirement for
+ * netdevice not for a specific queue (at least I didn't see the requirement for
  * this).
  *
  * RCU usage:
- * The tun_file and tun_struct are loosely coupled, the pointer from on to the
+ * The tun_file and tun_struct are loosely coupled, the pointer from one to the
  * other can only be read while rcu_read_lock or rtnl_lock is held.
  */
 struct tun_file {
@@ -153,7 +153,7 @@ struct tun_flow_entry {
 #define TUN_NUM_FLOW_ENTRIES 1024
 
 /* Since the socket were moved to tun_file, to preserve the behavior of persist
- * device, socket fileter, sndbuf and vnet header size were restore when the
+ * device, socket filter, sndbuf and vnet header size were restore when the
  * file were attached to a persist device.
  */
 struct tun_struct {
@@ -297,13 +297,12 @@ static void tun_flow_cleanup(unsigned long data)
        spin_unlock_bh(&tun->lock);
 }
 
-static void tun_flow_update(struct tun_struct *tun, struct sk_buff *skb,
+static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
                            u16 queue_index)
 {
        struct hlist_head *head;
        struct tun_flow_entry *e;
        unsigned long delay = tun->ageing_time;
-       u32 rxhash = skb_get_rxhash(skb);
 
        if (!rxhash)
                return;
@@ -373,10 +372,11 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
 static inline bool tun_not_capable(struct tun_struct *tun)
 {
        const struct cred *cred = current_cred();
+       struct net *net = dev_net(tun->dev);
 
        return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
                  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
-               !capable(CAP_NET_ADMIN);
+               !ns_capable(net->user_ns, CAP_NET_ADMIN);
 }
 
 static void tun_set_real_num_queues(struct tun_struct *tun)
@@ -492,9 +492,6 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
 
        tun_set_real_num_queues(tun);
 
-       if (tun->numqueues == 1)
-               netif_carrier_on(tun->dev);
-
        /* device is allowed to go away first, so no need to hold extra
         * refcnt.
         */
@@ -688,25 +685,12 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
            sk_filter(tfile->socket.sk, skb))
                goto drop;
 
-       /* Limit the number of packets queued by divining txq length with the
+       /* Limit the number of packets queued by dividing txq length with the
         * number of queues.
         */
        if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
-                         >= dev->tx_queue_len / tun->numqueues){
-               if (!(tun->flags & TUN_ONE_QUEUE)) {
-                       /* Normal queueing mode. */
-                       /* Packet scheduler handles dropping of further packets. */
-                       netif_stop_subqueue(dev, txq);
-
-                       /* We won't see all dropped packets individually, so overrun
-                        * error is more appropriate. */
-                       dev->stats.tx_fifo_errors++;
-               } else {
-                       /* Single queue mode.
-                        * Driver handles dropping of all packets itself. */
-                       goto drop;
-               }
-       }
+                         >= dev->tx_queue_len / tun->numqueues)
+               goto drop;
 
        /* Orphan the skb - required as we might hang on to it
         * for indefinite time. */
@@ -864,6 +848,7 @@ static void tun_net_init(struct net_device *dev)
                /* Ethernet TAP Device */
                ether_setup(dev);
                dev->priv_flags &= ~IFF_TX_SKB_SHARING;
+               dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
 
                eth_hw_addr_random(dev);
 
@@ -1024,6 +1009,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
        int copylen;
        bool zerocopy = false;
        int err;
+       u32 rxhash;
 
        if (!(tun->flags & TUN_NO_PI)) {
                if ((len -= sizeof(pi)) > total_len)
@@ -1176,12 +1162,13 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
                skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
        }
 
+       rxhash = skb_get_rxhash(skb);
        netif_rx_ni(skb);
 
        tun->dev->stats.rx_packets++;
        tun->dev->stats.rx_bytes += len;
 
-       tun_flow_update(tun, skb, tfile->queue_index);
+       tun_flow_update(tun, rxhash, tfile->queue_index);
        return total_len;
 }
 
@@ -1295,7 +1282,7 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
        struct sk_buff *skb;
        ssize_t ret = 0;
 
-       tun_debug(KERN_INFO, tun, "tun_chr_read\n");
+       tun_debug(KERN_INFO, tun, "tun_do_read\n");
 
        if (unlikely(!noblock))
                add_wait_queue(&tfile->wq.wait, &wait);
@@ -1321,7 +1308,6 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
                        schedule();
                        continue;
                }
-               netif_wake_subqueue(tun->dev, tfile->queue_index);
 
                ret = tun_put_user(tun, tfile, skb, iv, len);
                kfree_skb(skb);
@@ -1484,6 +1470,9 @@ static int tun_flags(struct tun_struct *tun)
        if (tun->flags & TUN_NO_PI)
                flags |= IFF_NO_PI;
 
+       /* This flag has no real effect.  We track the value for backwards
+        * compatibility.
+        */
        if (tun->flags & TUN_ONE_QUEUE)
                flags |= IFF_ONE_QUEUE;
 
@@ -1559,7 +1548,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                char *name;
                unsigned long flags = 0;
 
-               if (!capable(CAP_NET_ADMIN))
+               if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
                        return -EPERM;
                err = security_tun_dev_create();
                if (err < 0)
@@ -1604,13 +1593,18 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
                tun_net_init(dev);
 
-               if (tun_flow_init(tun))
+               err = tun_flow_init(tun);
+               if (err < 0)
                        goto err_free_dev;
 
                dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
                        TUN_USER_FEATURES;
                dev->features = dev->hw_features;
 
+               err = tun_attach(tun, file);
+               if (err < 0)
+                       goto err_free_dev;
+
                err = register_netdevice(tun->dev);
                if (err < 0)
                        goto err_free_dev;
@@ -1620,9 +1614,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
                    device_create_file(&tun->dev->dev, &dev_attr_group))
                        pr_err("Failed to create tun sysfs files\n");
 
-               err = tun_attach(tun, file);
-               if (err < 0)
-                       goto err_free_dev;
+               netif_carrier_on(tun->dev);
        }
 
        tun_debug(KERN_INFO, tun, "tun_set_iff\n");
@@ -1632,6 +1624,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
        else
                tun->flags &= ~TUN_NO_PI;
 
+       /* This flag has no real effect.  We track the value for backwards
+        * compatibility.
+        */
        if (ifr->ifr_flags & IFF_ONE_QUEUE)
                tun->flags |= TUN_ONE_QUEUE;
        else
@@ -1661,7 +1656,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
        return err;
 }
 
-static int tun_get_iff(struct net *net, struct tun_struct *tun,
+static void tun_get_iff(struct net *net, struct tun_struct *tun,
                       struct ifreq *ifr)
 {
        tun_debug(KERN_INFO, tun, "tun_get_iff\n");
@@ -1670,7 +1665,6 @@ static int tun_get_iff(struct net *net, struct tun_struct *tun,
 
        ifr->ifr_flags = tun_flags(tun);
 
-       return 0;
 }
 
 /* This is like a cut-down ethtool ops, except done via tun fd so no
@@ -1846,9 +1840,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        ret = 0;
        switch (cmd) {
        case TUNGETIFF:
-               ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
-               if (ret)
-                       break;
+               tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
 
                if (copy_to_user(argp, &ifr, ifreq_len))
                        ret = -EFAULT;