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
6 # Makefile for the Linux network (ethercard) device drivers.
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
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>
25 + * Modifications for PlanetLab by
26 + * Mark Huang <mlhuang@cs.princeton.edu>
27 + * Copyright (C) 2005 The Trustees of Princeton University
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.
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.
39 + * $Id: vnet_tun.c,v 1.10 2007/03/07 21:25:26 mef Exp $
43 + * Daniel Podlejski <underley@underley.eu.org>
44 + * Modifications for 2.3.99-pre5 kernel.
47 +#define TUN_VER "1.5"
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)
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>
74 +#include <asm/system.h>
75 +#include <asm/uaccess.h>
78 + * PlanetLab TAP device
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.
91 +struct net_device tun_netdev;
92 +static struct net_device_stats tun_stats;
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
98 +/* Extended fields */
100 + unsigned long mark;
101 + long timestamp_sec;
102 + long timestamp_usec;
104 +#define TUN_PKT_EXT 0x0002
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)
114 +static struct list_head tun_dev_hash[XIDHASH_SZ];
115 +static rwlock_t tun_dev_hash_lock = RW_LOCK_UNLOCKED;
118 +get_file_xid(struct file *file)
120 + return file->f_xid;
124 +set_file_xid(struct file *file, xid_t xid)
129 +static struct tun_struct *tun_get_by_xid(xid_t xid)
131 + struct tun_struct *tun;
133 + read_lock_bh(&tun_dev_hash_lock);
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);
142 + read_unlock_bh(&tun_dev_hash_lock);
147 +/* Network device part of the driver */
149 +static void tun_xmit(struct sk_buff *skb, struct tun_struct *tun)
151 + /* Drop packet if interface is not attached */
152 + if (!tun || !tun->attached)
155 + dbg("%s:%d: tun_xmit %d\n", tun->dev->name, tun->owner, skb->len);
158 + if (skb_queue_len(&tun->readq) >= tun->dev->tx_queue_len)
161 + skb = skb_clone(skb, GFP_ATOMIC);
165 + skb_queue_tail(&tun->readq, skb);
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);
174 + tun->stats.tx_dropped++;
175 + tun_stats.tx_dropped++;
178 +/* Net device start xmit */
179 +static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
181 + xid_t xid = get_sk_xid(skb->sk);
182 + struct tun_struct *tun = tun_get_by_xid(xid);
185 + set_skb_xid(skb, xid);
187 + tun_xmit(skb, tun);
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);
199 +static void tun_net_mclist(struct net_device *dev)
201 + /* Nothing to do for multicast filters.
202 + * We always accept all frames. */
206 +static struct net_device_stats *tun_net_stats(struct net_device *dev)
208 + struct tun_struct *tun = tun_get_by_xid(current->xid);
209 + return tun ? &tun->stats : &tun_stats;
212 +/* Character device part */
215 +static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
217 + struct tun_struct *tun = file->private_data;
218 + unsigned int mask = POLLOUT | POLLWRNORM;
223 + dbg("%s:%d: tun_chr_poll\n", tun->dev->name, tun->owner);
225 + poll_wait(file, &tun->read_wait, wait);
227 + if (skb_queue_len(&tun->readq))
228 + mask |= POLLIN | POLLRDNORM;
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)
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;
241 + if (!(tun->flags & TUN_NO_PI)) {
242 + if ((len -= sizeof(pi)) < 0)
245 + if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
248 + if (pi.flags & TUN_PKT_EXT) {
249 + if ((len -= sizeof(pi_ext)) < 0)
252 + if (memcpy_fromiovec((void *)&pi_ext, iv, sizeof(pi_ext)))
257 + if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
258 + tun->stats.rx_dropped++;
259 + tun_stats.rx_dropped++;
263 + skb_reserve(skb, 2);
264 + if (memcpy_fromiovec(skb_put(skb, len), iv, len))
267 + skb->dev = tun->dev;
268 + switch (tun->flags & TUN_TYPE_MASK) {
270 + skb_reset_mac_header(skb);
271 + skb->protocol = pi.proto;
274 + skb->protocol = eth_type_trans(skb, tun->dev);
278 + if (tun->flags & TUN_NOCHECKSUM)
279 + skb->ip_summed = CHECKSUM_UNNECESSARY;
282 + set_skb_xid(skb, tun->owner);
286 + tun->stats.rx_packets++;
287 + tun->stats.rx_bytes += len;
288 + tun_stats.rx_packets++;
289 + tun_stats.rx_bytes += len;
294 +static inline size_t iov_total(const struct iovec *iv, unsigned long count)
299 + for (i = 0, len = 0; i < count; i++)
300 + len += iv[i].iov_len;
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)
309 + struct tun_struct *tun = file->private_data;
314 + dbg("%s:%d: tun_chr_write %ld\n", tun->dev->name, tun->owner, count);
316 + return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
320 +static ssize_t tun_chr_write(struct file * file, const char __user * buf,
321 + size_t count, loff_t *pos)
323 + struct iovec iv = { (void __user *) buf, count };
324 + return tun_chr_writev(file, &iv, 1, pos);
327 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
329 +static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
331 + stamp->tv_sec = skb->stamp.tv_sec;
332 + stamp->tv_usec = skb->stamp.tv_usec;
335 +static inline void skb_set_timestamp(struct sk_buff *skb, const struct timeval *stamp)
337 + skb->stamp.tv_sec = stamp->tv_sec;
338 + skb->stamp.tv_usec = stamp->tv_usec;
341 +static void __net_timestamp(struct sk_buff *skb)
345 + do_gettimeofday(&tv);
346 + skb_set_timestamp(skb, &tv);
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)
357 + struct tun_pi_ext pi_ext;
358 + struct timeval stamp;
361 + if (!(tun->flags & TUN_NO_PI)) {
362 + struct iovec iv1 = { iv->iov_base, iv->iov_len };
364 + if ((len -= sizeof(pi)) < 0)
367 + if (memcpy_fromiovec((void *)&pi, &iv1, sizeof(pi)))
370 + if (pi.flags & TUN_PKT_EXT) {
371 + if ((len -= sizeof(pi_ext)) < 0)
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);
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;
388 + pi.proto = skb->protocol;
391 + if (len < skb->len) {
392 + /* Packet will be striped */
393 + pi.flags |= TUN_PKT_STRIP;
396 + if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
398 + total += sizeof(pi);
400 + if (pi.flags & TUN_PKT_EXT) {
401 + if (memcpy_toiovec(iv, (void *) &pi_ext, sizeof(pi_ext)))
403 + total += sizeof(pi_ext);
407 + len = min_t(int, skb->len, len);
409 + skb_copy_datagram_iovec(skb, 0, iv, len);
412 + tun->stats.tx_packets++;
413 + tun->stats.tx_bytes += len;
414 + tun_stats.tx_packets++;
415 + tun_stats.tx_bytes += len;
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)
424 + struct tun_struct *tun = file->private_data;
425 + DECLARE_WAITQUEUE(wait, current);
426 + struct sk_buff *skb;
427 + ssize_t len, ret = 0;
432 + dbg("%s:%d: tun_chr_read\n", tun->dev->name, tun->owner);
434 + len = iov_total(iv, count);
438 + add_wait_queue(&tun->read_wait, &wait);
440 + current->state = TASK_INTERRUPTIBLE;
442 + /* Read frames from the queue */
443 + if (!(skb=skb_dequeue(&tun->readq))) {
444 + if (file->f_flags & O_NONBLOCK) {
448 + if (signal_pending(current)) {
449 + ret = -ERESTARTSYS;
453 + /* Nothing to read, let's sleep */
458 + ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
464 + current->state = TASK_RUNNING;
465 + remove_wait_queue(&tun->read_wait, &wait);
471 +static ssize_t tun_chr_read(struct file * file, char __user * buf,
472 + size_t count, loff_t *pos)
474 + struct iovec iv = { buf, count };
475 + return tun_chr_readv(file, &iv, 1, pos);
478 +static int tun_set_iff(struct file *file, struct ifreq *ifr)
480 + struct tun_struct *tun;
482 + tun = tun_get_by_xid(get_file_xid(file));
487 + /* Check permissions */
488 + if (tun->owner != -1 &&
489 + get_file_xid(file) != tun->owner && !capable(CAP_NET_ADMIN))
493 + /* Create a new queue */
494 + tun = kmalloc(sizeof(struct tun_struct), GFP_KERNEL);
497 + memset(tun, 0, sizeof(struct tun_struct));
499 + tun->dev = &tun_netdev;
501 + skb_queue_head_init(&tun->readq);
502 + init_waitqueue_head(&tun->read_wait);
504 + tun->owner = get_file_xid(file);
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);
511 + dbg("%s:%d: tun_set_iff\n", tun->dev->name, tun->owner);
513 + tun->flags = TUN_TAP_DEV;
515 + if (ifr->ifr_flags & IFF_NO_PI)
516 + tun->flags |= TUN_NO_PI;
518 + file->private_data = tun;
521 + strcpy(ifr->ifr_name, tun->dev->name);
525 +static int tun_chr_ioctl(struct inode *inode, struct file *file,
526 + unsigned int cmd, unsigned long arg)
528 + struct tun_struct *tun = file->private_data;
530 + if (cmd == TUNSETIFF && !tun) {
534 + if (copy_from_user(&ifr, (void __user *)arg, sizeof(ifr)))
536 + ifr.ifr_name[IFNAMSIZ-1] = '\0';
538 + err = tun_set_iff(file, &ifr);
543 + if (copy_to_user((void __user *)arg, &ifr, sizeof(ifr)))
551 + dbg("%s:%d: tun_chr_ioctl cmd %d\n", tun->dev->name, tun->owner, cmd);
555 + /* Disable/Enable checksum */
557 + tun->flags |= TUN_NOCHECKSUM;
559 + tun->flags &= ~TUN_NOCHECKSUM;
561 + dbg("%s:%d: checksum %s\n",
562 + tun->dev->name, tun->owner, arg ? "disabled" : "enabled");
565 + case TUNSETPERSIST:
568 + /* Not applicable */
578 +static int tun_chr_fasync(int fd, struct file *file, int on)
580 + struct tun_struct *tun = file->private_data;
586 + dbg("%s:%d: tun_chr_fasync %d\n", tun->dev->name, tun->owner, on);
588 + if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
592 + ret = f_setown(file, current->pid, 0);
595 + tun->flags |= TUN_FASYNC;
597 + tun->flags &= ~TUN_FASYNC;
602 +static int tun_chr_open(struct inode *inode, struct file * file)
604 + dbg("tunX: tun_chr_open\n");
605 + file->private_data = NULL;
609 +static int tun_chr_close(struct inode *inode, struct file *file)
611 + struct tun_struct *tun = file->private_data;
616 + dbg("%s:%d: tun_chr_close\n", tun->dev->name, tun->owner);
618 + tun_chr_fasync(-1, file, 0);
620 + /* Detach from net device */
621 + file->private_data = NULL;
624 + /* Drop read queue */
625 + skb_queue_purge(&tun->readq);
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
644 +static struct miscdevice tun_miscdev = {
645 + .minor = TUN_MINOR,
648 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
649 + .devfs_name = "net/tun",
653 +int __init tun_init(void)
656 + struct net_device *dev = &tun_netdev;
659 + /* Initialize hash table */
660 + for (i = 0; i < XIDHASH_SZ; i++)
661 + INIT_LIST_HEAD(&tun_dev_hash[i]);
663 + ret = misc_register(&tun_miscdev);
665 + err("tun: Can't register misc device %d\n", TUN_MINOR);
669 + memset(dev, 0, sizeof(struct net_device));
671 + /* Ethernet TAP Device */
672 + dev->set_multicast_list = tun_net_mclist;
674 + /* Generate random Ethernet address. */
675 + *(u16 *)dev->dev_addr = htons(0x00FF);
676 + get_random_bytes(dev->dev_addr + sizeof(u16), 4);
680 + dev->flags |= IFF_NOARP | IFF_POINTOPOINT;
681 + dev->flags &= ~IFF_MULTICAST;
683 + SET_MODULE_OWNER(dev);
684 + dev->hard_start_xmit = tun_net_xmit;
685 + dev->get_stats = tun_net_stats;
687 + strcpy(dev->name, "tap0");
689 + ret = register_netdev(dev);
691 + misc_deregister(&tun_miscdev);
696 +void __exit tun_cleanup(void)
698 + struct tun_struct *tun, *nxt;
701 + misc_deregister(&tun_miscdev);
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);
710 + write_unlock_bh(&tun_dev_hash_lock);
712 + unregister_netdev(&tun_netdev);