[PATCH] revert "swsusp add check for suspension of X controlled devices"
[linux/fpc-iii.git] / drivers / net / tun.c
bloba1ed2d9837402a31f6f46d90b4ae4c49bbd3e485
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 * Changes:
21 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
24 * Mark Smith <markzzzsmith@yahoo.com.au>
25 * Use random_ether_addr() for tap MAC address.
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
30 * Added ethtool API.
31 * Minor cleanups
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
37 #define DRV_NAME "tun"
38 #define DRV_VERSION "1.6"
39 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
40 #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/major.h>
47 #include <linux/slab.h>
48 #include <linux/poll.h>
49 #include <linux/fcntl.h>
50 #include <linux/init.h>
51 #include <linux/skbuff.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/miscdevice.h>
55 #include <linux/ethtool.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/if.h>
58 #include <linux/if_arp.h>
59 #include <linux/if_ether.h>
60 #include <linux/if_tun.h>
61 #include <linux/crc32.h>
63 #include <asm/system.h>
64 #include <asm/uaccess.h>
66 #ifdef TUN_DEBUG
67 static int debug;
68 #endif
70 /* Network device part of the driver */
72 static LIST_HEAD(tun_dev_list);
73 static struct ethtool_ops tun_ethtool_ops;
75 /* Net device open. */
76 static int tun_net_open(struct net_device *dev)
78 netif_start_queue(dev);
79 return 0;
82 /* Net device close. */
83 static int tun_net_close(struct net_device *dev)
85 netif_stop_queue(dev);
86 return 0;
89 /* Net device start xmit */
90 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
92 struct tun_struct *tun = netdev_priv(dev);
94 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
96 /* Drop packet if interface is not attached */
97 if (!tun->attached)
98 goto drop;
100 /* Packet dropping */
101 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
102 if (!(tun->flags & TUN_ONE_QUEUE)) {
103 /* Normal queueing mode. */
104 /* Packet scheduler handles dropping of further packets. */
105 netif_stop_queue(dev);
107 /* We won't see all dropped packets individually, so overrun
108 * error is more appropriate. */
109 tun->stats.tx_fifo_errors++;
110 } else {
111 /* Single queue mode.
112 * Driver handles dropping of all packets itself. */
113 goto drop;
117 /* Queue packet */
118 skb_queue_tail(&tun->readq, skb);
119 dev->trans_start = jiffies;
121 /* Notify and wake up reader process */
122 if (tun->flags & TUN_FASYNC)
123 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
124 wake_up_interruptible(&tun->read_wait);
125 return 0;
127 drop:
128 tun->stats.tx_dropped++;
129 kfree_skb(skb);
130 return 0;
133 /** Add the specified Ethernet address to this multicast filter. */
134 static void
135 add_multi(u32* filter, const u8* addr)
137 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
138 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
141 /** Remove the specified Ethernet addres from this multicast filter. */
142 static void
143 del_multi(u32* filter, const u8* addr)
145 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
146 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
149 /** Update the list of multicast groups to which the network device belongs.
150 * This list is used to filter packets being sent from the character device to
151 * the network device. */
152 static void
153 tun_net_mclist(struct net_device *dev)
155 struct tun_struct *tun = netdev_priv(dev);
156 const struct dev_mc_list *mclist;
157 int i;
158 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
159 dev->name, dev->mc_count);
160 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
161 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
162 i++, mclist = mclist->next) {
163 add_multi(tun->net_filter, mclist->dmi_addr);
164 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
165 dev->name,
166 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
167 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
171 static struct net_device_stats *tun_net_stats(struct net_device *dev)
173 struct tun_struct *tun = netdev_priv(dev);
174 return &tun->stats;
177 /* Initialize net device. */
178 static void tun_net_init(struct net_device *dev)
180 struct tun_struct *tun = netdev_priv(dev);
182 switch (tun->flags & TUN_TYPE_MASK) {
183 case TUN_TUN_DEV:
184 /* Point-to-Point TUN Device */
185 dev->hard_header_len = 0;
186 dev->addr_len = 0;
187 dev->mtu = 1500;
189 /* Zero header length */
190 dev->type = ARPHRD_NONE;
191 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
192 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
193 break;
195 case TUN_TAP_DEV:
196 /* Ethernet TAP Device */
197 dev->set_multicast_list = tun_net_mclist;
199 ether_setup(dev);
200 random_ether_addr(dev->dev_addr);
201 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
202 break;
206 /* Character device part */
208 /* Poll */
209 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
211 struct tun_struct *tun = file->private_data;
212 unsigned int mask = POLLOUT | POLLWRNORM;
214 if (!tun)
215 return -EBADFD;
217 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
219 poll_wait(file, &tun->read_wait, wait);
221 if (!skb_queue_empty(&tun->readq))
222 mask |= POLLIN | POLLRDNORM;
224 return mask;
227 /* Get packet from user space buffer */
228 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
230 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
231 struct sk_buff *skb;
232 size_t len = count, align = 0;
234 if (!(tun->flags & TUN_NO_PI)) {
235 if ((len -= sizeof(pi)) > count)
236 return -EINVAL;
238 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
239 return -EFAULT;
242 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
243 align = NET_IP_ALIGN;
245 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
246 tun->stats.rx_dropped++;
247 return -ENOMEM;
250 if (align)
251 skb_reserve(skb, align);
252 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
253 tun->stats.rx_dropped++;
254 kfree_skb(skb);
255 return -EFAULT;
258 skb->dev = tun->dev;
259 switch (tun->flags & TUN_TYPE_MASK) {
260 case TUN_TUN_DEV:
261 skb->mac.raw = skb->data;
262 skb->protocol = pi.proto;
263 break;
264 case TUN_TAP_DEV:
265 skb->protocol = eth_type_trans(skb, tun->dev);
266 break;
269 if (tun->flags & TUN_NOCHECKSUM)
270 skb->ip_summed = CHECKSUM_UNNECESSARY;
272 netif_rx_ni(skb);
273 tun->dev->last_rx = jiffies;
275 tun->stats.rx_packets++;
276 tun->stats.rx_bytes += len;
278 return count;
281 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
283 unsigned long i;
284 size_t len;
286 for (i = 0, len = 0; i < count; i++)
287 len += iv[i].iov_len;
289 return len;
292 /* Writev */
293 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
294 unsigned long count, loff_t *pos)
296 struct tun_struct *tun = file->private_data;
298 if (!tun)
299 return -EBADFD;
301 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
303 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
306 /* Write */
307 static ssize_t tun_chr_write(struct file * file, const char __user * buf,
308 size_t count, loff_t *pos)
310 struct iovec iv = { (void __user *) buf, count };
311 return tun_chr_writev(file, &iv, 1, pos);
314 /* Put packet to the user space buffer */
315 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
316 struct sk_buff *skb,
317 struct iovec *iv, int len)
319 struct tun_pi pi = { 0, skb->protocol };
320 ssize_t total = 0;
322 if (!(tun->flags & TUN_NO_PI)) {
323 if ((len -= sizeof(pi)) < 0)
324 return -EINVAL;
326 if (len < skb->len) {
327 /* Packet will be striped */
328 pi.flags |= TUN_PKT_STRIP;
331 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
332 return -EFAULT;
333 total += sizeof(pi);
336 len = min_t(int, skb->len, len);
338 skb_copy_datagram_iovec(skb, 0, iv, len);
339 total += len;
341 tun->stats.tx_packets++;
342 tun->stats.tx_bytes += len;
344 return total;
347 /* Readv */
348 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
349 unsigned long count, loff_t *pos)
351 struct tun_struct *tun = file->private_data;
352 DECLARE_WAITQUEUE(wait, current);
353 struct sk_buff *skb;
354 ssize_t len, ret = 0;
356 if (!tun)
357 return -EBADFD;
359 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
361 len = iov_total(iv, count);
362 if (len < 0)
363 return -EINVAL;
365 add_wait_queue(&tun->read_wait, &wait);
366 while (len) {
367 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
368 u8 addr[ ETH_ALEN];
369 int bit_nr;
371 current->state = TASK_INTERRUPTIBLE;
373 /* Read frames from the queue */
374 if (!(skb=skb_dequeue(&tun->readq))) {
375 if (file->f_flags & O_NONBLOCK) {
376 ret = -EAGAIN;
377 break;
379 if (signal_pending(current)) {
380 ret = -ERESTARTSYS;
381 break;
384 /* Nothing to read, let's sleep */
385 schedule();
386 continue;
388 netif_wake_queue(tun->dev);
390 /** Decide whether to accept this packet. This code is designed to
391 * behave identically to an Ethernet interface. Accept the packet if
392 * - we are promiscuous.
393 * - the packet is addressed to us.
394 * - the packet is broadcast.
395 * - the packet is multicast and
396 * - we are multicast promiscous.
397 * - we belong to the multicast group.
399 memcpy(addr, skb->data,
400 min_t(size_t, sizeof addr, skb->len));
401 bit_nr = ether_crc(sizeof addr, addr) >> 26;
402 if ((tun->if_flags & IFF_PROMISC) ||
403 memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
404 memcmp(addr, ones, sizeof addr) == 0 ||
405 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
406 (addr[0] == 0x33 && addr[1] == 0x33)) &&
407 ((tun->if_flags & IFF_ALLMULTI) ||
408 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
409 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
410 tun->dev->name, addr[0], addr[1], addr[2],
411 addr[3], addr[4], addr[5]);
412 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
413 kfree_skb(skb);
414 break;
415 } else {
416 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
417 tun->dev->name, addr[0], addr[1], addr[2],
418 addr[3], addr[4], addr[5]);
419 kfree_skb(skb);
420 continue;
424 current->state = TASK_RUNNING;
425 remove_wait_queue(&tun->read_wait, &wait);
427 return ret;
430 /* Read */
431 static ssize_t tun_chr_read(struct file * file, char __user * buf,
432 size_t count, loff_t *pos)
434 struct iovec iv = { buf, count };
435 return tun_chr_readv(file, &iv, 1, pos);
438 static void tun_setup(struct net_device *dev)
440 struct tun_struct *tun = netdev_priv(dev);
442 skb_queue_head_init(&tun->readq);
443 init_waitqueue_head(&tun->read_wait);
445 tun->owner = -1;
447 SET_MODULE_OWNER(dev);
448 dev->open = tun_net_open;
449 dev->hard_start_xmit = tun_net_xmit;
450 dev->stop = tun_net_close;
451 dev->get_stats = tun_net_stats;
452 dev->ethtool_ops = &tun_ethtool_ops;
453 dev->destructor = free_netdev;
456 static struct tun_struct *tun_get_by_name(const char *name)
458 struct tun_struct *tun;
460 ASSERT_RTNL();
461 list_for_each_entry(tun, &tun_dev_list, list) {
462 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
463 return tun;
466 return NULL;
469 static int tun_set_iff(struct file *file, struct ifreq *ifr)
471 struct tun_struct *tun;
472 struct net_device *dev;
473 int err;
475 tun = tun_get_by_name(ifr->ifr_name);
476 if (tun) {
477 if (tun->attached)
478 return -EBUSY;
480 /* Check permissions */
481 if (tun->owner != -1 &&
482 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
483 return -EPERM;
485 else if (__dev_get_by_name(ifr->ifr_name))
486 return -EINVAL;
487 else {
488 char *name;
489 unsigned long flags = 0;
491 err = -EINVAL;
493 /* Set dev type */
494 if (ifr->ifr_flags & IFF_TUN) {
495 /* TUN device */
496 flags |= TUN_TUN_DEV;
497 name = "tun%d";
498 } else if (ifr->ifr_flags & IFF_TAP) {
499 /* TAP device */
500 flags |= TUN_TAP_DEV;
501 name = "tap%d";
502 } else
503 goto failed;
505 if (*ifr->ifr_name)
506 name = ifr->ifr_name;
508 dev = alloc_netdev(sizeof(struct tun_struct), name,
509 tun_setup);
510 if (!dev)
511 return -ENOMEM;
513 tun = netdev_priv(dev);
514 tun->dev = dev;
515 tun->flags = flags;
516 /* Be promiscuous by default to maintain previous behaviour. */
517 tun->if_flags = IFF_PROMISC;
518 /* Generate random Ethernet address. */
519 *(u16 *)tun->dev_addr = htons(0x00FF);
520 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
521 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
523 tun_net_init(dev);
525 if (strchr(dev->name, '%')) {
526 err = dev_alloc_name(dev, dev->name);
527 if (err < 0)
528 goto err_free_dev;
531 err = register_netdevice(tun->dev);
532 if (err < 0)
533 goto err_free_dev;
535 list_add(&tun->list, &tun_dev_list);
538 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
540 if (ifr->ifr_flags & IFF_NO_PI)
541 tun->flags |= TUN_NO_PI;
543 if (ifr->ifr_flags & IFF_ONE_QUEUE)
544 tun->flags |= TUN_ONE_QUEUE;
546 file->private_data = tun;
547 tun->attached = 1;
549 strcpy(ifr->ifr_name, tun->dev->name);
550 return 0;
552 err_free_dev:
553 free_netdev(dev);
554 failed:
555 return err;
558 static int tun_chr_ioctl(struct inode *inode, struct file *file,
559 unsigned int cmd, unsigned long arg)
561 struct tun_struct *tun = file->private_data;
562 void __user* argp = (void __user*)arg;
563 struct ifreq ifr;
565 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
566 if (copy_from_user(&ifr, argp, sizeof ifr))
567 return -EFAULT;
569 if (cmd == TUNSETIFF && !tun) {
570 int err;
572 ifr.ifr_name[IFNAMSIZ-1] = '\0';
574 rtnl_lock();
575 err = tun_set_iff(file, &ifr);
576 rtnl_unlock();
578 if (err)
579 return err;
581 if (copy_to_user(argp, &ifr, sizeof(ifr)))
582 return -EFAULT;
583 return 0;
586 if (!tun)
587 return -EBADFD;
589 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
591 switch (cmd) {
592 case TUNSETNOCSUM:
593 /* Disable/Enable checksum */
594 if (arg)
595 tun->flags |= TUN_NOCHECKSUM;
596 else
597 tun->flags &= ~TUN_NOCHECKSUM;
599 DBG(KERN_INFO "%s: checksum %s\n",
600 tun->dev->name, arg ? "disabled" : "enabled");
601 break;
603 case TUNSETPERSIST:
604 /* Disable/Enable persist mode */
605 if (arg)
606 tun->flags |= TUN_PERSIST;
607 else
608 tun->flags &= ~TUN_PERSIST;
610 DBG(KERN_INFO "%s: persist %s\n",
611 tun->dev->name, arg ? "disabled" : "enabled");
612 break;
614 case TUNSETOWNER:
615 /* Set owner of the device */
616 tun->owner = (uid_t) arg;
618 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
619 break;
621 case TUNSETLINK:
622 /* Only allow setting the type when the interface is down */
623 if (tun->dev->flags & IFF_UP) {
624 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
625 tun->dev->name);
626 return -EBUSY;
627 } else {
628 tun->dev->type = (int) arg;
629 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
631 break;
633 #ifdef TUN_DEBUG
634 case TUNSETDEBUG:
635 tun->debug = arg;
636 break;
637 #endif
639 case SIOCGIFFLAGS:
640 ifr.ifr_flags = tun->if_flags;
641 if (copy_to_user( argp, &ifr, sizeof ifr))
642 return -EFAULT;
643 return 0;
645 case SIOCSIFFLAGS:
646 /** Set the character device's interface flags. Currently only
647 * IFF_PROMISC and IFF_ALLMULTI are used. */
648 tun->if_flags = ifr.ifr_flags;
649 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
650 tun->dev->name, tun->if_flags);
651 return 0;
653 case SIOCGIFHWADDR:
654 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
655 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
656 if (copy_to_user( argp, &ifr, sizeof ifr))
657 return -EFAULT;
658 return 0;
660 case SIOCSIFHWADDR:
661 /** Set the character device's hardware address. This is used when
662 * filtering packets being sent from the network device to the character
663 * device. */
664 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
665 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
666 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
667 tun->dev->name,
668 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
669 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
670 return 0;
672 case SIOCADDMULTI:
673 /** Add the specified group to the character device's multicast filter
674 * list. */
675 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
676 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
677 tun->dev->name,
678 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
679 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
680 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
681 return 0;
683 case SIOCDELMULTI:
684 /** Remove the specified group from the character device's multicast
685 * filter list. */
686 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
687 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
688 tun->dev->name,
689 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
690 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
691 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
692 return 0;
694 default:
695 return -EINVAL;
698 return 0;
701 static int tun_chr_fasync(int fd, struct file *file, int on)
703 struct tun_struct *tun = file->private_data;
704 int ret;
706 if (!tun)
707 return -EBADFD;
709 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
711 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
712 return ret;
714 if (on) {
715 ret = f_setown(file, current->pid, 0);
716 if (ret)
717 return ret;
718 tun->flags |= TUN_FASYNC;
719 } else
720 tun->flags &= ~TUN_FASYNC;
722 return 0;
725 static int tun_chr_open(struct inode *inode, struct file * file)
727 DBG1(KERN_INFO "tunX: tun_chr_open\n");
728 file->private_data = NULL;
729 return 0;
732 static int tun_chr_close(struct inode *inode, struct file *file)
734 struct tun_struct *tun = file->private_data;
736 if (!tun)
737 return 0;
739 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
741 tun_chr_fasync(-1, file, 0);
743 rtnl_lock();
745 /* Detach from net device */
746 file->private_data = NULL;
747 tun->attached = 0;
749 /* Drop read queue */
750 skb_queue_purge(&tun->readq);
752 if (!(tun->flags & TUN_PERSIST)) {
753 list_del(&tun->list);
754 unregister_netdevice(tun->dev);
757 rtnl_unlock();
759 return 0;
762 static struct file_operations tun_fops = {
763 .owner = THIS_MODULE,
764 .llseek = no_llseek,
765 .read = tun_chr_read,
766 .readv = tun_chr_readv,
767 .write = tun_chr_write,
768 .writev = tun_chr_writev,
769 .poll = tun_chr_poll,
770 .ioctl = tun_chr_ioctl,
771 .open = tun_chr_open,
772 .release = tun_chr_close,
773 .fasync = tun_chr_fasync
776 static struct miscdevice tun_miscdev = {
777 .minor = TUN_MINOR,
778 .name = "tun",
779 .fops = &tun_fops,
780 .devfs_name = "net/tun",
783 /* ethtool interface */
785 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
787 cmd->supported = 0;
788 cmd->advertising = 0;
789 cmd->speed = SPEED_10;
790 cmd->duplex = DUPLEX_FULL;
791 cmd->port = PORT_TP;
792 cmd->phy_address = 0;
793 cmd->transceiver = XCVR_INTERNAL;
794 cmd->autoneg = AUTONEG_DISABLE;
795 cmd->maxtxpkt = 0;
796 cmd->maxrxpkt = 0;
797 return 0;
800 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
802 struct tun_struct *tun = netdev_priv(dev);
804 strcpy(info->driver, DRV_NAME);
805 strcpy(info->version, DRV_VERSION);
806 strcpy(info->fw_version, "N/A");
808 switch (tun->flags & TUN_TYPE_MASK) {
809 case TUN_TUN_DEV:
810 strcpy(info->bus_info, "tun");
811 break;
812 case TUN_TAP_DEV:
813 strcpy(info->bus_info, "tap");
814 break;
818 static u32 tun_get_msglevel(struct net_device *dev)
820 #ifdef TUN_DEBUG
821 struct tun_struct *tun = netdev_priv(dev);
822 return tun->debug;
823 #else
824 return -EOPNOTSUPP;
825 #endif
828 static void tun_set_msglevel(struct net_device *dev, u32 value)
830 #ifdef TUN_DEBUG
831 struct tun_struct *tun = netdev_priv(dev);
832 tun->debug = value;
833 #endif
836 static u32 tun_get_link(struct net_device *dev)
838 struct tun_struct *tun = netdev_priv(dev);
839 return tun->attached;
842 static u32 tun_get_rx_csum(struct net_device *dev)
844 struct tun_struct *tun = netdev_priv(dev);
845 return (tun->flags & TUN_NOCHECKSUM) == 0;
848 static int tun_set_rx_csum(struct net_device *dev, u32 data)
850 struct tun_struct *tun = netdev_priv(dev);
851 if (data)
852 tun->flags &= ~TUN_NOCHECKSUM;
853 else
854 tun->flags |= TUN_NOCHECKSUM;
855 return 0;
858 static struct ethtool_ops tun_ethtool_ops = {
859 .get_settings = tun_get_settings,
860 .get_drvinfo = tun_get_drvinfo,
861 .get_msglevel = tun_get_msglevel,
862 .set_msglevel = tun_set_msglevel,
863 .get_link = tun_get_link,
864 .get_rx_csum = tun_get_rx_csum,
865 .set_rx_csum = tun_set_rx_csum
868 static int __init tun_init(void)
870 int ret = 0;
872 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
873 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
875 ret = misc_register(&tun_miscdev);
876 if (ret)
877 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
878 return ret;
881 static void tun_cleanup(void)
883 struct tun_struct *tun, *nxt;
885 misc_deregister(&tun_miscdev);
887 rtnl_lock();
888 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
889 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
890 unregister_netdevice(tun->dev);
892 rtnl_unlock();
896 module_init(tun_init);
897 module_exit(tun_cleanup);
898 MODULE_DESCRIPTION(DRV_DESCRIPTION);
899 MODULE_AUTHOR(DRV_COPYRIGHT);
900 MODULE_LICENSE("GPL");
901 MODULE_ALIAS_MISCDEV(TUN_MINOR);