xref: /openbmc/linux/drivers/net/tun.c (revision 43b39dcdbdf823a1c0ac1f2aa2d76bd2f210adc8)
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>
251da177e4SLinus Torvalds  *    Use random_ether_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 
371da177e4SLinus Torvalds #define DRV_NAME	"tun"
381da177e4SLinus Torvalds #define DRV_VERSION	"1.6"
391da177e4SLinus Torvalds #define DRV_DESCRIPTION	"Universal TUN/TAP device driver"
401da177e4SLinus Torvalds #define DRV_COPYRIGHT	"(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/module.h>
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/major.h>
461da177e4SLinus Torvalds #include <linux/slab.h>
47fd3e05b6SArnd Bergmann #include <linux/smp_lock.h>
481da177e4SLinus Torvalds #include <linux/poll.h>
491da177e4SLinus Torvalds #include <linux/fcntl.h>
501da177e4SLinus Torvalds #include <linux/init.h>
511da177e4SLinus Torvalds #include <linux/skbuff.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531da177e4SLinus Torvalds #include <linux/etherdevice.h>
541da177e4SLinus Torvalds #include <linux/miscdevice.h>
551da177e4SLinus Torvalds #include <linux/ethtool.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <linux/if.h>
581da177e4SLinus Torvalds #include <linux/if_arp.h>
591da177e4SLinus Torvalds #include <linux/if_ether.h>
601da177e4SLinus Torvalds #include <linux/if_tun.h>
611da177e4SLinus Torvalds #include <linux/crc32.h>
62d647a591SPavel Emelyanov #include <linux/nsproxy.h>
63f43798c2SRusty Russell #include <linux/virtio_net.h>
64881d966bSEric W. Biederman #include <net/net_namespace.h>
6579d17604SPavel Emelyanov #include <net/netns/generic.h>
66f019a7a5SEric W. Biederman #include <net/rtnetlink.h>
6733dccbb0SHerbert Xu #include <net/sock.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <asm/system.h>
701da177e4SLinus Torvalds #include <asm/uaccess.h>
711da177e4SLinus Torvalds 
7214daa021SRusty Russell /* Uncomment to enable debugging */
7314daa021SRusty Russell /* #define TUN_DEBUG 1 */
7414daa021SRusty Russell 
751da177e4SLinus Torvalds #ifdef TUN_DEBUG
761da177e4SLinus Torvalds static int debug;
7714daa021SRusty Russell 
7814daa021SRusty Russell #define DBG  if(tun->debug)printk
7914daa021SRusty Russell #define DBG1 if(debug==2)printk
8014daa021SRusty Russell #else
8114daa021SRusty Russell #define DBG( a... )
8214daa021SRusty Russell #define DBG1( a... )
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds 
85f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
86f271b2ccSMax Krasnyansky struct tap_filter {
87f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
88f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
89f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
90f271b2ccSMax Krasnyansky };
91f271b2ccSMax Krasnyansky 
92631ab46bSEric W. Biederman struct tun_file {
93c70f1829SEric W. Biederman 	atomic_t count;
94631ab46bSEric W. Biederman 	struct tun_struct *tun;
9536b50babSEric W. Biederman 	struct net *net;
96631ab46bSEric W. Biederman };
97631ab46bSEric W. Biederman 
9833dccbb0SHerbert Xu struct tun_sock;
9933dccbb0SHerbert Xu 
10014daa021SRusty Russell struct tun_struct {
101631ab46bSEric W. Biederman 	struct tun_file		*tfile;
102f271b2ccSMax Krasnyansky 	unsigned int 		flags;
10314daa021SRusty Russell 	uid_t			owner;
10414daa021SRusty Russell 	gid_t			group;
10514daa021SRusty Russell 
10614daa021SRusty Russell 	struct sk_buff_head	readq;
10714daa021SRusty Russell 
10814daa021SRusty Russell 	struct net_device	*dev;
10914daa021SRusty Russell 	struct fasync_struct	*fasync;
11014daa021SRusty Russell 
111f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
11233dccbb0SHerbert Xu 	struct sock		*sk;
11333dccbb0SHerbert Xu 	struct socket		socket;
11414daa021SRusty Russell 
11514daa021SRusty Russell #ifdef TUN_DEBUG
11614daa021SRusty Russell 	int debug;
11714daa021SRusty Russell #endif
11814daa021SRusty Russell };
11914daa021SRusty Russell 
12033dccbb0SHerbert Xu struct tun_sock {
12133dccbb0SHerbert Xu 	struct sock		sk;
12233dccbb0SHerbert Xu 	struct tun_struct	*tun;
12333dccbb0SHerbert Xu };
12433dccbb0SHerbert Xu 
12533dccbb0SHerbert Xu static inline struct tun_sock *tun_sk(struct sock *sk)
12633dccbb0SHerbert Xu {
12733dccbb0SHerbert Xu 	return container_of(sk, struct tun_sock, sk);
12833dccbb0SHerbert Xu }
12933dccbb0SHerbert Xu 
130a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
131a7385ba2SEric W. Biederman {
132631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
133a7385ba2SEric W. Biederman 	const struct cred *cred = current_cred();
13438231b7aSEric W. Biederman 	int err;
135a7385ba2SEric W. Biederman 
136a7385ba2SEric W. Biederman 	ASSERT_RTNL();
137a7385ba2SEric W. Biederman 
138a7385ba2SEric W. Biederman 	/* Check permissions */
139a7385ba2SEric W. Biederman 	if (((tun->owner != -1 && cred->euid != tun->owner) ||
1401bded710SMichael Tokarev 	     (tun->group != -1 && !in_egroup_p(tun->group))) &&
141a7385ba2SEric W. Biederman 		!capable(CAP_NET_ADMIN))
142a7385ba2SEric W. Biederman 		return -EPERM;
143a7385ba2SEric W. Biederman 
14438231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
14538231b7aSEric W. Biederman 
14638231b7aSEric W. Biederman 	err = -EINVAL;
14738231b7aSEric W. Biederman 	if (tfile->tun)
14838231b7aSEric W. Biederman 		goto out;
14938231b7aSEric W. Biederman 
15038231b7aSEric W. Biederman 	err = -EBUSY;
15138231b7aSEric W. Biederman 	if (tun->tfile)
15238231b7aSEric W. Biederman 		goto out;
15338231b7aSEric W. Biederman 
15438231b7aSEric W. Biederman 	err = 0;
155631ab46bSEric W. Biederman 	tfile->tun = tun;
156631ab46bSEric W. Biederman 	tun->tfile = tfile;
157c70f1829SEric W. Biederman 	dev_hold(tun->dev);
1589c3fea6aSHerbert Xu 	sock_hold(tun->sk);
159c70f1829SEric W. Biederman 	atomic_inc(&tfile->count);
160a7385ba2SEric W. Biederman 
16138231b7aSEric W. Biederman out:
16238231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
16338231b7aSEric W. Biederman 	return err;
164a7385ba2SEric W. Biederman }
165a7385ba2SEric W. Biederman 
166631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
167631ab46bSEric W. Biederman {
168631ab46bSEric W. Biederman 	/* Detach from net device */
16938231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
170631ab46bSEric W. Biederman 	tun->tfile = NULL;
17138231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
172631ab46bSEric W. Biederman 
173631ab46bSEric W. Biederman 	/* Drop read queue */
174631ab46bSEric W. Biederman 	skb_queue_purge(&tun->readq);
175c70f1829SEric W. Biederman 
176c70f1829SEric W. Biederman 	/* Drop the extra count on the net device */
177c70f1829SEric W. Biederman 	dev_put(tun->dev);
178c70f1829SEric W. Biederman }
179c70f1829SEric W. Biederman 
180c70f1829SEric W. Biederman static void tun_detach(struct tun_struct *tun)
181c70f1829SEric W. Biederman {
182c70f1829SEric W. Biederman 	rtnl_lock();
183c70f1829SEric W. Biederman 	__tun_detach(tun);
184c70f1829SEric W. Biederman 	rtnl_unlock();
185631ab46bSEric W. Biederman }
186631ab46bSEric W. Biederman 
187631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
188631ab46bSEric W. Biederman {
189c70f1829SEric W. Biederman 	struct tun_struct *tun = NULL;
190c70f1829SEric W. Biederman 
191c70f1829SEric W. Biederman 	if (atomic_inc_not_zero(&tfile->count))
192c70f1829SEric W. Biederman 		tun = tfile->tun;
193c70f1829SEric W. Biederman 
194c70f1829SEric W. Biederman 	return tun;
195631ab46bSEric W. Biederman }
196631ab46bSEric W. Biederman 
197631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
198631ab46bSEric W. Biederman {
199631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
200631ab46bSEric W. Biederman }
201631ab46bSEric W. Biederman 
202631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
203631ab46bSEric W. Biederman {
204c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
205c70f1829SEric W. Biederman 
206c70f1829SEric W. Biederman 	if (atomic_dec_and_test(&tfile->count))
207c70f1829SEric W. Biederman 		tun_detach(tfile->tun);
208631ab46bSEric W. Biederman }
209631ab46bSEric W. Biederman 
210f271b2ccSMax Krasnyansky /* TAP filterting */
211f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
212f271b2ccSMax Krasnyansky {
213f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
214f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
215f271b2ccSMax Krasnyansky }
216f271b2ccSMax Krasnyansky 
217f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
218f271b2ccSMax Krasnyansky {
219f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
220f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
221f271b2ccSMax Krasnyansky }
222f271b2ccSMax Krasnyansky 
223f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
224f271b2ccSMax Krasnyansky {
225f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
226f271b2ccSMax Krasnyansky 	struct tun_filter uf;
227f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
228f271b2ccSMax Krasnyansky 
229f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
230f271b2ccSMax Krasnyansky 		return -EFAULT;
231f271b2ccSMax Krasnyansky 
232f271b2ccSMax Krasnyansky 	if (!uf.count) {
233f271b2ccSMax Krasnyansky 		/* Disabled */
234f271b2ccSMax Krasnyansky 		filter->count = 0;
235f271b2ccSMax Krasnyansky 		return 0;
236f271b2ccSMax Krasnyansky 	}
237f271b2ccSMax Krasnyansky 
238f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
239f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
240f271b2ccSMax Krasnyansky 	if (!addr)
241f271b2ccSMax Krasnyansky 		return -ENOMEM;
242f271b2ccSMax Krasnyansky 
243f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
244f271b2ccSMax Krasnyansky 		err = -EFAULT;
245f271b2ccSMax Krasnyansky 		goto done;
246f271b2ccSMax Krasnyansky 	}
247f271b2ccSMax Krasnyansky 
248f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
249f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
250f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
251f271b2ccSMax Krasnyansky 	filter->count = 0;
252f271b2ccSMax Krasnyansky 	wmb();
253f271b2ccSMax Krasnyansky 
254f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
255f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
256f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
257f271b2ccSMax Krasnyansky 
258f271b2ccSMax Krasnyansky 	nexact = n;
259f271b2ccSMax Krasnyansky 
260cfbf84fcSAlex Williamson 	/* Remaining multicast addresses are hashed,
261cfbf84fcSAlex Williamson 	 * unicast will leave the filter disabled. */
262f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
263cfbf84fcSAlex Williamson 	for (; n < uf.count; n++) {
264cfbf84fcSAlex Williamson 		if (!is_multicast_ether_addr(addr[n].u)) {
265cfbf84fcSAlex Williamson 			err = 0; /* no filter */
266cfbf84fcSAlex Williamson 			goto done;
267cfbf84fcSAlex Williamson 		}
268f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
269cfbf84fcSAlex Williamson 	}
270f271b2ccSMax Krasnyansky 
271f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
272f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
273f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
274f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
275f271b2ccSMax Krasnyansky 
276f271b2ccSMax Krasnyansky 	/* Now enable the filter */
277f271b2ccSMax Krasnyansky 	wmb();
278f271b2ccSMax Krasnyansky 	filter->count = nexact;
279f271b2ccSMax Krasnyansky 
280f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
281f271b2ccSMax Krasnyansky 	err = nexact;
282f271b2ccSMax Krasnyansky 
283f271b2ccSMax Krasnyansky done:
284f271b2ccSMax Krasnyansky 	kfree(addr);
285f271b2ccSMax Krasnyansky 	return err;
286f271b2ccSMax Krasnyansky }
287f271b2ccSMax Krasnyansky 
288f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
289f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
290f271b2ccSMax Krasnyansky {
291f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
292f271b2ccSMax Krasnyansky 	 * at this point. */
293f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
294f271b2ccSMax Krasnyansky 	int i;
295f271b2ccSMax Krasnyansky 
296f271b2ccSMax Krasnyansky 	/* Exact match */
297f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
298f271b2ccSMax Krasnyansky 		if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
299f271b2ccSMax Krasnyansky 			return 1;
300f271b2ccSMax Krasnyansky 
301f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
302f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
303f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
304f271b2ccSMax Krasnyansky 
305f271b2ccSMax Krasnyansky 	return 0;
306f271b2ccSMax Krasnyansky }
307f271b2ccSMax Krasnyansky 
308f271b2ccSMax Krasnyansky /*
309f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
310f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
311f271b2ccSMax Krasnyansky  */
312f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
313f271b2ccSMax Krasnyansky {
314f271b2ccSMax Krasnyansky 	if (!filter->count)
315f271b2ccSMax Krasnyansky 		return 1;
316f271b2ccSMax Krasnyansky 
317f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
318f271b2ccSMax Krasnyansky }
319f271b2ccSMax Krasnyansky 
3201da177e4SLinus Torvalds /* Network device part of the driver */
3211da177e4SLinus Torvalds 
3227282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
3231da177e4SLinus Torvalds 
324c70f1829SEric W. Biederman /* Net device detach from fd. */
325c70f1829SEric W. Biederman static void tun_net_uninit(struct net_device *dev)
326c70f1829SEric W. Biederman {
327c70f1829SEric W. Biederman 	struct tun_struct *tun = netdev_priv(dev);
328c70f1829SEric W. Biederman 	struct tun_file *tfile = tun->tfile;
329c70f1829SEric W. Biederman 
330c70f1829SEric W. Biederman 	/* Inform the methods they need to stop using the dev.
331c70f1829SEric W. Biederman 	 */
332c70f1829SEric W. Biederman 	if (tfile) {
333c40af84aSHerbert Xu 		wake_up_all(&tun->socket.wait);
334c70f1829SEric W. Biederman 		if (atomic_dec_and_test(&tfile->count))
335c70f1829SEric W. Biederman 			__tun_detach(tun);
336c70f1829SEric W. Biederman 	}
337c70f1829SEric W. Biederman }
338c70f1829SEric W. Biederman 
3399c3fea6aSHerbert Xu static void tun_free_netdev(struct net_device *dev)
3409c3fea6aSHerbert Xu {
3419c3fea6aSHerbert Xu 	struct tun_struct *tun = netdev_priv(dev);
3429c3fea6aSHerbert Xu 
3439c3fea6aSHerbert Xu 	sock_put(tun->sk);
3449c3fea6aSHerbert Xu }
3459c3fea6aSHerbert Xu 
3461da177e4SLinus Torvalds /* Net device open. */
3471da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	netif_start_queue(dev);
3501da177e4SLinus Torvalds 	return 0;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /* Net device close. */
3541da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	netif_stop_queue(dev);
3571da177e4SLinus Torvalds 	return 0;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /* Net device start xmit */
3611da177e4SLinus Torvalds static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
368631ab46bSEric W. Biederman 	if (!tun->tfile)
3691da177e4SLinus Torvalds 		goto drop;
3701da177e4SLinus Torvalds 
371f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
372f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
373f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
374f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
375f271b2ccSMax Krasnyansky 		goto drop;
376f271b2ccSMax Krasnyansky 
3771da177e4SLinus Torvalds 	if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
3781da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
3791da177e4SLinus Torvalds 			/* Normal queueing mode. */
3801da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
3811da177e4SLinus Torvalds 			netif_stop_queue(dev);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
3841da177e4SLinus Torvalds 			 * error is more appropriate. */
38509f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
3861da177e4SLinus Torvalds 		} else {
3871da177e4SLinus Torvalds 			/* Single queue mode.
3881da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
3891da177e4SLinus Torvalds 			goto drop;
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
393f271b2ccSMax Krasnyansky 	/* Enqueue packet */
3941da177e4SLinus Torvalds 	skb_queue_tail(&tun->readq, skb);
3951da177e4SLinus Torvalds 	dev->trans_start = jiffies;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	/* Notify and wake up reader process */
3981da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
3991da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
400c40af84aSHerbert Xu 	wake_up_interruptible(&tun->socket.wait);
4011da177e4SLinus Torvalds 	return 0;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds drop:
40409f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
4051da177e4SLinus Torvalds 	kfree_skb(skb);
4061da177e4SLinus Torvalds 	return 0;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
409f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
4101da177e4SLinus Torvalds {
411f271b2ccSMax Krasnyansky 	/*
412f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
413f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
414f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
415f271b2ccSMax Krasnyansky 	 */
416f271b2ccSMax Krasnyansky 	return;
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4194885a504SEd Swierk #define MIN_MTU 68
4204885a504SEd Swierk #define MAX_MTU 65535
4214885a504SEd Swierk 
4224885a504SEd Swierk static int
4234885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
4244885a504SEd Swierk {
4254885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
4264885a504SEd Swierk 		return -EINVAL;
4274885a504SEd Swierk 	dev->mtu = new_mtu;
4284885a504SEd Swierk 	return 0;
4294885a504SEd Swierk }
4304885a504SEd Swierk 
431758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
432c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
433758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
434758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
43500829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
436758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
437758e43b7SStephen Hemminger };
438758e43b7SStephen Hemminger 
439758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
440c70f1829SEric W. Biederman 	.ndo_uninit		= tun_net_uninit,
441758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
442758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
44300829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
444758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
445758e43b7SStephen Hemminger 	.ndo_set_multicast_list	= tun_net_mclist,
446758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
447758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
448758e43b7SStephen Hemminger };
449758e43b7SStephen Hemminger 
4501da177e4SLinus Torvalds /* Initialize net device. */
4511da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
4561da177e4SLinus Torvalds 	case TUN_TUN_DEV:
457758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
458758e43b7SStephen Hemminger 
4591da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
4601da177e4SLinus Torvalds 		dev->hard_header_len = 0;
4611da177e4SLinus Torvalds 		dev->addr_len = 0;
4621da177e4SLinus Torvalds 		dev->mtu = 1500;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 		/* Zero header length */
4651da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
4661da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
4671da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
4681da177e4SLinus Torvalds 		break;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	case TUN_TAP_DEV:
4717a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
4721da177e4SLinus Torvalds 		/* Ethernet TAP Device */
4731da177e4SLinus Torvalds 		ether_setup(dev);
47436226a8dSBrian Braunstein 
475f271b2ccSMax Krasnyansky 		random_ether_addr(dev->dev_addr);
47636226a8dSBrian Braunstein 
4771da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
4781da177e4SLinus Torvalds 		break;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds /* Character device part */
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds /* Poll */
4851da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
4861da177e4SLinus Torvalds {
487b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
488b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
48933dccbb0SHerbert Xu 	struct sock *sk = tun->sk;
49033dccbb0SHerbert Xu 	unsigned int mask = 0;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	if (!tun)
493eac9e902SEric W. Biederman 		return POLLERR;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
4961da177e4SLinus Torvalds 
497c40af84aSHerbert Xu 	poll_wait(file, &tun->socket.wait, wait);
4981da177e4SLinus Torvalds 
499b03efcfbSDavid S. Miller 	if (!skb_queue_empty(&tun->readq))
5001da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
5011da177e4SLinus Torvalds 
50233dccbb0SHerbert Xu 	if (sock_writeable(sk) ||
50333dccbb0SHerbert Xu 	    (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
50433dccbb0SHerbert Xu 	     sock_writeable(sk)))
50533dccbb0SHerbert Xu 		mask |= POLLOUT | POLLWRNORM;
50633dccbb0SHerbert Xu 
507c70f1829SEric W. Biederman 	if (tun->dev->reg_state != NETREG_REGISTERED)
508c70f1829SEric W. Biederman 		mask = POLLERR;
509c70f1829SEric W. Biederman 
510631ab46bSEric W. Biederman 	tun_put(tun);
5111da177e4SLinus Torvalds 	return mask;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
514f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
515f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
51633dccbb0SHerbert Xu static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
51733dccbb0SHerbert Xu 					    size_t prepad, size_t len,
51833dccbb0SHerbert Xu 					    size_t linear, int noblock)
519f42157cbSRusty Russell {
52033dccbb0SHerbert Xu 	struct sock *sk = tun->sk;
521f42157cbSRusty Russell 	struct sk_buff *skb;
52233dccbb0SHerbert Xu 	int err;
523f42157cbSRusty Russell 
524f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
5250eca93bcSHerbert Xu 	if (prepad + len < PAGE_SIZE || !linear)
52633dccbb0SHerbert Xu 		linear = len;
527f42157cbSRusty Russell 
52833dccbb0SHerbert Xu 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
52933dccbb0SHerbert Xu 				   &err);
530f42157cbSRusty Russell 	if (!skb)
53133dccbb0SHerbert Xu 		return ERR_PTR(err);
532f42157cbSRusty Russell 
533f42157cbSRusty Russell 	skb_reserve(skb, prepad);
534f42157cbSRusty Russell 	skb_put(skb, linear);
53533dccbb0SHerbert Xu 	skb->data_len = len - linear;
53633dccbb0SHerbert Xu 	skb->len += len - linear;
537f42157cbSRusty Russell 
538f42157cbSRusty Russell 	return skb;
539f42157cbSRusty Russell }
540f42157cbSRusty Russell 
5411da177e4SLinus Torvalds /* Get packet from user space buffer */
54233dccbb0SHerbert Xu static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
54333dccbb0SHerbert Xu 				       struct iovec *iv, size_t count,
54433dccbb0SHerbert Xu 				       int noblock)
5451da177e4SLinus Torvalds {
54609640e63SHarvey Harrison 	struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
5471da177e4SLinus Torvalds 	struct sk_buff *skb;
5481da177e4SLinus Torvalds 	size_t len = count, align = 0;
549f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
5521da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
5531da177e4SLinus Torvalds 			return -EINVAL;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 		if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
5561da177e4SLinus Torvalds 			return -EFAULT;
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds 
559f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
560f43798c2SRusty Russell 		if ((len -= sizeof(gso)) > count)
561f43798c2SRusty Russell 			return -EINVAL;
562f43798c2SRusty Russell 
563f43798c2SRusty Russell 		if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
564f43798c2SRusty Russell 			return -EFAULT;
565f43798c2SRusty Russell 
566f43798c2SRusty Russell 		if (gso.hdr_len > len)
567f43798c2SRusty Russell 			return -EINVAL;
568f43798c2SRusty Russell 	}
569f43798c2SRusty Russell 
570e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
5711da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
5720eca93bcSHerbert Xu 		if (unlikely(len < ETH_HLEN ||
5730eca93bcSHerbert Xu 			     (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
574e01bf1c8SRusty Russell 			return -EINVAL;
575e01bf1c8SRusty Russell 	}
5761da177e4SLinus Torvalds 
57733dccbb0SHerbert Xu 	skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
57833dccbb0SHerbert Xu 	if (IS_ERR(skb)) {
57933dccbb0SHerbert Xu 		if (PTR_ERR(skb) != -EAGAIN)
58009f75cd7SJeff Garzik 			tun->dev->stats.rx_dropped++;
58133dccbb0SHerbert Xu 		return PTR_ERR(skb);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 
584f42157cbSRusty Russell 	if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
58509f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
5868f22757eSDave Jones 		kfree_skb(skb);
5871da177e4SLinus Torvalds 		return -EFAULT;
5888f22757eSDave Jones 	}
5891da177e4SLinus Torvalds 
590f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
591f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
592f43798c2SRusty Russell 					  gso.csum_offset)) {
593f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
594f43798c2SRusty Russell 			kfree_skb(skb);
595f43798c2SRusty Russell 			return -EINVAL;
596f43798c2SRusty Russell 		}
597f43798c2SRusty Russell 	} else if (tun->flags & TUN_NOCHECKSUM)
598f43798c2SRusty Russell 		skb->ip_summed = CHECKSUM_UNNECESSARY;
599f43798c2SRusty Russell 
6001da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
6011da177e4SLinus Torvalds 	case TUN_TUN_DEV:
602f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
603f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
604f09f7ee2SAng Way Chuang 			case 0x40:
605f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
606f09f7ee2SAng Way Chuang 				break;
607f09f7ee2SAng Way Chuang 			case 0x60:
608f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
609f09f7ee2SAng Way Chuang 				break;
610f09f7ee2SAng Way Chuang 			default:
611f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
612f09f7ee2SAng Way Chuang 				kfree_skb(skb);
613f09f7ee2SAng Way Chuang 				return -EINVAL;
614f09f7ee2SAng Way Chuang 			}
615f09f7ee2SAng Way Chuang 		}
616f09f7ee2SAng Way Chuang 
617459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
6181da177e4SLinus Torvalds 		skb->protocol = pi.proto;
6194c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
6201da177e4SLinus Torvalds 		break;
6211da177e4SLinus Torvalds 	case TUN_TAP_DEV:
6221da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
6231da177e4SLinus Torvalds 		break;
6241da177e4SLinus Torvalds 	};
6251da177e4SLinus Torvalds 
626f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
627f43798c2SRusty Russell 		pr_debug("GSO!\n");
628f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
629f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
630f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
631f43798c2SRusty Russell 			break;
632f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
633f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
634f43798c2SRusty Russell 			break;
635f43798c2SRusty Russell 		default:
636f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
637f43798c2SRusty Russell 			kfree_skb(skb);
638f43798c2SRusty Russell 			return -EINVAL;
639f43798c2SRusty Russell 		}
640f43798c2SRusty Russell 
641f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
642f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
643f43798c2SRusty Russell 
644f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
645f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
646f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
647f43798c2SRusty Russell 			kfree_skb(skb);
648f43798c2SRusty Russell 			return -EINVAL;
649f43798c2SRusty Russell 		}
650f43798c2SRusty Russell 
651f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
652f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
653f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
654f43798c2SRusty Russell 	}
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	netif_rx_ni(skb);
6571da177e4SLinus Torvalds 
65809f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
65909f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	return count;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
664ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
665ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
6661da177e4SLinus Torvalds {
66733dccbb0SHerbert Xu 	struct file *file = iocb->ki_filp;
668ab46d779SHerbert Xu 	struct tun_struct *tun = tun_get(file);
669631ab46bSEric W. Biederman 	ssize_t result;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	if (!tun)
6721da177e4SLinus Torvalds 		return -EBADFD;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
6751da177e4SLinus Torvalds 
67633dccbb0SHerbert Xu 	result = tun_get_user(tun, (struct iovec *)iv, iov_length(iv, count),
67733dccbb0SHerbert Xu 			      file->f_flags & O_NONBLOCK);
678631ab46bSEric W. Biederman 
679631ab46bSEric W. Biederman 	tun_put(tun);
680631ab46bSEric W. Biederman 	return result;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds /* Put packet to the user space buffer */
6841da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
6851da177e4SLinus Torvalds 				       struct sk_buff *skb,
686*43b39dcdSMichael S. Tsirkin 				       const struct iovec *iv, int len)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
6891da177e4SLinus Torvalds 	ssize_t total = 0;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
6921da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
6931da177e4SLinus Torvalds 			return -EINVAL;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 		if (len < skb->len) {
6961da177e4SLinus Torvalds 			/* Packet will be striped */
6971da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
6981da177e4SLinus Torvalds 		}
6991da177e4SLinus Torvalds 
700*43b39dcdSMichael S. Tsirkin 		if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
7011da177e4SLinus Torvalds 			return -EFAULT;
7021da177e4SLinus Torvalds 		total += sizeof(pi);
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
705f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
706f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
707f43798c2SRusty Russell 		if ((len -= sizeof(gso)) < 0)
708f43798c2SRusty Russell 			return -EINVAL;
709f43798c2SRusty Russell 
710f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
711f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
712f43798c2SRusty Russell 
713f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
714f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
715f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
716f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
717f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
718f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
719f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
720f43798c2SRusty Russell 			else
721f43798c2SRusty Russell 				BUG();
722f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
723f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
724f43798c2SRusty Russell 		} else
725f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
726f43798c2SRusty Russell 
727f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
728f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
729f43798c2SRusty Russell 			gso.csum_start = skb->csum_start - skb_headroom(skb);
730f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
731f43798c2SRusty Russell 		} /* else everything is zero */
732f43798c2SRusty Russell 
733*43b39dcdSMichael S. Tsirkin 		if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
734*43b39dcdSMichael S. Tsirkin 					       sizeof(gso))))
735f43798c2SRusty Russell 			return -EFAULT;
736f43798c2SRusty Russell 		total += sizeof(gso);
737f43798c2SRusty Russell 	}
738f43798c2SRusty Russell 
7391da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
7401da177e4SLinus Torvalds 
741*43b39dcdSMichael S. Tsirkin 	skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
7421da177e4SLinus Torvalds 	total += len;
7431da177e4SLinus Torvalds 
74409f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
74509f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	return total;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
750ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
751ee0b3e67SBadari Pulavarty 			    unsigned long count, loff_t pos)
7521da177e4SLinus Torvalds {
753ee0b3e67SBadari Pulavarty 	struct file *file = iocb->ki_filp;
754b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
755b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
7561da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
7571da177e4SLinus Torvalds 	struct sk_buff *skb;
7581da177e4SLinus Torvalds 	ssize_t len, ret = 0;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (!tun)
7611da177e4SLinus Torvalds 		return -EBADFD;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
7641da177e4SLinus Torvalds 
76552427c9dSAkinobu Mita 	len = iov_length(iv, count);
766631ab46bSEric W. Biederman 	if (len < 0) {
767631ab46bSEric W. Biederman 		ret = -EINVAL;
768631ab46bSEric W. Biederman 		goto out;
769631ab46bSEric W. Biederman 	}
7701da177e4SLinus Torvalds 
771c40af84aSHerbert Xu 	add_wait_queue(&tun->socket.wait, &wait);
7721da177e4SLinus Torvalds 	while (len) {
7731da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 		/* Read frames from the queue */
7761da177e4SLinus Torvalds 		if (!(skb=skb_dequeue(&tun->readq))) {
7771da177e4SLinus Torvalds 			if (file->f_flags & O_NONBLOCK) {
7781da177e4SLinus Torvalds 				ret = -EAGAIN;
7791da177e4SLinus Torvalds 				break;
7801da177e4SLinus Torvalds 			}
7811da177e4SLinus Torvalds 			if (signal_pending(current)) {
7821da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
7831da177e4SLinus Torvalds 				break;
7841da177e4SLinus Torvalds 			}
785c70f1829SEric W. Biederman 			if (tun->dev->reg_state != NETREG_REGISTERED) {
786c70f1829SEric W. Biederman 				ret = -EIO;
787c70f1829SEric W. Biederman 				break;
788c70f1829SEric W. Biederman 			}
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
7911da177e4SLinus Torvalds 			schedule();
7921da177e4SLinus Torvalds 			continue;
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
7951da177e4SLinus Torvalds 
796*43b39dcdSMichael S. Tsirkin 		ret = tun_put_user(tun, skb, iv, len);
7971da177e4SLinus Torvalds 		kfree_skb(skb);
7981da177e4SLinus Torvalds 		break;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
802c40af84aSHerbert Xu 	remove_wait_queue(&tun->socket.wait, &wait);
8031da177e4SLinus Torvalds 
804631ab46bSEric W. Biederman out:
805631ab46bSEric W. Biederman 	tun_put(tun);
8061da177e4SLinus Torvalds 	return ret;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	skb_queue_head_init(&tun->readq);
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	tun->owner = -1;
8168c644623SGuido Guenther 	tun->group = -1;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
8199c3fea6aSHerbert Xu 	dev->destructor = tun_free_netdev;
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
822f019a7a5SEric W. Biederman /* Trivial set of netlink ops to allow deleting tun or tap
823f019a7a5SEric W. Biederman  * device with netlink.
824f019a7a5SEric W. Biederman  */
825f019a7a5SEric W. Biederman static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
826f019a7a5SEric W. Biederman {
827f019a7a5SEric W. Biederman 	return -EINVAL;
828f019a7a5SEric W. Biederman }
829f019a7a5SEric W. Biederman 
830f019a7a5SEric W. Biederman static struct rtnl_link_ops tun_link_ops __read_mostly = {
831f019a7a5SEric W. Biederman 	.kind		= DRV_NAME,
832f019a7a5SEric W. Biederman 	.priv_size	= sizeof(struct tun_struct),
833f019a7a5SEric W. Biederman 	.setup		= tun_setup,
834f019a7a5SEric W. Biederman 	.validate	= tun_validate,
835f019a7a5SEric W. Biederman };
836f019a7a5SEric W. Biederman 
83733dccbb0SHerbert Xu static void tun_sock_write_space(struct sock *sk)
83833dccbb0SHerbert Xu {
83933dccbb0SHerbert Xu 	struct tun_struct *tun;
84033dccbb0SHerbert Xu 
84133dccbb0SHerbert Xu 	if (!sock_writeable(sk))
84233dccbb0SHerbert Xu 		return;
84333dccbb0SHerbert Xu 
84433dccbb0SHerbert Xu 	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
84533dccbb0SHerbert Xu 		wake_up_interruptible_sync(sk->sk_sleep);
84633dccbb0SHerbert Xu 
84733dccbb0SHerbert Xu 	if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
84833dccbb0SHerbert Xu 		return;
84933dccbb0SHerbert Xu 
85033dccbb0SHerbert Xu 	tun = container_of(sk, struct tun_sock, sk)->tun;
85133dccbb0SHerbert Xu 	kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
85233dccbb0SHerbert Xu }
85333dccbb0SHerbert Xu 
85433dccbb0SHerbert Xu static void tun_sock_destruct(struct sock *sk)
85533dccbb0SHerbert Xu {
8569c3fea6aSHerbert Xu 	free_netdev(container_of(sk, struct tun_sock, sk)->tun->dev);
85733dccbb0SHerbert Xu }
85833dccbb0SHerbert Xu 
85933dccbb0SHerbert Xu static struct proto tun_proto = {
86033dccbb0SHerbert Xu 	.name		= "tun",
86133dccbb0SHerbert Xu 	.owner		= THIS_MODULE,
86233dccbb0SHerbert Xu 	.obj_size	= sizeof(struct tun_sock),
86333dccbb0SHerbert Xu };
864f019a7a5SEric W. Biederman 
865d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
8661da177e4SLinus Torvalds {
86733dccbb0SHerbert Xu 	struct sock *sk;
8681da177e4SLinus Torvalds 	struct tun_struct *tun;
8691da177e4SLinus Torvalds 	struct net_device *dev;
8701da177e4SLinus Torvalds 	int err;
8711da177e4SLinus Torvalds 
87274a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
87374a3e5a7SEric W. Biederman 	if (dev) {
87474a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
87574a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
87674a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
87774a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
87874a3e5a7SEric W. Biederman 		else
87974a3e5a7SEric W. Biederman 			return -EINVAL;
88074a3e5a7SEric W. Biederman 
881a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
882a7385ba2SEric W. Biederman 		if (err < 0)
883a7385ba2SEric W. Biederman 			return err;
88486a264abSDavid Howells 	}
8851da177e4SLinus Torvalds 	else {
8861da177e4SLinus Torvalds 		char *name;
8871da177e4SLinus Torvalds 		unsigned long flags = 0;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 		err = -EINVAL;
8901da177e4SLinus Torvalds 
891ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
892ca6bb5d7SDavid Woodhouse 			return -EPERM;
893ca6bb5d7SDavid Woodhouse 
8941da177e4SLinus Torvalds 		/* Set dev type */
8951da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
8961da177e4SLinus Torvalds 			/* TUN device */
8971da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
8981da177e4SLinus Torvalds 			name = "tun%d";
8991da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
9001da177e4SLinus Torvalds 			/* TAP device */
9011da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
9021da177e4SLinus Torvalds 			name = "tap%d";
9031da177e4SLinus Torvalds 		} else
9041da177e4SLinus Torvalds 			goto failed;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 		if (*ifr->ifr_name)
9071da177e4SLinus Torvalds 			name = ifr->ifr_name;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
9101da177e4SLinus Torvalds 				   tun_setup);
9111da177e4SLinus Torvalds 		if (!dev)
9121da177e4SLinus Torvalds 			return -ENOMEM;
9131da177e4SLinus Torvalds 
914fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
915f019a7a5SEric W. Biederman 		dev->rtnl_link_ops = &tun_link_ops;
916758e43b7SStephen Hemminger 
9171da177e4SLinus Torvalds 		tun = netdev_priv(dev);
9181da177e4SLinus Torvalds 		tun->dev = dev;
9191da177e4SLinus Torvalds 		tun->flags = flags;
920f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
9211da177e4SLinus Torvalds 
92233dccbb0SHerbert Xu 		err = -ENOMEM;
92333dccbb0SHerbert Xu 		sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
92433dccbb0SHerbert Xu 		if (!sk)
92533dccbb0SHerbert Xu 			goto err_free_dev;
92633dccbb0SHerbert Xu 
927c40af84aSHerbert Xu 		init_waitqueue_head(&tun->socket.wait);
92833dccbb0SHerbert Xu 		sock_init_data(&tun->socket, sk);
92933dccbb0SHerbert Xu 		sk->sk_write_space = tun_sock_write_space;
93033dccbb0SHerbert Xu 		sk->sk_sndbuf = INT_MAX;
93133dccbb0SHerbert Xu 
93233dccbb0SHerbert Xu 		tun->sk = sk;
93333dccbb0SHerbert Xu 		container_of(sk, struct tun_sock, sk)->tun = tun;
93433dccbb0SHerbert Xu 
9351da177e4SLinus Torvalds 		tun_net_init(dev);
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
9381da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
9391da177e4SLinus Torvalds 			if (err < 0)
94033dccbb0SHerbert Xu 				goto err_free_sk;
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 
94333dccbb0SHerbert Xu 		err = -EINVAL;
9441da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
9451da177e4SLinus Torvalds 		if (err < 0)
9469c3fea6aSHerbert Xu 			goto err_free_sk;
9479c3fea6aSHerbert Xu 
9489c3fea6aSHerbert Xu 		sk->sk_destruct = tun_sock_destruct;
949a7385ba2SEric W. Biederman 
950a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
951a7385ba2SEric W. Biederman 		if (err < 0)
9529c3fea6aSHerbert Xu 			goto failed;
9531da177e4SLinus Torvalds 	}
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
9581da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
959a26af1e0SNathaniel Filardo 	else
960a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
9631da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
964a26af1e0SNathaniel Filardo 	else
965a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
9661da177e4SLinus Torvalds 
967f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
968f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
969f43798c2SRusty Russell 	else
970f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
971f43798c2SRusty Russell 
972e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
973e35259a9SMax Krasnyansky 	 * xoff state.
974e35259a9SMax Krasnyansky 	 */
975e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
976e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
977e35259a9SMax Krasnyansky 
9781da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
9791da177e4SLinus Torvalds 	return 0;
9801da177e4SLinus Torvalds 
98133dccbb0SHerbert Xu  err_free_sk:
98233dccbb0SHerbert Xu 	sock_put(sk);
9831da177e4SLinus Torvalds  err_free_dev:
9841da177e4SLinus Torvalds 	free_netdev(dev);
9851da177e4SLinus Torvalds  failed:
9861da177e4SLinus Torvalds 	return err;
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
989e3b99556SMark McLoughlin static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
990e3b99556SMark McLoughlin {
991631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
992e3b99556SMark McLoughlin 
993e3b99556SMark McLoughlin 	if (!tun)
994e3b99556SMark McLoughlin 		return -EBADFD;
995e3b99556SMark McLoughlin 
996e3b99556SMark McLoughlin 	DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
997e3b99556SMark McLoughlin 
998e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
999e3b99556SMark McLoughlin 
1000e3b99556SMark McLoughlin 	ifr->ifr_flags = 0;
1001e3b99556SMark McLoughlin 
1002e3b99556SMark McLoughlin 	if (ifr->ifr_flags & TUN_TUN_DEV)
1003e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_TUN;
1004e3b99556SMark McLoughlin 	else
1005e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_TAP;
1006e3b99556SMark McLoughlin 
1007e3b99556SMark McLoughlin 	if (tun->flags & TUN_NO_PI)
1008e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_NO_PI;
1009e3b99556SMark McLoughlin 
1010e3b99556SMark McLoughlin 	if (tun->flags & TUN_ONE_QUEUE)
1011e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_ONE_QUEUE;
1012e3b99556SMark McLoughlin 
1013e3b99556SMark McLoughlin 	if (tun->flags & TUN_VNET_HDR)
1014e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_VNET_HDR;
1015e3b99556SMark McLoughlin 
1016631ab46bSEric W. Biederman 	tun_put(tun);
1017e3b99556SMark McLoughlin 	return 0;
1018e3b99556SMark McLoughlin }
1019e3b99556SMark McLoughlin 
10205228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
10215228ddc9SRusty Russell  * privs required. */
10225228ddc9SRusty Russell static int set_offload(struct net_device *dev, unsigned long arg)
10235228ddc9SRusty Russell {
10245228ddc9SRusty Russell 	unsigned int old_features, features;
10255228ddc9SRusty Russell 
10265228ddc9SRusty Russell 	old_features = dev->features;
10275228ddc9SRusty Russell 	/* Unset features, set them as we chew on the arg. */
10285228ddc9SRusty Russell 	features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
10295228ddc9SRusty Russell 				    |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
10305228ddc9SRusty Russell 
10315228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
10325228ddc9SRusty Russell 		features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
10335228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
10345228ddc9SRusty Russell 
10355228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
10365228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
10375228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
10385228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
10395228ddc9SRusty Russell 			}
10405228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
10415228ddc9SRusty Russell 				features |= NETIF_F_TSO;
10425228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
10435228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
10445228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
10455228ddc9SRusty Russell 		}
10465228ddc9SRusty Russell 	}
10475228ddc9SRusty Russell 
10485228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
10495228ddc9SRusty Russell 	 * trying to set them. */
10505228ddc9SRusty Russell 	if (arg)
10515228ddc9SRusty Russell 		return -EINVAL;
10525228ddc9SRusty Russell 
10535228ddc9SRusty Russell 	dev->features = features;
10545228ddc9SRusty Russell 	if (old_features != dev->features)
10555228ddc9SRusty Russell 		netdev_features_change(dev);
10565228ddc9SRusty Russell 
10575228ddc9SRusty Russell 	return 0;
10585228ddc9SRusty Russell }
10595228ddc9SRusty Russell 
10601da177e4SLinus Torvalds static int tun_chr_ioctl(struct inode *inode, struct file *file,
10611da177e4SLinus Torvalds 			 unsigned int cmd, unsigned long arg)
10621da177e4SLinus Torvalds {
106336b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1064631ab46bSEric W. Biederman 	struct tun_struct *tun;
10651da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
10661da177e4SLinus Torvalds 	struct ifreq ifr;
106733dccbb0SHerbert Xu 	int sndbuf;
1068f271b2ccSMax Krasnyansky 	int ret;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
10711da177e4SLinus Torvalds 		if (copy_from_user(&ifr, argp, sizeof ifr))
10721da177e4SLinus Torvalds 			return -EFAULT;
10731da177e4SLinus Torvalds 
1074631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
1075631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
1076631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
1077631ab46bSEric W. Biederman 		 * TUNSETIFF. */
1078631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1079631ab46bSEric W. Biederman 				IFF_VNET_HDR,
1080631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
1081631ab46bSEric W. Biederman 	}
1082631ab46bSEric W. Biederman 
108336b50babSEric W. Biederman 	tun = __tun_get(tfile);
10841da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
10851da177e4SLinus Torvalds 		int err;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 		rtnl_lock();
109036b50babSEric W. Biederman 		err = tun_set_iff(tfile->net, file, &ifr);
10911da177e4SLinus Torvalds 		rtnl_unlock();
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 		if (err)
10941da177e4SLinus Torvalds 			return err;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
10971da177e4SLinus Torvalds 			return -EFAULT;
10981da177e4SLinus Torvalds 		return 0;
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 
110107240fd0SRusty Russell 
11021da177e4SLinus Torvalds 	if (!tun)
11031da177e4SLinus Torvalds 		return -EBADFD;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
11061da177e4SLinus Torvalds 
1107631ab46bSEric W. Biederman 	ret = 0;
11081da177e4SLinus Torvalds 	switch (cmd) {
1109e3b99556SMark McLoughlin 	case TUNGETIFF:
1110e3b99556SMark McLoughlin 		ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
1111e3b99556SMark McLoughlin 		if (ret)
1112631ab46bSEric W. Biederman 			break;
1113e3b99556SMark McLoughlin 
1114e3b99556SMark McLoughlin 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
1115631ab46bSEric W. Biederman 			ret = -EFAULT;
1116e3b99556SMark McLoughlin 		break;
1117e3b99556SMark McLoughlin 
11181da177e4SLinus Torvalds 	case TUNSETNOCSUM:
11191da177e4SLinus Torvalds 		/* Disable/Enable checksum */
11201da177e4SLinus Torvalds 		if (arg)
11211da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
11221da177e4SLinus Torvalds 		else
11231da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
11261da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
11271da177e4SLinus Torvalds 		break;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	case TUNSETPERSIST:
11301da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
11311da177e4SLinus Torvalds 		if (arg)
11321da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
11331da177e4SLinus Torvalds 		else
11341da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
1137c6e991deSToyo Abe 		    tun->dev->name, arg ? "enabled" : "disabled");
11381da177e4SLinus Torvalds 		break;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	case TUNSETOWNER:
11411da177e4SLinus Torvalds 		/* Set owner of the device */
11421da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
11451da177e4SLinus Torvalds 		break;
11461da177e4SLinus Torvalds 
11478c644623SGuido Guenther 	case TUNSETGROUP:
11488c644623SGuido Guenther 		/* Set group of the device */
11498c644623SGuido Guenther 		tun->group= (gid_t) arg;
11508c644623SGuido Guenther 
11518c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
11528c644623SGuido Guenther 		break;
11538c644623SGuido Guenther 
1154ff4cc3acSMike Kershaw 	case TUNSETLINK:
1155ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
115648abfe05SDavid S. Miller 		rtnl_lock();
1157ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
1158ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
1159ff4cc3acSMike Kershaw 				tun->dev->name);
116048abfe05SDavid S. Miller 			ret = -EBUSY;
1161ff4cc3acSMike Kershaw 		} else {
1162ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
1163ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
116448abfe05SDavid S. Miller 			ret = 0;
1165ff4cc3acSMike Kershaw 		}
116648abfe05SDavid S. Miller 		rtnl_unlock();
1167631ab46bSEric W. Biederman 		break;
1168ff4cc3acSMike Kershaw 
11691da177e4SLinus Torvalds #ifdef TUN_DEBUG
11701da177e4SLinus Torvalds 	case TUNSETDEBUG:
11711da177e4SLinus Torvalds 		tun->debug = arg;
11721da177e4SLinus Torvalds 		break;
11731da177e4SLinus Torvalds #endif
11745228ddc9SRusty Russell 	case TUNSETOFFLOAD:
11755228ddc9SRusty Russell 		rtnl_lock();
11765228ddc9SRusty Russell 		ret = set_offload(tun->dev, arg);
11775228ddc9SRusty Russell 		rtnl_unlock();
1178631ab46bSEric W. Biederman 		break;
11795228ddc9SRusty Russell 
1180f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1181f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1182631ab46bSEric W. Biederman 		ret = -EINVAL;
1183f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1184631ab46bSEric W. Biederman 			break;
1185f271b2ccSMax Krasnyansky 		rtnl_lock();
1186c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1187f271b2ccSMax Krasnyansky 		rtnl_unlock();
1188631ab46bSEric W. Biederman 		break;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1191f271b2ccSMax Krasnyansky 		/* Get hw addres */
1192f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1193f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
11941da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof ifr))
1195631ab46bSEric W. Biederman 			ret = -EFAULT;
1196631ab46bSEric W. Biederman 		break;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1199f271b2ccSMax Krasnyansky 		/* Set hw address */
1200e174961cSJohannes Berg 		DBG(KERN_DEBUG "%s: set hw address: %pM\n",
1201e174961cSJohannes Berg 			tun->dev->name, ifr.ifr_hwaddr.sa_data);
120240102371SKim B. Heino 
120340102371SKim B. Heino 		rtnl_lock();
120440102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
120540102371SKim B. Heino 		rtnl_unlock();
1206631ab46bSEric W. Biederman 		break;
120733dccbb0SHerbert Xu 
120833dccbb0SHerbert Xu 	case TUNGETSNDBUF:
120933dccbb0SHerbert Xu 		sndbuf = tun->sk->sk_sndbuf;
121033dccbb0SHerbert Xu 		if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
121133dccbb0SHerbert Xu 			ret = -EFAULT;
121233dccbb0SHerbert Xu 		break;
121333dccbb0SHerbert Xu 
121433dccbb0SHerbert Xu 	case TUNSETSNDBUF:
121533dccbb0SHerbert Xu 		if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
121633dccbb0SHerbert Xu 			ret = -EFAULT;
121733dccbb0SHerbert Xu 			break;
121833dccbb0SHerbert Xu 		}
121933dccbb0SHerbert Xu 
122033dccbb0SHerbert Xu 		tun->sk->sk_sndbuf = sndbuf;
122133dccbb0SHerbert Xu 		break;
122233dccbb0SHerbert Xu 
12231da177e4SLinus Torvalds 	default:
1224631ab46bSEric W. Biederman 		ret = -EINVAL;
1225631ab46bSEric W. Biederman 		break;
12261da177e4SLinus Torvalds 	};
12271da177e4SLinus Torvalds 
1228631ab46bSEric W. Biederman 	tun_put(tun);
1229631ab46bSEric W. Biederman 	return ret;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
12331da177e4SLinus Torvalds {
1234631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
12351da177e4SLinus Torvalds 	int ret;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	if (!tun)
12381da177e4SLinus Torvalds 		return -EBADFD;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
12411da177e4SLinus Torvalds 
12429d319522SJonathan Corbet 	lock_kernel();
12431da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
12449d319522SJonathan Corbet 		goto out;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	if (on) {
1247609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
12481da177e4SLinus Torvalds 		if (ret)
12499d319522SJonathan Corbet 			goto out;
12501da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
12511da177e4SLinus Torvalds 	} else
12521da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
12539d319522SJonathan Corbet 	ret = 0;
12549d319522SJonathan Corbet out:
12559d319522SJonathan Corbet 	unlock_kernel();
1256631ab46bSEric W. Biederman 	tun_put(tun);
12579d319522SJonathan Corbet 	return ret;
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
12611da177e4SLinus Torvalds {
1262631ab46bSEric W. Biederman 	struct tun_file *tfile;
1263fd3e05b6SArnd Bergmann 	cycle_kernel_lock();
12641da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
1265631ab46bSEric W. Biederman 
1266631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1267631ab46bSEric W. Biederman 	if (!tfile)
1268631ab46bSEric W. Biederman 		return -ENOMEM;
1269c70f1829SEric W. Biederman 	atomic_set(&tfile->count, 0);
1270631ab46bSEric W. Biederman 	tfile->tun = NULL;
127136b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1272631ab46bSEric W. Biederman 	file->private_data = tfile;
12731da177e4SLinus Torvalds 	return 0;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
12771da177e4SLinus Torvalds {
1278631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1279631ab46bSEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 
1282631ab46bSEric W. Biederman 	if (tun) {
12831da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 		rtnl_lock();
1286631ab46bSEric W. Biederman 		__tun_detach(tun);
12871da177e4SLinus Torvalds 
1288631ab46bSEric W. Biederman 		/* If desireable, unregister the netdevice. */
12899c3fea6aSHerbert Xu 		if (!(tun->flags & TUN_PERSIST))
12901da177e4SLinus Torvalds 			unregister_netdevice(tun->dev);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		rtnl_unlock();
1293631ab46bSEric W. Biederman 	}
1294631ab46bSEric W. Biederman 
12959c3fea6aSHerbert Xu 	tun = tfile->tun;
12969c3fea6aSHerbert Xu 	if (tun)
12979c3fea6aSHerbert Xu 		sock_put(tun->sk);
12989c3fea6aSHerbert Xu 
129936b50babSEric W. Biederman 	put_net(tfile->net);
1300631ab46bSEric W. Biederman 	kfree(tfile);
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	return 0;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
1305d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
13061da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
13071da177e4SLinus Torvalds 	.llseek = no_llseek,
1308ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1309ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1310ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1311ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
13121da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
13131da177e4SLinus Torvalds 	.ioctl	= tun_chr_ioctl,
13141da177e4SLinus Torvalds 	.open	= tun_chr_open,
13151da177e4SLinus Torvalds 	.release = tun_chr_close,
13161da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
13171da177e4SLinus Torvalds };
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
13201da177e4SLinus Torvalds 	.minor = TUN_MINOR,
13211da177e4SLinus Torvalds 	.name = "tun",
13221da177e4SLinus Torvalds 	.fops = &tun_fops,
13231da177e4SLinus Torvalds };
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds /* ethtool interface */
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
13281da177e4SLinus Torvalds {
13291da177e4SLinus Torvalds 	cmd->supported		= 0;
13301da177e4SLinus Torvalds 	cmd->advertising	= 0;
13311da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
13321da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
13331da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
13341da177e4SLinus Torvalds 	cmd->phy_address	= 0;
13351da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
13361da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
13371da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
13381da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
13391da177e4SLinus Torvalds 	return 0;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
13471da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
13481da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
13511da177e4SLinus Torvalds 	case TUN_TUN_DEV:
13521da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
13531da177e4SLinus Torvalds 		break;
13541da177e4SLinus Torvalds 	case TUN_TAP_DEV:
13551da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
13561da177e4SLinus Torvalds 		break;
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds #ifdef TUN_DEBUG
13631da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
13641da177e4SLinus Torvalds 	return tun->debug;
13651da177e4SLinus Torvalds #else
13661da177e4SLinus Torvalds 	return -EOPNOTSUPP;
13671da177e4SLinus Torvalds #endif
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
13711da177e4SLinus Torvalds {
13721da177e4SLinus Torvalds #ifdef TUN_DEBUG
13731da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
13741da177e4SLinus Torvalds 	tun->debug = value;
13751da177e4SLinus Torvalds #endif
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
13791da177e4SLinus Torvalds {
13801da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1381631ab46bSEric W. Biederman 	return !!tun->tfile;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
13851da177e4SLinus Torvalds {
13861da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
13871da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
13911da177e4SLinus Torvalds {
13921da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
13931da177e4SLinus Torvalds 	if (data)
13941da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
13951da177e4SLinus Torvalds 	else
13961da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
13971da177e4SLinus Torvalds 	return 0;
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
14007282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
14011da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
14021da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
14031da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
14041da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
14051da177e4SLinus Torvalds 	.get_link	= tun_get_link,
14061da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
14071da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
14081da177e4SLinus Torvalds };
14091da177e4SLinus Torvalds 
141079d17604SPavel Emelyanov 
14111da177e4SLinus Torvalds static int __init tun_init(void)
14121da177e4SLinus Torvalds {
14131da177e4SLinus Torvalds 	int ret = 0;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
14161da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
14171da177e4SLinus Torvalds 
1418f019a7a5SEric W. Biederman 	ret = rtnl_link_register(&tun_link_ops);
141979d17604SPavel Emelyanov 	if (ret) {
1420f019a7a5SEric W. Biederman 		printk(KERN_ERR "tun: Can't register link_ops\n");
1421f019a7a5SEric W. Biederman 		goto err_linkops;
142279d17604SPavel Emelyanov 	}
142379d17604SPavel Emelyanov 
14241da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
142579d17604SPavel Emelyanov 	if (ret) {
14261da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
142779d17604SPavel Emelyanov 		goto err_misc;
142879d17604SPavel Emelyanov 	}
142979d17604SPavel Emelyanov 	return  0;
143079d17604SPavel Emelyanov err_misc:
1431f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
1432f019a7a5SEric W. Biederman err_linkops:
14331da177e4SLinus Torvalds 	return ret;
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds static void tun_cleanup(void)
14371da177e4SLinus Torvalds {
14381da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
1439f019a7a5SEric W. Biederman 	rtnl_link_unregister(&tun_link_ops);
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds module_init(tun_init);
14431da177e4SLinus Torvalds module_exit(tun_cleanup);
14441da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
14451da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
14461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
14471da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1448