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/module.h>
43 #include <linux/errno.h>
44 #include <linux/kernel.h>
45 #include <linux/major.h>
46 #include <linux/slab.h>
47 #include <linux/poll.h>
48 #include <linux/fcntl.h>
49 #include <linux/init.h>
50 #include <linux/skbuff.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/miscdevice.h>
54 #include <linux/ethtool.h>
55 #include <linux/rtnetlink.h>
57 #include <linux/if_arp.h>
58 #include <linux/if_ether.h>
59 #include <linux/if_tun.h>
60 #include <linux/crc32.h>
62 #include <asm/system.h>
63 #include <asm/uaccess.h>
69 /* Network device part of the driver */
71 static LIST_HEAD(tun_dev_list);
72 static const struct ethtool_ops tun_ethtool_ops;
74 /* Net device open. */
75 static int tun_net_open(struct net_device *dev)
77 netif_start_queue(dev);
81 /* Net device close. */
82 static int tun_net_close(struct net_device *dev)
84 netif_stop_queue(dev);
88 /* Net device start xmit */
89 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
91 struct tun_struct *tun = netdev_priv(dev);
93 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
95 /* Drop packet if interface is not attached */
100 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
101 if (!(tun->flags & TUN_ONE_QUEUE)) {
102 /* Normal queueing mode. */
103 /* Packet scheduler handles dropping of further packets. */
104 netif_stop_queue(dev);
106 /* We won't see all dropped packets individually, so overrun
107 * error is more appropriate. */
108 tun->stats.tx_fifo_errors++;
110 /* Single queue mode.
111 * Driver handles dropping of all packets itself. */
117 skb_queue_tail(&tun->readq, skb);
118 dev->trans_start = jiffies;
120 /* Notify and wake up reader process */
121 if (tun->flags & TUN_FASYNC)
122 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
123 wake_up_interruptible(&tun->read_wait);
127 tun->stats.tx_dropped++;
132 /** Add the specified Ethernet address to this multicast filter. */
134 add_multi(u32* filter, const u8* addr)
136 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
137 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
140 /** Remove the specified Ethernet addres from this multicast filter. */
142 del_multi(u32* filter, const u8* addr)
144 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
145 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
148 /** Update the list of multicast groups to which the network device belongs.
149 * This list is used to filter packets being sent from the character device to
150 * the network device. */
152 tun_net_mclist(struct net_device *dev)
154 struct tun_struct *tun = netdev_priv(dev);
155 const struct dev_mc_list *mclist;
157 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
158 dev->name, dev->mc_count);
159 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
160 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
161 i++, mclist = mclist->next) {
162 add_multi(tun->net_filter, mclist->dmi_addr);
163 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
165 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
166 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
170 static struct net_device_stats *tun_net_stats(struct net_device *dev)
172 struct tun_struct *tun = netdev_priv(dev);
176 /* Initialize net device. */
177 static void tun_net_init(struct net_device *dev)
179 struct tun_struct *tun = netdev_priv(dev);
181 switch (tun->flags & TUN_TYPE_MASK) {
183 /* Point-to-Point TUN Device */
184 dev->hard_header_len = 0;
188 /* Zero header length */
189 dev->type = ARPHRD_NONE;
190 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
191 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
195 /* Ethernet TAP Device */
196 dev->set_multicast_list = tun_net_mclist;
199 random_ether_addr(dev->dev_addr);
200 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
205 /* Character device part */
208 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
210 struct tun_struct *tun = file->private_data;
211 unsigned int mask = POLLOUT | POLLWRNORM;
216 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
218 poll_wait(file, &tun->read_wait, wait);
220 if (!skb_queue_empty(&tun->readq))
221 mask |= POLLIN | POLLRDNORM;
226 /* Get packet from user space buffer */
227 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
229 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
231 size_t len = count, align = 0;
233 if (!(tun->flags & TUN_NO_PI)) {
234 if ((len -= sizeof(pi)) > count)
237 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
241 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
242 align = NET_IP_ALIGN;
244 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
245 tun->stats.rx_dropped++;
250 skb_reserve(skb, align);
251 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
252 tun->stats.rx_dropped++;
258 switch (tun->flags & TUN_TYPE_MASK) {
260 skb->mac.raw = skb->data;
261 skb->protocol = pi.proto;
264 skb->protocol = eth_type_trans(skb, tun->dev);
268 if (tun->flags & TUN_NOCHECKSUM)
269 skb->ip_summed = CHECKSUM_UNNECESSARY;
272 tun->dev->last_rx = jiffies;
274 tun->stats.rx_packets++;
275 tun->stats.rx_bytes += len;
280 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
285 for (i = 0, len = 0; i < count; i++)
286 len += iv[i].iov_len;
291 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
292 unsigned long count, loff_t pos)
294 struct tun_struct *tun = iocb->ki_filp->private_data;
299 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
301 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
304 /* Put packet to the user space buffer */
305 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
307 struct iovec *iv, int len)
309 struct tun_pi pi = { 0, skb->protocol };
312 if (!(tun->flags & TUN_NO_PI)) {
313 if ((len -= sizeof(pi)) < 0)
316 if (len < skb->len) {
317 /* Packet will be striped */
318 pi.flags |= TUN_PKT_STRIP;
321 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
326 len = min_t(int, skb->len, len);
328 skb_copy_datagram_iovec(skb, 0, iv, len);
331 tun->stats.tx_packets++;
332 tun->stats.tx_bytes += len;
337 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
338 unsigned long count, loff_t pos)
340 struct file *file = iocb->ki_filp;
341 struct tun_struct *tun = file->private_data;
342 DECLARE_WAITQUEUE(wait, current);
344 ssize_t len, ret = 0;
349 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
351 len = iov_total(iv, count);
355 add_wait_queue(&tun->read_wait, &wait);
357 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
361 current->state = TASK_INTERRUPTIBLE;
363 /* Read frames from the queue */
364 if (!(skb=skb_dequeue(&tun->readq))) {
365 if (file->f_flags & O_NONBLOCK) {
369 if (signal_pending(current)) {
374 /* Nothing to read, let's sleep */
378 netif_wake_queue(tun->dev);
380 /** Decide whether to accept this packet. This code is designed to
381 * behave identically to an Ethernet interface. Accept the packet if
382 * - we are promiscuous.
383 * - the packet is addressed to us.
384 * - the packet is broadcast.
385 * - the packet is multicast and
386 * - we are multicast promiscous.
387 * - we belong to the multicast group.
389 memcpy(addr, skb->data,
390 min_t(size_t, sizeof addr, skb->len));
391 bit_nr = ether_crc(sizeof addr, addr) >> 26;
392 if ((tun->if_flags & IFF_PROMISC) ||
393 memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
394 memcmp(addr, ones, sizeof addr) == 0 ||
395 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
396 (addr[0] == 0x33 && addr[1] == 0x33)) &&
397 ((tun->if_flags & IFF_ALLMULTI) ||
398 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
399 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
400 tun->dev->name, addr[0], addr[1], addr[2],
401 addr[3], addr[4], addr[5]);
402 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
406 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
407 tun->dev->name, addr[0], addr[1], addr[2],
408 addr[3], addr[4], addr[5]);
414 current->state = TASK_RUNNING;
415 remove_wait_queue(&tun->read_wait, &wait);
420 static void tun_setup(struct net_device *dev)
422 struct tun_struct *tun = netdev_priv(dev);
424 skb_queue_head_init(&tun->readq);
425 init_waitqueue_head(&tun->read_wait);
429 SET_MODULE_OWNER(dev);
430 dev->open = tun_net_open;
431 dev->hard_start_xmit = tun_net_xmit;
432 dev->stop = tun_net_close;
433 dev->get_stats = tun_net_stats;
434 dev->ethtool_ops = &tun_ethtool_ops;
435 dev->destructor = free_netdev;
438 static struct tun_struct *tun_get_by_name(const char *name)
440 struct tun_struct *tun;
443 list_for_each_entry(tun, &tun_dev_list, list) {
444 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
451 static int tun_set_iff(struct file *file, struct ifreq *ifr)
453 struct tun_struct *tun;
454 struct net_device *dev;
457 tun = tun_get_by_name(ifr->ifr_name);
462 /* Check permissions */
463 if (tun->owner != -1 &&
464 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
467 else if (__dev_get_by_name(ifr->ifr_name))
471 unsigned long flags = 0;
475 if (!capable(CAP_NET_ADMIN))
479 if (ifr->ifr_flags & IFF_TUN) {
481 flags |= TUN_TUN_DEV;
483 } else if (ifr->ifr_flags & IFF_TAP) {
485 flags |= TUN_TAP_DEV;
491 name = ifr->ifr_name;
493 dev = alloc_netdev(sizeof(struct tun_struct), name,
498 tun = netdev_priv(dev);
501 /* Be promiscuous by default to maintain previous behaviour. */
502 tun->if_flags = IFF_PROMISC;
503 /* Generate random Ethernet address. */
504 *(u16 *)tun->dev_addr = htons(0x00FF);
505 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
506 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
510 if (strchr(dev->name, '%')) {
511 err = dev_alloc_name(dev, dev->name);
516 err = register_netdevice(tun->dev);
520 list_add(&tun->list, &tun_dev_list);
523 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
525 if (ifr->ifr_flags & IFF_NO_PI)
526 tun->flags |= TUN_NO_PI;
528 if (ifr->ifr_flags & IFF_ONE_QUEUE)
529 tun->flags |= TUN_ONE_QUEUE;
531 file->private_data = tun;
534 strcpy(ifr->ifr_name, tun->dev->name);
543 static int tun_chr_ioctl(struct inode *inode, struct file *file,
544 unsigned int cmd, unsigned long arg)
546 struct tun_struct *tun = file->private_data;
547 void __user* argp = (void __user*)arg;
550 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
551 if (copy_from_user(&ifr, argp, sizeof ifr))
554 if (cmd == TUNSETIFF && !tun) {
557 ifr.ifr_name[IFNAMSIZ-1] = '\0';
560 err = tun_set_iff(file, &ifr);
566 if (copy_to_user(argp, &ifr, sizeof(ifr)))
574 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
578 /* Disable/Enable checksum */
580 tun->flags |= TUN_NOCHECKSUM;
582 tun->flags &= ~TUN_NOCHECKSUM;
584 DBG(KERN_INFO "%s: checksum %s\n",
585 tun->dev->name, arg ? "disabled" : "enabled");
589 /* Disable/Enable persist mode */
591 tun->flags |= TUN_PERSIST;
593 tun->flags &= ~TUN_PERSIST;
595 DBG(KERN_INFO "%s: persist %s\n",
596 tun->dev->name, arg ? "disabled" : "enabled");
600 /* Set owner of the device */
601 tun->owner = (uid_t) arg;
603 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
607 /* Only allow setting the type when the interface is down */
608 if (tun->dev->flags & IFF_UP) {
609 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
613 tun->dev->type = (int) arg;
614 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
625 ifr.ifr_flags = tun->if_flags;
626 if (copy_to_user( argp, &ifr, sizeof ifr))
631 /** Set the character device's interface flags. Currently only
632 * IFF_PROMISC and IFF_ALLMULTI are used. */
633 tun->if_flags = ifr.ifr_flags;
634 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
635 tun->dev->name, tun->if_flags);
639 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
640 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
641 if (copy_to_user( argp, &ifr, sizeof ifr))
646 /** Set the character device's hardware address. This is used when
647 * filtering packets being sent from the network device to the character
649 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
650 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
651 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
653 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
654 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
658 /** Add the specified group to the character device's multicast filter
660 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
661 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
663 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
664 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
665 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
669 /** Remove the specified group from the character device's multicast
671 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
672 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
674 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
675 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
676 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
686 static int tun_chr_fasync(int fd, struct file *file, int on)
688 struct tun_struct *tun = file->private_data;
694 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
696 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
700 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
703 tun->flags |= TUN_FASYNC;
705 tun->flags &= ~TUN_FASYNC;
710 static int tun_chr_open(struct inode *inode, struct file * file)
712 DBG1(KERN_INFO "tunX: tun_chr_open\n");
713 file->private_data = NULL;
717 static int tun_chr_close(struct inode *inode, struct file *file)
719 struct tun_struct *tun = file->private_data;
724 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
726 tun_chr_fasync(-1, file, 0);
730 /* Detach from net device */
731 file->private_data = NULL;
734 /* Drop read queue */
735 skb_queue_purge(&tun->readq);
737 if (!(tun->flags & TUN_PERSIST)) {
738 list_del(&tun->list);
739 unregister_netdevice(tun->dev);
747 static const struct file_operations tun_fops = {
748 .owner = THIS_MODULE,
750 .read = do_sync_read,
751 .aio_read = tun_chr_aio_read,
752 .write = do_sync_write,
753 .aio_write = tun_chr_aio_write,
754 .poll = tun_chr_poll,
755 .ioctl = tun_chr_ioctl,
756 .open = tun_chr_open,
757 .release = tun_chr_close,
758 .fasync = tun_chr_fasync
761 static struct miscdevice tun_miscdev = {
767 /* ethtool interface */
769 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
772 cmd->advertising = 0;
773 cmd->speed = SPEED_10;
774 cmd->duplex = DUPLEX_FULL;
776 cmd->phy_address = 0;
777 cmd->transceiver = XCVR_INTERNAL;
778 cmd->autoneg = AUTONEG_DISABLE;
784 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
786 struct tun_struct *tun = netdev_priv(dev);
788 strcpy(info->driver, DRV_NAME);
789 strcpy(info->version, DRV_VERSION);
790 strcpy(info->fw_version, "N/A");
792 switch (tun->flags & TUN_TYPE_MASK) {
794 strcpy(info->bus_info, "tun");
797 strcpy(info->bus_info, "tap");
802 static u32 tun_get_msglevel(struct net_device *dev)
805 struct tun_struct *tun = netdev_priv(dev);
812 static void tun_set_msglevel(struct net_device *dev, u32 value)
815 struct tun_struct *tun = netdev_priv(dev);
820 static u32 tun_get_link(struct net_device *dev)
822 struct tun_struct *tun = netdev_priv(dev);
823 return tun->attached;
826 static u32 tun_get_rx_csum(struct net_device *dev)
828 struct tun_struct *tun = netdev_priv(dev);
829 return (tun->flags & TUN_NOCHECKSUM) == 0;
832 static int tun_set_rx_csum(struct net_device *dev, u32 data)
834 struct tun_struct *tun = netdev_priv(dev);
836 tun->flags &= ~TUN_NOCHECKSUM;
838 tun->flags |= TUN_NOCHECKSUM;
842 static const struct ethtool_ops tun_ethtool_ops = {
843 .get_settings = tun_get_settings,
844 .get_drvinfo = tun_get_drvinfo,
845 .get_msglevel = tun_get_msglevel,
846 .set_msglevel = tun_set_msglevel,
847 .get_link = tun_get_link,
848 .get_rx_csum = tun_get_rx_csum,
849 .set_rx_csum = tun_set_rx_csum
852 static int __init tun_init(void)
856 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
857 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
859 ret = misc_register(&tun_miscdev);
861 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
865 static void tun_cleanup(void)
867 struct tun_struct *tun, *nxt;
869 misc_deregister(&tun_miscdev);
872 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
873 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
874 unregister_netdevice(tun->dev);
880 module_init(tun_init);
881 module_exit(tun_cleanup);
882 MODULE_DESCRIPTION(DRV_DESCRIPTION);
883 MODULE_AUTHOR(DRV_COPYRIGHT);
884 MODULE_LICENSE("GPL");
885 MODULE_ALIAS_MISCDEV(TUN_MINOR);