xref: /openbmc/linux/drivers/net/tun.c (revision 758e43b74c903ad368ceced31601ba5d96f31238)
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 
9014daa021SRusty Russell struct tun_struct {
9114daa021SRusty Russell 	struct list_head        list;
92f271b2ccSMax Krasnyansky 	unsigned int 		flags;
9314daa021SRusty Russell 	int			attached;
9414daa021SRusty Russell 	uid_t			owner;
9514daa021SRusty Russell 	gid_t			group;
9614daa021SRusty Russell 
9714daa021SRusty Russell 	wait_queue_head_t	read_wait;
9814daa021SRusty Russell 	struct sk_buff_head	readq;
9914daa021SRusty Russell 
10014daa021SRusty Russell 	struct net_device	*dev;
10114daa021SRusty Russell 	struct fasync_struct	*fasync;
10214daa021SRusty Russell 
103f271b2ccSMax Krasnyansky 	struct tap_filter       txflt;
10414daa021SRusty Russell 
10514daa021SRusty Russell #ifdef TUN_DEBUG
10614daa021SRusty Russell 	int debug;
10714daa021SRusty Russell #endif
10814daa021SRusty Russell };
10914daa021SRusty Russell 
110f271b2ccSMax Krasnyansky /* TAP filterting */
111f271b2ccSMax Krasnyansky static void addr_hash_set(u32 *mask, const u8 *addr)
112f271b2ccSMax Krasnyansky {
113f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
114f271b2ccSMax Krasnyansky 	mask[n >> 5] |= (1 << (n & 31));
115f271b2ccSMax Krasnyansky }
116f271b2ccSMax Krasnyansky 
117f271b2ccSMax Krasnyansky static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
118f271b2ccSMax Krasnyansky {
119f271b2ccSMax Krasnyansky 	int n = ether_crc(ETH_ALEN, addr) >> 26;
120f271b2ccSMax Krasnyansky 	return mask[n >> 5] & (1 << (n & 31));
121f271b2ccSMax Krasnyansky }
122f271b2ccSMax Krasnyansky 
123f271b2ccSMax Krasnyansky static int update_filter(struct tap_filter *filter, void __user *arg)
124f271b2ccSMax Krasnyansky {
125f271b2ccSMax Krasnyansky 	struct { u8 u[ETH_ALEN]; } *addr;
126f271b2ccSMax Krasnyansky 	struct tun_filter uf;
127f271b2ccSMax Krasnyansky 	int err, alen, n, nexact;
128f271b2ccSMax Krasnyansky 
129f271b2ccSMax Krasnyansky 	if (copy_from_user(&uf, arg, sizeof(uf)))
130f271b2ccSMax Krasnyansky 		return -EFAULT;
131f271b2ccSMax Krasnyansky 
132f271b2ccSMax Krasnyansky 	if (!uf.count) {
133f271b2ccSMax Krasnyansky 		/* Disabled */
134f271b2ccSMax Krasnyansky 		filter->count = 0;
135f271b2ccSMax Krasnyansky 		return 0;
136f271b2ccSMax Krasnyansky 	}
137f271b2ccSMax Krasnyansky 
138f271b2ccSMax Krasnyansky 	alen = ETH_ALEN * uf.count;
139f271b2ccSMax Krasnyansky 	addr = kmalloc(alen, GFP_KERNEL);
140f271b2ccSMax Krasnyansky 	if (!addr)
141f271b2ccSMax Krasnyansky 		return -ENOMEM;
142f271b2ccSMax Krasnyansky 
143f271b2ccSMax Krasnyansky 	if (copy_from_user(addr, arg + sizeof(uf), alen)) {
144f271b2ccSMax Krasnyansky 		err = -EFAULT;
145f271b2ccSMax Krasnyansky 		goto done;
146f271b2ccSMax Krasnyansky 	}
147f271b2ccSMax Krasnyansky 
148f271b2ccSMax Krasnyansky 	/* The filter is updated without holding any locks. Which is
149f271b2ccSMax Krasnyansky 	 * perfectly safe. We disable it first and in the worst
150f271b2ccSMax Krasnyansky 	 * case we'll accept a few undesired packets. */
151f271b2ccSMax Krasnyansky 	filter->count = 0;
152f271b2ccSMax Krasnyansky 	wmb();
153f271b2ccSMax Krasnyansky 
154f271b2ccSMax Krasnyansky 	/* Use first set of addresses as an exact filter */
155f271b2ccSMax Krasnyansky 	for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
156f271b2ccSMax Krasnyansky 		memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
157f271b2ccSMax Krasnyansky 
158f271b2ccSMax Krasnyansky 	nexact = n;
159f271b2ccSMax Krasnyansky 
160f271b2ccSMax Krasnyansky 	/* The rest is hashed */
161f271b2ccSMax Krasnyansky 	memset(filter->mask, 0, sizeof(filter->mask));
162f271b2ccSMax Krasnyansky 	for (; n < uf.count; n++)
163f271b2ccSMax Krasnyansky 		addr_hash_set(filter->mask, addr[n].u);
164f271b2ccSMax Krasnyansky 
165f271b2ccSMax Krasnyansky 	/* For ALLMULTI just set the mask to all ones.
166f271b2ccSMax Krasnyansky 	 * This overrides the mask populated above. */
167f271b2ccSMax Krasnyansky 	if ((uf.flags & TUN_FLT_ALLMULTI))
168f271b2ccSMax Krasnyansky 		memset(filter->mask, ~0, sizeof(filter->mask));
169f271b2ccSMax Krasnyansky 
170f271b2ccSMax Krasnyansky 	/* Now enable the filter */
171f271b2ccSMax Krasnyansky 	wmb();
172f271b2ccSMax Krasnyansky 	filter->count = nexact;
173f271b2ccSMax Krasnyansky 
174f271b2ccSMax Krasnyansky 	/* Return the number of exact filters */
175f271b2ccSMax Krasnyansky 	err = nexact;
176f271b2ccSMax Krasnyansky 
177f271b2ccSMax Krasnyansky done:
178f271b2ccSMax Krasnyansky 	kfree(addr);
179f271b2ccSMax Krasnyansky 	return err;
180f271b2ccSMax Krasnyansky }
181f271b2ccSMax Krasnyansky 
182f271b2ccSMax Krasnyansky /* Returns: 0 - drop, !=0 - accept */
183f271b2ccSMax Krasnyansky static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
184f271b2ccSMax Krasnyansky {
185f271b2ccSMax Krasnyansky 	/* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
186f271b2ccSMax Krasnyansky 	 * at this point. */
187f271b2ccSMax Krasnyansky 	struct ethhdr *eh = (struct ethhdr *) skb->data;
188f271b2ccSMax Krasnyansky 	int i;
189f271b2ccSMax Krasnyansky 
190f271b2ccSMax Krasnyansky 	/* Exact match */
191f271b2ccSMax Krasnyansky 	for (i = 0; i < filter->count; i++)
192f271b2ccSMax Krasnyansky 		if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
193f271b2ccSMax Krasnyansky 			return 1;
194f271b2ccSMax Krasnyansky 
195f271b2ccSMax Krasnyansky 	/* Inexact match (multicast only) */
196f271b2ccSMax Krasnyansky 	if (is_multicast_ether_addr(eh->h_dest))
197f271b2ccSMax Krasnyansky 		return addr_hash_test(filter->mask, eh->h_dest);
198f271b2ccSMax Krasnyansky 
199f271b2ccSMax Krasnyansky 	return 0;
200f271b2ccSMax Krasnyansky }
201f271b2ccSMax Krasnyansky 
202f271b2ccSMax Krasnyansky /*
203f271b2ccSMax Krasnyansky  * Checks whether the packet is accepted or not.
204f271b2ccSMax Krasnyansky  * Returns: 0 - drop, !=0 - accept
205f271b2ccSMax Krasnyansky  */
206f271b2ccSMax Krasnyansky static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
207f271b2ccSMax Krasnyansky {
208f271b2ccSMax Krasnyansky 	if (!filter->count)
209f271b2ccSMax Krasnyansky 		return 1;
210f271b2ccSMax Krasnyansky 
211f271b2ccSMax Krasnyansky 	return run_filter(filter, skb);
212f271b2ccSMax Krasnyansky }
213f271b2ccSMax Krasnyansky 
2141da177e4SLinus Torvalds /* Network device part of the driver */
2151da177e4SLinus Torvalds 
21679d17604SPavel Emelyanov static unsigned int tun_net_id;
21779d17604SPavel Emelyanov struct tun_net {
21879d17604SPavel Emelyanov 	struct list_head dev_list;
21979d17604SPavel Emelyanov };
22079d17604SPavel Emelyanov 
2217282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /* Net device open. */
2241da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	netif_start_queue(dev);
2271da177e4SLinus Torvalds 	return 0;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /* Net device close. */
2311da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	netif_stop_queue(dev);
2341da177e4SLinus Torvalds 	return 0;
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds /* Net device start xmit */
2381da177e4SLinus Torvalds static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
2451da177e4SLinus Torvalds 	if (!tun->attached)
2461da177e4SLinus Torvalds 		goto drop;
2471da177e4SLinus Torvalds 
248f271b2ccSMax Krasnyansky 	/* Drop if the filter does not like it.
249f271b2ccSMax Krasnyansky 	 * This is a noop if the filter is disabled.
250f271b2ccSMax Krasnyansky 	 * Filter can be enabled only for the TAP devices. */
251f271b2ccSMax Krasnyansky 	if (!check_filter(&tun->txflt, skb))
252f271b2ccSMax Krasnyansky 		goto drop;
253f271b2ccSMax Krasnyansky 
2541da177e4SLinus Torvalds 	if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
2551da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
2561da177e4SLinus Torvalds 			/* Normal queueing mode. */
2571da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
2581da177e4SLinus Torvalds 			netif_stop_queue(dev);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
2611da177e4SLinus Torvalds 			 * error is more appropriate. */
26209f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
2631da177e4SLinus Torvalds 		} else {
2641da177e4SLinus Torvalds 			/* Single queue mode.
2651da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
2661da177e4SLinus Torvalds 			goto drop;
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
270f271b2ccSMax Krasnyansky 	/* Enqueue packet */
2711da177e4SLinus Torvalds 	skb_queue_tail(&tun->readq, skb);
2721da177e4SLinus Torvalds 	dev->trans_start = jiffies;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/* Notify and wake up reader process */
2751da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
2761da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
2771da177e4SLinus Torvalds 	wake_up_interruptible(&tun->read_wait);
2781da177e4SLinus Torvalds 	return 0;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds drop:
28109f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
2821da177e4SLinus Torvalds 	kfree_skb(skb);
2831da177e4SLinus Torvalds 	return 0;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
286f271b2ccSMax Krasnyansky static void tun_net_mclist(struct net_device *dev)
2871da177e4SLinus Torvalds {
288f271b2ccSMax Krasnyansky 	/*
289f271b2ccSMax Krasnyansky 	 * This callback is supposed to deal with mc filter in
290f271b2ccSMax Krasnyansky 	 * _rx_ path and has nothing to do with the _tx_ path.
291f271b2ccSMax Krasnyansky 	 * In rx path we always accept everything userspace gives us.
292f271b2ccSMax Krasnyansky 	 */
293f271b2ccSMax Krasnyansky 	return;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2964885a504SEd Swierk #define MIN_MTU 68
2974885a504SEd Swierk #define MAX_MTU 65535
2984885a504SEd Swierk 
2994885a504SEd Swierk static int
3004885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
3014885a504SEd Swierk {
3024885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
3034885a504SEd Swierk 		return -EINVAL;
3044885a504SEd Swierk 	dev->mtu = new_mtu;
3054885a504SEd Swierk 	return 0;
3064885a504SEd Swierk }
3074885a504SEd Swierk 
308*758e43b7SStephen Hemminger static const struct net_device_ops tun_netdev_ops = {
309*758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
310*758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
311*758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
312*758e43b7SStephen Hemminger 
313*758e43b7SStephen Hemminger };
314*758e43b7SStephen Hemminger 
315*758e43b7SStephen Hemminger static const struct net_device_ops tap_netdev_ops = {
316*758e43b7SStephen Hemminger 	.ndo_open		= tun_net_open,
317*758e43b7SStephen Hemminger 	.ndo_stop		= tun_net_close,
318*758e43b7SStephen Hemminger 	.ndo_change_mtu		= tun_net_change_mtu,
319*758e43b7SStephen Hemminger 	.ndo_set_multicast_list	= tun_net_mclist,
320*758e43b7SStephen Hemminger 	.ndo_set_mac_address	= eth_mac_addr,
321*758e43b7SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
322*758e43b7SStephen Hemminger };
323*758e43b7SStephen Hemminger 
3241da177e4SLinus Torvalds /* Initialize net device. */
3251da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
3301da177e4SLinus Torvalds 	case TUN_TUN_DEV:
331*758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
332*758e43b7SStephen Hemminger 
3331da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
3341da177e4SLinus Torvalds 		dev->hard_header_len = 0;
3351da177e4SLinus Torvalds 		dev->addr_len = 0;
3361da177e4SLinus Torvalds 		dev->mtu = 1500;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		/* Zero header length */
3391da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
3401da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3411da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
3421da177e4SLinus Torvalds 		break;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	case TUN_TAP_DEV:
345*758e43b7SStephen Hemminger 		dev->netdev_ops = &tun_netdev_ops;
3461da177e4SLinus Torvalds 		/* Ethernet TAP Device */
3471da177e4SLinus Torvalds 		ether_setup(dev);
34836226a8dSBrian Braunstein 
349f271b2ccSMax Krasnyansky 		random_ether_addr(dev->dev_addr);
35036226a8dSBrian Braunstein 
3511da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
3521da177e4SLinus Torvalds 		break;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds /* Character device part */
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds /* Poll */
3591da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
3621da177e4SLinus Torvalds 	unsigned int mask = POLLOUT | POLLWRNORM;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (!tun)
3651da177e4SLinus Torvalds 		return -EBADFD;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	poll_wait(file, &tun->read_wait, wait);
3701da177e4SLinus Torvalds 
371b03efcfbSDavid S. Miller 	if (!skb_queue_empty(&tun->readq))
3721da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	return mask;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
377f42157cbSRusty Russell /* prepad is the amount to reserve at front.  len is length after that.
378f42157cbSRusty Russell  * linear is a hint as to how much to copy (usually headers). */
379f42157cbSRusty Russell static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear,
380f42157cbSRusty Russell 				     gfp_t gfp)
381f42157cbSRusty Russell {
382f42157cbSRusty Russell 	struct sk_buff *skb;
383f42157cbSRusty Russell 	unsigned int i;
384f42157cbSRusty Russell 
385f42157cbSRusty Russell 	skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
386f42157cbSRusty Russell 	if (skb) {
387f42157cbSRusty Russell 		skb_reserve(skb, prepad);
388f42157cbSRusty Russell 		skb_put(skb, len);
389f42157cbSRusty Russell 		return skb;
390f42157cbSRusty Russell 	}
391f42157cbSRusty Russell 
392f42157cbSRusty Russell 	/* Under a page?  Don't bother with paged skb. */
393f42157cbSRusty Russell 	if (prepad + len < PAGE_SIZE)
394f42157cbSRusty Russell 		return NULL;
395f42157cbSRusty Russell 
396f42157cbSRusty Russell 	/* Start with a normal skb, and add pages. */
397f42157cbSRusty Russell 	skb = alloc_skb(prepad + linear, gfp);
398f42157cbSRusty Russell 	if (!skb)
399f42157cbSRusty Russell 		return NULL;
400f42157cbSRusty Russell 
401f42157cbSRusty Russell 	skb_reserve(skb, prepad);
402f42157cbSRusty Russell 	skb_put(skb, linear);
403f42157cbSRusty Russell 
404f42157cbSRusty Russell 	len -= linear;
405f42157cbSRusty Russell 
406f42157cbSRusty Russell 	for (i = 0; i < MAX_SKB_FRAGS; i++) {
407f42157cbSRusty Russell 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
408f42157cbSRusty Russell 
409f42157cbSRusty Russell 		f->page = alloc_page(gfp|__GFP_ZERO);
410f42157cbSRusty Russell 		if (!f->page)
411f42157cbSRusty Russell 			break;
412f42157cbSRusty Russell 
413f42157cbSRusty Russell 		f->page_offset = 0;
414f42157cbSRusty Russell 		f->size = PAGE_SIZE;
415f42157cbSRusty Russell 
416f42157cbSRusty Russell 		skb->data_len += PAGE_SIZE;
417f42157cbSRusty Russell 		skb->len += PAGE_SIZE;
418f42157cbSRusty Russell 		skb->truesize += PAGE_SIZE;
419f42157cbSRusty Russell 		skb_shinfo(skb)->nr_frags++;
420f42157cbSRusty Russell 
421f42157cbSRusty Russell 		if (len < PAGE_SIZE) {
422f42157cbSRusty Russell 			len = 0;
423f42157cbSRusty Russell 			break;
424f42157cbSRusty Russell 		}
425f42157cbSRusty Russell 		len -= PAGE_SIZE;
426f42157cbSRusty Russell 	}
427f42157cbSRusty Russell 
428f42157cbSRusty Russell 	/* Too large, or alloc fail? */
429f42157cbSRusty Russell 	if (unlikely(len)) {
430f42157cbSRusty Russell 		kfree_skb(skb);
431f42157cbSRusty Russell 		skb = NULL;
432f42157cbSRusty Russell 	}
433f42157cbSRusty Russell 
434f42157cbSRusty Russell 	return skb;
435f42157cbSRusty Russell }
436f42157cbSRusty Russell 
4371da177e4SLinus Torvalds /* Get packet from user space buffer */
4381da177e4SLinus Torvalds static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
4411da177e4SLinus Torvalds 	struct sk_buff *skb;
4421da177e4SLinus Torvalds 	size_t len = count, align = 0;
443f43798c2SRusty Russell 	struct virtio_net_hdr gso = { 0 };
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
4461da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
4471da177e4SLinus Torvalds 			return -EINVAL;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 		if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
4501da177e4SLinus Torvalds 			return -EFAULT;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
453f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
454f43798c2SRusty Russell 		if ((len -= sizeof(gso)) > count)
455f43798c2SRusty Russell 			return -EINVAL;
456f43798c2SRusty Russell 
457f43798c2SRusty Russell 		if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso)))
458f43798c2SRusty Russell 			return -EFAULT;
459f43798c2SRusty Russell 
460f43798c2SRusty Russell 		if (gso.hdr_len > len)
461f43798c2SRusty Russell 			return -EINVAL;
462f43798c2SRusty Russell 	}
463f43798c2SRusty Russell 
464e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
4651da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
466e01bf1c8SRusty Russell 		if (unlikely(len < ETH_HLEN))
467e01bf1c8SRusty Russell 			return -EINVAL;
468e01bf1c8SRusty Russell 	}
4691da177e4SLinus Torvalds 
470f42157cbSRusty Russell 	if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) {
47109f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
4721da177e4SLinus Torvalds 		return -ENOMEM;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 
475f42157cbSRusty Russell 	if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
47609f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
4778f22757eSDave Jones 		kfree_skb(skb);
4781da177e4SLinus Torvalds 		return -EFAULT;
4798f22757eSDave Jones 	}
4801da177e4SLinus Torvalds 
481f43798c2SRusty Russell 	if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
482f43798c2SRusty Russell 		if (!skb_partial_csum_set(skb, gso.csum_start,
483f43798c2SRusty Russell 					  gso.csum_offset)) {
484f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
485f43798c2SRusty Russell 			kfree_skb(skb);
486f43798c2SRusty Russell 			return -EINVAL;
487f43798c2SRusty Russell 		}
488f43798c2SRusty Russell 	} else if (tun->flags & TUN_NOCHECKSUM)
489f43798c2SRusty Russell 		skb->ip_summed = CHECKSUM_UNNECESSARY;
490f43798c2SRusty Russell 
4911da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
4921da177e4SLinus Torvalds 	case TUN_TUN_DEV:
493f09f7ee2SAng Way Chuang 		if (tun->flags & TUN_NO_PI) {
494f09f7ee2SAng Way Chuang 			switch (skb->data[0] & 0xf0) {
495f09f7ee2SAng Way Chuang 			case 0x40:
496f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IP);
497f09f7ee2SAng Way Chuang 				break;
498f09f7ee2SAng Way Chuang 			case 0x60:
499f09f7ee2SAng Way Chuang 				pi.proto = htons(ETH_P_IPV6);
500f09f7ee2SAng Way Chuang 				break;
501f09f7ee2SAng Way Chuang 			default:
502f09f7ee2SAng Way Chuang 				tun->dev->stats.rx_dropped++;
503f09f7ee2SAng Way Chuang 				kfree_skb(skb);
504f09f7ee2SAng Way Chuang 				return -EINVAL;
505f09f7ee2SAng Way Chuang 			}
506f09f7ee2SAng Way Chuang 		}
507f09f7ee2SAng Way Chuang 
508459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
5091da177e4SLinus Torvalds 		skb->protocol = pi.proto;
5104c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
5111da177e4SLinus Torvalds 		break;
5121da177e4SLinus Torvalds 	case TUN_TAP_DEV:
5131da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
5141da177e4SLinus Torvalds 		break;
5151da177e4SLinus Torvalds 	};
5161da177e4SLinus Torvalds 
517f43798c2SRusty Russell 	if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
518f43798c2SRusty Russell 		pr_debug("GSO!\n");
519f43798c2SRusty Russell 		switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
520f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
521f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
522f43798c2SRusty Russell 			break;
523f43798c2SRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
524f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
525f43798c2SRusty Russell 			break;
526f43798c2SRusty Russell 		default:
527f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
528f43798c2SRusty Russell 			kfree_skb(skb);
529f43798c2SRusty Russell 			return -EINVAL;
530f43798c2SRusty Russell 		}
531f43798c2SRusty Russell 
532f43798c2SRusty Russell 		if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
533f43798c2SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
534f43798c2SRusty Russell 
535f43798c2SRusty Russell 		skb_shinfo(skb)->gso_size = gso.gso_size;
536f43798c2SRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
537f43798c2SRusty Russell 			tun->dev->stats.rx_frame_errors++;
538f43798c2SRusty Russell 			kfree_skb(skb);
539f43798c2SRusty Russell 			return -EINVAL;
540f43798c2SRusty Russell 		}
541f43798c2SRusty Russell 
542f43798c2SRusty Russell 		/* Header must be checked, and gso_segs computed. */
543f43798c2SRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
544f43798c2SRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
545f43798c2SRusty Russell 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	netif_rx_ni(skb);
5481da177e4SLinus Torvalds 
54909f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
55009f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	return count;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
555ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
556ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
5571da177e4SLinus Torvalds {
558ee0b3e67SBadari Pulavarty 	struct tun_struct *tun = iocb->ki_filp->private_data;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	if (!tun)
5611da177e4SLinus Torvalds 		return -EBADFD;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
5641da177e4SLinus Torvalds 
56552427c9dSAkinobu Mita 	return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds /* Put packet to the user space buffer */
5691da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
5701da177e4SLinus Torvalds 				       struct sk_buff *skb,
5711da177e4SLinus Torvalds 				       struct iovec *iv, int len)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
5741da177e4SLinus Torvalds 	ssize_t total = 0;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
5771da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
5781da177e4SLinus Torvalds 			return -EINVAL;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 		if (len < skb->len) {
5811da177e4SLinus Torvalds 			/* Packet will be striped */
5821da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
5831da177e4SLinus Torvalds 		}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 		if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
5861da177e4SLinus Torvalds 			return -EFAULT;
5871da177e4SLinus Torvalds 		total += sizeof(pi);
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds 
590f43798c2SRusty Russell 	if (tun->flags & TUN_VNET_HDR) {
591f43798c2SRusty Russell 		struct virtio_net_hdr gso = { 0 }; /* no info leak */
592f43798c2SRusty Russell 		if ((len -= sizeof(gso)) < 0)
593f43798c2SRusty Russell 			return -EINVAL;
594f43798c2SRusty Russell 
595f43798c2SRusty Russell 		if (skb_is_gso(skb)) {
596f43798c2SRusty Russell 			struct skb_shared_info *sinfo = skb_shinfo(skb);
597f43798c2SRusty Russell 
598f43798c2SRusty Russell 			/* This is a hint as to how much should be linear. */
599f43798c2SRusty Russell 			gso.hdr_len = skb_headlen(skb);
600f43798c2SRusty Russell 			gso.gso_size = sinfo->gso_size;
601f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCPV4)
602f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
603f43798c2SRusty Russell 			else if (sinfo->gso_type & SKB_GSO_TCPV6)
604f43798c2SRusty Russell 				gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
605f43798c2SRusty Russell 			else
606f43798c2SRusty Russell 				BUG();
607f43798c2SRusty Russell 			if (sinfo->gso_type & SKB_GSO_TCP_ECN)
608f43798c2SRusty Russell 				gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
609f43798c2SRusty Russell 		} else
610f43798c2SRusty Russell 			gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
611f43798c2SRusty Russell 
612f43798c2SRusty Russell 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
613f43798c2SRusty Russell 			gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
614f43798c2SRusty Russell 			gso.csum_start = skb->csum_start - skb_headroom(skb);
615f43798c2SRusty Russell 			gso.csum_offset = skb->csum_offset;
616f43798c2SRusty Russell 		} /* else everything is zero */
617f43798c2SRusty Russell 
618f43798c2SRusty Russell 		if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso))))
619f43798c2SRusty Russell 			return -EFAULT;
620f43798c2SRusty Russell 		total += sizeof(gso);
621f43798c2SRusty Russell 	}
622f43798c2SRusty Russell 
6231da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	skb_copy_datagram_iovec(skb, 0, iv, len);
6261da177e4SLinus Torvalds 	total += len;
6271da177e4SLinus Torvalds 
62809f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
62909f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	return total;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
634ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
635ee0b3e67SBadari Pulavarty 			    unsigned long count, loff_t pos)
6361da177e4SLinus Torvalds {
637ee0b3e67SBadari Pulavarty 	struct file *file = iocb->ki_filp;
6381da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
6391da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
6401da177e4SLinus Torvalds 	struct sk_buff *skb;
6411da177e4SLinus Torvalds 	ssize_t len, ret = 0;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	if (!tun)
6441da177e4SLinus Torvalds 		return -EBADFD;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
6471da177e4SLinus Torvalds 
64852427c9dSAkinobu Mita 	len = iov_length(iv, count);
6491da177e4SLinus Torvalds 	if (len < 0)
6501da177e4SLinus Torvalds 		return -EINVAL;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	add_wait_queue(&tun->read_wait, &wait);
6531da177e4SLinus Torvalds 	while (len) {
6541da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 		/* Read frames from the queue */
6571da177e4SLinus Torvalds 		if (!(skb=skb_dequeue(&tun->readq))) {
6581da177e4SLinus Torvalds 			if (file->f_flags & O_NONBLOCK) {
6591da177e4SLinus Torvalds 				ret = -EAGAIN;
6601da177e4SLinus Torvalds 				break;
6611da177e4SLinus Torvalds 			}
6621da177e4SLinus Torvalds 			if (signal_pending(current)) {
6631da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
6641da177e4SLinus Torvalds 				break;
6651da177e4SLinus Torvalds 			}
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
6681da177e4SLinus Torvalds 			schedule();
6691da177e4SLinus Torvalds 			continue;
6701da177e4SLinus Torvalds 		}
6711da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
6741da177e4SLinus Torvalds 		kfree_skb(skb);
6751da177e4SLinus Torvalds 		break;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
6791da177e4SLinus Torvalds 	remove_wait_queue(&tun->read_wait, &wait);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	return ret;
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	skb_queue_head_init(&tun->readq);
6891da177e4SLinus Torvalds 	init_waitqueue_head(&tun->read_wait);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	tun->owner = -1;
6928c644623SGuido Guenther 	tun->group = -1;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	dev->hard_start_xmit = tun_net_xmit;
6951da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
6961da177e4SLinus Torvalds 	dev->destructor = free_netdev;
697fc54c658SPavel Emelyanov 	dev->features |= NETIF_F_NETNS_LOCAL;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
700d647a591SPavel Emelyanov static struct tun_struct *tun_get_by_name(struct tun_net *tn, const char *name)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	struct tun_struct *tun;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	ASSERT_RTNL();
705d647a591SPavel Emelyanov 	list_for_each_entry(tun, &tn->dev_list, list) {
7061da177e4SLinus Torvalds 		if (!strncmp(tun->dev->name, name, IFNAMSIZ))
7071da177e4SLinus Torvalds 		    return tun;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	return NULL;
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
713d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
7141da177e4SLinus Torvalds {
715d647a591SPavel Emelyanov 	struct tun_net *tn;
7161da177e4SLinus Torvalds 	struct tun_struct *tun;
7171da177e4SLinus Torvalds 	struct net_device *dev;
7181da177e4SLinus Torvalds 	int err;
7191da177e4SLinus Torvalds 
720d647a591SPavel Emelyanov 	tn = net_generic(net, tun_net_id);
721d647a591SPavel Emelyanov 	tun = tun_get_by_name(tn, ifr->ifr_name);
7221da177e4SLinus Torvalds 	if (tun) {
7231da177e4SLinus Torvalds 		if (tun->attached)
7241da177e4SLinus Torvalds 			return -EBUSY;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		/* Check permissions */
7278c644623SGuido Guenther 		if (((tun->owner != -1 &&
7288c644623SGuido Guenther 		      current->euid != tun->owner) ||
7298c644623SGuido Guenther 		     (tun->group != -1 &&
7308c644623SGuido Guenther 		      current->egid != tun->group)) &&
7318c644623SGuido Guenther 		     !capable(CAP_NET_ADMIN))
7321da177e4SLinus Torvalds 			return -EPERM;
7331da177e4SLinus Torvalds 	}
734d647a591SPavel Emelyanov 	else if (__dev_get_by_name(net, ifr->ifr_name))
7351da177e4SLinus Torvalds 		return -EINVAL;
7361da177e4SLinus Torvalds 	else {
7371da177e4SLinus Torvalds 		char *name;
7381da177e4SLinus Torvalds 		unsigned long flags = 0;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 		err = -EINVAL;
7411da177e4SLinus Torvalds 
742ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
743ca6bb5d7SDavid Woodhouse 			return -EPERM;
744ca6bb5d7SDavid Woodhouse 
7451da177e4SLinus Torvalds 		/* Set dev type */
7461da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
7471da177e4SLinus Torvalds 			/* TUN device */
7481da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
7491da177e4SLinus Torvalds 			name = "tun%d";
7501da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
7511da177e4SLinus Torvalds 			/* TAP device */
7521da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
7531da177e4SLinus Torvalds 			name = "tap%d";
7541da177e4SLinus Torvalds 		} else
7551da177e4SLinus Torvalds 			goto failed;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 		if (*ifr->ifr_name)
7581da177e4SLinus Torvalds 			name = ifr->ifr_name;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
7611da177e4SLinus Torvalds 				   tun_setup);
7621da177e4SLinus Torvalds 		if (!dev)
7631da177e4SLinus Torvalds 			return -ENOMEM;
7641da177e4SLinus Torvalds 
765fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
766*758e43b7SStephen Hemminger 
7671da177e4SLinus Torvalds 		tun = netdev_priv(dev);
7681da177e4SLinus Torvalds 		tun->dev = dev;
7691da177e4SLinus Torvalds 		tun->flags = flags;
770f271b2ccSMax Krasnyansky 		tun->txflt.count = 0;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		tun_net_init(dev);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
7751da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
7761da177e4SLinus Torvalds 			if (err < 0)
7771da177e4SLinus Torvalds 				goto err_free_dev;
7781da177e4SLinus Torvalds 		}
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
7811da177e4SLinus Torvalds 		if (err < 0)
7821da177e4SLinus Torvalds 			goto err_free_dev;
7831da177e4SLinus Torvalds 
784d647a591SPavel Emelyanov 		list_add(&tun->list, &tn->dev_list);
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
7901da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
791a26af1e0SNathaniel Filardo 	else
792a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
7951da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
796a26af1e0SNathaniel Filardo 	else
797a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
7981da177e4SLinus Torvalds 
799f43798c2SRusty Russell 	if (ifr->ifr_flags & IFF_VNET_HDR)
800f43798c2SRusty Russell 		tun->flags |= TUN_VNET_HDR;
801f43798c2SRusty Russell 	else
802f43798c2SRusty Russell 		tun->flags &= ~TUN_VNET_HDR;
803f43798c2SRusty Russell 
8041da177e4SLinus Torvalds 	file->private_data = tun;
8051da177e4SLinus Torvalds 	tun->attached = 1;
806fc54c658SPavel Emelyanov 	get_net(dev_net(tun->dev));
8071da177e4SLinus Torvalds 
808e35259a9SMax Krasnyansky 	/* Make sure persistent devices do not get stuck in
809e35259a9SMax Krasnyansky 	 * xoff state.
810e35259a9SMax Krasnyansky 	 */
811e35259a9SMax Krasnyansky 	if (netif_running(tun->dev))
812e35259a9SMax Krasnyansky 		netif_wake_queue(tun->dev);
813e35259a9SMax Krasnyansky 
8141da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
8151da177e4SLinus Torvalds 	return 0;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds  err_free_dev:
8181da177e4SLinus Torvalds 	free_netdev(dev);
8191da177e4SLinus Torvalds  failed:
8201da177e4SLinus Torvalds 	return err;
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
823e3b99556SMark McLoughlin static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
824e3b99556SMark McLoughlin {
825e3b99556SMark McLoughlin 	struct tun_struct *tun = file->private_data;
826e3b99556SMark McLoughlin 
827e3b99556SMark McLoughlin 	if (!tun)
828e3b99556SMark McLoughlin 		return -EBADFD;
829e3b99556SMark McLoughlin 
830e3b99556SMark McLoughlin 	DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name);
831e3b99556SMark McLoughlin 
832e3b99556SMark McLoughlin 	strcpy(ifr->ifr_name, tun->dev->name);
833e3b99556SMark McLoughlin 
834e3b99556SMark McLoughlin 	ifr->ifr_flags = 0;
835e3b99556SMark McLoughlin 
836e3b99556SMark McLoughlin 	if (ifr->ifr_flags & TUN_TUN_DEV)
837e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_TUN;
838e3b99556SMark McLoughlin 	else
839e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_TAP;
840e3b99556SMark McLoughlin 
841e3b99556SMark McLoughlin 	if (tun->flags & TUN_NO_PI)
842e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_NO_PI;
843e3b99556SMark McLoughlin 
844e3b99556SMark McLoughlin 	if (tun->flags & TUN_ONE_QUEUE)
845e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_ONE_QUEUE;
846e3b99556SMark McLoughlin 
847e3b99556SMark McLoughlin 	if (tun->flags & TUN_VNET_HDR)
848e3b99556SMark McLoughlin 		ifr->ifr_flags |= IFF_VNET_HDR;
849e3b99556SMark McLoughlin 
850e3b99556SMark McLoughlin 	return 0;
851e3b99556SMark McLoughlin }
852e3b99556SMark McLoughlin 
8535228ddc9SRusty Russell /* This is like a cut-down ethtool ops, except done via tun fd so no
8545228ddc9SRusty Russell  * privs required. */
8555228ddc9SRusty Russell static int set_offload(struct net_device *dev, unsigned long arg)
8565228ddc9SRusty Russell {
8575228ddc9SRusty Russell 	unsigned int old_features, features;
8585228ddc9SRusty Russell 
8595228ddc9SRusty Russell 	old_features = dev->features;
8605228ddc9SRusty Russell 	/* Unset features, set them as we chew on the arg. */
8615228ddc9SRusty Russell 	features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST
8625228ddc9SRusty Russell 				    |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6));
8635228ddc9SRusty Russell 
8645228ddc9SRusty Russell 	if (arg & TUN_F_CSUM) {
8655228ddc9SRusty Russell 		features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
8665228ddc9SRusty Russell 		arg &= ~TUN_F_CSUM;
8675228ddc9SRusty Russell 
8685228ddc9SRusty Russell 		if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
8695228ddc9SRusty Russell 			if (arg & TUN_F_TSO_ECN) {
8705228ddc9SRusty Russell 				features |= NETIF_F_TSO_ECN;
8715228ddc9SRusty Russell 				arg &= ~TUN_F_TSO_ECN;
8725228ddc9SRusty Russell 			}
8735228ddc9SRusty Russell 			if (arg & TUN_F_TSO4)
8745228ddc9SRusty Russell 				features |= NETIF_F_TSO;
8755228ddc9SRusty Russell 			if (arg & TUN_F_TSO6)
8765228ddc9SRusty Russell 				features |= NETIF_F_TSO6;
8775228ddc9SRusty Russell 			arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
8785228ddc9SRusty Russell 		}
8795228ddc9SRusty Russell 	}
8805228ddc9SRusty Russell 
8815228ddc9SRusty Russell 	/* This gives the user a way to test for new features in future by
8825228ddc9SRusty Russell 	 * trying to set them. */
8835228ddc9SRusty Russell 	if (arg)
8845228ddc9SRusty Russell 		return -EINVAL;
8855228ddc9SRusty Russell 
8865228ddc9SRusty Russell 	dev->features = features;
8875228ddc9SRusty Russell 	if (old_features != dev->features)
8885228ddc9SRusty Russell 		netdev_features_change(dev);
8895228ddc9SRusty Russell 
8905228ddc9SRusty Russell 	return 0;
8915228ddc9SRusty Russell }
8925228ddc9SRusty Russell 
8931da177e4SLinus Torvalds static int tun_chr_ioctl(struct inode *inode, struct file *file,
8941da177e4SLinus Torvalds 			 unsigned int cmd, unsigned long arg)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
8971da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
8981da177e4SLinus Torvalds 	struct ifreq ifr;
899f271b2ccSMax Krasnyansky 	int ret;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
9021da177e4SLinus Torvalds 		if (copy_from_user(&ifr, argp, sizeof ifr))
9031da177e4SLinus Torvalds 			return -EFAULT;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
9061da177e4SLinus Torvalds 		int err;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 		rtnl_lock();
911d647a591SPavel Emelyanov 		err = tun_set_iff(current->nsproxy->net_ns, file, &ifr);
9121da177e4SLinus Torvalds 		rtnl_unlock();
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		if (err)
9151da177e4SLinus Torvalds 			return err;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
9181da177e4SLinus Torvalds 			return -EFAULT;
9191da177e4SLinus Torvalds 		return 0;
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 
92207240fd0SRusty Russell 	if (cmd == TUNGETFEATURES) {
92307240fd0SRusty Russell 		/* Currently this just means: "what IFF flags are valid?".
92407240fd0SRusty Russell 		 * This is needed because we never checked for invalid flags on
92507240fd0SRusty Russell 		 * TUNSETIFF. */
926f43798c2SRusty Russell 		return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
927f43798c2SRusty Russell 				IFF_VNET_HDR,
92807240fd0SRusty Russell 				(unsigned int __user*)argp);
92907240fd0SRusty Russell 	}
93007240fd0SRusty Russell 
9311da177e4SLinus Torvalds 	if (!tun)
9321da177e4SLinus Torvalds 		return -EBADFD;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	switch (cmd) {
937e3b99556SMark McLoughlin 	case TUNGETIFF:
938e3b99556SMark McLoughlin 		ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr);
939e3b99556SMark McLoughlin 		if (ret)
940e3b99556SMark McLoughlin 			return ret;
941e3b99556SMark McLoughlin 
942e3b99556SMark McLoughlin 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
943e3b99556SMark McLoughlin 			return -EFAULT;
944e3b99556SMark McLoughlin 		break;
945e3b99556SMark McLoughlin 
9461da177e4SLinus Torvalds 	case TUNSETNOCSUM:
9471da177e4SLinus Torvalds 		/* Disable/Enable checksum */
9481da177e4SLinus Torvalds 		if (arg)
9491da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
9501da177e4SLinus Torvalds 		else
9511da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
9541da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
9551da177e4SLinus Torvalds 		break;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	case TUNSETPERSIST:
9581da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
9591da177e4SLinus Torvalds 		if (arg)
9601da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
9611da177e4SLinus Torvalds 		else
9621da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
965c6e991deSToyo Abe 		    tun->dev->name, arg ? "enabled" : "disabled");
9661da177e4SLinus Torvalds 		break;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	case TUNSETOWNER:
9691da177e4SLinus Torvalds 		/* Set owner of the device */
9701da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
9731da177e4SLinus Torvalds 		break;
9741da177e4SLinus Torvalds 
9758c644623SGuido Guenther 	case TUNSETGROUP:
9768c644623SGuido Guenther 		/* Set group of the device */
9778c644623SGuido Guenther 		tun->group= (gid_t) arg;
9788c644623SGuido Guenther 
9798c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
9808c644623SGuido Guenther 		break;
9818c644623SGuido Guenther 
982ff4cc3acSMike Kershaw 	case TUNSETLINK:
983ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
98448abfe05SDavid S. Miller 		rtnl_lock();
985ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
986ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
987ff4cc3acSMike Kershaw 				tun->dev->name);
98848abfe05SDavid S. Miller 			ret = -EBUSY;
989ff4cc3acSMike Kershaw 		} else {
990ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
991ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
99248abfe05SDavid S. Miller 			ret = 0;
993ff4cc3acSMike Kershaw 		}
99448abfe05SDavid S. Miller 		rtnl_unlock();
99548abfe05SDavid S. Miller 		return ret;
996ff4cc3acSMike Kershaw 
9971da177e4SLinus Torvalds #ifdef TUN_DEBUG
9981da177e4SLinus Torvalds 	case TUNSETDEBUG:
9991da177e4SLinus Torvalds 		tun->debug = arg;
10001da177e4SLinus Torvalds 		break;
10011da177e4SLinus Torvalds #endif
10025228ddc9SRusty Russell 	case TUNSETOFFLOAD:
10035228ddc9SRusty Russell 		rtnl_lock();
10045228ddc9SRusty Russell 		ret = set_offload(tun->dev, arg);
10055228ddc9SRusty Russell 		rtnl_unlock();
10065228ddc9SRusty Russell 		return ret;
10075228ddc9SRusty Russell 
1008f271b2ccSMax Krasnyansky 	case TUNSETTXFILTER:
1009f271b2ccSMax Krasnyansky 		/* Can be set only for TAPs */
1010f271b2ccSMax Krasnyansky 		if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1011f271b2ccSMax Krasnyansky 			return -EINVAL;
1012f271b2ccSMax Krasnyansky 		rtnl_lock();
1013c0e5a8c2SHarvey Harrison 		ret = update_filter(&tun->txflt, (void __user *)arg);
1014f271b2ccSMax Krasnyansky 		rtnl_unlock();
1015f271b2ccSMax Krasnyansky 		return ret;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
1018f271b2ccSMax Krasnyansky 		/* Get hw addres */
1019f271b2ccSMax Krasnyansky 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1020f271b2ccSMax Krasnyansky 		ifr.ifr_hwaddr.sa_family = tun->dev->type;
10211da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof ifr))
10221da177e4SLinus Torvalds 			return -EFAULT;
10231da177e4SLinus Torvalds 		return 0;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
1026f271b2ccSMax Krasnyansky 		/* Set hw address */
1027e174961cSJohannes Berg 		DBG(KERN_DEBUG "%s: set hw address: %pM\n",
1028e174961cSJohannes Berg 			tun->dev->name, ifr.ifr_hwaddr.sa_data);
102940102371SKim B. Heino 
103040102371SKim B. Heino 		rtnl_lock();
103140102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
103240102371SKim B. Heino 		rtnl_unlock();
103336226a8dSBrian Braunstein 		return ret;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	default:
10361da177e4SLinus Torvalds 		return -EINVAL;
10371da177e4SLinus Torvalds 	};
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	return 0;
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
10451da177e4SLinus Torvalds 	int ret;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	if (!tun)
10481da177e4SLinus Torvalds 		return -EBADFD;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
10511da177e4SLinus Torvalds 
10529d319522SJonathan Corbet 	lock_kernel();
10531da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
10549d319522SJonathan Corbet 		goto out;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	if (on) {
1057609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
10581da177e4SLinus Torvalds 		if (ret)
10599d319522SJonathan Corbet 			goto out;
10601da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
10611da177e4SLinus Torvalds 	} else
10621da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
10639d319522SJonathan Corbet 	ret = 0;
10649d319522SJonathan Corbet out:
10659d319522SJonathan Corbet 	unlock_kernel();
10669d319522SJonathan Corbet 	return ret;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
10701da177e4SLinus Torvalds {
1071fd3e05b6SArnd Bergmann 	cycle_kernel_lock();
10721da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
10731da177e4SLinus Torvalds 	file->private_data = NULL;
10741da177e4SLinus Torvalds 	return 0;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	if (!tun)
10821da177e4SLinus Torvalds 		return 0;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	rtnl_lock();
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	/* Detach from net device */
10891da177e4SLinus Torvalds 	file->private_data = NULL;
10901da177e4SLinus Torvalds 	tun->attached = 0;
1091fc54c658SPavel Emelyanov 	put_net(dev_net(tun->dev));
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	/* Drop read queue */
10941da177e4SLinus Torvalds 	skb_queue_purge(&tun->readq);
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (!(tun->flags & TUN_PERSIST)) {
10971da177e4SLinus Torvalds 		list_del(&tun->list);
10981da177e4SLinus Torvalds 		unregister_netdevice(tun->dev);
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	rtnl_unlock();
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	return 0;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
1106d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
11071da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
11081da177e4SLinus Torvalds 	.llseek = no_llseek,
1109ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
1110ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
1111ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
1112ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
11131da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
11141da177e4SLinus Torvalds 	.ioctl	= tun_chr_ioctl,
11151da177e4SLinus Torvalds 	.open	= tun_chr_open,
11161da177e4SLinus Torvalds 	.release = tun_chr_close,
11171da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
11181da177e4SLinus Torvalds };
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
11211da177e4SLinus Torvalds 	.minor = TUN_MINOR,
11221da177e4SLinus Torvalds 	.name = "tun",
11231da177e4SLinus Torvalds 	.fops = &tun_fops,
11241da177e4SLinus Torvalds };
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds /* ethtool interface */
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11291da177e4SLinus Torvalds {
11301da177e4SLinus Torvalds 	cmd->supported		= 0;
11311da177e4SLinus Torvalds 	cmd->advertising	= 0;
11321da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
11331da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
11341da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
11351da177e4SLinus Torvalds 	cmd->phy_address	= 0;
11361da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
11371da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
11381da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
11391da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
11401da177e4SLinus Torvalds 	return 0;
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11441da177e4SLinus Torvalds {
11451da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
11481da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
11491da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
11521da177e4SLinus Torvalds 	case TUN_TUN_DEV:
11531da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
11541da177e4SLinus Torvalds 		break;
11551da177e4SLinus Torvalds 	case TUN_TAP_DEV:
11561da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
11571da177e4SLinus Torvalds 		break;
11581da177e4SLinus Torvalds 	}
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
11621da177e4SLinus Torvalds {
11631da177e4SLinus Torvalds #ifdef TUN_DEBUG
11641da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
11651da177e4SLinus Torvalds 	return tun->debug;
11661da177e4SLinus Torvalds #else
11671da177e4SLinus Torvalds 	return -EOPNOTSUPP;
11681da177e4SLinus Torvalds #endif
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds #ifdef TUN_DEBUG
11741da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
11751da177e4SLinus Torvalds 	tun->debug = value;
11761da177e4SLinus Torvalds #endif
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
11801da177e4SLinus Torvalds {
11811da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
11821da177e4SLinus Torvalds 	return tun->attached;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
11881da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
11921da177e4SLinus Torvalds {
11931da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
11941da177e4SLinus Torvalds 	if (data)
11951da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
11961da177e4SLinus Torvalds 	else
11971da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
11981da177e4SLinus Torvalds 	return 0;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12017282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
12021da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
12031da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
12041da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
12051da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
12061da177e4SLinus Torvalds 	.get_link	= tun_get_link,
12071da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
12081da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
12091da177e4SLinus Torvalds };
12101da177e4SLinus Torvalds 
121179d17604SPavel Emelyanov static int tun_init_net(struct net *net)
121279d17604SPavel Emelyanov {
121379d17604SPavel Emelyanov 	struct tun_net *tn;
121479d17604SPavel Emelyanov 
121579d17604SPavel Emelyanov 	tn = kmalloc(sizeof(*tn), GFP_KERNEL);
121679d17604SPavel Emelyanov 	if (tn == NULL)
121779d17604SPavel Emelyanov 		return -ENOMEM;
121879d17604SPavel Emelyanov 
121979d17604SPavel Emelyanov 	INIT_LIST_HEAD(&tn->dev_list);
122079d17604SPavel Emelyanov 
122179d17604SPavel Emelyanov 	if (net_assign_generic(net, tun_net_id, tn)) {
122279d17604SPavel Emelyanov 		kfree(tn);
122379d17604SPavel Emelyanov 		return -ENOMEM;
122479d17604SPavel Emelyanov 	}
122579d17604SPavel Emelyanov 
122679d17604SPavel Emelyanov 	return 0;
122779d17604SPavel Emelyanov }
122879d17604SPavel Emelyanov 
122979d17604SPavel Emelyanov static void tun_exit_net(struct net *net)
123079d17604SPavel Emelyanov {
123179d17604SPavel Emelyanov 	struct tun_net *tn;
1232d647a591SPavel Emelyanov 	struct tun_struct *tun, *nxt;
123379d17604SPavel Emelyanov 
123479d17604SPavel Emelyanov 	tn = net_generic(net, tun_net_id);
1235d647a591SPavel Emelyanov 
1236d647a591SPavel Emelyanov 	rtnl_lock();
1237d647a591SPavel Emelyanov 	list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
1238d647a591SPavel Emelyanov 		DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
1239d647a591SPavel Emelyanov 		unregister_netdevice(tun->dev);
1240d647a591SPavel Emelyanov 	}
1241d647a591SPavel Emelyanov 	rtnl_unlock();
1242d647a591SPavel Emelyanov 
124379d17604SPavel Emelyanov 	kfree(tn);
124479d17604SPavel Emelyanov }
124579d17604SPavel Emelyanov 
124679d17604SPavel Emelyanov static struct pernet_operations tun_net_ops = {
124779d17604SPavel Emelyanov 	.init = tun_init_net,
124879d17604SPavel Emelyanov 	.exit = tun_exit_net,
124979d17604SPavel Emelyanov };
125079d17604SPavel Emelyanov 
12511da177e4SLinus Torvalds static int __init tun_init(void)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	int ret = 0;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
12561da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
12571da177e4SLinus Torvalds 
125879d17604SPavel Emelyanov 	ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
125979d17604SPavel Emelyanov 	if (ret) {
126079d17604SPavel Emelyanov 		printk(KERN_ERR "tun: Can't register pernet ops\n");
126179d17604SPavel Emelyanov 		goto err_pernet;
126279d17604SPavel Emelyanov 	}
126379d17604SPavel Emelyanov 
12641da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
126579d17604SPavel Emelyanov 	if (ret) {
12661da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
126779d17604SPavel Emelyanov 		goto err_misc;
126879d17604SPavel Emelyanov 	}
126979d17604SPavel Emelyanov 	return 0;
127079d17604SPavel Emelyanov 
127179d17604SPavel Emelyanov err_misc:
127279d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
127379d17604SPavel Emelyanov err_pernet:
12741da177e4SLinus Torvalds 	return ret;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds static void tun_cleanup(void)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
128079d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds module_init(tun_init);
12841da177e4SLinus Torvalds module_exit(tun_cleanup);
12851da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
12861da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
12871da177e4SLinus Torvalds MODULE_LICENSE("GPL");
12881da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1289