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.1.1.1 2005/04/11 02:50:29 jack 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>
28 #include <linux/errno.h>
29 #include <linux/kernel.h>
30 #include <linux/major.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/fcntl.h>
35 #include <linux/init.h>
36 #include <linux/random.h>
38 #include <linux/skbuff.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/miscdevice.h>
42 #include <linux/rtnetlink.h>
44 #include <linux/if_arp.h>
45 #include <linux/if_ether.h>
46 #include <linux/if_tun.h>
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
55 /* Network device part of the driver */
57 /* Net device open. */
58 static int tun_net_open(struct net_device *dev)
60 netif_start_queue(dev);
64 /* Net device close. */
65 static int tun_net_close(struct net_device *dev)
67 netif_stop_queue(dev);
71 /* Net device start xmit */
72 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
74 struct tun_struct *tun = (struct tun_struct *)dev->priv;
76 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->name, skb->len);
78 /* Drop packet if interface is not attached */
83 if (!(tun->flags & TUN_ONE_QUEUE)) {
84 /* Normal queueing mode.
85 * Packet scheduler handles dropping. */
86 if (skb_queue_len(&tun->readq) >= TUN_READQ_SIZE)
87 netif_stop_queue(dev);
90 * Driver handles dropping itself. */
91 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len)
94 skb_queue_tail(&tun->readq, skb);
96 /* Notify and wake up reader process */
97 if (tun->flags & TUN_FASYNC)
98 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
99 wake_up_interruptible(&tun->read_wait);
103 tun->stats.tx_dropped++;
108 static void tun_net_mclist(struct net_device *dev)
110 /* Nothing to do for multicast filters.
111 * We always accept all frames. */
115 static struct net_device_stats *tun_net_stats(struct net_device *dev)
117 struct tun_struct *tun = (struct tun_struct *)dev->priv;
121 /* Initialize net device. */
122 int tun_net_init(struct net_device *dev)
124 struct tun_struct *tun = (struct tun_struct *)dev->priv;
126 DBG(KERN_INFO "%s: tun_net_init\n", tun->name);
128 SET_MODULE_OWNER(dev);
129 dev->open = tun_net_open;
130 dev->hard_start_xmit = tun_net_xmit;
131 dev->stop = tun_net_close;
132 dev->get_stats = tun_net_stats;
134 switch (tun->flags & TUN_TYPE_MASK) {
136 /* Point-to-Point TUN Device */
137 dev->hard_header_len = 0;
141 /* Type PPP seems most suitable */
142 dev->type = ARPHRD_PPP;
143 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
144 dev->tx_queue_len = 10;
148 /* Ethernet TAP Device */
149 dev->set_multicast_list = tun_net_mclist;
151 /* Generate random Ethernet address. */
152 *(u16 *)dev->dev_addr = htons(0x00FF);
153 get_random_bytes(dev->dev_addr + sizeof(u16), 4);
162 /* Character device part */
165 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
167 struct tun_struct *tun = (struct tun_struct *)file->private_data;
168 unsigned int mask = POLLOUT | POLLWRNORM;
173 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->name);
175 poll_wait(file, &tun->read_wait, wait);
177 if (skb_queue_len(&tun->readq))
178 mask |= POLLIN | POLLRDNORM;
183 /* Get packet from user space buffer(already verified) */
184 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
186 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
190 if (!(tun->flags & TUN_NO_PI)) {
191 if ((len -= sizeof(pi)) < 0)
194 memcpy_fromiovec((void *)&pi, iv, sizeof(pi));
197 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
198 tun->stats.rx_dropped++;
203 memcpy_fromiovec(skb_put(skb, len), iv, len);
205 skb->dev = &tun->dev;
206 switch (tun->flags & TUN_TYPE_MASK) {
208 skb->mac.raw = skb->data;
209 skb->protocol = pi.proto;
212 skb->protocol = eth_type_trans(skb, &tun->dev);
216 if (tun->flags & TUN_NOCHECKSUM)
217 skb->ip_summed = CHECKSUM_UNNECESSARY;
221 tun->stats.rx_packets++;
222 tun->stats.rx_bytes += len;
228 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
229 unsigned long count, loff_t *pos)
231 struct tun_struct *tun = (struct tun_struct *)file->private_data;
238 DBG(KERN_INFO "%s: tun_chr_write %d\n", tun->name, count);
240 for (i = 0, len = 0; i < count; i++) {
241 if (verify_area(VERIFY_READ, iv[i].iov_base, iv[i].iov_len))
243 len += iv[i].iov_len;
246 return tun_get_user(tun, (struct iovec *) iv, len);
250 static ssize_t tun_chr_write(struct file * file, const char * buf,
251 size_t count, loff_t *pos)
253 struct iovec iv = { (void *) buf, count };
254 return tun_chr_writev(file, &iv, 1, pos);
257 /* Put packet to the user space buffer (already verified) */
258 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
260 struct iovec *iv, int len)
262 struct tun_pi pi = { 0, skb->protocol };
265 if (!(tun->flags & TUN_NO_PI)) {
266 if ((len -= sizeof(pi)) < 0)
269 if (len < skb->len) {
270 /* Packet will be striped */
271 pi.flags |= TUN_PKT_STRIP;
274 memcpy_toiovec(iv, (void *) &pi, sizeof(pi));
278 len = MIN(skb->len, len);
280 skb_copy_datagram_iovec(skb, 0, iv, len);
283 tun->stats.tx_packets++;
284 tun->stats.tx_bytes += len;
290 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
291 unsigned long count, loff_t *pos)
293 struct tun_struct *tun = (struct tun_struct *)file->private_data;
294 DECLARE_WAITQUEUE(wait, current);
296 ssize_t len, ret = 0;
302 DBG(KERN_INFO "%s: tun_chr_read\n", tun->name);
304 for (i = 0, len = 0; i < count; i++) {
305 if (verify_area(VERIFY_WRITE, iv[i].iov_base, iv[i].iov_len))
307 len += iv[i].iov_len;
310 add_wait_queue(&tun->read_wait, &wait);
312 current->state = TASK_INTERRUPTIBLE;
314 /* Read frames from the queue */
315 if (!(skb=skb_dequeue(&tun->readq))) {
316 if (file->f_flags & O_NONBLOCK) {
320 if (signal_pending(current)) {
325 /* Nothing to read, let's sleep */
329 netif_start_queue(&tun->dev);
331 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
337 current->state = TASK_RUNNING;
338 remove_wait_queue(&tun->read_wait, &wait);
344 static ssize_t tun_chr_read(struct file * file, char * buf,
345 size_t count, loff_t *pos)
347 struct iovec iv = { buf, count };
348 return tun_chr_readv(file, &iv, 1, pos);
351 static int tun_set_iff(struct file *file, struct ifreq *ifr)
353 struct tun_struct *tun;
354 struct net_device *dev;
357 dev = __dev_get_by_name(ifr->ifr_name);
362 if (dev->init != tun_net_init || tun->attached)
365 /* Check permissions */
366 if (tun->owner != -1)
367 if (current->euid != tun->owner && !capable(CAP_NET_ADMIN))
372 /* Allocate new device */
373 if (!(tun = kmalloc(sizeof(struct tun_struct), GFP_KERNEL)) )
375 memset(tun, 0, sizeof(struct tun_struct));
377 skb_queue_head_init(&tun->readq);
378 init_waitqueue_head(&tun->read_wait);
381 tun->dev.init = tun_net_init;
387 if (ifr->ifr_flags & IFF_TUN) {
389 tun->flags |= TUN_TUN_DEV;
391 } else if (ifr->ifr_flags & IFF_TAP) {
393 tun->flags |= TUN_TAP_DEV;
399 name = ifr->ifr_name;
401 if ((err = dev_alloc_name(&tun->dev, name)) < 0)
403 if ((err = register_netdevice(&tun->dev)))
408 tun->name = tun->dev.name;
411 DBG(KERN_INFO "%s: tun_set_iff\n", tun->name);
413 if (ifr->ifr_flags & IFF_NO_PI)
414 tun->flags |= TUN_NO_PI;
416 if (ifr->ifr_flags & IFF_ONE_QUEUE)
417 tun->flags |= TUN_ONE_QUEUE;
419 file->private_data = tun;
422 strcpy(ifr->ifr_name, tun->name);
430 static int tun_chr_ioctl(struct inode *inode, struct file *file,
431 unsigned int cmd, unsigned long arg)
433 struct tun_struct *tun = (struct tun_struct *)file->private_data;
435 if (cmd == TUNSETIFF && !tun) {
439 if (copy_from_user(&ifr, (void *)arg, sizeof(ifr)))
441 ifr.ifr_name[IFNAMSIZ-1] = '\0';
444 err = tun_set_iff(file, &ifr);
450 copy_to_user((void *)arg, &ifr, sizeof(ifr));
457 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->name, cmd);
461 /* Disable/Enable checksum */
463 tun->flags |= TUN_NOCHECKSUM;
465 tun->flags &= ~TUN_NOCHECKSUM;
467 DBG(KERN_INFO "%s: checksum %s\n",
468 tun->name, arg ? "disabled" : "enabled");
472 /* Disable/Enable persist mode */
474 tun->flags |= TUN_PERSIST;
476 tun->flags &= ~TUN_PERSIST;
478 DBG(KERN_INFO "%s: persist %s\n",
479 tun->name, arg ? "disabled" : "enabled");
483 /* Set owner of the device */
484 tun->owner = (uid_t) arg;
486 DBG(KERN_INFO "%s: owner set to %d\n", tun->owner);
502 static int tun_chr_fasync(int fd, struct file *file, int on)
504 struct tun_struct *tun = (struct tun_struct *)file->private_data;
510 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->name, on);
512 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
516 tun->flags |= TUN_FASYNC;
517 if (!file->f_owner.pid) {
518 file->f_owner.pid = current->pid;
519 file->f_owner.uid = current->uid;
520 file->f_owner.euid = current->euid;
523 tun->flags &= ~TUN_FASYNC;
528 static int tun_chr_open(struct inode *inode, struct file * file)
530 DBG1(KERN_INFO "tunX: tun_chr_open\n");
531 file->private_data = NULL;
535 static int tun_chr_close(struct inode *inode, struct file *file)
537 struct tun_struct *tun = (struct tun_struct *)file->private_data;
542 DBG(KERN_INFO "%s: tun_chr_close\n", tun->name);
544 tun_chr_fasync(-1, file, 0);
548 /* Detach from net device */
549 file->private_data = NULL;
552 /* Drop read queue */
553 skb_queue_purge(&tun->readq);
555 if (!(tun->flags & TUN_PERSIST)) {
556 dev_close(&tun->dev);
557 unregister_netdevice(&tun->dev);
566 static struct file_operations tun_fops = {
570 readv: tun_chr_readv,
571 write: tun_chr_write,
572 writev: tun_chr_writev,
574 ioctl: tun_chr_ioctl,
576 release:tun_chr_close,
577 fasync: tun_chr_fasync
580 static struct miscdevice tun_miscdev=
587 int __init tun_init(void)
589 printk(KERN_INFO "Universal TUN/TAP device driver %s "
590 "(C)1999-2002 Maxim Krasnyansky\n", TUN_VER);
592 if (misc_register(&tun_miscdev)) {
593 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
600 void tun_cleanup(void)
602 misc_deregister(&tun_miscdev);
605 module_init(tun_init);
606 module_exit(tun_cleanup);
607 MODULE_LICENSE("GPL");