xref: /openbmc/linux/drivers/net/tun.c (revision d647a591dae06ccc96210cea938aa0342ef935fc)
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>
65*d647a591SPavel Emelyanov #include <linux/nsproxy.h>
66881d966bSEric W. Biederman #include <net/net_namespace.h>
6779d17604SPavel Emelyanov #include <net/netns/generic.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <asm/system.h>
701da177e4SLinus Torvalds #include <asm/uaccess.h>
711da177e4SLinus Torvalds 
7214daa021SRusty Russell /* Uncomment to enable debugging */
7314daa021SRusty Russell /* #define TUN_DEBUG 1 */
7414daa021SRusty Russell 
751da177e4SLinus Torvalds #ifdef TUN_DEBUG
761da177e4SLinus Torvalds static int debug;
7714daa021SRusty Russell 
7814daa021SRusty Russell #define DBG  if(tun->debug)printk
7914daa021SRusty Russell #define DBG1 if(debug==2)printk
8014daa021SRusty Russell #else
8114daa021SRusty Russell #define DBG( a... )
8214daa021SRusty Russell #define DBG1( a... )
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds 
8514daa021SRusty Russell struct tun_struct {
8614daa021SRusty Russell 	struct list_head        list;
8714daa021SRusty Russell 	unsigned long 		flags;
8814daa021SRusty Russell 	int			attached;
8914daa021SRusty Russell 	uid_t			owner;
9014daa021SRusty Russell 	gid_t			group;
9114daa021SRusty Russell 
9214daa021SRusty Russell 	wait_queue_head_t	read_wait;
9314daa021SRusty Russell 	struct sk_buff_head	readq;
9414daa021SRusty Russell 
9514daa021SRusty Russell 	struct net_device	*dev;
9614daa021SRusty Russell 
9714daa021SRusty Russell 	struct fasync_struct    *fasync;
9814daa021SRusty Russell 
9914daa021SRusty Russell 	unsigned long if_flags;
10014daa021SRusty Russell 	u8 dev_addr[ETH_ALEN];
10114daa021SRusty Russell 	u32 chr_filter[2];
10214daa021SRusty Russell 	u32 net_filter[2];
10314daa021SRusty Russell 
10414daa021SRusty Russell #ifdef TUN_DEBUG
10514daa021SRusty Russell 	int debug;
10614daa021SRusty Russell #endif
10714daa021SRusty Russell };
10814daa021SRusty Russell 
1091da177e4SLinus Torvalds /* Network device part of the driver */
1101da177e4SLinus Torvalds 
11179d17604SPavel Emelyanov static unsigned int tun_net_id;
11279d17604SPavel Emelyanov struct tun_net {
11379d17604SPavel Emelyanov 	struct list_head dev_list;
11479d17604SPavel Emelyanov };
11579d17604SPavel Emelyanov 
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 
482*d647a591SPavel Emelyanov static struct tun_struct *tun_get_by_name(struct tun_net *tn, const char *name)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	struct tun_struct *tun;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	ASSERT_RTNL();
487*d647a591SPavel Emelyanov 	list_for_each_entry(tun, &tn->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 
495*d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
4961da177e4SLinus Torvalds {
497*d647a591SPavel Emelyanov 	struct tun_net *tn;
4981da177e4SLinus Torvalds 	struct tun_struct *tun;
4991da177e4SLinus Torvalds 	struct net_device *dev;
5001da177e4SLinus Torvalds 	int err;
5011da177e4SLinus Torvalds 
502*d647a591SPavel Emelyanov 	tn = net_generic(net, tun_net_id);
503*d647a591SPavel Emelyanov 	tun = tun_get_by_name(tn, ifr->ifr_name);
5041da177e4SLinus Torvalds 	if (tun) {
5051da177e4SLinus Torvalds 		if (tun->attached)
5061da177e4SLinus Torvalds 			return -EBUSY;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 		/* Check permissions */
5098c644623SGuido Guenther 		if (((tun->owner != -1 &&
5108c644623SGuido Guenther 		      current->euid != tun->owner) ||
5118c644623SGuido Guenther 		     (tun->group != -1 &&
5128c644623SGuido Guenther 		      current->egid != tun->group)) &&
5138c644623SGuido Guenther 		     !capable(CAP_NET_ADMIN))
5141da177e4SLinus Torvalds 			return -EPERM;
5151da177e4SLinus Torvalds 	}
516*d647a591SPavel Emelyanov 	else if (__dev_get_by_name(net, ifr->ifr_name))
5171da177e4SLinus Torvalds 		return -EINVAL;
5181da177e4SLinus Torvalds 	else {
5191da177e4SLinus Torvalds 		char *name;
5201da177e4SLinus Torvalds 		unsigned long flags = 0;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 		err = -EINVAL;
5231da177e4SLinus Torvalds 
524ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
525ca6bb5d7SDavid Woodhouse 			return -EPERM;
526ca6bb5d7SDavid Woodhouse 
5271da177e4SLinus Torvalds 		/* Set dev type */
5281da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
5291da177e4SLinus Torvalds 			/* TUN device */
5301da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
5311da177e4SLinus Torvalds 			name = "tun%d";
5321da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
5331da177e4SLinus Torvalds 			/* TAP device */
5341da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
5351da177e4SLinus Torvalds 			name = "tap%d";
5361da177e4SLinus Torvalds 		} else
5371da177e4SLinus Torvalds 			goto failed;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 		if (*ifr->ifr_name)
5401da177e4SLinus Torvalds 			name = ifr->ifr_name;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
5431da177e4SLinus Torvalds 				   tun_setup);
5441da177e4SLinus Torvalds 		if (!dev)
5451da177e4SLinus Torvalds 			return -ENOMEM;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 		tun = netdev_priv(dev);
5481da177e4SLinus Torvalds 		tun->dev = dev;
5491da177e4SLinus Torvalds 		tun->flags = flags;
5501da177e4SLinus Torvalds 		/* Be promiscuous by default to maintain previous behaviour. */
5511da177e4SLinus Torvalds 		tun->if_flags = IFF_PROMISC;
5521da177e4SLinus Torvalds 		/* Generate random Ethernet address. */
553a3edb083SAl Viro 		*(__be16 *)tun->dev_addr = htons(0x00FF);
5541da177e4SLinus Torvalds 		get_random_bytes(tun->dev_addr + sizeof(u16), 4);
5551da177e4SLinus Torvalds 		memset(tun->chr_filter, 0, sizeof tun->chr_filter);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 		tun_net_init(dev);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
5601da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
5611da177e4SLinus Torvalds 			if (err < 0)
5621da177e4SLinus Torvalds 				goto err_free_dev;
5631da177e4SLinus Torvalds 		}
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
5661da177e4SLinus Torvalds 		if (err < 0)
5671da177e4SLinus Torvalds 			goto err_free_dev;
5681da177e4SLinus Torvalds 
569*d647a591SPavel Emelyanov 		list_add(&tun->list, &tn->dev_list);
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
5751da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
576a26af1e0SNathaniel Filardo 	else
577a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
5801da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
581a26af1e0SNathaniel Filardo 	else
582a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	file->private_data = tun;
5851da177e4SLinus Torvalds 	tun->attached = 1;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
5881da177e4SLinus Torvalds 	return 0;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds  err_free_dev:
5911da177e4SLinus Torvalds 	free_netdev(dev);
5921da177e4SLinus Torvalds  failed:
5931da177e4SLinus Torvalds 	return err;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds static int tun_chr_ioctl(struct inode *inode, struct file *file,
5971da177e4SLinus Torvalds 			 unsigned int cmd, unsigned long arg)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
6001da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
6011da177e4SLinus Torvalds 	struct ifreq ifr;
6020795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
6051da177e4SLinus Torvalds 		if (copy_from_user(&ifr, argp, sizeof ifr))
6061da177e4SLinus Torvalds 			return -EFAULT;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
6091da177e4SLinus Torvalds 		int err;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 		rtnl_lock();
614*d647a591SPavel Emelyanov 		err = tun_set_iff(current->nsproxy->net_ns, file, &ifr);
6151da177e4SLinus Torvalds 		rtnl_unlock();
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 		if (err)
6181da177e4SLinus Torvalds 			return err;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
6211da177e4SLinus Torvalds 			return -EFAULT;
6221da177e4SLinus Torvalds 		return 0;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (!tun)
6261da177e4SLinus Torvalds 		return -EBADFD;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	switch (cmd) {
6311da177e4SLinus Torvalds 	case TUNSETNOCSUM:
6321da177e4SLinus Torvalds 		/* Disable/Enable checksum */
6331da177e4SLinus Torvalds 		if (arg)
6341da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
6351da177e4SLinus Torvalds 		else
6361da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
6391da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
6401da177e4SLinus Torvalds 		break;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	case TUNSETPERSIST:
6431da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
6441da177e4SLinus Torvalds 		if (arg)
6451da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
6461da177e4SLinus Torvalds 		else
6471da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
650c6e991deSToyo Abe 		    tun->dev->name, arg ? "enabled" : "disabled");
6511da177e4SLinus Torvalds 		break;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	case TUNSETOWNER:
6541da177e4SLinus Torvalds 		/* Set owner of the device */
6551da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
6581da177e4SLinus Torvalds 		break;
6591da177e4SLinus Torvalds 
6608c644623SGuido Guenther 	case TUNSETGROUP:
6618c644623SGuido Guenther 		/* Set group of the device */
6628c644623SGuido Guenther 		tun->group= (gid_t) arg;
6638c644623SGuido Guenther 
6648c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
6658c644623SGuido Guenther 		break;
6668c644623SGuido Guenther 
667ff4cc3acSMike Kershaw 	case TUNSETLINK:
668ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
669ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
670ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
671ff4cc3acSMike Kershaw 				tun->dev->name);
672ff4cc3acSMike Kershaw 			return -EBUSY;
673ff4cc3acSMike Kershaw 		} else {
674ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
675ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
676ff4cc3acSMike Kershaw 		}
677ff4cc3acSMike Kershaw 		break;
678ff4cc3acSMike Kershaw 
6791da177e4SLinus Torvalds #ifdef TUN_DEBUG
6801da177e4SLinus Torvalds 	case TUNSETDEBUG:
6811da177e4SLinus Torvalds 		tun->debug = arg;
6821da177e4SLinus Torvalds 		break;
6831da177e4SLinus Torvalds #endif
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	case SIOCGIFFLAGS:
6861da177e4SLinus Torvalds 		ifr.ifr_flags = tun->if_flags;
6871da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
6881da177e4SLinus Torvalds 			return -EFAULT;
6891da177e4SLinus Torvalds 		return 0;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
6921da177e4SLinus Torvalds 		/** Set the character device's interface flags. Currently only
6931da177e4SLinus Torvalds 		 * IFF_PROMISC and IFF_ALLMULTI are used. */
6941da177e4SLinus Torvalds 		tun->if_flags = ifr.ifr_flags;
6951da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: interface flags 0x%lx\n",
6961da177e4SLinus Torvalds 				tun->dev->name, tun->if_flags);
6971da177e4SLinus Torvalds 		return 0;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
70036226a8dSBrian Braunstein 		/* Note: the actual net device's address may be different */
7011da177e4SLinus Torvalds 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
7021da177e4SLinus Torvalds 				min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
7031da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
7041da177e4SLinus Torvalds 			return -EFAULT;
7051da177e4SLinus Torvalds 		return 0;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
70836226a8dSBrian Braunstein 	{
70936226a8dSBrian Braunstein 		/* try to set the actual net device's hw address */
71040102371SKim B. Heino 		int ret;
71140102371SKim B. Heino 
71240102371SKim B. Heino 		rtnl_lock();
71340102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
71440102371SKim B. Heino 		rtnl_unlock();
71536226a8dSBrian Braunstein 
71636226a8dSBrian Braunstein 		if (ret == 0) {
7171da177e4SLinus Torvalds 			/** Set the character device's hardware address. This is used when
7181da177e4SLinus Torvalds 			 * filtering packets being sent from the network device to the character
7191da177e4SLinus Torvalds 			 * device. */
7201da177e4SLinus Torvalds 			memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
7211da177e4SLinus Torvalds 					min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
7221da177e4SLinus Torvalds 			DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
7231da177e4SLinus Torvalds 					tun->dev->name,
7241da177e4SLinus Torvalds 					tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
7251da177e4SLinus Torvalds 					tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
72636226a8dSBrian Braunstein 		}
72736226a8dSBrian Braunstein 
72836226a8dSBrian Braunstein 		return  ret;
72936226a8dSBrian Braunstein 	}
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	case SIOCADDMULTI:
7321da177e4SLinus Torvalds 		/** Add the specified group to the character device's multicast filter
7331da177e4SLinus Torvalds 		 * list. */
7341da177e4SLinus Torvalds 		add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
7350795af57SJoe Perches 		DBG(KERN_DEBUG "%s: add multi: %s\n",
7360795af57SJoe Perches 		    tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
7371da177e4SLinus Torvalds 		return 0;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	case SIOCDELMULTI:
7401da177e4SLinus Torvalds 		/** Remove the specified group from the character device's multicast
7411da177e4SLinus Torvalds 		 * filter list. */
7421da177e4SLinus Torvalds 		del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
7430795af57SJoe Perches 		DBG(KERN_DEBUG "%s: del multi: %s\n",
7440795af57SJoe Perches 		    tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
7451da177e4SLinus Torvalds 		return 0;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	default:
7481da177e4SLinus Torvalds 		return -EINVAL;
7491da177e4SLinus Torvalds 	};
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	return 0;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
7571da177e4SLinus Torvalds 	int ret;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	if (!tun)
7601da177e4SLinus Torvalds 		return -EBADFD;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
7651da177e4SLinus Torvalds 		return ret;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	if (on) {
768609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
7691da177e4SLinus Torvalds 		if (ret)
7701da177e4SLinus Torvalds 			return ret;
7711da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
7721da177e4SLinus Torvalds 	} else
7731da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	return 0;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
7811da177e4SLinus Torvalds 	file->private_data = NULL;
7821da177e4SLinus Torvalds 	return 0;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
7861da177e4SLinus Torvalds {
7871da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (!tun)
7901da177e4SLinus Torvalds 		return 0;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	tun_chr_fasync(-1, file, 0);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	rtnl_lock();
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	/* Detach from net device */
7991da177e4SLinus Torvalds 	file->private_data = NULL;
8001da177e4SLinus Torvalds 	tun->attached = 0;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/* Drop read queue */
8031da177e4SLinus Torvalds 	skb_queue_purge(&tun->readq);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	if (!(tun->flags & TUN_PERSIST)) {
8061da177e4SLinus Torvalds 		list_del(&tun->list);
8071da177e4SLinus Torvalds 		unregister_netdevice(tun->dev);
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	rtnl_unlock();
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	return 0;
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
815d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
8161da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
8171da177e4SLinus Torvalds 	.llseek = no_llseek,
818ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
819ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
820ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
821ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
8221da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
8231da177e4SLinus Torvalds 	.ioctl	= tun_chr_ioctl,
8241da177e4SLinus Torvalds 	.open	= tun_chr_open,
8251da177e4SLinus Torvalds 	.release = tun_chr_close,
8261da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
8271da177e4SLinus Torvalds };
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
8301da177e4SLinus Torvalds 	.minor = TUN_MINOR,
8311da177e4SLinus Torvalds 	.name = "tun",
8321da177e4SLinus Torvalds 	.fops = &tun_fops,
8331da177e4SLinus Torvalds };
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds /* ethtool interface */
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
8381da177e4SLinus Torvalds {
8391da177e4SLinus Torvalds 	cmd->supported		= 0;
8401da177e4SLinus Torvalds 	cmd->advertising	= 0;
8411da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
8421da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
8431da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
8441da177e4SLinus Torvalds 	cmd->phy_address	= 0;
8451da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
8461da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
8471da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
8481da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
8491da177e4SLinus Torvalds 	return 0;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
8571da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
8581da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
8611da177e4SLinus Torvalds 	case TUN_TUN_DEV:
8621da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
8631da177e4SLinus Torvalds 		break;
8641da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8651da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
8661da177e4SLinus Torvalds 		break;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
8711da177e4SLinus Torvalds {
8721da177e4SLinus Torvalds #ifdef TUN_DEBUG
8731da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8741da177e4SLinus Torvalds 	return tun->debug;
8751da177e4SLinus Torvalds #else
8761da177e4SLinus Torvalds 	return -EOPNOTSUPP;
8771da177e4SLinus Torvalds #endif
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds #ifdef TUN_DEBUG
8831da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8841da177e4SLinus Torvalds 	tun->debug = value;
8851da177e4SLinus Torvalds #endif
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8911da177e4SLinus Torvalds 	return tun->attached;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8971da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9031da177e4SLinus Torvalds 	if (data)
9041da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
9051da177e4SLinus Torvalds 	else
9061da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
9071da177e4SLinus Torvalds 	return 0;
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
9107282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
9111da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
9121da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
9131da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
9141da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
9151da177e4SLinus Torvalds 	.get_link	= tun_get_link,
9161da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
9171da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
9181da177e4SLinus Torvalds };
9191da177e4SLinus Torvalds 
92079d17604SPavel Emelyanov static int tun_init_net(struct net *net)
92179d17604SPavel Emelyanov {
92279d17604SPavel Emelyanov 	struct tun_net *tn;
92379d17604SPavel Emelyanov 
92479d17604SPavel Emelyanov 	tn = kmalloc(sizeof(*tn), GFP_KERNEL);
92579d17604SPavel Emelyanov 	if (tn == NULL)
92679d17604SPavel Emelyanov 		return -ENOMEM;
92779d17604SPavel Emelyanov 
92879d17604SPavel Emelyanov 	INIT_LIST_HEAD(&tn->dev_list);
92979d17604SPavel Emelyanov 
93079d17604SPavel Emelyanov 	if (net_assign_generic(net, tun_net_id, tn)) {
93179d17604SPavel Emelyanov 		kfree(tn);
93279d17604SPavel Emelyanov 		return -ENOMEM;
93379d17604SPavel Emelyanov 	}
93479d17604SPavel Emelyanov 
93579d17604SPavel Emelyanov 	return 0;
93679d17604SPavel Emelyanov }
93779d17604SPavel Emelyanov 
93879d17604SPavel Emelyanov static void tun_exit_net(struct net *net)
93979d17604SPavel Emelyanov {
94079d17604SPavel Emelyanov 	struct tun_net *tn;
941*d647a591SPavel Emelyanov 	struct tun_struct *tun, *nxt;
94279d17604SPavel Emelyanov 
94379d17604SPavel Emelyanov 	tn = net_generic(net, tun_net_id);
944*d647a591SPavel Emelyanov 
945*d647a591SPavel Emelyanov 	rtnl_lock();
946*d647a591SPavel Emelyanov 	list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
947*d647a591SPavel Emelyanov 		DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
948*d647a591SPavel Emelyanov 		unregister_netdevice(tun->dev);
949*d647a591SPavel Emelyanov 	}
950*d647a591SPavel Emelyanov 	rtnl_unlock();
951*d647a591SPavel Emelyanov 
95279d17604SPavel Emelyanov 	kfree(tn);
95379d17604SPavel Emelyanov }
95479d17604SPavel Emelyanov 
95579d17604SPavel Emelyanov static struct pernet_operations tun_net_ops = {
95679d17604SPavel Emelyanov 	.init = tun_init_net,
95779d17604SPavel Emelyanov 	.exit = tun_exit_net,
95879d17604SPavel Emelyanov };
95979d17604SPavel Emelyanov 
9601da177e4SLinus Torvalds static int __init tun_init(void)
9611da177e4SLinus Torvalds {
9621da177e4SLinus Torvalds 	int ret = 0;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
9651da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
9661da177e4SLinus Torvalds 
96779d17604SPavel Emelyanov 	ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
96879d17604SPavel Emelyanov 	if (ret) {
96979d17604SPavel Emelyanov 		printk(KERN_ERR "tun: Can't register pernet ops\n");
97079d17604SPavel Emelyanov 		goto err_pernet;
97179d17604SPavel Emelyanov 	}
97279d17604SPavel Emelyanov 
9731da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
97479d17604SPavel Emelyanov 	if (ret) {
9751da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
97679d17604SPavel Emelyanov 		goto err_misc;
97779d17604SPavel Emelyanov 	}
97879d17604SPavel Emelyanov 	return 0;
97979d17604SPavel Emelyanov 
98079d17604SPavel Emelyanov err_misc:
98179d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
98279d17604SPavel Emelyanov err_pernet:
9831da177e4SLinus Torvalds 	return ret;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds static void tun_cleanup(void)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
98979d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds module_init(tun_init);
9931da177e4SLinus Torvalds module_exit(tun_cleanup);
9941da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
9951da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
9961da177e4SLinus Torvalds MODULE_LICENSE("GPL");
9971da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
998