xref: /openbmc/linux/drivers/net/tun.c (revision fd3e05b6c82ebee06f888482975172028e89382d)
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>
51*fd3e05b6SArnd Bergmann #include <linux/smp_lock.h>
521da177e4SLinus Torvalds #include <linux/poll.h>
531da177e4SLinus Torvalds #include <linux/fcntl.h>
541da177e4SLinus Torvalds #include <linux/init.h>
551da177e4SLinus Torvalds #include <linux/skbuff.h>
561da177e4SLinus Torvalds #include <linux/netdevice.h>
571da177e4SLinus Torvalds #include <linux/etherdevice.h>
581da177e4SLinus Torvalds #include <linux/miscdevice.h>
591da177e4SLinus Torvalds #include <linux/ethtool.h>
601da177e4SLinus Torvalds #include <linux/rtnetlink.h>
611da177e4SLinus Torvalds #include <linux/if.h>
621da177e4SLinus Torvalds #include <linux/if_arp.h>
631da177e4SLinus Torvalds #include <linux/if_ether.h>
641da177e4SLinus Torvalds #include <linux/if_tun.h>
651da177e4SLinus Torvalds #include <linux/crc32.h>
66d647a591SPavel Emelyanov #include <linux/nsproxy.h>
67881d966bSEric W. Biederman #include <net/net_namespace.h>
6879d17604SPavel Emelyanov #include <net/netns/generic.h>
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #include <asm/system.h>
711da177e4SLinus Torvalds #include <asm/uaccess.h>
721da177e4SLinus Torvalds 
7314daa021SRusty Russell /* Uncomment to enable debugging */
7414daa021SRusty Russell /* #define TUN_DEBUG 1 */
7514daa021SRusty Russell 
761da177e4SLinus Torvalds #ifdef TUN_DEBUG
771da177e4SLinus Torvalds static int debug;
7814daa021SRusty Russell 
7914daa021SRusty Russell #define DBG  if(tun->debug)printk
8014daa021SRusty Russell #define DBG1 if(debug==2)printk
8114daa021SRusty Russell #else
8214daa021SRusty Russell #define DBG( a... )
8314daa021SRusty Russell #define DBG1( a... )
841da177e4SLinus Torvalds #endif
851da177e4SLinus Torvalds 
8614daa021SRusty Russell struct tun_struct {
8714daa021SRusty Russell 	struct list_head        list;
8814daa021SRusty Russell 	unsigned long 		flags;
8914daa021SRusty Russell 	int			attached;
9014daa021SRusty Russell 	uid_t			owner;
9114daa021SRusty Russell 	gid_t			group;
9214daa021SRusty Russell 
9314daa021SRusty Russell 	wait_queue_head_t	read_wait;
9414daa021SRusty Russell 	struct sk_buff_head	readq;
9514daa021SRusty Russell 
9614daa021SRusty Russell 	struct net_device	*dev;
9714daa021SRusty Russell 
9814daa021SRusty Russell 	struct fasync_struct    *fasync;
9914daa021SRusty Russell 
10014daa021SRusty Russell 	unsigned long if_flags;
10114daa021SRusty Russell 	u8 dev_addr[ETH_ALEN];
10214daa021SRusty Russell 	u32 chr_filter[2];
10314daa021SRusty Russell 	u32 net_filter[2];
10414daa021SRusty Russell 
10514daa021SRusty Russell #ifdef TUN_DEBUG
10614daa021SRusty Russell 	int debug;
10714daa021SRusty Russell #endif
10814daa021SRusty Russell };
10914daa021SRusty Russell 
1101da177e4SLinus Torvalds /* Network device part of the driver */
1111da177e4SLinus Torvalds 
11279d17604SPavel Emelyanov static unsigned int tun_net_id;
11379d17604SPavel Emelyanov struct tun_net {
11479d17604SPavel Emelyanov 	struct list_head dev_list;
11579d17604SPavel Emelyanov };
11679d17604SPavel Emelyanov 
1177282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* Net device open. */
1201da177e4SLinus Torvalds static int tun_net_open(struct net_device *dev)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	netif_start_queue(dev);
1231da177e4SLinus Torvalds 	return 0;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /* Net device close. */
1271da177e4SLinus Torvalds static int tun_net_close(struct net_device *dev)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	netif_stop_queue(dev);
1301da177e4SLinus Torvalds 	return 0;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /* Net device start xmit */
1341da177e4SLinus Torvalds static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/* Drop packet if interface is not attached */
1411da177e4SLinus Torvalds 	if (!tun->attached)
1421da177e4SLinus Torvalds 		goto drop;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	/* Packet dropping */
1451da177e4SLinus Torvalds 	if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
1461da177e4SLinus Torvalds 		if (!(tun->flags & TUN_ONE_QUEUE)) {
1471da177e4SLinus Torvalds 			/* Normal queueing mode. */
1481da177e4SLinus Torvalds 			/* Packet scheduler handles dropping of further packets. */
1491da177e4SLinus Torvalds 			netif_stop_queue(dev);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 			/* We won't see all dropped packets individually, so overrun
1521da177e4SLinus Torvalds 			 * error is more appropriate. */
15309f75cd7SJeff Garzik 			dev->stats.tx_fifo_errors++;
1541da177e4SLinus Torvalds 		} else {
1551da177e4SLinus Torvalds 			/* Single queue mode.
1561da177e4SLinus Torvalds 			 * Driver handles dropping of all packets itself. */
1571da177e4SLinus Torvalds 			goto drop;
1581da177e4SLinus Torvalds 		}
1591da177e4SLinus Torvalds 	}
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* Queue packet */
1621da177e4SLinus Torvalds 	skb_queue_tail(&tun->readq, skb);
1631da177e4SLinus Torvalds 	dev->trans_start = jiffies;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	/* Notify and wake up reader process */
1661da177e4SLinus Torvalds 	if (tun->flags & TUN_FASYNC)
1671da177e4SLinus Torvalds 		kill_fasync(&tun->fasync, SIGIO, POLL_IN);
1681da177e4SLinus Torvalds 	wake_up_interruptible(&tun->read_wait);
1691da177e4SLinus Torvalds 	return 0;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds drop:
17209f75cd7SJeff Garzik 	dev->stats.tx_dropped++;
1731da177e4SLinus Torvalds 	kfree_skb(skb);
1741da177e4SLinus Torvalds 	return 0;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /** Add the specified Ethernet address to this multicast filter. */
1781da177e4SLinus Torvalds static void
1791da177e4SLinus Torvalds add_multi(u32* filter, const u8* addr)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
1821da177e4SLinus Torvalds 	filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds /** Remove the specified Ethernet addres from this multicast filter. */
1861da177e4SLinus Torvalds static void
1871da177e4SLinus Torvalds del_multi(u32* filter, const u8* addr)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
1901da177e4SLinus Torvalds 	filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /** Update the list of multicast groups to which the network device belongs.
1941da177e4SLinus Torvalds  * This list is used to filter packets being sent from the character device to
1951da177e4SLinus Torvalds  * the network device. */
1961da177e4SLinus Torvalds static void
1971da177e4SLinus Torvalds tun_net_mclist(struct net_device *dev)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
2001da177e4SLinus Torvalds 	const struct dev_mc_list *mclist;
2011da177e4SLinus Torvalds 	int i;
2020795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
2031da177e4SLinus Torvalds 	DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
2041da177e4SLinus Torvalds 			dev->name, dev->mc_count);
2051da177e4SLinus Torvalds 	memset(tun->chr_filter, 0, sizeof tun->chr_filter);
2061da177e4SLinus Torvalds 	for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
2071da177e4SLinus Torvalds 			i++, mclist = mclist->next) {
2081da177e4SLinus Torvalds 		add_multi(tun->net_filter, mclist->dmi_addr);
2090795af57SJoe Perches 		DBG(KERN_DEBUG "%s: tun_net_mclist: %s\n",
2100795af57SJoe Perches 		    dev->name, print_mac(mac, mclist->dmi_addr));
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2144885a504SEd Swierk #define MIN_MTU 68
2154885a504SEd Swierk #define MAX_MTU 65535
2164885a504SEd Swierk 
2174885a504SEd Swierk static int
2184885a504SEd Swierk tun_net_change_mtu(struct net_device *dev, int new_mtu)
2194885a504SEd Swierk {
2204885a504SEd Swierk 	if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
2214885a504SEd Swierk 		return -EINVAL;
2224885a504SEd Swierk 	dev->mtu = new_mtu;
2234885a504SEd Swierk 	return 0;
2244885a504SEd Swierk }
2254885a504SEd Swierk 
2261da177e4SLinus Torvalds /* Initialize net device. */
2271da177e4SLinus Torvalds static void tun_net_init(struct net_device *dev)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
2321da177e4SLinus Torvalds 	case TUN_TUN_DEV:
2331da177e4SLinus Torvalds 		/* Point-to-Point TUN Device */
2341da177e4SLinus Torvalds 		dev->hard_header_len = 0;
2351da177e4SLinus Torvalds 		dev->addr_len = 0;
2361da177e4SLinus Torvalds 		dev->mtu = 1500;
2374885a504SEd Swierk 		dev->change_mtu = tun_net_change_mtu;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 		/* Zero header length */
2401da177e4SLinus Torvalds 		dev->type = ARPHRD_NONE;
2411da177e4SLinus Torvalds 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2421da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
2431da177e4SLinus Torvalds 		break;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	case TUN_TAP_DEV:
2461da177e4SLinus Torvalds 		/* Ethernet TAP Device */
2471da177e4SLinus Torvalds 		dev->set_multicast_list = tun_net_mclist;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 		ether_setup(dev);
2504885a504SEd Swierk 		dev->change_mtu = tun_net_change_mtu;
25136226a8dSBrian Braunstein 
25236226a8dSBrian Braunstein 		/* random address already created for us by tun_set_iff, use it */
25336226a8dSBrian Braunstein 		memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) );
25436226a8dSBrian Braunstein 
2551da177e4SLinus Torvalds 		dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
2561da177e4SLinus Torvalds 		break;
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds /* Character device part */
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /* Poll */
2631da177e4SLinus Torvalds static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
2661da177e4SLinus Torvalds 	unsigned int mask = POLLOUT | POLLWRNORM;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	if (!tun)
2691da177e4SLinus Torvalds 		return -EBADFD;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	poll_wait(file, &tun->read_wait, wait);
2741da177e4SLinus Torvalds 
275b03efcfbSDavid S. Miller 	if (!skb_queue_empty(&tun->readq))
2761da177e4SLinus Torvalds 		mask |= POLLIN | POLLRDNORM;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	return mask;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds /* Get packet from user space buffer */
2821da177e4SLinus Torvalds static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
2851da177e4SLinus Torvalds 	struct sk_buff *skb;
2861da177e4SLinus Torvalds 	size_t len = count, align = 0;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
2891da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) > count)
2901da177e4SLinus Torvalds 			return -EINVAL;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 		if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
2931da177e4SLinus Torvalds 			return -EFAULT;
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds 
296e01bf1c8SRusty Russell 	if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
2971da177e4SLinus Torvalds 		align = NET_IP_ALIGN;
298e01bf1c8SRusty Russell 		if (unlikely(len < ETH_HLEN))
299e01bf1c8SRusty Russell 			return -EINVAL;
300e01bf1c8SRusty Russell 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
30309f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
3041da177e4SLinus Torvalds 		return -ENOMEM;
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	if (align)
3081da177e4SLinus Torvalds 		skb_reserve(skb, align);
3098f22757eSDave Jones 	if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
31009f75cd7SJeff Garzik 		tun->dev->stats.rx_dropped++;
3118f22757eSDave Jones 		kfree_skb(skb);
3121da177e4SLinus Torvalds 		return -EFAULT;
3138f22757eSDave Jones 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
3161da177e4SLinus Torvalds 	case TUN_TUN_DEV:
317459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(skb);
3181da177e4SLinus Torvalds 		skb->protocol = pi.proto;
3194c13eb66SArnaldo Carvalho de Melo 		skb->dev = tun->dev;
3201da177e4SLinus Torvalds 		break;
3211da177e4SLinus Torvalds 	case TUN_TAP_DEV:
3221da177e4SLinus Torvalds 		skb->protocol = eth_type_trans(skb, tun->dev);
3231da177e4SLinus Torvalds 		break;
3241da177e4SLinus Torvalds 	};
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	if (tun->flags & TUN_NOCHECKSUM)
3271da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	netif_rx_ni(skb);
3301da177e4SLinus Torvalds 	tun->dev->last_rx = jiffies;
3311da177e4SLinus Torvalds 
33209f75cd7SJeff Garzik 	tun->dev->stats.rx_packets++;
33309f75cd7SJeff Garzik 	tun->dev->stats.rx_bytes += len;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return count;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
338ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
339ee0b3e67SBadari Pulavarty 			      unsigned long count, loff_t pos)
3401da177e4SLinus Torvalds {
341ee0b3e67SBadari Pulavarty 	struct tun_struct *tun = iocb->ki_filp->private_data;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (!tun)
3441da177e4SLinus Torvalds 		return -EBADFD;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
3471da177e4SLinus Torvalds 
34852427c9dSAkinobu Mita 	return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds /* Put packet to the user space buffer */
3521da177e4SLinus Torvalds static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
3531da177e4SLinus Torvalds 				       struct sk_buff *skb,
3541da177e4SLinus Torvalds 				       struct iovec *iv, int len)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	struct tun_pi pi = { 0, skb->protocol };
3571da177e4SLinus Torvalds 	ssize_t total = 0;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (!(tun->flags & TUN_NO_PI)) {
3601da177e4SLinus Torvalds 		if ((len -= sizeof(pi)) < 0)
3611da177e4SLinus Torvalds 			return -EINVAL;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 		if (len < skb->len) {
3641da177e4SLinus Torvalds 			/* Packet will be striped */
3651da177e4SLinus Torvalds 			pi.flags |= TUN_PKT_STRIP;
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 		if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
3691da177e4SLinus Torvalds 			return -EFAULT;
3701da177e4SLinus Torvalds 		total += sizeof(pi);
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	len = min_t(int, skb->len, len);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	skb_copy_datagram_iovec(skb, 0, iv, len);
3761da177e4SLinus Torvalds 	total += len;
3771da177e4SLinus Torvalds 
37809f75cd7SJeff Garzik 	tun->dev->stats.tx_packets++;
37909f75cd7SJeff Garzik 	tun->dev->stats.tx_bytes += len;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	return total;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
384ee0b3e67SBadari Pulavarty static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
385ee0b3e67SBadari Pulavarty 			    unsigned long count, loff_t pos)
3861da177e4SLinus Torvalds {
387ee0b3e67SBadari Pulavarty 	struct file *file = iocb->ki_filp;
3881da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
3891da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
3901da177e4SLinus Torvalds 	struct sk_buff *skb;
3911da177e4SLinus Torvalds 	ssize_t len, ret = 0;
3920795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	if (!tun)
3951da177e4SLinus Torvalds 		return -EBADFD;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
3981da177e4SLinus Torvalds 
39952427c9dSAkinobu Mita 	len = iov_length(iv, count);
4001da177e4SLinus Torvalds 	if (len < 0)
4011da177e4SLinus Torvalds 		return -EINVAL;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	add_wait_queue(&tun->read_wait, &wait);
4041da177e4SLinus Torvalds 	while (len) {
4051da177e4SLinus Torvalds 		const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4061da177e4SLinus Torvalds 		u8 addr[ ETH_ALEN];
4071da177e4SLinus Torvalds 		int bit_nr;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 		/* Read frames from the queue */
4121da177e4SLinus Torvalds 		if (!(skb=skb_dequeue(&tun->readq))) {
4131da177e4SLinus Torvalds 			if (file->f_flags & O_NONBLOCK) {
4141da177e4SLinus Torvalds 				ret = -EAGAIN;
4151da177e4SLinus Torvalds 				break;
4161da177e4SLinus Torvalds 			}
4171da177e4SLinus Torvalds 			if (signal_pending(current)) {
4181da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
4191da177e4SLinus Torvalds 				break;
4201da177e4SLinus Torvalds 			}
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 			/* Nothing to read, let's sleep */
4231da177e4SLinus Torvalds 			schedule();
4241da177e4SLinus Torvalds 			continue;
4251da177e4SLinus Torvalds 		}
4261da177e4SLinus Torvalds 		netif_wake_queue(tun->dev);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 		/** Decide whether to accept this packet. This code is designed to
4291da177e4SLinus Torvalds 		 * behave identically to an Ethernet interface. Accept the packet if
4301da177e4SLinus Torvalds 		 * - we are promiscuous.
4311da177e4SLinus Torvalds 		 * - the packet is addressed to us.
4321da177e4SLinus Torvalds 		 * - the packet is broadcast.
4331da177e4SLinus Torvalds 		 * - the packet is multicast and
4341da177e4SLinus Torvalds 		 *   - we are multicast promiscous.
4351da177e4SLinus Torvalds 		 *   - we belong to the multicast group.
4361da177e4SLinus Torvalds 		 */
437d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
438d626f62bSArnaldo Carvalho de Melo 								   skb->len));
4391da177e4SLinus Torvalds 		bit_nr = ether_crc(sizeof addr, addr) >> 26;
4401da177e4SLinus Torvalds 		if ((tun->if_flags & IFF_PROMISC) ||
4411da177e4SLinus Torvalds 				memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
4421da177e4SLinus Torvalds 				memcmp(addr, ones, sizeof addr) == 0 ||
4431da177e4SLinus Torvalds 				(((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
4441da177e4SLinus Torvalds 				  (addr[0] == 0x33 && addr[1] == 0x33)) &&
4451da177e4SLinus Torvalds 				 ((tun->if_flags & IFF_ALLMULTI) ||
4461da177e4SLinus Torvalds 				  (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
4470795af57SJoe Perches 			DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %s\n",
4480795af57SJoe Perches 					tun->dev->name, print_mac(mac, addr));
4491da177e4SLinus Torvalds 			ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
4501da177e4SLinus Torvalds 			kfree_skb(skb);
4511da177e4SLinus Torvalds 			break;
4521da177e4SLinus Torvalds 		} else {
4530795af57SJoe Perches 			DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %s\n",
4540795af57SJoe Perches 					tun->dev->name, print_mac(mac, addr));
4551da177e4SLinus Torvalds 			kfree_skb(skb);
4561da177e4SLinus Torvalds 			continue;
4571da177e4SLinus Torvalds 		}
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	current->state = TASK_RUNNING;
4611da177e4SLinus Torvalds 	remove_wait_queue(&tun->read_wait, &wait);
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	return ret;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds static void tun_setup(struct net_device *dev)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	skb_queue_head_init(&tun->readq);
4711da177e4SLinus Torvalds 	init_waitqueue_head(&tun->read_wait);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	tun->owner = -1;
4748c644623SGuido Guenther 	tun->group = -1;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	dev->open = tun_net_open;
4771da177e4SLinus Torvalds 	dev->hard_start_xmit = tun_net_xmit;
4781da177e4SLinus Torvalds 	dev->stop = tun_net_close;
4791da177e4SLinus Torvalds 	dev->ethtool_ops = &tun_ethtool_ops;
4801da177e4SLinus Torvalds 	dev->destructor = free_netdev;
481fc54c658SPavel Emelyanov 	dev->features |= NETIF_F_NETNS_LOCAL;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484d647a591SPavel Emelyanov static struct tun_struct *tun_get_by_name(struct tun_net *tn, const char *name)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	struct tun_struct *tun;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	ASSERT_RTNL();
489d647a591SPavel Emelyanov 	list_for_each_entry(tun, &tn->dev_list, list) {
4901da177e4SLinus Torvalds 		if (!strncmp(tun->dev->name, name, IFNAMSIZ))
4911da177e4SLinus Torvalds 		    return tun;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	return NULL;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
497d647a591SPavel Emelyanov static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
4981da177e4SLinus Torvalds {
499d647a591SPavel Emelyanov 	struct tun_net *tn;
5001da177e4SLinus Torvalds 	struct tun_struct *tun;
5011da177e4SLinus Torvalds 	struct net_device *dev;
5021da177e4SLinus Torvalds 	int err;
5031da177e4SLinus Torvalds 
504d647a591SPavel Emelyanov 	tn = net_generic(net, tun_net_id);
505d647a591SPavel Emelyanov 	tun = tun_get_by_name(tn, ifr->ifr_name);
5061da177e4SLinus Torvalds 	if (tun) {
5071da177e4SLinus Torvalds 		if (tun->attached)
5081da177e4SLinus Torvalds 			return -EBUSY;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 		/* Check permissions */
5118c644623SGuido Guenther 		if (((tun->owner != -1 &&
5128c644623SGuido Guenther 		      current->euid != tun->owner) ||
5138c644623SGuido Guenther 		     (tun->group != -1 &&
5148c644623SGuido Guenther 		      current->egid != tun->group)) &&
5158c644623SGuido Guenther 		     !capable(CAP_NET_ADMIN))
5161da177e4SLinus Torvalds 			return -EPERM;
5171da177e4SLinus Torvalds 	}
518d647a591SPavel Emelyanov 	else if (__dev_get_by_name(net, ifr->ifr_name))
5191da177e4SLinus Torvalds 		return -EINVAL;
5201da177e4SLinus Torvalds 	else {
5211da177e4SLinus Torvalds 		char *name;
5221da177e4SLinus Torvalds 		unsigned long flags = 0;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 		err = -EINVAL;
5251da177e4SLinus Torvalds 
526ca6bb5d7SDavid Woodhouse 		if (!capable(CAP_NET_ADMIN))
527ca6bb5d7SDavid Woodhouse 			return -EPERM;
528ca6bb5d7SDavid Woodhouse 
5291da177e4SLinus Torvalds 		/* Set dev type */
5301da177e4SLinus Torvalds 		if (ifr->ifr_flags & IFF_TUN) {
5311da177e4SLinus Torvalds 			/* TUN device */
5321da177e4SLinus Torvalds 			flags |= TUN_TUN_DEV;
5331da177e4SLinus Torvalds 			name = "tun%d";
5341da177e4SLinus Torvalds 		} else if (ifr->ifr_flags & IFF_TAP) {
5351da177e4SLinus Torvalds 			/* TAP device */
5361da177e4SLinus Torvalds 			flags |= TUN_TAP_DEV;
5371da177e4SLinus Torvalds 			name = "tap%d";
5381da177e4SLinus Torvalds 		} else
5391da177e4SLinus Torvalds 			goto failed;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 		if (*ifr->ifr_name)
5421da177e4SLinus Torvalds 			name = ifr->ifr_name;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		dev = alloc_netdev(sizeof(struct tun_struct), name,
5451da177e4SLinus Torvalds 				   tun_setup);
5461da177e4SLinus Torvalds 		if (!dev)
5471da177e4SLinus Torvalds 			return -ENOMEM;
5481da177e4SLinus Torvalds 
549fc54c658SPavel Emelyanov 		dev_net_set(dev, net);
5501da177e4SLinus Torvalds 		tun = netdev_priv(dev);
5511da177e4SLinus Torvalds 		tun->dev = dev;
5521da177e4SLinus Torvalds 		tun->flags = flags;
5531da177e4SLinus Torvalds 		/* Be promiscuous by default to maintain previous behaviour. */
5541da177e4SLinus Torvalds 		tun->if_flags = IFF_PROMISC;
5551da177e4SLinus Torvalds 		/* Generate random Ethernet address. */
556a3edb083SAl Viro 		*(__be16 *)tun->dev_addr = htons(0x00FF);
5571da177e4SLinus Torvalds 		get_random_bytes(tun->dev_addr + sizeof(u16), 4);
5581da177e4SLinus Torvalds 		memset(tun->chr_filter, 0, sizeof tun->chr_filter);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 		tun_net_init(dev);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 		if (strchr(dev->name, '%')) {
5631da177e4SLinus Torvalds 			err = dev_alloc_name(dev, dev->name);
5641da177e4SLinus Torvalds 			if (err < 0)
5651da177e4SLinus Torvalds 				goto err_free_dev;
5661da177e4SLinus Torvalds 		}
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 		err = register_netdevice(tun->dev);
5691da177e4SLinus Torvalds 		if (err < 0)
5701da177e4SLinus Torvalds 			goto err_free_dev;
5711da177e4SLinus Torvalds 
572d647a591SPavel Emelyanov 		list_add(&tun->list, &tn->dev_list);
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_NO_PI)
5781da177e4SLinus Torvalds 		tun->flags |= TUN_NO_PI;
579a26af1e0SNathaniel Filardo 	else
580a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_NO_PI;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	if (ifr->ifr_flags & IFF_ONE_QUEUE)
5831da177e4SLinus Torvalds 		tun->flags |= TUN_ONE_QUEUE;
584a26af1e0SNathaniel Filardo 	else
585a26af1e0SNathaniel Filardo 		tun->flags &= ~TUN_ONE_QUEUE;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	file->private_data = tun;
5881da177e4SLinus Torvalds 	tun->attached = 1;
589fc54c658SPavel Emelyanov 	get_net(dev_net(tun->dev));
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	strcpy(ifr->ifr_name, tun->dev->name);
5921da177e4SLinus Torvalds 	return 0;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds  err_free_dev:
5951da177e4SLinus Torvalds 	free_netdev(dev);
5961da177e4SLinus Torvalds  failed:
5971da177e4SLinus Torvalds 	return err;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds static int tun_chr_ioctl(struct inode *inode, struct file *file,
6011da177e4SLinus Torvalds 			 unsigned int cmd, unsigned long arg)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
6041da177e4SLinus Torvalds 	void __user* argp = (void __user*)arg;
6051da177e4SLinus Torvalds 	struct ifreq ifr;
6060795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
6091da177e4SLinus Torvalds 		if (copy_from_user(&ifr, argp, sizeof ifr))
6101da177e4SLinus Torvalds 			return -EFAULT;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (cmd == TUNSETIFF && !tun) {
6131da177e4SLinus Torvalds 		int err;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 		ifr.ifr_name[IFNAMSIZ-1] = '\0';
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 		rtnl_lock();
618d647a591SPavel Emelyanov 		err = tun_set_iff(current->nsproxy->net_ns, file, &ifr);
6191da177e4SLinus Torvalds 		rtnl_unlock();
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 		if (err)
6221da177e4SLinus Torvalds 			return err;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 		if (copy_to_user(argp, &ifr, sizeof(ifr)))
6251da177e4SLinus Torvalds 			return -EFAULT;
6261da177e4SLinus Torvalds 		return 0;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	if (!tun)
6301da177e4SLinus Torvalds 		return -EBADFD;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	switch (cmd) {
6351da177e4SLinus Torvalds 	case TUNSETNOCSUM:
6361da177e4SLinus Torvalds 		/* Disable/Enable checksum */
6371da177e4SLinus Torvalds 		if (arg)
6381da177e4SLinus Torvalds 			tun->flags |= TUN_NOCHECKSUM;
6391da177e4SLinus Torvalds 		else
6401da177e4SLinus Torvalds 			tun->flags &= ~TUN_NOCHECKSUM;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: checksum %s\n",
6431da177e4SLinus Torvalds 		    tun->dev->name, arg ? "disabled" : "enabled");
6441da177e4SLinus Torvalds 		break;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	case TUNSETPERSIST:
6471da177e4SLinus Torvalds 		/* Disable/Enable persist mode */
6481da177e4SLinus Torvalds 		if (arg)
6491da177e4SLinus Torvalds 			tun->flags |= TUN_PERSIST;
6501da177e4SLinus Torvalds 		else
6511da177e4SLinus Torvalds 			tun->flags &= ~TUN_PERSIST;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: persist %s\n",
654c6e991deSToyo Abe 		    tun->dev->name, arg ? "enabled" : "disabled");
6551da177e4SLinus Torvalds 		break;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	case TUNSETOWNER:
6581da177e4SLinus Torvalds 		/* Set owner of the device */
6591da177e4SLinus Torvalds 		tun->owner = (uid_t) arg;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
6621da177e4SLinus Torvalds 		break;
6631da177e4SLinus Torvalds 
6648c644623SGuido Guenther 	case TUNSETGROUP:
6658c644623SGuido Guenther 		/* Set group of the device */
6668c644623SGuido Guenther 		tun->group= (gid_t) arg;
6678c644623SGuido Guenther 
6688c644623SGuido Guenther 		DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
6698c644623SGuido Guenther 		break;
6708c644623SGuido Guenther 
671ff4cc3acSMike Kershaw 	case TUNSETLINK:
67248abfe05SDavid S. Miller 	{
67348abfe05SDavid S. Miller 		int ret;
67448abfe05SDavid S. Miller 
675ff4cc3acSMike Kershaw 		/* Only allow setting the type when the interface is down */
67648abfe05SDavid S. Miller 		rtnl_lock();
677ff4cc3acSMike Kershaw 		if (tun->dev->flags & IFF_UP) {
678ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
679ff4cc3acSMike Kershaw 				tun->dev->name);
68048abfe05SDavid S. Miller 			ret = -EBUSY;
681ff4cc3acSMike Kershaw 		} else {
682ff4cc3acSMike Kershaw 			tun->dev->type = (int) arg;
683ff4cc3acSMike Kershaw 			DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
68448abfe05SDavid S. Miller 			ret = 0;
685ff4cc3acSMike Kershaw 		}
68648abfe05SDavid S. Miller 		rtnl_unlock();
68748abfe05SDavid S. Miller 		return ret;
68848abfe05SDavid S. Miller 	}
689ff4cc3acSMike Kershaw 
6901da177e4SLinus Torvalds #ifdef TUN_DEBUG
6911da177e4SLinus Torvalds 	case TUNSETDEBUG:
6921da177e4SLinus Torvalds 		tun->debug = arg;
6931da177e4SLinus Torvalds 		break;
6941da177e4SLinus Torvalds #endif
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	case SIOCGIFFLAGS:
6971da177e4SLinus Torvalds 		ifr.ifr_flags = tun->if_flags;
6981da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
6991da177e4SLinus Torvalds 			return -EFAULT;
7001da177e4SLinus Torvalds 		return 0;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
7031da177e4SLinus Torvalds 		/** Set the character device's interface flags. Currently only
7041da177e4SLinus Torvalds 		 * IFF_PROMISC and IFF_ALLMULTI are used. */
7051da177e4SLinus Torvalds 		tun->if_flags = ifr.ifr_flags;
7061da177e4SLinus Torvalds 		DBG(KERN_INFO "%s: interface flags 0x%lx\n",
7071da177e4SLinus Torvalds 				tun->dev->name, tun->if_flags);
7081da177e4SLinus Torvalds 		return 0;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	case SIOCGIFHWADDR:
71136226a8dSBrian Braunstein 		/* Note: the actual net device's address may be different */
7121da177e4SLinus Torvalds 		memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
7131da177e4SLinus Torvalds 				min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
7141da177e4SLinus Torvalds 		if (copy_to_user( argp, &ifr, sizeof ifr))
7151da177e4SLinus Torvalds 			return -EFAULT;
7161da177e4SLinus Torvalds 		return 0;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	case SIOCSIFHWADDR:
71936226a8dSBrian Braunstein 	{
72036226a8dSBrian Braunstein 		/* try to set the actual net device's hw address */
72140102371SKim B. Heino 		int ret;
72240102371SKim B. Heino 
72340102371SKim B. Heino 		rtnl_lock();
72440102371SKim B. Heino 		ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
72540102371SKim B. Heino 		rtnl_unlock();
72636226a8dSBrian Braunstein 
72736226a8dSBrian Braunstein 		if (ret == 0) {
7281da177e4SLinus Torvalds 			/** Set the character device's hardware address. This is used when
7291da177e4SLinus Torvalds 			 * filtering packets being sent from the network device to the character
7301da177e4SLinus Torvalds 			 * device. */
7311da177e4SLinus Torvalds 			memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
7321da177e4SLinus Torvalds 					min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
7331da177e4SLinus Torvalds 			DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
7341da177e4SLinus Torvalds 					tun->dev->name,
7351da177e4SLinus Torvalds 					tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
7361da177e4SLinus Torvalds 					tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
73736226a8dSBrian Braunstein 		}
73836226a8dSBrian Braunstein 
73936226a8dSBrian Braunstein 		return  ret;
74036226a8dSBrian Braunstein 	}
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	case SIOCADDMULTI:
7431da177e4SLinus Torvalds 		/** Add the specified group to the character device's multicast filter
7441da177e4SLinus Torvalds 		 * list. */
7459edb74ccSDavid S. Miller 		rtnl_lock();
7469edb74ccSDavid S. Miller 		netif_tx_lock_bh(tun->dev);
7471da177e4SLinus Torvalds 		add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
7489edb74ccSDavid S. Miller 		netif_tx_unlock_bh(tun->dev);
7499edb74ccSDavid S. Miller 		rtnl_unlock();
7509edb74ccSDavid S. Miller 
7510795af57SJoe Perches 		DBG(KERN_DEBUG "%s: add multi: %s\n",
7520795af57SJoe Perches 		    tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
7531da177e4SLinus Torvalds 		return 0;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	case SIOCDELMULTI:
7561da177e4SLinus Torvalds 		/** Remove the specified group from the character device's multicast
7571da177e4SLinus Torvalds 		 * filter list. */
7589edb74ccSDavid S. Miller 		rtnl_lock();
7599edb74ccSDavid S. Miller 		netif_tx_lock_bh(tun->dev);
7601da177e4SLinus Torvalds 		del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
7619edb74ccSDavid S. Miller 		netif_tx_unlock_bh(tun->dev);
7629edb74ccSDavid S. Miller 		rtnl_unlock();
7639edb74ccSDavid S. Miller 
7640795af57SJoe Perches 		DBG(KERN_DEBUG "%s: del multi: %s\n",
7650795af57SJoe Perches 		    tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
7661da177e4SLinus Torvalds 		return 0;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	default:
7691da177e4SLinus Torvalds 		return -EINVAL;
7701da177e4SLinus Torvalds 	};
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	return 0;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds static int tun_chr_fasync(int fd, struct file *file, int on)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
7781da177e4SLinus Torvalds 	int ret;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	if (!tun)
7811da177e4SLinus Torvalds 		return -EBADFD;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
7861da177e4SLinus Torvalds 		return ret;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (on) {
789609d7fa9SEric W. Biederman 		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
7901da177e4SLinus Torvalds 		if (ret)
7911da177e4SLinus Torvalds 			return ret;
7921da177e4SLinus Torvalds 		tun->flags |= TUN_FASYNC;
7931da177e4SLinus Torvalds 	} else
7941da177e4SLinus Torvalds 		tun->flags &= ~TUN_FASYNC;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	return 0;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static int tun_chr_open(struct inode *inode, struct file * file)
8001da177e4SLinus Torvalds {
801*fd3e05b6SArnd Bergmann 	cycle_kernel_lock();
8021da177e4SLinus Torvalds 	DBG1(KERN_INFO "tunX: tun_chr_open\n");
8031da177e4SLinus Torvalds 	file->private_data = NULL;
8041da177e4SLinus Torvalds 	return 0;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds static int tun_chr_close(struct inode *inode, struct file *file)
8081da177e4SLinus Torvalds {
8091da177e4SLinus Torvalds 	struct tun_struct *tun = file->private_data;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	if (!tun)
8121da177e4SLinus Torvalds 		return 0;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	tun_chr_fasync(-1, file, 0);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	rtnl_lock();
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	/* Detach from net device */
8211da177e4SLinus Torvalds 	file->private_data = NULL;
8221da177e4SLinus Torvalds 	tun->attached = 0;
823fc54c658SPavel Emelyanov 	put_net(dev_net(tun->dev));
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	/* Drop read queue */
8261da177e4SLinus Torvalds 	skb_queue_purge(&tun->readq);
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	if (!(tun->flags & TUN_PERSIST)) {
8291da177e4SLinus Torvalds 		list_del(&tun->list);
8301da177e4SLinus Torvalds 		unregister_netdevice(tun->dev);
8311da177e4SLinus Torvalds 	}
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	rtnl_unlock();
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	return 0;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
838d54b1fdbSArjan van de Ven static const struct file_operations tun_fops = {
8391da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
8401da177e4SLinus Torvalds 	.llseek = no_llseek,
841ee0b3e67SBadari Pulavarty 	.read  = do_sync_read,
842ee0b3e67SBadari Pulavarty 	.aio_read  = tun_chr_aio_read,
843ee0b3e67SBadari Pulavarty 	.write = do_sync_write,
844ee0b3e67SBadari Pulavarty 	.aio_write = tun_chr_aio_write,
8451da177e4SLinus Torvalds 	.poll	= tun_chr_poll,
8461da177e4SLinus Torvalds 	.ioctl	= tun_chr_ioctl,
8471da177e4SLinus Torvalds 	.open	= tun_chr_open,
8481da177e4SLinus Torvalds 	.release = tun_chr_close,
8491da177e4SLinus Torvalds 	.fasync = tun_chr_fasync
8501da177e4SLinus Torvalds };
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds static struct miscdevice tun_miscdev = {
8531da177e4SLinus Torvalds 	.minor = TUN_MINOR,
8541da177e4SLinus Torvalds 	.name = "tun",
8551da177e4SLinus Torvalds 	.fops = &tun_fops,
8561da177e4SLinus Torvalds };
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds /* ethtool interface */
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
8611da177e4SLinus Torvalds {
8621da177e4SLinus Torvalds 	cmd->supported		= 0;
8631da177e4SLinus Torvalds 	cmd->advertising	= 0;
8641da177e4SLinus Torvalds 	cmd->speed		= SPEED_10;
8651da177e4SLinus Torvalds 	cmd->duplex		= DUPLEX_FULL;
8661da177e4SLinus Torvalds 	cmd->port		= PORT_TP;
8671da177e4SLinus Torvalds 	cmd->phy_address	= 0;
8681da177e4SLinus Torvalds 	cmd->transceiver	= XCVR_INTERNAL;
8691da177e4SLinus Torvalds 	cmd->autoneg		= AUTONEG_DISABLE;
8701da177e4SLinus Torvalds 	cmd->maxtxpkt		= 0;
8711da177e4SLinus Torvalds 	cmd->maxrxpkt		= 0;
8721da177e4SLinus Torvalds 	return 0;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	strcpy(info->driver, DRV_NAME);
8801da177e4SLinus Torvalds 	strcpy(info->version, DRV_VERSION);
8811da177e4SLinus Torvalds 	strcpy(info->fw_version, "N/A");
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	switch (tun->flags & TUN_TYPE_MASK) {
8841da177e4SLinus Torvalds 	case TUN_TUN_DEV:
8851da177e4SLinus Torvalds 		strcpy(info->bus_info, "tun");
8861da177e4SLinus Torvalds 		break;
8871da177e4SLinus Torvalds 	case TUN_TAP_DEV:
8881da177e4SLinus Torvalds 		strcpy(info->bus_info, "tap");
8891da177e4SLinus Torvalds 		break;
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds static u32 tun_get_msglevel(struct net_device *dev)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds #ifdef TUN_DEBUG
8961da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
8971da177e4SLinus Torvalds 	return tun->debug;
8981da177e4SLinus Torvalds #else
8991da177e4SLinus Torvalds 	return -EOPNOTSUPP;
9001da177e4SLinus Torvalds #endif
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds static void tun_set_msglevel(struct net_device *dev, u32 value)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds #ifdef TUN_DEBUG
9061da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9071da177e4SLinus Torvalds 	tun->debug = value;
9081da177e4SLinus Torvalds #endif
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds static u32 tun_get_link(struct net_device *dev)
9121da177e4SLinus Torvalds {
9131da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9141da177e4SLinus Torvalds 	return tun->attached;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds static u32 tun_get_rx_csum(struct net_device *dev)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9201da177e4SLinus Torvalds 	return (tun->flags & TUN_NOCHECKSUM) == 0;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds static int tun_set_rx_csum(struct net_device *dev, u32 data)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct tun_struct *tun = netdev_priv(dev);
9261da177e4SLinus Torvalds 	if (data)
9271da177e4SLinus Torvalds 		tun->flags &= ~TUN_NOCHECKSUM;
9281da177e4SLinus Torvalds 	else
9291da177e4SLinus Torvalds 		tun->flags |= TUN_NOCHECKSUM;
9301da177e4SLinus Torvalds 	return 0;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9337282d491SJeff Garzik static const struct ethtool_ops tun_ethtool_ops = {
9341da177e4SLinus Torvalds 	.get_settings	= tun_get_settings,
9351da177e4SLinus Torvalds 	.get_drvinfo	= tun_get_drvinfo,
9361da177e4SLinus Torvalds 	.get_msglevel	= tun_get_msglevel,
9371da177e4SLinus Torvalds 	.set_msglevel	= tun_set_msglevel,
9381da177e4SLinus Torvalds 	.get_link	= tun_get_link,
9391da177e4SLinus Torvalds 	.get_rx_csum	= tun_get_rx_csum,
9401da177e4SLinus Torvalds 	.set_rx_csum	= tun_set_rx_csum
9411da177e4SLinus Torvalds };
9421da177e4SLinus Torvalds 
94379d17604SPavel Emelyanov static int tun_init_net(struct net *net)
94479d17604SPavel Emelyanov {
94579d17604SPavel Emelyanov 	struct tun_net *tn;
94679d17604SPavel Emelyanov 
94779d17604SPavel Emelyanov 	tn = kmalloc(sizeof(*tn), GFP_KERNEL);
94879d17604SPavel Emelyanov 	if (tn == NULL)
94979d17604SPavel Emelyanov 		return -ENOMEM;
95079d17604SPavel Emelyanov 
95179d17604SPavel Emelyanov 	INIT_LIST_HEAD(&tn->dev_list);
95279d17604SPavel Emelyanov 
95379d17604SPavel Emelyanov 	if (net_assign_generic(net, tun_net_id, tn)) {
95479d17604SPavel Emelyanov 		kfree(tn);
95579d17604SPavel Emelyanov 		return -ENOMEM;
95679d17604SPavel Emelyanov 	}
95779d17604SPavel Emelyanov 
95879d17604SPavel Emelyanov 	return 0;
95979d17604SPavel Emelyanov }
96079d17604SPavel Emelyanov 
96179d17604SPavel Emelyanov static void tun_exit_net(struct net *net)
96279d17604SPavel Emelyanov {
96379d17604SPavel Emelyanov 	struct tun_net *tn;
964d647a591SPavel Emelyanov 	struct tun_struct *tun, *nxt;
96579d17604SPavel Emelyanov 
96679d17604SPavel Emelyanov 	tn = net_generic(net, tun_net_id);
967d647a591SPavel Emelyanov 
968d647a591SPavel Emelyanov 	rtnl_lock();
969d647a591SPavel Emelyanov 	list_for_each_entry_safe(tun, nxt, &tn->dev_list, list) {
970d647a591SPavel Emelyanov 		DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
971d647a591SPavel Emelyanov 		unregister_netdevice(tun->dev);
972d647a591SPavel Emelyanov 	}
973d647a591SPavel Emelyanov 	rtnl_unlock();
974d647a591SPavel Emelyanov 
97579d17604SPavel Emelyanov 	kfree(tn);
97679d17604SPavel Emelyanov }
97779d17604SPavel Emelyanov 
97879d17604SPavel Emelyanov static struct pernet_operations tun_net_ops = {
97979d17604SPavel Emelyanov 	.init = tun_init_net,
98079d17604SPavel Emelyanov 	.exit = tun_exit_net,
98179d17604SPavel Emelyanov };
98279d17604SPavel Emelyanov 
9831da177e4SLinus Torvalds static int __init tun_init(void)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	int ret = 0;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
9881da177e4SLinus Torvalds 	printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
9891da177e4SLinus Torvalds 
99079d17604SPavel Emelyanov 	ret = register_pernet_gen_device(&tun_net_id, &tun_net_ops);
99179d17604SPavel Emelyanov 	if (ret) {
99279d17604SPavel Emelyanov 		printk(KERN_ERR "tun: Can't register pernet ops\n");
99379d17604SPavel Emelyanov 		goto err_pernet;
99479d17604SPavel Emelyanov 	}
99579d17604SPavel Emelyanov 
9961da177e4SLinus Torvalds 	ret = misc_register(&tun_miscdev);
99779d17604SPavel Emelyanov 	if (ret) {
9981da177e4SLinus Torvalds 		printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
99979d17604SPavel Emelyanov 		goto err_misc;
100079d17604SPavel Emelyanov 	}
100179d17604SPavel Emelyanov 	return 0;
100279d17604SPavel Emelyanov 
100379d17604SPavel Emelyanov err_misc:
100479d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
100579d17604SPavel Emelyanov err_pernet:
10061da177e4SLinus Torvalds 	return ret;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds static void tun_cleanup(void)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	misc_deregister(&tun_miscdev);
101279d17604SPavel Emelyanov 	unregister_pernet_gen_device(tun_net_id, &tun_net_ops);
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds module_init(tun_init);
10161da177e4SLinus Torvalds module_exit(tun_cleanup);
10171da177e4SLinus Torvalds MODULE_DESCRIPTION(DRV_DESCRIPTION);
10181da177e4SLinus Torvalds MODULE_AUTHOR(DRV_COPYRIGHT);
10191da177e4SLinus Torvalds MODULE_LICENSE("GPL");
10201da177e4SLinus Torvalds MODULE_ALIAS_MISCDEV(TUN_MINOR);
1021