GPL'd tun/tap and declared missing init/cleanup funcitons.
[linux-2.6.git] / linux-2.6-526-tun-tap.patch
1 diff -Nurb linux-2.6.22-525/drivers/net/Makefile linux-2.6.22-526/drivers/net/Makefile
2 --- linux-2.6.22-525/drivers/net/Makefile       2008-07-13 23:58:01.000000000 -0400
3 +++ linux-2.6.22-526/drivers/net/Makefile       2008-07-13 23:58:45.000000000 -0400
4 @@ -1,7 +1,7 @@
5  #
6  # Makefile for the Linux network (ethercard) device drivers.
7  #
8 -
9 +obj-m += vnet_tun.o
10  obj-$(CONFIG_E1000) += e1000/
11  obj-$(CONFIG_E1000E) += e1000e/
12  obj-$(CONFIG_IBM_EMAC) += ibm_emac/
13 diff -Nurb linux-2.6.22-525/drivers/net/vnet_tun.c linux-2.6.22-526/drivers/net/vnet_tun.c
14 --- linux-2.6.22-525/drivers/net/vnet_tun.c     1969-12-31 19:00:00.000000000 -0500
15 +++ linux-2.6.22-526/drivers/net/vnet_tun.c     2008-07-14 12:11:21.000000000 -0400
16 @@ -0,0 +1,718 @@
17 +/*
18 + *  TUN - Universal TUN/TAP device driver.
19 + *  Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
20 + *  Modifications for PlanetLab by
21 + *  Mark Huang <mlhuang@cs.princeton.edu>
22 + *  Copyright (C) 2005 The Trustees of Princeton University
23 + *  Ported to PlanetLab 4.2 by Sapan Bhatia <sapanb@cs.princeton.edu>
24 + *
25 + *  Modifications for PlanetLab by
26 + *  Mark Huang <mlhuang@cs.princeton.edu>
27 + *  Copyright (C) 2005 The Trustees of Princeton University
28 + *
29 + *  This program is free software; you can redistribute it and/or modify
30 + *  it under the terms of the GNU General Public License as published by
31 + *  the Free Software Foundation; either version 2 of the License, or
32 + *  (at your option) any later version.
33 + *
34 + *  This program is distributed in the hope that it will be useful,
35 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
36 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 + *  GNU General Public License for more details.
38 + *
39 + *  $Id: vnet_tun.c,v 1.10 2007/03/07 21:25:26 mef Exp $
40 + */
41 +
42 +/*
43 + *  Daniel Podlejski <underley@underley.eu.org>
44 + *    Modifications for 2.3.99-pre5 kernel.
45 + */
46 +
47 +#define TUN_VER "1.5"
48 +
49 +int vnet_verbose=1;
50 +
51 +#define dbg(format, args...) do { if (vnet_verbose >= 2) { printk(format, ## args); } } while (0)
52 +#define err(format, args...) do { if (vnet_verbose >= 1) { printk(format, ## args); } } while (0)
53 +
54 +#include <linux/version.h>
55 +#include <linux/module.h>
56 +#include <linux/errno.h>
57 +#include <linux/kernel.h>
58 +#include <linux/major.h>
59 +#include <linux/slab.h>
60 +#include <linux/poll.h>
61 +#include <linux/fcntl.h>
62 +#include <linux/init.h>
63 +#include <linux/random.h>
64 +#include <linux/skbuff.h>
65 +#include <linux/netdevice.h>
66 +#include <linux/etherdevice.h>
67 +#include <linux/miscdevice.h>
68 +#include <linux/rtnetlink.h>
69 +#include <linux/if.h>
70 +#include <linux/if_arp.h>
71 +#include <linux/if_ether.h>
72 +#include <linux/if_tun.h>
73 +#include <net/sock.h>
74 +
75 +#include <asm/system.h>
76 +#include <asm/uaccess.h>
77 +
78 +/*
79 + * PlanetLab TAP device
80 + *
81 + * A single, persistent tap0 to /dev/net/tun tunnel. Packets sent out
82 + * the tap0 device, via either IP or raw sockets, are queued to both
83 + * the sending slice's /dev/net/tun queue, and the root queue. Only
84 + * one reader of each queue is allowed at a time. Any type of packet
85 + * may be written to /dev/net/tun and received via packet socket(s)
86 + * bound to tap0. However, only locally destined IP packets will be
87 + * injected into the stack, and such packets are subject to the same
88 + * connection tracking and ownership assignment that all inbound IP
89 + * packets are subject to.
90 + */
91 +
92 +struct net_device tun_netdev;
93 +static struct net_device_stats tun_stats;
94 +
95 +int print_once=1;
96 +
97 +static inline xid_t
98 +get_sk_xid(struct sock *sk)
99 +{
100 +       if (sk) {
101 +               return (int) sk->sk_nid >= 0 ? sk->sk_nid : 0;
102 +       } 
103 +       else {
104 +               if (in_interrupt() && print_once) {
105 +                       print_once=0;
106 +                       printk(KERN_EMERG "vnet_tun: get-sk_xid called in interrupt context!\n");
107 +               }
108 +               return current->xid;
109 +       }
110 +}
111 +
112 +#define set_sk_xid(sk,xid) sk->sk_nid=xid
113 +#define set_skb_xid(skb,xid) skb->skb_tag=xid
114 +
115 +/* Extended fields */
116 +struct tun_pi_ext {
117 +       unsigned long mark;
118 +       long timestamp_sec;
119 +       long timestamp_usec;
120 +};
121 +#define TUN_PKT_EXT    0x0002
122 +       
123 +#include <net/ip.h>
124 +
125 +/* UID hash function stolen from kernel/user.c */
126 +#define XIDHASH_BITS           8
127 +#define XIDHASH_SZ             (1 << XIDHASH_BITS)
128 +#define XIDHASH_MASK           (XIDHASH_SZ - 1)
129 +#define __xidhashfn(xid)       (((xid >> XIDHASH_BITS) + xid) & XIDHASH_MASK)
130 +
131 +static struct list_head tun_dev_hash[XIDHASH_SZ];
132 +static rwlock_t tun_dev_hash_lock = RW_LOCK_UNLOCKED;
133 +
134 +static inline xid_t
135 +get_file_xid(struct file *file)
136 +{
137 +               return file->f_xid;
138 +}
139 +
140 +static inline void
141 +set_file_xid(struct file *file, xid_t xid)
142 +{
143 +               file->f_xid = xid;
144 +}
145 +
146 +static struct tun_struct *tun_get_by_xid(xid_t xid)
147 +{
148 +       struct tun_struct *tun;
149 +
150 +       read_lock_bh(&tun_dev_hash_lock);
151 +
152 +       list_for_each_entry(tun, &tun_dev_hash[__xidhashfn(xid)], list) {
153 +               if (tun->owner == xid) {
154 +                       read_unlock_bh(&tun_dev_hash_lock);
155 +                       return tun;
156 +               }
157 +       }
158 +
159 +       read_unlock_bh(&tun_dev_hash_lock);
160 +
161 +       return NULL;
162 +}
163 +
164 +/* Network device part of the driver */
165 +
166 +static void tun_xmit(struct sk_buff *skb, struct tun_struct *tun)
167 +{
168 +       /* Drop packet if interface is not attached */
169 +       if (!tun || !tun->attached)
170 +               goto drop;
171 +
172 +       dbg("%s:%d: tun_xmit %d\n", tun->dev->name, tun->owner, skb->len);
173 +
174 +       /* Queue packet */
175 +       if (skb_queue_len(&tun->readq) >= tun->dev->tx_queue_len)
176 +               goto drop;
177 +
178 +       skb = skb_clone(skb, GFP_ATOMIC);
179 +       if (!skb)
180 +               goto drop;
181 +
182 +       skb_queue_tail(&tun->readq, skb);
183 +
184 +       /* Notify and wake up reader process */
185 +       if (tun->flags & TUN_FASYNC)
186 +               kill_fasync(&tun->fasync, SIGIO, POLL_IN);
187 +       wake_up_interruptible(&tun->read_wait);
188 +
189 + drop:
190 +       if (tun)
191 +               tun->stats.tx_dropped++;
192 +       tun_stats.tx_dropped++;
193 +}
194 +
195 +/* Net device start xmit */
196 +static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
197 +{
198 +       xid_t xid = get_sk_xid(skb->sk);
199 +       struct tun_struct *tun = tun_get_by_xid(xid);
200 +
201 +       /* Mark packet */
202 +       set_skb_xid(skb, xid);
203 +
204 +       tun_xmit(skb, tun);
205 +
206 +       /* Copy root on packets that the slice is not listening for */
207 +       if ((!tun || !tun->attached) && xid) {
208 +               tun = tun_get_by_xid(0);
209 +               tun_xmit(skb, tun);
210 +       }
211 +
212 +       kfree_skb(skb);
213 +       return 0;
214 +}
215 +
216 +static void tun_net_mclist(struct net_device *dev)
217 +{
218 +       /* Nothing to do for multicast filters. 
219 +        * We always accept all frames. */
220 +       return;
221 +}
222 +
223 +static struct net_device_stats *tun_net_stats(struct net_device *dev)
224 +{
225 +       struct tun_struct *tun = tun_get_by_xid(current->xid);
226 +       return tun ? &tun->stats : &tun_stats;
227 +}
228 +
229 +/* Character device part */
230 +
231 +/* Poll */
232 +static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
233 +{  
234 +       struct tun_struct *tun = file->private_data;
235 +       unsigned int mask = POLLOUT | POLLWRNORM;
236 +
237 +       if (!tun)
238 +               return -EBADFD;
239 +
240 +       dbg("%s:%d: tun_chr_poll\n", tun->dev->name, tun->owner);
241 +
242 +       poll_wait(file, &tun->read_wait, wait);
243
244 +       if (skb_queue_len(&tun->readq))
245 +               mask |= POLLIN | POLLRDNORM;
246 +
247 +       return mask;
248 +}
249 +
250 +/* Get packet from user space buffer */
251 +static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
252 +{
253 +       struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
254 +       struct tun_pi_ext pi_ext;
255 +       struct sk_buff *skb;
256 +       size_t len = count;
257 +
258 +       if (!(tun->flags & TUN_NO_PI)) {
259 +               if ((len -= sizeof(pi)) < 0)
260 +                       return -EINVAL;
261 +
262 +               if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
263 +                       return -EFAULT;
264 +
265 +               if (pi.flags & TUN_PKT_EXT) {
266 +                       if ((len -= sizeof(pi_ext)) < 0)
267 +                               return -EINVAL;
268 +
269 +                       if (memcpy_fromiovec((void *)&pi_ext, iv, sizeof(pi_ext)))
270 +                               return -EFAULT;
271 +               }
272 +       }
273
274 +       if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
275 +               tun->stats.rx_dropped++;
276 +               tun_stats.rx_dropped++;
277 +               return -ENOMEM;
278 +       }
279 +
280 +       skb_reserve(skb, 2);
281 +       if (memcpy_fromiovec(skb_put(skb, len), iv, len))
282 +               return -EFAULT;
283 +
284 +       skb->dev = tun->dev;
285 +       switch (tun->flags & TUN_TYPE_MASK) {
286 +       case TUN_TUN_DEV:
287 +               skb_reset_mac_header(skb);
288 +               skb->protocol = pi.proto;
289 +               break;
290 +       case TUN_TAP_DEV:
291 +               skb->protocol = eth_type_trans(skb, tun->dev);
292 +               break;
293 +       };
294 +
295 +       if (tun->flags & TUN_NOCHECKSUM)
296 +               skb->ip_summed = CHECKSUM_UNNECESSARY;
297 +
298 +       /* Mark packet */
299 +       set_skb_xid(skb, tun->owner);
300 +
301 +       netif_rx_ni(skb);
302 +   
303 +       tun->stats.rx_packets++;
304 +       tun->stats.rx_bytes += len;
305 +       tun_stats.rx_packets++;
306 +       tun_stats.rx_bytes += len;
307 +
308 +       return count;
309 +} 
310 +
311 +static inline size_t iov_total(const struct iovec *iv, unsigned long count)
312 +{
313 +       unsigned long i;
314 +       size_t len;
315 +
316 +       for (i = 0, len = 0; i < count; i++) 
317 +               len += iv[i].iov_len;
318 +
319 +       return len;
320 +}
321 +
322 +/* Writev - Obsolete in 2.6.22, but let's keep this aroudn just in case */
323 +static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv, 
324 +                             unsigned long count, loff_t *pos)
325 +{
326 +       struct tun_struct *tun = file->private_data;
327 +
328 +       if (!tun)
329 +               return -EBADFD;
330 +
331 +       dbg("%s:%d: tun_chr_write %ld\n", tun->dev->name, tun->owner, count);
332 +
333 +       return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
334 +}
335 +
336 +/* Write */
337 +static ssize_t tun_chr_write(struct file * file, const char __user * buf, 
338 +                            size_t count, loff_t *pos)
339 +{
340 +       struct iovec iv = { (void __user *) buf, count };
341 +       return tun_chr_writev(file, &iv, 1, pos);
342 +}
343 +
344 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
345 +
346 +static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
347 +{
348 +       stamp->tv_sec = skb->stamp.tv_sec;
349 +       stamp->tv_usec = skb->stamp.tv_usec;
350 +}
351 +
352 +static inline void skb_set_timestamp(struct sk_buff *skb, const struct timeval *stamp)
353 +{
354 +       skb->stamp.tv_sec = stamp->tv_sec;
355 +       skb->stamp.tv_usec = stamp->tv_usec;
356 +}
357 +
358 +static void __net_timestamp(struct sk_buff *skb)
359 +{
360 +       struct timeval tv;
361 +
362 +       do_gettimeofday(&tv);
363 +       skb_set_timestamp(skb, &tv);
364 +}
365 +
366 +#endif
367 +
368 +/* Put packet to the user space buffer */
369 +static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
370 +                                      struct sk_buff *skb,
371 +                                      struct iovec *iv, int len)
372 +{
373 +       struct tun_pi pi;
374 +       struct tun_pi_ext pi_ext;
375 +       struct timeval stamp;
376 +       ssize_t total = 0;
377 +
378 +       if (!(tun->flags & TUN_NO_PI)) {
379 +               struct iovec iv1 = { iv->iov_base, iv->iov_len };
380 +
381 +               if ((len -= sizeof(pi)) < 0)
382 +                       return -EINVAL;
383 +
384 +               if (memcpy_fromiovec((void *)&pi, &iv1, sizeof(pi)))
385 +                       return -EFAULT;
386 +
387 +               if (pi.flags & TUN_PKT_EXT) {
388 +                       if ((len -= sizeof(pi_ext)) < 0)
389 +                               return -EINVAL;
390 +
391 +                       /* We might not have a timestamp, get one */
392 +                       skb_get_timestamp(skb, &stamp);
393 +                       if (stamp.tv_sec == 0) {
394 +                               __net_timestamp(skb);
395 +                               skb_get_timestamp(skb, &stamp);
396 +                       }
397 +
398 +                       pi.flags = TUN_PKT_EXT;
399 +                       pi.proto = skb->protocol;
400 +                       pi_ext.timestamp_sec = stamp.tv_sec;
401 +                       pi_ext.timestamp_usec = stamp.tv_usec;
402 +                       pi_ext.mark = skb->skb_tag;
403 +               } else {
404 +                       pi.flags = 0;
405 +                       pi.proto = skb->protocol;
406 +               }
407 +
408 +               if (len < skb->len) {
409 +                       /* Packet will be striped */
410 +                       pi.flags |= TUN_PKT_STRIP;
411 +               }
412
413 +               if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
414 +                       return -EFAULT;
415 +               total += sizeof(pi);
416 +
417 +               if (pi.flags & TUN_PKT_EXT) {
418 +                       if (memcpy_toiovec(iv, (void *) &pi_ext, sizeof(pi_ext)))
419 +                               return -EFAULT;
420 +                       total += sizeof(pi_ext);
421 +               }
422 +       }       
423 +
424 +       len = min_t(int, skb->len, len);
425 +
426 +       skb_copy_datagram_iovec(skb, 0, iv, len);
427 +       total += len;
428 +
429 +       tun->stats.tx_packets++;
430 +       tun->stats.tx_bytes += len;
431 +       tun_stats.tx_packets++;
432 +       tun_stats.tx_bytes += len;
433 +
434 +       return total;
435 +}
436 +
437 +/* Readv - Obsolete in 2.6.22, but let's keep this aroudn just in case */
438 +static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
439 +                           unsigned long count, loff_t *pos)
440 +{
441 +       struct tun_struct *tun = file->private_data;
442 +       DECLARE_WAITQUEUE(wait, current);
443 +       struct sk_buff *skb;
444 +       ssize_t len, ret = 0;
445 +
446 +       if (!tun)
447 +               return -EBADFD;
448 +
449 +       dbg("%s:%d: tun_chr_read\n", tun->dev->name, tun->owner);
450 +
451 +       len = iov_total(iv, count);
452 +       if (len < 0)
453 +               return -EINVAL;
454 +
455 +       add_wait_queue(&tun->read_wait, &wait);
456 +       while (len) {
457 +               current->state = TASK_INTERRUPTIBLE;
458 +
459 +               /* Read frames from the queue */
460 +               if (!(skb=skb_dequeue(&tun->readq))) {
461 +                       if (file->f_flags & O_NONBLOCK) {
462 +                               ret = -EAGAIN;
463 +                               break;
464 +                       }
465 +                       if (signal_pending(current)) {
466 +                               ret = -ERESTARTSYS;
467 +                               break;
468 +                       }
469 +
470 +                       /* Nothing to read, let's sleep */
471 +                       schedule();
472 +                       continue;
473 +               }
474 +
475 +               ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
476 +
477 +               kfree_skb(skb);
478 +               break;
479 +       }
480 +
481 +       current->state = TASK_RUNNING;
482 +       remove_wait_queue(&tun->read_wait, &wait);
483 +
484 +       return ret;
485 +}
486 +
487 +/* Read */
488 +static ssize_t tun_chr_read(struct file * file, char __user * buf, 
489 +                           size_t count, loff_t *pos)
490 +{
491 +       struct iovec iv = { buf, count };
492 +       return tun_chr_readv(file, &iv, 1, pos);
493 +}
494 +
495 +static int tun_set_iff(struct file *file, struct ifreq *ifr)
496 +{
497 +       struct tun_struct *tun;
498 +
499 +       tun = tun_get_by_xid(get_file_xid(file));
500 +       if (tun) {
501 +               if (tun->attached)
502 +                       return -EBUSY;
503 +
504 +               /* Check permissions */
505 +               if (tun->owner != -1 &&
506 +                   get_file_xid(file) != tun->owner && !capable(CAP_NET_ADMIN))
507 +                       return -EPERM;
508 +       }
509 +       else {
510 +               /* Create a new queue */
511 +               tun = kmalloc(sizeof(struct tun_struct), GFP_KERNEL);
512 +               if (!tun)
513 +                       return -ENOMEM;
514 +               memset(tun, 0, sizeof(struct tun_struct));
515 +
516 +               tun->dev = &tun_netdev;
517 +
518 +               skb_queue_head_init(&tun->readq);
519 +               init_waitqueue_head(&tun->read_wait);
520 +
521 +               tun->owner = get_file_xid(file);
522 +
523 +               write_lock_bh(&tun_dev_hash_lock);
524 +               list_add(&tun->list, &tun_dev_hash[__xidhashfn(get_file_xid(file))]);
525 +               write_unlock_bh(&tun_dev_hash_lock);
526 +       }
527 +
528 +       dbg("%s:%d: tun_set_iff\n", tun->dev->name, tun->owner);
529 +
530 +       tun->flags = TUN_TAP_DEV;
531 +
532 +       if (ifr->ifr_flags & IFF_NO_PI)
533 +               tun->flags |= TUN_NO_PI;
534 +
535 +       file->private_data = tun;
536 +       tun->attached = 1;
537 +
538 +       strcpy(ifr->ifr_name, tun->dev->name);
539 +       return 0;
540 +}
541 +
542 +static int tun_chr_ioctl(struct inode *inode, struct file *file, 
543 +                        unsigned int cmd, unsigned long arg)
544 +{
545 +       struct tun_struct *tun = file->private_data;
546 +
547 +       if (cmd == TUNSETIFF && !tun) {
548 +               struct ifreq ifr;
549 +               int err;
550 +
551 +               if (copy_from_user(&ifr, (void __user *)arg, sizeof(ifr)))
552 +                       return -EFAULT;
553 +               ifr.ifr_name[IFNAMSIZ-1] = '\0';
554 +
555 +               err = tun_set_iff(file, &ifr);
556 +
557 +               if (err)
558 +                       return err;
559 +
560 +               if (copy_to_user((void __user *)arg, &ifr, sizeof(ifr)))
561 +                       return -EFAULT;
562 +               return 0;
563 +       }
564 +
565 +       if (!tun)
566 +               return -EBADFD;
567 +
568 +       dbg("%s:%d: tun_chr_ioctl cmd %d\n", tun->dev->name, tun->owner, cmd);
569 +
570 +       switch (cmd) {
571 +       case TUNSETNOCSUM:
572 +               /* Disable/Enable checksum */
573 +               if (arg)
574 +                       tun->flags |= TUN_NOCHECKSUM;
575 +               else
576 +                       tun->flags &= ~TUN_NOCHECKSUM;
577 +
578 +               dbg("%s:%d: checksum %s\n",
579 +                   tun->dev->name, tun->owner, arg ? "disabled" : "enabled");
580 +               break;
581 +
582 +       case TUNSETPERSIST:
583 +       case TUNSETOWNER:
584 +       case TUNSETDEBUG:
585 +               /* Not applicable */
586 +               break;
587 +
588 +       default:
589 +               return -EINVAL;
590 +       };
591 +
592 +       return 0;
593 +}
594 +
595 +static int tun_chr_fasync(int fd, struct file *file, int on)
596 +{
597 +       struct tun_struct *tun = file->private_data;
598 +       int ret;
599 +
600 +       if (!tun)
601 +               return -EBADFD;
602 +
603 +       dbg("%s:%d: tun_chr_fasync %d\n", tun->dev->name, tun->owner, on);
604 +
605 +       if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
606 +               return ret; 
607
608 +       if (on) {
609 +               ret = f_setown(file, current->pid, 0);
610 +               if (ret)
611 +                       return ret;
612 +               tun->flags |= TUN_FASYNC;
613 +       } else 
614 +               tun->flags &= ~TUN_FASYNC;
615 +
616 +       return 0;
617 +}
618 +
619 +static int tun_chr_open(struct inode *inode, struct file * file)
620 +{
621 +       dbg("tunX: tun_chr_open\n");
622 +       file->private_data = NULL;
623 +       return 0;
624 +}
625 +
626 +static int tun_chr_close(struct inode *inode, struct file *file)
627 +{
628 +       struct tun_struct *tun = file->private_data;
629 +
630 +       if (!tun)
631 +               return 0;
632 +
633 +       dbg("%s:%d: tun_chr_close\n", tun->dev->name, tun->owner);
634 +
635 +       tun_chr_fasync(-1, file, 0);
636 +
637 +       /* Detach from net device */
638 +       file->private_data = NULL;
639 +       tun->attached = 0;
640 +
641 +       /* Drop read queue */
642 +       skb_queue_purge(&tun->readq);
643 +
644 +       return 0;
645 +}
646 +
647 +static struct file_operations tun_fops = {
648 +       .owner  = THIS_MODULE,  
649 +       .llseek = no_llseek,
650 +       .read   = tun_chr_read,
651 +       //.readv        = tun_chr_readv,
652 +       .write  = tun_chr_write,
653 +       //.writev = tun_chr_writev,
654 +       .poll   = tun_chr_poll,
655 +       .ioctl  = tun_chr_ioctl,
656 +       .open   = tun_chr_open,
657 +       .release = tun_chr_close,
658 +       .fasync = tun_chr_fasync                
659 +};
660 +
661 +static struct miscdevice tun_miscdev = {
662 +       .minor = TUN_MINOR,
663 +       .name = "tun",
664 +       .fops = &tun_fops,
665 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
666 +       .devfs_name = "net/tun",
667 +#endif
668 +};
669 +
670 +int __init tun_init(void)
671 +{
672 +       int ret = 0;
673 +       struct net_device *dev = &tun_netdev;
674 +       int i;
675 +
676 +       /* Initialize hash table */
677 +       for (i = 0; i < XIDHASH_SZ; i++)
678 +               INIT_LIST_HEAD(&tun_dev_hash[i]);
679 +
680 +       ret = misc_register(&tun_miscdev);
681 +       if (ret) {
682 +               err("tun: Can't register misc device %d\n", TUN_MINOR);
683 +               return ret;
684 +       }
685 +
686 +       memset(dev, 0, sizeof(struct net_device));
687 +
688 +       /* Ethernet TAP Device */
689 +       dev->set_multicast_list = tun_net_mclist;
690 +
691 +       /* Generate random Ethernet address.  */
692 +       *(u16 *)dev->dev_addr = htons(0x00FF);
693 +       get_random_bytes(dev->dev_addr + sizeof(u16), 4);
694 +
695 +       ether_setup(dev);
696 +
697 +       dev->flags |= IFF_NOARP | IFF_POINTOPOINT;
698 +       dev->flags &= ~IFF_MULTICAST;
699 +
700 +       SET_MODULE_OWNER(dev);
701 +       dev->hard_start_xmit = tun_net_xmit;
702 +       dev->get_stats = tun_net_stats;
703 +
704 +       strcpy(dev->name, "tap0");
705 +
706 +       ret = register_netdev(dev);
707 +       if (ret < 0)
708 +               misc_deregister(&tun_miscdev);
709 +
710 +       return ret;
711 +}
712 +
713 +void __exit tun_cleanup(void)
714 +{
715 +       struct tun_struct *tun, *nxt;
716 +       int i;
717 +
718 +       misc_deregister(&tun_miscdev);  
719 +
720 +       write_lock_bh(&tun_dev_hash_lock);
721 +       for (i = 0; i < XIDHASH_SZ; i++) {
722 +               list_for_each_entry_safe(tun, nxt, &tun_dev_hash[i], list) {
723 +                       skb_queue_purge(&tun->readq);
724 +                       kfree(tun);
725 +               }
726 +       }
727 +       write_unlock_bh(&tun_dev_hash_lock);
728 +
729 +       unregister_netdev(&tun_netdev);
730 +}
731 +
732 +MODULE_LICENSE("GPL");
733 +module_init(tun_init);
734 +module_cleanup(tun_cleanup);