2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
21 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
24 * Mark Smith <markzzzsmith@yahoo.com.au>
25 * Use random_ether_addr() for tap MAC address.
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
37 #define DRV_NAME "tun"
38 #define DRV_VERSION "1.6"
39 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
40 #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/major.h>
47 #include <linux/slab.h>
48 #include <linux/poll.h>
49 #include <linux/fcntl.h>
50 #include <linux/init.h>
51 #include <linux/skbuff.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/miscdevice.h>
55 #include <linux/ethtool.h>
56 #include <linux/rtnetlink.h>
58 #include <linux/if_arp.h>
59 #include <linux/if_ether.h>
60 #include <linux/if_tun.h>
61 #include <linux/crc32.h>
63 #include <asm/system.h>
64 #include <asm/uaccess.h>
70 /* Network device part of the driver */
72 static LIST_HEAD(tun_dev_list);
73 static struct ethtool_ops tun_ethtool_ops;
75 /* Net device open. */
76 static int tun_net_open(struct net_device *dev)
78 netif_start_queue(dev);
82 /* Net device close. */
83 static int tun_net_close(struct net_device *dev)
85 netif_stop_queue(dev);
89 /* Net device start xmit */
90 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
92 struct tun_struct *tun = netdev_priv(dev);
94 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
96 /* Drop packet if interface is not attached */
100 /* Packet dropping */
101 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
102 if (!(tun->flags & TUN_ONE_QUEUE)) {
103 /* Normal queueing mode. */
104 /* Packet scheduler handles dropping of further packets. */
105 netif_stop_queue(dev);
107 /* We won't see all dropped packets individually, so overrun
108 * error is more appropriate. */
109 tun->stats.tx_fifo_errors++;
111 /* Single queue mode.
112 * Driver handles dropping of all packets itself. */
118 skb_queue_tail(&tun->readq, skb);
119 dev->trans_start = jiffies;
121 /* Notify and wake up reader process */
122 if (tun->flags & TUN_FASYNC)
123 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
124 wake_up_interruptible(&tun->read_wait);
128 tun->stats.tx_dropped++;
133 /** Add the specified Ethernet address to this multicast filter. */
135 add_multi(u32* filter, const u8* addr)
137 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
138 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
141 /** Remove the specified Ethernet addres from this multicast filter. */
143 del_multi(u32* filter, const u8* addr)
145 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
146 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
149 /** Update the list of multicast groups to which the network device belongs.
150 * This list is used to filter packets being sent from the character device to
151 * the network device. */
153 tun_net_mclist(struct net_device *dev)
155 struct tun_struct *tun = netdev_priv(dev);
156 const struct dev_mc_list *mclist;
158 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
159 dev->name, dev->mc_count);
160 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
161 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
162 i++, mclist = mclist->next) {
163 add_multi(tun->net_filter, mclist->dmi_addr);
164 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
166 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
167 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
171 static struct net_device_stats *tun_net_stats(struct net_device *dev)
173 struct tun_struct *tun = netdev_priv(dev);
177 /* Initialize net device. */
178 static void tun_net_init(struct net_device *dev)
180 struct tun_struct *tun = netdev_priv(dev);
182 switch (tun->flags & TUN_TYPE_MASK) {
184 /* Point-to-Point TUN Device */
185 dev->hard_header_len = 0;
189 /* Zero header length */
190 dev->type = ARPHRD_NONE;
191 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
192 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
196 /* Ethernet TAP Device */
197 dev->set_multicast_list = tun_net_mclist;
200 random_ether_addr(dev->dev_addr);
201 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
206 /* Character device part */
209 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
211 struct tun_struct *tun = file->private_data;
212 unsigned int mask = POLLOUT | POLLWRNORM;
217 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
219 poll_wait(file, &tun->read_wait, wait);
221 if (!skb_queue_empty(&tun->readq))
222 mask |= POLLIN | POLLRDNORM;
227 /* Get packet from user space buffer */
228 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
230 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
232 size_t len = count, align = 0;
234 if (!(tun->flags & TUN_NO_PI)) {
235 if ((len -= sizeof(pi)) > count)
238 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
242 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
243 align = NET_IP_ALIGN;
245 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
246 tun->stats.rx_dropped++;
251 skb_reserve(skb, align);
252 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
253 tun->stats.rx_dropped++;
259 switch (tun->flags & TUN_TYPE_MASK) {
261 skb->mac.raw = skb->data;
262 skb->protocol = pi.proto;
265 skb->protocol = eth_type_trans(skb, tun->dev);
269 if (tun->flags & TUN_NOCHECKSUM)
270 skb->ip_summed = CHECKSUM_UNNECESSARY;
273 tun->dev->last_rx = jiffies;
275 tun->stats.rx_packets++;
276 tun->stats.rx_bytes += len;
281 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
286 for (i = 0, len = 0; i < count; i++)
287 len += iv[i].iov_len;
293 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
294 unsigned long count, loff_t *pos)
296 struct tun_struct *tun = file->private_data;
301 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
303 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
307 static ssize_t tun_chr_write(struct file * file, const char __user * buf,
308 size_t count, loff_t *pos)
310 struct iovec iv = { (void __user *) buf, count };
311 return tun_chr_writev(file, &iv, 1, pos);
314 /* Put packet to the user space buffer */
315 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
317 struct iovec *iv, int len)
319 struct tun_pi pi = { 0, skb->protocol };
322 if (!(tun->flags & TUN_NO_PI)) {
323 if ((len -= sizeof(pi)) < 0)
326 if (len < skb->len) {
327 /* Packet will be striped */
328 pi.flags |= TUN_PKT_STRIP;
331 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
336 len = min_t(int, skb->len, len);
338 skb_copy_datagram_iovec(skb, 0, iv, len);
341 tun->stats.tx_packets++;
342 tun->stats.tx_bytes += len;
348 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
349 unsigned long count, loff_t *pos)
351 struct tun_struct *tun = file->private_data;
352 DECLARE_WAITQUEUE(wait, current);
354 ssize_t len, ret = 0;
359 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
361 len = iov_total(iv, count);
365 add_wait_queue(&tun->read_wait, &wait);
367 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
371 current->state = TASK_INTERRUPTIBLE;
373 /* Read frames from the queue */
374 if (!(skb=skb_dequeue(&tun->readq))) {
375 if (file->f_flags & O_NONBLOCK) {
379 if (signal_pending(current)) {
384 /* Nothing to read, let's sleep */
388 netif_wake_queue(tun->dev);
390 /** Decide whether to accept this packet. This code is designed to
391 * behave identically to an Ethernet interface. Accept the packet if
392 * - we are promiscuous.
393 * - the packet is addressed to us.
394 * - the packet is broadcast.
395 * - the packet is multicast and
396 * - we are multicast promiscous.
397 * - we belong to the multicast group.
399 memcpy(addr, skb->data,
400 min_t(size_t, sizeof addr, skb->len));
401 bit_nr = ether_crc(sizeof addr, addr) >> 26;
402 if ((tun->if_flags & IFF_PROMISC) ||
403 memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
404 memcmp(addr, ones, sizeof addr) == 0 ||
405 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
406 (addr[0] == 0x33 && addr[1] == 0x33)) &&
407 ((tun->if_flags & IFF_ALLMULTI) ||
408 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
409 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
410 tun->dev->name, addr[0], addr[1], addr[2],
411 addr[3], addr[4], addr[5]);
412 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
416 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
417 tun->dev->name, addr[0], addr[1], addr[2],
418 addr[3], addr[4], addr[5]);
424 current->state = TASK_RUNNING;
425 remove_wait_queue(&tun->read_wait, &wait);
431 static ssize_t tun_chr_read(struct file * file, char __user * buf,
432 size_t count, loff_t *pos)
434 struct iovec iv = { buf, count };
435 return tun_chr_readv(file, &iv, 1, pos);
438 static void tun_setup(struct net_device *dev)
440 struct tun_struct *tun = netdev_priv(dev);
442 skb_queue_head_init(&tun->readq);
443 init_waitqueue_head(&tun->read_wait);
447 SET_MODULE_OWNER(dev);
448 dev->open = tun_net_open;
449 dev->hard_start_xmit = tun_net_xmit;
450 dev->stop = tun_net_close;
451 dev->get_stats = tun_net_stats;
452 dev->ethtool_ops = &tun_ethtool_ops;
453 dev->destructor = free_netdev;
456 static struct tun_struct *tun_get_by_name(const char *name)
458 struct tun_struct *tun;
461 list_for_each_entry(tun, &tun_dev_list, list) {
462 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
469 static int tun_set_iff(struct file *file, struct ifreq *ifr)
471 struct tun_struct *tun;
472 struct net_device *dev;
475 tun = tun_get_by_name(ifr->ifr_name);
480 /* Check permissions */
481 if (tun->owner != -1 &&
482 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
485 else if (__dev_get_by_name(ifr->ifr_name))
489 unsigned long flags = 0;
494 if (ifr->ifr_flags & IFF_TUN) {
496 flags |= TUN_TUN_DEV;
498 } else if (ifr->ifr_flags & IFF_TAP) {
500 flags |= TUN_TAP_DEV;
506 name = ifr->ifr_name;
508 dev = alloc_netdev(sizeof(struct tun_struct), name,
513 tun = netdev_priv(dev);
516 /* Be promiscuous by default to maintain previous behaviour. */
517 tun->if_flags = IFF_PROMISC;
518 /* Generate random Ethernet address. */
519 *(u16 *)tun->dev_addr = htons(0x00FF);
520 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
521 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
525 if (strchr(dev->name, '%')) {
526 err = dev_alloc_name(dev, dev->name);
531 err = register_netdevice(tun->dev);
535 list_add(&tun->list, &tun_dev_list);
538 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
540 if (ifr->ifr_flags & IFF_NO_PI)
541 tun->flags |= TUN_NO_PI;
543 if (ifr->ifr_flags & IFF_ONE_QUEUE)
544 tun->flags |= TUN_ONE_QUEUE;
546 file->private_data = tun;
549 strcpy(ifr->ifr_name, tun->dev->name);
558 static int tun_chr_ioctl(struct inode *inode, struct file *file,
559 unsigned int cmd, unsigned long arg)
561 struct tun_struct *tun = file->private_data;
562 void __user* argp = (void __user*)arg;
565 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
566 if (copy_from_user(&ifr, argp, sizeof ifr))
569 if (cmd == TUNSETIFF && !tun) {
572 ifr.ifr_name[IFNAMSIZ-1] = '\0';
575 err = tun_set_iff(file, &ifr);
581 if (copy_to_user(argp, &ifr, sizeof(ifr)))
589 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
593 /* Disable/Enable checksum */
595 tun->flags |= TUN_NOCHECKSUM;
597 tun->flags &= ~TUN_NOCHECKSUM;
599 DBG(KERN_INFO "%s: checksum %s\n",
600 tun->dev->name, arg ? "disabled" : "enabled");
604 /* Disable/Enable persist mode */
606 tun->flags |= TUN_PERSIST;
608 tun->flags &= ~TUN_PERSIST;
610 DBG(KERN_INFO "%s: persist %s\n",
611 tun->dev->name, arg ? "disabled" : "enabled");
615 /* Set owner of the device */
616 tun->owner = (uid_t) arg;
618 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
622 /* Only allow setting the type when the interface is down */
623 if (tun->dev->flags & IFF_UP) {
624 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
628 tun->dev->type = (int) arg;
629 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
640 ifr.ifr_flags = tun->if_flags;
641 if (copy_to_user( argp, &ifr, sizeof ifr))
646 /** Set the character device's interface flags. Currently only
647 * IFF_PROMISC and IFF_ALLMULTI are used. */
648 tun->if_flags = ifr.ifr_flags;
649 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
650 tun->dev->name, tun->if_flags);
654 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
655 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
656 if (copy_to_user( argp, &ifr, sizeof ifr))
661 /** Set the character device's hardware address. This is used when
662 * filtering packets being sent from the network device to the character
664 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
665 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
666 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
668 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
669 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
673 /** Add the specified group to the character device's multicast filter
675 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
676 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
678 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
679 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
680 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
684 /** Remove the specified group from the character device's multicast
686 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
687 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
689 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
690 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
691 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
701 static int tun_chr_fasync(int fd, struct file *file, int on)
703 struct tun_struct *tun = file->private_data;
709 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
711 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
715 ret = f_setown(file, current->pid, 0);
718 tun->flags |= TUN_FASYNC;
720 tun->flags &= ~TUN_FASYNC;
725 static int tun_chr_open(struct inode *inode, struct file * file)
727 DBG1(KERN_INFO "tunX: tun_chr_open\n");
728 file->private_data = NULL;
732 static int tun_chr_close(struct inode *inode, struct file *file)
734 struct tun_struct *tun = file->private_data;
739 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
741 tun_chr_fasync(-1, file, 0);
745 /* Detach from net device */
746 file->private_data = NULL;
749 /* Drop read queue */
750 skb_queue_purge(&tun->readq);
752 if (!(tun->flags & TUN_PERSIST)) {
753 list_del(&tun->list);
754 unregister_netdevice(tun->dev);
762 static struct file_operations tun_fops = {
763 .owner = THIS_MODULE,
765 .read = tun_chr_read,
766 .readv = tun_chr_readv,
767 .write = tun_chr_write,
768 .writev = tun_chr_writev,
769 .poll = tun_chr_poll,
770 .ioctl = tun_chr_ioctl,
771 .open = tun_chr_open,
772 .release = tun_chr_close,
773 .fasync = tun_chr_fasync
776 static struct miscdevice tun_miscdev = {
780 .devfs_name = "net/tun",
783 /* ethtool interface */
785 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
788 cmd->advertising = 0;
789 cmd->speed = SPEED_10;
790 cmd->duplex = DUPLEX_FULL;
792 cmd->phy_address = 0;
793 cmd->transceiver = XCVR_INTERNAL;
794 cmd->autoneg = AUTONEG_DISABLE;
800 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
802 struct tun_struct *tun = netdev_priv(dev);
804 strcpy(info->driver, DRV_NAME);
805 strcpy(info->version, DRV_VERSION);
806 strcpy(info->fw_version, "N/A");
808 switch (tun->flags & TUN_TYPE_MASK) {
810 strcpy(info->bus_info, "tun");
813 strcpy(info->bus_info, "tap");
818 static u32 tun_get_msglevel(struct net_device *dev)
821 struct tun_struct *tun = netdev_priv(dev);
828 static void tun_set_msglevel(struct net_device *dev, u32 value)
831 struct tun_struct *tun = netdev_priv(dev);
836 static u32 tun_get_link(struct net_device *dev)
838 struct tun_struct *tun = netdev_priv(dev);
839 return tun->attached;
842 static u32 tun_get_rx_csum(struct net_device *dev)
844 struct tun_struct *tun = netdev_priv(dev);
845 return (tun->flags & TUN_NOCHECKSUM) == 0;
848 static int tun_set_rx_csum(struct net_device *dev, u32 data)
850 struct tun_struct *tun = netdev_priv(dev);
852 tun->flags &= ~TUN_NOCHECKSUM;
854 tun->flags |= TUN_NOCHECKSUM;
858 static struct ethtool_ops tun_ethtool_ops = {
859 .get_settings = tun_get_settings,
860 .get_drvinfo = tun_get_drvinfo,
861 .get_msglevel = tun_get_msglevel,
862 .set_msglevel = tun_set_msglevel,
863 .get_link = tun_get_link,
864 .get_rx_csum = tun_get_rx_csum,
865 .set_rx_csum = tun_set_rx_csum
868 static int __init tun_init(void)
872 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
873 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
875 ret = misc_register(&tun_miscdev);
877 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
881 static void tun_cleanup(void)
883 struct tun_struct *tun, *nxt;
885 misc_deregister(&tun_miscdev);
888 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
889 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
890 unregister_netdevice(tun->dev);
896 module_init(tun_init);
897 module_exit(tun_cleanup);
898 MODULE_DESCRIPTION(DRV_DESCRIPTION);
899 MODULE_AUTHOR(DRV_COPYRIGHT);
900 MODULE_LICENSE("GPL");
901 MODULE_ALIAS_MISCDEV(TUN_MINOR);