xref: /openbmc/linux/drivers/net/tun.c (revision 1cf8e410b6d3906aa1ecacdd15dd9b448c9ce111)
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>
685188cd44SBen Hutchings #include <net/ipv6.h>
69881d966bSEric W. Biederman #include <net/net_namespace.h>
7079d17604SPavel Emelyanov #include <net/netns/generic.h>
71f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
7233dccbb0SHerbert Xu #include <net/sock.h>
7393e14b6dSMasatake YAMATO #include <linux/seq_file.h>
74e0b46d0eSHerbert Xu #include <linux/uio.h>
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #include <asm/uaccess.h>
771da177e4SLinus Torvalds 
7814daa021SRusty Russell /* Uncomment to enable debugging */
7914daa021SRusty Russell /* #define TUN_DEBUG 1 */
8014daa021SRusty Russell 
811da177e4SLinus Torvalds #ifdef TUN_DEBUG
821da177e4SLinus Torvalds static int debug;
8314daa021SRusty Russell 
846b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
856b8a66eeSJoe Perches do {								\
866b8a66eeSJoe Perches 	if (tun->debug)						\
876b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
886b8a66eeSJoe Perches } while (0)
896b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
906b8a66eeSJoe Perches do {								\
916b8a66eeSJoe Perches 	if (debug == 2)						\
926b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
936b8a66eeSJoe Perches } while (0)
9414daa021SRusty Russell #else
956b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
966b8a66eeSJoe Perches do {								\
976b8a66eeSJoe Perches 	if (0)							\
986b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
996b8a66eeSJoe Perches } while (0)
1006b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
1016b8a66eeSJoe Perches do {								\
1026b8a66eeSJoe Perches 	if (0)							\
1036b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
1046b8a66eeSJoe Perches } while (0)
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
107031f5e03SMichael S. Tsirkin /* TUN device flags */
108031f5e03SMichael S. Tsirkin 
109031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags,
110031f5e03SMichael S. Tsirkin  * overload it to mean fasync when stored there.
111031f5e03SMichael S. Tsirkin  */
112031f5e03SMichael S. Tsirkin #define TUN_FASYNC	IFF_ATTACH_QUEUE
113*1cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */
114*1cf8e410SMichael S. Tsirkin #define TUN_VNET_LE     0x80000000
115031f5e03SMichael S. Tsirkin 
116031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
117*1cf8e410SMichael S. Tsirkin 		      IFF_MULTI_QUEUE)
1180690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128
1190690899bSMichael S. Tsirkin 
120f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
121f271b2ccSMax Krasnyansky struct tap_filter {
122f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
123f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
124f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
125f271b2ccSMax Krasnyansky };
126f271b2ccSMax Krasnyansky 
12792d4ea6eSstephen hemminger /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for
128edfb6a14SJason Wang  * the netdevice to be fit in one page. So we can make sure the success of
129edfb6a14SJason Wang  * memory allocation. TODO: increase the limit. */
130edfb6a14SJason Wang #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
131b8732fb7SJason Wang #define MAX_TAP_FLOWS  4096
132c8d68e6bSJason Wang 
13396442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ)
13496442e42SJason Wang 
13554f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
13692d4ea6eSstephen hemminger  * also contains all socket related structures (except sock_fprog and tap_filter)
13754f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
13854f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
13936fe8c09SRami Rosen  * netdevice not for a specific queue (at least I didn't see the requirement for
14054f968d6SJason Wang  * this).
1416e914fc7SJason Wang  *
1426e914fc7SJason Wang  * RCU usage:
14336fe8c09SRami Rosen  * The tun_file and tun_struct are loosely coupled, the pointer from one to the
1446e914fc7SJason Wang  * other can only be read while rcu_read_lock or rtnl_lock is held.
14554f968d6SJason Wang  */
146631ab46bSEric W. Biederman struct tun_file {
14754f968d6SJason Wang 	struct sock sk;
14854f968d6SJason Wang 	struct socket socket;
14954f968d6SJason Wang 	struct socket_wq wq;
1506e914fc7SJason Wang 	struct tun_struct __rcu *tun;
15136b50babSEric W. Biederman 	struct net *net;
15254f968d6SJason Wang 	struct fasync_struct *fasync;
15354f968d6SJason Wang 	/* only used for fasnyc */
15454f968d6SJason Wang 	unsigned int flags;
155fb7589a1SPavel Emelyanov 	union {
156c8d68e6bSJason Wang 		u16 queue_index;
157fb7589a1SPavel Emelyanov 		unsigned int ifindex;
158fb7589a1SPavel Emelyanov 	};
1594008e97fSJason Wang 	struct list_head next;
1604008e97fSJason Wang 	struct tun_struct *detached;
161631ab46bSEric W. Biederman };
162631ab46bSEric W. Biederman 
16396442e42SJason Wang struct tun_flow_entry {
16496442e42SJason Wang 	struct hlist_node hash_link;
16596442e42SJason Wang 	struct rcu_head rcu;
16696442e42SJason Wang 	struct tun_struct *tun;
16796442e42SJason Wang 
16896442e42SJason Wang 	u32 rxhash;
1699bc88939STom Herbert 	u32 rps_rxhash;
17096442e42SJason Wang 	int queue_index;
17196442e42SJason Wang 	unsigned long updated;
17296442e42SJason Wang };
17396442e42SJason Wang 
17496442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024
17596442e42SJason Wang 
17654f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
17736fe8c09SRami Rosen  * device, socket filter, sndbuf and vnet header size were restore when the
17854f968d6SJason Wang  * file were attached to a persist device.
17954f968d6SJason Wang  */
18014daa021SRusty Russell struct tun_struct {
181c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
182c8d68e6bSJason Wang 	unsigned int            numqueues;
183f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1840625c883SEric W. Biederman 	kuid_t			owner;
1850625c883SEric W. Biederman 	kgid_t			group;
18614daa021SRusty Russell 
18714daa021SRusty Russell 	struct net_device	*dev;
188c8f44affSMichał Mirosław 	netdev_features_t	set_features;
18988255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
1903d0ad094SBen Hutchings 			  NETIF_F_TSO6)
191d9d52b51SMichael S. Tsirkin 
192d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
19354f968d6SJason Wang 	int			sndbuf;
19454f968d6SJason Wang 	struct tap_filter	txflt;
19554f968d6SJason Wang 	struct sock_fprog	fprog;
19654f968d6SJason Wang 	/* protected by rtnl lock */
19754f968d6SJason Wang 	bool			filter_attached;
19814daa021SRusty Russell #ifdef TUN_DEBUG
19914daa021SRusty Russell 	int debug;
20014daa021SRusty Russell #endif
20196442e42SJason Wang 	spinlock_t lock;
20296442e42SJason Wang 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
20396442e42SJason Wang 	struct timer_list flow_gc_timer;
20496442e42SJason Wang 	unsigned long ageing_time;
2054008e97fSJason Wang 	unsigned int numdisabled;
2064008e97fSJason Wang 	struct list_head disabled;
2075dbbaf2dSPaul Moore 	void *security;
208b8732fb7SJason Wang 	u32 flow_count;
20914daa021SRusty Russell };
21014daa021SRusty Russell 
21156f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
21256f0dcc5SMichael S. Tsirkin {
213*1cf8e410SMichael S. Tsirkin 	return __virtio16_to_cpu(tun->flags & TUN_VNET_LE, val);
21456f0dcc5SMichael S. Tsirkin }
21556f0dcc5SMichael S. Tsirkin 
21656f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
21756f0dcc5SMichael S. Tsirkin {
218*1cf8e410SMichael S. Tsirkin 	return __cpu_to_virtio16(tun->flags & TUN_VNET_LE, val);
21956f0dcc5SMichael S. Tsirkin }
22056f0dcc5SMichael S. Tsirkin 
22196442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash)
22296442e42SJason Wang {
22396442e42SJason Wang 	return rxhash & 0x3ff;
22496442e42SJason Wang }
22596442e42SJason Wang 
22696442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
22796442e42SJason Wang {
22896442e42SJason Wang 	struct tun_flow_entry *e;
22996442e42SJason Wang 
230b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(e, head, hash_link) {
23196442e42SJason Wang 		if (e->rxhash == rxhash)
23296442e42SJason Wang 			return e;
23396442e42SJason Wang 	}
23496442e42SJason Wang 	return NULL;
23596442e42SJason Wang }
23696442e42SJason Wang 
23796442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
23896442e42SJason Wang 					      struct hlist_head *head,
23996442e42SJason Wang 					      u32 rxhash, u16 queue_index)
24096442e42SJason Wang {
2419fdc6befSEric Dumazet 	struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
2429fdc6befSEric Dumazet 
24396442e42SJason Wang 	if (e) {
24496442e42SJason Wang 		tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
24596442e42SJason Wang 			  rxhash, queue_index);
24696442e42SJason Wang 		e->updated = jiffies;
24796442e42SJason Wang 		e->rxhash = rxhash;
2489bc88939STom Herbert 		e->rps_rxhash = 0;
24996442e42SJason Wang 		e->queue_index = queue_index;
25096442e42SJason Wang 		e->tun = tun;
25196442e42SJason Wang 		hlist_add_head_rcu(&e->hash_link, head);
252b8732fb7SJason Wang 		++tun->flow_count;
25396442e42SJason Wang 	}
25496442e42SJason Wang 	return e;
25596442e42SJason Wang }
25696442e42SJason Wang 
25796442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
25896442e42SJason Wang {
25996442e42SJason Wang 	tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
26096442e42SJason Wang 		  e->rxhash, e->queue_index);
2619bc88939STom Herbert 	sock_rps_reset_flow_hash(e->rps_rxhash);
26296442e42SJason Wang 	hlist_del_rcu(&e->hash_link);
2639fdc6befSEric Dumazet 	kfree_rcu(e, rcu);
264b8732fb7SJason Wang 	--tun->flow_count;
26596442e42SJason Wang }
26696442e42SJason Wang 
26796442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun)
26896442e42SJason Wang {
26996442e42SJason Wang 	int i;
27096442e42SJason Wang 
27196442e42SJason Wang 	spin_lock_bh(&tun->lock);
27296442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
27396442e42SJason Wang 		struct tun_flow_entry *e;
274b67bfe0dSSasha Levin 		struct hlist_node *n;
27596442e42SJason Wang 
276b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
27796442e42SJason Wang 			tun_flow_delete(tun, e);
27896442e42SJason Wang 	}
27996442e42SJason Wang 	spin_unlock_bh(&tun->lock);
28096442e42SJason Wang }
28196442e42SJason Wang 
28296442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
28396442e42SJason Wang {
28496442e42SJason Wang 	int i;
28596442e42SJason Wang 
28696442e42SJason Wang 	spin_lock_bh(&tun->lock);
28796442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
28896442e42SJason Wang 		struct tun_flow_entry *e;
289b67bfe0dSSasha Levin 		struct hlist_node *n;
29096442e42SJason Wang 
291b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
29296442e42SJason Wang 			if (e->queue_index == queue_index)
29396442e42SJason Wang 				tun_flow_delete(tun, e);
29496442e42SJason Wang 		}
29596442e42SJason Wang 	}
29696442e42SJason Wang 	spin_unlock_bh(&tun->lock);
29796442e42SJason Wang }
29896442e42SJason Wang 
29996442e42SJason Wang static void tun_flow_cleanup(unsigned long data)
30096442e42SJason Wang {
30196442e42SJason Wang 	struct tun_struct *tun = (struct tun_struct *)data;
30296442e42SJason Wang 	unsigned long delay = tun->ageing_time;
30396442e42SJason Wang 	unsigned long next_timer = jiffies + delay;
30496442e42SJason Wang 	unsigned long count = 0;
30596442e42SJason Wang 	int i;
30696442e42SJason Wang 
30796442e42SJason Wang 	tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
30896442e42SJason Wang 
30996442e42SJason Wang 	spin_lock_bh(&tun->lock);
31096442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
31196442e42SJason Wang 		struct tun_flow_entry *e;
312b67bfe0dSSasha Levin 		struct hlist_node *n;
31396442e42SJason Wang 
314b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
31596442e42SJason Wang 			unsigned long this_timer;
31696442e42SJason Wang 			count++;
31796442e42SJason Wang 			this_timer = e->updated + delay;
31896442e42SJason Wang 			if (time_before_eq(this_timer, jiffies))
31996442e42SJason Wang 				tun_flow_delete(tun, e);
32096442e42SJason Wang 			else if (time_before(this_timer, next_timer))
32196442e42SJason Wang 				next_timer = this_timer;
32296442e42SJason Wang 		}
32396442e42SJason Wang 	}
32496442e42SJason Wang 
32596442e42SJason Wang 	if (count)
32696442e42SJason Wang 		mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
32796442e42SJason Wang 	spin_unlock_bh(&tun->lock);
32896442e42SJason Wang }
32996442e42SJason Wang 
33049974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
3319e85722dSJason Wang 			    struct tun_file *tfile)
33296442e42SJason Wang {
33396442e42SJason Wang 	struct hlist_head *head;
33496442e42SJason Wang 	struct tun_flow_entry *e;
33596442e42SJason Wang 	unsigned long delay = tun->ageing_time;
3369e85722dSJason Wang 	u16 queue_index = tfile->queue_index;
33796442e42SJason Wang 
33896442e42SJason Wang 	if (!rxhash)
33996442e42SJason Wang 		return;
34096442e42SJason Wang 	else
34196442e42SJason Wang 		head = &tun->flows[tun_hashfn(rxhash)];
34296442e42SJason Wang 
34396442e42SJason Wang 	rcu_read_lock();
34496442e42SJason Wang 
3459e85722dSJason Wang 	/* We may get a very small possibility of OOO during switching, not
3469e85722dSJason Wang 	 * worth to optimize.*/
3479e85722dSJason Wang 	if (tun->numqueues == 1 || tfile->detached)
34896442e42SJason Wang 		goto unlock;
34996442e42SJason Wang 
35096442e42SJason Wang 	e = tun_flow_find(head, rxhash);
35196442e42SJason Wang 	if (likely(e)) {
35296442e42SJason Wang 		/* TODO: keep queueing to old queue until it's empty? */
35396442e42SJason Wang 		e->queue_index = queue_index;
35496442e42SJason Wang 		e->updated = jiffies;
3559bc88939STom Herbert 		sock_rps_record_flow_hash(e->rps_rxhash);
35696442e42SJason Wang 	} else {
35796442e42SJason Wang 		spin_lock_bh(&tun->lock);
358b8732fb7SJason Wang 		if (!tun_flow_find(head, rxhash) &&
359b8732fb7SJason Wang 		    tun->flow_count < MAX_TAP_FLOWS)
36096442e42SJason Wang 			tun_flow_create(tun, head, rxhash, queue_index);
36196442e42SJason Wang 
36296442e42SJason Wang 		if (!timer_pending(&tun->flow_gc_timer))
36396442e42SJason Wang 			mod_timer(&tun->flow_gc_timer,
36496442e42SJason Wang 				  round_jiffies_up(jiffies + delay));
36596442e42SJason Wang 		spin_unlock_bh(&tun->lock);
36696442e42SJason Wang 	}
36796442e42SJason Wang 
36896442e42SJason Wang unlock:
36996442e42SJason Wang 	rcu_read_unlock();
37096442e42SJason Wang }
37196442e42SJason Wang 
3729bc88939STom Herbert /**
3739bc88939STom Herbert  * Save the hash received in the stack receive path and update the
3749bc88939STom Herbert  * flow_hash table accordingly.
3759bc88939STom Herbert  */
3769bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
3779bc88939STom Herbert {
3789bc88939STom Herbert 	if (unlikely(e->rps_rxhash != hash)) {
3799bc88939STom Herbert 		sock_rps_reset_flow_hash(e->rps_rxhash);
3809bc88939STom Herbert 		e->rps_rxhash = hash;
3819bc88939STom Herbert 	}
3829bc88939STom Herbert }
3839bc88939STom Herbert 
384c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
38592d4ea6eSstephen hemminger  * we do not check rxq no. is because some cards(e.g 82599), chooses
386c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
387c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
388c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
389c8d68e6bSJason Wang  * hope the rxq no. may help here.
390c8d68e6bSJason Wang  */
391f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
39299932d4fSDaniel Borkmann 			    void *accel_priv, select_queue_fallback_t fallback)
393c8d68e6bSJason Wang {
394c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
39596442e42SJason Wang 	struct tun_flow_entry *e;
396c8d68e6bSJason Wang 	u32 txq = 0;
397c8d68e6bSJason Wang 	u32 numqueues = 0;
398c8d68e6bSJason Wang 
399c8d68e6bSJason Wang 	rcu_read_lock();
40092bb73eaSJason Wang 	numqueues = ACCESS_ONCE(tun->numqueues);
401c8d68e6bSJason Wang 
4023958afa1STom Herbert 	txq = skb_get_hash(skb);
403c8d68e6bSJason Wang 	if (txq) {
40496442e42SJason Wang 		e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
4059bc88939STom Herbert 		if (e) {
4069bc88939STom Herbert 			tun_flow_save_rps_rxhash(e, txq);
407fbe4d456SZhi Yong Wu 			txq = e->queue_index;
4089bc88939STom Herbert 		} else
409c8d68e6bSJason Wang 			/* use multiply and shift instead of expensive divide */
410c8d68e6bSJason Wang 			txq = ((u64)txq * numqueues) >> 32;
411c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
412c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
413c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
414c8d68e6bSJason Wang 			txq -= numqueues;
415c8d68e6bSJason Wang 	}
416c8d68e6bSJason Wang 
417c8d68e6bSJason Wang 	rcu_read_unlock();
418c8d68e6bSJason Wang 	return txq;
419c8d68e6bSJason Wang }
420c8d68e6bSJason Wang 
421cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
422cde8b15fSJason Wang {
423cde8b15fSJason Wang 	const struct cred *cred = current_cred();
424c260b772SEric W. Biederman 	struct net *net = dev_net(tun->dev);
425cde8b15fSJason Wang 
426cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
427cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
428c260b772SEric W. Biederman 		!ns_capable(net->user_ns, CAP_NET_ADMIN);
429cde8b15fSJason Wang }
430cde8b15fSJason Wang 
431c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
432c8d68e6bSJason Wang {
433c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
434c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
435c8d68e6bSJason Wang }
436c8d68e6bSJason Wang 
4374008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
4384008e97fSJason Wang {
4394008e97fSJason Wang 	tfile->detached = tun;
4404008e97fSJason Wang 	list_add_tail(&tfile->next, &tun->disabled);
4414008e97fSJason Wang 	++tun->numdisabled;
4424008e97fSJason Wang }
4434008e97fSJason Wang 
444d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
4454008e97fSJason Wang {
4464008e97fSJason Wang 	struct tun_struct *tun = tfile->detached;
4474008e97fSJason Wang 
4484008e97fSJason Wang 	tfile->detached = NULL;
4494008e97fSJason Wang 	list_del_init(&tfile->next);
4504008e97fSJason Wang 	--tun->numdisabled;
4514008e97fSJason Wang 	return tun;
4524008e97fSJason Wang }
4534008e97fSJason Wang 
4544bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile)
4554bfb0513SJason Wang {
4564bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_receive_queue);
4574bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_error_queue);
4584bfb0513SJason Wang }
4594bfb0513SJason Wang 
460c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
461c8d68e6bSJason Wang {
462c8d68e6bSJason Wang 	struct tun_file *ntfile;
463c8d68e6bSJason Wang 	struct tun_struct *tun;
464c8d68e6bSJason Wang 
465b8deabd3SJason Wang 	tun = rtnl_dereference(tfile->tun);
466b8deabd3SJason Wang 
4679e85722dSJason Wang 	if (tun && !tfile->detached) {
468c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
469c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
470c8d68e6bSJason Wang 
471c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
472c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
473b8deabd3SJason Wang 		ntfile = rtnl_dereference(tun->tfiles[index]);
474c8d68e6bSJason Wang 		ntfile->queue_index = index;
475c8d68e6bSJason Wang 
476c8d68e6bSJason Wang 		--tun->numqueues;
4779e85722dSJason Wang 		if (clean) {
478c956674bSMonam Agarwal 			RCU_INIT_POINTER(tfile->tun, NULL);
479c8d68e6bSJason Wang 			sock_put(&tfile->sk);
4809e85722dSJason Wang 		} else
4814008e97fSJason Wang 			tun_disable_queue(tun, tfile);
482c8d68e6bSJason Wang 
483c8d68e6bSJason Wang 		synchronize_net();
48496442e42SJason Wang 		tun_flow_delete_by_queue(tun, tun->numqueues + 1);
485c8d68e6bSJason Wang 		/* Drop read queue */
4864bfb0513SJason Wang 		tun_queue_purge(tfile);
487c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
488dd38bd85SJason Wang 	} else if (tfile->detached && clean) {
4894008e97fSJason Wang 		tun = tun_enable_queue(tfile);
490dd38bd85SJason Wang 		sock_put(&tfile->sk);
491dd38bd85SJason Wang 	}
492c8d68e6bSJason Wang 
493c8d68e6bSJason Wang 	if (clean) {
494af668b3cSMichael S. Tsirkin 		if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
495af668b3cSMichael S. Tsirkin 			netif_carrier_off(tun->dev);
496af668b3cSMichael S. Tsirkin 
49740630b82SMichael S. Tsirkin 			if (!(tun->flags & IFF_PERSIST) &&
498af668b3cSMichael S. Tsirkin 			    tun->dev->reg_state == NETREG_REGISTERED)
4994008e97fSJason Wang 				unregister_netdevice(tun->dev);
500af668b3cSMichael S. Tsirkin 		}
5014008e97fSJason Wang 
502c8d68e6bSJason Wang 		BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
503c8d68e6bSJason Wang 				 &tfile->socket.flags));
504c8d68e6bSJason Wang 		sk_release_kernel(&tfile->sk);
505c8d68e6bSJason Wang 	}
506c8d68e6bSJason Wang }
507c8d68e6bSJason Wang 
508c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
509c8d68e6bSJason Wang {
510c8d68e6bSJason Wang 	rtnl_lock();
511c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
512c8d68e6bSJason Wang 	rtnl_unlock();
513c8d68e6bSJason Wang }
514c8d68e6bSJason Wang 
515c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
516c8d68e6bSJason Wang {
517c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
5184008e97fSJason Wang 	struct tun_file *tfile, *tmp;
519c8d68e6bSJason Wang 	int i, n = tun->numqueues;
520c8d68e6bSJason Wang 
521c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
522b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
523c8d68e6bSJason Wang 		BUG_ON(!tfile);
5249e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
525c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
526c8d68e6bSJason Wang 		--tun->numqueues;
527c8d68e6bSJason Wang 	}
5289e85722dSJason Wang 	list_for_each_entry(tfile, &tun->disabled, next) {
5299e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
530c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
5319e85722dSJason Wang 	}
532c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
533c8d68e6bSJason Wang 
534c8d68e6bSJason Wang 	synchronize_net();
535c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
536b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
537c8d68e6bSJason Wang 		/* Drop read queue */
5384bfb0513SJason Wang 		tun_queue_purge(tfile);
539c8d68e6bSJason Wang 		sock_put(&tfile->sk);
540c8d68e6bSJason Wang 	}
5414008e97fSJason Wang 	list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
5424008e97fSJason Wang 		tun_enable_queue(tfile);
5434bfb0513SJason Wang 		tun_queue_purge(tfile);
5444008e97fSJason Wang 		sock_put(&tfile->sk);
5454008e97fSJason Wang 	}
5464008e97fSJason Wang 	BUG_ON(tun->numdisabled != 0);
547dd38bd85SJason Wang 
54840630b82SMichael S. Tsirkin 	if (tun->flags & IFF_PERSIST)
549dd38bd85SJason Wang 		module_put(THIS_MODULE);
550c8d68e6bSJason Wang }
551c8d68e6bSJason Wang 
552849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
553a7385ba2SEric W. Biederman {
554631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
55538231b7aSEric W. Biederman 	int err;
556a7385ba2SEric W. Biederman 
5575dbbaf2dSPaul Moore 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
5585dbbaf2dSPaul Moore 	if (err < 0)
5595dbbaf2dSPaul Moore 		goto out;
5605dbbaf2dSPaul Moore 
56138231b7aSEric W. Biederman 	err = -EINVAL;
5629e85722dSJason Wang 	if (rtnl_dereference(tfile->tun) && !tfile->detached)
56338231b7aSEric W. Biederman 		goto out;
56438231b7aSEric W. Biederman 
56538231b7aSEric W. Biederman 	err = -EBUSY;
56640630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
567c8d68e6bSJason Wang 		goto out;
568c8d68e6bSJason Wang 
569c8d68e6bSJason Wang 	err = -E2BIG;
5704008e97fSJason Wang 	if (!tfile->detached &&
5714008e97fSJason Wang 	    tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
57238231b7aSEric W. Biederman 		goto out;
57338231b7aSEric W. Biederman 
57438231b7aSEric W. Biederman 	err = 0;
57554f968d6SJason Wang 
57692d4ea6eSstephen hemminger 	/* Re-attach the filter to persist device */
577849c9b6fSPavel Emelyanov 	if (!skip_filter && (tun->filter_attached == true)) {
57854f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
57954f968d6SJason Wang 		if (!err)
58054f968d6SJason Wang 			goto out;
58154f968d6SJason Wang 	}
582c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
5836e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
584c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
585c8d68e6bSJason Wang 	tun->numqueues++;
586c8d68e6bSJason Wang 
5874008e97fSJason Wang 	if (tfile->detached)
5884008e97fSJason Wang 		tun_enable_queue(tfile);
5894008e97fSJason Wang 	else
5904008e97fSJason Wang 		sock_hold(&tfile->sk);
5914008e97fSJason Wang 
592c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
593c8d68e6bSJason Wang 
594c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
595c8d68e6bSJason Wang 	 * refcnt.
596c8d68e6bSJason Wang 	 */
597a7385ba2SEric W. Biederman 
59838231b7aSEric W. Biederman out:
59938231b7aSEric W. Biederman 	return err;
600a7385ba2SEric W. Biederman }
601a7385ba2SEric W. Biederman 
602631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
603631ab46bSEric W. Biederman {
6046e914fc7SJason Wang 	struct tun_struct *tun;
605c70f1829SEric W. Biederman 
6066e914fc7SJason Wang 	rcu_read_lock();
6076e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
6086e914fc7SJason Wang 	if (tun)
6096e914fc7SJason Wang 		dev_hold(tun->dev);
6106e914fc7SJason Wang 	rcu_read_unlock();
611c70f1829SEric W. Biederman 
612c70f1829SEric W. Biederman 	return tun;
613631ab46bSEric W. Biederman }
614631ab46bSEric W. Biederman 
615631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
616631ab46bSEric W. Biederman {
617631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
618631ab46bSEric W. Biederman }
619631ab46bSEric W. Biederman 
620631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
621631ab46bSEric W. Biederman {
6226e914fc7SJason Wang 	dev_put(tun->dev);
623631ab46bSEric W. Biederman }
624631ab46bSEric W. Biederman 
6256b8a66eeSJoe Perches /* TAP filtering */
626f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
627f271b2ccSMax Krasnyansky {
628f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
629f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
630f271b2ccSMax Krasnyansky }
631f271b2ccSMax Krasnyansky 
632f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
633f271b2ccSMax Krasnyansky {
634f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
635f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
636f271b2ccSMax Krasnyansky }
637f271b2ccSMax Krasnyansky 
638f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
639f271b2ccSMax Krasnyansky {
640f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
641f271b2ccSMax Krasnyansky 	struct tun_filter uf;
642f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
643f271b2ccSMax Krasnyansky 
644f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
645f271b2ccSMax Krasnyansky 		return -EFAULT;
646f271b2ccSMax Krasnyansky 
647f271b2ccSMax Krasnyansky 	if (!uf.count) {
648f271b2ccSMax Krasnyansky 		/* Disabled */
649f271b2ccSMax Krasnyansky 		filter->count = 0;
650f271b2ccSMax Krasnyansky 		return 0;
651f271b2ccSMax Krasnyansky 	}
652f271b2ccSMax Krasnyansky 
653f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
654f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
655f271b2ccSMax Krasnyansky 	if (!addr)
656f271b2ccSMax Krasnyansky 		return -ENOMEM;
657f271b2ccSMax Krasnyansky 
658f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
659f271b2ccSMax Krasnyansky 		err = -EFAULT;
660f271b2ccSMax Krasnyansky 		goto done;
661f271b2ccSMax Krasnyansky 	}
662f271b2ccSMax Krasnyansky 
663f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
664f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
665f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
666f271b2ccSMax Krasnyansky 	filter->count = 0;
667f271b2ccSMax Krasnyansky 	wmb();
668f271b2ccSMax Krasnyansky 
669f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
670f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
671f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
672f271b2ccSMax Krasnyansky 
673f271b2ccSMax Krasnyansky 	nexact = n;
674f271b2ccSMax Krasnyansky 
675cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
676cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
677f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
678cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
679cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
680cfbf84fcSAlex Williamson 			err = 0; /* no filter */
681cfbf84fcSAlex Williamson 			goto done;
682cfbf84fcSAlex Williamson 		}
683f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
684cfbf84fcSAlex Williamson 	}
685f271b2ccSMax Krasnyansky 
686f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
687f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
688f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
689f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
690f271b2ccSMax Krasnyansky 
691f271b2ccSMax Krasnyansky 	/* Now enable the filter */
692f271b2ccSMax Krasnyansky 	wmb();
693f271b2ccSMax Krasnyansky 	filter->count = nexact;
694f271b2ccSMax Krasnyansky 
695f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
696f271b2ccSMax Krasnyansky 	err = nexact;
697f271b2ccSMax Krasnyansky 
698f271b2ccSMax Krasnyansky done:
699f271b2ccSMax Krasnyansky 	kfree(addr);
700f271b2ccSMax Krasnyansky 	return err;
701f271b2ccSMax Krasnyansky }
702f271b2ccSMax Krasnyansky 
703f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
704f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
705f271b2ccSMax Krasnyansky {
706f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
707f271b2ccSMax Krasnyansky 	 * at this point. */
708f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
709f271b2ccSMax Krasnyansky 	int i;
710f271b2ccSMax Krasnyansky 
711f271b2ccSMax Krasnyansky 	/* Exact match */
712f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
7132e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
714f271b2ccSMax Krasnyansky 			return 1;
715f271b2ccSMax Krasnyansky 
716f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
717f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
718f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
719f271b2ccSMax Krasnyansky 
720f271b2ccSMax Krasnyansky 	return 0;
721f271b2ccSMax Krasnyansky }
722f271b2ccSMax Krasnyansky 
723f271b2ccSMax Krasnyansky /*
724f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
725f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
726f271b2ccSMax Krasnyansky  */
727f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
728f271b2ccSMax Krasnyansky {
729f271b2ccSMax Krasnyansky 	if (!filter->count)
730f271b2ccSMax Krasnyansky 		return 1;
731f271b2ccSMax Krasnyansky 
732f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
733f271b2ccSMax Krasnyansky }
734f271b2ccSMax Krasnyansky 
7351da177e4SLinus Torvalds /* Network device part of the driver */
7361da177e4SLinus Torvalds 
7377282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
7381da177e4SLinus Torvalds 
739c70f1829SEric W. Biederman /* Net device detach from fd. */
740c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
741c70f1829SEric W. Biederman {
742c8d68e6bSJason Wang 	tun_detach_all(dev);
743c70f1829SEric W. Biederman }
744c70f1829SEric W. Biederman 
7451da177e4SLinus Torvalds /* Net device open. */
7461da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
7471da177e4SLinus Torvalds {
748c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
7491da177e4SLinus Torvalds 	return 0;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /* Net device close. */
7531da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
7541da177e4SLinus Torvalds {
755c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
7561da177e4SLinus Torvalds 	return 0;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /* Net device start xmit */
760424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
763c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
7646e914fc7SJason Wang 	struct tun_file *tfile;
765fa35864eSDominic Curran 	u32 numqueues = 0;
7661da177e4SLinus Torvalds 
7676e914fc7SJason Wang 	rcu_read_lock();
768c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
769fa35864eSDominic Curran 	numqueues = ACCESS_ONCE(tun->numqueues);
770c8d68e6bSJason Wang 
7711da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
772fa35864eSDominic Curran 	if (txq >= numqueues)
7731da177e4SLinus Torvalds 		goto drop;
7741da177e4SLinus Torvalds 
775fa35864eSDominic Curran 	if (numqueues == 1) {
7769bc88939STom Herbert 		/* Select queue was not called for the skbuff, so we extract the
7779bc88939STom Herbert 		 * RPS hash and save it into the flow_table here.
7789bc88939STom Herbert 		 */
7799bc88939STom Herbert 		__u32 rxhash;
7809bc88939STom Herbert 
7819bc88939STom Herbert 		rxhash = skb_get_hash(skb);
7829bc88939STom Herbert 		if (rxhash) {
7839bc88939STom Herbert 			struct tun_flow_entry *e;
7849bc88939STom Herbert 			e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
7859bc88939STom Herbert 					rxhash);
7869bc88939STom Herbert 			if (e)
7879bc88939STom Herbert 				tun_flow_save_rps_rxhash(e, rxhash);
7889bc88939STom Herbert 		}
7899bc88939STom Herbert 	}
7909bc88939STom Herbert 
7916e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
7926e914fc7SJason Wang 
793c8d68e6bSJason Wang 	BUG_ON(!tfile);
794c8d68e6bSJason Wang 
795f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
796f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
797f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
798f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
799f271b2ccSMax Krasnyansky 		goto drop;
800f271b2ccSMax Krasnyansky 
80154f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
80254f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
80399405162SMichael S. Tsirkin 		goto drop;
80499405162SMichael S. Tsirkin 
80536fe8c09SRami Rosen 	/* Limit the number of packets queued by dividing txq length with the
806c8d68e6bSJason Wang 	 * number of queues.
807c8d68e6bSJason Wang 	 */
808fa35864eSDominic Curran 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
809fa35864eSDominic Curran 			  >= dev->tx_queue_len)
8101da177e4SLinus Torvalds 		goto drop;
8111da177e4SLinus Torvalds 
8127bf66305SJason Wang 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
8137bf66305SJason Wang 		goto drop;
8147bf66305SJason Wang 
815eda29772SRichard Cochran 	if (skb->sk) {
816eda29772SRichard Cochran 		sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
817eda29772SRichard Cochran 		sw_tx_timestamp(skb);
818eda29772SRichard Cochran 	}
819eda29772SRichard Cochran 
8200110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
8217bf66305SJason Wang 	 * for indefinite time.
8227bf66305SJason Wang 	 */
8230110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
8240110d6f2SMichael S. Tsirkin 
825f8af75f3SEric Dumazet 	nf_reset(skb);
826f8af75f3SEric Dumazet 
827f271b2ccSMax Krasnyansky 	/* Enqueue packet */
82854f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	/* Notify and wake up reader process */
83154f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
83254f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
8339e641bdcSXi Wang 	tfile->socket.sk->sk_data_ready(tfile->socket.sk);
8346e914fc7SJason Wang 
8356e914fc7SJason Wang 	rcu_read_unlock();
8366ed10654SPatrick McHardy 	return NETDEV_TX_OK;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds drop:
83909f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
840149d36f7SMichael S. Tsirkin 	skb_tx_error(skb);
8411da177e4SLinus Torvalds 	kfree_skb(skb);
8426e914fc7SJason Wang 	rcu_read_unlock();
843baeababbSJason Wang 	return NET_XMIT_DROP;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
8471da177e4SLinus Torvalds {
848f271b2ccSMax Krasnyansky 	/*
849f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
850f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
851f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
852f271b2ccSMax Krasnyansky 	 */
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8554885a504SEd Swierk #define MIN_MTU 68
8564885a504SEd Swierk #define MAX_MTU 65535
8574885a504SEd Swierk 
8584885a504SEd Swierk static int
8594885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
8604885a504SEd Swierk {
8614885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
8624885a504SEd Swierk 		return -EINVAL;
8634885a504SEd Swierk 	dev->mtu = new_mtu;
8644885a504SEd Swierk 	return 0;
8654885a504SEd Swierk }
8664885a504SEd Swierk 
867c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
868c8f44affSMichał Mirosław 	netdev_features_t features)
86988255375SMichał Mirosław {
87088255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
87188255375SMichał Mirosław 
87288255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
87388255375SMichał Mirosław }
874bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
875bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
876bebd097aSNeil Horman {
877bebd097aSNeil Horman 	/*
878bebd097aSNeil Horman 	 * Tun only receives frames when:
879bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
880bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
88192d4ea6eSstephen hemminger 	 * Since both of those are synchronous operations, we are guaranteed
882bebd097aSNeil Horman 	 * never to have pending data when we poll for it
88392d4ea6eSstephen hemminger 	 * so there is nothing to do here but return.
884bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
885bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
886bebd097aSNeil Horman 	 * still use netconsole
887bebd097aSNeil Horman 	 */
888bebd097aSNeil Horman 	return;
889bebd097aSNeil Horman }
890bebd097aSNeil Horman #endif
891758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
892c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
893758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
894758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
89500829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
896758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
89788255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
898c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
899bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
900bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
901bebd097aSNeil Horman #endif
902758e43b7SStephen Hemminger };
903758e43b7SStephen Hemminger 
904758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
905c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
906758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
907758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
90800829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
909758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
91088255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
911afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
912758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
913758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
914c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
915bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
916bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
917bebd097aSNeil Horman #endif
918758e43b7SStephen Hemminger };
919758e43b7SStephen Hemminger 
920944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun)
92196442e42SJason Wang {
92296442e42SJason Wang 	int i;
92396442e42SJason Wang 
92496442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
92596442e42SJason Wang 		INIT_HLIST_HEAD(&tun->flows[i]);
92696442e42SJason Wang 
92796442e42SJason Wang 	tun->ageing_time = TUN_FLOW_EXPIRE;
92896442e42SJason Wang 	setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
92996442e42SJason Wang 	mod_timer(&tun->flow_gc_timer,
93096442e42SJason Wang 		  round_jiffies_up(jiffies + tun->ageing_time));
93196442e42SJason Wang }
93296442e42SJason Wang 
93396442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun)
93496442e42SJason Wang {
93596442e42SJason Wang 	del_timer_sync(&tun->flow_gc_timer);
93696442e42SJason Wang 	tun_flow_flush(tun);
93796442e42SJason Wang }
93896442e42SJason Wang 
9391da177e4SLinus Torvalds /* Initialize net device. */
9401da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
94540630b82SMichael S. Tsirkin 	case IFF_TUN:
946758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
947758e43b7SStephen Hemminger 
9481da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
9491da177e4SLinus Torvalds 		dev->hard_header_len = 0;
9501da177e4SLinus Torvalds 		dev->addr_len = 0;
9511da177e4SLinus Torvalds 		dev->mtu = 1500;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 		/* Zero header length */
9541da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
9551da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
9561da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
9571da177e4SLinus Torvalds 		break;
9581da177e4SLinus Torvalds 
95940630b82SMichael S. Tsirkin 	case IFF_TAP:
9607a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
9611da177e4SLinus Torvalds 		/* Ethernet TAP Device */
9621da177e4SLinus Torvalds 		ether_setup(dev);
963550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
964a676847bSstephen hemminger 		dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
96536226a8dSBrian Braunstein 
966f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
96736226a8dSBrian Braunstein 
9681da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
9691da177e4SLinus Torvalds 		break;
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds /* Character device part */
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds /* Poll */
9761da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
9771da177e4SLinus Torvalds {
978b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
979b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
9803c8a9c63SMariusz Kozlowski 	struct sock *sk;
98133dccbb0SHerbert Xu 	unsigned int mask = 0;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	if (!tun)
984eac9e902SEric W. Biederman 		return POLLERR;
9851da177e4SLinus Torvalds 
98654f968d6SJason Wang 	sk = tfile->socket.sk;
9873c8a9c63SMariusz Kozlowski 
9886b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
9891da177e4SLinus Torvalds 
9909e641bdcSXi Wang 	poll_wait(file, sk_sleep(sk), wait);
9911da177e4SLinus Torvalds 
99289f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
9931da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
9941da177e4SLinus Torvalds 
99533dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
99633dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
99733dccbb0SHerbert Xu 	     sock_writeable(sk)))
99833dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
99933dccbb0SHerbert Xu 
1000c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
1001c70f1829SEric W. Biederman 		mask = POLLERR;
1002c70f1829SEric W. Biederman 
1003631ab46bSEric W. Biederman 	tun_put(tun);
10041da177e4SLinus Torvalds 	return mask;
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
1007f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
1008f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
100954f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
101033dccbb0SHerbert Xu 				     size_t prepad, size_t len,
101133dccbb0SHerbert Xu 				     size_t linear, int noblock)
1012f42157cbSRusty Russell {
101354f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
1014f42157cbSRusty Russell 	struct sk_buff *skb;
101533dccbb0SHerbert Xu 	int err;
1016f42157cbSRusty Russell 
1017f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
10180eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
101933dccbb0SHerbert Xu 		linear = len;
1020f42157cbSRusty Russell 
102133dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
102228d64271SEric Dumazet 				   &err, 0);
1023f42157cbSRusty Russell 	if (!skb)
102433dccbb0SHerbert Xu 		return ERR_PTR(err);
1025f42157cbSRusty Russell 
1026f42157cbSRusty Russell 	skb_reserve(skb, prepad);
1027f42157cbSRusty Russell 	skb_put(skb, linear);
102833dccbb0SHerbert Xu 	skb->data_len = len - linear;
102933dccbb0SHerbert Xu 	skb->len += len - linear;
1030f42157cbSRusty Russell 
1031f42157cbSRusty Russell 	return skb;
1032f42157cbSRusty Russell }
1033f42157cbSRusty Russell 
10341da177e4SLinus Torvalds /* Get packet from user space buffer */
103554f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1036f5ff53b4SAl Viro 			    void *msg_control, struct iov_iter *from,
1037f5ff53b4SAl Viro 			    int noblock)
10381da177e4SLinus Torvalds {
103909640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
10401da177e4SLinus Torvalds 	struct sk_buff *skb;
1041f5ff53b4SAl Viro 	size_t total_len = iov_iter_count(from);
10423dd5c330SJason Wang 	size_t len = total_len, align = NET_SKB_PAD, linear;
1043f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
104496f8d9ecSJason Wang 	int good_linear;
10450690899bSMichael S. Tsirkin 	int copylen;
10460690899bSMichael S. Tsirkin 	bool zerocopy = false;
10470690899bSMichael S. Tsirkin 	int err;
104849974420SEric Dumazet 	u32 rxhash;
1049f5ff53b4SAl Viro 	ssize_t n;
10501da177e4SLinus Torvalds 
105140630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
105215718ea0SDan Carpenter 		if (len < sizeof(pi))
10531da177e4SLinus Torvalds 			return -EINVAL;
105415718ea0SDan Carpenter 		len -= sizeof(pi);
10551da177e4SLinus Torvalds 
1056f5ff53b4SAl Viro 		n = copy_from_iter(&pi, sizeof(pi), from);
1057f5ff53b4SAl Viro 		if (n != sizeof(pi))
10581da177e4SLinus Torvalds 			return -EFAULT;
10591da177e4SLinus Torvalds 	}
10601da177e4SLinus Torvalds 
106140630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR) {
106215718ea0SDan Carpenter 		if (len < tun->vnet_hdr_sz)
1063f43798c2SRusty Russell 			return -EINVAL;
106415718ea0SDan Carpenter 		len -= tun->vnet_hdr_sz;
1065f43798c2SRusty Russell 
1066f5ff53b4SAl Viro 		n = copy_from_iter(&gso, sizeof(gso), from);
1067f5ff53b4SAl Viro 		if (n != sizeof(gso))
1068f43798c2SRusty Russell 			return -EFAULT;
1069f43798c2SRusty Russell 
10704909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
107156f0dcc5SMichael S. Tsirkin 		    tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
107256f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
10734909122fSHerbert Xu 
107456f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > len)
1075f43798c2SRusty Russell 			return -EINVAL;
1076d8febb77SHerbert Xu 		iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso));
1077f43798c2SRusty Russell 	}
1078f43798c2SRusty Russell 
107940630b82SMichael S. Tsirkin 	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
1080a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
10810eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
108256f0dcc5SMichael S. Tsirkin 			     (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
1083e01bf1c8SRusty Russell 			return -EINVAL;
1084e01bf1c8SRusty Russell 	}
10851da177e4SLinus Torvalds 
108696f8d9ecSJason Wang 	good_linear = SKB_MAX_HEAD(align);
108796f8d9ecSJason Wang 
108888529176SJason Wang 	if (msg_control) {
1089f5ff53b4SAl Viro 		struct iov_iter i = *from;
1090f5ff53b4SAl Viro 
109188529176SJason Wang 		/* There are 256 bytes to be copied in skb, so there is
109288529176SJason Wang 		 * enough room for skb expand head in case it is used.
10930690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
10940690899bSMichael S. Tsirkin 		 */
109556f0dcc5SMichael S. Tsirkin 		copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
109696f8d9ecSJason Wang 		if (copylen > good_linear)
109796f8d9ecSJason Wang 			copylen = good_linear;
10983dd5c330SJason Wang 		linear = copylen;
1099f5ff53b4SAl Viro 		iov_iter_advance(&i, copylen);
1100f5ff53b4SAl Viro 		if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
110188529176SJason Wang 			zerocopy = true;
110288529176SJason Wang 	}
110388529176SJason Wang 
110488529176SJason Wang 	if (!zerocopy) {
11050690899bSMichael S. Tsirkin 		copylen = len;
110656f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
110796f8d9ecSJason Wang 			linear = good_linear;
110896f8d9ecSJason Wang 		else
110956f0dcc5SMichael S. Tsirkin 			linear = tun16_to_cpu(tun, gso.hdr_len);
11103dd5c330SJason Wang 	}
11110690899bSMichael S. Tsirkin 
11123dd5c330SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
111333dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
111433dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
111509f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
111633dccbb0SHerbert Xu 		return PTR_ERR(skb);
11171da177e4SLinus Torvalds 	}
11181da177e4SLinus Torvalds 
11190690899bSMichael S. Tsirkin 	if (zerocopy)
1120f5ff53b4SAl Viro 		err = zerocopy_sg_from_iter(skb, from);
112188529176SJason Wang 	else {
1122f5ff53b4SAl Viro 		err = skb_copy_datagram_from_iter(skb, 0, from, len);
112388529176SJason Wang 		if (!err && msg_control) {
112488529176SJason Wang 			struct ubuf_info *uarg = msg_control;
112588529176SJason Wang 			uarg->callback(uarg, false);
112688529176SJason Wang 		}
112788529176SJason Wang 	}
11280690899bSMichael S. Tsirkin 
11290690899bSMichael S. Tsirkin 	if (err) {
113009f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
11318f22757eSDave Jones 		kfree_skb(skb);
11321da177e4SLinus Torvalds 		return -EFAULT;
11338f22757eSDave Jones 	}
11341da177e4SLinus Torvalds 
1135f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
113656f0dcc5SMichael S. Tsirkin 		if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
113756f0dcc5SMichael S. Tsirkin 					  tun16_to_cpu(tun, gso.csum_offset))) {
1138f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1139f43798c2SRusty Russell 			kfree_skb(skb);
1140f43798c2SRusty Russell 			return -EINVAL;
1141f43798c2SRusty Russell 		}
114288255375SMichał Mirosław 	}
1143f43798c2SRusty Russell 
11441da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
114540630b82SMichael S. Tsirkin 	case IFF_TUN:
114640630b82SMichael S. Tsirkin 		if (tun->flags & IFF_NO_PI) {
1147f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
1148f09f7ee2SAng Way Chuang 			case 0x40:
1149f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
1150f09f7ee2SAng Way Chuang 				break;
1151f09f7ee2SAng Way Chuang 			case 0x60:
1152f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
1153f09f7ee2SAng Way Chuang 				break;
1154f09f7ee2SAng Way Chuang 			default:
1155f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
1156f09f7ee2SAng Way Chuang 				kfree_skb(skb);
1157f09f7ee2SAng Way Chuang 				return -EINVAL;
1158f09f7ee2SAng Way Chuang 			}
1159f09f7ee2SAng Way Chuang 		}
1160f09f7ee2SAng Way Chuang 
1161459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
11621da177e4SLinus Torvalds 		skb->protocol = pi.proto;
11634c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
11641da177e4SLinus Torvalds 		break;
116540630b82SMichael S. Tsirkin 	case IFF_TAP:
11661da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
11671da177e4SLinus Torvalds 		break;
11686403eab1SJoe Perches 	}
11691da177e4SLinus Torvalds 
11705188cd44SBen Hutchings 	skb_reset_network_header(skb);
11715188cd44SBen Hutchings 
1172f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1173f43798c2SRusty Russell 		pr_debug("GSO!\n");
1174f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1175f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
1176c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1177f43798c2SRusty Russell 			break;
1178f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
1179c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1180f43798c2SRusty Russell 			break;
1181e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
11823d0ad094SBen Hutchings 		{
11833d0ad094SBen Hutchings 			static bool warned;
11843d0ad094SBen Hutchings 
11853d0ad094SBen Hutchings 			if (!warned) {
11863d0ad094SBen Hutchings 				warned = true;
11873d0ad094SBen Hutchings 				netdev_warn(tun->dev,
11883d0ad094SBen Hutchings 					    "%s: using disabled UFO feature; please fix this program\n",
11893d0ad094SBen Hutchings 					    current->comm);
11903d0ad094SBen Hutchings 			}
1191c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
11925188cd44SBen Hutchings 			if (skb->protocol == htons(ETH_P_IPV6))
11935188cd44SBen Hutchings 				ipv6_proxy_select_ident(skb);
1194e36aa25aSSridhar Samudrala 			break;
11953d0ad094SBen Hutchings 		}
1196f43798c2SRusty Russell 		default:
1197f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1198f43798c2SRusty Russell 			kfree_skb(skb);
1199f43798c2SRusty Russell 			return -EINVAL;
1200f43798c2SRusty Russell 		}
1201f43798c2SRusty Russell 
1202f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1203c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1204f43798c2SRusty Russell 
120556f0dcc5SMichael S. Tsirkin 		skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
1206f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
1207f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1208f43798c2SRusty Russell 			kfree_skb(skb);
1209f43798c2SRusty Russell 			return -EINVAL;
1210f43798c2SRusty Russell 		}
1211f43798c2SRusty Russell 
1212f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
1213f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1214f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
1215f43798c2SRusty Russell 	}
12161da177e4SLinus Torvalds 
12170690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
12180690899bSMichael S. Tsirkin 	if (zerocopy) {
12190690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
12200690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1221c9af6db4SPravin B Shelar 		skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
12220690899bSMichael S. Tsirkin 	}
12230690899bSMichael S. Tsirkin 
122440893fd0SJason Wang 	skb_probe_transport_header(skb, 0);
122538502af7SJason Wang 
12263958afa1STom Herbert 	rxhash = skb_get_hash(skb);
12271da177e4SLinus Torvalds 	netif_rx_ni(skb);
12281da177e4SLinus Torvalds 
122909f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
123009f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
12311da177e4SLinus Torvalds 
12329e85722dSJason Wang 	tun_flow_update(tun, rxhash, tfile);
12330690899bSMichael S. Tsirkin 	return total_len;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
1236f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
12371da177e4SLinus Torvalds {
123833dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
1239ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
124054f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
1241631ab46bSEric W. Biederman 	ssize_t result;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	if (!tun)
12441da177e4SLinus Torvalds 		return -EBADFD;
12451da177e4SLinus Torvalds 
1246f5ff53b4SAl Viro 	result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
1247631ab46bSEric W. Biederman 
1248631ab46bSEric W. Biederman 	tun_put(tun);
1249631ab46bSEric W. Biederman 	return result;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds /* Put packet to the user space buffer */
12536f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
125454f968d6SJason Wang 			    struct tun_file *tfile,
12551da177e4SLinus Torvalds 			    struct sk_buff *skb,
1256e0b46d0eSHerbert Xu 			    struct iov_iter *iter)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
1259e0b46d0eSHerbert Xu 	ssize_t total;
12608c847d25SJason Wang 	int vlan_offset = 0;
1261a8f9bfdfSHerbert Xu 	int vlan_hlen = 0;
12622eb783c4SHerbert Xu 	int vnet_hdr_sz = 0;
1263a8f9bfdfSHerbert Xu 
1264a8f9bfdfSHerbert Xu 	if (vlan_tx_tag_present(skb))
1265a8f9bfdfSHerbert Xu 		vlan_hlen = VLAN_HLEN;
12661da177e4SLinus Torvalds 
126740630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR)
12682eb783c4SHerbert Xu 		vnet_hdr_sz = tun->vnet_hdr_sz;
12691da177e4SLinus Torvalds 
1270e0b46d0eSHerbert Xu 	total = skb->len + vlan_hlen + vnet_hdr_sz;
1271e0b46d0eSHerbert Xu 
127240630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
1273e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < sizeof(pi))
12741da177e4SLinus Torvalds 			return -EINVAL;
12751da177e4SLinus Torvalds 
1276e0b46d0eSHerbert Xu 		total += sizeof(pi);
1277e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < total) {
12781da177e4SLinus Torvalds 			/* Packet will be striped */
12791da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
12801da177e4SLinus Torvalds 		}
12811da177e4SLinus Torvalds 
1282e0b46d0eSHerbert Xu 		if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
12831da177e4SLinus Torvalds 			return -EFAULT;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 
12862eb783c4SHerbert Xu 	if (vnet_hdr_sz) {
1287f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1288e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < vnet_hdr_sz)
1289f43798c2SRusty Russell 			return -EINVAL;
1290f43798c2SRusty Russell 
1291f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1292f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1293f43798c2SRusty Russell 
1294f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
129556f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
129656f0dcc5SMichael S. Tsirkin 			gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
1297f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1298f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1299f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1300f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1301ef3db4a5SMichael S. Tsirkin 			else {
13026b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1303ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
130456f0dcc5SMichael S. Tsirkin 				       sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
130556f0dcc5SMichael S. Tsirkin 				       tun16_to_cpu(tun, gso.hdr_len));
1306ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1307ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1308ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
130956f0dcc5SMichael S. Tsirkin 					       min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1310ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1311ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1312ef3db4a5SMichael S. Tsirkin 			}
1313f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1314f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1315f43798c2SRusty Russell 		} else
1316f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1317f43798c2SRusty Russell 
1318f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1319f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
132056f0dcc5SMichael S. Tsirkin 			gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
132156f0dcc5SMichael S. Tsirkin 						      vlan_hlen);
132256f0dcc5SMichael S. Tsirkin 			gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
132310a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
132410a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1325f43798c2SRusty Russell 		} /* else everything is zero */
1326f43798c2SRusty Russell 
1327e0b46d0eSHerbert Xu 		if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
1328f43798c2SRusty Russell 			return -EFAULT;
13298c847d25SJason Wang 
13308c847d25SJason Wang 		iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
1331f43798c2SRusty Russell 	}
1332f43798c2SRusty Russell 
1333a8f9bfdfSHerbert Xu 	if (vlan_hlen) {
1334e0b46d0eSHerbert Xu 		int ret;
13356680ec68SJason Wang 		struct {
13366680ec68SJason Wang 			__be16 h_vlan_proto;
13376680ec68SJason Wang 			__be16 h_vlan_TCI;
13386680ec68SJason Wang 		} veth;
13391da177e4SLinus Torvalds 
13406680ec68SJason Wang 		veth.h_vlan_proto = skb->vlan_proto;
13416680ec68SJason Wang 		veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
13421da177e4SLinus Torvalds 
13436680ec68SJason Wang 		vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
13446680ec68SJason Wang 
1345e0b46d0eSHerbert Xu 		ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1346e0b46d0eSHerbert Xu 		if (ret || !iov_iter_count(iter))
13476680ec68SJason Wang 			goto done;
13486680ec68SJason Wang 
1349e0b46d0eSHerbert Xu 		ret = copy_to_iter(&veth, sizeof(veth), iter);
1350e0b46d0eSHerbert Xu 		if (ret != sizeof(veth) || !iov_iter_count(iter))
13516680ec68SJason Wang 			goto done;
13526680ec68SJason Wang 	}
13536680ec68SJason Wang 
1354e0b46d0eSHerbert Xu 	skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
13556680ec68SJason Wang 
13566680ec68SJason Wang done:
135709f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
1358e0b46d0eSHerbert Xu 	tun->dev->stats.tx_bytes += skb->len + vlan_hlen;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	return total;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
136354f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
13649b067034SAl Viro 			   struct iov_iter *to,
13659b067034SAl Viro 			   int noblock)
13661da177e4SLinus Torvalds {
13671da177e4SLinus Torvalds 	struct sk_buff *skb;
13689b067034SAl Viro 	ssize_t ret;
13699e641bdcSXi Wang 	int peeked, err, off = 0;
13701da177e4SLinus Torvalds 
13713872baf6SRami Rosen 	tun_debug(KERN_INFO, tun, "tun_do_read\n");
13721da177e4SLinus Torvalds 
13739b067034SAl Viro 	if (!iov_iter_count(to))
13749b067034SAl Viro 		return 0;
13751da177e4SLinus Torvalds 
13769e641bdcSXi Wang 	if (tun->dev->reg_state != NETREG_REGISTERED)
13779e641bdcSXi Wang 		return -EIO;
13781da177e4SLinus Torvalds 
13799e641bdcSXi Wang 	/* Read frames from queue */
13809e641bdcSXi Wang 	skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
13819e641bdcSXi Wang 				  &peeked, &off, &err);
1382e0b46d0eSHerbert Xu 	if (!skb)
13839b067034SAl Viro 		return 0;
1384e0b46d0eSHerbert Xu 
13859b067034SAl Viro 	ret = tun_put_user(tun, tfile, skb, to);
1386f51a5e82SJason Wang 	if (unlikely(ret < 0))
13871da177e4SLinus Torvalds 		kfree_skb(skb);
1388f51a5e82SJason Wang 	else
1389f51a5e82SJason Wang 		consume_skb(skb);
13901da177e4SLinus Torvalds 
139105c2828cSMichael S. Tsirkin 	return ret;
139205c2828cSMichael S. Tsirkin }
139305c2828cSMichael S. Tsirkin 
13949b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
139505c2828cSMichael S. Tsirkin {
139605c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
139705c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
139805c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
13999b067034SAl Viro 	ssize_t len = iov_iter_count(to), ret;
140005c2828cSMichael S. Tsirkin 
140105c2828cSMichael S. Tsirkin 	if (!tun)
140205c2828cSMichael S. Tsirkin 		return -EBADFD;
14039b067034SAl Viro 	ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK);
140442404c09SDavid S. Miller 	ret = min_t(ssize_t, ret, len);
1405d0b7da8aSZhi Yong Wu 	if (ret > 0)
1406d0b7da8aSZhi Yong Wu 		iocb->ki_pos = ret;
1407631ab46bSEric W. Biederman 	tun_put(tun);
14081da177e4SLinus Torvalds 	return ret;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
141196442e42SJason Wang static void tun_free_netdev(struct net_device *dev)
141296442e42SJason Wang {
141396442e42SJason Wang 	struct tun_struct *tun = netdev_priv(dev);
141496442e42SJason Wang 
14154008e97fSJason Wang 	BUG_ON(!(list_empty(&tun->disabled)));
141696442e42SJason Wang 	tun_flow_uninit(tun);
14175dbbaf2dSPaul Moore 	security_tun_dev_free_security(tun->security);
141896442e42SJason Wang 	free_netdev(dev);
141996442e42SJason Wang }
142096442e42SJason Wang 
14211da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
14221da177e4SLinus Torvalds {
14231da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
14241da177e4SLinus Torvalds 
14250625c883SEric W. Biederman 	tun->owner = INVALID_UID;
14260625c883SEric W. Biederman 	tun->group = INVALID_GID;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
142996442e42SJason Wang 	dev->destructor = tun_free_netdev;
14301da177e4SLinus Torvalds }
14311da177e4SLinus Torvalds 
1432f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1433f019a7a5SEric W. Biederman  * device with netlink.
1434f019a7a5SEric W. Biederman  */
1435f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1436f019a7a5SEric W. Biederman {
1437f019a7a5SEric W. Biederman 	return -EINVAL;
1438f019a7a5SEric W. Biederman }
1439f019a7a5SEric W. Biederman 
1440f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1441f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1442f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1443f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1444f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1445f019a7a5SEric W. Biederman };
1446f019a7a5SEric W. Biederman 
144733dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
144833dccbb0SHerbert Xu {
144954f968d6SJason Wang 	struct tun_file *tfile;
145043815482SEric Dumazet 	wait_queue_head_t *wqueue;
145133dccbb0SHerbert Xu 
145233dccbb0SHerbert Xu 	if (!sock_writeable(sk))
145333dccbb0SHerbert Xu 		return;
145433dccbb0SHerbert Xu 
145533dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
145633dccbb0SHerbert Xu 		return;
145733dccbb0SHerbert Xu 
145843815482SEric Dumazet 	wqueue = sk_sleep(sk);
145943815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
146043815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
146105c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1462c722c625SHerbert Xu 
146354f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
146454f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
146533dccbb0SHerbert Xu }
146633dccbb0SHerbert Xu 
146705c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
146805c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
146905c2828cSMichael S. Tsirkin {
147054f968d6SJason Wang 	int ret;
147154f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
147254f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
147354f968d6SJason Wang 
147454f968d6SJason Wang 	if (!tun)
147554f968d6SJason Wang 		return -EBADFD;
1476f5ff53b4SAl Viro 
1477c0371da6SAl Viro 	ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
1478f5ff53b4SAl Viro 			   m->msg_flags & MSG_DONTWAIT);
147954f968d6SJason Wang 	tun_put(tun);
148054f968d6SJason Wang 	return ret;
148105c2828cSMichael S. Tsirkin }
148205c2828cSMichael S. Tsirkin 
148305c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
148405c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
148505c2828cSMichael S. Tsirkin 		       int flags)
148605c2828cSMichael S. Tsirkin {
148754f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
148854f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
148905c2828cSMichael S. Tsirkin 	int ret;
149054f968d6SJason Wang 
149154f968d6SJason Wang 	if (!tun)
149254f968d6SJason Wang 		return -EBADFD;
149354f968d6SJason Wang 
1494eda29772SRichard Cochran 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
14953811ae76SGao feng 		ret = -EINVAL;
14963811ae76SGao feng 		goto out;
14973811ae76SGao feng 	}
1498eda29772SRichard Cochran 	if (flags & MSG_ERRQUEUE) {
1499eda29772SRichard Cochran 		ret = sock_recv_errqueue(sock->sk, m, total_len,
1500eda29772SRichard Cochran 					 SOL_PACKET, TUN_TX_TIMESTAMP);
1501eda29772SRichard Cochran 		goto out;
1502eda29772SRichard Cochran 	}
1503c0371da6SAl Viro 	ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT);
150442404c09SDavid S. Miller 	if (ret > total_len) {
150542404c09SDavid S. Miller 		m->msg_flags |= MSG_TRUNC;
150642404c09SDavid S. Miller 		ret = flags & MSG_TRUNC ? ret : total_len;
150742404c09SDavid S. Miller 	}
15083811ae76SGao feng out:
150954f968d6SJason Wang 	tun_put(tun);
151005c2828cSMichael S. Tsirkin 	return ret;
151105c2828cSMichael S. Tsirkin }
151205c2828cSMichael S. Tsirkin 
15131ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
15141ab5ecb9SStanislav Kinsbursky {
15151ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
15161ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
15171ab5ecb9SStanislav Kinsbursky 	return 0;
15181ab5ecb9SStanislav Kinsbursky }
15191ab5ecb9SStanislav Kinsbursky 
152005c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
152105c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
152205c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
152305c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
15241ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
152505c2828cSMichael S. Tsirkin };
152605c2828cSMichael S. Tsirkin 
152733dccbb0SHerbert Xu static struct proto tun_proto = {
152833dccbb0SHerbert Xu 	.name		= "tun",
152933dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
153054f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
153133dccbb0SHerbert Xu };
1532f019a7a5SEric W. Biederman 
1533980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1534980c9e8cSDavid Woodhouse {
1535031f5e03SMichael S. Tsirkin 	return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
1536980c9e8cSDavid Woodhouse }
1537980c9e8cSDavid Woodhouse 
1538980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1539980c9e8cSDavid Woodhouse 			      char *buf)
1540980c9e8cSDavid Woodhouse {
1541980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1542980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1543980c9e8cSDavid Woodhouse }
1544980c9e8cSDavid Woodhouse 
1545980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1546980c9e8cSDavid Woodhouse 			      char *buf)
1547980c9e8cSDavid Woodhouse {
1548980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15490625c883SEric W. Biederman 	return uid_valid(tun->owner)?
15500625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15510625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
15520625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1553980c9e8cSDavid Woodhouse }
1554980c9e8cSDavid Woodhouse 
1555980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1556980c9e8cSDavid Woodhouse 			      char *buf)
1557980c9e8cSDavid Woodhouse {
1558980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15590625c883SEric W. Biederman 	return gid_valid(tun->group) ?
15600625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15610625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
15620625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1563980c9e8cSDavid Woodhouse }
1564980c9e8cSDavid Woodhouse 
1565980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1566980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1567980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1568980c9e8cSDavid Woodhouse 
1569d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
15701da177e4SLinus Torvalds {
15711da177e4SLinus Torvalds 	struct tun_struct *tun;
157254f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
15731da177e4SLinus Torvalds 	struct net_device *dev;
15741da177e4SLinus Torvalds 	int err;
15751da177e4SLinus Torvalds 
15767c0c3b1aSJason Wang 	if (tfile->detached)
15777c0c3b1aSJason Wang 		return -EINVAL;
15787c0c3b1aSJason Wang 
157974a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
158074a3e5a7SEric W. Biederman 	if (dev) {
1581f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1582f85ba780SDavid Woodhouse 			return -EBUSY;
158374a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
158474a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
158574a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
158674a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
158774a3e5a7SEric W. Biederman 		else
158874a3e5a7SEric W. Biederman 			return -EINVAL;
158974a3e5a7SEric W. Biederman 
15908e6d91aeSJason Wang 		if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
159140630b82SMichael S. Tsirkin 		    !!(tun->flags & IFF_MULTI_QUEUE))
15928e6d91aeSJason Wang 			return -EINVAL;
15938e6d91aeSJason Wang 
1594cde8b15fSJason Wang 		if (tun_not_capable(tun))
15952b980dbdSPaul Moore 			return -EPERM;
15965dbbaf2dSPaul Moore 		err = security_tun_dev_open(tun->security);
15972b980dbdSPaul Moore 		if (err < 0)
15982b980dbdSPaul Moore 			return err;
15992b980dbdSPaul Moore 
1600849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
1601a7385ba2SEric W. Biederman 		if (err < 0)
1602a7385ba2SEric W. Biederman 			return err;
16034008e97fSJason Wang 
160440630b82SMichael S. Tsirkin 		if (tun->flags & IFF_MULTI_QUEUE &&
1605e8dbad66SJason Wang 		    (tun->numqueues + tun->numdisabled > 1)) {
1606e8dbad66SJason Wang 			/* One or more queue has already been attached, no need
1607e8dbad66SJason Wang 			 * to initialize the device again.
1608e8dbad66SJason Wang 			 */
1609e8dbad66SJason Wang 			return 0;
1610e8dbad66SJason Wang 		}
161186a264abSDavid Howells 	}
16121da177e4SLinus Torvalds 	else {
16131da177e4SLinus Torvalds 		char *name;
16141da177e4SLinus Torvalds 		unsigned long flags = 0;
1615edfb6a14SJason Wang 		int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1616edfb6a14SJason Wang 			     MAX_TAP_QUEUES : 1;
16171da177e4SLinus Torvalds 
1618c260b772SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1619ca6bb5d7SDavid Woodhouse 			return -EPERM;
16202b980dbdSPaul Moore 		err = security_tun_dev_create();
16212b980dbdSPaul Moore 		if (err < 0)
16222b980dbdSPaul Moore 			return err;
1623ca6bb5d7SDavid Woodhouse 
16241da177e4SLinus Torvalds 		/* Set dev type */
16251da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
16261da177e4SLinus Torvalds 			/* TUN device */
162740630b82SMichael S. Tsirkin 			flags |= IFF_TUN;
16281da177e4SLinus Torvalds 			name = "tun%d";
16291da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
16301da177e4SLinus Torvalds 			/* TAP device */
163140630b82SMichael S. Tsirkin 			flags |= IFF_TAP;
16321da177e4SLinus Torvalds 			name = "tap%d";
16331da177e4SLinus Torvalds 		} else
163436989b90SKusanagi Kouichi 			return -EINVAL;
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 		if (*ifr->ifr_name)
16371da177e4SLinus Torvalds 			name = ifr->ifr_name;
16381da177e4SLinus Torvalds 
1639c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1640c835a677STom Gundersen 				       NET_NAME_UNKNOWN, tun_setup, queues,
1641c835a677STom Gundersen 				       queues);
1642edfb6a14SJason Wang 
16431da177e4SLinus Torvalds 		if (!dev)
16441da177e4SLinus Torvalds 			return -ENOMEM;
16451da177e4SLinus Torvalds 
1646fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1647f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1648fb7589a1SPavel Emelyanov 		dev->ifindex = tfile->ifindex;
1649758e43b7SStephen Hemminger 
16501da177e4SLinus Torvalds 		tun = netdev_priv(dev);
16511da177e4SLinus Torvalds 		tun->dev = dev;
16521da177e4SLinus Torvalds 		tun->flags = flags;
1653f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1654d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
16551da177e4SLinus Torvalds 
165654f968d6SJason Wang 		tun->filter_attached = false;
165754f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
165833dccbb0SHerbert Xu 
165996442e42SJason Wang 		spin_lock_init(&tun->lock);
166096442e42SJason Wang 
16615dbbaf2dSPaul Moore 		err = security_tun_dev_alloc_security(&tun->security);
16625dbbaf2dSPaul Moore 		if (err < 0)
16635dbbaf2dSPaul Moore 			goto err_free_dev;
16642b980dbdSPaul Moore 
16651da177e4SLinus Torvalds 		tun_net_init(dev);
1666944a1376SPavel Emelyanov 		tun_flow_init(tun);
166796442e42SJason Wang 
166888255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
16696680ec68SJason Wang 				   TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
16706680ec68SJason Wang 				   NETIF_F_HW_VLAN_STAG_TX;
167188255375SMichał Mirosław 		dev->features = dev->hw_features;
16726671b224SFernando Luis Vazquez Cao 		dev->vlan_features = dev->features &
16736671b224SFernando Luis Vazquez Cao 				     ~(NETIF_F_HW_VLAN_CTAG_TX |
16746671b224SFernando Luis Vazquez Cao 				       NETIF_F_HW_VLAN_STAG_TX);
167588255375SMichał Mirosław 
16764008e97fSJason Wang 		INIT_LIST_HEAD(&tun->disabled);
1677849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, false);
1678eb0fb363SJason Wang 		if (err < 0)
1679662ca437SJason Wang 			goto err_free_flow;
1680eb0fb363SJason Wang 
16811da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
16821da177e4SLinus Torvalds 		if (err < 0)
1683662ca437SJason Wang 			goto err_detach;
16849c3fea6aSHerbert Xu 
1685980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1686980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1687980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
16886b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1689af668b3cSMichael S. Tsirkin 	}
1690980c9e8cSDavid Woodhouse 
1691eb0fb363SJason Wang 	netif_carrier_on(tun->dev);
16921da177e4SLinus Torvalds 
16936b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
16941da177e4SLinus Torvalds 
1695031f5e03SMichael S. Tsirkin 	tun->flags = (tun->flags & ~TUN_FEATURES) |
1696031f5e03SMichael S. Tsirkin 		(ifr->ifr_flags & TUN_FEATURES);
1697c8d68e6bSJason Wang 
1698e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1699e35259a9SMax Krasnyansky 	 * xoff state.
1700e35259a9SMax Krasnyansky 	 */
1701e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1702c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1703e35259a9SMax Krasnyansky 
17041da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
17051da177e4SLinus Torvalds 	return 0;
17061da177e4SLinus Torvalds 
1707662ca437SJason Wang err_detach:
1708662ca437SJason Wang 	tun_detach_all(dev);
1709662ca437SJason Wang err_free_flow:
1710662ca437SJason Wang 	tun_flow_uninit(tun);
1711662ca437SJason Wang 	security_tun_dev_free_security(tun->security);
17121da177e4SLinus Torvalds err_free_dev:
17131da177e4SLinus Torvalds 	free_netdev(dev);
17141da177e4SLinus Torvalds 	return err;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17179ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun,
1718876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1719e3b99556SMark McLoughlin {
17206b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1721e3b99556SMark McLoughlin 
1722e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1723e3b99556SMark McLoughlin 
1724980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1725e3b99556SMark McLoughlin 
1726e3b99556SMark McLoughlin }
1727e3b99556SMark McLoughlin 
17285228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
17295228ddc9SRusty Russell  * privs required. */
173088255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
17315228ddc9SRusty Russell {
1732c8f44affSMichał Mirosław 	netdev_features_t features = 0;
17335228ddc9SRusty Russell 
17345228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
173588255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
17365228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
17375228ddc9SRusty Russell 
17385228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
17395228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
17405228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
17415228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
17425228ddc9SRusty Russell 			}
17435228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
17445228ddc9SRusty Russell 				features |= NETIF_F_TSO;
17455228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
17465228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
17475228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
17485228ddc9SRusty Russell 		}
17495228ddc9SRusty Russell 	}
17505228ddc9SRusty Russell 
17515228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
17525228ddc9SRusty Russell 	 * trying to set them. */
17535228ddc9SRusty Russell 	if (arg)
17545228ddc9SRusty Russell 		return -EINVAL;
17555228ddc9SRusty Russell 
175688255375SMichał Mirosław 	tun->set_features = features;
175788255375SMichał Mirosław 	netdev_update_features(tun->dev);
17585228ddc9SRusty Russell 
17595228ddc9SRusty Russell 	return 0;
17605228ddc9SRusty Russell }
17615228ddc9SRusty Russell 
1762c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1763c8d68e6bSJason Wang {
1764c8d68e6bSJason Wang 	int i;
1765c8d68e6bSJason Wang 	struct tun_file *tfile;
1766c8d68e6bSJason Wang 
1767c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1768b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1769c8d68e6bSJason Wang 		sk_detach_filter(tfile->socket.sk);
1770c8d68e6bSJason Wang 	}
1771c8d68e6bSJason Wang 
1772c8d68e6bSJason Wang 	tun->filter_attached = false;
1773c8d68e6bSJason Wang }
1774c8d68e6bSJason Wang 
1775c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1776c8d68e6bSJason Wang {
1777c8d68e6bSJason Wang 	int i, ret = 0;
1778c8d68e6bSJason Wang 	struct tun_file *tfile;
1779c8d68e6bSJason Wang 
1780c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1781b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1782c8d68e6bSJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1783c8d68e6bSJason Wang 		if (ret) {
1784c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1785c8d68e6bSJason Wang 			return ret;
1786c8d68e6bSJason Wang 		}
1787c8d68e6bSJason Wang 	}
1788c8d68e6bSJason Wang 
1789c8d68e6bSJason Wang 	tun->filter_attached = true;
1790c8d68e6bSJason Wang 	return ret;
1791c8d68e6bSJason Wang }
1792c8d68e6bSJason Wang 
1793c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1794c8d68e6bSJason Wang {
1795c8d68e6bSJason Wang 	struct tun_file *tfile;
1796c8d68e6bSJason Wang 	int i;
1797c8d68e6bSJason Wang 
1798c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1799b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1800c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1801c8d68e6bSJason Wang 	}
1802c8d68e6bSJason Wang }
1803c8d68e6bSJason Wang 
1804cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1805cde8b15fSJason Wang {
1806cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1807cde8b15fSJason Wang 	struct tun_struct *tun;
1808cde8b15fSJason Wang 	int ret = 0;
1809cde8b15fSJason Wang 
1810cde8b15fSJason Wang 	rtnl_lock();
1811cde8b15fSJason Wang 
1812cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
18134008e97fSJason Wang 		tun = tfile->detached;
18145dbbaf2dSPaul Moore 		if (!tun) {
1815cde8b15fSJason Wang 			ret = -EINVAL;
18165dbbaf2dSPaul Moore 			goto unlock;
18175dbbaf2dSPaul Moore 		}
18185dbbaf2dSPaul Moore 		ret = security_tun_dev_attach_queue(tun->security);
18195dbbaf2dSPaul Moore 		if (ret < 0)
18205dbbaf2dSPaul Moore 			goto unlock;
1821849c9b6fSPavel Emelyanov 		ret = tun_attach(tun, file, false);
18224008e97fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
1823b8deabd3SJason Wang 		tun = rtnl_dereference(tfile->tun);
182440630b82SMichael S. Tsirkin 		if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
18254008e97fSJason Wang 			ret = -EINVAL;
1826cde8b15fSJason Wang 		else
18274008e97fSJason Wang 			__tun_detach(tfile, false);
18284008e97fSJason Wang 	} else
1829cde8b15fSJason Wang 		ret = -EINVAL;
1830cde8b15fSJason Wang 
18315dbbaf2dSPaul Moore unlock:
1832cde8b15fSJason Wang 	rtnl_unlock();
1833cde8b15fSJason Wang 	return ret;
1834cde8b15fSJason Wang }
1835cde8b15fSJason Wang 
183650857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
183750857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
18381da177e4SLinus Torvalds {
183936b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1840631ab46bSEric W. Biederman 	struct tun_struct *tun;
18411da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
18421da177e4SLinus Torvalds 	struct ifreq ifr;
18430625c883SEric W. Biederman 	kuid_t owner;
18440625c883SEric W. Biederman 	kgid_t group;
184533dccbb0SHerbert Xu 	int sndbuf;
1846d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1847fb7589a1SPavel Emelyanov 	unsigned int ifindex;
1848*1cf8e410SMichael S. Tsirkin 	int le;
1849f271b2ccSMax Krasnyansky 	int ret;
18501da177e4SLinus Torvalds 
1851cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
185250857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
18531da177e4SLinus Torvalds 			return -EFAULT;
18548bbb1813SDavid S. Miller 	} else {
1855a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
18568bbb1813SDavid S. Miller 	}
1857631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1858631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1859631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1860031f5e03SMichael S. Tsirkin 		 * TUNSETIFF.
1861031f5e03SMichael S. Tsirkin 		 */
1862031f5e03SMichael S. Tsirkin 		return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
1863631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1864cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
1865cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
1866631ab46bSEric W. Biederman 
1867c8d68e6bSJason Wang 	ret = 0;
1868876bfd4dSHerbert Xu 	rtnl_lock();
1869876bfd4dSHerbert Xu 
187036b50babSEric W. Biederman 	tun = __tun_get(tfile);
18711da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
18721da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
18731da177e4SLinus Torvalds 
1874876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
18751da177e4SLinus Torvalds 
1876876bfd4dSHerbert Xu 		if (ret)
1877876bfd4dSHerbert Xu 			goto unlock;
18781da177e4SLinus Torvalds 
187950857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1880876bfd4dSHerbert Xu 			ret = -EFAULT;
1881876bfd4dSHerbert Xu 		goto unlock;
18821da177e4SLinus Torvalds 	}
1883fb7589a1SPavel Emelyanov 	if (cmd == TUNSETIFINDEX) {
1884fb7589a1SPavel Emelyanov 		ret = -EPERM;
1885fb7589a1SPavel Emelyanov 		if (tun)
1886fb7589a1SPavel Emelyanov 			goto unlock;
1887fb7589a1SPavel Emelyanov 
1888fb7589a1SPavel Emelyanov 		ret = -EFAULT;
1889fb7589a1SPavel Emelyanov 		if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1890fb7589a1SPavel Emelyanov 			goto unlock;
1891fb7589a1SPavel Emelyanov 
1892fb7589a1SPavel Emelyanov 		ret = 0;
1893fb7589a1SPavel Emelyanov 		tfile->ifindex = ifindex;
1894fb7589a1SPavel Emelyanov 		goto unlock;
1895fb7589a1SPavel Emelyanov 	}
18961da177e4SLinus Torvalds 
1897876bfd4dSHerbert Xu 	ret = -EBADFD;
18981da177e4SLinus Torvalds 	if (!tun)
1899876bfd4dSHerbert Xu 		goto unlock;
19001da177e4SLinus Torvalds 
19011e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
19021da177e4SLinus Torvalds 
1903631ab46bSEric W. Biederman 	ret = 0;
19041da177e4SLinus Torvalds 	switch (cmd) {
1905e3b99556SMark McLoughlin 	case TUNGETIFF:
19069ce99cf6SRami Rosen 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1907e3b99556SMark McLoughlin 
19083d407a80SPavel Emelyanov 		if (tfile->detached)
19093d407a80SPavel Emelyanov 			ifr.ifr_flags |= IFF_DETACH_QUEUE;
1910849c9b6fSPavel Emelyanov 		if (!tfile->socket.sk->sk_filter)
1911849c9b6fSPavel Emelyanov 			ifr.ifr_flags |= IFF_NOFILTER;
19123d407a80SPavel Emelyanov 
191350857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1914631ab46bSEric W. Biederman 			ret = -EFAULT;
1915e3b99556SMark McLoughlin 		break;
1916e3b99556SMark McLoughlin 
19171da177e4SLinus Torvalds 	case TUNSETNOCSUM:
19181da177e4SLinus Torvalds 		/* Disable/Enable checksum */
19191da177e4SLinus Torvalds 
192088255375SMichał Mirosław 		/* [unimplemented] */
192188255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
19226b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
19231da177e4SLinus Torvalds 		break;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	case TUNSETPERSIST:
192654f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
192754f968d6SJason Wang 		 * module to prevent the module being unprobed.
192854f968d6SJason Wang 		 */
192940630b82SMichael S. Tsirkin 		if (arg && !(tun->flags & IFF_PERSIST)) {
193040630b82SMichael S. Tsirkin 			tun->flags |= IFF_PERSIST;
193154f968d6SJason Wang 			__module_get(THIS_MODULE);
1932dd38bd85SJason Wang 		}
193340630b82SMichael S. Tsirkin 		if (!arg && (tun->flags & IFF_PERSIST)) {
193440630b82SMichael S. Tsirkin 			tun->flags &= ~IFF_PERSIST;
193554f968d6SJason Wang 			module_put(THIS_MODULE);
193654f968d6SJason Wang 		}
19371da177e4SLinus Torvalds 
19386b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
19396b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
19401da177e4SLinus Torvalds 		break;
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 	case TUNSETOWNER:
19431da177e4SLinus Torvalds 		/* Set owner of the device */
19440625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
19450625c883SEric W. Biederman 		if (!uid_valid(owner)) {
19460625c883SEric W. Biederman 			ret = -EINVAL;
19470625c883SEric W. Biederman 			break;
19480625c883SEric W. Biederman 		}
19490625c883SEric W. Biederman 		tun->owner = owner;
19501e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
19510625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
19521da177e4SLinus Torvalds 		break;
19531da177e4SLinus Torvalds 
19548c644623SGuido Guenther 	case TUNSETGROUP:
19558c644623SGuido Guenther 		/* Set group of the device */
19560625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
19570625c883SEric W. Biederman 		if (!gid_valid(group)) {
19580625c883SEric W. Biederman 			ret = -EINVAL;
19590625c883SEric W. Biederman 			break;
19600625c883SEric W. Biederman 		}
19610625c883SEric W. Biederman 		tun->group = group;
19621e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
19630625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
19648c644623SGuido Guenther 		break;
19658c644623SGuido Guenther 
1966ff4cc3acSMike Kershaw 	case TUNSETLINK:
1967ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1968ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
19696b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
19706b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
197148abfe05SDavid S. Miller 			ret = -EBUSY;
1972ff4cc3acSMike Kershaw 		} else {
1973ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
19746b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
19756b8a66eeSJoe Perches 				  tun->dev->type);
197648abfe05SDavid S. Miller 			ret = 0;
1977ff4cc3acSMike Kershaw 		}
1978631ab46bSEric W. Biederman 		break;
1979ff4cc3acSMike Kershaw 
19801da177e4SLinus Torvalds #ifdef TUN_DEBUG
19811da177e4SLinus Torvalds 	case TUNSETDEBUG:
19821da177e4SLinus Torvalds 		tun->debug = arg;
19831da177e4SLinus Torvalds 		break;
19841da177e4SLinus Torvalds #endif
19855228ddc9SRusty Russell 	case TUNSETOFFLOAD:
198688255375SMichał Mirosław 		ret = set_offload(tun, arg);
1987631ab46bSEric W. Biederman 		break;
19885228ddc9SRusty Russell 
1989f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1990f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1991631ab46bSEric W. Biederman 		ret = -EINVAL;
199240630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1993631ab46bSEric W. Biederman 			break;
1994c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1995631ab46bSEric W. Biederman 		break;
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1998b595076aSUwe Kleine-König 		/* Get hw address */
1999f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2000f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
200150857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
2002631ab46bSEric W. Biederman 			ret = -EFAULT;
2003631ab46bSEric W. Biederman 		break;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
2006f271b2ccSMax Krasnyansky 		/* Set hw address */
20076b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
20086b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
200940102371SKim B. Heino 
201040102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
2011631ab46bSEric W. Biederman 		break;
201233dccbb0SHerbert Xu 
201333dccbb0SHerbert Xu 	case TUNGETSNDBUF:
201454f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
201533dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
201633dccbb0SHerbert Xu 			ret = -EFAULT;
201733dccbb0SHerbert Xu 		break;
201833dccbb0SHerbert Xu 
201933dccbb0SHerbert Xu 	case TUNSETSNDBUF:
202033dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
202133dccbb0SHerbert Xu 			ret = -EFAULT;
202233dccbb0SHerbert Xu 			break;
202333dccbb0SHerbert Xu 		}
202433dccbb0SHerbert Xu 
2025c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
2026c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
202733dccbb0SHerbert Xu 		break;
202833dccbb0SHerbert Xu 
2029d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
2030d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
2031d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2032d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2033d9d52b51SMichael S. Tsirkin 		break;
2034d9d52b51SMichael S. Tsirkin 
2035d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
2036d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2037d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2038d9d52b51SMichael S. Tsirkin 			break;
2039d9d52b51SMichael S. Tsirkin 		}
2040d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2041d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
2042d9d52b51SMichael S. Tsirkin 			break;
2043d9d52b51SMichael S. Tsirkin 		}
2044d9d52b51SMichael S. Tsirkin 
2045d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
2046d9d52b51SMichael S. Tsirkin 		break;
2047d9d52b51SMichael S. Tsirkin 
2048*1cf8e410SMichael S. Tsirkin 	case TUNGETVNETLE:
2049*1cf8e410SMichael S. Tsirkin 		le = !!(tun->flags & TUN_VNET_LE);
2050*1cf8e410SMichael S. Tsirkin 		if (put_user(le, (int __user *)argp))
2051*1cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
2052*1cf8e410SMichael S. Tsirkin 		break;
2053*1cf8e410SMichael S. Tsirkin 
2054*1cf8e410SMichael S. Tsirkin 	case TUNSETVNETLE:
2055*1cf8e410SMichael S. Tsirkin 		if (get_user(le, (int __user *)argp)) {
2056*1cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
2057*1cf8e410SMichael S. Tsirkin 			break;
2058*1cf8e410SMichael S. Tsirkin 		}
2059*1cf8e410SMichael S. Tsirkin 		if (le)
2060*1cf8e410SMichael S. Tsirkin 			tun->flags |= TUN_VNET_LE;
2061*1cf8e410SMichael S. Tsirkin 		else
2062*1cf8e410SMichael S. Tsirkin 			tun->flags &= ~TUN_VNET_LE;
2063*1cf8e410SMichael S. Tsirkin 		break;
2064*1cf8e410SMichael S. Tsirkin 
206599405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
206699405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
206799405162SMichael S. Tsirkin 		ret = -EINVAL;
206840630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
206999405162SMichael S. Tsirkin 			break;
207099405162SMichael S. Tsirkin 		ret = -EFAULT;
207154f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
207299405162SMichael S. Tsirkin 			break;
207399405162SMichael S. Tsirkin 
2074c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
207599405162SMichael S. Tsirkin 		break;
207699405162SMichael S. Tsirkin 
207799405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
207899405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
207999405162SMichael S. Tsirkin 		ret = -EINVAL;
208040630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
208199405162SMichael S. Tsirkin 			break;
2082c8d68e6bSJason Wang 		ret = 0;
2083c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
208499405162SMichael S. Tsirkin 		break;
208599405162SMichael S. Tsirkin 
208676975e9cSPavel Emelyanov 	case TUNGETFILTER:
208776975e9cSPavel Emelyanov 		ret = -EINVAL;
208840630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
208976975e9cSPavel Emelyanov 			break;
209076975e9cSPavel Emelyanov 		ret = -EFAULT;
209176975e9cSPavel Emelyanov 		if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
209276975e9cSPavel Emelyanov 			break;
209376975e9cSPavel Emelyanov 		ret = 0;
209476975e9cSPavel Emelyanov 		break;
209576975e9cSPavel Emelyanov 
20961da177e4SLinus Torvalds 	default:
2097631ab46bSEric W. Biederman 		ret = -EINVAL;
2098631ab46bSEric W. Biederman 		break;
2099ee289b64SJoe Perches 	}
21001da177e4SLinus Torvalds 
2101876bfd4dSHerbert Xu unlock:
2102876bfd4dSHerbert Xu 	rtnl_unlock();
2103876bfd4dSHerbert Xu 	if (tun)
2104631ab46bSEric W. Biederman 		tun_put(tun);
2105631ab46bSEric W. Biederman 	return ret;
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
210850857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
210950857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
211050857e2aSArnd Bergmann {
211150857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
211250857e2aSArnd Bergmann }
211350857e2aSArnd Bergmann 
211450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
211550857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
211650857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
211750857e2aSArnd Bergmann {
211850857e2aSArnd Bergmann 	switch (cmd) {
211950857e2aSArnd Bergmann 	case TUNSETIFF:
212050857e2aSArnd Bergmann 	case TUNGETIFF:
212150857e2aSArnd Bergmann 	case TUNSETTXFILTER:
212250857e2aSArnd Bergmann 	case TUNGETSNDBUF:
212350857e2aSArnd Bergmann 	case TUNSETSNDBUF:
212450857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
212550857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
212650857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
212750857e2aSArnd Bergmann 		break;
212850857e2aSArnd Bergmann 	default:
212950857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
213050857e2aSArnd Bergmann 		break;
213150857e2aSArnd Bergmann 	}
213250857e2aSArnd Bergmann 
213350857e2aSArnd Bergmann 	/*
213450857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
213550857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
213650857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
213750857e2aSArnd Bergmann 	 * contents.
213850857e2aSArnd Bergmann 	 */
213950857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
214050857e2aSArnd Bergmann }
214150857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
214250857e2aSArnd Bergmann 
21431da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
21441da177e4SLinus Torvalds {
214554f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
21461da177e4SLinus Torvalds 	int ret;
21471da177e4SLinus Torvalds 
214854f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
21499d319522SJonathan Corbet 		goto out;
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds 	if (on) {
2152e0b93eddSJeff Layton 		__f_setown(file, task_pid(current), PIDTYPE_PID, 0);
215354f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
21541da177e4SLinus Torvalds 	} else
215554f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
21569d319522SJonathan Corbet 	ret = 0;
21579d319522SJonathan Corbet out:
21589d319522SJonathan Corbet 	return ret;
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
21621da177e4SLinus Torvalds {
2163631ab46bSEric W. Biederman 	struct tun_file *tfile;
2164deed49fbSThomas Gleixner 
21656b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2166631ab46bSEric W. Biederman 
216754f968d6SJason Wang 	tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
216854f968d6SJason Wang 					    &tun_proto);
2169631ab46bSEric W. Biederman 	if (!tfile)
2170631ab46bSEric W. Biederman 		return -ENOMEM;
2171c956674bSMonam Agarwal 	RCU_INIT_POINTER(tfile->tun, NULL);
217236b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
217354f968d6SJason Wang 	tfile->flags = 0;
2174fb7589a1SPavel Emelyanov 	tfile->ifindex = 0;
217554f968d6SJason Wang 
217654f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
21779e641bdcSXi Wang 	RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
217854f968d6SJason Wang 
217954f968d6SJason Wang 	tfile->socket.file = file;
218054f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
218154f968d6SJason Wang 
218254f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
218354f968d6SJason Wang 	sk_change_net(&tfile->sk, tfile->net);
218454f968d6SJason Wang 
218554f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
218654f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
218754f968d6SJason Wang 
2188631ab46bSEric W. Biederman 	file->private_data = tfile;
218954f968d6SJason Wang 	set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
21904008e97fSJason Wang 	INIT_LIST_HEAD(&tfile->next);
219154f968d6SJason Wang 
219219a6afb2SJason Wang 	sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
219319a6afb2SJason Wang 
21941da177e4SLinus Torvalds 	return 0;
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
21981da177e4SLinus Torvalds {
2199631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
220054f968d6SJason Wang 	struct net *net = tfile->net;
22011da177e4SLinus Torvalds 
2202c8d68e6bSJason Wang 	tun_detach(tfile, true);
220354f968d6SJason Wang 	put_net(net);
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 	return 0;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
220893e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
2209a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
221093e14b6dSMasatake YAMATO {
221193e14b6dSMasatake YAMATO 	struct tun_struct *tun;
221293e14b6dSMasatake YAMATO 	struct ifreq ifr;
221393e14b6dSMasatake YAMATO 
221493e14b6dSMasatake YAMATO 	memset(&ifr, 0, sizeof(ifr));
221593e14b6dSMasatake YAMATO 
221693e14b6dSMasatake YAMATO 	rtnl_lock();
221793e14b6dSMasatake YAMATO 	tun = tun_get(f);
221893e14b6dSMasatake YAMATO 	if (tun)
221993e14b6dSMasatake YAMATO 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
222093e14b6dSMasatake YAMATO 	rtnl_unlock();
222193e14b6dSMasatake YAMATO 
222293e14b6dSMasatake YAMATO 	if (tun)
222393e14b6dSMasatake YAMATO 		tun_put(tun);
222493e14b6dSMasatake YAMATO 
2225a3816ab0SJoe Perches 	seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
222693e14b6dSMasatake YAMATO }
222793e14b6dSMasatake YAMATO #endif
222893e14b6dSMasatake YAMATO 
2229d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
22301da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
22311da177e4SLinus Torvalds 	.llseek = no_llseek,
22329b067034SAl Viro 	.read  = new_sync_read,
2233f5ff53b4SAl Viro 	.write = new_sync_write,
22349b067034SAl Viro 	.read_iter  = tun_chr_read_iter,
2235f5ff53b4SAl Viro 	.write_iter = tun_chr_write_iter,
22361da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
2237876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
223850857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
223950857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
224050857e2aSArnd Bergmann #endif
22411da177e4SLinus Torvalds 	.open	= tun_chr_open,
22421da177e4SLinus Torvalds 	.release = tun_chr_close,
224393e14b6dSMasatake YAMATO 	.fasync = tun_chr_fasync,
224493e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
224593e14b6dSMasatake YAMATO 	.show_fdinfo = tun_chr_show_fdinfo,
224693e14b6dSMasatake YAMATO #endif
22471da177e4SLinus Torvalds };
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
22501da177e4SLinus Torvalds 	.minor = TUN_MINOR,
22511da177e4SLinus Torvalds 	.name = "tun",
2252e454cea2SKay Sievers 	.nodename = "net/tun",
22531da177e4SLinus Torvalds 	.fops = &tun_fops,
22541da177e4SLinus Torvalds };
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds /* ethtool interface */
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
22591da177e4SLinus Torvalds {
22601da177e4SLinus Torvalds 	cmd->supported		= 0;
22611da177e4SLinus Torvalds 	cmd->advertising	= 0;
226270739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
22631da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
22641da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
22651da177e4SLinus Torvalds 	cmd->phy_address	= 0;
22661da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
22671da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
22681da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
22691da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
22701da177e4SLinus Torvalds 	return 0;
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
22741da177e4SLinus Torvalds {
22751da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22761da177e4SLinus Torvalds 
227733a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
227833a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
228140630b82SMichael S. Tsirkin 	case IFF_TUN:
228233a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
22831da177e4SLinus Torvalds 		break;
228440630b82SMichael S. Tsirkin 	case IFF_TAP:
228533a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
22861da177e4SLinus Torvalds 		break;
22871da177e4SLinus Torvalds 	}
22881da177e4SLinus Torvalds }
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
22911da177e4SLinus Torvalds {
22921da177e4SLinus Torvalds #ifdef TUN_DEBUG
22931da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22941da177e4SLinus Torvalds 	return tun->debug;
22951da177e4SLinus Torvalds #else
22961da177e4SLinus Torvalds 	return -EOPNOTSUPP;
22971da177e4SLinus Torvalds #endif
22981da177e4SLinus Torvalds }
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
23011da177e4SLinus Torvalds {
23021da177e4SLinus Torvalds #ifdef TUN_DEBUG
23031da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
23041da177e4SLinus Torvalds 	tun->debug = value;
23051da177e4SLinus Torvalds #endif
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
23087282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
23091da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
23101da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
23111da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
23121da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
2313bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
2314eda29772SRichard Cochran 	.get_ts_info	= ethtool_op_get_ts_info,
23151da177e4SLinus Torvalds };
23161da177e4SLinus Torvalds 
231779d17604SPavel Emelyanov 
23181da177e4SLinus Torvalds static int __init tun_init(void)
23191da177e4SLinus Torvalds {
23201da177e4SLinus Torvalds 	int ret = 0;
23211da177e4SLinus Torvalds 
23226b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
23236b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
23241da177e4SLinus Torvalds 
2325f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
232679d17604SPavel Emelyanov 	if (ret) {
23276b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
2328f019a7a5SEric W. Biederman 		goto err_linkops;
232979d17604SPavel Emelyanov 	}
233079d17604SPavel Emelyanov 
23311da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
233279d17604SPavel Emelyanov 	if (ret) {
23336b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
233479d17604SPavel Emelyanov 		goto err_misc;
233579d17604SPavel Emelyanov 	}
233679d17604SPavel Emelyanov 	return  0;
233779d17604SPavel Emelyanov err_misc:
2338f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2339f019a7a5SEric W. Biederman err_linkops:
23401da177e4SLinus Torvalds 	return ret;
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds static void tun_cleanup(void)
23441da177e4SLinus Torvalds {
23451da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2346f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
234905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
235005c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
235105c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
235205c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
235305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
235405c2828cSMichael S. Tsirkin {
23556e914fc7SJason Wang 	struct tun_file *tfile;
235605c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
235705c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
23586e914fc7SJason Wang 	tfile = file->private_data;
23596e914fc7SJason Wang 	if (!tfile)
236005c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
236154f968d6SJason Wang 	return &tfile->socket;
236205c2828cSMichael S. Tsirkin }
236305c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
236405c2828cSMichael S. Tsirkin 
23651da177e4SLinus Torvalds module_init(tun_init);
23661da177e4SLinus Torvalds module_exit(tun_cleanup);
23671da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
23681da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
23691da177e4SLinus Torvalds MODULE_LICENSE("GPL");
23701da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2371578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2372