vserver 1.9.5.x5
[linux-2.6.git] / drivers / net / tun.c
1 /*
2  *  TUN - Universal TUN/TAP device driver.
3  *  Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4  *
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.
9  *
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.
14  *
15  *  $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16  */
17
18 /*
19  *  Changes:
20  *
21  *  Mark Smith <markzzzsmith@yahoo.com.au>
22  *   Use random_ether_addr() for tap MAC address.
23  *
24  *  Harald Roelle <harald.roelle@ifi.lmu.de>  2004/04/20
25  *    Fixes in packet dropping, queue length setting and queue wakeup.
26  *    Increased default tx queue length.
27  *    Added ethtool API.
28  *    Minor cleanups
29  *
30  *  Daniel Podlejski <underley@underley.eu.org>
31  *    Modifications for 2.3.99-pre5 kernel.
32  */
33
34 #define DRV_NAME        "tun"
35 #define DRV_VERSION     "1.6"
36 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
37 #define DRV_COPYRIGHT   "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
38
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/major.h>
44 #include <linux/slab.h>
45 #include <linux/poll.h>
46 #include <linux/fcntl.h>
47 #include <linux/init.h>
48 #include <linux/skbuff.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/miscdevice.h>
52 #include <linux/ethtool.h>
53 #include <linux/rtnetlink.h>
54 #include <linux/if.h>
55 #include <linux/if_arp.h>
56 #include <linux/if_ether.h>
57 #include <linux/if_tun.h>
58 #include <linux/crc32.h>
59
60 #include <asm/system.h>
61 #include <asm/uaccess.h>
62
63 #ifdef TUN_DEBUG
64 static int debug;
65 #endif
66
67 /* Network device part of the driver */
68
69 static LIST_HEAD(tun_dev_list);
70 static struct ethtool_ops tun_ethtool_ops;
71
72 /* Net device open. */
73 static int tun_net_open(struct net_device *dev)
74 {
75         netif_start_queue(dev);
76         return 0;
77 }
78
79 /* Net device close. */
80 static int tun_net_close(struct net_device *dev)
81 {
82         netif_stop_queue(dev);
83         return 0;
84 }
85
86 /* Net device start xmit */
87 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
88 {
89         struct tun_struct *tun = netdev_priv(dev);
90
91         DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
92
93         /* Drop packet if interface is not attached */
94         if (!tun->attached)
95                 goto drop;
96
97         /* Packet dropping */
98         if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
99                 if (!(tun->flags & TUN_ONE_QUEUE)) {
100                         /* Normal queueing mode. */
101                         /* Packet scheduler handles dropping of further packets. */
102                         netif_stop_queue(dev);
103
104                         /* We won't see all dropped packets individually, so overrun
105                          * error is more appropriate. */
106                         tun->stats.tx_fifo_errors++;
107                 } else {
108                         /* Single queue mode.
109                          * Driver handles dropping of all packets itself. */
110                         goto drop;
111                 }
112         }
113
114         /* Queue packet */
115         skb_queue_tail(&tun->readq, skb);
116         dev->trans_start = jiffies;
117
118         /* Notify and wake up reader process */
119         if (tun->flags & TUN_FASYNC)
120                 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
121         wake_up_interruptible(&tun->read_wait);
122         return 0;
123
124 drop:
125         tun->stats.tx_dropped++;
126         kfree_skb(skb);
127         return 0;
128 }
129
130 /** Add the specified Ethernet address to this multicast filter. */
131 static void
132 add_multi(u32* filter, const u8* addr)
133 {
134         int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
135         filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
136 }
137
138 /** Remove the specified Ethernet addres from this multicast filter. */
139 static void
140 del_multi(u32* filter, const u8* addr)
141 {
142         int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
143         filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
144 }
145
146 /** Update the list of multicast groups to which the network device belongs.
147  * This list is used to filter packets being sent from the character device to
148  * the network device. */
149 static void
150 tun_net_mclist(struct net_device *dev)
151 {
152         struct tun_struct *tun = netdev_priv(dev);
153         const struct dev_mc_list *mclist;
154         int i;
155         DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
156                         dev->name, dev->mc_count);
157         memset(tun->chr_filter, 0, sizeof tun->chr_filter);
158         for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
159                         i++, mclist = mclist->next) {
160                 add_multi(tun->net_filter, mclist->dmi_addr);
161                 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
162                                 dev->name,
163                                 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
164                                 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
165         }
166 }
167
168 static struct net_device_stats *tun_net_stats(struct net_device *dev)
169 {
170         struct tun_struct *tun = netdev_priv(dev);
171         return &tun->stats;
172 }
173
174 /* Initialize net device. */
175 static void tun_net_init(struct net_device *dev)
176 {
177         struct tun_struct *tun = netdev_priv(dev);
178    
179         switch (tun->flags & TUN_TYPE_MASK) {
180         case TUN_TUN_DEV:
181                 /* Point-to-Point TUN Device */
182                 dev->hard_header_len = 0;
183                 dev->addr_len = 0;
184                 dev->mtu = 1500;
185
186                 /* Zero header length */
187                 dev->type = ARPHRD_NONE; 
188                 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
189                 dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
190                 break;
191
192         case TUN_TAP_DEV:
193                 /* Ethernet TAP Device */
194                 dev->set_multicast_list = tun_net_mclist;
195
196                 ether_setup(dev);
197                 random_ether_addr(dev->dev_addr);
198                 dev->tx_queue_len = TUN_READQ_SIZE;  /* We prefer our own queue length */
199                 break;
200         }
201 }
202
203 /* Character device part */
204
205 /* Poll */
206 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
207 {  
208         struct tun_struct *tun = file->private_data;
209         unsigned int mask = POLLOUT | POLLWRNORM;
210
211         if (!tun)
212                 return -EBADFD;
213
214         DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
215
216         poll_wait(file, &tun->read_wait, wait);
217  
218         if (skb_queue_len(&tun->readq))
219                 mask |= POLLIN | POLLRDNORM;
220
221         return mask;
222 }
223
224 /* Get packet from user space buffer */
225 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
226 {
227         struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
228         struct sk_buff *skb;
229         size_t len = count;
230
231         if (!(tun->flags & TUN_NO_PI)) {
232                 if ((len -= sizeof(pi)) > count)
233                         return -EINVAL;
234
235                 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
236                         return -EFAULT;
237         }
238  
239         if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
240                 tun->stats.rx_dropped++;
241                 return -ENOMEM;
242         }
243
244         skb_reserve(skb, 2);
245         if (memcpy_fromiovec(skb_put(skb, len), iv, len))
246                 return -EFAULT;
247
248         skb->dev = tun->dev;
249         switch (tun->flags & TUN_TYPE_MASK) {
250         case TUN_TUN_DEV:
251                 skb->mac.raw = skb->data;
252                 skb->protocol = pi.proto;
253                 break;
254         case TUN_TAP_DEV:
255                 skb->protocol = eth_type_trans(skb, tun->dev);
256                 break;
257         };
258
259         if (tun->flags & TUN_NOCHECKSUM)
260                 skb->ip_summed = CHECKSUM_UNNECESSARY;
261  
262         netif_rx_ni(skb);
263         tun->dev->last_rx = jiffies;
264    
265         tun->stats.rx_packets++;
266         tun->stats.rx_bytes += len;
267
268         return count;
269
270
271 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
272 {
273         unsigned long i;
274         size_t len;
275
276         for (i = 0, len = 0; i < count; i++) 
277                 len += iv[i].iov_len;
278
279         return len;
280 }
281
282 /* Writev */
283 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv, 
284                               unsigned long count, loff_t *pos)
285 {
286         struct tun_struct *tun = file->private_data;
287
288         if (!tun)
289                 return -EBADFD;
290
291         DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
292
293         return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
294 }
295
296 /* Write */
297 static ssize_t tun_chr_write(struct file * file, const char __user * buf, 
298                              size_t count, loff_t *pos)
299 {
300         struct iovec iv = { (void __user *) buf, count };
301         return tun_chr_writev(file, &iv, 1, pos);
302 }
303
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)
308 {
309         struct tun_pi pi = { 0, skb->protocol };
310         ssize_t total = 0;
311
312         if (!(tun->flags & TUN_NO_PI)) {
313                 if ((len -= sizeof(pi)) < 0)
314                         return -EINVAL;
315
316                 if (len < skb->len) {
317                         /* Packet will be striped */
318                         pi.flags |= TUN_PKT_STRIP;
319                 }
320  
321                 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
322                         return -EFAULT;
323                 total += sizeof(pi);
324         }       
325
326         len = min_t(int, skb->len, len);
327
328         skb_copy_datagram_iovec(skb, 0, iv, len);
329         total += len;
330
331         tun->stats.tx_packets++;
332         tun->stats.tx_bytes += len;
333
334         return total;
335 }
336
337 /* Readv */
338 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
339                             unsigned long count, loff_t *pos)
340 {
341         struct tun_struct *tun = file->private_data;
342         DECLARE_WAITQUEUE(wait, current);
343         struct sk_buff *skb;
344         ssize_t len, ret = 0;
345
346         if (!tun)
347                 return -EBADFD;
348
349         DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
350
351         len = iov_total(iv, count);
352         if (len < 0)
353                 return -EINVAL;
354
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;
360
361                 current->state = TASK_INTERRUPTIBLE;
362
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;
368                         }
369                         if (signal_pending(current)) {
370                                 ret = -ERESTARTSYS;
371                                 break;
372                         }
373
374                         /* Nothing to read, let's sleep */
375                         schedule();
376                         continue;
377                 }
378                 netif_wake_queue(tun->dev);
379
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.
388                  */
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;
411                 }
412         }
413
414         current->state = TASK_RUNNING;
415         remove_wait_queue(&tun->read_wait, &wait);
416
417         return ret;
418 }
419
420 /* Read */
421 static ssize_t tun_chr_read(struct file * file, char __user * buf, 
422                             size_t count, loff_t *pos)
423 {
424         struct iovec iv = { buf, count };
425         return tun_chr_readv(file, &iv, 1, pos);
426 }
427
428 static void tun_setup(struct net_device *dev)
429 {
430         struct tun_struct *tun = netdev_priv(dev);
431
432         skb_queue_head_init(&tun->readq);
433         init_waitqueue_head(&tun->read_wait);
434
435         tun->owner = -1;
436
437         SET_MODULE_OWNER(dev);
438         dev->open = tun_net_open;
439         dev->hard_start_xmit = tun_net_xmit;
440         dev->stop = tun_net_close;
441         dev->get_stats = tun_net_stats;
442         dev->ethtool_ops = &tun_ethtool_ops;
443         dev->destructor = free_netdev;
444 }
445
446 static struct tun_struct *tun_get_by_name(const char *name)
447 {
448         struct tun_struct *tun;
449
450         ASSERT_RTNL();
451         list_for_each_entry(tun, &tun_dev_list, list) {
452                 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
453                     return tun;
454         }
455
456         return NULL;
457 }
458
459 static int tun_set_iff(struct file *file, struct ifreq *ifr)
460 {
461         struct tun_struct *tun;
462         struct net_device *dev;
463         int err;
464
465         tun = tun_get_by_name(ifr->ifr_name);
466         if (tun) {
467                 if (tun->attached)
468                         return -EBUSY;
469
470                 /* Check permissions */
471                 if (tun->owner != -1 &&
472                     current->euid != tun->owner && !capable(CAP_NET_ADMIN))
473                         return -EPERM;
474         } 
475         else if (__dev_get_by_name(ifr->ifr_name)) 
476                 return -EINVAL;
477         else {
478                 char *name;
479                 unsigned long flags = 0;
480
481                 err = -EINVAL;
482
483                 /* Set dev type */
484                 if (ifr->ifr_flags & IFF_TUN) {
485                         /* TUN device */
486                         flags |= TUN_TUN_DEV;
487                         name = "tun%d";
488                 } else if (ifr->ifr_flags & IFF_TAP) {
489                         /* TAP device */
490                         flags |= TUN_TAP_DEV;
491                         name = "tap%d";
492                 } else 
493                         goto failed;
494    
495                 if (*ifr->ifr_name)
496                         name = ifr->ifr_name;
497
498                 dev = alloc_netdev(sizeof(struct tun_struct), name,
499                                    tun_setup);
500                 if (!dev)
501                         return -ENOMEM;
502
503                 tun = netdev_priv(dev);
504                 tun->dev = dev;
505                 tun->flags = flags;
506                 /* Be promiscuous by default to maintain previous behaviour. */
507                 tun->if_flags = IFF_PROMISC;
508                 /* Generate random Ethernet address. */
509                 *(u16 *)tun->dev_addr = htons(0x00FF);
510                 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
511                 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
512
513                 tun_net_init(dev);
514
515                 if (strchr(dev->name, '%')) {
516                         err = dev_alloc_name(dev, dev->name);
517                         if (err < 0)
518                                 goto err_free_dev;
519                 }
520
521                 err = register_netdevice(tun->dev);
522                 if (err < 0)
523                         goto err_free_dev;
524         
525                 list_add(&tun->list, &tun_dev_list);
526         }
527
528         DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
529
530         if (ifr->ifr_flags & IFF_NO_PI)
531                 tun->flags |= TUN_NO_PI;
532
533         if (ifr->ifr_flags & IFF_ONE_QUEUE)
534                 tun->flags |= TUN_ONE_QUEUE;
535
536         file->private_data = tun;
537         tun->attached = 1;
538
539         strcpy(ifr->ifr_name, tun->dev->name);
540         return 0;
541
542  err_free_dev:
543         free_netdev(dev);
544  failed:
545         return err;
546 }
547
548 static int tun_chr_ioctl(struct inode *inode, struct file *file, 
549                          unsigned int cmd, unsigned long arg)
550 {
551         struct tun_struct *tun = file->private_data;
552         void __user* argp = (void __user*)arg;
553         struct ifreq ifr;
554
555         if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
556                 if (copy_from_user(&ifr, argp, sizeof ifr))
557                         return -EFAULT;
558
559         if (cmd == TUNSETIFF && !tun) {
560                 int err;
561
562                 ifr.ifr_name[IFNAMSIZ-1] = '\0';
563
564                 rtnl_lock();
565                 err = tun_set_iff(file, &ifr);
566                 rtnl_unlock();
567
568                 if (err)
569                         return err;
570
571                 if (copy_to_user(argp, &ifr, sizeof(ifr)))
572                         return -EFAULT;
573                 return 0;
574         }
575
576         if (!tun)
577                 return -EBADFD;
578
579         DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
580
581         switch (cmd) {
582         case TUNSETNOCSUM:
583                 /* Disable/Enable checksum */
584                 if (arg)
585                         tun->flags |= TUN_NOCHECKSUM;
586                 else
587                         tun->flags &= ~TUN_NOCHECKSUM;
588
589                 DBG(KERN_INFO "%s: checksum %s\n",
590                     tun->dev->name, arg ? "disabled" : "enabled");
591                 break;
592
593         case TUNSETPERSIST:
594                 /* Disable/Enable persist mode */
595                 if (arg)
596                         tun->flags |= TUN_PERSIST;
597                 else
598                         tun->flags &= ~TUN_PERSIST;
599
600                 DBG(KERN_INFO "%s: persist %s\n",
601                     tun->dev->name, arg ? "disabled" : "enabled");
602                 break;
603
604         case TUNSETOWNER:
605                 /* Set owner of the device */
606                 tun->owner = (uid_t) arg;
607
608                 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
609                 break;
610
611 #ifdef TUN_DEBUG
612         case TUNSETDEBUG:
613                 tun->debug = arg;
614                 break;
615 #endif
616
617         case SIOCGIFFLAGS:
618                 ifr.ifr_flags = tun->if_flags;
619                 if (copy_to_user( argp, &ifr, sizeof ifr))
620                         return -EFAULT;
621                 return 0;
622
623         case SIOCSIFFLAGS:
624                 /** Set the character device's interface flags. Currently only
625                  * IFF_PROMISC and IFF_ALLMULTI are used. */
626                 tun->if_flags = ifr.ifr_flags;
627                 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
628                                 tun->dev->name, tun->if_flags);
629                 return 0;
630
631         case SIOCGIFHWADDR:
632                 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
633                                 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
634                 if (copy_to_user( argp, &ifr, sizeof ifr))
635                         return -EFAULT;
636                 return 0;
637
638         case SIOCSIFHWADDR:
639                 /** Set the character device's hardware address. This is used when
640                  * filtering packets being sent from the network device to the character
641                  * device. */
642                 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
643                                 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
644                 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
645                                 tun->dev->name,
646                                 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
647                                 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
648                 return 0;
649
650         case SIOCADDMULTI:
651                 /** Add the specified group to the character device's multicast filter
652                  * list. */
653                 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
654                 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
655                                 tun->dev->name,
656                                 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
657                                 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
658                                 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
659                 return 0;
660
661         case SIOCDELMULTI:
662                 /** Remove the specified group from the character device's multicast
663                  * filter list. */
664                 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
665                 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
666                                 tun->dev->name,
667                                 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
668                                 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
669                                 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
670                 return 0;
671
672         default:
673                 return -EINVAL;
674         };
675
676         return 0;
677 }
678
679 static int tun_chr_fasync(int fd, struct file *file, int on)
680 {
681         struct tun_struct *tun = file->private_data;
682         int ret;
683
684         if (!tun)
685                 return -EBADFD;
686
687         DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
688
689         if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
690                 return ret; 
691  
692         if (on) {
693                 ret = f_setown(file, current->pid, 0);
694                 if (ret)
695                         return ret;
696                 tun->flags |= TUN_FASYNC;
697         } else 
698                 tun->flags &= ~TUN_FASYNC;
699
700         return 0;
701 }
702
703 static int tun_chr_open(struct inode *inode, struct file * file)
704 {
705         DBG1(KERN_INFO "tunX: tun_chr_open\n");
706         file->private_data = NULL;
707         return 0;
708 }
709
710 static int tun_chr_close(struct inode *inode, struct file *file)
711 {
712         struct tun_struct *tun = file->private_data;
713
714         if (!tun)
715                 return 0;
716
717         DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
718
719         tun_chr_fasync(-1, file, 0);
720
721         rtnl_lock();
722
723         /* Detach from net device */
724         file->private_data = NULL;
725         tun->attached = 0;
726
727         /* Drop read queue */
728         skb_queue_purge(&tun->readq);
729
730         if (!(tun->flags & TUN_PERSIST)) {
731                 list_del(&tun->list);
732                 unregister_netdevice(tun->dev);
733         }
734
735         rtnl_unlock();
736
737         return 0;
738 }
739
740 static struct file_operations tun_fops = {
741         .owner  = THIS_MODULE,  
742         .llseek = no_llseek,
743         .read   = tun_chr_read,
744         .readv  = tun_chr_readv,
745         .write  = tun_chr_write,
746         .writev = tun_chr_writev,
747         .poll   = tun_chr_poll,
748         .ioctl  = tun_chr_ioctl,
749         .open   = tun_chr_open,
750         .release = tun_chr_close,
751         .fasync = tun_chr_fasync                
752 };
753
754 static struct miscdevice tun_miscdev = {
755         .minor = TUN_MINOR,
756         .name = "tun",
757         .fops = &tun_fops,
758         .devfs_name = "net/tun",
759 };
760
761 /* ethtool interface */
762
763 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
764 {
765         cmd->supported          = 0;
766         cmd->advertising        = 0;
767         cmd->speed              = SPEED_10;
768         cmd->duplex             = DUPLEX_FULL;
769         cmd->port               = PORT_TP;
770         cmd->phy_address        = 0;
771         cmd->transceiver        = XCVR_INTERNAL;
772         cmd->autoneg            = AUTONEG_DISABLE;
773         cmd->maxtxpkt           = 0;
774         cmd->maxrxpkt           = 0;
775         return 0;
776 }
777
778 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
779 {
780         struct tun_struct *tun = netdev_priv(dev);
781
782         strcpy(info->driver, DRV_NAME);
783         strcpy(info->version, DRV_VERSION);
784         strcpy(info->fw_version, "N/A");
785
786         switch (tun->flags & TUN_TYPE_MASK) {
787         case TUN_TUN_DEV:
788                 strcpy(info->bus_info, "tun");
789                 break;
790         case TUN_TAP_DEV:
791                 strcpy(info->bus_info, "tap");
792                 break;
793         }
794 }
795
796 static u32 tun_get_msglevel(struct net_device *dev)
797 {
798 #ifdef TUN_DEBUG
799         struct tun_struct *tun = netdev_priv(dev);
800         return tun->debug;
801 #else
802         return -EOPNOTSUPP;
803 #endif
804 }
805
806 static void tun_set_msglevel(struct net_device *dev, u32 value)
807 {
808 #ifdef TUN_DEBUG
809         struct tun_struct *tun = netdev_priv(dev);
810         tun->debug = value;
811 #endif
812 }
813
814 static u32 tun_get_link(struct net_device *dev)
815 {
816         struct tun_struct *tun = netdev_priv(dev);
817         return tun->attached;
818 }
819
820 static u32 tun_get_rx_csum(struct net_device *dev)
821 {
822         struct tun_struct *tun = netdev_priv(dev);
823         return (tun->flags & TUN_NOCHECKSUM) == 0;
824 }
825
826 static int tun_set_rx_csum(struct net_device *dev, u32 data)
827 {
828         struct tun_struct *tun = netdev_priv(dev);
829         if (data)
830                 tun->flags &= ~TUN_NOCHECKSUM;
831         else
832                 tun->flags |= TUN_NOCHECKSUM;
833         return 0;
834 }
835
836 static struct ethtool_ops tun_ethtool_ops = {
837         .get_settings   = tun_get_settings,
838         .get_drvinfo    = tun_get_drvinfo,
839         .get_msglevel   = tun_get_msglevel,
840         .set_msglevel   = tun_set_msglevel,
841         .get_link       = tun_get_link,
842         .get_rx_csum    = tun_get_rx_csum,
843         .set_rx_csum    = tun_set_rx_csum
844 };
845
846 int __init tun_init(void)
847 {
848         int ret = 0;
849
850         printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
851         printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
852
853         ret = misc_register(&tun_miscdev);
854         if (ret)
855                 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
856         return ret;
857 }
858
859 void tun_cleanup(void)
860 {
861         struct tun_struct *tun, *nxt;
862
863         misc_deregister(&tun_miscdev);  
864
865         rtnl_lock();
866         list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
867                 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
868                 unregister_netdevice(tun->dev);
869         }
870         rtnl_unlock();
871         
872 }
873
874 module_init(tun_init);
875 module_exit(tun_cleanup);
876 MODULE_DESCRIPTION(DRV_DESCRIPTION);
877 MODULE_AUTHOR(DRV_COPYRIGHT);
878 MODULE_LICENSE("GPL");
879 MODULE_ALIAS_MISCDEV(TUN_MINOR);