[GFS2] Fix change nlink deadlock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / tun.c
blob151a2e10e4f3dc980720dcbd277dd240ea84174a
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/module.h>
43 #include <linux/errno.h>
44 #include <linux/kernel.h>
45 #include <linux/major.h>
46 #include <linux/slab.h>
47 #include <linux/poll.h>
48 #include <linux/fcntl.h>
49 #include <linux/init.h>
50 #include <linux/skbuff.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/miscdevice.h>
54 #include <linux/ethtool.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/if.h>
57 #include <linux/if_arp.h>
58 #include <linux/if_ether.h>
59 #include <linux/if_tun.h>
60 #include <linux/crc32.h>
62 #include <asm/system.h>
63 #include <asm/uaccess.h>
65 #ifdef TUN_DEBUG
66 static int debug;
67 #endif
69 /* Network device part of the driver */
71 static LIST_HEAD(tun_dev_list);
72 static const struct ethtool_ops tun_ethtool_ops;
74 /* Net device open. */
75 static int tun_net_open(struct net_device *dev)
77 netif_start_queue(dev);
78 return 0;
81 /* Net device close. */
82 static int tun_net_close(struct net_device *dev)
84 netif_stop_queue(dev);
85 return 0;
88 /* Net device start xmit */
89 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
91 struct tun_struct *tun = netdev_priv(dev);
93 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
95 /* Drop packet if interface is not attached */
96 if (!tun->attached)
97 goto drop;
99 /* Packet dropping */
100 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
101 if (!(tun->flags & TUN_ONE_QUEUE)) {
102 /* Normal queueing mode. */
103 /* Packet scheduler handles dropping of further packets. */
104 netif_stop_queue(dev);
106 /* We won't see all dropped packets individually, so overrun
107 * error is more appropriate. */
108 tun->stats.tx_fifo_errors++;
109 } else {
110 /* Single queue mode.
111 * Driver handles dropping of all packets itself. */
112 goto drop;
116 /* Queue packet */
117 skb_queue_tail(&tun->readq, skb);
118 dev->trans_start = jiffies;
120 /* Notify and wake up reader process */
121 if (tun->flags & TUN_FASYNC)
122 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
123 wake_up_interruptible(&tun->read_wait);
124 return 0;
126 drop:
127 tun->stats.tx_dropped++;
128 kfree_skb(skb);
129 return 0;
132 /** Add the specified Ethernet address to this multicast filter. */
133 static void
134 add_multi(u32* filter, const u8* addr)
136 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
137 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
140 /** Remove the specified Ethernet addres from this multicast filter. */
141 static void
142 del_multi(u32* filter, const u8* addr)
144 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
145 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
148 /** Update the list of multicast groups to which the network device belongs.
149 * This list is used to filter packets being sent from the character device to
150 * the network device. */
151 static void
152 tun_net_mclist(struct net_device *dev)
154 struct tun_struct *tun = netdev_priv(dev);
155 const struct dev_mc_list *mclist;
156 int i;
157 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
158 dev->name, dev->mc_count);
159 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
160 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
161 i++, mclist = mclist->next) {
162 add_multi(tun->net_filter, mclist->dmi_addr);
163 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
164 dev->name,
165 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
166 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
170 static struct net_device_stats *tun_net_stats(struct net_device *dev)
172 struct tun_struct *tun = netdev_priv(dev);
173 return &tun->stats;
176 /* Initialize net device. */
177 static void tun_net_init(struct net_device *dev)
179 struct tun_struct *tun = netdev_priv(dev);
181 switch (tun->flags & TUN_TYPE_MASK) {
182 case TUN_TUN_DEV:
183 /* Point-to-Point TUN Device */
184 dev->hard_header_len = 0;
185 dev->addr_len = 0;
186 dev->mtu = 1500;
188 /* Zero header length */
189 dev->type = ARPHRD_NONE;
190 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
191 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
192 break;
194 case TUN_TAP_DEV:
195 /* Ethernet TAP Device */
196 dev->set_multicast_list = tun_net_mclist;
198 ether_setup(dev);
199 random_ether_addr(dev->dev_addr);
200 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
201 break;
205 /* Character device part */
207 /* Poll */
208 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
210 struct tun_struct *tun = file->private_data;
211 unsigned int mask = POLLOUT | POLLWRNORM;
213 if (!tun)
214 return -EBADFD;
216 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
218 poll_wait(file, &tun->read_wait, wait);
220 if (!skb_queue_empty(&tun->readq))
221 mask |= POLLIN | POLLRDNORM;
223 return mask;
226 /* Get packet from user space buffer */
227 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
229 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
230 struct sk_buff *skb;
231 size_t len = count, align = 0;
233 if (!(tun->flags & TUN_NO_PI)) {
234 if ((len -= sizeof(pi)) > count)
235 return -EINVAL;
237 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
238 return -EFAULT;
241 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV)
242 align = NET_IP_ALIGN;
244 if (!(skb = alloc_skb(len + align, GFP_KERNEL))) {
245 tun->stats.rx_dropped++;
246 return -ENOMEM;
249 if (align)
250 skb_reserve(skb, align);
251 if (memcpy_fromiovec(skb_put(skb, len), iv, len)) {
252 tun->stats.rx_dropped++;
253 kfree_skb(skb);
254 return -EFAULT;
257 skb->dev = tun->dev;
258 switch (tun->flags & TUN_TYPE_MASK) {
259 case TUN_TUN_DEV:
260 skb->mac.raw = skb->data;
261 skb->protocol = pi.proto;
262 break;
263 case TUN_TAP_DEV:
264 skb->protocol = eth_type_trans(skb, tun->dev);
265 break;
268 if (tun->flags & TUN_NOCHECKSUM)
269 skb->ip_summed = CHECKSUM_UNNECESSARY;
271 netif_rx_ni(skb);
272 tun->dev->last_rx = jiffies;
274 tun->stats.rx_packets++;
275 tun->stats.rx_bytes += len;
277 return count;
280 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
282 unsigned long i;
283 size_t len;
285 for (i = 0, len = 0; i < count; i++)
286 len += iv[i].iov_len;
288 return len;
291 static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
292 unsigned long count, loff_t pos)
294 struct tun_struct *tun = iocb->ki_filp->private_data;
296 if (!tun)
297 return -EBADFD;
299 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
301 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
304 /* Put packet to the user space buffer */
305 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
306 struct sk_buff *skb,
307 struct iovec *iv, int len)
309 struct tun_pi pi = { 0, skb->protocol };
310 ssize_t total = 0;
312 if (!(tun->flags & TUN_NO_PI)) {
313 if ((len -= sizeof(pi)) < 0)
314 return -EINVAL;
316 if (len < skb->len) {
317 /* Packet will be striped */
318 pi.flags |= TUN_PKT_STRIP;
321 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
322 return -EFAULT;
323 total += sizeof(pi);
326 len = min_t(int, skb->len, len);
328 skb_copy_datagram_iovec(skb, 0, iv, len);
329 total += len;
331 tun->stats.tx_packets++;
332 tun->stats.tx_bytes += len;
334 return total;
337 static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
338 unsigned long count, loff_t pos)
340 struct file *file = iocb->ki_filp;
341 struct tun_struct *tun = file->private_data;
342 DECLARE_WAITQUEUE(wait, current);
343 struct sk_buff *skb;
344 ssize_t len, ret = 0;
346 if (!tun)
347 return -EBADFD;
349 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
351 len = iov_total(iv, count);
352 if (len < 0)
353 return -EINVAL;
355 add_wait_queue(&tun->read_wait, &wait);
356 while (len) {
357 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
358 u8 addr[ ETH_ALEN];
359 int bit_nr;
361 current->state = TASK_INTERRUPTIBLE;
363 /* Read frames from the queue */
364 if (!(skb=skb_dequeue(&tun->readq))) {
365 if (file->f_flags & O_NONBLOCK) {
366 ret = -EAGAIN;
367 break;
369 if (signal_pending(current)) {
370 ret = -ERESTARTSYS;
371 break;
374 /* Nothing to read, let's sleep */
375 schedule();
376 continue;
378 netif_wake_queue(tun->dev);
380 /** Decide whether to accept this packet. This code is designed to
381 * behave identically to an Ethernet interface. Accept the packet if
382 * - we are promiscuous.
383 * - the packet is addressed to us.
384 * - the packet is broadcast.
385 * - the packet is multicast and
386 * - we are multicast promiscous.
387 * - we belong to the multicast group.
389 memcpy(addr, skb->data,
390 min_t(size_t, sizeof addr, skb->len));
391 bit_nr = ether_crc(sizeof addr, addr) >> 26;
392 if ((tun->if_flags & IFF_PROMISC) ||
393 memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
394 memcmp(addr, ones, sizeof addr) == 0 ||
395 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
396 (addr[0] == 0x33 && addr[1] == 0x33)) &&
397 ((tun->if_flags & IFF_ALLMULTI) ||
398 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
399 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
400 tun->dev->name, addr[0], addr[1], addr[2],
401 addr[3], addr[4], addr[5]);
402 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
403 kfree_skb(skb);
404 break;
405 } else {
406 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
407 tun->dev->name, addr[0], addr[1], addr[2],
408 addr[3], addr[4], addr[5]);
409 kfree_skb(skb);
410 continue;
414 current->state = TASK_RUNNING;
415 remove_wait_queue(&tun->read_wait, &wait);
417 return ret;
420 static void tun_setup(struct net_device *dev)
422 struct tun_struct *tun = netdev_priv(dev);
424 skb_queue_head_init(&tun->readq);
425 init_waitqueue_head(&tun->read_wait);
427 tun->owner = -1;
429 SET_MODULE_OWNER(dev);
430 dev->open = tun_net_open;
431 dev->hard_start_xmit = tun_net_xmit;
432 dev->stop = tun_net_close;
433 dev->get_stats = tun_net_stats;
434 dev->ethtool_ops = &tun_ethtool_ops;
435 dev->destructor = free_netdev;
438 static struct tun_struct *tun_get_by_name(const char *name)
440 struct tun_struct *tun;
442 ASSERT_RTNL();
443 list_for_each_entry(tun, &tun_dev_list, list) {
444 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
445 return tun;
448 return NULL;
451 static int tun_set_iff(struct file *file, struct ifreq *ifr)
453 struct tun_struct *tun;
454 struct net_device *dev;
455 int err;
457 tun = tun_get_by_name(ifr->ifr_name);
458 if (tun) {
459 if (tun->attached)
460 return -EBUSY;
462 /* Check permissions */
463 if (tun->owner != -1 &&
464 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
465 return -EPERM;
467 else if (__dev_get_by_name(ifr->ifr_name))
468 return -EINVAL;
469 else {
470 char *name;
471 unsigned long flags = 0;
473 err = -EINVAL;
475 if (!capable(CAP_NET_ADMIN))
476 return -EPERM;
478 /* Set dev type */
479 if (ifr->ifr_flags & IFF_TUN) {
480 /* TUN device */
481 flags |= TUN_TUN_DEV;
482 name = "tun%d";
483 } else if (ifr->ifr_flags & IFF_TAP) {
484 /* TAP device */
485 flags |= TUN_TAP_DEV;
486 name = "tap%d";
487 } else
488 goto failed;
490 if (*ifr->ifr_name)
491 name = ifr->ifr_name;
493 dev = alloc_netdev(sizeof(struct tun_struct), name,
494 tun_setup);
495 if (!dev)
496 return -ENOMEM;
498 tun = netdev_priv(dev);
499 tun->dev = dev;
500 tun->flags = flags;
501 /* Be promiscuous by default to maintain previous behaviour. */
502 tun->if_flags = IFF_PROMISC;
503 /* Generate random Ethernet address. */
504 *(u16 *)tun->dev_addr = htons(0x00FF);
505 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
506 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
508 tun_net_init(dev);
510 if (strchr(dev->name, '%')) {
511 err = dev_alloc_name(dev, dev->name);
512 if (err < 0)
513 goto err_free_dev;
516 err = register_netdevice(tun->dev);
517 if (err < 0)
518 goto err_free_dev;
520 list_add(&tun->list, &tun_dev_list);
523 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
525 if (ifr->ifr_flags & IFF_NO_PI)
526 tun->flags |= TUN_NO_PI;
528 if (ifr->ifr_flags & IFF_ONE_QUEUE)
529 tun->flags |= TUN_ONE_QUEUE;
531 file->private_data = tun;
532 tun->attached = 1;
534 strcpy(ifr->ifr_name, tun->dev->name);
535 return 0;
537 err_free_dev:
538 free_netdev(dev);
539 failed:
540 return err;
543 static int tun_chr_ioctl(struct inode *inode, struct file *file,
544 unsigned int cmd, unsigned long arg)
546 struct tun_struct *tun = file->private_data;
547 void __user* argp = (void __user*)arg;
548 struct ifreq ifr;
550 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
551 if (copy_from_user(&ifr, argp, sizeof ifr))
552 return -EFAULT;
554 if (cmd == TUNSETIFF && !tun) {
555 int err;
557 ifr.ifr_name[IFNAMSIZ-1] = '\0';
559 rtnl_lock();
560 err = tun_set_iff(file, &ifr);
561 rtnl_unlock();
563 if (err)
564 return err;
566 if (copy_to_user(argp, &ifr, sizeof(ifr)))
567 return -EFAULT;
568 return 0;
571 if (!tun)
572 return -EBADFD;
574 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
576 switch (cmd) {
577 case TUNSETNOCSUM:
578 /* Disable/Enable checksum */
579 if (arg)
580 tun->flags |= TUN_NOCHECKSUM;
581 else
582 tun->flags &= ~TUN_NOCHECKSUM;
584 DBG(KERN_INFO "%s: checksum %s\n",
585 tun->dev->name, arg ? "disabled" : "enabled");
586 break;
588 case TUNSETPERSIST:
589 /* Disable/Enable persist mode */
590 if (arg)
591 tun->flags |= TUN_PERSIST;
592 else
593 tun->flags &= ~TUN_PERSIST;
595 DBG(KERN_INFO "%s: persist %s\n",
596 tun->dev->name, arg ? "disabled" : "enabled");
597 break;
599 case TUNSETOWNER:
600 /* Set owner of the device */
601 tun->owner = (uid_t) arg;
603 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
604 break;
606 case TUNSETLINK:
607 /* Only allow setting the type when the interface is down */
608 if (tun->dev->flags & IFF_UP) {
609 DBG(KERN_INFO "%s: Linktype set failed because interface is up\n",
610 tun->dev->name);
611 return -EBUSY;
612 } else {
613 tun->dev->type = (int) arg;
614 DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type);
616 break;
618 #ifdef TUN_DEBUG
619 case TUNSETDEBUG:
620 tun->debug = arg;
621 break;
622 #endif
624 case SIOCGIFFLAGS:
625 ifr.ifr_flags = tun->if_flags;
626 if (copy_to_user( argp, &ifr, sizeof ifr))
627 return -EFAULT;
628 return 0;
630 case SIOCSIFFLAGS:
631 /** Set the character device's interface flags. Currently only
632 * IFF_PROMISC and IFF_ALLMULTI are used. */
633 tun->if_flags = ifr.ifr_flags;
634 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
635 tun->dev->name, tun->if_flags);
636 return 0;
638 case SIOCGIFHWADDR:
639 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
640 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
641 if (copy_to_user( argp, &ifr, sizeof ifr))
642 return -EFAULT;
643 return 0;
645 case SIOCSIFHWADDR:
646 /** Set the character device's hardware address. This is used when
647 * filtering packets being sent from the network device to the character
648 * device. */
649 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
650 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
651 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
652 tun->dev->name,
653 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
654 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
655 return 0;
657 case SIOCADDMULTI:
658 /** Add the specified group to the character device's multicast filter
659 * list. */
660 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
661 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
662 tun->dev->name,
663 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
664 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
665 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
666 return 0;
668 case SIOCDELMULTI:
669 /** Remove the specified group from the character device's multicast
670 * filter list. */
671 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
672 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
673 tun->dev->name,
674 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
675 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
676 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
677 return 0;
679 default:
680 return -EINVAL;
683 return 0;
686 static int tun_chr_fasync(int fd, struct file *file, int on)
688 struct tun_struct *tun = file->private_data;
689 int ret;
691 if (!tun)
692 return -EBADFD;
694 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
696 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
697 return ret;
699 if (on) {
700 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
701 if (ret)
702 return ret;
703 tun->flags |= TUN_FASYNC;
704 } else
705 tun->flags &= ~TUN_FASYNC;
707 return 0;
710 static int tun_chr_open(struct inode *inode, struct file * file)
712 DBG1(KERN_INFO "tunX: tun_chr_open\n");
713 file->private_data = NULL;
714 return 0;
717 static int tun_chr_close(struct inode *inode, struct file *file)
719 struct tun_struct *tun = file->private_data;
721 if (!tun)
722 return 0;
724 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
726 tun_chr_fasync(-1, file, 0);
728 rtnl_lock();
730 /* Detach from net device */
731 file->private_data = NULL;
732 tun->attached = 0;
734 /* Drop read queue */
735 skb_queue_purge(&tun->readq);
737 if (!(tun->flags & TUN_PERSIST)) {
738 list_del(&tun->list);
739 unregister_netdevice(tun->dev);
742 rtnl_unlock();
744 return 0;
747 static struct file_operations tun_fops = {
748 .owner = THIS_MODULE,
749 .llseek = no_llseek,
750 .read = do_sync_read,
751 .aio_read = tun_chr_aio_read,
752 .write = do_sync_write,
753 .aio_write = tun_chr_aio_write,
754 .poll = tun_chr_poll,
755 .ioctl = tun_chr_ioctl,
756 .open = tun_chr_open,
757 .release = tun_chr_close,
758 .fasync = tun_chr_fasync
761 static struct miscdevice tun_miscdev = {
762 .minor = TUN_MINOR,
763 .name = "tun",
764 .fops = &tun_fops,
767 /* ethtool interface */
769 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
771 cmd->supported = 0;
772 cmd->advertising = 0;
773 cmd->speed = SPEED_10;
774 cmd->duplex = DUPLEX_FULL;
775 cmd->port = PORT_TP;
776 cmd->phy_address = 0;
777 cmd->transceiver = XCVR_INTERNAL;
778 cmd->autoneg = AUTONEG_DISABLE;
779 cmd->maxtxpkt = 0;
780 cmd->maxrxpkt = 0;
781 return 0;
784 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
786 struct tun_struct *tun = netdev_priv(dev);
788 strcpy(info->driver, DRV_NAME);
789 strcpy(info->version, DRV_VERSION);
790 strcpy(info->fw_version, "N/A");
792 switch (tun->flags & TUN_TYPE_MASK) {
793 case TUN_TUN_DEV:
794 strcpy(info->bus_info, "tun");
795 break;
796 case TUN_TAP_DEV:
797 strcpy(info->bus_info, "tap");
798 break;
802 static u32 tun_get_msglevel(struct net_device *dev)
804 #ifdef TUN_DEBUG
805 struct tun_struct *tun = netdev_priv(dev);
806 return tun->debug;
807 #else
808 return -EOPNOTSUPP;
809 #endif
812 static void tun_set_msglevel(struct net_device *dev, u32 value)
814 #ifdef TUN_DEBUG
815 struct tun_struct *tun = netdev_priv(dev);
816 tun->debug = value;
817 #endif
820 static u32 tun_get_link(struct net_device *dev)
822 struct tun_struct *tun = netdev_priv(dev);
823 return tun->attached;
826 static u32 tun_get_rx_csum(struct net_device *dev)
828 struct tun_struct *tun = netdev_priv(dev);
829 return (tun->flags & TUN_NOCHECKSUM) == 0;
832 static int tun_set_rx_csum(struct net_device *dev, u32 data)
834 struct tun_struct *tun = netdev_priv(dev);
835 if (data)
836 tun->flags &= ~TUN_NOCHECKSUM;
837 else
838 tun->flags |= TUN_NOCHECKSUM;
839 return 0;
842 static const struct ethtool_ops tun_ethtool_ops = {
843 .get_settings = tun_get_settings,
844 .get_drvinfo = tun_get_drvinfo,
845 .get_msglevel = tun_get_msglevel,
846 .set_msglevel = tun_set_msglevel,
847 .get_link = tun_get_link,
848 .get_rx_csum = tun_get_rx_csum,
849 .set_rx_csum = tun_set_rx_csum
852 static int __init tun_init(void)
854 int ret = 0;
856 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
857 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
859 ret = misc_register(&tun_miscdev);
860 if (ret)
861 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
862 return ret;
865 static void tun_cleanup(void)
867 struct tun_struct *tun, *nxt;
869 misc_deregister(&tun_miscdev);
871 rtnl_lock();
872 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
873 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
874 unregister_netdevice(tun->dev);
876 rtnl_unlock();
880 module_init(tun_init);
881 module_exit(tun_cleanup);
882 MODULE_DESCRIPTION(DRV_DESCRIPTION);
883 MODULE_AUTHOR(DRV_COPYRIGHT);
884 MODULE_LICENSE("GPL");
885 MODULE_ALIAS_MISCDEV(TUN_MINOR);