xref: /openbmc/linux/drivers/net/tun.c (revision b2430de37ef0bc0799ffba7b5219d38ca417eb76)
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>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <asm/system.h>
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds 
7014daa021SRusty Russell /* Uncomment to enable debugging */
7114daa021SRusty Russell /* #define TUN_DEBUG 1 */
7214daa021SRusty Russell 
731da177e4SLinus Torvalds #ifdef TUN_DEBUG
741da177e4SLinus Torvalds static int debug;
7514daa021SRusty Russell 
7614daa021SRusty Russell #define DBG  if(tun->debug)printk
7714daa021SRusty Russell #define DBG1 if(debug==2)printk
7814daa021SRusty Russell #else
7914daa021SRusty Russell #define DBG( a... )
8014daa021SRusty Russell #define DBG1( a... )
811da177e4SLinus Torvalds #endif
821da177e4SLinus Torvalds 
83f271b2ccSMax Krasnyansky #define FLT_EXACT_COUNT 8
84f271b2ccSMax Krasnyansky struct tap_filter {
85f271b2ccSMax Krasnyansky 	unsigned int    count;    /* Number of addrs. Zero means disabled */
86f271b2ccSMax Krasnyansky 	u32             mask[2];  /* Mask of the hashed addrs */
87f271b2ccSMax Krasnyansky 	unsigned char	addr[FLT_EXACT_COUNT][ETH_ALEN];
88f271b2ccSMax Krasnyansky };
89f271b2ccSMax Krasnyansky 
90631ab46bSEric W. Biederman struct tun_file {
91631ab46bSEric W. Biederman 	struct tun_struct *tun;
9236b50babSEric W. Biederman 	struct net *net;
93*b2430de3SEric W. Biederman 	wait_queue_head_t	read_wait;
94631ab46bSEric W. Biederman };
95631ab46bSEric W. Biederman 
9614daa021SRusty Russell struct tun_struct {
97631ab46bSEric W. Biederman 	struct tun_file		*tfile;
98f271b2ccSMax Krasnyansky 	unsigned int 		flags;
9914daa021SRusty Russell 	uid_t			owner;
10014daa021SRusty Russell 	gid_t			group;
10114daa021SRusty Russell 
10214daa021SRusty Russell 	struct sk_buff_head	readq;
10314daa021SRusty Russell 
10414daa021SRusty Russell 	struct net_device	*dev;
10514daa021SRusty Russell 	struct fasync_struct	*fasync;
10614daa021SRusty Russell 
107f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
10814daa021SRusty Russell 
10914daa021SRusty Russell #ifdef TUN_DEBUG
11014daa021SRusty Russell 	int debug;
11114daa021SRusty Russell #endif
11214daa021SRusty Russell };
11314daa021SRusty Russell 
114a7385ba2SEric W. Biederman static int tun_attach(struct tun_struct *tun, struct file *file)
115a7385ba2SEric W. Biederman {
116631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
117a7385ba2SEric W. Biederman 	const struct cred *cred = current_cred();
11838231b7aSEric W. Biederman 	int err;
119a7385ba2SEric W. Biederman 
120a7385ba2SEric W. Biederman 	ASSERT_RTNL();
121a7385ba2SEric W. Biederman 
122a7385ba2SEric W. Biederman 	/* Check permissions */
123a7385ba2SEric W. Biederman 	if (((tun->owner != -1 && cred->euid != tun->owner) ||
124a7385ba2SEric W. Biederman 	     (tun->group != -1 && cred->egid != tun->group)) &&
125a7385ba2SEric W. Biederman 		!capable(CAP_NET_ADMIN))
126a7385ba2SEric W. Biederman 		return -EPERM;
127a7385ba2SEric W. Biederman 
12838231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
12938231b7aSEric W. Biederman 
13038231b7aSEric W. Biederman 	err = -EINVAL;
13138231b7aSEric W. Biederman 	if (tfile->tun)
13238231b7aSEric W. Biederman 		goto out;
13338231b7aSEric W. Biederman 
13438231b7aSEric W. Biederman 	err = -EBUSY;
13538231b7aSEric W. Biederman 	if (tun->tfile)
13638231b7aSEric W. Biederman 		goto out;
13738231b7aSEric W. Biederman 
13838231b7aSEric W. Biederman 	err = 0;
139631ab46bSEric W. Biederman 	tfile->tun = tun;
140631ab46bSEric W. Biederman 	tun->tfile = tfile;
141a7385ba2SEric W. Biederman 
14238231b7aSEric W. Biederman out:
14338231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
14438231b7aSEric W. Biederman 	return err;
145a7385ba2SEric W. Biederman }
146a7385ba2SEric W. Biederman 
147631ab46bSEric W. Biederman static void __tun_detach(struct tun_struct *tun)
148631ab46bSEric W. Biederman {
149631ab46bSEric W. Biederman 	struct tun_file *tfile = tun->tfile;
150631ab46bSEric W. Biederman 
151631ab46bSEric W. Biederman 	/* Detach from net device */
15238231b7aSEric W. Biederman 	netif_tx_lock_bh(tun->dev);
153631ab46bSEric W. Biederman 	tfile->tun = NULL;
154631ab46bSEric W. Biederman 	tun->tfile = NULL;
15538231b7aSEric W. Biederman 	netif_tx_unlock_bh(tun->dev);
156631ab46bSEric W. Biederman 
157631ab46bSEric W. Biederman 	/* Drop read queue */
158631ab46bSEric W. Biederman 	skb_queue_purge(&tun->readq);
159631ab46bSEric W. Biederman }
160631ab46bSEric W. Biederman 
161631ab46bSEric W. Biederman static struct tun_struct *__tun_get(struct tun_file *tfile)
162631ab46bSEric W. Biederman {
163631ab46bSEric W. Biederman 	return tfile->tun;
164631ab46bSEric W. Biederman }
165631ab46bSEric W. Biederman 
166631ab46bSEric W. Biederman static struct tun_struct *tun_get(struct file *file)
167631ab46bSEric W. Biederman {
168631ab46bSEric W. Biederman 	return __tun_get(file->private_data);
169631ab46bSEric W. Biederman }
170631ab46bSEric W. Biederman 
171631ab46bSEric W. Biederman static void tun_put(struct tun_struct *tun)
172631ab46bSEric W. Biederman {
173631ab46bSEric W. Biederman 	/* Noop for now */
174631ab46bSEric W. Biederman }
175631ab46bSEric W. Biederman 
176f271b2ccSMax Krasnyansky /* TAP filterting */
177f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
178f271b2ccSMax Krasnyansky {
179f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
180f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
181f271b2ccSMax Krasnyansky }
182f271b2ccSMax Krasnyansky 
183f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
184f271b2ccSMax Krasnyansky {
185f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
186f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
187f271b2ccSMax Krasnyansky }
188f271b2ccSMax Krasnyansky 
189f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
190f271b2ccSMax Krasnyansky {
191f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
192f271b2ccSMax Krasnyansky 	struct tun_filter uf;
193f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
194f271b2ccSMax Krasnyansky 
195f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
196f271b2ccSMax Krasnyansky 		return -EFAULT;
197f271b2ccSMax Krasnyansky 
198f271b2ccSMax Krasnyansky 	if (!uf.count) {
199f271b2ccSMax Krasnyansky 		/* Disabled */
200f271b2ccSMax Krasnyansky 		filter->count = 0;
201f271b2ccSMax Krasnyansky 		return 0;
202f271b2ccSMax Krasnyansky 	}
203f271b2ccSMax Krasnyansky 
204f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
205f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
206f271b2ccSMax Krasnyansky 	if (!addr)
207f271b2ccSMax Krasnyansky 		return -ENOMEM;
208f271b2ccSMax Krasnyansky 
209f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
210f271b2ccSMax Krasnyansky 		err = -EFAULT;
211f271b2ccSMax Krasnyansky 		goto done;
212f271b2ccSMax Krasnyansky 	}
213f271b2ccSMax Krasnyansky 
214f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
215f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
216f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
217f271b2ccSMax Krasnyansky 	filter->count = 0;
218f271b2ccSMax Krasnyansky 	wmb();
219f271b2ccSMax Krasnyansky 
220f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
221f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
222f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
223f271b2ccSMax Krasnyansky 
224f271b2ccSMax Krasnyansky 	nexact = n;
225f271b2ccSMax Krasnyansky 
226f271b2ccSMax Krasnyansky 	/* The rest is hashed */
227f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
228f271b2ccSMax Krasnyansky 	for (; n < uf.count; n++)
229f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
230f271b2ccSMax Krasnyansky 
231f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
232f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
233f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
234f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
235f271b2ccSMax Krasnyansky 
236f271b2ccSMax Krasnyansky 	/* Now enable the filter */
237f271b2ccSMax Krasnyansky 	wmb();
238f271b2ccSMax Krasnyansky 	filter->count = nexact;
239f271b2ccSMax Krasnyansky 
240f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
241f271b2ccSMax Krasnyansky 	err = nexact;
242f271b2ccSMax Krasnyansky 
243f271b2ccSMax Krasnyansky done:
244f271b2ccSMax Krasnyansky 	kfree(addr);
245f271b2ccSMax Krasnyansky 	return err;
246f271b2ccSMax Krasnyansky }
247f271b2ccSMax Krasnyansky 
248f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
249f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
250f271b2ccSMax Krasnyansky {
251f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
252f271b2ccSMax Krasnyansky 	 * at this point. */
253f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
254f271b2ccSMax Krasnyansky 	int i;
255f271b2ccSMax Krasnyansky 
256f271b2ccSMax Krasnyansky 	/* Exact match */
257f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
258f271b2ccSMax Krasnyansky 		if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
259f271b2ccSMax Krasnyansky 			return 1;
260f271b2ccSMax Krasnyansky 
261f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
262f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
263f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
264f271b2ccSMax Krasnyansky 
265f271b2ccSMax Krasnyansky 	return 0;
266f271b2ccSMax Krasnyansky }
267f271b2ccSMax Krasnyansky 
268f271b2ccSMax Krasnyansky /*
269f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
270f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
271f271b2ccSMax Krasnyansky  */
272f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
273f271b2ccSMax Krasnyansky {
274f271b2ccSMax Krasnyansky 	if (!filter->count)
275f271b2ccSMax Krasnyansky 		return 1;
276f271b2ccSMax Krasnyansky 
277f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
278f271b2ccSMax Krasnyansky }
279f271b2ccSMax Krasnyansky 
2801da177e4SLinus Torvalds /* Network device part of the driver */
2811da177e4SLinus Torvalds 
2827282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /* Net device open. */
2851da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	netif_start_queue(dev);
2881da177e4SLinus Torvalds 	return 0;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds /* Net device close. */
2921da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	netif_stop_queue(dev);
2951da177e4SLinus Torvalds 	return 0;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds /* Net device start xmit */
2991da177e4SLinus Torvalds static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
306631ab46bSEric W. Biederman 	if (!tun->tfile)
3071da177e4SLinus Torvalds 		goto drop;
3081da177e4SLinus Torvalds 
309f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
310f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
311f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
312f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
313f271b2ccSMax Krasnyansky 		goto drop;
314f271b2ccSMax Krasnyansky 
3151da177e4SLinus Torvalds 	if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
3161da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
3171da177e4SLinus Torvalds 			/* Normal queueing mode. */
3181da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
3191da177e4SLinus Torvalds 			netif_stop_queue(dev);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
3221da177e4SLinus Torvalds 			 * error is more appropriate. */
32309f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
3241da177e4SLinus Torvalds 		} else {
3251da177e4SLinus Torvalds 			/* Single queue mode.
3261da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
3271da177e4SLinus Torvalds 			goto drop;
3281da177e4SLinus Torvalds 		}
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 
331f271b2ccSMax Krasnyansky 	/* Enqueue packet */
3321da177e4SLinus Torvalds 	skb_queue_tail(&tun->readq, skb);
3331da177e4SLinus Torvalds 	dev->trans_start = jiffies;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* Notify and wake up reader process */
3361da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
3371da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
338*b2430de3SEric W. Biederman 	wake_up_interruptible(&tun->tfile->read_wait);
3391da177e4SLinus Torvalds 	return 0;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds drop:
34209f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
3431da177e4SLinus Torvalds 	kfree_skb(skb);
3441da177e4SLinus Torvalds 	return 0;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
347f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
3481da177e4SLinus Torvalds {
349f271b2ccSMax Krasnyansky 	/*
350f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
351f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
352f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
353f271b2ccSMax Krasnyansky 	 */
354f271b2ccSMax Krasnyansky 	return;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3574885a504SEd Swierk #define MIN_MTU 68
3584885a504SEd Swierk #define MAX_MTU 65535
3594885a504SEd Swierk 
3604885a504SEd Swierk static int
3614885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
3624885a504SEd Swierk {
3634885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
3644885a504SEd Swierk 		return -EINVAL;
3654885a504SEd Swierk 	dev->mtu = new_mtu;
3664885a504SEd Swierk 	return 0;
3674885a504SEd Swierk }
3684885a504SEd Swierk 
369758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
370758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
371758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
37200829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
373758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
374758e43b7SStephen Hemminger };
375758e43b7SStephen Hemminger 
376758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
377758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
378758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
37900829823SStephen Hemminger 	.ndo_start_xmit		= tun_net_xmit,
380758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
381758e43b7SStephen Hemminger 	.ndo_set_multicast_list	= tun_net_mclist,
382758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
383758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
384758e43b7SStephen Hemminger };
385758e43b7SStephen Hemminger 
3861da177e4SLinus Torvalds /* Initialize net device. */
3871da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
3921da177e4SLinus Torvalds 	case TUN_TUN_DEV:
393758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
394758e43b7SStephen Hemminger 
3951da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
3961da177e4SLinus Torvalds 		dev->hard_header_len = 0;
3971da177e4SLinus Torvalds 		dev->addr_len = 0;
3981da177e4SLinus Torvalds 		dev->mtu = 1500;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 		/* Zero header length */
4011da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
4021da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
4031da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
4041da177e4SLinus Torvalds 		break;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	case TUN_TAP_DEV:
4077a0a9608SKusanagi Kouichi 		dev->netdev_ops = &tap_netdev_ops;
4081da177e4SLinus Torvalds 		/* Ethernet TAP Device */
4091da177e4SLinus Torvalds 		ether_setup(dev);
41036226a8dSBrian Braunstein 
411f271b2ccSMax Krasnyansky 		random_ether_addr(dev->dev_addr);
41236226a8dSBrian Braunstein 
4131da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
4141da177e4SLinus Torvalds 		break;
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds /* Character device part */
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds /* Poll */
4211da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
4221da177e4SLinus Torvalds {
423*b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
424*b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
4251da177e4SLinus Torvalds 	unsigned int mask = POLLOUT | POLLWRNORM;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	if (!tun)
428eac9e902SEric W. Biederman 		return POLLERR;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
4311da177e4SLinus Torvalds 
432*b2430de3SEric W. Biederman 	poll_wait(file, &tfile->read_wait, wait);
4331da177e4SLinus Torvalds 
434b03efcfbSDavid S. Miller 	if (!skb_queue_empty(&tun->readq))
4351da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
4361da177e4SLinus Torvalds 
437631ab46bSEric W. Biederman 	tun_put(tun);
4381da177e4SLinus Torvalds 	return mask;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
441f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
442f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
443f42157cbSRusty Russell static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear,
444f42157cbSRusty Russell 				     gfp_t gfp)
445f42157cbSRusty Russell {
446f42157cbSRusty Russell 	struct sk_buff *skb;
447f42157cbSRusty Russell 	unsigned int i;
448f42157cbSRusty Russell 
449f42157cbSRusty Russell 	skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
450f42157cbSRusty Russell 	if (skb) {
451f42157cbSRusty Russell 		skb_reserve(skb, prepad);
452f42157cbSRusty Russell 		skb_put(skb, len);
453f42157cbSRusty Russell 		return skb;
454f42157cbSRusty Russell 	}
455f42157cbSRusty Russell 
456f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
457f42157cbSRusty Russell 	if (prepad + len < PAGE_SIZE)
458f42157cbSRusty Russell 		return NULL;
459f42157cbSRusty Russell 
460f42157cbSRusty Russell 	/* Start with a normal skb, and add pages. */
461f42157cbSRusty Russell 	skb = alloc_skb(prepad + linear, gfp);
462f42157cbSRusty Russell 	if (!skb)
463f42157cbSRusty Russell 		return NULL;
464f42157cbSRusty Russell 
465f42157cbSRusty Russell 	skb_reserve(skb, prepad);
466f42157cbSRusty Russell 	skb_put(skb, linear);
467f42157cbSRusty Russell 
468f42157cbSRusty Russell 	len -= linear;
469f42157cbSRusty Russell 
470f42157cbSRusty Russell 	for (i = 0; i < MAX_SKB_FRAGS; i++) {
471f42157cbSRusty Russell 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
472f42157cbSRusty Russell 
473f42157cbSRusty Russell 		f->page = alloc_page(gfp|__GFP_ZERO);
474f42157cbSRusty Russell 		if (!f->page)
475f42157cbSRusty Russell 			break;
476f42157cbSRusty Russell 
477f42157cbSRusty Russell 		f->page_offset = 0;
478f42157cbSRusty Russell 		f->size = PAGE_SIZE;
479f42157cbSRusty Russell 
480f42157cbSRusty Russell 		skb->data_len += PAGE_SIZE;
481f42157cbSRusty Russell 		skb->len += PAGE_SIZE;
482f42157cbSRusty Russell 		skb->truesize += PAGE_SIZE;
483f42157cbSRusty Russell 		skb_shinfo(skb)->nr_frags++;
484f42157cbSRusty Russell 
485f42157cbSRusty Russell 		if (len < PAGE_SIZE) {
486f42157cbSRusty Russell 			len = 0;
487f42157cbSRusty Russell 			break;
488f42157cbSRusty Russell 		}
489f42157cbSRusty Russell 		len -= PAGE_SIZE;
490f42157cbSRusty Russell 	}
491f42157cbSRusty Russell 
492f42157cbSRusty Russell 	/* Too large, or alloc fail? */
493f42157cbSRusty Russell 	if (unlikely(len)) {
494f42157cbSRusty Russell 		kfree_skb(skb);
495f42157cbSRusty Russell 		skb = NULL;
496f42157cbSRusty Russell 	}
497f42157cbSRusty Russell 
498f42157cbSRusty Russell 	return skb;
499f42157cbSRusty Russell }
500f42157cbSRusty Russell 
5011da177e4SLinus Torvalds /* Get packet from user space buffer */
5021da177e4SLinus Torvalds static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
5051da177e4SLinus Torvalds 	struct sk_buff *skb;
5061da177e4SLinus Torvalds 	size_t len = count, align = 0;
507f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
5101da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
5111da177e4SLinus Torvalds 			return -EINVAL;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 		if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
5141da177e4SLinus Torvalds 			return -EFAULT;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
517f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
518f43798c2SRusty Russell 		if ((len -= sizeof(gso)) > count)
519f43798c2SRusty Russell 			return -EINVAL;
520f43798c2SRusty Russell 
521f43798c2SRusty Russell 		if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
522f43798c2SRusty Russell 			return -EFAULT;
523f43798c2SRusty Russell 
524f43798c2SRusty Russell 		if (gso.hdr_len > len)
525f43798c2SRusty Russell 			return -EINVAL;
526f43798c2SRusty Russell 	}
527f43798c2SRusty Russell 
528e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
5291da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
530e01bf1c8SRusty Russell 		if (unlikely(len < ETH_HLEN))
531e01bf1c8SRusty Russell 			return -EINVAL;
532e01bf1c8SRusty Russell 	}
5331da177e4SLinus Torvalds 
534f42157cbSRusty Russell 	if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
53509f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
5361da177e4SLinus Torvalds 		return -ENOMEM;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
539f42157cbSRusty Russell 	if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
54009f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
5418f22757eSDave Jones 		kfree_skb(skb);
5421da177e4SLinus Torvalds 		return -EFAULT;
5438f22757eSDave Jones 	}
5441da177e4SLinus Torvalds 
545f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
546f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
547f43798c2SRusty Russell 					  gso.csum_offset)) {
548f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
549f43798c2SRusty Russell 			kfree_skb(skb);
550f43798c2SRusty Russell 			return -EINVAL;
551f43798c2SRusty Russell 		}
552f43798c2SRusty Russell 	} else if (tun->flags & TUN_NOCHECKSUM)
553f43798c2SRusty Russell 		skb->ip_summed = CHECKSUM_UNNECESSARY;
554f43798c2SRusty Russell 
5551da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
5561da177e4SLinus Torvalds 	case TUN_TUN_DEV:
557f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
558f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
559f09f7ee2SAng Way Chuang 			case 0x40:
560f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
561f09f7ee2SAng Way Chuang 				break;
562f09f7ee2SAng Way Chuang 			case 0x60:
563f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
564f09f7ee2SAng Way Chuang 				break;
565f09f7ee2SAng Way Chuang 			default:
566f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
567f09f7ee2SAng Way Chuang 				kfree_skb(skb);
568f09f7ee2SAng Way Chuang 				return -EINVAL;
569f09f7ee2SAng Way Chuang 			}
570f09f7ee2SAng Way Chuang 		}
571f09f7ee2SAng Way Chuang 
572459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
5731da177e4SLinus Torvalds 		skb->protocol = pi.proto;
5744c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
5751da177e4SLinus Torvalds 		break;
5761da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5771da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
5781da177e4SLinus Torvalds 		break;
5791da177e4SLinus Torvalds 	};
5801da177e4SLinus Torvalds 
581f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
582f43798c2SRusty Russell 		pr_debug("GSO!\n");
583f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
584f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
585f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
586f43798c2SRusty Russell 			break;
587f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
588f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
589f43798c2SRusty Russell 			break;
590f43798c2SRusty Russell 		default:
591f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
592f43798c2SRusty Russell 			kfree_skb(skb);
593f43798c2SRusty Russell 			return -EINVAL;
594f43798c2SRusty Russell 		}
595f43798c2SRusty Russell 
596f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
597f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
598f43798c2SRusty Russell 
599f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
600f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
601f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
602f43798c2SRusty Russell 			kfree_skb(skb);
603f43798c2SRusty Russell 			return -EINVAL;
604f43798c2SRusty Russell 		}
605f43798c2SRusty Russell 
606f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
607f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
608f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
609f43798c2SRusty Russell 	}
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	netif_rx_ni(skb);
6121da177e4SLinus Torvalds 
61309f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
61409f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	return count;
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
619ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
620ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
6211da177e4SLinus Torvalds {
622631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(iocb->ki_filp);
623631ab46bSEric W. Biederman 	ssize_t result;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (!tun)
6261da177e4SLinus Torvalds 		return -EBADFD;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
6291da177e4SLinus Torvalds 
630631ab46bSEric W. Biederman 	result = tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
631631ab46bSEric W. Biederman 
632631ab46bSEric W. Biederman 	tun_put(tun);
633631ab46bSEric W. Biederman 	return result;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds /* Put packet to the user space buffer */
6371da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
6381da177e4SLinus Torvalds 				       struct sk_buff *skb,
6391da177e4SLinus Torvalds 				       struct iovec *iv, int len)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
6421da177e4SLinus Torvalds 	ssize_t total = 0;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
6451da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
6461da177e4SLinus Torvalds 			return -EINVAL;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 		if (len < skb->len) {
6491da177e4SLinus Torvalds 			/* Packet will be striped */
6501da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
6511da177e4SLinus Torvalds 		}
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 		if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
6541da177e4SLinus Torvalds 			return -EFAULT;
6551da177e4SLinus Torvalds 		total += sizeof(pi);
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 
658f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
659f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
660f43798c2SRusty Russell 		if ((len -= sizeof(gso)) < 0)
661f43798c2SRusty Russell 			return -EINVAL;
662f43798c2SRusty Russell 
663f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
664f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
665f43798c2SRusty Russell 
666f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
667f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
668f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
669f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
670f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
671f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
672f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
673f43798c2SRusty Russell 			else
674f43798c2SRusty Russell 				BUG();
675f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
676f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
677f43798c2SRusty Russell 		} else
678f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
679f43798c2SRusty Russell 
680f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
681f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
682f43798c2SRusty Russell 			gso.csum_start = skb->csum_start - skb_headroom(skb);
683f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
684f43798c2SRusty Russell 		} /* else everything is zero */
685f43798c2SRusty Russell 
686f43798c2SRusty Russell 		if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
687f43798c2SRusty Russell 			return -EFAULT;
688f43798c2SRusty Russell 		total += sizeof(gso);
689f43798c2SRusty Russell 	}
690f43798c2SRusty Russell 
6911da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	skb_copy_datagram_iovec(skb, 0, iv, len);
6941da177e4SLinus Torvalds 	total += len;
6951da177e4SLinus Torvalds 
69609f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
69709f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	return total;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
702ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
703ee0b3e67SBadari Pulavarty 			    unsigned long count, loff_t pos)
7041da177e4SLinus Torvalds {
705ee0b3e67SBadari Pulavarty 	struct file *file = iocb->ki_filp;
706*b2430de3SEric W. Biederman 	struct tun_file *tfile = file->private_data;
707*b2430de3SEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
7081da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
7091da177e4SLinus Torvalds 	struct sk_buff *skb;
7101da177e4SLinus Torvalds 	ssize_t len, ret = 0;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	if (!tun)
7131da177e4SLinus Torvalds 		return -EBADFD;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
7161da177e4SLinus Torvalds 
71752427c9dSAkinobu Mita 	len = iov_length(iv, count);
718631ab46bSEric W. Biederman 	if (len < 0) {
719631ab46bSEric W. Biederman 		ret = -EINVAL;
720631ab46bSEric W. Biederman 		goto out;
721631ab46bSEric W. Biederman 	}
7221da177e4SLinus Torvalds 
723*b2430de3SEric W. Biederman 	add_wait_queue(&tfile->read_wait, &wait);
7241da177e4SLinus Torvalds 	while (len) {
7251da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 		/* Read frames from the queue */
7281da177e4SLinus Torvalds 		if (!(skb=skb_dequeue(&tun->readq))) {
7291da177e4SLinus Torvalds 			if (file->f_flags & O_NONBLOCK) {
7301da177e4SLinus Torvalds 				ret = -EAGAIN;
7311da177e4SLinus Torvalds 				break;
7321da177e4SLinus Torvalds 			}
7331da177e4SLinus Torvalds 			if (signal_pending(current)) {
7341da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
7351da177e4SLinus Torvalds 				break;
7361da177e4SLinus Torvalds 			}
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
7391da177e4SLinus Torvalds 			schedule();
7401da177e4SLinus Torvalds 			continue;
7411da177e4SLinus Torvalds 		}
7421da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 		ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
7451da177e4SLinus Torvalds 		kfree_skb(skb);
7461da177e4SLinus Torvalds 		break;
7471da177e4SLinus Torvalds 	}
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
750*b2430de3SEric W. Biederman 	remove_wait_queue(&tfile->read_wait, &wait);
7511da177e4SLinus Torvalds 
752631ab46bSEric W. Biederman out:
753631ab46bSEric W. Biederman 	tun_put(tun);
7541da177e4SLinus Torvalds 	return ret;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	skb_queue_head_init(&tun->readq);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	tun->owner = -1;
7648c644623SGuido Guenther 	tun->group = -1;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
7671da177e4SLinus Torvalds 	dev->destructor = free_netdev;
768fc54c658SPavel Emelyanov 	dev->features |= NETIF_F_NETNS_LOCAL;
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
771d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	struct tun_struct *tun;
7741da177e4SLinus Torvalds 	struct net_device *dev;
7751da177e4SLinus Torvalds 	int err;
7761da177e4SLinus Torvalds 
77774a3e5a7SEric W. Biederman 	dev = __dev_get_by_name(net, ifr->ifr_name);
77874a3e5a7SEric W. Biederman 	if (dev) {
77974a3e5a7SEric W. Biederman 		if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
78074a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
78174a3e5a7SEric W. Biederman 		else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
78274a3e5a7SEric W. Biederman 			tun = netdev_priv(dev);
78374a3e5a7SEric W. Biederman 		else
78474a3e5a7SEric W. Biederman 			return -EINVAL;
78574a3e5a7SEric W. Biederman 
786a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
787a7385ba2SEric W. Biederman 		if (err < 0)
788a7385ba2SEric W. Biederman 			return err;
78986a264abSDavid Howells 	}
7901da177e4SLinus Torvalds 	else {
7911da177e4SLinus Torvalds 		char *name;
7921da177e4SLinus Torvalds 		unsigned long flags = 0;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 		err = -EINVAL;
7951da177e4SLinus Torvalds 
796ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
797ca6bb5d7SDavid Woodhouse 			return -EPERM;
798ca6bb5d7SDavid Woodhouse 
7991da177e4SLinus Torvalds 		/* Set dev type */
8001da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
8011da177e4SLinus Torvalds 			/* TUN device */
8021da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
8031da177e4SLinus Torvalds 			name = "tun%d";
8041da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
8051da177e4SLinus Torvalds 			/* TAP device */
8061da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
8071da177e4SLinus Torvalds 			name = "tap%d";
8081da177e4SLinus Torvalds 		} else
8091da177e4SLinus Torvalds 			goto failed;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 		if (*ifr->ifr_name)
8121da177e4SLinus Torvalds 			name = ifr->ifr_name;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
8151da177e4SLinus Torvalds 				   tun_setup);
8161da177e4SLinus Torvalds 		if (!dev)
8171da177e4SLinus Torvalds 			return -ENOMEM;
8181da177e4SLinus Torvalds 
819fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
820758e43b7SStephen Hemminger 
8211da177e4SLinus Torvalds 		tun = netdev_priv(dev);
8221da177e4SLinus Torvalds 		tun->dev = dev;
8231da177e4SLinus Torvalds 		tun->flags = flags;
824f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		tun_net_init(dev);
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
8291da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
8301da177e4SLinus Torvalds 			if (err < 0)
8311da177e4SLinus Torvalds 				goto err_free_dev;
8321da177e4SLinus Torvalds 		}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
8351da177e4SLinus Torvalds 		if (err < 0)
8361da177e4SLinus Torvalds 			goto err_free_dev;
837a7385ba2SEric W. Biederman 
838a7385ba2SEric W. Biederman 		err = tun_attach(tun, file);
839a7385ba2SEric W. Biederman 		if (err < 0)
840a7385ba2SEric W. Biederman 			goto err_free_dev;
8411da177e4SLinus Torvalds 	}
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
8461da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
847a26af1e0SNathaniel Filardo 	else
848a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
8511da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
852a26af1e0SNathaniel Filardo 	else
853a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
8541da177e4SLinus Torvalds 
855f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
856f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
857f43798c2SRusty Russell 	else
858f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
859f43798c2SRusty Russell 
860e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
861e35259a9SMax Krasnyansky 	 * xoff state.
862e35259a9SMax Krasnyansky 	 */
863e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
864e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
865e35259a9SMax Krasnyansky 
8661da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
8671da177e4SLinus Torvalds 	return 0;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds  err_free_dev:
8701da177e4SLinus Torvalds 	free_netdev(dev);
8711da177e4SLinus Torvalds  failed:
8721da177e4SLinus Torvalds 	return err;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
875e3b99556SMark McLoughlin static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
876e3b99556SMark McLoughlin {
877631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
878e3b99556SMark McLoughlin 
879e3b99556SMark McLoughlin 	if (!tun)
880e3b99556SMark McLoughlin 		return -EBADFD;
881e3b99556SMark McLoughlin 
882e3b99556SMark McLoughlin 	DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
883e3b99556SMark McLoughlin 
884e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
885e3b99556SMark McLoughlin 
886e3b99556SMark McLoughlin 	ifr->ifr_flags = 0;
887e3b99556SMark McLoughlin 
888e3b99556SMark McLoughlin 	if (ifr->ifr_flags & TUN_TUN_DEV)
889e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_TUN;
890e3b99556SMark McLoughlin 	else
891e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_TAP;
892e3b99556SMark McLoughlin 
893e3b99556SMark McLoughlin 	if (tun->flags & TUN_NO_PI)
894e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_NO_PI;
895e3b99556SMark McLoughlin 
896e3b99556SMark McLoughlin 	if (tun->flags & TUN_ONE_QUEUE)
897e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_ONE_QUEUE;
898e3b99556SMark McLoughlin 
899e3b99556SMark McLoughlin 	if (tun->flags & TUN_VNET_HDR)
900e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_VNET_HDR;
901e3b99556SMark McLoughlin 
902631ab46bSEric W. Biederman 	tun_put(tun);
903e3b99556SMark McLoughlin 	return 0;
904e3b99556SMark McLoughlin }
905e3b99556SMark McLoughlin 
9065228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
9075228ddc9SRusty Russell  * privs required. */
9085228ddc9SRusty Russell static int set_offload(struct net_device *dev, unsigned long arg)
9095228ddc9SRusty Russell {
9105228ddc9SRusty Russell 	unsigned int old_features, features;
9115228ddc9SRusty Russell 
9125228ddc9SRusty Russell 	old_features = dev->features;
9135228ddc9SRusty Russell 	/* Unset features, set them as we chew on the arg. */
9145228ddc9SRusty Russell 	features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
9155228ddc9SRusty Russell 				    |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
9165228ddc9SRusty Russell 
9175228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
9185228ddc9SRusty Russell 		features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
9195228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
9205228ddc9SRusty Russell 
9215228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
9225228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
9235228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
9245228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
9255228ddc9SRusty Russell 			}
9265228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
9275228ddc9SRusty Russell 				features |= NETIF_F_TSO;
9285228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
9295228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
9305228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
9315228ddc9SRusty Russell 		}
9325228ddc9SRusty Russell 	}
9335228ddc9SRusty Russell 
9345228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
9355228ddc9SRusty Russell 	 * trying to set them. */
9365228ddc9SRusty Russell 	if (arg)
9375228ddc9SRusty Russell 		return -EINVAL;
9385228ddc9SRusty Russell 
9395228ddc9SRusty Russell 	dev->features = features;
9405228ddc9SRusty Russell 	if (old_features != dev->features)
9415228ddc9SRusty Russell 		netdev_features_change(dev);
9425228ddc9SRusty Russell 
9435228ddc9SRusty Russell 	return 0;
9445228ddc9SRusty Russell }
9455228ddc9SRusty Russell 
9461da177e4SLinus Torvalds static int tun_chr_ioctl(struct inode *inode, struct file *file,
9471da177e4SLinus Torvalds 			 unsigned int cmd, unsigned long arg)
9481da177e4SLinus Torvalds {
94936b50babSEric W. Biederman 	struct tun_file *tfile = file->private_data;
950631ab46bSEric W. Biederman 	struct tun_struct *tun;
9511da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
9521da177e4SLinus Torvalds 	struct ifreq ifr;
953f271b2ccSMax Krasnyansky 	int ret;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
9561da177e4SLinus Torvalds 		if (copy_from_user(&ifr, argp, sizeof ifr))
9571da177e4SLinus Torvalds 			return -EFAULT;
9581da177e4SLinus Torvalds 
959631ab46bSEric W. Biederman 	if (cmd == TUNGETFEATURES) {
960631ab46bSEric W. Biederman 		/* Currently this just means: "what IFF flags are valid?".
961631ab46bSEric W. Biederman 		 * This is needed because we never checked for invalid flags on
962631ab46bSEric W. Biederman 		 * TUNSETIFF. */
963631ab46bSEric W. Biederman 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
964631ab46bSEric W. Biederman 				IFF_VNET_HDR,
965631ab46bSEric W. Biederman 				(unsigned int __user*)argp);
966631ab46bSEric W. Biederman 	}
967631ab46bSEric W. Biederman 
96836b50babSEric W. Biederman 	tun = __tun_get(tfile);
9691da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
9701da177e4SLinus Torvalds 		int err;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 		rtnl_lock();
97536b50babSEric W. Biederman 		err = tun_set_iff(tfile->net, file, &ifr);
9761da177e4SLinus Torvalds 		rtnl_unlock();
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 		if (err)
9791da177e4SLinus Torvalds 			return err;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
9821da177e4SLinus Torvalds 			return -EFAULT;
9831da177e4SLinus Torvalds 		return 0;
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 
98607240fd0SRusty Russell 
9871da177e4SLinus Torvalds 	if (!tun)
9881da177e4SLinus Torvalds 		return -EBADFD;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
9911da177e4SLinus Torvalds 
992631ab46bSEric W. Biederman 	ret = 0;
9931da177e4SLinus Torvalds 	switch (cmd) {
994e3b99556SMark McLoughlin 	case TUNGETIFF:
995e3b99556SMark McLoughlin 		ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
996e3b99556SMark McLoughlin 		if (ret)
997631ab46bSEric W. Biederman 			break;
998e3b99556SMark McLoughlin 
999e3b99556SMark McLoughlin 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
1000631ab46bSEric W. Biederman 			ret = -EFAULT;
1001e3b99556SMark McLoughlin 		break;
1002e3b99556SMark McLoughlin 
10031da177e4SLinus Torvalds 	case TUNSETNOCSUM:
10041da177e4SLinus Torvalds 		/* Disable/Enable checksum */
10051da177e4SLinus Torvalds 		if (arg)
10061da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
10071da177e4SLinus Torvalds 		else
10081da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
10111da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
10121da177e4SLinus Torvalds 		break;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	case TUNSETPERSIST:
10151da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
10161da177e4SLinus Torvalds 		if (arg)
10171da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
10181da177e4SLinus Torvalds 		else
10191da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
1022c6e991deSToyo Abe 		    tun->dev->name, arg ? "enabled" : "disabled");
10231da177e4SLinus Torvalds 		break;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	case TUNSETOWNER:
10261da177e4SLinus Torvalds 		/* Set owner of the device */
10271da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
10301da177e4SLinus Torvalds 		break;
10311da177e4SLinus Torvalds 
10328c644623SGuido Guenther 	case TUNSETGROUP:
10338c644623SGuido Guenther 		/* Set group of the device */
10348c644623SGuido Guenther 		tun->group= (gid_t) arg;
10358c644623SGuido Guenther 
10368c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
10378c644623SGuido Guenther 		break;
10388c644623SGuido Guenther 
1039ff4cc3acSMike Kershaw 	case TUNSETLINK:
1040ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
104148abfe05SDavid S. Miller 		rtnl_lock();
1042ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
1043ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
1044ff4cc3acSMike Kershaw 				tun->dev->name);
104548abfe05SDavid S. Miller 			ret = -EBUSY;
1046ff4cc3acSMike Kershaw 		} else {
1047ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
1048ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
104948abfe05SDavid S. Miller 			ret = 0;
1050ff4cc3acSMike Kershaw 		}
105148abfe05SDavid S. Miller 		rtnl_unlock();
1052631ab46bSEric W. Biederman 		break;
1053ff4cc3acSMike Kershaw 
10541da177e4SLinus Torvalds #ifdef TUN_DEBUG
10551da177e4SLinus Torvalds 	case TUNSETDEBUG:
10561da177e4SLinus Torvalds 		tun->debug = arg;
10571da177e4SLinus Torvalds 		break;
10581da177e4SLinus Torvalds #endif
10595228ddc9SRusty Russell 	case TUNSETOFFLOAD:
10605228ddc9SRusty Russell 		rtnl_lock();
10615228ddc9SRusty Russell 		ret = set_offload(tun->dev, arg);
10625228ddc9SRusty Russell 		rtnl_unlock();
1063631ab46bSEric W. Biederman 		break;
10645228ddc9SRusty Russell 
1065f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1066f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1067631ab46bSEric W. Biederman 		ret = -EINVAL;
1068f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1069631ab46bSEric W. Biederman 			break;
1070f271b2ccSMax Krasnyansky 		rtnl_lock();
1071c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1072f271b2ccSMax Krasnyansky 		rtnl_unlock();
1073631ab46bSEric W. Biederman 		break;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1076f271b2ccSMax Krasnyansky 		/* Get hw addres */
1077f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1078f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
10791da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof ifr))
1080631ab46bSEric W. Biederman 			ret = -EFAULT;
1081631ab46bSEric W. Biederman 		break;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1084f271b2ccSMax Krasnyansky 		/* Set hw address */
1085e174961cSJohannes Berg 		DBG(KERN_DEBUG "%s: set hw address: %pM\n",
1086e174961cSJohannes Berg 			tun->dev->name, ifr.ifr_hwaddr.sa_data);
108740102371SKim B. Heino 
108840102371SKim B. Heino 		rtnl_lock();
108940102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
109040102371SKim B. Heino 		rtnl_unlock();
1091631ab46bSEric W. Biederman 		break;
10921da177e4SLinus Torvalds 	default:
1093631ab46bSEric W. Biederman 		ret = -EINVAL;
1094631ab46bSEric W. Biederman 		break;
10951da177e4SLinus Torvalds 	};
10961da177e4SLinus Torvalds 
1097631ab46bSEric W. Biederman 	tun_put(tun);
1098631ab46bSEric W. Biederman 	return ret;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
11021da177e4SLinus Torvalds {
1103631ab46bSEric W. Biederman 	struct tun_struct *tun = tun_get(file);
11041da177e4SLinus Torvalds 	int ret;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	if (!tun)
11071da177e4SLinus Torvalds 		return -EBADFD;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
11101da177e4SLinus Torvalds 
11119d319522SJonathan Corbet 	lock_kernel();
11121da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
11139d319522SJonathan Corbet 		goto out;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	if (on) {
1116609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
11171da177e4SLinus Torvalds 		if (ret)
11189d319522SJonathan Corbet 			goto out;
11191da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
11201da177e4SLinus Torvalds 	} else
11211da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
11229d319522SJonathan Corbet 	ret = 0;
11239d319522SJonathan Corbet out:
11249d319522SJonathan Corbet 	unlock_kernel();
1125631ab46bSEric W. Biederman 	tun_put(tun);
11269d319522SJonathan Corbet 	return ret;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
11301da177e4SLinus Torvalds {
1131631ab46bSEric W. Biederman 	struct tun_file *tfile;
1132fd3e05b6SArnd Bergmann 	cycle_kernel_lock();
11331da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
1134631ab46bSEric W. Biederman 
1135631ab46bSEric W. Biederman 	tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
1136631ab46bSEric W. Biederman 	if (!tfile)
1137631ab46bSEric W. Biederman 		return -ENOMEM;
1138631ab46bSEric W. Biederman 	tfile->tun = NULL;
113936b50babSEric W. Biederman 	tfile->net = get_net(current->nsproxy->net_ns);
1140*b2430de3SEric W. Biederman 	init_waitqueue_head(&tfile->read_wait);
1141631ab46bSEric W. Biederman 	file->private_data = tfile;
11421da177e4SLinus Torvalds 	return 0;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
11461da177e4SLinus Torvalds {
1147631ab46bSEric W. Biederman 	struct tun_file *tfile = file->private_data;
1148631ab46bSEric W. Biederman 	struct tun_struct *tun = __tun_get(tfile);
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 
1151631ab46bSEric W. Biederman 	if (tun) {
11521da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 		rtnl_lock();
1155631ab46bSEric W. Biederman 		__tun_detach(tun);
11561da177e4SLinus Torvalds 
1157631ab46bSEric W. Biederman 		/* If desireable, unregister the netdevice. */
115874a3e5a7SEric W. Biederman 		if (!(tun->flags & TUN_PERSIST))
11591da177e4SLinus Torvalds 			unregister_netdevice(tun->dev);
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 		rtnl_unlock();
1162631ab46bSEric W. Biederman 	}
1163631ab46bSEric W. Biederman 
116436b50babSEric W. Biederman 	put_net(tfile->net);
1165631ab46bSEric W. Biederman 	kfree(tfile);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	return 0;
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
1170d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
11711da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
11721da177e4SLinus Torvalds 	.llseek = no_llseek,
1173ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1174ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1175ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1176ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
11771da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
11781da177e4SLinus Torvalds 	.ioctl	= tun_chr_ioctl,
11791da177e4SLinus Torvalds 	.open	= tun_chr_open,
11801da177e4SLinus Torvalds 	.release = tun_chr_close,
11811da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
11821da177e4SLinus Torvalds };
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
11851da177e4SLinus Torvalds 	.minor = TUN_MINOR,
11861da177e4SLinus Torvalds 	.name = "tun",
11871da177e4SLinus Torvalds 	.fops = &tun_fops,
11881da177e4SLinus Torvalds };
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds /* ethtool interface */
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	cmd->supported		= 0;
11951da177e4SLinus Torvalds 	cmd->advertising	= 0;
11961da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
11971da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
11981da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
11991da177e4SLinus Torvalds 	cmd->phy_address	= 0;
12001da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
12011da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
12021da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
12031da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
12041da177e4SLinus Torvalds 	return 0;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
12121da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
12131da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
12161da177e4SLinus Torvalds 	case TUN_TUN_DEV:
12171da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
12181da177e4SLinus Torvalds 		break;
12191da177e4SLinus Torvalds 	case TUN_TAP_DEV:
12201da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
12211da177e4SLinus Torvalds 		break;
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds }
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
12261da177e4SLinus Torvalds {
12271da177e4SLinus Torvalds #ifdef TUN_DEBUG
12281da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
12291da177e4SLinus Torvalds 	return tun->debug;
12301da177e4SLinus Torvalds #else
12311da177e4SLinus Torvalds 	return -EOPNOTSUPP;
12321da177e4SLinus Torvalds #endif
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
12361da177e4SLinus Torvalds {
12371da177e4SLinus Torvalds #ifdef TUN_DEBUG
12381da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
12391da177e4SLinus Torvalds 	tun->debug = value;
12401da177e4SLinus Torvalds #endif
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1246631ab46bSEric W. Biederman 	return !!tun->tfile;
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
12501da177e4SLinus Torvalds {
12511da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
12521da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
12581da177e4SLinus Torvalds 	if (data)
12591da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
12601da177e4SLinus Torvalds 	else
12611da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
12621da177e4SLinus Torvalds 	return 0;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12657282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
12661da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
12671da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
12681da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
12691da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
12701da177e4SLinus Torvalds 	.get_link	= tun_get_link,
12711da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
12721da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
12731da177e4SLinus Torvalds };
12741da177e4SLinus Torvalds 
127579d17604SPavel Emelyanov static int tun_init_net(struct net *net)
127679d17604SPavel Emelyanov {
127779d17604SPavel Emelyanov 	return 0;
127879d17604SPavel Emelyanov }
127979d17604SPavel Emelyanov 
128079d17604SPavel Emelyanov static void tun_exit_net(struct net *net)
128179d17604SPavel Emelyanov {
128274a3e5a7SEric W. Biederman 	struct net_device *dev, *next;
1283d647a591SPavel Emelyanov 
1284d647a591SPavel Emelyanov 	rtnl_lock();
128574a3e5a7SEric W. Biederman 	for_each_netdev_safe(net, dev, next) {
128674a3e5a7SEric W. Biederman 		if (dev->ethtool_ops != &tun_ethtool_ops)
128774a3e5a7SEric W. Biederman 			continue;
128874a3e5a7SEric W. Biederman 		DBG(KERN_INFO "%s cleaned up\n", dev->name);
128974a3e5a7SEric W. Biederman 		unregister_netdevice(dev);
1290d647a591SPavel Emelyanov 	}
1291d647a591SPavel Emelyanov 	rtnl_unlock();
129279d17604SPavel Emelyanov }
129379d17604SPavel Emelyanov 
129479d17604SPavel Emelyanov static struct pernet_operations tun_net_ops = {
129579d17604SPavel Emelyanov 	.init = tun_init_net,
129679d17604SPavel Emelyanov 	.exit = tun_exit_net,
129779d17604SPavel Emelyanov };
129879d17604SPavel Emelyanov 
12991da177e4SLinus Torvalds static int __init tun_init(void)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	int ret = 0;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
13041da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
13051da177e4SLinus Torvalds 
130674a3e5a7SEric W. Biederman 	ret = register_pernet_device(&tun_net_ops);
130779d17604SPavel Emelyanov 	if (ret) {
130879d17604SPavel Emelyanov 		printk(KERN_ERR "tun: Can't register pernet ops\n");
130979d17604SPavel Emelyanov 		goto err_pernet;
131079d17604SPavel Emelyanov 	}
131179d17604SPavel Emelyanov 
13121da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
131379d17604SPavel Emelyanov 	if (ret) {
13141da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
131579d17604SPavel Emelyanov 		goto err_misc;
131679d17604SPavel Emelyanov 	}
131779d17604SPavel Emelyanov 	return 0;
131879d17604SPavel Emelyanov 
131979d17604SPavel Emelyanov err_misc:
132074a3e5a7SEric W. Biederman 	unregister_pernet_device(&tun_net_ops);
132179d17604SPavel Emelyanov err_pernet:
13221da177e4SLinus Torvalds 	return ret;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds static void tun_cleanup(void)
13261da177e4SLinus Torvalds {
13271da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
132874a3e5a7SEric W. Biederman 	unregister_pernet_device(&tun_net_ops);
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds module_init(tun_init);
13321da177e4SLinus Torvalds module_exit(tun_cleanup);
13331da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
13341da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
13351da177e4SLinus Torvalds MODULE_LICENSE("GPL");
13361da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1337