xref: /openbmc/linux/drivers/net/tun.c (revision cde8b15f1aabe327038ee4e0e11dd6b798572f69)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  TUN - Universal TUN/TAP device driver.
31da177e4SLinus Torvalds  *  Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *  it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *  the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *  (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131da177e4SLinus Torvalds  *  GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds  *  Changes:
201da177e4SLinus Torvalds  *
21ff4cc3acSMike Kershaw  *  Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22ff4cc3acSMike Kershaw  *    Add TUNSETLINK ioctl to set the link encapsulation
23ff4cc3acSMike Kershaw  *
241da177e4SLinus Torvalds  *  Mark Smith <markzzzsmith@yahoo.com.au>
25344dc8edSJoe Perches  *    Use eth_random_addr() for tap MAC address.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
281da177e4SLinus Torvalds  *    Fixes in packet dropping, queue length setting and queue wakeup.
291da177e4SLinus Torvalds  *    Increased default tx queue length.
301da177e4SLinus Torvalds  *    Added ethtool API.
311da177e4SLinus Torvalds  *    Minor cleanups
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  *  Daniel Podlejski <underley@underley.eu.org>
341da177e4SLinus Torvalds  *    Modifications for 2.3.99-pre5 kernel.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
376b8a66eeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
386b8a66eeSJoe Perches 
391da177e4SLinus Torvalds #define DRV_NAME	"tun"
401da177e4SLinus Torvalds #define DRV_VERSION	"1.6"
411da177e4SLinus Torvalds #define DRV_DESCRIPTION	"Universal TUN/TAP device driver"
421da177e4SLinus Torvalds #define DRV_COPYRIGHT	"(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/module.h>
451da177e4SLinus Torvalds #include <linux/errno.h>
461da177e4SLinus Torvalds #include <linux/kernel.h>
471da177e4SLinus Torvalds #include <linux/major.h>
481da177e4SLinus Torvalds #include <linux/slab.h>
491da177e4SLinus Torvalds #include <linux/poll.h>
501da177e4SLinus Torvalds #include <linux/fcntl.h>
511da177e4SLinus Torvalds #include <linux/init.h>
521da177e4SLinus Torvalds #include <linux/skbuff.h>
531da177e4SLinus Torvalds #include <linux/netdevice.h>
541da177e4SLinus Torvalds #include <linux/etherdevice.h>
551da177e4SLinus Torvalds #include <linux/miscdevice.h>
561da177e4SLinus Torvalds #include <linux/ethtool.h>
571da177e4SLinus Torvalds #include <linux/rtnetlink.h>
5850857e2aSArnd Bergmann #include <linux/compat.h>
591da177e4SLinus Torvalds #include <linux/if.h>
601da177e4SLinus Torvalds #include <linux/if_arp.h>
611da177e4SLinus Torvalds #include <linux/if_ether.h>
621da177e4SLinus Torvalds #include <linux/if_tun.h>
631da177e4SLinus Torvalds #include <linux/crc32.h>
64d647a591SPavel Emelyanov #include <linux/nsproxy.h>
65f43798c2SRusty Russell #include <linux/virtio_net.h>
6699405162SMichael S. Tsirkin #include <linux/rcupdate.h>
67881d966bSEric W. Biederman #include <net/net_namespace.h>
6879d17604SPavel Emelyanov #include <net/netns/generic.h>
69f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
7033dccbb0SHerbert Xu #include <net/sock.h>
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #include <asm/uaccess.h>
731da177e4SLinus Torvalds 
7414daa021SRusty Russell /* Uncomment to enable debugging */
7514daa021SRusty Russell /* #define TUN_DEBUG 1 */
7614daa021SRusty Russell 
771da177e4SLinus Torvalds #ifdef TUN_DEBUG
781da177e4SLinus Torvalds static int debug;
7914daa021SRusty Russell 
806b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
816b8a66eeSJoe Perches do {								\
826b8a66eeSJoe Perches 	if (tun->debug)						\
836b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
846b8a66eeSJoe Perches } while (0)
856b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
866b8a66eeSJoe Perches do {								\
876b8a66eeSJoe Perches 	if (debug == 2)						\
886b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
896b8a66eeSJoe Perches } while (0)
9014daa021SRusty Russell #else
916b8a66eeSJoe Perches #define tun_debug(level, tun, fmt, args...)			\
926b8a66eeSJoe Perches do {								\
936b8a66eeSJoe Perches 	if (0)							\
946b8a66eeSJoe Perches 		netdev_printk(level, tun->dev, fmt, ##args);	\
956b8a66eeSJoe Perches } while (0)
966b8a66eeSJoe Perches #define DBG1(level, fmt, args...)				\
976b8a66eeSJoe Perches do {								\
986b8a66eeSJoe Perches 	if (0)							\
996b8a66eeSJoe Perches 		printk(level fmt, ##args);			\
1006b8a66eeSJoe Perches } while (0)
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
1030690899bSMichael S. Tsirkin #define GOODCOPY_LEN 128
1040690899bSMichael S. Tsirkin 
105f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
106f271b2ccSMax Krasnyansky struct tap_filter {
107f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
108f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
109f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
110f271b2ccSMax Krasnyansky };
111f271b2ccSMax Krasnyansky 
112c8d68e6bSJason Wang /* 1024 is probably a high enough limit: modern hypervisors seem to support on
113c8d68e6bSJason Wang  * the order of 100-200 CPUs so this leaves us some breathing space if we want
114c8d68e6bSJason Wang  * to match a queue per guest CPU.
115c8d68e6bSJason Wang  */
116c8d68e6bSJason Wang #define MAX_TAP_QUEUES 1024
117c8d68e6bSJason Wang 
11854f968d6SJason Wang /* A tun_file connects an open character device to a tuntap netdevice. It
11954f968d6SJason Wang  * also contains all socket related strctures (except sock_fprog and tap_filter)
12054f968d6SJason Wang  * to serve as one transmit queue for tuntap device. The sock_fprog and
12154f968d6SJason Wang  * tap_filter were kept in tun_struct since they were used for filtering for the
12254f968d6SJason Wang  * netdevice not for a specific queue (at least I didn't see the reqirement for
12354f968d6SJason Wang  * this).
1246e914fc7SJason Wang  *
1256e914fc7SJason Wang  * RCU usage:
1266e914fc7SJason Wang  * The tun_file and tun_struct are loosely coupled, the pointer from on to the
1276e914fc7SJason Wang  * other can only be read while rcu_read_lock or rtnl_lock is held.
12854f968d6SJason Wang  */
129631ab46bSEric W. Biederman struct tun_file {
13054f968d6SJason Wang 	struct sock sk;
13154f968d6SJason Wang 	struct socket socket;
13254f968d6SJason Wang 	struct socket_wq wq;
1336e914fc7SJason Wang 	struct tun_struct __rcu *tun;
13436b50babSEric W. Biederman 	struct net *net;
13554f968d6SJason Wang 	struct fasync_struct *fasync;
13654f968d6SJason Wang 	/* only used for fasnyc */
13754f968d6SJason Wang 	unsigned int flags;
138c8d68e6bSJason Wang 	u16 queue_index;
139631ab46bSEric W. Biederman };
140631ab46bSEric W. Biederman 
14154f968d6SJason Wang /* Since the socket were moved to tun_file, to preserve the behavior of persist
14254f968d6SJason Wang  * device, socket fileter, sndbuf and vnet header size were restore when the
14354f968d6SJason Wang  * file were attached to a persist device.
14454f968d6SJason Wang  */
14514daa021SRusty Russell struct tun_struct {
146c8d68e6bSJason Wang 	struct tun_file __rcu	*tfiles[MAX_TAP_QUEUES];
147c8d68e6bSJason Wang 	unsigned int            numqueues;
148f271b2ccSMax Krasnyansky 	unsigned int 		flags;
1490625c883SEric W. Biederman 	kuid_t			owner;
1500625c883SEric W. Biederman 	kgid_t			group;
15114daa021SRusty Russell 
15214daa021SRusty Russell 	struct net_device	*dev;
153c8f44affSMichał Mirosław 	netdev_features_t	set_features;
15488255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
15588255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
156d9d52b51SMichael S. Tsirkin 
157d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
15854f968d6SJason Wang 	int			sndbuf;
15954f968d6SJason Wang 	struct tap_filter	txflt;
16054f968d6SJason Wang 	struct sock_fprog	fprog;
16154f968d6SJason Wang 	/* protected by rtnl lock */
16254f968d6SJason Wang 	bool			filter_attached;
16314daa021SRusty Russell #ifdef TUN_DEBUG
16414daa021SRusty Russell 	int debug;
16514daa021SRusty Russell #endif
16614daa021SRusty Russell };
16714daa021SRusty Russell 
168c8d68e6bSJason Wang /* We try to identify a flow through its rxhash first. The reason that
169c8d68e6bSJason Wang  * we do not check rxq no. is becuase some cards(e.g 82599), chooses
170c8d68e6bSJason Wang  * the rxq based on the txq where the last packet of the flow comes. As
171c8d68e6bSJason Wang  * the userspace application move between processors, we may get a
172c8d68e6bSJason Wang  * different rxq no. here. If we could not get rxhash, then we would
173c8d68e6bSJason Wang  * hope the rxq no. may help here.
174c8d68e6bSJason Wang  */
175c8d68e6bSJason Wang static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
176c8d68e6bSJason Wang {
177c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
178c8d68e6bSJason Wang 	u32 txq = 0;
179c8d68e6bSJason Wang 	u32 numqueues = 0;
180c8d68e6bSJason Wang 
181c8d68e6bSJason Wang 	rcu_read_lock();
182c8d68e6bSJason Wang 	numqueues = tun->numqueues;
183c8d68e6bSJason Wang 
184c8d68e6bSJason Wang 	txq = skb_get_rxhash(skb);
185c8d68e6bSJason Wang 	if (txq) {
186c8d68e6bSJason Wang 		/* use multiply and shift instead of expensive divide */
187c8d68e6bSJason Wang 		txq = ((u64)txq * numqueues) >> 32;
188c8d68e6bSJason Wang 	} else if (likely(skb_rx_queue_recorded(skb))) {
189c8d68e6bSJason Wang 		txq = skb_get_rx_queue(skb);
190c8d68e6bSJason Wang 		while (unlikely(txq >= numqueues))
191c8d68e6bSJason Wang 			txq -= numqueues;
192c8d68e6bSJason Wang 	}
193c8d68e6bSJason Wang 
194c8d68e6bSJason Wang 	rcu_read_unlock();
195c8d68e6bSJason Wang 	return txq;
196c8d68e6bSJason Wang }
197c8d68e6bSJason Wang 
198*cde8b15fSJason Wang static inline bool tun_not_capable(struct tun_struct *tun)
199*cde8b15fSJason Wang {
200*cde8b15fSJason Wang 	const struct cred *cred = current_cred();
201*cde8b15fSJason Wang 
202*cde8b15fSJason Wang 	return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
203*cde8b15fSJason Wang 		  (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
204*cde8b15fSJason Wang 		!capable(CAP_NET_ADMIN);
205*cde8b15fSJason Wang }
206*cde8b15fSJason Wang 
207c8d68e6bSJason Wang static void tun_set_real_num_queues(struct tun_struct *tun)
208c8d68e6bSJason Wang {
209c8d68e6bSJason Wang 	netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
210c8d68e6bSJason Wang 	netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
211c8d68e6bSJason Wang }
212c8d68e6bSJason Wang 
213c8d68e6bSJason Wang static void __tun_detach(struct tun_file *tfile, bool clean)
214c8d68e6bSJason Wang {
215c8d68e6bSJason Wang 	struct tun_file *ntfile;
216c8d68e6bSJason Wang 	struct tun_struct *tun;
217c8d68e6bSJason Wang 	struct net_device *dev;
218c8d68e6bSJason Wang 
219c8d68e6bSJason Wang 	tun = rcu_dereference_protected(tfile->tun,
220c8d68e6bSJason Wang 					lockdep_rtnl_is_held());
221c8d68e6bSJason Wang 	if (tun) {
222c8d68e6bSJason Wang 		u16 index = tfile->queue_index;
223c8d68e6bSJason Wang 		BUG_ON(index >= tun->numqueues);
224c8d68e6bSJason Wang 		dev = tun->dev;
225c8d68e6bSJason Wang 
226c8d68e6bSJason Wang 		rcu_assign_pointer(tun->tfiles[index],
227c8d68e6bSJason Wang 				   tun->tfiles[tun->numqueues - 1]);
228c8d68e6bSJason Wang 		rcu_assign_pointer(tfile->tun, NULL);
229c8d68e6bSJason Wang 		ntfile = rcu_dereference_protected(tun->tfiles[index],
230c8d68e6bSJason Wang 						   lockdep_rtnl_is_held());
231c8d68e6bSJason Wang 		ntfile->queue_index = index;
232c8d68e6bSJason Wang 
233c8d68e6bSJason Wang 		--tun->numqueues;
234c8d68e6bSJason Wang 		sock_put(&tfile->sk);
235c8d68e6bSJason Wang 
236c8d68e6bSJason Wang 		synchronize_net();
237c8d68e6bSJason Wang 		/* Drop read queue */
238c8d68e6bSJason Wang 		skb_queue_purge(&tfile->sk.sk_receive_queue);
239c8d68e6bSJason Wang 		tun_set_real_num_queues(tun);
240c8d68e6bSJason Wang 
241c8d68e6bSJason Wang 		if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
242c8d68e6bSJason Wang 			if (dev->reg_state == NETREG_REGISTERED)
243c8d68e6bSJason Wang 				unregister_netdevice(dev);
244c8d68e6bSJason Wang 	}
245c8d68e6bSJason Wang 
246c8d68e6bSJason Wang 	if (clean) {
247c8d68e6bSJason Wang 		BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
248c8d68e6bSJason Wang 				 &tfile->socket.flags));
249c8d68e6bSJason Wang 		sk_release_kernel(&tfile->sk);
250c8d68e6bSJason Wang 	}
251c8d68e6bSJason Wang }
252c8d68e6bSJason Wang 
253c8d68e6bSJason Wang static void tun_detach(struct tun_file *tfile, bool clean)
254c8d68e6bSJason Wang {
255c8d68e6bSJason Wang 	rtnl_lock();
256c8d68e6bSJason Wang 	__tun_detach(tfile, clean);
257c8d68e6bSJason Wang 	rtnl_unlock();
258c8d68e6bSJason Wang }
259c8d68e6bSJason Wang 
260c8d68e6bSJason Wang static void tun_detach_all(struct net_device *dev)
261c8d68e6bSJason Wang {
262c8d68e6bSJason Wang 	struct tun_struct *tun = netdev_priv(dev);
263c8d68e6bSJason Wang 	struct tun_file *tfile;
264c8d68e6bSJason Wang 	int i, n = tun->numqueues;
265c8d68e6bSJason Wang 
266c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
267c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
268c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
269c8d68e6bSJason Wang 		BUG_ON(!tfile);
270c8d68e6bSJason Wang 		wake_up_all(&tfile->wq.wait);
271c8d68e6bSJason Wang 		rcu_assign_pointer(tfile->tun, NULL);
272c8d68e6bSJason Wang 		--tun->numqueues;
273c8d68e6bSJason Wang 	}
274c8d68e6bSJason Wang 	BUG_ON(tun->numqueues != 0);
275c8d68e6bSJason Wang 
276c8d68e6bSJason Wang 	synchronize_net();
277c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
278c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
279c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
280c8d68e6bSJason Wang 		/* Drop read queue */
281c8d68e6bSJason Wang 		skb_queue_purge(&tfile->sk.sk_receive_queue);
282c8d68e6bSJason Wang 		sock_put(&tfile->sk);
283c8d68e6bSJason Wang 	}
284c8d68e6bSJason Wang }
285c8d68e6bSJason Wang 
286a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
287a7385ba2SEric W. Biederman {
288631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
28938231b7aSEric W. Biederman 	int err;
290a7385ba2SEric W. Biederman 
29138231b7aSEric W. Biederman 	err = -EINVAL;
292c8d68e6bSJason Wang 	if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
29338231b7aSEric W. Biederman 		goto out;
29438231b7aSEric W. Biederman 
29538231b7aSEric W. Biederman 	err = -EBUSY;
296c8d68e6bSJason Wang 	if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
297c8d68e6bSJason Wang 		goto out;
298c8d68e6bSJason Wang 
299c8d68e6bSJason Wang 	err = -E2BIG;
300c8d68e6bSJason Wang 	if (tun->numqueues == MAX_TAP_QUEUES)
30138231b7aSEric W. Biederman 		goto out;
30238231b7aSEric W. Biederman 
30338231b7aSEric W. Biederman 	err = 0;
30454f968d6SJason Wang 
305c8d68e6bSJason Wang 	/* Re-attach the filter to presist device */
30654f968d6SJason Wang 	if (tun->filter_attached == true) {
30754f968d6SJason Wang 		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
30854f968d6SJason Wang 		if (!err)
30954f968d6SJason Wang 			goto out;
31054f968d6SJason Wang 	}
311c8d68e6bSJason Wang 	tfile->queue_index = tun->numqueues;
3126e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, tun);
313c8d68e6bSJason Wang 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
31454f968d6SJason Wang 	sock_hold(&tfile->sk);
315c8d68e6bSJason Wang 	tun->numqueues++;
316c8d68e6bSJason Wang 
317c8d68e6bSJason Wang 	tun_set_real_num_queues(tun);
318c8d68e6bSJason Wang 
319c8d68e6bSJason Wang 	if (tun->numqueues == 1)
320c8d68e6bSJason Wang 		netif_carrier_on(tun->dev);
321c8d68e6bSJason Wang 
322c8d68e6bSJason Wang 	/* device is allowed to go away first, so no need to hold extra
323c8d68e6bSJason Wang 	 * refcnt.
324c8d68e6bSJason Wang 	 */
325a7385ba2SEric W. Biederman 
32638231b7aSEric W. Biederman out:
32738231b7aSEric W. Biederman 	return err;
328a7385ba2SEric W. Biederman }
329a7385ba2SEric W. Biederman 
330631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
331631ab46bSEric W. Biederman {
3326e914fc7SJason Wang 	struct tun_struct *tun;
333c70f1829SEric W. Biederman 
3346e914fc7SJason Wang 	rcu_read_lock();
3356e914fc7SJason Wang 	tun = rcu_dereference(tfile->tun);
3366e914fc7SJason Wang 	if (tun)
3376e914fc7SJason Wang 		dev_hold(tun->dev);
3386e914fc7SJason Wang 	rcu_read_unlock();
339c70f1829SEric W. Biederman 
340c70f1829SEric W. Biederman 	return tun;
341631ab46bSEric W. Biederman }
342631ab46bSEric W. Biederman 
343631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
344631ab46bSEric W. Biederman {
345631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
346631ab46bSEric W. Biederman }
347631ab46bSEric W. Biederman 
348631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
349631ab46bSEric W. Biederman {
3506e914fc7SJason Wang 	dev_put(tun->dev);
351631ab46bSEric W. Biederman }
352631ab46bSEric W. Biederman 
3536b8a66eeSJoe Perches /* TAP filtering */
354f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
355f271b2ccSMax Krasnyansky {
356f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
357f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
358f271b2ccSMax Krasnyansky }
359f271b2ccSMax Krasnyansky 
360f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
361f271b2ccSMax Krasnyansky {
362f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
363f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
364f271b2ccSMax Krasnyansky }
365f271b2ccSMax Krasnyansky 
366f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
367f271b2ccSMax Krasnyansky {
368f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
369f271b2ccSMax Krasnyansky 	struct tun_filter uf;
370f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
371f271b2ccSMax Krasnyansky 
372f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
373f271b2ccSMax Krasnyansky 		return -EFAULT;
374f271b2ccSMax Krasnyansky 
375f271b2ccSMax Krasnyansky 	if (!uf.count) {
376f271b2ccSMax Krasnyansky 		/* Disabled */
377f271b2ccSMax Krasnyansky 		filter->count = 0;
378f271b2ccSMax Krasnyansky 		return 0;
379f271b2ccSMax Krasnyansky 	}
380f271b2ccSMax Krasnyansky 
381f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
382f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
383f271b2ccSMax Krasnyansky 	if (!addr)
384f271b2ccSMax Krasnyansky 		return -ENOMEM;
385f271b2ccSMax Krasnyansky 
386f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
387f271b2ccSMax Krasnyansky 		err = -EFAULT;
388f271b2ccSMax Krasnyansky 		goto done;
389f271b2ccSMax Krasnyansky 	}
390f271b2ccSMax Krasnyansky 
391f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
392f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
393f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
394f271b2ccSMax Krasnyansky 	filter->count = 0;
395f271b2ccSMax Krasnyansky 	wmb();
396f271b2ccSMax Krasnyansky 
397f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
398f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
399f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
400f271b2ccSMax Krasnyansky 
401f271b2ccSMax Krasnyansky 	nexact = n;
402f271b2ccSMax Krasnyansky 
403cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
404cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
405f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
406cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
407cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
408cfbf84fcSAlex Williamson 			err = 0; /* no filter */
409cfbf84fcSAlex Williamson 			goto done;
410cfbf84fcSAlex Williamson 		}
411f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
412cfbf84fcSAlex Williamson 	}
413f271b2ccSMax Krasnyansky 
414f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
415f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
416f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
417f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
418f271b2ccSMax Krasnyansky 
419f271b2ccSMax Krasnyansky 	/* Now enable the filter */
420f271b2ccSMax Krasnyansky 	wmb();
421f271b2ccSMax Krasnyansky 	filter->count = nexact;
422f271b2ccSMax Krasnyansky 
423f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
424f271b2ccSMax Krasnyansky 	err = nexact;
425f271b2ccSMax Krasnyansky 
426f271b2ccSMax Krasnyansky done:
427f271b2ccSMax Krasnyansky 	kfree(addr);
428f271b2ccSMax Krasnyansky 	return err;
429f271b2ccSMax Krasnyansky }
430f271b2ccSMax Krasnyansky 
431f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
432f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
433f271b2ccSMax Krasnyansky {
434f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
435f271b2ccSMax Krasnyansky 	 * at this point. */
436f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
437f271b2ccSMax Krasnyansky 	int i;
438f271b2ccSMax Krasnyansky 
439f271b2ccSMax Krasnyansky 	/* Exact match */
440f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
4412e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
442f271b2ccSMax Krasnyansky 			return 1;
443f271b2ccSMax Krasnyansky 
444f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
445f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
446f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
447f271b2ccSMax Krasnyansky 
448f271b2ccSMax Krasnyansky 	return 0;
449f271b2ccSMax Krasnyansky }
450f271b2ccSMax Krasnyansky 
451f271b2ccSMax Krasnyansky /*
452f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
453f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
454f271b2ccSMax Krasnyansky  */
455f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
456f271b2ccSMax Krasnyansky {
457f271b2ccSMax Krasnyansky 	if (!filter->count)
458f271b2ccSMax Krasnyansky 		return 1;
459f271b2ccSMax Krasnyansky 
460f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
461f271b2ccSMax Krasnyansky }
462f271b2ccSMax Krasnyansky 
4631da177e4SLinus Torvalds /* Network device part of the driver */
4641da177e4SLinus Torvalds 
4657282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
4661da177e4SLinus Torvalds 
467c70f1829SEric W. Biederman /* Net device detach from fd. */
468c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
469c70f1829SEric W. Biederman {
470c8d68e6bSJason Wang 	tun_detach_all(dev);
471c70f1829SEric W. Biederman }
472c70f1829SEric W. Biederman 
4731da177e4SLinus Torvalds /* Net device open. */
4741da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
4751da177e4SLinus Torvalds {
476c8d68e6bSJason Wang 	netif_tx_start_all_queues(dev);
4771da177e4SLinus Torvalds 	return 0;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /* Net device close. */
4811da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
4821da177e4SLinus Torvalds {
483c8d68e6bSJason Wang 	netif_tx_stop_all_queues(dev);
4841da177e4SLinus Torvalds 	return 0;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* Net device start xmit */
488424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
491c8d68e6bSJason Wang 	int txq = skb->queue_mapping;
4926e914fc7SJason Wang 	struct tun_file *tfile;
4931da177e4SLinus Torvalds 
4946e914fc7SJason Wang 	rcu_read_lock();
495c8d68e6bSJason Wang 	tfile = rcu_dereference(tun->tfiles[txq]);
496c8d68e6bSJason Wang 
4971da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
498c8d68e6bSJason Wang 	if (txq >= tun->numqueues)
4991da177e4SLinus Torvalds 		goto drop;
5001da177e4SLinus Torvalds 
5016e914fc7SJason Wang 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
5026e914fc7SJason Wang 
503c8d68e6bSJason Wang 	BUG_ON(!tfile);
504c8d68e6bSJason Wang 
505f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
506f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
507f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
508f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
509f271b2ccSMax Krasnyansky 		goto drop;
510f271b2ccSMax Krasnyansky 
51154f968d6SJason Wang 	if (tfile->socket.sk->sk_filter &&
51254f968d6SJason Wang 	    sk_filter(tfile->socket.sk, skb))
51399405162SMichael S. Tsirkin 		goto drop;
51499405162SMichael S. Tsirkin 
515c8d68e6bSJason Wang 	/* Limit the number of packets queued by divining txq length with the
516c8d68e6bSJason Wang 	 * number of queues.
517c8d68e6bSJason Wang 	 */
51854f968d6SJason Wang 	if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
519c8d68e6bSJason Wang 			  >= dev->tx_queue_len / tun->numqueues){
5201da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
5211da177e4SLinus Torvalds 			/* Normal queueing mode. */
5221da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
523c8d68e6bSJason Wang 			netif_stop_subqueue(dev, txq);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
5261da177e4SLinus Torvalds 			 * error is more appropriate. */
52709f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
5281da177e4SLinus Torvalds 		} else {
5291da177e4SLinus Torvalds 			/* Single queue mode.
5301da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
5311da177e4SLinus Torvalds 			goto drop;
5321da177e4SLinus Torvalds 		}
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 
5350110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
5360110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
537868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
538868eefebSMichael S. Tsirkin 		goto drop;
5390110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
5400110d6f2SMichael S. Tsirkin 
541f271b2ccSMax Krasnyansky 	/* Enqueue packet */
54254f968d6SJason Wang 	skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	/* Notify and wake up reader process */
54554f968d6SJason Wang 	if (tfile->flags & TUN_FASYNC)
54654f968d6SJason Wang 		kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
54754f968d6SJason Wang 	wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
54805c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
5496e914fc7SJason Wang 
5506e914fc7SJason Wang 	rcu_read_unlock();
5516ed10654SPatrick McHardy 	return NETDEV_TX_OK;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds drop:
55409f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
5551da177e4SLinus Torvalds 	kfree_skb(skb);
5566e914fc7SJason Wang 	rcu_read_unlock();
5576ed10654SPatrick McHardy 	return NETDEV_TX_OK;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
560f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
5611da177e4SLinus Torvalds {
562f271b2ccSMax Krasnyansky 	/*
563f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
564f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
565f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
566f271b2ccSMax Krasnyansky 	 */
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5694885a504SEd Swierk #define MIN_MTU 68
5704885a504SEd Swierk #define MAX_MTU 65535
5714885a504SEd Swierk 
5724885a504SEd Swierk static int
5734885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
5744885a504SEd Swierk {
5754885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
5764885a504SEd Swierk 		return -EINVAL;
5774885a504SEd Swierk 	dev->mtu = new_mtu;
5784885a504SEd Swierk 	return 0;
5794885a504SEd Swierk }
5804885a504SEd Swierk 
581c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
582c8f44affSMichał Mirosław 	netdev_features_t features)
58388255375SMichał Mirosław {
58488255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
58588255375SMichał Mirosław 
58688255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
58788255375SMichał Mirosław }
588bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
589bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
590bebd097aSNeil Horman {
591bebd097aSNeil Horman 	/*
592bebd097aSNeil Horman 	 * Tun only receives frames when:
593bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
594bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
595bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
596bebd097aSNeil Horman 	 * never to have pending data when we poll for it
597bebd097aSNeil Horman 	 * so theres nothing to do here but return.
598bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
599bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
600bebd097aSNeil Horman 	 * still use netconsole
601bebd097aSNeil Horman 	 */
602bebd097aSNeil Horman 	return;
603bebd097aSNeil Horman }
604bebd097aSNeil Horman #endif
605758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
606c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
607758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
608758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
60900829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
610758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
61188255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
612c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
613bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
614bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
615bebd097aSNeil Horman #endif
616758e43b7SStephen Hemminger };
617758e43b7SStephen Hemminger 
618758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
619c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
620758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
621758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
62200829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
623758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
62488255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
625afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
626758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
627758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
628c8d68e6bSJason Wang 	.ndo_select_queue	= tun_select_queue,
629bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
630bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
631bebd097aSNeil Horman #endif
632758e43b7SStephen Hemminger };
633758e43b7SStephen Hemminger 
6341da177e4SLinus Torvalds /* Initialize net device. */
6351da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
6361da177e4SLinus Torvalds {
6371da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
6401da177e4SLinus Torvalds 	case TUN_TUN_DEV:
641758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
642758e43b7SStephen Hemminger 
6431da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
6441da177e4SLinus Torvalds 		dev->hard_header_len = 0;
6451da177e4SLinus Torvalds 		dev->addr_len = 0;
6461da177e4SLinus Torvalds 		dev->mtu = 1500;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 		/* Zero header length */
6491da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
6501da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
6511da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
6521da177e4SLinus Torvalds 		break;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	case TUN_TAP_DEV:
6557a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
6561da177e4SLinus Torvalds 		/* Ethernet TAP Device */
6571da177e4SLinus Torvalds 		ether_setup(dev);
658550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
65936226a8dSBrian Braunstein 
660f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
66136226a8dSBrian Braunstein 
6621da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
6631da177e4SLinus Torvalds 		break;
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds /* Character device part */
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds /* Poll */
6701da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
6711da177e4SLinus Torvalds {
672b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
673b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
6743c8a9c63SMariusz Kozlowski 	struct sock *sk;
67533dccbb0SHerbert Xu 	unsigned int mask = 0;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	if (!tun)
678eac9e902SEric W. Biederman 		return POLLERR;
6791da177e4SLinus Torvalds 
68054f968d6SJason Wang 	sk = tfile->socket.sk;
6813c8a9c63SMariusz Kozlowski 
6826b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
6831da177e4SLinus Torvalds 
68454f968d6SJason Wang 	poll_wait(file, &tfile->wq.wait, wait);
6851da177e4SLinus Torvalds 
68689f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
6871da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
6881da177e4SLinus Torvalds 
68933dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
69033dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
69133dccbb0SHerbert Xu 	     sock_writeable(sk)))
69233dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
69333dccbb0SHerbert Xu 
694c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
695c70f1829SEric W. Biederman 		mask = POLLERR;
696c70f1829SEric W. Biederman 
697631ab46bSEric W. Biederman 	tun_put(tun);
6981da177e4SLinus Torvalds 	return mask;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
701f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
702f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
70354f968d6SJason Wang static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
70433dccbb0SHerbert Xu 				     size_t prepad, size_t len,
70533dccbb0SHerbert Xu 				     size_t linear, int noblock)
706f42157cbSRusty Russell {
70754f968d6SJason Wang 	struct sock *sk = tfile->socket.sk;
708f42157cbSRusty Russell 	struct sk_buff *skb;
70933dccbb0SHerbert Xu 	int err;
710f42157cbSRusty Russell 
711f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
7120eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
71333dccbb0SHerbert Xu 		linear = len;
714f42157cbSRusty Russell 
71533dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
71633dccbb0SHerbert Xu 				   &err);
717f42157cbSRusty Russell 	if (!skb)
71833dccbb0SHerbert Xu 		return ERR_PTR(err);
719f42157cbSRusty Russell 
720f42157cbSRusty Russell 	skb_reserve(skb, prepad);
721f42157cbSRusty Russell 	skb_put(skb, linear);
72233dccbb0SHerbert Xu 	skb->data_len = len - linear;
72333dccbb0SHerbert Xu 	skb->len += len - linear;
724f42157cbSRusty Russell 
725f42157cbSRusty Russell 	return skb;
726f42157cbSRusty Russell }
727f42157cbSRusty Russell 
7280690899bSMichael S. Tsirkin /* set skb frags from iovec, this can move to core network code for reuse */
7290690899bSMichael S. Tsirkin static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
7300690899bSMichael S. Tsirkin 				  int offset, size_t count)
7310690899bSMichael S. Tsirkin {
7320690899bSMichael S. Tsirkin 	int len = iov_length(from, count) - offset;
7330690899bSMichael S. Tsirkin 	int copy = skb_headlen(skb);
7340690899bSMichael S. Tsirkin 	int size, offset1 = 0;
7350690899bSMichael S. Tsirkin 	int i = 0;
7360690899bSMichael S. Tsirkin 
7370690899bSMichael S. Tsirkin 	/* Skip over from offset */
7380690899bSMichael S. Tsirkin 	while (count && (offset >= from->iov_len)) {
7390690899bSMichael S. Tsirkin 		offset -= from->iov_len;
7400690899bSMichael S. Tsirkin 		++from;
7410690899bSMichael S. Tsirkin 		--count;
7420690899bSMichael S. Tsirkin 	}
7430690899bSMichael S. Tsirkin 
7440690899bSMichael S. Tsirkin 	/* copy up to skb headlen */
7450690899bSMichael S. Tsirkin 	while (count && (copy > 0)) {
7460690899bSMichael S. Tsirkin 		size = min_t(unsigned int, copy, from->iov_len - offset);
7470690899bSMichael S. Tsirkin 		if (copy_from_user(skb->data + offset1, from->iov_base + offset,
7480690899bSMichael S. Tsirkin 				   size))
7490690899bSMichael S. Tsirkin 			return -EFAULT;
7500690899bSMichael S. Tsirkin 		if (copy > size) {
7510690899bSMichael S. Tsirkin 			++from;
7520690899bSMichael S. Tsirkin 			--count;
7530690899bSMichael S. Tsirkin 			offset = 0;
7540690899bSMichael S. Tsirkin 		} else
7550690899bSMichael S. Tsirkin 			offset += size;
7560690899bSMichael S. Tsirkin 		copy -= size;
7570690899bSMichael S. Tsirkin 		offset1 += size;
7580690899bSMichael S. Tsirkin 	}
7590690899bSMichael S. Tsirkin 
7600690899bSMichael S. Tsirkin 	if (len == offset1)
7610690899bSMichael S. Tsirkin 		return 0;
7620690899bSMichael S. Tsirkin 
7630690899bSMichael S. Tsirkin 	while (count--) {
7640690899bSMichael S. Tsirkin 		struct page *page[MAX_SKB_FRAGS];
7650690899bSMichael S. Tsirkin 		int num_pages;
7660690899bSMichael S. Tsirkin 		unsigned long base;
7670690899bSMichael S. Tsirkin 		unsigned long truesize;
7680690899bSMichael S. Tsirkin 
7690690899bSMichael S. Tsirkin 		len = from->iov_len - offset;
7700690899bSMichael S. Tsirkin 		if (!len) {
7710690899bSMichael S. Tsirkin 			offset = 0;
7720690899bSMichael S. Tsirkin 			++from;
7730690899bSMichael S. Tsirkin 			continue;
7740690899bSMichael S. Tsirkin 		}
7750690899bSMichael S. Tsirkin 		base = (unsigned long)from->iov_base + offset;
7760690899bSMichael S. Tsirkin 		size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
7770690899bSMichael S. Tsirkin 		if (i + size > MAX_SKB_FRAGS)
7780690899bSMichael S. Tsirkin 			return -EMSGSIZE;
7790690899bSMichael S. Tsirkin 		num_pages = get_user_pages_fast(base, size, 0, &page[i]);
7800690899bSMichael S. Tsirkin 		if (num_pages != size) {
7810690899bSMichael S. Tsirkin 			for (i = 0; i < num_pages; i++)
7820690899bSMichael S. Tsirkin 				put_page(page[i]);
7830690899bSMichael S. Tsirkin 			return -EFAULT;
7840690899bSMichael S. Tsirkin 		}
7850690899bSMichael S. Tsirkin 		truesize = size * PAGE_SIZE;
7860690899bSMichael S. Tsirkin 		skb->data_len += len;
7870690899bSMichael S. Tsirkin 		skb->len += len;
7880690899bSMichael S. Tsirkin 		skb->truesize += truesize;
7890690899bSMichael S. Tsirkin 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
7900690899bSMichael S. Tsirkin 		while (len) {
7910690899bSMichael S. Tsirkin 			int off = base & ~PAGE_MASK;
7920690899bSMichael S. Tsirkin 			int size = min_t(int, len, PAGE_SIZE - off);
7930690899bSMichael S. Tsirkin 			__skb_fill_page_desc(skb, i, page[i], off, size);
7940690899bSMichael S. Tsirkin 			skb_shinfo(skb)->nr_frags++;
7950690899bSMichael S. Tsirkin 			/* increase sk_wmem_alloc */
7960690899bSMichael S. Tsirkin 			base += size;
7970690899bSMichael S. Tsirkin 			len -= size;
7980690899bSMichael S. Tsirkin 			i++;
7990690899bSMichael S. Tsirkin 		}
8000690899bSMichael S. Tsirkin 		offset = 0;
8010690899bSMichael S. Tsirkin 		++from;
8020690899bSMichael S. Tsirkin 	}
8030690899bSMichael S. Tsirkin 	return 0;
8040690899bSMichael S. Tsirkin }
8050690899bSMichael S. Tsirkin 
8061da177e4SLinus Torvalds /* Get packet from user space buffer */
80754f968d6SJason Wang static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
80854f968d6SJason Wang 			    void *msg_control, const struct iovec *iv,
80954f968d6SJason Wang 			    size_t total_len, size_t count, int noblock)
8101da177e4SLinus Torvalds {
81109640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
8121da177e4SLinus Torvalds 	struct sk_buff *skb;
8130690899bSMichael S. Tsirkin 	size_t len = total_len, align = NET_SKB_PAD;
814f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
8156f26c9a7SMichael S. Tsirkin 	int offset = 0;
8160690899bSMichael S. Tsirkin 	int copylen;
8170690899bSMichael S. Tsirkin 	bool zerocopy = false;
8180690899bSMichael S. Tsirkin 	int err;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
8210690899bSMichael S. Tsirkin 		if ((len -= sizeof(pi)) > total_len)
8221da177e4SLinus Torvalds 			return -EINVAL;
8231da177e4SLinus Torvalds 
8246f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
8251da177e4SLinus Torvalds 			return -EFAULT;
8266f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
8271da177e4SLinus Torvalds 	}
8281da177e4SLinus Torvalds 
829f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
8300690899bSMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > total_len)
831f43798c2SRusty Russell 			return -EINVAL;
832f43798c2SRusty Russell 
8336f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
834f43798c2SRusty Russell 			return -EFAULT;
835f43798c2SRusty Russell 
8364909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
8374909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
8384909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
8394909122fSHerbert Xu 
840f43798c2SRusty Russell 		if (gso.hdr_len > len)
841f43798c2SRusty Russell 			return -EINVAL;
842d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
843f43798c2SRusty Russell 	}
844f43798c2SRusty Russell 
845e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
846a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
8470eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
8480eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
849e01bf1c8SRusty Russell 			return -EINVAL;
850e01bf1c8SRusty Russell 	}
8511da177e4SLinus Torvalds 
8520690899bSMichael S. Tsirkin 	if (msg_control)
8530690899bSMichael S. Tsirkin 		zerocopy = true;
8540690899bSMichael S. Tsirkin 
8550690899bSMichael S. Tsirkin 	if (zerocopy) {
8560690899bSMichael S. Tsirkin 		/* Userspace may produce vectors with count greater than
8570690899bSMichael S. Tsirkin 		 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
8580690899bSMichael S. Tsirkin 		 * to let the rest of data to be fit in the frags.
8590690899bSMichael S. Tsirkin 		 */
8600690899bSMichael S. Tsirkin 		if (count > MAX_SKB_FRAGS) {
8610690899bSMichael S. Tsirkin 			copylen = iov_length(iv, count - MAX_SKB_FRAGS);
8620690899bSMichael S. Tsirkin 			if (copylen < offset)
8630690899bSMichael S. Tsirkin 				copylen = 0;
8640690899bSMichael S. Tsirkin 			else
8650690899bSMichael S. Tsirkin 				copylen -= offset;
8660690899bSMichael S. Tsirkin 		} else
8670690899bSMichael S. Tsirkin 				copylen = 0;
8680690899bSMichael S. Tsirkin 		/* There are 256 bytes to be copied in skb, so there is enough
8690690899bSMichael S. Tsirkin 		 * room for skb expand head in case it is used.
8700690899bSMichael S. Tsirkin 		 * The rest of the buffer is mapped from userspace.
8710690899bSMichael S. Tsirkin 		 */
8720690899bSMichael S. Tsirkin 		if (copylen < gso.hdr_len)
8730690899bSMichael S. Tsirkin 			copylen = gso.hdr_len;
8740690899bSMichael S. Tsirkin 		if (!copylen)
8750690899bSMichael S. Tsirkin 			copylen = GOODCOPY_LEN;
8760690899bSMichael S. Tsirkin 	} else
8770690899bSMichael S. Tsirkin 		copylen = len;
8780690899bSMichael S. Tsirkin 
87954f968d6SJason Wang 	skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
88033dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
88133dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
88209f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
88333dccbb0SHerbert Xu 		return PTR_ERR(skb);
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
8860690899bSMichael S. Tsirkin 	if (zerocopy)
8870690899bSMichael S. Tsirkin 		err = zerocopy_sg_from_iovec(skb, iv, offset, count);
8880690899bSMichael S. Tsirkin 	else
8890690899bSMichael S. Tsirkin 		err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
8900690899bSMichael S. Tsirkin 
8910690899bSMichael S. Tsirkin 	if (err) {
89209f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
8938f22757eSDave Jones 		kfree_skb(skb);
8941da177e4SLinus Torvalds 		return -EFAULT;
8958f22757eSDave Jones 	}
8961da177e4SLinus Torvalds 
897f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
898f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
899f43798c2SRusty Russell 					  gso.csum_offset)) {
900f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
901f43798c2SRusty Russell 			kfree_skb(skb);
902f43798c2SRusty Russell 			return -EINVAL;
903f43798c2SRusty Russell 		}
90488255375SMichał Mirosław 	}
905f43798c2SRusty Russell 
9061da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
9071da177e4SLinus Torvalds 	case TUN_TUN_DEV:
908f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
909f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
910f09f7ee2SAng Way Chuang 			case 0x40:
911f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
912f09f7ee2SAng Way Chuang 				break;
913f09f7ee2SAng Way Chuang 			case 0x60:
914f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
915f09f7ee2SAng Way Chuang 				break;
916f09f7ee2SAng Way Chuang 			default:
917f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
918f09f7ee2SAng Way Chuang 				kfree_skb(skb);
919f09f7ee2SAng Way Chuang 				return -EINVAL;
920f09f7ee2SAng Way Chuang 			}
921f09f7ee2SAng Way Chuang 		}
922f09f7ee2SAng Way Chuang 
923459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
9241da177e4SLinus Torvalds 		skb->protocol = pi.proto;
9254c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
9261da177e4SLinus Torvalds 		break;
9271da177e4SLinus Torvalds 	case TUN_TAP_DEV:
9281da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
9291da177e4SLinus Torvalds 		break;
9306403eab1SJoe Perches 	}
9311da177e4SLinus Torvalds 
932f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
933f43798c2SRusty Russell 		pr_debug("GSO!\n");
934f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
935f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
936f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
937f43798c2SRusty Russell 			break;
938f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
939f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
940f43798c2SRusty Russell 			break;
941e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
942e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
943e36aa25aSSridhar Samudrala 			break;
944f43798c2SRusty Russell 		default:
945f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
946f43798c2SRusty Russell 			kfree_skb(skb);
947f43798c2SRusty Russell 			return -EINVAL;
948f43798c2SRusty Russell 		}
949f43798c2SRusty Russell 
950f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
951f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
952f43798c2SRusty Russell 
953f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
954f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
955f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
956f43798c2SRusty Russell 			kfree_skb(skb);
957f43798c2SRusty Russell 			return -EINVAL;
958f43798c2SRusty Russell 		}
959f43798c2SRusty Russell 
960f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
961f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
962f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
963f43798c2SRusty Russell 	}
9641da177e4SLinus Torvalds 
9650690899bSMichael S. Tsirkin 	/* copy skb_ubuf_info for callback when skb has no error */
9660690899bSMichael S. Tsirkin 	if (zerocopy) {
9670690899bSMichael S. Tsirkin 		skb_shinfo(skb)->destructor_arg = msg_control;
9680690899bSMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
9690690899bSMichael S. Tsirkin 	}
9700690899bSMichael S. Tsirkin 
9711da177e4SLinus Torvalds 	netif_rx_ni(skb);
9721da177e4SLinus Torvalds 
97309f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
97409f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
9751da177e4SLinus Torvalds 
9760690899bSMichael S. Tsirkin 	return total_len;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
979ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
980ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
9811da177e4SLinus Torvalds {
98233dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
983ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
98454f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
985631ab46bSEric W. Biederman 	ssize_t result;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	if (!tun)
9881da177e4SLinus Torvalds 		return -EBADFD;
9891da177e4SLinus Torvalds 
9906b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
9911da177e4SLinus Torvalds 
99254f968d6SJason Wang 	result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
99354f968d6SJason Wang 			      count, file->f_flags & O_NONBLOCK);
994631ab46bSEric W. Biederman 
995631ab46bSEric W. Biederman 	tun_put(tun);
996631ab46bSEric W. Biederman 	return result;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds /* Put packet to the user space buffer */
10006f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
100154f968d6SJason Wang 			    struct tun_file *tfile,
10021da177e4SLinus Torvalds 			    struct sk_buff *skb,
100343b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
10041da177e4SLinus Torvalds {
10051da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
10061da177e4SLinus Torvalds 	ssize_t total = 0;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
10091da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
10101da177e4SLinus Torvalds 			return -EINVAL;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 		if (len < skb->len) {
10131da177e4SLinus Torvalds 			/* Packet will be striped */
10141da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
10151da177e4SLinus Torvalds 		}
10161da177e4SLinus Torvalds 
101743b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
10181da177e4SLinus Torvalds 			return -EFAULT;
10191da177e4SLinus Torvalds 		total += sizeof(pi);
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds 
1022f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
1023f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
1024d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
1025f43798c2SRusty Russell 			return -EINVAL;
1026f43798c2SRusty Russell 
1027f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
1028f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
1029f43798c2SRusty Russell 
1030f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
1031f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
1032f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
1033f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
1034f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1035f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
1036f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1037e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
1038e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1039ef3db4a5SMichael S. Tsirkin 			else {
10406b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
1041ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
1042ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
1043ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
1044ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
1045ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
1046ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
1047ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
1048ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
1049ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
1050ef3db4a5SMichael S. Tsirkin 			}
1051f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1052f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1053f43798c2SRusty Russell 		} else
1054f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1055f43798c2SRusty Russell 
1056f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1057f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
105855508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
1059f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
106010a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
106110a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1062f43798c2SRusty Russell 		} /* else everything is zero */
1063f43798c2SRusty Russell 
106443b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
106543b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
1066f43798c2SRusty Russell 			return -EFAULT;
1067d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
1068f43798c2SRusty Russell 	}
1069f43798c2SRusty Russell 
10701da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
10711da177e4SLinus Torvalds 
107243b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
107305c2828cSMichael S. Tsirkin 	total += skb->len;
10741da177e4SLinus Torvalds 
107509f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
107609f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	return total;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
108154f968d6SJason Wang static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
108205c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
108305c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
10861da177e4SLinus Torvalds 	struct sk_buff *skb;
108705c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
10881da177e4SLinus Torvalds 
10896b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_read\n");
10901da177e4SLinus Torvalds 
109161a5ff15SAmos Kong 	if (unlikely(!noblock))
109254f968d6SJason Wang 		add_wait_queue(&tfile->wq.wait, &wait);
10931da177e4SLinus Torvalds 	while (len) {
10941da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 		/* Read frames from the queue */
109754f968d6SJason Wang 		if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
109805c2828cSMichael S. Tsirkin 			if (noblock) {
10991da177e4SLinus Torvalds 				ret = -EAGAIN;
11001da177e4SLinus Torvalds 				break;
11011da177e4SLinus Torvalds 			}
11021da177e4SLinus Torvalds 			if (signal_pending(current)) {
11031da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
11041da177e4SLinus Torvalds 				break;
11051da177e4SLinus Torvalds 			}
1106c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
1107c70f1829SEric W. Biederman 				ret = -EIO;
1108c70f1829SEric W. Biederman 				break;
1109c70f1829SEric W. Biederman 			}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
11121da177e4SLinus Torvalds 			schedule();
11131da177e4SLinus Torvalds 			continue;
11141da177e4SLinus Torvalds 		}
1115c8d68e6bSJason Wang 		netif_wake_subqueue(tun->dev, tfile->queue_index);
11161da177e4SLinus Torvalds 
111754f968d6SJason Wang 		ret = tun_put_user(tun, tfile, skb, iv, len);
11181da177e4SLinus Torvalds 		kfree_skb(skb);
11191da177e4SLinus Torvalds 		break;
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
112361a5ff15SAmos Kong 	if (unlikely(!noblock))
112454f968d6SJason Wang 		remove_wait_queue(&tfile->wq.wait, &wait);
11251da177e4SLinus Torvalds 
112605c2828cSMichael S. Tsirkin 	return ret;
112705c2828cSMichael S. Tsirkin }
112805c2828cSMichael S. Tsirkin 
112905c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
113005c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
113105c2828cSMichael S. Tsirkin {
113205c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
113305c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
113405c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
113505c2828cSMichael S. Tsirkin 	ssize_t len, ret;
113605c2828cSMichael S. Tsirkin 
113705c2828cSMichael S. Tsirkin 	if (!tun)
113805c2828cSMichael S. Tsirkin 		return -EBADFD;
113905c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
114005c2828cSMichael S. Tsirkin 	if (len < 0) {
114105c2828cSMichael S. Tsirkin 		ret = -EINVAL;
114205c2828cSMichael S. Tsirkin 		goto out;
114305c2828cSMichael S. Tsirkin 	}
114405c2828cSMichael S. Tsirkin 
114554f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, iv, len,
114654f968d6SJason Wang 			  file->f_flags & O_NONBLOCK);
114705c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
1148631ab46bSEric W. Biederman out:
1149631ab46bSEric W. Biederman 	tun_put(tun);
11501da177e4SLinus Torvalds 	return ret;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
11541da177e4SLinus Torvalds {
11551da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
11561da177e4SLinus Torvalds 
11570625c883SEric W. Biederman 	tun->owner = INVALID_UID;
11580625c883SEric W. Biederman 	tun->group = INVALID_GID;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
116154f968d6SJason Wang 	dev->destructor = free_netdev;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
1164f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
1165f019a7a5SEric W. Biederman  * device with netlink.
1166f019a7a5SEric W. Biederman  */
1167f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1168f019a7a5SEric W. Biederman {
1169f019a7a5SEric W. Biederman 	return -EINVAL;
1170f019a7a5SEric W. Biederman }
1171f019a7a5SEric W. Biederman 
1172f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
1173f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
1174f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
1175f019a7a5SEric W. Biederman 	.setup		= tun_setup,
1176f019a7a5SEric W. Biederman 	.validate	= tun_validate,
1177f019a7a5SEric W. Biederman };
1178f019a7a5SEric W. Biederman 
117933dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
118033dccbb0SHerbert Xu {
118154f968d6SJason Wang 	struct tun_file *tfile;
118243815482SEric Dumazet 	wait_queue_head_t *wqueue;
118333dccbb0SHerbert Xu 
118433dccbb0SHerbert Xu 	if (!sock_writeable(sk))
118533dccbb0SHerbert Xu 		return;
118633dccbb0SHerbert Xu 
118733dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
118833dccbb0SHerbert Xu 		return;
118933dccbb0SHerbert Xu 
119043815482SEric Dumazet 	wqueue = sk_sleep(sk);
119143815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
119243815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
119305c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
1194c722c625SHerbert Xu 
119554f968d6SJason Wang 	tfile = container_of(sk, struct tun_file, sk);
119654f968d6SJason Wang 	kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
119733dccbb0SHerbert Xu }
119833dccbb0SHerbert Xu 
119905c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
120005c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
120105c2828cSMichael S. Tsirkin {
120254f968d6SJason Wang 	int ret;
120354f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
120454f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
120554f968d6SJason Wang 
120654f968d6SJason Wang 	if (!tun)
120754f968d6SJason Wang 		return -EBADFD;
120854f968d6SJason Wang 	ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
12090690899bSMichael S. Tsirkin 			   m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
121054f968d6SJason Wang 	tun_put(tun);
121154f968d6SJason Wang 	return ret;
121205c2828cSMichael S. Tsirkin }
121305c2828cSMichael S. Tsirkin 
121454f968d6SJason Wang 
121505c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
121605c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
121705c2828cSMichael S. Tsirkin 		       int flags)
121805c2828cSMichael S. Tsirkin {
121954f968d6SJason Wang 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
122054f968d6SJason Wang 	struct tun_struct *tun = __tun_get(tfile);
122105c2828cSMichael S. Tsirkin 	int ret;
122254f968d6SJason Wang 
122354f968d6SJason Wang 	if (!tun)
122454f968d6SJason Wang 		return -EBADFD;
122554f968d6SJason Wang 
122605c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
122705c2828cSMichael S. Tsirkin 		return -EINVAL;
122854f968d6SJason Wang 	ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
122905c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
123005c2828cSMichael S. Tsirkin 	if (ret > total_len) {
123105c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
123205c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
123305c2828cSMichael S. Tsirkin 	}
123454f968d6SJason Wang 	tun_put(tun);
123505c2828cSMichael S. Tsirkin 	return ret;
123605c2828cSMichael S. Tsirkin }
123705c2828cSMichael S. Tsirkin 
12381ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
12391ab5ecb9SStanislav Kinsbursky {
12401ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
12411ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
12421ab5ecb9SStanislav Kinsbursky 	return 0;
12431ab5ecb9SStanislav Kinsbursky }
12441ab5ecb9SStanislav Kinsbursky 
124505c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
124605c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
124705c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
124805c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
12491ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
125005c2828cSMichael S. Tsirkin };
125105c2828cSMichael S. Tsirkin 
125233dccbb0SHerbert Xu static struct proto tun_proto = {
125333dccbb0SHerbert Xu 	.name		= "tun",
125433dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
125554f968d6SJason Wang 	.obj_size	= sizeof(struct tun_file),
125633dccbb0SHerbert Xu };
1257f019a7a5SEric W. Biederman 
1258980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1259980c9e8cSDavid Woodhouse {
1260980c9e8cSDavid Woodhouse 	int flags = 0;
1261980c9e8cSDavid Woodhouse 
1262980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1263980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1264980c9e8cSDavid Woodhouse 	else
1265980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1266980c9e8cSDavid Woodhouse 
1267980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1268980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1269980c9e8cSDavid Woodhouse 
1270980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1271980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1272980c9e8cSDavid Woodhouse 
1273980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1274980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1275980c9e8cSDavid Woodhouse 
1276c8d68e6bSJason Wang 	if (tun->flags & TUN_TAP_MQ)
1277c8d68e6bSJason Wang 		flags |= IFF_MULTI_QUEUE;
1278c8d68e6bSJason Wang 
1279980c9e8cSDavid Woodhouse 	return flags;
1280980c9e8cSDavid Woodhouse }
1281980c9e8cSDavid Woodhouse 
1282980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1283980c9e8cSDavid Woodhouse 			      char *buf)
1284980c9e8cSDavid Woodhouse {
1285980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1286980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1287980c9e8cSDavid Woodhouse }
1288980c9e8cSDavid Woodhouse 
1289980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1290980c9e8cSDavid Woodhouse 			      char *buf)
1291980c9e8cSDavid Woodhouse {
1292980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
12930625c883SEric W. Biederman 	return uid_valid(tun->owner)?
12940625c883SEric W. Biederman 		sprintf(buf, "%u\n",
12950625c883SEric W. Biederman 			from_kuid_munged(current_user_ns(), tun->owner)):
12960625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1297980c9e8cSDavid Woodhouse }
1298980c9e8cSDavid Woodhouse 
1299980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1300980c9e8cSDavid Woodhouse 			      char *buf)
1301980c9e8cSDavid Woodhouse {
1302980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
13030625c883SEric W. Biederman 	return gid_valid(tun->group) ?
13040625c883SEric W. Biederman 		sprintf(buf, "%u\n",
13050625c883SEric W. Biederman 			from_kgid_munged(current_user_ns(), tun->group)):
13060625c883SEric W. Biederman 		sprintf(buf, "-1\n");
1307980c9e8cSDavid Woodhouse }
1308980c9e8cSDavid Woodhouse 
1309980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1310980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1311980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1312980c9e8cSDavid Woodhouse 
1313d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	struct tun_struct *tun;
131654f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
13171da177e4SLinus Torvalds 	struct net_device *dev;
13181da177e4SLinus Torvalds 	int err;
13191da177e4SLinus Torvalds 
132074a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
132174a3e5a7SEric W. Biederman 	if (dev) {
1322f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1323f85ba780SDavid Woodhouse 			return -EBUSY;
132474a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
132574a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
132674a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
132774a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
132874a3e5a7SEric W. Biederman 		else
132974a3e5a7SEric W. Biederman 			return -EINVAL;
133074a3e5a7SEric W. Biederman 
1331*cde8b15fSJason Wang 		if (tun_not_capable(tun))
13322b980dbdSPaul Moore 			return -EPERM;
133354f968d6SJason Wang 		err = security_tun_dev_attach(tfile->socket.sk);
13342b980dbdSPaul Moore 		if (err < 0)
13352b980dbdSPaul Moore 			return err;
13362b980dbdSPaul Moore 
1337a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1338a7385ba2SEric W. Biederman 		if (err < 0)
1339a7385ba2SEric W. Biederman 			return err;
134086a264abSDavid Howells 	}
13411da177e4SLinus Torvalds 	else {
13421da177e4SLinus Torvalds 		char *name;
13431da177e4SLinus Torvalds 		unsigned long flags = 0;
13441da177e4SLinus Torvalds 
1345ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1346ca6bb5d7SDavid Woodhouse 			return -EPERM;
13472b980dbdSPaul Moore 		err = security_tun_dev_create();
13482b980dbdSPaul Moore 		if (err < 0)
13492b980dbdSPaul Moore 			return err;
1350ca6bb5d7SDavid Woodhouse 
13511da177e4SLinus Torvalds 		/* Set dev type */
13521da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
13531da177e4SLinus Torvalds 			/* TUN device */
13541da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
13551da177e4SLinus Torvalds 			name = "tun%d";
13561da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
13571da177e4SLinus Torvalds 			/* TAP device */
13581da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
13591da177e4SLinus Torvalds 			name = "tap%d";
13601da177e4SLinus Torvalds 		} else
136136989b90SKusanagi Kouichi 			return -EINVAL;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 		if (*ifr->ifr_name)
13641da177e4SLinus Torvalds 			name = ifr->ifr_name;
13651da177e4SLinus Torvalds 
1366c8d68e6bSJason Wang 		dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
1367c8d68e6bSJason Wang 				       tun_setup,
1368c8d68e6bSJason Wang 				       MAX_TAP_QUEUES, MAX_TAP_QUEUES);
13691da177e4SLinus Torvalds 		if (!dev)
13701da177e4SLinus Torvalds 			return -ENOMEM;
13711da177e4SLinus Torvalds 
1372fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1373f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1374758e43b7SStephen Hemminger 
13751da177e4SLinus Torvalds 		tun = netdev_priv(dev);
13761da177e4SLinus Torvalds 		tun->dev = dev;
13771da177e4SLinus Torvalds 		tun->flags = flags;
1378f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1379d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
13801da177e4SLinus Torvalds 
138154f968d6SJason Wang 		tun->filter_attached = false;
138254f968d6SJason Wang 		tun->sndbuf = tfile->socket.sk->sk_sndbuf;
138333dccbb0SHerbert Xu 
138454f968d6SJason Wang 		security_tun_dev_post_create(&tfile->sk);
13852b980dbdSPaul Moore 
13861da177e4SLinus Torvalds 		tun_net_init(dev);
13871da177e4SLinus Torvalds 
138888255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
138988255375SMichał Mirosław 			TUN_USER_FEATURES;
139088255375SMichał Mirosław 		dev->features = dev->hw_features;
139188255375SMichał Mirosław 
13921da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
13931da177e4SLinus Torvalds 		if (err < 0)
139454f968d6SJason Wang 			goto err_free_dev;
13959c3fea6aSHerbert Xu 
1396980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1397980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1398980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
13996b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1400980c9e8cSDavid Woodhouse 
1401a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1402a7385ba2SEric W. Biederman 		if (err < 0)
1403c8d68e6bSJason Wang 			goto err_free_dev;
14041da177e4SLinus Torvalds 	}
14051da177e4SLinus Torvalds 
14066b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
14091da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1410a26af1e0SNathaniel Filardo 	else
1411a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
14141da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1415a26af1e0SNathaniel Filardo 	else
1416a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
14171da177e4SLinus Torvalds 
1418f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1419f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1420f43798c2SRusty Russell 	else
1421f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1422f43798c2SRusty Russell 
1423c8d68e6bSJason Wang 	if (ifr->ifr_flags & IFF_MULTI_QUEUE)
1424c8d68e6bSJason Wang 		tun->flags |= TUN_TAP_MQ;
1425c8d68e6bSJason Wang 	else
1426c8d68e6bSJason Wang 		tun->flags &= ~TUN_TAP_MQ;
1427c8d68e6bSJason Wang 
1428e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1429e35259a9SMax Krasnyansky 	 * xoff state.
1430e35259a9SMax Krasnyansky 	 */
1431e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1432c8d68e6bSJason Wang 		netif_tx_wake_all_queues(tun->dev);
1433e35259a9SMax Krasnyansky 
14341da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
14351da177e4SLinus Torvalds 	return 0;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds  err_free_dev:
14381da177e4SLinus Torvalds 	free_netdev(dev);
14391da177e4SLinus Torvalds 	return err;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
1442876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1443876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1444e3b99556SMark McLoughlin {
14456b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1446e3b99556SMark McLoughlin 
1447e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1448e3b99556SMark McLoughlin 
1449980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1450e3b99556SMark McLoughlin 
1451e3b99556SMark McLoughlin 	return 0;
1452e3b99556SMark McLoughlin }
1453e3b99556SMark McLoughlin 
14545228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
14555228ddc9SRusty Russell  * privs required. */
145688255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
14575228ddc9SRusty Russell {
1458c8f44affSMichał Mirosław 	netdev_features_t features = 0;
14595228ddc9SRusty Russell 
14605228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
146188255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
14625228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
14635228ddc9SRusty Russell 
14645228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
14655228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
14665228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
14675228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
14685228ddc9SRusty Russell 			}
14695228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
14705228ddc9SRusty Russell 				features |= NETIF_F_TSO;
14715228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
14725228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
14735228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
14745228ddc9SRusty Russell 		}
1475e36aa25aSSridhar Samudrala 
1476e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1477e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1478e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1479e36aa25aSSridhar Samudrala 		}
14805228ddc9SRusty Russell 	}
14815228ddc9SRusty Russell 
14825228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
14835228ddc9SRusty Russell 	 * trying to set them. */
14845228ddc9SRusty Russell 	if (arg)
14855228ddc9SRusty Russell 		return -EINVAL;
14865228ddc9SRusty Russell 
148788255375SMichał Mirosław 	tun->set_features = features;
148888255375SMichał Mirosław 	netdev_update_features(tun->dev);
14895228ddc9SRusty Russell 
14905228ddc9SRusty Russell 	return 0;
14915228ddc9SRusty Russell }
14925228ddc9SRusty Russell 
1493c8d68e6bSJason Wang static void tun_detach_filter(struct tun_struct *tun, int n)
1494c8d68e6bSJason Wang {
1495c8d68e6bSJason Wang 	int i;
1496c8d68e6bSJason Wang 	struct tun_file *tfile;
1497c8d68e6bSJason Wang 
1498c8d68e6bSJason Wang 	for (i = 0; i < n; i++) {
1499c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
1500c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
1501c8d68e6bSJason Wang 		sk_detach_filter(tfile->socket.sk);
1502c8d68e6bSJason Wang 	}
1503c8d68e6bSJason Wang 
1504c8d68e6bSJason Wang 	tun->filter_attached = false;
1505c8d68e6bSJason Wang }
1506c8d68e6bSJason Wang 
1507c8d68e6bSJason Wang static int tun_attach_filter(struct tun_struct *tun)
1508c8d68e6bSJason Wang {
1509c8d68e6bSJason Wang 	int i, ret = 0;
1510c8d68e6bSJason Wang 	struct tun_file *tfile;
1511c8d68e6bSJason Wang 
1512c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1513c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
1514c8d68e6bSJason Wang 						  lockdep_rtnl_is_held());
1515c8d68e6bSJason Wang 		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1516c8d68e6bSJason Wang 		if (ret) {
1517c8d68e6bSJason Wang 			tun_detach_filter(tun, i);
1518c8d68e6bSJason Wang 			return ret;
1519c8d68e6bSJason Wang 		}
1520c8d68e6bSJason Wang 	}
1521c8d68e6bSJason Wang 
1522c8d68e6bSJason Wang 	tun->filter_attached = true;
1523c8d68e6bSJason Wang 	return ret;
1524c8d68e6bSJason Wang }
1525c8d68e6bSJason Wang 
1526c8d68e6bSJason Wang static void tun_set_sndbuf(struct tun_struct *tun)
1527c8d68e6bSJason Wang {
1528c8d68e6bSJason Wang 	struct tun_file *tfile;
1529c8d68e6bSJason Wang 	int i;
1530c8d68e6bSJason Wang 
1531c8d68e6bSJason Wang 	for (i = 0; i < tun->numqueues; i++) {
1532c8d68e6bSJason Wang 		tfile = rcu_dereference_protected(tun->tfiles[i],
1533c8d68e6bSJason Wang 						lockdep_rtnl_is_held());
1534c8d68e6bSJason Wang 		tfile->socket.sk->sk_sndbuf = tun->sndbuf;
1535c8d68e6bSJason Wang 	}
1536c8d68e6bSJason Wang }
1537c8d68e6bSJason Wang 
1538*cde8b15fSJason Wang static int tun_set_queue(struct file *file, struct ifreq *ifr)
1539*cde8b15fSJason Wang {
1540*cde8b15fSJason Wang 	struct tun_file *tfile = file->private_data;
1541*cde8b15fSJason Wang 	struct tun_struct *tun;
1542*cde8b15fSJason Wang 	struct net_device *dev;
1543*cde8b15fSJason Wang 	int ret = 0;
1544*cde8b15fSJason Wang 
1545*cde8b15fSJason Wang 	rtnl_lock();
1546*cde8b15fSJason Wang 
1547*cde8b15fSJason Wang 	if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
1548*cde8b15fSJason Wang 		dev = __dev_get_by_name(tfile->net, ifr->ifr_name);
1549*cde8b15fSJason Wang 		if (!dev) {
1550*cde8b15fSJason Wang 			ret = -EINVAL;
1551*cde8b15fSJason Wang 			goto unlock;
1552*cde8b15fSJason Wang 		}
1553*cde8b15fSJason Wang 
1554*cde8b15fSJason Wang 		tun = netdev_priv(dev);
1555*cde8b15fSJason Wang 		if (dev->netdev_ops != &tap_netdev_ops &&
1556*cde8b15fSJason Wang 			dev->netdev_ops != &tun_netdev_ops)
1557*cde8b15fSJason Wang 			ret = -EINVAL;
1558*cde8b15fSJason Wang 		else if (tun_not_capable(tun))
1559*cde8b15fSJason Wang 			ret = -EPERM;
1560*cde8b15fSJason Wang 		else
1561*cde8b15fSJason Wang 			ret = tun_attach(tun, file);
1562*cde8b15fSJason Wang 	} else if (ifr->ifr_flags & IFF_DETACH_QUEUE)
1563*cde8b15fSJason Wang 		__tun_detach(tfile, false);
1564*cde8b15fSJason Wang 	else
1565*cde8b15fSJason Wang 		ret = -EINVAL;
1566*cde8b15fSJason Wang 
1567*cde8b15fSJason Wang unlock:
1568*cde8b15fSJason Wang 	rtnl_unlock();
1569*cde8b15fSJason Wang 	return ret;
1570*cde8b15fSJason Wang }
1571*cde8b15fSJason Wang 
157250857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
157350857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
15741da177e4SLinus Torvalds {
157536b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1576631ab46bSEric W. Biederman 	struct tun_struct *tun;
15771da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
15781da177e4SLinus Torvalds 	struct ifreq ifr;
15790625c883SEric W. Biederman 	kuid_t owner;
15800625c883SEric W. Biederman 	kgid_t group;
158133dccbb0SHerbert Xu 	int sndbuf;
1582d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1583f271b2ccSMax Krasnyansky 	int ret;
15841da177e4SLinus Torvalds 
1585*cde8b15fSJason Wang 	if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
158650857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
15871da177e4SLinus Torvalds 			return -EFAULT;
15888bbb1813SDavid S. Miller 	} else {
1589a117dacdSMathias Krause 		memset(&ifr, 0, sizeof(ifr));
15908bbb1813SDavid S. Miller 	}
1591631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1592631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1593631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1594631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1595631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1596*cde8b15fSJason Wang 				IFF_VNET_HDR | IFF_MULTI_QUEUE,
1597631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1598*cde8b15fSJason Wang 	} else if (cmd == TUNSETQUEUE)
1599*cde8b15fSJason Wang 		return tun_set_queue(file, &ifr);
1600631ab46bSEric W. Biederman 
1601c8d68e6bSJason Wang 	ret = 0;
1602876bfd4dSHerbert Xu 	rtnl_lock();
1603876bfd4dSHerbert Xu 
160436b50babSEric W. Biederman 	tun = __tun_get(tfile);
16051da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
16061da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
16071da177e4SLinus Torvalds 
1608876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
16091da177e4SLinus Torvalds 
1610876bfd4dSHerbert Xu 		if (ret)
1611876bfd4dSHerbert Xu 			goto unlock;
16121da177e4SLinus Torvalds 
161350857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1614876bfd4dSHerbert Xu 			ret = -EFAULT;
1615876bfd4dSHerbert Xu 		goto unlock;
16161da177e4SLinus Torvalds 	}
16171da177e4SLinus Torvalds 
1618876bfd4dSHerbert Xu 	ret = -EBADFD;
16191da177e4SLinus Torvalds 	if (!tun)
1620876bfd4dSHerbert Xu 		goto unlock;
16211da177e4SLinus Torvalds 
16221e588338SJason Wang 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
16231da177e4SLinus Torvalds 
1624631ab46bSEric W. Biederman 	ret = 0;
16251da177e4SLinus Torvalds 	switch (cmd) {
1626e3b99556SMark McLoughlin 	case TUNGETIFF:
1627876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1628e3b99556SMark McLoughlin 		if (ret)
1629631ab46bSEric W. Biederman 			break;
1630e3b99556SMark McLoughlin 
163150857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1632631ab46bSEric W. Biederman 			ret = -EFAULT;
1633e3b99556SMark McLoughlin 		break;
1634e3b99556SMark McLoughlin 
16351da177e4SLinus Torvalds 	case TUNSETNOCSUM:
16361da177e4SLinus Torvalds 		/* Disable/Enable checksum */
16371da177e4SLinus Torvalds 
163888255375SMichał Mirosław 		/* [unimplemented] */
163988255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
16406b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
16411da177e4SLinus Torvalds 		break;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	case TUNSETPERSIST:
164454f968d6SJason Wang 		/* Disable/Enable persist mode. Keep an extra reference to the
164554f968d6SJason Wang 		 * module to prevent the module being unprobed.
164654f968d6SJason Wang 		 */
164754f968d6SJason Wang 		if (arg) {
16481da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
164954f968d6SJason Wang 			__module_get(THIS_MODULE);
165054f968d6SJason Wang 		} else {
16511da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
165254f968d6SJason Wang 			module_put(THIS_MODULE);
165354f968d6SJason Wang 		}
16541da177e4SLinus Torvalds 
16556b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
16566b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
16571da177e4SLinus Torvalds 		break;
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	case TUNSETOWNER:
16601da177e4SLinus Torvalds 		/* Set owner of the device */
16610625c883SEric W. Biederman 		owner = make_kuid(current_user_ns(), arg);
16620625c883SEric W. Biederman 		if (!uid_valid(owner)) {
16630625c883SEric W. Biederman 			ret = -EINVAL;
16640625c883SEric W. Biederman 			break;
16650625c883SEric W. Biederman 		}
16660625c883SEric W. Biederman 		tun->owner = owner;
16671e588338SJason Wang 		tun_debug(KERN_INFO, tun, "owner set to %u\n",
16680625c883SEric W. Biederman 			  from_kuid(&init_user_ns, tun->owner));
16691da177e4SLinus Torvalds 		break;
16701da177e4SLinus Torvalds 
16718c644623SGuido Guenther 	case TUNSETGROUP:
16728c644623SGuido Guenther 		/* Set group of the device */
16730625c883SEric W. Biederman 		group = make_kgid(current_user_ns(), arg);
16740625c883SEric W. Biederman 		if (!gid_valid(group)) {
16750625c883SEric W. Biederman 			ret = -EINVAL;
16760625c883SEric W. Biederman 			break;
16770625c883SEric W. Biederman 		}
16780625c883SEric W. Biederman 		tun->group = group;
16791e588338SJason Wang 		tun_debug(KERN_INFO, tun, "group set to %u\n",
16800625c883SEric W. Biederman 			  from_kgid(&init_user_ns, tun->group));
16818c644623SGuido Guenther 		break;
16828c644623SGuido Guenther 
1683ff4cc3acSMike Kershaw 	case TUNSETLINK:
1684ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1685ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
16866b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
16876b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
168848abfe05SDavid S. Miller 			ret = -EBUSY;
1689ff4cc3acSMike Kershaw 		} else {
1690ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
16916b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
16926b8a66eeSJoe Perches 				  tun->dev->type);
169348abfe05SDavid S. Miller 			ret = 0;
1694ff4cc3acSMike Kershaw 		}
1695631ab46bSEric W. Biederman 		break;
1696ff4cc3acSMike Kershaw 
16971da177e4SLinus Torvalds #ifdef TUN_DEBUG
16981da177e4SLinus Torvalds 	case TUNSETDEBUG:
16991da177e4SLinus Torvalds 		tun->debug = arg;
17001da177e4SLinus Torvalds 		break;
17011da177e4SLinus Torvalds #endif
17025228ddc9SRusty Russell 	case TUNSETOFFLOAD:
170388255375SMichał Mirosław 		ret = set_offload(tun, arg);
1704631ab46bSEric W. Biederman 		break;
17055228ddc9SRusty Russell 
1706f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1707f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1708631ab46bSEric W. Biederman 		ret = -EINVAL;
1709f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1710631ab46bSEric W. Biederman 			break;
1711c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1712631ab46bSEric W. Biederman 		break;
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1715b595076aSUwe Kleine-König 		/* Get hw address */
1716f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1717f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
171850857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1719631ab46bSEric W. Biederman 			ret = -EFAULT;
1720631ab46bSEric W. Biederman 		break;
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1723f271b2ccSMax Krasnyansky 		/* Set hw address */
17246b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
17256b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
172640102371SKim B. Heino 
172740102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1728631ab46bSEric W. Biederman 		break;
172933dccbb0SHerbert Xu 
173033dccbb0SHerbert Xu 	case TUNGETSNDBUF:
173154f968d6SJason Wang 		sndbuf = tfile->socket.sk->sk_sndbuf;
173233dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
173333dccbb0SHerbert Xu 			ret = -EFAULT;
173433dccbb0SHerbert Xu 		break;
173533dccbb0SHerbert Xu 
173633dccbb0SHerbert Xu 	case TUNSETSNDBUF:
173733dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
173833dccbb0SHerbert Xu 			ret = -EFAULT;
173933dccbb0SHerbert Xu 			break;
174033dccbb0SHerbert Xu 		}
174133dccbb0SHerbert Xu 
1742c8d68e6bSJason Wang 		tun->sndbuf = sndbuf;
1743c8d68e6bSJason Wang 		tun_set_sndbuf(tun);
174433dccbb0SHerbert Xu 		break;
174533dccbb0SHerbert Xu 
1746d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1747d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1748d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1749d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1750d9d52b51SMichael S. Tsirkin 		break;
1751d9d52b51SMichael S. Tsirkin 
1752d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1753d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1754d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1755d9d52b51SMichael S. Tsirkin 			break;
1756d9d52b51SMichael S. Tsirkin 		}
1757d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1758d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1759d9d52b51SMichael S. Tsirkin 			break;
1760d9d52b51SMichael S. Tsirkin 		}
1761d9d52b51SMichael S. Tsirkin 
1762d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1763d9d52b51SMichael S. Tsirkin 		break;
1764d9d52b51SMichael S. Tsirkin 
176599405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
176699405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
176799405162SMichael S. Tsirkin 		ret = -EINVAL;
176899405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
176999405162SMichael S. Tsirkin 			break;
177099405162SMichael S. Tsirkin 		ret = -EFAULT;
177154f968d6SJason Wang 		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
177299405162SMichael S. Tsirkin 			break;
177399405162SMichael S. Tsirkin 
1774c8d68e6bSJason Wang 		ret = tun_attach_filter(tun);
177599405162SMichael S. Tsirkin 		break;
177699405162SMichael S. Tsirkin 
177799405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
177899405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
177999405162SMichael S. Tsirkin 		ret = -EINVAL;
178099405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
178199405162SMichael S. Tsirkin 			break;
1782c8d68e6bSJason Wang 		ret = 0;
1783c8d68e6bSJason Wang 		tun_detach_filter(tun, tun->numqueues);
178499405162SMichael S. Tsirkin 		break;
178599405162SMichael S. Tsirkin 
17861da177e4SLinus Torvalds 	default:
1787631ab46bSEric W. Biederman 		ret = -EINVAL;
1788631ab46bSEric W. Biederman 		break;
1789ee289b64SJoe Perches 	}
17901da177e4SLinus Torvalds 
1791876bfd4dSHerbert Xu unlock:
1792876bfd4dSHerbert Xu 	rtnl_unlock();
1793876bfd4dSHerbert Xu 	if (tun)
1794631ab46bSEric W. Biederman 		tun_put(tun);
1795631ab46bSEric W. Biederman 	return ret;
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
179850857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
179950857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
180050857e2aSArnd Bergmann {
180150857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
180250857e2aSArnd Bergmann }
180350857e2aSArnd Bergmann 
180450857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
180550857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
180650857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
180750857e2aSArnd Bergmann {
180850857e2aSArnd Bergmann 	switch (cmd) {
180950857e2aSArnd Bergmann 	case TUNSETIFF:
181050857e2aSArnd Bergmann 	case TUNGETIFF:
181150857e2aSArnd Bergmann 	case TUNSETTXFILTER:
181250857e2aSArnd Bergmann 	case TUNGETSNDBUF:
181350857e2aSArnd Bergmann 	case TUNSETSNDBUF:
181450857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
181550857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
181650857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
181750857e2aSArnd Bergmann 		break;
181850857e2aSArnd Bergmann 	default:
181950857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
182050857e2aSArnd Bergmann 		break;
182150857e2aSArnd Bergmann 	}
182250857e2aSArnd Bergmann 
182350857e2aSArnd Bergmann 	/*
182450857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
182550857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
182650857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
182750857e2aSArnd Bergmann 	 * contents.
182850857e2aSArnd Bergmann 	 */
182950857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
183050857e2aSArnd Bergmann }
183150857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
183250857e2aSArnd Bergmann 
18331da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
18341da177e4SLinus Torvalds {
183554f968d6SJason Wang 	struct tun_file *tfile = file->private_data;
18361da177e4SLinus Torvalds 	int ret;
18371da177e4SLinus Torvalds 
183854f968d6SJason Wang 	if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
18399d319522SJonathan Corbet 		goto out;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	if (on) {
1842609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
18431da177e4SLinus Torvalds 		if (ret)
18449d319522SJonathan Corbet 			goto out;
184554f968d6SJason Wang 		tfile->flags |= TUN_FASYNC;
18461da177e4SLinus Torvalds 	} else
184754f968d6SJason Wang 		tfile->flags &= ~TUN_FASYNC;
18489d319522SJonathan Corbet 	ret = 0;
18499d319522SJonathan Corbet out:
18509d319522SJonathan Corbet 	return ret;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
18541da177e4SLinus Torvalds {
1855631ab46bSEric W. Biederman 	struct tun_file *tfile;
1856deed49fbSThomas Gleixner 
18576b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1858631ab46bSEric W. Biederman 
185954f968d6SJason Wang 	tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
186054f968d6SJason Wang 					    &tun_proto);
1861631ab46bSEric W. Biederman 	if (!tfile)
1862631ab46bSEric W. Biederman 		return -ENOMEM;
18636e914fc7SJason Wang 	rcu_assign_pointer(tfile->tun, NULL);
186436b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
186554f968d6SJason Wang 	tfile->flags = 0;
186654f968d6SJason Wang 
186754f968d6SJason Wang 	rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
186854f968d6SJason Wang 	init_waitqueue_head(&tfile->wq.wait);
186954f968d6SJason Wang 
187054f968d6SJason Wang 	tfile->socket.file = file;
187154f968d6SJason Wang 	tfile->socket.ops = &tun_socket_ops;
187254f968d6SJason Wang 
187354f968d6SJason Wang 	sock_init_data(&tfile->socket, &tfile->sk);
187454f968d6SJason Wang 	sk_change_net(&tfile->sk, tfile->net);
187554f968d6SJason Wang 
187654f968d6SJason Wang 	tfile->sk.sk_write_space = tun_sock_write_space;
187754f968d6SJason Wang 	tfile->sk.sk_sndbuf = INT_MAX;
187854f968d6SJason Wang 
1879631ab46bSEric W. Biederman 	file->private_data = tfile;
188054f968d6SJason Wang 	set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
188154f968d6SJason Wang 
18821da177e4SLinus Torvalds 	return 0;
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
18861da177e4SLinus Torvalds {
1887631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
188854f968d6SJason Wang 	struct net *net = tfile->net;
18891da177e4SLinus Torvalds 
1890c8d68e6bSJason Wang 	tun_detach(tfile, true);
189154f968d6SJason Wang 	put_net(net);
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	return 0;
18941da177e4SLinus Torvalds }
18951da177e4SLinus Torvalds 
1896d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
18971da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
18981da177e4SLinus Torvalds 	.llseek = no_llseek,
1899ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1900ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1901ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1902ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
19031da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1904876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
190550857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
190650857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
190750857e2aSArnd Bergmann #endif
19081da177e4SLinus Torvalds 	.open	= tun_chr_open,
19091da177e4SLinus Torvalds 	.release = tun_chr_close,
19101da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
19111da177e4SLinus Torvalds };
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
19141da177e4SLinus Torvalds 	.minor = TUN_MINOR,
19151da177e4SLinus Torvalds 	.name = "tun",
1916e454cea2SKay Sievers 	.nodename = "net/tun",
19171da177e4SLinus Torvalds 	.fops = &tun_fops,
19181da177e4SLinus Torvalds };
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds /* ethtool interface */
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
19231da177e4SLinus Torvalds {
19241da177e4SLinus Torvalds 	cmd->supported		= 0;
19251da177e4SLinus Torvalds 	cmd->advertising	= 0;
192670739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
19271da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
19281da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
19291da177e4SLinus Torvalds 	cmd->phy_address	= 0;
19301da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
19311da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
19321da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
19331da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
19341da177e4SLinus Torvalds 	return 0;
19351da177e4SLinus Torvalds }
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
19381da177e4SLinus Torvalds {
19391da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
19401da177e4SLinus Torvalds 
194133a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
194233a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
19451da177e4SLinus Torvalds 	case TUN_TUN_DEV:
194633a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
19471da177e4SLinus Torvalds 		break;
19481da177e4SLinus Torvalds 	case TUN_TAP_DEV:
194933a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
19501da177e4SLinus Torvalds 		break;
19511da177e4SLinus Torvalds 	}
19521da177e4SLinus Torvalds }
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
19551da177e4SLinus Torvalds {
19561da177e4SLinus Torvalds #ifdef TUN_DEBUG
19571da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
19581da177e4SLinus Torvalds 	return tun->debug;
19591da177e4SLinus Torvalds #else
19601da177e4SLinus Torvalds 	return -EOPNOTSUPP;
19611da177e4SLinus Torvalds #endif
19621da177e4SLinus Torvalds }
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
19651da177e4SLinus Torvalds {
19661da177e4SLinus Torvalds #ifdef TUN_DEBUG
19671da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
19681da177e4SLinus Torvalds 	tun->debug = value;
19691da177e4SLinus Torvalds #endif
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds 
19727282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
19731da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
19741da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
19751da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
19761da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
1977bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
19781da177e4SLinus Torvalds };
19791da177e4SLinus Torvalds 
198079d17604SPavel Emelyanov 
19811da177e4SLinus Torvalds static int __init tun_init(void)
19821da177e4SLinus Torvalds {
19831da177e4SLinus Torvalds 	int ret = 0;
19841da177e4SLinus Torvalds 
19856b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
19866b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
19871da177e4SLinus Torvalds 
1988f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
198979d17604SPavel Emelyanov 	if (ret) {
19906b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
1991f019a7a5SEric W. Biederman 		goto err_linkops;
199279d17604SPavel Emelyanov 	}
199379d17604SPavel Emelyanov 
19941da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
199579d17604SPavel Emelyanov 	if (ret) {
19966b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
199779d17604SPavel Emelyanov 		goto err_misc;
199879d17604SPavel Emelyanov 	}
199979d17604SPavel Emelyanov 	return  0;
200079d17604SPavel Emelyanov err_misc:
2001f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
2002f019a7a5SEric W. Biederman err_linkops:
20031da177e4SLinus Torvalds 	return ret;
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds static void tun_cleanup(void)
20071da177e4SLinus Torvalds {
20081da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
2009f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
20101da177e4SLinus Torvalds }
20111da177e4SLinus Torvalds 
201205c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
201305c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
201405c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
201505c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
201605c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
201705c2828cSMichael S. Tsirkin {
20186e914fc7SJason Wang 	struct tun_file *tfile;
201905c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
202005c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
20216e914fc7SJason Wang 	tfile = file->private_data;
20226e914fc7SJason Wang 	if (!tfile)
202305c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
202454f968d6SJason Wang 	return &tfile->socket;
202505c2828cSMichael S. Tsirkin }
202605c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
202705c2828cSMichael S. Tsirkin 
20281da177e4SLinus Torvalds module_init(tun_init);
20291da177e4SLinus Torvalds module_exit(tun_cleanup);
20301da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
20311da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
20321da177e4SLinus Torvalds MODULE_LICENSE("GPL");
20331da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
2034578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
2035