xref: /openbmc/linux/drivers/net/tun.c (revision 9cd3e072b0be17446e37d7414eac8a3499e0601e)
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 
13454f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
13592d4ea6eSstephen hemminger  * also contains all socket related structures (except sock_fprog and tap_filter)
13654f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
13754f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
13836fe8c09SRami Rosen  * netdevice not for a specific queue (at least I didn't see the requirement for
13954f968d6SJason Wang  * this).
1406e914fc7SJason Wang  *
1416e914fc7SJason Wang  * RCU usage:
14236fe8c09SRami Rosen  * The tun_file and tun_struct are loosely coupled, the pointer from one to the
1436e914fc7SJason Wang  * other can only be read while rcu_read_lock or rtnl_lock is held.
14454f968d6SJason Wang  */
145631ab46bSEric W. Biederman struct tun_file {
14654f968d6SJason Wang 	struct sock sk;
14754f968d6SJason Wang 	struct socket socket;
14854f968d6SJason Wang 	struct socket_wq wq;
1496e914fc7SJason Wang 	struct tun_struct __rcu *tun;
15054f968d6SJason Wang 	struct fasync_struct *fasync;
15154f968d6SJason Wang 	/* only used for fasnyc */
15254f968d6SJason Wang 	unsigned int flags;
153fb7589a1SPavel Emelyanov 	union {
154c8d68e6bSJason Wang 		u16 queue_index;
155fb7589a1SPavel Emelyanov 		unsigned int ifindex;
156fb7589a1SPavel Emelyanov 	};
1574008e97fSJason Wang 	struct list_head next;
1584008e97fSJason Wang 	struct tun_struct *detached;
159631ab46bSEric W. Biederman };
160631ab46bSEric W. Biederman 
16196442e42SJason Wang struct tun_flow_entry {
16296442e42SJason Wang 	struct hlist_node hash_link;
16396442e42SJason Wang 	struct rcu_head rcu;
16496442e42SJason Wang 	struct tun_struct *tun;
16596442e42SJason Wang 
16696442e42SJason Wang 	u32 rxhash;
1679bc88939STom Herbert 	u32 rps_rxhash;
16896442e42SJason Wang 	int queue_index;
16996442e42SJason Wang 	unsigned long updated;
17096442e42SJason Wang };
17196442e42SJason Wang 
17296442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024
17396442e42SJason Wang 
17454f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
17536fe8c09SRami Rosen  * device, socket filter, sndbuf and vnet header size were restore when the
17654f968d6SJason Wang  * file were attached to a persist device.
17754f968d6SJason Wang  */
17814daa021SRusty Russell struct tun_struct {
179c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
180c8d68e6bSJason Wang 	unsigned int            numqueues;
181f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1820625c883SEric W. Biederman 	kuid_t			owner;
1830625c883SEric W. Biederman 	kgid_t			group;
18414daa021SRusty Russell 
18514daa021SRusty Russell 	struct net_device	*dev;
186c8f44affSMichał Mirosław 	netdev_features_t	set_features;
18788255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
188e3e3c423SVlad Yasevich 			  NETIF_F_TSO6|NETIF_F_UFO)
189d9d52b51SMichael S. Tsirkin 
190d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
19154f968d6SJason Wang 	int			sndbuf;
19254f968d6SJason Wang 	struct tap_filter	txflt;
19354f968d6SJason Wang 	struct sock_fprog	fprog;
19454f968d6SJason Wang 	/* protected by rtnl lock */
19554f968d6SJason Wang 	bool			filter_attached;
19614daa021SRusty Russell #ifdef TUN_DEBUG
19714daa021SRusty Russell 	int debug;
19814daa021SRusty Russell #endif
19996442e42SJason Wang 	spinlock_t lock;
20096442e42SJason Wang 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
20196442e42SJason Wang 	struct timer_list flow_gc_timer;
20296442e42SJason Wang 	unsigned long ageing_time;
2034008e97fSJason Wang 	unsigned int numdisabled;
2044008e97fSJason Wang 	struct list_head disabled;
2055dbbaf2dSPaul Moore 	void *security;
206b8732fb7SJason Wang 	u32 flow_count;
20714daa021SRusty Russell };
20814daa021SRusty Russell 
2098b8e658bSGreg Kurz #ifdef CONFIG_TUN_VNET_CROSS_LE
2108b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
2118b8e658bSGreg Kurz {
2128b8e658bSGreg Kurz 	return tun->flags & TUN_VNET_BE ? false :
2138b8e658bSGreg Kurz 		virtio_legacy_is_little_endian();
2148b8e658bSGreg Kurz }
2158b8e658bSGreg Kurz 
2168b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
2178b8e658bSGreg Kurz {
2188b8e658bSGreg Kurz 	int be = !!(tun->flags & TUN_VNET_BE);
2198b8e658bSGreg Kurz 
2208b8e658bSGreg Kurz 	if (put_user(be, argp))
2218b8e658bSGreg Kurz 		return -EFAULT;
2228b8e658bSGreg Kurz 
2238b8e658bSGreg Kurz 	return 0;
2248b8e658bSGreg Kurz }
2258b8e658bSGreg Kurz 
2268b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
2278b8e658bSGreg Kurz {
2288b8e658bSGreg Kurz 	int be;
2298b8e658bSGreg Kurz 
2308b8e658bSGreg Kurz 	if (get_user(be, argp))
2318b8e658bSGreg Kurz 		return -EFAULT;
2328b8e658bSGreg Kurz 
2338b8e658bSGreg Kurz 	if (be)
2348b8e658bSGreg Kurz 		tun->flags |= TUN_VNET_BE;
2358b8e658bSGreg Kurz 	else
2368b8e658bSGreg Kurz 		tun->flags &= ~TUN_VNET_BE;
2378b8e658bSGreg Kurz 
2388b8e658bSGreg Kurz 	return 0;
2398b8e658bSGreg Kurz }
2408b8e658bSGreg Kurz #else
2418b8e658bSGreg Kurz static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
2428b8e658bSGreg Kurz {
2438b8e658bSGreg Kurz 	return virtio_legacy_is_little_endian();
2448b8e658bSGreg Kurz }
2458b8e658bSGreg Kurz 
2468b8e658bSGreg Kurz static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
2478b8e658bSGreg Kurz {
2488b8e658bSGreg Kurz 	return -EINVAL;
2498b8e658bSGreg Kurz }
2508b8e658bSGreg Kurz 
2518b8e658bSGreg Kurz static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
2528b8e658bSGreg Kurz {
2538b8e658bSGreg Kurz 	return -EINVAL;
2548b8e658bSGreg Kurz }
2558b8e658bSGreg Kurz #endif /* CONFIG_TUN_VNET_CROSS_LE */
2568b8e658bSGreg Kurz 
25725bd55bbSGreg Kurz static inline bool tun_is_little_endian(struct tun_struct *tun)
25825bd55bbSGreg Kurz {
2597d824109SGreg Kurz 	return tun->flags & TUN_VNET_LE ||
2608b8e658bSGreg Kurz 		tun_legacy_is_little_endian(tun);
26125bd55bbSGreg Kurz }
26225bd55bbSGreg Kurz 
26356f0dcc5SMichael S. Tsirkin static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
26456f0dcc5SMichael S. Tsirkin {
26525bd55bbSGreg Kurz 	return __virtio16_to_cpu(tun_is_little_endian(tun), val);
26656f0dcc5SMichael S. Tsirkin }
26756f0dcc5SMichael S. Tsirkin 
26856f0dcc5SMichael S. Tsirkin static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
26956f0dcc5SMichael S. Tsirkin {
27025bd55bbSGreg Kurz 	return __cpu_to_virtio16(tun_is_little_endian(tun), val);
27156f0dcc5SMichael S. Tsirkin }
27256f0dcc5SMichael S. Tsirkin 
27396442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash)
27496442e42SJason Wang {
27596442e42SJason Wang 	return rxhash & 0x3ff;
27696442e42SJason Wang }
27796442e42SJason Wang 
27896442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
27996442e42SJason Wang {
28096442e42SJason Wang 	struct tun_flow_entry *e;
28196442e42SJason Wang 
282b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(e, head, hash_link) {
28396442e42SJason Wang 		if (e->rxhash == rxhash)
28496442e42SJason Wang 			return e;
28596442e42SJason Wang 	}
28696442e42SJason Wang 	return NULL;
28796442e42SJason Wang }
28896442e42SJason Wang 
28996442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
29096442e42SJason Wang 					      struct hlist_head *head,
29196442e42SJason Wang 					      u32 rxhash, u16 queue_index)
29296442e42SJason Wang {
2939fdc6befSEric Dumazet 	struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
2949fdc6befSEric Dumazet 
29596442e42SJason Wang 	if (e) {
29696442e42SJason Wang 		tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
29796442e42SJason Wang 			  rxhash, queue_index);
29896442e42SJason Wang 		e->updated = jiffies;
29996442e42SJason Wang 		e->rxhash = rxhash;
3009bc88939STom Herbert 		e->rps_rxhash = 0;
30196442e42SJason Wang 		e->queue_index = queue_index;
30296442e42SJason Wang 		e->tun = tun;
30396442e42SJason Wang 		hlist_add_head_rcu(&e->hash_link, head);
304b8732fb7SJason Wang 		++tun->flow_count;
30596442e42SJason Wang 	}
30696442e42SJason Wang 	return e;
30796442e42SJason Wang }
30896442e42SJason Wang 
30996442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
31096442e42SJason Wang {
31196442e42SJason Wang 	tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
31296442e42SJason Wang 		  e->rxhash, e->queue_index);
31396442e42SJason Wang 	hlist_del_rcu(&e->hash_link);
3149fdc6befSEric Dumazet 	kfree_rcu(e, rcu);
315b8732fb7SJason Wang 	--tun->flow_count;
31696442e42SJason Wang }
31796442e42SJason Wang 
31896442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun)
31996442e42SJason Wang {
32096442e42SJason Wang 	int i;
32196442e42SJason Wang 
32296442e42SJason Wang 	spin_lock_bh(&tun->lock);
32396442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
32496442e42SJason Wang 		struct tun_flow_entry *e;
325b67bfe0dSSasha Levin 		struct hlist_node *n;
32696442e42SJason Wang 
327b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
32896442e42SJason Wang 			tun_flow_delete(tun, e);
32996442e42SJason Wang 	}
33096442e42SJason Wang 	spin_unlock_bh(&tun->lock);
33196442e42SJason Wang }
33296442e42SJason Wang 
33396442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
33496442e42SJason Wang {
33596442e42SJason Wang 	int i;
33696442e42SJason Wang 
33796442e42SJason Wang 	spin_lock_bh(&tun->lock);
33896442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
33996442e42SJason Wang 		struct tun_flow_entry *e;
340b67bfe0dSSasha Levin 		struct hlist_node *n;
34196442e42SJason Wang 
342b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
34396442e42SJason Wang 			if (e->queue_index == queue_index)
34496442e42SJason Wang 				tun_flow_delete(tun, e);
34596442e42SJason Wang 		}
34696442e42SJason Wang 	}
34796442e42SJason Wang 	spin_unlock_bh(&tun->lock);
34896442e42SJason Wang }
34996442e42SJason Wang 
35096442e42SJason Wang static void tun_flow_cleanup(unsigned long data)
35196442e42SJason Wang {
35296442e42SJason Wang 	struct tun_struct *tun = (struct tun_struct *)data;
35396442e42SJason Wang 	unsigned long delay = tun->ageing_time;
35496442e42SJason Wang 	unsigned long next_timer = jiffies + delay;
35596442e42SJason Wang 	unsigned long count = 0;
35696442e42SJason Wang 	int i;
35796442e42SJason Wang 
35896442e42SJason Wang 	tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
35996442e42SJason Wang 
36096442e42SJason Wang 	spin_lock_bh(&tun->lock);
36196442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
36296442e42SJason Wang 		struct tun_flow_entry *e;
363b67bfe0dSSasha Levin 		struct hlist_node *n;
36496442e42SJason Wang 
365b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
36696442e42SJason Wang 			unsigned long this_timer;
36796442e42SJason Wang 			count++;
36896442e42SJason Wang 			this_timer = e->updated + delay;
36996442e42SJason Wang 			if (time_before_eq(this_timer, jiffies))
37096442e42SJason Wang 				tun_flow_delete(tun, e);
37196442e42SJason Wang 			else if (time_before(this_timer, next_timer))
37296442e42SJason Wang 				next_timer = this_timer;
37396442e42SJason Wang 		}
37496442e42SJason Wang 	}
37596442e42SJason Wang 
37696442e42SJason Wang 	if (count)
37796442e42SJason Wang 		mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
37896442e42SJason Wang 	spin_unlock_bh(&tun->lock);
37996442e42SJason Wang }
38096442e42SJason Wang 
38149974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
3829e85722dSJason Wang 			    struct tun_file *tfile)
38396442e42SJason Wang {
38496442e42SJason Wang 	struct hlist_head *head;
38596442e42SJason Wang 	struct tun_flow_entry *e;
38696442e42SJason Wang 	unsigned long delay = tun->ageing_time;
3879e85722dSJason Wang 	u16 queue_index = tfile->queue_index;
38896442e42SJason Wang 
38996442e42SJason Wang 	if (!rxhash)
39096442e42SJason Wang 		return;
39196442e42SJason Wang 	else
39296442e42SJason Wang 		head = &tun->flows[tun_hashfn(rxhash)];
39396442e42SJason Wang 
39496442e42SJason Wang 	rcu_read_lock();
39596442e42SJason Wang 
3969e85722dSJason Wang 	/* We may get a very small possibility of OOO during switching, not
3979e85722dSJason Wang 	 * worth to optimize.*/
3989e85722dSJason Wang 	if (tun->numqueues == 1 || tfile->detached)
39996442e42SJason Wang 		goto unlock;
40096442e42SJason Wang 
40196442e42SJason Wang 	e = tun_flow_find(head, rxhash);
40296442e42SJason Wang 	if (likely(e)) {
40396442e42SJason Wang 		/* TODO: keep queueing to old queue until it's empty? */
40496442e42SJason Wang 		e->queue_index = queue_index;
40596442e42SJason Wang 		e->updated = jiffies;
4069bc88939STom Herbert 		sock_rps_record_flow_hash(e->rps_rxhash);
40796442e42SJason Wang 	} else {
40896442e42SJason Wang 		spin_lock_bh(&tun->lock);
409b8732fb7SJason Wang 		if (!tun_flow_find(head, rxhash) &&
410b8732fb7SJason Wang 		    tun->flow_count < MAX_TAP_FLOWS)
41196442e42SJason Wang 			tun_flow_create(tun, head, rxhash, queue_index);
41296442e42SJason Wang 
41396442e42SJason Wang 		if (!timer_pending(&tun->flow_gc_timer))
41496442e42SJason Wang 			mod_timer(&tun->flow_gc_timer,
41596442e42SJason Wang 				  round_jiffies_up(jiffies + delay));
41696442e42SJason Wang 		spin_unlock_bh(&tun->lock);
41796442e42SJason Wang 	}
41896442e42SJason Wang 
41996442e42SJason Wang unlock:
42096442e42SJason Wang 	rcu_read_unlock();
42196442e42SJason Wang }
42296442e42SJason Wang 
4239bc88939STom Herbert /**
4249bc88939STom Herbert  * Save the hash received in the stack receive path and update the
4259bc88939STom Herbert  * flow_hash table accordingly.
4269bc88939STom Herbert  */
4279bc88939STom Herbert static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
4289bc88939STom Herbert {
429567e4b79SEric Dumazet 	if (unlikely(e->rps_rxhash != hash))
4309bc88939STom Herbert 		e->rps_rxhash = hash;
4319bc88939STom Herbert }
4329bc88939STom Herbert 
433c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
43492d4ea6eSstephen hemminger  * we do not check rxq no. is because some cards(e.g 82599), chooses
435c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
436c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
437c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
438c8d68e6bSJason Wang  * hope the rxq no. may help here.
439c8d68e6bSJason Wang  */
440f663dd9aSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
44199932d4fSDaniel Borkmann 			    void *accel_priv, select_queue_fallback_t fallback)
442c8d68e6bSJason Wang {
443c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
44496442e42SJason Wang 	struct tun_flow_entry *e;
445c8d68e6bSJason Wang 	u32 txq = 0;
446c8d68e6bSJason Wang 	u32 numqueues = 0;
447c8d68e6bSJason Wang 
448c8d68e6bSJason Wang 	rcu_read_lock();
44992bb73eaSJason Wang 	numqueues = ACCESS_ONCE(tun->numqueues);
450c8d68e6bSJason Wang 
4513958afa1STom Herbert 	txq = skb_get_hash(skb);
452c8d68e6bSJason Wang 	if (txq) {
45396442e42SJason Wang 		e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
4549bc88939STom Herbert 		if (e) {
4559bc88939STom Herbert 			tun_flow_save_rps_rxhash(e, txq);
456fbe4d456SZhi Yong Wu 			txq = e->queue_index;
4579bc88939STom Herbert 		} else
458c8d68e6bSJason Wang 			/* use multiply and shift instead of expensive divide */
459c8d68e6bSJason Wang 			txq = ((u64)txq * numqueues) >> 32;
460c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
461c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
462c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
463c8d68e6bSJason Wang 			txq -= numqueues;
464c8d68e6bSJason Wang 	}
465c8d68e6bSJason Wang 
466c8d68e6bSJason Wang 	rcu_read_unlock();
467c8d68e6bSJason Wang 	return txq;
468c8d68e6bSJason Wang }
469c8d68e6bSJason Wang 
470cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
471cde8b15fSJason Wang {
472cde8b15fSJason Wang 	const struct cred *cred = current_cred();
473c260b772SEric W. Biederman 	struct net *net = dev_net(tun->dev);
474cde8b15fSJason Wang 
475cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
476cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
477c260b772SEric W. Biederman 		!ns_capable(net->user_ns, CAP_NET_ADMIN);
478cde8b15fSJason Wang }
479cde8b15fSJason Wang 
480c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
481c8d68e6bSJason Wang {
482c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
483c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
484c8d68e6bSJason Wang }
485c8d68e6bSJason Wang 
4864008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
4874008e97fSJason Wang {
4884008e97fSJason Wang 	tfile->detached = tun;
4894008e97fSJason Wang 	list_add_tail(&tfile->next, &tun->disabled);
4904008e97fSJason Wang 	++tun->numdisabled;
4914008e97fSJason Wang }
4924008e97fSJason Wang 
493d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
4944008e97fSJason Wang {
4954008e97fSJason Wang 	struct tun_struct *tun = tfile->detached;
4964008e97fSJason Wang 
4974008e97fSJason Wang 	tfile->detached = NULL;
4984008e97fSJason Wang 	list_del_init(&tfile->next);
4994008e97fSJason Wang 	--tun->numdisabled;
5004008e97fSJason Wang 	return tun;
5014008e97fSJason Wang }
5024008e97fSJason Wang 
5034bfb0513SJason Wang static void tun_queue_purge(struct tun_file *tfile)
5044bfb0513SJason Wang {
5054bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_receive_queue);
5064bfb0513SJason Wang 	skb_queue_purge(&tfile->sk.sk_error_queue);
5074bfb0513SJason Wang }
5084bfb0513SJason Wang 
509c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
510c8d68e6bSJason Wang {
511c8d68e6bSJason Wang 	struct tun_file *ntfile;
512c8d68e6bSJason Wang 	struct tun_struct *tun;
513c8d68e6bSJason Wang 
514b8deabd3SJason Wang 	tun = rtnl_dereference(tfile->tun);
515b8deabd3SJason Wang 
5169e85722dSJason Wang 	if (tun && !tfile->detached) {
517c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
518c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
519c8d68e6bSJason Wang 
520c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
521c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
522b8deabd3SJason Wang 		ntfile = rtnl_dereference(tun->tfiles[index]);
523c8d68e6bSJason Wang 		ntfile->queue_index = index;
524c8d68e6bSJason Wang 
525c8d68e6bSJason Wang 		--tun->numqueues;
5269e85722dSJason Wang 		if (clean) {
527c956674bSMonam Agarwal 			RCU_INIT_POINTER(tfile->tun, NULL);
528c8d68e6bSJason Wang 			sock_put(&tfile->sk);
5299e85722dSJason Wang 		} else
5304008e97fSJason Wang 			tun_disable_queue(tun, tfile);
531c8d68e6bSJason Wang 
532c8d68e6bSJason Wang 		synchronize_net();
53396442e42SJason Wang 		tun_flow_delete_by_queue(tun, tun->numqueues + 1);
534c8d68e6bSJason Wang 		/* Drop read queue */
5354bfb0513SJason Wang 		tun_queue_purge(tfile);
536c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
537dd38bd85SJason Wang 	} else if (tfile->detached && clean) {
5384008e97fSJason Wang 		tun = tun_enable_queue(tfile);
539dd38bd85SJason Wang 		sock_put(&tfile->sk);
540dd38bd85SJason Wang 	}
541c8d68e6bSJason Wang 
542c8d68e6bSJason Wang 	if (clean) {
543af668b3cSMichael S. Tsirkin 		if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
544af668b3cSMichael S. Tsirkin 			netif_carrier_off(tun->dev);
545af668b3cSMichael S. Tsirkin 
54640630b82SMichael S. Tsirkin 			if (!(tun->flags & IFF_PERSIST) &&
547af668b3cSMichael S. Tsirkin 			    tun->dev->reg_state == NETREG_REGISTERED)
5484008e97fSJason Wang 				unregister_netdevice(tun->dev);
549af668b3cSMichael S. Tsirkin 		}
550140e807dSEric W. Biederman 		sock_put(&tfile->sk);
551c8d68e6bSJason Wang 	}
552c8d68e6bSJason Wang }
553c8d68e6bSJason Wang 
554c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
555c8d68e6bSJason Wang {
556c8d68e6bSJason Wang 	rtnl_lock();
557c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
558c8d68e6bSJason Wang 	rtnl_unlock();
559c8d68e6bSJason Wang }
560c8d68e6bSJason Wang 
561c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
562c8d68e6bSJason Wang {
563c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
5644008e97fSJason Wang 	struct tun_file *tfile, *tmp;
565c8d68e6bSJason Wang 	int i, n = tun->numqueues;
566c8d68e6bSJason Wang 
567c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
568b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
569c8d68e6bSJason Wang 		BUG_ON(!tfile);
5709e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
571c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
572c8d68e6bSJason Wang 		--tun->numqueues;
573c8d68e6bSJason Wang 	}
5749e85722dSJason Wang 	list_for_each_entry(tfile, &tun->disabled, next) {
5759e641bdcSXi Wang 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
576c956674bSMonam Agarwal 		RCU_INIT_POINTER(tfile->tun, NULL);
5779e85722dSJason Wang 	}
578c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
579c8d68e6bSJason Wang 
580c8d68e6bSJason Wang 	synchronize_net();
581c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
582b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
583c8d68e6bSJason Wang 		/* Drop read queue */
5844bfb0513SJason Wang 		tun_queue_purge(tfile);
585c8d68e6bSJason Wang 		sock_put(&tfile->sk);
586c8d68e6bSJason Wang 	}
5874008e97fSJason Wang 	list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
5884008e97fSJason Wang 		tun_enable_queue(tfile);
5894bfb0513SJason Wang 		tun_queue_purge(tfile);
5904008e97fSJason Wang 		sock_put(&tfile->sk);
5914008e97fSJason Wang 	}
5924008e97fSJason Wang 	BUG_ON(tun->numdisabled != 0);
593dd38bd85SJason Wang 
59440630b82SMichael S. Tsirkin 	if (tun->flags & IFF_PERSIST)
595dd38bd85SJason Wang 		module_put(THIS_MODULE);
596c8d68e6bSJason Wang }
597c8d68e6bSJason Wang 
598849c9b6fSPavel Emelyanov static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
599a7385ba2SEric W. Biederman {
600631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
60138231b7aSEric W. Biederman 	int err;
602a7385ba2SEric W. Biederman 
6035dbbaf2dSPaul Moore 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
6045dbbaf2dSPaul Moore 	if (err < 0)
6055dbbaf2dSPaul Moore 		goto out;
6065dbbaf2dSPaul Moore 
60738231b7aSEric W. Biederman 	err = -EINVAL;
6089e85722dSJason Wang 	if (rtnl_dereference(tfile->tun) && !tfile->detached)
60938231b7aSEric W. Biederman 		goto out;
61038231b7aSEric W. Biederman 
61138231b7aSEric W. Biederman 	err = -EBUSY;
61240630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
613c8d68e6bSJason Wang 		goto out;
614c8d68e6bSJason Wang 
615c8d68e6bSJason Wang 	err = -E2BIG;
6164008e97fSJason Wang 	if (!tfile->detached &&
6174008e97fSJason Wang 	    tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
61838231b7aSEric W. Biederman 		goto out;
61938231b7aSEric W. Biederman 
62038231b7aSEric W. Biederman 	err = 0;
62154f968d6SJason Wang 
62292d4ea6eSstephen hemminger 	/* Re-attach the filter to persist device */
623849c9b6fSPavel Emelyanov 	if (!skip_filter && (tun->filter_attached == true)) {
62454f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
62554f968d6SJason Wang 		if (!err)
62654f968d6SJason Wang 			goto out;
62754f968d6SJason Wang 	}
628c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
6296e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
630c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
631c8d68e6bSJason Wang 	tun->numqueues++;
632c8d68e6bSJason Wang 
6334008e97fSJason Wang 	if (tfile->detached)
6344008e97fSJason Wang 		tun_enable_queue(tfile);
6354008e97fSJason Wang 	else
6364008e97fSJason Wang 		sock_hold(&tfile->sk);
6374008e97fSJason Wang 
638c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
639c8d68e6bSJason Wang 
640c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
641c8d68e6bSJason Wang 	 * refcnt.
642c8d68e6bSJason Wang 	 */
643a7385ba2SEric W. Biederman 
64438231b7aSEric W. Biederman out:
64538231b7aSEric W. Biederman 	return err;
646a7385ba2SEric W. Biederman }
647a7385ba2SEric W. Biederman 
648631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
649631ab46bSEric W. Biederman {
6506e914fc7SJason Wang 	struct tun_struct *tun;
651c70f1829SEric W. Biederman 
6526e914fc7SJason Wang 	rcu_read_lock();
6536e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
6546e914fc7SJason Wang 	if (tun)
6556e914fc7SJason Wang 		dev_hold(tun->dev);
6566e914fc7SJason Wang 	rcu_read_unlock();
657c70f1829SEric W. Biederman 
658c70f1829SEric W. Biederman 	return tun;
659631ab46bSEric W. Biederman }
660631ab46bSEric W. Biederman 
661631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
662631ab46bSEric W. Biederman {
663631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
664631ab46bSEric W. Biederman }
665631ab46bSEric W. Biederman 
666631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
667631ab46bSEric W. Biederman {
6686e914fc7SJason Wang 	dev_put(tun->dev);
669631ab46bSEric W. Biederman }
670631ab46bSEric W. Biederman 
6716b8a66eeSJoe Perches /* TAP filtering */
672f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
673f271b2ccSMax Krasnyansky {
674f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
675f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
676f271b2ccSMax Krasnyansky }
677f271b2ccSMax Krasnyansky 
678f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
679f271b2ccSMax Krasnyansky {
680f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
681f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
682f271b2ccSMax Krasnyansky }
683f271b2ccSMax Krasnyansky 
684f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
685f271b2ccSMax Krasnyansky {
686f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
687f271b2ccSMax Krasnyansky 	struct tun_filter uf;
688f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
689f271b2ccSMax Krasnyansky 
690f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
691f271b2ccSMax Krasnyansky 		return -EFAULT;
692f271b2ccSMax Krasnyansky 
693f271b2ccSMax Krasnyansky 	if (!uf.count) {
694f271b2ccSMax Krasnyansky 		/* Disabled */
695f271b2ccSMax Krasnyansky 		filter->count = 0;
696f271b2ccSMax Krasnyansky 		return 0;
697f271b2ccSMax Krasnyansky 	}
698f271b2ccSMax Krasnyansky 
699f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
700f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
701f271b2ccSMax Krasnyansky 	if (!addr)
702f271b2ccSMax Krasnyansky 		return -ENOMEM;
703f271b2ccSMax Krasnyansky 
704f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
705f271b2ccSMax Krasnyansky 		err = -EFAULT;
706f271b2ccSMax Krasnyansky 		goto done;
707f271b2ccSMax Krasnyansky 	}
708f271b2ccSMax Krasnyansky 
709f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
710f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
711f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
712f271b2ccSMax Krasnyansky 	filter->count = 0;
713f271b2ccSMax Krasnyansky 	wmb();
714f271b2ccSMax Krasnyansky 
715f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
716f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
717f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
718f271b2ccSMax Krasnyansky 
719f271b2ccSMax Krasnyansky 	nexact = n;
720f271b2ccSMax Krasnyansky 
721cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
722cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
723f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
724cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
725cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
726cfbf84fcSAlex Williamson 			err = 0; /* no filter */
727cfbf84fcSAlex Williamson 			goto done;
728cfbf84fcSAlex Williamson 		}
729f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
730cfbf84fcSAlex Williamson 	}
731f271b2ccSMax Krasnyansky 
732f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
733f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
734f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
735f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
736f271b2ccSMax Krasnyansky 
737f271b2ccSMax Krasnyansky 	/* Now enable the filter */
738f271b2ccSMax Krasnyansky 	wmb();
739f271b2ccSMax Krasnyansky 	filter->count = nexact;
740f271b2ccSMax Krasnyansky 
741f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
742f271b2ccSMax Krasnyansky 	err = nexact;
743f271b2ccSMax Krasnyansky 
744f271b2ccSMax Krasnyansky done:
745f271b2ccSMax Krasnyansky 	kfree(addr);
746f271b2ccSMax Krasnyansky 	return err;
747f271b2ccSMax Krasnyansky }
748f271b2ccSMax Krasnyansky 
749f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
750f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
751f271b2ccSMax Krasnyansky {
752f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
753f271b2ccSMax Krasnyansky 	 * at this point. */
754f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
755f271b2ccSMax Krasnyansky 	int i;
756f271b2ccSMax Krasnyansky 
757f271b2ccSMax Krasnyansky 	/* Exact match */
758f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
7592e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
760f271b2ccSMax Krasnyansky 			return 1;
761f271b2ccSMax Krasnyansky 
762f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
763f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
764f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
765f271b2ccSMax Krasnyansky 
766f271b2ccSMax Krasnyansky 	return 0;
767f271b2ccSMax Krasnyansky }
768f271b2ccSMax Krasnyansky 
769f271b2ccSMax Krasnyansky /*
770f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
771f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
772f271b2ccSMax Krasnyansky  */
773f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
774f271b2ccSMax Krasnyansky {
775f271b2ccSMax Krasnyansky 	if (!filter->count)
776f271b2ccSMax Krasnyansky 		return 1;
777f271b2ccSMax Krasnyansky 
778f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
779f271b2ccSMax Krasnyansky }
780f271b2ccSMax Krasnyansky 
7811da177e4SLinus Torvalds /* Network device part of the driver */
7821da177e4SLinus Torvalds 
7837282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
7841da177e4SLinus Torvalds 
785c70f1829SEric W. Biederman /* Net device detach from fd. */
786c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
787c70f1829SEric W. Biederman {
788c8d68e6bSJason Wang 	tun_detach_all(dev);
789c70f1829SEric W. Biederman }
790c70f1829SEric W. Biederman 
7911da177e4SLinus Torvalds /* Net device open. */
7921da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
7931da177e4SLinus Torvalds {
794c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
7951da177e4SLinus Torvalds 	return 0;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds /* Net device close. */
7991da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
8001da177e4SLinus Torvalds {
801c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
8021da177e4SLinus Torvalds 	return 0;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds /* Net device start xmit */
806424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
8071da177e4SLinus Torvalds {
8081da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
809c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
8106e914fc7SJason Wang 	struct tun_file *tfile;
811fa35864eSDominic Curran 	u32 numqueues = 0;
8121da177e4SLinus Torvalds 
8136e914fc7SJason Wang 	rcu_read_lock();
814c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
815fa35864eSDominic Curran 	numqueues = ACCESS_ONCE(tun->numqueues);
816c8d68e6bSJason Wang 
8171da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
818fa35864eSDominic Curran 	if (txq >= numqueues)
8191da177e4SLinus Torvalds 		goto drop;
8201da177e4SLinus Torvalds 
821fa35864eSDominic Curran 	if (numqueues == 1) {
8229bc88939STom Herbert 		/* Select queue was not called for the skbuff, so we extract the
8239bc88939STom Herbert 		 * RPS hash and save it into the flow_table here.
8249bc88939STom Herbert 		 */
8259bc88939STom Herbert 		__u32 rxhash;
8269bc88939STom Herbert 
8279bc88939STom Herbert 		rxhash = skb_get_hash(skb);
8289bc88939STom Herbert 		if (rxhash) {
8299bc88939STom Herbert 			struct tun_flow_entry *e;
8309bc88939STom Herbert 			e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
8319bc88939STom Herbert 					rxhash);
8329bc88939STom Herbert 			if (e)
8339bc88939STom Herbert 				tun_flow_save_rps_rxhash(e, rxhash);
8349bc88939STom Herbert 		}
8359bc88939STom Herbert 	}
8369bc88939STom Herbert 
8376e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
8386e914fc7SJason Wang 
839c8d68e6bSJason Wang 	BUG_ON(!tfile);
840c8d68e6bSJason Wang 
841f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
842f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
843f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
844f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
845f271b2ccSMax Krasnyansky 		goto drop;
846f271b2ccSMax Krasnyansky 
84754f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
84854f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
84999405162SMichael S. Tsirkin 		goto drop;
85099405162SMichael S. Tsirkin 
85136fe8c09SRami Rosen 	/* Limit the number of packets queued by dividing txq length with the
852c8d68e6bSJason Wang 	 * number of queues.
853c8d68e6bSJason Wang 	 */
854fa35864eSDominic Curran 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
855fa35864eSDominic Curran 			  >= dev->tx_queue_len)
8561da177e4SLinus Torvalds 		goto drop;
8571da177e4SLinus Torvalds 
8587bf66305SJason Wang 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
8597bf66305SJason Wang 		goto drop;
8607bf66305SJason Wang 
8615fcd2d8bSEric Dumazet 	if (skb->sk && sk_fullsock(skb->sk)) {
862eda29772SRichard Cochran 		sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags);
863eda29772SRichard Cochran 		sw_tx_timestamp(skb);
864eda29772SRichard Cochran 	}
865eda29772SRichard Cochran 
8660110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
8677bf66305SJason Wang 	 * for indefinite time.
8687bf66305SJason Wang 	 */
8690110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
8700110d6f2SMichael S. Tsirkin 
871f8af75f3SEric Dumazet 	nf_reset(skb);
872f8af75f3SEric Dumazet 
873f271b2ccSMax Krasnyansky 	/* Enqueue packet */
87454f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	/* Notify and wake up reader process */
87754f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
87854f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
8799e641bdcSXi Wang 	tfile->socket.sk->sk_data_ready(tfile->socket.sk);
8806e914fc7SJason Wang 
8816e914fc7SJason Wang 	rcu_read_unlock();
8826ed10654SPatrick McHardy 	return NETDEV_TX_OK;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds drop:
88509f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
886149d36f7SMichael S. Tsirkin 	skb_tx_error(skb);
8871da177e4SLinus Torvalds 	kfree_skb(skb);
8886e914fc7SJason Wang 	rcu_read_unlock();
889baeababbSJason Wang 	return NET_XMIT_DROP;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
892f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
8931da177e4SLinus Torvalds {
894f271b2ccSMax Krasnyansky 	/*
895f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
896f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
897f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
898f271b2ccSMax Krasnyansky 	 */
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9014885a504SEd Swierk #define MIN_MTU 68
9024885a504SEd Swierk #define MAX_MTU 65535
9034885a504SEd Swierk 
9044885a504SEd Swierk static int
9054885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
9064885a504SEd Swierk {
9074885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
9084885a504SEd Swierk 		return -EINVAL;
9094885a504SEd Swierk 	dev->mtu = new_mtu;
9104885a504SEd Swierk 	return 0;
9114885a504SEd Swierk }
9124885a504SEd Swierk 
913c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
914c8f44affSMichał Mirosław 	netdev_features_t features)
91588255375SMichał Mirosław {
91688255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
91788255375SMichał Mirosław 
91888255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
91988255375SMichał Mirosław }
920bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
921bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
922bebd097aSNeil Horman {
923bebd097aSNeil Horman 	/*
924bebd097aSNeil Horman 	 * Tun only receives frames when:
925bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
926bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
92792d4ea6eSstephen hemminger 	 * Since both of those are synchronous operations, we are guaranteed
928bebd097aSNeil Horman 	 * never to have pending data when we poll for it
92992d4ea6eSstephen hemminger 	 * so there is nothing to do here but return.
930bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
931bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
932bebd097aSNeil Horman 	 * still use netconsole
933bebd097aSNeil Horman 	 */
934bebd097aSNeil Horman 	return;
935bebd097aSNeil Horman }
936bebd097aSNeil Horman #endif
937758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
938c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
939758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
940758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
94100829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
942758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
94388255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
944c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
945bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
946bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
947bebd097aSNeil Horman #endif
948758e43b7SStephen Hemminger };
949758e43b7SStephen Hemminger 
950758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
951c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
952758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
953758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
95400829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
955758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
95688255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
957afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
958758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
959758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
960c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
961bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
962bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
963bebd097aSNeil Horman #endif
9645e52796aSToshiaki Makita 	.ndo_features_check	= passthru_features_check,
965758e43b7SStephen Hemminger };
966758e43b7SStephen Hemminger 
967944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun)
96896442e42SJason Wang {
96996442e42SJason Wang 	int i;
97096442e42SJason Wang 
97196442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
97296442e42SJason Wang 		INIT_HLIST_HEAD(&tun->flows[i]);
97396442e42SJason Wang 
97496442e42SJason Wang 	tun->ageing_time = TUN_FLOW_EXPIRE;
97596442e42SJason Wang 	setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
97696442e42SJason Wang 	mod_timer(&tun->flow_gc_timer,
97796442e42SJason Wang 		  round_jiffies_up(jiffies + tun->ageing_time));
97896442e42SJason Wang }
97996442e42SJason Wang 
98096442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun)
98196442e42SJason Wang {
98296442e42SJason Wang 	del_timer_sync(&tun->flow_gc_timer);
98396442e42SJason Wang 	tun_flow_flush(tun);
98496442e42SJason Wang }
98596442e42SJason Wang 
9861da177e4SLinus Torvalds /* Initialize net device. */
9871da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
9881da177e4SLinus Torvalds {
9891da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
99240630b82SMichael S. Tsirkin 	case IFF_TUN:
993758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
994758e43b7SStephen Hemminger 
9951da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
9961da177e4SLinus Torvalds 		dev->hard_header_len = 0;
9971da177e4SLinus Torvalds 		dev->addr_len = 0;
9981da177e4SLinus Torvalds 		dev->mtu = 1500;
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 		/* Zero header length */
10011da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
10021da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
10031da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
10041da177e4SLinus Torvalds 		break;
10051da177e4SLinus Torvalds 
100640630b82SMichael S. Tsirkin 	case IFF_TAP:
10077a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
10081da177e4SLinus Torvalds 		/* Ethernet TAP Device */
10091da177e4SLinus Torvalds 		ether_setup(dev);
1010550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1011a676847bSstephen hemminger 		dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
101236226a8dSBrian Braunstein 
1013f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
101436226a8dSBrian Braunstein 
10151da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
10161da177e4SLinus Torvalds 		break;
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds /* Character device part */
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds /* Poll */
10231da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
10241da177e4SLinus Torvalds {
1025b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
1026b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
10273c8a9c63SMariusz Kozlowski 	struct sock *sk;
102833dccbb0SHerbert Xu 	unsigned int mask = 0;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	if (!tun)
1031eac9e902SEric W. Biederman 		return POLLERR;
10321da177e4SLinus Torvalds 
103354f968d6SJason Wang 	sk = tfile->socket.sk;
10343c8a9c63SMariusz Kozlowski 
10356b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
10361da177e4SLinus Torvalds 
10379e641bdcSXi Wang 	poll_wait(file, sk_sleep(sk), wait);
10381da177e4SLinus Torvalds 
103989f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
10401da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
10411da177e4SLinus Torvalds 
104233dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
1043*9cd3e072SEric Dumazet 	    (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
104433dccbb0SHerbert Xu 	     sock_writeable(sk)))
104533dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
104633dccbb0SHerbert Xu 
1047c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
1048c70f1829SEric W. Biederman 		mask = POLLERR;
1049c70f1829SEric W. Biederman 
1050631ab46bSEric W. Biederman 	tun_put(tun);
10511da177e4SLinus Torvalds 	return mask;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
1054f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
1055f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
105654f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
105733dccbb0SHerbert Xu 				     size_t prepad, size_t len,
105833dccbb0SHerbert Xu 				     size_t linear, int noblock)
1059f42157cbSRusty Russell {
106054f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
1061f42157cbSRusty Russell 	struct sk_buff *skb;
106233dccbb0SHerbert Xu 	int err;
1063f42157cbSRusty Russell 
1064f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
10650eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
106633dccbb0SHerbert Xu 		linear = len;
1067f42157cbSRusty Russell 
106833dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
106928d64271SEric Dumazet 				   &err, 0);
1070f42157cbSRusty Russell 	if (!skb)
107133dccbb0SHerbert Xu 		return ERR_PTR(err);
1072f42157cbSRusty Russell 
1073f42157cbSRusty Russell 	skb_reserve(skb, prepad);
1074f42157cbSRusty Russell 	skb_put(skb, linear);
107533dccbb0SHerbert Xu 	skb->data_len = len - linear;
107633dccbb0SHerbert Xu 	skb->len += len - linear;
1077f42157cbSRusty Russell 
1078f42157cbSRusty Russell 	return skb;
1079f42157cbSRusty Russell }
1080f42157cbSRusty Russell 
10811da177e4SLinus Torvalds /* Get packet from user space buffer */
108254f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1083f5ff53b4SAl Viro 			    void *msg_control, struct iov_iter *from,
1084f5ff53b4SAl Viro 			    int noblock)
10851da177e4SLinus Torvalds {
108609640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
10871da177e4SLinus Torvalds 	struct sk_buff *skb;
1088f5ff53b4SAl Viro 	size_t total_len = iov_iter_count(from);
10893dd5c330SJason Wang 	size_t len = total_len, align = NET_SKB_PAD, linear;
1090f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
109196f8d9ecSJason Wang 	int good_linear;
10920690899bSMichael S. Tsirkin 	int copylen;
10930690899bSMichael S. Tsirkin 	bool zerocopy = false;
10940690899bSMichael S. Tsirkin 	int err;
109549974420SEric Dumazet 	u32 rxhash;
1096f5ff53b4SAl Viro 	ssize_t n;
10971da177e4SLinus Torvalds 
109840630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
109915718ea0SDan Carpenter 		if (len < sizeof(pi))
11001da177e4SLinus Torvalds 			return -EINVAL;
110115718ea0SDan Carpenter 		len -= sizeof(pi);
11021da177e4SLinus Torvalds 
1103f5ff53b4SAl Viro 		n = copy_from_iter(&pi, sizeof(pi), from);
1104f5ff53b4SAl Viro 		if (n != sizeof(pi))
11051da177e4SLinus Torvalds 			return -EFAULT;
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 
110840630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR) {
110915718ea0SDan Carpenter 		if (len < tun->vnet_hdr_sz)
1110f43798c2SRusty Russell 			return -EINVAL;
111115718ea0SDan Carpenter 		len -= tun->vnet_hdr_sz;
1112f43798c2SRusty Russell 
1113f5ff53b4SAl Viro 		n = copy_from_iter(&gso, sizeof(gso), from);
1114f5ff53b4SAl Viro 		if (n != sizeof(gso))
1115f43798c2SRusty Russell 			return -EFAULT;
1116f43798c2SRusty Russell 
11174909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
111856f0dcc5SMichael S. Tsirkin 		    tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
111956f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
11204909122fSHerbert Xu 
112156f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > len)
1122f43798c2SRusty Russell 			return -EINVAL;
1123d8febb77SHerbert Xu 		iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso));
1124f43798c2SRusty Russell 	}
1125f43798c2SRusty Russell 
112640630b82SMichael S. Tsirkin 	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
1127a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
11280eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
112956f0dcc5SMichael S. Tsirkin 			     (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
1130e01bf1c8SRusty Russell 			return -EINVAL;
1131e01bf1c8SRusty Russell 	}
11321da177e4SLinus Torvalds 
113396f8d9ecSJason Wang 	good_linear = SKB_MAX_HEAD(align);
113496f8d9ecSJason Wang 
113588529176SJason Wang 	if (msg_control) {
1136f5ff53b4SAl Viro 		struct iov_iter i = *from;
1137f5ff53b4SAl Viro 
113888529176SJason Wang 		/* There are 256 bytes to be copied in skb, so there is
113988529176SJason Wang 		 * enough room for skb expand head in case it is used.
11400690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
11410690899bSMichael S. Tsirkin 		 */
114256f0dcc5SMichael S. Tsirkin 		copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
114396f8d9ecSJason Wang 		if (copylen > good_linear)
114496f8d9ecSJason Wang 			copylen = good_linear;
11453dd5c330SJason Wang 		linear = copylen;
1146f5ff53b4SAl Viro 		iov_iter_advance(&i, copylen);
1147f5ff53b4SAl Viro 		if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
114888529176SJason Wang 			zerocopy = true;
114988529176SJason Wang 	}
115088529176SJason Wang 
115188529176SJason Wang 	if (!zerocopy) {
11520690899bSMichael S. Tsirkin 		copylen = len;
115356f0dcc5SMichael S. Tsirkin 		if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
115496f8d9ecSJason Wang 			linear = good_linear;
115596f8d9ecSJason Wang 		else
115656f0dcc5SMichael S. Tsirkin 			linear = tun16_to_cpu(tun, gso.hdr_len);
11573dd5c330SJason Wang 	}
11580690899bSMichael S. Tsirkin 
11593dd5c330SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
116033dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
116133dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
116209f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
116333dccbb0SHerbert Xu 		return PTR_ERR(skb);
11641da177e4SLinus Torvalds 	}
11651da177e4SLinus Torvalds 
11660690899bSMichael S. Tsirkin 	if (zerocopy)
1167f5ff53b4SAl Viro 		err = zerocopy_sg_from_iter(skb, from);
116888529176SJason Wang 	else {
1169f5ff53b4SAl Viro 		err = skb_copy_datagram_from_iter(skb, 0, from, len);
117088529176SJason Wang 		if (!err && msg_control) {
117188529176SJason Wang 			struct ubuf_info *uarg = msg_control;
117288529176SJason Wang 			uarg->callback(uarg, false);
117388529176SJason Wang 		}
117488529176SJason Wang 	}
11750690899bSMichael S. Tsirkin 
11760690899bSMichael S. Tsirkin 	if (err) {
117709f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
11788f22757eSDave Jones 		kfree_skb(skb);
11791da177e4SLinus Torvalds 		return -EFAULT;
11808f22757eSDave Jones 	}
11811da177e4SLinus Torvalds 
1182f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
118356f0dcc5SMichael S. Tsirkin 		if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
118456f0dcc5SMichael S. Tsirkin 					  tun16_to_cpu(tun, gso.csum_offset))) {
1185f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1186f43798c2SRusty Russell 			kfree_skb(skb);
1187f43798c2SRusty Russell 			return -EINVAL;
1188f43798c2SRusty Russell 		}
118988255375SMichał Mirosław 	}
1190f43798c2SRusty Russell 
11911da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
119240630b82SMichael S. Tsirkin 	case IFF_TUN:
119340630b82SMichael S. Tsirkin 		if (tun->flags & IFF_NO_PI) {
1194f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
1195f09f7ee2SAng Way Chuang 			case 0x40:
1196f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
1197f09f7ee2SAng Way Chuang 				break;
1198f09f7ee2SAng Way Chuang 			case 0x60:
1199f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
1200f09f7ee2SAng Way Chuang 				break;
1201f09f7ee2SAng Way Chuang 			default:
1202f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
1203f09f7ee2SAng Way Chuang 				kfree_skb(skb);
1204f09f7ee2SAng Way Chuang 				return -EINVAL;
1205f09f7ee2SAng Way Chuang 			}
1206f09f7ee2SAng Way Chuang 		}
1207f09f7ee2SAng Way Chuang 
1208459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
12091da177e4SLinus Torvalds 		skb->protocol = pi.proto;
12104c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
12111da177e4SLinus Torvalds 		break;
121240630b82SMichael S. Tsirkin 	case IFF_TAP:
12131da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
12141da177e4SLinus Torvalds 		break;
12156403eab1SJoe Perches 	}
12161da177e4SLinus Torvalds 
1217f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1218f43798c2SRusty Russell 		pr_debug("GSO!\n");
1219f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1220f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
1221c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1222f43798c2SRusty Russell 			break;
1223f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
1224c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1225f43798c2SRusty Russell 			break;
1226e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
1227c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1228e36aa25aSSridhar Samudrala 			break;
1229f43798c2SRusty Russell 		default:
1230f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1231f43798c2SRusty Russell 			kfree_skb(skb);
1232f43798c2SRusty Russell 			return -EINVAL;
1233f43798c2SRusty Russell 		}
1234f43798c2SRusty Russell 
1235f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1236c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1237f43798c2SRusty Russell 
123856f0dcc5SMichael S. Tsirkin 		skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
1239f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
1240f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1241f43798c2SRusty Russell 			kfree_skb(skb);
1242f43798c2SRusty Russell 			return -EINVAL;
1243f43798c2SRusty Russell 		}
1244f43798c2SRusty Russell 
1245f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
1246f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1247f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
1248f43798c2SRusty Russell 	}
12491da177e4SLinus Torvalds 
12500690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
12510690899bSMichael S. Tsirkin 	if (zerocopy) {
12520690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
12530690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1254c9af6db4SPravin B Shelar 		skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
12550690899bSMichael S. Tsirkin 	}
12560690899bSMichael S. Tsirkin 
125772f65107SVlad Yasevich 	skb_reset_network_header(skb);
125840893fd0SJason Wang 	skb_probe_transport_header(skb, 0);
125938502af7SJason Wang 
12603958afa1STom Herbert 	rxhash = skb_get_hash(skb);
12611da177e4SLinus Torvalds 	netif_rx_ni(skb);
12621da177e4SLinus Torvalds 
126309f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
126409f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
12651da177e4SLinus Torvalds 
12669e85722dSJason Wang 	tun_flow_update(tun, rxhash, tfile);
12670690899bSMichael S. Tsirkin 	return total_len;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
1270f5ff53b4SAl Viro static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
12711da177e4SLinus Torvalds {
127233dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
1273ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
127454f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
1275631ab46bSEric W. Biederman 	ssize_t result;
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	if (!tun)
12781da177e4SLinus Torvalds 		return -EBADFD;
12791da177e4SLinus Torvalds 
1280f5ff53b4SAl Viro 	result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
1281631ab46bSEric W. Biederman 
1282631ab46bSEric W. Biederman 	tun_put(tun);
1283631ab46bSEric W. Biederman 	return result;
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds /* Put packet to the user space buffer */
12876f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
128854f968d6SJason Wang 			    struct tun_file *tfile,
12891da177e4SLinus Torvalds 			    struct sk_buff *skb,
1290e0b46d0eSHerbert Xu 			    struct iov_iter *iter)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
1293e0b46d0eSHerbert Xu 	ssize_t total;
12948c847d25SJason Wang 	int vlan_offset = 0;
1295a8f9bfdfSHerbert Xu 	int vlan_hlen = 0;
12962eb783c4SHerbert Xu 	int vnet_hdr_sz = 0;
1297a8f9bfdfSHerbert Xu 
1298df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb))
1299a8f9bfdfSHerbert Xu 		vlan_hlen = VLAN_HLEN;
13001da177e4SLinus Torvalds 
130140630b82SMichael S. Tsirkin 	if (tun->flags & IFF_VNET_HDR)
13022eb783c4SHerbert Xu 		vnet_hdr_sz = tun->vnet_hdr_sz;
13031da177e4SLinus Torvalds 
1304e0b46d0eSHerbert Xu 	total = skb->len + vlan_hlen + vnet_hdr_sz;
1305e0b46d0eSHerbert Xu 
130640630b82SMichael S. Tsirkin 	if (!(tun->flags & IFF_NO_PI)) {
1307e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < sizeof(pi))
13081da177e4SLinus Torvalds 			return -EINVAL;
13091da177e4SLinus Torvalds 
1310e0b46d0eSHerbert Xu 		total += sizeof(pi);
1311e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < total) {
13121da177e4SLinus Torvalds 			/* Packet will be striped */
13131da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 
1316e0b46d0eSHerbert Xu 		if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
13171da177e4SLinus Torvalds 			return -EFAULT;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
13202eb783c4SHerbert Xu 	if (vnet_hdr_sz) {
1321f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1322e0b46d0eSHerbert Xu 		if (iov_iter_count(iter) < vnet_hdr_sz)
1323f43798c2SRusty Russell 			return -EINVAL;
1324f43798c2SRusty Russell 
1325f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1326f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1327f43798c2SRusty Russell 
1328f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
132956f0dcc5SMichael S. Tsirkin 			gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
133056f0dcc5SMichael S. Tsirkin 			gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
1331f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1332f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1333f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1334f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1335e3e3c423SVlad Yasevich 			else if (sinfo->gso_type & SKB_GSO_UDP)
1336e3e3c423SVlad Yasevich 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1337ef3db4a5SMichael S. Tsirkin 			else {
13386b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1339ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
134056f0dcc5SMichael S. Tsirkin 				       sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
134156f0dcc5SMichael S. Tsirkin 				       tun16_to_cpu(tun, gso.hdr_len));
1342ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1343ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1344ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
134556f0dcc5SMichael S. Tsirkin 					       min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1346ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1347ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1348ef3db4a5SMichael S. Tsirkin 			}
1349f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1350f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1351f43798c2SRusty Russell 		} else
1352f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1353f43798c2SRusty Russell 
1354f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1355f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
135656f0dcc5SMichael S. Tsirkin 			gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
135756f0dcc5SMichael S. Tsirkin 						      vlan_hlen);
135856f0dcc5SMichael S. Tsirkin 			gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
135910a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
136010a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1361f43798c2SRusty Russell 		} /* else everything is zero */
1362f43798c2SRusty Russell 
1363e0b46d0eSHerbert Xu 		if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
1364f43798c2SRusty Russell 			return -EFAULT;
13658c847d25SJason Wang 
13668c847d25SJason Wang 		iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
1367f43798c2SRusty Russell 	}
1368f43798c2SRusty Russell 
1369a8f9bfdfSHerbert Xu 	if (vlan_hlen) {
1370e0b46d0eSHerbert Xu 		int ret;
13716680ec68SJason Wang 		struct {
13726680ec68SJason Wang 			__be16 h_vlan_proto;
13736680ec68SJason Wang 			__be16 h_vlan_TCI;
13746680ec68SJason Wang 		} veth;
13751da177e4SLinus Torvalds 
13766680ec68SJason Wang 		veth.h_vlan_proto = skb->vlan_proto;
1377df8a39deSJiri Pirko 		veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
13781da177e4SLinus Torvalds 
13796680ec68SJason Wang 		vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
13806680ec68SJason Wang 
1381e0b46d0eSHerbert Xu 		ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1382e0b46d0eSHerbert Xu 		if (ret || !iov_iter_count(iter))
13836680ec68SJason Wang 			goto done;
13846680ec68SJason Wang 
1385e0b46d0eSHerbert Xu 		ret = copy_to_iter(&veth, sizeof(veth), iter);
1386e0b46d0eSHerbert Xu 		if (ret != sizeof(veth) || !iov_iter_count(iter))
13876680ec68SJason Wang 			goto done;
13886680ec68SJason Wang 	}
13896680ec68SJason Wang 
1390e0b46d0eSHerbert Xu 	skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
13916680ec68SJason Wang 
13926680ec68SJason Wang done:
139309f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
1394e0b46d0eSHerbert Xu 	tun->dev->stats.tx_bytes += skb->len + vlan_hlen;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	return total;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
139954f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
14009b067034SAl Viro 			   struct iov_iter *to,
14019b067034SAl Viro 			   int noblock)
14021da177e4SLinus Torvalds {
14031da177e4SLinus Torvalds 	struct sk_buff *skb;
14049b067034SAl Viro 	ssize_t ret;
14059e641bdcSXi Wang 	int peeked, err, off = 0;
14061da177e4SLinus Torvalds 
14073872baf6SRami Rosen 	tun_debug(KERN_INFO, tun, "tun_do_read\n");
14081da177e4SLinus Torvalds 
14099b067034SAl Viro 	if (!iov_iter_count(to))
14109b067034SAl Viro 		return 0;
14111da177e4SLinus Torvalds 
14129e641bdcSXi Wang 	if (tun->dev->reg_state != NETREG_REGISTERED)
14139e641bdcSXi Wang 		return -EIO;
14141da177e4SLinus Torvalds 
14159e641bdcSXi Wang 	/* Read frames from queue */
14169e641bdcSXi Wang 	skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
14179e641bdcSXi Wang 				  &peeked, &off, &err);
1418e0b46d0eSHerbert Xu 	if (!skb)
1419957f094fSAlex Gartrell 		return err;
1420e0b46d0eSHerbert Xu 
14219b067034SAl Viro 	ret = tun_put_user(tun, tfile, skb, to);
1422f51a5e82SJason Wang 	if (unlikely(ret < 0))
14231da177e4SLinus Torvalds 		kfree_skb(skb);
1424f51a5e82SJason Wang 	else
1425f51a5e82SJason Wang 		consume_skb(skb);
14261da177e4SLinus Torvalds 
142705c2828cSMichael S. Tsirkin 	return ret;
142805c2828cSMichael S. Tsirkin }
142905c2828cSMichael S. Tsirkin 
14309b067034SAl Viro static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
143105c2828cSMichael S. Tsirkin {
143205c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
143305c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
143405c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
14359b067034SAl Viro 	ssize_t len = iov_iter_count(to), ret;
143605c2828cSMichael S. Tsirkin 
143705c2828cSMichael S. Tsirkin 	if (!tun)
143805c2828cSMichael S. Tsirkin 		return -EBADFD;
14399b067034SAl Viro 	ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK);
144042404c09SDavid S. Miller 	ret = min_t(ssize_t, ret, len);
1441d0b7da8aSZhi Yong Wu 	if (ret > 0)
1442d0b7da8aSZhi Yong Wu 		iocb->ki_pos = ret;
1443631ab46bSEric W. Biederman 	tun_put(tun);
14441da177e4SLinus Torvalds 	return ret;
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds 
144796442e42SJason Wang static void tun_free_netdev(struct net_device *dev)
144896442e42SJason Wang {
144996442e42SJason Wang 	struct tun_struct *tun = netdev_priv(dev);
145096442e42SJason Wang 
14514008e97fSJason Wang 	BUG_ON(!(list_empty(&tun->disabled)));
145296442e42SJason Wang 	tun_flow_uninit(tun);
14535dbbaf2dSPaul Moore 	security_tun_dev_free_security(tun->security);
145496442e42SJason Wang 	free_netdev(dev);
145596442e42SJason Wang }
145696442e42SJason Wang 
14571da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
14581da177e4SLinus Torvalds {
14591da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
14601da177e4SLinus Torvalds 
14610625c883SEric W. Biederman 	tun->owner = INVALID_UID;
14620625c883SEric W. Biederman 	tun->group = INVALID_GID;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
146596442e42SJason Wang 	dev->destructor = tun_free_netdev;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
1468f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1469f019a7a5SEric W. Biederman  * device with netlink.
1470f019a7a5SEric W. Biederman  */
1471f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1472f019a7a5SEric W. Biederman {
1473f019a7a5SEric W. Biederman 	return -EINVAL;
1474f019a7a5SEric W. Biederman }
1475f019a7a5SEric W. Biederman 
1476f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1477f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1478f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1479f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1480f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1481f019a7a5SEric W. Biederman };
1482f019a7a5SEric W. Biederman 
148333dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
148433dccbb0SHerbert Xu {
148554f968d6SJason Wang 	struct tun_file *tfile;
148643815482SEric Dumazet 	wait_queue_head_t *wqueue;
148733dccbb0SHerbert Xu 
148833dccbb0SHerbert Xu 	if (!sock_writeable(sk))
148933dccbb0SHerbert Xu 		return;
149033dccbb0SHerbert Xu 
1491*9cd3e072SEric Dumazet 	if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
149233dccbb0SHerbert Xu 		return;
149333dccbb0SHerbert Xu 
149443815482SEric Dumazet 	wqueue = sk_sleep(sk);
149543815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
149643815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
149705c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1498c722c625SHerbert Xu 
149954f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
150054f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
150133dccbb0SHerbert Xu }
150233dccbb0SHerbert Xu 
15031b784140SYing Xue static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
150405c2828cSMichael S. Tsirkin {
150554f968d6SJason Wang 	int ret;
150654f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
150754f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
150854f968d6SJason Wang 
150954f968d6SJason Wang 	if (!tun)
151054f968d6SJason Wang 		return -EBADFD;
1511f5ff53b4SAl Viro 
1512c0371da6SAl Viro 	ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
1513f5ff53b4SAl Viro 			   m->msg_flags & MSG_DONTWAIT);
151454f968d6SJason Wang 	tun_put(tun);
151554f968d6SJason Wang 	return ret;
151605c2828cSMichael S. Tsirkin }
151705c2828cSMichael S. Tsirkin 
15181b784140SYing Xue static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
151905c2828cSMichael S. Tsirkin 		       int flags)
152005c2828cSMichael S. Tsirkin {
152154f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
152254f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
152305c2828cSMichael S. Tsirkin 	int ret;
152454f968d6SJason Wang 
152554f968d6SJason Wang 	if (!tun)
152654f968d6SJason Wang 		return -EBADFD;
152754f968d6SJason Wang 
1528eda29772SRichard Cochran 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
15293811ae76SGao feng 		ret = -EINVAL;
15303811ae76SGao feng 		goto out;
15313811ae76SGao feng 	}
1532eda29772SRichard Cochran 	if (flags & MSG_ERRQUEUE) {
1533eda29772SRichard Cochran 		ret = sock_recv_errqueue(sock->sk, m, total_len,
1534eda29772SRichard Cochran 					 SOL_PACKET, TUN_TX_TIMESTAMP);
1535eda29772SRichard Cochran 		goto out;
1536eda29772SRichard Cochran 	}
1537c0371da6SAl Viro 	ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT);
153887897931SAlex Gartrell 	if (ret > (ssize_t)total_len) {
153942404c09SDavid S. Miller 		m->msg_flags |= MSG_TRUNC;
154042404c09SDavid S. Miller 		ret = flags & MSG_TRUNC ? ret : total_len;
154142404c09SDavid S. Miller 	}
15423811ae76SGao feng out:
154354f968d6SJason Wang 	tun_put(tun);
154405c2828cSMichael S. Tsirkin 	return ret;
154505c2828cSMichael S. Tsirkin }
154605c2828cSMichael S. Tsirkin 
154705c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
154805c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
154905c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
155005c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
155105c2828cSMichael S. Tsirkin };
155205c2828cSMichael S. Tsirkin 
155333dccbb0SHerbert Xu static struct proto tun_proto = {
155433dccbb0SHerbert Xu 	.name		= "tun",
155533dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
155654f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
155733dccbb0SHerbert Xu };
1558f019a7a5SEric W. Biederman 
1559980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1560980c9e8cSDavid Woodhouse {
1561031f5e03SMichael S. Tsirkin 	return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
1562980c9e8cSDavid Woodhouse }
1563980c9e8cSDavid Woodhouse 
1564980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1565980c9e8cSDavid Woodhouse 			      char *buf)
1566980c9e8cSDavid Woodhouse {
1567980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1568980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1569980c9e8cSDavid Woodhouse }
1570980c9e8cSDavid Woodhouse 
1571980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1572980c9e8cSDavid Woodhouse 			      char *buf)
1573980c9e8cSDavid Woodhouse {
1574980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15750625c883SEric W. Biederman 	return uid_valid(tun->owner)?
15760625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15770625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
15780625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1579980c9e8cSDavid Woodhouse }
1580980c9e8cSDavid Woodhouse 
1581980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1582980c9e8cSDavid Woodhouse 			      char *buf)
1583980c9e8cSDavid Woodhouse {
1584980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15850625c883SEric W. Biederman 	return gid_valid(tun->group) ?
15860625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15870625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
15880625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1589980c9e8cSDavid Woodhouse }
1590980c9e8cSDavid Woodhouse 
1591980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1592980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1593980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1594980c9e8cSDavid Woodhouse 
1595c4d33e24STakashi Iwai static struct attribute *tun_dev_attrs[] = {
1596c4d33e24STakashi Iwai 	&dev_attr_tun_flags.attr,
1597c4d33e24STakashi Iwai 	&dev_attr_owner.attr,
1598c4d33e24STakashi Iwai 	&dev_attr_group.attr,
1599c4d33e24STakashi Iwai 	NULL
1600c4d33e24STakashi Iwai };
1601c4d33e24STakashi Iwai 
1602c4d33e24STakashi Iwai static const struct attribute_group tun_attr_group = {
1603c4d33e24STakashi Iwai 	.attrs = tun_dev_attrs
1604c4d33e24STakashi Iwai };
1605c4d33e24STakashi Iwai 
1606d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
16071da177e4SLinus Torvalds {
16081da177e4SLinus Torvalds 	struct tun_struct *tun;
160954f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
16101da177e4SLinus Torvalds 	struct net_device *dev;
16111da177e4SLinus Torvalds 	int err;
16121da177e4SLinus Torvalds 
16137c0c3b1aSJason Wang 	if (tfile->detached)
16147c0c3b1aSJason Wang 		return -EINVAL;
16157c0c3b1aSJason Wang 
161674a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
161774a3e5a7SEric W. Biederman 	if (dev) {
1618f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1619f85ba780SDavid Woodhouse 			return -EBUSY;
162074a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
162174a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
162274a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
162374a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
162474a3e5a7SEric W. Biederman 		else
162574a3e5a7SEric W. Biederman 			return -EINVAL;
162674a3e5a7SEric W. Biederman 
16278e6d91aeSJason Wang 		if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
162840630b82SMichael S. Tsirkin 		    !!(tun->flags & IFF_MULTI_QUEUE))
16298e6d91aeSJason Wang 			return -EINVAL;
16308e6d91aeSJason Wang 
1631cde8b15fSJason Wang 		if (tun_not_capable(tun))
16322b980dbdSPaul Moore 			return -EPERM;
16335dbbaf2dSPaul Moore 		err = security_tun_dev_open(tun->security);
16342b980dbdSPaul Moore 		if (err < 0)
16352b980dbdSPaul Moore 			return err;
16362b980dbdSPaul Moore 
1637849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
1638a7385ba2SEric W. Biederman 		if (err < 0)
1639a7385ba2SEric W. Biederman 			return err;
16404008e97fSJason Wang 
164140630b82SMichael S. Tsirkin 		if (tun->flags & IFF_MULTI_QUEUE &&
1642e8dbad66SJason Wang 		    (tun->numqueues + tun->numdisabled > 1)) {
1643e8dbad66SJason Wang 			/* One or more queue has already been attached, no need
1644e8dbad66SJason Wang 			 * to initialize the device again.
1645e8dbad66SJason Wang 			 */
1646e8dbad66SJason Wang 			return 0;
1647e8dbad66SJason Wang 		}
164886a264abSDavid Howells 	}
16491da177e4SLinus Torvalds 	else {
16501da177e4SLinus Torvalds 		char *name;
16511da177e4SLinus Torvalds 		unsigned long flags = 0;
1652edfb6a14SJason Wang 		int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1653edfb6a14SJason Wang 			     MAX_TAP_QUEUES : 1;
16541da177e4SLinus Torvalds 
1655c260b772SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1656ca6bb5d7SDavid Woodhouse 			return -EPERM;
16572b980dbdSPaul Moore 		err = security_tun_dev_create();
16582b980dbdSPaul Moore 		if (err < 0)
16592b980dbdSPaul Moore 			return err;
1660ca6bb5d7SDavid Woodhouse 
16611da177e4SLinus Torvalds 		/* Set dev type */
16621da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
16631da177e4SLinus Torvalds 			/* TUN device */
166440630b82SMichael S. Tsirkin 			flags |= IFF_TUN;
16651da177e4SLinus Torvalds 			name = "tun%d";
16661da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
16671da177e4SLinus Torvalds 			/* TAP device */
166840630b82SMichael S. Tsirkin 			flags |= IFF_TAP;
16691da177e4SLinus Torvalds 			name = "tap%d";
16701da177e4SLinus Torvalds 		} else
167136989b90SKusanagi Kouichi 			return -EINVAL;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 		if (*ifr->ifr_name)
16741da177e4SLinus Torvalds 			name = ifr->ifr_name;
16751da177e4SLinus Torvalds 
1676c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1677c835a677STom Gundersen 				       NET_NAME_UNKNOWN, tun_setup, queues,
1678c835a677STom Gundersen 				       queues);
1679edfb6a14SJason Wang 
16801da177e4SLinus Torvalds 		if (!dev)
16811da177e4SLinus Torvalds 			return -ENOMEM;
16821da177e4SLinus Torvalds 
1683fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1684f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1685fb7589a1SPavel Emelyanov 		dev->ifindex = tfile->ifindex;
1686c4d33e24STakashi Iwai 		dev->sysfs_groups[0] = &tun_attr_group;
1687758e43b7SStephen Hemminger 
16881da177e4SLinus Torvalds 		tun = netdev_priv(dev);
16891da177e4SLinus Torvalds 		tun->dev = dev;
16901da177e4SLinus Torvalds 		tun->flags = flags;
1691f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1692d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
16931da177e4SLinus Torvalds 
169454f968d6SJason Wang 		tun->filter_attached = false;
169554f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
169633dccbb0SHerbert Xu 
169796442e42SJason Wang 		spin_lock_init(&tun->lock);
169896442e42SJason Wang 
16995dbbaf2dSPaul Moore 		err = security_tun_dev_alloc_security(&tun->security);
17005dbbaf2dSPaul Moore 		if (err < 0)
17015dbbaf2dSPaul Moore 			goto err_free_dev;
17022b980dbdSPaul Moore 
17031da177e4SLinus Torvalds 		tun_net_init(dev);
1704944a1376SPavel Emelyanov 		tun_flow_init(tun);
170596442e42SJason Wang 
170688255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
17076680ec68SJason Wang 				   TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
17086680ec68SJason Wang 				   NETIF_F_HW_VLAN_STAG_TX;
170988255375SMichał Mirosław 		dev->features = dev->hw_features;
17106671b224SFernando Luis Vazquez Cao 		dev->vlan_features = dev->features &
17116671b224SFernando Luis Vazquez Cao 				     ~(NETIF_F_HW_VLAN_CTAG_TX |
17126671b224SFernando Luis Vazquez Cao 				       NETIF_F_HW_VLAN_STAG_TX);
171388255375SMichał Mirosław 
17144008e97fSJason Wang 		INIT_LIST_HEAD(&tun->disabled);
1715849c9b6fSPavel Emelyanov 		err = tun_attach(tun, file, false);
1716eb0fb363SJason Wang 		if (err < 0)
1717662ca437SJason Wang 			goto err_free_flow;
1718eb0fb363SJason Wang 
17191da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
17201da177e4SLinus Torvalds 		if (err < 0)
1721662ca437SJason Wang 			goto err_detach;
1722af668b3cSMichael S. Tsirkin 	}
1723980c9e8cSDavid Woodhouse 
1724eb0fb363SJason Wang 	netif_carrier_on(tun->dev);
17251da177e4SLinus Torvalds 
17266b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
17271da177e4SLinus Torvalds 
1728031f5e03SMichael S. Tsirkin 	tun->flags = (tun->flags & ~TUN_FEATURES) |
1729031f5e03SMichael S. Tsirkin 		(ifr->ifr_flags & TUN_FEATURES);
1730c8d68e6bSJason Wang 
1731e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1732e35259a9SMax Krasnyansky 	 * xoff state.
1733e35259a9SMax Krasnyansky 	 */
1734e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1735c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1736e35259a9SMax Krasnyansky 
17371da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
17381da177e4SLinus Torvalds 	return 0;
17391da177e4SLinus Torvalds 
1740662ca437SJason Wang err_detach:
1741662ca437SJason Wang 	tun_detach_all(dev);
1742662ca437SJason Wang err_free_flow:
1743662ca437SJason Wang 	tun_flow_uninit(tun);
1744662ca437SJason Wang 	security_tun_dev_free_security(tun->security);
17451da177e4SLinus Torvalds err_free_dev:
17461da177e4SLinus Torvalds 	free_netdev(dev);
17471da177e4SLinus Torvalds 	return err;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
17509ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun,
1751876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1752e3b99556SMark McLoughlin {
17536b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1754e3b99556SMark McLoughlin 
1755e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1756e3b99556SMark McLoughlin 
1757980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1758e3b99556SMark McLoughlin 
1759e3b99556SMark McLoughlin }
1760e3b99556SMark McLoughlin 
17615228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
17625228ddc9SRusty Russell  * privs required. */
176388255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
17645228ddc9SRusty Russell {
1765c8f44affSMichał Mirosław 	netdev_features_t features = 0;
17665228ddc9SRusty Russell 
17675228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
176888255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
17695228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
17705228ddc9SRusty Russell 
17715228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
17725228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
17735228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
17745228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
17755228ddc9SRusty Russell 			}
17765228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
17775228ddc9SRusty Russell 				features |= NETIF_F_TSO;
17785228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
17795228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
17805228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
17815228ddc9SRusty Russell 		}
1782e3e3c423SVlad Yasevich 
1783e3e3c423SVlad Yasevich 		if (arg & TUN_F_UFO) {
1784e3e3c423SVlad Yasevich 			features |= NETIF_F_UFO;
1785e3e3c423SVlad Yasevich 			arg &= ~TUN_F_UFO;
1786e3e3c423SVlad Yasevich 		}
17875228ddc9SRusty Russell 	}
17885228ddc9SRusty Russell 
17895228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
17905228ddc9SRusty Russell 	 * trying to set them. */
17915228ddc9SRusty Russell 	if (arg)
17925228ddc9SRusty Russell 		return -EINVAL;
17935228ddc9SRusty Russell 
179488255375SMichał Mirosław 	tun->set_features = features;
179588255375SMichał Mirosław 	netdev_update_features(tun->dev);
17965228ddc9SRusty Russell 
17975228ddc9SRusty Russell 	return 0;
17985228ddc9SRusty Russell }
17995228ddc9SRusty Russell 
1800c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1801c8d68e6bSJason Wang {
1802c8d68e6bSJason Wang 	int i;
1803c8d68e6bSJason Wang 	struct tun_file *tfile;
1804c8d68e6bSJason Wang 
1805c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1806b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1807c8d68e6bSJason Wang 		sk_detach_filter(tfile->socket.sk);
1808c8d68e6bSJason Wang 	}
1809c8d68e6bSJason Wang 
1810c8d68e6bSJason Wang 	tun->filter_attached = false;
1811c8d68e6bSJason Wang }
1812c8d68e6bSJason Wang 
1813c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1814c8d68e6bSJason Wang {
1815c8d68e6bSJason Wang 	int i, ret = 0;
1816c8d68e6bSJason Wang 	struct tun_file *tfile;
1817c8d68e6bSJason Wang 
1818c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1819b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1820c8d68e6bSJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1821c8d68e6bSJason Wang 		if (ret) {
1822c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1823c8d68e6bSJason Wang 			return ret;
1824c8d68e6bSJason Wang 		}
1825c8d68e6bSJason Wang 	}
1826c8d68e6bSJason Wang 
1827c8d68e6bSJason Wang 	tun->filter_attached = true;
1828c8d68e6bSJason Wang 	return ret;
1829c8d68e6bSJason Wang }
1830c8d68e6bSJason Wang 
1831c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1832c8d68e6bSJason Wang {
1833c8d68e6bSJason Wang 	struct tun_file *tfile;
1834c8d68e6bSJason Wang 	int i;
1835c8d68e6bSJason Wang 
1836c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1837b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1838c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1839c8d68e6bSJason Wang 	}
1840c8d68e6bSJason Wang }
1841c8d68e6bSJason Wang 
1842cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1843cde8b15fSJason Wang {
1844cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1845cde8b15fSJason Wang 	struct tun_struct *tun;
1846cde8b15fSJason Wang 	int ret = 0;
1847cde8b15fSJason Wang 
1848cde8b15fSJason Wang 	rtnl_lock();
1849cde8b15fSJason Wang 
1850cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
18514008e97fSJason Wang 		tun = tfile->detached;
18525dbbaf2dSPaul Moore 		if (!tun) {
1853cde8b15fSJason Wang 			ret = -EINVAL;
18545dbbaf2dSPaul Moore 			goto unlock;
18555dbbaf2dSPaul Moore 		}
18565dbbaf2dSPaul Moore 		ret = security_tun_dev_attach_queue(tun->security);
18575dbbaf2dSPaul Moore 		if (ret < 0)
18585dbbaf2dSPaul Moore 			goto unlock;
1859849c9b6fSPavel Emelyanov 		ret = tun_attach(tun, file, false);
18604008e97fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
1861b8deabd3SJason Wang 		tun = rtnl_dereference(tfile->tun);
186240630b82SMichael S. Tsirkin 		if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
18634008e97fSJason Wang 			ret = -EINVAL;
1864cde8b15fSJason Wang 		else
18654008e97fSJason Wang 			__tun_detach(tfile, false);
18664008e97fSJason Wang 	} else
1867cde8b15fSJason Wang 		ret = -EINVAL;
1868cde8b15fSJason Wang 
18695dbbaf2dSPaul Moore unlock:
1870cde8b15fSJason Wang 	rtnl_unlock();
1871cde8b15fSJason Wang 	return ret;
1872cde8b15fSJason Wang }
1873cde8b15fSJason Wang 
187450857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
187550857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
18761da177e4SLinus Torvalds {
187736b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1878631ab46bSEric W. Biederman 	struct tun_struct *tun;
18791da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
18801da177e4SLinus Torvalds 	struct ifreq ifr;
18810625c883SEric W. Biederman 	kuid_t owner;
18820625c883SEric W. Biederman 	kgid_t group;
188333dccbb0SHerbert Xu 	int sndbuf;
1884d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1885fb7589a1SPavel Emelyanov 	unsigned int ifindex;
18861cf8e410SMichael S. Tsirkin 	int le;
1887f271b2ccSMax Krasnyansky 	int ret;
18881da177e4SLinus Torvalds 
1889cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
189050857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
18911da177e4SLinus Torvalds 			return -EFAULT;
18928bbb1813SDavid S. Miller 	} else {
1893a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
18948bbb1813SDavid S. Miller 	}
1895631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1896631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1897631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1898031f5e03SMichael S. Tsirkin 		 * TUNSETIFF.
1899031f5e03SMichael S. Tsirkin 		 */
1900031f5e03SMichael S. Tsirkin 		return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
1901631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1902cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
1903cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
1904631ab46bSEric W. Biederman 
1905c8d68e6bSJason Wang 	ret = 0;
1906876bfd4dSHerbert Xu 	rtnl_lock();
1907876bfd4dSHerbert Xu 
190836b50babSEric W. Biederman 	tun = __tun_get(tfile);
19091da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
19101da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
19111da177e4SLinus Torvalds 
1912140e807dSEric W. Biederman 		ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
19131da177e4SLinus Torvalds 
1914876bfd4dSHerbert Xu 		if (ret)
1915876bfd4dSHerbert Xu 			goto unlock;
19161da177e4SLinus Torvalds 
191750857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1918876bfd4dSHerbert Xu 			ret = -EFAULT;
1919876bfd4dSHerbert Xu 		goto unlock;
19201da177e4SLinus Torvalds 	}
1921fb7589a1SPavel Emelyanov 	if (cmd == TUNSETIFINDEX) {
1922fb7589a1SPavel Emelyanov 		ret = -EPERM;
1923fb7589a1SPavel Emelyanov 		if (tun)
1924fb7589a1SPavel Emelyanov 			goto unlock;
1925fb7589a1SPavel Emelyanov 
1926fb7589a1SPavel Emelyanov 		ret = -EFAULT;
1927fb7589a1SPavel Emelyanov 		if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
1928fb7589a1SPavel Emelyanov 			goto unlock;
1929fb7589a1SPavel Emelyanov 
1930fb7589a1SPavel Emelyanov 		ret = 0;
1931fb7589a1SPavel Emelyanov 		tfile->ifindex = ifindex;
1932fb7589a1SPavel Emelyanov 		goto unlock;
1933fb7589a1SPavel Emelyanov 	}
19341da177e4SLinus Torvalds 
1935876bfd4dSHerbert Xu 	ret = -EBADFD;
19361da177e4SLinus Torvalds 	if (!tun)
1937876bfd4dSHerbert Xu 		goto unlock;
19381da177e4SLinus Torvalds 
19391e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
19401da177e4SLinus Torvalds 
1941631ab46bSEric W. Biederman 	ret = 0;
19421da177e4SLinus Torvalds 	switch (cmd) {
1943e3b99556SMark McLoughlin 	case TUNGETIFF:
19449ce99cf6SRami Rosen 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1945e3b99556SMark McLoughlin 
19463d407a80SPavel Emelyanov 		if (tfile->detached)
19473d407a80SPavel Emelyanov 			ifr.ifr_flags |= IFF_DETACH_QUEUE;
1948849c9b6fSPavel Emelyanov 		if (!tfile->socket.sk->sk_filter)
1949849c9b6fSPavel Emelyanov 			ifr.ifr_flags |= IFF_NOFILTER;
19503d407a80SPavel Emelyanov 
195150857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1952631ab46bSEric W. Biederman 			ret = -EFAULT;
1953e3b99556SMark McLoughlin 		break;
1954e3b99556SMark McLoughlin 
19551da177e4SLinus Torvalds 	case TUNSETNOCSUM:
19561da177e4SLinus Torvalds 		/* Disable/Enable checksum */
19571da177e4SLinus Torvalds 
195888255375SMichał Mirosław 		/* [unimplemented] */
195988255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
19606b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
19611da177e4SLinus Torvalds 		break;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	case TUNSETPERSIST:
196454f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
196554f968d6SJason Wang 		 * module to prevent the module being unprobed.
196654f968d6SJason Wang 		 */
196740630b82SMichael S. Tsirkin 		if (arg && !(tun->flags & IFF_PERSIST)) {
196840630b82SMichael S. Tsirkin 			tun->flags |= IFF_PERSIST;
196954f968d6SJason Wang 			__module_get(THIS_MODULE);
1970dd38bd85SJason Wang 		}
197140630b82SMichael S. Tsirkin 		if (!arg && (tun->flags & IFF_PERSIST)) {
197240630b82SMichael S. Tsirkin 			tun->flags &= ~IFF_PERSIST;
197354f968d6SJason Wang 			module_put(THIS_MODULE);
197454f968d6SJason Wang 		}
19751da177e4SLinus Torvalds 
19766b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
19776b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
19781da177e4SLinus Torvalds 		break;
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	case TUNSETOWNER:
19811da177e4SLinus Torvalds 		/* Set owner of the device */
19820625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
19830625c883SEric W. Biederman 		if (!uid_valid(owner)) {
19840625c883SEric W. Biederman 			ret = -EINVAL;
19850625c883SEric W. Biederman 			break;
19860625c883SEric W. Biederman 		}
19870625c883SEric W. Biederman 		tun->owner = owner;
19881e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
19890625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
19901da177e4SLinus Torvalds 		break;
19911da177e4SLinus Torvalds 
19928c644623SGuido Guenther 	case TUNSETGROUP:
19938c644623SGuido Guenther 		/* Set group of the device */
19940625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
19950625c883SEric W. Biederman 		if (!gid_valid(group)) {
19960625c883SEric W. Biederman 			ret = -EINVAL;
19970625c883SEric W. Biederman 			break;
19980625c883SEric W. Biederman 		}
19990625c883SEric W. Biederman 		tun->group = group;
20001e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
20010625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
20028c644623SGuido Guenther 		break;
20038c644623SGuido Guenther 
2004ff4cc3acSMike Kershaw 	case TUNSETLINK:
2005ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
2006ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
20076b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
20086b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
200948abfe05SDavid S. Miller 			ret = -EBUSY;
2010ff4cc3acSMike Kershaw 		} else {
2011ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
20126b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
20136b8a66eeSJoe Perches 				  tun->dev->type);
201448abfe05SDavid S. Miller 			ret = 0;
2015ff4cc3acSMike Kershaw 		}
2016631ab46bSEric W. Biederman 		break;
2017ff4cc3acSMike Kershaw 
20181da177e4SLinus Torvalds #ifdef TUN_DEBUG
20191da177e4SLinus Torvalds 	case TUNSETDEBUG:
20201da177e4SLinus Torvalds 		tun->debug = arg;
20211da177e4SLinus Torvalds 		break;
20221da177e4SLinus Torvalds #endif
20235228ddc9SRusty Russell 	case TUNSETOFFLOAD:
202488255375SMichał Mirosław 		ret = set_offload(tun, arg);
2025631ab46bSEric W. Biederman 		break;
20265228ddc9SRusty Russell 
2027f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
2028f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
2029631ab46bSEric W. Biederman 		ret = -EINVAL;
203040630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
2031631ab46bSEric W. Biederman 			break;
2032c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
2033631ab46bSEric W. Biederman 		break;
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
2036b595076aSUwe Kleine-König 		/* Get hw address */
2037f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2038f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
203950857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
2040631ab46bSEric W. Biederman 			ret = -EFAULT;
2041631ab46bSEric W. Biederman 		break;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
2044f271b2ccSMax Krasnyansky 		/* Set hw address */
20456b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
20466b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
204740102371SKim B. Heino 
204840102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
2049631ab46bSEric W. Biederman 		break;
205033dccbb0SHerbert Xu 
205133dccbb0SHerbert Xu 	case TUNGETSNDBUF:
205254f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
205333dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
205433dccbb0SHerbert Xu 			ret = -EFAULT;
205533dccbb0SHerbert Xu 		break;
205633dccbb0SHerbert Xu 
205733dccbb0SHerbert Xu 	case TUNSETSNDBUF:
205833dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
205933dccbb0SHerbert Xu 			ret = -EFAULT;
206033dccbb0SHerbert Xu 			break;
206133dccbb0SHerbert Xu 		}
206233dccbb0SHerbert Xu 
2063c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
2064c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
206533dccbb0SHerbert Xu 		break;
206633dccbb0SHerbert Xu 
2067d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
2068d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
2069d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2070d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2071d9d52b51SMichael S. Tsirkin 		break;
2072d9d52b51SMichael S. Tsirkin 
2073d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
2074d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2075d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2076d9d52b51SMichael S. Tsirkin 			break;
2077d9d52b51SMichael S. Tsirkin 		}
2078d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2079d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
2080d9d52b51SMichael S. Tsirkin 			break;
2081d9d52b51SMichael S. Tsirkin 		}
2082d9d52b51SMichael S. Tsirkin 
2083d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
2084d9d52b51SMichael S. Tsirkin 		break;
2085d9d52b51SMichael S. Tsirkin 
20861cf8e410SMichael S. Tsirkin 	case TUNGETVNETLE:
20871cf8e410SMichael S. Tsirkin 		le = !!(tun->flags & TUN_VNET_LE);
20881cf8e410SMichael S. Tsirkin 		if (put_user(le, (int __user *)argp))
20891cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
20901cf8e410SMichael S. Tsirkin 		break;
20911cf8e410SMichael S. Tsirkin 
20921cf8e410SMichael S. Tsirkin 	case TUNSETVNETLE:
20931cf8e410SMichael S. Tsirkin 		if (get_user(le, (int __user *)argp)) {
20941cf8e410SMichael S. Tsirkin 			ret = -EFAULT;
20951cf8e410SMichael S. Tsirkin 			break;
20961cf8e410SMichael S. Tsirkin 		}
20971cf8e410SMichael S. Tsirkin 		if (le)
20981cf8e410SMichael S. Tsirkin 			tun->flags |= TUN_VNET_LE;
20991cf8e410SMichael S. Tsirkin 		else
21001cf8e410SMichael S. Tsirkin 			tun->flags &= ~TUN_VNET_LE;
21011cf8e410SMichael S. Tsirkin 		break;
21021cf8e410SMichael S. Tsirkin 
21038b8e658bSGreg Kurz 	case TUNGETVNETBE:
21048b8e658bSGreg Kurz 		ret = tun_get_vnet_be(tun, argp);
21058b8e658bSGreg Kurz 		break;
21068b8e658bSGreg Kurz 
21078b8e658bSGreg Kurz 	case TUNSETVNETBE:
21088b8e658bSGreg Kurz 		ret = tun_set_vnet_be(tun, argp);
21098b8e658bSGreg Kurz 		break;
21108b8e658bSGreg Kurz 
211199405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
211299405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
211399405162SMichael S. Tsirkin 		ret = -EINVAL;
211440630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
211599405162SMichael S. Tsirkin 			break;
211699405162SMichael S. Tsirkin 		ret = -EFAULT;
211754f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
211899405162SMichael S. Tsirkin 			break;
211999405162SMichael S. Tsirkin 
2120c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
212199405162SMichael S. Tsirkin 		break;
212299405162SMichael S. Tsirkin 
212399405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
212499405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
212599405162SMichael S. Tsirkin 		ret = -EINVAL;
212640630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
212799405162SMichael S. Tsirkin 			break;
2128c8d68e6bSJason Wang 		ret = 0;
2129c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
213099405162SMichael S. Tsirkin 		break;
213199405162SMichael S. Tsirkin 
213276975e9cSPavel Emelyanov 	case TUNGETFILTER:
213376975e9cSPavel Emelyanov 		ret = -EINVAL;
213440630b82SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
213576975e9cSPavel Emelyanov 			break;
213676975e9cSPavel Emelyanov 		ret = -EFAULT;
213776975e9cSPavel Emelyanov 		if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
213876975e9cSPavel Emelyanov 			break;
213976975e9cSPavel Emelyanov 		ret = 0;
214076975e9cSPavel Emelyanov 		break;
214176975e9cSPavel Emelyanov 
21421da177e4SLinus Torvalds 	default:
2143631ab46bSEric W. Biederman 		ret = -EINVAL;
2144631ab46bSEric W. Biederman 		break;
2145ee289b64SJoe Perches 	}
21461da177e4SLinus Torvalds 
2147876bfd4dSHerbert Xu unlock:
2148876bfd4dSHerbert Xu 	rtnl_unlock();
2149876bfd4dSHerbert Xu 	if (tun)
2150631ab46bSEric W. Biederman 		tun_put(tun);
2151631ab46bSEric W. Biederman 	return ret;
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
215450857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
215550857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
215650857e2aSArnd Bergmann {
215750857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
215850857e2aSArnd Bergmann }
215950857e2aSArnd Bergmann 
216050857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
216150857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
216250857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
216350857e2aSArnd Bergmann {
216450857e2aSArnd Bergmann 	switch (cmd) {
216550857e2aSArnd Bergmann 	case TUNSETIFF:
216650857e2aSArnd Bergmann 	case TUNGETIFF:
216750857e2aSArnd Bergmann 	case TUNSETTXFILTER:
216850857e2aSArnd Bergmann 	case TUNGETSNDBUF:
216950857e2aSArnd Bergmann 	case TUNSETSNDBUF:
217050857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
217150857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
217250857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
217350857e2aSArnd Bergmann 		break;
217450857e2aSArnd Bergmann 	default:
217550857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
217650857e2aSArnd Bergmann 		break;
217750857e2aSArnd Bergmann 	}
217850857e2aSArnd Bergmann 
217950857e2aSArnd Bergmann 	/*
218050857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
218150857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
218250857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
218350857e2aSArnd Bergmann 	 * contents.
218450857e2aSArnd Bergmann 	 */
218550857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
218650857e2aSArnd Bergmann }
218750857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
218850857e2aSArnd Bergmann 
21891da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
21901da177e4SLinus Torvalds {
219154f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
21921da177e4SLinus Torvalds 	int ret;
21931da177e4SLinus Torvalds 
219454f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
21959d319522SJonathan Corbet 		goto out;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	if (on) {
2198e0b93eddSJeff Layton 		__f_setown(file, task_pid(current), PIDTYPE_PID, 0);
219954f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
22001da177e4SLinus Torvalds 	} else
220154f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
22029d319522SJonathan Corbet 	ret = 0;
22039d319522SJonathan Corbet out:
22049d319522SJonathan Corbet 	return ret;
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
22081da177e4SLinus Torvalds {
2209140e807dSEric W. Biederman 	struct net *net = current->nsproxy->net_ns;
2210631ab46bSEric W. Biederman 	struct tun_file *tfile;
2211deed49fbSThomas Gleixner 
22126b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2213631ab46bSEric W. Biederman 
2214140e807dSEric W. Biederman 	tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
221511aa9c28SEric W. Biederman 					    &tun_proto, 0);
2216631ab46bSEric W. Biederman 	if (!tfile)
2217631ab46bSEric W. Biederman 		return -ENOMEM;
2218c956674bSMonam Agarwal 	RCU_INIT_POINTER(tfile->tun, NULL);
221954f968d6SJason Wang 	tfile->flags = 0;
2220fb7589a1SPavel Emelyanov 	tfile->ifindex = 0;
222154f968d6SJason Wang 
222254f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
22239e641bdcSXi Wang 	RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
222454f968d6SJason Wang 
222554f968d6SJason Wang 	tfile->socket.file = file;
222654f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
222754f968d6SJason Wang 
222854f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
222954f968d6SJason Wang 
223054f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
223154f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
223254f968d6SJason Wang 
2233631ab46bSEric W. Biederman 	file->private_data = tfile;
22344008e97fSJason Wang 	INIT_LIST_HEAD(&tfile->next);
223554f968d6SJason Wang 
223619a6afb2SJason Wang 	sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
223719a6afb2SJason Wang 
22381da177e4SLinus Torvalds 	return 0;
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
22421da177e4SLinus Torvalds {
2243631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
22441da177e4SLinus Torvalds 
2245c8d68e6bSJason Wang 	tun_detach(tfile, true);
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	return 0;
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
225093e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
2251a3816ab0SJoe Perches static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
225293e14b6dSMasatake YAMATO {
225393e14b6dSMasatake YAMATO 	struct tun_struct *tun;
225493e14b6dSMasatake YAMATO 	struct ifreq ifr;
225593e14b6dSMasatake YAMATO 
225693e14b6dSMasatake YAMATO 	memset(&ifr, 0, sizeof(ifr));
225793e14b6dSMasatake YAMATO 
225893e14b6dSMasatake YAMATO 	rtnl_lock();
225993e14b6dSMasatake YAMATO 	tun = tun_get(f);
226093e14b6dSMasatake YAMATO 	if (tun)
226193e14b6dSMasatake YAMATO 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
226293e14b6dSMasatake YAMATO 	rtnl_unlock();
226393e14b6dSMasatake YAMATO 
226493e14b6dSMasatake YAMATO 	if (tun)
226593e14b6dSMasatake YAMATO 		tun_put(tun);
226693e14b6dSMasatake YAMATO 
2267a3816ab0SJoe Perches 	seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
226893e14b6dSMasatake YAMATO }
226993e14b6dSMasatake YAMATO #endif
227093e14b6dSMasatake YAMATO 
2271d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
22721da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
22731da177e4SLinus Torvalds 	.llseek = no_llseek,
22749b067034SAl Viro 	.read_iter  = tun_chr_read_iter,
2275f5ff53b4SAl Viro 	.write_iter = tun_chr_write_iter,
22761da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
2277876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
227850857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
227950857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
228050857e2aSArnd Bergmann #endif
22811da177e4SLinus Torvalds 	.open	= tun_chr_open,
22821da177e4SLinus Torvalds 	.release = tun_chr_close,
228393e14b6dSMasatake YAMATO 	.fasync = tun_chr_fasync,
228493e14b6dSMasatake YAMATO #ifdef CONFIG_PROC_FS
228593e14b6dSMasatake YAMATO 	.show_fdinfo = tun_chr_show_fdinfo,
228693e14b6dSMasatake YAMATO #endif
22871da177e4SLinus Torvalds };
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
22901da177e4SLinus Torvalds 	.minor = TUN_MINOR,
22911da177e4SLinus Torvalds 	.name = "tun",
2292e454cea2SKay Sievers 	.nodename = "net/tun",
22931da177e4SLinus Torvalds 	.fops = &tun_fops,
22941da177e4SLinus Torvalds };
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds /* ethtool interface */
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
22991da177e4SLinus Torvalds {
23001da177e4SLinus Torvalds 	cmd->supported		= 0;
23011da177e4SLinus Torvalds 	cmd->advertising	= 0;
230270739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
23031da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
23041da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
23051da177e4SLinus Torvalds 	cmd->phy_address	= 0;
23061da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
23071da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
23081da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
23091da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
23101da177e4SLinus Torvalds 	return 0;
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
23141da177e4SLinus Torvalds {
23151da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
23161da177e4SLinus Torvalds 
231733a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
231833a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
232140630b82SMichael S. Tsirkin 	case IFF_TUN:
232233a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
23231da177e4SLinus Torvalds 		break;
232440630b82SMichael S. Tsirkin 	case IFF_TAP:
232533a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
23261da177e4SLinus Torvalds 		break;
23271da177e4SLinus Torvalds 	}
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
23311da177e4SLinus Torvalds {
23321da177e4SLinus Torvalds #ifdef TUN_DEBUG
23331da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
23341da177e4SLinus Torvalds 	return tun->debug;
23351da177e4SLinus Torvalds #else
23361da177e4SLinus Torvalds 	return -EOPNOTSUPP;
23371da177e4SLinus Torvalds #endif
23381da177e4SLinus Torvalds }
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
23411da177e4SLinus Torvalds {
23421da177e4SLinus Torvalds #ifdef TUN_DEBUG
23431da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
23441da177e4SLinus Torvalds 	tun->debug = value;
23451da177e4SLinus Torvalds #endif
23461da177e4SLinus Torvalds }
23471da177e4SLinus Torvalds 
23487282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
23491da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
23501da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
23511da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
23521da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
2353bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
2354eda29772SRichard Cochran 	.get_ts_info	= ethtool_op_get_ts_info,
23551da177e4SLinus Torvalds };
23561da177e4SLinus Torvalds 
235779d17604SPavel Emelyanov 
23581da177e4SLinus Torvalds static int __init tun_init(void)
23591da177e4SLinus Torvalds {
23601da177e4SLinus Torvalds 	int ret = 0;
23611da177e4SLinus Torvalds 
23626b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
23636b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
23641da177e4SLinus Torvalds 
2365f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
236679d17604SPavel Emelyanov 	if (ret) {
23676b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
2368f019a7a5SEric W. Biederman 		goto err_linkops;
236979d17604SPavel Emelyanov 	}
237079d17604SPavel Emelyanov 
23711da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
237279d17604SPavel Emelyanov 	if (ret) {
23736b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
237479d17604SPavel Emelyanov 		goto err_misc;
237579d17604SPavel Emelyanov 	}
237679d17604SPavel Emelyanov 	return  0;
237779d17604SPavel Emelyanov err_misc:
2378f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2379f019a7a5SEric W. Biederman err_linkops:
23801da177e4SLinus Torvalds 	return ret;
23811da177e4SLinus Torvalds }
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds static void tun_cleanup(void)
23841da177e4SLinus Torvalds {
23851da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2386f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
238905c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
239005c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
239105c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
239205c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
239305c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
239405c2828cSMichael S. Tsirkin {
23956e914fc7SJason Wang 	struct tun_file *tfile;
239605c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
239705c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
23986e914fc7SJason Wang 	tfile = file->private_data;
23996e914fc7SJason Wang 	if (!tfile)
240005c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
240154f968d6SJason Wang 	return &tfile->socket;
240205c2828cSMichael S. Tsirkin }
240305c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
240405c2828cSMichael S. Tsirkin 
24051da177e4SLinus Torvalds module_init(tun_init);
24061da177e4SLinus Torvalds module_exit(tun_cleanup);
24071da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
24081da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
24091da177e4SLinus Torvalds MODULE_LICENSE("GPL");
24101da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2411578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2412