xref: /openbmc/linux/drivers/net/tun.c (revision 15718ea0d844e4816dbd95d57a8a0e3e264ba90e)
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 
112edfb6a14SJason Wang /* DEFAULT_MAX_NUM_RSS_QUEUES were choosed to let the rx/tx queues allocated for
113edfb6a14SJason Wang  * the netdevice to be fit in one page. So we can make sure the success of
114edfb6a14SJason Wang  * memory allocation. TODO: increase the limit. */
115edfb6a14SJason Wang #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES
116b8732fb7SJason Wang #define MAX_TAP_FLOWS  4096
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;
1414008e97fSJason Wang 	struct list_head next;
1424008e97fSJason Wang 	struct tun_struct *detached;
143631ab46bSEric W. Biederman };
144631ab46bSEric W. Biederman 
14596442e42SJason Wang struct tun_flow_entry {
14696442e42SJason Wang 	struct hlist_node hash_link;
14796442e42SJason Wang 	struct rcu_head rcu;
14896442e42SJason Wang 	struct tun_struct *tun;
14996442e42SJason Wang 
15096442e42SJason Wang 	u32 rxhash;
15196442e42SJason Wang 	int queue_index;
15296442e42SJason Wang 	unsigned long updated;
15396442e42SJason Wang };
15496442e42SJason Wang 
15596442e42SJason Wang #define TUN_NUM_FLOW_ENTRIES 1024
15696442e42SJason Wang 
15754f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
15836fe8c09SRami Rosen  * device, socket filter, sndbuf and vnet header size were restore when the
15954f968d6SJason Wang  * file were attached to a persist device.
16054f968d6SJason Wang  */
16114daa021SRusty Russell struct tun_struct {
162c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
163c8d68e6bSJason Wang 	unsigned int            numqueues;
164f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1650625c883SEric W. Biederman 	kuid_t			owner;
1660625c883SEric W. Biederman 	kgid_t			group;
16714daa021SRusty Russell 
16814daa021SRusty Russell 	struct net_device	*dev;
169c8f44affSMichał Mirosław 	netdev_features_t	set_features;
17088255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
17188255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
172d9d52b51SMichael S. Tsirkin 
173d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
17454f968d6SJason Wang 	int			sndbuf;
17554f968d6SJason Wang 	struct tap_filter	txflt;
17654f968d6SJason Wang 	struct sock_fprog	fprog;
17754f968d6SJason Wang 	/* protected by rtnl lock */
17854f968d6SJason Wang 	bool			filter_attached;
17914daa021SRusty Russell #ifdef TUN_DEBUG
18014daa021SRusty Russell 	int debug;
18114daa021SRusty Russell #endif
18296442e42SJason Wang 	spinlock_t lock;
18396442e42SJason Wang 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
18496442e42SJason Wang 	struct timer_list flow_gc_timer;
18596442e42SJason Wang 	unsigned long ageing_time;
1864008e97fSJason Wang 	unsigned int numdisabled;
1874008e97fSJason Wang 	struct list_head disabled;
1885dbbaf2dSPaul Moore 	void *security;
189b8732fb7SJason Wang 	u32 flow_count;
19014daa021SRusty Russell };
19114daa021SRusty Russell 
19296442e42SJason Wang static inline u32 tun_hashfn(u32 rxhash)
19396442e42SJason Wang {
19496442e42SJason Wang 	return rxhash & 0x3ff;
19596442e42SJason Wang }
19696442e42SJason Wang 
19796442e42SJason Wang static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
19896442e42SJason Wang {
19996442e42SJason Wang 	struct tun_flow_entry *e;
20096442e42SJason Wang 
201b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(e, head, hash_link) {
20296442e42SJason Wang 		if (e->rxhash == rxhash)
20396442e42SJason Wang 			return e;
20496442e42SJason Wang 	}
20596442e42SJason Wang 	return NULL;
20696442e42SJason Wang }
20796442e42SJason Wang 
20896442e42SJason Wang static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
20996442e42SJason Wang 					      struct hlist_head *head,
21096442e42SJason Wang 					      u32 rxhash, u16 queue_index)
21196442e42SJason Wang {
2129fdc6befSEric Dumazet 	struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
2139fdc6befSEric Dumazet 
21496442e42SJason Wang 	if (e) {
21596442e42SJason Wang 		tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
21696442e42SJason Wang 			  rxhash, queue_index);
21796442e42SJason Wang 		e->updated = jiffies;
21896442e42SJason Wang 		e->rxhash = rxhash;
21996442e42SJason Wang 		e->queue_index = queue_index;
22096442e42SJason Wang 		e->tun = tun;
22196442e42SJason Wang 		hlist_add_head_rcu(&e->hash_link, head);
222b8732fb7SJason Wang 		++tun->flow_count;
22396442e42SJason Wang 	}
22496442e42SJason Wang 	return e;
22596442e42SJason Wang }
22696442e42SJason Wang 
22796442e42SJason Wang static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
22896442e42SJason Wang {
22996442e42SJason Wang 	tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
23096442e42SJason Wang 		  e->rxhash, e->queue_index);
23196442e42SJason Wang 	hlist_del_rcu(&e->hash_link);
2329fdc6befSEric Dumazet 	kfree_rcu(e, rcu);
233b8732fb7SJason Wang 	--tun->flow_count;
23496442e42SJason Wang }
23596442e42SJason Wang 
23696442e42SJason Wang static void tun_flow_flush(struct tun_struct *tun)
23796442e42SJason Wang {
23896442e42SJason Wang 	int i;
23996442e42SJason Wang 
24096442e42SJason Wang 	spin_lock_bh(&tun->lock);
24196442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
24296442e42SJason Wang 		struct tun_flow_entry *e;
243b67bfe0dSSasha Levin 		struct hlist_node *n;
24496442e42SJason Wang 
245b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
24696442e42SJason Wang 			tun_flow_delete(tun, e);
24796442e42SJason Wang 	}
24896442e42SJason Wang 	spin_unlock_bh(&tun->lock);
24996442e42SJason Wang }
25096442e42SJason Wang 
25196442e42SJason Wang static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
25296442e42SJason Wang {
25396442e42SJason Wang 	int i;
25496442e42SJason Wang 
25596442e42SJason Wang 	spin_lock_bh(&tun->lock);
25696442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
25796442e42SJason Wang 		struct tun_flow_entry *e;
258b67bfe0dSSasha Levin 		struct hlist_node *n;
25996442e42SJason Wang 
260b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
26196442e42SJason Wang 			if (e->queue_index == queue_index)
26296442e42SJason Wang 				tun_flow_delete(tun, e);
26396442e42SJason Wang 		}
26496442e42SJason Wang 	}
26596442e42SJason Wang 	spin_unlock_bh(&tun->lock);
26696442e42SJason Wang }
26796442e42SJason Wang 
26896442e42SJason Wang static void tun_flow_cleanup(unsigned long data)
26996442e42SJason Wang {
27096442e42SJason Wang 	struct tun_struct *tun = (struct tun_struct *)data;
27196442e42SJason Wang 	unsigned long delay = tun->ageing_time;
27296442e42SJason Wang 	unsigned long next_timer = jiffies + delay;
27396442e42SJason Wang 	unsigned long count = 0;
27496442e42SJason Wang 	int i;
27596442e42SJason Wang 
27696442e42SJason Wang 	tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
27796442e42SJason Wang 
27896442e42SJason Wang 	spin_lock_bh(&tun->lock);
27996442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
28096442e42SJason Wang 		struct tun_flow_entry *e;
281b67bfe0dSSasha Levin 		struct hlist_node *n;
28296442e42SJason Wang 
283b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
28496442e42SJason Wang 			unsigned long this_timer;
28596442e42SJason Wang 			count++;
28696442e42SJason Wang 			this_timer = e->updated + delay;
28796442e42SJason Wang 			if (time_before_eq(this_timer, jiffies))
28896442e42SJason Wang 				tun_flow_delete(tun, e);
28996442e42SJason Wang 			else if (time_before(this_timer, next_timer))
29096442e42SJason Wang 				next_timer = this_timer;
29196442e42SJason Wang 		}
29296442e42SJason Wang 	}
29396442e42SJason Wang 
29496442e42SJason Wang 	if (count)
29596442e42SJason Wang 		mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
29696442e42SJason Wang 	spin_unlock_bh(&tun->lock);
29796442e42SJason Wang }
29896442e42SJason Wang 
29949974420SEric Dumazet static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
3009e85722dSJason Wang 			    struct tun_file *tfile)
30196442e42SJason Wang {
30296442e42SJason Wang 	struct hlist_head *head;
30396442e42SJason Wang 	struct tun_flow_entry *e;
30496442e42SJason Wang 	unsigned long delay = tun->ageing_time;
3059e85722dSJason Wang 	u16 queue_index = tfile->queue_index;
30696442e42SJason Wang 
30796442e42SJason Wang 	if (!rxhash)
30896442e42SJason Wang 		return;
30996442e42SJason Wang 	else
31096442e42SJason Wang 		head = &tun->flows[tun_hashfn(rxhash)];
31196442e42SJason Wang 
31296442e42SJason Wang 	rcu_read_lock();
31396442e42SJason Wang 
3149e85722dSJason Wang 	/* We may get a very small possibility of OOO during switching, not
3159e85722dSJason Wang 	 * worth to optimize.*/
3169e85722dSJason Wang 	if (tun->numqueues == 1 || tfile->detached)
31796442e42SJason Wang 		goto unlock;
31896442e42SJason Wang 
31996442e42SJason Wang 	e = tun_flow_find(head, rxhash);
32096442e42SJason Wang 	if (likely(e)) {
32196442e42SJason Wang 		/* TODO: keep queueing to old queue until it's empty? */
32296442e42SJason Wang 		e->queue_index = queue_index;
32396442e42SJason Wang 		e->updated = jiffies;
32496442e42SJason Wang 	} else {
32596442e42SJason Wang 		spin_lock_bh(&tun->lock);
326b8732fb7SJason Wang 		if (!tun_flow_find(head, rxhash) &&
327b8732fb7SJason Wang 		    tun->flow_count < MAX_TAP_FLOWS)
32896442e42SJason Wang 			tun_flow_create(tun, head, rxhash, queue_index);
32996442e42SJason Wang 
33096442e42SJason Wang 		if (!timer_pending(&tun->flow_gc_timer))
33196442e42SJason Wang 			mod_timer(&tun->flow_gc_timer,
33296442e42SJason Wang 				  round_jiffies_up(jiffies + delay));
33396442e42SJason Wang 		spin_unlock_bh(&tun->lock);
33496442e42SJason Wang 	}
33596442e42SJason Wang 
33696442e42SJason Wang unlock:
33796442e42SJason Wang 	rcu_read_unlock();
33896442e42SJason Wang }
33996442e42SJason Wang 
340c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
341c8d68e6bSJason Wang  * we do not check rxq no. is becuase some cards(e.g 82599), chooses
342c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
343c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
344c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
345c8d68e6bSJason Wang  * hope the rxq no. may help here.
346c8d68e6bSJason Wang  */
347c8d68e6bSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
348c8d68e6bSJason Wang {
349c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
35096442e42SJason Wang 	struct tun_flow_entry *e;
351c8d68e6bSJason Wang 	u32 txq = 0;
352c8d68e6bSJason Wang 	u32 numqueues = 0;
353c8d68e6bSJason Wang 
354c8d68e6bSJason Wang 	rcu_read_lock();
35592bb73eaSJason Wang 	numqueues = ACCESS_ONCE(tun->numqueues);
356c8d68e6bSJason Wang 
357c8d68e6bSJason Wang 	txq = skb_get_rxhash(skb);
358c8d68e6bSJason Wang 	if (txq) {
35996442e42SJason Wang 		e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
36096442e42SJason Wang 		if (e)
36196442e42SJason Wang 			txq = e->queue_index;
36296442e42SJason Wang 		else
363c8d68e6bSJason Wang 			/* use multiply and shift instead of expensive divide */
364c8d68e6bSJason Wang 			txq = ((u64)txq * numqueues) >> 32;
365c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
366c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
367c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
368c8d68e6bSJason Wang 			txq -= numqueues;
369c8d68e6bSJason Wang 	}
370c8d68e6bSJason Wang 
371c8d68e6bSJason Wang 	rcu_read_unlock();
372c8d68e6bSJason Wang 	return txq;
373c8d68e6bSJason Wang }
374c8d68e6bSJason Wang 
375cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
376cde8b15fSJason Wang {
377cde8b15fSJason Wang 	const struct cred *cred = current_cred();
378c260b772SEric W. Biederman 	struct net *net = dev_net(tun->dev);
379cde8b15fSJason Wang 
380cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
381cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
382c260b772SEric W. Biederman 		!ns_capable(net->user_ns, CAP_NET_ADMIN);
383cde8b15fSJason Wang }
384cde8b15fSJason Wang 
385c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
386c8d68e6bSJason Wang {
387c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
388c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
389c8d68e6bSJason Wang }
390c8d68e6bSJason Wang 
3914008e97fSJason Wang static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
3924008e97fSJason Wang {
3934008e97fSJason Wang 	tfile->detached = tun;
3944008e97fSJason Wang 	list_add_tail(&tfile->next, &tun->disabled);
3954008e97fSJason Wang 	++tun->numdisabled;
3964008e97fSJason Wang }
3974008e97fSJason Wang 
398d32649d1SJason Wang static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
3994008e97fSJason Wang {
4004008e97fSJason Wang 	struct tun_struct *tun = tfile->detached;
4014008e97fSJason Wang 
4024008e97fSJason Wang 	tfile->detached = NULL;
4034008e97fSJason Wang 	list_del_init(&tfile->next);
4044008e97fSJason Wang 	--tun->numdisabled;
4054008e97fSJason Wang 	return tun;
4064008e97fSJason Wang }
4074008e97fSJason Wang 
408c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
409c8d68e6bSJason Wang {
410c8d68e6bSJason Wang 	struct tun_file *ntfile;
411c8d68e6bSJason Wang 	struct tun_struct *tun;
412c8d68e6bSJason Wang 
413b8deabd3SJason Wang 	tun = rtnl_dereference(tfile->tun);
414b8deabd3SJason Wang 
4159e85722dSJason Wang 	if (tun && !tfile->detached) {
416c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
417c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
418c8d68e6bSJason Wang 
419c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
420c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
421b8deabd3SJason Wang 		ntfile = rtnl_dereference(tun->tfiles[index]);
422c8d68e6bSJason Wang 		ntfile->queue_index = index;
423c8d68e6bSJason Wang 
424c8d68e6bSJason Wang 		--tun->numqueues;
4259e85722dSJason Wang 		if (clean) {
4269e85722dSJason Wang 			rcu_assign_pointer(tfile->tun, NULL);
427c8d68e6bSJason Wang 			sock_put(&tfile->sk);
4289e85722dSJason Wang 		} else
4294008e97fSJason Wang 			tun_disable_queue(tun, tfile);
430c8d68e6bSJason Wang 
431c8d68e6bSJason Wang 		synchronize_net();
43296442e42SJason Wang 		tun_flow_delete_by_queue(tun, tun->numqueues + 1);
433c8d68e6bSJason Wang 		/* Drop read queue */
434c8d68e6bSJason Wang 		skb_queue_purge(&tfile->sk.sk_receive_queue);
435c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
436dd38bd85SJason Wang 	} else if (tfile->detached && clean) {
4374008e97fSJason Wang 		tun = tun_enable_queue(tfile);
438dd38bd85SJason Wang 		sock_put(&tfile->sk);
439dd38bd85SJason Wang 	}
440c8d68e6bSJason Wang 
441c8d68e6bSJason Wang 	if (clean) {
442af668b3cSMichael S. Tsirkin 		if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
443af668b3cSMichael S. Tsirkin 			netif_carrier_off(tun->dev);
444af668b3cSMichael S. Tsirkin 
445af668b3cSMichael S. Tsirkin 			if (!(tun->flags & TUN_PERSIST) &&
446af668b3cSMichael S. Tsirkin 			    tun->dev->reg_state == NETREG_REGISTERED)
4474008e97fSJason Wang 				unregister_netdevice(tun->dev);
448af668b3cSMichael S. Tsirkin 		}
4494008e97fSJason Wang 
450c8d68e6bSJason Wang 		BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
451c8d68e6bSJason Wang 				 &tfile->socket.flags));
452c8d68e6bSJason Wang 		sk_release_kernel(&tfile->sk);
453c8d68e6bSJason Wang 	}
454c8d68e6bSJason Wang }
455c8d68e6bSJason Wang 
456c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
457c8d68e6bSJason Wang {
458c8d68e6bSJason Wang 	rtnl_lock();
459c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
460c8d68e6bSJason Wang 	rtnl_unlock();
461c8d68e6bSJason Wang }
462c8d68e6bSJason Wang 
463c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
464c8d68e6bSJason Wang {
465c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
4664008e97fSJason Wang 	struct tun_file *tfile, *tmp;
467c8d68e6bSJason Wang 	int i, n = tun->numqueues;
468c8d68e6bSJason Wang 
469c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
470b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
471c8d68e6bSJason Wang 		BUG_ON(!tfile);
472c8d68e6bSJason Wang 		wake_up_all(&tfile->wq.wait);
473c8d68e6bSJason Wang 		rcu_assign_pointer(tfile->tun, NULL);
474c8d68e6bSJason Wang 		--tun->numqueues;
475c8d68e6bSJason Wang 	}
4769e85722dSJason Wang 	list_for_each_entry(tfile, &tun->disabled, next) {
4779e85722dSJason Wang 		wake_up_all(&tfile->wq.wait);
4789e85722dSJason Wang 		rcu_assign_pointer(tfile->tun, NULL);
4799e85722dSJason Wang 	}
480c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
481c8d68e6bSJason Wang 
482c8d68e6bSJason Wang 	synchronize_net();
483c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
484b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
485c8d68e6bSJason Wang 		/* Drop read queue */
486c8d68e6bSJason Wang 		skb_queue_purge(&tfile->sk.sk_receive_queue);
487c8d68e6bSJason Wang 		sock_put(&tfile->sk);
488c8d68e6bSJason Wang 	}
4894008e97fSJason Wang 	list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
4904008e97fSJason Wang 		tun_enable_queue(tfile);
4914008e97fSJason Wang 		skb_queue_purge(&tfile->sk.sk_receive_queue);
4924008e97fSJason Wang 		sock_put(&tfile->sk);
4934008e97fSJason Wang 	}
4944008e97fSJason Wang 	BUG_ON(tun->numdisabled != 0);
495dd38bd85SJason Wang 
496dd38bd85SJason Wang 	if (tun->flags & TUN_PERSIST)
497dd38bd85SJason Wang 		module_put(THIS_MODULE);
498c8d68e6bSJason Wang }
499c8d68e6bSJason Wang 
500a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
501a7385ba2SEric W. Biederman {
502631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
50338231b7aSEric W. Biederman 	int err;
504a7385ba2SEric W. Biederman 
5055dbbaf2dSPaul Moore 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
5065dbbaf2dSPaul Moore 	if (err < 0)
5075dbbaf2dSPaul Moore 		goto out;
5085dbbaf2dSPaul Moore 
50938231b7aSEric W. Biederman 	err = -EINVAL;
5109e85722dSJason Wang 	if (rtnl_dereference(tfile->tun) && !tfile->detached)
51138231b7aSEric W. Biederman 		goto out;
51238231b7aSEric W. Biederman 
51338231b7aSEric W. Biederman 	err = -EBUSY;
514c8d68e6bSJason Wang 	if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
515c8d68e6bSJason Wang 		goto out;
516c8d68e6bSJason Wang 
517c8d68e6bSJason Wang 	err = -E2BIG;
5184008e97fSJason Wang 	if (!tfile->detached &&
5194008e97fSJason Wang 	    tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
52038231b7aSEric W. Biederman 		goto out;
52138231b7aSEric W. Biederman 
52238231b7aSEric W. Biederman 	err = 0;
52354f968d6SJason Wang 
524c8d68e6bSJason Wang 	/* Re-attach the filter to presist device */
52554f968d6SJason Wang 	if (tun->filter_attached == true) {
52654f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
52754f968d6SJason Wang 		if (!err)
52854f968d6SJason Wang 			goto out;
52954f968d6SJason Wang 	}
530c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
5316e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
532c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
533c8d68e6bSJason Wang 	tun->numqueues++;
534c8d68e6bSJason Wang 
5354008e97fSJason Wang 	if (tfile->detached)
5364008e97fSJason Wang 		tun_enable_queue(tfile);
5374008e97fSJason Wang 	else
5384008e97fSJason Wang 		sock_hold(&tfile->sk);
5394008e97fSJason Wang 
540c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
541c8d68e6bSJason Wang 
542c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
543c8d68e6bSJason Wang 	 * refcnt.
544c8d68e6bSJason Wang 	 */
545a7385ba2SEric W. Biederman 
54638231b7aSEric W. Biederman out:
54738231b7aSEric W. Biederman 	return err;
548a7385ba2SEric W. Biederman }
549a7385ba2SEric W. Biederman 
550631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
551631ab46bSEric W. Biederman {
5526e914fc7SJason Wang 	struct tun_struct *tun;
553c70f1829SEric W. Biederman 
5546e914fc7SJason Wang 	rcu_read_lock();
5556e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
5566e914fc7SJason Wang 	if (tun)
5576e914fc7SJason Wang 		dev_hold(tun->dev);
5586e914fc7SJason Wang 	rcu_read_unlock();
559c70f1829SEric W. Biederman 
560c70f1829SEric W. Biederman 	return tun;
561631ab46bSEric W. Biederman }
562631ab46bSEric W. Biederman 
563631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
564631ab46bSEric W. Biederman {
565631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
566631ab46bSEric W. Biederman }
567631ab46bSEric W. Biederman 
568631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
569631ab46bSEric W. Biederman {
5706e914fc7SJason Wang 	dev_put(tun->dev);
571631ab46bSEric W. Biederman }
572631ab46bSEric W. Biederman 
5736b8a66eeSJoe Perches /* TAP filtering */
574f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
575f271b2ccSMax Krasnyansky {
576f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
577f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
578f271b2ccSMax Krasnyansky }
579f271b2ccSMax Krasnyansky 
580f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
581f271b2ccSMax Krasnyansky {
582f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
583f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
584f271b2ccSMax Krasnyansky }
585f271b2ccSMax Krasnyansky 
586f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
587f271b2ccSMax Krasnyansky {
588f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
589f271b2ccSMax Krasnyansky 	struct tun_filter uf;
590f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
591f271b2ccSMax Krasnyansky 
592f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
593f271b2ccSMax Krasnyansky 		return -EFAULT;
594f271b2ccSMax Krasnyansky 
595f271b2ccSMax Krasnyansky 	if (!uf.count) {
596f271b2ccSMax Krasnyansky 		/* Disabled */
597f271b2ccSMax Krasnyansky 		filter->count = 0;
598f271b2ccSMax Krasnyansky 		return 0;
599f271b2ccSMax Krasnyansky 	}
600f271b2ccSMax Krasnyansky 
601f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
602f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
603f271b2ccSMax Krasnyansky 	if (!addr)
604f271b2ccSMax Krasnyansky 		return -ENOMEM;
605f271b2ccSMax Krasnyansky 
606f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
607f271b2ccSMax Krasnyansky 		err = -EFAULT;
608f271b2ccSMax Krasnyansky 		goto done;
609f271b2ccSMax Krasnyansky 	}
610f271b2ccSMax Krasnyansky 
611f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
612f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
613f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
614f271b2ccSMax Krasnyansky 	filter->count = 0;
615f271b2ccSMax Krasnyansky 	wmb();
616f271b2ccSMax Krasnyansky 
617f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
618f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
619f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
620f271b2ccSMax Krasnyansky 
621f271b2ccSMax Krasnyansky 	nexact = n;
622f271b2ccSMax Krasnyansky 
623cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
624cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
625f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
626cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
627cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
628cfbf84fcSAlex Williamson 			err = 0; /* no filter */
629cfbf84fcSAlex Williamson 			goto done;
630cfbf84fcSAlex Williamson 		}
631f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
632cfbf84fcSAlex Williamson 	}
633f271b2ccSMax Krasnyansky 
634f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
635f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
636f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
637f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
638f271b2ccSMax Krasnyansky 
639f271b2ccSMax Krasnyansky 	/* Now enable the filter */
640f271b2ccSMax Krasnyansky 	wmb();
641f271b2ccSMax Krasnyansky 	filter->count = nexact;
642f271b2ccSMax Krasnyansky 
643f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
644f271b2ccSMax Krasnyansky 	err = nexact;
645f271b2ccSMax Krasnyansky 
646f271b2ccSMax Krasnyansky done:
647f271b2ccSMax Krasnyansky 	kfree(addr);
648f271b2ccSMax Krasnyansky 	return err;
649f271b2ccSMax Krasnyansky }
650f271b2ccSMax Krasnyansky 
651f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
652f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
653f271b2ccSMax Krasnyansky {
654f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
655f271b2ccSMax Krasnyansky 	 * at this point. */
656f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
657f271b2ccSMax Krasnyansky 	int i;
658f271b2ccSMax Krasnyansky 
659f271b2ccSMax Krasnyansky 	/* Exact match */
660f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
6612e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
662f271b2ccSMax Krasnyansky 			return 1;
663f271b2ccSMax Krasnyansky 
664f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
665f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
666f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
667f271b2ccSMax Krasnyansky 
668f271b2ccSMax Krasnyansky 	return 0;
669f271b2ccSMax Krasnyansky }
670f271b2ccSMax Krasnyansky 
671f271b2ccSMax Krasnyansky /*
672f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
673f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
674f271b2ccSMax Krasnyansky  */
675f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
676f271b2ccSMax Krasnyansky {
677f271b2ccSMax Krasnyansky 	if (!filter->count)
678f271b2ccSMax Krasnyansky 		return 1;
679f271b2ccSMax Krasnyansky 
680f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
681f271b2ccSMax Krasnyansky }
682f271b2ccSMax Krasnyansky 
6831da177e4SLinus Torvalds /* Network device part of the driver */
6841da177e4SLinus Torvalds 
6857282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
6861da177e4SLinus Torvalds 
687c70f1829SEric W. Biederman /* Net device detach from fd. */
688c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
689c70f1829SEric W. Biederman {
690c8d68e6bSJason Wang 	tun_detach_all(dev);
691c70f1829SEric W. Biederman }
692c70f1829SEric W. Biederman 
6931da177e4SLinus Torvalds /* Net device open. */
6941da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
6951da177e4SLinus Torvalds {
696c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
6971da177e4SLinus Torvalds 	return 0;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds /* Net device close. */
7011da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
7021da177e4SLinus Torvalds {
703c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
7041da177e4SLinus Torvalds 	return 0;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /* Net device start xmit */
708424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
711c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
7126e914fc7SJason Wang 	struct tun_file *tfile;
7131da177e4SLinus Torvalds 
7146e914fc7SJason Wang 	rcu_read_lock();
715c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
716c8d68e6bSJason Wang 
7171da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
718c8d68e6bSJason Wang 	if (txq >= tun->numqueues)
7191da177e4SLinus Torvalds 		goto drop;
7201da177e4SLinus Torvalds 
7216e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
7226e914fc7SJason Wang 
723c8d68e6bSJason Wang 	BUG_ON(!tfile);
724c8d68e6bSJason Wang 
725f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
726f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
727f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
728f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
729f271b2ccSMax Krasnyansky 		goto drop;
730f271b2ccSMax Krasnyansky 
73154f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
73254f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
73399405162SMichael S. Tsirkin 		goto drop;
73499405162SMichael S. Tsirkin 
73536fe8c09SRami Rosen 	/* Limit the number of packets queued by dividing txq length with the
736c8d68e6bSJason Wang 	 * number of queues.
737c8d68e6bSJason Wang 	 */
73854f968d6SJason Wang 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
7395d097109SMichael S. Tsirkin 			  >= dev->tx_queue_len / tun->numqueues)
7401da177e4SLinus Torvalds 		goto drop;
7411da177e4SLinus Torvalds 
7420110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
7430110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
744868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
745868eefebSMichael S. Tsirkin 		goto drop;
7460110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
7470110d6f2SMichael S. Tsirkin 
748f8af75f3SEric Dumazet 	nf_reset(skb);
749f8af75f3SEric Dumazet 
750f271b2ccSMax Krasnyansky 	/* Enqueue packet */
75154f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	/* Notify and wake up reader process */
75454f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
75554f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
75654f968d6SJason Wang 	wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
75705c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
7586e914fc7SJason Wang 
7596e914fc7SJason Wang 	rcu_read_unlock();
7606ed10654SPatrick McHardy 	return NETDEV_TX_OK;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds drop:
76309f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
764149d36f7SMichael S. Tsirkin 	skb_tx_error(skb);
7651da177e4SLinus Torvalds 	kfree_skb(skb);
7666e914fc7SJason Wang 	rcu_read_unlock();
7676ed10654SPatrick McHardy 	return NETDEV_TX_OK;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
770f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
7711da177e4SLinus Torvalds {
772f271b2ccSMax Krasnyansky 	/*
773f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
774f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
775f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
776f271b2ccSMax Krasnyansky 	 */
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
7794885a504SEd Swierk #define MIN_MTU 68
7804885a504SEd Swierk #define MAX_MTU 65535
7814885a504SEd Swierk 
7824885a504SEd Swierk static int
7834885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
7844885a504SEd Swierk {
7854885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
7864885a504SEd Swierk 		return -EINVAL;
7874885a504SEd Swierk 	dev->mtu = new_mtu;
7884885a504SEd Swierk 	return 0;
7894885a504SEd Swierk }
7904885a504SEd Swierk 
791c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
792c8f44affSMichał Mirosław 	netdev_features_t features)
79388255375SMichał Mirosław {
79488255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
79588255375SMichał Mirosław 
79688255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
79788255375SMichał Mirosław }
798bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
799bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
800bebd097aSNeil Horman {
801bebd097aSNeil Horman 	/*
802bebd097aSNeil Horman 	 * Tun only receives frames when:
803bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
804bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
805bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
806bebd097aSNeil Horman 	 * never to have pending data when we poll for it
807bebd097aSNeil Horman 	 * so theres nothing to do here but return.
808bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
809bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
810bebd097aSNeil Horman 	 * still use netconsole
811bebd097aSNeil Horman 	 */
812bebd097aSNeil Horman 	return;
813bebd097aSNeil Horman }
814bebd097aSNeil Horman #endif
815758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
816c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
817758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
818758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
81900829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
820758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
82188255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
822c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
823bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
824bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
825bebd097aSNeil Horman #endif
826758e43b7SStephen Hemminger };
827758e43b7SStephen Hemminger 
828758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
829c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
830758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
831758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
83200829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
833758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
83488255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
835afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
836758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
837758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
838c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
839bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
840bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
841bebd097aSNeil Horman #endif
842758e43b7SStephen Hemminger };
843758e43b7SStephen Hemminger 
844944a1376SPavel Emelyanov static void tun_flow_init(struct tun_struct *tun)
84596442e42SJason Wang {
84696442e42SJason Wang 	int i;
84796442e42SJason Wang 
84896442e42SJason Wang 	for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
84996442e42SJason Wang 		INIT_HLIST_HEAD(&tun->flows[i]);
85096442e42SJason Wang 
85196442e42SJason Wang 	tun->ageing_time = TUN_FLOW_EXPIRE;
85296442e42SJason Wang 	setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
85396442e42SJason Wang 	mod_timer(&tun->flow_gc_timer,
85496442e42SJason Wang 		  round_jiffies_up(jiffies + tun->ageing_time));
85596442e42SJason Wang }
85696442e42SJason Wang 
85796442e42SJason Wang static void tun_flow_uninit(struct tun_struct *tun)
85896442e42SJason Wang {
85996442e42SJason Wang 	del_timer_sync(&tun->flow_gc_timer);
86096442e42SJason Wang 	tun_flow_flush(tun);
86196442e42SJason Wang }
86296442e42SJason Wang 
8631da177e4SLinus Torvalds /* Initialize net device. */
8641da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
8691da177e4SLinus Torvalds 	case TUN_TUN_DEV:
870758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
871758e43b7SStephen Hemminger 
8721da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
8731da177e4SLinus Torvalds 		dev->hard_header_len = 0;
8741da177e4SLinus Torvalds 		dev->addr_len = 0;
8751da177e4SLinus Torvalds 		dev->mtu = 1500;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 		/* Zero header length */
8781da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
8791da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
8801da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
8811da177e4SLinus Torvalds 		break;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8847a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
8851da177e4SLinus Torvalds 		/* Ethernet TAP Device */
8861da177e4SLinus Torvalds 		ether_setup(dev);
887550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
888a676847bSstephen hemminger 		dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
88936226a8dSBrian Braunstein 
890f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
89136226a8dSBrian Braunstein 
8921da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
8931da177e4SLinus Torvalds 		break;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /* Character device part */
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds /* Poll */
9001da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
9011da177e4SLinus Torvalds {
902b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
903b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
9043c8a9c63SMariusz Kozlowski 	struct sock *sk;
90533dccbb0SHerbert Xu 	unsigned int mask = 0;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	if (!tun)
908eac9e902SEric W. Biederman 		return POLLERR;
9091da177e4SLinus Torvalds 
91054f968d6SJason Wang 	sk = tfile->socket.sk;
9113c8a9c63SMariusz Kozlowski 
9126b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
9131da177e4SLinus Torvalds 
91454f968d6SJason Wang 	poll_wait(file, &tfile->wq.wait, wait);
9151da177e4SLinus Torvalds 
91689f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
9171da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
9181da177e4SLinus Torvalds 
91933dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
92033dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
92133dccbb0SHerbert Xu 	     sock_writeable(sk)))
92233dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
92333dccbb0SHerbert Xu 
924c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
925c70f1829SEric W. Biederman 		mask = POLLERR;
926c70f1829SEric W. Biederman 
927631ab46bSEric W. Biederman 	tun_put(tun);
9281da177e4SLinus Torvalds 	return mask;
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
931f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
932f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
93354f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
93433dccbb0SHerbert Xu 				     size_t prepad, size_t len,
93533dccbb0SHerbert Xu 				     size_t linear, int noblock)
936f42157cbSRusty Russell {
93754f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
938f42157cbSRusty Russell 	struct sk_buff *skb;
93933dccbb0SHerbert Xu 	int err;
940f42157cbSRusty Russell 
941f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
9420eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
94333dccbb0SHerbert Xu 		linear = len;
944f42157cbSRusty Russell 
94533dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
94633dccbb0SHerbert Xu 				   &err);
947f42157cbSRusty Russell 	if (!skb)
94833dccbb0SHerbert Xu 		return ERR_PTR(err);
949f42157cbSRusty Russell 
950f42157cbSRusty Russell 	skb_reserve(skb, prepad);
951f42157cbSRusty Russell 	skb_put(skb, linear);
95233dccbb0SHerbert Xu 	skb->data_len = len - linear;
95333dccbb0SHerbert Xu 	skb->len += len - linear;
954f42157cbSRusty Russell 
955f42157cbSRusty Russell 	return skb;
956f42157cbSRusty Russell }
957f42157cbSRusty Russell 
9580690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */
9590690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
9600690899bSMichael S. Tsirkin 				  int offset, size_t count)
9610690899bSMichael S. Tsirkin {
9620690899bSMichael S. Tsirkin 	int len = iov_length(from, count) - offset;
9630690899bSMichael S. Tsirkin 	int copy = skb_headlen(skb);
9640690899bSMichael S. Tsirkin 	int size, offset1 = 0;
9650690899bSMichael S. Tsirkin 	int i = 0;
9660690899bSMichael S. Tsirkin 
9670690899bSMichael S. Tsirkin 	/* Skip over from offset */
9680690899bSMichael S. Tsirkin 	while (count && (offset >= from->iov_len)) {
9690690899bSMichael S. Tsirkin 		offset -= from->iov_len;
9700690899bSMichael S. Tsirkin 		++from;
9710690899bSMichael S. Tsirkin 		--count;
9720690899bSMichael S. Tsirkin 	}
9730690899bSMichael S. Tsirkin 
9740690899bSMichael S. Tsirkin 	/* copy up to skb headlen */
9750690899bSMichael S. Tsirkin 	while (count && (copy > 0)) {
9760690899bSMichael S. Tsirkin 		size = min_t(unsigned int, copy, from->iov_len - offset);
9770690899bSMichael S. Tsirkin 		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
9780690899bSMichael S. Tsirkin 				   size))
9790690899bSMichael S. Tsirkin 			return -EFAULT;
9800690899bSMichael S. Tsirkin 		if (copy > size) {
9810690899bSMichael S. Tsirkin 			++from;
9820690899bSMichael S. Tsirkin 			--count;
9830690899bSMichael S. Tsirkin 			offset = 0;
9840690899bSMichael S. Tsirkin 		} else
9850690899bSMichael S. Tsirkin 			offset += size;
9860690899bSMichael S. Tsirkin 		copy -= size;
9870690899bSMichael S. Tsirkin 		offset1 += size;
9880690899bSMichael S. Tsirkin 	}
9890690899bSMichael S. Tsirkin 
9900690899bSMichael S. Tsirkin 	if (len == offset1)
9910690899bSMichael S. Tsirkin 		return 0;
9920690899bSMichael S. Tsirkin 
9930690899bSMichael S. Tsirkin 	while (count--) {
9940690899bSMichael S. Tsirkin 		struct page *page[MAX_SKB_FRAGS];
9950690899bSMichael S. Tsirkin 		int num_pages;
9960690899bSMichael S. Tsirkin 		unsigned long base;
9970690899bSMichael S. Tsirkin 		unsigned long truesize;
9980690899bSMichael S. Tsirkin 
9990690899bSMichael S. Tsirkin 		len = from->iov_len - offset;
10000690899bSMichael S. Tsirkin 		if (!len) {
10010690899bSMichael S. Tsirkin 			offset = 0;
10020690899bSMichael S. Tsirkin 			++from;
10030690899bSMichael S. Tsirkin 			continue;
10040690899bSMichael S. Tsirkin 		}
10050690899bSMichael S. Tsirkin 		base = (unsigned long)from->iov_base + offset;
10060690899bSMichael S. Tsirkin 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
10070690899bSMichael S. Tsirkin 		if (i + size > MAX_SKB_FRAGS)
10080690899bSMichael S. Tsirkin 			return -EMSGSIZE;
10090690899bSMichael S. Tsirkin 		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
10100690899bSMichael S. Tsirkin 		if (num_pages != size) {
10117e24bfbeSMichael S. Tsirkin 			int j;
10127e24bfbeSMichael S. Tsirkin 
10137e24bfbeSMichael S. Tsirkin 			for (j = 0; j < num_pages; j++)
10147e24bfbeSMichael S. Tsirkin 				put_page(page[i + j]);
10150690899bSMichael S. Tsirkin 			return -EFAULT;
10160690899bSMichael S. Tsirkin 		}
10170690899bSMichael S. Tsirkin 		truesize = size * PAGE_SIZE;
10180690899bSMichael S. Tsirkin 		skb->data_len += len;
10190690899bSMichael S. Tsirkin 		skb->len += len;
10200690899bSMichael S. Tsirkin 		skb->truesize += truesize;
10210690899bSMichael S. Tsirkin 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
10220690899bSMichael S. Tsirkin 		while (len) {
10230690899bSMichael S. Tsirkin 			int off = base & ~PAGE_MASK;
10240690899bSMichael S. Tsirkin 			int size = min_t(int, len, PAGE_SIZE - off);
10250690899bSMichael S. Tsirkin 			__skb_fill_page_desc(skb, i, page[i], off, size);
10260690899bSMichael S. Tsirkin 			skb_shinfo(skb)->nr_frags++;
10270690899bSMichael S. Tsirkin 			/* increase sk_wmem_alloc */
10280690899bSMichael S. Tsirkin 			base += size;
10290690899bSMichael S. Tsirkin 			len -= size;
10300690899bSMichael S. Tsirkin 			i++;
10310690899bSMichael S. Tsirkin 		}
10320690899bSMichael S. Tsirkin 		offset = 0;
10330690899bSMichael S. Tsirkin 		++from;
10340690899bSMichael S. Tsirkin 	}
10350690899bSMichael S. Tsirkin 	return 0;
10360690899bSMichael S. Tsirkin }
10370690899bSMichael S. Tsirkin 
103888529176SJason Wang static unsigned long iov_pages(const struct iovec *iv, int offset,
103988529176SJason Wang 			       unsigned long nr_segs)
104088529176SJason Wang {
104188529176SJason Wang 	unsigned long seg, base;
104288529176SJason Wang 	int pages = 0, len, size;
104388529176SJason Wang 
104488529176SJason Wang 	while (nr_segs && (offset >= iv->iov_len)) {
104588529176SJason Wang 		offset -= iv->iov_len;
104688529176SJason Wang 		++iv;
104788529176SJason Wang 		--nr_segs;
104888529176SJason Wang 	}
104988529176SJason Wang 
105088529176SJason Wang 	for (seg = 0; seg < nr_segs; seg++) {
105188529176SJason Wang 		base = (unsigned long)iv[seg].iov_base + offset;
105288529176SJason Wang 		len = iv[seg].iov_len - offset;
105388529176SJason Wang 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
105488529176SJason Wang 		pages += size;
105588529176SJason Wang 		offset = 0;
105688529176SJason Wang 	}
105788529176SJason Wang 
105888529176SJason Wang 	return pages;
105988529176SJason Wang }
106088529176SJason Wang 
10611da177e4SLinus Torvalds /* Get packet from user space buffer */
106254f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
106354f968d6SJason Wang 			    void *msg_control, const struct iovec *iv,
106454f968d6SJason Wang 			    size_t total_len, size_t count, int noblock)
10651da177e4SLinus Torvalds {
106609640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
10671da177e4SLinus Torvalds 	struct sk_buff *skb;
10683dd5c330SJason Wang 	size_t len = total_len, align = NET_SKB_PAD, linear;
1069f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
10706f26c9a7SMichael S. Tsirkin 	int offset = 0;
10710690899bSMichael S. Tsirkin 	int copylen;
10720690899bSMichael S. Tsirkin 	bool zerocopy = false;
10730690899bSMichael S. Tsirkin 	int err;
107449974420SEric Dumazet 	u32 rxhash;
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
1077*15718ea0SDan Carpenter 		if (len < sizeof(pi))
10781da177e4SLinus Torvalds 			return -EINVAL;
1079*15718ea0SDan Carpenter 		len -= sizeof(pi);
10801da177e4SLinus Torvalds 
10816f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
10821da177e4SLinus Torvalds 			return -EFAULT;
10836f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
1086f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
1087*15718ea0SDan Carpenter 		if (len < tun->vnet_hdr_sz)
1088f43798c2SRusty Russell 			return -EINVAL;
1089*15718ea0SDan Carpenter 		len -= tun->vnet_hdr_sz;
1090f43798c2SRusty Russell 
10916f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
1092f43798c2SRusty Russell 			return -EFAULT;
1093f43798c2SRusty Russell 
10944909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
10954909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
10964909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
10974909122fSHerbert Xu 
1098f43798c2SRusty Russell 		if (gso.hdr_len > len)
1099f43798c2SRusty Russell 			return -EINVAL;
1100d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
1101f43798c2SRusty Russell 	}
1102f43798c2SRusty Russell 
1103e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
1104a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
11050eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
11060eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
1107e01bf1c8SRusty Russell 			return -EINVAL;
1108e01bf1c8SRusty Russell 	}
11091da177e4SLinus Torvalds 
111088529176SJason Wang 	if (msg_control) {
111188529176SJason Wang 		/* There are 256 bytes to be copied in skb, so there is
111288529176SJason Wang 		 * enough room for skb expand head in case it is used.
11130690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
11140690899bSMichael S. Tsirkin 		 */
111588529176SJason Wang 		copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN;
11163dd5c330SJason Wang 		linear = copylen;
111788529176SJason Wang 		if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
111888529176SJason Wang 			zerocopy = true;
111988529176SJason Wang 	}
112088529176SJason Wang 
112188529176SJason Wang 	if (!zerocopy) {
11220690899bSMichael S. Tsirkin 		copylen = len;
11233dd5c330SJason Wang 		linear = gso.hdr_len;
11243dd5c330SJason Wang 	}
11250690899bSMichael S. Tsirkin 
11263dd5c330SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
112733dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
112833dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
112909f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
113033dccbb0SHerbert Xu 		return PTR_ERR(skb);
11311da177e4SLinus Torvalds 	}
11321da177e4SLinus Torvalds 
11330690899bSMichael S. Tsirkin 	if (zerocopy)
11340690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
113588529176SJason Wang 	else {
11360690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
113788529176SJason Wang 		if (!err && msg_control) {
113888529176SJason Wang 			struct ubuf_info *uarg = msg_control;
113988529176SJason Wang 			uarg->callback(uarg, false);
114088529176SJason Wang 		}
114188529176SJason Wang 	}
11420690899bSMichael S. Tsirkin 
11430690899bSMichael S. Tsirkin 	if (err) {
114409f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
11458f22757eSDave Jones 		kfree_skb(skb);
11461da177e4SLinus Torvalds 		return -EFAULT;
11478f22757eSDave Jones 	}
11481da177e4SLinus Torvalds 
1149f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1150f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
1151f43798c2SRusty Russell 					  gso.csum_offset)) {
1152f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1153f43798c2SRusty Russell 			kfree_skb(skb);
1154f43798c2SRusty Russell 			return -EINVAL;
1155f43798c2SRusty Russell 		}
115688255375SMichał Mirosław 	}
1157f43798c2SRusty Russell 
11581da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
11591da177e4SLinus Torvalds 	case TUN_TUN_DEV:
1160f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
1161f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
1162f09f7ee2SAng Way Chuang 			case 0x40:
1163f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
1164f09f7ee2SAng Way Chuang 				break;
1165f09f7ee2SAng Way Chuang 			case 0x60:
1166f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
1167f09f7ee2SAng Way Chuang 				break;
1168f09f7ee2SAng Way Chuang 			default:
1169f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
1170f09f7ee2SAng Way Chuang 				kfree_skb(skb);
1171f09f7ee2SAng Way Chuang 				return -EINVAL;
1172f09f7ee2SAng Way Chuang 			}
1173f09f7ee2SAng Way Chuang 		}
1174f09f7ee2SAng Way Chuang 
1175459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
11761da177e4SLinus Torvalds 		skb->protocol = pi.proto;
11774c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
11781da177e4SLinus Torvalds 		break;
11791da177e4SLinus Torvalds 	case TUN_TAP_DEV:
11801da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
11811da177e4SLinus Torvalds 		break;
11826403eab1SJoe Perches 	}
11831da177e4SLinus Torvalds 
1184f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1185f43798c2SRusty Russell 		pr_debug("GSO!\n");
1186f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1187f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
1188c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1189f43798c2SRusty Russell 			break;
1190f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
1191c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1192f43798c2SRusty Russell 			break;
1193e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
1194c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1195e36aa25aSSridhar Samudrala 			break;
1196f43798c2SRusty Russell 		default:
1197f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1198f43798c2SRusty Russell 			kfree_skb(skb);
1199f43798c2SRusty Russell 			return -EINVAL;
1200f43798c2SRusty Russell 		}
1201f43798c2SRusty Russell 
1202f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1203c9af6db4SPravin B Shelar 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1204f43798c2SRusty Russell 
1205f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
1206f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
1207f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
1208f43798c2SRusty Russell 			kfree_skb(skb);
1209f43798c2SRusty Russell 			return -EINVAL;
1210f43798c2SRusty Russell 		}
1211f43798c2SRusty Russell 
1212f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
1213f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1214f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
1215f43798c2SRusty Russell 	}
12161da177e4SLinus Torvalds 
12170690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
12180690899bSMichael S. Tsirkin 	if (zerocopy) {
12190690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
12200690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1221c9af6db4SPravin B Shelar 		skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
12220690899bSMichael S. Tsirkin 	}
12230690899bSMichael S. Tsirkin 
122476fe4581SEric Dumazet 	skb_reset_network_header(skb);
122540893fd0SJason Wang 	skb_probe_transport_header(skb, 0);
122638502af7SJason Wang 
122749974420SEric Dumazet 	rxhash = skb_get_rxhash(skb);
12281da177e4SLinus Torvalds 	netif_rx_ni(skb);
12291da177e4SLinus Torvalds 
123009f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
123109f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
12321da177e4SLinus Torvalds 
12339e85722dSJason Wang 	tun_flow_update(tun, rxhash, tfile);
12340690899bSMichael S. Tsirkin 	return total_len;
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds 
1237ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1238ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
12391da177e4SLinus Torvalds {
124033dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
1241ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
124254f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
1243631ab46bSEric W. Biederman 	ssize_t result;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	if (!tun)
12461da177e4SLinus Torvalds 		return -EBADFD;
12471da177e4SLinus Torvalds 
12486b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
12491da177e4SLinus Torvalds 
125054f968d6SJason Wang 	result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
125154f968d6SJason Wang 			      count, file->f_flags & O_NONBLOCK);
1252631ab46bSEric W. Biederman 
1253631ab46bSEric W. Biederman 	tun_put(tun);
1254631ab46bSEric W. Biederman 	return result;
12551da177e4SLinus Torvalds }
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds /* Put packet to the user space buffer */
12586f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
125954f968d6SJason Wang 			    struct tun_file *tfile,
12601da177e4SLinus Torvalds 			    struct sk_buff *skb,
126143b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
12641da177e4SLinus Torvalds 	ssize_t total = 0;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
12671da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
12681da177e4SLinus Torvalds 			return -EINVAL;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 		if (len < skb->len) {
12711da177e4SLinus Torvalds 			/* Packet will be striped */
12721da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
12731da177e4SLinus Torvalds 		}
12741da177e4SLinus Torvalds 
127543b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
12761da177e4SLinus Torvalds 			return -EFAULT;
12771da177e4SLinus Torvalds 		total += sizeof(pi);
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds 
1280f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
1281f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1282d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
1283f43798c2SRusty Russell 			return -EINVAL;
1284f43798c2SRusty Russell 
1285f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1286f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1287f43798c2SRusty Russell 
1288f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
1289f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
1290f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
1291f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1292f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1293f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1294f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1295e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
1296e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1297ef3db4a5SMichael S. Tsirkin 			else {
12986b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1299ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
1300ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
1301ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
1302ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1303ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1304ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
1305ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
1306ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1307ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1308ef3db4a5SMichael S. Tsirkin 			}
1309f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1310f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1311f43798c2SRusty Russell 		} else
1312f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1313f43798c2SRusty Russell 
1314f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1315f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
131655508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
1317f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
131810a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
131910a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1320f43798c2SRusty Russell 		} /* else everything is zero */
1321f43798c2SRusty Russell 
132243b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
132343b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
1324f43798c2SRusty Russell 			return -EFAULT;
1325d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
1326f43798c2SRusty Russell 	}
1327f43798c2SRusty Russell 
13281da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
13291da177e4SLinus Torvalds 
133043b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
133105c2828cSMichael S. Tsirkin 	total += skb->len;
13321da177e4SLinus Torvalds 
133309f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
133409f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	return total;
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
133954f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
134005c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
134105c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
13421da177e4SLinus Torvalds {
13431da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
13441da177e4SLinus Torvalds 	struct sk_buff *skb;
134505c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
13461da177e4SLinus Torvalds 
13473872baf6SRami Rosen 	tun_debug(KERN_INFO, tun, "tun_do_read\n");
13481da177e4SLinus Torvalds 
134961a5ff15SAmos Kong 	if (unlikely(!noblock))
135054f968d6SJason Wang 		add_wait_queue(&tfile->wq.wait, &wait);
13511da177e4SLinus Torvalds 	while (len) {
13521da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 		/* Read frames from the queue */
135554f968d6SJason Wang 		if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
135605c2828cSMichael S. Tsirkin 			if (noblock) {
13571da177e4SLinus Torvalds 				ret = -EAGAIN;
13581da177e4SLinus Torvalds 				break;
13591da177e4SLinus Torvalds 			}
13601da177e4SLinus Torvalds 			if (signal_pending(current)) {
13611da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
13621da177e4SLinus Torvalds 				break;
13631da177e4SLinus Torvalds 			}
1364c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
1365c70f1829SEric W. Biederman 				ret = -EIO;
1366c70f1829SEric W. Biederman 				break;
1367c70f1829SEric W. Biederman 			}
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
13701da177e4SLinus Torvalds 			schedule();
13711da177e4SLinus Torvalds 			continue;
13721da177e4SLinus Torvalds 		}
13731da177e4SLinus Torvalds 
137454f968d6SJason Wang 		ret = tun_put_user(tun, tfile, skb, iv, len);
13751da177e4SLinus Torvalds 		kfree_skb(skb);
13761da177e4SLinus Torvalds 		break;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
138061a5ff15SAmos Kong 	if (unlikely(!noblock))
138154f968d6SJason Wang 		remove_wait_queue(&tfile->wq.wait, &wait);
13821da177e4SLinus Torvalds 
138305c2828cSMichael S. Tsirkin 	return ret;
138405c2828cSMichael S. Tsirkin }
138505c2828cSMichael S. Tsirkin 
138605c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
138705c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
138805c2828cSMichael S. Tsirkin {
138905c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
139005c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
139105c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
139205c2828cSMichael S. Tsirkin 	ssize_t len, ret;
139305c2828cSMichael S. Tsirkin 
139405c2828cSMichael S. Tsirkin 	if (!tun)
139505c2828cSMichael S. Tsirkin 		return -EBADFD;
139605c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
139705c2828cSMichael S. Tsirkin 	if (len < 0) {
139805c2828cSMichael S. Tsirkin 		ret = -EINVAL;
139905c2828cSMichael S. Tsirkin 		goto out;
140005c2828cSMichael S. Tsirkin 	}
140105c2828cSMichael S. Tsirkin 
140254f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, iv, len,
140354f968d6SJason Wang 			  file->f_flags & O_NONBLOCK);
140405c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
1405631ab46bSEric W. Biederman out:
1406631ab46bSEric W. Biederman 	tun_put(tun);
14071da177e4SLinus Torvalds 	return ret;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
141096442e42SJason Wang static void tun_free_netdev(struct net_device *dev)
141196442e42SJason Wang {
141296442e42SJason Wang 	struct tun_struct *tun = netdev_priv(dev);
141396442e42SJason Wang 
14144008e97fSJason Wang 	BUG_ON(!(list_empty(&tun->disabled)));
141596442e42SJason Wang 	tun_flow_uninit(tun);
14165dbbaf2dSPaul Moore 	security_tun_dev_free_security(tun->security);
141796442e42SJason Wang 	free_netdev(dev);
141896442e42SJason Wang }
141996442e42SJason Wang 
14201da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
14231da177e4SLinus Torvalds 
14240625c883SEric W. Biederman 	tun->owner = INVALID_UID;
14250625c883SEric W. Biederman 	tun->group = INVALID_GID;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
142896442e42SJason Wang 	dev->destructor = tun_free_netdev;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
1431f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1432f019a7a5SEric W. Biederman  * device with netlink.
1433f019a7a5SEric W. Biederman  */
1434f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1435f019a7a5SEric W. Biederman {
1436f019a7a5SEric W. Biederman 	return -EINVAL;
1437f019a7a5SEric W. Biederman }
1438f019a7a5SEric W. Biederman 
1439f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1440f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1441f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1442f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1443f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1444f019a7a5SEric W. Biederman };
1445f019a7a5SEric W. Biederman 
144633dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
144733dccbb0SHerbert Xu {
144854f968d6SJason Wang 	struct tun_file *tfile;
144943815482SEric Dumazet 	wait_queue_head_t *wqueue;
145033dccbb0SHerbert Xu 
145133dccbb0SHerbert Xu 	if (!sock_writeable(sk))
145233dccbb0SHerbert Xu 		return;
145333dccbb0SHerbert Xu 
145433dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
145533dccbb0SHerbert Xu 		return;
145633dccbb0SHerbert Xu 
145743815482SEric Dumazet 	wqueue = sk_sleep(sk);
145843815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
145943815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
146005c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1461c722c625SHerbert Xu 
146254f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
146354f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
146433dccbb0SHerbert Xu }
146533dccbb0SHerbert Xu 
146605c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
146705c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
146805c2828cSMichael S. Tsirkin {
146954f968d6SJason Wang 	int ret;
147054f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
147154f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
147254f968d6SJason Wang 
147354f968d6SJason Wang 	if (!tun)
147454f968d6SJason Wang 		return -EBADFD;
147554f968d6SJason Wang 	ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
14760690899bSMichael S. Tsirkin 			   m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
147754f968d6SJason Wang 	tun_put(tun);
147854f968d6SJason Wang 	return ret;
147905c2828cSMichael S. Tsirkin }
148005c2828cSMichael S. Tsirkin 
148154f968d6SJason Wang 
148205c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
148305c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
148405c2828cSMichael S. Tsirkin 		       int flags)
148505c2828cSMichael S. Tsirkin {
148654f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
148754f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
148805c2828cSMichael S. Tsirkin 	int ret;
148954f968d6SJason Wang 
149054f968d6SJason Wang 	if (!tun)
149154f968d6SJason Wang 		return -EBADFD;
149254f968d6SJason Wang 
14933811ae76SGao feng 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
14943811ae76SGao feng 		ret = -EINVAL;
14953811ae76SGao feng 		goto out;
14963811ae76SGao feng 	}
149754f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
149805c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
149905c2828cSMichael S. Tsirkin 	if (ret > total_len) {
150005c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
150105c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
150205c2828cSMichael S. Tsirkin 	}
15033811ae76SGao feng out:
150454f968d6SJason Wang 	tun_put(tun);
150505c2828cSMichael S. Tsirkin 	return ret;
150605c2828cSMichael S. Tsirkin }
150705c2828cSMichael S. Tsirkin 
15081ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
15091ab5ecb9SStanislav Kinsbursky {
15101ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
15111ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
15121ab5ecb9SStanislav Kinsbursky 	return 0;
15131ab5ecb9SStanislav Kinsbursky }
15141ab5ecb9SStanislav Kinsbursky 
151505c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
151605c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
151705c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
151805c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
15191ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
152005c2828cSMichael S. Tsirkin };
152105c2828cSMichael S. Tsirkin 
152233dccbb0SHerbert Xu static struct proto tun_proto = {
152333dccbb0SHerbert Xu 	.name		= "tun",
152433dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
152554f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
152633dccbb0SHerbert Xu };
1527f019a7a5SEric W. Biederman 
1528980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1529980c9e8cSDavid Woodhouse {
1530980c9e8cSDavid Woodhouse 	int flags = 0;
1531980c9e8cSDavid Woodhouse 
1532980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1533980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1534980c9e8cSDavid Woodhouse 	else
1535980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1536980c9e8cSDavid Woodhouse 
1537980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1538980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1539980c9e8cSDavid Woodhouse 
15405d097109SMichael S. Tsirkin 	/* This flag has no real effect.  We track the value for backwards
15415d097109SMichael S. Tsirkin 	 * compatibility.
15425d097109SMichael S. Tsirkin 	 */
1543980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1544980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1545980c9e8cSDavid Woodhouse 
1546980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1547980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1548980c9e8cSDavid Woodhouse 
1549c8d68e6bSJason Wang 	if (tun->flags & TUN_TAP_MQ)
1550c8d68e6bSJason Wang 		flags |= IFF_MULTI_QUEUE;
1551c8d68e6bSJason Wang 
1552274038f8SPavel Emelyanov 	if (tun->flags & TUN_PERSIST)
1553274038f8SPavel Emelyanov 		flags |= IFF_PERSIST;
1554274038f8SPavel Emelyanov 
1555980c9e8cSDavid Woodhouse 	return flags;
1556980c9e8cSDavid Woodhouse }
1557980c9e8cSDavid Woodhouse 
1558980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1559980c9e8cSDavid Woodhouse 			      char *buf)
1560980c9e8cSDavid Woodhouse {
1561980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1562980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1563980c9e8cSDavid Woodhouse }
1564980c9e8cSDavid Woodhouse 
1565980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1566980c9e8cSDavid Woodhouse 			      char *buf)
1567980c9e8cSDavid Woodhouse {
1568980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15690625c883SEric W. Biederman 	return uid_valid(tun->owner)?
15700625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15710625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
15720625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1573980c9e8cSDavid Woodhouse }
1574980c9e8cSDavid Woodhouse 
1575980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1576980c9e8cSDavid Woodhouse 			      char *buf)
1577980c9e8cSDavid Woodhouse {
1578980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
15790625c883SEric W. Biederman 	return gid_valid(tun->group) ?
15800625c883SEric W. Biederman 		sprintf(buf, "%u\n",
15810625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
15820625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1583980c9e8cSDavid Woodhouse }
1584980c9e8cSDavid Woodhouse 
1585980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1586980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1587980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1588980c9e8cSDavid Woodhouse 
1589d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	struct tun_struct *tun;
159254f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
15931da177e4SLinus Torvalds 	struct net_device *dev;
15941da177e4SLinus Torvalds 	int err;
15951da177e4SLinus Torvalds 
15967c0c3b1aSJason Wang 	if (tfile->detached)
15977c0c3b1aSJason Wang 		return -EINVAL;
15987c0c3b1aSJason Wang 
159974a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
160074a3e5a7SEric W. Biederman 	if (dev) {
1601f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1602f85ba780SDavid Woodhouse 			return -EBUSY;
160374a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
160474a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
160574a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
160674a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
160774a3e5a7SEric W. Biederman 		else
160874a3e5a7SEric W. Biederman 			return -EINVAL;
160974a3e5a7SEric W. Biederman 
16108e6d91aeSJason Wang 		if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
16118e6d91aeSJason Wang 		    !!(tun->flags & TUN_TAP_MQ))
16128e6d91aeSJason Wang 			return -EINVAL;
16138e6d91aeSJason Wang 
1614cde8b15fSJason Wang 		if (tun_not_capable(tun))
16152b980dbdSPaul Moore 			return -EPERM;
16165dbbaf2dSPaul Moore 		err = security_tun_dev_open(tun->security);
16172b980dbdSPaul Moore 		if (err < 0)
16182b980dbdSPaul Moore 			return err;
16192b980dbdSPaul Moore 
1620a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1621a7385ba2SEric W. Biederman 		if (err < 0)
1622a7385ba2SEric W. Biederman 			return err;
16234008e97fSJason Wang 
16244008e97fSJason Wang 		if (tun->flags & TUN_TAP_MQ &&
1625e8dbad66SJason Wang 		    (tun->numqueues + tun->numdisabled > 1)) {
1626e8dbad66SJason Wang 			/* One or more queue has already been attached, no need
1627e8dbad66SJason Wang 			 * to initialize the device again.
1628e8dbad66SJason Wang 			 */
1629e8dbad66SJason Wang 			return 0;
1630e8dbad66SJason Wang 		}
163186a264abSDavid Howells 	}
16321da177e4SLinus Torvalds 	else {
16331da177e4SLinus Torvalds 		char *name;
16341da177e4SLinus Torvalds 		unsigned long flags = 0;
1635edfb6a14SJason Wang 		int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
1636edfb6a14SJason Wang 			     MAX_TAP_QUEUES : 1;
16371da177e4SLinus Torvalds 
1638c260b772SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1639ca6bb5d7SDavid Woodhouse 			return -EPERM;
16402b980dbdSPaul Moore 		err = security_tun_dev_create();
16412b980dbdSPaul Moore 		if (err < 0)
16422b980dbdSPaul Moore 			return err;
1643ca6bb5d7SDavid Woodhouse 
16441da177e4SLinus Torvalds 		/* Set dev type */
16451da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
16461da177e4SLinus Torvalds 			/* TUN device */
16471da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
16481da177e4SLinus Torvalds 			name = "tun%d";
16491da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
16501da177e4SLinus Torvalds 			/* TAP device */
16511da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
16521da177e4SLinus Torvalds 			name = "tap%d";
16531da177e4SLinus Torvalds 		} else
165436989b90SKusanagi Kouichi 			return -EINVAL;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 		if (*ifr->ifr_name)
16571da177e4SLinus Torvalds 			name = ifr->ifr_name;
16581da177e4SLinus Torvalds 
1659c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1660edfb6a14SJason Wang 				       tun_setup, queues, queues);
1661edfb6a14SJason Wang 
16621da177e4SLinus Torvalds 		if (!dev)
16631da177e4SLinus Torvalds 			return -ENOMEM;
16641da177e4SLinus Torvalds 
1665fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1666f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1667758e43b7SStephen Hemminger 
16681da177e4SLinus Torvalds 		tun = netdev_priv(dev);
16691da177e4SLinus Torvalds 		tun->dev = dev;
16701da177e4SLinus Torvalds 		tun->flags = flags;
1671f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1672d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
16731da177e4SLinus Torvalds 
167454f968d6SJason Wang 		tun->filter_attached = false;
167554f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
167633dccbb0SHerbert Xu 
167796442e42SJason Wang 		spin_lock_init(&tun->lock);
167896442e42SJason Wang 
16795dbbaf2dSPaul Moore 		err = security_tun_dev_alloc_security(&tun->security);
16805dbbaf2dSPaul Moore 		if (err < 0)
16815dbbaf2dSPaul Moore 			goto err_free_dev;
16822b980dbdSPaul Moore 
16831da177e4SLinus Torvalds 		tun_net_init(dev);
1684944a1376SPavel Emelyanov 		tun_flow_init(tun);
168596442e42SJason Wang 
168688255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
168788255375SMichał Mirosław 			TUN_USER_FEATURES;
168888255375SMichał Mirosław 		dev->features = dev->hw_features;
1689c0317998SJason Wang 		dev->vlan_features = dev->features;
169088255375SMichał Mirosław 
16914008e97fSJason Wang 		INIT_LIST_HEAD(&tun->disabled);
1692eb0fb363SJason Wang 		err = tun_attach(tun, file);
1693eb0fb363SJason Wang 		if (err < 0)
1694eb0fb363SJason Wang 			goto err_free_dev;
1695eb0fb363SJason Wang 
16961da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
16971da177e4SLinus Torvalds 		if (err < 0)
169854f968d6SJason Wang 			goto err_free_dev;
16999c3fea6aSHerbert Xu 
1700980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1701980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1702980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
17036b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1704af668b3cSMichael S. Tsirkin 	}
1705980c9e8cSDavid Woodhouse 
1706eb0fb363SJason Wang 	netif_carrier_on(tun->dev);
17071da177e4SLinus Torvalds 
17086b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
17111da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1712a26af1e0SNathaniel Filardo 	else
1713a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
17141da177e4SLinus Torvalds 
17155d097109SMichael S. Tsirkin 	/* This flag has no real effect.  We track the value for backwards
17165d097109SMichael S. Tsirkin 	 * compatibility.
17175d097109SMichael S. Tsirkin 	 */
17181da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
17191da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1720a26af1e0SNathaniel Filardo 	else
1721a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
17221da177e4SLinus Torvalds 
1723f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1724f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1725f43798c2SRusty Russell 	else
1726f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1727f43798c2SRusty Russell 
1728c8d68e6bSJason Wang 	if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1729c8d68e6bSJason Wang 		tun->flags |= TUN_TAP_MQ;
1730c8d68e6bSJason Wang 	else
1731c8d68e6bSJason Wang 		tun->flags &= ~TUN_TAP_MQ;
1732c8d68e6bSJason Wang 
1733e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1734e35259a9SMax Krasnyansky 	 * xoff state.
1735e35259a9SMax Krasnyansky 	 */
1736e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1737c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1738e35259a9SMax Krasnyansky 
17391da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
17401da177e4SLinus Torvalds 	return 0;
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds  err_free_dev:
17431da177e4SLinus Torvalds 	free_netdev(dev);
17441da177e4SLinus Torvalds 	return err;
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
17479ce99cf6SRami Rosen static void tun_get_iff(struct net *net, struct tun_struct *tun,
1748876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1749e3b99556SMark McLoughlin {
17506b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1751e3b99556SMark McLoughlin 
1752e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1753e3b99556SMark McLoughlin 
1754980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1755e3b99556SMark McLoughlin 
1756e3b99556SMark McLoughlin }
1757e3b99556SMark McLoughlin 
17585228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
17595228ddc9SRusty Russell  * privs required. */
176088255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
17615228ddc9SRusty Russell {
1762c8f44affSMichał Mirosław 	netdev_features_t features = 0;
17635228ddc9SRusty Russell 
17645228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
176588255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
17665228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
17675228ddc9SRusty Russell 
17685228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
17695228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
17705228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
17715228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
17725228ddc9SRusty Russell 			}
17735228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
17745228ddc9SRusty Russell 				features |= NETIF_F_TSO;
17755228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
17765228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
17775228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
17785228ddc9SRusty Russell 		}
1779e36aa25aSSridhar Samudrala 
1780e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1781e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1782e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1783e36aa25aSSridhar Samudrala 		}
17845228ddc9SRusty Russell 	}
17855228ddc9SRusty Russell 
17865228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
17875228ddc9SRusty Russell 	 * trying to set them. */
17885228ddc9SRusty Russell 	if (arg)
17895228ddc9SRusty Russell 		return -EINVAL;
17905228ddc9SRusty Russell 
179188255375SMichał Mirosław 	tun->set_features = features;
179288255375SMichał Mirosław 	netdev_update_features(tun->dev);
17935228ddc9SRusty Russell 
17945228ddc9SRusty Russell 	return 0;
17955228ddc9SRusty Russell }
17965228ddc9SRusty Russell 
1797c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1798c8d68e6bSJason Wang {
1799c8d68e6bSJason Wang 	int i;
1800c8d68e6bSJason Wang 	struct tun_file *tfile;
1801c8d68e6bSJason Wang 
1802c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1803b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1804c8d68e6bSJason Wang 		sk_detach_filter(tfile->socket.sk);
1805c8d68e6bSJason Wang 	}
1806c8d68e6bSJason Wang 
1807c8d68e6bSJason Wang 	tun->filter_attached = false;
1808c8d68e6bSJason Wang }
1809c8d68e6bSJason Wang 
1810c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1811c8d68e6bSJason Wang {
1812c8d68e6bSJason Wang 	int i, ret = 0;
1813c8d68e6bSJason Wang 	struct tun_file *tfile;
1814c8d68e6bSJason Wang 
1815c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1816b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1817c8d68e6bSJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1818c8d68e6bSJason Wang 		if (ret) {
1819c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1820c8d68e6bSJason Wang 			return ret;
1821c8d68e6bSJason Wang 		}
1822c8d68e6bSJason Wang 	}
1823c8d68e6bSJason Wang 
1824c8d68e6bSJason Wang 	tun->filter_attached = true;
1825c8d68e6bSJason Wang 	return ret;
1826c8d68e6bSJason Wang }
1827c8d68e6bSJason Wang 
1828c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1829c8d68e6bSJason Wang {
1830c8d68e6bSJason Wang 	struct tun_file *tfile;
1831c8d68e6bSJason Wang 	int i;
1832c8d68e6bSJason Wang 
1833c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1834b8deabd3SJason Wang 		tfile = rtnl_dereference(tun->tfiles[i]);
1835c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1836c8d68e6bSJason Wang 	}
1837c8d68e6bSJason Wang }
1838c8d68e6bSJason Wang 
1839cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1840cde8b15fSJason Wang {
1841cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1842cde8b15fSJason Wang 	struct tun_struct *tun;
1843cde8b15fSJason Wang 	int ret = 0;
1844cde8b15fSJason Wang 
1845cde8b15fSJason Wang 	rtnl_lock();
1846cde8b15fSJason Wang 
1847cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
18484008e97fSJason Wang 		tun = tfile->detached;
18495dbbaf2dSPaul Moore 		if (!tun) {
1850cde8b15fSJason Wang 			ret = -EINVAL;
18515dbbaf2dSPaul Moore 			goto unlock;
18525dbbaf2dSPaul Moore 		}
18535dbbaf2dSPaul Moore 		ret = security_tun_dev_attach_queue(tun->security);
18545dbbaf2dSPaul Moore 		if (ret < 0)
18555dbbaf2dSPaul Moore 			goto unlock;
1856cde8b15fSJason Wang 		ret = tun_attach(tun, file);
18574008e97fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
1858b8deabd3SJason Wang 		tun = rtnl_dereference(tfile->tun);
18599e85722dSJason Wang 		if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached)
18604008e97fSJason Wang 			ret = -EINVAL;
1861cde8b15fSJason Wang 		else
18624008e97fSJason Wang 			__tun_detach(tfile, false);
18634008e97fSJason Wang 	} else
1864cde8b15fSJason Wang 		ret = -EINVAL;
1865cde8b15fSJason Wang 
18665dbbaf2dSPaul Moore unlock:
1867cde8b15fSJason Wang 	rtnl_unlock();
1868cde8b15fSJason Wang 	return ret;
1869cde8b15fSJason Wang }
1870cde8b15fSJason Wang 
187150857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
187250857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
18731da177e4SLinus Torvalds {
187436b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1875631ab46bSEric W. Biederman 	struct tun_struct *tun;
18761da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
18771da177e4SLinus Torvalds 	struct ifreq ifr;
18780625c883SEric W. Biederman 	kuid_t owner;
18790625c883SEric W. Biederman 	kgid_t group;
188033dccbb0SHerbert Xu 	int sndbuf;
1881d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1882f271b2ccSMax Krasnyansky 	int ret;
18831da177e4SLinus Torvalds 
1884cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
188550857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
18861da177e4SLinus Torvalds 			return -EFAULT;
18878bbb1813SDavid S. Miller 	} else {
1888a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
18898bbb1813SDavid S. Miller 	}
1890631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1891631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1892631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1893631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1894631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1895cde8b15fSJason Wang 				IFF_VNET_HDR | IFF_MULTI_QUEUE,
1896631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1897cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
1898cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
1899631ab46bSEric W. Biederman 
1900c8d68e6bSJason Wang 	ret = 0;
1901876bfd4dSHerbert Xu 	rtnl_lock();
1902876bfd4dSHerbert Xu 
190336b50babSEric W. Biederman 	tun = __tun_get(tfile);
19041da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
19051da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
19061da177e4SLinus Torvalds 
1907876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
19081da177e4SLinus Torvalds 
1909876bfd4dSHerbert Xu 		if (ret)
1910876bfd4dSHerbert Xu 			goto unlock;
19111da177e4SLinus Torvalds 
191250857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1913876bfd4dSHerbert Xu 			ret = -EFAULT;
1914876bfd4dSHerbert Xu 		goto unlock;
19151da177e4SLinus Torvalds 	}
19161da177e4SLinus Torvalds 
1917876bfd4dSHerbert Xu 	ret = -EBADFD;
19181da177e4SLinus Torvalds 	if (!tun)
1919876bfd4dSHerbert Xu 		goto unlock;
19201da177e4SLinus Torvalds 
19211e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
19221da177e4SLinus Torvalds 
1923631ab46bSEric W. Biederman 	ret = 0;
19241da177e4SLinus Torvalds 	switch (cmd) {
1925e3b99556SMark McLoughlin 	case TUNGETIFF:
19269ce99cf6SRami Rosen 		tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1927e3b99556SMark McLoughlin 
192850857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1929631ab46bSEric W. Biederman 			ret = -EFAULT;
1930e3b99556SMark McLoughlin 		break;
1931e3b99556SMark McLoughlin 
19321da177e4SLinus Torvalds 	case TUNSETNOCSUM:
19331da177e4SLinus Torvalds 		/* Disable/Enable checksum */
19341da177e4SLinus Torvalds 
193588255375SMichał Mirosław 		/* [unimplemented] */
193688255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
19376b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
19381da177e4SLinus Torvalds 		break;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	case TUNSETPERSIST:
194154f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
194254f968d6SJason Wang 		 * module to prevent the module being unprobed.
194354f968d6SJason Wang 		 */
1944dd38bd85SJason Wang 		if (arg && !(tun->flags & TUN_PERSIST)) {
19451da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
194654f968d6SJason Wang 			__module_get(THIS_MODULE);
1947dd38bd85SJason Wang 		}
1948dd38bd85SJason Wang 		if (!arg && (tun->flags & TUN_PERSIST)) {
19491da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
195054f968d6SJason Wang 			module_put(THIS_MODULE);
195154f968d6SJason Wang 		}
19521da177e4SLinus Torvalds 
19536b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
19546b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
19551da177e4SLinus Torvalds 		break;
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds 	case TUNSETOWNER:
19581da177e4SLinus Torvalds 		/* Set owner of the device */
19590625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
19600625c883SEric W. Biederman 		if (!uid_valid(owner)) {
19610625c883SEric W. Biederman 			ret = -EINVAL;
19620625c883SEric W. Biederman 			break;
19630625c883SEric W. Biederman 		}
19640625c883SEric W. Biederman 		tun->owner = owner;
19651e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
19660625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
19671da177e4SLinus Torvalds 		break;
19681da177e4SLinus Torvalds 
19698c644623SGuido Guenther 	case TUNSETGROUP:
19708c644623SGuido Guenther 		/* Set group of the device */
19710625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
19720625c883SEric W. Biederman 		if (!gid_valid(group)) {
19730625c883SEric W. Biederman 			ret = -EINVAL;
19740625c883SEric W. Biederman 			break;
19750625c883SEric W. Biederman 		}
19760625c883SEric W. Biederman 		tun->group = group;
19771e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
19780625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
19798c644623SGuido Guenther 		break;
19808c644623SGuido Guenther 
1981ff4cc3acSMike Kershaw 	case TUNSETLINK:
1982ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1983ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
19846b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
19856b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
198648abfe05SDavid S. Miller 			ret = -EBUSY;
1987ff4cc3acSMike Kershaw 		} else {
1988ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
19896b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
19906b8a66eeSJoe Perches 				  tun->dev->type);
199148abfe05SDavid S. Miller 			ret = 0;
1992ff4cc3acSMike Kershaw 		}
1993631ab46bSEric W. Biederman 		break;
1994ff4cc3acSMike Kershaw 
19951da177e4SLinus Torvalds #ifdef TUN_DEBUG
19961da177e4SLinus Torvalds 	case TUNSETDEBUG:
19971da177e4SLinus Torvalds 		tun->debug = arg;
19981da177e4SLinus Torvalds 		break;
19991da177e4SLinus Torvalds #endif
20005228ddc9SRusty Russell 	case TUNSETOFFLOAD:
200188255375SMichał Mirosław 		ret = set_offload(tun, arg);
2002631ab46bSEric W. Biederman 		break;
20035228ddc9SRusty Russell 
2004f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
2005f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
2006631ab46bSEric W. Biederman 		ret = -EINVAL;
2007f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
2008631ab46bSEric W. Biederman 			break;
2009c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
2010631ab46bSEric W. Biederman 		break;
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
2013b595076aSUwe Kleine-König 		/* Get hw address */
2014f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2015f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
201650857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
2017631ab46bSEric W. Biederman 			ret = -EFAULT;
2018631ab46bSEric W. Biederman 		break;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
2021f271b2ccSMax Krasnyansky 		/* Set hw address */
20226b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
20236b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
202440102371SKim B. Heino 
202540102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
2026631ab46bSEric W. Biederman 		break;
202733dccbb0SHerbert Xu 
202833dccbb0SHerbert Xu 	case TUNGETSNDBUF:
202954f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
203033dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
203133dccbb0SHerbert Xu 			ret = -EFAULT;
203233dccbb0SHerbert Xu 		break;
203333dccbb0SHerbert Xu 
203433dccbb0SHerbert Xu 	case TUNSETSNDBUF:
203533dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
203633dccbb0SHerbert Xu 			ret = -EFAULT;
203733dccbb0SHerbert Xu 			break;
203833dccbb0SHerbert Xu 		}
203933dccbb0SHerbert Xu 
2040c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
2041c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
204233dccbb0SHerbert Xu 		break;
204333dccbb0SHerbert Xu 
2044d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
2045d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
2046d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2047d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2048d9d52b51SMichael S. Tsirkin 		break;
2049d9d52b51SMichael S. Tsirkin 
2050d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
2051d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2052d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
2053d9d52b51SMichael S. Tsirkin 			break;
2054d9d52b51SMichael S. Tsirkin 		}
2055d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2056d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
2057d9d52b51SMichael S. Tsirkin 			break;
2058d9d52b51SMichael S. Tsirkin 		}
2059d9d52b51SMichael S. Tsirkin 
2060d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
2061d9d52b51SMichael S. Tsirkin 		break;
2062d9d52b51SMichael S. Tsirkin 
206399405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
206499405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
206599405162SMichael S. Tsirkin 		ret = -EINVAL;
206699405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
206799405162SMichael S. Tsirkin 			break;
206899405162SMichael S. Tsirkin 		ret = -EFAULT;
206954f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
207099405162SMichael S. Tsirkin 			break;
207199405162SMichael S. Tsirkin 
2072c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
207399405162SMichael S. Tsirkin 		break;
207499405162SMichael S. Tsirkin 
207599405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
207699405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
207799405162SMichael S. Tsirkin 		ret = -EINVAL;
207899405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
207999405162SMichael S. Tsirkin 			break;
2080c8d68e6bSJason Wang 		ret = 0;
2081c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
208299405162SMichael S. Tsirkin 		break;
208399405162SMichael S. Tsirkin 
20841da177e4SLinus Torvalds 	default:
2085631ab46bSEric W. Biederman 		ret = -EINVAL;
2086631ab46bSEric W. Biederman 		break;
2087ee289b64SJoe Perches 	}
20881da177e4SLinus Torvalds 
2089876bfd4dSHerbert Xu unlock:
2090876bfd4dSHerbert Xu 	rtnl_unlock();
2091876bfd4dSHerbert Xu 	if (tun)
2092631ab46bSEric W. Biederman 		tun_put(tun);
2093631ab46bSEric W. Biederman 	return ret;
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds 
209650857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
209750857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
209850857e2aSArnd Bergmann {
209950857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
210050857e2aSArnd Bergmann }
210150857e2aSArnd Bergmann 
210250857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
210350857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
210450857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
210550857e2aSArnd Bergmann {
210650857e2aSArnd Bergmann 	switch (cmd) {
210750857e2aSArnd Bergmann 	case TUNSETIFF:
210850857e2aSArnd Bergmann 	case TUNGETIFF:
210950857e2aSArnd Bergmann 	case TUNSETTXFILTER:
211050857e2aSArnd Bergmann 	case TUNGETSNDBUF:
211150857e2aSArnd Bergmann 	case TUNSETSNDBUF:
211250857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
211350857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
211450857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
211550857e2aSArnd Bergmann 		break;
211650857e2aSArnd Bergmann 	default:
211750857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
211850857e2aSArnd Bergmann 		break;
211950857e2aSArnd Bergmann 	}
212050857e2aSArnd Bergmann 
212150857e2aSArnd Bergmann 	/*
212250857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
212350857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
212450857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
212550857e2aSArnd Bergmann 	 * contents.
212650857e2aSArnd Bergmann 	 */
212750857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
212850857e2aSArnd Bergmann }
212950857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
213050857e2aSArnd Bergmann 
21311da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
21321da177e4SLinus Torvalds {
213354f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
21341da177e4SLinus Torvalds 	int ret;
21351da177e4SLinus Torvalds 
213654f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
21379d319522SJonathan Corbet 		goto out;
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	if (on) {
2140609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
21411da177e4SLinus Torvalds 		if (ret)
21429d319522SJonathan Corbet 			goto out;
214354f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
21441da177e4SLinus Torvalds 	} else
214554f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
21469d319522SJonathan Corbet 	ret = 0;
21479d319522SJonathan Corbet out:
21489d319522SJonathan Corbet 	return ret;
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
21521da177e4SLinus Torvalds {
2153631ab46bSEric W. Biederman 	struct tun_file *tfile;
2154deed49fbSThomas Gleixner 
21556b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
2156631ab46bSEric W. Biederman 
215754f968d6SJason Wang 	tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
215854f968d6SJason Wang 					    &tun_proto);
2159631ab46bSEric W. Biederman 	if (!tfile)
2160631ab46bSEric W. Biederman 		return -ENOMEM;
21616e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, NULL);
216236b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
216354f968d6SJason Wang 	tfile->flags = 0;
216454f968d6SJason Wang 
216554f968d6SJason Wang 	rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
216654f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
216754f968d6SJason Wang 
216854f968d6SJason Wang 	tfile->socket.file = file;
216954f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
217054f968d6SJason Wang 
217154f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
217254f968d6SJason Wang 	sk_change_net(&tfile->sk, tfile->net);
217354f968d6SJason Wang 
217454f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
217554f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
217654f968d6SJason Wang 
2177631ab46bSEric W. Biederman 	file->private_data = tfile;
217854f968d6SJason Wang 	set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
21794008e97fSJason Wang 	INIT_LIST_HEAD(&tfile->next);
218054f968d6SJason Wang 
218119a6afb2SJason Wang 	sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
218219a6afb2SJason Wang 
21831da177e4SLinus Torvalds 	return 0;
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
21871da177e4SLinus Torvalds {
2188631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
218954f968d6SJason Wang 	struct net *net = tfile->net;
21901da177e4SLinus Torvalds 
2191c8d68e6bSJason Wang 	tun_detach(tfile, true);
219254f968d6SJason Wang 	put_net(net);
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	return 0;
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
2197d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
21981da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
21991da177e4SLinus Torvalds 	.llseek = no_llseek,
2200ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
2201ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
2202ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
2203ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
22041da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
2205876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
220650857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
220750857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
220850857e2aSArnd Bergmann #endif
22091da177e4SLinus Torvalds 	.open	= tun_chr_open,
22101da177e4SLinus Torvalds 	.release = tun_chr_close,
22111da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
22121da177e4SLinus Torvalds };
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
22151da177e4SLinus Torvalds 	.minor = TUN_MINOR,
22161da177e4SLinus Torvalds 	.name = "tun",
2217e454cea2SKay Sievers 	.nodename = "net/tun",
22181da177e4SLinus Torvalds 	.fops = &tun_fops,
22191da177e4SLinus Torvalds };
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds /* ethtool interface */
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
22241da177e4SLinus Torvalds {
22251da177e4SLinus Torvalds 	cmd->supported		= 0;
22261da177e4SLinus Torvalds 	cmd->advertising	= 0;
222770739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
22281da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
22291da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
22301da177e4SLinus Torvalds 	cmd->phy_address	= 0;
22311da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
22321da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
22331da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
22341da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
22351da177e4SLinus Torvalds 	return 0;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
22391da177e4SLinus Torvalds {
22401da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22411da177e4SLinus Torvalds 
224233a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
224333a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
22461da177e4SLinus Torvalds 	case TUN_TUN_DEV:
224733a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
22481da177e4SLinus Torvalds 		break;
22491da177e4SLinus Torvalds 	case TUN_TAP_DEV:
225033a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
22511da177e4SLinus Torvalds 		break;
22521da177e4SLinus Torvalds 	}
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds #ifdef TUN_DEBUG
22581da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22591da177e4SLinus Torvalds 	return tun->debug;
22601da177e4SLinus Torvalds #else
22611da177e4SLinus Torvalds 	return -EOPNOTSUPP;
22621da177e4SLinus Torvalds #endif
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
22661da177e4SLinus Torvalds {
22671da177e4SLinus Torvalds #ifdef TUN_DEBUG
22681da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
22691da177e4SLinus Torvalds 	tun->debug = value;
22701da177e4SLinus Torvalds #endif
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
22737282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
22741da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
22751da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
22761da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
22771da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
2278bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
22791da177e4SLinus Torvalds };
22801da177e4SLinus Torvalds 
228179d17604SPavel Emelyanov 
22821da177e4SLinus Torvalds static int __init tun_init(void)
22831da177e4SLinus Torvalds {
22841da177e4SLinus Torvalds 	int ret = 0;
22851da177e4SLinus Torvalds 
22866b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
22876b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
22881da177e4SLinus Torvalds 
2289f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
229079d17604SPavel Emelyanov 	if (ret) {
22916b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
2292f019a7a5SEric W. Biederman 		goto err_linkops;
229379d17604SPavel Emelyanov 	}
229479d17604SPavel Emelyanov 
22951da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
229679d17604SPavel Emelyanov 	if (ret) {
22976b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
229879d17604SPavel Emelyanov 		goto err_misc;
229979d17604SPavel Emelyanov 	}
230079d17604SPavel Emelyanov 	return  0;
230179d17604SPavel Emelyanov err_misc:
2302f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2303f019a7a5SEric W. Biederman err_linkops:
23041da177e4SLinus Torvalds 	return ret;
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds static void tun_cleanup(void)
23081da177e4SLinus Torvalds {
23091da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2310f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds 
231305c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
231405c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
231505c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
231605c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
231705c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
231805c2828cSMichael S. Tsirkin {
23196e914fc7SJason Wang 	struct tun_file *tfile;
232005c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
232105c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
23226e914fc7SJason Wang 	tfile = file->private_data;
23236e914fc7SJason Wang 	if (!tfile)
232405c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
232554f968d6SJason Wang 	return &tfile->socket;
232605c2828cSMichael S. Tsirkin }
232705c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
232805c2828cSMichael S. Tsirkin 
23291da177e4SLinus Torvalds module_init(tun_init);
23301da177e4SLinus Torvalds module_exit(tun_cleanup);
23311da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
23321da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
23331da177e4SLinus Torvalds MODULE_LICENSE("GPL");
23341da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2335578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2336