xref: /openbmc/linux/drivers/net/tun.c (revision 72f6510745592c87f612f62ae4f16bb002934df4)
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>
68881d966bSEric W. Biederman #include <net/net_namespace.h>
6979d17604SPavel Emelyanov #include <net/netns/generic.h>
70f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
7133dccbb0SHerbert Xu #include <net/sock.h>
7293e14b6dSMasatake YAMATO #include <linux/seq_file.h>
73e0b46d0eSHerbert Xu #include <linux/uio.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 
106031f5e03SMichael S. Tsirkin /* TUN device flags */
107031f5e03SMichael S. Tsirkin 
108031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags,
109031f5e03SMichael S. Tsirkin  * overload it to mean fasync when stored there.
110031f5e03SMichael S. Tsirkin  */
111031f5e03SMichael S. Tsirkin #define TUN_FASYNC	IFF_ATTACH_QUEUE
1121cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */
1131cf8e410SMichael S. Tsirkin #define TUN_VNET_LE     0x80000000
114031f5e03SMichael S. Tsirkin 
115031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
1161cf8e410SMichael S. Tsirkin 		      IFF_MULTI_QUEUE)
1170690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128
1180690899bSMichael S. Tsirkin 
119f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
120f271b2ccSMax Krasnyansky struct tap_filter {
121f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
122f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
123f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
124f271b2ccSMax Krasnyansky };
125f271b2ccSMax Krasnyansky 
12692d4ea6eSstephen hemminger /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for
127edfb6a14SJason Wang  * the netdevice to be fit in one page. So we can make sure the success of
128edfb6a14SJason Wang  * memory allocation. TODO: increase the limit. */
129edfb6a14SJason Wang #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
130b8732fb7SJason Wang #define MAX_TAP_FLOWS  4096
131c8d68e6bSJason Wang 
13296442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ)
13396442e42SJason Wang 
13454f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
13592d4ea6eSstephen hemminger  * also contains all socket related structures (except sock_fprog and tap_filter)
13654f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
13754f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
13836fe8c09SRami Rosen  * netdevice not for a specific queue (at least I didn't see the requirement for
13954f968d6SJason Wang  * this).
1406e914fc7SJason Wang  *
1416e914fc7SJason Wang  * RCU usage:
14236fe8c09SRami Rosen  * The tun_file and tun_struct are loosely coupled, the pointer from one to the
1436e914fc7SJason Wang  * other can only be read while rcu_read_lock or rtnl_lock is held.
14454f968d6SJason Wang  */
145631ab46bSEric W. Biederman struct tun_file {
14654f968d6SJason Wang 	struct sock sk;
14754f968d6SJason Wang 	struct socket socket;
14854f968d6SJason Wang 	struct socket_wq wq;
1496e914fc7SJason Wang 	struct tun_struct __rcu *tun;
15036b50babSEric W. Biederman 	struct net *net;
15154f968d6SJason Wang 	struct fasync_struct *fasync;
15254f968d6SJason Wang 	/* only used for fasnyc */
15354f968d6SJason Wang 	unsigned int flags;
154fb7589a1SPavel Emelyanov 	union {
155c8d68e6bSJason Wang 		u16 queue_index;
156fb7589a1SPavel Emelyanov 		unsigned int ifindex;
157fb7589a1SPavel Emelyanov 	};
1584008e97fSJason Wang 	struct list_head next;
1594008e97fSJason Wang 	struct tun_struct *detached;
160631ab46bSEric W. Biederman };
161631ab46bSEric W. Biederman 
16296442e42SJason Wang struct tun_flow_entry {
16396442e42SJason Wang 	struct hlist_node hash_link;
16496442e42SJason Wang 	struct rcu_head rcu;
16596442e42SJason Wang 	struct tun_struct *tun;
16696442e42SJason Wang 
16796442e42SJason Wang 	u32 rxhash;
1689bc88939STom Herbert 	u32 rps_rxhash;
16996442e42SJason Wang 	int queue_index;
17096442e42SJason Wang 	unsigned long updated;
17196442e42SJason Wang };
17296442e42SJason Wang 
17396442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024
17496442e42SJason Wang 
17554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
17636fe8c09SRami Rosen  * device, socket filter, sndbuf and vnet header size were restore when the
17754f968d6SJason Wang  * file were attached to a persist device.
17854f968d6SJason Wang  */
17914daa021SRusty Russell struct tun_struct {
180c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
181c8d68e6bSJason Wang 	unsigned int            numqueues;
182f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1830625c883SEric W. Biederman 	kuid_t			owner;
1840625c883SEric W. Biederman 	kgid_t			group;
18514daa021SRusty Russell 
18614daa021SRusty Russell 	struct net_device	*dev;
187c8f44affSMichał Mirosław 	netdev_features_t	set_features;
18888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
1893d0ad094SBen Hutchings 			  NETIF_F_TSO6)
190d9d52b51SMichael S. Tsirkin 
191d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
19254f968d6SJason Wang 	int			sndbuf;
19354f968d6SJason Wang 	struct tap_filter	txflt;
19454f968d6SJason Wang 	struct sock_fprog	fprog;
19554f968d6SJason Wang 	/* protected by rtnl lock */
19654f968d6SJason Wang 	bool			filter_attached;
19714daa021SRusty Russell #ifdef TUN_DEBUG
19814daa021SRusty Russell 	int debug;
19914daa021SRusty Russell #endif
20096442e42SJason Wang 	spinlock_t lock;
20196442e42SJason Wang 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
20296442e42SJason Wang 	struct timer_list flow_gc_timer;
20396442e42SJason Wang 	unsigned long ageing_time;
2044008e97fSJason Wang 	unsigned int numdisabled;
2054008e97fSJason Wang 	struct list_head disabled;
2065dbbaf2dSPaul Moore 	void *security;
207b8732fb7SJason Wang 	u32 flow_count;
20814daa021SRusty Russell };
20914daa021SRusty Russell 
21056f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
21156f0dcc5SMichael S. Tsirkin {
2121cf8e410SMichael S. Tsirkin 	return __virtio16_to_cpu(tun->flags & TUN_VNET_LE, val);
21356f0dcc5SMichael S. Tsirkin }
21456f0dcc5SMichael S. Tsirkin 
21556f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
21656f0dcc5SMichael S. Tsirkin {
2171cf8e410SMichael S. Tsirkin 	return __cpu_to_virtio16(tun->flags & TUN_VNET_LE, val);
21856f0dcc5SMichael S. Tsirkin }
21956f0dcc5SMichael S. Tsirkin 
22096442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash)
22196442e42SJason Wang {
22296442e42SJason Wang 	return rxhash & 0x3ff;
22396442e42SJason Wang }
22496442e42SJason Wang 
22596442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
22696442e42SJason Wang {
22796442e42SJason Wang 	struct tun_flow_entry *e;
22896442e42SJason Wang 
229b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(e, head, hash_link) {
23096442e42SJason Wang 		if (e->rxhash == rxhash)
23196442e42SJason Wang 			return e;
23296442e42SJason Wang 	}
23396442e42SJason Wang 	return NULL;
23496442e42SJason Wang }
23596442e42SJason Wang 
23696442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
23796442e42SJason Wang 					      struct hlist_head *head,
23896442e42SJason Wang 					      u32 rxhash, u16 queue_index)
23996442e42SJason Wang {
2409fdc6befSEric Dumazet 	struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
2419fdc6befSEric Dumazet 
24296442e42SJason Wang 	if (e) {
24396442e42SJason Wang 		tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
24496442e42SJason Wang 			  rxhash, queue_index);
24596442e42SJason Wang 		e->updated = jiffies;
24696442e42SJason Wang 		e->rxhash = rxhash;
2479bc88939STom Herbert 		e->rps_rxhash = 0;
24896442e42SJason Wang 		e->queue_index = queue_index;
24996442e42SJason Wang 		e->tun = tun;
25096442e42SJason Wang 		hlist_add_head_rcu(&e->hash_link, head);
251b8732fb7SJason Wang 		++tun->flow_count;
25296442e42SJason Wang 	}
25396442e42SJason Wang 	return e;
25496442e42SJason Wang }
25596442e42SJason Wang 
25696442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
25796442e42SJason Wang {
25896442e42SJason Wang 	tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
25996442e42SJason Wang 		  e->rxhash, e->queue_index);
2609bc88939STom Herbert 	sock_rps_reset_flow_hash(e->rps_rxhash);
26196442e42SJason Wang 	hlist_del_rcu(&e->hash_link);
2629fdc6befSEric Dumazet 	kfree_rcu(e, rcu);
263b8732fb7SJason Wang 	--tun->flow_count;
26496442e42SJason Wang }
26596442e42SJason Wang 
26696442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun)
26796442e42SJason Wang {
26896442e42SJason Wang 	int i;
26996442e42SJason Wang 
27096442e42SJason Wang 	spin_lock_bh(&tun->lock);
27196442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
27296442e42SJason Wang 		struct tun_flow_entry *e;
273b67bfe0dSSasha Levin 		struct hlist_node *n;
27496442e42SJason Wang 
275b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
27696442e42SJason Wang 			tun_flow_delete(tun, e);
27796442e42SJason Wang 	}
27896442e42SJason Wang 	spin_unlock_bh(&tun->lock);
27996442e42SJason Wang }
28096442e42SJason Wang 
28196442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
28296442e42SJason Wang {
28396442e42SJason Wang 	int i;
28496442e42SJason Wang 
28596442e42SJason Wang 	spin_lock_bh(&tun->lock);
28696442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
28796442e42SJason Wang 		struct tun_flow_entry *e;
288b67bfe0dSSasha Levin 		struct hlist_node *n;
28996442e42SJason Wang 
290b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
29196442e42SJason Wang 			if (e->queue_index == queue_index)
29296442e42SJason Wang 				tun_flow_delete(tun, e);
29396442e42SJason Wang 		}
29496442e42SJason Wang 	}
29596442e42SJason Wang 	spin_unlock_bh(&tun->lock);
29696442e42SJason Wang }
29796442e42SJason Wang 
29896442e42SJason Wang static void tun_flow_cleanup(unsigned long data)
29996442e42SJason Wang {
30096442e42SJason Wang 	struct tun_struct *tun = (struct tun_struct *)data;
30196442e42SJason Wang 	unsigned long delay = tun->ageing_time;
30296442e42SJason Wang 	unsigned long next_timer = jiffies + delay;
30396442e42SJason Wang 	unsigned long count = 0;
30496442e42SJason Wang 	int i;
30596442e42SJason Wang 
30696442e42SJason Wang 	tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
30796442e42SJason Wang 
30896442e42SJason Wang 	spin_lock_bh(&tun->lock);
30996442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
31096442e42SJason Wang 		struct tun_flow_entry *e;
311b67bfe0dSSasha Levin 		struct hlist_node *n;
31296442e42SJason Wang 
313b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
31496442e42SJason Wang 			unsigned long this_timer;
31596442e42SJason Wang 			count++;
31696442e42SJason Wang 			this_timer = e->updated + delay;
31796442e42SJason Wang 			if (time_before_eq(this_timer, jiffies))
31896442e42SJason Wang 				tun_flow_delete(tun, e);
31996442e42SJason Wang 			else if (time_before(this_timer, next_timer))
32096442e42SJason Wang 				next_timer = this_timer;
32196442e42SJason Wang 		}
32296442e42SJason Wang 	}
32396442e42SJason Wang 
32496442e42SJason Wang 	if (count)
32596442e42SJason Wang 		mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
32696442e42SJason Wang 	spin_unlock_bh(&tun->lock);
32796442e42SJason Wang }
32896442e42SJason Wang 
32949974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
3309e85722dSJason Wang 			    struct tun_file *tfile)
33196442e42SJason Wang {
33296442e42SJason Wang 	struct hlist_head *head;
33396442e42SJason Wang 	struct tun_flow_entry *e;
33496442e42SJason Wang 	unsigned long delay = tun->ageing_time;
3359e85722dSJason Wang 	u16 queue_index = tfile->queue_index;
33696442e42SJason Wang 
33796442e42SJason Wang 	if (!rxhash)
33896442e42SJason Wang 		return;
33996442e42SJason Wang 	else
34096442e42SJason Wang 		head = &tun->flows[tun_hashfn(rxhash)];
34196442e42SJason Wang 
34296442e42SJason Wang 	rcu_read_lock();
34396442e42SJason Wang 
3449e85722dSJason Wang 	/* We may get a very small possibility of OOO during switching, not
3459e85722dSJason Wang 	 * worth to optimize.*/
3469e85722dSJason Wang 	if (tun->numqueues == 1 || tfile->detached)
34796442e42SJason Wang 		goto unlock;
34896442e42SJason Wang 
34996442e42SJason Wang 	e = tun_flow_find(head, rxhash);
35096442e42SJason Wang 	if (likely(e)) {
35196442e42SJason Wang 		/* TODO: keep queueing to old queue until it's empty? */
35296442e42SJason Wang 		e->queue_index = queue_index;
35396442e42SJason Wang 		e->updated = jiffies;
3549bc88939STom Herbert 		sock_rps_record_flow_hash(e->rps_rxhash);
35596442e42SJason Wang 	} else {
35696442e42SJason Wang 		spin_lock_bh(&tun->lock);
357b8732fb7SJason Wang 		if (!tun_flow_find(head, rxhash) &&
358b8732fb7SJason Wang 		    tun->flow_count < MAX_TAP_FLOWS)
35996442e42SJason Wang 			tun_flow_create(tun, head, rxhash, queue_index);
36096442e42SJason Wang 
36196442e42SJason Wang 		if (!timer_pending(&tun->flow_gc_timer))
36296442e42SJason Wang 			mod_timer(&tun->flow_gc_timer,
36396442e42SJason Wang 				  round_jiffies_up(jiffies + delay));
36496442e42SJason Wang 		spin_unlock_bh(&tun->lock);
36596442e42SJason Wang 	}
36696442e42SJason Wang 
36796442e42SJason Wang unlock:
36896442e42SJason Wang 	rcu_read_unlock();
36996442e42SJason Wang }
37096442e42SJason Wang 
3719bc88939STom Herbert /**
3729bc88939STom Herbert  * Save the hash received in the stack receive path and update the
3739bc88939STom Herbert  * flow_hash table accordingly.
3749bc88939STom Herbert  */
3759bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
3769bc88939STom Herbert {
3779bc88939STom Herbert 	if (unlikely(e->rps_rxhash != hash)) {
3789bc88939STom Herbert 		sock_rps_reset_flow_hash(e->rps_rxhash);
3799bc88939STom Herbert 		e->rps_rxhash = hash;
3809bc88939STom Herbert 	}
3819bc88939STom Herbert }
3829bc88939STom Herbert 
383c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
38492d4ea6eSstephen hemminger  * we do not check rxq no. is because some cards(e.g 82599), chooses
385c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
386c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
387c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
388c8d68e6bSJason Wang  * hope the rxq no. may help here.
389c8d68e6bSJason Wang  */
390f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
39199932d4fSDaniel Borkmann 			    void *accel_priv, select_queue_fallback_t fallback)
392c8d68e6bSJason Wang {
393c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
39496442e42SJason Wang 	struct tun_flow_entry *e;
395c8d68e6bSJason Wang 	u32 txq = 0;
396c8d68e6bSJason Wang 	u32 numqueues = 0;
397c8d68e6bSJason Wang 
398c8d68e6bSJason Wang 	rcu_read_lock();
39992bb73eaSJason Wang 	numqueues = ACCESS_ONCE(tun->numqueues);
400c8d68e6bSJason Wang 
4013958afa1STom Herbert 	txq = skb_get_hash(skb);
402c8d68e6bSJason Wang 	if (txq) {
40396442e42SJason Wang 		e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
4049bc88939STom Herbert 		if (e) {
4059bc88939STom Herbert 			tun_flow_save_rps_rxhash(e, txq);
406fbe4d456SZhi Yong Wu 			txq = e->queue_index;
4079bc88939STom Herbert 		} else
408c8d68e6bSJason Wang 			/* use multiply and shift instead of expensive divide */
409c8d68e6bSJason Wang 			txq = ((u64)txq * numqueues) >> 32;
410c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
411c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
412c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
413c8d68e6bSJason Wang 			txq -= numqueues;
414c8d68e6bSJason Wang 	}
415c8d68e6bSJason Wang 
416c8d68e6bSJason Wang 	rcu_read_unlock();
417c8d68e6bSJason Wang 	return txq;
418c8d68e6bSJason Wang }
419c8d68e6bSJason Wang 
420cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
421cde8b15fSJason Wang {
422cde8b15fSJason Wang 	const struct cred *cred = current_cred();
423c260b772SEric W. Biederman 	struct net *net = dev_net(tun->dev);
424cde8b15fSJason Wang 
425cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
426cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
427c260b772SEric W. Biederman 		!ns_capable(net->user_ns, CAP_NET_ADMIN);
428cde8b15fSJason Wang }
429cde8b15fSJason Wang 
430c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
431c8d68e6bSJason Wang {
432c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
433c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
434c8d68e6bSJason Wang }
435c8d68e6bSJason Wang 
4364008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
4374008e97fSJason Wang {
4384008e97fSJason Wang 	tfile->detached = tun;
4394008e97fSJason Wang 	list_add_tail(&tfile->next, &tun->disabled);
4404008e97fSJason Wang 	++tun->numdisabled;
4414008e97fSJason Wang }
4424008e97fSJason Wang 
443d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
4444008e97fSJason Wang {
4454008e97fSJason Wang 	struct tun_struct *tun = tfile->detached;
4464008e97fSJason Wang 
4474008e97fSJason Wang 	tfile->detached = NULL;
4484008e97fSJason Wang 	list_del_init(&tfile->next);
4494008e97fSJason Wang 	--tun->numdisabled;
4504008e97fSJason Wang 	return tun;
4514008e97fSJason Wang }
4524008e97fSJason Wang 
4534bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile)
4544bfb0513SJason Wang {
4554bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_receive_queue);
4564bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_error_queue);
4574bfb0513SJason Wang }
4584bfb0513SJason Wang 
459c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
460c8d68e6bSJason Wang {
461c8d68e6bSJason Wang 	struct tun_file *ntfile;
462c8d68e6bSJason Wang 	struct tun_struct *tun;
463c8d68e6bSJason Wang 
464b8deabd3SJason Wang 	tun = rtnl_dereference(tfile->tun);
465b8deabd3SJason Wang 
4669e85722dSJason Wang 	if (tun && !tfile->detached) {
467c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
468c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
469c8d68e6bSJason Wang 
470c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
471c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
472b8deabd3SJason Wang 		ntfile = rtnl_dereference(tun->tfiles[index]);
473c8d68e6bSJason Wang 		ntfile->queue_index = index;
474c8d68e6bSJason Wang 
475c8d68e6bSJason Wang 		--tun->numqueues;
4769e85722dSJason Wang 		if (clean) {
477c956674bSMonam Agarwal 			RCU_INIT_POINTER(tfile->tun, NULL);
478c8d68e6bSJason Wang 			sock_put(&tfile->sk);
4799e85722dSJason Wang 		} else
4804008e97fSJason Wang 			tun_disable_queue(tun, tfile);
481c8d68e6bSJason Wang 
482c8d68e6bSJason Wang 		synchronize_net();
48396442e42SJason Wang 		tun_flow_delete_by_queue(tun, tun->numqueues + 1);
484c8d68e6bSJason Wang 		/* Drop read queue */
4854bfb0513SJason Wang 		tun_queue_purge(tfile);
486c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
487dd38bd85SJason Wang 	} else if (tfile->detached && clean) {
4884008e97fSJason Wang 		tun = tun_enable_queue(tfile);
489dd38bd85SJason Wang 		sock_put(&tfile->sk);
490dd38bd85SJason Wang 	}
491c8d68e6bSJason Wang 
492c8d68e6bSJason Wang 	if (clean) {
493af668b3cSMichael S. Tsirkin 		if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
494af668b3cSMichael S. Tsirkin 			netif_carrier_off(tun->dev);
495af668b3cSMichael S. Tsirkin 
49640630b82SMichael S. Tsirkin 			if (!(tun->flags & IFF_PERSIST) &&
497af668b3cSMichael S. Tsirkin 			    tun->dev->reg_state == NETREG_REGISTERED)
4984008e97fSJason Wang 				unregister_netdevice(tun->dev);
499af668b3cSMichael S. Tsirkin 		}
5004008e97fSJason Wang 
501c8d68e6bSJason Wang 		BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
502c8d68e6bSJason Wang 				 &tfile->socket.flags));
503c8d68e6bSJason Wang 		sk_release_kernel(&tfile->sk);
504c8d68e6bSJason Wang 	}
505c8d68e6bSJason Wang }
506c8d68e6bSJason Wang 
507c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
508c8d68e6bSJason Wang {
509c8d68e6bSJason Wang 	rtnl_lock();
510c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
511c8d68e6bSJason Wang 	rtnl_unlock();
512c8d68e6bSJason Wang }
513c8d68e6bSJason Wang 
514c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
515c8d68e6bSJason Wang {
516c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
5174008e97fSJason Wang 	struct tun_file *tfile, *tmp;
518c8d68e6bSJason Wang 	int i, n = tun->numqueues;
519c8d68e6bSJason Wang 
520c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
521b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
522c8d68e6bSJason Wang 		BUG_ON(!tfile);
5239e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
524c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
525c8d68e6bSJason Wang 		--tun->numqueues;
526c8d68e6bSJason Wang 	}
5279e85722dSJason Wang 	list_for_each_entry(tfile, &tun->disabled, next) {
5289e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
529c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
5309e85722dSJason Wang 	}
531c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
532c8d68e6bSJason Wang 
533c8d68e6bSJason Wang 	synchronize_net();
534c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
535b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
536c8d68e6bSJason Wang 		/* Drop read queue */
5374bfb0513SJason Wang 		tun_queue_purge(tfile);
538c8d68e6bSJason Wang 		sock_put(&tfile->sk);
539c8d68e6bSJason Wang 	}
5404008e97fSJason Wang 	list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
5414008e97fSJason Wang 		tun_enable_queue(tfile);
5424bfb0513SJason Wang 		tun_queue_purge(tfile);
5434008e97fSJason Wang 		sock_put(&tfile->sk);
5444008e97fSJason Wang 	}
5454008e97fSJason Wang 	BUG_ON(tun->numdisabled != 0);
546dd38bd85SJason Wang 
54740630b82SMichael S. Tsirkin 	if (tun->flags & IFF_PERSIST)
548dd38bd85SJason Wang 		module_put(THIS_MODULE);
549c8d68e6bSJason Wang }
550c8d68e6bSJason Wang 
551849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
552a7385ba2SEric W. Biederman {
553631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
55438231b7aSEric W. Biederman 	int err;
555a7385ba2SEric W. Biederman 
5565dbbaf2dSPaul Moore 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
5575dbbaf2dSPaul Moore 	if (err < 0)
5585dbbaf2dSPaul Moore 		goto out;
5595dbbaf2dSPaul Moore 
56038231b7aSEric W. Biederman 	err = -EINVAL;
5619e85722dSJason Wang 	if (rtnl_dereference(tfile->tun) && !tfile->detached)
56238231b7aSEric W. Biederman 		goto out;
56338231b7aSEric W. Biederman 
56438231b7aSEric W. Biederman 	err = -EBUSY;
56540630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
566c8d68e6bSJason Wang 		goto out;
567c8d68e6bSJason Wang 
568c8d68e6bSJason Wang 	err = -E2BIG;
5694008e97fSJason Wang 	if (!tfile->detached &&
5704008e97fSJason Wang 	    tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
57138231b7aSEric W. Biederman 		goto out;
57238231b7aSEric W. Biederman 
57338231b7aSEric W. Biederman 	err = 0;
57454f968d6SJason Wang 
57592d4ea6eSstephen hemminger 	/* Re-attach the filter to persist device */
576849c9b6fSPavel Emelyanov 	if (!skip_filter && (tun->filter_attached == true)) {
57754f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
57854f968d6SJason Wang 		if (!err)
57954f968d6SJason Wang 			goto out;
58054f968d6SJason Wang 	}
581c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
5826e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
583c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
584c8d68e6bSJason Wang 	tun->numqueues++;
585c8d68e6bSJason Wang 
5864008e97fSJason Wang 	if (tfile->detached)
5874008e97fSJason Wang 		tun_enable_queue(tfile);
5884008e97fSJason Wang 	else
5894008e97fSJason Wang 		sock_hold(&tfile->sk);
5904008e97fSJason Wang 
591c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
592c8d68e6bSJason Wang 
593c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
594c8d68e6bSJason Wang 	 * refcnt.
595c8d68e6bSJason Wang 	 */
596a7385ba2SEric W. Biederman 
59738231b7aSEric W. Biederman out:
59838231b7aSEric W. Biederman 	return err;
599a7385ba2SEric W. Biederman }
600a7385ba2SEric W. Biederman 
601631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
602631ab46bSEric W. Biederman {
6036e914fc7SJason Wang 	struct tun_struct *tun;
604c70f1829SEric W. Biederman 
6056e914fc7SJason Wang 	rcu_read_lock();
6066e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
6076e914fc7SJason Wang 	if (tun)
6086e914fc7SJason Wang 		dev_hold(tun->dev);
6096e914fc7SJason Wang 	rcu_read_unlock();
610c70f1829SEric W. Biederman 
611c70f1829SEric W. Biederman 	return tun;
612631ab46bSEric W. Biederman }
613631ab46bSEric W. Biederman 
614631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
615631ab46bSEric W. Biederman {
616631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
617631ab46bSEric W. Biederman }
618631ab46bSEric W. Biederman 
619631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
620631ab46bSEric W. Biederman {
6216e914fc7SJason Wang 	dev_put(tun->dev);
622631ab46bSEric W. Biederman }
623631ab46bSEric W. Biederman 
6246b8a66eeSJoe Perches /* TAP filtering */
625f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
626f271b2ccSMax Krasnyansky {
627f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
628f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
629f271b2ccSMax Krasnyansky }
630f271b2ccSMax Krasnyansky 
631f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
632f271b2ccSMax Krasnyansky {
633f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
634f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
635f271b2ccSMax Krasnyansky }
636f271b2ccSMax Krasnyansky 
637f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
638f271b2ccSMax Krasnyansky {
639f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
640f271b2ccSMax Krasnyansky 	struct tun_filter uf;
641f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
642f271b2ccSMax Krasnyansky 
643f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
644f271b2ccSMax Krasnyansky 		return -EFAULT;
645f271b2ccSMax Krasnyansky 
646f271b2ccSMax Krasnyansky 	if (!uf.count) {
647f271b2ccSMax Krasnyansky 		/* Disabled */
648f271b2ccSMax Krasnyansky 		filter->count = 0;
649f271b2ccSMax Krasnyansky 		return 0;
650f271b2ccSMax Krasnyansky 	}
651f271b2ccSMax Krasnyansky 
652f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
653f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
654f271b2ccSMax Krasnyansky 	if (!addr)
655f271b2ccSMax Krasnyansky 		return -ENOMEM;
656f271b2ccSMax Krasnyansky 
657f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
658f271b2ccSMax Krasnyansky 		err = -EFAULT;
659f271b2ccSMax Krasnyansky 		goto done;
660f271b2ccSMax Krasnyansky 	}
661f271b2ccSMax Krasnyansky 
662f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
663f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
664f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
665f271b2ccSMax Krasnyansky 	filter->count = 0;
666f271b2ccSMax Krasnyansky 	wmb();
667f271b2ccSMax Krasnyansky 
668f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
669f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
670f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
671f271b2ccSMax Krasnyansky 
672f271b2ccSMax Krasnyansky 	nexact = n;
673f271b2ccSMax Krasnyansky 
674cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
675cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
676f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
677cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
678cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
679cfbf84fcSAlex Williamson 			err = 0; /* no filter */
680cfbf84fcSAlex Williamson 			goto done;
681cfbf84fcSAlex Williamson 		}
682f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
683cfbf84fcSAlex Williamson 	}
684f271b2ccSMax Krasnyansky 
685f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
686f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
687f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
688f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
689f271b2ccSMax Krasnyansky 
690f271b2ccSMax Krasnyansky 	/* Now enable the filter */
691f271b2ccSMax Krasnyansky 	wmb();
692f271b2ccSMax Krasnyansky 	filter->count = nexact;
693f271b2ccSMax Krasnyansky 
694f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
695f271b2ccSMax Krasnyansky 	err = nexact;
696f271b2ccSMax Krasnyansky 
697f271b2ccSMax Krasnyansky done:
698f271b2ccSMax Krasnyansky 	kfree(addr);
699f271b2ccSMax Krasnyansky 	return err;
700f271b2ccSMax Krasnyansky }
701f271b2ccSMax Krasnyansky 
702f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
703f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
704f271b2ccSMax Krasnyansky {
705f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
706f271b2ccSMax Krasnyansky 	 * at this point. */
707f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
708f271b2ccSMax Krasnyansky 	int i;
709f271b2ccSMax Krasnyansky 
710f271b2ccSMax Krasnyansky 	/* Exact match */
711f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
7122e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
713f271b2ccSMax Krasnyansky 			return 1;
714f271b2ccSMax Krasnyansky 
715f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
716f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
717f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
718f271b2ccSMax Krasnyansky 
719f271b2ccSMax Krasnyansky 	return 0;
720f271b2ccSMax Krasnyansky }
721f271b2ccSMax Krasnyansky 
722f271b2ccSMax Krasnyansky /*
723f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
724f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
725f271b2ccSMax Krasnyansky  */
726f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
727f271b2ccSMax Krasnyansky {
728f271b2ccSMax Krasnyansky 	if (!filter->count)
729f271b2ccSMax Krasnyansky 		return 1;
730f271b2ccSMax Krasnyansky 
731f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
732f271b2ccSMax Krasnyansky }
733f271b2ccSMax Krasnyansky 
7341da177e4SLinus Torvalds /* Network device part of the driver */
7351da177e4SLinus Torvalds 
7367282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
7371da177e4SLinus Torvalds 
738c70f1829SEric W. Biederman /* Net device detach from fd. */
739c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
740c70f1829SEric W. Biederman {
741c8d68e6bSJason Wang 	tun_detach_all(dev);
742c70f1829SEric W. Biederman }
743c70f1829SEric W. Biederman 
7441da177e4SLinus Torvalds /* Net device open. */
7451da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
7461da177e4SLinus Torvalds {
747c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
7481da177e4SLinus Torvalds 	return 0;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds /* Net device close. */
7521da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
7531da177e4SLinus Torvalds {
754c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
7551da177e4SLinus Torvalds 	return 0;
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds /* Net device start xmit */
759424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
7601da177e4SLinus Torvalds {
7611da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
762c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
7636e914fc7SJason Wang 	struct tun_file *tfile;
764fa35864eSDominic Curran 	u32 numqueues = 0;
7651da177e4SLinus Torvalds 
7666e914fc7SJason Wang 	rcu_read_lock();
767c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
768fa35864eSDominic Curran 	numqueues = ACCESS_ONCE(tun->numqueues);
769c8d68e6bSJason Wang 
7701da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
771fa35864eSDominic Curran 	if (txq >= numqueues)
7721da177e4SLinus Torvalds 		goto drop;
7731da177e4SLinus Torvalds 
774fa35864eSDominic Curran 	if (numqueues == 1) {
7759bc88939STom Herbert 		/* Select queue was not called for the skbuff, so we extract the
7769bc88939STom Herbert 		 * RPS hash and save it into the flow_table here.
7779bc88939STom Herbert 		 */
7789bc88939STom Herbert 		__u32 rxhash;
7799bc88939STom Herbert 
7809bc88939STom Herbert 		rxhash = skb_get_hash(skb);
7819bc88939STom Herbert 		if (rxhash) {
7829bc88939STom Herbert 			struct tun_flow_entry *e;
7839bc88939STom Herbert 			e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
7849bc88939STom Herbert 					rxhash);
7859bc88939STom Herbert 			if (e)
7869bc88939STom Herbert 				tun_flow_save_rps_rxhash(e, rxhash);
7879bc88939STom Herbert 		}
7889bc88939STom Herbert 	}
7899bc88939STom Herbert 
7906e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
7916e914fc7SJason Wang 
792c8d68e6bSJason Wang 	BUG_ON(!tfile);
793c8d68e6bSJason Wang 
794f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
795f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
796f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
797f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
798f271b2ccSMax Krasnyansky 		goto drop;
799f271b2ccSMax Krasnyansky 
80054f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
80154f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
80299405162SMichael S. Tsirkin 		goto drop;
80399405162SMichael S. Tsirkin 
80436fe8c09SRami Rosen 	/* Limit the number of packets queued by dividing txq length with the
805c8d68e6bSJason Wang 	 * number of queues.
806c8d68e6bSJason Wang 	 */
807fa35864eSDominic Curran 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
808fa35864eSDominic Curran 			  >= dev->tx_queue_len)
8091da177e4SLinus Torvalds 		goto drop;
8101da177e4SLinus Torvalds 
8117bf66305SJason Wang 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
8127bf66305SJason Wang 		goto drop;
8137bf66305SJason Wang 
814eda29772SRichard Cochran 	if (skb->sk) {
815eda29772SRichard Cochran 		sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
816eda29772SRichard Cochran 		sw_tx_timestamp(skb);
817eda29772SRichard Cochran 	}
818eda29772SRichard Cochran 
8190110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
8207bf66305SJason Wang 	 * for indefinite time.
8217bf66305SJason Wang 	 */
8220110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
8230110d6f2SMichael S. Tsirkin 
824f8af75f3SEric Dumazet 	nf_reset(skb);
825f8af75f3SEric Dumazet 
826f271b2ccSMax Krasnyansky 	/* Enqueue packet */
82754f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	/* Notify and wake up reader process */
83054f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
83154f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
8329e641bdcSXi Wang 	tfile->socket.sk->sk_data_ready(tfile->socket.sk);
8336e914fc7SJason Wang 
8346e914fc7SJason Wang 	rcu_read_unlock();
8356ed10654SPatrick McHardy 	return NETDEV_TX_OK;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds drop:
83809f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
839149d36f7SMichael S. Tsirkin 	skb_tx_error(skb);
8401da177e4SLinus Torvalds 	kfree_skb(skb);
8416e914fc7SJason Wang 	rcu_read_unlock();
842baeababbSJason Wang 	return NET_XMIT_DROP;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
845f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
8461da177e4SLinus Torvalds {
847f271b2ccSMax Krasnyansky 	/*
848f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
849f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
850f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
851f271b2ccSMax Krasnyansky 	 */
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8544885a504SEd Swierk #define MIN_MTU 68
8554885a504SEd Swierk #define MAX_MTU 65535
8564885a504SEd Swierk 
8574885a504SEd Swierk static int
8584885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
8594885a504SEd Swierk {
8604885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
8614885a504SEd Swierk 		return -EINVAL;
8624885a504SEd Swierk 	dev->mtu = new_mtu;
8634885a504SEd Swierk 	return 0;
8644885a504SEd Swierk }
8654885a504SEd Swierk 
866c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
867c8f44affSMichał Mirosław 	netdev_features_t features)
86888255375SMichał Mirosław {
86988255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
87088255375SMichał Mirosław 
87188255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
87288255375SMichał Mirosław }
873bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
874bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
875bebd097aSNeil Horman {
876bebd097aSNeil Horman 	/*
877bebd097aSNeil Horman 	 * Tun only receives frames when:
878bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
879bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
88092d4ea6eSstephen hemminger 	 * Since both of those are synchronous operations, we are guaranteed
881bebd097aSNeil Horman 	 * never to have pending data when we poll for it
88292d4ea6eSstephen hemminger 	 * so there is nothing to do here but return.
883bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
884bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
885bebd097aSNeil Horman 	 * still use netconsole
886bebd097aSNeil Horman 	 */
887bebd097aSNeil Horman 	return;
888bebd097aSNeil Horman }
889bebd097aSNeil Horman #endif
890758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
891c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
892758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
893758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
89400829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
895758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
89688255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
897c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
898bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
899bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
900bebd097aSNeil Horman #endif
901758e43b7SStephen Hemminger };
902758e43b7SStephen Hemminger 
903758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
904c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
905758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
906758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
90700829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
908758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
90988255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
910afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
911758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
912758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
913c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
914bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
915bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
916bebd097aSNeil Horman #endif
917758e43b7SStephen Hemminger };
918758e43b7SStephen Hemminger 
919944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun)
92096442e42SJason Wang {
92196442e42SJason Wang 	int i;
92296442e42SJason Wang 
92396442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
92496442e42SJason Wang 		INIT_HLIST_HEAD(&tun->flows[i]);
92596442e42SJason Wang 
92696442e42SJason Wang 	tun->ageing_time = TUN_FLOW_EXPIRE;
92796442e42SJason Wang 	setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
92896442e42SJason Wang 	mod_timer(&tun->flow_gc_timer,
92996442e42SJason Wang 		  round_jiffies_up(jiffies + tun->ageing_time));
93096442e42SJason Wang }
93196442e42SJason Wang 
93296442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun)
93396442e42SJason Wang {
93496442e42SJason Wang 	del_timer_sync(&tun->flow_gc_timer);
93596442e42SJason Wang 	tun_flow_flush(tun);
93696442e42SJason Wang }
93796442e42SJason Wang 
9381da177e4SLinus Torvalds /* Initialize net device. */
9391da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
94440630b82SMichael S. Tsirkin 	case IFF_TUN:
945758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
946758e43b7SStephen Hemminger 
9471da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
9481da177e4SLinus Torvalds 		dev->hard_header_len = 0;
9491da177e4SLinus Torvalds 		dev->addr_len = 0;
9501da177e4SLinus Torvalds 		dev->mtu = 1500;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 		/* Zero header length */
9531da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
9541da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
9551da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
9561da177e4SLinus Torvalds 		break;
9571da177e4SLinus Torvalds 
95840630b82SMichael S. Tsirkin 	case IFF_TAP:
9597a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
9601da177e4SLinus Torvalds 		/* Ethernet TAP Device */
9611da177e4SLinus Torvalds 		ether_setup(dev);
962550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
963a676847bSstephen hemminger 		dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
96436226a8dSBrian Braunstein 
965f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
96636226a8dSBrian Braunstein 
9671da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
9681da177e4SLinus Torvalds 		break;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds /* Character device part */
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds /* Poll */
9751da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
9761da177e4SLinus Torvalds {
977b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
978b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
9793c8a9c63SMariusz Kozlowski 	struct sock *sk;
98033dccbb0SHerbert Xu 	unsigned int mask = 0;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	if (!tun)
983eac9e902SEric W. Biederman 		return POLLERR;
9841da177e4SLinus Torvalds 
98554f968d6SJason Wang 	sk = tfile->socket.sk;
9863c8a9c63SMariusz Kozlowski 
9876b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
9881da177e4SLinus Torvalds 
9899e641bdcSXi Wang 	poll_wait(file, sk_sleep(sk), wait);
9901da177e4SLinus Torvalds 
99189f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
9921da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
9931da177e4SLinus Torvalds 
99433dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
99533dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
99633dccbb0SHerbert Xu 	     sock_writeable(sk)))
99733dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
99833dccbb0SHerbert Xu 
999c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
1000c70f1829SEric W. Biederman 		mask = POLLERR;
1001c70f1829SEric W. Biederman 
1002631ab46bSEric W. Biederman 	tun_put(tun);
10031da177e4SLinus Torvalds 	return mask;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
1006f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
1007f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
100854f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
100933dccbb0SHerbert Xu 				     size_t prepad, size_t len,
101033dccbb0SHerbert Xu 				     size_t linear, int noblock)
1011f42157cbSRusty Russell {
101254f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
1013f42157cbSRusty Russell 	struct sk_buff *skb;
101433dccbb0SHerbert Xu 	int err;
1015f42157cbSRusty Russell 
1016f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
10170eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
101833dccbb0SHerbert Xu 		linear = len;
1019f42157cbSRusty Russell 
102033dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
102128d64271SEric Dumazet 				   &err, 0);
1022f42157cbSRusty Russell 	if (!skb)
102333dccbb0SHerbert Xu 		return ERR_PTR(err);
1024f42157cbSRusty Russell 
1025f42157cbSRusty Russell 	skb_reserve(skb, prepad);
1026f42157cbSRusty Russell 	skb_put(skb, linear);
102733dccbb0SHerbert Xu 	skb->data_len = len - linear;
102833dccbb0SHerbert Xu 	skb->len += len - linear;
1029f42157cbSRusty Russell 
1030f42157cbSRusty Russell 	return skb;
1031f42157cbSRusty Russell }
1032f42157cbSRusty Russell 
10331da177e4SLinus Torvalds /* Get packet from user space buffer */
103454f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1035f5ff53b4SAl Viro 			    void *msg_control, struct iov_iter *from,
1036f5ff53b4SAl Viro 			    int noblock)
10371da177e4SLinus Torvalds {
103809640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
10391da177e4SLinus Torvalds 	struct sk_buff *skb;
1040f5ff53b4SAl Viro 	size_t total_len = iov_iter_count(from);
10413dd5c330SJason Wang 	size_t len = total_len, align = NET_SKB_PAD, linear;
1042f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
104396f8d9ecSJason Wang 	int good_linear;
10440690899bSMichael S. Tsirkin 	int copylen;
10450690899bSMichael S. Tsirkin 	bool zerocopy = false;
10460690899bSMichael S. Tsirkin 	int err;
104749974420SEric Dumazet 	u32 rxhash;
1048f5ff53b4SAl Viro 	ssize_t n;
10491da177e4SLinus Torvalds 
105040630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
105115718ea0SDan Carpenter 		if (len < sizeof(pi))
10521da177e4SLinus Torvalds 			return -EINVAL;
105315718ea0SDan Carpenter 		len -= sizeof(pi);
10541da177e4SLinus Torvalds 
1055f5ff53b4SAl Viro 		n = copy_from_iter(&pi, sizeof(pi), from);
1056f5ff53b4SAl Viro 		if (n != sizeof(pi))
10571da177e4SLinus Torvalds 			return -EFAULT;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
106040630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR) {
106115718ea0SDan Carpenter 		if (len < tun->vnet_hdr_sz)
1062f43798c2SRusty Russell 			return -EINVAL;
106315718ea0SDan Carpenter 		len -= tun->vnet_hdr_sz;
1064f43798c2SRusty Russell 
1065f5ff53b4SAl Viro 		n = copy_from_iter(&gso, sizeof(gso), from);
1066f5ff53b4SAl Viro 		if (n != sizeof(gso))
1067f43798c2SRusty Russell 			return -EFAULT;
1068f43798c2SRusty Russell 
10694909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
107056f0dcc5SMichael S. Tsirkin 		    tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
107156f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
10724909122fSHerbert Xu 
107356f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > len)
1074f43798c2SRusty Russell 			return -EINVAL;
1075d8febb77SHerbert Xu 		iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso));
1076f43798c2SRusty Russell 	}
1077f43798c2SRusty Russell 
107840630b82SMichael S. Tsirkin 	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
1079a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
10800eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
108156f0dcc5SMichael S. Tsirkin 			     (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
1082e01bf1c8SRusty Russell 			return -EINVAL;
1083e01bf1c8SRusty Russell 	}
10841da177e4SLinus Torvalds 
108596f8d9ecSJason Wang 	good_linear = SKB_MAX_HEAD(align);
108696f8d9ecSJason Wang 
108788529176SJason Wang 	if (msg_control) {
1088f5ff53b4SAl Viro 		struct iov_iter i = *from;
1089f5ff53b4SAl Viro 
109088529176SJason Wang 		/* There are 256 bytes to be copied in skb, so there is
109188529176SJason Wang 		 * enough room for skb expand head in case it is used.
10920690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
10930690899bSMichael S. Tsirkin 		 */
109456f0dcc5SMichael S. Tsirkin 		copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
109596f8d9ecSJason Wang 		if (copylen > good_linear)
109696f8d9ecSJason Wang 			copylen = good_linear;
10973dd5c330SJason Wang 		linear = copylen;
1098f5ff53b4SAl Viro 		iov_iter_advance(&i, copylen);
1099f5ff53b4SAl Viro 		if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
110088529176SJason Wang 			zerocopy = true;
110188529176SJason Wang 	}
110288529176SJason Wang 
110388529176SJason Wang 	if (!zerocopy) {
11040690899bSMichael S. Tsirkin 		copylen = len;
110556f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
110696f8d9ecSJason Wang 			linear = good_linear;
110796f8d9ecSJason Wang 		else
110856f0dcc5SMichael S. Tsirkin 			linear = tun16_to_cpu(tun, gso.hdr_len);
11093dd5c330SJason Wang 	}
11100690899bSMichael S. Tsirkin 
11113dd5c330SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
111233dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
111333dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
111409f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
111533dccbb0SHerbert Xu 		return PTR_ERR(skb);
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
11180690899bSMichael S. Tsirkin 	if (zerocopy)
1119f5ff53b4SAl Viro 		err = zerocopy_sg_from_iter(skb, from);
112088529176SJason Wang 	else {
1121f5ff53b4SAl Viro 		err = skb_copy_datagram_from_iter(skb, 0, from, len);
112288529176SJason Wang 		if (!err && msg_control) {
112388529176SJason Wang 			struct ubuf_info *uarg = msg_control;
112488529176SJason Wang 			uarg->callback(uarg, false);
112588529176SJason Wang 		}
112688529176SJason Wang 	}
11270690899bSMichael S. Tsirkin 
11280690899bSMichael S. Tsirkin 	if (err) {
112909f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
11308f22757eSDave Jones 		kfree_skb(skb);
11311da177e4SLinus Torvalds 		return -EFAULT;
11328f22757eSDave Jones 	}
11331da177e4SLinus Torvalds 
1134f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
113556f0dcc5SMichael S. Tsirkin 		if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
113656f0dcc5SMichael S. Tsirkin 					  tun16_to_cpu(tun, gso.csum_offset))) {
1137f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1138f43798c2SRusty Russell 			kfree_skb(skb);
1139f43798c2SRusty Russell 			return -EINVAL;
1140f43798c2SRusty Russell 		}
114188255375SMichał Mirosław 	}
1142f43798c2SRusty Russell 
11431da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
114440630b82SMichael S. Tsirkin 	case IFF_TUN:
114540630b82SMichael S. Tsirkin 		if (tun->flags & IFF_NO_PI) {
1146f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
1147f09f7ee2SAng Way Chuang 			case 0x40:
1148f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
1149f09f7ee2SAng Way Chuang 				break;
1150f09f7ee2SAng Way Chuang 			case 0x60:
1151f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
1152f09f7ee2SAng Way Chuang 				break;
1153f09f7ee2SAng Way Chuang 			default:
1154f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
1155f09f7ee2SAng Way Chuang 				kfree_skb(skb);
1156f09f7ee2SAng Way Chuang 				return -EINVAL;
1157f09f7ee2SAng Way Chuang 			}
1158f09f7ee2SAng Way Chuang 		}
1159f09f7ee2SAng Way Chuang 
1160459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
11611da177e4SLinus Torvalds 		skb->protocol = pi.proto;
11624c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
11631da177e4SLinus Torvalds 		break;
116440630b82SMichael S. Tsirkin 	case IFF_TAP:
11651da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
11661da177e4SLinus Torvalds 		break;
11676403eab1SJoe Perches 	}
11681da177e4SLinus Torvalds 
1169f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1170f43798c2SRusty Russell 		pr_debug("GSO!\n");
1171f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1172f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
1173c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1174f43798c2SRusty Russell 			break;
1175f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
1176c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1177f43798c2SRusty Russell 			break;
1178e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
11793d0ad094SBen Hutchings 		{
11803d0ad094SBen Hutchings 			static bool warned;
11813d0ad094SBen Hutchings 
11823d0ad094SBen Hutchings 			if (!warned) {
11833d0ad094SBen Hutchings 				warned = true;
11843d0ad094SBen Hutchings 				netdev_warn(tun->dev,
11853d0ad094SBen Hutchings 					    "%s: using disabled UFO feature; please fix this program\n",
11863d0ad094SBen Hutchings 					    current->comm);
11873d0ad094SBen Hutchings 			}
1188c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1189e36aa25aSSridhar Samudrala 			break;
11903d0ad094SBen Hutchings 		}
1191f43798c2SRusty Russell 		default:
1192f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1193f43798c2SRusty Russell 			kfree_skb(skb);
1194f43798c2SRusty Russell 			return -EINVAL;
1195f43798c2SRusty Russell 		}
1196f43798c2SRusty Russell 
1197f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1198c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1199f43798c2SRusty Russell 
120056f0dcc5SMichael S. Tsirkin 		skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
1201f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
1202f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1203f43798c2SRusty Russell 			kfree_skb(skb);
1204f43798c2SRusty Russell 			return -EINVAL;
1205f43798c2SRusty Russell 		}
1206f43798c2SRusty Russell 
1207f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
1208f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1209f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
1210f43798c2SRusty Russell 	}
12111da177e4SLinus Torvalds 
12120690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
12130690899bSMichael S. Tsirkin 	if (zerocopy) {
12140690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
12150690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1216c9af6db4SPravin B Shelar 		skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
12170690899bSMichael S. Tsirkin 	}
12180690899bSMichael S. Tsirkin 
1219*72f65107SVlad Yasevich 	skb_reset_network_header(skb);
122040893fd0SJason Wang 	skb_probe_transport_header(skb, 0);
122138502af7SJason Wang 
12223958afa1STom Herbert 	rxhash = skb_get_hash(skb);
12231da177e4SLinus Torvalds 	netif_rx_ni(skb);
12241da177e4SLinus Torvalds 
122509f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
122609f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
12271da177e4SLinus Torvalds 
12289e85722dSJason Wang 	tun_flow_update(tun, rxhash, tfile);
12290690899bSMichael S. Tsirkin 	return total_len;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
1232f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
12331da177e4SLinus Torvalds {
123433dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
1235ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
123654f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
1237631ab46bSEric W. Biederman 	ssize_t result;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	if (!tun)
12401da177e4SLinus Torvalds 		return -EBADFD;
12411da177e4SLinus Torvalds 
1242f5ff53b4SAl Viro 	result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
1243631ab46bSEric W. Biederman 
1244631ab46bSEric W. Biederman 	tun_put(tun);
1245631ab46bSEric W. Biederman 	return result;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds /* Put packet to the user space buffer */
12496f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
125054f968d6SJason Wang 			    struct tun_file *tfile,
12511da177e4SLinus Torvalds 			    struct sk_buff *skb,
1252e0b46d0eSHerbert Xu 			    struct iov_iter *iter)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
1255e0b46d0eSHerbert Xu 	ssize_t total;
12568c847d25SJason Wang 	int vlan_offset = 0;
1257a8f9bfdfSHerbert Xu 	int vlan_hlen = 0;
12582eb783c4SHerbert Xu 	int vnet_hdr_sz = 0;
1259a8f9bfdfSHerbert Xu 
1260a8f9bfdfSHerbert Xu 	if (vlan_tx_tag_present(skb))
1261a8f9bfdfSHerbert Xu 		vlan_hlen = VLAN_HLEN;
12621da177e4SLinus Torvalds 
126340630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR)
12642eb783c4SHerbert Xu 		vnet_hdr_sz = tun->vnet_hdr_sz;
12651da177e4SLinus Torvalds 
1266e0b46d0eSHerbert Xu 	total = skb->len + vlan_hlen + vnet_hdr_sz;
1267e0b46d0eSHerbert Xu 
126840630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
1269e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < sizeof(pi))
12701da177e4SLinus Torvalds 			return -EINVAL;
12711da177e4SLinus Torvalds 
1272e0b46d0eSHerbert Xu 		total += sizeof(pi);
1273e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < total) {
12741da177e4SLinus Torvalds 			/* Packet will be striped */
12751da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 
1278e0b46d0eSHerbert Xu 		if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
12791da177e4SLinus Torvalds 			return -EFAULT;
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 
12822eb783c4SHerbert Xu 	if (vnet_hdr_sz) {
1283f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1284e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < vnet_hdr_sz)
1285f43798c2SRusty Russell 			return -EINVAL;
1286f43798c2SRusty Russell 
1287f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1288f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1289f43798c2SRusty Russell 
1290f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
129156f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
129256f0dcc5SMichael S. Tsirkin 			gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
1293f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1294f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1295f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1296f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1297ef3db4a5SMichael S. Tsirkin 			else {
12986b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1299ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
130056f0dcc5SMichael S. Tsirkin 				       sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
130156f0dcc5SMichael S. Tsirkin 				       tun16_to_cpu(tun, gso.hdr_len));
1302ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1303ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1304ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
130556f0dcc5SMichael S. Tsirkin 					       min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1306ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1307ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1308ef3db4a5SMichael S. Tsirkin 			}
1309f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1310f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1311f43798c2SRusty Russell 		} else
1312f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1313f43798c2SRusty Russell 
1314f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1315f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
131656f0dcc5SMichael S. Tsirkin 			gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
131756f0dcc5SMichael S. Tsirkin 						      vlan_hlen);
131856f0dcc5SMichael S. Tsirkin 			gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
131910a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
132010a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1321f43798c2SRusty Russell 		} /* else everything is zero */
1322f43798c2SRusty Russell 
1323e0b46d0eSHerbert Xu 		if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
1324f43798c2SRusty Russell 			return -EFAULT;
13258c847d25SJason Wang 
13268c847d25SJason Wang 		iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
1327f43798c2SRusty Russell 	}
1328f43798c2SRusty Russell 
1329a8f9bfdfSHerbert Xu 	if (vlan_hlen) {
1330e0b46d0eSHerbert Xu 		int ret;
13316680ec68SJason Wang 		struct {
13326680ec68SJason Wang 			__be16 h_vlan_proto;
13336680ec68SJason Wang 			__be16 h_vlan_TCI;
13346680ec68SJason Wang 		} veth;
13351da177e4SLinus Torvalds 
13366680ec68SJason Wang 		veth.h_vlan_proto = skb->vlan_proto;
13376680ec68SJason Wang 		veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
13381da177e4SLinus Torvalds 
13396680ec68SJason Wang 		vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
13406680ec68SJason Wang 
1341e0b46d0eSHerbert Xu 		ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1342e0b46d0eSHerbert Xu 		if (ret || !iov_iter_count(iter))
13436680ec68SJason Wang 			goto done;
13446680ec68SJason Wang 
1345e0b46d0eSHerbert Xu 		ret = copy_to_iter(&veth, sizeof(veth), iter);
1346e0b46d0eSHerbert Xu 		if (ret != sizeof(veth) || !iov_iter_count(iter))
13476680ec68SJason Wang 			goto done;
13486680ec68SJason Wang 	}
13496680ec68SJason Wang 
1350e0b46d0eSHerbert Xu 	skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
13516680ec68SJason Wang 
13526680ec68SJason Wang done:
135309f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
1354e0b46d0eSHerbert Xu 	tun->dev->stats.tx_bytes += skb->len + vlan_hlen;
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	return total;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
135954f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
13609b067034SAl Viro 			   struct iov_iter *to,
13619b067034SAl Viro 			   int noblock)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	struct sk_buff *skb;
13649b067034SAl Viro 	ssize_t ret;
13659e641bdcSXi Wang 	int peeked, err, off = 0;
13661da177e4SLinus Torvalds 
13673872baf6SRami Rosen 	tun_debug(KERN_INFO, tun, "tun_do_read\n");
13681da177e4SLinus Torvalds 
13699b067034SAl Viro 	if (!iov_iter_count(to))
13709b067034SAl Viro 		return 0;
13711da177e4SLinus Torvalds 
13729e641bdcSXi Wang 	if (tun->dev->reg_state != NETREG_REGISTERED)
13739e641bdcSXi Wang 		return -EIO;
13741da177e4SLinus Torvalds 
13759e641bdcSXi Wang 	/* Read frames from queue */
13769e641bdcSXi Wang 	skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
13779e641bdcSXi Wang 				  &peeked, &off, &err);
1378e0b46d0eSHerbert Xu 	if (!skb)
13799b067034SAl Viro 		return 0;
1380e0b46d0eSHerbert Xu 
13819b067034SAl Viro 	ret = tun_put_user(tun, tfile, skb, to);
1382f51a5e82SJason Wang 	if (unlikely(ret < 0))
13831da177e4SLinus Torvalds 		kfree_skb(skb);
1384f51a5e82SJason Wang 	else
1385f51a5e82SJason Wang 		consume_skb(skb);
13861da177e4SLinus Torvalds 
138705c2828cSMichael S. Tsirkin 	return ret;
138805c2828cSMichael S. Tsirkin }
138905c2828cSMichael S. Tsirkin 
13909b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
139105c2828cSMichael S. Tsirkin {
139205c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
139305c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
139405c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
13959b067034SAl Viro 	ssize_t len = iov_iter_count(to), ret;
139605c2828cSMichael S. Tsirkin 
139705c2828cSMichael S. Tsirkin 	if (!tun)
139805c2828cSMichael S. Tsirkin 		return -EBADFD;
13999b067034SAl Viro 	ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK);
140042404c09SDavid S. Miller 	ret = min_t(ssize_t, ret, len);
1401d0b7da8aSZhi Yong Wu 	if (ret > 0)
1402d0b7da8aSZhi Yong Wu 		iocb->ki_pos = ret;
1403631ab46bSEric W. Biederman 	tun_put(tun);
14041da177e4SLinus Torvalds 	return ret;
14051da177e4SLinus Torvalds }
14061da177e4SLinus Torvalds 
140796442e42SJason Wang static void tun_free_netdev(struct net_device *dev)
140896442e42SJason Wang {
140996442e42SJason Wang 	struct tun_struct *tun = netdev_priv(dev);
141096442e42SJason Wang 
14114008e97fSJason Wang 	BUG_ON(!(list_empty(&tun->disabled)));
141296442e42SJason Wang 	tun_flow_uninit(tun);
14135dbbaf2dSPaul Moore 	security_tun_dev_free_security(tun->security);
141496442e42SJason Wang 	free_netdev(dev);
141596442e42SJason Wang }
141696442e42SJason Wang 
14171da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
14181da177e4SLinus Torvalds {
14191da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
14201da177e4SLinus Torvalds 
14210625c883SEric W. Biederman 	tun->owner = INVALID_UID;
14220625c883SEric W. Biederman 	tun->group = INVALID_GID;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
142596442e42SJason Wang 	dev->destructor = tun_free_netdev;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
1428f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1429f019a7a5SEric W. Biederman  * device with netlink.
1430f019a7a5SEric W. Biederman  */
1431f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1432f019a7a5SEric W. Biederman {
1433f019a7a5SEric W. Biederman 	return -EINVAL;
1434f019a7a5SEric W. Biederman }
1435f019a7a5SEric W. Biederman 
1436f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1437f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1438f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1439f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1440f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1441f019a7a5SEric W. Biederman };
1442f019a7a5SEric W. Biederman 
144333dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
144433dccbb0SHerbert Xu {
144554f968d6SJason Wang 	struct tun_file *tfile;
144643815482SEric Dumazet 	wait_queue_head_t *wqueue;
144733dccbb0SHerbert Xu 
144833dccbb0SHerbert Xu 	if (!sock_writeable(sk))
144933dccbb0SHerbert Xu 		return;
145033dccbb0SHerbert Xu 
145133dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
145233dccbb0SHerbert Xu 		return;
145333dccbb0SHerbert Xu 
145443815482SEric Dumazet 	wqueue = sk_sleep(sk);
145543815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
145643815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
145705c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1458c722c625SHerbert Xu 
145954f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
146054f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
146133dccbb0SHerbert Xu }
146233dccbb0SHerbert Xu 
146305c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
146405c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
146505c2828cSMichael S. Tsirkin {
146654f968d6SJason Wang 	int ret;
146754f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
146854f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
146954f968d6SJason Wang 
147054f968d6SJason Wang 	if (!tun)
147154f968d6SJason Wang 		return -EBADFD;
1472f5ff53b4SAl Viro 
1473c0371da6SAl Viro 	ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
1474f5ff53b4SAl Viro 			   m->msg_flags & MSG_DONTWAIT);
147554f968d6SJason Wang 	tun_put(tun);
147654f968d6SJason Wang 	return ret;
147705c2828cSMichael S. Tsirkin }
147805c2828cSMichael S. Tsirkin 
147905c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
148005c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
148105c2828cSMichael S. Tsirkin 		       int flags)
148205c2828cSMichael S. Tsirkin {
148354f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
148454f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
148505c2828cSMichael S. Tsirkin 	int ret;
148654f968d6SJason Wang 
148754f968d6SJason Wang 	if (!tun)
148854f968d6SJason Wang 		return -EBADFD;
148954f968d6SJason Wang 
1490eda29772SRichard Cochran 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
14913811ae76SGao feng 		ret = -EINVAL;
14923811ae76SGao feng 		goto out;
14933811ae76SGao feng 	}
1494eda29772SRichard Cochran 	if (flags & MSG_ERRQUEUE) {
1495eda29772SRichard Cochran 		ret = sock_recv_errqueue(sock->sk, m, total_len,
1496eda29772SRichard Cochran 					 SOL_PACKET, TUN_TX_TIMESTAMP);
1497eda29772SRichard Cochran 		goto out;
1498eda29772SRichard Cochran 	}
1499c0371da6SAl Viro 	ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT);
150042404c09SDavid S. Miller 	if (ret > total_len) {
150142404c09SDavid S. Miller 		m->msg_flags |= MSG_TRUNC;
150242404c09SDavid S. Miller 		ret = flags & MSG_TRUNC ? ret : total_len;
150342404c09SDavid S. Miller 	}
15043811ae76SGao feng out:
150554f968d6SJason Wang 	tun_put(tun);
150605c2828cSMichael S. Tsirkin 	return ret;
150705c2828cSMichael S. Tsirkin }
150805c2828cSMichael S. Tsirkin 
15091ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
15101ab5ecb9SStanislav Kinsbursky {
15111ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
15121ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
15131ab5ecb9SStanislav Kinsbursky 	return 0;
15141ab5ecb9SStanislav Kinsbursky }
15151ab5ecb9SStanislav Kinsbursky 
151605c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
151705c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
151805c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
151905c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
15201ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
152105c2828cSMichael S. Tsirkin };
152205c2828cSMichael S. Tsirkin 
152333dccbb0SHerbert Xu static struct proto tun_proto = {
152433dccbb0SHerbert Xu 	.name		= "tun",
152533dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
152654f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
152733dccbb0SHerbert Xu };
1528f019a7a5SEric W. Biederman 
1529980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1530980c9e8cSDavid Woodhouse {
1531031f5e03SMichael S. Tsirkin 	return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
1532980c9e8cSDavid Woodhouse }
1533980c9e8cSDavid Woodhouse 
1534980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1535980c9e8cSDavid Woodhouse 			      char *buf)
1536980c9e8cSDavid Woodhouse {
1537980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1538980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1539980c9e8cSDavid Woodhouse }
1540980c9e8cSDavid Woodhouse 
1541980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1542980c9e8cSDavid Woodhouse 			      char *buf)
1543980c9e8cSDavid Woodhouse {
1544980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15450625c883SEric W. Biederman 	return uid_valid(tun->owner)?
15460625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15470625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
15480625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1549980c9e8cSDavid Woodhouse }
1550980c9e8cSDavid Woodhouse 
1551980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1552980c9e8cSDavid Woodhouse 			      char *buf)
1553980c9e8cSDavid Woodhouse {
1554980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15550625c883SEric W. Biederman 	return gid_valid(tun->group) ?
15560625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15570625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
15580625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1559980c9e8cSDavid Woodhouse }
1560980c9e8cSDavid Woodhouse 
1561980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1562980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1563980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1564980c9e8cSDavid Woodhouse 
1565d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
15661da177e4SLinus Torvalds {
15671da177e4SLinus Torvalds 	struct tun_struct *tun;
156854f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
15691da177e4SLinus Torvalds 	struct net_device *dev;
15701da177e4SLinus Torvalds 	int err;
15711da177e4SLinus Torvalds 
15727c0c3b1aSJason Wang 	if (tfile->detached)
15737c0c3b1aSJason Wang 		return -EINVAL;
15747c0c3b1aSJason Wang 
157574a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
157674a3e5a7SEric W. Biederman 	if (dev) {
1577f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1578f85ba780SDavid Woodhouse 			return -EBUSY;
157974a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
158074a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
158174a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
158274a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
158374a3e5a7SEric W. Biederman 		else
158474a3e5a7SEric W. Biederman 			return -EINVAL;
158574a3e5a7SEric W. Biederman 
15868e6d91aeSJason Wang 		if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
158740630b82SMichael S. Tsirkin 		    !!(tun->flags & IFF_MULTI_QUEUE))
15888e6d91aeSJason Wang 			return -EINVAL;
15898e6d91aeSJason Wang 
1590cde8b15fSJason Wang 		if (tun_not_capable(tun))
15912b980dbdSPaul Moore 			return -EPERM;
15925dbbaf2dSPaul Moore 		err = security_tun_dev_open(tun->security);
15932b980dbdSPaul Moore 		if (err < 0)
15942b980dbdSPaul Moore 			return err;
15952b980dbdSPaul Moore 
1596849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
1597a7385ba2SEric W. Biederman 		if (err < 0)
1598a7385ba2SEric W. Biederman 			return err;
15994008e97fSJason Wang 
160040630b82SMichael S. Tsirkin 		if (tun->flags & IFF_MULTI_QUEUE &&
1601e8dbad66SJason Wang 		    (tun->numqueues + tun->numdisabled > 1)) {
1602e8dbad66SJason Wang 			/* One or more queue has already been attached, no need
1603e8dbad66SJason Wang 			 * to initialize the device again.
1604e8dbad66SJason Wang 			 */
1605e8dbad66SJason Wang 			return 0;
1606e8dbad66SJason Wang 		}
160786a264abSDavid Howells 	}
16081da177e4SLinus Torvalds 	else {
16091da177e4SLinus Torvalds 		char *name;
16101da177e4SLinus Torvalds 		unsigned long flags = 0;
1611edfb6a14SJason Wang 		int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1612edfb6a14SJason Wang 			     MAX_TAP_QUEUES : 1;
16131da177e4SLinus Torvalds 
1614c260b772SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1615ca6bb5d7SDavid Woodhouse 			return -EPERM;
16162b980dbdSPaul Moore 		err = security_tun_dev_create();
16172b980dbdSPaul Moore 		if (err < 0)
16182b980dbdSPaul Moore 			return err;
1619ca6bb5d7SDavid Woodhouse 
16201da177e4SLinus Torvalds 		/* Set dev type */
16211da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
16221da177e4SLinus Torvalds 			/* TUN device */
162340630b82SMichael S. Tsirkin 			flags |= IFF_TUN;
16241da177e4SLinus Torvalds 			name = "tun%d";
16251da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
16261da177e4SLinus Torvalds 			/* TAP device */
162740630b82SMichael S. Tsirkin 			flags |= IFF_TAP;
16281da177e4SLinus Torvalds 			name = "tap%d";
16291da177e4SLinus Torvalds 		} else
163036989b90SKusanagi Kouichi 			return -EINVAL;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		if (*ifr->ifr_name)
16331da177e4SLinus Torvalds 			name = ifr->ifr_name;
16341da177e4SLinus Torvalds 
1635c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1636c835a677STom Gundersen 				       NET_NAME_UNKNOWN, tun_setup, queues,
1637c835a677STom Gundersen 				       queues);
1638edfb6a14SJason Wang 
16391da177e4SLinus Torvalds 		if (!dev)
16401da177e4SLinus Torvalds 			return -ENOMEM;
16411da177e4SLinus Torvalds 
1642fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1643f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1644fb7589a1SPavel Emelyanov 		dev->ifindex = tfile->ifindex;
1645758e43b7SStephen Hemminger 
16461da177e4SLinus Torvalds 		tun = netdev_priv(dev);
16471da177e4SLinus Torvalds 		tun->dev = dev;
16481da177e4SLinus Torvalds 		tun->flags = flags;
1649f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1650d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
16511da177e4SLinus Torvalds 
165254f968d6SJason Wang 		tun->filter_attached = false;
165354f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
165433dccbb0SHerbert Xu 
165596442e42SJason Wang 		spin_lock_init(&tun->lock);
165696442e42SJason Wang 
16575dbbaf2dSPaul Moore 		err = security_tun_dev_alloc_security(&tun->security);
16585dbbaf2dSPaul Moore 		if (err < 0)
16595dbbaf2dSPaul Moore 			goto err_free_dev;
16602b980dbdSPaul Moore 
16611da177e4SLinus Torvalds 		tun_net_init(dev);
1662944a1376SPavel Emelyanov 		tun_flow_init(tun);
166396442e42SJason Wang 
166488255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
16656680ec68SJason Wang 				   TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
16666680ec68SJason Wang 				   NETIF_F_HW_VLAN_STAG_TX;
166788255375SMichał Mirosław 		dev->features = dev->hw_features;
16686671b224SFernando Luis Vazquez Cao 		dev->vlan_features = dev->features &
16696671b224SFernando Luis Vazquez Cao 				     ~(NETIF_F_HW_VLAN_CTAG_TX |
16706671b224SFernando Luis Vazquez Cao 				       NETIF_F_HW_VLAN_STAG_TX);
167188255375SMichał Mirosław 
16724008e97fSJason Wang 		INIT_LIST_HEAD(&tun->disabled);
1673849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, false);
1674eb0fb363SJason Wang 		if (err < 0)
1675662ca437SJason Wang 			goto err_free_flow;
1676eb0fb363SJason Wang 
16771da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
16781da177e4SLinus Torvalds 		if (err < 0)
1679662ca437SJason Wang 			goto err_detach;
16809c3fea6aSHerbert Xu 
1681980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1682980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1683980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
16846b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1685af668b3cSMichael S. Tsirkin 	}
1686980c9e8cSDavid Woodhouse 
1687eb0fb363SJason Wang 	netif_carrier_on(tun->dev);
16881da177e4SLinus Torvalds 
16896b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
16901da177e4SLinus Torvalds 
1691031f5e03SMichael S. Tsirkin 	tun->flags = (tun->flags & ~TUN_FEATURES) |
1692031f5e03SMichael S. Tsirkin 		(ifr->ifr_flags & TUN_FEATURES);
1693c8d68e6bSJason Wang 
1694e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1695e35259a9SMax Krasnyansky 	 * xoff state.
1696e35259a9SMax Krasnyansky 	 */
1697e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1698c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1699e35259a9SMax Krasnyansky 
17001da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
17011da177e4SLinus Torvalds 	return 0;
17021da177e4SLinus Torvalds 
1703662ca437SJason Wang err_detach:
1704662ca437SJason Wang 	tun_detach_all(dev);
1705662ca437SJason Wang err_free_flow:
1706662ca437SJason Wang 	tun_flow_uninit(tun);
1707662ca437SJason Wang 	security_tun_dev_free_security(tun->security);
17081da177e4SLinus Torvalds err_free_dev:
17091da177e4SLinus Torvalds 	free_netdev(dev);
17101da177e4SLinus Torvalds 	return err;
17111da177e4SLinus Torvalds }
17121da177e4SLinus Torvalds 
17139ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun,
1714876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1715e3b99556SMark McLoughlin {
17166b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1717e3b99556SMark McLoughlin 
1718e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1719e3b99556SMark McLoughlin 
1720980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1721e3b99556SMark McLoughlin 
1722e3b99556SMark McLoughlin }
1723e3b99556SMark McLoughlin 
17245228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
17255228ddc9SRusty Russell  * privs required. */
172688255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
17275228ddc9SRusty Russell {
1728c8f44affSMichał Mirosław 	netdev_features_t features = 0;
17295228ddc9SRusty Russell 
17305228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
173188255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
17325228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
17335228ddc9SRusty Russell 
17345228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
17355228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
17365228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
17375228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
17385228ddc9SRusty Russell 			}
17395228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
17405228ddc9SRusty Russell 				features |= NETIF_F_TSO;
17415228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
17425228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
17435228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
17445228ddc9SRusty Russell 		}
17455228ddc9SRusty Russell 	}
17465228ddc9SRusty Russell 
17475228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
17485228ddc9SRusty Russell 	 * trying to set them. */
17495228ddc9SRusty Russell 	if (arg)
17505228ddc9SRusty Russell 		return -EINVAL;
17515228ddc9SRusty Russell 
175288255375SMichał Mirosław 	tun->set_features = features;
175388255375SMichał Mirosław 	netdev_update_features(tun->dev);
17545228ddc9SRusty Russell 
17555228ddc9SRusty Russell 	return 0;
17565228ddc9SRusty Russell }
17575228ddc9SRusty Russell 
1758c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1759c8d68e6bSJason Wang {
1760c8d68e6bSJason Wang 	int i;
1761c8d68e6bSJason Wang 	struct tun_file *tfile;
1762c8d68e6bSJason Wang 
1763c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1764b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1765c8d68e6bSJason Wang 		sk_detach_filter(tfile->socket.sk);
1766c8d68e6bSJason Wang 	}
1767c8d68e6bSJason Wang 
1768c8d68e6bSJason Wang 	tun->filter_attached = false;
1769c8d68e6bSJason Wang }
1770c8d68e6bSJason Wang 
1771c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1772c8d68e6bSJason Wang {
1773c8d68e6bSJason Wang 	int i, ret = 0;
1774c8d68e6bSJason Wang 	struct tun_file *tfile;
1775c8d68e6bSJason Wang 
1776c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1777b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1778c8d68e6bSJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1779c8d68e6bSJason Wang 		if (ret) {
1780c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1781c8d68e6bSJason Wang 			return ret;
1782c8d68e6bSJason Wang 		}
1783c8d68e6bSJason Wang 	}
1784c8d68e6bSJason Wang 
1785c8d68e6bSJason Wang 	tun->filter_attached = true;
1786c8d68e6bSJason Wang 	return ret;
1787c8d68e6bSJason Wang }
1788c8d68e6bSJason Wang 
1789c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1790c8d68e6bSJason Wang {
1791c8d68e6bSJason Wang 	struct tun_file *tfile;
1792c8d68e6bSJason Wang 	int i;
1793c8d68e6bSJason Wang 
1794c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1795b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1796c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1797c8d68e6bSJason Wang 	}
1798c8d68e6bSJason Wang }
1799c8d68e6bSJason Wang 
1800cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1801cde8b15fSJason Wang {
1802cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1803cde8b15fSJason Wang 	struct tun_struct *tun;
1804cde8b15fSJason Wang 	int ret = 0;
1805cde8b15fSJason Wang 
1806cde8b15fSJason Wang 	rtnl_lock();
1807cde8b15fSJason Wang 
1808cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
18094008e97fSJason Wang 		tun = tfile->detached;
18105dbbaf2dSPaul Moore 		if (!tun) {
1811cde8b15fSJason Wang 			ret = -EINVAL;
18125dbbaf2dSPaul Moore 			goto unlock;
18135dbbaf2dSPaul Moore 		}
18145dbbaf2dSPaul Moore 		ret = security_tun_dev_attach_queue(tun->security);
18155dbbaf2dSPaul Moore 		if (ret < 0)
18165dbbaf2dSPaul Moore 			goto unlock;
1817849c9b6fSPavel Emelyanov 		ret = tun_attach(tun, file, false);
18184008e97fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
1819b8deabd3SJason Wang 		tun = rtnl_dereference(tfile->tun);
182040630b82SMichael S. Tsirkin 		if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
18214008e97fSJason Wang 			ret = -EINVAL;
1822cde8b15fSJason Wang 		else
18234008e97fSJason Wang 			__tun_detach(tfile, false);
18244008e97fSJason Wang 	} else
1825cde8b15fSJason Wang 		ret = -EINVAL;
1826cde8b15fSJason Wang 
18275dbbaf2dSPaul Moore unlock:
1828cde8b15fSJason Wang 	rtnl_unlock();
1829cde8b15fSJason Wang 	return ret;
1830cde8b15fSJason Wang }
1831cde8b15fSJason Wang 
183250857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
183350857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
18341da177e4SLinus Torvalds {
183536b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1836631ab46bSEric W. Biederman 	struct tun_struct *tun;
18371da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
18381da177e4SLinus Torvalds 	struct ifreq ifr;
18390625c883SEric W. Biederman 	kuid_t owner;
18400625c883SEric W. Biederman 	kgid_t group;
184133dccbb0SHerbert Xu 	int sndbuf;
1842d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1843fb7589a1SPavel Emelyanov 	unsigned int ifindex;
18441cf8e410SMichael S. Tsirkin 	int le;
1845f271b2ccSMax Krasnyansky 	int ret;
18461da177e4SLinus Torvalds 
1847cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
184850857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
18491da177e4SLinus Torvalds 			return -EFAULT;
18508bbb1813SDavid S. Miller 	} else {
1851a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
18528bbb1813SDavid S. Miller 	}
1853631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1854631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1855631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1856031f5e03SMichael S. Tsirkin 		 * TUNSETIFF.
1857031f5e03SMichael S. Tsirkin 		 */
1858031f5e03SMichael S. Tsirkin 		return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
1859631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1860cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
1861cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
1862631ab46bSEric W. Biederman 
1863c8d68e6bSJason Wang 	ret = 0;
1864876bfd4dSHerbert Xu 	rtnl_lock();
1865876bfd4dSHerbert Xu 
186636b50babSEric W. Biederman 	tun = __tun_get(tfile);
18671da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
18681da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
18691da177e4SLinus Torvalds 
1870876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
18711da177e4SLinus Torvalds 
1872876bfd4dSHerbert Xu 		if (ret)
1873876bfd4dSHerbert Xu 			goto unlock;
18741da177e4SLinus Torvalds 
187550857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1876876bfd4dSHerbert Xu 			ret = -EFAULT;
1877876bfd4dSHerbert Xu 		goto unlock;
18781da177e4SLinus Torvalds 	}
1879fb7589a1SPavel Emelyanov 	if (cmd == TUNSETIFINDEX) {
1880fb7589a1SPavel Emelyanov 		ret = -EPERM;
1881fb7589a1SPavel Emelyanov 		if (tun)
1882fb7589a1SPavel Emelyanov 			goto unlock;
1883fb7589a1SPavel Emelyanov 
1884fb7589a1SPavel Emelyanov 		ret = -EFAULT;
1885fb7589a1SPavel Emelyanov 		if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1886fb7589a1SPavel Emelyanov 			goto unlock;
1887fb7589a1SPavel Emelyanov 
1888fb7589a1SPavel Emelyanov 		ret = 0;
1889fb7589a1SPavel Emelyanov 		tfile->ifindex = ifindex;
1890fb7589a1SPavel Emelyanov 		goto unlock;
1891fb7589a1SPavel Emelyanov 	}
18921da177e4SLinus Torvalds 
1893876bfd4dSHerbert Xu 	ret = -EBADFD;
18941da177e4SLinus Torvalds 	if (!tun)
1895876bfd4dSHerbert Xu 		goto unlock;
18961da177e4SLinus Torvalds 
18971e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
18981da177e4SLinus Torvalds 
1899631ab46bSEric W. Biederman 	ret = 0;
19001da177e4SLinus Torvalds 	switch (cmd) {
1901e3b99556SMark McLoughlin 	case TUNGETIFF:
19029ce99cf6SRami Rosen 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1903e3b99556SMark McLoughlin 
19043d407a80SPavel Emelyanov 		if (tfile->detached)
19053d407a80SPavel Emelyanov 			ifr.ifr_flags |= IFF_DETACH_QUEUE;
1906849c9b6fSPavel Emelyanov 		if (!tfile->socket.sk->sk_filter)
1907849c9b6fSPavel Emelyanov 			ifr.ifr_flags |= IFF_NOFILTER;
19083d407a80SPavel Emelyanov 
190950857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1910631ab46bSEric W. Biederman 			ret = -EFAULT;
1911e3b99556SMark McLoughlin 		break;
1912e3b99556SMark McLoughlin 
19131da177e4SLinus Torvalds 	case TUNSETNOCSUM:
19141da177e4SLinus Torvalds 		/* Disable/Enable checksum */
19151da177e4SLinus Torvalds 
191688255375SMichał Mirosław 		/* [unimplemented] */
191788255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
19186b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
19191da177e4SLinus Torvalds 		break;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	case TUNSETPERSIST:
192254f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
192354f968d6SJason Wang 		 * module to prevent the module being unprobed.
192454f968d6SJason Wang 		 */
192540630b82SMichael S. Tsirkin 		if (arg && !(tun->flags & IFF_PERSIST)) {
192640630b82SMichael S. Tsirkin 			tun->flags |= IFF_PERSIST;
192754f968d6SJason Wang 			__module_get(THIS_MODULE);
1928dd38bd85SJason Wang 		}
192940630b82SMichael S. Tsirkin 		if (!arg && (tun->flags & IFF_PERSIST)) {
193040630b82SMichael S. Tsirkin 			tun->flags &= ~IFF_PERSIST;
193154f968d6SJason Wang 			module_put(THIS_MODULE);
193254f968d6SJason Wang 		}
19331da177e4SLinus Torvalds 
19346b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
19356b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
19361da177e4SLinus Torvalds 		break;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	case TUNSETOWNER:
19391da177e4SLinus Torvalds 		/* Set owner of the device */
19400625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
19410625c883SEric W. Biederman 		if (!uid_valid(owner)) {
19420625c883SEric W. Biederman 			ret = -EINVAL;
19430625c883SEric W. Biederman 			break;
19440625c883SEric W. Biederman 		}
19450625c883SEric W. Biederman 		tun->owner = owner;
19461e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
19470625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
19481da177e4SLinus Torvalds 		break;
19491da177e4SLinus Torvalds 
19508c644623SGuido Guenther 	case TUNSETGROUP:
19518c644623SGuido Guenther 		/* Set group of the device */
19520625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
19530625c883SEric W. Biederman 		if (!gid_valid(group)) {
19540625c883SEric W. Biederman 			ret = -EINVAL;
19550625c883SEric W. Biederman 			break;
19560625c883SEric W. Biederman 		}
19570625c883SEric W. Biederman 		tun->group = group;
19581e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
19590625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
19608c644623SGuido Guenther 		break;
19618c644623SGuido Guenther 
1962ff4cc3acSMike Kershaw 	case TUNSETLINK:
1963ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1964ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
19656b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
19666b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
196748abfe05SDavid S. Miller 			ret = -EBUSY;
1968ff4cc3acSMike Kershaw 		} else {
1969ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
19706b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
19716b8a66eeSJoe Perches 				  tun->dev->type);
197248abfe05SDavid S. Miller 			ret = 0;
1973ff4cc3acSMike Kershaw 		}
1974631ab46bSEric W. Biederman 		break;
1975ff4cc3acSMike Kershaw 
19761da177e4SLinus Torvalds #ifdef TUN_DEBUG
19771da177e4SLinus Torvalds 	case TUNSETDEBUG:
19781da177e4SLinus Torvalds 		tun->debug = arg;
19791da177e4SLinus Torvalds 		break;
19801da177e4SLinus Torvalds #endif
19815228ddc9SRusty Russell 	case TUNSETOFFLOAD:
198288255375SMichał Mirosław 		ret = set_offload(tun, arg);
1983631ab46bSEric W. Biederman 		break;
19845228ddc9SRusty Russell 
1985f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1986f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1987631ab46bSEric W. Biederman 		ret = -EINVAL;
198840630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1989631ab46bSEric W. Biederman 			break;
1990c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1991631ab46bSEric W. Biederman 		break;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1994b595076aSUwe Kleine-König 		/* Get hw address */
1995f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1996f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
199750857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1998631ab46bSEric W. Biederman 			ret = -EFAULT;
1999631ab46bSEric W. Biederman 		break;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
2002f271b2ccSMax Krasnyansky 		/* Set hw address */
20036b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
20046b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
200540102371SKim B. Heino 
200640102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
2007631ab46bSEric W. Biederman 		break;
200833dccbb0SHerbert Xu 
200933dccbb0SHerbert Xu 	case TUNGETSNDBUF:
201054f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
201133dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
201233dccbb0SHerbert Xu 			ret = -EFAULT;
201333dccbb0SHerbert Xu 		break;
201433dccbb0SHerbert Xu 
201533dccbb0SHerbert Xu 	case TUNSETSNDBUF:
201633dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
201733dccbb0SHerbert Xu 			ret = -EFAULT;
201833dccbb0SHerbert Xu 			break;
201933dccbb0SHerbert Xu 		}
202033dccbb0SHerbert Xu 
2021c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
2022c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
202333dccbb0SHerbert Xu 		break;
202433dccbb0SHerbert Xu 
2025d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
2026d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
2027d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2028d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2029d9d52b51SMichael S. Tsirkin 		break;
2030d9d52b51SMichael S. Tsirkin 
2031d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
2032d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2033d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2034d9d52b51SMichael S. Tsirkin 			break;
2035d9d52b51SMichael S. Tsirkin 		}
2036d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2037d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
2038d9d52b51SMichael S. Tsirkin 			break;
2039d9d52b51SMichael S. Tsirkin 		}
2040d9d52b51SMichael S. Tsirkin 
2041d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
2042d9d52b51SMichael S. Tsirkin 		break;
2043d9d52b51SMichael S. Tsirkin 
20441cf8e410SMichael S. Tsirkin 	case TUNGETVNETLE:
20451cf8e410SMichael S. Tsirkin 		le = !!(tun->flags & TUN_VNET_LE);
20461cf8e410SMichael S. Tsirkin 		if (put_user(le, (int __user *)argp))
20471cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
20481cf8e410SMichael S. Tsirkin 		break;
20491cf8e410SMichael S. Tsirkin 
20501cf8e410SMichael S. Tsirkin 	case TUNSETVNETLE:
20511cf8e410SMichael S. Tsirkin 		if (get_user(le, (int __user *)argp)) {
20521cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
20531cf8e410SMichael S. Tsirkin 			break;
20541cf8e410SMichael S. Tsirkin 		}
20551cf8e410SMichael S. Tsirkin 		if (le)
20561cf8e410SMichael S. Tsirkin 			tun->flags |= TUN_VNET_LE;
20571cf8e410SMichael S. Tsirkin 		else
20581cf8e410SMichael S. Tsirkin 			tun->flags &= ~TUN_VNET_LE;
20591cf8e410SMichael S. Tsirkin 		break;
20601cf8e410SMichael S. Tsirkin 
206199405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
206299405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
206399405162SMichael S. Tsirkin 		ret = -EINVAL;
206440630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
206599405162SMichael S. Tsirkin 			break;
206699405162SMichael S. Tsirkin 		ret = -EFAULT;
206754f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
206899405162SMichael S. Tsirkin 			break;
206999405162SMichael S. Tsirkin 
2070c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
207199405162SMichael S. Tsirkin 		break;
207299405162SMichael S. Tsirkin 
207399405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
207499405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
207599405162SMichael S. Tsirkin 		ret = -EINVAL;
207640630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
207799405162SMichael S. Tsirkin 			break;
2078c8d68e6bSJason Wang 		ret = 0;
2079c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
208099405162SMichael S. Tsirkin 		break;
208199405162SMichael S. Tsirkin 
208276975e9cSPavel Emelyanov 	case TUNGETFILTER:
208376975e9cSPavel Emelyanov 		ret = -EINVAL;
208440630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
208576975e9cSPavel Emelyanov 			break;
208676975e9cSPavel Emelyanov 		ret = -EFAULT;
208776975e9cSPavel Emelyanov 		if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
208876975e9cSPavel Emelyanov 			break;
208976975e9cSPavel Emelyanov 		ret = 0;
209076975e9cSPavel Emelyanov 		break;
209176975e9cSPavel Emelyanov 
20921da177e4SLinus Torvalds 	default:
2093631ab46bSEric W. Biederman 		ret = -EINVAL;
2094631ab46bSEric W. Biederman 		break;
2095ee289b64SJoe Perches 	}
20961da177e4SLinus Torvalds 
2097876bfd4dSHerbert Xu unlock:
2098876bfd4dSHerbert Xu 	rtnl_unlock();
2099876bfd4dSHerbert Xu 	if (tun)
2100631ab46bSEric W. Biederman 		tun_put(tun);
2101631ab46bSEric W. Biederman 	return ret;
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
210450857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
210550857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
210650857e2aSArnd Bergmann {
210750857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
210850857e2aSArnd Bergmann }
210950857e2aSArnd Bergmann 
211050857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
211150857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
211250857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
211350857e2aSArnd Bergmann {
211450857e2aSArnd Bergmann 	switch (cmd) {
211550857e2aSArnd Bergmann 	case TUNSETIFF:
211650857e2aSArnd Bergmann 	case TUNGETIFF:
211750857e2aSArnd Bergmann 	case TUNSETTXFILTER:
211850857e2aSArnd Bergmann 	case TUNGETSNDBUF:
211950857e2aSArnd Bergmann 	case TUNSETSNDBUF:
212050857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
212150857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
212250857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
212350857e2aSArnd Bergmann 		break;
212450857e2aSArnd Bergmann 	default:
212550857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
212650857e2aSArnd Bergmann 		break;
212750857e2aSArnd Bergmann 	}
212850857e2aSArnd Bergmann 
212950857e2aSArnd Bergmann 	/*
213050857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
213150857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
213250857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
213350857e2aSArnd Bergmann 	 * contents.
213450857e2aSArnd Bergmann 	 */
213550857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
213650857e2aSArnd Bergmann }
213750857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
213850857e2aSArnd Bergmann 
21391da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
21401da177e4SLinus Torvalds {
214154f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
21421da177e4SLinus Torvalds 	int ret;
21431da177e4SLinus Torvalds 
214454f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
21459d319522SJonathan Corbet 		goto out;
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds 	if (on) {
2148e0b93eddSJeff Layton 		__f_setown(file, task_pid(current), PIDTYPE_PID, 0);
214954f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
21501da177e4SLinus Torvalds 	} else
215154f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
21529d319522SJonathan Corbet 	ret = 0;
21539d319522SJonathan Corbet out:
21549d319522SJonathan Corbet 	return ret;
21551da177e4SLinus Torvalds }
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
21581da177e4SLinus Torvalds {
2159631ab46bSEric W. Biederman 	struct tun_file *tfile;
2160deed49fbSThomas Gleixner 
21616b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2162631ab46bSEric W. Biederman 
216354f968d6SJason Wang 	tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
216454f968d6SJason Wang 					    &tun_proto);
2165631ab46bSEric W. Biederman 	if (!tfile)
2166631ab46bSEric W. Biederman 		return -ENOMEM;
2167c956674bSMonam Agarwal 	RCU_INIT_POINTER(tfile->tun, NULL);
216836b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
216954f968d6SJason Wang 	tfile->flags = 0;
2170fb7589a1SPavel Emelyanov 	tfile->ifindex = 0;
217154f968d6SJason Wang 
217254f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
21739e641bdcSXi Wang 	RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
217454f968d6SJason Wang 
217554f968d6SJason Wang 	tfile->socket.file = file;
217654f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
217754f968d6SJason Wang 
217854f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
217954f968d6SJason Wang 	sk_change_net(&tfile->sk, tfile->net);
218054f968d6SJason Wang 
218154f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
218254f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
218354f968d6SJason Wang 
2184631ab46bSEric W. Biederman 	file->private_data = tfile;
218554f968d6SJason Wang 	set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
21864008e97fSJason Wang 	INIT_LIST_HEAD(&tfile->next);
218754f968d6SJason Wang 
218819a6afb2SJason Wang 	sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
218919a6afb2SJason Wang 
21901da177e4SLinus Torvalds 	return 0;
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
21941da177e4SLinus Torvalds {
2195631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
219654f968d6SJason Wang 	struct net *net = tfile->net;
21971da177e4SLinus Torvalds 
2198c8d68e6bSJason Wang 	tun_detach(tfile, true);
219954f968d6SJason Wang 	put_net(net);
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	return 0;
22021da177e4SLinus Torvalds }
22031da177e4SLinus Torvalds 
220493e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
2205a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
220693e14b6dSMasatake YAMATO {
220793e14b6dSMasatake YAMATO 	struct tun_struct *tun;
220893e14b6dSMasatake YAMATO 	struct ifreq ifr;
220993e14b6dSMasatake YAMATO 
221093e14b6dSMasatake YAMATO 	memset(&ifr, 0, sizeof(ifr));
221193e14b6dSMasatake YAMATO 
221293e14b6dSMasatake YAMATO 	rtnl_lock();
221393e14b6dSMasatake YAMATO 	tun = tun_get(f);
221493e14b6dSMasatake YAMATO 	if (tun)
221593e14b6dSMasatake YAMATO 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
221693e14b6dSMasatake YAMATO 	rtnl_unlock();
221793e14b6dSMasatake YAMATO 
221893e14b6dSMasatake YAMATO 	if (tun)
221993e14b6dSMasatake YAMATO 		tun_put(tun);
222093e14b6dSMasatake YAMATO 
2221a3816ab0SJoe Perches 	seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
222293e14b6dSMasatake YAMATO }
222393e14b6dSMasatake YAMATO #endif
222493e14b6dSMasatake YAMATO 
2225d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
22261da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
22271da177e4SLinus Torvalds 	.llseek = no_llseek,
22289b067034SAl Viro 	.read  = new_sync_read,
2229f5ff53b4SAl Viro 	.write = new_sync_write,
22309b067034SAl Viro 	.read_iter  = tun_chr_read_iter,
2231f5ff53b4SAl Viro 	.write_iter = tun_chr_write_iter,
22321da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
2233876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
223450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
223550857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
223650857e2aSArnd Bergmann #endif
22371da177e4SLinus Torvalds 	.open	= tun_chr_open,
22381da177e4SLinus Torvalds 	.release = tun_chr_close,
223993e14b6dSMasatake YAMATO 	.fasync = tun_chr_fasync,
224093e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
224193e14b6dSMasatake YAMATO 	.show_fdinfo = tun_chr_show_fdinfo,
224293e14b6dSMasatake YAMATO #endif
22431da177e4SLinus Torvalds };
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
22461da177e4SLinus Torvalds 	.minor = TUN_MINOR,
22471da177e4SLinus Torvalds 	.name = "tun",
2248e454cea2SKay Sievers 	.nodename = "net/tun",
22491da177e4SLinus Torvalds 	.fops = &tun_fops,
22501da177e4SLinus Torvalds };
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds /* ethtool interface */
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
22551da177e4SLinus Torvalds {
22561da177e4SLinus Torvalds 	cmd->supported		= 0;
22571da177e4SLinus Torvalds 	cmd->advertising	= 0;
225870739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
22591da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
22601da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
22611da177e4SLinus Torvalds 	cmd->phy_address	= 0;
22621da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
22631da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
22641da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
22651da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
22661da177e4SLinus Torvalds 	return 0;
22671da177e4SLinus Torvalds }
22681da177e4SLinus Torvalds 
22691da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
22701da177e4SLinus Torvalds {
22711da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22721da177e4SLinus Torvalds 
227333a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
227433a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
227740630b82SMichael S. Tsirkin 	case IFF_TUN:
227833a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
22791da177e4SLinus Torvalds 		break;
228040630b82SMichael S. Tsirkin 	case IFF_TAP:
228133a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
22821da177e4SLinus Torvalds 		break;
22831da177e4SLinus Torvalds 	}
22841da177e4SLinus Torvalds }
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
22871da177e4SLinus Torvalds {
22881da177e4SLinus Torvalds #ifdef TUN_DEBUG
22891da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22901da177e4SLinus Torvalds 	return tun->debug;
22911da177e4SLinus Torvalds #else
22921da177e4SLinus Torvalds 	return -EOPNOTSUPP;
22931da177e4SLinus Torvalds #endif
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
22971da177e4SLinus Torvalds {
22981da177e4SLinus Torvalds #ifdef TUN_DEBUG
22991da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
23001da177e4SLinus Torvalds 	tun->debug = value;
23011da177e4SLinus Torvalds #endif
23021da177e4SLinus Torvalds }
23031da177e4SLinus Torvalds 
23047282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
23051da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
23061da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
23071da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
23081da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
2309bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
2310eda29772SRichard Cochran 	.get_ts_info	= ethtool_op_get_ts_info,
23111da177e4SLinus Torvalds };
23121da177e4SLinus Torvalds 
231379d17604SPavel Emelyanov 
23141da177e4SLinus Torvalds static int __init tun_init(void)
23151da177e4SLinus Torvalds {
23161da177e4SLinus Torvalds 	int ret = 0;
23171da177e4SLinus Torvalds 
23186b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
23196b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
23201da177e4SLinus Torvalds 
2321f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
232279d17604SPavel Emelyanov 	if (ret) {
23236b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
2324f019a7a5SEric W. Biederman 		goto err_linkops;
232579d17604SPavel Emelyanov 	}
232679d17604SPavel Emelyanov 
23271da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
232879d17604SPavel Emelyanov 	if (ret) {
23296b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
233079d17604SPavel Emelyanov 		goto err_misc;
233179d17604SPavel Emelyanov 	}
233279d17604SPavel Emelyanov 	return  0;
233379d17604SPavel Emelyanov err_misc:
2334f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2335f019a7a5SEric W. Biederman err_linkops:
23361da177e4SLinus Torvalds 	return ret;
23371da177e4SLinus Torvalds }
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds static void tun_cleanup(void)
23401da177e4SLinus Torvalds {
23411da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2342f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
23431da177e4SLinus Torvalds }
23441da177e4SLinus Torvalds 
234505c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
234605c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
234705c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
234805c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
234905c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
235005c2828cSMichael S. Tsirkin {
23516e914fc7SJason Wang 	struct tun_file *tfile;
235205c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
235305c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
23546e914fc7SJason Wang 	tfile = file->private_data;
23556e914fc7SJason Wang 	if (!tfile)
235605c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
235754f968d6SJason Wang 	return &tfile->socket;
235805c2828cSMichael S. Tsirkin }
235905c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
236005c2828cSMichael S. Tsirkin 
23611da177e4SLinus Torvalds module_init(tun_init);
23621da177e4SLinus Torvalds module_exit(tun_cleanup);
23631da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
23641da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
23651da177e4SLinus Torvalds MODULE_LICENSE("GPL");
23661da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2367578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2368