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