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 * Mark Smith <markzzzsmith@yahoo.com.au>
22 * Use random_ether_addr() for tap MAC address.
24 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
25 * Fixes in packet dropping, queue length setting and queue wakeup.
26 * Increased default tx queue length.
30 * Daniel Podlejski <underley@underley.eu.org>
31 * Modifications for 2.3.99-pre5 kernel.
34 #define DRV_NAME "tun"
35 #define DRV_VERSION "1.6"
36 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
37 #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/major.h>
44 #include <linux/slab.h>
45 #include <linux/poll.h>
46 #include <linux/fcntl.h>
47 #include <linux/init.h>
48 #include <linux/skbuff.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/miscdevice.h>
52 #include <linux/ethtool.h>
53 #include <linux/rtnetlink.h>
55 #include <linux/if_arp.h>
56 #include <linux/if_ether.h>
57 #include <linux/if_tun.h>
58 #include <linux/crc32.h>
60 #include <asm/system.h>
61 #include <asm/uaccess.h>
67 /* Network device part of the driver */
69 static LIST_HEAD(tun_dev_list);
70 static struct ethtool_ops tun_ethtool_ops;
72 /* Net device open. */
73 static int tun_net_open(struct net_device *dev)
75 netif_start_queue(dev);
79 /* Net device close. */
80 static int tun_net_close(struct net_device *dev)
82 netif_stop_queue(dev);
86 /* Net device start xmit */
87 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
89 struct tun_struct *tun = netdev_priv(dev);
91 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
93 /* Drop packet if interface is not attached */
98 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
99 if (!(tun->flags & TUN_ONE_QUEUE)) {
100 /* Normal queueing mode. */
101 /* Packet scheduler handles dropping of further packets. */
102 netif_stop_queue(dev);
104 /* We won't see all dropped packets individually, so overrun
105 * error is more appropriate. */
106 tun->stats.tx_fifo_errors++;
108 /* Single queue mode.
109 * Driver handles dropping of all packets itself. */
115 skb_queue_tail(&tun->readq, skb);
116 dev->trans_start = jiffies;
118 /* Notify and wake up reader process */
119 if (tun->flags & TUN_FASYNC)
120 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
121 wake_up_interruptible(&tun->read_wait);
125 tun->stats.tx_dropped++;
130 /** Add the specified Ethernet address to this multicast filter. */
132 add_multi(u32* filter, const u8* addr)
134 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
135 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
138 /** Remove the specified Ethernet addres from this multicast filter. */
140 del_multi(u32* filter, const u8* addr)
142 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
143 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
146 /** Update the list of multicast groups to which the network device belongs.
147 * This list is used to filter packets being sent from the character device to
148 * the network device. */
150 tun_net_mclist(struct net_device *dev)
152 struct tun_struct *tun = netdev_priv(dev);
153 const struct dev_mc_list *mclist;
155 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
156 dev->name, dev->mc_count);
157 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
158 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
159 i++, mclist = mclist->next) {
160 add_multi(tun->net_filter, mclist->dmi_addr);
161 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
163 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
164 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
168 static struct net_device_stats *tun_net_stats(struct net_device *dev)
170 struct tun_struct *tun = netdev_priv(dev);
174 /* Initialize net device. */
175 static void tun_net_init(struct net_device *dev)
177 struct tun_struct *tun = netdev_priv(dev);
179 switch (tun->flags & TUN_TYPE_MASK) {
181 /* Point-to-Point TUN Device */
182 dev->hard_header_len = 0;
186 /* Zero header length */
187 dev->type = ARPHRD_NONE;
188 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
189 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
193 /* Ethernet TAP Device */
194 dev->set_multicast_list = tun_net_mclist;
197 random_ether_addr(dev->dev_addr);
198 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
203 /* Character device part */
206 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
208 struct tun_struct *tun = file->private_data;
209 unsigned int mask = POLLOUT | POLLWRNORM;
214 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
216 poll_wait(file, &tun->read_wait, wait);
218 if (skb_queue_len(&tun->readq))
219 mask |= POLLIN | POLLRDNORM;
224 /* Get packet from user space buffer */
225 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
227 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
231 if (!(tun->flags & TUN_NO_PI)) {
232 if ((len -= sizeof(pi)) > count)
235 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
239 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
240 tun->stats.rx_dropped++;
245 if (memcpy_fromiovec(skb_put(skb, len), iv, len))
249 switch (tun->flags & TUN_TYPE_MASK) {
251 skb->mac.raw = skb->data;
252 skb->protocol = pi.proto;
255 skb->protocol = eth_type_trans(skb, tun->dev);
259 if (tun->flags & TUN_NOCHECKSUM)
260 skb->ip_summed = CHECKSUM_UNNECESSARY;
263 tun->dev->last_rx = jiffies;
265 tun->stats.rx_packets++;
266 tun->stats.rx_bytes += len;
271 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
276 for (i = 0, len = 0; i < count; i++)
277 len += iv[i].iov_len;
283 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
284 unsigned long count, loff_t *pos)
286 struct tun_struct *tun = file->private_data;
291 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
293 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
297 static ssize_t tun_chr_write(struct file * file, const char __user * buf,
298 size_t count, loff_t *pos)
300 struct iovec iv = { (void __user *) buf, count };
301 return tun_chr_writev(file, &iv, 1, pos);
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;
338 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
339 unsigned long count, loff_t *pos)
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);
421 static ssize_t tun_chr_read(struct file * file, char __user * buf,
422 size_t count, loff_t *pos)
424 struct iovec iv = { buf, count };
425 return tun_chr_readv(file, &iv, 1, pos);
428 static void tun_setup(struct net_device *dev)
430 struct tun_struct *tun = netdev_priv(dev);
432 skb_queue_head_init(&tun->readq);
433 init_waitqueue_head(&tun->read_wait);
437 SET_MODULE_OWNER(dev);
438 dev->open = tun_net_open;
439 dev->hard_start_xmit = tun_net_xmit;
440 dev->stop = tun_net_close;
441 dev->get_stats = tun_net_stats;
442 dev->ethtool_ops = &tun_ethtool_ops;
443 dev->destructor = free_netdev;
446 static struct tun_struct *tun_get_by_name(const char *name)
448 struct tun_struct *tun;
451 list_for_each_entry(tun, &tun_dev_list, list) {
452 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
459 static int tun_set_iff(struct file *file, struct ifreq *ifr)
461 struct tun_struct *tun;
462 struct net_device *dev;
465 tun = tun_get_by_name(ifr->ifr_name);
470 /* Check permissions */
471 if (tun->owner != -1 &&
472 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
475 else if (__dev_get_by_name(ifr->ifr_name))
479 unsigned long flags = 0;
484 if (ifr->ifr_flags & IFF_TUN) {
486 flags |= TUN_TUN_DEV;
488 } else if (ifr->ifr_flags & IFF_TAP) {
490 flags |= TUN_TAP_DEV;
496 name = ifr->ifr_name;
498 dev = alloc_netdev(sizeof(struct tun_struct), name,
503 tun = netdev_priv(dev);
506 /* Be promiscuous by default to maintain previous behaviour. */
507 tun->if_flags = IFF_PROMISC;
508 /* Generate random Ethernet address. */
509 *(u16 *)tun->dev_addr = htons(0x00FF);
510 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
511 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
515 if (strchr(dev->name, '%')) {
516 err = dev_alloc_name(dev, dev->name);
521 err = register_netdevice(tun->dev);
525 list_add(&tun->list, &tun_dev_list);
528 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
530 if (ifr->ifr_flags & IFF_NO_PI)
531 tun->flags |= TUN_NO_PI;
533 if (ifr->ifr_flags & IFF_ONE_QUEUE)
534 tun->flags |= TUN_ONE_QUEUE;
536 file->private_data = tun;
539 strcpy(ifr->ifr_name, tun->dev->name);
548 static int tun_chr_ioctl(struct inode *inode, struct file *file,
549 unsigned int cmd, unsigned long arg)
551 struct tun_struct *tun = file->private_data;
552 void __user* argp = (void __user*)arg;
555 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
556 if (copy_from_user(&ifr, argp, sizeof ifr))
559 if (cmd == TUNSETIFF && !tun) {
562 ifr.ifr_name[IFNAMSIZ-1] = '\0';
565 err = tun_set_iff(file, &ifr);
571 if (copy_to_user(argp, &ifr, sizeof(ifr)))
579 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
583 /* Disable/Enable checksum */
585 tun->flags |= TUN_NOCHECKSUM;
587 tun->flags &= ~TUN_NOCHECKSUM;
589 DBG(KERN_INFO "%s: checksum %s\n",
590 tun->dev->name, arg ? "disabled" : "enabled");
594 /* Disable/Enable persist mode */
596 tun->flags |= TUN_PERSIST;
598 tun->flags &= ~TUN_PERSIST;
600 DBG(KERN_INFO "%s: persist %s\n",
601 tun->dev->name, arg ? "disabled" : "enabled");
605 /* Set owner of the device */
606 tun->owner = (uid_t) arg;
608 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
618 ifr.ifr_flags = tun->if_flags;
619 if (copy_to_user( argp, &ifr, sizeof ifr))
624 /** Set the character device's interface flags. Currently only
625 * IFF_PROMISC and IFF_ALLMULTI are used. */
626 tun->if_flags = ifr.ifr_flags;
627 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
628 tun->dev->name, tun->if_flags);
632 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
633 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
634 if (copy_to_user( argp, &ifr, sizeof ifr))
639 /** Set the character device's hardware address. This is used when
640 * filtering packets being sent from the network device to the character
642 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
643 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
644 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
646 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
647 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
651 /** Add the specified group to the character device's multicast filter
653 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
654 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
656 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
657 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
658 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
662 /** Remove the specified group from the character device's multicast
664 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
665 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
667 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
668 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
669 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
679 static int tun_chr_fasync(int fd, struct file *file, int on)
681 struct tun_struct *tun = file->private_data;
687 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
689 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
693 ret = f_setown(file, current->pid, 0);
696 tun->flags |= TUN_FASYNC;
698 tun->flags &= ~TUN_FASYNC;
703 static int tun_chr_open(struct inode *inode, struct file * file)
705 DBG1(KERN_INFO "tunX: tun_chr_open\n");
706 file->private_data = NULL;
710 static int tun_chr_close(struct inode *inode, struct file *file)
712 struct tun_struct *tun = file->private_data;
717 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
719 tun_chr_fasync(-1, file, 0);
723 /* Detach from net device */
724 file->private_data = NULL;
727 /* Drop read queue */
728 skb_queue_purge(&tun->readq);
730 if (!(tun->flags & TUN_PERSIST)) {
731 list_del(&tun->list);
732 unregister_netdevice(tun->dev);
740 static struct file_operations tun_fops = {
741 .owner = THIS_MODULE,
743 .read = tun_chr_read,
744 .readv = tun_chr_readv,
745 .write = tun_chr_write,
746 .writev = tun_chr_writev,
747 .poll = tun_chr_poll,
748 .ioctl = tun_chr_ioctl,
749 .open = tun_chr_open,
750 .release = tun_chr_close,
751 .fasync = tun_chr_fasync
754 static struct miscdevice tun_miscdev = {
758 .devfs_name = "net/tun",
761 /* ethtool interface */
763 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
766 cmd->advertising = 0;
767 cmd->speed = SPEED_10;
768 cmd->duplex = DUPLEX_FULL;
770 cmd->phy_address = 0;
771 cmd->transceiver = XCVR_INTERNAL;
772 cmd->autoneg = AUTONEG_DISABLE;
778 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
780 struct tun_struct *tun = netdev_priv(dev);
782 strcpy(info->driver, DRV_NAME);
783 strcpy(info->version, DRV_VERSION);
784 strcpy(info->fw_version, "N/A");
786 switch (tun->flags & TUN_TYPE_MASK) {
788 strcpy(info->bus_info, "tun");
791 strcpy(info->bus_info, "tap");
796 static u32 tun_get_msglevel(struct net_device *dev)
799 struct tun_struct *tun = netdev_priv(dev);
806 static void tun_set_msglevel(struct net_device *dev, u32 value)
809 struct tun_struct *tun = netdev_priv(dev);
814 static u32 tun_get_link(struct net_device *dev)
816 struct tun_struct *tun = netdev_priv(dev);
817 return tun->attached;
820 static u32 tun_get_rx_csum(struct net_device *dev)
822 struct tun_struct *tun = netdev_priv(dev);
823 return (tun->flags & TUN_NOCHECKSUM) == 0;
826 static int tun_set_rx_csum(struct net_device *dev, u32 data)
828 struct tun_struct *tun = netdev_priv(dev);
830 tun->flags &= ~TUN_NOCHECKSUM;
832 tun->flags |= TUN_NOCHECKSUM;
836 static struct ethtool_ops tun_ethtool_ops = {
837 .get_settings = tun_get_settings,
838 .get_drvinfo = tun_get_drvinfo,
839 .get_msglevel = tun_get_msglevel,
840 .set_msglevel = tun_set_msglevel,
841 .get_link = tun_get_link,
842 .get_rx_csum = tun_get_rx_csum,
843 .set_rx_csum = tun_set_rx_csum
846 int __init tun_init(void)
850 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
851 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
853 ret = misc_register(&tun_miscdev);
855 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
859 void tun_cleanup(void)
861 struct tun_struct *tun, *nxt;
863 misc_deregister(&tun_miscdev);
866 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
867 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
868 unregister_netdevice(tun->dev);
874 module_init(tun_init);
875 module_exit(tun_cleanup);
876 MODULE_DESCRIPTION(DRV_DESCRIPTION);
877 MODULE_AUTHOR(DRV_COPYRIGHT);
878 MODULE_LICENSE("GPL");
879 MODULE_ALIAS_MISCDEV(TUN_MINOR);