xref: /openbmc/linux/drivers/net/tun.c (revision eb0fb363f920e4d8c70d30b712f9dd433271218c)
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>
631da177e4SLinus Torvalds #include <linux/crc32.h>
64d647a591SPavel Emelyanov #include <linux/nsproxy.h>
65f43798c2SRusty Russell #include <linux/virtio_net.h>
6699405162SMichael S. Tsirkin #include <linux/rcupdate.h>
67881d966bSEric W. Biederman #include <net/net_namespace.h>
6879d17604SPavel Emelyanov #include <net/netns/generic.h>
69f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
7033dccbb0SHerbert Xu #include <net/sock.h>
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #include <asm/uaccess.h>
731da177e4SLinus Torvalds 
7414daa021SRusty Russell /* Uncomment to enable debugging */
7514daa021SRusty Russell /* #define TUN_DEBUG 1 */
7614daa021SRusty Russell 
771da177e4SLinus Torvalds #ifdef TUN_DEBUG
781da177e4SLinus Torvalds static int debug;
7914daa021SRusty Russell 
806b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
816b8a66eeSJoe Perches do {								\
826b8a66eeSJoe Perches 	if (tun->debug)						\
836b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
846b8a66eeSJoe Perches } while (0)
856b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
866b8a66eeSJoe Perches do {								\
876b8a66eeSJoe Perches 	if (debug == 2)						\
886b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
896b8a66eeSJoe Perches } while (0)
9014daa021SRusty Russell #else
916b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
926b8a66eeSJoe Perches do {								\
936b8a66eeSJoe Perches 	if (0)							\
946b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
956b8a66eeSJoe Perches } while (0)
966b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
976b8a66eeSJoe Perches do {								\
986b8a66eeSJoe Perches 	if (0)							\
996b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
1006b8a66eeSJoe Perches } while (0)
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
1030690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128
1040690899bSMichael S. Tsirkin 
105f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
106f271b2ccSMax Krasnyansky struct tap_filter {
107f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
108f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
109f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
110f271b2ccSMax Krasnyansky };
111f271b2ccSMax Krasnyansky 
112c8d68e6bSJason Wang /* 1024 is probably a high enough limit: modern hypervisors seem to support on
113c8d68e6bSJason Wang  * the order of 100-200 CPUs so this leaves us some breathing space if we want
114c8d68e6bSJason Wang  * to match a queue per guest CPU.
115c8d68e6bSJason Wang  */
116c8d68e6bSJason Wang #define MAX_TAP_QUEUES 1024
117c8d68e6bSJason Wang 
11896442e42SJason Wang #define TUN_FLOW_EXPIRE (3 * HZ)
11996442e42SJason Wang 
12054f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
12154f968d6SJason Wang  * also contains all socket related strctures (except sock_fprog and tap_filter)
12254f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
12354f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
12436fe8c09SRami Rosen  * netdevice not for a specific queue (at least I didn't see the requirement for
12554f968d6SJason Wang  * this).
1266e914fc7SJason Wang  *
1276e914fc7SJason Wang  * RCU usage:
12836fe8c09SRami Rosen  * The tun_file and tun_struct are loosely coupled, the pointer from one to the
1296e914fc7SJason Wang  * other can only be read while rcu_read_lock or rtnl_lock is held.
13054f968d6SJason Wang  */
131631ab46bSEric W. Biederman struct tun_file {
13254f968d6SJason Wang 	struct sock sk;
13354f968d6SJason Wang 	struct socket socket;
13454f968d6SJason Wang 	struct socket_wq wq;
1356e914fc7SJason Wang 	struct tun_struct __rcu *tun;
13636b50babSEric W. Biederman 	struct net *net;
13754f968d6SJason Wang 	struct fasync_struct *fasync;
13854f968d6SJason Wang 	/* only used for fasnyc */
13954f968d6SJason Wang 	unsigned int flags;
140c8d68e6bSJason Wang 	u16 queue_index;
141631ab46bSEric W. Biederman };
142631ab46bSEric W. Biederman 
14396442e42SJason Wang struct tun_flow_entry {
14496442e42SJason Wang 	struct hlist_node hash_link;
14596442e42SJason Wang 	struct rcu_head rcu;
14696442e42SJason Wang 	struct tun_struct *tun;
14796442e42SJason Wang 
14896442e42SJason Wang 	u32 rxhash;
14996442e42SJason Wang 	int queue_index;
15096442e42SJason Wang 	unsigned long updated;
15196442e42SJason Wang };
15296442e42SJason Wang 
15396442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024
15496442e42SJason Wang 
15554f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
15636fe8c09SRami Rosen  * device, socket filter, sndbuf and vnet header size were restore when the
15754f968d6SJason Wang  * file were attached to a persist device.
15854f968d6SJason Wang  */
15914daa021SRusty Russell struct tun_struct {
160c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
161c8d68e6bSJason Wang 	unsigned int            numqueues;
162f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1630625c883SEric W. Biederman 	kuid_t			owner;
1640625c883SEric W. Biederman 	kgid_t			group;
16514daa021SRusty Russell 
16614daa021SRusty Russell 	struct net_device	*dev;
167c8f44affSMichał Mirosław 	netdev_features_t	set_features;
16888255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
16988255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
170d9d52b51SMichael S. Tsirkin 
171d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
17254f968d6SJason Wang 	int			sndbuf;
17354f968d6SJason Wang 	struct tap_filter	txflt;
17454f968d6SJason Wang 	struct sock_fprog	fprog;
17554f968d6SJason Wang 	/* protected by rtnl lock */
17654f968d6SJason Wang 	bool			filter_attached;
17714daa021SRusty Russell #ifdef TUN_DEBUG
17814daa021SRusty Russell 	int debug;
17914daa021SRusty Russell #endif
18096442e42SJason Wang 	spinlock_t lock;
18196442e42SJason Wang 	struct kmem_cache *flow_cache;
18296442e42SJason Wang 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
18396442e42SJason Wang 	struct timer_list flow_gc_timer;
18496442e42SJason Wang 	unsigned long ageing_time;
18514daa021SRusty Russell };
18614daa021SRusty Russell 
18796442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash)
18896442e42SJason Wang {
18996442e42SJason Wang 	return rxhash & 0x3ff;
19096442e42SJason Wang }
19196442e42SJason Wang 
19296442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
19396442e42SJason Wang {
19496442e42SJason Wang 	struct tun_flow_entry *e;
19596442e42SJason Wang 	struct hlist_node *n;
19696442e42SJason Wang 
19796442e42SJason Wang 	hlist_for_each_entry_rcu(e, n, head, hash_link) {
19896442e42SJason Wang 		if (e->rxhash == rxhash)
19996442e42SJason Wang 			return e;
20096442e42SJason Wang 	}
20196442e42SJason Wang 	return NULL;
20296442e42SJason Wang }
20396442e42SJason Wang 
20496442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
20596442e42SJason Wang 					      struct hlist_head *head,
20696442e42SJason Wang 					      u32 rxhash, u16 queue_index)
20796442e42SJason Wang {
20896442e42SJason Wang 	struct tun_flow_entry *e = kmem_cache_alloc(tun->flow_cache,
20996442e42SJason Wang 						    GFP_ATOMIC);
21096442e42SJason Wang 	if (e) {
21196442e42SJason Wang 		tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
21296442e42SJason Wang 			  rxhash, queue_index);
21396442e42SJason Wang 		e->updated = jiffies;
21496442e42SJason Wang 		e->rxhash = rxhash;
21596442e42SJason Wang 		e->queue_index = queue_index;
21696442e42SJason Wang 		e->tun = tun;
21796442e42SJason Wang 		hlist_add_head_rcu(&e->hash_link, head);
21896442e42SJason Wang 	}
21996442e42SJason Wang 	return e;
22096442e42SJason Wang }
22196442e42SJason Wang 
22296442e42SJason Wang static void tun_flow_free(struct rcu_head *head)
22396442e42SJason Wang {
22496442e42SJason Wang 	struct tun_flow_entry *e
22596442e42SJason Wang 		= container_of(head, struct tun_flow_entry, rcu);
22696442e42SJason Wang 	kmem_cache_free(e->tun->flow_cache, e);
22796442e42SJason Wang }
22896442e42SJason Wang 
22996442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
23096442e42SJason Wang {
23196442e42SJason Wang 	tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
23296442e42SJason Wang 		  e->rxhash, e->queue_index);
23396442e42SJason Wang 	hlist_del_rcu(&e->hash_link);
23496442e42SJason Wang 	call_rcu(&e->rcu, tun_flow_free);
23596442e42SJason Wang }
23696442e42SJason Wang 
23796442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun)
23896442e42SJason Wang {
23996442e42SJason Wang 	int i;
24096442e42SJason Wang 
24196442e42SJason Wang 	spin_lock_bh(&tun->lock);
24296442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
24396442e42SJason Wang 		struct tun_flow_entry *e;
24496442e42SJason Wang 		struct hlist_node *h, *n;
24596442e42SJason Wang 
24696442e42SJason Wang 		hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link)
24796442e42SJason Wang 			tun_flow_delete(tun, e);
24896442e42SJason Wang 	}
24996442e42SJason Wang 	spin_unlock_bh(&tun->lock);
25096442e42SJason Wang }
25196442e42SJason Wang 
25296442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
25396442e42SJason Wang {
25496442e42SJason Wang 	int i;
25596442e42SJason Wang 
25696442e42SJason Wang 	spin_lock_bh(&tun->lock);
25796442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
25896442e42SJason Wang 		struct tun_flow_entry *e;
25996442e42SJason Wang 		struct hlist_node *h, *n;
26096442e42SJason Wang 
26196442e42SJason Wang 		hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) {
26296442e42SJason Wang 			if (e->queue_index == queue_index)
26396442e42SJason Wang 				tun_flow_delete(tun, e);
26496442e42SJason Wang 		}
26596442e42SJason Wang 	}
26696442e42SJason Wang 	spin_unlock_bh(&tun->lock);
26796442e42SJason Wang }
26896442e42SJason Wang 
26996442e42SJason Wang static void tun_flow_cleanup(unsigned long data)
27096442e42SJason Wang {
27196442e42SJason Wang 	struct tun_struct *tun = (struct tun_struct *)data;
27296442e42SJason Wang 	unsigned long delay = tun->ageing_time;
27396442e42SJason Wang 	unsigned long next_timer = jiffies + delay;
27496442e42SJason Wang 	unsigned long count = 0;
27596442e42SJason Wang 	int i;
27696442e42SJason Wang 
27796442e42SJason Wang 	tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
27896442e42SJason Wang 
27996442e42SJason Wang 	spin_lock_bh(&tun->lock);
28096442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
28196442e42SJason Wang 		struct tun_flow_entry *e;
28296442e42SJason Wang 		struct hlist_node *h, *n;
28396442e42SJason Wang 
28496442e42SJason Wang 		hlist_for_each_entry_safe(e, h, n, &tun->flows[i], hash_link) {
28596442e42SJason Wang 			unsigned long this_timer;
28696442e42SJason Wang 			count++;
28796442e42SJason Wang 			this_timer = e->updated + delay;
28896442e42SJason Wang 			if (time_before_eq(this_timer, jiffies))
28996442e42SJason Wang 				tun_flow_delete(tun, e);
29096442e42SJason Wang 			else if (time_before(this_timer, next_timer))
29196442e42SJason Wang 				next_timer = this_timer;
29296442e42SJason Wang 		}
29396442e42SJason Wang 	}
29496442e42SJason Wang 
29596442e42SJason Wang 	if (count)
29696442e42SJason Wang 		mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
29796442e42SJason Wang 	spin_unlock_bh(&tun->lock);
29896442e42SJason Wang }
29996442e42SJason Wang 
30096442e42SJason Wang static void tun_flow_update(struct tun_struct *tun, struct sk_buff *skb,
30196442e42SJason Wang 			    u16 queue_index)
30296442e42SJason Wang {
30396442e42SJason Wang 	struct hlist_head *head;
30496442e42SJason Wang 	struct tun_flow_entry *e;
30596442e42SJason Wang 	unsigned long delay = tun->ageing_time;
30696442e42SJason Wang 	u32 rxhash = skb_get_rxhash(skb);
30796442e42SJason Wang 
30896442e42SJason Wang 	if (!rxhash)
30996442e42SJason Wang 		return;
31096442e42SJason Wang 	else
31196442e42SJason Wang 		head = &tun->flows[tun_hashfn(rxhash)];
31296442e42SJason Wang 
31396442e42SJason Wang 	rcu_read_lock();
31496442e42SJason Wang 
31596442e42SJason Wang 	if (tun->numqueues == 1)
31696442e42SJason Wang 		goto unlock;
31796442e42SJason Wang 
31896442e42SJason Wang 	e = tun_flow_find(head, rxhash);
31996442e42SJason Wang 	if (likely(e)) {
32096442e42SJason Wang 		/* TODO: keep queueing to old queue until it's empty? */
32196442e42SJason Wang 		e->queue_index = queue_index;
32296442e42SJason Wang 		e->updated = jiffies;
32396442e42SJason Wang 	} else {
32496442e42SJason Wang 		spin_lock_bh(&tun->lock);
32596442e42SJason Wang 		if (!tun_flow_find(head, rxhash))
32696442e42SJason Wang 			tun_flow_create(tun, head, rxhash, queue_index);
32796442e42SJason Wang 
32896442e42SJason Wang 		if (!timer_pending(&tun->flow_gc_timer))
32996442e42SJason Wang 			mod_timer(&tun->flow_gc_timer,
33096442e42SJason Wang 				  round_jiffies_up(jiffies + delay));
33196442e42SJason Wang 		spin_unlock_bh(&tun->lock);
33296442e42SJason Wang 	}
33396442e42SJason Wang 
33496442e42SJason Wang unlock:
33596442e42SJason Wang 	rcu_read_unlock();
33696442e42SJason Wang }
33796442e42SJason Wang 
338c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
339c8d68e6bSJason Wang  * we do not check rxq no. is becuase some cards(e.g 82599), chooses
340c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
341c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
342c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
343c8d68e6bSJason Wang  * hope the rxq no. may help here.
344c8d68e6bSJason Wang  */
345c8d68e6bSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
346c8d68e6bSJason Wang {
347c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
34896442e42SJason Wang 	struct tun_flow_entry *e;
349c8d68e6bSJason Wang 	u32 txq = 0;
350c8d68e6bSJason Wang 	u32 numqueues = 0;
351c8d68e6bSJason Wang 
352c8d68e6bSJason Wang 	rcu_read_lock();
353c8d68e6bSJason Wang 	numqueues = tun->numqueues;
354c8d68e6bSJason Wang 
355c8d68e6bSJason Wang 	txq = skb_get_rxhash(skb);
356c8d68e6bSJason Wang 	if (txq) {
35796442e42SJason Wang 		e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
35896442e42SJason Wang 		if (e)
35996442e42SJason Wang 			txq = e->queue_index;
36096442e42SJason Wang 		else
361c8d68e6bSJason Wang 			/* use multiply and shift instead of expensive divide */
362c8d68e6bSJason Wang 			txq = ((u64)txq * numqueues) >> 32;
363c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
364c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
365c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
366c8d68e6bSJason Wang 			txq -= numqueues;
367c8d68e6bSJason Wang 	}
368c8d68e6bSJason Wang 
369c8d68e6bSJason Wang 	rcu_read_unlock();
370c8d68e6bSJason Wang 	return txq;
371c8d68e6bSJason Wang }
372c8d68e6bSJason Wang 
373cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
374cde8b15fSJason Wang {
375cde8b15fSJason Wang 	const struct cred *cred = current_cred();
376c260b772SEric W. Biederman 	struct net *net = dev_net(tun->dev);
377cde8b15fSJason Wang 
378cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
379cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
380c260b772SEric W. Biederman 		!ns_capable(net->user_ns, CAP_NET_ADMIN);
381cde8b15fSJason Wang }
382cde8b15fSJason Wang 
383c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
384c8d68e6bSJason Wang {
385c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
386c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
387c8d68e6bSJason Wang }
388c8d68e6bSJason Wang 
389c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
390c8d68e6bSJason Wang {
391c8d68e6bSJason Wang 	struct tun_file *ntfile;
392c8d68e6bSJason Wang 	struct tun_struct *tun;
393c8d68e6bSJason Wang 	struct net_device *dev;
394c8d68e6bSJason Wang 
395c8d68e6bSJason Wang 	tun = rcu_dereference_protected(tfile->tun,
396c8d68e6bSJason Wang 					lockdep_rtnl_is_held());
397c8d68e6bSJason Wang 	if (tun) {
398c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
399c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
400c8d68e6bSJason Wang 		dev = tun->dev;
401c8d68e6bSJason Wang 
402c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
403c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
404c8d68e6bSJason Wang 		rcu_assign_pointer(tfile->tun, NULL);
405c8d68e6bSJason Wang 		ntfile = rcu_dereference_protected(tun->tfiles[index],
406c8d68e6bSJason Wang 						   lockdep_rtnl_is_held());
407c8d68e6bSJason Wang 		ntfile->queue_index = index;
408c8d68e6bSJason Wang 
409c8d68e6bSJason Wang 		--tun->numqueues;
410c8d68e6bSJason Wang 		sock_put(&tfile->sk);
411c8d68e6bSJason Wang 
412c8d68e6bSJason Wang 		synchronize_net();
41396442e42SJason Wang 		tun_flow_delete_by_queue(tun, tun->numqueues + 1);
414c8d68e6bSJason Wang 		/* Drop read queue */
415c8d68e6bSJason Wang 		skb_queue_purge(&tfile->sk.sk_receive_queue);
416c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
417c8d68e6bSJason Wang 
418c8d68e6bSJason Wang 		if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
419c8d68e6bSJason Wang 			if (dev->reg_state == NETREG_REGISTERED)
420c8d68e6bSJason Wang 				unregister_netdevice(dev);
421c8d68e6bSJason Wang 	}
422c8d68e6bSJason Wang 
423c8d68e6bSJason Wang 	if (clean) {
424c8d68e6bSJason Wang 		BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
425c8d68e6bSJason Wang 				 &tfile->socket.flags));
426c8d68e6bSJason Wang 		sk_release_kernel(&tfile->sk);
427c8d68e6bSJason Wang 	}
428c8d68e6bSJason Wang }
429c8d68e6bSJason Wang 
430c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
431c8d68e6bSJason Wang {
432c8d68e6bSJason Wang 	rtnl_lock();
433c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
434c8d68e6bSJason Wang 	rtnl_unlock();
435c8d68e6bSJason Wang }
436c8d68e6bSJason Wang 
437c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
438c8d68e6bSJason Wang {
439c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
440c8d68e6bSJason Wang 	struct tun_file *tfile;
441c8d68e6bSJason Wang 	int i, n = tun->numqueues;
442c8d68e6bSJason Wang 
443c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
444c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
445c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
446c8d68e6bSJason Wang 		BUG_ON(!tfile);
447c8d68e6bSJason Wang 		wake_up_all(&tfile->wq.wait);
448c8d68e6bSJason Wang 		rcu_assign_pointer(tfile->tun, NULL);
449c8d68e6bSJason Wang 		--tun->numqueues;
450c8d68e6bSJason Wang 	}
451c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
452c8d68e6bSJason Wang 
453c8d68e6bSJason Wang 	synchronize_net();
454c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
455c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
456c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
457c8d68e6bSJason Wang 		/* Drop read queue */
458c8d68e6bSJason Wang 		skb_queue_purge(&tfile->sk.sk_receive_queue);
459c8d68e6bSJason Wang 		sock_put(&tfile->sk);
460c8d68e6bSJason Wang 	}
461c8d68e6bSJason Wang }
462c8d68e6bSJason Wang 
463a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
464a7385ba2SEric W. Biederman {
465631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
46638231b7aSEric W. Biederman 	int err;
467a7385ba2SEric W. Biederman 
46838231b7aSEric W. Biederman 	err = -EINVAL;
469c8d68e6bSJason Wang 	if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
47038231b7aSEric W. Biederman 		goto out;
47138231b7aSEric W. Biederman 
47238231b7aSEric W. Biederman 	err = -EBUSY;
473c8d68e6bSJason Wang 	if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
474c8d68e6bSJason Wang 		goto out;
475c8d68e6bSJason Wang 
476c8d68e6bSJason Wang 	err = -E2BIG;
477c8d68e6bSJason Wang 	if (tun->numqueues == MAX_TAP_QUEUES)
47838231b7aSEric W. Biederman 		goto out;
47938231b7aSEric W. Biederman 
48038231b7aSEric W. Biederman 	err = 0;
48154f968d6SJason Wang 
482c8d68e6bSJason Wang 	/* Re-attach the filter to presist device */
48354f968d6SJason Wang 	if (tun->filter_attached == true) {
48454f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
48554f968d6SJason Wang 		if (!err)
48654f968d6SJason Wang 			goto out;
48754f968d6SJason Wang 	}
488c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
4896e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
490c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
49154f968d6SJason Wang 	sock_hold(&tfile->sk);
492c8d68e6bSJason Wang 	tun->numqueues++;
493c8d68e6bSJason Wang 
494c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
495c8d68e6bSJason Wang 
496c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
497c8d68e6bSJason Wang 	 * refcnt.
498c8d68e6bSJason Wang 	 */
499a7385ba2SEric W. Biederman 
50038231b7aSEric W. Biederman out:
50138231b7aSEric W. Biederman 	return err;
502a7385ba2SEric W. Biederman }
503a7385ba2SEric W. Biederman 
504631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
505631ab46bSEric W. Biederman {
5066e914fc7SJason Wang 	struct tun_struct *tun;
507c70f1829SEric W. Biederman 
5086e914fc7SJason Wang 	rcu_read_lock();
5096e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
5106e914fc7SJason Wang 	if (tun)
5116e914fc7SJason Wang 		dev_hold(tun->dev);
5126e914fc7SJason Wang 	rcu_read_unlock();
513c70f1829SEric W. Biederman 
514c70f1829SEric W. Biederman 	return tun;
515631ab46bSEric W. Biederman }
516631ab46bSEric W. Biederman 
517631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
518631ab46bSEric W. Biederman {
519631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
520631ab46bSEric W. Biederman }
521631ab46bSEric W. Biederman 
522631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
523631ab46bSEric W. Biederman {
5246e914fc7SJason Wang 	dev_put(tun->dev);
525631ab46bSEric W. Biederman }
526631ab46bSEric W. Biederman 
5276b8a66eeSJoe Perches /* TAP filtering */
528f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
529f271b2ccSMax Krasnyansky {
530f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
531f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
532f271b2ccSMax Krasnyansky }
533f271b2ccSMax Krasnyansky 
534f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
535f271b2ccSMax Krasnyansky {
536f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
537f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
538f271b2ccSMax Krasnyansky }
539f271b2ccSMax Krasnyansky 
540f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
541f271b2ccSMax Krasnyansky {
542f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
543f271b2ccSMax Krasnyansky 	struct tun_filter uf;
544f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
545f271b2ccSMax Krasnyansky 
546f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
547f271b2ccSMax Krasnyansky 		return -EFAULT;
548f271b2ccSMax Krasnyansky 
549f271b2ccSMax Krasnyansky 	if (!uf.count) {
550f271b2ccSMax Krasnyansky 		/* Disabled */
551f271b2ccSMax Krasnyansky 		filter->count = 0;
552f271b2ccSMax Krasnyansky 		return 0;
553f271b2ccSMax Krasnyansky 	}
554f271b2ccSMax Krasnyansky 
555f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
556f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
557f271b2ccSMax Krasnyansky 	if (!addr)
558f271b2ccSMax Krasnyansky 		return -ENOMEM;
559f271b2ccSMax Krasnyansky 
560f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
561f271b2ccSMax Krasnyansky 		err = -EFAULT;
562f271b2ccSMax Krasnyansky 		goto done;
563f271b2ccSMax Krasnyansky 	}
564f271b2ccSMax Krasnyansky 
565f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
566f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
567f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
568f271b2ccSMax Krasnyansky 	filter->count = 0;
569f271b2ccSMax Krasnyansky 	wmb();
570f271b2ccSMax Krasnyansky 
571f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
572f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
573f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
574f271b2ccSMax Krasnyansky 
575f271b2ccSMax Krasnyansky 	nexact = n;
576f271b2ccSMax Krasnyansky 
577cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
578cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
579f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
580cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
581cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
582cfbf84fcSAlex Williamson 			err = 0; /* no filter */
583cfbf84fcSAlex Williamson 			goto done;
584cfbf84fcSAlex Williamson 		}
585f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
586cfbf84fcSAlex Williamson 	}
587f271b2ccSMax Krasnyansky 
588f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
589f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
590f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
591f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
592f271b2ccSMax Krasnyansky 
593f271b2ccSMax Krasnyansky 	/* Now enable the filter */
594f271b2ccSMax Krasnyansky 	wmb();
595f271b2ccSMax Krasnyansky 	filter->count = nexact;
596f271b2ccSMax Krasnyansky 
597f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
598f271b2ccSMax Krasnyansky 	err = nexact;
599f271b2ccSMax Krasnyansky 
600f271b2ccSMax Krasnyansky done:
601f271b2ccSMax Krasnyansky 	kfree(addr);
602f271b2ccSMax Krasnyansky 	return err;
603f271b2ccSMax Krasnyansky }
604f271b2ccSMax Krasnyansky 
605f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
606f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
607f271b2ccSMax Krasnyansky {
608f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
609f271b2ccSMax Krasnyansky 	 * at this point. */
610f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
611f271b2ccSMax Krasnyansky 	int i;
612f271b2ccSMax Krasnyansky 
613f271b2ccSMax Krasnyansky 	/* Exact match */
614f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
6152e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
616f271b2ccSMax Krasnyansky 			return 1;
617f271b2ccSMax Krasnyansky 
618f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
619f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
620f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
621f271b2ccSMax Krasnyansky 
622f271b2ccSMax Krasnyansky 	return 0;
623f271b2ccSMax Krasnyansky }
624f271b2ccSMax Krasnyansky 
625f271b2ccSMax Krasnyansky /*
626f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
627f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
628f271b2ccSMax Krasnyansky  */
629f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
630f271b2ccSMax Krasnyansky {
631f271b2ccSMax Krasnyansky 	if (!filter->count)
632f271b2ccSMax Krasnyansky 		return 1;
633f271b2ccSMax Krasnyansky 
634f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
635f271b2ccSMax Krasnyansky }
636f271b2ccSMax Krasnyansky 
6371da177e4SLinus Torvalds /* Network device part of the driver */
6381da177e4SLinus Torvalds 
6397282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
6401da177e4SLinus Torvalds 
641c70f1829SEric W. Biederman /* Net device detach from fd. */
642c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
643c70f1829SEric W. Biederman {
644c8d68e6bSJason Wang 	tun_detach_all(dev);
645c70f1829SEric W. Biederman }
646c70f1829SEric W. Biederman 
6471da177e4SLinus Torvalds /* Net device open. */
6481da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
6491da177e4SLinus Torvalds {
650c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
6511da177e4SLinus Torvalds 	return 0;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds /* Net device close. */
6551da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
6561da177e4SLinus Torvalds {
657c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
6581da177e4SLinus Torvalds 	return 0;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /* Net device start xmit */
662424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
665c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
6666e914fc7SJason Wang 	struct tun_file *tfile;
6671da177e4SLinus Torvalds 
6686e914fc7SJason Wang 	rcu_read_lock();
669c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
670c8d68e6bSJason Wang 
6711da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
672c8d68e6bSJason Wang 	if (txq >= tun->numqueues)
6731da177e4SLinus Torvalds 		goto drop;
6741da177e4SLinus Torvalds 
6756e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
6766e914fc7SJason Wang 
677c8d68e6bSJason Wang 	BUG_ON(!tfile);
678c8d68e6bSJason Wang 
679f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
680f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
681f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
682f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
683f271b2ccSMax Krasnyansky 		goto drop;
684f271b2ccSMax Krasnyansky 
68554f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
68654f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
68799405162SMichael S. Tsirkin 		goto drop;
68899405162SMichael S. Tsirkin 
68936fe8c09SRami Rosen 	/* Limit the number of packets queued by dividing txq length with the
690c8d68e6bSJason Wang 	 * number of queues.
691c8d68e6bSJason Wang 	 */
69254f968d6SJason Wang 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
693c8d68e6bSJason Wang 			  >= dev->tx_queue_len / tun->numqueues){
6941da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
6951da177e4SLinus Torvalds 			/* Normal queueing mode. */
6961da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
697c8d68e6bSJason Wang 			netif_stop_subqueue(dev, txq);
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
7001da177e4SLinus Torvalds 			 * error is more appropriate. */
70109f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
7021da177e4SLinus Torvalds 		} else {
7031da177e4SLinus Torvalds 			/* Single queue mode.
7041da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
7051da177e4SLinus Torvalds 			goto drop;
7061da177e4SLinus Torvalds 		}
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 
7090110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
7100110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
711868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
712868eefebSMichael S. Tsirkin 		goto drop;
7130110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
7140110d6f2SMichael S. Tsirkin 
715f271b2ccSMax Krasnyansky 	/* Enqueue packet */
71654f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	/* Notify and wake up reader process */
71954f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
72054f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
72154f968d6SJason Wang 	wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
72205c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
7236e914fc7SJason Wang 
7246e914fc7SJason Wang 	rcu_read_unlock();
7256ed10654SPatrick McHardy 	return NETDEV_TX_OK;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds drop:
72809f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
729149d36f7SMichael S. Tsirkin 	skb_tx_error(skb);
7301da177e4SLinus Torvalds 	kfree_skb(skb);
7316e914fc7SJason Wang 	rcu_read_unlock();
7326ed10654SPatrick McHardy 	return NETDEV_TX_OK;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
735f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
7361da177e4SLinus Torvalds {
737f271b2ccSMax Krasnyansky 	/*
738f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
739f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
740f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
741f271b2ccSMax Krasnyansky 	 */
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7444885a504SEd Swierk #define MIN_MTU 68
7454885a504SEd Swierk #define MAX_MTU 65535
7464885a504SEd Swierk 
7474885a504SEd Swierk static int
7484885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
7494885a504SEd Swierk {
7504885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
7514885a504SEd Swierk 		return -EINVAL;
7524885a504SEd Swierk 	dev->mtu = new_mtu;
7534885a504SEd Swierk 	return 0;
7544885a504SEd Swierk }
7554885a504SEd Swierk 
756c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
757c8f44affSMichał Mirosław 	netdev_features_t features)
75888255375SMichał Mirosław {
75988255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
76088255375SMichał Mirosław 
76188255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
76288255375SMichał Mirosław }
763bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
764bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
765bebd097aSNeil Horman {
766bebd097aSNeil Horman 	/*
767bebd097aSNeil Horman 	 * Tun only receives frames when:
768bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
769bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
770bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
771bebd097aSNeil Horman 	 * never to have pending data when we poll for it
772bebd097aSNeil Horman 	 * so theres nothing to do here but return.
773bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
774bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
775bebd097aSNeil Horman 	 * still use netconsole
776bebd097aSNeil Horman 	 */
777bebd097aSNeil Horman 	return;
778bebd097aSNeil Horman }
779bebd097aSNeil Horman #endif
780758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
781c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
782758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
783758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
78400829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
785758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
78688255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
787c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
788bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
789bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
790bebd097aSNeil Horman #endif
791758e43b7SStephen Hemminger };
792758e43b7SStephen Hemminger 
793758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
794c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
795758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
796758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
79700829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
798758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
79988255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
800afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
801758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
802758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
803c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
804bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
805bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
806bebd097aSNeil Horman #endif
807758e43b7SStephen Hemminger };
808758e43b7SStephen Hemminger 
80996442e42SJason Wang static int tun_flow_init(struct tun_struct *tun)
81096442e42SJason Wang {
81196442e42SJason Wang 	int i;
81296442e42SJason Wang 
81396442e42SJason Wang 	tun->flow_cache = kmem_cache_create("tun_flow_cache",
81496442e42SJason Wang 					    sizeof(struct tun_flow_entry), 0, 0,
81596442e42SJason Wang 					    NULL);
81696442e42SJason Wang 	if (!tun->flow_cache)
81796442e42SJason Wang 		return -ENOMEM;
81896442e42SJason Wang 
81996442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
82096442e42SJason Wang 		INIT_HLIST_HEAD(&tun->flows[i]);
82196442e42SJason Wang 
82296442e42SJason Wang 	tun->ageing_time = TUN_FLOW_EXPIRE;
82396442e42SJason Wang 	setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
82496442e42SJason Wang 	mod_timer(&tun->flow_gc_timer,
82596442e42SJason Wang 		  round_jiffies_up(jiffies + tun->ageing_time));
82696442e42SJason Wang 
82796442e42SJason Wang 	return 0;
82896442e42SJason Wang }
82996442e42SJason Wang 
83096442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun)
83196442e42SJason Wang {
83296442e42SJason Wang 	del_timer_sync(&tun->flow_gc_timer);
83396442e42SJason Wang 	tun_flow_flush(tun);
83496442e42SJason Wang 
83596442e42SJason Wang 	/* Wait for completion of call_rcu()'s */
83696442e42SJason Wang 	rcu_barrier();
83796442e42SJason Wang 	kmem_cache_destroy(tun->flow_cache);
83896442e42SJason Wang }
83996442e42SJason Wang 
8401da177e4SLinus Torvalds /* Initialize net device. */
8411da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
8461da177e4SLinus Torvalds 	case TUN_TUN_DEV:
847758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
848758e43b7SStephen Hemminger 
8491da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
8501da177e4SLinus Torvalds 		dev->hard_header_len = 0;
8511da177e4SLinus Torvalds 		dev->addr_len = 0;
8521da177e4SLinus Torvalds 		dev->mtu = 1500;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		/* Zero header length */
8551da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
8561da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
8571da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
8581da177e4SLinus Torvalds 		break;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8617a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
8621da177e4SLinus Torvalds 		/* Ethernet TAP Device */
8631da177e4SLinus Torvalds 		ether_setup(dev);
864550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
86536226a8dSBrian Braunstein 
866f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
86736226a8dSBrian Braunstein 
8681da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
8691da177e4SLinus Torvalds 		break;
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds /* Character device part */
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds /* Poll */
8761da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
8771da177e4SLinus Torvalds {
878b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
879b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
8803c8a9c63SMariusz Kozlowski 	struct sock *sk;
88133dccbb0SHerbert Xu 	unsigned int mask = 0;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	if (!tun)
884eac9e902SEric W. Biederman 		return POLLERR;
8851da177e4SLinus Torvalds 
88654f968d6SJason Wang 	sk = tfile->socket.sk;
8873c8a9c63SMariusz Kozlowski 
8886b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
8891da177e4SLinus Torvalds 
89054f968d6SJason Wang 	poll_wait(file, &tfile->wq.wait, wait);
8911da177e4SLinus Torvalds 
89289f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
8931da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
8941da177e4SLinus Torvalds 
89533dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
89633dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
89733dccbb0SHerbert Xu 	     sock_writeable(sk)))
89833dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
89933dccbb0SHerbert Xu 
900c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
901c70f1829SEric W. Biederman 		mask = POLLERR;
902c70f1829SEric W. Biederman 
903631ab46bSEric W. Biederman 	tun_put(tun);
9041da177e4SLinus Torvalds 	return mask;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
907f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
908f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
90954f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
91033dccbb0SHerbert Xu 				     size_t prepad, size_t len,
91133dccbb0SHerbert Xu 				     size_t linear, int noblock)
912f42157cbSRusty Russell {
91354f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
914f42157cbSRusty Russell 	struct sk_buff *skb;
91533dccbb0SHerbert Xu 	int err;
916f42157cbSRusty Russell 
917f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
9180eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
91933dccbb0SHerbert Xu 		linear = len;
920f42157cbSRusty Russell 
92133dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
92233dccbb0SHerbert Xu 				   &err);
923f42157cbSRusty Russell 	if (!skb)
92433dccbb0SHerbert Xu 		return ERR_PTR(err);
925f42157cbSRusty Russell 
926f42157cbSRusty Russell 	skb_reserve(skb, prepad);
927f42157cbSRusty Russell 	skb_put(skb, linear);
92833dccbb0SHerbert Xu 	skb->data_len = len - linear;
92933dccbb0SHerbert Xu 	skb->len += len - linear;
930f42157cbSRusty Russell 
931f42157cbSRusty Russell 	return skb;
932f42157cbSRusty Russell }
933f42157cbSRusty Russell 
9340690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */
9350690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
9360690899bSMichael S. Tsirkin 				  int offset, size_t count)
9370690899bSMichael S. Tsirkin {
9380690899bSMichael S. Tsirkin 	int len = iov_length(from, count) - offset;
9390690899bSMichael S. Tsirkin 	int copy = skb_headlen(skb);
9400690899bSMichael S. Tsirkin 	int size, offset1 = 0;
9410690899bSMichael S. Tsirkin 	int i = 0;
9420690899bSMichael S. Tsirkin 
9430690899bSMichael S. Tsirkin 	/* Skip over from offset */
9440690899bSMichael S. Tsirkin 	while (count && (offset >= from->iov_len)) {
9450690899bSMichael S. Tsirkin 		offset -= from->iov_len;
9460690899bSMichael S. Tsirkin 		++from;
9470690899bSMichael S. Tsirkin 		--count;
9480690899bSMichael S. Tsirkin 	}
9490690899bSMichael S. Tsirkin 
9500690899bSMichael S. Tsirkin 	/* copy up to skb headlen */
9510690899bSMichael S. Tsirkin 	while (count && (copy > 0)) {
9520690899bSMichael S. Tsirkin 		size = min_t(unsigned int, copy, from->iov_len - offset);
9530690899bSMichael S. Tsirkin 		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
9540690899bSMichael S. Tsirkin 				   size))
9550690899bSMichael S. Tsirkin 			return -EFAULT;
9560690899bSMichael S. Tsirkin 		if (copy > size) {
9570690899bSMichael S. Tsirkin 			++from;
9580690899bSMichael S. Tsirkin 			--count;
9590690899bSMichael S. Tsirkin 			offset = 0;
9600690899bSMichael S. Tsirkin 		} else
9610690899bSMichael S. Tsirkin 			offset += size;
9620690899bSMichael S. Tsirkin 		copy -= size;
9630690899bSMichael S. Tsirkin 		offset1 += size;
9640690899bSMichael S. Tsirkin 	}
9650690899bSMichael S. Tsirkin 
9660690899bSMichael S. Tsirkin 	if (len == offset1)
9670690899bSMichael S. Tsirkin 		return 0;
9680690899bSMichael S. Tsirkin 
9690690899bSMichael S. Tsirkin 	while (count--) {
9700690899bSMichael S. Tsirkin 		struct page *page[MAX_SKB_FRAGS];
9710690899bSMichael S. Tsirkin 		int num_pages;
9720690899bSMichael S. Tsirkin 		unsigned long base;
9730690899bSMichael S. Tsirkin 		unsigned long truesize;
9740690899bSMichael S. Tsirkin 
9750690899bSMichael S. Tsirkin 		len = from->iov_len - offset;
9760690899bSMichael S. Tsirkin 		if (!len) {
9770690899bSMichael S. Tsirkin 			offset = 0;
9780690899bSMichael S. Tsirkin 			++from;
9790690899bSMichael S. Tsirkin 			continue;
9800690899bSMichael S. Tsirkin 		}
9810690899bSMichael S. Tsirkin 		base = (unsigned long)from->iov_base + offset;
9820690899bSMichael S. Tsirkin 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
9830690899bSMichael S. Tsirkin 		if (i + size > MAX_SKB_FRAGS)
9840690899bSMichael S. Tsirkin 			return -EMSGSIZE;
9850690899bSMichael S. Tsirkin 		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
9860690899bSMichael S. Tsirkin 		if (num_pages != size) {
9870690899bSMichael S. Tsirkin 			for (i = 0; i < num_pages; i++)
9880690899bSMichael S. Tsirkin 				put_page(page[i]);
9890690899bSMichael S. Tsirkin 			return -EFAULT;
9900690899bSMichael S. Tsirkin 		}
9910690899bSMichael S. Tsirkin 		truesize = size * PAGE_SIZE;
9920690899bSMichael S. Tsirkin 		skb->data_len += len;
9930690899bSMichael S. Tsirkin 		skb->len += len;
9940690899bSMichael S. Tsirkin 		skb->truesize += truesize;
9950690899bSMichael S. Tsirkin 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
9960690899bSMichael S. Tsirkin 		while (len) {
9970690899bSMichael S. Tsirkin 			int off = base & ~PAGE_MASK;
9980690899bSMichael S. Tsirkin 			int size = min_t(int, len, PAGE_SIZE - off);
9990690899bSMichael S. Tsirkin 			__skb_fill_page_desc(skb, i, page[i], off, size);
10000690899bSMichael S. Tsirkin 			skb_shinfo(skb)->nr_frags++;
10010690899bSMichael S. Tsirkin 			/* increase sk_wmem_alloc */
10020690899bSMichael S. Tsirkin 			base += size;
10030690899bSMichael S. Tsirkin 			len -= size;
10040690899bSMichael S. Tsirkin 			i++;
10050690899bSMichael S. Tsirkin 		}
10060690899bSMichael S. Tsirkin 		offset = 0;
10070690899bSMichael S. Tsirkin 		++from;
10080690899bSMichael S. Tsirkin 	}
10090690899bSMichael S. Tsirkin 	return 0;
10100690899bSMichael S. Tsirkin }
10110690899bSMichael S. Tsirkin 
10121da177e4SLinus Torvalds /* Get packet from user space buffer */
101354f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
101454f968d6SJason Wang 			    void *msg_control, const struct iovec *iv,
101554f968d6SJason Wang 			    size_t total_len, size_t count, int noblock)
10161da177e4SLinus Torvalds {
101709640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
10181da177e4SLinus Torvalds 	struct sk_buff *skb;
10190690899bSMichael S. Tsirkin 	size_t len = total_len, align = NET_SKB_PAD;
1020f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
10216f26c9a7SMichael S. Tsirkin 	int offset = 0;
10220690899bSMichael S. Tsirkin 	int copylen;
10230690899bSMichael S. Tsirkin 	bool zerocopy = false;
10240690899bSMichael S. Tsirkin 	int err;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
10270690899bSMichael S. Tsirkin 		if ((len -= sizeof(pi)) > total_len)
10281da177e4SLinus Torvalds 			return -EINVAL;
10291da177e4SLinus Torvalds 
10306f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
10311da177e4SLinus Torvalds 			return -EFAULT;
10326f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds 
1035f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
10360690899bSMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > total_len)
1037f43798c2SRusty Russell 			return -EINVAL;
1038f43798c2SRusty Russell 
10396f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
1040f43798c2SRusty Russell 			return -EFAULT;
1041f43798c2SRusty Russell 
10424909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
10434909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
10444909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
10454909122fSHerbert Xu 
1046f43798c2SRusty Russell 		if (gso.hdr_len > len)
1047f43798c2SRusty Russell 			return -EINVAL;
1048d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
1049f43798c2SRusty Russell 	}
1050f43798c2SRusty Russell 
1051e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
1052a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
10530eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
10540eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
1055e01bf1c8SRusty Russell 			return -EINVAL;
1056e01bf1c8SRusty Russell 	}
10571da177e4SLinus Torvalds 
10580690899bSMichael S. Tsirkin 	if (msg_control)
10590690899bSMichael S. Tsirkin 		zerocopy = true;
10600690899bSMichael S. Tsirkin 
10610690899bSMichael S. Tsirkin 	if (zerocopy) {
10620690899bSMichael S. Tsirkin 		/* Userspace may produce vectors with count greater than
10630690899bSMichael S. Tsirkin 		 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
10640690899bSMichael S. Tsirkin 		 * to let the rest of data to be fit in the frags.
10650690899bSMichael S. Tsirkin 		 */
10660690899bSMichael S. Tsirkin 		if (count > MAX_SKB_FRAGS) {
10670690899bSMichael S. Tsirkin 			copylen = iov_length(iv, count - MAX_SKB_FRAGS);
10680690899bSMichael S. Tsirkin 			if (copylen < offset)
10690690899bSMichael S. Tsirkin 				copylen = 0;
10700690899bSMichael S. Tsirkin 			else
10710690899bSMichael S. Tsirkin 				copylen -= offset;
10720690899bSMichael S. Tsirkin 		} else
10730690899bSMichael S. Tsirkin 				copylen = 0;
10740690899bSMichael S. Tsirkin 		/* There are 256 bytes to be copied in skb, so there is enough
10750690899bSMichael S. Tsirkin 		 * room for skb expand head in case it is used.
10760690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
10770690899bSMichael S. Tsirkin 		 */
10780690899bSMichael S. Tsirkin 		if (copylen < gso.hdr_len)
10790690899bSMichael S. Tsirkin 			copylen = gso.hdr_len;
10800690899bSMichael S. Tsirkin 		if (!copylen)
10810690899bSMichael S. Tsirkin 			copylen = GOODCOPY_LEN;
10820690899bSMichael S. Tsirkin 	} else
10830690899bSMichael S. Tsirkin 		copylen = len;
10840690899bSMichael S. Tsirkin 
108554f968d6SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
108633dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
108733dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
108809f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
108933dccbb0SHerbert Xu 		return PTR_ERR(skb);
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
10920690899bSMichael S. Tsirkin 	if (zerocopy)
10930690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
10940690899bSMichael S. Tsirkin 	else
10950690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
10960690899bSMichael S. Tsirkin 
10970690899bSMichael S. Tsirkin 	if (err) {
109809f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
10998f22757eSDave Jones 		kfree_skb(skb);
11001da177e4SLinus Torvalds 		return -EFAULT;
11018f22757eSDave Jones 	}
11021da177e4SLinus Torvalds 
1103f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1104f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
1105f43798c2SRusty Russell 					  gso.csum_offset)) {
1106f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1107f43798c2SRusty Russell 			kfree_skb(skb);
1108f43798c2SRusty Russell 			return -EINVAL;
1109f43798c2SRusty Russell 		}
111088255375SMichał Mirosław 	}
1111f43798c2SRusty Russell 
11121da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
11131da177e4SLinus Torvalds 	case TUN_TUN_DEV:
1114f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
1115f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
1116f09f7ee2SAng Way Chuang 			case 0x40:
1117f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
1118f09f7ee2SAng Way Chuang 				break;
1119f09f7ee2SAng Way Chuang 			case 0x60:
1120f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
1121f09f7ee2SAng Way Chuang 				break;
1122f09f7ee2SAng Way Chuang 			default:
1123f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
1124f09f7ee2SAng Way Chuang 				kfree_skb(skb);
1125f09f7ee2SAng Way Chuang 				return -EINVAL;
1126f09f7ee2SAng Way Chuang 			}
1127f09f7ee2SAng Way Chuang 		}
1128f09f7ee2SAng Way Chuang 
1129459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
11301da177e4SLinus Torvalds 		skb->protocol = pi.proto;
11314c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
11321da177e4SLinus Torvalds 		break;
11331da177e4SLinus Torvalds 	case TUN_TAP_DEV:
11341da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
11351da177e4SLinus Torvalds 		break;
11366403eab1SJoe Perches 	}
11371da177e4SLinus Torvalds 
1138f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1139f43798c2SRusty Russell 		pr_debug("GSO!\n");
1140f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1141f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
1142f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1143f43798c2SRusty Russell 			break;
1144f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
1145f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1146f43798c2SRusty Russell 			break;
1147e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
1148e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1149e36aa25aSSridhar Samudrala 			break;
1150f43798c2SRusty Russell 		default:
1151f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1152f43798c2SRusty Russell 			kfree_skb(skb);
1153f43798c2SRusty Russell 			return -EINVAL;
1154f43798c2SRusty Russell 		}
1155f43798c2SRusty Russell 
1156f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1157f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1158f43798c2SRusty Russell 
1159f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
1160f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
1161f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1162f43798c2SRusty Russell 			kfree_skb(skb);
1163f43798c2SRusty Russell 			return -EINVAL;
1164f43798c2SRusty Russell 		}
1165f43798c2SRusty Russell 
1166f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
1167f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1168f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
1169f43798c2SRusty Russell 	}
11701da177e4SLinus Torvalds 
11710690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
11720690899bSMichael S. Tsirkin 	if (zerocopy) {
11730690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
11740690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
11750690899bSMichael S. Tsirkin 	}
11760690899bSMichael S. Tsirkin 
11771da177e4SLinus Torvalds 	netif_rx_ni(skb);
11781da177e4SLinus Torvalds 
117909f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
118009f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
11811da177e4SLinus Torvalds 
118296442e42SJason Wang 	tun_flow_update(tun, skb, tfile->queue_index);
11830690899bSMichael S. Tsirkin 	return total_len;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
1186ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1187ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
11881da177e4SLinus Torvalds {
118933dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
1190ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
119154f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
1192631ab46bSEric W. Biederman 	ssize_t result;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	if (!tun)
11951da177e4SLinus Torvalds 		return -EBADFD;
11961da177e4SLinus Torvalds 
11976b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
11981da177e4SLinus Torvalds 
119954f968d6SJason Wang 	result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
120054f968d6SJason Wang 			      count, file->f_flags & O_NONBLOCK);
1201631ab46bSEric W. Biederman 
1202631ab46bSEric W. Biederman 	tun_put(tun);
1203631ab46bSEric W. Biederman 	return result;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds /* Put packet to the user space buffer */
12076f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
120854f968d6SJason Wang 			    struct tun_file *tfile,
12091da177e4SLinus Torvalds 			    struct sk_buff *skb,
121043b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
12131da177e4SLinus Torvalds 	ssize_t total = 0;
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
12161da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
12171da177e4SLinus Torvalds 			return -EINVAL;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 		if (len < skb->len) {
12201da177e4SLinus Torvalds 			/* Packet will be striped */
12211da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 
122443b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
12251da177e4SLinus Torvalds 			return -EFAULT;
12261da177e4SLinus Torvalds 		total += sizeof(pi);
12271da177e4SLinus Torvalds 	}
12281da177e4SLinus Torvalds 
1229f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
1230f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1231d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
1232f43798c2SRusty Russell 			return -EINVAL;
1233f43798c2SRusty Russell 
1234f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1235f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1236f43798c2SRusty Russell 
1237f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
1238f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
1239f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
1240f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1241f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1242f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1243f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1244e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
1245e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1246ef3db4a5SMichael S. Tsirkin 			else {
12476b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1248ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
1249ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
1250ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
1251ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1252ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1253ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
1254ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
1255ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1256ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1257ef3db4a5SMichael S. Tsirkin 			}
1258f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1259f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1260f43798c2SRusty Russell 		} else
1261f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1262f43798c2SRusty Russell 
1263f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1264f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
126555508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
1266f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
126710a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
126810a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1269f43798c2SRusty Russell 		} /* else everything is zero */
1270f43798c2SRusty Russell 
127143b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
127243b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
1273f43798c2SRusty Russell 			return -EFAULT;
1274d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
1275f43798c2SRusty Russell 	}
1276f43798c2SRusty Russell 
12771da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
12781da177e4SLinus Torvalds 
127943b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
128005c2828cSMichael S. Tsirkin 	total += skb->len;
12811da177e4SLinus Torvalds 
128209f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
128309f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	return total;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
128854f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
128905c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
129005c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
12931da177e4SLinus Torvalds 	struct sk_buff *skb;
129405c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
12951da177e4SLinus Torvalds 
12963872baf6SRami Rosen 	tun_debug(KERN_INFO, tun, "tun_do_read\n");
12971da177e4SLinus Torvalds 
129861a5ff15SAmos Kong 	if (unlikely(!noblock))
129954f968d6SJason Wang 		add_wait_queue(&tfile->wq.wait, &wait);
13001da177e4SLinus Torvalds 	while (len) {
13011da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 		/* Read frames from the queue */
130454f968d6SJason Wang 		if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
130505c2828cSMichael S. Tsirkin 			if (noblock) {
13061da177e4SLinus Torvalds 				ret = -EAGAIN;
13071da177e4SLinus Torvalds 				break;
13081da177e4SLinus Torvalds 			}
13091da177e4SLinus Torvalds 			if (signal_pending(current)) {
13101da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
13111da177e4SLinus Torvalds 				break;
13121da177e4SLinus Torvalds 			}
1313c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
1314c70f1829SEric W. Biederman 				ret = -EIO;
1315c70f1829SEric W. Biederman 				break;
1316c70f1829SEric W. Biederman 			}
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
13191da177e4SLinus Torvalds 			schedule();
13201da177e4SLinus Torvalds 			continue;
13211da177e4SLinus Torvalds 		}
1322c8d68e6bSJason Wang 		netif_wake_subqueue(tun->dev, tfile->queue_index);
13231da177e4SLinus Torvalds 
132454f968d6SJason Wang 		ret = tun_put_user(tun, tfile, skb, iv, len);
13251da177e4SLinus Torvalds 		kfree_skb(skb);
13261da177e4SLinus Torvalds 		break;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
133061a5ff15SAmos Kong 	if (unlikely(!noblock))
133154f968d6SJason Wang 		remove_wait_queue(&tfile->wq.wait, &wait);
13321da177e4SLinus Torvalds 
133305c2828cSMichael S. Tsirkin 	return ret;
133405c2828cSMichael S. Tsirkin }
133505c2828cSMichael S. Tsirkin 
133605c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
133705c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
133805c2828cSMichael S. Tsirkin {
133905c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
134005c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
134105c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
134205c2828cSMichael S. Tsirkin 	ssize_t len, ret;
134305c2828cSMichael S. Tsirkin 
134405c2828cSMichael S. Tsirkin 	if (!tun)
134505c2828cSMichael S. Tsirkin 		return -EBADFD;
134605c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
134705c2828cSMichael S. Tsirkin 	if (len < 0) {
134805c2828cSMichael S. Tsirkin 		ret = -EINVAL;
134905c2828cSMichael S. Tsirkin 		goto out;
135005c2828cSMichael S. Tsirkin 	}
135105c2828cSMichael S. Tsirkin 
135254f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, iv, len,
135354f968d6SJason Wang 			  file->f_flags & O_NONBLOCK);
135405c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
1355631ab46bSEric W. Biederman out:
1356631ab46bSEric W. Biederman 	tun_put(tun);
13571da177e4SLinus Torvalds 	return ret;
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
136096442e42SJason Wang static void tun_free_netdev(struct net_device *dev)
136196442e42SJason Wang {
136296442e42SJason Wang 	struct tun_struct *tun = netdev_priv(dev);
136396442e42SJason Wang 
136496442e42SJason Wang 	tun_flow_uninit(tun);
136596442e42SJason Wang 	free_netdev(dev);
136696442e42SJason Wang }
136796442e42SJason Wang 
13681da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
13691da177e4SLinus Torvalds {
13701da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
13711da177e4SLinus Torvalds 
13720625c883SEric W. Biederman 	tun->owner = INVALID_UID;
13730625c883SEric W. Biederman 	tun->group = INVALID_GID;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
137696442e42SJason Wang 	dev->destructor = tun_free_netdev;
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds 
1379f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1380f019a7a5SEric W. Biederman  * device with netlink.
1381f019a7a5SEric W. Biederman  */
1382f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1383f019a7a5SEric W. Biederman {
1384f019a7a5SEric W. Biederman 	return -EINVAL;
1385f019a7a5SEric W. Biederman }
1386f019a7a5SEric W. Biederman 
1387f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1388f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1389f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1390f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1391f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1392f019a7a5SEric W. Biederman };
1393f019a7a5SEric W. Biederman 
139433dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
139533dccbb0SHerbert Xu {
139654f968d6SJason Wang 	struct tun_file *tfile;
139743815482SEric Dumazet 	wait_queue_head_t *wqueue;
139833dccbb0SHerbert Xu 
139933dccbb0SHerbert Xu 	if (!sock_writeable(sk))
140033dccbb0SHerbert Xu 		return;
140133dccbb0SHerbert Xu 
140233dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
140333dccbb0SHerbert Xu 		return;
140433dccbb0SHerbert Xu 
140543815482SEric Dumazet 	wqueue = sk_sleep(sk);
140643815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
140743815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
140805c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1409c722c625SHerbert Xu 
141054f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
141154f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
141233dccbb0SHerbert Xu }
141333dccbb0SHerbert Xu 
141405c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
141505c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
141605c2828cSMichael S. Tsirkin {
141754f968d6SJason Wang 	int ret;
141854f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
141954f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
142054f968d6SJason Wang 
142154f968d6SJason Wang 	if (!tun)
142254f968d6SJason Wang 		return -EBADFD;
142354f968d6SJason Wang 	ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
14240690899bSMichael S. Tsirkin 			   m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
142554f968d6SJason Wang 	tun_put(tun);
142654f968d6SJason Wang 	return ret;
142705c2828cSMichael S. Tsirkin }
142805c2828cSMichael S. Tsirkin 
142954f968d6SJason Wang 
143005c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
143105c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
143205c2828cSMichael S. Tsirkin 		       int flags)
143305c2828cSMichael S. Tsirkin {
143454f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
143554f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
143605c2828cSMichael S. Tsirkin 	int ret;
143754f968d6SJason Wang 
143854f968d6SJason Wang 	if (!tun)
143954f968d6SJason Wang 		return -EBADFD;
144054f968d6SJason Wang 
144105c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
144205c2828cSMichael S. Tsirkin 		return -EINVAL;
144354f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
144405c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
144505c2828cSMichael S. Tsirkin 	if (ret > total_len) {
144605c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
144705c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
144805c2828cSMichael S. Tsirkin 	}
144954f968d6SJason Wang 	tun_put(tun);
145005c2828cSMichael S. Tsirkin 	return ret;
145105c2828cSMichael S. Tsirkin }
145205c2828cSMichael S. Tsirkin 
14531ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
14541ab5ecb9SStanislav Kinsbursky {
14551ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
14561ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
14571ab5ecb9SStanislav Kinsbursky 	return 0;
14581ab5ecb9SStanislav Kinsbursky }
14591ab5ecb9SStanislav Kinsbursky 
146005c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
146105c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
146205c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
146305c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
14641ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
146505c2828cSMichael S. Tsirkin };
146605c2828cSMichael S. Tsirkin 
146733dccbb0SHerbert Xu static struct proto tun_proto = {
146833dccbb0SHerbert Xu 	.name		= "tun",
146933dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
147054f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
147133dccbb0SHerbert Xu };
1472f019a7a5SEric W. Biederman 
1473980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1474980c9e8cSDavid Woodhouse {
1475980c9e8cSDavid Woodhouse 	int flags = 0;
1476980c9e8cSDavid Woodhouse 
1477980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1478980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1479980c9e8cSDavid Woodhouse 	else
1480980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1481980c9e8cSDavid Woodhouse 
1482980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1483980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1484980c9e8cSDavid Woodhouse 
1485980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1486980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1487980c9e8cSDavid Woodhouse 
1488980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1489980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1490980c9e8cSDavid Woodhouse 
1491c8d68e6bSJason Wang 	if (tun->flags & TUN_TAP_MQ)
1492c8d68e6bSJason Wang 		flags |= IFF_MULTI_QUEUE;
1493c8d68e6bSJason Wang 
1494980c9e8cSDavid Woodhouse 	return flags;
1495980c9e8cSDavid Woodhouse }
1496980c9e8cSDavid Woodhouse 
1497980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1498980c9e8cSDavid Woodhouse 			      char *buf)
1499980c9e8cSDavid Woodhouse {
1500980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1501980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1502980c9e8cSDavid Woodhouse }
1503980c9e8cSDavid Woodhouse 
1504980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1505980c9e8cSDavid Woodhouse 			      char *buf)
1506980c9e8cSDavid Woodhouse {
1507980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15080625c883SEric W. Biederman 	return uid_valid(tun->owner)?
15090625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15100625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
15110625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1512980c9e8cSDavid Woodhouse }
1513980c9e8cSDavid Woodhouse 
1514980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1515980c9e8cSDavid Woodhouse 			      char *buf)
1516980c9e8cSDavid Woodhouse {
1517980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15180625c883SEric W. Biederman 	return gid_valid(tun->group) ?
15190625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15200625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
15210625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1522980c9e8cSDavid Woodhouse }
1523980c9e8cSDavid Woodhouse 
1524980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1525980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1526980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1527980c9e8cSDavid Woodhouse 
1528d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
15291da177e4SLinus Torvalds {
15301da177e4SLinus Torvalds 	struct tun_struct *tun;
153154f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
15321da177e4SLinus Torvalds 	struct net_device *dev;
15331da177e4SLinus Torvalds 	int err;
15341da177e4SLinus Torvalds 
153574a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
153674a3e5a7SEric W. Biederman 	if (dev) {
1537f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1538f85ba780SDavid Woodhouse 			return -EBUSY;
153974a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
154074a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
154174a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
154274a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
154374a3e5a7SEric W. Biederman 		else
154474a3e5a7SEric W. Biederman 			return -EINVAL;
154574a3e5a7SEric W. Biederman 
1546cde8b15fSJason Wang 		if (tun_not_capable(tun))
15472b980dbdSPaul Moore 			return -EPERM;
154854f968d6SJason Wang 		err = security_tun_dev_attach(tfile->socket.sk);
15492b980dbdSPaul Moore 		if (err < 0)
15502b980dbdSPaul Moore 			return err;
15512b980dbdSPaul Moore 
1552a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1553a7385ba2SEric W. Biederman 		if (err < 0)
1554a7385ba2SEric W. Biederman 			return err;
155586a264abSDavid Howells 	}
15561da177e4SLinus Torvalds 	else {
15571da177e4SLinus Torvalds 		char *name;
15581da177e4SLinus Torvalds 		unsigned long flags = 0;
15591da177e4SLinus Torvalds 
1560c260b772SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1561ca6bb5d7SDavid Woodhouse 			return -EPERM;
15622b980dbdSPaul Moore 		err = security_tun_dev_create();
15632b980dbdSPaul Moore 		if (err < 0)
15642b980dbdSPaul Moore 			return err;
1565ca6bb5d7SDavid Woodhouse 
15661da177e4SLinus Torvalds 		/* Set dev type */
15671da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
15681da177e4SLinus Torvalds 			/* TUN device */
15691da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
15701da177e4SLinus Torvalds 			name = "tun%d";
15711da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
15721da177e4SLinus Torvalds 			/* TAP device */
15731da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
15741da177e4SLinus Torvalds 			name = "tap%d";
15751da177e4SLinus Torvalds 		} else
157636989b90SKusanagi Kouichi 			return -EINVAL;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 		if (*ifr->ifr_name)
15791da177e4SLinus Torvalds 			name = ifr->ifr_name;
15801da177e4SLinus Torvalds 
1581c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1582c8d68e6bSJason Wang 				       tun_setup,
1583c8d68e6bSJason Wang 				       MAX_TAP_QUEUES, MAX_TAP_QUEUES);
15841da177e4SLinus Torvalds 		if (!dev)
15851da177e4SLinus Torvalds 			return -ENOMEM;
15861da177e4SLinus Torvalds 
1587fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1588f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1589758e43b7SStephen Hemminger 
15901da177e4SLinus Torvalds 		tun = netdev_priv(dev);
15911da177e4SLinus Torvalds 		tun->dev = dev;
15921da177e4SLinus Torvalds 		tun->flags = flags;
1593f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1594d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
15951da177e4SLinus Torvalds 
159654f968d6SJason Wang 		tun->filter_attached = false;
159754f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
159833dccbb0SHerbert Xu 
159996442e42SJason Wang 		spin_lock_init(&tun->lock);
160096442e42SJason Wang 
160154f968d6SJason Wang 		security_tun_dev_post_create(&tfile->sk);
16022b980dbdSPaul Moore 
16031da177e4SLinus Torvalds 		tun_net_init(dev);
16041da177e4SLinus Torvalds 
160596442e42SJason Wang 		if (tun_flow_init(tun))
160696442e42SJason Wang 			goto err_free_dev;
160796442e42SJason Wang 
160888255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
160988255375SMichał Mirosław 			TUN_USER_FEATURES;
161088255375SMichał Mirosław 		dev->features = dev->hw_features;
161188255375SMichał Mirosław 
1612*eb0fb363SJason Wang 		err = tun_attach(tun, file);
1613*eb0fb363SJason Wang 		if (err < 0)
1614*eb0fb363SJason Wang 			goto err_free_dev;
1615*eb0fb363SJason Wang 
16161da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
16171da177e4SLinus Torvalds 		if (err < 0)
161854f968d6SJason Wang 			goto err_free_dev;
16199c3fea6aSHerbert Xu 
1620980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1621980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1622980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
16236b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1624980c9e8cSDavid Woodhouse 
1625*eb0fb363SJason Wang 		netif_carrier_on(tun->dev);
16261da177e4SLinus Torvalds 	}
16271da177e4SLinus Torvalds 
16286b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
16311da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1632a26af1e0SNathaniel Filardo 	else
1633a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
16361da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1637a26af1e0SNathaniel Filardo 	else
1638a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
16391da177e4SLinus Torvalds 
1640f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1641f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1642f43798c2SRusty Russell 	else
1643f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1644f43798c2SRusty Russell 
1645c8d68e6bSJason Wang 	if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1646c8d68e6bSJason Wang 		tun->flags |= TUN_TAP_MQ;
1647c8d68e6bSJason Wang 	else
1648c8d68e6bSJason Wang 		tun->flags &= ~TUN_TAP_MQ;
1649c8d68e6bSJason Wang 
1650e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1651e35259a9SMax Krasnyansky 	 * xoff state.
1652e35259a9SMax Krasnyansky 	 */
1653e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1654c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1655e35259a9SMax Krasnyansky 
16561da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
16571da177e4SLinus Torvalds 	return 0;
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds  err_free_dev:
16601da177e4SLinus Torvalds 	free_netdev(dev);
16611da177e4SLinus Torvalds 	return err;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
16649ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun,
1665876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1666e3b99556SMark McLoughlin {
16676b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1668e3b99556SMark McLoughlin 
1669e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1670e3b99556SMark McLoughlin 
1671980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1672e3b99556SMark McLoughlin 
1673e3b99556SMark McLoughlin }
1674e3b99556SMark McLoughlin 
16755228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
16765228ddc9SRusty Russell  * privs required. */
167788255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
16785228ddc9SRusty Russell {
1679c8f44affSMichał Mirosław 	netdev_features_t features = 0;
16805228ddc9SRusty Russell 
16815228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
168288255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
16835228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
16845228ddc9SRusty Russell 
16855228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
16865228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
16875228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
16885228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
16895228ddc9SRusty Russell 			}
16905228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
16915228ddc9SRusty Russell 				features |= NETIF_F_TSO;
16925228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
16935228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
16945228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
16955228ddc9SRusty Russell 		}
1696e36aa25aSSridhar Samudrala 
1697e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1698e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1699e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1700e36aa25aSSridhar Samudrala 		}
17015228ddc9SRusty Russell 	}
17025228ddc9SRusty Russell 
17035228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
17045228ddc9SRusty Russell 	 * trying to set them. */
17055228ddc9SRusty Russell 	if (arg)
17065228ddc9SRusty Russell 		return -EINVAL;
17075228ddc9SRusty Russell 
170888255375SMichał Mirosław 	tun->set_features = features;
170988255375SMichał Mirosław 	netdev_update_features(tun->dev);
17105228ddc9SRusty Russell 
17115228ddc9SRusty Russell 	return 0;
17125228ddc9SRusty Russell }
17135228ddc9SRusty Russell 
1714c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1715c8d68e6bSJason Wang {
1716c8d68e6bSJason Wang 	int i;
1717c8d68e6bSJason Wang 	struct tun_file *tfile;
1718c8d68e6bSJason Wang 
1719c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1720c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
1721c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
1722c8d68e6bSJason Wang 		sk_detach_filter(tfile->socket.sk);
1723c8d68e6bSJason Wang 	}
1724c8d68e6bSJason Wang 
1725c8d68e6bSJason Wang 	tun->filter_attached = false;
1726c8d68e6bSJason Wang }
1727c8d68e6bSJason Wang 
1728c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1729c8d68e6bSJason Wang {
1730c8d68e6bSJason Wang 	int i, ret = 0;
1731c8d68e6bSJason Wang 	struct tun_file *tfile;
1732c8d68e6bSJason Wang 
1733c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1734c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
1735c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
1736c8d68e6bSJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1737c8d68e6bSJason Wang 		if (ret) {
1738c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1739c8d68e6bSJason Wang 			return ret;
1740c8d68e6bSJason Wang 		}
1741c8d68e6bSJason Wang 	}
1742c8d68e6bSJason Wang 
1743c8d68e6bSJason Wang 	tun->filter_attached = true;
1744c8d68e6bSJason Wang 	return ret;
1745c8d68e6bSJason Wang }
1746c8d68e6bSJason Wang 
1747c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1748c8d68e6bSJason Wang {
1749c8d68e6bSJason Wang 	struct tun_file *tfile;
1750c8d68e6bSJason Wang 	int i;
1751c8d68e6bSJason Wang 
1752c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1753c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
1754c8d68e6bSJason Wang 						lockdep_rtnl_is_held());
1755c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1756c8d68e6bSJason Wang 	}
1757c8d68e6bSJason Wang }
1758c8d68e6bSJason Wang 
1759cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1760cde8b15fSJason Wang {
1761cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1762cde8b15fSJason Wang 	struct tun_struct *tun;
1763cde8b15fSJason Wang 	struct net_device *dev;
1764cde8b15fSJason Wang 	int ret = 0;
1765cde8b15fSJason Wang 
1766cde8b15fSJason Wang 	rtnl_lock();
1767cde8b15fSJason Wang 
1768cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
1769cde8b15fSJason Wang 		dev = __dev_get_by_name(tfile->net, ifr->ifr_name);
1770cde8b15fSJason Wang 		if (!dev) {
1771cde8b15fSJason Wang 			ret = -EINVAL;
1772cde8b15fSJason Wang 			goto unlock;
1773cde8b15fSJason Wang 		}
1774cde8b15fSJason Wang 
1775cde8b15fSJason Wang 		tun = netdev_priv(dev);
1776cde8b15fSJason Wang 		if (dev->netdev_ops != &tap_netdev_ops &&
1777cde8b15fSJason Wang 			dev->netdev_ops != &tun_netdev_ops)
1778cde8b15fSJason Wang 			ret = -EINVAL;
1779cde8b15fSJason Wang 		else if (tun_not_capable(tun))
1780cde8b15fSJason Wang 			ret = -EPERM;
1781cde8b15fSJason Wang 		else
1782cde8b15fSJason Wang 			ret = tun_attach(tun, file);
1783cde8b15fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE)
1784cde8b15fSJason Wang 		__tun_detach(tfile, false);
1785cde8b15fSJason Wang 	else
1786cde8b15fSJason Wang 		ret = -EINVAL;
1787cde8b15fSJason Wang 
1788cde8b15fSJason Wang unlock:
1789cde8b15fSJason Wang 	rtnl_unlock();
1790cde8b15fSJason Wang 	return ret;
1791cde8b15fSJason Wang }
1792cde8b15fSJason Wang 
179350857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
179450857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
17951da177e4SLinus Torvalds {
179636b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1797631ab46bSEric W. Biederman 	struct tun_struct *tun;
17981da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
17991da177e4SLinus Torvalds 	struct ifreq ifr;
18000625c883SEric W. Biederman 	kuid_t owner;
18010625c883SEric W. Biederman 	kgid_t group;
180233dccbb0SHerbert Xu 	int sndbuf;
1803d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1804f271b2ccSMax Krasnyansky 	int ret;
18051da177e4SLinus Torvalds 
1806cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
180750857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
18081da177e4SLinus Torvalds 			return -EFAULT;
18098bbb1813SDavid S. Miller 	} else {
1810a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
18118bbb1813SDavid S. Miller 	}
1812631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1813631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1814631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1815631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1816631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1817cde8b15fSJason Wang 				IFF_VNET_HDR | IFF_MULTI_QUEUE,
1818631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1819cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
1820cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
1821631ab46bSEric W. Biederman 
1822c8d68e6bSJason Wang 	ret = 0;
1823876bfd4dSHerbert Xu 	rtnl_lock();
1824876bfd4dSHerbert Xu 
182536b50babSEric W. Biederman 	tun = __tun_get(tfile);
18261da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
18271da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
18281da177e4SLinus Torvalds 
1829876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
18301da177e4SLinus Torvalds 
1831876bfd4dSHerbert Xu 		if (ret)
1832876bfd4dSHerbert Xu 			goto unlock;
18331da177e4SLinus Torvalds 
183450857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1835876bfd4dSHerbert Xu 			ret = -EFAULT;
1836876bfd4dSHerbert Xu 		goto unlock;
18371da177e4SLinus Torvalds 	}
18381da177e4SLinus Torvalds 
1839876bfd4dSHerbert Xu 	ret = -EBADFD;
18401da177e4SLinus Torvalds 	if (!tun)
1841876bfd4dSHerbert Xu 		goto unlock;
18421da177e4SLinus Torvalds 
18431e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
18441da177e4SLinus Torvalds 
1845631ab46bSEric W. Biederman 	ret = 0;
18461da177e4SLinus Torvalds 	switch (cmd) {
1847e3b99556SMark McLoughlin 	case TUNGETIFF:
18489ce99cf6SRami Rosen 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1849e3b99556SMark McLoughlin 
185050857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1851631ab46bSEric W. Biederman 			ret = -EFAULT;
1852e3b99556SMark McLoughlin 		break;
1853e3b99556SMark McLoughlin 
18541da177e4SLinus Torvalds 	case TUNSETNOCSUM:
18551da177e4SLinus Torvalds 		/* Disable/Enable checksum */
18561da177e4SLinus Torvalds 
185788255375SMichał Mirosław 		/* [unimplemented] */
185888255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
18596b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
18601da177e4SLinus Torvalds 		break;
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	case TUNSETPERSIST:
186354f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
186454f968d6SJason Wang 		 * module to prevent the module being unprobed.
186554f968d6SJason Wang 		 */
186654f968d6SJason Wang 		if (arg) {
18671da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
186854f968d6SJason Wang 			__module_get(THIS_MODULE);
186954f968d6SJason Wang 		} else {
18701da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
187154f968d6SJason Wang 			module_put(THIS_MODULE);
187254f968d6SJason Wang 		}
18731da177e4SLinus Torvalds 
18746b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
18756b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
18761da177e4SLinus Torvalds 		break;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	case TUNSETOWNER:
18791da177e4SLinus Torvalds 		/* Set owner of the device */
18800625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
18810625c883SEric W. Biederman 		if (!uid_valid(owner)) {
18820625c883SEric W. Biederman 			ret = -EINVAL;
18830625c883SEric W. Biederman 			break;
18840625c883SEric W. Biederman 		}
18850625c883SEric W. Biederman 		tun->owner = owner;
18861e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
18870625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
18881da177e4SLinus Torvalds 		break;
18891da177e4SLinus Torvalds 
18908c644623SGuido Guenther 	case TUNSETGROUP:
18918c644623SGuido Guenther 		/* Set group of the device */
18920625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
18930625c883SEric W. Biederman 		if (!gid_valid(group)) {
18940625c883SEric W. Biederman 			ret = -EINVAL;
18950625c883SEric W. Biederman 			break;
18960625c883SEric W. Biederman 		}
18970625c883SEric W. Biederman 		tun->group = group;
18981e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
18990625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
19008c644623SGuido Guenther 		break;
19018c644623SGuido Guenther 
1902ff4cc3acSMike Kershaw 	case TUNSETLINK:
1903ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1904ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
19056b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
19066b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
190748abfe05SDavid S. Miller 			ret = -EBUSY;
1908ff4cc3acSMike Kershaw 		} else {
1909ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
19106b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
19116b8a66eeSJoe Perches 				  tun->dev->type);
191248abfe05SDavid S. Miller 			ret = 0;
1913ff4cc3acSMike Kershaw 		}
1914631ab46bSEric W. Biederman 		break;
1915ff4cc3acSMike Kershaw 
19161da177e4SLinus Torvalds #ifdef TUN_DEBUG
19171da177e4SLinus Torvalds 	case TUNSETDEBUG:
19181da177e4SLinus Torvalds 		tun->debug = arg;
19191da177e4SLinus Torvalds 		break;
19201da177e4SLinus Torvalds #endif
19215228ddc9SRusty Russell 	case TUNSETOFFLOAD:
192288255375SMichał Mirosław 		ret = set_offload(tun, arg);
1923631ab46bSEric W. Biederman 		break;
19245228ddc9SRusty Russell 
1925f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1926f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1927631ab46bSEric W. Biederman 		ret = -EINVAL;
1928f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1929631ab46bSEric W. Biederman 			break;
1930c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1931631ab46bSEric W. Biederman 		break;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1934b595076aSUwe Kleine-König 		/* Get hw address */
1935f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1936f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
193750857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1938631ab46bSEric W. Biederman 			ret = -EFAULT;
1939631ab46bSEric W. Biederman 		break;
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1942f271b2ccSMax Krasnyansky 		/* Set hw address */
19436b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
19446b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
194540102371SKim B. Heino 
194640102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1947631ab46bSEric W. Biederman 		break;
194833dccbb0SHerbert Xu 
194933dccbb0SHerbert Xu 	case TUNGETSNDBUF:
195054f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
195133dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
195233dccbb0SHerbert Xu 			ret = -EFAULT;
195333dccbb0SHerbert Xu 		break;
195433dccbb0SHerbert Xu 
195533dccbb0SHerbert Xu 	case TUNSETSNDBUF:
195633dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
195733dccbb0SHerbert Xu 			ret = -EFAULT;
195833dccbb0SHerbert Xu 			break;
195933dccbb0SHerbert Xu 		}
196033dccbb0SHerbert Xu 
1961c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
1962c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
196333dccbb0SHerbert Xu 		break;
196433dccbb0SHerbert Xu 
1965d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1966d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1967d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1968d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1969d9d52b51SMichael S. Tsirkin 		break;
1970d9d52b51SMichael S. Tsirkin 
1971d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1972d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1973d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1974d9d52b51SMichael S. Tsirkin 			break;
1975d9d52b51SMichael S. Tsirkin 		}
1976d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1977d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1978d9d52b51SMichael S. Tsirkin 			break;
1979d9d52b51SMichael S. Tsirkin 		}
1980d9d52b51SMichael S. Tsirkin 
1981d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1982d9d52b51SMichael S. Tsirkin 		break;
1983d9d52b51SMichael S. Tsirkin 
198499405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
198599405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
198699405162SMichael S. Tsirkin 		ret = -EINVAL;
198799405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
198899405162SMichael S. Tsirkin 			break;
198999405162SMichael S. Tsirkin 		ret = -EFAULT;
199054f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
199199405162SMichael S. Tsirkin 			break;
199299405162SMichael S. Tsirkin 
1993c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
199499405162SMichael S. Tsirkin 		break;
199599405162SMichael S. Tsirkin 
199699405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
199799405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
199899405162SMichael S. Tsirkin 		ret = -EINVAL;
199999405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
200099405162SMichael S. Tsirkin 			break;
2001c8d68e6bSJason Wang 		ret = 0;
2002c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
200399405162SMichael S. Tsirkin 		break;
200499405162SMichael S. Tsirkin 
20051da177e4SLinus Torvalds 	default:
2006631ab46bSEric W. Biederman 		ret = -EINVAL;
2007631ab46bSEric W. Biederman 		break;
2008ee289b64SJoe Perches 	}
20091da177e4SLinus Torvalds 
2010876bfd4dSHerbert Xu unlock:
2011876bfd4dSHerbert Xu 	rtnl_unlock();
2012876bfd4dSHerbert Xu 	if (tun)
2013631ab46bSEric W. Biederman 		tun_put(tun);
2014631ab46bSEric W. Biederman 	return ret;
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
201750857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
201850857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
201950857e2aSArnd Bergmann {
202050857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
202150857e2aSArnd Bergmann }
202250857e2aSArnd Bergmann 
202350857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
202450857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
202550857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
202650857e2aSArnd Bergmann {
202750857e2aSArnd Bergmann 	switch (cmd) {
202850857e2aSArnd Bergmann 	case TUNSETIFF:
202950857e2aSArnd Bergmann 	case TUNGETIFF:
203050857e2aSArnd Bergmann 	case TUNSETTXFILTER:
203150857e2aSArnd Bergmann 	case TUNGETSNDBUF:
203250857e2aSArnd Bergmann 	case TUNSETSNDBUF:
203350857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
203450857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
203550857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
203650857e2aSArnd Bergmann 		break;
203750857e2aSArnd Bergmann 	default:
203850857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
203950857e2aSArnd Bergmann 		break;
204050857e2aSArnd Bergmann 	}
204150857e2aSArnd Bergmann 
204250857e2aSArnd Bergmann 	/*
204350857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
204450857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
204550857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
204650857e2aSArnd Bergmann 	 * contents.
204750857e2aSArnd Bergmann 	 */
204850857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
204950857e2aSArnd Bergmann }
205050857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
205150857e2aSArnd Bergmann 
20521da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
20531da177e4SLinus Torvalds {
205454f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
20551da177e4SLinus Torvalds 	int ret;
20561da177e4SLinus Torvalds 
205754f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
20589d319522SJonathan Corbet 		goto out;
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 	if (on) {
2061609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
20621da177e4SLinus Torvalds 		if (ret)
20639d319522SJonathan Corbet 			goto out;
206454f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
20651da177e4SLinus Torvalds 	} else
206654f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
20679d319522SJonathan Corbet 	ret = 0;
20689d319522SJonathan Corbet out:
20699d319522SJonathan Corbet 	return ret;
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
20731da177e4SLinus Torvalds {
2074631ab46bSEric W. Biederman 	struct tun_file *tfile;
2075deed49fbSThomas Gleixner 
20766b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2077631ab46bSEric W. Biederman 
207854f968d6SJason Wang 	tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
207954f968d6SJason Wang 					    &tun_proto);
2080631ab46bSEric W. Biederman 	if (!tfile)
2081631ab46bSEric W. Biederman 		return -ENOMEM;
20826e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, NULL);
208336b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
208454f968d6SJason Wang 	tfile->flags = 0;
208554f968d6SJason Wang 
208654f968d6SJason Wang 	rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
208754f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
208854f968d6SJason Wang 
208954f968d6SJason Wang 	tfile->socket.file = file;
209054f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
209154f968d6SJason Wang 
209254f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
209354f968d6SJason Wang 	sk_change_net(&tfile->sk, tfile->net);
209454f968d6SJason Wang 
209554f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
209654f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
209754f968d6SJason Wang 
2098631ab46bSEric W. Biederman 	file->private_data = tfile;
209954f968d6SJason Wang 	set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
210054f968d6SJason Wang 
21011da177e4SLinus Torvalds 	return 0;
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
21051da177e4SLinus Torvalds {
2106631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
210754f968d6SJason Wang 	struct net *net = tfile->net;
21081da177e4SLinus Torvalds 
2109c8d68e6bSJason Wang 	tun_detach(tfile, true);
211054f968d6SJason Wang 	put_net(net);
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	return 0;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
2115d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
21161da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
21171da177e4SLinus Torvalds 	.llseek = no_llseek,
2118ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
2119ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
2120ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
2121ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
21221da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
2123876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
212450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
212550857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
212650857e2aSArnd Bergmann #endif
21271da177e4SLinus Torvalds 	.open	= tun_chr_open,
21281da177e4SLinus Torvalds 	.release = tun_chr_close,
21291da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
21301da177e4SLinus Torvalds };
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
21331da177e4SLinus Torvalds 	.minor = TUN_MINOR,
21341da177e4SLinus Torvalds 	.name = "tun",
2135e454cea2SKay Sievers 	.nodename = "net/tun",
21361da177e4SLinus Torvalds 	.fops = &tun_fops,
21371da177e4SLinus Torvalds };
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds /* ethtool interface */
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
21421da177e4SLinus Torvalds {
21431da177e4SLinus Torvalds 	cmd->supported		= 0;
21441da177e4SLinus Torvalds 	cmd->advertising	= 0;
214570739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
21461da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
21471da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
21481da177e4SLinus Torvalds 	cmd->phy_address	= 0;
21491da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
21501da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
21511da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
21521da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
21531da177e4SLinus Torvalds 	return 0;
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
21571da177e4SLinus Torvalds {
21581da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
21591da177e4SLinus Torvalds 
216033a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
216133a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
21641da177e4SLinus Torvalds 	case TUN_TUN_DEV:
216533a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
21661da177e4SLinus Torvalds 		break;
21671da177e4SLinus Torvalds 	case TUN_TAP_DEV:
216833a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
21691da177e4SLinus Torvalds 		break;
21701da177e4SLinus Torvalds 	}
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
21741da177e4SLinus Torvalds {
21751da177e4SLinus Torvalds #ifdef TUN_DEBUG
21761da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
21771da177e4SLinus Torvalds 	return tun->debug;
21781da177e4SLinus Torvalds #else
21791da177e4SLinus Torvalds 	return -EOPNOTSUPP;
21801da177e4SLinus Torvalds #endif
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
21841da177e4SLinus Torvalds {
21851da177e4SLinus Torvalds #ifdef TUN_DEBUG
21861da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
21871da177e4SLinus Torvalds 	tun->debug = value;
21881da177e4SLinus Torvalds #endif
21891da177e4SLinus Torvalds }
21901da177e4SLinus Torvalds 
21917282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
21921da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
21931da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
21941da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
21951da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
2196bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
21971da177e4SLinus Torvalds };
21981da177e4SLinus Torvalds 
219979d17604SPavel Emelyanov 
22001da177e4SLinus Torvalds static int __init tun_init(void)
22011da177e4SLinus Torvalds {
22021da177e4SLinus Torvalds 	int ret = 0;
22031da177e4SLinus Torvalds 
22046b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
22056b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
22061da177e4SLinus Torvalds 
2207f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
220879d17604SPavel Emelyanov 	if (ret) {
22096b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
2210f019a7a5SEric W. Biederman 		goto err_linkops;
221179d17604SPavel Emelyanov 	}
221279d17604SPavel Emelyanov 
22131da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
221479d17604SPavel Emelyanov 	if (ret) {
22156b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
221679d17604SPavel Emelyanov 		goto err_misc;
221779d17604SPavel Emelyanov 	}
221879d17604SPavel Emelyanov 	return  0;
221979d17604SPavel Emelyanov err_misc:
2220f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2221f019a7a5SEric W. Biederman err_linkops:
22221da177e4SLinus Torvalds 	return ret;
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds static void tun_cleanup(void)
22261da177e4SLinus Torvalds {
22271da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2228f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
22291da177e4SLinus Torvalds }
22301da177e4SLinus Torvalds 
223105c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
223205c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
223305c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
223405c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
223505c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
223605c2828cSMichael S. Tsirkin {
22376e914fc7SJason Wang 	struct tun_file *tfile;
223805c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
223905c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
22406e914fc7SJason Wang 	tfile = file->private_data;
22416e914fc7SJason Wang 	if (!tfile)
224205c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
224354f968d6SJason Wang 	return &tfile->socket;
224405c2828cSMichael S. Tsirkin }
224505c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
224605c2828cSMichael S. Tsirkin 
22471da177e4SLinus Torvalds module_init(tun_init);
22481da177e4SLinus Torvalds module_exit(tun_cleanup);
22491da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
22501da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
22511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
22521da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2253578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2254