MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / tun.c
blobd0fe0f656ea68bcad14a4fdef4199de1091ad4cf
1 /*
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.
23 #define TUN_VER "1.5"
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>
40 #include <linux/if.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>
48 #ifdef TUN_DEBUG
49 static int debug;
50 #endif
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);
60 return 0;
63 /* Net device close. */
64 static int tun_net_close(struct net_device *dev)
66 netif_stop_queue(dev);
67 return 0;
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 */
78 if (!tun->attached)
79 goto drop;
81 /* Queue packet */
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);
87 } else {
88 /* Single queue mode.
89 * Driver handles dropping itself. */
90 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len)
91 goto drop;
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);
99 return 0;
101 drop:
102 tun->stats.tx_dropped++;
103 kfree_skb(skb);
104 return 0;
107 static void tun_net_mclist(struct net_device *dev)
109 /* Nothing to do for multicast filters.
110 * We always accept all frames. */
111 return;
114 static struct net_device_stats *tun_net_stats(struct net_device *dev)
116 struct tun_struct *tun = netdev_priv(dev);
117 return &tun->stats;
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) {
126 case TUN_TUN_DEV:
127 /* Point-to-Point TUN Device */
128 dev->hard_header_len = 0;
129 dev->addr_len = 0;
130 dev->mtu = 1500;
132 /* Zero header length */
133 dev->type = ARPHRD_NONE;
134 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
135 dev->tx_queue_len = 10;
136 break;
138 case TUN_TAP_DEV:
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);
146 ether_setup(dev);
147 break;
151 /* Character device part */
153 /* Poll */
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;
159 if (!tun)
160 return -EBADFD;
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;
169 return mask;
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) };
176 struct sk_buff *skb;
177 size_t len = count;
179 if (!(tun->flags & TUN_NO_PI)) {
180 if ((len -= sizeof(pi)) > len)
181 return -EINVAL;
183 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
184 return -EFAULT;
187 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
188 tun->stats.rx_dropped++;
189 return -ENOMEM;
192 skb_reserve(skb, 2);
193 if (memcpy_fromiovec(skb_put(skb, len), iv, len))
194 return -EFAULT;
196 skb->dev = tun->dev;
197 switch (tun->flags & TUN_TYPE_MASK) {
198 case TUN_TUN_DEV:
199 skb->mac.raw = skb->data;
200 skb->protocol = pi.proto;
201 break;
202 case TUN_TAP_DEV:
203 skb->protocol = eth_type_trans(skb, tun->dev);
204 break;
207 if (tun->flags & TUN_NOCHECKSUM)
208 skb->ip_summed = CHECKSUM_UNNECESSARY;
210 netif_rx_ni(skb);
212 tun->stats.rx_packets++;
213 tun->stats.rx_bytes += len;
215 return count;
218 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
220 unsigned long i;
221 size_t len;
223 for (i = 0, len = 0; i < count; i++)
224 len += iv[i].iov_len;
226 return len;
229 /* Writev */
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;
235 if (!tun)
236 return -EBADFD;
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));
243 /* Write */
244 static ssize_t tun_chr_write(struct file * file, const char __user * buf,
245 size_t count, loff_t *pos)
247 struct iovec iv = { (void __user *) 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,
253 struct sk_buff *skb,
254 struct iovec *iv, int len)
256 struct tun_pi pi = { 0, skb->protocol };
257 ssize_t total = 0;
259 if (!(tun->flags & TUN_NO_PI)) {
260 if ((len -= sizeof(pi)) < 0)
261 return -EINVAL;
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)))
269 return -EFAULT;
270 total += sizeof(pi);
273 len = min_t(int, skb->len, len);
275 skb_copy_datagram_iovec(skb, 0, iv, len);
276 total += len;
278 tun->stats.tx_packets++;
279 tun->stats.tx_bytes += len;
281 return total;
284 /* Readv */
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);
290 struct sk_buff *skb;
291 ssize_t len, ret = 0;
293 if (!tun)
294 return -EBADFD;
296 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
298 len = iov_total(iv, count);
299 if (len < 0)
300 return -EINVAL;
302 add_wait_queue(&tun->read_wait, &wait);
303 while (len) {
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) {
309 ret = -EAGAIN;
310 break;
312 if (signal_pending(current)) {
313 ret = -ERESTARTSYS;
314 break;
317 /* Nothing to read, let's sleep */
318 schedule();
319 continue;
321 netif_start_queue(tun->dev);
323 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
325 kfree_skb(skb);
326 break;
329 current->state = TASK_RUNNING;
330 remove_wait_queue(&tun->read_wait, &wait);
332 return ret;
335 /* Read */
336 static ssize_t tun_chr_read(struct file * file, char __user * 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);
350 tun->owner = -1;
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;
364 ASSERT_RTNL();
365 list_for_each_entry(tun, &tun_dev_list, list) {
366 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
367 return tun;
370 return NULL;
373 static int tun_set_iff(struct file *file, struct ifreq *ifr)
375 struct tun_struct *tun;
376 struct net_device *dev;
377 int err;
379 tun = tun_get_by_name(ifr->ifr_name);
380 if (tun) {
381 if (tun->attached)
382 return -EBUSY;
384 /* Check permissions */
385 if (tun->owner != -1 &&
386 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
387 return -EPERM;
389 else if (__dev_get_by_name(ifr->ifr_name))
390 return -EINVAL;
391 else {
392 char *name;
393 unsigned long flags = 0;
395 err = -EINVAL;
397 /* Set dev type */
398 if (ifr->ifr_flags & IFF_TUN) {
399 /* TUN device */
400 flags |= TUN_TUN_DEV;
401 name = "tun%d";
402 } else if (ifr->ifr_flags & IFF_TAP) {
403 /* TAP device */
404 flags |= TUN_TAP_DEV;
405 name = "tap%d";
406 } else
407 goto failed;
409 if (*ifr->ifr_name)
410 name = ifr->ifr_name;
412 dev = alloc_netdev(sizeof(struct tun_struct), name,
413 tun_setup);
414 if (!dev)
415 return -ENOMEM;
417 tun = netdev_priv(dev);
418 tun->dev = dev;
419 tun->flags = flags;
421 tun_net_init(dev);
423 if (strchr(dev->name, '%')) {
424 err = dev_alloc_name(dev, dev->name);
425 if (err < 0)
426 goto err_free_dev;
429 err = register_netdevice(tun->dev);
430 if (err < 0)
431 goto err_free_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;
445 tun->attached = 1;
447 strcpy(ifr->ifr_name, tun->dev->name);
448 return 0;
450 err_free_dev:
451 free_netdev(dev);
452 failed:
453 return err;
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) {
462 struct ifreq ifr;
463 int err;
465 if (copy_from_user(&ifr, (void __user *)arg, sizeof(ifr)))
466 return -EFAULT;
467 ifr.ifr_name[IFNAMSIZ-1] = '\0';
469 rtnl_lock();
470 err = tun_set_iff(file, &ifr);
471 rtnl_unlock();
473 if (err)
474 return err;
476 if (copy_to_user((void __user *)arg, &ifr, sizeof(ifr)))
477 return -EFAULT;
478 return 0;
481 if (!tun)
482 return -EBADFD;
484 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
486 switch (cmd) {
487 case TUNSETNOCSUM:
488 /* Disable/Enable checksum */
489 if (arg)
490 tun->flags |= TUN_NOCHECKSUM;
491 else
492 tun->flags &= ~TUN_NOCHECKSUM;
494 DBG(KERN_INFO "%s: checksum %s\n",
495 tun->dev->name, arg ? "disabled" : "enabled");
496 break;
498 case TUNSETPERSIST:
499 /* Disable/Enable persist mode */
500 if (arg)
501 tun->flags |= TUN_PERSIST;
502 else
503 tun->flags &= ~TUN_PERSIST;
505 DBG(KERN_INFO "%s: persist %s\n",
506 tun->dev->name, arg ? "disabled" : "enabled");
507 break;
509 case TUNSETOWNER:
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);
514 break;
516 #ifdef TUN_DEBUG
517 case TUNSETDEBUG:
518 tun->debug = arg;
519 break;
520 #endif
522 default:
523 return -EINVAL;
526 return 0;
529 static int tun_chr_fasync(int fd, struct file *file, int on)
531 struct tun_struct *tun = file->private_data;
532 int ret;
534 if (!tun)
535 return -EBADFD;
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)
540 return ret;
542 if (on) {
543 ret = f_setown(file, current->pid, 0);
544 if (ret)
545 return ret;
546 tun->flags |= TUN_FASYNC;
547 } else
548 tun->flags &= ~TUN_FASYNC;
550 return 0;
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;
557 return 0;
560 static int tun_chr_close(struct inode *inode, struct file *file)
562 struct tun_struct *tun = file->private_data;
564 if (!tun)
565 return 0;
567 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
569 tun_chr_fasync(-1, file, 0);
571 rtnl_lock();
573 /* Detach from net device */
574 file->private_data = NULL;
575 tun->attached = 0;
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);
585 rtnl_unlock();
587 return 0;
590 static struct file_operations tun_fops = {
591 .owner = THIS_MODULE,
592 .llseek = no_llseek,
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 = {
605 .minor = TUN_MINOR,
606 .name = "tun",
607 .fops = &tun_fops,
608 .devfs_name = "net/tun",
611 int __init tun_init(void)
613 int ret = 0;
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);
619 if (ret)
620 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
621 return ret;
624 void tun_cleanup(void)
626 struct tun_struct *tun, *nxt;
628 misc_deregister(&tun_miscdev);
630 rtnl_lock();
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);
635 rtnl_unlock();
639 module_init(tun_init);
640 module_exit(tun_cleanup);
641 MODULE_LICENSE("GPL");
642 MODULE_ALIAS_MISCDEV(TUN_MINOR);