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 $
19 * Daniel Podlejski <underley@underley.eu.org>
20 * Modifications for 2.3.99-pre5 kernel.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/major.h>
30 #include <linux/slab.h>
31 #include <linux/poll.h>
32 #include <linux/fcntl.h>
33 #include <linux/init.h>
34 #include <linux/random.h>
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/miscdevice.h>
39 #include <linux/rtnetlink.h>
41 #include <linux/if_arp.h>
42 #include <linux/if_ether.h>
43 #include <linux/if_tun.h>
45 #include <asm/system.h>
46 #include <asm/uaccess.h>
52 /* Network device part of the driver */
54 static LIST_HEAD(tun_dev_list);
56 /* Net device open. */
57 static int tun_net_open(struct net_device *dev)
59 netif_start_queue(dev);
63 /* Net device close. */
64 static int tun_net_close(struct net_device *dev)
66 netif_stop_queue(dev);
70 /* Net device start xmit */
71 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
73 struct tun_struct *tun = netdev_priv(dev);
75 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
77 /* Drop packet if interface is not attached */
82 if (!(tun->flags & TUN_ONE_QUEUE)) {
83 /* Normal queueing mode.
84 * Packet scheduler handles dropping. */
85 if (skb_queue_len(&tun->readq) >= TUN_READQ_SIZE)
86 netif_stop_queue(dev);
89 * Driver handles dropping itself. */
90 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len)
93 skb_queue_tail(&tun->readq, skb);
95 /* Notify and wake up reader process */
96 if (tun->flags & TUN_FASYNC)
97 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
98 wake_up_interruptible(&tun->read_wait);
102 tun->stats.tx_dropped++;
107 static void tun_net_mclist(struct net_device *dev)
109 /* Nothing to do for multicast filters.
110 * We always accept all frames. */
114 static struct net_device_stats *tun_net_stats(struct net_device *dev)
116 struct tun_struct *tun = netdev_priv(dev);
120 /* Initialize net device. */
121 static void tun_net_init(struct net_device *dev)
123 struct tun_struct *tun = netdev_priv(dev);
125 switch (tun->flags & TUN_TYPE_MASK) {
127 /* Point-to-Point TUN Device */
128 dev->hard_header_len = 0;
132 /* Type PPP seems most suitable */
133 dev->type = ARPHRD_PPP;
134 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
135 dev->tx_queue_len = 10;
139 /* Ethernet TAP Device */
140 dev->set_multicast_list = tun_net_mclist;
142 /* Generate random Ethernet address. */
143 *(u16 *)dev->dev_addr = htons(0x00FF);
144 get_random_bytes(dev->dev_addr + sizeof(u16), 4);
151 /* Character device part */
154 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
156 struct tun_struct *tun = file->private_data;
157 unsigned int mask = POLLOUT | POLLWRNORM;
162 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
164 poll_wait(file, &tun->read_wait, wait);
166 if (skb_queue_len(&tun->readq))
167 mask |= POLLIN | POLLRDNORM;
172 /* Get packet from user space buffer */
173 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
175 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
179 if (!(tun->flags & TUN_NO_PI)) {
180 if ((len -= sizeof(pi)) > len)
183 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
187 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
188 tun->stats.rx_dropped++;
193 if (memcpy_fromiovec(skb_put(skb, len), iv, len))
197 switch (tun->flags & TUN_TYPE_MASK) {
199 skb->mac.raw = skb->data;
200 skb->protocol = pi.proto;
203 skb->protocol = eth_type_trans(skb, tun->dev);
207 if (tun->flags & TUN_NOCHECKSUM)
208 skb->ip_summed = CHECKSUM_UNNECESSARY;
212 tun->stats.rx_packets++;
213 tun->stats.rx_bytes += len;
218 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
223 for (i = 0, len = 0; i < count; i++)
224 len += iv[i].iov_len;
230 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
231 unsigned long count, loff_t *pos)
233 struct tun_struct *tun = file->private_data;
238 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
240 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
244 static ssize_t tun_chr_write(struct file * file, const char * buf,
245 size_t count, loff_t *pos)
247 struct iovec iv = { (void *) buf, count };
248 return tun_chr_writev(file, &iv, 1, pos);
251 /* Put packet to the user space buffer */
252 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
254 struct iovec *iv, int len)
256 struct tun_pi pi = { 0, skb->protocol };
259 if (!(tun->flags & TUN_NO_PI)) {
260 if ((len -= sizeof(pi)) < 0)
263 if (len < skb->len) {
264 /* Packet will be striped */
265 pi.flags |= TUN_PKT_STRIP;
268 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
273 len = min_t(int, skb->len, len);
275 skb_copy_datagram_iovec(skb, 0, iv, len);
278 tun->stats.tx_packets++;
279 tun->stats.tx_bytes += len;
285 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
286 unsigned long count, loff_t *pos)
288 struct tun_struct *tun = file->private_data;
289 DECLARE_WAITQUEUE(wait, current);
291 ssize_t len, ret = 0;
296 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
298 len = iov_total(iv, count);
302 add_wait_queue(&tun->read_wait, &wait);
304 current->state = TASK_INTERRUPTIBLE;
306 /* Read frames from the queue */
307 if (!(skb=skb_dequeue(&tun->readq))) {
308 if (file->f_flags & O_NONBLOCK) {
312 if (signal_pending(current)) {
317 /* Nothing to read, let's sleep */
321 netif_start_queue(tun->dev);
323 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
329 current->state = TASK_RUNNING;
330 remove_wait_queue(&tun->read_wait, &wait);
336 static ssize_t tun_chr_read(struct file * file, char * buf,
337 size_t count, loff_t *pos)
339 struct iovec iv = { buf, count };
340 return tun_chr_readv(file, &iv, 1, pos);
343 static void tun_setup(struct net_device *dev)
345 struct tun_struct *tun = netdev_priv(dev);
347 skb_queue_head_init(&tun->readq);
348 init_waitqueue_head(&tun->read_wait);
352 SET_MODULE_OWNER(dev);
353 dev->open = tun_net_open;
354 dev->hard_start_xmit = tun_net_xmit;
355 dev->stop = tun_net_close;
356 dev->get_stats = tun_net_stats;
357 dev->destructor = free_netdev;
360 static struct tun_struct *tun_get_by_name(const char *name)
362 struct tun_struct *tun;
365 list_for_each_entry(tun, &tun_dev_list, list) {
366 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
373 static int tun_set_iff(struct file *file, struct ifreq *ifr)
375 struct tun_struct *tun;
376 struct net_device *dev;
379 tun = tun_get_by_name(ifr->ifr_name);
384 /* Check permissions */
385 if (tun->owner != -1 &&
386 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
389 else if (__dev_get_by_name(ifr->ifr_name))
393 unsigned long flags = 0;
398 if (ifr->ifr_flags & IFF_TUN) {
400 flags |= TUN_TUN_DEV;
402 } else if (ifr->ifr_flags & IFF_TAP) {
404 flags |= TUN_TAP_DEV;
410 name = ifr->ifr_name;
412 dev = alloc_netdev(sizeof(struct tun_struct), name,
417 tun = netdev_priv(dev);
423 if (strchr(dev->name, '%')) {
424 err = dev_alloc_name(dev, dev->name);
429 err = register_netdevice(tun->dev);
433 list_add(&tun->list, &tun_dev_list);
436 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
438 if (ifr->ifr_flags & IFF_NO_PI)
439 tun->flags |= TUN_NO_PI;
441 if (ifr->ifr_flags & IFF_ONE_QUEUE)
442 tun->flags |= TUN_ONE_QUEUE;
444 file->private_data = tun;
447 strcpy(ifr->ifr_name, tun->dev->name);
456 static int tun_chr_ioctl(struct inode *inode, struct file *file,
457 unsigned int cmd, unsigned long arg)
459 struct tun_struct *tun = file->private_data;
461 if (cmd == TUNSETIFF && !tun) {
465 if (copy_from_user(&ifr, (void *)arg, sizeof(ifr)))
467 ifr.ifr_name[IFNAMSIZ-1] = '\0';
470 err = tun_set_iff(file, &ifr);
476 if (copy_to_user((void *)arg, &ifr, sizeof(ifr)))
484 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
488 /* Disable/Enable checksum */
490 tun->flags |= TUN_NOCHECKSUM;
492 tun->flags &= ~TUN_NOCHECKSUM;
494 DBG(KERN_INFO "%s: checksum %s\n",
495 tun->dev->name, arg ? "disabled" : "enabled");
499 /* Disable/Enable persist mode */
501 tun->flags |= TUN_PERSIST;
503 tun->flags &= ~TUN_PERSIST;
505 DBG(KERN_INFO "%s: persist %s\n",
506 tun->dev->name, arg ? "disabled" : "enabled");
510 /* Set owner of the device */
511 tun->owner = (uid_t) arg;
513 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
529 static int tun_chr_fasync(int fd, struct file *file, int on)
531 struct tun_struct *tun = file->private_data;
537 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
539 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
543 ret = f_setown(file, current->pid, 0);
546 tun->flags |= TUN_FASYNC;
548 tun->flags &= ~TUN_FASYNC;
553 static int tun_chr_open(struct inode *inode, struct file * file)
555 DBG1(KERN_INFO "tunX: tun_chr_open\n");
556 file->private_data = NULL;
560 static int tun_chr_close(struct inode *inode, struct file *file)
562 struct tun_struct *tun = file->private_data;
567 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
569 tun_chr_fasync(-1, file, 0);
573 /* Detach from net device */
574 file->private_data = NULL;
577 /* Drop read queue */
578 skb_queue_purge(&tun->readq);
580 if (!(tun->flags & TUN_PERSIST)) {
581 list_del(&tun->list);
582 unregister_netdevice(tun->dev);
590 static struct file_operations tun_fops = {
591 .owner = THIS_MODULE,
593 .read = tun_chr_read,
594 .readv = tun_chr_readv,
595 .write = tun_chr_write,
596 .writev = tun_chr_writev,
597 .poll = tun_chr_poll,
598 .ioctl = tun_chr_ioctl,
599 .open = tun_chr_open,
600 .release = tun_chr_close,
601 .fasync = tun_chr_fasync
604 static struct miscdevice tun_miscdev = {
608 .devfs_name = "net/tun",
611 int __init tun_init(void)
615 printk(KERN_INFO "Universal TUN/TAP device driver %s "
616 "(C)1999-2002 Maxim Krasnyansky\n", TUN_VER);
618 ret = misc_register(&tun_miscdev);
620 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
624 void tun_cleanup(void)
626 struct tun_struct *tun, *nxt;
628 misc_deregister(&tun_miscdev);
631 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
632 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
633 unregister_netdevice(tun->dev);
639 module_init(tun_init);
640 module_exit(tun_cleanup);
641 MODULE_LICENSE("GPL");
642 MODULE_ALIAS_MISCDEV(TUN_MINOR);