xref: /openbmc/linux/drivers/net/tun.c (revision 5188cd44c55db3e92cd9e77a40b5baa7ed4340f7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  TUN - Universal TUN/TAP device driver.
31da177e4SLinus Torvalds  *  Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *  it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *  the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *  (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131da177e4SLinus Torvalds  *  GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds  *  Changes:
201da177e4SLinus Torvalds  *
21ff4cc3acSMike Kershaw  *  Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22ff4cc3acSMike Kershaw  *    Add TUNSETLINK ioctl to set the link encapsulation
23ff4cc3acSMike Kershaw  *
241da177e4SLinus Torvalds  *  Mark Smith <markzzzsmith@yahoo.com.au>
25344dc8edSJoe Perches  *    Use eth_random_addr() for tap MAC address.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
281da177e4SLinus Torvalds  *    Fixes in packet dropping, queue length setting and queue wakeup.
291da177e4SLinus Torvalds  *    Increased default tx queue length.
301da177e4SLinus Torvalds  *    Added ethtool API.
311da177e4SLinus Torvalds  *    Minor cleanups
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  *  Daniel Podlejski <underley@underley.eu.org>
341da177e4SLinus Torvalds  *    Modifications for 2.3.99-pre5 kernel.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
386b8a66eeSJoe Perches 
391da177e4SLinus Torvalds #define DRV_NAME	"tun"
401da177e4SLinus Torvalds #define DRV_VERSION	"1.6"
411da177e4SLinus Torvalds #define DRV_DESCRIPTION	"Universal TUN/TAP device driver"
421da177e4SLinus Torvalds #define DRV_COPYRIGHT	"(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/module.h>
451da177e4SLinus Torvalds #include <linux/errno.h>
461da177e4SLinus Torvalds #include <linux/kernel.h>
471da177e4SLinus Torvalds #include <linux/major.h>
481da177e4SLinus Torvalds #include <linux/slab.h>
491da177e4SLinus Torvalds #include <linux/poll.h>
501da177e4SLinus Torvalds #include <linux/fcntl.h>
511da177e4SLinus Torvalds #include <linux/init.h>
521da177e4SLinus Torvalds #include <linux/skbuff.h>
531da177e4SLinus Torvalds #include <linux/netdevice.h>
541da177e4SLinus Torvalds #include <linux/etherdevice.h>
551da177e4SLinus Torvalds #include <linux/miscdevice.h>
561da177e4SLinus Torvalds #include <linux/ethtool.h>
571da177e4SLinus Torvalds #include <linux/rtnetlink.h>
5850857e2aSArnd Bergmann #include <linux/compat.h>
591da177e4SLinus Torvalds #include <linux/if.h>
601da177e4SLinus Torvalds #include <linux/if_arp.h>
611da177e4SLinus Torvalds #include <linux/if_ether.h>
621da177e4SLinus Torvalds #include <linux/if_tun.h>
636680ec68SJason Wang #include <linux/if_vlan.h>
641da177e4SLinus Torvalds #include <linux/crc32.h>
65d647a591SPavel Emelyanov #include <linux/nsproxy.h>
66f43798c2SRusty Russell #include <linux/virtio_net.h>
6799405162SMichael S. Tsirkin #include <linux/rcupdate.h>
68*5188cd44SBen Hutchings #include <net/ipv6.h>
69881d966bSEric W. Biederman #include <net/net_namespace.h>
7079d17604SPavel Emelyanov #include <net/netns/generic.h>
71f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
7233dccbb0SHerbert Xu #include <net/sock.h>
7393e14b6dSMasatake YAMATO #include <linux/seq_file.h>
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #include <asm/uaccess.h>
761da177e4SLinus Torvalds 
7714daa021SRusty Russell /* Uncomment to enable debugging */
7814daa021SRusty Russell /* #define TUN_DEBUG 1 */
7914daa021SRusty Russell 
801da177e4SLinus Torvalds #ifdef TUN_DEBUG
811da177e4SLinus Torvalds static int debug;
8214daa021SRusty Russell 
836b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
846b8a66eeSJoe Perches do {								\
856b8a66eeSJoe Perches 	if (tun->debug)						\
866b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
876b8a66eeSJoe Perches } while (0)
886b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
896b8a66eeSJoe Perches do {								\
906b8a66eeSJoe Perches 	if (debug == 2)						\
916b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
926b8a66eeSJoe Perches } while (0)
9314daa021SRusty Russell #else
946b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
956b8a66eeSJoe Perches do {								\
966b8a66eeSJoe Perches 	if (0)							\
976b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
986b8a66eeSJoe Perches } while (0)
996b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
1006b8a66eeSJoe Perches do {								\
1016b8a66eeSJoe Perches 	if (0)							\
1026b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
1036b8a66eeSJoe Perches } while (0)
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
1060690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128
1070690899bSMichael S. Tsirkin 
108f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
109f271b2ccSMax Krasnyansky struct tap_filter {
110f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
111f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
112f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
113f271b2ccSMax Krasnyansky };
114f271b2ccSMax Krasnyansky 
11592d4ea6eSstephen hemminger /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for
116edfb6a14SJason Wang  * the netdevice to be fit in one page. So we can make sure the success of
117edfb6a14SJason Wang  * memory allocation. TODO: increase the limit. */
118edfb6a14SJason Wang #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
119b8732fb7SJason Wang #define MAX_TAP_FLOWS  4096
120c8d68e6bSJason Wang 
12196442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ)
12296442e42SJason Wang 
12354f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
12492d4ea6eSstephen hemminger  * also contains all socket related structures (except sock_fprog and tap_filter)
12554f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
12654f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
12736fe8c09SRami Rosen  * netdevice not for a specific queue (at least I didn't see the requirement for
12854f968d6SJason Wang  * this).
1296e914fc7SJason Wang  *
1306e914fc7SJason Wang  * RCU usage:
13136fe8c09SRami Rosen  * The tun_file and tun_struct are loosely coupled, the pointer from one to the
1326e914fc7SJason Wang  * other can only be read while rcu_read_lock or rtnl_lock is held.
13354f968d6SJason Wang  */
134631ab46bSEric W. Biederman struct tun_file {
13554f968d6SJason Wang 	struct sock sk;
13654f968d6SJason Wang 	struct socket socket;
13754f968d6SJason Wang 	struct socket_wq wq;
1386e914fc7SJason Wang 	struct tun_struct __rcu *tun;
13936b50babSEric W. Biederman 	struct net *net;
14054f968d6SJason Wang 	struct fasync_struct *fasync;
14154f968d6SJason Wang 	/* only used for fasnyc */
14254f968d6SJason Wang 	unsigned int flags;
143fb7589a1SPavel Emelyanov 	union {
144c8d68e6bSJason Wang 		u16 queue_index;
145fb7589a1SPavel Emelyanov 		unsigned int ifindex;
146fb7589a1SPavel Emelyanov 	};
1474008e97fSJason Wang 	struct list_head next;
1484008e97fSJason Wang 	struct tun_struct *detached;
149631ab46bSEric W. Biederman };
150631ab46bSEric W. Biederman 
15196442e42SJason Wang struct tun_flow_entry {
15296442e42SJason Wang 	struct hlist_node hash_link;
15396442e42SJason Wang 	struct rcu_head rcu;
15496442e42SJason Wang 	struct tun_struct *tun;
15596442e42SJason Wang 
15696442e42SJason Wang 	u32 rxhash;
1579bc88939STom Herbert 	u32 rps_rxhash;
15896442e42SJason Wang 	int queue_index;
15996442e42SJason Wang 	unsigned long updated;
16096442e42SJason Wang };
16196442e42SJason Wang 
16296442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024
16396442e42SJason Wang 
16454f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
16536fe8c09SRami Rosen  * device, socket filter, sndbuf and vnet header size were restore when the
16654f968d6SJason Wang  * file were attached to a persist device.
16754f968d6SJason Wang  */
16814daa021SRusty Russell struct tun_struct {
169c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
170c8d68e6bSJason Wang 	unsigned int            numqueues;
171f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1720625c883SEric W. Biederman 	kuid_t			owner;
1730625c883SEric W. Biederman 	kgid_t			group;
17414daa021SRusty Russell 
17514daa021SRusty Russell 	struct net_device	*dev;
176c8f44affSMichał Mirosław 	netdev_features_t	set_features;
17788255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
1783d0ad094SBen Hutchings 			  NETIF_F_TSO6)
179d9d52b51SMichael S. Tsirkin 
180d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
18154f968d6SJason Wang 	int			sndbuf;
18254f968d6SJason Wang 	struct tap_filter	txflt;
18354f968d6SJason Wang 	struct sock_fprog	fprog;
18454f968d6SJason Wang 	/* protected by rtnl lock */
18554f968d6SJason Wang 	bool			filter_attached;
18614daa021SRusty Russell #ifdef TUN_DEBUG
18714daa021SRusty Russell 	int debug;
18814daa021SRusty Russell #endif
18996442e42SJason Wang 	spinlock_t lock;
19096442e42SJason Wang 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
19196442e42SJason Wang 	struct timer_list flow_gc_timer;
19296442e42SJason Wang 	unsigned long ageing_time;
1934008e97fSJason Wang 	unsigned int numdisabled;
1944008e97fSJason Wang 	struct list_head disabled;
1955dbbaf2dSPaul Moore 	void *security;
196b8732fb7SJason Wang 	u32 flow_count;
19714daa021SRusty Russell };
19814daa021SRusty Russell 
19996442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash)
20096442e42SJason Wang {
20196442e42SJason Wang 	return rxhash & 0x3ff;
20296442e42SJason Wang }
20396442e42SJason Wang 
20496442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
20596442e42SJason Wang {
20696442e42SJason Wang 	struct tun_flow_entry *e;
20796442e42SJason Wang 
208b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(e, head, hash_link) {
20996442e42SJason Wang 		if (e->rxhash == rxhash)
21096442e42SJason Wang 			return e;
21196442e42SJason Wang 	}
21296442e42SJason Wang 	return NULL;
21396442e42SJason Wang }
21496442e42SJason Wang 
21596442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
21696442e42SJason Wang 					      struct hlist_head *head,
21796442e42SJason Wang 					      u32 rxhash, u16 queue_index)
21896442e42SJason Wang {
2199fdc6befSEric Dumazet 	struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
2209fdc6befSEric Dumazet 
22196442e42SJason Wang 	if (e) {
22296442e42SJason Wang 		tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
22396442e42SJason Wang 			  rxhash, queue_index);
22496442e42SJason Wang 		e->updated = jiffies;
22596442e42SJason Wang 		e->rxhash = rxhash;
2269bc88939STom Herbert 		e->rps_rxhash = 0;
22796442e42SJason Wang 		e->queue_index = queue_index;
22896442e42SJason Wang 		e->tun = tun;
22996442e42SJason Wang 		hlist_add_head_rcu(&e->hash_link, head);
230b8732fb7SJason Wang 		++tun->flow_count;
23196442e42SJason Wang 	}
23296442e42SJason Wang 	return e;
23396442e42SJason Wang }
23496442e42SJason Wang 
23596442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
23696442e42SJason Wang {
23796442e42SJason Wang 	tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
23896442e42SJason Wang 		  e->rxhash, e->queue_index);
2399bc88939STom Herbert 	sock_rps_reset_flow_hash(e->rps_rxhash);
24096442e42SJason Wang 	hlist_del_rcu(&e->hash_link);
2419fdc6befSEric Dumazet 	kfree_rcu(e, rcu);
242b8732fb7SJason Wang 	--tun->flow_count;
24396442e42SJason Wang }
24496442e42SJason Wang 
24596442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun)
24696442e42SJason Wang {
24796442e42SJason Wang 	int i;
24896442e42SJason Wang 
24996442e42SJason Wang 	spin_lock_bh(&tun->lock);
25096442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
25196442e42SJason Wang 		struct tun_flow_entry *e;
252b67bfe0dSSasha Levin 		struct hlist_node *n;
25396442e42SJason Wang 
254b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
25596442e42SJason Wang 			tun_flow_delete(tun, e);
25696442e42SJason Wang 	}
25796442e42SJason Wang 	spin_unlock_bh(&tun->lock);
25896442e42SJason Wang }
25996442e42SJason Wang 
26096442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
26196442e42SJason Wang {
26296442e42SJason Wang 	int i;
26396442e42SJason Wang 
26496442e42SJason Wang 	spin_lock_bh(&tun->lock);
26596442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
26696442e42SJason Wang 		struct tun_flow_entry *e;
267b67bfe0dSSasha Levin 		struct hlist_node *n;
26896442e42SJason Wang 
269b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
27096442e42SJason Wang 			if (e->queue_index == queue_index)
27196442e42SJason Wang 				tun_flow_delete(tun, e);
27296442e42SJason Wang 		}
27396442e42SJason Wang 	}
27496442e42SJason Wang 	spin_unlock_bh(&tun->lock);
27596442e42SJason Wang }
27696442e42SJason Wang 
27796442e42SJason Wang static void tun_flow_cleanup(unsigned long data)
27896442e42SJason Wang {
27996442e42SJason Wang 	struct tun_struct *tun = (struct tun_struct *)data;
28096442e42SJason Wang 	unsigned long delay = tun->ageing_time;
28196442e42SJason Wang 	unsigned long next_timer = jiffies + delay;
28296442e42SJason Wang 	unsigned long count = 0;
28396442e42SJason Wang 	int i;
28496442e42SJason Wang 
28596442e42SJason Wang 	tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
28696442e42SJason Wang 
28796442e42SJason Wang 	spin_lock_bh(&tun->lock);
28896442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
28996442e42SJason Wang 		struct tun_flow_entry *e;
290b67bfe0dSSasha Levin 		struct hlist_node *n;
29196442e42SJason Wang 
292b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
29396442e42SJason Wang 			unsigned long this_timer;
29496442e42SJason Wang 			count++;
29596442e42SJason Wang 			this_timer = e->updated + delay;
29696442e42SJason Wang 			if (time_before_eq(this_timer, jiffies))
29796442e42SJason Wang 				tun_flow_delete(tun, e);
29896442e42SJason Wang 			else if (time_before(this_timer, next_timer))
29996442e42SJason Wang 				next_timer = this_timer;
30096442e42SJason Wang 		}
30196442e42SJason Wang 	}
30296442e42SJason Wang 
30396442e42SJason Wang 	if (count)
30496442e42SJason Wang 		mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
30596442e42SJason Wang 	spin_unlock_bh(&tun->lock);
30696442e42SJason Wang }
30796442e42SJason Wang 
30849974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
3099e85722dSJason Wang 			    struct tun_file *tfile)
31096442e42SJason Wang {
31196442e42SJason Wang 	struct hlist_head *head;
31296442e42SJason Wang 	struct tun_flow_entry *e;
31396442e42SJason Wang 	unsigned long delay = tun->ageing_time;
3149e85722dSJason Wang 	u16 queue_index = tfile->queue_index;
31596442e42SJason Wang 
31696442e42SJason Wang 	if (!rxhash)
31796442e42SJason Wang 		return;
31896442e42SJason Wang 	else
31996442e42SJason Wang 		head = &tun->flows[tun_hashfn(rxhash)];
32096442e42SJason Wang 
32196442e42SJason Wang 	rcu_read_lock();
32296442e42SJason Wang 
3239e85722dSJason Wang 	/* We may get a very small possibility of OOO during switching, not
3249e85722dSJason Wang 	 * worth to optimize.*/
3259e85722dSJason Wang 	if (tun->numqueues == 1 || tfile->detached)
32696442e42SJason Wang 		goto unlock;
32796442e42SJason Wang 
32896442e42SJason Wang 	e = tun_flow_find(head, rxhash);
32996442e42SJason Wang 	if (likely(e)) {
33096442e42SJason Wang 		/* TODO: keep queueing to old queue until it's empty? */
33196442e42SJason Wang 		e->queue_index = queue_index;
33296442e42SJason Wang 		e->updated = jiffies;
3339bc88939STom Herbert 		sock_rps_record_flow_hash(e->rps_rxhash);
33496442e42SJason Wang 	} else {
33596442e42SJason Wang 		spin_lock_bh(&tun->lock);
336b8732fb7SJason Wang 		if (!tun_flow_find(head, rxhash) &&
337b8732fb7SJason Wang 		    tun->flow_count < MAX_TAP_FLOWS)
33896442e42SJason Wang 			tun_flow_create(tun, head, rxhash, queue_index);
33996442e42SJason Wang 
34096442e42SJason Wang 		if (!timer_pending(&tun->flow_gc_timer))
34196442e42SJason Wang 			mod_timer(&tun->flow_gc_timer,
34296442e42SJason Wang 				  round_jiffies_up(jiffies + delay));
34396442e42SJason Wang 		spin_unlock_bh(&tun->lock);
34496442e42SJason Wang 	}
34596442e42SJason Wang 
34696442e42SJason Wang unlock:
34796442e42SJason Wang 	rcu_read_unlock();
34896442e42SJason Wang }
34996442e42SJason Wang 
3509bc88939STom Herbert /**
3519bc88939STom Herbert  * Save the hash received in the stack receive path and update the
3529bc88939STom Herbert  * flow_hash table accordingly.
3539bc88939STom Herbert  */
3549bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
3559bc88939STom Herbert {
3569bc88939STom Herbert 	if (unlikely(e->rps_rxhash != hash)) {
3579bc88939STom Herbert 		sock_rps_reset_flow_hash(e->rps_rxhash);
3589bc88939STom Herbert 		e->rps_rxhash = hash;
3599bc88939STom Herbert 	}
3609bc88939STom Herbert }
3619bc88939STom Herbert 
362c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
36392d4ea6eSstephen hemminger  * we do not check rxq no. is because some cards(e.g 82599), chooses
364c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
365c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
366c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
367c8d68e6bSJason Wang  * hope the rxq no. may help here.
368c8d68e6bSJason Wang  */
369f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
37099932d4fSDaniel Borkmann 			    void *accel_priv, select_queue_fallback_t fallback)
371c8d68e6bSJason Wang {
372c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
37396442e42SJason Wang 	struct tun_flow_entry *e;
374c8d68e6bSJason Wang 	u32 txq = 0;
375c8d68e6bSJason Wang 	u32 numqueues = 0;
376c8d68e6bSJason Wang 
377c8d68e6bSJason Wang 	rcu_read_lock();
37892bb73eaSJason Wang 	numqueues = ACCESS_ONCE(tun->numqueues);
379c8d68e6bSJason Wang 
3803958afa1STom Herbert 	txq = skb_get_hash(skb);
381c8d68e6bSJason Wang 	if (txq) {
38296442e42SJason Wang 		e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
3839bc88939STom Herbert 		if (e) {
3849bc88939STom Herbert 			tun_flow_save_rps_rxhash(e, txq);
385fbe4d456SZhi Yong Wu 			txq = e->queue_index;
3869bc88939STom Herbert 		} else
387c8d68e6bSJason Wang 			/* use multiply and shift instead of expensive divide */
388c8d68e6bSJason Wang 			txq = ((u64)txq * numqueues) >> 32;
389c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
390c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
391c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
392c8d68e6bSJason Wang 			txq -= numqueues;
393c8d68e6bSJason Wang 	}
394c8d68e6bSJason Wang 
395c8d68e6bSJason Wang 	rcu_read_unlock();
396c8d68e6bSJason Wang 	return txq;
397c8d68e6bSJason Wang }
398c8d68e6bSJason Wang 
399cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
400cde8b15fSJason Wang {
401cde8b15fSJason Wang 	const struct cred *cred = current_cred();
402c260b772SEric W. Biederman 	struct net *net = dev_net(tun->dev);
403cde8b15fSJason Wang 
404cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
405cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
406c260b772SEric W. Biederman 		!ns_capable(net->user_ns, CAP_NET_ADMIN);
407cde8b15fSJason Wang }
408cde8b15fSJason Wang 
409c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
410c8d68e6bSJason Wang {
411c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
412c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
413c8d68e6bSJason Wang }
414c8d68e6bSJason Wang 
4154008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
4164008e97fSJason Wang {
4174008e97fSJason Wang 	tfile->detached = tun;
4184008e97fSJason Wang 	list_add_tail(&tfile->next, &tun->disabled);
4194008e97fSJason Wang 	++tun->numdisabled;
4204008e97fSJason Wang }
4214008e97fSJason Wang 
422d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
4234008e97fSJason Wang {
4244008e97fSJason Wang 	struct tun_struct *tun = tfile->detached;
4254008e97fSJason Wang 
4264008e97fSJason Wang 	tfile->detached = NULL;
4274008e97fSJason Wang 	list_del_init(&tfile->next);
4284008e97fSJason Wang 	--tun->numdisabled;
4294008e97fSJason Wang 	return tun;
4304008e97fSJason Wang }
4314008e97fSJason Wang 
4324bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile)
4334bfb0513SJason Wang {
4344bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_receive_queue);
4354bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_error_queue);
4364bfb0513SJason Wang }
4374bfb0513SJason Wang 
438c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
439c8d68e6bSJason Wang {
440c8d68e6bSJason Wang 	struct tun_file *ntfile;
441c8d68e6bSJason Wang 	struct tun_struct *tun;
442c8d68e6bSJason Wang 
443b8deabd3SJason Wang 	tun = rtnl_dereference(tfile->tun);
444b8deabd3SJason Wang 
4459e85722dSJason Wang 	if (tun && !tfile->detached) {
446c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
447c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
448c8d68e6bSJason Wang 
449c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
450c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
451b8deabd3SJason Wang 		ntfile = rtnl_dereference(tun->tfiles[index]);
452c8d68e6bSJason Wang 		ntfile->queue_index = index;
453c8d68e6bSJason Wang 
454c8d68e6bSJason Wang 		--tun->numqueues;
4559e85722dSJason Wang 		if (clean) {
456c956674bSMonam Agarwal 			RCU_INIT_POINTER(tfile->tun, NULL);
457c8d68e6bSJason Wang 			sock_put(&tfile->sk);
4589e85722dSJason Wang 		} else
4594008e97fSJason Wang 			tun_disable_queue(tun, tfile);
460c8d68e6bSJason Wang 
461c8d68e6bSJason Wang 		synchronize_net();
46296442e42SJason Wang 		tun_flow_delete_by_queue(tun, tun->numqueues + 1);
463c8d68e6bSJason Wang 		/* Drop read queue */
4644bfb0513SJason Wang 		tun_queue_purge(tfile);
465c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
466dd38bd85SJason Wang 	} else if (tfile->detached && clean) {
4674008e97fSJason Wang 		tun = tun_enable_queue(tfile);
468dd38bd85SJason Wang 		sock_put(&tfile->sk);
469dd38bd85SJason Wang 	}
470c8d68e6bSJason Wang 
471c8d68e6bSJason Wang 	if (clean) {
472af668b3cSMichael S. Tsirkin 		if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
473af668b3cSMichael S. Tsirkin 			netif_carrier_off(tun->dev);
474af668b3cSMichael S. Tsirkin 
475af668b3cSMichael S. Tsirkin 			if (!(tun->flags & TUN_PERSIST) &&
476af668b3cSMichael S. Tsirkin 			    tun->dev->reg_state == NETREG_REGISTERED)
4774008e97fSJason Wang 				unregister_netdevice(tun->dev);
478af668b3cSMichael S. Tsirkin 		}
4794008e97fSJason Wang 
480c8d68e6bSJason Wang 		BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
481c8d68e6bSJason Wang 				 &tfile->socket.flags));
482c8d68e6bSJason Wang 		sk_release_kernel(&tfile->sk);
483c8d68e6bSJason Wang 	}
484c8d68e6bSJason Wang }
485c8d68e6bSJason Wang 
486c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
487c8d68e6bSJason Wang {
488c8d68e6bSJason Wang 	rtnl_lock();
489c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
490c8d68e6bSJason Wang 	rtnl_unlock();
491c8d68e6bSJason Wang }
492c8d68e6bSJason Wang 
493c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
494c8d68e6bSJason Wang {
495c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
4964008e97fSJason Wang 	struct tun_file *tfile, *tmp;
497c8d68e6bSJason Wang 	int i, n = tun->numqueues;
498c8d68e6bSJason Wang 
499c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
500b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
501c8d68e6bSJason Wang 		BUG_ON(!tfile);
5029e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
503c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
504c8d68e6bSJason Wang 		--tun->numqueues;
505c8d68e6bSJason Wang 	}
5069e85722dSJason Wang 	list_for_each_entry(tfile, &tun->disabled, next) {
5079e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
508c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
5099e85722dSJason Wang 	}
510c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
511c8d68e6bSJason Wang 
512c8d68e6bSJason Wang 	synchronize_net();
513c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
514b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
515c8d68e6bSJason Wang 		/* Drop read queue */
5164bfb0513SJason Wang 		tun_queue_purge(tfile);
517c8d68e6bSJason Wang 		sock_put(&tfile->sk);
518c8d68e6bSJason Wang 	}
5194008e97fSJason Wang 	list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
5204008e97fSJason Wang 		tun_enable_queue(tfile);
5214bfb0513SJason Wang 		tun_queue_purge(tfile);
5224008e97fSJason Wang 		sock_put(&tfile->sk);
5234008e97fSJason Wang 	}
5244008e97fSJason Wang 	BUG_ON(tun->numdisabled != 0);
525dd38bd85SJason Wang 
526dd38bd85SJason Wang 	if (tun->flags & TUN_PERSIST)
527dd38bd85SJason Wang 		module_put(THIS_MODULE);
528c8d68e6bSJason Wang }
529c8d68e6bSJason Wang 
530849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
531a7385ba2SEric W. Biederman {
532631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
53338231b7aSEric W. Biederman 	int err;
534a7385ba2SEric W. Biederman 
5355dbbaf2dSPaul Moore 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
5365dbbaf2dSPaul Moore 	if (err < 0)
5375dbbaf2dSPaul Moore 		goto out;
5385dbbaf2dSPaul Moore 
53938231b7aSEric W. Biederman 	err = -EINVAL;
5409e85722dSJason Wang 	if (rtnl_dereference(tfile->tun) && !tfile->detached)
54138231b7aSEric W. Biederman 		goto out;
54238231b7aSEric W. Biederman 
54338231b7aSEric W. Biederman 	err = -EBUSY;
544c8d68e6bSJason Wang 	if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
545c8d68e6bSJason Wang 		goto out;
546c8d68e6bSJason Wang 
547c8d68e6bSJason Wang 	err = -E2BIG;
5484008e97fSJason Wang 	if (!tfile->detached &&
5494008e97fSJason Wang 	    tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
55038231b7aSEric W. Biederman 		goto out;
55138231b7aSEric W. Biederman 
55238231b7aSEric W. Biederman 	err = 0;
55354f968d6SJason Wang 
55492d4ea6eSstephen hemminger 	/* Re-attach the filter to persist device */
555849c9b6fSPavel Emelyanov 	if (!skip_filter && (tun->filter_attached == true)) {
55654f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
55754f968d6SJason Wang 		if (!err)
55854f968d6SJason Wang 			goto out;
55954f968d6SJason Wang 	}
560c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
5616e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
562c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
563c8d68e6bSJason Wang 	tun->numqueues++;
564c8d68e6bSJason Wang 
5654008e97fSJason Wang 	if (tfile->detached)
5664008e97fSJason Wang 		tun_enable_queue(tfile);
5674008e97fSJason Wang 	else
5684008e97fSJason Wang 		sock_hold(&tfile->sk);
5694008e97fSJason Wang 
570c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
571c8d68e6bSJason Wang 
572c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
573c8d68e6bSJason Wang 	 * refcnt.
574c8d68e6bSJason Wang 	 */
575a7385ba2SEric W. Biederman 
57638231b7aSEric W. Biederman out:
57738231b7aSEric W. Biederman 	return err;
578a7385ba2SEric W. Biederman }
579a7385ba2SEric W. Biederman 
580631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
581631ab46bSEric W. Biederman {
5826e914fc7SJason Wang 	struct tun_struct *tun;
583c70f1829SEric W. Biederman 
5846e914fc7SJason Wang 	rcu_read_lock();
5856e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
5866e914fc7SJason Wang 	if (tun)
5876e914fc7SJason Wang 		dev_hold(tun->dev);
5886e914fc7SJason Wang 	rcu_read_unlock();
589c70f1829SEric W. Biederman 
590c70f1829SEric W. Biederman 	return tun;
591631ab46bSEric W. Biederman }
592631ab46bSEric W. Biederman 
593631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
594631ab46bSEric W. Biederman {
595631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
596631ab46bSEric W. Biederman }
597631ab46bSEric W. Biederman 
598631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
599631ab46bSEric W. Biederman {
6006e914fc7SJason Wang 	dev_put(tun->dev);
601631ab46bSEric W. Biederman }
602631ab46bSEric W. Biederman 
6036b8a66eeSJoe Perches /* TAP filtering */
604f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
605f271b2ccSMax Krasnyansky {
606f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
607f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
608f271b2ccSMax Krasnyansky }
609f271b2ccSMax Krasnyansky 
610f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
611f271b2ccSMax Krasnyansky {
612f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
613f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
614f271b2ccSMax Krasnyansky }
615f271b2ccSMax Krasnyansky 
616f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
617f271b2ccSMax Krasnyansky {
618f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
619f271b2ccSMax Krasnyansky 	struct tun_filter uf;
620f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
621f271b2ccSMax Krasnyansky 
622f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
623f271b2ccSMax Krasnyansky 		return -EFAULT;
624f271b2ccSMax Krasnyansky 
625f271b2ccSMax Krasnyansky 	if (!uf.count) {
626f271b2ccSMax Krasnyansky 		/* Disabled */
627f271b2ccSMax Krasnyansky 		filter->count = 0;
628f271b2ccSMax Krasnyansky 		return 0;
629f271b2ccSMax Krasnyansky 	}
630f271b2ccSMax Krasnyansky 
631f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
632f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
633f271b2ccSMax Krasnyansky 	if (!addr)
634f271b2ccSMax Krasnyansky 		return -ENOMEM;
635f271b2ccSMax Krasnyansky 
636f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
637f271b2ccSMax Krasnyansky 		err = -EFAULT;
638f271b2ccSMax Krasnyansky 		goto done;
639f271b2ccSMax Krasnyansky 	}
640f271b2ccSMax Krasnyansky 
641f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
642f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
643f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
644f271b2ccSMax Krasnyansky 	filter->count = 0;
645f271b2ccSMax Krasnyansky 	wmb();
646f271b2ccSMax Krasnyansky 
647f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
648f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
649f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
650f271b2ccSMax Krasnyansky 
651f271b2ccSMax Krasnyansky 	nexact = n;
652f271b2ccSMax Krasnyansky 
653cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
654cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
655f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
656cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
657cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
658cfbf84fcSAlex Williamson 			err = 0; /* no filter */
659cfbf84fcSAlex Williamson 			goto done;
660cfbf84fcSAlex Williamson 		}
661f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
662cfbf84fcSAlex Williamson 	}
663f271b2ccSMax Krasnyansky 
664f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
665f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
666f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
667f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
668f271b2ccSMax Krasnyansky 
669f271b2ccSMax Krasnyansky 	/* Now enable the filter */
670f271b2ccSMax Krasnyansky 	wmb();
671f271b2ccSMax Krasnyansky 	filter->count = nexact;
672f271b2ccSMax Krasnyansky 
673f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
674f271b2ccSMax Krasnyansky 	err = nexact;
675f271b2ccSMax Krasnyansky 
676f271b2ccSMax Krasnyansky done:
677f271b2ccSMax Krasnyansky 	kfree(addr);
678f271b2ccSMax Krasnyansky 	return err;
679f271b2ccSMax Krasnyansky }
680f271b2ccSMax Krasnyansky 
681f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
682f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
683f271b2ccSMax Krasnyansky {
684f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
685f271b2ccSMax Krasnyansky 	 * at this point. */
686f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
687f271b2ccSMax Krasnyansky 	int i;
688f271b2ccSMax Krasnyansky 
689f271b2ccSMax Krasnyansky 	/* Exact match */
690f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
6912e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
692f271b2ccSMax Krasnyansky 			return 1;
693f271b2ccSMax Krasnyansky 
694f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
695f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
696f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
697f271b2ccSMax Krasnyansky 
698f271b2ccSMax Krasnyansky 	return 0;
699f271b2ccSMax Krasnyansky }
700f271b2ccSMax Krasnyansky 
701f271b2ccSMax Krasnyansky /*
702f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
703f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
704f271b2ccSMax Krasnyansky  */
705f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
706f271b2ccSMax Krasnyansky {
707f271b2ccSMax Krasnyansky 	if (!filter->count)
708f271b2ccSMax Krasnyansky 		return 1;
709f271b2ccSMax Krasnyansky 
710f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
711f271b2ccSMax Krasnyansky }
712f271b2ccSMax Krasnyansky 
7131da177e4SLinus Torvalds /* Network device part of the driver */
7141da177e4SLinus Torvalds 
7157282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
7161da177e4SLinus Torvalds 
717c70f1829SEric W. Biederman /* Net device detach from fd. */
718c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
719c70f1829SEric W. Biederman {
720c8d68e6bSJason Wang 	tun_detach_all(dev);
721c70f1829SEric W. Biederman }
722c70f1829SEric W. Biederman 
7231da177e4SLinus Torvalds /* Net device open. */
7241da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
7251da177e4SLinus Torvalds {
726c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
7271da177e4SLinus Torvalds 	return 0;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds /* Net device close. */
7311da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
7321da177e4SLinus Torvalds {
733c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
7341da177e4SLinus Torvalds 	return 0;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds /* Net device start xmit */
738424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
741c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
7426e914fc7SJason Wang 	struct tun_file *tfile;
743fa35864eSDominic Curran 	u32 numqueues = 0;
7441da177e4SLinus Torvalds 
7456e914fc7SJason Wang 	rcu_read_lock();
746c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
747fa35864eSDominic Curran 	numqueues = ACCESS_ONCE(tun->numqueues);
748c8d68e6bSJason Wang 
7491da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
750fa35864eSDominic Curran 	if (txq >= numqueues)
7511da177e4SLinus Torvalds 		goto drop;
7521da177e4SLinus Torvalds 
753fa35864eSDominic Curran 	if (numqueues == 1) {
7549bc88939STom Herbert 		/* Select queue was not called for the skbuff, so we extract the
7559bc88939STom Herbert 		 * RPS hash and save it into the flow_table here.
7569bc88939STom Herbert 		 */
7579bc88939STom Herbert 		__u32 rxhash;
7589bc88939STom Herbert 
7599bc88939STom Herbert 		rxhash = skb_get_hash(skb);
7609bc88939STom Herbert 		if (rxhash) {
7619bc88939STom Herbert 			struct tun_flow_entry *e;
7629bc88939STom Herbert 			e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
7639bc88939STom Herbert 					rxhash);
7649bc88939STom Herbert 			if (e)
7659bc88939STom Herbert 				tun_flow_save_rps_rxhash(e, rxhash);
7669bc88939STom Herbert 		}
7679bc88939STom Herbert 	}
7689bc88939STom Herbert 
7696e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
7706e914fc7SJason Wang 
771c8d68e6bSJason Wang 	BUG_ON(!tfile);
772c8d68e6bSJason Wang 
773f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
774f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
775f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
776f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
777f271b2ccSMax Krasnyansky 		goto drop;
778f271b2ccSMax Krasnyansky 
77954f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
78054f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
78199405162SMichael S. Tsirkin 		goto drop;
78299405162SMichael S. Tsirkin 
78336fe8c09SRami Rosen 	/* Limit the number of packets queued by dividing txq length with the
784c8d68e6bSJason Wang 	 * number of queues.
785c8d68e6bSJason Wang 	 */
786fa35864eSDominic Curran 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
787fa35864eSDominic Curran 			  >= dev->tx_queue_len)
7881da177e4SLinus Torvalds 		goto drop;
7891da177e4SLinus Torvalds 
7907bf66305SJason Wang 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
7917bf66305SJason Wang 		goto drop;
7927bf66305SJason Wang 
793eda29772SRichard Cochran 	if (skb->sk) {
794eda29772SRichard Cochran 		sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
795eda29772SRichard Cochran 		sw_tx_timestamp(skb);
796eda29772SRichard Cochran 	}
797eda29772SRichard Cochran 
7980110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
7997bf66305SJason Wang 	 * for indefinite time.
8007bf66305SJason Wang 	 */
8010110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
8020110d6f2SMichael S. Tsirkin 
803f8af75f3SEric Dumazet 	nf_reset(skb);
804f8af75f3SEric Dumazet 
805f271b2ccSMax Krasnyansky 	/* Enqueue packet */
80654f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	/* Notify and wake up reader process */
80954f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
81054f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
8119e641bdcSXi Wang 	tfile->socket.sk->sk_data_ready(tfile->socket.sk);
8126e914fc7SJason Wang 
8136e914fc7SJason Wang 	rcu_read_unlock();
8146ed10654SPatrick McHardy 	return NETDEV_TX_OK;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds drop:
81709f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
818149d36f7SMichael S. Tsirkin 	skb_tx_error(skb);
8191da177e4SLinus Torvalds 	kfree_skb(skb);
8206e914fc7SJason Wang 	rcu_read_unlock();
8216ed10654SPatrick McHardy 	return NETDEV_TX_OK;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds 
824f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
8251da177e4SLinus Torvalds {
826f271b2ccSMax Krasnyansky 	/*
827f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
828f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
829f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
830f271b2ccSMax Krasnyansky 	 */
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8334885a504SEd Swierk #define MIN_MTU 68
8344885a504SEd Swierk #define MAX_MTU 65535
8354885a504SEd Swierk 
8364885a504SEd Swierk static int
8374885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
8384885a504SEd Swierk {
8394885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
8404885a504SEd Swierk 		return -EINVAL;
8414885a504SEd Swierk 	dev->mtu = new_mtu;
8424885a504SEd Swierk 	return 0;
8434885a504SEd Swierk }
8444885a504SEd Swierk 
845c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
846c8f44affSMichał Mirosław 	netdev_features_t features)
84788255375SMichał Mirosław {
84888255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
84988255375SMichał Mirosław 
85088255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
85188255375SMichał Mirosław }
852bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
853bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
854bebd097aSNeil Horman {
855bebd097aSNeil Horman 	/*
856bebd097aSNeil Horman 	 * Tun only receives frames when:
857bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
858bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
85992d4ea6eSstephen hemminger 	 * Since both of those are synchronous operations, we are guaranteed
860bebd097aSNeil Horman 	 * never to have pending data when we poll for it
86192d4ea6eSstephen hemminger 	 * so there is nothing to do here but return.
862bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
863bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
864bebd097aSNeil Horman 	 * still use netconsole
865bebd097aSNeil Horman 	 */
866bebd097aSNeil Horman 	return;
867bebd097aSNeil Horman }
868bebd097aSNeil Horman #endif
869758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
870c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
871758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
872758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
87300829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
874758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
87588255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
876c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
877bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
878bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
879bebd097aSNeil Horman #endif
880758e43b7SStephen Hemminger };
881758e43b7SStephen Hemminger 
882758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
883c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
884758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
885758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
88600829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
887758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
88888255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
889afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
890758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
891758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
892c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
893bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
894bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
895bebd097aSNeil Horman #endif
896758e43b7SStephen Hemminger };
897758e43b7SStephen Hemminger 
898944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun)
89996442e42SJason Wang {
90096442e42SJason Wang 	int i;
90196442e42SJason Wang 
90296442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
90396442e42SJason Wang 		INIT_HLIST_HEAD(&tun->flows[i]);
90496442e42SJason Wang 
90596442e42SJason Wang 	tun->ageing_time = TUN_FLOW_EXPIRE;
90696442e42SJason Wang 	setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
90796442e42SJason Wang 	mod_timer(&tun->flow_gc_timer,
90896442e42SJason Wang 		  round_jiffies_up(jiffies + tun->ageing_time));
90996442e42SJason Wang }
91096442e42SJason Wang 
91196442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun)
91296442e42SJason Wang {
91396442e42SJason Wang 	del_timer_sync(&tun->flow_gc_timer);
91496442e42SJason Wang 	tun_flow_flush(tun);
91596442e42SJason Wang }
91696442e42SJason Wang 
9171da177e4SLinus Torvalds /* Initialize net device. */
9181da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
9231da177e4SLinus Torvalds 	case TUN_TUN_DEV:
924758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
925758e43b7SStephen Hemminger 
9261da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
9271da177e4SLinus Torvalds 		dev->hard_header_len = 0;
9281da177e4SLinus Torvalds 		dev->addr_len = 0;
9291da177e4SLinus Torvalds 		dev->mtu = 1500;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 		/* Zero header length */
9321da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
9331da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
9341da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
9351da177e4SLinus Torvalds 		break;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	case TUN_TAP_DEV:
9387a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
9391da177e4SLinus Torvalds 		/* Ethernet TAP Device */
9401da177e4SLinus Torvalds 		ether_setup(dev);
941550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
942a676847bSstephen hemminger 		dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
94336226a8dSBrian Braunstein 
944f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
94536226a8dSBrian Braunstein 
9461da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
9471da177e4SLinus Torvalds 		break;
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds /* Character device part */
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds /* Poll */
9541da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
9551da177e4SLinus Torvalds {
956b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
957b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
9583c8a9c63SMariusz Kozlowski 	struct sock *sk;
95933dccbb0SHerbert Xu 	unsigned int mask = 0;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	if (!tun)
962eac9e902SEric W. Biederman 		return POLLERR;
9631da177e4SLinus Torvalds 
96454f968d6SJason Wang 	sk = tfile->socket.sk;
9653c8a9c63SMariusz Kozlowski 
9666b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
9671da177e4SLinus Torvalds 
9689e641bdcSXi Wang 	poll_wait(file, sk_sleep(sk), wait);
9691da177e4SLinus Torvalds 
97089f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
9711da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
9721da177e4SLinus Torvalds 
97333dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
97433dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
97533dccbb0SHerbert Xu 	     sock_writeable(sk)))
97633dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
97733dccbb0SHerbert Xu 
978c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
979c70f1829SEric W. Biederman 		mask = POLLERR;
980c70f1829SEric W. Biederman 
981631ab46bSEric W. Biederman 	tun_put(tun);
9821da177e4SLinus Torvalds 	return mask;
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
985f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
986f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
98754f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
98833dccbb0SHerbert Xu 				     size_t prepad, size_t len,
98933dccbb0SHerbert Xu 				     size_t linear, int noblock)
990f42157cbSRusty Russell {
99154f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
992f42157cbSRusty Russell 	struct sk_buff *skb;
99333dccbb0SHerbert Xu 	int err;
994f42157cbSRusty Russell 
995f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
9960eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
99733dccbb0SHerbert Xu 		linear = len;
998f42157cbSRusty Russell 
99933dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
100028d64271SEric Dumazet 				   &err, 0);
1001f42157cbSRusty Russell 	if (!skb)
100233dccbb0SHerbert Xu 		return ERR_PTR(err);
1003f42157cbSRusty Russell 
1004f42157cbSRusty Russell 	skb_reserve(skb, prepad);
1005f42157cbSRusty Russell 	skb_put(skb, linear);
100633dccbb0SHerbert Xu 	skb->data_len = len - linear;
100733dccbb0SHerbert Xu 	skb->len += len - linear;
1008f42157cbSRusty Russell 
1009f42157cbSRusty Russell 	return skb;
1010f42157cbSRusty Russell }
1011f42157cbSRusty Russell 
10121da177e4SLinus Torvalds /* Get packet from user space buffer */
101354f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
101454f968d6SJason Wang 			    void *msg_control, const struct iovec *iv,
101554f968d6SJason Wang 			    size_t total_len, size_t count, int noblock)
10161da177e4SLinus Torvalds {
101709640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
10181da177e4SLinus Torvalds 	struct sk_buff *skb;
10193dd5c330SJason Wang 	size_t len = total_len, align = NET_SKB_PAD, linear;
1020f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
102196f8d9ecSJason Wang 	int good_linear;
10226f26c9a7SMichael S. Tsirkin 	int offset = 0;
10230690899bSMichael S. Tsirkin 	int copylen;
10240690899bSMichael S. Tsirkin 	bool zerocopy = false;
10250690899bSMichael S. Tsirkin 	int err;
102649974420SEric Dumazet 	u32 rxhash;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
102915718ea0SDan Carpenter 		if (len < sizeof(pi))
10301da177e4SLinus Torvalds 			return -EINVAL;
103115718ea0SDan Carpenter 		len -= sizeof(pi);
10321da177e4SLinus Torvalds 
10336f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
10341da177e4SLinus Torvalds 			return -EFAULT;
10356f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
1038f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
103915718ea0SDan Carpenter 		if (len < tun->vnet_hdr_sz)
1040f43798c2SRusty Russell 			return -EINVAL;
104115718ea0SDan Carpenter 		len -= tun->vnet_hdr_sz;
1042f43798c2SRusty Russell 
10436f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
1044f43798c2SRusty Russell 			return -EFAULT;
1045f43798c2SRusty Russell 
10464909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
10474909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
10484909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
10494909122fSHerbert Xu 
1050f43798c2SRusty Russell 		if (gso.hdr_len > len)
1051f43798c2SRusty Russell 			return -EINVAL;
1052d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
1053f43798c2SRusty Russell 	}
1054f43798c2SRusty Russell 
1055e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
1056a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
10570eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
10580eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
1059e01bf1c8SRusty Russell 			return -EINVAL;
1060e01bf1c8SRusty Russell 	}
10611da177e4SLinus Torvalds 
106296f8d9ecSJason Wang 	good_linear = SKB_MAX_HEAD(align);
106396f8d9ecSJason Wang 
106488529176SJason Wang 	if (msg_control) {
106588529176SJason Wang 		/* There are 256 bytes to be copied in skb, so there is
106688529176SJason Wang 		 * enough room for skb expand head in case it is used.
10670690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
10680690899bSMichael S. Tsirkin 		 */
106988529176SJason Wang 		copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN;
107096f8d9ecSJason Wang 		if (copylen > good_linear)
107196f8d9ecSJason Wang 			copylen = good_linear;
10723dd5c330SJason Wang 		linear = copylen;
107388529176SJason Wang 		if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
107488529176SJason Wang 			zerocopy = true;
107588529176SJason Wang 	}
107688529176SJason Wang 
107788529176SJason Wang 	if (!zerocopy) {
10780690899bSMichael S. Tsirkin 		copylen = len;
107996f8d9ecSJason Wang 		if (gso.hdr_len > good_linear)
108096f8d9ecSJason Wang 			linear = good_linear;
108196f8d9ecSJason Wang 		else
10823dd5c330SJason Wang 			linear = gso.hdr_len;
10833dd5c330SJason Wang 	}
10840690899bSMichael S. Tsirkin 
10853dd5c330SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
108633dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
108733dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
108809f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
108933dccbb0SHerbert Xu 		return PTR_ERR(skb);
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
10920690899bSMichael S. Tsirkin 	if (zerocopy)
10930690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
109488529176SJason Wang 	else {
10950690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
109688529176SJason Wang 		if (!err && msg_control) {
109788529176SJason Wang 			struct ubuf_info *uarg = msg_control;
109888529176SJason Wang 			uarg->callback(uarg, false);
109988529176SJason Wang 		}
110088529176SJason Wang 	}
11010690899bSMichael S. Tsirkin 
11020690899bSMichael S. Tsirkin 	if (err) {
110309f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
11048f22757eSDave Jones 		kfree_skb(skb);
11051da177e4SLinus Torvalds 		return -EFAULT;
11068f22757eSDave Jones 	}
11071da177e4SLinus Torvalds 
1108f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1109f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
1110f43798c2SRusty Russell 					  gso.csum_offset)) {
1111f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1112f43798c2SRusty Russell 			kfree_skb(skb);
1113f43798c2SRusty Russell 			return -EINVAL;
1114f43798c2SRusty Russell 		}
111588255375SMichał Mirosław 	}
1116f43798c2SRusty Russell 
11171da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
11181da177e4SLinus Torvalds 	case TUN_TUN_DEV:
1119f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
1120f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
1121f09f7ee2SAng Way Chuang 			case 0x40:
1122f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
1123f09f7ee2SAng Way Chuang 				break;
1124f09f7ee2SAng Way Chuang 			case 0x60:
1125f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
1126f09f7ee2SAng Way Chuang 				break;
1127f09f7ee2SAng Way Chuang 			default:
1128f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
1129f09f7ee2SAng Way Chuang 				kfree_skb(skb);
1130f09f7ee2SAng Way Chuang 				return -EINVAL;
1131f09f7ee2SAng Way Chuang 			}
1132f09f7ee2SAng Way Chuang 		}
1133f09f7ee2SAng Way Chuang 
1134459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
11351da177e4SLinus Torvalds 		skb->protocol = pi.proto;
11364c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
11371da177e4SLinus Torvalds 		break;
11381da177e4SLinus Torvalds 	case TUN_TAP_DEV:
11391da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
11401da177e4SLinus Torvalds 		break;
11416403eab1SJoe Perches 	}
11421da177e4SLinus Torvalds 
1143*5188cd44SBen Hutchings 	skb_reset_network_header(skb);
1144*5188cd44SBen Hutchings 
1145f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1146f43798c2SRusty Russell 		pr_debug("GSO!\n");
1147f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1148f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
1149c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1150f43798c2SRusty Russell 			break;
1151f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
1152c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1153f43798c2SRusty Russell 			break;
1154e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
11553d0ad094SBen Hutchings 		{
11563d0ad094SBen Hutchings 			static bool warned;
11573d0ad094SBen Hutchings 
11583d0ad094SBen Hutchings 			if (!warned) {
11593d0ad094SBen Hutchings 				warned = true;
11603d0ad094SBen Hutchings 				netdev_warn(tun->dev,
11613d0ad094SBen Hutchings 					    "%s: using disabled UFO feature; please fix this program\n",
11623d0ad094SBen Hutchings 					    current->comm);
11633d0ad094SBen Hutchings 			}
1164c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1165*5188cd44SBen Hutchings 			if (skb->protocol == htons(ETH_P_IPV6))
1166*5188cd44SBen Hutchings 				ipv6_proxy_select_ident(skb);
1167e36aa25aSSridhar Samudrala 			break;
11683d0ad094SBen Hutchings 		}
1169f43798c2SRusty Russell 		default:
1170f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1171f43798c2SRusty Russell 			kfree_skb(skb);
1172f43798c2SRusty Russell 			return -EINVAL;
1173f43798c2SRusty Russell 		}
1174f43798c2SRusty Russell 
1175f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1176c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1177f43798c2SRusty Russell 
1178f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
1179f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
1180f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1181f43798c2SRusty Russell 			kfree_skb(skb);
1182f43798c2SRusty Russell 			return -EINVAL;
1183f43798c2SRusty Russell 		}
1184f43798c2SRusty Russell 
1185f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
1186f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1187f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
1188f43798c2SRusty Russell 	}
11891da177e4SLinus Torvalds 
11900690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
11910690899bSMichael S. Tsirkin 	if (zerocopy) {
11920690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
11930690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1194c9af6db4SPravin B Shelar 		skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
11950690899bSMichael S. Tsirkin 	}
11960690899bSMichael S. Tsirkin 
119740893fd0SJason Wang 	skb_probe_transport_header(skb, 0);
119838502af7SJason Wang 
11993958afa1STom Herbert 	rxhash = skb_get_hash(skb);
12001da177e4SLinus Torvalds 	netif_rx_ni(skb);
12011da177e4SLinus Torvalds 
120209f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
120309f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
12041da177e4SLinus Torvalds 
12059e85722dSJason Wang 	tun_flow_update(tun, rxhash, tfile);
12060690899bSMichael S. Tsirkin 	return total_len;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
1209ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1210ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
12111da177e4SLinus Torvalds {
121233dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
1213ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
121454f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
1215631ab46bSEric W. Biederman 	ssize_t result;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	if (!tun)
12181da177e4SLinus Torvalds 		return -EBADFD;
12191da177e4SLinus Torvalds 
12206b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
12211da177e4SLinus Torvalds 
122254f968d6SJason Wang 	result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
122354f968d6SJason Wang 			      count, file->f_flags & O_NONBLOCK);
1224631ab46bSEric W. Biederman 
1225631ab46bSEric W. Biederman 	tun_put(tun);
1226631ab46bSEric W. Biederman 	return result;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds /* Put packet to the user space buffer */
12306f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
123154f968d6SJason Wang 			    struct tun_file *tfile,
12321da177e4SLinus Torvalds 			    struct sk_buff *skb,
123343b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
12341da177e4SLinus Torvalds {
12351da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
12361da177e4SLinus Torvalds 	ssize_t total = 0;
1237e6fd07c8SJason Wang 	int vlan_offset = 0, copied;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
12401da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
12411da177e4SLinus Torvalds 			return -EINVAL;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 		if (len < skb->len) {
12441da177e4SLinus Torvalds 			/* Packet will be striped */
12451da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 
124843b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
12491da177e4SLinus Torvalds 			return -EFAULT;
12501da177e4SLinus Torvalds 		total += sizeof(pi);
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 
1253f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
1254f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1255d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
1256f43798c2SRusty Russell 			return -EINVAL;
1257f43798c2SRusty Russell 
1258f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1259f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1260f43798c2SRusty Russell 
1261f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
1262f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
1263f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
1264f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1265f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1266f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1267f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1268ef3db4a5SMichael S. Tsirkin 			else {
12696b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1270ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
1271ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
1272ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
1273ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1274ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1275ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
1276ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
1277ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1278ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1279ef3db4a5SMichael S. Tsirkin 			}
1280f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1281f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1282f43798c2SRusty Russell 		} else
1283f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1284f43798c2SRusty Russell 
1285f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1286f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
128755508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
1288f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
128910a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
129010a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1291f43798c2SRusty Russell 		} /* else everything is zero */
1292f43798c2SRusty Russell 
129343b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
129443b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
1295f43798c2SRusty Russell 			return -EFAULT;
1296d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
1297f43798c2SRusty Russell 	}
1298f43798c2SRusty Russell 
1299e6fd07c8SJason Wang 	copied = total;
1300e6fd07c8SJason Wang 	total += skb->len;
13016680ec68SJason Wang 	if (!vlan_tx_tag_present(skb)) {
13021da177e4SLinus Torvalds 		len = min_t(int, skb->len, len);
13036680ec68SJason Wang 	} else {
13046680ec68SJason Wang 		int copy, ret;
13056680ec68SJason Wang 		struct {
13066680ec68SJason Wang 			__be16 h_vlan_proto;
13076680ec68SJason Wang 			__be16 h_vlan_TCI;
13086680ec68SJason Wang 		} veth;
13091da177e4SLinus Torvalds 
13106680ec68SJason Wang 		veth.h_vlan_proto = skb->vlan_proto;
13116680ec68SJason Wang 		veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
13121da177e4SLinus Torvalds 
13136680ec68SJason Wang 		vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
13146680ec68SJason Wang 		len = min_t(int, skb->len + VLAN_HLEN, len);
1315e6fd07c8SJason Wang 		total += VLAN_HLEN;
13166680ec68SJason Wang 
13176680ec68SJason Wang 		copy = min_t(int, vlan_offset, len);
1318e6fd07c8SJason Wang 		ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
13196680ec68SJason Wang 		len -= copy;
1320e6fd07c8SJason Wang 		copied += copy;
13216680ec68SJason Wang 		if (ret || !len)
13226680ec68SJason Wang 			goto done;
13236680ec68SJason Wang 
13246680ec68SJason Wang 		copy = min_t(int, sizeof(veth), len);
1325e6fd07c8SJason Wang 		ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
13266680ec68SJason Wang 		len -= copy;
1327e6fd07c8SJason Wang 		copied += copy;
13286680ec68SJason Wang 		if (ret || !len)
13296680ec68SJason Wang 			goto done;
13306680ec68SJason Wang 	}
13316680ec68SJason Wang 
1332e6fd07c8SJason Wang 	skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
13336680ec68SJason Wang 
13346680ec68SJason Wang done:
133509f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
133609f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	return total;
13391da177e4SLinus Torvalds }
13401da177e4SLinus Torvalds 
134154f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
1342f96eb74cSZhi Yong Wu 			   const struct iovec *iv, ssize_t len, int noblock)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	struct sk_buff *skb;
134505c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
13469e641bdcSXi Wang 	int peeked, err, off = 0;
13471da177e4SLinus Torvalds 
13483872baf6SRami Rosen 	tun_debug(KERN_INFO, tun, "tun_do_read\n");
13491da177e4SLinus Torvalds 
13509e641bdcSXi Wang 	if (!len)
13519e641bdcSXi Wang 		return ret;
13521da177e4SLinus Torvalds 
13539e641bdcSXi Wang 	if (tun->dev->reg_state != NETREG_REGISTERED)
13549e641bdcSXi Wang 		return -EIO;
13551da177e4SLinus Torvalds 
13569e641bdcSXi Wang 	/* Read frames from queue */
13579e641bdcSXi Wang 	skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
13589e641bdcSXi Wang 				  &peeked, &off, &err);
13599e641bdcSXi Wang 	if (skb) {
136054f968d6SJason Wang 		ret = tun_put_user(tun, tfile, skb, iv, len);
13611da177e4SLinus Torvalds 		kfree_skb(skb);
13629e641bdcSXi Wang 	} else
13639e641bdcSXi Wang 		ret = err;
13641da177e4SLinus Torvalds 
136505c2828cSMichael S. Tsirkin 	return ret;
136605c2828cSMichael S. Tsirkin }
136705c2828cSMichael S. Tsirkin 
136805c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
136905c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
137005c2828cSMichael S. Tsirkin {
137105c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
137205c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
137305c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
137405c2828cSMichael S. Tsirkin 	ssize_t len, ret;
137505c2828cSMichael S. Tsirkin 
137605c2828cSMichael S. Tsirkin 	if (!tun)
137705c2828cSMichael S. Tsirkin 		return -EBADFD;
137805c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
137905c2828cSMichael S. Tsirkin 	if (len < 0) {
138005c2828cSMichael S. Tsirkin 		ret = -EINVAL;
138105c2828cSMichael S. Tsirkin 		goto out;
138205c2828cSMichael S. Tsirkin 	}
138305c2828cSMichael S. Tsirkin 
1384f96eb74cSZhi Yong Wu 	ret = tun_do_read(tun, tfile, iv, len,
138554f968d6SJason Wang 			  file->f_flags & O_NONBLOCK);
138642404c09SDavid S. Miller 	ret = min_t(ssize_t, ret, len);
1387d0b7da8aSZhi Yong Wu 	if (ret > 0)
1388d0b7da8aSZhi Yong Wu 		iocb->ki_pos = ret;
1389631ab46bSEric W. Biederman out:
1390631ab46bSEric W. Biederman 	tun_put(tun);
13911da177e4SLinus Torvalds 	return ret;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
139496442e42SJason Wang static void tun_free_netdev(struct net_device *dev)
139596442e42SJason Wang {
139696442e42SJason Wang 	struct tun_struct *tun = netdev_priv(dev);
139796442e42SJason Wang 
13984008e97fSJason Wang 	BUG_ON(!(list_empty(&tun->disabled)));
139996442e42SJason Wang 	tun_flow_uninit(tun);
14005dbbaf2dSPaul Moore 	security_tun_dev_free_security(tun->security);
140196442e42SJason Wang 	free_netdev(dev);
140296442e42SJason Wang }
140396442e42SJason Wang 
14041da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
14051da177e4SLinus Torvalds {
14061da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
14071da177e4SLinus Torvalds 
14080625c883SEric W. Biederman 	tun->owner = INVALID_UID;
14090625c883SEric W. Biederman 	tun->group = INVALID_GID;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
141296442e42SJason Wang 	dev->destructor = tun_free_netdev;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
1415f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1416f019a7a5SEric W. Biederman  * device with netlink.
1417f019a7a5SEric W. Biederman  */
1418f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1419f019a7a5SEric W. Biederman {
1420f019a7a5SEric W. Biederman 	return -EINVAL;
1421f019a7a5SEric W. Biederman }
1422f019a7a5SEric W. Biederman 
1423f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1424f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1425f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1426f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1427f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1428f019a7a5SEric W. Biederman };
1429f019a7a5SEric W. Biederman 
143033dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
143133dccbb0SHerbert Xu {
143254f968d6SJason Wang 	struct tun_file *tfile;
143343815482SEric Dumazet 	wait_queue_head_t *wqueue;
143433dccbb0SHerbert Xu 
143533dccbb0SHerbert Xu 	if (!sock_writeable(sk))
143633dccbb0SHerbert Xu 		return;
143733dccbb0SHerbert Xu 
143833dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
143933dccbb0SHerbert Xu 		return;
144033dccbb0SHerbert Xu 
144143815482SEric Dumazet 	wqueue = sk_sleep(sk);
144243815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
144343815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
144405c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1445c722c625SHerbert Xu 
144654f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
144754f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
144833dccbb0SHerbert Xu }
144933dccbb0SHerbert Xu 
145005c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
145105c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
145205c2828cSMichael S. Tsirkin {
145354f968d6SJason Wang 	int ret;
145454f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
145554f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
145654f968d6SJason Wang 
145754f968d6SJason Wang 	if (!tun)
145854f968d6SJason Wang 		return -EBADFD;
145954f968d6SJason Wang 	ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
14600690899bSMichael S. Tsirkin 			   m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
146154f968d6SJason Wang 	tun_put(tun);
146254f968d6SJason Wang 	return ret;
146305c2828cSMichael S. Tsirkin }
146405c2828cSMichael S. Tsirkin 
146505c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
146605c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
146705c2828cSMichael S. Tsirkin 		       int flags)
146805c2828cSMichael S. Tsirkin {
146954f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
147054f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
147105c2828cSMichael S. Tsirkin 	int ret;
147254f968d6SJason Wang 
147354f968d6SJason Wang 	if (!tun)
147454f968d6SJason Wang 		return -EBADFD;
147554f968d6SJason Wang 
1476eda29772SRichard Cochran 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
14773811ae76SGao feng 		ret = -EINVAL;
14783811ae76SGao feng 		goto out;
14793811ae76SGao feng 	}
1480eda29772SRichard Cochran 	if (flags & MSG_ERRQUEUE) {
1481eda29772SRichard Cochran 		ret = sock_recv_errqueue(sock->sk, m, total_len,
1482eda29772SRichard Cochran 					 SOL_PACKET, TUN_TX_TIMESTAMP);
1483eda29772SRichard Cochran 		goto out;
1484eda29772SRichard Cochran 	}
1485f96eb74cSZhi Yong Wu 	ret = tun_do_read(tun, tfile, m->msg_iov, total_len,
148605c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
148742404c09SDavid S. Miller 	if (ret > total_len) {
148842404c09SDavid S. Miller 		m->msg_flags |= MSG_TRUNC;
148942404c09SDavid S. Miller 		ret = flags & MSG_TRUNC ? ret : total_len;
149042404c09SDavid S. Miller 	}
14913811ae76SGao feng out:
149254f968d6SJason Wang 	tun_put(tun);
149305c2828cSMichael S. Tsirkin 	return ret;
149405c2828cSMichael S. Tsirkin }
149505c2828cSMichael S. Tsirkin 
14961ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
14971ab5ecb9SStanislav Kinsbursky {
14981ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
14991ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
15001ab5ecb9SStanislav Kinsbursky 	return 0;
15011ab5ecb9SStanislav Kinsbursky }
15021ab5ecb9SStanislav Kinsbursky 
150305c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
150405c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
150505c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
150605c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
15071ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
150805c2828cSMichael S. Tsirkin };
150905c2828cSMichael S. Tsirkin 
151033dccbb0SHerbert Xu static struct proto tun_proto = {
151133dccbb0SHerbert Xu 	.name		= "tun",
151233dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
151354f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
151433dccbb0SHerbert Xu };
1515f019a7a5SEric W. Biederman 
1516980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1517980c9e8cSDavid Woodhouse {
1518980c9e8cSDavid Woodhouse 	int flags = 0;
1519980c9e8cSDavid Woodhouse 
1520980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1521980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1522980c9e8cSDavid Woodhouse 	else
1523980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1524980c9e8cSDavid Woodhouse 
1525980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1526980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1527980c9e8cSDavid Woodhouse 
15285d097109SMichael S. Tsirkin 	/* This flag has no real effect.  We track the value for backwards
15295d097109SMichael S. Tsirkin 	 * compatibility.
15305d097109SMichael S. Tsirkin 	 */
1531980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1532980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1533980c9e8cSDavid Woodhouse 
1534980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1535980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1536980c9e8cSDavid Woodhouse 
1537c8d68e6bSJason Wang 	if (tun->flags & TUN_TAP_MQ)
1538c8d68e6bSJason Wang 		flags |= IFF_MULTI_QUEUE;
1539c8d68e6bSJason Wang 
1540274038f8SPavel Emelyanov 	if (tun->flags & TUN_PERSIST)
1541274038f8SPavel Emelyanov 		flags |= IFF_PERSIST;
1542274038f8SPavel Emelyanov 
1543980c9e8cSDavid Woodhouse 	return flags;
1544980c9e8cSDavid Woodhouse }
1545980c9e8cSDavid Woodhouse 
1546980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1547980c9e8cSDavid Woodhouse 			      char *buf)
1548980c9e8cSDavid Woodhouse {
1549980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1550980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1551980c9e8cSDavid Woodhouse }
1552980c9e8cSDavid Woodhouse 
1553980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1554980c9e8cSDavid Woodhouse 			      char *buf)
1555980c9e8cSDavid Woodhouse {
1556980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15570625c883SEric W. Biederman 	return uid_valid(tun->owner)?
15580625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15590625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
15600625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1561980c9e8cSDavid Woodhouse }
1562980c9e8cSDavid Woodhouse 
1563980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1564980c9e8cSDavid Woodhouse 			      char *buf)
1565980c9e8cSDavid Woodhouse {
1566980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15670625c883SEric W. Biederman 	return gid_valid(tun->group) ?
15680625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15690625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
15700625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1571980c9e8cSDavid Woodhouse }
1572980c9e8cSDavid Woodhouse 
1573980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1574980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1575980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1576980c9e8cSDavid Woodhouse 
1577d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	struct tun_struct *tun;
158054f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
15811da177e4SLinus Torvalds 	struct net_device *dev;
15821da177e4SLinus Torvalds 	int err;
15831da177e4SLinus Torvalds 
15847c0c3b1aSJason Wang 	if (tfile->detached)
15857c0c3b1aSJason Wang 		return -EINVAL;
15867c0c3b1aSJason Wang 
158774a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
158874a3e5a7SEric W. Biederman 	if (dev) {
1589f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1590f85ba780SDavid Woodhouse 			return -EBUSY;
159174a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
159274a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
159374a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
159474a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
159574a3e5a7SEric W. Biederman 		else
159674a3e5a7SEric W. Biederman 			return -EINVAL;
159774a3e5a7SEric W. Biederman 
15988e6d91aeSJason Wang 		if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
15998e6d91aeSJason Wang 		    !!(tun->flags & TUN_TAP_MQ))
16008e6d91aeSJason Wang 			return -EINVAL;
16018e6d91aeSJason Wang 
1602cde8b15fSJason Wang 		if (tun_not_capable(tun))
16032b980dbdSPaul Moore 			return -EPERM;
16045dbbaf2dSPaul Moore 		err = security_tun_dev_open(tun->security);
16052b980dbdSPaul Moore 		if (err < 0)
16062b980dbdSPaul Moore 			return err;
16072b980dbdSPaul Moore 
1608849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
1609a7385ba2SEric W. Biederman 		if (err < 0)
1610a7385ba2SEric W. Biederman 			return err;
16114008e97fSJason Wang 
16124008e97fSJason Wang 		if (tun->flags & TUN_TAP_MQ &&
1613e8dbad66SJason Wang 		    (tun->numqueues + tun->numdisabled > 1)) {
1614e8dbad66SJason Wang 			/* One or more queue has already been attached, no need
1615e8dbad66SJason Wang 			 * to initialize the device again.
1616e8dbad66SJason Wang 			 */
1617e8dbad66SJason Wang 			return 0;
1618e8dbad66SJason Wang 		}
161986a264abSDavid Howells 	}
16201da177e4SLinus Torvalds 	else {
16211da177e4SLinus Torvalds 		char *name;
16221da177e4SLinus Torvalds 		unsigned long flags = 0;
1623edfb6a14SJason Wang 		int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1624edfb6a14SJason Wang 			     MAX_TAP_QUEUES : 1;
16251da177e4SLinus Torvalds 
1626c260b772SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1627ca6bb5d7SDavid Woodhouse 			return -EPERM;
16282b980dbdSPaul Moore 		err = security_tun_dev_create();
16292b980dbdSPaul Moore 		if (err < 0)
16302b980dbdSPaul Moore 			return err;
1631ca6bb5d7SDavid Woodhouse 
16321da177e4SLinus Torvalds 		/* Set dev type */
16331da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
16341da177e4SLinus Torvalds 			/* TUN device */
16351da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
16361da177e4SLinus Torvalds 			name = "tun%d";
16371da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
16381da177e4SLinus Torvalds 			/* TAP device */
16391da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
16401da177e4SLinus Torvalds 			name = "tap%d";
16411da177e4SLinus Torvalds 		} else
164236989b90SKusanagi Kouichi 			return -EINVAL;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 		if (*ifr->ifr_name)
16451da177e4SLinus Torvalds 			name = ifr->ifr_name;
16461da177e4SLinus Torvalds 
1647c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1648c835a677STom Gundersen 				       NET_NAME_UNKNOWN, tun_setup, queues,
1649c835a677STom Gundersen 				       queues);
1650edfb6a14SJason Wang 
16511da177e4SLinus Torvalds 		if (!dev)
16521da177e4SLinus Torvalds 			return -ENOMEM;
16531da177e4SLinus Torvalds 
1654fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1655f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1656fb7589a1SPavel Emelyanov 		dev->ifindex = tfile->ifindex;
1657758e43b7SStephen Hemminger 
16581da177e4SLinus Torvalds 		tun = netdev_priv(dev);
16591da177e4SLinus Torvalds 		tun->dev = dev;
16601da177e4SLinus Torvalds 		tun->flags = flags;
1661f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1662d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
16631da177e4SLinus Torvalds 
166454f968d6SJason Wang 		tun->filter_attached = false;
166554f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
166633dccbb0SHerbert Xu 
166796442e42SJason Wang 		spin_lock_init(&tun->lock);
166896442e42SJason Wang 
16695dbbaf2dSPaul Moore 		err = security_tun_dev_alloc_security(&tun->security);
16705dbbaf2dSPaul Moore 		if (err < 0)
16715dbbaf2dSPaul Moore 			goto err_free_dev;
16722b980dbdSPaul Moore 
16731da177e4SLinus Torvalds 		tun_net_init(dev);
1674944a1376SPavel Emelyanov 		tun_flow_init(tun);
167596442e42SJason Wang 
167688255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
16776680ec68SJason Wang 				   TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
16786680ec68SJason Wang 				   NETIF_F_HW_VLAN_STAG_TX;
167988255375SMichał Mirosław 		dev->features = dev->hw_features;
16806671b224SFernando Luis Vazquez Cao 		dev->vlan_features = dev->features &
16816671b224SFernando Luis Vazquez Cao 				     ~(NETIF_F_HW_VLAN_CTAG_TX |
16826671b224SFernando Luis Vazquez Cao 				       NETIF_F_HW_VLAN_STAG_TX);
168388255375SMichał Mirosław 
16844008e97fSJason Wang 		INIT_LIST_HEAD(&tun->disabled);
1685849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, false);
1686eb0fb363SJason Wang 		if (err < 0)
1687662ca437SJason Wang 			goto err_free_flow;
1688eb0fb363SJason Wang 
16891da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
16901da177e4SLinus Torvalds 		if (err < 0)
1691662ca437SJason Wang 			goto err_detach;
16929c3fea6aSHerbert Xu 
1693980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1694980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1695980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
16966b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1697af668b3cSMichael S. Tsirkin 	}
1698980c9e8cSDavid Woodhouse 
1699eb0fb363SJason Wang 	netif_carrier_on(tun->dev);
17001da177e4SLinus Torvalds 
17016b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
17041da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1705a26af1e0SNathaniel Filardo 	else
1706a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
17071da177e4SLinus Torvalds 
17085d097109SMichael S. Tsirkin 	/* This flag has no real effect.  We track the value for backwards
17095d097109SMichael S. Tsirkin 	 * compatibility.
17105d097109SMichael S. Tsirkin 	 */
17111da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
17121da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1713a26af1e0SNathaniel Filardo 	else
1714a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
17151da177e4SLinus Torvalds 
1716f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1717f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1718f43798c2SRusty Russell 	else
1719f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1720f43798c2SRusty Russell 
1721c8d68e6bSJason Wang 	if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1722c8d68e6bSJason Wang 		tun->flags |= TUN_TAP_MQ;
1723c8d68e6bSJason Wang 	else
1724c8d68e6bSJason Wang 		tun->flags &= ~TUN_TAP_MQ;
1725c8d68e6bSJason Wang 
1726e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1727e35259a9SMax Krasnyansky 	 * xoff state.
1728e35259a9SMax Krasnyansky 	 */
1729e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1730c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1731e35259a9SMax Krasnyansky 
17321da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
17331da177e4SLinus Torvalds 	return 0;
17341da177e4SLinus Torvalds 
1735662ca437SJason Wang err_detach:
1736662ca437SJason Wang 	tun_detach_all(dev);
1737662ca437SJason Wang err_free_flow:
1738662ca437SJason Wang 	tun_flow_uninit(tun);
1739662ca437SJason Wang 	security_tun_dev_free_security(tun->security);
17401da177e4SLinus Torvalds err_free_dev:
17411da177e4SLinus Torvalds 	free_netdev(dev);
17421da177e4SLinus Torvalds 	return err;
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
17459ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun,
1746876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1747e3b99556SMark McLoughlin {
17486b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1749e3b99556SMark McLoughlin 
1750e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1751e3b99556SMark McLoughlin 
1752980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1753e3b99556SMark McLoughlin 
1754e3b99556SMark McLoughlin }
1755e3b99556SMark McLoughlin 
17565228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
17575228ddc9SRusty Russell  * privs required. */
175888255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
17595228ddc9SRusty Russell {
1760c8f44affSMichał Mirosław 	netdev_features_t features = 0;
17615228ddc9SRusty Russell 
17625228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
176388255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
17645228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
17655228ddc9SRusty Russell 
17665228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
17675228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
17685228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
17695228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
17705228ddc9SRusty Russell 			}
17715228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
17725228ddc9SRusty Russell 				features |= NETIF_F_TSO;
17735228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
17745228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
17755228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
17765228ddc9SRusty Russell 		}
17775228ddc9SRusty Russell 	}
17785228ddc9SRusty Russell 
17795228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
17805228ddc9SRusty Russell 	 * trying to set them. */
17815228ddc9SRusty Russell 	if (arg)
17825228ddc9SRusty Russell 		return -EINVAL;
17835228ddc9SRusty Russell 
178488255375SMichał Mirosław 	tun->set_features = features;
178588255375SMichał Mirosław 	netdev_update_features(tun->dev);
17865228ddc9SRusty Russell 
17875228ddc9SRusty Russell 	return 0;
17885228ddc9SRusty Russell }
17895228ddc9SRusty Russell 
1790c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1791c8d68e6bSJason Wang {
1792c8d68e6bSJason Wang 	int i;
1793c8d68e6bSJason Wang 	struct tun_file *tfile;
1794c8d68e6bSJason Wang 
1795c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1796b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1797c8d68e6bSJason Wang 		sk_detach_filter(tfile->socket.sk);
1798c8d68e6bSJason Wang 	}
1799c8d68e6bSJason Wang 
1800c8d68e6bSJason Wang 	tun->filter_attached = false;
1801c8d68e6bSJason Wang }
1802c8d68e6bSJason Wang 
1803c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1804c8d68e6bSJason Wang {
1805c8d68e6bSJason Wang 	int i, ret = 0;
1806c8d68e6bSJason Wang 	struct tun_file *tfile;
1807c8d68e6bSJason Wang 
1808c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1809b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1810c8d68e6bSJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1811c8d68e6bSJason Wang 		if (ret) {
1812c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1813c8d68e6bSJason Wang 			return ret;
1814c8d68e6bSJason Wang 		}
1815c8d68e6bSJason Wang 	}
1816c8d68e6bSJason Wang 
1817c8d68e6bSJason Wang 	tun->filter_attached = true;
1818c8d68e6bSJason Wang 	return ret;
1819c8d68e6bSJason Wang }
1820c8d68e6bSJason Wang 
1821c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1822c8d68e6bSJason Wang {
1823c8d68e6bSJason Wang 	struct tun_file *tfile;
1824c8d68e6bSJason Wang 	int i;
1825c8d68e6bSJason Wang 
1826c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1827b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1828c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1829c8d68e6bSJason Wang 	}
1830c8d68e6bSJason Wang }
1831c8d68e6bSJason Wang 
1832cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1833cde8b15fSJason Wang {
1834cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1835cde8b15fSJason Wang 	struct tun_struct *tun;
1836cde8b15fSJason Wang 	int ret = 0;
1837cde8b15fSJason Wang 
1838cde8b15fSJason Wang 	rtnl_lock();
1839cde8b15fSJason Wang 
1840cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
18414008e97fSJason Wang 		tun = tfile->detached;
18425dbbaf2dSPaul Moore 		if (!tun) {
1843cde8b15fSJason Wang 			ret = -EINVAL;
18445dbbaf2dSPaul Moore 			goto unlock;
18455dbbaf2dSPaul Moore 		}
18465dbbaf2dSPaul Moore 		ret = security_tun_dev_attach_queue(tun->security);
18475dbbaf2dSPaul Moore 		if (ret < 0)
18485dbbaf2dSPaul Moore 			goto unlock;
1849849c9b6fSPavel Emelyanov 		ret = tun_attach(tun, file, false);
18504008e97fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
1851b8deabd3SJason Wang 		tun = rtnl_dereference(tfile->tun);
18529e85722dSJason Wang 		if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached)
18534008e97fSJason Wang 			ret = -EINVAL;
1854cde8b15fSJason Wang 		else
18554008e97fSJason Wang 			__tun_detach(tfile, false);
18564008e97fSJason Wang 	} else
1857cde8b15fSJason Wang 		ret = -EINVAL;
1858cde8b15fSJason Wang 
18595dbbaf2dSPaul Moore unlock:
1860cde8b15fSJason Wang 	rtnl_unlock();
1861cde8b15fSJason Wang 	return ret;
1862cde8b15fSJason Wang }
1863cde8b15fSJason Wang 
186450857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
186550857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
18661da177e4SLinus Torvalds {
186736b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1868631ab46bSEric W. Biederman 	struct tun_struct *tun;
18691da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
18701da177e4SLinus Torvalds 	struct ifreq ifr;
18710625c883SEric W. Biederman 	kuid_t owner;
18720625c883SEric W. Biederman 	kgid_t group;
187333dccbb0SHerbert Xu 	int sndbuf;
1874d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1875fb7589a1SPavel Emelyanov 	unsigned int ifindex;
1876f271b2ccSMax Krasnyansky 	int ret;
18771da177e4SLinus Torvalds 
1878cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
187950857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
18801da177e4SLinus Torvalds 			return -EFAULT;
18818bbb1813SDavid S. Miller 	} else {
1882a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
18838bbb1813SDavid S. Miller 	}
1884631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1885631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1886631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1887631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1888631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1889cde8b15fSJason Wang 				IFF_VNET_HDR | IFF_MULTI_QUEUE,
1890631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1891cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
1892cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
1893631ab46bSEric W. Biederman 
1894c8d68e6bSJason Wang 	ret = 0;
1895876bfd4dSHerbert Xu 	rtnl_lock();
1896876bfd4dSHerbert Xu 
189736b50babSEric W. Biederman 	tun = __tun_get(tfile);
18981da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
18991da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
19001da177e4SLinus Torvalds 
1901876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
19021da177e4SLinus Torvalds 
1903876bfd4dSHerbert Xu 		if (ret)
1904876bfd4dSHerbert Xu 			goto unlock;
19051da177e4SLinus Torvalds 
190650857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1907876bfd4dSHerbert Xu 			ret = -EFAULT;
1908876bfd4dSHerbert Xu 		goto unlock;
19091da177e4SLinus Torvalds 	}
1910fb7589a1SPavel Emelyanov 	if (cmd == TUNSETIFINDEX) {
1911fb7589a1SPavel Emelyanov 		ret = -EPERM;
1912fb7589a1SPavel Emelyanov 		if (tun)
1913fb7589a1SPavel Emelyanov 			goto unlock;
1914fb7589a1SPavel Emelyanov 
1915fb7589a1SPavel Emelyanov 		ret = -EFAULT;
1916fb7589a1SPavel Emelyanov 		if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1917fb7589a1SPavel Emelyanov 			goto unlock;
1918fb7589a1SPavel Emelyanov 
1919fb7589a1SPavel Emelyanov 		ret = 0;
1920fb7589a1SPavel Emelyanov 		tfile->ifindex = ifindex;
1921fb7589a1SPavel Emelyanov 		goto unlock;
1922fb7589a1SPavel Emelyanov 	}
19231da177e4SLinus Torvalds 
1924876bfd4dSHerbert Xu 	ret = -EBADFD;
19251da177e4SLinus Torvalds 	if (!tun)
1926876bfd4dSHerbert Xu 		goto unlock;
19271da177e4SLinus Torvalds 
19281e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
19291da177e4SLinus Torvalds 
1930631ab46bSEric W. Biederman 	ret = 0;
19311da177e4SLinus Torvalds 	switch (cmd) {
1932e3b99556SMark McLoughlin 	case TUNGETIFF:
19339ce99cf6SRami Rosen 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1934e3b99556SMark McLoughlin 
19353d407a80SPavel Emelyanov 		if (tfile->detached)
19363d407a80SPavel Emelyanov 			ifr.ifr_flags |= IFF_DETACH_QUEUE;
1937849c9b6fSPavel Emelyanov 		if (!tfile->socket.sk->sk_filter)
1938849c9b6fSPavel Emelyanov 			ifr.ifr_flags |= IFF_NOFILTER;
19393d407a80SPavel Emelyanov 
194050857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1941631ab46bSEric W. Biederman 			ret = -EFAULT;
1942e3b99556SMark McLoughlin 		break;
1943e3b99556SMark McLoughlin 
19441da177e4SLinus Torvalds 	case TUNSETNOCSUM:
19451da177e4SLinus Torvalds 		/* Disable/Enable checksum */
19461da177e4SLinus Torvalds 
194788255375SMichał Mirosław 		/* [unimplemented] */
194888255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
19496b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
19501da177e4SLinus Torvalds 		break;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	case TUNSETPERSIST:
195354f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
195454f968d6SJason Wang 		 * module to prevent the module being unprobed.
195554f968d6SJason Wang 		 */
1956dd38bd85SJason Wang 		if (arg && !(tun->flags & TUN_PERSIST)) {
19571da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
195854f968d6SJason Wang 			__module_get(THIS_MODULE);
1959dd38bd85SJason Wang 		}
1960dd38bd85SJason Wang 		if (!arg && (tun->flags & TUN_PERSIST)) {
19611da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
196254f968d6SJason Wang 			module_put(THIS_MODULE);
196354f968d6SJason Wang 		}
19641da177e4SLinus Torvalds 
19656b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
19666b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
19671da177e4SLinus Torvalds 		break;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	case TUNSETOWNER:
19701da177e4SLinus Torvalds 		/* Set owner of the device */
19710625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
19720625c883SEric W. Biederman 		if (!uid_valid(owner)) {
19730625c883SEric W. Biederman 			ret = -EINVAL;
19740625c883SEric W. Biederman 			break;
19750625c883SEric W. Biederman 		}
19760625c883SEric W. Biederman 		tun->owner = owner;
19771e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
19780625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
19791da177e4SLinus Torvalds 		break;
19801da177e4SLinus Torvalds 
19818c644623SGuido Guenther 	case TUNSETGROUP:
19828c644623SGuido Guenther 		/* Set group of the device */
19830625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
19840625c883SEric W. Biederman 		if (!gid_valid(group)) {
19850625c883SEric W. Biederman 			ret = -EINVAL;
19860625c883SEric W. Biederman 			break;
19870625c883SEric W. Biederman 		}
19880625c883SEric W. Biederman 		tun->group = group;
19891e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
19900625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
19918c644623SGuido Guenther 		break;
19928c644623SGuido Guenther 
1993ff4cc3acSMike Kershaw 	case TUNSETLINK:
1994ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1995ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
19966b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
19976b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
199848abfe05SDavid S. Miller 			ret = -EBUSY;
1999ff4cc3acSMike Kershaw 		} else {
2000ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
20016b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
20026b8a66eeSJoe Perches 				  tun->dev->type);
200348abfe05SDavid S. Miller 			ret = 0;
2004ff4cc3acSMike Kershaw 		}
2005631ab46bSEric W. Biederman 		break;
2006ff4cc3acSMike Kershaw 
20071da177e4SLinus Torvalds #ifdef TUN_DEBUG
20081da177e4SLinus Torvalds 	case TUNSETDEBUG:
20091da177e4SLinus Torvalds 		tun->debug = arg;
20101da177e4SLinus Torvalds 		break;
20111da177e4SLinus Torvalds #endif
20125228ddc9SRusty Russell 	case TUNSETOFFLOAD:
201388255375SMichał Mirosław 		ret = set_offload(tun, arg);
2014631ab46bSEric W. Biederman 		break;
20155228ddc9SRusty Russell 
2016f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
2017f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
2018631ab46bSEric W. Biederman 		ret = -EINVAL;
2019f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2020631ab46bSEric W. Biederman 			break;
2021c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
2022631ab46bSEric W. Biederman 		break;
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
2025b595076aSUwe Kleine-König 		/* Get hw address */
2026f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2027f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
202850857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
2029631ab46bSEric W. Biederman 			ret = -EFAULT;
2030631ab46bSEric W. Biederman 		break;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
2033f271b2ccSMax Krasnyansky 		/* Set hw address */
20346b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
20356b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
203640102371SKim B. Heino 
203740102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
2038631ab46bSEric W. Biederman 		break;
203933dccbb0SHerbert Xu 
204033dccbb0SHerbert Xu 	case TUNGETSNDBUF:
204154f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
204233dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
204333dccbb0SHerbert Xu 			ret = -EFAULT;
204433dccbb0SHerbert Xu 		break;
204533dccbb0SHerbert Xu 
204633dccbb0SHerbert Xu 	case TUNSETSNDBUF:
204733dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
204833dccbb0SHerbert Xu 			ret = -EFAULT;
204933dccbb0SHerbert Xu 			break;
205033dccbb0SHerbert Xu 		}
205133dccbb0SHerbert Xu 
2052c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
2053c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
205433dccbb0SHerbert Xu 		break;
205533dccbb0SHerbert Xu 
2056d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
2057d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
2058d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2059d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2060d9d52b51SMichael S. Tsirkin 		break;
2061d9d52b51SMichael S. Tsirkin 
2062d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
2063d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2064d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2065d9d52b51SMichael S. Tsirkin 			break;
2066d9d52b51SMichael S. Tsirkin 		}
2067d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2068d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
2069d9d52b51SMichael S. Tsirkin 			break;
2070d9d52b51SMichael S. Tsirkin 		}
2071d9d52b51SMichael S. Tsirkin 
2072d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
2073d9d52b51SMichael S. Tsirkin 		break;
2074d9d52b51SMichael S. Tsirkin 
207599405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
207699405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
207799405162SMichael S. Tsirkin 		ret = -EINVAL;
207899405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
207999405162SMichael S. Tsirkin 			break;
208099405162SMichael S. Tsirkin 		ret = -EFAULT;
208154f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
208299405162SMichael S. Tsirkin 			break;
208399405162SMichael S. Tsirkin 
2084c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
208599405162SMichael S. Tsirkin 		break;
208699405162SMichael S. Tsirkin 
208799405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
208899405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
208999405162SMichael S. Tsirkin 		ret = -EINVAL;
209099405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
209199405162SMichael S. Tsirkin 			break;
2092c8d68e6bSJason Wang 		ret = 0;
2093c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
209499405162SMichael S. Tsirkin 		break;
209599405162SMichael S. Tsirkin 
209676975e9cSPavel Emelyanov 	case TUNGETFILTER:
209776975e9cSPavel Emelyanov 		ret = -EINVAL;
209876975e9cSPavel Emelyanov 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
209976975e9cSPavel Emelyanov 			break;
210076975e9cSPavel Emelyanov 		ret = -EFAULT;
210176975e9cSPavel Emelyanov 		if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
210276975e9cSPavel Emelyanov 			break;
210376975e9cSPavel Emelyanov 		ret = 0;
210476975e9cSPavel Emelyanov 		break;
210576975e9cSPavel Emelyanov 
21061da177e4SLinus Torvalds 	default:
2107631ab46bSEric W. Biederman 		ret = -EINVAL;
2108631ab46bSEric W. Biederman 		break;
2109ee289b64SJoe Perches 	}
21101da177e4SLinus Torvalds 
2111876bfd4dSHerbert Xu unlock:
2112876bfd4dSHerbert Xu 	rtnl_unlock();
2113876bfd4dSHerbert Xu 	if (tun)
2114631ab46bSEric W. Biederman 		tun_put(tun);
2115631ab46bSEric W. Biederman 	return ret;
21161da177e4SLinus Torvalds }
21171da177e4SLinus Torvalds 
211850857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
211950857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
212050857e2aSArnd Bergmann {
212150857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
212250857e2aSArnd Bergmann }
212350857e2aSArnd Bergmann 
212450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
212550857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
212650857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
212750857e2aSArnd Bergmann {
212850857e2aSArnd Bergmann 	switch (cmd) {
212950857e2aSArnd Bergmann 	case TUNSETIFF:
213050857e2aSArnd Bergmann 	case TUNGETIFF:
213150857e2aSArnd Bergmann 	case TUNSETTXFILTER:
213250857e2aSArnd Bergmann 	case TUNGETSNDBUF:
213350857e2aSArnd Bergmann 	case TUNSETSNDBUF:
213450857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
213550857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
213650857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
213750857e2aSArnd Bergmann 		break;
213850857e2aSArnd Bergmann 	default:
213950857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
214050857e2aSArnd Bergmann 		break;
214150857e2aSArnd Bergmann 	}
214250857e2aSArnd Bergmann 
214350857e2aSArnd Bergmann 	/*
214450857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
214550857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
214650857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
214750857e2aSArnd Bergmann 	 * contents.
214850857e2aSArnd Bergmann 	 */
214950857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
215050857e2aSArnd Bergmann }
215150857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
215250857e2aSArnd Bergmann 
21531da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
21541da177e4SLinus Torvalds {
215554f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
21561da177e4SLinus Torvalds 	int ret;
21571da177e4SLinus Torvalds 
215854f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
21599d319522SJonathan Corbet 		goto out;
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	if (on) {
2162e0b93eddSJeff Layton 		__f_setown(file, task_pid(current), PIDTYPE_PID, 0);
216354f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
21641da177e4SLinus Torvalds 	} else
216554f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
21669d319522SJonathan Corbet 	ret = 0;
21679d319522SJonathan Corbet out:
21689d319522SJonathan Corbet 	return ret;
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
21721da177e4SLinus Torvalds {
2173631ab46bSEric W. Biederman 	struct tun_file *tfile;
2174deed49fbSThomas Gleixner 
21756b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2176631ab46bSEric W. Biederman 
217754f968d6SJason Wang 	tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
217854f968d6SJason Wang 					    &tun_proto);
2179631ab46bSEric W. Biederman 	if (!tfile)
2180631ab46bSEric W. Biederman 		return -ENOMEM;
2181c956674bSMonam Agarwal 	RCU_INIT_POINTER(tfile->tun, NULL);
218236b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
218354f968d6SJason Wang 	tfile->flags = 0;
2184fb7589a1SPavel Emelyanov 	tfile->ifindex = 0;
218554f968d6SJason Wang 
218654f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
21879e641bdcSXi Wang 	RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
218854f968d6SJason Wang 
218954f968d6SJason Wang 	tfile->socket.file = file;
219054f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
219154f968d6SJason Wang 
219254f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
219354f968d6SJason Wang 	sk_change_net(&tfile->sk, tfile->net);
219454f968d6SJason Wang 
219554f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
219654f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
219754f968d6SJason Wang 
2198631ab46bSEric W. Biederman 	file->private_data = tfile;
219954f968d6SJason Wang 	set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
22004008e97fSJason Wang 	INIT_LIST_HEAD(&tfile->next);
220154f968d6SJason Wang 
220219a6afb2SJason Wang 	sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
220319a6afb2SJason Wang 
22041da177e4SLinus Torvalds 	return 0;
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
22081da177e4SLinus Torvalds {
2209631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
221054f968d6SJason Wang 	struct net *net = tfile->net;
22111da177e4SLinus Torvalds 
2212c8d68e6bSJason Wang 	tun_detach(tfile, true);
221354f968d6SJason Wang 	put_net(net);
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 	return 0;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
221893e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
221993e14b6dSMasatake YAMATO static int tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
222093e14b6dSMasatake YAMATO {
222193e14b6dSMasatake YAMATO 	struct tun_struct *tun;
222293e14b6dSMasatake YAMATO 	struct ifreq ifr;
222393e14b6dSMasatake YAMATO 
222493e14b6dSMasatake YAMATO 	memset(&ifr, 0, sizeof(ifr));
222593e14b6dSMasatake YAMATO 
222693e14b6dSMasatake YAMATO 	rtnl_lock();
222793e14b6dSMasatake YAMATO 	tun = tun_get(f);
222893e14b6dSMasatake YAMATO 	if (tun)
222993e14b6dSMasatake YAMATO 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
223093e14b6dSMasatake YAMATO 	rtnl_unlock();
223193e14b6dSMasatake YAMATO 
223293e14b6dSMasatake YAMATO 	if (tun)
223393e14b6dSMasatake YAMATO 		tun_put(tun);
223493e14b6dSMasatake YAMATO 
223593e14b6dSMasatake YAMATO 	return seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
223693e14b6dSMasatake YAMATO }
223793e14b6dSMasatake YAMATO #endif
223893e14b6dSMasatake YAMATO 
2239d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
22401da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
22411da177e4SLinus Torvalds 	.llseek = no_llseek,
2242ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
2243ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
2244ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
2245ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
22461da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
2247876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
224850857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
224950857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
225050857e2aSArnd Bergmann #endif
22511da177e4SLinus Torvalds 	.open	= tun_chr_open,
22521da177e4SLinus Torvalds 	.release = tun_chr_close,
225393e14b6dSMasatake YAMATO 	.fasync = tun_chr_fasync,
225493e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
225593e14b6dSMasatake YAMATO 	.show_fdinfo = tun_chr_show_fdinfo,
225693e14b6dSMasatake YAMATO #endif
22571da177e4SLinus Torvalds };
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
22601da177e4SLinus Torvalds 	.minor = TUN_MINOR,
22611da177e4SLinus Torvalds 	.name = "tun",
2262e454cea2SKay Sievers 	.nodename = "net/tun",
22631da177e4SLinus Torvalds 	.fops = &tun_fops,
22641da177e4SLinus Torvalds };
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds /* ethtool interface */
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
22691da177e4SLinus Torvalds {
22701da177e4SLinus Torvalds 	cmd->supported		= 0;
22711da177e4SLinus Torvalds 	cmd->advertising	= 0;
227270739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
22731da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
22741da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
22751da177e4SLinus Torvalds 	cmd->phy_address	= 0;
22761da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
22771da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
22781da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
22791da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
22801da177e4SLinus Torvalds 	return 0;
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
22841da177e4SLinus Torvalds {
22851da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22861da177e4SLinus Torvalds 
228733a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
228833a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
22911da177e4SLinus Torvalds 	case TUN_TUN_DEV:
229233a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
22931da177e4SLinus Torvalds 		break;
22941da177e4SLinus Torvalds 	case TUN_TAP_DEV:
229533a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
22961da177e4SLinus Torvalds 		break;
22971da177e4SLinus Torvalds 	}
22981da177e4SLinus Torvalds }
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
23011da177e4SLinus Torvalds {
23021da177e4SLinus Torvalds #ifdef TUN_DEBUG
23031da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
23041da177e4SLinus Torvalds 	return tun->debug;
23051da177e4SLinus Torvalds #else
23061da177e4SLinus Torvalds 	return -EOPNOTSUPP;
23071da177e4SLinus Torvalds #endif
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
23111da177e4SLinus Torvalds {
23121da177e4SLinus Torvalds #ifdef TUN_DEBUG
23131da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
23141da177e4SLinus Torvalds 	tun->debug = value;
23151da177e4SLinus Torvalds #endif
23161da177e4SLinus Torvalds }
23171da177e4SLinus Torvalds 
23187282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
23191da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
23201da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
23211da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
23221da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
2323bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
2324eda29772SRichard Cochran 	.get_ts_info	= ethtool_op_get_ts_info,
23251da177e4SLinus Torvalds };
23261da177e4SLinus Torvalds 
232779d17604SPavel Emelyanov 
23281da177e4SLinus Torvalds static int __init tun_init(void)
23291da177e4SLinus Torvalds {
23301da177e4SLinus Torvalds 	int ret = 0;
23311da177e4SLinus Torvalds 
23326b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
23336b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
23341da177e4SLinus Torvalds 
2335f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
233679d17604SPavel Emelyanov 	if (ret) {
23376b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
2338f019a7a5SEric W. Biederman 		goto err_linkops;
233979d17604SPavel Emelyanov 	}
234079d17604SPavel Emelyanov 
23411da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
234279d17604SPavel Emelyanov 	if (ret) {
23436b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
234479d17604SPavel Emelyanov 		goto err_misc;
234579d17604SPavel Emelyanov 	}
234679d17604SPavel Emelyanov 	return  0;
234779d17604SPavel Emelyanov err_misc:
2348f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2349f019a7a5SEric W. Biederman err_linkops:
23501da177e4SLinus Torvalds 	return ret;
23511da177e4SLinus Torvalds }
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds static void tun_cleanup(void)
23541da177e4SLinus Torvalds {
23551da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2356f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds 
235905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
236005c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
236105c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
236205c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
236305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
236405c2828cSMichael S. Tsirkin {
23656e914fc7SJason Wang 	struct tun_file *tfile;
236605c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
236705c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
23686e914fc7SJason Wang 	tfile = file->private_data;
23696e914fc7SJason Wang 	if (!tfile)
237005c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
237154f968d6SJason Wang 	return &tfile->socket;
237205c2828cSMichael S. Tsirkin }
237305c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
237405c2828cSMichael S. Tsirkin 
23751da177e4SLinus Torvalds module_init(tun_init);
23761da177e4SLinus Torvalds module_exit(tun_cleanup);
23771da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
23781da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
23791da177e4SLinus Torvalds MODULE_LICENSE("GPL");
23801da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2381578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2382