xref: /openbmc/linux/drivers/net/tun.c (revision 79d1760491a25bfc70aeac363142cadd3f1cda71)
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  *
2136226a8dSBrian Braunstein  *  Brian Braunstein <linuxkernel@bristyle.com> 2007/03/23
2236226a8dSBrian Braunstein  *    Fixed hw address handling.  Now net_device.dev_addr is kept consistent
2336226a8dSBrian Braunstein  *    with tun.dev_addr when the address is set by this module.
2436226a8dSBrian Braunstein  *
25ff4cc3acSMike Kershaw  *  Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
26ff4cc3acSMike Kershaw  *    Add TUNSETLINK ioctl to set the link encapsulation
27ff4cc3acSMike Kershaw  *
281da177e4SLinus Torvalds  *  Mark Smith <markzzzsmith@yahoo.com.au>
291da177e4SLinus Torvalds  *   Use random_ether_addr() for tap MAC address.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
321da177e4SLinus Torvalds  *    Fixes in packet dropping, queue length setting and queue wakeup.
331da177e4SLinus Torvalds  *    Increased default tx queue length.
341da177e4SLinus Torvalds  *    Added ethtool API.
351da177e4SLinus Torvalds  *    Minor cleanups
361da177e4SLinus Torvalds  *
371da177e4SLinus Torvalds  *  Daniel Podlejski <underley@underley.eu.org>
381da177e4SLinus Torvalds  *    Modifications for 2.3.99-pre5 kernel.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define DRV_NAME	"tun"
421da177e4SLinus Torvalds #define DRV_VERSION	"1.6"
431da177e4SLinus Torvalds #define DRV_DESCRIPTION	"Universal TUN/TAP device driver"
441da177e4SLinus Torvalds #define DRV_COPYRIGHT	"(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <linux/module.h>
471da177e4SLinus Torvalds #include <linux/errno.h>
481da177e4SLinus Torvalds #include <linux/kernel.h>
491da177e4SLinus Torvalds #include <linux/major.h>
501da177e4SLinus Torvalds #include <linux/slab.h>
511da177e4SLinus Torvalds #include <linux/poll.h>
521da177e4SLinus Torvalds #include <linux/fcntl.h>
531da177e4SLinus Torvalds #include <linux/init.h>
541da177e4SLinus Torvalds #include <linux/skbuff.h>
551da177e4SLinus Torvalds #include <linux/netdevice.h>
561da177e4SLinus Torvalds #include <linux/etherdevice.h>
571da177e4SLinus Torvalds #include <linux/miscdevice.h>
581da177e4SLinus Torvalds #include <linux/ethtool.h>
591da177e4SLinus Torvalds #include <linux/rtnetlink.h>
601da177e4SLinus Torvalds #include <linux/if.h>
611da177e4SLinus Torvalds #include <linux/if_arp.h>
621da177e4SLinus Torvalds #include <linux/if_ether.h>
631da177e4SLinus Torvalds #include <linux/if_tun.h>
641da177e4SLinus Torvalds #include <linux/crc32.h>
65881d966bSEric W. Biederman #include <net/net_namespace.h>
66*79d17604SPavel Emelyanov #include <net/netns/generic.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <asm/system.h>
691da177e4SLinus Torvalds #include <asm/uaccess.h>
701da177e4SLinus Torvalds 
7114daa021SRusty Russell /* Uncomment to enable debugging */
7214daa021SRusty Russell /* #define TUN_DEBUG 1 */
7314daa021SRusty Russell 
741da177e4SLinus Torvalds #ifdef TUN_DEBUG
751da177e4SLinus Torvalds static int debug;
7614daa021SRusty Russell 
7714daa021SRusty Russell #define DBG  if(tun->debug)printk
7814daa021SRusty Russell #define DBG1 if(debug==2)printk
7914daa021SRusty Russell #else
8014daa021SRusty Russell #define DBG( a... )
8114daa021SRusty Russell #define DBG1( a... )
821da177e4SLinus Torvalds #endif
831da177e4SLinus Torvalds 
8414daa021SRusty Russell struct tun_struct {
8514daa021SRusty Russell 	struct list_head        list;
8614daa021SRusty Russell 	unsigned long 		flags;
8714daa021SRusty Russell 	int			attached;
8814daa021SRusty Russell 	uid_t			owner;
8914daa021SRusty Russell 	gid_t			group;
9014daa021SRusty Russell 
9114daa021SRusty Russell 	wait_queue_head_t	read_wait;
9214daa021SRusty Russell 	struct sk_buff_head	readq;
9314daa021SRusty Russell 
9414daa021SRusty Russell 	struct net_device	*dev;
9514daa021SRusty Russell 
9614daa021SRusty Russell 	struct fasync_struct    *fasync;
9714daa021SRusty Russell 
9814daa021SRusty Russell 	unsigned long if_flags;
9914daa021SRusty Russell 	u8 dev_addr[ETH_ALEN];
10014daa021SRusty Russell 	u32 chr_filter[2];
10114daa021SRusty Russell 	u32 net_filter[2];
10214daa021SRusty Russell 
10314daa021SRusty Russell #ifdef TUN_DEBUG
10414daa021SRusty Russell 	int debug;
10514daa021SRusty Russell #endif
10614daa021SRusty Russell };
10714daa021SRusty Russell 
1081da177e4SLinus Torvalds /* Network device part of the driver */
1091da177e4SLinus Torvalds 
110*79d17604SPavel Emelyanov static unsigned int tun_net_id;
111*79d17604SPavel Emelyanov struct tun_net {
112*79d17604SPavel Emelyanov 	struct list_head dev_list;
113*79d17604SPavel Emelyanov };
114*79d17604SPavel Emelyanov 
1151da177e4SLinus Torvalds static LIST_HEAD(tun_dev_list);
1167282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /* Net device open. */
1191da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	netif_start_queue(dev);
1221da177e4SLinus Torvalds 	return 0;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /* Net device close. */
1261da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	netif_stop_queue(dev);
1291da177e4SLinus Torvalds 	return 0;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* Net device start xmit */
1331da177e4SLinus Torvalds static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
1401da177e4SLinus Torvalds 	if (!tun->attached)
1411da177e4SLinus Torvalds 		goto drop;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	/* Packet dropping */
1441da177e4SLinus Torvalds 	if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
1451da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
1461da177e4SLinus Torvalds 			/* Normal queueing mode. */
1471da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
1481da177e4SLinus Torvalds 			netif_stop_queue(dev);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
1511da177e4SLinus Torvalds 			 * error is more appropriate. */
15209f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
1531da177e4SLinus Torvalds 		} else {
1541da177e4SLinus Torvalds 			/* Single queue mode.
1551da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
1561da177e4SLinus Torvalds 			goto drop;
1571da177e4SLinus Torvalds 		}
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	/* Queue packet */
1611da177e4SLinus Torvalds 	skb_queue_tail(&tun->readq, skb);
1621da177e4SLinus Torvalds 	dev->trans_start = jiffies;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/* Notify and wake up reader process */
1651da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
1661da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
1671da177e4SLinus Torvalds 	wake_up_interruptible(&tun->read_wait);
1681da177e4SLinus Torvalds 	return 0;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds drop:
17109f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
1721da177e4SLinus Torvalds 	kfree_skb(skb);
1731da177e4SLinus Torvalds 	return 0;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds /** Add the specified Ethernet address to this multicast filter. */
1771da177e4SLinus Torvalds static void
1781da177e4SLinus Torvalds add_multi(u32* filter, const u8* addr)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
1811da177e4SLinus Torvalds 	filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds /** Remove the specified Ethernet addres from this multicast filter. */
1851da177e4SLinus Torvalds static void
1861da177e4SLinus Torvalds del_multi(u32* filter, const u8* addr)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
1891da177e4SLinus Torvalds 	filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /** Update the list of multicast groups to which the network device belongs.
1931da177e4SLinus Torvalds  * This list is used to filter packets being sent from the character device to
1941da177e4SLinus Torvalds  * the network device. */
1951da177e4SLinus Torvalds static void
1961da177e4SLinus Torvalds tun_net_mclist(struct net_device *dev)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1991da177e4SLinus Torvalds 	const struct dev_mc_list *mclist;
2001da177e4SLinus Torvalds 	int i;
2010795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
2021da177e4SLinus Torvalds 	DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
2031da177e4SLinus Torvalds 			dev->name, dev->mc_count);
2041da177e4SLinus Torvalds 	memset(tun->chr_filter, 0, sizeof tun->chr_filter);
2051da177e4SLinus Torvalds 	for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
2061da177e4SLinus Torvalds 			i++, mclist = mclist->next) {
2071da177e4SLinus Torvalds 		add_multi(tun->net_filter, mclist->dmi_addr);
2080795af57SJoe Perches 		DBG(KERN_DEBUG "%s: tun_net_mclist: %s\n",
2090795af57SJoe Perches 		    dev->name, print_mac(mac, mclist->dmi_addr));
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2134885a504SEd Swierk #define MIN_MTU 68
2144885a504SEd Swierk #define MAX_MTU 65535
2154885a504SEd Swierk 
2164885a504SEd Swierk static int
2174885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
2184885a504SEd Swierk {
2194885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
2204885a504SEd Swierk 		return -EINVAL;
2214885a504SEd Swierk 	dev->mtu = new_mtu;
2224885a504SEd Swierk 	return 0;
2234885a504SEd Swierk }
2244885a504SEd Swierk 
2251da177e4SLinus Torvalds /* Initialize net device. */
2261da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
2311da177e4SLinus Torvalds 	case TUN_TUN_DEV:
2321da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
2331da177e4SLinus Torvalds 		dev->hard_header_len = 0;
2341da177e4SLinus Torvalds 		dev->addr_len = 0;
2351da177e4SLinus Torvalds 		dev->mtu = 1500;
2364885a504SEd Swierk 		dev->change_mtu = tun_net_change_mtu;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 		/* Zero header length */
2391da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
2401da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2411da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
2421da177e4SLinus Torvalds 		break;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	case TUN_TAP_DEV:
2451da177e4SLinus Torvalds 		/* Ethernet TAP Device */
2461da177e4SLinus Torvalds 		dev->set_multicast_list = tun_net_mclist;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 		ether_setup(dev);
2494885a504SEd Swierk 		dev->change_mtu = tun_net_change_mtu;
25036226a8dSBrian Braunstein 
25136226a8dSBrian Braunstein 		/* random address already created for us by tun_set_iff, use it */
25236226a8dSBrian Braunstein 		memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) );
25336226a8dSBrian Braunstein 
2541da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
2551da177e4SLinus Torvalds 		break;
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds /* Character device part */
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /* Poll */
2621da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
2651da177e4SLinus Torvalds 	unsigned int mask = POLLOUT | POLLWRNORM;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	if (!tun)
2681da177e4SLinus Torvalds 		return -EBADFD;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	poll_wait(file, &tun->read_wait, wait);
2731da177e4SLinus Torvalds 
274b03efcfbSDavid S. Miller 	if (!skb_queue_empty(&tun->readq))
2751da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	return mask;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds /* Get packet from user space buffer */
2811da177e4SLinus Torvalds static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
2841da177e4SLinus Torvalds 	struct sk_buff *skb;
2851da177e4SLinus Torvalds 	size_t len = count, align = 0;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
2881da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
2891da177e4SLinus Torvalds 			return -EINVAL;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 		if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
2921da177e4SLinus Torvalds 			return -EFAULT;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 
295e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
2961da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
297e01bf1c8SRusty Russell 		if (unlikely(len < ETH_HLEN))
298e01bf1c8SRusty Russell 			return -EINVAL;
299e01bf1c8SRusty Russell 	}
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
30209f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
3031da177e4SLinus Torvalds 		return -ENOMEM;
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	if (align)
3071da177e4SLinus Torvalds 		skb_reserve(skb, align);
3088f22757eSDave Jones 	if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
30909f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
3108f22757eSDave Jones 		kfree_skb(skb);
3111da177e4SLinus Torvalds 		return -EFAULT;
3128f22757eSDave Jones 	}
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
3151da177e4SLinus Torvalds 	case TUN_TUN_DEV:
316459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
3171da177e4SLinus Torvalds 		skb->protocol = pi.proto;
3184c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
3191da177e4SLinus Torvalds 		break;
3201da177e4SLinus Torvalds 	case TUN_TAP_DEV:
3211da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
3221da177e4SLinus Torvalds 		break;
3231da177e4SLinus Torvalds 	};
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	if (tun->flags & TUN_NOCHECKSUM)
3261da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	netif_rx_ni(skb);
3291da177e4SLinus Torvalds 	tun->dev->last_rx = jiffies;
3301da177e4SLinus Torvalds 
33109f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
33209f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	return count;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
337ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
338ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
3391da177e4SLinus Torvalds {
340ee0b3e67SBadari Pulavarty 	struct tun_struct *tun = iocb->ki_filp->private_data;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	if (!tun)
3431da177e4SLinus Torvalds 		return -EBADFD;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
3461da177e4SLinus Torvalds 
34752427c9dSAkinobu Mita 	return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /* Put packet to the user space buffer */
3511da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
3521da177e4SLinus Torvalds 				       struct sk_buff *skb,
3531da177e4SLinus Torvalds 				       struct iovec *iv, int len)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
3561da177e4SLinus Torvalds 	ssize_t total = 0;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
3591da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
3601da177e4SLinus Torvalds 			return -EINVAL;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		if (len < skb->len) {
3631da177e4SLinus Torvalds 			/* Packet will be striped */
3641da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
3651da177e4SLinus Torvalds 		}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
3681da177e4SLinus Torvalds 			return -EFAULT;
3691da177e4SLinus Torvalds 		total += sizeof(pi);
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	skb_copy_datagram_iovec(skb, 0, iv, len);
3751da177e4SLinus Torvalds 	total += len;
3761da177e4SLinus Torvalds 
37709f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
37809f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	return total;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
383ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
384ee0b3e67SBadari Pulavarty 			    unsigned long count, loff_t pos)
3851da177e4SLinus Torvalds {
386ee0b3e67SBadari Pulavarty 	struct file *file = iocb->ki_filp;
3871da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
3881da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
3891da177e4SLinus Torvalds 	struct sk_buff *skb;
3901da177e4SLinus Torvalds 	ssize_t len, ret = 0;
3910795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	if (!tun)
3941da177e4SLinus Torvalds 		return -EBADFD;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
3971da177e4SLinus Torvalds 
39852427c9dSAkinobu Mita 	len = iov_length(iv, count);
3991da177e4SLinus Torvalds 	if (len < 0)
4001da177e4SLinus Torvalds 		return -EINVAL;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	add_wait_queue(&tun->read_wait, &wait);
4031da177e4SLinus Torvalds 	while (len) {
4041da177e4SLinus Torvalds 		const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4051da177e4SLinus Torvalds 		u8 addr[ ETH_ALEN];
4061da177e4SLinus Torvalds 		int bit_nr;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 		/* Read frames from the queue */
4111da177e4SLinus Torvalds 		if (!(skb=skb_dequeue(&tun->readq))) {
4121da177e4SLinus Torvalds 			if (file->f_flags & O_NONBLOCK) {
4131da177e4SLinus Torvalds 				ret = -EAGAIN;
4141da177e4SLinus Torvalds 				break;
4151da177e4SLinus Torvalds 			}
4161da177e4SLinus Torvalds 			if (signal_pending(current)) {
4171da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
4181da177e4SLinus Torvalds 				break;
4191da177e4SLinus Torvalds 			}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
4221da177e4SLinus Torvalds 			schedule();
4231da177e4SLinus Torvalds 			continue;
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 		/** Decide whether to accept this packet. This code is designed to
4281da177e4SLinus Torvalds 		 * behave identically to an Ethernet interface. Accept the packet if
4291da177e4SLinus Torvalds 		 * - we are promiscuous.
4301da177e4SLinus Torvalds 		 * - the packet is addressed to us.
4311da177e4SLinus Torvalds 		 * - the packet is broadcast.
4321da177e4SLinus Torvalds 		 * - the packet is multicast and
4331da177e4SLinus Torvalds 		 *   - we are multicast promiscous.
4341da177e4SLinus Torvalds 		 *   - we belong to the multicast group.
4351da177e4SLinus Torvalds 		 */
436d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
437d626f62bSArnaldo Carvalho de Melo 								   skb->len));
4381da177e4SLinus Torvalds 		bit_nr = ether_crc(sizeof addr, addr) >> 26;
4391da177e4SLinus Torvalds 		if ((tun->if_flags & IFF_PROMISC) ||
4401da177e4SLinus Torvalds 				memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
4411da177e4SLinus Torvalds 				memcmp(addr, ones, sizeof addr) == 0 ||
4421da177e4SLinus Torvalds 				(((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
4431da177e4SLinus Torvalds 				  (addr[0] == 0x33 && addr[1] == 0x33)) &&
4441da177e4SLinus Torvalds 				 ((tun->if_flags & IFF_ALLMULTI) ||
4451da177e4SLinus Torvalds 				  (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
4460795af57SJoe Perches 			DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %s\n",
4470795af57SJoe Perches 					tun->dev->name, print_mac(mac, addr));
4481da177e4SLinus Torvalds 			ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
4491da177e4SLinus Torvalds 			kfree_skb(skb);
4501da177e4SLinus Torvalds 			break;
4511da177e4SLinus Torvalds 		} else {
4520795af57SJoe Perches 			DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %s\n",
4530795af57SJoe Perches 					tun->dev->name, print_mac(mac, addr));
4541da177e4SLinus Torvalds 			kfree_skb(skb);
4551da177e4SLinus Torvalds 			continue;
4561da177e4SLinus Torvalds 		}
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
4601da177e4SLinus Torvalds 	remove_wait_queue(&tun->read_wait, &wait);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	return ret;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	skb_queue_head_init(&tun->readq);
4701da177e4SLinus Torvalds 	init_waitqueue_head(&tun->read_wait);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	tun->owner = -1;
4738c644623SGuido Guenther 	tun->group = -1;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	dev->open = tun_net_open;
4761da177e4SLinus Torvalds 	dev->hard_start_xmit = tun_net_xmit;
4771da177e4SLinus Torvalds 	dev->stop = tun_net_close;
4781da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
4791da177e4SLinus Torvalds 	dev->destructor = free_netdev;
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds static struct tun_struct *tun_get_by_name(const char *name)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	struct tun_struct *tun;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	ASSERT_RTNL();
4871da177e4SLinus Torvalds 	list_for_each_entry(tun, &tun_dev_list, list) {
4881da177e4SLinus Torvalds 		if (!strncmp(tun->dev->name, name, IFNAMSIZ))
4891da177e4SLinus Torvalds 		    return tun;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	return NULL;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds static int tun_set_iff(struct file *file, struct ifreq *ifr)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	struct tun_struct *tun;
4981da177e4SLinus Torvalds 	struct net_device *dev;
4991da177e4SLinus Torvalds 	int err;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	tun = tun_get_by_name(ifr->ifr_name);
5021da177e4SLinus Torvalds 	if (tun) {
5031da177e4SLinus Torvalds 		if (tun->attached)
5041da177e4SLinus Torvalds 			return -EBUSY;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 		/* Check permissions */
5078c644623SGuido Guenther 		if (((tun->owner != -1 &&
5088c644623SGuido Guenther 		      current->euid != tun->owner) ||
5098c644623SGuido Guenther 		     (tun->group != -1 &&
5108c644623SGuido Guenther 		      current->egid != tun->group)) &&
5118c644623SGuido Guenther 		     !capable(CAP_NET_ADMIN))
5121da177e4SLinus Torvalds 			return -EPERM;
5131da177e4SLinus Torvalds 	}
514881d966bSEric W. Biederman 	else if (__dev_get_by_name(&init_net, ifr->ifr_name))
5151da177e4SLinus Torvalds 		return -EINVAL;
5161da177e4SLinus Torvalds 	else {
5171da177e4SLinus Torvalds 		char *name;
5181da177e4SLinus Torvalds 		unsigned long flags = 0;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 		err = -EINVAL;
5211da177e4SLinus Torvalds 
522ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
523ca6bb5d7SDavid Woodhouse 			return -EPERM;
524ca6bb5d7SDavid Woodhouse 
5251da177e4SLinus Torvalds 		/* Set dev type */
5261da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
5271da177e4SLinus Torvalds 			/* TUN device */
5281da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
5291da177e4SLinus Torvalds 			name = "tun%d";
5301da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
5311da177e4SLinus Torvalds 			/* TAP device */
5321da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
5331da177e4SLinus Torvalds 			name = "tap%d";
5341da177e4SLinus Torvalds 		} else
5351da177e4SLinus Torvalds 			goto failed;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 		if (*ifr->ifr_name)
5381da177e4SLinus Torvalds 			name = ifr->ifr_name;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
5411da177e4SLinus Torvalds 				   tun_setup);
5421da177e4SLinus Torvalds 		if (!dev)
5431da177e4SLinus Torvalds 			return -ENOMEM;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 		tun = netdev_priv(dev);
5461da177e4SLinus Torvalds 		tun->dev = dev;
5471da177e4SLinus Torvalds 		tun->flags = flags;
5481da177e4SLinus Torvalds 		/* Be promiscuous by default to maintain previous behaviour. */
5491da177e4SLinus Torvalds 		tun->if_flags = IFF_PROMISC;
5501da177e4SLinus Torvalds 		/* Generate random Ethernet address. */
551a3edb083SAl Viro 		*(__be16 *)tun->dev_addr = htons(0x00FF);
5521da177e4SLinus Torvalds 		get_random_bytes(tun->dev_addr + sizeof(u16), 4);
5531da177e4SLinus Torvalds 		memset(tun->chr_filter, 0, sizeof tun->chr_filter);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 		tun_net_init(dev);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
5581da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
5591da177e4SLinus Torvalds 			if (err < 0)
5601da177e4SLinus Torvalds 				goto err_free_dev;
5611da177e4SLinus Torvalds 		}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
5641da177e4SLinus Torvalds 		if (err < 0)
5651da177e4SLinus Torvalds 			goto err_free_dev;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 		list_add(&tun->list, &tun_dev_list);
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
5731da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
574a26af1e0SNathaniel Filardo 	else
575a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
5781da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
579a26af1e0SNathaniel Filardo 	else
580a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	file->private_data = tun;
5831da177e4SLinus Torvalds 	tun->attached = 1;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
5861da177e4SLinus Torvalds 	return 0;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds  err_free_dev:
5891da177e4SLinus Torvalds 	free_netdev(dev);
5901da177e4SLinus Torvalds  failed:
5911da177e4SLinus Torvalds 	return err;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds static int tun_chr_ioctl(struct inode *inode, struct file *file,
5951da177e4SLinus Torvalds 			 unsigned int cmd, unsigned long arg)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
5981da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
5991da177e4SLinus Torvalds 	struct ifreq ifr;
6000795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
6031da177e4SLinus Torvalds 		if (copy_from_user(&ifr, argp, sizeof ifr))
6041da177e4SLinus Torvalds 			return -EFAULT;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
6071da177e4SLinus Torvalds 		int err;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 		rtnl_lock();
6121da177e4SLinus Torvalds 		err = tun_set_iff(file, &ifr);
6131da177e4SLinus Torvalds 		rtnl_unlock();
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 		if (err)
6161da177e4SLinus Torvalds 			return err;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
6191da177e4SLinus Torvalds 			return -EFAULT;
6201da177e4SLinus Torvalds 		return 0;
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	if (!tun)
6241da177e4SLinus Torvalds 		return -EBADFD;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	switch (cmd) {
6291da177e4SLinus Torvalds 	case TUNSETNOCSUM:
6301da177e4SLinus Torvalds 		/* Disable/Enable checksum */
6311da177e4SLinus Torvalds 		if (arg)
6321da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
6331da177e4SLinus Torvalds 		else
6341da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
6371da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
6381da177e4SLinus Torvalds 		break;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	case TUNSETPERSIST:
6411da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
6421da177e4SLinus Torvalds 		if (arg)
6431da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
6441da177e4SLinus Torvalds 		else
6451da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
648c6e991deSToyo Abe 		    tun->dev->name, arg ? "enabled" : "disabled");
6491da177e4SLinus Torvalds 		break;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	case TUNSETOWNER:
6521da177e4SLinus Torvalds 		/* Set owner of the device */
6531da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
6561da177e4SLinus Torvalds 		break;
6571da177e4SLinus Torvalds 
6588c644623SGuido Guenther 	case TUNSETGROUP:
6598c644623SGuido Guenther 		/* Set group of the device */
6608c644623SGuido Guenther 		tun->group= (gid_t) arg;
6618c644623SGuido Guenther 
6628c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
6638c644623SGuido Guenther 		break;
6648c644623SGuido Guenther 
665ff4cc3acSMike Kershaw 	case TUNSETLINK:
666ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
667ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
668ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
669ff4cc3acSMike Kershaw 				tun->dev->name);
670ff4cc3acSMike Kershaw 			return -EBUSY;
671ff4cc3acSMike Kershaw 		} else {
672ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
673ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
674ff4cc3acSMike Kershaw 		}
675ff4cc3acSMike Kershaw 		break;
676ff4cc3acSMike Kershaw 
6771da177e4SLinus Torvalds #ifdef TUN_DEBUG
6781da177e4SLinus Torvalds 	case TUNSETDEBUG:
6791da177e4SLinus Torvalds 		tun->debug = arg;
6801da177e4SLinus Torvalds 		break;
6811da177e4SLinus Torvalds #endif
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	case SIOCGIFFLAGS:
6841da177e4SLinus Torvalds 		ifr.ifr_flags = tun->if_flags;
6851da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
6861da177e4SLinus Torvalds 			return -EFAULT;
6871da177e4SLinus Torvalds 		return 0;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
6901da177e4SLinus Torvalds 		/** Set the character device's interface flags. Currently only
6911da177e4SLinus Torvalds 		 * IFF_PROMISC and IFF_ALLMULTI are used. */
6921da177e4SLinus Torvalds 		tun->if_flags = ifr.ifr_flags;
6931da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: interface flags 0x%lx\n",
6941da177e4SLinus Torvalds 				tun->dev->name, tun->if_flags);
6951da177e4SLinus Torvalds 		return 0;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
69836226a8dSBrian Braunstein 		/* Note: the actual net device's address may be different */
6991da177e4SLinus Torvalds 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
7001da177e4SLinus Torvalds 				min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
7011da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
7021da177e4SLinus Torvalds 			return -EFAULT;
7031da177e4SLinus Torvalds 		return 0;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
70636226a8dSBrian Braunstein 	{
70736226a8dSBrian Braunstein 		/* try to set the actual net device's hw address */
70840102371SKim B. Heino 		int ret;
70940102371SKim B. Heino 
71040102371SKim B. Heino 		rtnl_lock();
71140102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
71240102371SKim B. Heino 		rtnl_unlock();
71336226a8dSBrian Braunstein 
71436226a8dSBrian Braunstein 		if (ret == 0) {
7151da177e4SLinus Torvalds 			/** Set the character device's hardware address. This is used when
7161da177e4SLinus Torvalds 			 * filtering packets being sent from the network device to the character
7171da177e4SLinus Torvalds 			 * device. */
7181da177e4SLinus Torvalds 			memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
7191da177e4SLinus Torvalds 					min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
7201da177e4SLinus Torvalds 			DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
7211da177e4SLinus Torvalds 					tun->dev->name,
7221da177e4SLinus Torvalds 					tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
7231da177e4SLinus Torvalds 					tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
72436226a8dSBrian Braunstein 		}
72536226a8dSBrian Braunstein 
72636226a8dSBrian Braunstein 		return  ret;
72736226a8dSBrian Braunstein 	}
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	case SIOCADDMULTI:
7301da177e4SLinus Torvalds 		/** Add the specified group to the character device's multicast filter
7311da177e4SLinus Torvalds 		 * list. */
7321da177e4SLinus Torvalds 		add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
7330795af57SJoe Perches 		DBG(KERN_DEBUG "%s: add multi: %s\n",
7340795af57SJoe Perches 		    tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
7351da177e4SLinus Torvalds 		return 0;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	case SIOCDELMULTI:
7381da177e4SLinus Torvalds 		/** Remove the specified group from the character device's multicast
7391da177e4SLinus Torvalds 		 * filter list. */
7401da177e4SLinus Torvalds 		del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
7410795af57SJoe Perches 		DBG(KERN_DEBUG "%s: del multi: %s\n",
7420795af57SJoe Perches 		    tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
7431da177e4SLinus Torvalds 		return 0;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	default:
7461da177e4SLinus Torvalds 		return -EINVAL;
7471da177e4SLinus Torvalds 	};
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	return 0;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
7551da177e4SLinus Torvalds 	int ret;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	if (!tun)
7581da177e4SLinus Torvalds 		return -EBADFD;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
7631da177e4SLinus Torvalds 		return ret;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	if (on) {
766609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
7671da177e4SLinus Torvalds 		if (ret)
7681da177e4SLinus Torvalds 			return ret;
7691da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
7701da177e4SLinus Torvalds 	} else
7711da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	return 0;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
7791da177e4SLinus Torvalds 	file->private_data = NULL;
7801da177e4SLinus Torvalds 	return 0;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	if (!tun)
7881da177e4SLinus Torvalds 		return 0;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	tun_chr_fasync(-1, file, 0);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	rtnl_lock();
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	/* Detach from net device */
7971da177e4SLinus Torvalds 	file->private_data = NULL;
7981da177e4SLinus Torvalds 	tun->attached = 0;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	/* Drop read queue */
8011da177e4SLinus Torvalds 	skb_queue_purge(&tun->readq);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	if (!(tun->flags & TUN_PERSIST)) {
8041da177e4SLinus Torvalds 		list_del(&tun->list);
8051da177e4SLinus Torvalds 		unregister_netdevice(tun->dev);
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	rtnl_unlock();
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	return 0;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
813d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
8141da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
8151da177e4SLinus Torvalds 	.llseek = no_llseek,
816ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
817ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
818ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
819ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
8201da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
8211da177e4SLinus Torvalds 	.ioctl	= tun_chr_ioctl,
8221da177e4SLinus Torvalds 	.open	= tun_chr_open,
8231da177e4SLinus Torvalds 	.release = tun_chr_close,
8241da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
8251da177e4SLinus Torvalds };
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
8281da177e4SLinus Torvalds 	.minor = TUN_MINOR,
8291da177e4SLinus Torvalds 	.name = "tun",
8301da177e4SLinus Torvalds 	.fops = &tun_fops,
8311da177e4SLinus Torvalds };
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds /* ethtool interface */
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	cmd->supported		= 0;
8381da177e4SLinus Torvalds 	cmd->advertising	= 0;
8391da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
8401da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
8411da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
8421da177e4SLinus Torvalds 	cmd->phy_address	= 0;
8431da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
8441da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
8451da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
8461da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
8471da177e4SLinus Torvalds 	return 0;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
8551da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
8561da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
8591da177e4SLinus Torvalds 	case TUN_TUN_DEV:
8601da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
8611da177e4SLinus Torvalds 		break;
8621da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8631da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
8641da177e4SLinus Torvalds 		break;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds #ifdef TUN_DEBUG
8711da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8721da177e4SLinus Torvalds 	return tun->debug;
8731da177e4SLinus Torvalds #else
8741da177e4SLinus Torvalds 	return -EOPNOTSUPP;
8751da177e4SLinus Torvalds #endif
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds #ifdef TUN_DEBUG
8811da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8821da177e4SLinus Torvalds 	tun->debug = value;
8831da177e4SLinus Torvalds #endif
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8891da177e4SLinus Torvalds 	return tun->attached;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8951da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9011da177e4SLinus Torvalds 	if (data)
9021da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
9031da177e4SLinus Torvalds 	else
9041da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
9051da177e4SLinus Torvalds 	return 0;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9087282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
9091da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
9101da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
9111da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
9121da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
9131da177e4SLinus Torvalds 	.get_link	= tun_get_link,
9141da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
9151da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
9161da177e4SLinus Torvalds };
9171da177e4SLinus Torvalds 
918*79d17604SPavel Emelyanov static int tun_init_net(struct net *net)
919*79d17604SPavel Emelyanov {
920*79d17604SPavel Emelyanov 	struct tun_net *tn;
921*79d17604SPavel Emelyanov 
922*79d17604SPavel Emelyanov 	tn = kmalloc(sizeof(*tn), GFP_KERNEL);
923*79d17604SPavel Emelyanov 	if (tn == NULL)
924*79d17604SPavel Emelyanov 		return -ENOMEM;
925*79d17604SPavel Emelyanov 
926*79d17604SPavel Emelyanov 	INIT_LIST_HEAD(&tn->dev_list);
927*79d17604SPavel Emelyanov 
928*79d17604SPavel Emelyanov 	if (net_assign_generic(net, tun_net_id, tn)) {
929*79d17604SPavel Emelyanov 		kfree(tn);
930*79d17604SPavel Emelyanov 		return -ENOMEM;
931*79d17604SPavel Emelyanov 	}
932*79d17604SPavel Emelyanov 
933*79d17604SPavel Emelyanov 	return 0;
934*79d17604SPavel Emelyanov }
935*79d17604SPavel Emelyanov 
936*79d17604SPavel Emelyanov static void tun_exit_net(struct net *net)
937*79d17604SPavel Emelyanov {
938*79d17604SPavel Emelyanov 	struct tun_net *tn;
939*79d17604SPavel Emelyanov 
940*79d17604SPavel Emelyanov 	tn = net_generic(net, tun_net_id);
941*79d17604SPavel Emelyanov 	kfree(tn);
942*79d17604SPavel Emelyanov }
943*79d17604SPavel Emelyanov 
944*79d17604SPavel Emelyanov static struct pernet_operations tun_net_ops = {
945*79d17604SPavel Emelyanov 	.init = tun_init_net,
946*79d17604SPavel Emelyanov 	.exit = tun_exit_net,
947*79d17604SPavel Emelyanov };
948*79d17604SPavel Emelyanov 
9491da177e4SLinus Torvalds static int __init tun_init(void)
9501da177e4SLinus Torvalds {
9511da177e4SLinus Torvalds 	int ret = 0;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
9541da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
9551da177e4SLinus Torvalds 
956*79d17604SPavel Emelyanov 	ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
957*79d17604SPavel Emelyanov 	if (ret) {
958*79d17604SPavel Emelyanov 		printk(KERN_ERR "tun: Can't register pernet ops\n");
959*79d17604SPavel Emelyanov 		goto err_pernet;
960*79d17604SPavel Emelyanov 	}
961*79d17604SPavel Emelyanov 
9621da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
963*79d17604SPavel Emelyanov 	if (ret) {
9641da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
965*79d17604SPavel Emelyanov 		goto err_misc;
966*79d17604SPavel Emelyanov 	}
967*79d17604SPavel Emelyanov 	return 0;
968*79d17604SPavel Emelyanov 
969*79d17604SPavel Emelyanov err_misc:
970*79d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
971*79d17604SPavel Emelyanov err_pernet:
9721da177e4SLinus Torvalds 	return ret;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds static void tun_cleanup(void)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	struct tun_struct *tun, *nxt;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	rtnl_lock();
9821da177e4SLinus Torvalds 	list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
9831da177e4SLinus Torvalds 		DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
9841da177e4SLinus Torvalds 		unregister_netdevice(tun->dev);
9851da177e4SLinus Torvalds 	}
9861da177e4SLinus Torvalds 	rtnl_unlock();
9871da177e4SLinus Torvalds 
988*79d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds module_init(tun_init);
9921da177e4SLinus Torvalds module_exit(tun_cleanup);
9931da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
9941da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
9951da177e4SLinus Torvalds MODULE_LICENSE("GPL");
9961da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
997