xref: /openbmc/linux/drivers/net/tun.c (revision 3df97ba83019d524c012fd43d3216d4cc3005955)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  TUN - Universal TUN/TAP device driver.
31da177e4SLinus Torvalds  *  Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *  it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *  the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *  (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131da177e4SLinus Torvalds  *  GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds  *  Changes:
201da177e4SLinus Torvalds  *
21ff4cc3acSMike Kershaw  *  Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22ff4cc3acSMike Kershaw  *    Add TUNSETLINK ioctl to set the link encapsulation
23ff4cc3acSMike Kershaw  *
241da177e4SLinus Torvalds  *  Mark Smith <markzzzsmith@yahoo.com.au>
25344dc8edSJoe Perches  *    Use eth_random_addr() for tap MAC address.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
281da177e4SLinus Torvalds  *    Fixes in packet dropping, queue length setting and queue wakeup.
291da177e4SLinus Torvalds  *    Increased default tx queue length.
301da177e4SLinus Torvalds  *    Added ethtool API.
311da177e4SLinus Torvalds  *    Minor cleanups
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  *  Daniel Podlejski <underley@underley.eu.org>
341da177e4SLinus Torvalds  *    Modifications for 2.3.99-pre5 kernel.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
386b8a66eeSJoe Perches 
391da177e4SLinus Torvalds #define DRV_NAME	"tun"
401da177e4SLinus Torvalds #define DRV_VERSION	"1.6"
411da177e4SLinus Torvalds #define DRV_DESCRIPTION	"Universal TUN/TAP device driver"
421da177e4SLinus Torvalds #define DRV_COPYRIGHT	"(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/module.h>
451da177e4SLinus Torvalds #include <linux/errno.h>
461da177e4SLinus Torvalds #include <linux/kernel.h>
471da177e4SLinus Torvalds #include <linux/major.h>
481da177e4SLinus Torvalds #include <linux/slab.h>
491da177e4SLinus Torvalds #include <linux/poll.h>
501da177e4SLinus Torvalds #include <linux/fcntl.h>
511da177e4SLinus Torvalds #include <linux/init.h>
521da177e4SLinus Torvalds #include <linux/skbuff.h>
531da177e4SLinus Torvalds #include <linux/netdevice.h>
541da177e4SLinus Torvalds #include <linux/etherdevice.h>
551da177e4SLinus Torvalds #include <linux/miscdevice.h>
561da177e4SLinus Torvalds #include <linux/ethtool.h>
571da177e4SLinus Torvalds #include <linux/rtnetlink.h>
5850857e2aSArnd Bergmann #include <linux/compat.h>
591da177e4SLinus Torvalds #include <linux/if.h>
601da177e4SLinus Torvalds #include <linux/if_arp.h>
611da177e4SLinus Torvalds #include <linux/if_ether.h>
621da177e4SLinus Torvalds #include <linux/if_tun.h>
636680ec68SJason Wang #include <linux/if_vlan.h>
641da177e4SLinus Torvalds #include <linux/crc32.h>
65d647a591SPavel Emelyanov #include <linux/nsproxy.h>
66f43798c2SRusty Russell #include <linux/virtio_net.h>
6799405162SMichael S. Tsirkin #include <linux/rcupdate.h>
68881d966bSEric W. Biederman #include <net/net_namespace.h>
6979d17604SPavel Emelyanov #include <net/netns/generic.h>
70f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
7133dccbb0SHerbert Xu #include <net/sock.h>
7293e14b6dSMasatake YAMATO #include <linux/seq_file.h>
73e0b46d0eSHerbert Xu #include <linux/uio.h>
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #include <asm/uaccess.h>
761da177e4SLinus Torvalds 
7714daa021SRusty Russell /* Uncomment to enable debugging */
7814daa021SRusty Russell /* #define TUN_DEBUG 1 */
7914daa021SRusty Russell 
801da177e4SLinus Torvalds #ifdef TUN_DEBUG
811da177e4SLinus Torvalds static int debug;
8214daa021SRusty Russell 
836b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
846b8a66eeSJoe Perches do {								\
856b8a66eeSJoe Perches 	if (tun->debug)						\
866b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
876b8a66eeSJoe Perches } while (0)
886b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
896b8a66eeSJoe Perches do {								\
906b8a66eeSJoe Perches 	if (debug == 2)						\
916b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
926b8a66eeSJoe Perches } while (0)
9314daa021SRusty Russell #else
946b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
956b8a66eeSJoe Perches do {								\
966b8a66eeSJoe Perches 	if (0)							\
976b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
986b8a66eeSJoe Perches } while (0)
996b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
1006b8a66eeSJoe Perches do {								\
1016b8a66eeSJoe Perches 	if (0)							\
1026b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
1036b8a66eeSJoe Perches } while (0)
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
106031f5e03SMichael S. Tsirkin /* TUN device flags */
107031f5e03SMichael S. Tsirkin 
108031f5e03SMichael S. Tsirkin /* IFF_ATTACH_QUEUE is never stored in device flags,
109031f5e03SMichael S. Tsirkin  * overload it to mean fasync when stored there.
110031f5e03SMichael S. Tsirkin  */
111031f5e03SMichael S. Tsirkin #define TUN_FASYNC	IFF_ATTACH_QUEUE
1121cf8e410SMichael S. Tsirkin /* High bits in flags field are unused. */
1131cf8e410SMichael S. Tsirkin #define TUN_VNET_LE     0x80000000
1148b8e658bSGreg Kurz #define TUN_VNET_BE     0x40000000
115031f5e03SMichael S. Tsirkin 
116031f5e03SMichael S. Tsirkin #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
1171cf8e410SMichael 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 
127baf71c5cSPankaj Gupta /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
128baf71c5cSPankaj Gupta  * to max number of VCPUs in guest. */
129baf71c5cSPankaj Gupta #define MAX_TAP_QUEUES 256
130b8732fb7SJason Wang #define MAX_TAP_FLOWS  4096
131c8d68e6bSJason Wang 
13296442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ)
13396442e42SJason Wang 
134608b9977SPaolo Abeni struct tun_pcpu_stats {
135608b9977SPaolo Abeni 	u64 rx_packets;
136608b9977SPaolo Abeni 	u64 rx_bytes;
137608b9977SPaolo Abeni 	u64 tx_packets;
138608b9977SPaolo Abeni 	u64 tx_bytes;
139608b9977SPaolo Abeni 	struct u64_stats_sync syncp;
140608b9977SPaolo Abeni 	u32 rx_dropped;
141608b9977SPaolo Abeni 	u32 tx_dropped;
142608b9977SPaolo Abeni 	u32 rx_frame_errors;
143608b9977SPaolo Abeni };
144608b9977SPaolo Abeni 
14554f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
14692d4ea6eSstephen hemminger  * also contains all socket related structures (except sock_fprog and tap_filter)
14754f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
14854f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
14936fe8c09SRami Rosen  * netdevice not for a specific queue (at least I didn't see the requirement for
15054f968d6SJason Wang  * this).
1516e914fc7SJason Wang  *
1526e914fc7SJason Wang  * RCU usage:
15336fe8c09SRami Rosen  * The tun_file and tun_struct are loosely coupled, the pointer from one to the
1546e914fc7SJason Wang  * other can only be read while rcu_read_lock or rtnl_lock is held.
15554f968d6SJason Wang  */
156631ab46bSEric W. Biederman struct tun_file {
15754f968d6SJason Wang 	struct sock sk;
15854f968d6SJason Wang 	struct socket socket;
15954f968d6SJason Wang 	struct socket_wq wq;
1606e914fc7SJason Wang 	struct tun_struct __rcu *tun;
16154f968d6SJason Wang 	struct fasync_struct *fasync;
16254f968d6SJason Wang 	/* only used for fasnyc */
16354f968d6SJason Wang 	unsigned int flags;
164fb7589a1SPavel Emelyanov 	union {
165c8d68e6bSJason Wang 		u16 queue_index;
166fb7589a1SPavel Emelyanov 		unsigned int ifindex;
167fb7589a1SPavel Emelyanov 	};
1684008e97fSJason Wang 	struct list_head next;
1694008e97fSJason Wang 	struct tun_struct *detached;
170631ab46bSEric W. Biederman };
171631ab46bSEric W. Biederman 
17296442e42SJason Wang struct tun_flow_entry {
17396442e42SJason Wang 	struct hlist_node hash_link;
17496442e42SJason Wang 	struct rcu_head rcu;
17596442e42SJason Wang 	struct tun_struct *tun;
17696442e42SJason Wang 
17796442e42SJason Wang 	u32 rxhash;
1789bc88939STom Herbert 	u32 rps_rxhash;
17996442e42SJason Wang 	int queue_index;
18096442e42SJason Wang 	unsigned long updated;
18196442e42SJason Wang };
18296442e42SJason Wang 
18396442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024
18496442e42SJason Wang 
18554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
18636fe8c09SRami Rosen  * device, socket filter, sndbuf and vnet header size were restore when the
18754f968d6SJason Wang  * file were attached to a persist device.
18854f968d6SJason Wang  */
18914daa021SRusty Russell struct tun_struct {
190c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
191c8d68e6bSJason Wang 	unsigned int            numqueues;
192f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1930625c883SEric W. Biederman 	kuid_t			owner;
1940625c883SEric W. Biederman 	kgid_t			group;
19514daa021SRusty Russell 
19614daa021SRusty Russell 	struct net_device	*dev;
197c8f44affSMichał Mirosław 	netdev_features_t	set_features;
19888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
199e3e3c423SVlad Yasevich 			  NETIF_F_TSO6|NETIF_F_UFO)
200d9d52b51SMichael S. Tsirkin 
201eaea34b2SPaolo Abeni 	int			align;
202d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
20354f968d6SJason Wang 	int			sndbuf;
20454f968d6SJason Wang 	struct tap_filter	txflt;
20554f968d6SJason Wang 	struct sock_fprog	fprog;
20654f968d6SJason Wang 	/* protected by rtnl lock */
20754f968d6SJason Wang 	bool			filter_attached;
20814daa021SRusty Russell #ifdef TUN_DEBUG
20914daa021SRusty Russell 	int debug;
21014daa021SRusty Russell #endif
21196442e42SJason Wang 	spinlock_t lock;
21296442e42SJason Wang 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
21396442e42SJason Wang 	struct timer_list flow_gc_timer;
21496442e42SJason Wang 	unsigned long ageing_time;
2154008e97fSJason Wang 	unsigned int numdisabled;
2164008e97fSJason Wang 	struct list_head disabled;
2175dbbaf2dSPaul Moore 	void *security;
218b8732fb7SJason Wang 	u32 flow_count;
219608b9977SPaolo Abeni 	struct tun_pcpu_stats __percpu *pcpu_stats;
22014daa021SRusty Russell };
22114daa021SRusty Russell 
2228b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE
2238b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
2248b8e658bSGreg Kurz {
2258b8e658bSGreg Kurz 	return tun->flags & TUN_VNET_BE ? false :
2268b8e658bSGreg Kurz 		virtio_legacy_is_little_endian();
2278b8e658bSGreg Kurz }
2288b8e658bSGreg Kurz 
2298b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
2308b8e658bSGreg Kurz {
2318b8e658bSGreg Kurz 	int be = !!(tun->flags & TUN_VNET_BE);
2328b8e658bSGreg Kurz 
2338b8e658bSGreg Kurz 	if (put_user(be, argp))
2348b8e658bSGreg Kurz 		return -EFAULT;
2358b8e658bSGreg Kurz 
2368b8e658bSGreg Kurz 	return 0;
2378b8e658bSGreg Kurz }
2388b8e658bSGreg Kurz 
2398b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
2408b8e658bSGreg Kurz {
2418b8e658bSGreg Kurz 	int be;
2428b8e658bSGreg Kurz 
2438b8e658bSGreg Kurz 	if (get_user(be, argp))
2448b8e658bSGreg Kurz 		return -EFAULT;
2458b8e658bSGreg Kurz 
2468b8e658bSGreg Kurz 	if (be)
2478b8e658bSGreg Kurz 		tun->flags |= TUN_VNET_BE;
2488b8e658bSGreg Kurz 	else
2498b8e658bSGreg Kurz 		tun->flags &= ~TUN_VNET_BE;
2508b8e658bSGreg Kurz 
2518b8e658bSGreg Kurz 	return 0;
2528b8e658bSGreg Kurz }
2538b8e658bSGreg Kurz #else
2548b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
2558b8e658bSGreg Kurz {
2568b8e658bSGreg Kurz 	return virtio_legacy_is_little_endian();
2578b8e658bSGreg Kurz }
2588b8e658bSGreg Kurz 
2598b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
2608b8e658bSGreg Kurz {
2618b8e658bSGreg Kurz 	return -EINVAL;
2628b8e658bSGreg Kurz }
2638b8e658bSGreg Kurz 
2648b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
2658b8e658bSGreg Kurz {
2668b8e658bSGreg Kurz 	return -EINVAL;
2678b8e658bSGreg Kurz }
2688b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */
2698b8e658bSGreg Kurz 
27025bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun)
27125bd55bbSGreg Kurz {
2727d824109SGreg Kurz 	return tun->flags & TUN_VNET_LE ||
2738b8e658bSGreg Kurz 		tun_legacy_is_little_endian(tun);
27425bd55bbSGreg Kurz }
27525bd55bbSGreg Kurz 
27656f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
27756f0dcc5SMichael S. Tsirkin {
27825bd55bbSGreg Kurz 	return __virtio16_to_cpu(tun_is_little_endian(tun), val);
27956f0dcc5SMichael S. Tsirkin }
28056f0dcc5SMichael S. Tsirkin 
28156f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
28256f0dcc5SMichael S. Tsirkin {
28325bd55bbSGreg Kurz 	return __cpu_to_virtio16(tun_is_little_endian(tun), val);
28456f0dcc5SMichael S. Tsirkin }
28556f0dcc5SMichael S. Tsirkin 
28696442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash)
28796442e42SJason Wang {
28896442e42SJason Wang 	return rxhash & 0x3ff;
28996442e42SJason Wang }
29096442e42SJason Wang 
29196442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
29296442e42SJason Wang {
29396442e42SJason Wang 	struct tun_flow_entry *e;
29496442e42SJason Wang 
295b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(e, head, hash_link) {
29696442e42SJason Wang 		if (e->rxhash == rxhash)
29796442e42SJason Wang 			return e;
29896442e42SJason Wang 	}
29996442e42SJason Wang 	return NULL;
30096442e42SJason Wang }
30196442e42SJason Wang 
30296442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
30396442e42SJason Wang 					      struct hlist_head *head,
30496442e42SJason Wang 					      u32 rxhash, u16 queue_index)
30596442e42SJason Wang {
3069fdc6befSEric Dumazet 	struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
3079fdc6befSEric Dumazet 
30896442e42SJason Wang 	if (e) {
30996442e42SJason Wang 		tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
31096442e42SJason Wang 			  rxhash, queue_index);
31196442e42SJason Wang 		e->updated = jiffies;
31296442e42SJason Wang 		e->rxhash = rxhash;
3139bc88939STom Herbert 		e->rps_rxhash = 0;
31496442e42SJason Wang 		e->queue_index = queue_index;
31596442e42SJason Wang 		e->tun = tun;
31696442e42SJason Wang 		hlist_add_head_rcu(&e->hash_link, head);
317b8732fb7SJason Wang 		++tun->flow_count;
31896442e42SJason Wang 	}
31996442e42SJason Wang 	return e;
32096442e42SJason Wang }
32196442e42SJason Wang 
32296442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
32396442e42SJason Wang {
32496442e42SJason Wang 	tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
32596442e42SJason Wang 		  e->rxhash, e->queue_index);
32696442e42SJason Wang 	hlist_del_rcu(&e->hash_link);
3279fdc6befSEric Dumazet 	kfree_rcu(e, rcu);
328b8732fb7SJason Wang 	--tun->flow_count;
32996442e42SJason Wang }
33096442e42SJason Wang 
33196442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun)
33296442e42SJason Wang {
33396442e42SJason Wang 	int i;
33496442e42SJason Wang 
33596442e42SJason Wang 	spin_lock_bh(&tun->lock);
33696442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
33796442e42SJason Wang 		struct tun_flow_entry *e;
338b67bfe0dSSasha Levin 		struct hlist_node *n;
33996442e42SJason Wang 
340b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
34196442e42SJason Wang 			tun_flow_delete(tun, e);
34296442e42SJason Wang 	}
34396442e42SJason Wang 	spin_unlock_bh(&tun->lock);
34496442e42SJason Wang }
34596442e42SJason Wang 
34696442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
34796442e42SJason Wang {
34896442e42SJason Wang 	int i;
34996442e42SJason Wang 
35096442e42SJason Wang 	spin_lock_bh(&tun->lock);
35196442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
35296442e42SJason Wang 		struct tun_flow_entry *e;
353b67bfe0dSSasha Levin 		struct hlist_node *n;
35496442e42SJason Wang 
355b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
35696442e42SJason Wang 			if (e->queue_index == queue_index)
35796442e42SJason Wang 				tun_flow_delete(tun, e);
35896442e42SJason Wang 		}
35996442e42SJason Wang 	}
36096442e42SJason Wang 	spin_unlock_bh(&tun->lock);
36196442e42SJason Wang }
36296442e42SJason Wang 
36396442e42SJason Wang static void tun_flow_cleanup(unsigned long data)
36496442e42SJason Wang {
36596442e42SJason Wang 	struct tun_struct *tun = (struct tun_struct *)data;
36696442e42SJason Wang 	unsigned long delay = tun->ageing_time;
36796442e42SJason Wang 	unsigned long next_timer = jiffies + delay;
36896442e42SJason Wang 	unsigned long count = 0;
36996442e42SJason Wang 	int i;
37096442e42SJason Wang 
37196442e42SJason Wang 	tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
37296442e42SJason Wang 
37396442e42SJason Wang 	spin_lock_bh(&tun->lock);
37496442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
37596442e42SJason Wang 		struct tun_flow_entry *e;
376b67bfe0dSSasha Levin 		struct hlist_node *n;
37796442e42SJason Wang 
378b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
37996442e42SJason Wang 			unsigned long this_timer;
38096442e42SJason Wang 			count++;
38196442e42SJason Wang 			this_timer = e->updated + delay;
38296442e42SJason Wang 			if (time_before_eq(this_timer, jiffies))
38396442e42SJason Wang 				tun_flow_delete(tun, e);
38496442e42SJason Wang 			else if (time_before(this_timer, next_timer))
38596442e42SJason Wang 				next_timer = this_timer;
38696442e42SJason Wang 		}
38796442e42SJason Wang 	}
38896442e42SJason Wang 
38996442e42SJason Wang 	if (count)
39096442e42SJason Wang 		mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
39196442e42SJason Wang 	spin_unlock_bh(&tun->lock);
39296442e42SJason Wang }
39396442e42SJason Wang 
39449974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
3959e85722dSJason Wang 			    struct tun_file *tfile)
39696442e42SJason Wang {
39796442e42SJason Wang 	struct hlist_head *head;
39896442e42SJason Wang 	struct tun_flow_entry *e;
39996442e42SJason Wang 	unsigned long delay = tun->ageing_time;
4009e85722dSJason Wang 	u16 queue_index = tfile->queue_index;
40196442e42SJason Wang 
40296442e42SJason Wang 	if (!rxhash)
40396442e42SJason Wang 		return;
40496442e42SJason Wang 	else
40596442e42SJason Wang 		head = &tun->flows[tun_hashfn(rxhash)];
40696442e42SJason Wang 
40796442e42SJason Wang 	rcu_read_lock();
40896442e42SJason Wang 
4099e85722dSJason Wang 	/* We may get a very small possibility of OOO during switching, not
4109e85722dSJason Wang 	 * worth to optimize.*/
4119e85722dSJason Wang 	if (tun->numqueues == 1 || tfile->detached)
41296442e42SJason Wang 		goto unlock;
41396442e42SJason Wang 
41496442e42SJason Wang 	e = tun_flow_find(head, rxhash);
41596442e42SJason Wang 	if (likely(e)) {
41696442e42SJason Wang 		/* TODO: keep queueing to old queue until it's empty? */
41796442e42SJason Wang 		e->queue_index = queue_index;
41896442e42SJason Wang 		e->updated = jiffies;
4199bc88939STom Herbert 		sock_rps_record_flow_hash(e->rps_rxhash);
42096442e42SJason Wang 	} else {
42196442e42SJason Wang 		spin_lock_bh(&tun->lock);
422b8732fb7SJason Wang 		if (!tun_flow_find(head, rxhash) &&
423b8732fb7SJason Wang 		    tun->flow_count < MAX_TAP_FLOWS)
42496442e42SJason Wang 			tun_flow_create(tun, head, rxhash, queue_index);
42596442e42SJason Wang 
42696442e42SJason Wang 		if (!timer_pending(&tun->flow_gc_timer))
42796442e42SJason Wang 			mod_timer(&tun->flow_gc_timer,
42896442e42SJason Wang 				  round_jiffies_up(jiffies + delay));
42996442e42SJason Wang 		spin_unlock_bh(&tun->lock);
43096442e42SJason Wang 	}
43196442e42SJason Wang 
43296442e42SJason Wang unlock:
43396442e42SJason Wang 	rcu_read_unlock();
43496442e42SJason Wang }
43596442e42SJason Wang 
4369bc88939STom Herbert /**
4379bc88939STom Herbert  * Save the hash received in the stack receive path and update the
4389bc88939STom Herbert  * flow_hash table accordingly.
4399bc88939STom Herbert  */
4409bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
4419bc88939STom Herbert {
442567e4b79SEric Dumazet 	if (unlikely(e->rps_rxhash != hash))
4439bc88939STom Herbert 		e->rps_rxhash = hash;
4449bc88939STom Herbert }
4459bc88939STom Herbert 
446c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
44792d4ea6eSstephen hemminger  * we do not check rxq no. is because some cards(e.g 82599), chooses
448c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
449c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
450c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
451c8d68e6bSJason Wang  * hope the rxq no. may help here.
452c8d68e6bSJason Wang  */
453f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
45499932d4fSDaniel Borkmann 			    void *accel_priv, select_queue_fallback_t fallback)
455c8d68e6bSJason Wang {
456c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
45796442e42SJason Wang 	struct tun_flow_entry *e;
458c8d68e6bSJason Wang 	u32 txq = 0;
459c8d68e6bSJason Wang 	u32 numqueues = 0;
460c8d68e6bSJason Wang 
461c8d68e6bSJason Wang 	rcu_read_lock();
46292bb73eaSJason Wang 	numqueues = ACCESS_ONCE(tun->numqueues);
463c8d68e6bSJason Wang 
4643958afa1STom Herbert 	txq = skb_get_hash(skb);
465c8d68e6bSJason Wang 	if (txq) {
46696442e42SJason Wang 		e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
4679bc88939STom Herbert 		if (e) {
4689bc88939STom Herbert 			tun_flow_save_rps_rxhash(e, txq);
469fbe4d456SZhi Yong Wu 			txq = e->queue_index;
4709bc88939STom Herbert 		} else
471c8d68e6bSJason Wang 			/* use multiply and shift instead of expensive divide */
472c8d68e6bSJason Wang 			txq = ((u64)txq * numqueues) >> 32;
473c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
474c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
475c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
476c8d68e6bSJason Wang 			txq -= numqueues;
477c8d68e6bSJason Wang 	}
478c8d68e6bSJason Wang 
479c8d68e6bSJason Wang 	rcu_read_unlock();
480c8d68e6bSJason Wang 	return txq;
481c8d68e6bSJason Wang }
482c8d68e6bSJason Wang 
483cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
484cde8b15fSJason Wang {
485cde8b15fSJason Wang 	const struct cred *cred = current_cred();
486c260b772SEric W. Biederman 	struct net *net = dev_net(tun->dev);
487cde8b15fSJason Wang 
488cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
489cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
490c260b772SEric W. Biederman 		!ns_capable(net->user_ns, CAP_NET_ADMIN);
491cde8b15fSJason Wang }
492cde8b15fSJason Wang 
493c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
494c8d68e6bSJason Wang {
495c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
496c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
497c8d68e6bSJason Wang }
498c8d68e6bSJason Wang 
4994008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
5004008e97fSJason Wang {
5014008e97fSJason Wang 	tfile->detached = tun;
5024008e97fSJason Wang 	list_add_tail(&tfile->next, &tun->disabled);
5034008e97fSJason Wang 	++tun->numdisabled;
5044008e97fSJason Wang }
5054008e97fSJason Wang 
506d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
5074008e97fSJason Wang {
5084008e97fSJason Wang 	struct tun_struct *tun = tfile->detached;
5094008e97fSJason Wang 
5104008e97fSJason Wang 	tfile->detached = NULL;
5114008e97fSJason Wang 	list_del_init(&tfile->next);
5124008e97fSJason Wang 	--tun->numdisabled;
5134008e97fSJason Wang 	return tun;
5144008e97fSJason Wang }
5154008e97fSJason Wang 
5164bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile)
5174bfb0513SJason Wang {
5184bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_receive_queue);
5194bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_error_queue);
5204bfb0513SJason Wang }
5214bfb0513SJason Wang 
522c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
523c8d68e6bSJason Wang {
524c8d68e6bSJason Wang 	struct tun_file *ntfile;
525c8d68e6bSJason Wang 	struct tun_struct *tun;
526c8d68e6bSJason Wang 
527b8deabd3SJason Wang 	tun = rtnl_dereference(tfile->tun);
528b8deabd3SJason Wang 
5299e85722dSJason Wang 	if (tun && !tfile->detached) {
530c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
531c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
532c8d68e6bSJason Wang 
533c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
534c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
535b8deabd3SJason Wang 		ntfile = rtnl_dereference(tun->tfiles[index]);
536c8d68e6bSJason Wang 		ntfile->queue_index = index;
537c8d68e6bSJason Wang 
538c8d68e6bSJason Wang 		--tun->numqueues;
5399e85722dSJason Wang 		if (clean) {
540c956674bSMonam Agarwal 			RCU_INIT_POINTER(tfile->tun, NULL);
541c8d68e6bSJason Wang 			sock_put(&tfile->sk);
5429e85722dSJason Wang 		} else
5434008e97fSJason Wang 			tun_disable_queue(tun, tfile);
544c8d68e6bSJason Wang 
545c8d68e6bSJason Wang 		synchronize_net();
54696442e42SJason Wang 		tun_flow_delete_by_queue(tun, tun->numqueues + 1);
547c8d68e6bSJason Wang 		/* Drop read queue */
5484bfb0513SJason Wang 		tun_queue_purge(tfile);
549c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
550dd38bd85SJason Wang 	} else if (tfile->detached && clean) {
5514008e97fSJason Wang 		tun = tun_enable_queue(tfile);
552dd38bd85SJason Wang 		sock_put(&tfile->sk);
553dd38bd85SJason Wang 	}
554c8d68e6bSJason Wang 
555c8d68e6bSJason Wang 	if (clean) {
556af668b3cSMichael S. Tsirkin 		if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
557af668b3cSMichael S. Tsirkin 			netif_carrier_off(tun->dev);
558af668b3cSMichael S. Tsirkin 
55940630b82SMichael S. Tsirkin 			if (!(tun->flags & IFF_PERSIST) &&
560af668b3cSMichael S. Tsirkin 			    tun->dev->reg_state == NETREG_REGISTERED)
5614008e97fSJason Wang 				unregister_netdevice(tun->dev);
562af668b3cSMichael S. Tsirkin 		}
563140e807dSEric W. Biederman 		sock_put(&tfile->sk);
564c8d68e6bSJason Wang 	}
565c8d68e6bSJason Wang }
566c8d68e6bSJason Wang 
567c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
568c8d68e6bSJason Wang {
569c8d68e6bSJason Wang 	rtnl_lock();
570c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
571c8d68e6bSJason Wang 	rtnl_unlock();
572c8d68e6bSJason Wang }
573c8d68e6bSJason Wang 
574c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
575c8d68e6bSJason Wang {
576c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
5774008e97fSJason Wang 	struct tun_file *tfile, *tmp;
578c8d68e6bSJason Wang 	int i, n = tun->numqueues;
579c8d68e6bSJason Wang 
580c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
581b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
582c8d68e6bSJason Wang 		BUG_ON(!tfile);
5839e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
584c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
585c8d68e6bSJason Wang 		--tun->numqueues;
586c8d68e6bSJason Wang 	}
5879e85722dSJason Wang 	list_for_each_entry(tfile, &tun->disabled, next) {
5889e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
589c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
5909e85722dSJason Wang 	}
591c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
592c8d68e6bSJason Wang 
593c8d68e6bSJason Wang 	synchronize_net();
594c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
595b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
596c8d68e6bSJason Wang 		/* Drop read queue */
5974bfb0513SJason Wang 		tun_queue_purge(tfile);
598c8d68e6bSJason Wang 		sock_put(&tfile->sk);
599c8d68e6bSJason Wang 	}
6004008e97fSJason Wang 	list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
6014008e97fSJason Wang 		tun_enable_queue(tfile);
6024bfb0513SJason Wang 		tun_queue_purge(tfile);
6034008e97fSJason Wang 		sock_put(&tfile->sk);
6044008e97fSJason Wang 	}
6054008e97fSJason Wang 	BUG_ON(tun->numdisabled != 0);
606dd38bd85SJason Wang 
60740630b82SMichael S. Tsirkin 	if (tun->flags & IFF_PERSIST)
608dd38bd85SJason Wang 		module_put(THIS_MODULE);
609c8d68e6bSJason Wang }
610c8d68e6bSJason Wang 
611849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
612a7385ba2SEric W. Biederman {
613631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
61438231b7aSEric W. Biederman 	int err;
615a7385ba2SEric W. Biederman 
6165dbbaf2dSPaul Moore 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
6175dbbaf2dSPaul Moore 	if (err < 0)
6185dbbaf2dSPaul Moore 		goto out;
6195dbbaf2dSPaul Moore 
62038231b7aSEric W. Biederman 	err = -EINVAL;
6219e85722dSJason Wang 	if (rtnl_dereference(tfile->tun) && !tfile->detached)
62238231b7aSEric W. Biederman 		goto out;
62338231b7aSEric W. Biederman 
62438231b7aSEric W. Biederman 	err = -EBUSY;
62540630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
626c8d68e6bSJason Wang 		goto out;
627c8d68e6bSJason Wang 
628c8d68e6bSJason Wang 	err = -E2BIG;
6294008e97fSJason Wang 	if (!tfile->detached &&
6304008e97fSJason Wang 	    tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
63138231b7aSEric W. Biederman 		goto out;
63238231b7aSEric W. Biederman 
63338231b7aSEric W. Biederman 	err = 0;
63454f968d6SJason Wang 
63592d4ea6eSstephen hemminger 	/* Re-attach the filter to persist device */
636849c9b6fSPavel Emelyanov 	if (!skip_filter && (tun->filter_attached == true)) {
6378ced425eSHannes Frederic Sowa 		lock_sock(tfile->socket.sk);
6388ced425eSHannes Frederic Sowa 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
6398ced425eSHannes Frederic Sowa 		release_sock(tfile->socket.sk);
64054f968d6SJason Wang 		if (!err)
64154f968d6SJason Wang 			goto out;
64254f968d6SJason Wang 	}
643c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
6446e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
645c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
646c8d68e6bSJason Wang 	tun->numqueues++;
647c8d68e6bSJason Wang 
6484008e97fSJason Wang 	if (tfile->detached)
6494008e97fSJason Wang 		tun_enable_queue(tfile);
6504008e97fSJason Wang 	else
6514008e97fSJason Wang 		sock_hold(&tfile->sk);
6524008e97fSJason Wang 
653c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
654c8d68e6bSJason Wang 
655c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
656c8d68e6bSJason Wang 	 * refcnt.
657c8d68e6bSJason Wang 	 */
658a7385ba2SEric W. Biederman 
65938231b7aSEric W. Biederman out:
66038231b7aSEric W. Biederman 	return err;
661a7385ba2SEric W. Biederman }
662a7385ba2SEric W. Biederman 
663631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
664631ab46bSEric W. Biederman {
6656e914fc7SJason Wang 	struct tun_struct *tun;
666c70f1829SEric W. Biederman 
6676e914fc7SJason Wang 	rcu_read_lock();
6686e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
6696e914fc7SJason Wang 	if (tun)
6706e914fc7SJason Wang 		dev_hold(tun->dev);
6716e914fc7SJason Wang 	rcu_read_unlock();
672c70f1829SEric W. Biederman 
673c70f1829SEric W. Biederman 	return tun;
674631ab46bSEric W. Biederman }
675631ab46bSEric W. Biederman 
676631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
677631ab46bSEric W. Biederman {
678631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
679631ab46bSEric W. Biederman }
680631ab46bSEric W. Biederman 
681631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
682631ab46bSEric W. Biederman {
6836e914fc7SJason Wang 	dev_put(tun->dev);
684631ab46bSEric W. Biederman }
685631ab46bSEric W. Biederman 
6866b8a66eeSJoe Perches /* TAP filtering */
687f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
688f271b2ccSMax Krasnyansky {
689f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
690f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
691f271b2ccSMax Krasnyansky }
692f271b2ccSMax Krasnyansky 
693f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
694f271b2ccSMax Krasnyansky {
695f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
696f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
697f271b2ccSMax Krasnyansky }
698f271b2ccSMax Krasnyansky 
699f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
700f271b2ccSMax Krasnyansky {
701f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
702f271b2ccSMax Krasnyansky 	struct tun_filter uf;
703f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
704f271b2ccSMax Krasnyansky 
705f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
706f271b2ccSMax Krasnyansky 		return -EFAULT;
707f271b2ccSMax Krasnyansky 
708f271b2ccSMax Krasnyansky 	if (!uf.count) {
709f271b2ccSMax Krasnyansky 		/* Disabled */
710f271b2ccSMax Krasnyansky 		filter->count = 0;
711f271b2ccSMax Krasnyansky 		return 0;
712f271b2ccSMax Krasnyansky 	}
713f271b2ccSMax Krasnyansky 
714f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
715f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
716f271b2ccSMax Krasnyansky 	if (!addr)
717f271b2ccSMax Krasnyansky 		return -ENOMEM;
718f271b2ccSMax Krasnyansky 
719f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
720f271b2ccSMax Krasnyansky 		err = -EFAULT;
721f271b2ccSMax Krasnyansky 		goto done;
722f271b2ccSMax Krasnyansky 	}
723f271b2ccSMax Krasnyansky 
724f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
725f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
726f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
727f271b2ccSMax Krasnyansky 	filter->count = 0;
728f271b2ccSMax Krasnyansky 	wmb();
729f271b2ccSMax Krasnyansky 
730f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
731f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
732f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
733f271b2ccSMax Krasnyansky 
734f271b2ccSMax Krasnyansky 	nexact = n;
735f271b2ccSMax Krasnyansky 
736cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
737cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
738f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
739cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
740cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
741cfbf84fcSAlex Williamson 			err = 0; /* no filter */
742cfbf84fcSAlex Williamson 			goto done;
743cfbf84fcSAlex Williamson 		}
744f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
745cfbf84fcSAlex Williamson 	}
746f271b2ccSMax Krasnyansky 
747f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
748f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
749f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
750f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
751f271b2ccSMax Krasnyansky 
752f271b2ccSMax Krasnyansky 	/* Now enable the filter */
753f271b2ccSMax Krasnyansky 	wmb();
754f271b2ccSMax Krasnyansky 	filter->count = nexact;
755f271b2ccSMax Krasnyansky 
756f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
757f271b2ccSMax Krasnyansky 	err = nexact;
758f271b2ccSMax Krasnyansky 
759f271b2ccSMax Krasnyansky done:
760f271b2ccSMax Krasnyansky 	kfree(addr);
761f271b2ccSMax Krasnyansky 	return err;
762f271b2ccSMax Krasnyansky }
763f271b2ccSMax Krasnyansky 
764f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
765f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
766f271b2ccSMax Krasnyansky {
767f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
768f271b2ccSMax Krasnyansky 	 * at this point. */
769f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
770f271b2ccSMax Krasnyansky 	int i;
771f271b2ccSMax Krasnyansky 
772f271b2ccSMax Krasnyansky 	/* Exact match */
773f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
7742e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
775f271b2ccSMax Krasnyansky 			return 1;
776f271b2ccSMax Krasnyansky 
777f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
778f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
779f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
780f271b2ccSMax Krasnyansky 
781f271b2ccSMax Krasnyansky 	return 0;
782f271b2ccSMax Krasnyansky }
783f271b2ccSMax Krasnyansky 
784f271b2ccSMax Krasnyansky /*
785f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
786f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
787f271b2ccSMax Krasnyansky  */
788f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
789f271b2ccSMax Krasnyansky {
790f271b2ccSMax Krasnyansky 	if (!filter->count)
791f271b2ccSMax Krasnyansky 		return 1;
792f271b2ccSMax Krasnyansky 
793f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
794f271b2ccSMax Krasnyansky }
795f271b2ccSMax Krasnyansky 
7961da177e4SLinus Torvalds /* Network device part of the driver */
7971da177e4SLinus Torvalds 
7987282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
7991da177e4SLinus Torvalds 
800c70f1829SEric W. Biederman /* Net device detach from fd. */
801c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
802c70f1829SEric W. Biederman {
803c8d68e6bSJason Wang 	tun_detach_all(dev);
804c70f1829SEric W. Biederman }
805c70f1829SEric W. Biederman 
8061da177e4SLinus Torvalds /* Net device open. */
8071da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
8081da177e4SLinus Torvalds {
809c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
8101da177e4SLinus Torvalds 	return 0;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds /* Net device close. */
8141da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
8151da177e4SLinus Torvalds {
816c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
8171da177e4SLinus Torvalds 	return 0;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds /* Net device start xmit */
821424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
824c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
8256e914fc7SJason Wang 	struct tun_file *tfile;
826fa35864eSDominic Curran 	u32 numqueues = 0;
8271da177e4SLinus Torvalds 
8286e914fc7SJason Wang 	rcu_read_lock();
829c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
830fa35864eSDominic Curran 	numqueues = ACCESS_ONCE(tun->numqueues);
831c8d68e6bSJason Wang 
8321da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
833fa35864eSDominic Curran 	if (txq >= numqueues)
8341da177e4SLinus Torvalds 		goto drop;
8351da177e4SLinus Torvalds 
836*3df97ba8SJason Wang #ifdef CONFIG_RPS
837*3df97ba8SJason Wang 	if (numqueues == 1 && static_key_false(&rps_needed)) {
8389bc88939STom Herbert 		/* Select queue was not called for the skbuff, so we extract the
8399bc88939STom Herbert 		 * RPS hash and save it into the flow_table here.
8409bc88939STom Herbert 		 */
8419bc88939STom Herbert 		__u32 rxhash;
8429bc88939STom Herbert 
8439bc88939STom Herbert 		rxhash = skb_get_hash(skb);
8449bc88939STom Herbert 		if (rxhash) {
8459bc88939STom Herbert 			struct tun_flow_entry *e;
8469bc88939STom Herbert 			e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
8479bc88939STom Herbert 					rxhash);
8489bc88939STom Herbert 			if (e)
8499bc88939STom Herbert 				tun_flow_save_rps_rxhash(e, rxhash);
8509bc88939STom Herbert 		}
8519bc88939STom Herbert 	}
852*3df97ba8SJason Wang #endif
8539bc88939STom Herbert 
8546e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
8556e914fc7SJason Wang 
856c8d68e6bSJason Wang 	BUG_ON(!tfile);
857c8d68e6bSJason Wang 
858f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
859f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
860f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
861f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
862f271b2ccSMax Krasnyansky 		goto drop;
863f271b2ccSMax Krasnyansky 
86454f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
86554f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
86699405162SMichael S. Tsirkin 		goto drop;
86799405162SMichael S. Tsirkin 
86836fe8c09SRami Rosen 	/* Limit the number of packets queued by dividing txq length with the
869c8d68e6bSJason Wang 	 * number of queues.
870c8d68e6bSJason Wang 	 */
871fa35864eSDominic Curran 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
872fa35864eSDominic Curran 			  >= dev->tx_queue_len)
8731da177e4SLinus Torvalds 		goto drop;
8741da177e4SLinus Torvalds 
8757bf66305SJason Wang 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
8767bf66305SJason Wang 		goto drop;
8777bf66305SJason Wang 
8785fcd2d8bSEric Dumazet 	if (skb->sk && sk_fullsock(skb->sk)) {
879c14ac945SSoheil Hassas Yeganeh 		sock_tx_timestamp(skb->sk, skb->sk->sk_tsflags,
880c14ac945SSoheil Hassas Yeganeh 				  &skb_shinfo(skb)->tx_flags);
881eda29772SRichard Cochran 		sw_tx_timestamp(skb);
882eda29772SRichard Cochran 	}
883eda29772SRichard Cochran 
8840110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
8857bf66305SJason Wang 	 * for indefinite time.
8867bf66305SJason Wang 	 */
8870110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
8880110d6f2SMichael S. Tsirkin 
889f8af75f3SEric Dumazet 	nf_reset(skb);
890f8af75f3SEric Dumazet 
891f271b2ccSMax Krasnyansky 	/* Enqueue packet */
89254f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	/* Notify and wake up reader process */
89554f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
89654f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
8979e641bdcSXi Wang 	tfile->socket.sk->sk_data_ready(tfile->socket.sk);
8986e914fc7SJason Wang 
8996e914fc7SJason Wang 	rcu_read_unlock();
9006ed10654SPatrick McHardy 	return NETDEV_TX_OK;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds drop:
903608b9977SPaolo Abeni 	this_cpu_inc(tun->pcpu_stats->tx_dropped);
904149d36f7SMichael S. Tsirkin 	skb_tx_error(skb);
9051da177e4SLinus Torvalds 	kfree_skb(skb);
9066e914fc7SJason Wang 	rcu_read_unlock();
907baeababbSJason Wang 	return NET_XMIT_DROP;
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
910f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
9111da177e4SLinus Torvalds {
912f271b2ccSMax Krasnyansky 	/*
913f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
914f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
915f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
916f271b2ccSMax Krasnyansky 	 */
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
9194885a504SEd Swierk #define MIN_MTU 68
9204885a504SEd Swierk #define MAX_MTU 65535
9214885a504SEd Swierk 
9224885a504SEd Swierk static int
9234885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
9244885a504SEd Swierk {
9254885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
9264885a504SEd Swierk 		return -EINVAL;
9274885a504SEd Swierk 	dev->mtu = new_mtu;
9284885a504SEd Swierk 	return 0;
9294885a504SEd Swierk }
9304885a504SEd Swierk 
931c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
932c8f44affSMichał Mirosław 	netdev_features_t features)
93388255375SMichał Mirosław {
93488255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
93588255375SMichał Mirosław 
93688255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
93788255375SMichał Mirosław }
938bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
939bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
940bebd097aSNeil Horman {
941bebd097aSNeil Horman 	/*
942bebd097aSNeil Horman 	 * Tun only receives frames when:
943bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
944bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
94592d4ea6eSstephen hemminger 	 * Since both of those are synchronous operations, we are guaranteed
946bebd097aSNeil Horman 	 * never to have pending data when we poll for it
94792d4ea6eSstephen hemminger 	 * so there is nothing to do here but return.
948bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
949bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
950bebd097aSNeil Horman 	 * still use netconsole
951bebd097aSNeil Horman 	 */
952bebd097aSNeil Horman 	return;
953bebd097aSNeil Horman }
954bebd097aSNeil Horman #endif
955eaea34b2SPaolo Abeni 
956eaea34b2SPaolo Abeni static void tun_set_headroom(struct net_device *dev, int new_hr)
957eaea34b2SPaolo Abeni {
958eaea34b2SPaolo Abeni 	struct tun_struct *tun = netdev_priv(dev);
959eaea34b2SPaolo Abeni 
960eaea34b2SPaolo Abeni 	if (new_hr < NET_SKB_PAD)
961eaea34b2SPaolo Abeni 		new_hr = NET_SKB_PAD;
962eaea34b2SPaolo Abeni 
963eaea34b2SPaolo Abeni 	tun->align = new_hr;
964eaea34b2SPaolo Abeni }
965eaea34b2SPaolo Abeni 
966608b9977SPaolo Abeni static struct rtnl_link_stats64 *
967608b9977SPaolo Abeni tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
968608b9977SPaolo Abeni {
969608b9977SPaolo Abeni 	u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
970608b9977SPaolo Abeni 	struct tun_struct *tun = netdev_priv(dev);
971608b9977SPaolo Abeni 	struct tun_pcpu_stats *p;
972608b9977SPaolo Abeni 	int i;
973608b9977SPaolo Abeni 
974608b9977SPaolo Abeni 	for_each_possible_cpu(i) {
975608b9977SPaolo Abeni 		u64 rxpackets, rxbytes, txpackets, txbytes;
976608b9977SPaolo Abeni 		unsigned int start;
977608b9977SPaolo Abeni 
978608b9977SPaolo Abeni 		p = per_cpu_ptr(tun->pcpu_stats, i);
979608b9977SPaolo Abeni 		do {
980608b9977SPaolo Abeni 			start = u64_stats_fetch_begin(&p->syncp);
981608b9977SPaolo Abeni 			rxpackets	= p->rx_packets;
982608b9977SPaolo Abeni 			rxbytes		= p->rx_bytes;
983608b9977SPaolo Abeni 			txpackets	= p->tx_packets;
984608b9977SPaolo Abeni 			txbytes		= p->tx_bytes;
985608b9977SPaolo Abeni 		} while (u64_stats_fetch_retry(&p->syncp, start));
986608b9977SPaolo Abeni 
987608b9977SPaolo Abeni 		stats->rx_packets	+= rxpackets;
988608b9977SPaolo Abeni 		stats->rx_bytes		+= rxbytes;
989608b9977SPaolo Abeni 		stats->tx_packets	+= txpackets;
990608b9977SPaolo Abeni 		stats->tx_bytes		+= txbytes;
991608b9977SPaolo Abeni 
992608b9977SPaolo Abeni 		/* u32 counters */
993608b9977SPaolo Abeni 		rx_dropped	+= p->rx_dropped;
994608b9977SPaolo Abeni 		rx_frame_errors	+= p->rx_frame_errors;
995608b9977SPaolo Abeni 		tx_dropped	+= p->tx_dropped;
996608b9977SPaolo Abeni 	}
997608b9977SPaolo Abeni 	stats->rx_dropped  = rx_dropped;
998608b9977SPaolo Abeni 	stats->rx_frame_errors = rx_frame_errors;
999608b9977SPaolo Abeni 	stats->tx_dropped = tx_dropped;
1000608b9977SPaolo Abeni 	return stats;
1001608b9977SPaolo Abeni }
1002608b9977SPaolo Abeni 
1003758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
1004c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
1005758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
1006758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
100700829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
1008758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
100988255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
1010c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
1011bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
1012bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
1013bebd097aSNeil Horman #endif
1014eaea34b2SPaolo Abeni 	.ndo_set_rx_headroom	= tun_set_headroom,
1015608b9977SPaolo Abeni 	.ndo_get_stats64	= tun_net_get_stats64,
1016758e43b7SStephen Hemminger };
1017758e43b7SStephen Hemminger 
1018758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
1019c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
1020758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
1021758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
102200829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
1023758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
102488255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
1025afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
1026758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
1027758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1028c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
1029bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
1030bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
1031bebd097aSNeil Horman #endif
10325e52796aSToshiaki Makita 	.ndo_features_check	= passthru_features_check,
1033eaea34b2SPaolo Abeni 	.ndo_set_rx_headroom	= tun_set_headroom,
1034608b9977SPaolo Abeni 	.ndo_get_stats64	= tun_net_get_stats64,
1035758e43b7SStephen Hemminger };
1036758e43b7SStephen Hemminger 
1037944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun)
103896442e42SJason Wang {
103996442e42SJason Wang 	int i;
104096442e42SJason Wang 
104196442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
104296442e42SJason Wang 		INIT_HLIST_HEAD(&tun->flows[i]);
104396442e42SJason Wang 
104496442e42SJason Wang 	tun->ageing_time = TUN_FLOW_EXPIRE;
104596442e42SJason Wang 	setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
104696442e42SJason Wang 	mod_timer(&tun->flow_gc_timer,
104796442e42SJason Wang 		  round_jiffies_up(jiffies + tun->ageing_time));
104896442e42SJason Wang }
104996442e42SJason Wang 
105096442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun)
105196442e42SJason Wang {
105296442e42SJason Wang 	del_timer_sync(&tun->flow_gc_timer);
105396442e42SJason Wang 	tun_flow_flush(tun);
105496442e42SJason Wang }
105596442e42SJason Wang 
10561da177e4SLinus Torvalds /* Initialize net device. */
10571da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
106240630b82SMichael S. Tsirkin 	case IFF_TUN:
1063758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
1064758e43b7SStephen Hemminger 
10651da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
10661da177e4SLinus Torvalds 		dev->hard_header_len = 0;
10671da177e4SLinus Torvalds 		dev->addr_len = 0;
10681da177e4SLinus Torvalds 		dev->mtu = 1500;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 		/* Zero header length */
10711da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
10721da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
10731da177e4SLinus Torvalds 		break;
10741da177e4SLinus Torvalds 
107540630b82SMichael S. Tsirkin 	case IFF_TAP:
10767a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
10771da177e4SLinus Torvalds 		/* Ethernet TAP Device */
10781da177e4SLinus Torvalds 		ether_setup(dev);
1079550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1080a676847bSstephen hemminger 		dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
108136226a8dSBrian Braunstein 
1082f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
108336226a8dSBrian Braunstein 
10841da177e4SLinus Torvalds 		break;
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds /* Character device part */
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /* Poll */
10911da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
10921da177e4SLinus Torvalds {
1093b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
1094b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
10953c8a9c63SMariusz Kozlowski 	struct sock *sk;
109633dccbb0SHerbert Xu 	unsigned int mask = 0;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	if (!tun)
1099eac9e902SEric W. Biederman 		return POLLERR;
11001da177e4SLinus Torvalds 
110154f968d6SJason Wang 	sk = tfile->socket.sk;
11023c8a9c63SMariusz Kozlowski 
11036b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
11041da177e4SLinus Torvalds 
11059e641bdcSXi Wang 	poll_wait(file, sk_sleep(sk), wait);
11061da177e4SLinus Torvalds 
110789f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
11081da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
11091da177e4SLinus Torvalds 
111033dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
11119cd3e072SEric Dumazet 	    (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
111233dccbb0SHerbert Xu 	     sock_writeable(sk)))
111333dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
111433dccbb0SHerbert Xu 
1115c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
1116c70f1829SEric W. Biederman 		mask = POLLERR;
1117c70f1829SEric W. Biederman 
1118631ab46bSEric W. Biederman 	tun_put(tun);
11191da177e4SLinus Torvalds 	return mask;
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
1122f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
1123f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
112454f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
112533dccbb0SHerbert Xu 				     size_t prepad, size_t len,
112633dccbb0SHerbert Xu 				     size_t linear, int noblock)
1127f42157cbSRusty Russell {
112854f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
1129f42157cbSRusty Russell 	struct sk_buff *skb;
113033dccbb0SHerbert Xu 	int err;
1131f42157cbSRusty Russell 
1132f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
11330eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
113433dccbb0SHerbert Xu 		linear = len;
1135f42157cbSRusty Russell 
113633dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
113728d64271SEric Dumazet 				   &err, 0);
1138f42157cbSRusty Russell 	if (!skb)
113933dccbb0SHerbert Xu 		return ERR_PTR(err);
1140f42157cbSRusty Russell 
1141f42157cbSRusty Russell 	skb_reserve(skb, prepad);
1142f42157cbSRusty Russell 	skb_put(skb, linear);
114333dccbb0SHerbert Xu 	skb->data_len = len - linear;
114433dccbb0SHerbert Xu 	skb->len += len - linear;
1145f42157cbSRusty Russell 
1146f42157cbSRusty Russell 	return skb;
1147f42157cbSRusty Russell }
1148f42157cbSRusty Russell 
11491da177e4SLinus Torvalds /* Get packet from user space buffer */
115054f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1151f5ff53b4SAl Viro 			    void *msg_control, struct iov_iter *from,
1152f5ff53b4SAl Viro 			    int noblock)
11531da177e4SLinus Torvalds {
115409640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
11551da177e4SLinus Torvalds 	struct sk_buff *skb;
1156f5ff53b4SAl Viro 	size_t total_len = iov_iter_count(from);
1157eaea34b2SPaolo Abeni 	size_t len = total_len, align = tun->align, linear;
1158f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
1159608b9977SPaolo Abeni 	struct tun_pcpu_stats *stats;
116096f8d9ecSJason Wang 	int good_linear;
11610690899bSMichael S. Tsirkin 	int copylen;
11620690899bSMichael S. Tsirkin 	bool zerocopy = false;
11630690899bSMichael S. Tsirkin 	int err;
116449974420SEric Dumazet 	u32 rxhash;
1165f5ff53b4SAl Viro 	ssize_t n;
11661da177e4SLinus Torvalds 
11671bd4978aSEric Dumazet 	if (!(tun->dev->flags & IFF_UP))
11681bd4978aSEric Dumazet 		return -EIO;
11691bd4978aSEric Dumazet 
117040630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
117115718ea0SDan Carpenter 		if (len < sizeof(pi))
11721da177e4SLinus Torvalds 			return -EINVAL;
117315718ea0SDan Carpenter 		len -= sizeof(pi);
11741da177e4SLinus Torvalds 
1175f5ff53b4SAl Viro 		n = copy_from_iter(&pi, sizeof(pi), from);
1176f5ff53b4SAl Viro 		if (n != sizeof(pi))
11771da177e4SLinus Torvalds 			return -EFAULT;
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 
118040630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR) {
118115718ea0SDan Carpenter 		if (len < tun->vnet_hdr_sz)
1182f43798c2SRusty Russell 			return -EINVAL;
118315718ea0SDan Carpenter 		len -= tun->vnet_hdr_sz;
1184f43798c2SRusty Russell 
1185f5ff53b4SAl Viro 		n = copy_from_iter(&gso, sizeof(gso), from);
1186f5ff53b4SAl Viro 		if (n != sizeof(gso))
1187f43798c2SRusty Russell 			return -EFAULT;
1188f43798c2SRusty Russell 
11894909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
119056f0dcc5SMichael S. Tsirkin 		    tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
119156f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
11924909122fSHerbert Xu 
119356f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > len)
1194f43798c2SRusty Russell 			return -EINVAL;
1195d8febb77SHerbert Xu 		iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso));
1196f43798c2SRusty Russell 	}
1197f43798c2SRusty Russell 
119840630b82SMichael S. Tsirkin 	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
1199a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
12000eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
120156f0dcc5SMichael S. Tsirkin 			     (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
1202e01bf1c8SRusty Russell 			return -EINVAL;
1203e01bf1c8SRusty Russell 	}
12041da177e4SLinus Torvalds 
120596f8d9ecSJason Wang 	good_linear = SKB_MAX_HEAD(align);
120696f8d9ecSJason Wang 
120788529176SJason Wang 	if (msg_control) {
1208f5ff53b4SAl Viro 		struct iov_iter i = *from;
1209f5ff53b4SAl Viro 
121088529176SJason Wang 		/* There are 256 bytes to be copied in skb, so there is
121188529176SJason Wang 		 * enough room for skb expand head in case it is used.
12120690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
12130690899bSMichael S. Tsirkin 		 */
121456f0dcc5SMichael S. Tsirkin 		copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
121596f8d9ecSJason Wang 		if (copylen > good_linear)
121696f8d9ecSJason Wang 			copylen = good_linear;
12173dd5c330SJason Wang 		linear = copylen;
1218f5ff53b4SAl Viro 		iov_iter_advance(&i, copylen);
1219f5ff53b4SAl Viro 		if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
122088529176SJason Wang 			zerocopy = true;
122188529176SJason Wang 	}
122288529176SJason Wang 
122388529176SJason Wang 	if (!zerocopy) {
12240690899bSMichael S. Tsirkin 		copylen = len;
122556f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
122696f8d9ecSJason Wang 			linear = good_linear;
122796f8d9ecSJason Wang 		else
122856f0dcc5SMichael S. Tsirkin 			linear = tun16_to_cpu(tun, gso.hdr_len);
12293dd5c330SJason Wang 	}
12300690899bSMichael S. Tsirkin 
12313dd5c330SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
123233dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
123333dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
1234608b9977SPaolo Abeni 			this_cpu_inc(tun->pcpu_stats->rx_dropped);
123533dccbb0SHerbert Xu 		return PTR_ERR(skb);
12361da177e4SLinus Torvalds 	}
12371da177e4SLinus Torvalds 
12380690899bSMichael S. Tsirkin 	if (zerocopy)
1239f5ff53b4SAl Viro 		err = zerocopy_sg_from_iter(skb, from);
124088529176SJason Wang 	else {
1241f5ff53b4SAl Viro 		err = skb_copy_datagram_from_iter(skb, 0, from, len);
124288529176SJason Wang 		if (!err && msg_control) {
124388529176SJason Wang 			struct ubuf_info *uarg = msg_control;
124488529176SJason Wang 			uarg->callback(uarg, false);
124588529176SJason Wang 		}
124688529176SJason Wang 	}
12470690899bSMichael S. Tsirkin 
12480690899bSMichael S. Tsirkin 	if (err) {
1249608b9977SPaolo Abeni 		this_cpu_inc(tun->pcpu_stats->rx_dropped);
12508f22757eSDave Jones 		kfree_skb(skb);
12511da177e4SLinus Torvalds 		return -EFAULT;
12528f22757eSDave Jones 	}
12531da177e4SLinus Torvalds 
1254f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
125556f0dcc5SMichael S. Tsirkin 		if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
125656f0dcc5SMichael S. Tsirkin 					  tun16_to_cpu(tun, gso.csum_offset))) {
1257608b9977SPaolo Abeni 			this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1258f43798c2SRusty Russell 			kfree_skb(skb);
1259f43798c2SRusty Russell 			return -EINVAL;
1260f43798c2SRusty Russell 		}
126188255375SMichał Mirosław 	}
1262f43798c2SRusty Russell 
12631da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
126440630b82SMichael S. Tsirkin 	case IFF_TUN:
126540630b82SMichael S. Tsirkin 		if (tun->flags & IFF_NO_PI) {
1266f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
1267f09f7ee2SAng Way Chuang 			case 0x40:
1268f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
1269f09f7ee2SAng Way Chuang 				break;
1270f09f7ee2SAng Way Chuang 			case 0x60:
1271f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
1272f09f7ee2SAng Way Chuang 				break;
1273f09f7ee2SAng Way Chuang 			default:
1274608b9977SPaolo Abeni 				this_cpu_inc(tun->pcpu_stats->rx_dropped);
1275f09f7ee2SAng Way Chuang 				kfree_skb(skb);
1276f09f7ee2SAng Way Chuang 				return -EINVAL;
1277f09f7ee2SAng Way Chuang 			}
1278f09f7ee2SAng Way Chuang 		}
1279f09f7ee2SAng Way Chuang 
1280459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
12811da177e4SLinus Torvalds 		skb->protocol = pi.proto;
12824c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
12831da177e4SLinus Torvalds 		break;
128440630b82SMichael S. Tsirkin 	case IFF_TAP:
12851da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
12861da177e4SLinus Torvalds 		break;
12876403eab1SJoe Perches 	}
12881da177e4SLinus Torvalds 
1289f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1290f43798c2SRusty Russell 		pr_debug("GSO!\n");
1291f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1292f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
1293c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1294f43798c2SRusty Russell 			break;
1295f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
1296c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1297f43798c2SRusty Russell 			break;
1298e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
1299c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1300e36aa25aSSridhar Samudrala 			break;
1301f43798c2SRusty Russell 		default:
1302608b9977SPaolo Abeni 			this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1303f43798c2SRusty Russell 			kfree_skb(skb);
1304f43798c2SRusty Russell 			return -EINVAL;
1305f43798c2SRusty Russell 		}
1306f43798c2SRusty Russell 
1307f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1308c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1309f43798c2SRusty Russell 
131056f0dcc5SMichael S. Tsirkin 		skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
1311f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
1312608b9977SPaolo Abeni 			this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1313f43798c2SRusty Russell 			kfree_skb(skb);
1314f43798c2SRusty Russell 			return -EINVAL;
1315f43798c2SRusty Russell 		}
1316f43798c2SRusty Russell 
1317f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
1318f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1319f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
1320f43798c2SRusty Russell 	}
13211da177e4SLinus Torvalds 
13220690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
13230690899bSMichael S. Tsirkin 	if (zerocopy) {
13240690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
13250690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1326c9af6db4SPravin B Shelar 		skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
13270690899bSMichael S. Tsirkin 	}
13280690899bSMichael S. Tsirkin 
132972f65107SVlad Yasevich 	skb_reset_network_header(skb);
133040893fd0SJason Wang 	skb_probe_transport_header(skb, 0);
133138502af7SJason Wang 
13323958afa1STom Herbert 	rxhash = skb_get_hash(skb);
13331da177e4SLinus Torvalds 	netif_rx_ni(skb);
13341da177e4SLinus Torvalds 
1335608b9977SPaolo Abeni 	stats = get_cpu_ptr(tun->pcpu_stats);
1336608b9977SPaolo Abeni 	u64_stats_update_begin(&stats->syncp);
1337608b9977SPaolo Abeni 	stats->rx_packets++;
1338608b9977SPaolo Abeni 	stats->rx_bytes += len;
1339608b9977SPaolo Abeni 	u64_stats_update_end(&stats->syncp);
1340608b9977SPaolo Abeni 	put_cpu_ptr(stats);
13411da177e4SLinus Torvalds 
13429e85722dSJason Wang 	tun_flow_update(tun, rxhash, tfile);
13430690899bSMichael S. Tsirkin 	return total_len;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
1346f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
13471da177e4SLinus Torvalds {
134833dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
1349ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
135054f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
1351631ab46bSEric W. Biederman 	ssize_t result;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	if (!tun)
13541da177e4SLinus Torvalds 		return -EBADFD;
13551da177e4SLinus Torvalds 
1356f5ff53b4SAl Viro 	result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
1357631ab46bSEric W. Biederman 
1358631ab46bSEric W. Biederman 	tun_put(tun);
1359631ab46bSEric W. Biederman 	return result;
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds /* Put packet to the user space buffer */
13636f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
136454f968d6SJason Wang 			    struct tun_file *tfile,
13651da177e4SLinus Torvalds 			    struct sk_buff *skb,
1366e0b46d0eSHerbert Xu 			    struct iov_iter *iter)
13671da177e4SLinus Torvalds {
13681da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
1369608b9977SPaolo Abeni 	struct tun_pcpu_stats *stats;
1370e0b46d0eSHerbert Xu 	ssize_t total;
13718c847d25SJason Wang 	int vlan_offset = 0;
1372a8f9bfdfSHerbert Xu 	int vlan_hlen = 0;
13732eb783c4SHerbert Xu 	int vnet_hdr_sz = 0;
1374a8f9bfdfSHerbert Xu 
1375df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb))
1376a8f9bfdfSHerbert Xu 		vlan_hlen = VLAN_HLEN;
13771da177e4SLinus Torvalds 
137840630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR)
13792eb783c4SHerbert Xu 		vnet_hdr_sz = tun->vnet_hdr_sz;
13801da177e4SLinus Torvalds 
1381e0b46d0eSHerbert Xu 	total = skb->len + vlan_hlen + vnet_hdr_sz;
1382e0b46d0eSHerbert Xu 
138340630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
1384e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < sizeof(pi))
13851da177e4SLinus Torvalds 			return -EINVAL;
13861da177e4SLinus Torvalds 
1387e0b46d0eSHerbert Xu 		total += sizeof(pi);
1388e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < total) {
13891da177e4SLinus Torvalds 			/* Packet will be striped */
13901da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
13911da177e4SLinus Torvalds 		}
13921da177e4SLinus Torvalds 
1393e0b46d0eSHerbert Xu 		if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
13941da177e4SLinus Torvalds 			return -EFAULT;
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds 
13972eb783c4SHerbert Xu 	if (vnet_hdr_sz) {
1398f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1399e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < vnet_hdr_sz)
1400f43798c2SRusty Russell 			return -EINVAL;
1401f43798c2SRusty Russell 
1402f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1403f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1404f43798c2SRusty Russell 
1405f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
140656f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
140756f0dcc5SMichael S. Tsirkin 			gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
1408f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1409f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1410f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1411f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1412e3e3c423SVlad Yasevich 			else if (sinfo->gso_type & SKB_GSO_UDP)
1413e3e3c423SVlad Yasevich 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1414ef3db4a5SMichael S. Tsirkin 			else {
14156b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1416ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
141756f0dcc5SMichael S. Tsirkin 				       sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
141856f0dcc5SMichael S. Tsirkin 				       tun16_to_cpu(tun, gso.hdr_len));
1419ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1420ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1421ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
142256f0dcc5SMichael S. Tsirkin 					       min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1423ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1424ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1425ef3db4a5SMichael S. Tsirkin 			}
1426f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1427f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1428f43798c2SRusty Russell 		} else
1429f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1430f43798c2SRusty Russell 
1431f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1432f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
143356f0dcc5SMichael S. Tsirkin 			gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
143456f0dcc5SMichael S. Tsirkin 						      vlan_hlen);
143556f0dcc5SMichael S. Tsirkin 			gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
143610a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
143710a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1438f43798c2SRusty Russell 		} /* else everything is zero */
1439f43798c2SRusty Russell 
1440e0b46d0eSHerbert Xu 		if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
1441f43798c2SRusty Russell 			return -EFAULT;
14428c847d25SJason Wang 
14438c847d25SJason Wang 		iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
1444f43798c2SRusty Russell 	}
1445f43798c2SRusty Russell 
1446a8f9bfdfSHerbert Xu 	if (vlan_hlen) {
1447e0b46d0eSHerbert Xu 		int ret;
14486680ec68SJason Wang 		struct {
14496680ec68SJason Wang 			__be16 h_vlan_proto;
14506680ec68SJason Wang 			__be16 h_vlan_TCI;
14516680ec68SJason Wang 		} veth;
14521da177e4SLinus Torvalds 
14536680ec68SJason Wang 		veth.h_vlan_proto = skb->vlan_proto;
1454df8a39deSJiri Pirko 		veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
14551da177e4SLinus Torvalds 
14566680ec68SJason Wang 		vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
14576680ec68SJason Wang 
1458e0b46d0eSHerbert Xu 		ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1459e0b46d0eSHerbert Xu 		if (ret || !iov_iter_count(iter))
14606680ec68SJason Wang 			goto done;
14616680ec68SJason Wang 
1462e0b46d0eSHerbert Xu 		ret = copy_to_iter(&veth, sizeof(veth), iter);
1463e0b46d0eSHerbert Xu 		if (ret != sizeof(veth) || !iov_iter_count(iter))
14646680ec68SJason Wang 			goto done;
14656680ec68SJason Wang 	}
14666680ec68SJason Wang 
1467e0b46d0eSHerbert Xu 	skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
14686680ec68SJason Wang 
14696680ec68SJason Wang done:
1470608b9977SPaolo Abeni 	/* caller is in process context, */
1471608b9977SPaolo Abeni 	stats = get_cpu_ptr(tun->pcpu_stats);
1472608b9977SPaolo Abeni 	u64_stats_update_begin(&stats->syncp);
1473608b9977SPaolo Abeni 	stats->tx_packets++;
1474608b9977SPaolo Abeni 	stats->tx_bytes += skb->len + vlan_hlen;
1475608b9977SPaolo Abeni 	u64_stats_update_end(&stats->syncp);
1476608b9977SPaolo Abeni 	put_cpu_ptr(tun->pcpu_stats);
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	return total;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
148154f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
14829b067034SAl Viro 			   struct iov_iter *to,
14839b067034SAl Viro 			   int noblock)
14841da177e4SLinus Torvalds {
14851da177e4SLinus Torvalds 	struct sk_buff *skb;
14869b067034SAl Viro 	ssize_t ret;
14879e641bdcSXi Wang 	int peeked, err, off = 0;
14881da177e4SLinus Torvalds 
14893872baf6SRami Rosen 	tun_debug(KERN_INFO, tun, "tun_do_read\n");
14901da177e4SLinus Torvalds 
14919b067034SAl Viro 	if (!iov_iter_count(to))
14929b067034SAl Viro 		return 0;
14931da177e4SLinus Torvalds 
14949e641bdcSXi Wang 	if (tun->dev->reg_state != NETREG_REGISTERED)
14959e641bdcSXi Wang 		return -EIO;
14961da177e4SLinus Torvalds 
14979e641bdcSXi Wang 	/* Read frames from queue */
14989e641bdcSXi Wang 	skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
14999e641bdcSXi Wang 				  &peeked, &off, &err);
1500e0b46d0eSHerbert Xu 	if (!skb)
1501957f094fSAlex Gartrell 		return err;
1502e0b46d0eSHerbert Xu 
15039b067034SAl Viro 	ret = tun_put_user(tun, tfile, skb, to);
1504f51a5e82SJason Wang 	if (unlikely(ret < 0))
15051da177e4SLinus Torvalds 		kfree_skb(skb);
1506f51a5e82SJason Wang 	else
1507f51a5e82SJason Wang 		consume_skb(skb);
15081da177e4SLinus Torvalds 
150905c2828cSMichael S. Tsirkin 	return ret;
151005c2828cSMichael S. Tsirkin }
151105c2828cSMichael S. Tsirkin 
15129b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
151305c2828cSMichael S. Tsirkin {
151405c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
151505c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
151605c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
15179b067034SAl Viro 	ssize_t len = iov_iter_count(to), ret;
151805c2828cSMichael S. Tsirkin 
151905c2828cSMichael S. Tsirkin 	if (!tun)
152005c2828cSMichael S. Tsirkin 		return -EBADFD;
15219b067034SAl Viro 	ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK);
152242404c09SDavid S. Miller 	ret = min_t(ssize_t, ret, len);
1523d0b7da8aSZhi Yong Wu 	if (ret > 0)
1524d0b7da8aSZhi Yong Wu 		iocb->ki_pos = ret;
1525631ab46bSEric W. Biederman 	tun_put(tun);
15261da177e4SLinus Torvalds 	return ret;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
152996442e42SJason Wang static void tun_free_netdev(struct net_device *dev)
153096442e42SJason Wang {
153196442e42SJason Wang 	struct tun_struct *tun = netdev_priv(dev);
153296442e42SJason Wang 
15334008e97fSJason Wang 	BUG_ON(!(list_empty(&tun->disabled)));
1534608b9977SPaolo Abeni 	free_percpu(tun->pcpu_stats);
153596442e42SJason Wang 	tun_flow_uninit(tun);
15365dbbaf2dSPaul Moore 	security_tun_dev_free_security(tun->security);
153796442e42SJason Wang 	free_netdev(dev);
153896442e42SJason Wang }
153996442e42SJason Wang 
15401da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
15431da177e4SLinus Torvalds 
15440625c883SEric W. Biederman 	tun->owner = INVALID_UID;
15450625c883SEric W. Biederman 	tun->group = INVALID_GID;
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
154896442e42SJason Wang 	dev->destructor = tun_free_netdev;
1549016adb72SJason Wang 	/* We prefer our own queue length */
1550016adb72SJason Wang 	dev->tx_queue_len = TUN_READQ_SIZE;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
1553f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1554f019a7a5SEric W. Biederman  * device with netlink.
1555f019a7a5SEric W. Biederman  */
1556f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1557f019a7a5SEric W. Biederman {
1558f019a7a5SEric W. Biederman 	return -EINVAL;
1559f019a7a5SEric W. Biederman }
1560f019a7a5SEric W. Biederman 
1561f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1562f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1563f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1564f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1565f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1566f019a7a5SEric W. Biederman };
1567f019a7a5SEric W. Biederman 
156833dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
156933dccbb0SHerbert Xu {
157054f968d6SJason Wang 	struct tun_file *tfile;
157143815482SEric Dumazet 	wait_queue_head_t *wqueue;
157233dccbb0SHerbert Xu 
157333dccbb0SHerbert Xu 	if (!sock_writeable(sk))
157433dccbb0SHerbert Xu 		return;
157533dccbb0SHerbert Xu 
15769cd3e072SEric Dumazet 	if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
157733dccbb0SHerbert Xu 		return;
157833dccbb0SHerbert Xu 
157943815482SEric Dumazet 	wqueue = sk_sleep(sk);
158043815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
158143815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
158205c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1583c722c625SHerbert Xu 
158454f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
158554f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
158633dccbb0SHerbert Xu }
158733dccbb0SHerbert Xu 
15881b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
158905c2828cSMichael S. Tsirkin {
159054f968d6SJason Wang 	int ret;
159154f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
159254f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
159354f968d6SJason Wang 
159454f968d6SJason Wang 	if (!tun)
159554f968d6SJason Wang 		return -EBADFD;
1596f5ff53b4SAl Viro 
1597c0371da6SAl Viro 	ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
1598f5ff53b4SAl Viro 			   m->msg_flags & MSG_DONTWAIT);
159954f968d6SJason Wang 	tun_put(tun);
160054f968d6SJason Wang 	return ret;
160105c2828cSMichael S. Tsirkin }
160205c2828cSMichael S. Tsirkin 
16031b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
160405c2828cSMichael S. Tsirkin 		       int flags)
160505c2828cSMichael S. Tsirkin {
160654f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
160754f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
160805c2828cSMichael S. Tsirkin 	int ret;
160954f968d6SJason Wang 
161054f968d6SJason Wang 	if (!tun)
161154f968d6SJason Wang 		return -EBADFD;
161254f968d6SJason Wang 
1613eda29772SRichard Cochran 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
16143811ae76SGao feng 		ret = -EINVAL;
16153811ae76SGao feng 		goto out;
16163811ae76SGao feng 	}
1617eda29772SRichard Cochran 	if (flags & MSG_ERRQUEUE) {
1618eda29772SRichard Cochran 		ret = sock_recv_errqueue(sock->sk, m, total_len,
1619eda29772SRichard Cochran 					 SOL_PACKET, TUN_TX_TIMESTAMP);
1620eda29772SRichard Cochran 		goto out;
1621eda29772SRichard Cochran 	}
1622c0371da6SAl Viro 	ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT);
162387897931SAlex Gartrell 	if (ret > (ssize_t)total_len) {
162442404c09SDavid S. Miller 		m->msg_flags |= MSG_TRUNC;
162542404c09SDavid S. Miller 		ret = flags & MSG_TRUNC ? ret : total_len;
162642404c09SDavid S. Miller 	}
16273811ae76SGao feng out:
162854f968d6SJason Wang 	tun_put(tun);
162905c2828cSMichael S. Tsirkin 	return ret;
163005c2828cSMichael S. Tsirkin }
163105c2828cSMichael S. Tsirkin 
163205c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
163305c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
163405c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
163505c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
163605c2828cSMichael S. Tsirkin };
163705c2828cSMichael S. Tsirkin 
163833dccbb0SHerbert Xu static struct proto tun_proto = {
163933dccbb0SHerbert Xu 	.name		= "tun",
164033dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
164154f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
164233dccbb0SHerbert Xu };
1643f019a7a5SEric W. Biederman 
1644980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1645980c9e8cSDavid Woodhouse {
1646031f5e03SMichael S. Tsirkin 	return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
1647980c9e8cSDavid Woodhouse }
1648980c9e8cSDavid Woodhouse 
1649980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1650980c9e8cSDavid Woodhouse 			      char *buf)
1651980c9e8cSDavid Woodhouse {
1652980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1653980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1654980c9e8cSDavid Woodhouse }
1655980c9e8cSDavid Woodhouse 
1656980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1657980c9e8cSDavid Woodhouse 			      char *buf)
1658980c9e8cSDavid Woodhouse {
1659980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
16600625c883SEric W. Biederman 	return uid_valid(tun->owner)?
16610625c883SEric W. Biederman 		sprintf(buf, "%u\n",
16620625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
16630625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1664980c9e8cSDavid Woodhouse }
1665980c9e8cSDavid Woodhouse 
1666980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1667980c9e8cSDavid Woodhouse 			      char *buf)
1668980c9e8cSDavid Woodhouse {
1669980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
16700625c883SEric W. Biederman 	return gid_valid(tun->group) ?
16710625c883SEric W. Biederman 		sprintf(buf, "%u\n",
16720625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
16730625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1674980c9e8cSDavid Woodhouse }
1675980c9e8cSDavid Woodhouse 
1676980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1677980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1678980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1679980c9e8cSDavid Woodhouse 
1680c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = {
1681c4d33e24STakashi Iwai 	&dev_attr_tun_flags.attr,
1682c4d33e24STakashi Iwai 	&dev_attr_owner.attr,
1683c4d33e24STakashi Iwai 	&dev_attr_group.attr,
1684c4d33e24STakashi Iwai 	NULL
1685c4d33e24STakashi Iwai };
1686c4d33e24STakashi Iwai 
1687c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = {
1688c4d33e24STakashi Iwai 	.attrs = tun_dev_attrs
1689c4d33e24STakashi Iwai };
1690c4d33e24STakashi Iwai 
1691d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
16921da177e4SLinus Torvalds {
16931da177e4SLinus Torvalds 	struct tun_struct *tun;
169454f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
16951da177e4SLinus Torvalds 	struct net_device *dev;
16961da177e4SLinus Torvalds 	int err;
16971da177e4SLinus Torvalds 
16987c0c3b1aSJason Wang 	if (tfile->detached)
16997c0c3b1aSJason Wang 		return -EINVAL;
17007c0c3b1aSJason Wang 
170174a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
170274a3e5a7SEric W. Biederman 	if (dev) {
1703f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1704f85ba780SDavid Woodhouse 			return -EBUSY;
170574a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
170674a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
170774a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
170874a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
170974a3e5a7SEric W. Biederman 		else
171074a3e5a7SEric W. Biederman 			return -EINVAL;
171174a3e5a7SEric W. Biederman 
17128e6d91aeSJason Wang 		if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
171340630b82SMichael S. Tsirkin 		    !!(tun->flags & IFF_MULTI_QUEUE))
17148e6d91aeSJason Wang 			return -EINVAL;
17158e6d91aeSJason Wang 
1716cde8b15fSJason Wang 		if (tun_not_capable(tun))
17172b980dbdSPaul Moore 			return -EPERM;
17185dbbaf2dSPaul Moore 		err = security_tun_dev_open(tun->security);
17192b980dbdSPaul Moore 		if (err < 0)
17202b980dbdSPaul Moore 			return err;
17212b980dbdSPaul Moore 
1722849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
1723a7385ba2SEric W. Biederman 		if (err < 0)
1724a7385ba2SEric W. Biederman 			return err;
17254008e97fSJason Wang 
172640630b82SMichael S. Tsirkin 		if (tun->flags & IFF_MULTI_QUEUE &&
1727e8dbad66SJason Wang 		    (tun->numqueues + tun->numdisabled > 1)) {
1728e8dbad66SJason Wang 			/* One or more queue has already been attached, no need
1729e8dbad66SJason Wang 			 * to initialize the device again.
1730e8dbad66SJason Wang 			 */
1731e8dbad66SJason Wang 			return 0;
1732e8dbad66SJason Wang 		}
173386a264abSDavid Howells 	}
17341da177e4SLinus Torvalds 	else {
17351da177e4SLinus Torvalds 		char *name;
17361da177e4SLinus Torvalds 		unsigned long flags = 0;
1737edfb6a14SJason Wang 		int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1738edfb6a14SJason Wang 			     MAX_TAP_QUEUES : 1;
17391da177e4SLinus Torvalds 
1740c260b772SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1741ca6bb5d7SDavid Woodhouse 			return -EPERM;
17422b980dbdSPaul Moore 		err = security_tun_dev_create();
17432b980dbdSPaul Moore 		if (err < 0)
17442b980dbdSPaul Moore 			return err;
1745ca6bb5d7SDavid Woodhouse 
17461da177e4SLinus Torvalds 		/* Set dev type */
17471da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
17481da177e4SLinus Torvalds 			/* TUN device */
174940630b82SMichael S. Tsirkin 			flags |= IFF_TUN;
17501da177e4SLinus Torvalds 			name = "tun%d";
17511da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
17521da177e4SLinus Torvalds 			/* TAP device */
175340630b82SMichael S. Tsirkin 			flags |= IFF_TAP;
17541da177e4SLinus Torvalds 			name = "tap%d";
17551da177e4SLinus Torvalds 		} else
175636989b90SKusanagi Kouichi 			return -EINVAL;
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 		if (*ifr->ifr_name)
17591da177e4SLinus Torvalds 			name = ifr->ifr_name;
17601da177e4SLinus Torvalds 
1761c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1762c835a677STom Gundersen 				       NET_NAME_UNKNOWN, tun_setup, queues,
1763c835a677STom Gundersen 				       queues);
1764edfb6a14SJason Wang 
17651da177e4SLinus Torvalds 		if (!dev)
17661da177e4SLinus Torvalds 			return -ENOMEM;
17671da177e4SLinus Torvalds 
1768fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1769f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1770fb7589a1SPavel Emelyanov 		dev->ifindex = tfile->ifindex;
1771c4d33e24STakashi Iwai 		dev->sysfs_groups[0] = &tun_attr_group;
1772758e43b7SStephen Hemminger 
17731da177e4SLinus Torvalds 		tun = netdev_priv(dev);
17741da177e4SLinus Torvalds 		tun->dev = dev;
17751da177e4SLinus Torvalds 		tun->flags = flags;
1776f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1777d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
17781da177e4SLinus Torvalds 
1779eaea34b2SPaolo Abeni 		tun->align = NET_SKB_PAD;
178054f968d6SJason Wang 		tun->filter_attached = false;
178154f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
178233dccbb0SHerbert Xu 
1783608b9977SPaolo Abeni 		tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
1784608b9977SPaolo Abeni 		if (!tun->pcpu_stats) {
1785608b9977SPaolo Abeni 			err = -ENOMEM;
1786608b9977SPaolo Abeni 			goto err_free_dev;
1787608b9977SPaolo Abeni 		}
1788608b9977SPaolo Abeni 
178996442e42SJason Wang 		spin_lock_init(&tun->lock);
179096442e42SJason Wang 
17915dbbaf2dSPaul Moore 		err = security_tun_dev_alloc_security(&tun->security);
17925dbbaf2dSPaul Moore 		if (err < 0)
1793608b9977SPaolo Abeni 			goto err_free_stat;
17942b980dbdSPaul Moore 
17951da177e4SLinus Torvalds 		tun_net_init(dev);
1796944a1376SPavel Emelyanov 		tun_flow_init(tun);
179796442e42SJason Wang 
179888255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
17996680ec68SJason Wang 				   TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
18006680ec68SJason Wang 				   NETIF_F_HW_VLAN_STAG_TX;
18012a2bbf17SPaolo Abeni 		dev->features = dev->hw_features | NETIF_F_LLTX;
18026671b224SFernando Luis Vazquez Cao 		dev->vlan_features = dev->features &
18036671b224SFernando Luis Vazquez Cao 				     ~(NETIF_F_HW_VLAN_CTAG_TX |
18046671b224SFernando Luis Vazquez Cao 				       NETIF_F_HW_VLAN_STAG_TX);
180588255375SMichał Mirosław 
18064008e97fSJason Wang 		INIT_LIST_HEAD(&tun->disabled);
1807849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, false);
1808eb0fb363SJason Wang 		if (err < 0)
1809662ca437SJason Wang 			goto err_free_flow;
1810eb0fb363SJason Wang 
18111da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
18121da177e4SLinus Torvalds 		if (err < 0)
1813662ca437SJason Wang 			goto err_detach;
1814af668b3cSMichael S. Tsirkin 	}
1815980c9e8cSDavid Woodhouse 
1816eb0fb363SJason Wang 	netif_carrier_on(tun->dev);
18171da177e4SLinus Torvalds 
18186b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
18191da177e4SLinus Torvalds 
1820031f5e03SMichael S. Tsirkin 	tun->flags = (tun->flags & ~TUN_FEATURES) |
1821031f5e03SMichael S. Tsirkin 		(ifr->ifr_flags & TUN_FEATURES);
1822c8d68e6bSJason Wang 
1823e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1824e35259a9SMax Krasnyansky 	 * xoff state.
1825e35259a9SMax Krasnyansky 	 */
1826e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1827c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1828e35259a9SMax Krasnyansky 
18291da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
18301da177e4SLinus Torvalds 	return 0;
18311da177e4SLinus Torvalds 
1832662ca437SJason Wang err_detach:
1833662ca437SJason Wang 	tun_detach_all(dev);
1834662ca437SJason Wang err_free_flow:
1835662ca437SJason Wang 	tun_flow_uninit(tun);
1836662ca437SJason Wang 	security_tun_dev_free_security(tun->security);
1837608b9977SPaolo Abeni err_free_stat:
1838608b9977SPaolo Abeni 	free_percpu(tun->pcpu_stats);
18391da177e4SLinus Torvalds err_free_dev:
18401da177e4SLinus Torvalds 	free_netdev(dev);
18411da177e4SLinus Torvalds 	return err;
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
18449ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun,
1845876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1846e3b99556SMark McLoughlin {
18476b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1848e3b99556SMark McLoughlin 
1849e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1850e3b99556SMark McLoughlin 
1851980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1852e3b99556SMark McLoughlin 
1853e3b99556SMark McLoughlin }
1854e3b99556SMark McLoughlin 
18555228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
18565228ddc9SRusty Russell  * privs required. */
185788255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
18585228ddc9SRusty Russell {
1859c8f44affSMichał Mirosław 	netdev_features_t features = 0;
18605228ddc9SRusty Russell 
18615228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
186288255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
18635228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
18645228ddc9SRusty Russell 
18655228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
18665228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
18675228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
18685228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
18695228ddc9SRusty Russell 			}
18705228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
18715228ddc9SRusty Russell 				features |= NETIF_F_TSO;
18725228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
18735228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
18745228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
18755228ddc9SRusty Russell 		}
1876e3e3c423SVlad Yasevich 
1877e3e3c423SVlad Yasevich 		if (arg & TUN_F_UFO) {
1878e3e3c423SVlad Yasevich 			features |= NETIF_F_UFO;
1879e3e3c423SVlad Yasevich 			arg &= ~TUN_F_UFO;
1880e3e3c423SVlad Yasevich 		}
18815228ddc9SRusty Russell 	}
18825228ddc9SRusty Russell 
18835228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
18845228ddc9SRusty Russell 	 * trying to set them. */
18855228ddc9SRusty Russell 	if (arg)
18865228ddc9SRusty Russell 		return -EINVAL;
18875228ddc9SRusty Russell 
188888255375SMichał Mirosław 	tun->set_features = features;
188988255375SMichał Mirosław 	netdev_update_features(tun->dev);
18905228ddc9SRusty Russell 
18915228ddc9SRusty Russell 	return 0;
18925228ddc9SRusty Russell }
18935228ddc9SRusty Russell 
1894c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1895c8d68e6bSJason Wang {
1896c8d68e6bSJason Wang 	int i;
1897c8d68e6bSJason Wang 	struct tun_file *tfile;
1898c8d68e6bSJason Wang 
1899c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1900b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
19018ced425eSHannes Frederic Sowa 		lock_sock(tfile->socket.sk);
19028ced425eSHannes Frederic Sowa 		sk_detach_filter(tfile->socket.sk);
19038ced425eSHannes Frederic Sowa 		release_sock(tfile->socket.sk);
1904c8d68e6bSJason Wang 	}
1905c8d68e6bSJason Wang 
1906c8d68e6bSJason Wang 	tun->filter_attached = false;
1907c8d68e6bSJason Wang }
1908c8d68e6bSJason Wang 
1909c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1910c8d68e6bSJason Wang {
1911c8d68e6bSJason Wang 	int i, ret = 0;
1912c8d68e6bSJason Wang 	struct tun_file *tfile;
1913c8d68e6bSJason Wang 
1914c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1915b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
19168ced425eSHannes Frederic Sowa 		lock_sock(tfile->socket.sk);
19178ced425eSHannes Frederic Sowa 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
19188ced425eSHannes Frederic Sowa 		release_sock(tfile->socket.sk);
1919c8d68e6bSJason Wang 		if (ret) {
1920c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1921c8d68e6bSJason Wang 			return ret;
1922c8d68e6bSJason Wang 		}
1923c8d68e6bSJason Wang 	}
1924c8d68e6bSJason Wang 
1925c8d68e6bSJason Wang 	tun->filter_attached = true;
1926c8d68e6bSJason Wang 	return ret;
1927c8d68e6bSJason Wang }
1928c8d68e6bSJason Wang 
1929c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1930c8d68e6bSJason Wang {
1931c8d68e6bSJason Wang 	struct tun_file *tfile;
1932c8d68e6bSJason Wang 	int i;
1933c8d68e6bSJason Wang 
1934c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1935b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1936c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1937c8d68e6bSJason Wang 	}
1938c8d68e6bSJason Wang }
1939c8d68e6bSJason Wang 
1940cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1941cde8b15fSJason Wang {
1942cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1943cde8b15fSJason Wang 	struct tun_struct *tun;
1944cde8b15fSJason Wang 	int ret = 0;
1945cde8b15fSJason Wang 
1946cde8b15fSJason Wang 	rtnl_lock();
1947cde8b15fSJason Wang 
1948cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
19494008e97fSJason Wang 		tun = tfile->detached;
19505dbbaf2dSPaul Moore 		if (!tun) {
1951cde8b15fSJason Wang 			ret = -EINVAL;
19525dbbaf2dSPaul Moore 			goto unlock;
19535dbbaf2dSPaul Moore 		}
19545dbbaf2dSPaul Moore 		ret = security_tun_dev_attach_queue(tun->security);
19555dbbaf2dSPaul Moore 		if (ret < 0)
19565dbbaf2dSPaul Moore 			goto unlock;
1957849c9b6fSPavel Emelyanov 		ret = tun_attach(tun, file, false);
19584008e97fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
1959b8deabd3SJason Wang 		tun = rtnl_dereference(tfile->tun);
196040630b82SMichael S. Tsirkin 		if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
19614008e97fSJason Wang 			ret = -EINVAL;
1962cde8b15fSJason Wang 		else
19634008e97fSJason Wang 			__tun_detach(tfile, false);
19644008e97fSJason Wang 	} else
1965cde8b15fSJason Wang 		ret = -EINVAL;
1966cde8b15fSJason Wang 
19675dbbaf2dSPaul Moore unlock:
1968cde8b15fSJason Wang 	rtnl_unlock();
1969cde8b15fSJason Wang 	return ret;
1970cde8b15fSJason Wang }
1971cde8b15fSJason Wang 
197250857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
197350857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
19741da177e4SLinus Torvalds {
197536b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1976631ab46bSEric W. Biederman 	struct tun_struct *tun;
19771da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
19781da177e4SLinus Torvalds 	struct ifreq ifr;
19790625c883SEric W. Biederman 	kuid_t owner;
19800625c883SEric W. Biederman 	kgid_t group;
198133dccbb0SHerbert Xu 	int sndbuf;
1982d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1983fb7589a1SPavel Emelyanov 	unsigned int ifindex;
19841cf8e410SMichael S. Tsirkin 	int le;
1985f271b2ccSMax Krasnyansky 	int ret;
19861da177e4SLinus Torvalds 
1987cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
198850857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
19891da177e4SLinus Torvalds 			return -EFAULT;
19908bbb1813SDavid S. Miller 	} else {
1991a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
19928bbb1813SDavid S. Miller 	}
1993631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1994631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1995631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1996031f5e03SMichael S. Tsirkin 		 * TUNSETIFF.
1997031f5e03SMichael S. Tsirkin 		 */
1998031f5e03SMichael S. Tsirkin 		return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
1999631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
2000cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
2001cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
2002631ab46bSEric W. Biederman 
2003c8d68e6bSJason Wang 	ret = 0;
2004876bfd4dSHerbert Xu 	rtnl_lock();
2005876bfd4dSHerbert Xu 
200636b50babSEric W. Biederman 	tun = __tun_get(tfile);
20071da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
20081da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
20091da177e4SLinus Torvalds 
2010140e807dSEric W. Biederman 		ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
20111da177e4SLinus Torvalds 
2012876bfd4dSHerbert Xu 		if (ret)
2013876bfd4dSHerbert Xu 			goto unlock;
20141da177e4SLinus Torvalds 
201550857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
2016876bfd4dSHerbert Xu 			ret = -EFAULT;
2017876bfd4dSHerbert Xu 		goto unlock;
20181da177e4SLinus Torvalds 	}
2019fb7589a1SPavel Emelyanov 	if (cmd == TUNSETIFINDEX) {
2020fb7589a1SPavel Emelyanov 		ret = -EPERM;
2021fb7589a1SPavel Emelyanov 		if (tun)
2022fb7589a1SPavel Emelyanov 			goto unlock;
2023fb7589a1SPavel Emelyanov 
2024fb7589a1SPavel Emelyanov 		ret = -EFAULT;
2025fb7589a1SPavel Emelyanov 		if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2026fb7589a1SPavel Emelyanov 			goto unlock;
2027fb7589a1SPavel Emelyanov 
2028fb7589a1SPavel Emelyanov 		ret = 0;
2029fb7589a1SPavel Emelyanov 		tfile->ifindex = ifindex;
2030fb7589a1SPavel Emelyanov 		goto unlock;
2031fb7589a1SPavel Emelyanov 	}
20321da177e4SLinus Torvalds 
2033876bfd4dSHerbert Xu 	ret = -EBADFD;
20341da177e4SLinus Torvalds 	if (!tun)
2035876bfd4dSHerbert Xu 		goto unlock;
20361da177e4SLinus Torvalds 
20371e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
20381da177e4SLinus Torvalds 
2039631ab46bSEric W. Biederman 	ret = 0;
20401da177e4SLinus Torvalds 	switch (cmd) {
2041e3b99556SMark McLoughlin 	case TUNGETIFF:
20429ce99cf6SRami Rosen 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2043e3b99556SMark McLoughlin 
20443d407a80SPavel Emelyanov 		if (tfile->detached)
20453d407a80SPavel Emelyanov 			ifr.ifr_flags |= IFF_DETACH_QUEUE;
2046849c9b6fSPavel Emelyanov 		if (!tfile->socket.sk->sk_filter)
2047849c9b6fSPavel Emelyanov 			ifr.ifr_flags |= IFF_NOFILTER;
20483d407a80SPavel Emelyanov 
204950857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
2050631ab46bSEric W. Biederman 			ret = -EFAULT;
2051e3b99556SMark McLoughlin 		break;
2052e3b99556SMark McLoughlin 
20531da177e4SLinus Torvalds 	case TUNSETNOCSUM:
20541da177e4SLinus Torvalds 		/* Disable/Enable checksum */
20551da177e4SLinus Torvalds 
205688255375SMichał Mirosław 		/* [unimplemented] */
205788255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
20586b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
20591da177e4SLinus Torvalds 		break;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	case TUNSETPERSIST:
206254f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
206354f968d6SJason Wang 		 * module to prevent the module being unprobed.
206454f968d6SJason Wang 		 */
206540630b82SMichael S. Tsirkin 		if (arg && !(tun->flags & IFF_PERSIST)) {
206640630b82SMichael S. Tsirkin 			tun->flags |= IFF_PERSIST;
206754f968d6SJason Wang 			__module_get(THIS_MODULE);
2068dd38bd85SJason Wang 		}
206940630b82SMichael S. Tsirkin 		if (!arg && (tun->flags & IFF_PERSIST)) {
207040630b82SMichael S. Tsirkin 			tun->flags &= ~IFF_PERSIST;
207154f968d6SJason Wang 			module_put(THIS_MODULE);
207254f968d6SJason Wang 		}
20731da177e4SLinus Torvalds 
20746b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
20756b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
20761da177e4SLinus Torvalds 		break;
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	case TUNSETOWNER:
20791da177e4SLinus Torvalds 		/* Set owner of the device */
20800625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
20810625c883SEric W. Biederman 		if (!uid_valid(owner)) {
20820625c883SEric W. Biederman 			ret = -EINVAL;
20830625c883SEric W. Biederman 			break;
20840625c883SEric W. Biederman 		}
20850625c883SEric W. Biederman 		tun->owner = owner;
20861e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
20870625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
20881da177e4SLinus Torvalds 		break;
20891da177e4SLinus Torvalds 
20908c644623SGuido Guenther 	case TUNSETGROUP:
20918c644623SGuido Guenther 		/* Set group of the device */
20920625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
20930625c883SEric W. Biederman 		if (!gid_valid(group)) {
20940625c883SEric W. Biederman 			ret = -EINVAL;
20950625c883SEric W. Biederman 			break;
20960625c883SEric W. Biederman 		}
20970625c883SEric W. Biederman 		tun->group = group;
20981e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
20990625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
21008c644623SGuido Guenther 		break;
21018c644623SGuido Guenther 
2102ff4cc3acSMike Kershaw 	case TUNSETLINK:
2103ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
2104ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
21056b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
21066b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
210748abfe05SDavid S. Miller 			ret = -EBUSY;
2108ff4cc3acSMike Kershaw 		} else {
2109ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
21106b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
21116b8a66eeSJoe Perches 				  tun->dev->type);
211248abfe05SDavid S. Miller 			ret = 0;
2113ff4cc3acSMike Kershaw 		}
2114631ab46bSEric W. Biederman 		break;
2115ff4cc3acSMike Kershaw 
21161da177e4SLinus Torvalds #ifdef TUN_DEBUG
21171da177e4SLinus Torvalds 	case TUNSETDEBUG:
21181da177e4SLinus Torvalds 		tun->debug = arg;
21191da177e4SLinus Torvalds 		break;
21201da177e4SLinus Torvalds #endif
21215228ddc9SRusty Russell 	case TUNSETOFFLOAD:
212288255375SMichał Mirosław 		ret = set_offload(tun, arg);
2123631ab46bSEric W. Biederman 		break;
21245228ddc9SRusty Russell 
2125f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
2126f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
2127631ab46bSEric W. Biederman 		ret = -EINVAL;
212840630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
2129631ab46bSEric W. Biederman 			break;
2130c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
2131631ab46bSEric W. Biederman 		break;
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
2134b595076aSUwe Kleine-König 		/* Get hw address */
2135f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2136f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
213750857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
2138631ab46bSEric W. Biederman 			ret = -EFAULT;
2139631ab46bSEric W. Biederman 		break;
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
2142f271b2ccSMax Krasnyansky 		/* Set hw address */
21436b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
21446b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
214540102371SKim B. Heino 
214640102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
2147631ab46bSEric W. Biederman 		break;
214833dccbb0SHerbert Xu 
214933dccbb0SHerbert Xu 	case TUNGETSNDBUF:
215054f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
215133dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
215233dccbb0SHerbert Xu 			ret = -EFAULT;
215333dccbb0SHerbert Xu 		break;
215433dccbb0SHerbert Xu 
215533dccbb0SHerbert Xu 	case TUNSETSNDBUF:
215633dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
215733dccbb0SHerbert Xu 			ret = -EFAULT;
215833dccbb0SHerbert Xu 			break;
215933dccbb0SHerbert Xu 		}
216033dccbb0SHerbert Xu 
2161c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
2162c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
216333dccbb0SHerbert Xu 		break;
216433dccbb0SHerbert Xu 
2165d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
2166d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
2167d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2168d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2169d9d52b51SMichael S. Tsirkin 		break;
2170d9d52b51SMichael S. Tsirkin 
2171d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
2172d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2173d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2174d9d52b51SMichael S. Tsirkin 			break;
2175d9d52b51SMichael S. Tsirkin 		}
2176d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2177d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
2178d9d52b51SMichael S. Tsirkin 			break;
2179d9d52b51SMichael S. Tsirkin 		}
2180d9d52b51SMichael S. Tsirkin 
2181d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
2182d9d52b51SMichael S. Tsirkin 		break;
2183d9d52b51SMichael S. Tsirkin 
21841cf8e410SMichael S. Tsirkin 	case TUNGETVNETLE:
21851cf8e410SMichael S. Tsirkin 		le = !!(tun->flags & TUN_VNET_LE);
21861cf8e410SMichael S. Tsirkin 		if (put_user(le, (int __user *)argp))
21871cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
21881cf8e410SMichael S. Tsirkin 		break;
21891cf8e410SMichael S. Tsirkin 
21901cf8e410SMichael S. Tsirkin 	case TUNSETVNETLE:
21911cf8e410SMichael S. Tsirkin 		if (get_user(le, (int __user *)argp)) {
21921cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
21931cf8e410SMichael S. Tsirkin 			break;
21941cf8e410SMichael S. Tsirkin 		}
21951cf8e410SMichael S. Tsirkin 		if (le)
21961cf8e410SMichael S. Tsirkin 			tun->flags |= TUN_VNET_LE;
21971cf8e410SMichael S. Tsirkin 		else
21981cf8e410SMichael S. Tsirkin 			tun->flags &= ~TUN_VNET_LE;
21991cf8e410SMichael S. Tsirkin 		break;
22001cf8e410SMichael S. Tsirkin 
22018b8e658bSGreg Kurz 	case TUNGETVNETBE:
22028b8e658bSGreg Kurz 		ret = tun_get_vnet_be(tun, argp);
22038b8e658bSGreg Kurz 		break;
22048b8e658bSGreg Kurz 
22058b8e658bSGreg Kurz 	case TUNSETVNETBE:
22068b8e658bSGreg Kurz 		ret = tun_set_vnet_be(tun, argp);
22078b8e658bSGreg Kurz 		break;
22088b8e658bSGreg Kurz 
220999405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
221099405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
221199405162SMichael S. Tsirkin 		ret = -EINVAL;
221240630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
221399405162SMichael S. Tsirkin 			break;
221499405162SMichael S. Tsirkin 		ret = -EFAULT;
221554f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
221699405162SMichael S. Tsirkin 			break;
221799405162SMichael S. Tsirkin 
2218c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
221999405162SMichael S. Tsirkin 		break;
222099405162SMichael S. Tsirkin 
222199405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
222299405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
222399405162SMichael S. Tsirkin 		ret = -EINVAL;
222440630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
222599405162SMichael S. Tsirkin 			break;
2226c8d68e6bSJason Wang 		ret = 0;
2227c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
222899405162SMichael S. Tsirkin 		break;
222999405162SMichael S. Tsirkin 
223076975e9cSPavel Emelyanov 	case TUNGETFILTER:
223176975e9cSPavel Emelyanov 		ret = -EINVAL;
223240630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
223376975e9cSPavel Emelyanov 			break;
223476975e9cSPavel Emelyanov 		ret = -EFAULT;
223576975e9cSPavel Emelyanov 		if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
223676975e9cSPavel Emelyanov 			break;
223776975e9cSPavel Emelyanov 		ret = 0;
223876975e9cSPavel Emelyanov 		break;
223976975e9cSPavel Emelyanov 
22401da177e4SLinus Torvalds 	default:
2241631ab46bSEric W. Biederman 		ret = -EINVAL;
2242631ab46bSEric W. Biederman 		break;
2243ee289b64SJoe Perches 	}
22441da177e4SLinus Torvalds 
2245876bfd4dSHerbert Xu unlock:
2246876bfd4dSHerbert Xu 	rtnl_unlock();
2247876bfd4dSHerbert Xu 	if (tun)
2248631ab46bSEric W. Biederman 		tun_put(tun);
2249631ab46bSEric W. Biederman 	return ret;
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds 
225250857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
225350857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
225450857e2aSArnd Bergmann {
225550857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
225650857e2aSArnd Bergmann }
225750857e2aSArnd Bergmann 
225850857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
225950857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
226050857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
226150857e2aSArnd Bergmann {
226250857e2aSArnd Bergmann 	switch (cmd) {
226350857e2aSArnd Bergmann 	case TUNSETIFF:
226450857e2aSArnd Bergmann 	case TUNGETIFF:
226550857e2aSArnd Bergmann 	case TUNSETTXFILTER:
226650857e2aSArnd Bergmann 	case TUNGETSNDBUF:
226750857e2aSArnd Bergmann 	case TUNSETSNDBUF:
226850857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
226950857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
227050857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
227150857e2aSArnd Bergmann 		break;
227250857e2aSArnd Bergmann 	default:
227350857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
227450857e2aSArnd Bergmann 		break;
227550857e2aSArnd Bergmann 	}
227650857e2aSArnd Bergmann 
227750857e2aSArnd Bergmann 	/*
227850857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
227950857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
228050857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
228150857e2aSArnd Bergmann 	 * contents.
228250857e2aSArnd Bergmann 	 */
228350857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
228450857e2aSArnd Bergmann }
228550857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
228650857e2aSArnd Bergmann 
22871da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
22881da177e4SLinus Torvalds {
228954f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
22901da177e4SLinus Torvalds 	int ret;
22911da177e4SLinus Torvalds 
229254f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
22939d319522SJonathan Corbet 		goto out;
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 	if (on) {
2296e0b93eddSJeff Layton 		__f_setown(file, task_pid(current), PIDTYPE_PID, 0);
229754f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
22981da177e4SLinus Torvalds 	} else
229954f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
23009d319522SJonathan Corbet 	ret = 0;
23019d319522SJonathan Corbet out:
23029d319522SJonathan Corbet 	return ret;
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
23061da177e4SLinus Torvalds {
2307140e807dSEric W. Biederman 	struct net *net = current->nsproxy->net_ns;
2308631ab46bSEric W. Biederman 	struct tun_file *tfile;
2309deed49fbSThomas Gleixner 
23106b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2311631ab46bSEric W. Biederman 
2312140e807dSEric W. Biederman 	tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
231311aa9c28SEric W. Biederman 					    &tun_proto, 0);
2314631ab46bSEric W. Biederman 	if (!tfile)
2315631ab46bSEric W. Biederman 		return -ENOMEM;
2316c956674bSMonam Agarwal 	RCU_INIT_POINTER(tfile->tun, NULL);
231754f968d6SJason Wang 	tfile->flags = 0;
2318fb7589a1SPavel Emelyanov 	tfile->ifindex = 0;
231954f968d6SJason Wang 
232054f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
23219e641bdcSXi Wang 	RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
232254f968d6SJason Wang 
232354f968d6SJason Wang 	tfile->socket.file = file;
232454f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
232554f968d6SJason Wang 
232654f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
232754f968d6SJason Wang 
232854f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
232954f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
233054f968d6SJason Wang 
2331631ab46bSEric W. Biederman 	file->private_data = tfile;
23324008e97fSJason Wang 	INIT_LIST_HEAD(&tfile->next);
233354f968d6SJason Wang 
233419a6afb2SJason Wang 	sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
233519a6afb2SJason Wang 
23361da177e4SLinus Torvalds 	return 0;
23371da177e4SLinus Torvalds }
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
23401da177e4SLinus Torvalds {
2341631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
23421da177e4SLinus Torvalds 
2343c8d68e6bSJason Wang 	tun_detach(tfile, true);
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	return 0;
23461da177e4SLinus Torvalds }
23471da177e4SLinus Torvalds 
234893e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
2349a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
235093e14b6dSMasatake YAMATO {
235193e14b6dSMasatake YAMATO 	struct tun_struct *tun;
235293e14b6dSMasatake YAMATO 	struct ifreq ifr;
235393e14b6dSMasatake YAMATO 
235493e14b6dSMasatake YAMATO 	memset(&ifr, 0, sizeof(ifr));
235593e14b6dSMasatake YAMATO 
235693e14b6dSMasatake YAMATO 	rtnl_lock();
235793e14b6dSMasatake YAMATO 	tun = tun_get(f);
235893e14b6dSMasatake YAMATO 	if (tun)
235993e14b6dSMasatake YAMATO 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
236093e14b6dSMasatake YAMATO 	rtnl_unlock();
236193e14b6dSMasatake YAMATO 
236293e14b6dSMasatake YAMATO 	if (tun)
236393e14b6dSMasatake YAMATO 		tun_put(tun);
236493e14b6dSMasatake YAMATO 
2365a3816ab0SJoe Perches 	seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
236693e14b6dSMasatake YAMATO }
236793e14b6dSMasatake YAMATO #endif
236893e14b6dSMasatake YAMATO 
2369d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
23701da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
23711da177e4SLinus Torvalds 	.llseek = no_llseek,
23729b067034SAl Viro 	.read_iter  = tun_chr_read_iter,
2373f5ff53b4SAl Viro 	.write_iter = tun_chr_write_iter,
23741da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
2375876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
237650857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
237750857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
237850857e2aSArnd Bergmann #endif
23791da177e4SLinus Torvalds 	.open	= tun_chr_open,
23801da177e4SLinus Torvalds 	.release = tun_chr_close,
238193e14b6dSMasatake YAMATO 	.fasync = tun_chr_fasync,
238293e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
238393e14b6dSMasatake YAMATO 	.show_fdinfo = tun_chr_show_fdinfo,
238493e14b6dSMasatake YAMATO #endif
23851da177e4SLinus Torvalds };
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
23881da177e4SLinus Torvalds 	.minor = TUN_MINOR,
23891da177e4SLinus Torvalds 	.name = "tun",
2390e454cea2SKay Sievers 	.nodename = "net/tun",
23911da177e4SLinus Torvalds 	.fops = &tun_fops,
23921da177e4SLinus Torvalds };
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds /* ethtool interface */
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
23971da177e4SLinus Torvalds {
23981da177e4SLinus Torvalds 	cmd->supported		= 0;
23991da177e4SLinus Torvalds 	cmd->advertising	= 0;
240070739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
24011da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
24021da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
24031da177e4SLinus Torvalds 	cmd->phy_address	= 0;
24041da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
24051da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
24061da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
24071da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
24081da177e4SLinus Torvalds 	return 0;
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
24121da177e4SLinus Torvalds {
24131da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
24141da177e4SLinus Torvalds 
241533a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
241633a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
241940630b82SMichael S. Tsirkin 	case IFF_TUN:
242033a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
24211da177e4SLinus Torvalds 		break;
242240630b82SMichael S. Tsirkin 	case IFF_TAP:
242333a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
24241da177e4SLinus Torvalds 		break;
24251da177e4SLinus Torvalds 	}
24261da177e4SLinus Torvalds }
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
24291da177e4SLinus Torvalds {
24301da177e4SLinus Torvalds #ifdef TUN_DEBUG
24311da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
24321da177e4SLinus Torvalds 	return tun->debug;
24331da177e4SLinus Torvalds #else
24341da177e4SLinus Torvalds 	return -EOPNOTSUPP;
24351da177e4SLinus Torvalds #endif
24361da177e4SLinus Torvalds }
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
24391da177e4SLinus Torvalds {
24401da177e4SLinus Torvalds #ifdef TUN_DEBUG
24411da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
24421da177e4SLinus Torvalds 	tun->debug = value;
24431da177e4SLinus Torvalds #endif
24441da177e4SLinus Torvalds }
24451da177e4SLinus Torvalds 
24467282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
24471da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
24481da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
24491da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
24501da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
2451bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
2452eda29772SRichard Cochran 	.get_ts_info	= ethtool_op_get_ts_info,
24531da177e4SLinus Torvalds };
24541da177e4SLinus Torvalds 
245579d17604SPavel Emelyanov 
24561da177e4SLinus Torvalds static int __init tun_init(void)
24571da177e4SLinus Torvalds {
24581da177e4SLinus Torvalds 	int ret = 0;
24591da177e4SLinus Torvalds 
24606b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
24616b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
24621da177e4SLinus Torvalds 
2463f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
246479d17604SPavel Emelyanov 	if (ret) {
24656b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
2466f019a7a5SEric W. Biederman 		goto err_linkops;
246779d17604SPavel Emelyanov 	}
246879d17604SPavel Emelyanov 
24691da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
247079d17604SPavel Emelyanov 	if (ret) {
24716b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
247279d17604SPavel Emelyanov 		goto err_misc;
247379d17604SPavel Emelyanov 	}
247479d17604SPavel Emelyanov 	return  0;
247579d17604SPavel Emelyanov err_misc:
2476f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2477f019a7a5SEric W. Biederman err_linkops:
24781da177e4SLinus Torvalds 	return ret;
24791da177e4SLinus Torvalds }
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds static void tun_cleanup(void)
24821da177e4SLinus Torvalds {
24831da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2484f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
24851da177e4SLinus Torvalds }
24861da177e4SLinus Torvalds 
248705c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
248805c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
248905c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
249005c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
249105c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
249205c2828cSMichael S. Tsirkin {
24936e914fc7SJason Wang 	struct tun_file *tfile;
249405c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
249505c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
24966e914fc7SJason Wang 	tfile = file->private_data;
24976e914fc7SJason Wang 	if (!tfile)
249805c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
249954f968d6SJason Wang 	return &tfile->socket;
250005c2828cSMichael S. Tsirkin }
250105c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
250205c2828cSMichael S. Tsirkin 
25031da177e4SLinus Torvalds module_init(tun_init);
25041da177e4SLinus Torvalds module_exit(tun_cleanup);
25051da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
25061da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
25071da177e4SLinus Torvalds MODULE_LICENSE("GPL");
25081da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2509578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2510