xref: /openbmc/linux/drivers/net/tun.c (revision 09f75cd7bf13720738e6a196cc0107ce9a5bd5a0)
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>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <asm/system.h>
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #ifdef TUN_DEBUG
711da177e4SLinus Torvalds static int debug;
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Network device part of the driver */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static LIST_HEAD(tun_dev_list);
777282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Net device open. */
801da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	netif_start_queue(dev);
831da177e4SLinus Torvalds 	return 0;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* Net device close. */
871da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	netif_stop_queue(dev);
901da177e4SLinus Torvalds 	return 0;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* Net device start xmit */
941da177e4SLinus Torvalds static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
1011da177e4SLinus Torvalds 	if (!tun->attached)
1021da177e4SLinus Torvalds 		goto drop;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	/* Packet dropping */
1051da177e4SLinus Torvalds 	if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
1061da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
1071da177e4SLinus Torvalds 			/* Normal queueing mode. */
1081da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
1091da177e4SLinus Torvalds 			netif_stop_queue(dev);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
1121da177e4SLinus Torvalds 			 * error is more appropriate. */
113*09f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
1141da177e4SLinus Torvalds 		} else {
1151da177e4SLinus Torvalds 			/* Single queue mode.
1161da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
1171da177e4SLinus Torvalds 			goto drop;
1181da177e4SLinus Torvalds 		}
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	/* Queue packet */
1221da177e4SLinus Torvalds 	skb_queue_tail(&tun->readq, skb);
1231da177e4SLinus Torvalds 	dev->trans_start = jiffies;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/* Notify and wake up reader process */
1261da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
1271da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
1281da177e4SLinus Torvalds 	wake_up_interruptible(&tun->read_wait);
1291da177e4SLinus Torvalds 	return 0;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds drop:
132*09f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
1331da177e4SLinus Torvalds 	kfree_skb(skb);
1341da177e4SLinus Torvalds 	return 0;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /** Add the specified Ethernet address to this multicast filter. */
1381da177e4SLinus Torvalds static void
1391da177e4SLinus Torvalds add_multi(u32* filter, const u8* addr)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
1421da177e4SLinus Torvalds 	filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /** Remove the specified Ethernet addres from this multicast filter. */
1461da177e4SLinus Torvalds static void
1471da177e4SLinus Torvalds del_multi(u32* filter, const u8* addr)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
1501da177e4SLinus Torvalds 	filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /** Update the list of multicast groups to which the network device belongs.
1541da177e4SLinus Torvalds  * This list is used to filter packets being sent from the character device to
1551da177e4SLinus Torvalds  * the network device. */
1561da177e4SLinus Torvalds static void
1571da177e4SLinus Torvalds tun_net_mclist(struct net_device *dev)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1601da177e4SLinus Torvalds 	const struct dev_mc_list *mclist;
1611da177e4SLinus Torvalds 	int i;
1621da177e4SLinus Torvalds 	DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
1631da177e4SLinus Torvalds 			dev->name, dev->mc_count);
1641da177e4SLinus Torvalds 	memset(tun->chr_filter, 0, sizeof tun->chr_filter);
1651da177e4SLinus Torvalds 	for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
1661da177e4SLinus Torvalds 			i++, mclist = mclist->next) {
1671da177e4SLinus Torvalds 		add_multi(tun->net_filter, mclist->dmi_addr);
1681da177e4SLinus Torvalds 		DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
1691da177e4SLinus Torvalds 				dev->name,
1701da177e4SLinus Torvalds 				mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
1711da177e4SLinus Torvalds 				mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* Initialize net device. */
1761da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
1811da177e4SLinus Torvalds 	case TUN_TUN_DEV:
1821da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
1831da177e4SLinus Torvalds 		dev->hard_header_len = 0;
1841da177e4SLinus Torvalds 		dev->addr_len = 0;
1851da177e4SLinus Torvalds 		dev->mtu = 1500;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 		/* Zero header length */
1881da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
1891da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1901da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
1911da177e4SLinus Torvalds 		break;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	case TUN_TAP_DEV:
1941da177e4SLinus Torvalds 		/* Ethernet TAP Device */
1951da177e4SLinus Torvalds 		dev->set_multicast_list = tun_net_mclist;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 		ether_setup(dev);
19836226a8dSBrian Braunstein 
19936226a8dSBrian Braunstein 		/* random address already created for us by tun_set_iff, use it */
20036226a8dSBrian Braunstein 		memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) );
20136226a8dSBrian Braunstein 
2021da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
2031da177e4SLinus Torvalds 		break;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* Character device part */
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /* Poll */
2101da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
2131da177e4SLinus Torvalds 	unsigned int mask = POLLOUT | POLLWRNORM;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	if (!tun)
2161da177e4SLinus Torvalds 		return -EBADFD;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	poll_wait(file, &tun->read_wait, wait);
2211da177e4SLinus Torvalds 
222b03efcfbSDavid S. Miller 	if (!skb_queue_empty(&tun->readq))
2231da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	return mask;
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds /* Get packet from user space buffer */
2291da177e4SLinus Torvalds static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
2321da177e4SLinus Torvalds 	struct sk_buff *skb;
2331da177e4SLinus Torvalds 	size_t len = count, align = 0;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
2361da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
2371da177e4SLinus Torvalds 			return -EINVAL;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 		if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
2401da177e4SLinus Torvalds 			return -EFAULT;
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
2441da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
247*09f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
2481da177e4SLinus Torvalds 		return -ENOMEM;
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	if (align)
2521da177e4SLinus Torvalds 		skb_reserve(skb, align);
2538f22757eSDave Jones 	if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
254*09f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
2558f22757eSDave Jones 		kfree_skb(skb);
2561da177e4SLinus Torvalds 		return -EFAULT;
2578f22757eSDave Jones 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
2601da177e4SLinus Torvalds 	case TUN_TUN_DEV:
261459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
2621da177e4SLinus Torvalds 		skb->protocol = pi.proto;
2634c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
2641da177e4SLinus Torvalds 		break;
2651da177e4SLinus Torvalds 	case TUN_TAP_DEV:
2661da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
2671da177e4SLinus Torvalds 		break;
2681da177e4SLinus Torvalds 	};
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (tun->flags & TUN_NOCHECKSUM)
2711da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	netif_rx_ni(skb);
2741da177e4SLinus Torvalds 	tun->dev->last_rx = jiffies;
2751da177e4SLinus Torvalds 
276*09f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
277*09f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	return count;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static inline size_t iov_total(const struct iovec *iv, unsigned long count)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	unsigned long i;
2851da177e4SLinus Torvalds 	size_t len;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	for (i = 0, len = 0; i < count; i++)
2881da177e4SLinus Torvalds 		len += iv[i].iov_len;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	return len;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
293ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
294ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
2951da177e4SLinus Torvalds {
296ee0b3e67SBadari Pulavarty 	struct tun_struct *tun = iocb->ki_filp->private_data;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (!tun)
2991da177e4SLinus Torvalds 		return -EBADFD;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds /* Put packet to the user space buffer */
3071da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
3081da177e4SLinus Torvalds 				       struct sk_buff *skb,
3091da177e4SLinus Torvalds 				       struct iovec *iv, int len)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
3121da177e4SLinus Torvalds 	ssize_t total = 0;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
3151da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
3161da177e4SLinus Torvalds 			return -EINVAL;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 		if (len < skb->len) {
3191da177e4SLinus Torvalds 			/* Packet will be striped */
3201da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
3211da177e4SLinus Torvalds 		}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 		if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
3241da177e4SLinus Torvalds 			return -EFAULT;
3251da177e4SLinus Torvalds 		total += sizeof(pi);
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	skb_copy_datagram_iovec(skb, 0, iv, len);
3311da177e4SLinus Torvalds 	total += len;
3321da177e4SLinus Torvalds 
333*09f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
334*09f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	return total;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
339ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
340ee0b3e67SBadari Pulavarty 			    unsigned long count, loff_t pos)
3411da177e4SLinus Torvalds {
342ee0b3e67SBadari Pulavarty 	struct file *file = iocb->ki_filp;
3431da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
3441da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
3451da177e4SLinus Torvalds 	struct sk_buff *skb;
3461da177e4SLinus Torvalds 	ssize_t len, ret = 0;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if (!tun)
3491da177e4SLinus Torvalds 		return -EBADFD;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	len = iov_total(iv, count);
3541da177e4SLinus Torvalds 	if (len < 0)
3551da177e4SLinus Torvalds 		return -EINVAL;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	add_wait_queue(&tun->read_wait, &wait);
3581da177e4SLinus Torvalds 	while (len) {
3591da177e4SLinus Torvalds 		const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3601da177e4SLinus Torvalds 		u8 addr[ ETH_ALEN];
3611da177e4SLinus Torvalds 		int bit_nr;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 		/* Read frames from the queue */
3661da177e4SLinus Torvalds 		if (!(skb=skb_dequeue(&tun->readq))) {
3671da177e4SLinus Torvalds 			if (file->f_flags & O_NONBLOCK) {
3681da177e4SLinus Torvalds 				ret = -EAGAIN;
3691da177e4SLinus Torvalds 				break;
3701da177e4SLinus Torvalds 			}
3711da177e4SLinus Torvalds 			if (signal_pending(current)) {
3721da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
3731da177e4SLinus Torvalds 				break;
3741da177e4SLinus Torvalds 			}
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
3771da177e4SLinus Torvalds 			schedule();
3781da177e4SLinus Torvalds 			continue;
3791da177e4SLinus Torvalds 		}
3801da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 		/** Decide whether to accept this packet. This code is designed to
3831da177e4SLinus Torvalds 		 * behave identically to an Ethernet interface. Accept the packet if
3841da177e4SLinus Torvalds 		 * - we are promiscuous.
3851da177e4SLinus Torvalds 		 * - the packet is addressed to us.
3861da177e4SLinus Torvalds 		 * - the packet is broadcast.
3871da177e4SLinus Torvalds 		 * - the packet is multicast and
3881da177e4SLinus Torvalds 		 *   - we are multicast promiscous.
3891da177e4SLinus Torvalds 		 *   - we belong to the multicast group.
3901da177e4SLinus Torvalds 		 */
391d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
392d626f62bSArnaldo Carvalho de Melo 								   skb->len));
3931da177e4SLinus Torvalds 		bit_nr = ether_crc(sizeof addr, addr) >> 26;
3941da177e4SLinus Torvalds 		if ((tun->if_flags & IFF_PROMISC) ||
3951da177e4SLinus Torvalds 				memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
3961da177e4SLinus Torvalds 				memcmp(addr, ones, sizeof addr) == 0 ||
3971da177e4SLinus Torvalds 				(((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
3981da177e4SLinus Torvalds 				  (addr[0] == 0x33 && addr[1] == 0x33)) &&
3991da177e4SLinus Torvalds 				 ((tun->if_flags & IFF_ALLMULTI) ||
4001da177e4SLinus Torvalds 				  (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
4011da177e4SLinus Torvalds 			DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
4021da177e4SLinus Torvalds 					tun->dev->name, addr[0], addr[1], addr[2],
4031da177e4SLinus Torvalds 					addr[3], addr[4], addr[5]);
4041da177e4SLinus Torvalds 			ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
4051da177e4SLinus Torvalds 			kfree_skb(skb);
4061da177e4SLinus Torvalds 			break;
4071da177e4SLinus Torvalds 		} else {
4081da177e4SLinus Torvalds 			DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
4091da177e4SLinus Torvalds 					tun->dev->name, addr[0], addr[1], addr[2],
4101da177e4SLinus Torvalds 					addr[3], addr[4], addr[5]);
4111da177e4SLinus Torvalds 			kfree_skb(skb);
4121da177e4SLinus Torvalds 			continue;
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
4171da177e4SLinus Torvalds 	remove_wait_queue(&tun->read_wait, &wait);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	return ret;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	skb_queue_head_init(&tun->readq);
4271da177e4SLinus Torvalds 	init_waitqueue_head(&tun->read_wait);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	tun->owner = -1;
4308c644623SGuido Guenther 	tun->group = -1;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	dev->open = tun_net_open;
4331da177e4SLinus Torvalds 	dev->hard_start_xmit = tun_net_xmit;
4341da177e4SLinus Torvalds 	dev->stop = tun_net_close;
4351da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
4361da177e4SLinus Torvalds 	dev->destructor = free_netdev;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static struct tun_struct *tun_get_by_name(const char *name)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	struct tun_struct *tun;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	ASSERT_RTNL();
4441da177e4SLinus Torvalds 	list_for_each_entry(tun, &tun_dev_list, list) {
4451da177e4SLinus Torvalds 		if (!strncmp(tun->dev->name, name, IFNAMSIZ))
4461da177e4SLinus Torvalds 		    return tun;
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	return NULL;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds static int tun_set_iff(struct file *file, struct ifreq *ifr)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct tun_struct *tun;
4551da177e4SLinus Torvalds 	struct net_device *dev;
4561da177e4SLinus Torvalds 	int err;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	tun = tun_get_by_name(ifr->ifr_name);
4591da177e4SLinus Torvalds 	if (tun) {
4601da177e4SLinus Torvalds 		if (tun->attached)
4611da177e4SLinus Torvalds 			return -EBUSY;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		/* Check permissions */
4648c644623SGuido Guenther 		if (((tun->owner != -1 &&
4658c644623SGuido Guenther 		      current->euid != tun->owner) ||
4668c644623SGuido Guenther 		     (tun->group != -1 &&
4678c644623SGuido Guenther 		      current->egid != tun->group)) &&
4688c644623SGuido Guenther 		     !capable(CAP_NET_ADMIN))
4691da177e4SLinus Torvalds 			return -EPERM;
4701da177e4SLinus Torvalds 	}
471881d966bSEric W. Biederman 	else if (__dev_get_by_name(&init_net, ifr->ifr_name))
4721da177e4SLinus Torvalds 		return -EINVAL;
4731da177e4SLinus Torvalds 	else {
4741da177e4SLinus Torvalds 		char *name;
4751da177e4SLinus Torvalds 		unsigned long flags = 0;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 		err = -EINVAL;
4781da177e4SLinus Torvalds 
479ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
480ca6bb5d7SDavid Woodhouse 			return -EPERM;
481ca6bb5d7SDavid Woodhouse 
4821da177e4SLinus Torvalds 		/* Set dev type */
4831da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
4841da177e4SLinus Torvalds 			/* TUN device */
4851da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
4861da177e4SLinus Torvalds 			name = "tun%d";
4871da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
4881da177e4SLinus Torvalds 			/* TAP device */
4891da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
4901da177e4SLinus Torvalds 			name = "tap%d";
4911da177e4SLinus Torvalds 		} else
4921da177e4SLinus Torvalds 			goto failed;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 		if (*ifr->ifr_name)
4951da177e4SLinus Torvalds 			name = ifr->ifr_name;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
4981da177e4SLinus Torvalds 				   tun_setup);
4991da177e4SLinus Torvalds 		if (!dev)
5001da177e4SLinus Torvalds 			return -ENOMEM;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 		tun = netdev_priv(dev);
5031da177e4SLinus Torvalds 		tun->dev = dev;
5041da177e4SLinus Torvalds 		tun->flags = flags;
5051da177e4SLinus Torvalds 		/* Be promiscuous by default to maintain previous behaviour. */
5061da177e4SLinus Torvalds 		tun->if_flags = IFF_PROMISC;
5071da177e4SLinus Torvalds 		/* Generate random Ethernet address. */
5081da177e4SLinus Torvalds 		*(u16 *)tun->dev_addr = htons(0x00FF);
5091da177e4SLinus Torvalds 		get_random_bytes(tun->dev_addr + sizeof(u16), 4);
5101da177e4SLinus Torvalds 		memset(tun->chr_filter, 0, sizeof tun->chr_filter);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		tun_net_init(dev);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
5151da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
5161da177e4SLinus Torvalds 			if (err < 0)
5171da177e4SLinus Torvalds 				goto err_free_dev;
5181da177e4SLinus Torvalds 		}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
5211da177e4SLinus Torvalds 		if (err < 0)
5221da177e4SLinus Torvalds 			goto err_free_dev;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 		list_add(&tun->list, &tun_dev_list);
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
5301da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
5331da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	file->private_data = tun;
5361da177e4SLinus Torvalds 	tun->attached = 1;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
5391da177e4SLinus Torvalds 	return 0;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds  err_free_dev:
5421da177e4SLinus Torvalds 	free_netdev(dev);
5431da177e4SLinus Torvalds  failed:
5441da177e4SLinus Torvalds 	return err;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds static int tun_chr_ioctl(struct inode *inode, struct file *file,
5481da177e4SLinus Torvalds 			 unsigned int cmd, unsigned long arg)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
5511da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
5521da177e4SLinus Torvalds 	struct ifreq ifr;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
5551da177e4SLinus Torvalds 		if (copy_from_user(&ifr, argp, sizeof ifr))
5561da177e4SLinus Torvalds 			return -EFAULT;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
5591da177e4SLinus Torvalds 		int err;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 		rtnl_lock();
5641da177e4SLinus Torvalds 		err = tun_set_iff(file, &ifr);
5651da177e4SLinus Torvalds 		rtnl_unlock();
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 		if (err)
5681da177e4SLinus Torvalds 			return err;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
5711da177e4SLinus Torvalds 			return -EFAULT;
5721da177e4SLinus Torvalds 		return 0;
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (!tun)
5761da177e4SLinus Torvalds 		return -EBADFD;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	switch (cmd) {
5811da177e4SLinus Torvalds 	case TUNSETNOCSUM:
5821da177e4SLinus Torvalds 		/* Disable/Enable checksum */
5831da177e4SLinus Torvalds 		if (arg)
5841da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
5851da177e4SLinus Torvalds 		else
5861da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
5891da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
5901da177e4SLinus Torvalds 		break;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	case TUNSETPERSIST:
5931da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
5941da177e4SLinus Torvalds 		if (arg)
5951da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
5961da177e4SLinus Torvalds 		else
5971da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
6001da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
6011da177e4SLinus Torvalds 		break;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	case TUNSETOWNER:
6041da177e4SLinus Torvalds 		/* Set owner of the device */
6051da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
6081da177e4SLinus Torvalds 		break;
6091da177e4SLinus Torvalds 
6108c644623SGuido Guenther 	case TUNSETGROUP:
6118c644623SGuido Guenther 		/* Set group of the device */
6128c644623SGuido Guenther 		tun->group= (gid_t) arg;
6138c644623SGuido Guenther 
6148c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
6158c644623SGuido Guenther 		break;
6168c644623SGuido Guenther 
617ff4cc3acSMike Kershaw 	case TUNSETLINK:
618ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
619ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
620ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
621ff4cc3acSMike Kershaw 				tun->dev->name);
622ff4cc3acSMike Kershaw 			return -EBUSY;
623ff4cc3acSMike Kershaw 		} else {
624ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
625ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
626ff4cc3acSMike Kershaw 		}
627ff4cc3acSMike Kershaw 		break;
628ff4cc3acSMike Kershaw 
6291da177e4SLinus Torvalds #ifdef TUN_DEBUG
6301da177e4SLinus Torvalds 	case TUNSETDEBUG:
6311da177e4SLinus Torvalds 		tun->debug = arg;
6321da177e4SLinus Torvalds 		break;
6331da177e4SLinus Torvalds #endif
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	case SIOCGIFFLAGS:
6361da177e4SLinus Torvalds 		ifr.ifr_flags = tun->if_flags;
6371da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
6381da177e4SLinus Torvalds 			return -EFAULT;
6391da177e4SLinus Torvalds 		return 0;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
6421da177e4SLinus Torvalds 		/** Set the character device's interface flags. Currently only
6431da177e4SLinus Torvalds 		 * IFF_PROMISC and IFF_ALLMULTI are used. */
6441da177e4SLinus Torvalds 		tun->if_flags = ifr.ifr_flags;
6451da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: interface flags 0x%lx\n",
6461da177e4SLinus Torvalds 				tun->dev->name, tun->if_flags);
6471da177e4SLinus Torvalds 		return 0;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
65036226a8dSBrian Braunstein 		/* Note: the actual net device's address may be different */
6511da177e4SLinus Torvalds 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
6521da177e4SLinus Torvalds 				min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
6531da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
6541da177e4SLinus Torvalds 			return -EFAULT;
6551da177e4SLinus Torvalds 		return 0;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
65836226a8dSBrian Braunstein 	{
65936226a8dSBrian Braunstein 		/* try to set the actual net device's hw address */
66036226a8dSBrian Braunstein 		int ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
66136226a8dSBrian Braunstein 
66236226a8dSBrian Braunstein 		if (ret == 0) {
6631da177e4SLinus Torvalds 			/** Set the character device's hardware address. This is used when
6641da177e4SLinus Torvalds 			 * filtering packets being sent from the network device to the character
6651da177e4SLinus Torvalds 			 * device. */
6661da177e4SLinus Torvalds 			memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
6671da177e4SLinus Torvalds 					min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
6681da177e4SLinus Torvalds 			DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
6691da177e4SLinus Torvalds 					tun->dev->name,
6701da177e4SLinus Torvalds 					tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
6711da177e4SLinus Torvalds 					tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
67236226a8dSBrian Braunstein 		}
67336226a8dSBrian Braunstein 
67436226a8dSBrian Braunstein 		return  ret;
67536226a8dSBrian Braunstein 	}
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	case SIOCADDMULTI:
6781da177e4SLinus Torvalds 		/** Add the specified group to the character device's multicast filter
6791da177e4SLinus Torvalds 		 * list. */
6801da177e4SLinus Torvalds 		add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
6811da177e4SLinus Torvalds 		DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
6821da177e4SLinus Torvalds 				tun->dev->name,
6831da177e4SLinus Torvalds 				(u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
6841da177e4SLinus Torvalds 				(u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
6851da177e4SLinus Torvalds 				(u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
6861da177e4SLinus Torvalds 		return 0;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	case SIOCDELMULTI:
6891da177e4SLinus Torvalds 		/** Remove the specified group from the character device's multicast
6901da177e4SLinus Torvalds 		 * filter list. */
6911da177e4SLinus Torvalds 		del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
6921da177e4SLinus Torvalds 		DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
6931da177e4SLinus Torvalds 				tun->dev->name,
6941da177e4SLinus Torvalds 				(u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
6951da177e4SLinus Torvalds 				(u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
6961da177e4SLinus Torvalds 				(u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
6971da177e4SLinus Torvalds 		return 0;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	default:
7001da177e4SLinus Torvalds 		return -EINVAL;
7011da177e4SLinus Torvalds 	};
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	return 0;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
7091da177e4SLinus Torvalds 	int ret;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	if (!tun)
7121da177e4SLinus Torvalds 		return -EBADFD;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
7171da177e4SLinus Torvalds 		return ret;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	if (on) {
720609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
7211da177e4SLinus Torvalds 		if (ret)
7221da177e4SLinus Torvalds 			return ret;
7231da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
7241da177e4SLinus Torvalds 	} else
7251da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	return 0;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
7331da177e4SLinus Torvalds 	file->private_data = NULL;
7341da177e4SLinus Torvalds 	return 0;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	if (!tun)
7421da177e4SLinus Torvalds 		return 0;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	tun_chr_fasync(-1, file, 0);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	rtnl_lock();
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	/* Detach from net device */
7511da177e4SLinus Torvalds 	file->private_data = NULL;
7521da177e4SLinus Torvalds 	tun->attached = 0;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	/* Drop read queue */
7551da177e4SLinus Torvalds 	skb_queue_purge(&tun->readq);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	if (!(tun->flags & TUN_PERSIST)) {
7581da177e4SLinus Torvalds 		list_del(&tun->list);
7591da177e4SLinus Torvalds 		unregister_netdevice(tun->dev);
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	rtnl_unlock();
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	return 0;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
767d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
7681da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
7691da177e4SLinus Torvalds 	.llseek = no_llseek,
770ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
771ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
772ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
773ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
7741da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
7751da177e4SLinus Torvalds 	.ioctl	= tun_chr_ioctl,
7761da177e4SLinus Torvalds 	.open	= tun_chr_open,
7771da177e4SLinus Torvalds 	.release = tun_chr_close,
7781da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
7791da177e4SLinus Torvalds };
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
7821da177e4SLinus Torvalds 	.minor = TUN_MINOR,
7831da177e4SLinus Torvalds 	.name = "tun",
7841da177e4SLinus Torvalds 	.fops = &tun_fops,
7851da177e4SLinus Torvalds };
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds /* ethtool interface */
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	cmd->supported		= 0;
7921da177e4SLinus Torvalds 	cmd->advertising	= 0;
7931da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
7941da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
7951da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
7961da177e4SLinus Torvalds 	cmd->phy_address	= 0;
7971da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
7981da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
7991da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
8001da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
8011da177e4SLinus Torvalds 	return 0;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
8091da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
8101da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
8131da177e4SLinus Torvalds 	case TUN_TUN_DEV:
8141da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
8151da177e4SLinus Torvalds 		break;
8161da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8171da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
8181da177e4SLinus Torvalds 		break;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds #ifdef TUN_DEBUG
8251da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8261da177e4SLinus Torvalds 	return tun->debug;
8271da177e4SLinus Torvalds #else
8281da177e4SLinus Torvalds 	return -EOPNOTSUPP;
8291da177e4SLinus Torvalds #endif
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds #ifdef TUN_DEBUG
8351da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8361da177e4SLinus Torvalds 	tun->debug = value;
8371da177e4SLinus Torvalds #endif
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8431da177e4SLinus Torvalds 	return tun->attached;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8491da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8551da177e4SLinus Torvalds 	if (data)
8561da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
8571da177e4SLinus Torvalds 	else
8581da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
8591da177e4SLinus Torvalds 	return 0;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8627282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
8631da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
8641da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
8651da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
8661da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
8671da177e4SLinus Torvalds 	.get_link	= tun_get_link,
8681da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
8691da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
8701da177e4SLinus Torvalds };
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds static int __init tun_init(void)
8731da177e4SLinus Torvalds {
8741da177e4SLinus Torvalds 	int ret = 0;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
8771da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
8801da177e4SLinus Torvalds 	if (ret)
8811da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
8821da177e4SLinus Torvalds 	return ret;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds static void tun_cleanup(void)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	struct tun_struct *tun, *nxt;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	rtnl_lock();
8921da177e4SLinus Torvalds 	list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
8931da177e4SLinus Torvalds 		DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
8941da177e4SLinus Torvalds 		unregister_netdevice(tun->dev);
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 	rtnl_unlock();
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds module_init(tun_init);
9011da177e4SLinus Torvalds module_exit(tun_cleanup);
9021da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
9031da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
9041da177e4SLinus Torvalds MODULE_LICENSE("GPL");
9051da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
906