xref: /openbmc/linux/drivers/net/tun.c (revision 868eefeb17d40f6acde00ad8165a268529cf6d24)
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 
103f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
104f271b2ccSMax Krasnyansky struct tap_filter {
105f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
106f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
107f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
108f271b2ccSMax Krasnyansky };
109f271b2ccSMax Krasnyansky 
110631ab46bSEric W. Biederman struct tun_file {
111c70f1829SEric W. Biederman 	atomic_t count;
112631ab46bSEric W. Biederman 	struct tun_struct *tun;
11336b50babSEric W. Biederman 	struct net *net;
114631ab46bSEric W. Biederman };
115631ab46bSEric W. Biederman 
11633dccbb0SHerbert Xu struct tun_sock;
11733dccbb0SHerbert Xu 
11814daa021SRusty Russell struct tun_struct {
119631ab46bSEric W. Biederman 	struct tun_file		*tfile;
120f271b2ccSMax Krasnyansky 	unsigned int 		flags;
12114daa021SRusty Russell 	uid_t			owner;
12214daa021SRusty Russell 	gid_t			group;
12314daa021SRusty Russell 
12414daa021SRusty Russell 	struct net_device	*dev;
125c8f44affSMichał Mirosław 	netdev_features_t	set_features;
12688255375SMichał Mirosław #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
12788255375SMichał Mirosław 			  NETIF_F_TSO6|NETIF_F_UFO)
12814daa021SRusty Russell 	struct fasync_struct	*fasync;
12914daa021SRusty Russell 
130f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
13133dccbb0SHerbert Xu 	struct socket		socket;
13243815482SEric Dumazet 	struct socket_wq	wq;
133d9d52b51SMichael S. Tsirkin 
134d9d52b51SMichael S. Tsirkin 	int			vnet_hdr_sz;
135d9d52b51SMichael S. Tsirkin 
13614daa021SRusty Russell #ifdef TUN_DEBUG
13714daa021SRusty Russell 	int debug;
13814daa021SRusty Russell #endif
13914daa021SRusty Russell };
14014daa021SRusty Russell 
14133dccbb0SHerbert Xu struct tun_sock {
14233dccbb0SHerbert Xu 	struct sock		sk;
14333dccbb0SHerbert Xu 	struct tun_struct	*tun;
14433dccbb0SHerbert Xu };
14533dccbb0SHerbert Xu 
14633dccbb0SHerbert Xu static inline struct tun_sock *tun_sk(struct sock *sk)
14733dccbb0SHerbert Xu {
14833dccbb0SHerbert Xu 	return container_of(sk, struct tun_sock, sk);
14933dccbb0SHerbert Xu }
15033dccbb0SHerbert Xu 
151a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
152a7385ba2SEric W. Biederman {
153631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
15438231b7aSEric W. Biederman 	int err;
155a7385ba2SEric W. Biederman 
156a7385ba2SEric W. Biederman 	ASSERT_RTNL();
157a7385ba2SEric W. Biederman 
15838231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
15938231b7aSEric W. Biederman 
16038231b7aSEric W. Biederman 	err = -EINVAL;
16138231b7aSEric W. Biederman 	if (tfile->tun)
16238231b7aSEric W. Biederman 		goto out;
16338231b7aSEric W. Biederman 
16438231b7aSEric W. Biederman 	err = -EBUSY;
16538231b7aSEric W. Biederman 	if (tun->tfile)
16638231b7aSEric W. Biederman 		goto out;
16738231b7aSEric W. Biederman 
16838231b7aSEric W. Biederman 	err = 0;
169631ab46bSEric W. Biederman 	tfile->tun = tun;
170631ab46bSEric W. Biederman 	tun->tfile = tfile;
17105c2828cSMichael S. Tsirkin 	tun->socket.file = file;
172bee31369SNolan Leake 	netif_carrier_on(tun->dev);
173c70f1829SEric W. Biederman 	dev_hold(tun->dev);
17489f56d1eSMichael S. Tsirkin 	sock_hold(tun->socket.sk);
175c70f1829SEric W. Biederman 	atomic_inc(&tfile->count);
176a7385ba2SEric W. Biederman 
17738231b7aSEric W. Biederman out:
17838231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
17938231b7aSEric W. Biederman 	return err;
180a7385ba2SEric W. Biederman }
181a7385ba2SEric W. Biederman 
182631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
183631ab46bSEric W. Biederman {
184631ab46bSEric W. Biederman 	/* Detach from net device */
18538231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
186bee31369SNolan Leake 	netif_carrier_off(tun->dev);
187631ab46bSEric W. Biederman 	tun->tfile = NULL;
18805c2828cSMichael S. Tsirkin 	tun->socket.file = NULL;
18938231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
190631ab46bSEric W. Biederman 
191631ab46bSEric W. Biederman 	/* Drop read queue */
19289f56d1eSMichael S. Tsirkin 	skb_queue_purge(&tun->socket.sk->sk_receive_queue);
193c70f1829SEric W. Biederman 
194c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
195c70f1829SEric W. Biederman 	dev_put(tun->dev);
196c70f1829SEric W. Biederman }
197c70f1829SEric W. Biederman 
198c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
199c70f1829SEric W. Biederman {
200c70f1829SEric W. Biederman 	rtnl_lock();
201c70f1829SEric W. Biederman 	__tun_detach(tun);
202c70f1829SEric W. Biederman 	rtnl_unlock();
203631ab46bSEric W. Biederman }
204631ab46bSEric W. Biederman 
205631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
206631ab46bSEric W. Biederman {
207c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
208c70f1829SEric W. Biederman 
209c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
210c70f1829SEric W. Biederman 		tun = tfile->tun;
211c70f1829SEric W. Biederman 
212c70f1829SEric W. Biederman 	return tun;
213631ab46bSEric W. Biederman }
214631ab46bSEric W. Biederman 
215631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
216631ab46bSEric W. Biederman {
217631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
218631ab46bSEric W. Biederman }
219631ab46bSEric W. Biederman 
220631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
221631ab46bSEric W. Biederman {
222c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
223c70f1829SEric W. Biederman 
224c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
225c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
226631ab46bSEric W. Biederman }
227631ab46bSEric W. Biederman 
2286b8a66eeSJoe Perches /* TAP filtering */
229f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
230f271b2ccSMax Krasnyansky {
231f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
232f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
233f271b2ccSMax Krasnyansky }
234f271b2ccSMax Krasnyansky 
235f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
236f271b2ccSMax Krasnyansky {
237f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
238f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
239f271b2ccSMax Krasnyansky }
240f271b2ccSMax Krasnyansky 
241f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
242f271b2ccSMax Krasnyansky {
243f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
244f271b2ccSMax Krasnyansky 	struct tun_filter uf;
245f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
246f271b2ccSMax Krasnyansky 
247f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
248f271b2ccSMax Krasnyansky 		return -EFAULT;
249f271b2ccSMax Krasnyansky 
250f271b2ccSMax Krasnyansky 	if (!uf.count) {
251f271b2ccSMax Krasnyansky 		/* Disabled */
252f271b2ccSMax Krasnyansky 		filter->count = 0;
253f271b2ccSMax Krasnyansky 		return 0;
254f271b2ccSMax Krasnyansky 	}
255f271b2ccSMax Krasnyansky 
256f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
257f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
258f271b2ccSMax Krasnyansky 	if (!addr)
259f271b2ccSMax Krasnyansky 		return -ENOMEM;
260f271b2ccSMax Krasnyansky 
261f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
262f271b2ccSMax Krasnyansky 		err = -EFAULT;
263f271b2ccSMax Krasnyansky 		goto done;
264f271b2ccSMax Krasnyansky 	}
265f271b2ccSMax Krasnyansky 
266f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
267f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
268f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
269f271b2ccSMax Krasnyansky 	filter->count = 0;
270f271b2ccSMax Krasnyansky 	wmb();
271f271b2ccSMax Krasnyansky 
272f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
273f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
274f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
275f271b2ccSMax Krasnyansky 
276f271b2ccSMax Krasnyansky 	nexact = n;
277f271b2ccSMax Krasnyansky 
278cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
279cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
280f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
281cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
282cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
283cfbf84fcSAlex Williamson 			err = 0; /* no filter */
284cfbf84fcSAlex Williamson 			goto done;
285cfbf84fcSAlex Williamson 		}
286f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
287cfbf84fcSAlex Williamson 	}
288f271b2ccSMax Krasnyansky 
289f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
290f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
291f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
292f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
293f271b2ccSMax Krasnyansky 
294f271b2ccSMax Krasnyansky 	/* Now enable the filter */
295f271b2ccSMax Krasnyansky 	wmb();
296f271b2ccSMax Krasnyansky 	filter->count = nexact;
297f271b2ccSMax Krasnyansky 
298f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
299f271b2ccSMax Krasnyansky 	err = nexact;
300f271b2ccSMax Krasnyansky 
301f271b2ccSMax Krasnyansky done:
302f271b2ccSMax Krasnyansky 	kfree(addr);
303f271b2ccSMax Krasnyansky 	return err;
304f271b2ccSMax Krasnyansky }
305f271b2ccSMax Krasnyansky 
306f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
307f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
308f271b2ccSMax Krasnyansky {
309f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
310f271b2ccSMax Krasnyansky 	 * at this point. */
311f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
312f271b2ccSMax Krasnyansky 	int i;
313f271b2ccSMax Krasnyansky 
314f271b2ccSMax Krasnyansky 	/* Exact match */
315f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
3162e42e474SJoe Perches 		if (ether_addr_equal(eh->h_dest, filter->addr[i]))
317f271b2ccSMax Krasnyansky 			return 1;
318f271b2ccSMax Krasnyansky 
319f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
320f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
321f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
322f271b2ccSMax Krasnyansky 
323f271b2ccSMax Krasnyansky 	return 0;
324f271b2ccSMax Krasnyansky }
325f271b2ccSMax Krasnyansky 
326f271b2ccSMax Krasnyansky /*
327f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
328f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
329f271b2ccSMax Krasnyansky  */
330f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
331f271b2ccSMax Krasnyansky {
332f271b2ccSMax Krasnyansky 	if (!filter->count)
333f271b2ccSMax Krasnyansky 		return 1;
334f271b2ccSMax Krasnyansky 
335f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
336f271b2ccSMax Krasnyansky }
337f271b2ccSMax Krasnyansky 
3381da177e4SLinus Torvalds /* Network device part of the driver */
3391da177e4SLinus Torvalds 
3407282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3411da177e4SLinus Torvalds 
342c70f1829SEric W. Biederman /* Net device detach from fd. */
343c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
344c70f1829SEric W. Biederman {
345c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
346c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
347c70f1829SEric W. Biederman 
348c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
349c70f1829SEric W. Biederman 	 */
350c70f1829SEric W. Biederman 	if (tfile) {
35143815482SEric Dumazet 		wake_up_all(&tun->wq.wait);
352c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
353c70f1829SEric W. Biederman 			__tun_detach(tun);
354c70f1829SEric W. Biederman 	}
355c70f1829SEric W. Biederman }
356c70f1829SEric W. Biederman 
3579c3fea6aSHerbert Xu static void tun_free_netdev(struct net_device *dev)
3589c3fea6aSHerbert Xu {
3599c3fea6aSHerbert Xu 	struct tun_struct *tun = netdev_priv(dev);
3609c3fea6aSHerbert Xu 
361b09e786bSMikulas Patocka 	BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags));
362b09e786bSMikulas Patocka 
3631ab5ecb9SStanislav Kinsbursky 	sk_release_kernel(tun->socket.sk);
3649c3fea6aSHerbert Xu }
3659c3fea6aSHerbert Xu 
3661da177e4SLinus Torvalds /* Net device open. */
3671da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	netif_start_queue(dev);
3701da177e4SLinus Torvalds 	return 0;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds /* Net device close. */
3741da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	netif_stop_queue(dev);
3771da177e4SLinus Torvalds 	return 0;
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /* Net device start xmit */
381424efe9cSStephen Hemminger static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3841da177e4SLinus Torvalds 
3856b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
388631ab46bSEric W. Biederman 	if (!tun->tfile)
3891da177e4SLinus Torvalds 		goto drop;
3901da177e4SLinus Torvalds 
391f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
392f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
393f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
394f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
395f271b2ccSMax Krasnyansky 		goto drop;
396f271b2ccSMax Krasnyansky 
39799405162SMichael S. Tsirkin 	if (tun->socket.sk->sk_filter &&
39899405162SMichael S. Tsirkin 	    sk_filter(tun->socket.sk, skb))
39999405162SMichael S. Tsirkin 		goto drop;
40099405162SMichael S. Tsirkin 
40189f56d1eSMichael S. Tsirkin 	if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
4021da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
4031da177e4SLinus Torvalds 			/* Normal queueing mode. */
4041da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
4051da177e4SLinus Torvalds 			netif_stop_queue(dev);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
4081da177e4SLinus Torvalds 			 * error is more appropriate. */
40909f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
4101da177e4SLinus Torvalds 		} else {
4111da177e4SLinus Torvalds 			/* Single queue mode.
4121da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
4131da177e4SLinus Torvalds 			goto drop;
4141da177e4SLinus Torvalds 		}
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 
4170110d6f2SMichael S. Tsirkin 	/* Orphan the skb - required as we might hang on to it
4180110d6f2SMichael S. Tsirkin 	 * for indefinite time. */
419*868eefebSMichael S. Tsirkin 	if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
420*868eefebSMichael S. Tsirkin 		goto drop;
4210110d6f2SMichael S. Tsirkin 	skb_orphan(skb);
4220110d6f2SMichael S. Tsirkin 
423f271b2ccSMax Krasnyansky 	/* Enqueue packet */
42489f56d1eSMichael S. Tsirkin 	skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	/* Notify and wake up reader process */
4271da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
4281da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
42943815482SEric Dumazet 	wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
43005c2828cSMichael S. Tsirkin 				   POLLRDNORM | POLLRDBAND);
4316ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds drop:
43409f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4351da177e4SLinus Torvalds 	kfree_skb(skb);
4366ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
439f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4401da177e4SLinus Torvalds {
441f271b2ccSMax Krasnyansky 	/*
442f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
443f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
444f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
445f271b2ccSMax Krasnyansky 	 */
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4484885a504SEd Swierk #define MIN_MTU 68
4494885a504SEd Swierk #define MAX_MTU 65535
4504885a504SEd Swierk 
4514885a504SEd Swierk static int
4524885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4534885a504SEd Swierk {
4544885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4554885a504SEd Swierk 		return -EINVAL;
4564885a504SEd Swierk 	dev->mtu = new_mtu;
4574885a504SEd Swierk 	return 0;
4584885a504SEd Swierk }
4594885a504SEd Swierk 
460c8f44affSMichał Mirosław static netdev_features_t tun_net_fix_features(struct net_device *dev,
461c8f44affSMichał Mirosław 	netdev_features_t features)
46288255375SMichał Mirosław {
46388255375SMichał Mirosław 	struct tun_struct *tun = netdev_priv(dev);
46488255375SMichał Mirosław 
46588255375SMichał Mirosław 	return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
46688255375SMichał Mirosław }
467bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
468bebd097aSNeil Horman static void tun_poll_controller(struct net_device *dev)
469bebd097aSNeil Horman {
470bebd097aSNeil Horman 	/*
471bebd097aSNeil Horman 	 * Tun only receives frames when:
472bebd097aSNeil Horman 	 * 1) the char device endpoint gets data from user space
473bebd097aSNeil Horman 	 * 2) the tun socket gets a sendmsg call from user space
474bebd097aSNeil Horman 	 * Since both of those are syncronous operations, we are guaranteed
475bebd097aSNeil Horman 	 * never to have pending data when we poll for it
476bebd097aSNeil Horman 	 * so theres nothing to do here but return.
477bebd097aSNeil Horman 	 * We need this though so netpoll recognizes us as an interface that
478bebd097aSNeil Horman 	 * supports polling, which enables bridge devices in virt setups to
479bebd097aSNeil Horman 	 * still use netconsole
480bebd097aSNeil Horman 	 */
481bebd097aSNeil Horman 	return;
482bebd097aSNeil Horman }
483bebd097aSNeil Horman #endif
484758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
485c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
486758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
487758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
48800829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
489758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
49088255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
491bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
492bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
493bebd097aSNeil Horman #endif
494758e43b7SStephen Hemminger };
495758e43b7SStephen Hemminger 
496758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
497c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
498758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
499758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
50000829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
501758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
50288255375SMichał Mirosław 	.ndo_fix_features	= tun_net_fix_features,
503afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= tun_net_mclist,
504758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
505758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
506bebd097aSNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
507bebd097aSNeil Horman 	.ndo_poll_controller	= tun_poll_controller,
508bebd097aSNeil Horman #endif
509758e43b7SStephen Hemminger };
510758e43b7SStephen Hemminger 
5111da177e4SLinus Torvalds /* Initialize net device. */
5121da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
5171da177e4SLinus Torvalds 	case TUN_TUN_DEV:
518758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
519758e43b7SStephen Hemminger 
5201da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
5211da177e4SLinus Torvalds 		dev->hard_header_len = 0;
5221da177e4SLinus Torvalds 		dev->addr_len = 0;
5231da177e4SLinus Torvalds 		dev->mtu = 1500;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		/* Zero header length */
5261da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
5271da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
5281da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5291da177e4SLinus Torvalds 		break;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5327a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
5331da177e4SLinus Torvalds 		/* Ethernet TAP Device */
5341da177e4SLinus Torvalds 		ether_setup(dev);
535550fd08cSNeil Horman 		dev->priv_flags &= ~IFF_TX_SKB_SHARING;
53636226a8dSBrian Braunstein 
537f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
53836226a8dSBrian Braunstein 
5391da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
5401da177e4SLinus Torvalds 		break;
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds /* Character device part */
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds /* Poll */
5471da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
5481da177e4SLinus Torvalds {
549b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
550b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
5513c8a9c63SMariusz Kozlowski 	struct sock *sk;
55233dccbb0SHerbert Xu 	unsigned int mask = 0;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	if (!tun)
555eac9e902SEric W. Biederman 		return POLLERR;
5561da177e4SLinus Torvalds 
55789f56d1eSMichael S. Tsirkin 	sk = tun->socket.sk;
5583c8a9c63SMariusz Kozlowski 
5596b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
5601da177e4SLinus Torvalds 
56143815482SEric Dumazet 	poll_wait(file, &tun->wq.wait, wait);
5621da177e4SLinus Torvalds 
56389f56d1eSMichael S. Tsirkin 	if (!skb_queue_empty(&sk->sk_receive_queue))
5641da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5651da177e4SLinus Torvalds 
56633dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
56733dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
56833dccbb0SHerbert Xu 	     sock_writeable(sk)))
56933dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
57033dccbb0SHerbert Xu 
571c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
572c70f1829SEric W. Biederman 		mask = POLLERR;
573c70f1829SEric W. Biederman 
574631ab46bSEric W. Biederman 	tun_put(tun);
5751da177e4SLinus Torvalds 	return mask;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
578f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
579f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
5806f7c156cSstephen hemminger static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
58133dccbb0SHerbert Xu 				     size_t prepad, size_t len,
58233dccbb0SHerbert Xu 				     size_t linear, int noblock)
583f42157cbSRusty Russell {
58489f56d1eSMichael S. Tsirkin 	struct sock *sk = tun->socket.sk;
585f42157cbSRusty Russell 	struct sk_buff *skb;
58633dccbb0SHerbert Xu 	int err;
587f42157cbSRusty Russell 
58882862742SHerbert Xu 	sock_update_classid(sk);
58982862742SHerbert Xu 
590f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5910eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
59233dccbb0SHerbert Xu 		linear = len;
593f42157cbSRusty Russell 
59433dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
59533dccbb0SHerbert Xu 				   &err);
596f42157cbSRusty Russell 	if (!skb)
59733dccbb0SHerbert Xu 		return ERR_PTR(err);
598f42157cbSRusty Russell 
599f42157cbSRusty Russell 	skb_reserve(skb, prepad);
600f42157cbSRusty Russell 	skb_put(skb, linear);
60133dccbb0SHerbert Xu 	skb->data_len = len - linear;
60233dccbb0SHerbert Xu 	skb->len += len - linear;
603f42157cbSRusty Russell 
604f42157cbSRusty Russell 	return skb;
605f42157cbSRusty Russell }
606f42157cbSRusty Russell 
6071da177e4SLinus Torvalds /* Get packet from user space buffer */
6086f7c156cSstephen hemminger static ssize_t tun_get_user(struct tun_struct *tun,
6096f26c9a7SMichael S. Tsirkin 			    const struct iovec *iv, size_t count,
61033dccbb0SHerbert Xu 			    int noblock)
6111da177e4SLinus Torvalds {
61209640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
6131da177e4SLinus Torvalds 	struct sk_buff *skb;
614a504b86eSstephen hemminger 	size_t len = count, align = NET_SKB_PAD;
615f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
6166f26c9a7SMichael S. Tsirkin 	int offset = 0;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
6191da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
6201da177e4SLinus Torvalds 			return -EINVAL;
6211da177e4SLinus Torvalds 
6226f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
6231da177e4SLinus Torvalds 			return -EFAULT;
6246f26c9a7SMichael S. Tsirkin 		offset += sizeof(pi);
6251da177e4SLinus Torvalds 	}
6261da177e4SLinus Torvalds 
627f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
628d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) > count)
629f43798c2SRusty Russell 			return -EINVAL;
630f43798c2SRusty Russell 
6316f26c9a7SMichael S. Tsirkin 		if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
632f43798c2SRusty Russell 			return -EFAULT;
633f43798c2SRusty Russell 
6344909122fSHerbert Xu 		if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
6354909122fSHerbert Xu 		    gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
6364909122fSHerbert Xu 			gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
6374909122fSHerbert Xu 
638f43798c2SRusty Russell 		if (gso.hdr_len > len)
639f43798c2SRusty Russell 			return -EINVAL;
640d9d52b51SMichael S. Tsirkin 		offset += tun->vnet_hdr_sz;
641f43798c2SRusty Russell 	}
642f43798c2SRusty Russell 
643e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
644a504b86eSstephen hemminger 		align += NET_IP_ALIGN;
6450eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
6460eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
647e01bf1c8SRusty Russell 			return -EINVAL;
648e01bf1c8SRusty Russell 	}
6491da177e4SLinus Torvalds 
65033dccbb0SHerbert Xu 	skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
65133dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
65233dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
65309f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
65433dccbb0SHerbert Xu 		return PTR_ERR(skb);
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6576f26c9a7SMichael S. Tsirkin 	if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
65809f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
6598f22757eSDave Jones 		kfree_skb(skb);
6601da177e4SLinus Torvalds 		return -EFAULT;
6618f22757eSDave Jones 	}
6621da177e4SLinus Torvalds 
663f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
664f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
665f43798c2SRusty Russell 					  gso.csum_offset)) {
666f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
667f43798c2SRusty Russell 			kfree_skb(skb);
668f43798c2SRusty Russell 			return -EINVAL;
669f43798c2SRusty Russell 		}
67088255375SMichał Mirosław 	}
671f43798c2SRusty Russell 
6721da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
6731da177e4SLinus Torvalds 	case TUN_TUN_DEV:
674f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
675f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
676f09f7ee2SAng Way Chuang 			case 0x40:
677f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
678f09f7ee2SAng Way Chuang 				break;
679f09f7ee2SAng Way Chuang 			case 0x60:
680f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
681f09f7ee2SAng Way Chuang 				break;
682f09f7ee2SAng Way Chuang 			default:
683f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
684f09f7ee2SAng Way Chuang 				kfree_skb(skb);
685f09f7ee2SAng Way Chuang 				return -EINVAL;
686f09f7ee2SAng Way Chuang 			}
687f09f7ee2SAng Way Chuang 		}
688f09f7ee2SAng Way Chuang 
689459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
6901da177e4SLinus Torvalds 		skb->protocol = pi.proto;
6914c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
6921da177e4SLinus Torvalds 		break;
6931da177e4SLinus Torvalds 	case TUN_TAP_DEV:
6941da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
6951da177e4SLinus Torvalds 		break;
6966403eab1SJoe Perches 	}
6971da177e4SLinus Torvalds 
698f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
699f43798c2SRusty Russell 		pr_debug("GSO!\n");
700f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
701f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
702f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
703f43798c2SRusty Russell 			break;
704f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
705f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
706f43798c2SRusty Russell 			break;
707e36aa25aSSridhar Samudrala 		case VIRTIO_NET_HDR_GSO_UDP:
708e36aa25aSSridhar Samudrala 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
709e36aa25aSSridhar Samudrala 			break;
710f43798c2SRusty Russell 		default:
711f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
712f43798c2SRusty Russell 			kfree_skb(skb);
713f43798c2SRusty Russell 			return -EINVAL;
714f43798c2SRusty Russell 		}
715f43798c2SRusty Russell 
716f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
717f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
718f43798c2SRusty Russell 
719f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
720f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
721f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
722f43798c2SRusty Russell 			kfree_skb(skb);
723f43798c2SRusty Russell 			return -EINVAL;
724f43798c2SRusty Russell 		}
725f43798c2SRusty Russell 
726f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
727f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
728f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
729f43798c2SRusty Russell 	}
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	netif_rx_ni(skb);
7321da177e4SLinus Torvalds 
73309f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
73409f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	return count;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
739ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
740ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
7411da177e4SLinus Torvalds {
74233dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
743ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
744631ab46bSEric W. Biederman 	ssize_t result;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	if (!tun)
7471da177e4SLinus Torvalds 		return -EBADFD;
7481da177e4SLinus Torvalds 
7496b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
7501da177e4SLinus Torvalds 
7516f26c9a7SMichael S. Tsirkin 	result = tun_get_user(tun, iv, iov_length(iv, count),
75233dccbb0SHerbert Xu 			      file->f_flags & O_NONBLOCK);
753631ab46bSEric W. Biederman 
754631ab46bSEric W. Biederman 	tun_put(tun);
755631ab46bSEric W. Biederman 	return result;
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds /* Put packet to the user space buffer */
7596f7c156cSstephen hemminger static ssize_t tun_put_user(struct tun_struct *tun,
7601da177e4SLinus Torvalds 			    struct sk_buff *skb,
76143b39dcdSMichael S. Tsirkin 			    const struct iovec *iv, int len)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
7641da177e4SLinus Torvalds 	ssize_t total = 0;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
7671da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
7681da177e4SLinus Torvalds 			return -EINVAL;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 		if (len < skb->len) {
7711da177e4SLinus Torvalds 			/* Packet will be striped */
7721da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
7731da177e4SLinus Torvalds 		}
7741da177e4SLinus Torvalds 
77543b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
7761da177e4SLinus Torvalds 			return -EFAULT;
7771da177e4SLinus Torvalds 		total += sizeof(pi);
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 
780f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
781f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
782d9d52b51SMichael S. Tsirkin 		if ((len -= tun->vnet_hdr_sz) < 0)
783f43798c2SRusty Russell 			return -EINVAL;
784f43798c2SRusty Russell 
785f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
786f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
787f43798c2SRusty Russell 
788f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
789f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
790f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
791f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
792f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
793f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
794f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
795e36aa25aSSridhar Samudrala 			else if (sinfo->gso_type & SKB_GSO_UDP)
796e36aa25aSSridhar Samudrala 				gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
797ef3db4a5SMichael S. Tsirkin 			else {
7986b8a66eeSJoe Perches 				pr_err("unexpected GSO type: "
799ef3db4a5SMichael S. Tsirkin 				       "0x%x, gso_size %d, hdr_len %d\n",
800ef3db4a5SMichael S. Tsirkin 				       sinfo->gso_type, gso.gso_size,
801ef3db4a5SMichael S. Tsirkin 				       gso.hdr_len);
802ef3db4a5SMichael S. Tsirkin 				print_hex_dump(KERN_ERR, "tun: ",
803ef3db4a5SMichael S. Tsirkin 					       DUMP_PREFIX_NONE,
804ef3db4a5SMichael S. Tsirkin 					       16, 1, skb->head,
805ef3db4a5SMichael S. Tsirkin 					       min((int)gso.hdr_len, 64), true);
806ef3db4a5SMichael S. Tsirkin 				WARN_ON_ONCE(1);
807ef3db4a5SMichael S. Tsirkin 				return -EINVAL;
808ef3db4a5SMichael S. Tsirkin 			}
809f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
810f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
811f43798c2SRusty Russell 		} else
812f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
813f43798c2SRusty Russell 
814f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
815f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
81655508d60SMichał Mirosław 			gso.csum_start = skb_checksum_start_offset(skb);
817f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
81810a8d94aSJason Wang 		} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
81910a8d94aSJason Wang 			gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
820f43798c2SRusty Russell 		} /* else everything is zero */
821f43798c2SRusty Russell 
82243b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
82343b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
824f43798c2SRusty Russell 			return -EFAULT;
825d9d52b51SMichael S. Tsirkin 		total += tun->vnet_hdr_sz;
826f43798c2SRusty Russell 	}
827f43798c2SRusty Russell 
8281da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
8291da177e4SLinus Torvalds 
83043b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
83105c2828cSMichael S. Tsirkin 	total += skb->len;
8321da177e4SLinus Torvalds 
83309f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
83409f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	return total;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
83905c2828cSMichael S. Tsirkin static ssize_t tun_do_read(struct tun_struct *tun,
84005c2828cSMichael S. Tsirkin 			   struct kiocb *iocb, const struct iovec *iv,
84105c2828cSMichael S. Tsirkin 			   ssize_t len, int noblock)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
8441da177e4SLinus Torvalds 	struct sk_buff *skb;
84505c2828cSMichael S. Tsirkin 	ssize_t ret = 0;
8461da177e4SLinus Torvalds 
8476b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_read\n");
8481da177e4SLinus Torvalds 
84961a5ff15SAmos Kong 	if (unlikely(!noblock))
85043815482SEric Dumazet 		add_wait_queue(&tun->wq.wait, &wait);
8511da177e4SLinus Torvalds 	while (len) {
8521da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		/* Read frames from the queue */
85589f56d1eSMichael S. Tsirkin 		if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
85605c2828cSMichael S. Tsirkin 			if (noblock) {
8571da177e4SLinus Torvalds 				ret = -EAGAIN;
8581da177e4SLinus Torvalds 				break;
8591da177e4SLinus Torvalds 			}
8601da177e4SLinus Torvalds 			if (signal_pending(current)) {
8611da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
8621da177e4SLinus Torvalds 				break;
8631da177e4SLinus Torvalds 			}
864c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
865c70f1829SEric W. Biederman 				ret = -EIO;
866c70f1829SEric W. Biederman 				break;
867c70f1829SEric W. Biederman 			}
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
8701da177e4SLinus Torvalds 			schedule();
8711da177e4SLinus Torvalds 			continue;
8721da177e4SLinus Torvalds 		}
8731da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
8741da177e4SLinus Torvalds 
87543b39dcdSMichael S. Tsirkin 		ret = tun_put_user(tun, skb, iv, len);
8761da177e4SLinus Torvalds 		kfree_skb(skb);
8771da177e4SLinus Torvalds 		break;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
88161a5ff15SAmos Kong 	if (unlikely(!noblock))
88243815482SEric Dumazet 		remove_wait_queue(&tun->wq.wait, &wait);
8831da177e4SLinus Torvalds 
88405c2828cSMichael S. Tsirkin 	return ret;
88505c2828cSMichael S. Tsirkin }
88605c2828cSMichael S. Tsirkin 
88705c2828cSMichael S. Tsirkin static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
88805c2828cSMichael S. Tsirkin 			    unsigned long count, loff_t pos)
88905c2828cSMichael S. Tsirkin {
89005c2828cSMichael S. Tsirkin 	struct file *file = iocb->ki_filp;
89105c2828cSMichael S. Tsirkin 	struct tun_file *tfile = file->private_data;
89205c2828cSMichael S. Tsirkin 	struct tun_struct *tun = __tun_get(tfile);
89305c2828cSMichael S. Tsirkin 	ssize_t len, ret;
89405c2828cSMichael S. Tsirkin 
89505c2828cSMichael S. Tsirkin 	if (!tun)
89605c2828cSMichael S. Tsirkin 		return -EBADFD;
89705c2828cSMichael S. Tsirkin 	len = iov_length(iv, count);
89805c2828cSMichael S. Tsirkin 	if (len < 0) {
89905c2828cSMichael S. Tsirkin 		ret = -EINVAL;
90005c2828cSMichael S. Tsirkin 		goto out;
90105c2828cSMichael S. Tsirkin 	}
90205c2828cSMichael S. Tsirkin 
90305c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
90405c2828cSMichael S. Tsirkin 	ret = min_t(ssize_t, ret, len);
905631ab46bSEric W. Biederman out:
906631ab46bSEric W. Biederman 	tun_put(tun);
9071da177e4SLinus Torvalds 	return ret;
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	tun->owner = -1;
9158c644623SGuido Guenther 	tun->group = -1;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
9189c3fea6aSHerbert Xu 	dev->destructor = tun_free_netdev;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
921f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
922f019a7a5SEric W. Biederman  * device with netlink.
923f019a7a5SEric W. Biederman  */
924f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
925f019a7a5SEric W. Biederman {
926f019a7a5SEric W. Biederman 	return -EINVAL;
927f019a7a5SEric W. Biederman }
928f019a7a5SEric W. Biederman 
929f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
930f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
931f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
932f019a7a5SEric W. Biederman 	.setup		= tun_setup,
933f019a7a5SEric W. Biederman 	.validate	= tun_validate,
934f019a7a5SEric W. Biederman };
935f019a7a5SEric W. Biederman 
93633dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
93733dccbb0SHerbert Xu {
93833dccbb0SHerbert Xu 	struct tun_struct *tun;
93943815482SEric Dumazet 	wait_queue_head_t *wqueue;
94033dccbb0SHerbert Xu 
94133dccbb0SHerbert Xu 	if (!sock_writeable(sk))
94233dccbb0SHerbert Xu 		return;
94333dccbb0SHerbert Xu 
94433dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
94533dccbb0SHerbert Xu 		return;
94633dccbb0SHerbert Xu 
94743815482SEric Dumazet 	wqueue = sk_sleep(sk);
94843815482SEric Dumazet 	if (wqueue && waitqueue_active(wqueue))
94943815482SEric Dumazet 		wake_up_interruptible_sync_poll(wqueue, POLLOUT |
95005c2828cSMichael S. Tsirkin 						POLLWRNORM | POLLWRBAND);
951c722c625SHerbert Xu 
95280924e5fSVitaliy Gusev 	tun = tun_sk(sk)->tun;
95333dccbb0SHerbert Xu 	kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
95433dccbb0SHerbert Xu }
95533dccbb0SHerbert Xu 
95633dccbb0SHerbert Xu static void tun_sock_destruct(struct sock *sk)
95733dccbb0SHerbert Xu {
95880924e5fSVitaliy Gusev 	free_netdev(tun_sk(sk)->tun->dev);
95933dccbb0SHerbert Xu }
96033dccbb0SHerbert Xu 
96105c2828cSMichael S. Tsirkin static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
96205c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len)
96305c2828cSMichael S. Tsirkin {
96405c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
96505c2828cSMichael S. Tsirkin 	return tun_get_user(tun, m->msg_iov, total_len,
96605c2828cSMichael S. Tsirkin 			    m->msg_flags & MSG_DONTWAIT);
96705c2828cSMichael S. Tsirkin }
96805c2828cSMichael S. Tsirkin 
96905c2828cSMichael S. Tsirkin static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
97005c2828cSMichael S. Tsirkin 		       struct msghdr *m, size_t total_len,
97105c2828cSMichael S. Tsirkin 		       int flags)
97205c2828cSMichael S. Tsirkin {
97305c2828cSMichael S. Tsirkin 	struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
97405c2828cSMichael S. Tsirkin 	int ret;
97505c2828cSMichael S. Tsirkin 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
97605c2828cSMichael S. Tsirkin 		return -EINVAL;
97705c2828cSMichael S. Tsirkin 	ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
97805c2828cSMichael S. Tsirkin 			  flags & MSG_DONTWAIT);
97905c2828cSMichael S. Tsirkin 	if (ret > total_len) {
98005c2828cSMichael S. Tsirkin 		m->msg_flags |= MSG_TRUNC;
98105c2828cSMichael S. Tsirkin 		ret = flags & MSG_TRUNC ? ret : total_len;
98205c2828cSMichael S. Tsirkin 	}
98305c2828cSMichael S. Tsirkin 	return ret;
98405c2828cSMichael S. Tsirkin }
98505c2828cSMichael S. Tsirkin 
9861ab5ecb9SStanislav Kinsbursky static int tun_release(struct socket *sock)
9871ab5ecb9SStanislav Kinsbursky {
9881ab5ecb9SStanislav Kinsbursky 	if (sock->sk)
9891ab5ecb9SStanislav Kinsbursky 		sock_put(sock->sk);
9901ab5ecb9SStanislav Kinsbursky 	return 0;
9911ab5ecb9SStanislav Kinsbursky }
9921ab5ecb9SStanislav Kinsbursky 
99305c2828cSMichael S. Tsirkin /* Ops structure to mimic raw sockets with tun */
99405c2828cSMichael S. Tsirkin static const struct proto_ops tun_socket_ops = {
99505c2828cSMichael S. Tsirkin 	.sendmsg = tun_sendmsg,
99605c2828cSMichael S. Tsirkin 	.recvmsg = tun_recvmsg,
9971ab5ecb9SStanislav Kinsbursky 	.release = tun_release,
99805c2828cSMichael S. Tsirkin };
99905c2828cSMichael S. Tsirkin 
100033dccbb0SHerbert Xu static struct proto tun_proto = {
100133dccbb0SHerbert Xu 	.name		= "tun",
100233dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
100333dccbb0SHerbert Xu 	.obj_size	= sizeof(struct tun_sock),
100433dccbb0SHerbert Xu };
1005f019a7a5SEric W. Biederman 
1006980c9e8cSDavid Woodhouse static int tun_flags(struct tun_struct *tun)
1007980c9e8cSDavid Woodhouse {
1008980c9e8cSDavid Woodhouse 	int flags = 0;
1009980c9e8cSDavid Woodhouse 
1010980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_TUN_DEV)
1011980c9e8cSDavid Woodhouse 		flags |= IFF_TUN;
1012980c9e8cSDavid Woodhouse 	else
1013980c9e8cSDavid Woodhouse 		flags |= IFF_TAP;
1014980c9e8cSDavid Woodhouse 
1015980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_NO_PI)
1016980c9e8cSDavid Woodhouse 		flags |= IFF_NO_PI;
1017980c9e8cSDavid Woodhouse 
1018980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_ONE_QUEUE)
1019980c9e8cSDavid Woodhouse 		flags |= IFF_ONE_QUEUE;
1020980c9e8cSDavid Woodhouse 
1021980c9e8cSDavid Woodhouse 	if (tun->flags & TUN_VNET_HDR)
1022980c9e8cSDavid Woodhouse 		flags |= IFF_VNET_HDR;
1023980c9e8cSDavid Woodhouse 
1024980c9e8cSDavid Woodhouse 	return flags;
1025980c9e8cSDavid Woodhouse }
1026980c9e8cSDavid Woodhouse 
1027980c9e8cSDavid Woodhouse static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1028980c9e8cSDavid Woodhouse 			      char *buf)
1029980c9e8cSDavid Woodhouse {
1030980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1031980c9e8cSDavid Woodhouse 	return sprintf(buf, "0x%x\n", tun_flags(tun));
1032980c9e8cSDavid Woodhouse }
1033980c9e8cSDavid Woodhouse 
1034980c9e8cSDavid Woodhouse static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1035980c9e8cSDavid Woodhouse 			      char *buf)
1036980c9e8cSDavid Woodhouse {
1037980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1038980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->owner);
1039980c9e8cSDavid Woodhouse }
1040980c9e8cSDavid Woodhouse 
1041980c9e8cSDavid Woodhouse static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1042980c9e8cSDavid Woodhouse 			      char *buf)
1043980c9e8cSDavid Woodhouse {
1044980c9e8cSDavid Woodhouse 	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1045980c9e8cSDavid Woodhouse 	return sprintf(buf, "%d\n", tun->group);
1046980c9e8cSDavid Woodhouse }
1047980c9e8cSDavid Woodhouse 
1048980c9e8cSDavid Woodhouse static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1049980c9e8cSDavid Woodhouse static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1050980c9e8cSDavid Woodhouse static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1051980c9e8cSDavid Woodhouse 
1052d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
10531da177e4SLinus Torvalds {
105433dccbb0SHerbert Xu 	struct sock *sk;
10551da177e4SLinus Torvalds 	struct tun_struct *tun;
10561da177e4SLinus Torvalds 	struct net_device *dev;
10571da177e4SLinus Torvalds 	int err;
10581da177e4SLinus Torvalds 
105974a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
106074a3e5a7SEric W. Biederman 	if (dev) {
10612b980dbdSPaul Moore 		const struct cred *cred = current_cred();
10622b980dbdSPaul Moore 
1063f85ba780SDavid Woodhouse 		if (ifr->ifr_flags & IFF_TUN_EXCL)
1064f85ba780SDavid Woodhouse 			return -EBUSY;
106574a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
106674a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
106774a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
106874a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
106974a3e5a7SEric W. Biederman 		else
107074a3e5a7SEric W. Biederman 			return -EINVAL;
107174a3e5a7SEric W. Biederman 
10722b980dbdSPaul Moore 		if (((tun->owner != -1 && cred->euid != tun->owner) ||
10732b980dbdSPaul Moore 		     (tun->group != -1 && !in_egroup_p(tun->group))) &&
10742b980dbdSPaul Moore 		    !capable(CAP_NET_ADMIN))
10752b980dbdSPaul Moore 			return -EPERM;
1076d7e9660aSLinus Torvalds 		err = security_tun_dev_attach(tun->socket.sk);
10772b980dbdSPaul Moore 		if (err < 0)
10782b980dbdSPaul Moore 			return err;
10792b980dbdSPaul Moore 
1080a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1081a7385ba2SEric W. Biederman 		if (err < 0)
1082a7385ba2SEric W. Biederman 			return err;
108386a264abSDavid Howells 	}
10841da177e4SLinus Torvalds 	else {
10851da177e4SLinus Torvalds 		char *name;
10861da177e4SLinus Torvalds 		unsigned long flags = 0;
10871da177e4SLinus Torvalds 
1088ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
1089ca6bb5d7SDavid Woodhouse 			return -EPERM;
10902b980dbdSPaul Moore 		err = security_tun_dev_create();
10912b980dbdSPaul Moore 		if (err < 0)
10922b980dbdSPaul Moore 			return err;
1093ca6bb5d7SDavid Woodhouse 
10941da177e4SLinus Torvalds 		/* Set dev type */
10951da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
10961da177e4SLinus Torvalds 			/* TUN device */
10971da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
10981da177e4SLinus Torvalds 			name = "tun%d";
10991da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
11001da177e4SLinus Torvalds 			/* TAP device */
11011da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
11021da177e4SLinus Torvalds 			name = "tap%d";
11031da177e4SLinus Torvalds 		} else
110436989b90SKusanagi Kouichi 			return -EINVAL;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 		if (*ifr->ifr_name)
11071da177e4SLinus Torvalds 			name = ifr->ifr_name;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
11101da177e4SLinus Torvalds 				   tun_setup);
11111da177e4SLinus Torvalds 		if (!dev)
11121da177e4SLinus Torvalds 			return -ENOMEM;
11131da177e4SLinus Torvalds 
1114fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
1115f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
1116758e43b7SStephen Hemminger 
11171da177e4SLinus Torvalds 		tun = netdev_priv(dev);
11181da177e4SLinus Torvalds 		tun->dev = dev;
11191da177e4SLinus Torvalds 		tun->flags = flags;
1120f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
1121d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
1122b09e786bSMikulas Patocka 		set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
11231da177e4SLinus Torvalds 
112433dccbb0SHerbert Xu 		err = -ENOMEM;
11251ab5ecb9SStanislav Kinsbursky 		sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
112633dccbb0SHerbert Xu 		if (!sk)
112733dccbb0SHerbert Xu 			goto err_free_dev;
112833dccbb0SHerbert Xu 
11291ab5ecb9SStanislav Kinsbursky 		sk_change_net(sk, net);
113043815482SEric Dumazet 		tun->socket.wq = &tun->wq;
113143815482SEric Dumazet 		init_waitqueue_head(&tun->wq.wait);
113205c2828cSMichael S. Tsirkin 		tun->socket.ops = &tun_socket_ops;
113333dccbb0SHerbert Xu 		sock_init_data(&tun->socket, sk);
113433dccbb0SHerbert Xu 		sk->sk_write_space = tun_sock_write_space;
113533dccbb0SHerbert Xu 		sk->sk_sndbuf = INT_MAX;
113633dccbb0SHerbert Xu 
113780924e5fSVitaliy Gusev 		tun_sk(sk)->tun = tun;
113833dccbb0SHerbert Xu 
11392b980dbdSPaul Moore 		security_tun_dev_post_create(sk);
11402b980dbdSPaul Moore 
11411da177e4SLinus Torvalds 		tun_net_init(dev);
11421da177e4SLinus Torvalds 
114388255375SMichał Mirosław 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
114488255375SMichał Mirosław 			TUN_USER_FEATURES;
114588255375SMichał Mirosław 		dev->features = dev->hw_features;
114688255375SMichał Mirosław 
11471da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
11481da177e4SLinus Torvalds 		if (err < 0)
11499c3fea6aSHerbert Xu 			goto err_free_sk;
11509c3fea6aSHerbert Xu 
1151980c9e8cSDavid Woodhouse 		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1152980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1153980c9e8cSDavid Woodhouse 		    device_create_file(&tun->dev->dev, &dev_attr_group))
11546b8a66eeSJoe Perches 			pr_err("Failed to create tun sysfs files\n");
1155980c9e8cSDavid Woodhouse 
11569c3fea6aSHerbert Xu 		sk->sk_destruct = tun_sock_destruct;
1157a7385ba2SEric W. Biederman 
1158a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
1159a7385ba2SEric W. Biederman 		if (err < 0)
11609c3fea6aSHerbert Xu 			goto failed;
11611da177e4SLinus Torvalds 	}
11621da177e4SLinus Torvalds 
11636b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_set_iff\n");
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
11661da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
1167a26af1e0SNathaniel Filardo 	else
1168a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
11711da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
1172a26af1e0SNathaniel Filardo 	else
1173a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
11741da177e4SLinus Torvalds 
1175f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
1176f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
1177f43798c2SRusty Russell 	else
1178f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
1179f43798c2SRusty Russell 
1180e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
1181e35259a9SMax Krasnyansky 	 * xoff state.
1182e35259a9SMax Krasnyansky 	 */
1183e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
1184e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
1185e35259a9SMax Krasnyansky 
11861da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
11871da177e4SLinus Torvalds 	return 0;
11881da177e4SLinus Torvalds 
118933dccbb0SHerbert Xu  err_free_sk:
11901ab5ecb9SStanislav Kinsbursky 	tun_free_netdev(dev);
11911da177e4SLinus Torvalds  err_free_dev:
11921da177e4SLinus Torvalds 	free_netdev(dev);
11931da177e4SLinus Torvalds  failed:
11941da177e4SLinus Torvalds 	return err;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
1197876bfd4dSHerbert Xu static int tun_get_iff(struct net *net, struct tun_struct *tun,
1198876bfd4dSHerbert Xu 		       struct ifreq *ifr)
1199e3b99556SMark McLoughlin {
12006b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_get_iff\n");
1201e3b99556SMark McLoughlin 
1202e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
1203e3b99556SMark McLoughlin 
1204980c9e8cSDavid Woodhouse 	ifr->ifr_flags = tun_flags(tun);
1205e3b99556SMark McLoughlin 
1206e3b99556SMark McLoughlin 	return 0;
1207e3b99556SMark McLoughlin }
1208e3b99556SMark McLoughlin 
12095228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
12105228ddc9SRusty Russell  * privs required. */
121188255375SMichał Mirosław static int set_offload(struct tun_struct *tun, unsigned long arg)
12125228ddc9SRusty Russell {
1213c8f44affSMichał Mirosław 	netdev_features_t features = 0;
12145228ddc9SRusty Russell 
12155228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
121688255375SMichał Mirosław 		features |= NETIF_F_HW_CSUM;
12175228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
12185228ddc9SRusty Russell 
12195228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
12205228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
12215228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
12225228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
12235228ddc9SRusty Russell 			}
12245228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
12255228ddc9SRusty Russell 				features |= NETIF_F_TSO;
12265228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
12275228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
12285228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
12295228ddc9SRusty Russell 		}
1230e36aa25aSSridhar Samudrala 
1231e36aa25aSSridhar Samudrala 		if (arg & TUN_F_UFO) {
1232e36aa25aSSridhar Samudrala 			features |= NETIF_F_UFO;
1233e36aa25aSSridhar Samudrala 			arg &= ~TUN_F_UFO;
1234e36aa25aSSridhar Samudrala 		}
12355228ddc9SRusty Russell 	}
12365228ddc9SRusty Russell 
12375228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
12385228ddc9SRusty Russell 	 * trying to set them. */
12395228ddc9SRusty Russell 	if (arg)
12405228ddc9SRusty Russell 		return -EINVAL;
12415228ddc9SRusty Russell 
124288255375SMichał Mirosław 	tun->set_features = features;
124388255375SMichał Mirosław 	netdev_update_features(tun->dev);
12445228ddc9SRusty Russell 
12455228ddc9SRusty Russell 	return 0;
12465228ddc9SRusty Russell }
12475228ddc9SRusty Russell 
124850857e2aSArnd Bergmann static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
124950857e2aSArnd Bergmann 			    unsigned long arg, int ifreq_len)
12501da177e4SLinus Torvalds {
125136b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1252631ab46bSEric W. Biederman 	struct tun_struct *tun;
12531da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
125499405162SMichael S. Tsirkin 	struct sock_fprog fprog;
12551da177e4SLinus Torvalds 	struct ifreq ifr;
125633dccbb0SHerbert Xu 	int sndbuf;
1257d9d52b51SMichael S. Tsirkin 	int vnet_hdr_sz;
1258f271b2ccSMax Krasnyansky 	int ret;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
126150857e2aSArnd Bergmann 		if (copy_from_user(&ifr, argp, ifreq_len))
12621da177e4SLinus Torvalds 			return -EFAULT;
12631da177e4SLinus Torvalds 
1264631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1265631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1266631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1267631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1268631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1269631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1270631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1271631ab46bSEric W. Biederman 	}
1272631ab46bSEric W. Biederman 
1273876bfd4dSHerbert Xu 	rtnl_lock();
1274876bfd4dSHerbert Xu 
127536b50babSEric W. Biederman 	tun = __tun_get(tfile);
12761da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
12771da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
12781da177e4SLinus Torvalds 
1279876bfd4dSHerbert Xu 		ret = tun_set_iff(tfile->net, file, &ifr);
12801da177e4SLinus Torvalds 
1281876bfd4dSHerbert Xu 		if (ret)
1282876bfd4dSHerbert Xu 			goto unlock;
12831da177e4SLinus Torvalds 
128450857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1285876bfd4dSHerbert Xu 			ret = -EFAULT;
1286876bfd4dSHerbert Xu 		goto unlock;
12871da177e4SLinus Torvalds 	}
12881da177e4SLinus Torvalds 
1289876bfd4dSHerbert Xu 	ret = -EBADFD;
12901da177e4SLinus Torvalds 	if (!tun)
1291876bfd4dSHerbert Xu 		goto unlock;
12921da177e4SLinus Torvalds 
12936b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
12941da177e4SLinus Torvalds 
1295631ab46bSEric W. Biederman 	ret = 0;
12961da177e4SLinus Torvalds 	switch (cmd) {
1297e3b99556SMark McLoughlin 	case TUNGETIFF:
1298876bfd4dSHerbert Xu 		ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
1299e3b99556SMark McLoughlin 		if (ret)
1300631ab46bSEric W. Biederman 			break;
1301e3b99556SMark McLoughlin 
130250857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1303631ab46bSEric W. Biederman 			ret = -EFAULT;
1304e3b99556SMark McLoughlin 		break;
1305e3b99556SMark McLoughlin 
13061da177e4SLinus Torvalds 	case TUNSETNOCSUM:
13071da177e4SLinus Torvalds 		/* Disable/Enable checksum */
13081da177e4SLinus Torvalds 
130988255375SMichał Mirosław 		/* [unimplemented] */
131088255375SMichał Mirosław 		tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
13116b8a66eeSJoe Perches 			  arg ? "disabled" : "enabled");
13121da177e4SLinus Torvalds 		break;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	case TUNSETPERSIST:
13151da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
13161da177e4SLinus Torvalds 		if (arg)
13171da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
13181da177e4SLinus Torvalds 		else
13191da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
13201da177e4SLinus Torvalds 
13216b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "persist %s\n",
13226b8a66eeSJoe Perches 			  arg ? "enabled" : "disabled");
13231da177e4SLinus Torvalds 		break;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	case TUNSETOWNER:
13261da177e4SLinus Torvalds 		/* Set owner of the device */
13271da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
13281da177e4SLinus Torvalds 
13296b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
13301da177e4SLinus Torvalds 		break;
13311da177e4SLinus Torvalds 
13328c644623SGuido Guenther 	case TUNSETGROUP:
13338c644623SGuido Guenther 		/* Set group of the device */
13348c644623SGuido Guenther 		tun->group= (gid_t) arg;
13358c644623SGuido Guenther 
13366b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
13378c644623SGuido Guenther 		break;
13388c644623SGuido Guenther 
1339ff4cc3acSMike Kershaw 	case TUNSETLINK:
1340ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
1341ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
13426b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun,
13436b8a66eeSJoe Perches 				  "Linktype set failed because interface is up\n");
134448abfe05SDavid S. Miller 			ret = -EBUSY;
1345ff4cc3acSMike Kershaw 		} else {
1346ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
13476b8a66eeSJoe Perches 			tun_debug(KERN_INFO, tun, "linktype set to %d\n",
13486b8a66eeSJoe Perches 				  tun->dev->type);
134948abfe05SDavid S. Miller 			ret = 0;
1350ff4cc3acSMike Kershaw 		}
1351631ab46bSEric W. Biederman 		break;
1352ff4cc3acSMike Kershaw 
13531da177e4SLinus Torvalds #ifdef TUN_DEBUG
13541da177e4SLinus Torvalds 	case TUNSETDEBUG:
13551da177e4SLinus Torvalds 		tun->debug = arg;
13561da177e4SLinus Torvalds 		break;
13571da177e4SLinus Torvalds #endif
13585228ddc9SRusty Russell 	case TUNSETOFFLOAD:
135988255375SMichał Mirosław 		ret = set_offload(tun, arg);
1360631ab46bSEric W. Biederman 		break;
13615228ddc9SRusty Russell 
1362f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1363f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1364631ab46bSEric W. Biederman 		ret = -EINVAL;
1365f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1366631ab46bSEric W. Biederman 			break;
1367c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1368631ab46bSEric W. Biederman 		break;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1371b595076aSUwe Kleine-König 		/* Get hw address */
1372f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1373f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
137450857e2aSArnd Bergmann 		if (copy_to_user(argp, &ifr, ifreq_len))
1375631ab46bSEric W. Biederman 			ret = -EFAULT;
1376631ab46bSEric W. Biederman 		break;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1379f271b2ccSMax Krasnyansky 		/* Set hw address */
13806b8a66eeSJoe Perches 		tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
13816b8a66eeSJoe Perches 			  ifr.ifr_hwaddr.sa_data);
138240102371SKim B. Heino 
138340102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
1384631ab46bSEric W. Biederman 		break;
138533dccbb0SHerbert Xu 
138633dccbb0SHerbert Xu 	case TUNGETSNDBUF:
138789f56d1eSMichael S. Tsirkin 		sndbuf = tun->socket.sk->sk_sndbuf;
138833dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
138933dccbb0SHerbert Xu 			ret = -EFAULT;
139033dccbb0SHerbert Xu 		break;
139133dccbb0SHerbert Xu 
139233dccbb0SHerbert Xu 	case TUNSETSNDBUF:
139333dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
139433dccbb0SHerbert Xu 			ret = -EFAULT;
139533dccbb0SHerbert Xu 			break;
139633dccbb0SHerbert Xu 		}
139733dccbb0SHerbert Xu 
139889f56d1eSMichael S. Tsirkin 		tun->socket.sk->sk_sndbuf = sndbuf;
139933dccbb0SHerbert Xu 		break;
140033dccbb0SHerbert Xu 
1401d9d52b51SMichael S. Tsirkin 	case TUNGETVNETHDRSZ:
1402d9d52b51SMichael S. Tsirkin 		vnet_hdr_sz = tun->vnet_hdr_sz;
1403d9d52b51SMichael S. Tsirkin 		if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1404d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1405d9d52b51SMichael S. Tsirkin 		break;
1406d9d52b51SMichael S. Tsirkin 
1407d9d52b51SMichael S. Tsirkin 	case TUNSETVNETHDRSZ:
1408d9d52b51SMichael S. Tsirkin 		if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1409d9d52b51SMichael S. Tsirkin 			ret = -EFAULT;
1410d9d52b51SMichael S. Tsirkin 			break;
1411d9d52b51SMichael S. Tsirkin 		}
1412d9d52b51SMichael S. Tsirkin 		if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1413d9d52b51SMichael S. Tsirkin 			ret = -EINVAL;
1414d9d52b51SMichael S. Tsirkin 			break;
1415d9d52b51SMichael S. Tsirkin 		}
1416d9d52b51SMichael S. Tsirkin 
1417d9d52b51SMichael S. Tsirkin 		tun->vnet_hdr_sz = vnet_hdr_sz;
1418d9d52b51SMichael S. Tsirkin 		break;
1419d9d52b51SMichael S. Tsirkin 
142099405162SMichael S. Tsirkin 	case TUNATTACHFILTER:
142199405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
142299405162SMichael S. Tsirkin 		ret = -EINVAL;
142399405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
142499405162SMichael S. Tsirkin 			break;
142599405162SMichael S. Tsirkin 		ret = -EFAULT;
142699405162SMichael S. Tsirkin 		if (copy_from_user(&fprog, argp, sizeof(fprog)))
142799405162SMichael S. Tsirkin 			break;
142899405162SMichael S. Tsirkin 
142999405162SMichael S. Tsirkin 		ret = sk_attach_filter(&fprog, tun->socket.sk);
143099405162SMichael S. Tsirkin 		break;
143199405162SMichael S. Tsirkin 
143299405162SMichael S. Tsirkin 	case TUNDETACHFILTER:
143399405162SMichael S. Tsirkin 		/* Can be set only for TAPs */
143499405162SMichael S. Tsirkin 		ret = -EINVAL;
143599405162SMichael S. Tsirkin 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
143699405162SMichael S. Tsirkin 			break;
143799405162SMichael S. Tsirkin 		ret = sk_detach_filter(tun->socket.sk);
143899405162SMichael S. Tsirkin 		break;
143999405162SMichael S. Tsirkin 
14401da177e4SLinus Torvalds 	default:
1441631ab46bSEric W. Biederman 		ret = -EINVAL;
1442631ab46bSEric W. Biederman 		break;
1443ee289b64SJoe Perches 	}
14441da177e4SLinus Torvalds 
1445876bfd4dSHerbert Xu unlock:
1446876bfd4dSHerbert Xu 	rtnl_unlock();
1447876bfd4dSHerbert Xu 	if (tun)
1448631ab46bSEric W. Biederman 		tun_put(tun);
1449631ab46bSEric W. Biederman 	return ret;
14501da177e4SLinus Torvalds }
14511da177e4SLinus Torvalds 
145250857e2aSArnd Bergmann static long tun_chr_ioctl(struct file *file,
145350857e2aSArnd Bergmann 			  unsigned int cmd, unsigned long arg)
145450857e2aSArnd Bergmann {
145550857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
145650857e2aSArnd Bergmann }
145750857e2aSArnd Bergmann 
145850857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
145950857e2aSArnd Bergmann static long tun_chr_compat_ioctl(struct file *file,
146050857e2aSArnd Bergmann 			 unsigned int cmd, unsigned long arg)
146150857e2aSArnd Bergmann {
146250857e2aSArnd Bergmann 	switch (cmd) {
146350857e2aSArnd Bergmann 	case TUNSETIFF:
146450857e2aSArnd Bergmann 	case TUNGETIFF:
146550857e2aSArnd Bergmann 	case TUNSETTXFILTER:
146650857e2aSArnd Bergmann 	case TUNGETSNDBUF:
146750857e2aSArnd Bergmann 	case TUNSETSNDBUF:
146850857e2aSArnd Bergmann 	case SIOCGIFHWADDR:
146950857e2aSArnd Bergmann 	case SIOCSIFHWADDR:
147050857e2aSArnd Bergmann 		arg = (unsigned long)compat_ptr(arg);
147150857e2aSArnd Bergmann 		break;
147250857e2aSArnd Bergmann 	default:
147350857e2aSArnd Bergmann 		arg = (compat_ulong_t)arg;
147450857e2aSArnd Bergmann 		break;
147550857e2aSArnd Bergmann 	}
147650857e2aSArnd Bergmann 
147750857e2aSArnd Bergmann 	/*
147850857e2aSArnd Bergmann 	 * compat_ifreq is shorter than ifreq, so we must not access beyond
147950857e2aSArnd Bergmann 	 * the end of that structure. All fields that are used in this
148050857e2aSArnd Bergmann 	 * driver are compatible though, we don't need to convert the
148150857e2aSArnd Bergmann 	 * contents.
148250857e2aSArnd Bergmann 	 */
148350857e2aSArnd Bergmann 	return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
148450857e2aSArnd Bergmann }
148550857e2aSArnd Bergmann #endif /* CONFIG_COMPAT */
148650857e2aSArnd Bergmann 
14871da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
14881da177e4SLinus Torvalds {
1489631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
14901da177e4SLinus Torvalds 	int ret;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	if (!tun)
14931da177e4SLinus Torvalds 		return -EBADFD;
14941da177e4SLinus Torvalds 
14956b8a66eeSJoe Perches 	tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
14989d319522SJonathan Corbet 		goto out;
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	if (on) {
1501609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
15021da177e4SLinus Torvalds 		if (ret)
15039d319522SJonathan Corbet 			goto out;
15041da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
15051da177e4SLinus Torvalds 	} else
15061da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
15079d319522SJonathan Corbet 	ret = 0;
15089d319522SJonathan Corbet out:
1509631ab46bSEric W. Biederman 	tun_put(tun);
15109d319522SJonathan Corbet 	return ret;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
15141da177e4SLinus Torvalds {
1515631ab46bSEric W. Biederman 	struct tun_file *tfile;
1516deed49fbSThomas Gleixner 
15176b8a66eeSJoe Perches 	DBG1(KERN_INFO, "tunX: tun_chr_open\n");
1518631ab46bSEric W. Biederman 
1519631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1520631ab46bSEric W. Biederman 	if (!tfile)
1521631ab46bSEric W. Biederman 		return -ENOMEM;
1522c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1523631ab46bSEric W. Biederman 	tfile->tun = NULL;
152436b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1525631ab46bSEric W. Biederman 	file->private_data = tfile;
15261da177e4SLinus Torvalds 	return 0;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
15301da177e4SLinus Torvalds {
1531631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1532f0a4d0e5SEric W. Biederman 	struct tun_struct *tun;
15331da177e4SLinus Torvalds 
1534f0a4d0e5SEric W. Biederman 	tun = __tun_get(tfile);
1535631ab46bSEric W. Biederman 	if (tun) {
1536d23e4365SHerbert Xu 		struct net_device *dev = tun->dev;
1537d23e4365SHerbert Xu 
15386b8a66eeSJoe Perches 		tun_debug(KERN_INFO, tun, "tun_chr_close\n");
15391da177e4SLinus Torvalds 
1540631ab46bSEric W. Biederman 		__tun_detach(tun);
15411da177e4SLinus Torvalds 
15423ad2f3fbSDaniel Mack 		/* If desirable, unregister the netdevice. */
1543d23e4365SHerbert Xu 		if (!(tun->flags & TUN_PERSIST)) {
1544d23e4365SHerbert Xu 			rtnl_lock();
1545d23e4365SHerbert Xu 			if (dev->reg_state == NETREG_REGISTERED)
1546d23e4365SHerbert Xu 				unregister_netdevice(dev);
1547f0a4d0e5SEric W. Biederman 			rtnl_unlock();
1548d23e4365SHerbert Xu 		}
1549d23e4365SHerbert Xu 	}
1550631ab46bSEric W. Biederman 
15519c3fea6aSHerbert Xu 	tun = tfile->tun;
15529c3fea6aSHerbert Xu 	if (tun)
155389f56d1eSMichael S. Tsirkin 		sock_put(tun->socket.sk);
15549c3fea6aSHerbert Xu 
155536b50babSEric W. Biederman 	put_net(tfile->net);
1556631ab46bSEric W. Biederman 	kfree(tfile);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	return 0;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
1561d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
15621da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
15631da177e4SLinus Torvalds 	.llseek = no_llseek,
1564ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1565ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1566ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1567ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
15681da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
1569876bfd4dSHerbert Xu 	.unlocked_ioctl	= tun_chr_ioctl,
157050857e2aSArnd Bergmann #ifdef CONFIG_COMPAT
157150857e2aSArnd Bergmann 	.compat_ioctl = tun_chr_compat_ioctl,
157250857e2aSArnd Bergmann #endif
15731da177e4SLinus Torvalds 	.open	= tun_chr_open,
15741da177e4SLinus Torvalds 	.release = tun_chr_close,
15751da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
15761da177e4SLinus Torvalds };
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
15791da177e4SLinus Torvalds 	.minor = TUN_MINOR,
15801da177e4SLinus Torvalds 	.name = "tun",
1581e454cea2SKay Sievers 	.nodename = "net/tun",
15821da177e4SLinus Torvalds 	.fops = &tun_fops,
15831da177e4SLinus Torvalds };
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds /* ethtool interface */
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	cmd->supported		= 0;
15901da177e4SLinus Torvalds 	cmd->advertising	= 0;
159170739497SDavid Decotigny 	ethtool_cmd_speed_set(cmd, SPEED_10);
15921da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
15931da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
15941da177e4SLinus Torvalds 	cmd->phy_address	= 0;
15951da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
15961da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
15971da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
15981da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
15991da177e4SLinus Torvalds 	return 0;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
16031da177e4SLinus Torvalds {
16041da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
16051da177e4SLinus Torvalds 
160633a5ba14SRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
160733a5ba14SRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
16101da177e4SLinus Torvalds 	case TUN_TUN_DEV:
161133a5ba14SRick Jones 		strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
16121da177e4SLinus Torvalds 		break;
16131da177e4SLinus Torvalds 	case TUN_TAP_DEV:
161433a5ba14SRick Jones 		strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
16151da177e4SLinus Torvalds 		break;
16161da177e4SLinus Torvalds 	}
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
16201da177e4SLinus Torvalds {
16211da177e4SLinus Torvalds #ifdef TUN_DEBUG
16221da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
16231da177e4SLinus Torvalds 	return tun->debug;
16241da177e4SLinus Torvalds #else
16251da177e4SLinus Torvalds 	return -EOPNOTSUPP;
16261da177e4SLinus Torvalds #endif
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
16301da177e4SLinus Torvalds {
16311da177e4SLinus Torvalds #ifdef TUN_DEBUG
16321da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
16331da177e4SLinus Torvalds 	tun->debug = value;
16341da177e4SLinus Torvalds #endif
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
16377282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
16381da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
16391da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
16401da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
16411da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
1642bee31369SNolan Leake 	.get_link	= ethtool_op_get_link,
16431da177e4SLinus Torvalds };
16441da177e4SLinus Torvalds 
164579d17604SPavel Emelyanov 
16461da177e4SLinus Torvalds static int __init tun_init(void)
16471da177e4SLinus Torvalds {
16481da177e4SLinus Torvalds 	int ret = 0;
16491da177e4SLinus Torvalds 
16506b8a66eeSJoe Perches 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
16516b8a66eeSJoe Perches 	pr_info("%s\n", DRV_COPYRIGHT);
16521da177e4SLinus Torvalds 
1653f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
165479d17604SPavel Emelyanov 	if (ret) {
16556b8a66eeSJoe Perches 		pr_err("Can't register link_ops\n");
1656f019a7a5SEric W. Biederman 		goto err_linkops;
165779d17604SPavel Emelyanov 	}
165879d17604SPavel Emelyanov 
16591da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
166079d17604SPavel Emelyanov 	if (ret) {
16616b8a66eeSJoe Perches 		pr_err("Can't register misc device %d\n", TUN_MINOR);
166279d17604SPavel Emelyanov 		goto err_misc;
166379d17604SPavel Emelyanov 	}
166479d17604SPavel Emelyanov 	return  0;
166579d17604SPavel Emelyanov err_misc:
1666f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1667f019a7a5SEric W. Biederman err_linkops:
16681da177e4SLinus Torvalds 	return ret;
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds static void tun_cleanup(void)
16721da177e4SLinus Torvalds {
16731da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1674f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
167705c2828cSMichael S. Tsirkin /* Get an underlying socket object from tun file.  Returns error unless file is
167805c2828cSMichael S. Tsirkin  * attached to a device.  The returned object works like a packet socket, it
167905c2828cSMichael S. Tsirkin  * can be used for sock_sendmsg/sock_recvmsg.  The caller is responsible for
168005c2828cSMichael S. Tsirkin  * holding a reference to the file for as long as the socket is in use. */
168105c2828cSMichael S. Tsirkin struct socket *tun_get_socket(struct file *file)
168205c2828cSMichael S. Tsirkin {
168305c2828cSMichael S. Tsirkin 	struct tun_struct *tun;
168405c2828cSMichael S. Tsirkin 	if (file->f_op != &tun_fops)
168505c2828cSMichael S. Tsirkin 		return ERR_PTR(-EINVAL);
168605c2828cSMichael S. Tsirkin 	tun = tun_get(file);
168705c2828cSMichael S. Tsirkin 	if (!tun)
168805c2828cSMichael S. Tsirkin 		return ERR_PTR(-EBADFD);
168905c2828cSMichael S. Tsirkin 	tun_put(tun);
169005c2828cSMichael S. Tsirkin 	return &tun->socket;
169105c2828cSMichael S. Tsirkin }
169205c2828cSMichael S. Tsirkin EXPORT_SYMBOL_GPL(tun_get_socket);
169305c2828cSMichael S. Tsirkin 
16941da177e4SLinus Torvalds module_init(tun_init);
16951da177e4SLinus Torvalds module_exit(tun_cleanup);
16961da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
16971da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
16981da177e4SLinus Torvalds MODULE_LICENSE("GPL");
16991da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1700578454ffSKay Sievers MODULE_ALIAS("devname:net/tun");
1701