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 * Brian Braunstein <linuxkernel@bristyle.com> 2007/03/23
22 * Fixed hw address handling. Now net_device.dev_addr is kept consistent
23 * with tun.dev_addr when the address is set by this module.
25 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
26 * Add TUNSETLINK ioctl to set the link encapsulation
28 * Mark Smith <markzzzsmith@yahoo.com.au>
29 * Use random_ether_addr() for tap MAC address.
31 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
32 * Fixes in packet dropping, queue length setting and queue wakeup.
33 * Increased default tx queue length.
37 * Daniel Podlejski <underley@underley.eu.org>
38 * Modifications for 2.3.99-pre5 kernel.
41 #define DRV_NAME "tun"
42 #define DRV_VERSION "1.6"
43 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
44 #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
46 #include <linux/module.h>
47 #include <linux/errno.h>
48 #include <linux/kernel.h>
49 #include <linux/major.h>
50 #include <linux/slab.h>
51 #include <linux/poll.h>
52 #include <linux/fcntl.h>
53 #include <linux/init.h>
54 #include <linux/skbuff.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/miscdevice.h>
58 #include <linux/ethtool.h>
59 #include <linux/rtnetlink.h>
61 #include <linux/if_arp.h>
62 #include <linux/if_ether.h>
63 #include <linux/if_tun.h>
64 #include <linux/crc32.h>
65 #include <net/net_namespace.h>
67 #include <asm/system.h>
68 #include <asm/uaccess.h>
70 /* Uncomment to enable debugging */
71 /* #define TUN_DEBUG 1 */
76 #define DBG if(tun->debug)printk
77 #define DBG1 if(debug==2)printk
84 struct list_head list;
90 wait_queue_head_t read_wait;
91 struct sk_buff_head readq;
93 struct net_device *dev;
95 struct fasync_struct *fasync;
97 unsigned long if_flags;
98 u8 dev_addr[ETH_ALEN];
107 /* Network device part of the driver */
109 static LIST_HEAD(tun_dev_list);
110 static const struct ethtool_ops tun_ethtool_ops;
112 /* Net device open. */
113 static int tun_net_open(struct net_device *dev)
115 netif_start_queue(dev);
119 /* Net device close. */
120 static int tun_net_close(struct net_device *dev)
122 netif_stop_queue(dev);
126 /* Net device start xmit */
127 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
129 struct tun_struct *tun = netdev_priv(dev);
131 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
133 /* Drop packet if interface is not attached */
137 /* Packet dropping */
138 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
139 if (!(tun->flags & TUN_ONE_QUEUE)) {
140 /* Normal queueing mode. */
141 /* Packet scheduler handles dropping of further packets. */
142 netif_stop_queue(dev);
144 /* We won't see all dropped packets individually, so overrun
145 * error is more appropriate. */
146 dev->stats.tx_fifo_errors++;
148 /* Single queue mode.
149 * Driver handles dropping of all packets itself. */
155 skb_queue_tail(&tun->readq, skb);
156 dev->trans_start = jiffies;
158 /* Notify and wake up reader process */
159 if (tun->flags & TUN_FASYNC)
160 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
161 wake_up_interruptible(&tun->read_wait);
165 dev->stats.tx_dropped++;
170 /** Add the specified Ethernet address to this multicast filter. */
172 add_multi(u32* filter, const u8* addr)
174 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
175 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
178 /** Remove the specified Ethernet addres from this multicast filter. */
180 del_multi(u32* filter, const u8* addr)
182 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
183 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
186 /** Update the list of multicast groups to which the network device belongs.
187 * This list is used to filter packets being sent from the character device to
188 * the network device. */
190 tun_net_mclist(struct net_device *dev)
192 struct tun_struct *tun = netdev_priv(dev);
193 const struct dev_mc_list *mclist;
195 DECLARE_MAC_BUF(mac);
196 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
197 dev->name, dev->mc_count);
198 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
199 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
200 i++, mclist = mclist->next) {
201 add_multi(tun->net_filter, mclist->dmi_addr);
202 DBG(KERN_DEBUG "%s: tun_net_mclist: %s\n",
203 dev->name, print_mac(mac, mclist->dmi_addr));
208 #define MAX_MTU 65535
211 tun_net_change_mtu(struct net_device *dev, int new_mtu)
213 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
219 /* Initialize net device. */
220 static void tun_net_init(struct net_device *dev)
222 struct tun_struct *tun = netdev_priv(dev);
224 switch (tun->flags & TUN_TYPE_MASK) {
226 /* Point-to-Point TUN Device */
227 dev->hard_header_len = 0;
230 dev->change_mtu = tun_net_change_mtu;
232 /* Zero header length */
233 dev->type = ARPHRD_NONE;
234 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
235 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
239 /* Ethernet TAP Device */
240 dev->set_multicast_list = tun_net_mclist;
243 dev->change_mtu = tun_net_change_mtu;
245 /* random address already created for us by tun_set_iff, use it */
246 memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) );
248 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
253 /* Character device part */
256 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
258 struct tun_struct *tun = file->private_data;
259 unsigned int mask = POLLOUT | POLLWRNORM;
264 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
266 poll_wait(file, &tun->read_wait, wait);
268 if (!skb_queue_empty(&tun->readq))
269 mask |= POLLIN | POLLRDNORM;
274 /* Get packet from user space buffer */
275 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
277 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
279 size_t len = count, align = 0;
281 if (!(tun->flags & TUN_NO_PI)) {
282 if ((len -= sizeof(pi)) > count)
285 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
289 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
290 align = NET_IP_ALIGN;
291 if (unlikely(len < ETH_HLEN))
295 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
296 tun->dev->stats.rx_dropped++;
301 skb_reserve(skb, align);
302 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
303 tun->dev->stats.rx_dropped++;
308 switch (tun->flags & TUN_TYPE_MASK) {
310 skb_reset_mac_header(skb);
311 skb->protocol = pi.proto;
315 skb->protocol = eth_type_trans(skb, tun->dev);
319 if (tun->flags & TUN_NOCHECKSUM)
320 skb->ip_summed = CHECKSUM_UNNECESSARY;
323 tun->dev->last_rx = jiffies;
325 tun->dev->stats.rx_packets++;
326 tun->dev->stats.rx_bytes += len;
331 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
332 unsigned long count, loff_t pos)
334 struct tun_struct *tun = iocb->ki_filp->private_data;
339 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
341 return tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count));
344 /* Put packet to the user space buffer */
345 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
347 struct iovec *iv, int len)
349 struct tun_pi pi = { 0, skb->protocol };
352 if (!(tun->flags & TUN_NO_PI)) {
353 if ((len -= sizeof(pi)) < 0)
356 if (len < skb->len) {
357 /* Packet will be striped */
358 pi.flags |= TUN_PKT_STRIP;
361 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
366 len = min_t(int, skb->len, len);
368 skb_copy_datagram_iovec(skb, 0, iv, len);
371 tun->dev->stats.tx_packets++;
372 tun->dev->stats.tx_bytes += len;
377 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
378 unsigned long count, loff_t pos)
380 struct file *file = iocb->ki_filp;
381 struct tun_struct *tun = file->private_data;
382 DECLARE_WAITQUEUE(wait, current);
384 ssize_t len, ret = 0;
385 DECLARE_MAC_BUF(mac);
390 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
392 len = iov_length(iv, count);
396 add_wait_queue(&tun->read_wait, &wait);
398 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
402 current->state = TASK_INTERRUPTIBLE;
404 /* Read frames from the queue */
405 if (!(skb=skb_dequeue(&tun->readq))) {
406 if (file->f_flags & O_NONBLOCK) {
410 if (signal_pending(current)) {
415 /* Nothing to read, let's sleep */
419 netif_wake_queue(tun->dev);
421 /** Decide whether to accept this packet. This code is designed to
422 * behave identically to an Ethernet interface. Accept the packet if
423 * - we are promiscuous.
424 * - the packet is addressed to us.
425 * - the packet is broadcast.
426 * - the packet is multicast and
427 * - we are multicast promiscous.
428 * - we belong to the multicast group.
430 skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr,
432 bit_nr = ether_crc(sizeof addr, addr) >> 26;
433 if ((tun->if_flags & IFF_PROMISC) ||
434 memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
435 memcmp(addr, ones, sizeof addr) == 0 ||
436 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
437 (addr[0] == 0x33 && addr[1] == 0x33)) &&
438 ((tun->if_flags & IFF_ALLMULTI) ||
439 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
440 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %s\n",
441 tun->dev->name, print_mac(mac, addr));
442 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
446 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %s\n",
447 tun->dev->name, print_mac(mac, addr));
453 current->state = TASK_RUNNING;
454 remove_wait_queue(&tun->read_wait, &wait);
459 static void tun_setup(struct net_device *dev)
461 struct tun_struct *tun = netdev_priv(dev);
463 skb_queue_head_init(&tun->readq);
464 init_waitqueue_head(&tun->read_wait);
469 dev->open = tun_net_open;
470 dev->hard_start_xmit = tun_net_xmit;
471 dev->stop = tun_net_close;
472 dev->ethtool_ops = &tun_ethtool_ops;
473 dev->destructor = free_netdev;
476 static struct tun_struct *tun_get_by_name(const char *name)
478 struct tun_struct *tun;
481 list_for_each_entry(tun, &tun_dev_list, list) {
482 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
489 static int tun_set_iff(struct file *file, struct ifreq *ifr)
491 struct tun_struct *tun;
492 struct net_device *dev;
495 tun = tun_get_by_name(ifr->ifr_name);
500 /* Check permissions */
501 if (((tun->owner != -1 &&
502 current->euid != tun->owner) ||
504 current->egid != tun->group)) &&
505 !capable(CAP_NET_ADMIN))
508 else if (__dev_get_by_name(&init_net, ifr->ifr_name))
512 unsigned long flags = 0;
516 if (!capable(CAP_NET_ADMIN))
520 if (ifr->ifr_flags & IFF_TUN) {
522 flags |= TUN_TUN_DEV;
524 } else if (ifr->ifr_flags & IFF_TAP) {
526 flags |= TUN_TAP_DEV;
532 name = ifr->ifr_name;
534 dev = alloc_netdev(sizeof(struct tun_struct), name,
539 tun = netdev_priv(dev);
542 /* Be promiscuous by default to maintain previous behaviour. */
543 tun->if_flags = IFF_PROMISC;
544 /* Generate random Ethernet address. */
545 *(__be16 *)tun->dev_addr = htons(0x00FF);
546 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
547 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
551 if (strchr(dev->name, '%')) {
552 err = dev_alloc_name(dev, dev->name);
557 err = register_netdevice(tun->dev);
561 list_add(&tun->list, &tun_dev_list);
564 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
566 if (ifr->ifr_flags & IFF_NO_PI)
567 tun->flags |= TUN_NO_PI;
569 tun->flags &= ~TUN_NO_PI;
571 if (ifr->ifr_flags & IFF_ONE_QUEUE)
572 tun->flags |= TUN_ONE_QUEUE;
574 tun->flags &= ~TUN_ONE_QUEUE;
576 file->private_data = tun;
579 strcpy(ifr->ifr_name, tun->dev->name);
588 static int tun_chr_ioctl(struct inode *inode, struct file *file,
589 unsigned int cmd, unsigned long arg)
591 struct tun_struct *tun = file->private_data;
592 void __user* argp = (void __user*)arg;
594 DECLARE_MAC_BUF(mac);
596 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
597 if (copy_from_user(&ifr, argp, sizeof ifr))
600 if (cmd == TUNSETIFF && !tun) {
603 ifr.ifr_name[IFNAMSIZ-1] = '\0';
606 err = tun_set_iff(file, &ifr);
612 if (copy_to_user(argp, &ifr, sizeof(ifr)))
620 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
624 /* Disable/Enable checksum */
626 tun->flags |= TUN_NOCHECKSUM;
628 tun->flags &= ~TUN_NOCHECKSUM;
630 DBG(KERN_INFO "%s: checksum %s\n",
631 tun->dev->name, arg ? "disabled" : "enabled");
635 /* Disable/Enable persist mode */
637 tun->flags |= TUN_PERSIST;
639 tun->flags &= ~TUN_PERSIST;
641 DBG(KERN_INFO "%s: persist %s\n",
642 tun->dev->name, arg ? "enabled" : "disabled");
646 /* Set owner of the device */
647 tun->owner = (uid_t) arg;
649 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
653 /* Set group of the device */
654 tun->group= (gid_t) arg;
656 DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group);
660 /* Only allow setting the type when the interface is down */
661 if (tun->dev->flags & IFF_UP) {
662 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
666 tun->dev->type = (int) arg;
667 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
678 ifr.ifr_flags = tun->if_flags;
679 if (copy_to_user( argp, &ifr, sizeof ifr))
684 /** Set the character device's interface flags. Currently only
685 * IFF_PROMISC and IFF_ALLMULTI are used. */
686 tun->if_flags = ifr.ifr_flags;
687 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
688 tun->dev->name, tun->if_flags);
692 /* Note: the actual net device's address may be different */
693 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
694 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
695 if (copy_to_user( argp, &ifr, sizeof ifr))
701 /* try to set the actual net device's hw address */
705 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
709 /** Set the character device's hardware address. This is used when
710 * filtering packets being sent from the network device to the character
712 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
713 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
714 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
716 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
717 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
724 /** Add the specified group to the character device's multicast filter
726 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
727 DBG(KERN_DEBUG "%s: add multi: %s\n",
728 tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
732 /** Remove the specified group from the character device's multicast
734 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
735 DBG(KERN_DEBUG "%s: del multi: %s\n",
736 tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data));
746 static int tun_chr_fasync(int fd, struct file *file, int on)
748 struct tun_struct *tun = file->private_data;
754 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
756 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
760 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
763 tun->flags |= TUN_FASYNC;
765 tun->flags &= ~TUN_FASYNC;
770 static int tun_chr_open(struct inode *inode, struct file * file)
772 DBG1(KERN_INFO "tunX: tun_chr_open\n");
773 file->private_data = NULL;
777 static int tun_chr_close(struct inode *inode, struct file *file)
779 struct tun_struct *tun = file->private_data;
784 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
786 tun_chr_fasync(-1, file, 0);
790 /* Detach from net device */
791 file->private_data = NULL;
794 /* Drop read queue */
795 skb_queue_purge(&tun->readq);
797 if (!(tun->flags & TUN_PERSIST)) {
798 list_del(&tun->list);
799 unregister_netdevice(tun->dev);
807 static const struct file_operations tun_fops = {
808 .owner = THIS_MODULE,
810 .read = do_sync_read,
811 .aio_read = tun_chr_aio_read,
812 .write = do_sync_write,
813 .aio_write = tun_chr_aio_write,
814 .poll = tun_chr_poll,
815 .ioctl = tun_chr_ioctl,
816 .open = tun_chr_open,
817 .release = tun_chr_close,
818 .fasync = tun_chr_fasync
821 static struct miscdevice tun_miscdev = {
827 /* ethtool interface */
829 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
832 cmd->advertising = 0;
833 cmd->speed = SPEED_10;
834 cmd->duplex = DUPLEX_FULL;
836 cmd->phy_address = 0;
837 cmd->transceiver = XCVR_INTERNAL;
838 cmd->autoneg = AUTONEG_DISABLE;
844 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
846 struct tun_struct *tun = netdev_priv(dev);
848 strcpy(info->driver, DRV_NAME);
849 strcpy(info->version, DRV_VERSION);
850 strcpy(info->fw_version, "N/A");
852 switch (tun->flags & TUN_TYPE_MASK) {
854 strcpy(info->bus_info, "tun");
857 strcpy(info->bus_info, "tap");
862 static u32 tun_get_msglevel(struct net_device *dev)
865 struct tun_struct *tun = netdev_priv(dev);
872 static void tun_set_msglevel(struct net_device *dev, u32 value)
875 struct tun_struct *tun = netdev_priv(dev);
880 static u32 tun_get_link(struct net_device *dev)
882 struct tun_struct *tun = netdev_priv(dev);
883 return tun->attached;
886 static u32 tun_get_rx_csum(struct net_device *dev)
888 struct tun_struct *tun = netdev_priv(dev);
889 return (tun->flags & TUN_NOCHECKSUM) == 0;
892 static int tun_set_rx_csum(struct net_device *dev, u32 data)
894 struct tun_struct *tun = netdev_priv(dev);
896 tun->flags &= ~TUN_NOCHECKSUM;
898 tun->flags |= TUN_NOCHECKSUM;
902 static const struct ethtool_ops tun_ethtool_ops = {
903 .get_settings = tun_get_settings,
904 .get_drvinfo = tun_get_drvinfo,
905 .get_msglevel = tun_get_msglevel,
906 .set_msglevel = tun_set_msglevel,
907 .get_link = tun_get_link,
908 .get_rx_csum = tun_get_rx_csum,
909 .set_rx_csum = tun_set_rx_csum
912 static int __init tun_init(void)
916 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
917 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
919 ret = misc_register(&tun_miscdev);
921 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
925 static void tun_cleanup(void)
927 struct tun_struct *tun, *nxt;
929 misc_deregister(&tun_miscdev);
932 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
933 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
934 unregister_netdevice(tun->dev);
940 module_init(tun_init);
941 module_exit(tun_cleanup);
942 MODULE_DESCRIPTION(DRV_DESCRIPTION);
943 MODULE_AUTHOR(DRV_COPYRIGHT);
944 MODULE_LICENSE("GPL");
945 MODULE_ALIAS_MISCDEV(TUN_MINOR);