diff -Nurb linux-2.6.22-594/include/linux/vserver/network.h.orig.orig linux-2.6.22-595/include/linux/vserver/network.h.orig.orig --- linux-2.6.22-594/include/linux/vserver/network.h.orig.orig 2008-03-20 01:27:27.000000000 -0400 +++ linux-2.6.22-595/include/linux/vserver/network.h.orig.orig 1969-12-31 19:00:00.000000000 -0500 @@ -1,143 +0,0 @@ -#ifndef _VX_NETWORK_H -#define _VX_NETWORK_H - -#include - - -#define MAX_N_CONTEXT 65535 /* Arbitrary limit */ - - -/* network flags */ - -#define NXF_INFO_PRIVATE 0x00000008 - -#define NXF_SINGLE_IP 0x00000100 -#define NXF_LBACK_REMAP 0x00000200 - -#define NXF_HIDE_NETIF 0x02000000 -#define NXF_HIDE_LBACK 0x04000000 - -#define NXF_STATE_SETUP (1ULL << 32) -#define NXF_STATE_ADMIN (1ULL << 34) - -#define NXF_SC_HELPER (1ULL << 36) -#define NXF_PERSISTENT (1ULL << 38) - -#define NXF_ONE_TIME (0x0005ULL << 32) - - -#define NXF_INIT_SET (__nxf_init_set()) - -static inline uint64_t __nxf_init_set(void) { - return NXF_STATE_ADMIN -#ifdef CONFIG_VSERVER_AUTO_LBACK - | NXF_LBACK_REMAP - | NXF_HIDE_LBACK -#endif -#ifdef CONFIG_VSERVER_AUTO_SINGLE - | NXF_SINGLE_IP -#endif - | NXF_HIDE_NETIF; -} - - -/* network caps */ - -#define NXC_RAW_ICMP 0x00000100 - - -/* address types */ - -#define NXA_TYPE_IPV4 0x0001 -#define NXA_TYPE_IPV6 0x0002 - -#define NXA_TYPE_NONE 0x0000 -#define NXA_TYPE_ANY 0x00FF - -#define NXA_TYPE_ADDR 0x0010 -#define NXA_TYPE_MASK 0x0020 -#define NXA_TYPE_RANGE 0x0040 - -#define NXA_MASK_ALL (NXA_TYPE_ADDR | NXA_TYPE_MASK | NXA_TYPE_RANGE) - -#define NXA_MOD_BCAST 0x0100 -#define NXA_MOD_LBACK 0x0200 - -#define NXA_LOOPBACK 0x1000 - -#define NXA_MASK_BIND (NXA_MASK_ALL | NXA_MOD_BCAST | NXA_MOD_LBACK) -#define NXA_MASK_SHOW (NXA_MASK_ALL | NXA_LOOPBACK) - -#ifdef __KERNEL__ - -#include -#include -#include -#include -#include -#include - -struct nx_addr_v4 { - struct nx_addr_v4 *next; - struct in_addr ip[2]; - struct in_addr mask; - uint16_t type; - uint16_t flags; -}; - -struct nx_addr_v6 { - struct nx_addr_v6 *next; - struct in6_addr ip; - struct in6_addr mask; - uint32_t prefix; - uint16_t type; - uint16_t flags; -}; - -struct nx_info { - struct hlist_node nx_hlist; /* linked list of nxinfos */ - nid_t nx_id; /* vnet id */ - atomic_t nx_usecnt; /* usage count */ - atomic_t nx_tasks; /* tasks count */ - int nx_state; /* context state */ - - uint64_t nx_flags; /* network flag word */ - uint64_t nx_ncaps; /* network capabilities */ - - struct in_addr v4_lback; /* Loopback address */ - struct in_addr v4_bcast; /* Broadcast address */ - struct nx_addr_v4 v4; /* First/Single ipv4 address */ -#ifdef CONFIG_IPV6 - struct nx_addr_v6 v6; /* First/Single ipv6 address */ -#endif - char nx_name[65]; /* network context name */ -}; - - -/* status flags */ - -#define NXS_HASHED 0x0001 -#define NXS_SHUTDOWN 0x0100 -#define NXS_RELEASED 0x8000 - -extern struct nx_info *lookup_nx_info(int); - -extern int get_nid_list(int, unsigned int *, int); -extern int nid_is_hashed(nid_t); - -extern int nx_migrate_task(struct task_struct *, struct nx_info *); - -extern long vs_net_change(struct nx_info *, unsigned int); - -struct sock; - - -#define NX_IPV4(n) ((n)->v4.type != NXA_TYPE_NONE) -#ifdef CONFIG_IPV6 -#define NX_IPV6(n) ((n)->v6.type != NXA_TYPE_NONE) -#else -#define NX_IPV6(n) (0) -#endif - -#endif /* __KERNEL__ */ -#endif /* _VX_NETWORK_H */ diff -Nurb linux-2.6.22-594/kernel/nsproxy.c.orig linux-2.6.22-595/kernel/nsproxy.c.orig --- linux-2.6.22-594/kernel/nsproxy.c.orig 2008-03-20 01:27:50.000000000 -0400 +++ linux-2.6.22-595/kernel/nsproxy.c.orig 1969-12-31 19:00:00.000000000 -0500 @@ -1,264 +0,0 @@ -/* - * Copyright (C) 2006 IBM Corporation - * - * Author: Serge Hallyn - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, version 2 of the - * License. - * - * Jun 2006 - namespaces support - * OpenVZ, SWsoft Inc. - * Pavel Emelianov - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static struct kmem_cache *nsproxy_cachep; - -struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy); - -void get_task_namespaces(struct task_struct *tsk) -{ - struct nsproxy *ns = tsk->nsproxy; - if (ns) { - get_nsproxy(ns); - } -} - -/* - * creates a copy of "orig" with refcount 1. - */ -static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig) -{ - struct nsproxy *ns; - - ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL); - if (ns) - atomic_set(&ns->count, 1); - vxdprintk(VXD_CBIT(space, 2), "clone_nsproxy(%p[%u] = %p[1]", - orig, atomic_read(&orig->count), ns); - atomic_inc(&vs_global_nsproxy); - return ns; -} - -/* - * Create new nsproxy and all of its the associated namespaces. - * Return the newly created nsproxy. Do not attach this to the task, - * leave it to the caller to do proper locking and attach it to task. - */ -static struct nsproxy *unshare_namespaces(int flags, struct nsproxy *orig, - struct fs_struct *new_fs) -{ - struct nsproxy *new_nsp; - int err = -ENOMEM; - - vxdprintk(VXD_CBIT(space, 4), - "unshare_namespaces(0x%08x,%p,%p)", - flags, orig, new_fs); - - new_nsp = clone_nsproxy(orig); - if (!new_nsp) - return ERR_PTR(-ENOMEM); - - new_nsp->mnt_ns = copy_mnt_ns(flags, orig->mnt_ns, new_fs); - if (IS_ERR(new_nsp->mnt_ns)) - goto out_ns; - - new_nsp->uts_ns = copy_utsname(flags, orig->uts_ns); - if (IS_ERR(new_nsp->uts_ns)) - goto out_uts; - - new_nsp->ipc_ns = copy_ipcs(flags, orig->ipc_ns); - if (IS_ERR(new_nsp->ipc_ns)) - goto out_ipc; - - new_nsp->pid_ns = copy_pid_ns(flags, orig->pid_ns); - if (IS_ERR(new_nsp->pid_ns)) - goto out_pid; - - new_nsp->user_ns = copy_user_ns(flags, orig->user_ns); - if (IS_ERR(new_nsp->user_ns)) - goto out_user; - - new_nsp->net_ns = copy_net_ns(flags, orig->net_ns); - if (IS_ERR(new_nsp->net_ns)) - goto out_net; - - return new_nsp; - -out_net: - if (new_nsp->user_ns) - put_user_ns(new_nsp->user_ns); - if (new_nsp->net_ns) - put_net(new_nsp->net_ns); -out_user: - if (new_nsp->pid_ns) - put_pid_ns(new_nsp->pid_ns); -out_pid: - if (new_nsp->ipc_ns) - put_ipc_ns(new_nsp->ipc_ns); -out_ipc: - if (new_nsp->uts_ns) - put_uts_ns(new_nsp->uts_ns); -out_uts: - if (new_nsp->mnt_ns) - put_mnt_ns(new_nsp->mnt_ns); -out_ns: - kmem_cache_free(nsproxy_cachep, new_nsp); - return ERR_PTR(err); -} - -static struct nsproxy *create_new_namespaces(unsigned long flags, struct task_struct *tsk, - struct fs_struct *new_fs) -{ - return unshare_namespaces(flags, tsk->nsproxy, new_fs); -} - -/* - * copies the nsproxy, setting refcount to 1, and grabbing a - * reference to all contained namespaces. - */ -struct nsproxy *copy_nsproxy(struct nsproxy *orig) -{ - struct nsproxy *ns = clone_nsproxy(orig); - - if (ns) { - if (ns->mnt_ns) - get_mnt_ns(ns->mnt_ns); - if (ns->uts_ns) - get_uts_ns(ns->uts_ns); - if (ns->ipc_ns) - get_ipc_ns(ns->ipc_ns); - if (ns->pid_ns) - get_pid_ns(ns->pid_ns); - } - return ns; -} - -/* - * called from clone. This now handles copy for nsproxy and all - * namespaces therein. - */ -int copy_namespaces(unsigned long flags, struct task_struct *tsk) -{ - struct nsproxy *old_ns = tsk->nsproxy; - struct nsproxy *new_ns = NULL; - int err = 0; - - vxdprintk(VXD_CBIT(space, 7), "copy_namespaces(0x%08x,%p[%p])", - flags, tsk, old_ns); - - if (!old_ns) - return 0; - - get_nsproxy(old_ns); - return 0; - - if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWNET))) - return 0; - - #ifndef CONFIG_NET_NS - if (unshare_flags & CLONE_NEWNET) - return -EINVAL; - #endif - - - if (!capable(CAP_SYS_ADMIN)) { - err = -EPERM; - goto out; - } - - new_ns = create_new_namespaces(flags, tsk, tsk->fs); - if (IS_ERR(new_ns)) { - err = PTR_ERR(new_ns); - goto out; - } - - err = ns_container_clone(tsk); - if (err) { - put_nsproxy(new_ns); - goto out; - } - - tsk->nsproxy = new_ns; - -out: - put_nsproxy(old_ns); - vxdprintk(VXD_CBIT(space, 3), - "copy_namespaces(0x%08x,%p[%p]) = %d [%p]", - flags, tsk, old_ns, err, new_ns); - return err; -} - -void free_nsproxy(struct nsproxy *ns) -{ - if (ns->mnt_ns) - put_mnt_ns(ns->mnt_ns); - if (ns->uts_ns) - put_uts_ns(ns->uts_ns); - if (ns->ipc_ns) - put_ipc_ns(ns->ipc_ns); - if (ns->pid_ns) - put_pid_ns(ns->pid_ns); - atomic_dec(&vs_global_nsproxy); - kfree(ns); -} - -/* - * Called from unshare. Unshare all the namespaces part of nsproxy. - * On success, returns the new nsproxy. - */ -int unshare_nsproxy_namespaces(unsigned long unshare_flags, - struct nsproxy **new_nsp, struct fs_struct *new_fs) -{ - int err = 0; - - vxdprintk(VXD_CBIT(space, 4), - "unshare_nsproxy_namespaces(0x%08lx,[%p])", - unshare_flags, current->nsproxy); - - if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | - CLONE_NEWUSER | CLONE_NEWNET))) - return 0; - -#ifndef CONFIG_NET_NS - if (unshare_flags & CLONE_NEWNET) - return -EINVAL; -#endif - if (!capable(CAP_SYS_ADMIN)) - return -EPERM; - - *new_nsp = create_new_namespaces(unshare_flags, current, - new_fs ? new_fs : current->fs); - if (IS_ERR(*new_nsp)) { - err = PTR_ERR(*new_nsp); - goto out; - } - - err = ns_container_clone(current); - if (err) - put_nsproxy(*new_nsp); - -out: - return err; -} - -static int __init nsproxy_cache_init(void) -{ - nsproxy_cachep = kmem_cache_create("nsproxy", sizeof(struct nsproxy), - 0, SLAB_PANIC, NULL, NULL); - return 0; -} - -module_init(nsproxy_cache_init); diff -Nurb linux-2.6.22-594/kernel/user.c.orig linux-2.6.22-595/kernel/user.c.orig --- linux-2.6.22-594/kernel/user.c.orig 2008-03-20 01:27:50.000000000 -0400 +++ linux-2.6.22-595/kernel/user.c.orig 1969-12-31 19:00:00.000000000 -0500 @@ -1,227 +0,0 @@ -/* - * The "user cache". - * - * (C) Copyright 1991-2000 Linus Torvalds - * - * We have a per-user structure to keep track of how many - * processes, files etc the user has claimed, in order to be - * able to have per-user limits for system resources. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -/* - * UID task count cache, to get fast user lookup in "alloc_uid" - * when changing user ID's (ie setuid() and friends). - */ - -#define UIDHASH_MASK (UIDHASH_SZ - 1) -#define __uidhashfn(xid,uid) ((((uid) >> UIDHASH_BITS) + ((uid)^(xid))) & UIDHASH_MASK) -#define uidhashentry(ns, xid, uid) ((ns)->uidhash_table + __uidhashfn(xid, uid)) - -static struct kmem_cache *uid_cachep; -static struct list_head uidhash_table[UIDHASH_SZ]; - -/* - * The uidhash_lock is mostly taken from process context, but it is - * occasionally also taken from softirq/tasklet context, when - * task-structs get RCU-freed. Hence all locking must be softirq-safe. - * But free_uid() is also called with local interrupts disabled, and running - * local_bh_enable() with local interrupts disabled is an error - we'll run - * softirq callbacks, and they can unconditionally enable interrupts, and - * the caller of free_uid() didn't expect that.. - */ -static DEFINE_SPINLOCK(uidhash_lock); - -struct user_struct root_user = { - .__count = ATOMIC_INIT(1), - .processes = ATOMIC_INIT(1), - .files = ATOMIC_INIT(0), - .sigpending = ATOMIC_INIT(0), - .mq_bytes = 0, - .locked_shm = 0, -#ifdef CONFIG_KEYS - .uid_keyring = &root_user_keyring, - .session_keyring = &root_session_keyring, -#endif -}; - -/* - * These routines must be called with the uidhash spinlock held! - */ -static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent) -{ - list_add(&up->uidhash_list, hashent); -} - -static inline void uid_hash_remove(struct user_struct *up) -{ - list_del(&up->uidhash_list); -} - -static inline struct user_struct *uid_hash_find(xid_t xid, uid_t uid, struct list_head *hashent) -{ - struct list_head *up; - - list_for_each(up, hashent) { - struct user_struct *user; - - user = list_entry(up, struct user_struct, uidhash_list); - - if(user->uid == uid && user->xid == xid) { - atomic_inc(&user->__count); - return user; - } - } - - return NULL; -} - -/* - * Locate the user_struct for the passed UID. If found, take a ref on it. The - * caller must undo that ref with free_uid(). - * - * If the user_struct could not be found, return NULL. - */ -struct user_struct *find_user(xid_t xid, uid_t uid) -{ - struct user_struct *ret; - unsigned long flags; - struct user_namespace *ns = current->nsproxy->user_ns; - - spin_lock_irqsave(&uidhash_lock, flags); - ret = uid_hash_find(xid, uid, uidhashentry(ns, xid, uid)); - spin_unlock_irqrestore(&uidhash_lock, flags); - return ret; -} - -void free_uid(struct user_struct *up) -{ - unsigned long flags; - - if (!up) - return; - - local_irq_save(flags); - if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) { - uid_hash_remove(up); - spin_unlock_irqrestore(&uidhash_lock, flags); - key_put(up->uid_keyring); - key_put(up->session_keyring); - kmem_cache_free(uid_cachep, up); - } else { - local_irq_restore(flags); - } -} - -struct user_struct * alloc_uid(xid_t xid, uid_t uid) -{ - struct user_namespace *ns = current->nsproxy->user_ns; - struct list_head *hashent = uidhashentry(ns,xid, uid); - struct user_struct *up; - - spin_lock_irq(&uidhash_lock); - up = uid_hash_find(xid, uid, hashent); - spin_unlock_irq(&uidhash_lock); - - if (!up) { - struct user_struct *new; - - new = kmem_cache_alloc(uid_cachep, GFP_KERNEL); - if (!new) - return NULL; - new->uid = uid; - new->xid = xid; - atomic_set(&new->__count, 1); - atomic_set(&new->processes, 0); - atomic_set(&new->files, 0); - atomic_set(&new->sigpending, 0); -#ifdef CONFIG_INOTIFY_USER - atomic_set(&new->inotify_watches, 0); - atomic_set(&new->inotify_devs, 0); -#endif - - new->mq_bytes = 0; - new->locked_shm = 0; - - if (alloc_uid_keyring(new, current) < 0) { - kmem_cache_free(uid_cachep, new); - return NULL; - } - - /* - * Before adding this, check whether we raced - * on adding the same user already.. - */ - spin_lock_irq(&uidhash_lock); - up = uid_hash_find(xid, uid, hashent); - if (up) { - key_put(new->uid_keyring); - key_put(new->session_keyring); - kmem_cache_free(uid_cachep, new); - } else { - uid_hash_insert(new, hashent); - up = new; - } - spin_unlock_irq(&uidhash_lock); - - } - return up; -} - -void switch_uid(struct user_struct *new_user) -{ - struct user_struct *old_user; - - /* What if a process setreuid()'s and this brings the - * new uid over his NPROC rlimit? We can check this now - * cheaply with the new uid cache, so if it matters - * we should be checking for it. -DaveM - */ - old_user = current->user; - atomic_inc(&new_user->processes); - atomic_dec(&old_user->processes); - switch_uid_keyring(new_user); - current->user = new_user; - - /* - * We need to synchronize with __sigqueue_alloc() - * doing a get_uid(p->user).. If that saw the old - * user value, we need to wait until it has exited - * its critical region before we can free the old - * structure. - */ - smp_mb(); - spin_unlock_wait(¤t->sighand->siglock); - - free_uid(old_user); - suid_keys(current); -} - - -static int __init uid_cache_init(void) -{ - int n; - - uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct), - 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); - - for(n = 0; n < UIDHASH_SZ; ++n) - INIT_LIST_HEAD(init_user_ns.uidhash_table + n); - - /* Insert the root user immediately (init already runs as root) */ - spin_lock_irq(&uidhash_lock); - uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0, 0)); - spin_unlock_irq(&uidhash_lock); - - return 0; -} - -module_init(uid_cache_init); diff -Nurb linux-2.6.22-594/kernel/vserver/context.c linux-2.6.22-595/kernel/vserver/context.c --- linux-2.6.22-594/kernel/vserver/context.c 2008-03-20 01:27:21.000000000 -0400 +++ linux-2.6.22-595/kernel/vserver/context.c 2008-03-20 01:28:00.000000000 -0400 @@ -589,13 +589,13 @@ struct nsproxy *old_nsp, *new_nsp; ret = unshare_nsproxy_namespaces( - CLONE_NEWUTS | CLONE_NEWIPC, + CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWNET, &new_nsp, NULL); if (ret) goto out; old_nsp = xchg(&p->nsproxy, new_nsp); - vx_set_space(vxi, CLONE_NEWUTS | CLONE_NEWIPC); + vx_set_space(vxi, CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWNET); put_nsproxy(old_nsp); } } @@ -781,7 +781,7 @@ if (vs_state_change(new_vxi, VSC_STARTUP)) goto out; - ret = vx_migrate_task(current, new_vxi, (!data)); + ret = vx_migrate_task(current, new_vxi, 1 /*(!data) Hack no. 1 - Sapan*/); if (ret) goto out; diff -Nurb linux-2.6.22-594/kernel/vserver/space.c linux-2.6.22-595/kernel/vserver/space.c --- linux-2.6.22-594/kernel/vserver/space.c 2008-03-20 01:27:54.000000000 -0400 +++ linux-2.6.22-595/kernel/vserver/space.c 2008-03-20 01:28:00.000000000 -0400 @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -55,6 +56,7 @@ struct mnt_namespace *old_ns; struct uts_namespace *old_uts; struct ipc_namespace *old_ipc; + struct net *old_net; struct nsproxy *nsproxy; nsproxy = copy_nsproxy(old_nsproxy); @@ -85,12 +87,26 @@ } else old_ipc = NULL; + if (mask & CLONE_NEWNET) { + old_net = nsproxy->net_ns; + nsproxy->net_ns = new_nsproxy->net_ns; + if (nsproxy->net_ns) { + get_net(nsproxy->net_ns); + printk(KERN_ALERT "Cloning network namespace\n"); + } + } else + old_net = NULL; + + if (old_ns) put_mnt_ns(old_ns); if (old_uts) put_uts_ns(old_uts); if (old_ipc) put_ipc_ns(old_ipc); + if (old_net) + put_net(old_net); + out: return nsproxy; } @@ -251,6 +267,7 @@ int vc_enter_space(struct vx_info *vxi, void __user *data) { + /* Ask dhozac how to pass this flag from user space - Sapan*/ struct vcmd_space_mask vc_data = { .mask = 0 }; if (data && copy_from_user(&vc_data, data, sizeof(vc_data))) diff -Nurb linux-2.6.22-594/net/core/dev.c linux-2.6.22-595/net/core/dev.c --- linux-2.6.22-594/net/core/dev.c 2008-03-20 01:27:55.000000000 -0400 +++ linux-2.6.22-595/net/core/dev.c 2008-03-20 01:32:22.000000000 -0400 @@ -2207,7 +2207,7 @@ total = 0; for_each_netdev(net, dev) { - if (!nx_dev_visible(current->nx_info, dev)) + if (net==&init_net && !nx_dev_visible(current->nx_info, dev)) continue; for (i = 0; i < NPROTO; i++) { if (gifconf_list[i]) { @@ -2274,8 +2274,9 @@ static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev) { struct net_device_stats *stats = dev->get_stats(dev); + struct net *net = seq->private; - if (!nx_dev_visible(current->nx_info, dev)) + if (net==&init_net && !nx_dev_visible(current->nx_info, dev)) return; seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu " diff -Nurb linux-2.6.22-594/net/core/dev.c.orig linux-2.6.22-595/net/core/dev.c.orig --- linux-2.6.22-594/net/core/dev.c.orig 1969-12-31 19:00:00.000000000 -0500 +++ linux-2.6.22-595/net/core/dev.c.orig 2008-03-20 01:27:55.000000000 -0400 @@ -0,0 +1,4141 @@ +/* + * NET3 Protocol independent device support routines. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Derived from the non IP parts of dev.c 1.0.19 + * Authors: Ross Biro + * Fred N. van Kempen, + * Mark Evans, + * + * Additional Authors: + * Florian la Roche + * Alan Cox + * David Hinds + * Alexey Kuznetsov + * Adam Sulmicki + * Pekka Riikonen + * + * Changes: + * D.J. Barrow : Fixed bug where dev->refcnt gets set + * to 2 if register_netdev gets called + * before net_dev_init & also removed a + * few lines of code in the process. + * Alan Cox : device private ioctl copies fields back. + * Alan Cox : Transmit queue code does relevant + * stunts to keep the queue safe. + * Alan Cox : Fixed double lock. + * Alan Cox : Fixed promisc NULL pointer trap + * ???????? : Support the full private ioctl range + * Alan Cox : Moved ioctl permission check into + * drivers + * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI + * Alan Cox : 100 backlog just doesn't cut it when + * you start doing multicast video 8) + * Alan Cox : Rewrote net_bh and list manager. + * Alan Cox : Fix ETH_P_ALL echoback lengths. + * Alan Cox : Took out transmit every packet pass + * Saved a few bytes in the ioctl handler + * Alan Cox : Network driver sets packet type before + * calling netif_rx. Saves a function + * call a packet. + * Alan Cox : Hashed net_bh() + * Richard Kooijman: Timestamp fixes. + * Alan Cox : Wrong field in SIOCGIFDSTADDR + * Alan Cox : Device lock protection. + * Alan Cox : Fixed nasty side effect of device close + * changes. + * Rudi Cilibrasi : Pass the right thing to + * set_mac_address() + * Dave Miller : 32bit quantity for the device lock to + * make it work out on a Sparc. + * Bjorn Ekwall : Added KERNELD hack. + * Alan Cox : Cleaned up the backlog initialise. + * Craig Metz : SIOCGIFCONF fix if space for under + * 1 device. + * Thomas Bogendoerfer : Return ENODEV for dev_open, if there + * is no device open function. + * Andi Kleen : Fix error reporting for SIOCGIFCONF + * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF + * Cyrus Durgin : Cleaned for KMOD + * Adam Sulmicki : Bug Fix : Network Device Unload + * A network device unload needs to purge + * the backlog queue. + * Paul Rusty Russell : SIOCSIFNAME + * Pekka Riikonen : Netdev boot-time settings code + * Andrew Morton : Make unregister_netdevice wait + * indefinitely on dev->refcnt + * J Hadi Salim : - Backlog queue sampling + * - netif_rx() feedback + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * The list of packet types we will receive (as opposed to discard) + * and the routines to invoke. + * + * Why 16. Because with 16 the only overlap we get on a hash of the + * low nibble of the protocol value is RARP/SNAP/X.25. + * + * NOTE: That is no longer true with the addition of VLAN tags. Not + * sure which should go first, but I bet it won't make much + * difference if we are running VLANs. The good news is that + * this protocol won't be in the list unless compiled in, so + * the average user (w/out VLANs) will not be adversely affected. + * --BLG + * + * 0800 IP + * 8100 802.1Q VLAN + * 0001 802.3 + * 0002 AX.25 + * 0004 802.2 + * 8035 RARP + * 0005 SNAP + * 0805 X.25 + * 0806 ARP + * 8137 IPX + * 0009 Localtalk + * 86DD IPv6 + */ + +static DEFINE_SPINLOCK(ptype_lock); +static struct list_head ptype_base[16] __read_mostly; /* 16 way hashed list */ +static struct list_head ptype_all __read_mostly; /* Taps */ + +#ifdef CONFIG_NET_DMA +struct net_dma { + struct dma_client client; + spinlock_t lock; + cpumask_t channel_mask; + struct dma_chan *channels[NR_CPUS]; +}; + +static enum dma_state_client +netdev_dma_event(struct dma_client *client, struct dma_chan *chan, + enum dma_state state); + +static struct net_dma net_dma = { + .client = { + .event_callback = netdev_dma_event, + }, +}; +#endif + +/* + * The @dev_base_head list is protected by @dev_base_lock and the rtnl + * semaphore. + * + * Pure readers hold dev_base_lock for reading. + * + * Writers must hold the rtnl semaphore while they loop through the + * dev_base_head list, and hold dev_base_lock for writing when they do the + * actual updates. This allows pure readers to access the list even + * while a writer is preparing to update it. + * + * To put it another way, dev_base_lock is held for writing only to + * protect against pure readers; the rtnl semaphore provides the + * protection against other writers. + * + * See, for example usages, register_netdevice() and + * unregister_netdevice(), which must be called with the rtnl + * semaphore held. + */ +DEFINE_RWLOCK(dev_base_lock); + +EXPORT_SYMBOL(dev_base_lock); + +#define NETDEV_HASHBITS 8 +#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) + +static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) +{ + unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ)); + return &net->dev_name_head[hash & ((1 << NETDEV_HASHBITS) - 1)]; +} + +static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) +{ + return &net->dev_index_head[ifindex & ((1 << NETDEV_HASHBITS) - 1)]; +} + +/* Device list insertion */ +static int list_netdevice(struct net_device *dev) +{ + struct net *net = dev->nd_net; + + ASSERT_RTNL(); + + write_lock_bh(&dev_base_lock); + list_add_tail(&dev->dev_list, &net->dev_base_head); + hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name)); + hlist_add_head(&dev->index_hlist, dev_index_hash(net, dev->ifindex)); + write_unlock_bh(&dev_base_lock); + return 0; +} + +/* Device list removal */ +static void unlist_netdevice(struct net_device *dev) +{ + ASSERT_RTNL(); + + /* Unlink dev from the device chain */ + write_lock_bh(&dev_base_lock); + list_del(&dev->dev_list); + hlist_del(&dev->name_hlist); + hlist_del(&dev->index_hlist); + write_unlock_bh(&dev_base_lock); +} + +/* + * Our notifier list + */ + +static RAW_NOTIFIER_HEAD(netdev_chain); + +/* + * Device drivers call our routines to queue packets here. We empty the + * queue in the local softnet handler. + */ +DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL }; + +#ifdef CONFIG_SYSFS +extern int netdev_sysfs_init(void); +extern int netdev_register_sysfs(struct net_device *); +extern void netdev_unregister_sysfs(struct net_device *); +#else +#define netdev_sysfs_init() (0) +#define netdev_register_sysfs(dev) (0) +#define netdev_unregister_sysfs(dev) do { } while(0) +#endif + +#ifdef CONFIG_DEBUG_LOCK_ALLOC +/* + * register_netdevice() inits dev->_xmit_lock and sets lockdep class + * according to dev->type + */ +static const unsigned short netdev_lock_type[] = + {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, + ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, + ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, + ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, + ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, + ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, + ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, + ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, + ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, + ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, + ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, + ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, + ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211, + ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID, + ARPHRD_NONE}; + +static const char *netdev_lock_name[] = + {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", + "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", + "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", + "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", + "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", + "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", + "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", + "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", + "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", + "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", + "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", + "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", + "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211", + "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID", + "_xmit_NONE"}; + +static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; + +static inline unsigned short netdev_lock_pos(unsigned short dev_type) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) + if (netdev_lock_type[i] == dev_type) + return i; + /* the last key is used by default */ + return ARRAY_SIZE(netdev_lock_type) - 1; +} + +static inline void netdev_set_lockdep_class(spinlock_t *lock, + unsigned short dev_type) +{ + int i; + + i = netdev_lock_pos(dev_type); + lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], + netdev_lock_name[i]); +} +#else +static inline void netdev_set_lockdep_class(spinlock_t *lock, + unsigned short dev_type) +{ +} +#endif + +/******************************************************************************* + + Protocol management and registration routines + +*******************************************************************************/ + +/* + * Add a protocol ID to the list. Now that the input handler is + * smarter we can dispense with all the messy stuff that used to be + * here. + * + * BEWARE!!! Protocol handlers, mangling input packets, + * MUST BE last in hash buckets and checking protocol handlers + * MUST start from promiscuous ptype_all chain in net_bh. + * It is true now, do not change it. + * Explanation follows: if protocol handler, mangling packet, will + * be the first on list, it is not able to sense, that packet + * is cloned and should be copied-on-write, so that it will + * change it and subsequent readers will get broken packet. + * --ANK (980803) + */ + +/** + * dev_add_pack - add packet handler + * @pt: packet type declaration + * + * Add a protocol handler to the networking stack. The passed &packet_type + * is linked into kernel lists and may not be freed until it has been + * removed from the kernel lists. + * + * This call does not sleep therefore it can not + * guarantee all CPU's that are in middle of receiving packets + * will see the new packet type (until the next received packet). + */ + +void dev_add_pack(struct packet_type *pt) +{ + int hash; + + spin_lock_bh(&ptype_lock); + if (pt->type == htons(ETH_P_ALL)) + list_add_rcu(&pt->list, &ptype_all); + else { + hash = ntohs(pt->type) & 15; + list_add_rcu(&pt->list, &ptype_base[hash]); + } + spin_unlock_bh(&ptype_lock); +} + +/** + * __dev_remove_pack - remove packet handler + * @pt: packet type declaration + * + * Remove a protocol handler that was previously added to the kernel + * protocol handlers by dev_add_pack(). The passed &packet_type is removed + * from the kernel lists and can be freed or reused once this function + * returns. + * + * The packet type might still be in use by receivers + * and must not be freed until after all the CPU's have gone + * through a quiescent state. + */ +void __dev_remove_pack(struct packet_type *pt) +{ + struct list_head *head; + struct packet_type *pt1; + + spin_lock_bh(&ptype_lock); + + if (pt->type == htons(ETH_P_ALL)) + head = &ptype_all; + else + head = &ptype_base[ntohs(pt->type) & 15]; + + list_for_each_entry(pt1, head, list) { + if (pt == pt1) { + list_del_rcu(&pt->list); + goto out; + } + } + + printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt); +out: + spin_unlock_bh(&ptype_lock); +} +/** + * dev_remove_pack - remove packet handler + * @pt: packet type declaration + * + * Remove a protocol handler that was previously added to the kernel + * protocol handlers by dev_add_pack(). The passed &packet_type is removed + * from the kernel lists and can be freed or reused once this function + * returns. + * + * This call sleeps to guarantee that no CPU is looking at the packet + * type after return. + */ +void dev_remove_pack(struct packet_type *pt) +{ + __dev_remove_pack(pt); + + synchronize_net(); +} + +/****************************************************************************** + + Device Boot-time Settings Routines + +*******************************************************************************/ + +/* Boot time configuration table */ +static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; + +/** + * netdev_boot_setup_add - add new setup entry + * @name: name of the device + * @map: configured settings for the device + * + * Adds new setup entry to the dev_boot_setup list. The function + * returns 0 on error and 1 on success. This is a generic routine to + * all netdevices. + */ +static int netdev_boot_setup_add(char *name, struct ifmap *map) +{ + struct netdev_boot_setup *s; + int i; + + s = dev_boot_setup; + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { + if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { + memset(s[i].name, 0, sizeof(s[i].name)); + strcpy(s[i].name, name); + memcpy(&s[i].map, map, sizeof(s[i].map)); + break; + } + } + + return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; +} + +/** + * netdev_boot_setup_check - check boot time settings + * @dev: the netdevice + * + * Check boot time settings for the device. + * The found settings are set for the device to be used + * later in the device probing. + * Returns 0 if no settings found, 1 if they are. + */ +int netdev_boot_setup_check(struct net_device *dev) +{ + struct netdev_boot_setup *s = dev_boot_setup; + int i; + + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { + if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && + !strncmp(dev->name, s[i].name, strlen(s[i].name))) { + dev->irq = s[i].map.irq; + dev->base_addr = s[i].map.base_addr; + dev->mem_start = s[i].map.mem_start; + dev->mem_end = s[i].map.mem_end; + return 1; + } + } + return 0; +} + + +/** + * netdev_boot_base - get address from boot time settings + * @prefix: prefix for network device + * @unit: id for network device + * + * Check boot time settings for the base address of device. + * The found settings are set for the device to be used + * later in the device probing. + * Returns 0 if no settings found. + */ +unsigned long netdev_boot_base(const char *prefix, int unit) +{ + const struct netdev_boot_setup *s = dev_boot_setup; + char name[IFNAMSIZ]; + int i; + + sprintf(name, "%s%d", prefix, unit); + + /* + * If device already registered then return base of 1 + * to indicate not to probe for this interface + */ + if (__dev_get_by_name(&init_net, name)) + return 1; + + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) + if (!strcmp(name, s[i].name)) + return s[i].map.base_addr; + return 0; +} + +/* + * Saves at boot time configured settings for any netdevice. + */ +int __init netdev_boot_setup(char *str) +{ + int ints[5]; + struct ifmap map; + + str = get_options(str, ARRAY_SIZE(ints), ints); + if (!str || !*str) + return 0; + + /* Save settings */ + memset(&map, 0, sizeof(map)); + if (ints[0] > 0) + map.irq = ints[1]; + if (ints[0] > 1) + map.base_addr = ints[2]; + if (ints[0] > 2) + map.mem_start = ints[3]; + if (ints[0] > 3) + map.mem_end = ints[4]; + + /* Add new entry to the list */ + return netdev_boot_setup_add(str, &map); +} + +__setup("netdev=", netdev_boot_setup); + +/******************************************************************************* + + Device Interface Subroutines + +*******************************************************************************/ + +/** + * __dev_get_by_name - find a device by its name + * @name: name to find + * + * Find an interface by name. Must be called under RTNL semaphore + * or @dev_base_lock. If the name is found a pointer to the device + * is returned. If the name is not found then %NULL is returned. The + * reference counters are not incremented so the caller must be + * careful with locks. + */ + +struct net_device *__dev_get_by_name(struct net *net, const char *name) +{ + struct hlist_node *p; + + hlist_for_each(p, dev_name_hash(net, name)) { + struct net_device *dev + = hlist_entry(p, struct net_device, name_hlist); + if (!strncmp(dev->name, name, IFNAMSIZ)) + return dev; + } + return NULL; +} + +/** + * dev_get_by_name - find a device by its name + * @name: name to find + * + * Find an interface by name. This can be called from any + * context and does its own locking. The returned handle has + * the usage count incremented and the caller must use dev_put() to + * release it when it is no longer needed. %NULL is returned if no + * matching device is found. + */ + +struct net_device *dev_get_by_name(struct net *net, const char *name) +{ + struct net_device *dev; + + read_lock(&dev_base_lock); + dev = __dev_get_by_name(net, name); + if (dev) + dev_hold(dev); + read_unlock(&dev_base_lock); + return dev; +} + +/** + * __dev_get_by_index - find a device by its ifindex + * @ifindex: index of device + * + * Search for an interface by index. Returns %NULL if the device + * is not found or a pointer to the device. The device has not + * had its reference counter increased so the caller must be careful + * about locking. The caller must hold either the RTNL semaphore + * or @dev_base_lock. + */ + +struct net_device *__dev_get_by_index(struct net *net, int ifindex) +{ + struct hlist_node *p; + + hlist_for_each(p, dev_index_hash(net, ifindex)) { + struct net_device *dev + = hlist_entry(p, struct net_device, index_hlist); + if (dev->ifindex == ifindex) + return dev; + } + return NULL; +} + + +/** + * dev_get_by_index - find a device by its ifindex + * @ifindex: index of device + * + * Search for an interface by index. Returns NULL if the device + * is not found or a pointer to the device. The device returned has + * had a reference added and the pointer is safe until the user calls + * dev_put to indicate they have finished with it. + */ + +struct net_device *dev_get_by_index(struct net *net, int ifindex) +{ + struct net_device *dev; + + read_lock(&dev_base_lock); + dev = __dev_get_by_index(net, ifindex); + if (dev) + dev_hold(dev); + read_unlock(&dev_base_lock); + return dev; +} + +/** + * dev_getbyhwaddr - find a device by its hardware address + * @type: media type of device + * @ha: hardware address + * + * Search for an interface by MAC address. Returns NULL if the device + * is not found or a pointer to the device. The caller must hold the + * rtnl semaphore. The returned device has not had its ref count increased + * and the caller must therefore be careful about locking + * + * BUGS: + * If the API was consistent this would be __dev_get_by_hwaddr + */ + +struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha) +{ + struct net_device *dev; + + ASSERT_RTNL(); + + for_each_netdev(&init_net, dev) + if (dev->type == type && + !memcmp(dev->dev_addr, ha, dev->addr_len)) + return dev; + + return NULL; +} + +EXPORT_SYMBOL(dev_getbyhwaddr); + +struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) +{ + struct net_device *dev; + + ASSERT_RTNL(); + for_each_netdev(net, dev) + if (dev->type == type) + return dev; + + return NULL; +} + +EXPORT_SYMBOL(__dev_getfirstbyhwtype); + +struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) +{ + struct net_device *dev; + + rtnl_lock(); + dev = __dev_getfirstbyhwtype(net, type); + if (dev) + dev_hold(dev); + rtnl_unlock(); + return dev; +} + +EXPORT_SYMBOL(dev_getfirstbyhwtype); + +/** + * dev_get_by_flags - find any device with given flags + * @if_flags: IFF_* values + * @mask: bitmask of bits in if_flags to check + * + * Search for any interface with the given flags. Returns NULL if a device + * is not found or a pointer to the device. The device returned has + * had a reference added and the pointer is safe until the user calls + * dev_put to indicate they have finished with it. + */ + +struct net_device * dev_get_by_flags(struct net *net, unsigned short if_flags, unsigned short mask) +{ + struct net_device *dev, *ret; + + ret = NULL; + read_lock(&dev_base_lock); + for_each_netdev(net, dev) { + if (((dev->flags ^ if_flags) & mask) == 0) { + dev_hold(dev); + ret = dev; + break; + } + } + read_unlock(&dev_base_lock); + return ret; +} + +/** + * dev_valid_name - check if name is okay for network device + * @name: name string + * + * Network device names need to be valid file names to + * to allow sysfs to work. We also disallow any kind of + * whitespace. + */ +int dev_valid_name(const char *name) +{ + if (*name == '\0') + return 0; + if (strlen(name) >= IFNAMSIZ) + return 0; + if (!strcmp(name, ".") || !strcmp(name, "..")) + return 0; + + while (*name) { + if (*name == '/' || isspace(*name)) + return 0; + name++; + } + return 1; +} + +/** + * __dev_alloc_name - allocate a name for a device + * @net: network namespace to allocate the device name in + * @name: name format string + * @buf: scratch buffer and result name string + * + * Passed a format string - eg "lt%d" it will try and find a suitable + * id. It scans list of devices to build up a free map, then chooses + * the first empty slot. The caller must hold the dev_base or rtnl lock + * while allocating the name and adding the device in order to avoid + * duplicates. + * Limited to bits_per_byte * page size devices (ie 32K on most platforms). + * Returns the number of the unit assigned or a negative errno code. + */ + +static int __dev_alloc_name(struct net *net, const char *name, char *buf) +{ + int i = 0; + const char *p; + const int max_netdevices = 8*PAGE_SIZE; + long *inuse; + struct net_device *d; + + p = strnchr(name, IFNAMSIZ-1, '%'); + if (p) { + /* + * Verify the string as this thing may have come from + * the user. There must be either one "%d" and no other "%" + * characters. + */ + if (p[1] != 'd' || strchr(p + 2, '%')) + return -EINVAL; + + /* Use one page as a bit array of possible slots */ + inuse = (long *) get_zeroed_page(GFP_ATOMIC); + if (!inuse) + return -ENOMEM; + + for_each_netdev(net, d) { + if (!sscanf(d->name, name, &i)) + continue; + if (i < 0 || i >= max_netdevices) + continue; + + /* avoid cases where sscanf is not exact inverse of printf */ + snprintf(buf, IFNAMSIZ, name, i); + if (!strncmp(buf, d->name, IFNAMSIZ)) + set_bit(i, inuse); + } + + i = find_first_zero_bit(inuse, max_netdevices); + free_page((unsigned long) inuse); + } + + snprintf(buf, IFNAMSIZ, name, i); + if (!__dev_get_by_name(net, buf)) + return i; + + /* It is possible to run out of possible slots + * when the name is long and there isn't enough space left + * for the digits, or if all bits are used. + */ + return -ENFILE; +} + +/** + * dev_alloc_name - allocate a name for a device + * @dev: device + * @name: name format string + * + * Passed a format string - eg "lt%d" it will try and find a suitable + * id. It scans list of devices to build up a free map, then chooses + * the first empty slot. The caller must hold the dev_base or rtnl lock + * while allocating the name and adding the device in order to avoid + * duplicates. + * Limited to bits_per_byte * page size devices (ie 32K on most platforms). + * Returns the number of the unit assigned or a negative errno code. + */ + +int dev_alloc_name(struct net_device *dev, const char *name) +{ + char buf[IFNAMSIZ]; + struct net *net; + int ret; + + BUG_ON(!dev->nd_net); + net = dev->nd_net; + ret = __dev_alloc_name(net, name, buf); + if (ret >= 0) + strlcpy(dev->name, buf, IFNAMSIZ); + return ret; +} + + +/** + * dev_change_name - change name of a device + * @dev: device + * @newname: name (or format string) must be at least IFNAMSIZ + * + * Change name of a device, can pass format strings "eth%d". + * for wildcarding. + */ +int dev_change_name(struct net_device *dev, char *newname) +{ + int err = 0; + struct net *net; + + ASSERT_RTNL(); + BUG_ON(!dev->nd_net); + + net = dev->nd_net; + if (dev->flags & IFF_UP) + return -EBUSY; + + if (!dev_valid_name(newname)) + return -EINVAL; + + if (strchr(newname, '%')) { + err = dev_alloc_name(dev, newname); + if (err < 0) + return err; + strcpy(newname, dev->name); + } + else if (__dev_get_by_name(net, newname)) + return -EEXIST; + else { + if (strncmp(newname, dev->name, IFNAMSIZ)) + printk(KERN_INFO "%s renamed to %s\n", + dev->name, newname); + strlcpy(dev->name, newname, IFNAMSIZ); + } + + device_rename(&dev->dev, dev->name); + hlist_del(&dev->name_hlist); + hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name)); + raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev); + + return err; +} + +/** + * netdev_features_change - device changes features + * @dev: device to cause notification + * + * Called to indicate a device has changed features. + */ +void netdev_features_change(struct net_device *dev) +{ + raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev); +} +EXPORT_SYMBOL(netdev_features_change); + +/** + * netdev_state_change - device changes state + * @dev: device to cause notification + * + * Called to indicate a device has changed state. This function calls + * the notifier chains for netdev_chain and sends a NEWLINK message + * to the routing socket. + */ +void netdev_state_change(struct net_device *dev) +{ + if (dev->flags & IFF_UP) { + raw_notifier_call_chain(&netdev_chain, + NETDEV_CHANGE, dev); + rtmsg_ifinfo(RTM_NEWLINK, dev, 0); + } +} + +/** + * dev_load - load a network module + * @name: name of interface + * + * If a network interface is not present and the process has suitable + * privileges this function loads the module. If module loading is not + * available in this kernel then it becomes a nop. + */ + +void dev_load(struct net *net, const char *name) +{ + struct net_device *dev; + + read_lock(&dev_base_lock); + dev = __dev_get_by_name(net, name); + read_unlock(&dev_base_lock); + + if (!dev && capable(CAP_SYS_MODULE)) + request_module("%s", name); +} + +static int default_rebuild_header(struct sk_buff *skb) +{ + printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n", + skb->dev ? skb->dev->name : "NULL!!!"); + kfree_skb(skb); + return 1; +} + +/** + * dev_open - prepare an interface for use. + * @dev: device to open + * + * Takes a device from down to up state. The device's private open + * function is invoked and then the multicast lists are loaded. Finally + * the device is moved into the up state and a %NETDEV_UP message is + * sent to the netdev notifier chain. + * + * Calling this function on an active interface is a nop. On a failure + * a negative errno code is returned. + */ +int dev_open(struct net_device *dev) +{ + int ret = 0; + + /* + * Is it already up? + */ + + if (dev->flags & IFF_UP) + return 0; + + /* + * Is it even present? + */ + if (!netif_device_present(dev)) + return -ENODEV; + + /* + * Call device private open method + */ + set_bit(__LINK_STATE_START, &dev->state); + if (dev->open) { + ret = dev->open(dev); + if (ret) + clear_bit(__LINK_STATE_START, &dev->state); + } + + /* + * If it went open OK then: + */ + + if (!ret) { + /* + * Set the flags. + */ + dev->flags |= IFF_UP; + + /* + * Initialize multicasting status + */ + dev_mc_upload(dev); + + /* + * Wakeup transmit queue engine + */ + dev_activate(dev); + + /* + * ... and announce new interface. + */ + raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev); + } + return ret; +} + +/** + * dev_close - shutdown an interface. + * @dev: device to shutdown + * + * This function moves an active device into down state. A + * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device + * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier + * chain. + */ +int dev_close(struct net_device *dev) +{ + if (!(dev->flags & IFF_UP)) + return 0; + + /* + * Tell people we are going down, so that they can + * prepare to death, when device is still operating. + */ + raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev); + + dev_deactivate(dev); + + clear_bit(__LINK_STATE_START, &dev->state); + + /* Synchronize to scheduled poll. We cannot touch poll list, + * it can be even on different cpu. So just clear netif_running(), + * and wait when poll really will happen. Actually, the best place + * for this is inside dev->stop() after device stopped its irq + * engine, but this requires more changes in devices. */ + + smp_mb__after_clear_bit(); /* Commit netif_running(). */ + while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) { + /* No hurry. */ + msleep(1); + } + + /* + * Call the device specific close. This cannot fail. + * Only if device is UP + * + * We allow it to be called even after a DETACH hot-plug + * event. + */ + if (dev->stop) + dev->stop(dev); + + /* + * Device is now down. + */ + + dev->flags &= ~IFF_UP; + + /* + * Tell people we are down + */ + raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev); + + return 0; +} + + +static int dev_boot_phase = 1; + +/* + * Device change register/unregister. These are not inline or static + * as we export them to the world. + */ + +/** + * register_netdevice_notifier - register a network notifier block + * @nb: notifier + * + * Register a notifier to be called when network device events occur. + * The notifier passed is linked into the kernel structures and must + * not be reused until it has been unregistered. A negative errno code + * is returned on a failure. + * + * When registered all registration and up events are replayed + * to the new notifier to allow device to have a race free + * view of the network device list. + */ + +int register_netdevice_notifier(struct notifier_block *nb) +{ + struct net_device *dev; + int err; + + rtnl_lock(); + err = raw_notifier_chain_register(&netdev_chain, nb); + if (!err && !dev_boot_phase) { + struct net *net; + for_each_net(net) { + for_each_netdev(net, dev) { + nb->notifier_call(nb, NETDEV_REGISTER, dev); + + if (dev->flags & IFF_UP) + nb->notifier_call(nb, NETDEV_UP, dev); + } + } + } + rtnl_unlock(); + return err; +} + +/** + * unregister_netdevice_notifier - unregister a network notifier block + * @nb: notifier + * + * Unregister a notifier previously registered by + * register_netdevice_notifier(). The notifier is unlinked into the + * kernel structures and may then be reused. A negative errno code + * is returned on a failure. + */ + +int unregister_netdevice_notifier(struct notifier_block *nb) +{ + int err; + + rtnl_lock(); + err = raw_notifier_chain_unregister(&netdev_chain, nb); + rtnl_unlock(); + return err; +} + +/** + * call_netdevice_notifiers - call all network notifier blocks + * @val: value passed unmodified to notifier function + * @v: pointer passed unmodified to notifier function + * + * Call all network notifier blocks. Parameters and return value + * are as for raw_notifier_call_chain(). + */ + +int call_netdevice_notifiers(unsigned long val, struct net_device *dev) +{ + return raw_notifier_call_chain(&netdev_chain, val, dev); +} + +/* When > 0 there are consumers of rx skb time stamps */ +static atomic_t netstamp_needed = ATOMIC_INIT(0); + +void net_enable_timestamp(void) +{ + atomic_inc(&netstamp_needed); +} + +void net_disable_timestamp(void) +{ + atomic_dec(&netstamp_needed); +} + +static inline void net_timestamp(struct sk_buff *skb) +{ + if (atomic_read(&netstamp_needed)) + __net_timestamp(skb); + else + skb->tstamp.tv64 = 0; +} + +/* + * Support routine. Sends outgoing frames to any network + * taps currently in use. + */ + +static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) +{ + struct packet_type *ptype; + + net_timestamp(skb); + + rcu_read_lock(); + list_for_each_entry_rcu(ptype, &ptype_all, list) { + /* Never send packets back to the socket + * they originated from - MvS (miquels@drinkel.ow.org) + */ + if ((ptype->dev == dev || !ptype->dev) && + (ptype->af_packet_priv == NULL || + (struct sock *)ptype->af_packet_priv != skb->sk)) { + struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC); + if (!skb2) + break; + + /* skb->nh should be correctly + set by sender, so that the second statement is + just protection against buggy protocols. + */ + skb_reset_mac_header(skb2); + + if (skb_network_header(skb2) < skb2->data || + skb2->network_header > skb2->tail) { + if (net_ratelimit()) + printk(KERN_CRIT "protocol %04x is " + "buggy, dev %s\n", + skb2->protocol, dev->name); + skb_reset_network_header(skb2); + } + + skb2->transport_header = skb2->network_header; + skb2->pkt_type = PACKET_OUTGOING; + ptype->func(skb2, skb->dev, ptype, skb->dev); + } + } + rcu_read_unlock(); +} + + +void __netif_schedule(struct net_device *dev) +{ + if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) { + unsigned long flags; + struct softnet_data *sd; + + local_irq_save(flags); + sd = &__get_cpu_var(softnet_data); + dev->next_sched = sd->output_queue; + sd->output_queue = dev; + raise_softirq_irqoff(NET_TX_SOFTIRQ); + local_irq_restore(flags); + } +} +EXPORT_SYMBOL(__netif_schedule); + +void __netif_rx_schedule(struct net_device *dev) +{ + unsigned long flags; + + local_irq_save(flags); + dev_hold(dev); + list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list); + if (dev->quota < 0) + dev->quota += dev->weight; + else + dev->quota = dev->weight; + __raise_softirq_irqoff(NET_RX_SOFTIRQ); + local_irq_restore(flags); +} +EXPORT_SYMBOL(__netif_rx_schedule); + +void dev_kfree_skb_any(struct sk_buff *skb) +{ + if (in_irq() || irqs_disabled()) + dev_kfree_skb_irq(skb); + else + dev_kfree_skb(skb); +} +EXPORT_SYMBOL(dev_kfree_skb_any); + + +/* Hot-plugging. */ +void netif_device_detach(struct net_device *dev) +{ + if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && + netif_running(dev)) { + netif_stop_queue(dev); + } +} +EXPORT_SYMBOL(netif_device_detach); + +void netif_device_attach(struct net_device *dev) +{ + if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && + netif_running(dev)) { + netif_wake_queue(dev); + __netdev_watchdog_up(dev); + } +} +EXPORT_SYMBOL(netif_device_attach); + + +/* + * Invalidate hardware checksum when packet is to be mangled, and + * complete checksum manually on outgoing path. + */ +int skb_checksum_help(struct sk_buff *skb) +{ + __wsum csum; + int ret = 0, offset; + + if (skb->ip_summed == CHECKSUM_COMPLETE) + goto out_set_summed; + + if (unlikely(skb_shinfo(skb)->gso_size)) { + /* Let GSO fix up the checksum. */ + goto out_set_summed; + } + + if (skb_cloned(skb)) { + ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); + if (ret) + goto out; + } + + offset = skb->csum_start - skb_headroom(skb); + BUG_ON(offset > (int)skb->len); + csum = skb_checksum(skb, offset, skb->len-offset, 0); + + offset = skb_headlen(skb) - offset; + BUG_ON(offset <= 0); + BUG_ON(skb->csum_offset + 2 > offset); + + *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = + csum_fold(csum); +out_set_summed: + skb->ip_summed = CHECKSUM_NONE; +out: + return ret; +} + +/** + * skb_gso_segment - Perform segmentation on skb. + * @skb: buffer to segment + * @features: features for the output path (see dev->features) + * + * This function segments the given skb and returns a list of segments. + * + * It may return NULL if the skb requires no segmentation. This is + * only possible when GSO is used for verifying header integrity. + */ +struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features) +{ + struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); + struct packet_type *ptype; + __be16 type = skb->protocol; + int err; + + BUG_ON(skb_shinfo(skb)->frag_list); + + skb_reset_mac_header(skb); + skb->mac_len = skb->network_header - skb->mac_header; + __skb_pull(skb, skb->mac_len); + + if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) { + if (skb_header_cloned(skb) && + (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) + return ERR_PTR(err); + } + + rcu_read_lock(); + list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) { + if (ptype->type == type && !ptype->dev && ptype->gso_segment) { + if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) { + err = ptype->gso_send_check(skb); + segs = ERR_PTR(err); + if (err || skb_gso_ok(skb, features)) + break; + __skb_push(skb, (skb->data - + skb_network_header(skb))); + } + segs = ptype->gso_segment(skb, features); + break; + } + } + rcu_read_unlock(); + + __skb_push(skb, skb->data - skb_mac_header(skb)); + + return segs; +} + +EXPORT_SYMBOL(skb_gso_segment); + +/* Take action when hardware reception checksum errors are detected. */ +#ifdef CONFIG_BUG +void netdev_rx_csum_fault(struct net_device *dev) +{ + if (net_ratelimit()) { + printk(KERN_ERR "%s: hw csum failure.\n", + dev ? dev->name : ""); + dump_stack(); + } +} +EXPORT_SYMBOL(netdev_rx_csum_fault); +#endif + +/* Actually, we should eliminate this check as soon as we know, that: + * 1. IOMMU is present and allows to map all the memory. + * 2. No high memory really exists on this machine. + */ + +static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb) +{ +#ifdef CONFIG_HIGHMEM + int i; + + if (dev->features & NETIF_F_HIGHDMA) + return 0; + + for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) + if (PageHighMem(skb_shinfo(skb)->frags[i].page)) + return 1; + +#endif + return 0; +} + +struct dev_gso_cb { + void (*destructor)(struct sk_buff *skb); +}; + +#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb) + +static void dev_gso_skb_destructor(struct sk_buff *skb) +{ + struct dev_gso_cb *cb; + + do { + struct sk_buff *nskb = skb->next; + + skb->next = nskb->next; + nskb->next = NULL; + kfree_skb(nskb); + } while (skb->next); + + cb = DEV_GSO_CB(skb); + if (cb->destructor) + cb->destructor(skb); +} + +/** + * dev_gso_segment - Perform emulated hardware segmentation on skb. + * @skb: buffer to segment + * + * This function segments the given skb and stores the list of segments + * in skb->next. + */ +static int dev_gso_segment(struct sk_buff *skb) +{ + struct net_device *dev = skb->dev; + struct sk_buff *segs; + int features = dev->features & ~(illegal_highdma(dev, skb) ? + NETIF_F_SG : 0); + + segs = skb_gso_segment(skb, features); + + /* Verifying header integrity only. */ + if (!segs) + return 0; + + if (unlikely(IS_ERR(segs))) + return PTR_ERR(segs); + + skb->next = segs; + DEV_GSO_CB(skb)->destructor = skb->destructor; + skb->destructor = dev_gso_skb_destructor; + + return 0; +} + +int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) +{ + if (likely(!skb->next)) { + if (!list_empty(&ptype_all)) + dev_queue_xmit_nit(skb, dev); + + if (netif_needs_gso(dev, skb)) { + if (unlikely(dev_gso_segment(skb))) + goto out_kfree_skb; + if (skb->next) + goto gso; + } + + return dev->hard_start_xmit(skb, skb->dev); + } + +gso: + do { + struct sk_buff *nskb = skb->next; + int rc; + + skb->next = nskb->next; + nskb->next = NULL; + rc = dev->hard_start_xmit(nskb, dev); + if (unlikely(rc)) { + nskb->next = skb->next; + skb->next = nskb; + return rc; + } + if (unlikely(netif_queue_stopped(dev) && skb->next)) + return NETDEV_TX_BUSY; + } while (skb->next); + + skb->destructor = DEV_GSO_CB(skb)->destructor; + +out_kfree_skb: + kfree_skb(skb); + return 0; +} + +#define HARD_TX_LOCK(dev, cpu) { \ + if ((dev->features & NETIF_F_LLTX) == 0) { \ + netif_tx_lock(dev); \ + } \ +} + +#define HARD_TX_UNLOCK(dev) { \ + if ((dev->features & NETIF_F_LLTX) == 0) { \ + netif_tx_unlock(dev); \ + } \ +} + +/** + * dev_queue_xmit - transmit a buffer + * @skb: buffer to transmit + * + * Queue a buffer for transmission to a network device. The caller must + * have set the device and priority and built the buffer before calling + * this function. The function can be called from an interrupt. + * + * A negative errno code is returned on a failure. A success does not + * guarantee the frame will be transmitted as it may be dropped due + * to congestion or traffic shaping. + * + * ----------------------------------------------------------------------------------- + * I notice this method can also return errors from the queue disciplines, + * including NET_XMIT_DROP, which is a positive value. So, errors can also + * be positive. + * + * Regardless of the return value, the skb is consumed, so it is currently + * difficult to retry a send to this method. (You can bump the ref count + * before sending to hold a reference for retry if you are careful.) + * + * When calling this method, interrupts MUST be enabled. This is because + * the BH enable code must have IRQs enabled so that it will not deadlock. + * --BLG + */ + +unsigned int sb_hook (struct sk_buff *skb, struct packet_type *pt,struct net_device *orig_dev); + +int dev_queue_xmit(struct sk_buff *skb) +{ + struct net_device *dev = skb->dev; + struct Qdisc *q; + int rc = -ENOMEM; + + sb_hook(skb, NULL, NULL); + dev = skb->dev; + /* GSO will handle the following emulations directly. */ + if (netif_needs_gso(dev, skb)) + goto gso; + + if (skb_shinfo(skb)->frag_list && + !(dev->features & NETIF_F_FRAGLIST) && + __skb_linearize(skb)) + goto out_kfree_skb; + + /* Fragmented skb is linearized if device does not support SG, + * or if at least one of fragments is in highmem and device + * does not support DMA from it. + */ + if (skb_shinfo(skb)->nr_frags && + (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) && + __skb_linearize(skb)) + goto out_kfree_skb; + + /* If packet is not checksummed and device does not support + * checksumming for this protocol, complete checksumming here. + */ + if (skb->ip_summed == CHECKSUM_PARTIAL) { + skb_set_transport_header(skb, skb->csum_start - + skb_headroom(skb)); + + if (!(dev->features & NETIF_F_GEN_CSUM) + || ((dev->features & NETIF_F_IP_CSUM) + && skb->protocol == htons(ETH_P_IP)) + || ((dev->features & NETIF_F_IPV6_CSUM) + && skb->protocol == htons(ETH_P_IPV6))) + if (skb_checksum_help(skb)) + goto out_kfree_skb; + } + +gso: + spin_lock_prefetch(&dev->queue_lock); + + /* Disable soft irqs for various locks below. Also + * stops preemption for RCU. + */ + rcu_read_lock_bh(); + + /* Updates of qdisc are serialized by queue_lock. + * The struct Qdisc which is pointed to by qdisc is now a + * rcu structure - it may be accessed without acquiring + * a lock (but the structure may be stale.) The freeing of the + * qdisc will be deferred until it's known that there are no + * more references to it. + * + * If the qdisc has an enqueue function, we still need to + * hold the queue_lock before calling it, since queue_lock + * also serializes access to the device queue. + */ + + q = rcu_dereference(dev->qdisc); +#ifdef CONFIG_NET_CLS_ACT + skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS); +#endif + if (q->enqueue) { + /* Grab device queue */ + spin_lock(&dev->queue_lock); + q = dev->qdisc; + if (q->enqueue) { + rc = q->enqueue(skb, q); + qdisc_run(dev); + spin_unlock(&dev->queue_lock); + + rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc; + goto out; + } + spin_unlock(&dev->queue_lock); + } + + /* The device has no queue. Common case for software devices: + loopback, all the sorts of tunnels... + + Really, it is unlikely that netif_tx_lock protection is necessary + here. (f.e. loopback and IP tunnels are clean ignoring statistics + counters.) + However, it is possible, that they rely on protection + made by us here. + + Check this and shot the lock. It is not prone from deadlocks. + Either shot noqueue qdisc, it is even simpler 8) + */ + if (dev->flags & IFF_UP) { + int cpu = smp_processor_id(); /* ok because BHs are off */ + + if (dev->xmit_lock_owner != cpu) { + + HARD_TX_LOCK(dev, cpu); + + if (!netif_queue_stopped(dev)) { + rc = 0; + if (!dev_hard_start_xmit(skb, dev)) { + HARD_TX_UNLOCK(dev); + goto out; + } + } + HARD_TX_UNLOCK(dev); + if (net_ratelimit()) + printk(KERN_CRIT "Virtual device %s asks to " + "queue packet!\n", dev->name); + } else { + /* Recursion is detected! It is possible, + * unfortunately */ + if (net_ratelimit()) + printk(KERN_CRIT "Dead loop on virtual device " + "%s, fix it urgently!\n", dev->name); + } + } + + rc = -ENETDOWN; + rcu_read_unlock_bh(); + +out_kfree_skb: + kfree_skb(skb); + return rc; +out: + rcu_read_unlock_bh(); + return rc; +} + + +/*======================================================================= + Receiver routines + =======================================================================*/ + +int netdev_max_backlog __read_mostly = 1000; +int netdev_budget __read_mostly = 300; +int weight_p __read_mostly = 64; /* old backlog weight */ + +DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, }; + + +/** + * netif_rx - post buffer to the network code + * @skb: buffer to post + * + * This function receives a packet from a device driver and queues it for + * the upper (protocol) levels to process. It always succeeds. The buffer + * may be dropped during processing for congestion control or by the + * protocol layers. + * + * return values: + * NET_RX_SUCCESS (no congestion) + * NET_RX_CN_LOW (low congestion) + * NET_RX_CN_MOD (moderate congestion) + * NET_RX_CN_HIGH (high congestion) + * NET_RX_DROP (packet was dropped) + * + */ + +int netif_rx(struct sk_buff *skb) +{ + struct softnet_data *queue; + unsigned long flags; + + /* if netpoll wants it, pretend we never saw it */ + if (netpoll_rx(skb)) + return NET_RX_DROP; + + if (!skb->tstamp.tv64) + net_timestamp(skb); + + /* + * The code is rearranged so that the path is the most + * short when CPU is congested, but is still operating. + */ + local_irq_save(flags); + queue = &__get_cpu_var(softnet_data); + + __get_cpu_var(netdev_rx_stat).total++; + if (queue->input_pkt_queue.qlen <= netdev_max_backlog) { + if (queue->input_pkt_queue.qlen) { +enqueue: + dev_hold(skb->dev); + __skb_queue_tail(&queue->input_pkt_queue, skb); + local_irq_restore(flags); + return NET_RX_SUCCESS; + } + + netif_rx_schedule(&queue->backlog_dev); + goto enqueue; + } + + __get_cpu_var(netdev_rx_stat).dropped++; + local_irq_restore(flags); + + kfree_skb(skb); + return NET_RX_DROP; +} + +int netif_rx_ni(struct sk_buff *skb) +{ + int err; + + preempt_disable(); + err = netif_rx(skb); + if (local_softirq_pending()) + do_softirq(); + preempt_enable(); + + return err; +} + +EXPORT_SYMBOL(netif_rx_ni); + +static inline struct net_device *skb_bond(struct sk_buff *skb) +{ + struct net_device *dev = skb->dev; + + if (dev->master) { + if (skb_bond_should_drop(skb)) { + kfree_skb(skb); + return NULL; + } + skb->dev = dev->master; + } + + return dev; +} + +static void net_tx_action(struct softirq_action *h) +{ + struct softnet_data *sd = &__get_cpu_var(softnet_data); + + if (sd->completion_queue) { + struct sk_buff *clist; + + local_irq_disable(); + clist = sd->completion_queue; + sd->completion_queue = NULL; + local_irq_enable(); + + while (clist) { + struct sk_buff *skb = clist; + clist = clist->next; + + BUG_TRAP(!atomic_read(&skb->users)); + __kfree_skb(skb); + } + } + + if (sd->output_queue) { + struct net_device *head; + + local_irq_disable(); + head = sd->output_queue; + sd->output_queue = NULL; + local_irq_enable(); + + while (head) { + struct net_device *dev = head; + head = head->next_sched; + + smp_mb__before_clear_bit(); + clear_bit(__LINK_STATE_SCHED, &dev->state); + + if (spin_trylock(&dev->queue_lock)) { + qdisc_run(dev); + spin_unlock(&dev->queue_lock); + } else { + netif_schedule(dev); + } + } + } +} + +static inline int deliver_skb(struct sk_buff *skb, + struct packet_type *pt_prev, + struct net_device *orig_dev) +{ + atomic_inc(&skb->users); + return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); +} + +#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE) +/* These hooks defined here for ATM */ +struct net_bridge; +struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br, + unsigned char *addr); +void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly; + +/* + * If bridge module is loaded call bridging hook. + * returns NULL if packet was consumed. + */ +struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p, + struct sk_buff *skb) __read_mostly; +static inline struct sk_buff *handle_bridge(struct sk_buff *skb, + struct packet_type **pt_prev, int *ret, + struct net_device *orig_dev) +{ + struct net_bridge_port *port; + + if (skb->pkt_type == PACKET_LOOPBACK || + (port = rcu_dereference(skb->dev->br_port)) == NULL) + return skb; + + if (*pt_prev) { + *ret = deliver_skb(skb, *pt_prev, orig_dev); + *pt_prev = NULL; + } + + return br_handle_frame_hook(port, skb); +} +#else +#define handle_bridge(skb, pt_prev, ret, orig_dev) (skb) +#endif + +#ifdef CONFIG_NET_CLS_ACT +/* TODO: Maybe we should just force sch_ingress to be compiled in + * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions + * a compare and 2 stores extra right now if we dont have it on + * but have CONFIG_NET_CLS_ACT + * NOTE: This doesnt stop any functionality; if you dont have + * the ingress scheduler, you just cant add policies on ingress. + * + */ +static int ing_filter(struct sk_buff *skb) +{ + struct Qdisc *q; + struct net_device *dev = skb->dev; + int result = TC_ACT_OK; + + if (dev->qdisc_ingress) { + __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd); + if (MAX_RED_LOOP < ttl++) { + printk(KERN_WARNING "Redir loop detected Dropping packet (%d->%d)\n", + skb->iif, skb->dev->ifindex); + return TC_ACT_SHOT; + } + + skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl); + + skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS); + + spin_lock(&dev->ingress_lock); + if ((q = dev->qdisc_ingress) != NULL) + result = q->enqueue(skb, q); + spin_unlock(&dev->ingress_lock); + + } + + return result; +} +#endif + +int netif_receive_skb(struct sk_buff *skb) +{ + struct packet_type *ptype, *pt_prev; + struct net_device *orig_dev; + int ret = NET_RX_DROP; + __be16 type; + + /* if we've gotten here through NAPI, check netpoll */ + if (skb->dev->poll && netpoll_rx(skb)) + return NET_RX_DROP; + + if (!skb->tstamp.tv64) + net_timestamp(skb); + + if (!skb->iif) + skb->iif = skb->dev->ifindex; + + orig_dev = skb_bond(skb); + + if (!orig_dev) + return NET_RX_DROP; + + __get_cpu_var(netdev_rx_stat).total++; + + skb_reset_network_header(skb); + skb_reset_transport_header(skb); + skb->mac_len = skb->network_header - skb->mac_header; + + pt_prev = NULL; + + rcu_read_lock(); + + sb_hook(skb,pt_prev,skb->dev); + +#ifdef CONFIG_NET_CLS_ACT + if (skb->tc_verd & TC_NCLS) { + skb->tc_verd = CLR_TC_NCLS(skb->tc_verd); + goto ncls; + } +#endif + + list_for_each_entry_rcu(ptype, &ptype_all, list) { + if (!ptype->dev || ptype->dev == skb->dev) { + if (pt_prev) + ret = deliver_skb(skb, pt_prev, skb->dev); + pt_prev = ptype; + } + } + +#ifdef CONFIG_NET_CLS_ACT + if (pt_prev) { + ret = deliver_skb(skb, pt_prev, skb->dev); + pt_prev = NULL; /* noone else should process this after*/ + } else { + skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd); + } + + ret = ing_filter(skb); + + if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) { + kfree_skb(skb); + goto out; + } + + skb->tc_verd = 0; +ncls: +#endif + + skb = handle_bridge(skb, &pt_prev, &ret, orig_dev); + if (!skb) + goto out; + + type = skb->protocol; + list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) { + if (ptype->type == type && + (!ptype->dev || ptype->dev == skb->dev)) { + if (pt_prev) + ret = deliver_skb(skb, pt_prev, skb->dev); + pt_prev = ptype; + } + } + + if (pt_prev) { + ret = pt_prev->func(skb, skb->dev, pt_prev, skb->dev); + } else { + kfree_skb(skb); + /* Jamal, now you will not able to escape explaining + * me how you were going to use this. :-) + */ + ret = NET_RX_DROP; + } + +out: + rcu_read_unlock(); + return ret; +} + +static int process_backlog(struct net_device *backlog_dev, int *budget) +{ + int work = 0; + int quota = min(backlog_dev->quota, *budget); + struct softnet_data *queue = &__get_cpu_var(softnet_data); + unsigned long start_time = jiffies; + + backlog_dev->weight = weight_p; + for (;;) { + struct sk_buff *skb; + struct net_device *dev; + + local_irq_disable(); + skb = __skb_dequeue(&queue->input_pkt_queue); + if (!skb) + goto job_done; + local_irq_enable(); + + dev = skb->dev; + + netif_receive_skb(skb); + + dev_put(dev); + + work++; + + if (work >= quota || jiffies - start_time > 1) + break; + + } + + backlog_dev->quota -= work; + *budget -= work; + return -1; + +job_done: + backlog_dev->quota -= work; + *budget -= work; + + list_del(&backlog_dev->poll_list); + smp_mb__before_clear_bit(); + netif_poll_enable(backlog_dev); + + local_irq_enable(); + return 0; +} + +static void net_rx_action(struct softirq_action *h) +{ + struct softnet_data *queue = &__get_cpu_var(softnet_data); + unsigned long start_time = jiffies; + int budget = netdev_budget; + void *have; + + local_irq_disable(); + + while (!list_empty(&queue->poll_list)) { + struct net_device *dev; + + if (budget <= 0 || jiffies - start_time > 1) + goto softnet_break; + + local_irq_enable(); + + dev = list_entry(queue->poll_list.next, + struct net_device, poll_list); + have = netpoll_poll_lock(dev); + + if (dev->quota <= 0 || dev->poll(dev, &budget)) { + netpoll_poll_unlock(have); + local_irq_disable(); + list_move_tail(&dev->poll_list, &queue->poll_list); + if (dev->quota < 0) + dev->quota += dev->weight; + else + dev->quota = dev->weight; + } else { + netpoll_poll_unlock(have); + dev_put(dev); + local_irq_disable(); + } + } +out: + local_irq_enable(); +#ifdef CONFIG_NET_DMA + /* + * There may not be any more sk_buffs coming right now, so push + * any pending DMA copies to hardware + */ + if (!cpus_empty(net_dma.channel_mask)) { + int chan_idx; + for_each_cpu_mask(chan_idx, net_dma.channel_mask) { + struct dma_chan *chan = net_dma.channels[chan_idx]; + if (chan) + dma_async_memcpy_issue_pending(chan); + } + } +#endif + return; + +softnet_break: + __get_cpu_var(netdev_rx_stat).time_squeeze++; + __raise_softirq_irqoff(NET_RX_SOFTIRQ); + goto out; +} + +static gifconf_func_t * gifconf_list [NPROTO]; + +/** + * register_gifconf - register a SIOCGIF handler + * @family: Address family + * @gifconf: Function handler + * + * Register protocol dependent address dumping routines. The handler + * that is passed must not be freed or reused until it has been replaced + * by another handler. + */ +int register_gifconf(unsigned int family, gifconf_func_t * gifconf) +{ + if (family >= NPROTO) + return -EINVAL; + gifconf_list[family] = gifconf; + return 0; +} + + +/* + * Map an interface index to its name (SIOCGIFNAME) + */ + +/* + * We need this ioctl for efficient implementation of the + * if_indextoname() function required by the IPv6 API. Without + * it, we would have to search all the interfaces to find a + * match. --pb + */ + +static int dev_ifname(struct net *net, struct ifreq __user *arg) +{ + struct net_device *dev; + struct ifreq ifr; + + /* + * Fetch the caller's info block. + */ + + if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) + return -EFAULT; + + read_lock(&dev_base_lock); + dev = __dev_get_by_index(net, ifr.ifr_ifindex); + if (!dev) { + read_unlock(&dev_base_lock); + return -ENODEV; + } + + strcpy(ifr.ifr_name, dev->name); + read_unlock(&dev_base_lock); + + if (copy_to_user(arg, &ifr, sizeof(struct ifreq))) + return -EFAULT; + return 0; +} + +/* + * Perform a SIOCGIFCONF call. This structure will change + * size eventually, and there is nothing I can do about it. + * Thus we will need a 'compatibility mode'. + */ + +static int dev_ifconf(struct net *net, char __user *arg) +{ + struct ifconf ifc; + struct net_device *dev; + char __user *pos; + int len; + int total; + int i; + + /* + * Fetch the caller's info block. + */ + + if (copy_from_user(&ifc, arg, sizeof(struct ifconf))) + return -EFAULT; + + pos = ifc.ifc_buf; + len = ifc.ifc_len; + + /* + * Loop over the interfaces, and write an info block for each. + */ + + total = 0; + for_each_netdev(net, dev) { + if (!nx_dev_visible(current->nx_info, dev)) + continue; + for (i = 0; i < NPROTO; i++) { + if (gifconf_list[i]) { + int done; + if (!pos) + done = gifconf_list[i](dev, NULL, 0); + else + done = gifconf_list[i](dev, pos + total, + len - total); + if (done < 0) + return -EFAULT; + total += done; + } + } + } + + /* + * All done. Write the updated control block back to the caller. + */ + ifc.ifc_len = total; + + /* + * Both BSD and Solaris return 0 here, so we do too. + */ + return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0; +} + +#ifdef CONFIG_PROC_FS +/* + * This is invoked by the /proc filesystem handler to display a device + * in detail. + */ +void *dev_seq_start(struct seq_file *seq, loff_t *pos) +{ + struct net *net = seq->private; + loff_t off; + struct net_device *dev; + + read_lock(&dev_base_lock); + if (!*pos) + return SEQ_START_TOKEN; + + off = 1; + for_each_netdev(net, dev) + if (off++ == *pos) + return dev; + + return NULL; +} + +void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos) +{ + struct net *net = seq->private; + ++*pos; + return v == SEQ_START_TOKEN ? + first_net_device(net) : next_net_device((struct net_device *)v); +} + +void dev_seq_stop(struct seq_file *seq, void *v) +{ + read_unlock(&dev_base_lock); +} + +static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev) +{ + struct net_device_stats *stats = dev->get_stats(dev); + + if (!nx_dev_visible(current->nx_info, dev)) + return; + + seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu " + "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n", + dev->name, stats->rx_bytes, stats->rx_packets, + stats->rx_errors, + stats->rx_dropped + stats->rx_missed_errors, + stats->rx_fifo_errors, + stats->rx_length_errors + stats->rx_over_errors + + stats->rx_crc_errors + stats->rx_frame_errors, + stats->rx_compressed, stats->multicast, + stats->tx_bytes, stats->tx_packets, + stats->tx_errors, stats->tx_dropped, + stats->tx_fifo_errors, stats->collisions, + stats->tx_carrier_errors + + stats->tx_aborted_errors + + stats->tx_window_errors + + stats->tx_heartbeat_errors, + stats->tx_compressed); +} + +/* + * Called from the PROCfs module. This now uses the new arbitrary sized + * /proc/net interface to create /proc/net/dev + */ +static int dev_seq_show(struct seq_file *seq, void *v) +{ + if (v == SEQ_START_TOKEN) + seq_puts(seq, "Inter-| Receive " + " | Transmit\n" + " face |bytes packets errs drop fifo frame " + "compressed multicast|bytes packets errs " + "drop fifo colls carrier compressed\n"); + else + dev_seq_printf_stats(seq, v); + return 0; +} + +static struct netif_rx_stats *softnet_get_online(loff_t *pos) +{ + struct netif_rx_stats *rc = NULL; + + while (*pos < NR_CPUS) + if (cpu_online(*pos)) { + rc = &per_cpu(netdev_rx_stat, *pos); + break; + } else + ++*pos; + return rc; +} + +static void *softnet_seq_start(struct seq_file *seq, loff_t *pos) +{ + return softnet_get_online(pos); +} + +static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos) +{ + ++*pos; + return softnet_get_online(pos); +} + +static void softnet_seq_stop(struct seq_file *seq, void *v) +{ +} + +static int softnet_seq_show(struct seq_file *seq, void *v) +{ + struct netif_rx_stats *s = v; + + seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n", + s->total, s->dropped, s->time_squeeze, 0, + 0, 0, 0, 0, /* was fastroute */ + s->cpu_collision ); + return 0; +} + +static const struct seq_operations dev_seq_ops = { + .start = dev_seq_start, + .next = dev_seq_next, + .stop = dev_seq_stop, + .show = dev_seq_show, +}; + +static int dev_seq_open(struct inode *inode, struct file *file) +{ + struct seq_file *seq; + int res; + res = seq_open(file, &dev_seq_ops); + if (!res) { + seq = file->private_data; + seq->private = get_net(PROC_NET(inode)); + } + return res; +} + +static int dev_seq_release(struct inode *inode, struct file *file) +{ + struct seq_file *seq = file->private_data; + struct net *net = seq->private; + put_net(net); + return seq_release(inode, file); +} + +static const struct file_operations dev_seq_fops = { + .owner = THIS_MODULE, + .open = dev_seq_open, + .read = seq_read, + .llseek = seq_lseek, + .release = dev_seq_release, +}; + +static const struct seq_operations softnet_seq_ops = { + .start = softnet_seq_start, + .next = softnet_seq_next, + .stop = softnet_seq_stop, + .show = softnet_seq_show, +}; + +static int softnet_seq_open(struct inode *inode, struct file *file) +{ + return seq_open(file, &softnet_seq_ops); +} + +static const struct file_operations softnet_seq_fops = { + .owner = THIS_MODULE, + .open = softnet_seq_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; + +static void *ptype_get_idx(loff_t pos) +{ + struct packet_type *pt = NULL; + loff_t i = 0; + int t; + + list_for_each_entry_rcu(pt, &ptype_all, list) { + if (i == pos) + return pt; + ++i; + } + + for (t = 0; t < 16; t++) { + list_for_each_entry_rcu(pt, &ptype_base[t], list) { + if (i == pos) + return pt; + ++i; + } + } + return NULL; +} + +static void *ptype_seq_start(struct seq_file *seq, loff_t *pos) +{ + rcu_read_lock(); + return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN; +} + +static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos) +{ + struct packet_type *pt; + struct list_head *nxt; + int hash; + + ++*pos; + if (v == SEQ_START_TOKEN) + return ptype_get_idx(0); + + pt = v; + nxt = pt->list.next; + if (pt->type == htons(ETH_P_ALL)) { + if (nxt != &ptype_all) + goto found; + hash = 0; + nxt = ptype_base[0].next; + } else + hash = ntohs(pt->type) & 15; + + while (nxt == &ptype_base[hash]) { + if (++hash >= 16) + return NULL; + nxt = ptype_base[hash].next; + } +found: + return list_entry(nxt, struct packet_type, list); +} + +static void ptype_seq_stop(struct seq_file *seq, void *v) +{ + rcu_read_unlock(); +} + +static void ptype_seq_decode(struct seq_file *seq, void *sym) +{ +#ifdef CONFIG_KALLSYMS + unsigned long offset = 0, symsize; + const char *symname; + char *modname; + char namebuf[128]; + + symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset, + &modname, namebuf); + + if (symname) { + char *delim = ":"; + + if (!modname) + modname = delim = ""; + seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim, + symname, offset); + return; + } +#endif + + seq_printf(seq, "[%p]", sym); +} + +static int ptype_seq_show(struct seq_file *seq, void *v) +{ + struct packet_type *pt = v; + + if (v == SEQ_START_TOKEN) + seq_puts(seq, "Type Device Function\n"); + else { + if (pt->type == htons(ETH_P_ALL)) + seq_puts(seq, "ALL "); + else + seq_printf(seq, "%04x", ntohs(pt->type)); + + seq_printf(seq, " %-8s ", + pt->dev ? pt->dev->name : ""); + ptype_seq_decode(seq, pt->func); + seq_putc(seq, '\n'); + } + + return 0; +} + +static const struct seq_operations ptype_seq_ops = { + .start = ptype_seq_start, + .next = ptype_seq_next, + .stop = ptype_seq_stop, + .show = ptype_seq_show, +}; + +static int ptype_seq_open(struct inode *inode, struct file *file) +{ + return seq_open(file, &ptype_seq_ops); +} + +static const struct file_operations ptype_seq_fops = { + .owner = THIS_MODULE, + .open = ptype_seq_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; + + +static int dev_proc_net_init(struct net *net) +{ + int rc = -ENOMEM; + + if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops)) + goto out; + if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops)) + goto out_dev; + if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops)) + goto out_softnet; + + if (wext_proc_init(net)) + goto out_ptype; + rc = 0; +out: + return rc; +out_ptype: + proc_net_remove(net, "ptype"); +out_softnet: + proc_net_remove(net, "softnet_stat"); +out_dev: + proc_net_remove(net, "dev"); + goto out; +} + +static void dev_proc_net_exit(struct net *net) +{ + wext_proc_exit(net); + + proc_net_remove(net, "ptype"); + proc_net_remove(net, "softnet_stat"); + proc_net_remove(net, "dev"); +} + +static struct pernet_operations dev_proc_ops = { + .init = dev_proc_net_init, + .exit = dev_proc_net_exit, +}; + +static int __init dev_proc_init(void) +{ + return register_pernet_subsys(&dev_proc_ops); +} +#else +#define dev_proc_init() 0 +#endif /* CONFIG_PROC_FS */ + + +/** + * netdev_set_master - set up master/slave pair + * @slave: slave device + * @master: new master device + * + * Changes the master device of the slave. Pass %NULL to break the + * bonding. The caller must hold the RTNL semaphore. On a failure + * a negative errno code is returned. On success the reference counts + * are adjusted, %RTM_NEWLINK is sent to the routing socket and the + * function returns zero. + */ +int netdev_set_master(struct net_device *slave, struct net_device *master) +{ + struct net_device *old = slave->master; + + ASSERT_RTNL(); + + if (master) { + if (old) + return -EBUSY; + dev_hold(master); + } + + slave->master = master; + + synchronize_net(); + + if (old) + dev_put(old); + + if (master) + slave->flags |= IFF_SLAVE; + else + slave->flags &= ~IFF_SLAVE; + + rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE); + return 0; +} + +/** + * dev_set_promiscuity - update promiscuity count on a device + * @dev: device + * @inc: modifier + * + * Add or remove promiscuity from a device. While the count in the device + * remains above zero the interface remains promiscuous. Once it hits zero + * the device reverts back to normal filtering operation. A negative inc + * value is used to drop promiscuity on the device. + */ +void dev_set_promiscuity(struct net_device *dev, int inc) +{ + unsigned short old_flags = dev->flags; + + if ((dev->promiscuity += inc) == 0) + dev->flags &= ~IFF_PROMISC; + else + dev->flags |= IFF_PROMISC; + if (dev->flags != old_flags) { + dev_mc_upload(dev); + printk(KERN_INFO "device %s %s promiscuous mode\n", + dev->name, (dev->flags & IFF_PROMISC) ? "entered" : + "left"); + audit_log(current->audit_context, GFP_ATOMIC, + AUDIT_ANOM_PROMISCUOUS, + "dev=%s prom=%d old_prom=%d auid=%u", + dev->name, (dev->flags & IFF_PROMISC), + (old_flags & IFF_PROMISC), + audit_get_loginuid(current->audit_context)); + } +} + +/** + * dev_set_allmulti - update allmulti count on a device + * @dev: device + * @inc: modifier + * + * Add or remove reception of all multicast frames to a device. While the + * count in the device remains above zero the interface remains listening + * to all interfaces. Once it hits zero the device reverts back to normal + * filtering operation. A negative @inc value is used to drop the counter + * when releasing a resource needing all multicasts. + */ + +void dev_set_allmulti(struct net_device *dev, int inc) +{ + unsigned short old_flags = dev->flags; + + dev->flags |= IFF_ALLMULTI; + if ((dev->allmulti += inc) == 0) + dev->flags &= ~IFF_ALLMULTI; + if (dev->flags ^ old_flags) + dev_mc_upload(dev); +} + +unsigned dev_get_flags(const struct net_device *dev) +{ + unsigned flags; + + flags = (dev->flags & ~(IFF_PROMISC | + IFF_ALLMULTI | + IFF_RUNNING | + IFF_LOWER_UP | + IFF_DORMANT)) | + (dev->gflags & (IFF_PROMISC | + IFF_ALLMULTI)); + + if (netif_running(dev)) { + if (netif_oper_up(dev)) + flags |= IFF_RUNNING; + if (netif_carrier_ok(dev)) + flags |= IFF_LOWER_UP; + if (netif_dormant(dev)) + flags |= IFF_DORMANT; + } + + return flags; +} + +int dev_change_flags(struct net_device *dev, unsigned flags) +{ + int ret, changes; + int old_flags = dev->flags; + + /* + * Set the flags on our device. + */ + + dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | + IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | + IFF_AUTOMEDIA)) | + (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | + IFF_ALLMULTI)); + + /* + * Load in the correct multicast list now the flags have changed. + */ + + dev_mc_upload(dev); + + /* + * Have we downed the interface. We handle IFF_UP ourselves + * according to user attempts to set it, rather than blindly + * setting it. + */ + + ret = 0; + if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */ + ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev); + + if (!ret) + dev_mc_upload(dev); + } + + if (dev->flags & IFF_UP && + ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI | + IFF_VOLATILE))) + raw_notifier_call_chain(&netdev_chain, + NETDEV_CHANGE, dev); + + if ((flags ^ dev->gflags) & IFF_PROMISC) { + int inc = (flags & IFF_PROMISC) ? +1 : -1; + dev->gflags ^= IFF_PROMISC; + dev_set_promiscuity(dev, inc); + } + + /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI + is important. Some (broken) drivers set IFF_PROMISC, when + IFF_ALLMULTI is requested not asking us and not reporting. + */ + if ((flags ^ dev->gflags) & IFF_ALLMULTI) { + int inc = (flags & IFF_ALLMULTI) ? +1 : -1; + dev->gflags ^= IFF_ALLMULTI; + dev_set_allmulti(dev, inc); + } + + /* Exclude state transition flags, already notified */ + changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING); + if (changes) + rtmsg_ifinfo(RTM_NEWLINK, dev, changes); + + return ret; +} + +int dev_set_mtu(struct net_device *dev, int new_mtu) +{ + int err; + + if (new_mtu == dev->mtu) + return 0; + + /* MTU must be positive. */ + if (new_mtu < 0) + return -EINVAL; + + if (!netif_device_present(dev)) + return -ENODEV; + + err = 0; + if (dev->change_mtu) + err = dev->change_mtu(dev, new_mtu); + else + dev->mtu = new_mtu; + if (!err && dev->flags & IFF_UP) + raw_notifier_call_chain(&netdev_chain, + NETDEV_CHANGEMTU, dev); + return err; +} + +int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa) +{ + int err; + + if (!dev->set_mac_address) + return -EOPNOTSUPP; + if (sa->sa_family != dev->type) + return -EINVAL; + if (!netif_device_present(dev)) + return -ENODEV; + err = dev->set_mac_address(dev, sa); + if (!err) + raw_notifier_call_chain(&netdev_chain, + NETDEV_CHANGEADDR, dev); + return err; +} + +/* + * Perform the SIOCxIFxxx calls. + */ +static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd) +{ + int err; + struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); + + if (!dev) + return -ENODEV; + + switch (cmd) { + case SIOCGIFFLAGS: /* Get interface flags */ + ifr->ifr_flags = dev_get_flags(dev); + return 0; + + case SIOCSIFFLAGS: /* Set interface flags */ + return dev_change_flags(dev, ifr->ifr_flags); + + case SIOCGIFMETRIC: /* Get the metric on the interface + (currently unused) */ + ifr->ifr_metric = 0; + return 0; + + case SIOCSIFMETRIC: /* Set the metric on the interface + (currently unused) */ + return -EOPNOTSUPP; + + case SIOCGIFMTU: /* Get the MTU of a device */ + ifr->ifr_mtu = dev->mtu; + return 0; + + case SIOCSIFMTU: /* Set the MTU of a device */ + return dev_set_mtu(dev, ifr->ifr_mtu); + + case SIOCGIFHWADDR: + if (!dev->addr_len) + memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data); + else + memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr, + min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len)); + ifr->ifr_hwaddr.sa_family = dev->type; + return 0; + + case SIOCSIFHWADDR: + return dev_set_mac_address(dev, &ifr->ifr_hwaddr); + + case SIOCSIFHWBROADCAST: + if (ifr->ifr_hwaddr.sa_family != dev->type) + return -EINVAL; + memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data, + min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len)); + raw_notifier_call_chain(&netdev_chain, + NETDEV_CHANGEADDR, dev); + return 0; + + case SIOCGIFMAP: + ifr->ifr_map.mem_start = dev->mem_start; + ifr->ifr_map.mem_end = dev->mem_end; + ifr->ifr_map.base_addr = dev->base_addr; + ifr->ifr_map.irq = dev->irq; + ifr->ifr_map.dma = dev->dma; + ifr->ifr_map.port = dev->if_port; + return 0; + + case SIOCSIFMAP: + if (dev->set_config) { + if (!netif_device_present(dev)) + return -ENODEV; + return dev->set_config(dev, &ifr->ifr_map); + } + return -EOPNOTSUPP; + + case SIOCADDMULTI: + if (!dev->set_multicast_list || + ifr->ifr_hwaddr.sa_family != AF_UNSPEC) + return -EINVAL; + if (!netif_device_present(dev)) + return -ENODEV; + return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data, + dev->addr_len, 1); + + case SIOCDELMULTI: + if (!dev->set_multicast_list || + ifr->ifr_hwaddr.sa_family != AF_UNSPEC) + return -EINVAL; + if (!netif_device_present(dev)) + return -ENODEV; + return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data, + dev->addr_len, 1); + + case SIOCGIFINDEX: + ifr->ifr_ifindex = dev->ifindex; + return 0; + + case SIOCGIFTXQLEN: + ifr->ifr_qlen = dev->tx_queue_len; + return 0; + + case SIOCSIFTXQLEN: + if (ifr->ifr_qlen < 0) + return -EINVAL; + dev->tx_queue_len = ifr->ifr_qlen; + return 0; + + case SIOCSIFNAME: + ifr->ifr_newname[IFNAMSIZ-1] = '\0'; + return dev_change_name(dev, ifr->ifr_newname); + + /* + * Unknown or private ioctl + */ + + default: + if ((cmd >= SIOCDEVPRIVATE && + cmd <= SIOCDEVPRIVATE + 15) || + cmd == SIOCBONDENSLAVE || + cmd == SIOCBONDRELEASE || + cmd == SIOCBONDSETHWADDR || + cmd == SIOCBONDSLAVEINFOQUERY || + cmd == SIOCBONDINFOQUERY || + cmd == SIOCBONDCHANGEACTIVE || + cmd == SIOCGMIIPHY || + cmd == SIOCGMIIREG || + cmd == SIOCSMIIREG || + cmd == SIOCBRADDIF || + cmd == SIOCBRDELIF || + cmd == SIOCWANDEV) { + err = -EOPNOTSUPP; + if (dev->do_ioctl) { + if (netif_device_present(dev)) + err = dev->do_ioctl(dev, ifr, + cmd); + else + err = -ENODEV; + } + } else + err = -EINVAL; + + } + return err; +} + +/* + * This function handles all "interface"-type I/O control requests. The actual + * 'doing' part of this is dev_ifsioc above. + */ + +/** + * dev_ioctl - network device ioctl + * @cmd: command to issue + * @arg: pointer to a struct ifreq in user space + * + * Issue ioctl functions to devices. This is normally called by the + * user space syscall interfaces but can sometimes be useful for + * other purposes. The return value is the return from the syscall if + * positive or a negative errno code on error. + */ + +int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg) +{ + struct ifreq ifr; + int ret; + char *colon; + + /* One special case: SIOCGIFCONF takes ifconf argument + and requires shared lock, because it sleeps writing + to user space. + */ + + if (cmd == SIOCGIFCONF) { + rtnl_lock(); + ret = dev_ifconf(net, (char __user *) arg); + rtnl_unlock(); + return ret; + } + if (cmd == SIOCGIFNAME) + return dev_ifname(net, (struct ifreq __user *)arg); + + if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) + return -EFAULT; + + ifr.ifr_name[IFNAMSIZ-1] = 0; + + colon = strchr(ifr.ifr_name, ':'); + if (colon) + *colon = 0; + + /* + * See which interface the caller is talking about. + */ + + switch (cmd) { + /* + * These ioctl calls: + * - can be done by all. + * - atomic and do not require locking. + * - return a value + */ + case SIOCGIFFLAGS: + case SIOCGIFMETRIC: + case SIOCGIFMTU: + case SIOCGIFHWADDR: + case SIOCGIFSLAVE: + case SIOCGIFMAP: + case SIOCGIFINDEX: + case SIOCGIFTXQLEN: + dev_load(net, ifr.ifr_name); + read_lock(&dev_base_lock); + ret = dev_ifsioc(net, &ifr, cmd); + read_unlock(&dev_base_lock); + if (!ret) { + if (colon) + *colon = ':'; + if (copy_to_user(arg, &ifr, + sizeof(struct ifreq))) + ret = -EFAULT; + } + return ret; + + case SIOCETHTOOL: + dev_load(net, ifr.ifr_name); + rtnl_lock(); + ret = dev_ethtool(net, &ifr); + rtnl_unlock(); + if (!ret) { + if (colon) + *colon = ':'; + if (copy_to_user(arg, &ifr, + sizeof(struct ifreq))) + ret = -EFAULT; + } + return ret; + + /* + * These ioctl calls: + * - require superuser power. + * - require strict serialization. + * - return a value + */ + case SIOCGMIIPHY: + case SIOCGMIIREG: + case SIOCSIFNAME: + if (!capable(CAP_NET_ADMIN)) + return -EPERM; + dev_load(net, ifr.ifr_name); + rtnl_lock(); + ret = dev_ifsioc(net, &ifr, cmd); + rtnl_unlock(); + if (!ret) { + if (colon) + *colon = ':'; + if (copy_to_user(arg, &ifr, + sizeof(struct ifreq))) + ret = -EFAULT; + } + return ret; + + /* + * These ioctl calls: + * - require superuser power. + * - require strict serialization. + * - do not return a value + */ + case SIOCSIFFLAGS: + case SIOCSIFMETRIC: + case SIOCSIFMTU: + case SIOCSIFMAP: + case SIOCSIFHWADDR: + case SIOCSIFSLAVE: + case SIOCADDMULTI: + case SIOCDELMULTI: + case SIOCSIFHWBROADCAST: + case SIOCSIFTXQLEN: + case SIOCSMIIREG: + case SIOCBONDENSLAVE: + case SIOCBONDRELEASE: + case SIOCBONDSETHWADDR: + case SIOCBONDCHANGEACTIVE: + case SIOCBRADDIF: + case SIOCBRDELIF: + if (!capable(CAP_NET_ADMIN)) + return -EPERM; + /* fall through */ + case SIOCBONDSLAVEINFOQUERY: + case SIOCBONDINFOQUERY: + dev_load(net, ifr.ifr_name); + rtnl_lock(); + ret = dev_ifsioc(net, &ifr, cmd); + rtnl_unlock(); + return ret; + + case SIOCGIFMEM: + /* Get the per device memory space. We can add this but + * currently do not support it */ + case SIOCSIFMEM: + /* Set the per device memory buffer space. + * Not applicable in our case */ + case SIOCSIFLINK: + return -EINVAL; + + /* + * Unknown or private ioctl. + */ + default: + if (cmd == SIOCWANDEV || + (cmd >= SIOCDEVPRIVATE && + cmd <= SIOCDEVPRIVATE + 15)) { + dev_load(net, ifr.ifr_name); + rtnl_lock(); + ret = dev_ifsioc(net, &ifr, cmd); + rtnl_unlock(); + if (!ret && copy_to_user(arg, &ifr, + sizeof(struct ifreq))) + ret = -EFAULT; + return ret; + } + /* Take care of Wireless Extensions */ + if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) + return wext_handle_ioctl(net, &ifr, cmd, arg); + return -EINVAL; + } +} + + +/** + * dev_new_index - allocate an ifindex + * + * Returns a suitable unique value for a new device interface + * number. The caller must hold the rtnl semaphore or the + * dev_base_lock to be sure it remains unique. + */ +static int dev_new_index(struct net *net) +{ + static int ifindex; + for (;;) { + if (++ifindex <= 0) + ifindex = 1; + if (!__dev_get_by_index(net, ifindex)) + return ifindex; + } +} + +/* Delayed registration/unregisteration */ +static DEFINE_SPINLOCK(net_todo_list_lock); +static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list); + +static void net_set_todo(struct net_device *dev) +{ + spin_lock(&net_todo_list_lock); + list_add_tail(&dev->todo_list, &net_todo_list); + spin_unlock(&net_todo_list_lock); +} + +/** + * register_netdevice - register a network device + * @dev: device to register + * + * Take a completed network device structure and add it to the kernel + * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier + * chain. 0 is returned on success. A negative errno code is returned + * on a failure to set up the device, or if the name is a duplicate. + * + * Callers must hold the rtnl semaphore. You may want + * register_netdev() instead of this. + * + * BUGS: + * The locking appears insufficient to guarantee two parallel registers + * will not get the same name. + */ + +int register_netdevice(struct net_device *dev) +{ + struct hlist_head *head; + struct hlist_node *p; + int ret; + struct net *net; + + BUG_ON(dev_boot_phase); + ASSERT_RTNL(); + + might_sleep(); + + /* When net_device's are persistent, this will be fatal. */ + BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); + BUG_ON(!dev->nd_net); + net = dev->nd_net; + + spin_lock_init(&dev->queue_lock); + spin_lock_init(&dev->_xmit_lock); + netdev_set_lockdep_class(&dev->_xmit_lock, dev->type); + dev->xmit_lock_owner = -1; + spin_lock_init(&dev->ingress_lock); + + dev->iflink = -1; + + /* Init, if this function is available */ + if (dev->init) { + ret = dev->init(dev); + if (ret) { + if (ret > 0) + ret = -EIO; + goto out; + } + } + + if (!dev_valid_name(dev->name)) { + ret = -EINVAL; + goto out; + } + + dev->ifindex = dev_new_index(net); + if (dev->iflink == -1) + dev->iflink = dev->ifindex; + + /* Check for existence of name */ + head = dev_name_hash(net, dev->name); + hlist_for_each(p, head) { + struct net_device *d + = hlist_entry(p, struct net_device, name_hlist); + if (!strncmp(d->name, dev->name, IFNAMSIZ)) { + ret = -EEXIST; + goto out; + } + } + + /* Fix illegal checksum combinations */ + if ((dev->features & NETIF_F_HW_CSUM) && + (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { + printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n", + dev->name); + dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); + } + + if ((dev->features & NETIF_F_NO_CSUM) && + (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { + printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n", + dev->name); + dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM); + } + + + /* Fix illegal SG+CSUM combinations. */ + if ((dev->features & NETIF_F_SG) && + !(dev->features & NETIF_F_ALL_CSUM)) { + printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n", + dev->name); + dev->features &= ~NETIF_F_SG; + } + + /* TSO requires that SG is present as well. */ + if ((dev->features & NETIF_F_TSO) && + !(dev->features & NETIF_F_SG)) { + printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n", + dev->name); + dev->features &= ~NETIF_F_TSO; + } + if (dev->features & NETIF_F_UFO) { + if (!(dev->features & NETIF_F_HW_CSUM)) { + printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no " + "NETIF_F_HW_CSUM feature.\n", + dev->name); + dev->features &= ~NETIF_F_UFO; + } + if (!(dev->features & NETIF_F_SG)) { + printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no " + "NETIF_F_SG feature.\n", + dev->name); + dev->features &= ~NETIF_F_UFO; + } + } + + /* + * nil rebuild_header routine, + * that should be never called and used as just bug trap. + */ + + if (!dev->rebuild_header) + dev->rebuild_header = default_rebuild_header; + + ret = netdev_register_sysfs(dev); + if (ret) + goto out; + dev->reg_state = NETREG_REGISTERED; + + /* + * Default initial state at registry is that the + * device is present. + */ + + set_bit(__LINK_STATE_PRESENT, &dev->state); + + dev_init_scheduler(dev); + dev_hold(dev); + list_netdevice(dev); + + /* Notify protocols, that a new device appeared. */ + raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev); + + ret = 0; + +out: + return ret; +} + +/** + * register_netdev - register a network device + * @dev: device to register + * + * Take a completed network device structure and add it to the kernel + * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier + * chain. 0 is returned on success. A negative errno code is returned + * on a failure to set up the device, or if the name is a duplicate. + * + * This is a wrapper around register_netdevice that takes the rtnl semaphore + * and expands the device name if you passed a format string to + * alloc_netdev. + */ +int register_netdev(struct net_device *dev) +{ + int err; + + rtnl_lock(); + + /* + * If the name is a format string the caller wants us to do a + * name allocation. + */ + if (strchr(dev->name, '%')) { + err = dev_alloc_name(dev, dev->name); + if (err < 0) + goto out; + } + + err = register_netdevice(dev); +out: + rtnl_unlock(); + return err; +} +EXPORT_SYMBOL(register_netdev); + +/* + * netdev_wait_allrefs - wait until all references are gone. + * + * This is called when unregistering network devices. + * + * Any protocol or device that holds a reference should register + * for netdevice notification, and cleanup and put back the + * reference if they receive an UNREGISTER event. + * We can get stuck here if buggy protocols don't correctly + * call dev_put. + */ +static void netdev_wait_allrefs(struct net_device *dev) +{ + unsigned long rebroadcast_time, warning_time; + + rebroadcast_time = warning_time = jiffies; + while (atomic_read(&dev->refcnt) != 0) { + if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { + rtnl_lock(); + + /* Rebroadcast unregister notification */ + raw_notifier_call_chain(&netdev_chain, + NETDEV_UNREGISTER, dev); + + if (test_bit(__LINK_STATE_LINKWATCH_PENDING, + &dev->state)) { + /* We must not have linkwatch events + * pending on unregister. If this + * happens, we simply run the queue + * unscheduled, resulting in a noop + * for this device. + */ + linkwatch_run_queue(); + } + + __rtnl_unlock(); + + rebroadcast_time = jiffies; + } + + msleep(250); + + if (time_after(jiffies, warning_time + 10 * HZ)) { + printk(KERN_EMERG "unregister_netdevice: " + "waiting for %s to become free. Usage " + "count = %d\n", + dev->name, atomic_read(&dev->refcnt)); + warning_time = jiffies; + } + } +} + +/* The sequence is: + * + * rtnl_lock(); + * ... + * register_netdevice(x1); + * register_netdevice(x2); + * ... + * unregister_netdevice(y1); + * unregister_netdevice(y2); + * ... + * rtnl_unlock(); + * free_netdev(y1); + * free_netdev(y2); + * + * We are invoked by rtnl_unlock() after it drops the semaphore. + * This allows us to deal with problems: + * 1) We can delete sysfs objects which invoke hotplug + * without deadlocking with linkwatch via keventd. + * 2) Since we run with the RTNL semaphore not held, we can sleep + * safely in order to wait for the netdev refcnt to drop to zero. + */ +static DEFINE_MUTEX(net_todo_run_mutex); +void netdev_run_todo(void) +{ + struct list_head list; + + /* Need to guard against multiple cpu's getting out of order. */ + mutex_lock(&net_todo_run_mutex); + + /* Not safe to do outside the semaphore. We must not return + * until all unregister events invoked by the local processor + * have been completed (either by this todo run, or one on + * another cpu). + */ + if (list_empty(&net_todo_list)) + goto out; + + /* Snapshot list, allow later requests */ + spin_lock(&net_todo_list_lock); + list_replace_init(&net_todo_list, &list); + spin_unlock(&net_todo_list_lock); + + while (!list_empty(&list)) { + struct net_device *dev + = list_entry(list.next, struct net_device, todo_list); + list_del(&dev->todo_list); + + if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { + printk(KERN_ERR "network todo '%s' but state %d\n", + dev->name, dev->reg_state); + dump_stack(); + continue; + } + + dev->reg_state = NETREG_UNREGISTERED; + + netdev_wait_allrefs(dev); + + /* paranoia */ + BUG_ON(atomic_read(&dev->refcnt)); + BUG_TRAP(!dev->ip_ptr); + BUG_TRAP(!dev->ip6_ptr); + BUG_TRAP(!dev->dn_ptr); + + if (dev->destructor) + dev->destructor(dev); + + /* Free network device */ + kobject_put(&dev->dev.kobj); + } + +out: + mutex_unlock(&net_todo_run_mutex); +} + +static struct net_device_stats *internal_stats(struct net_device *dev) +{ + return &dev->stats; +} + +/** + * alloc_netdev - allocate network device + * @sizeof_priv: size of private data to allocate space for + * @name: device name format string + * @setup: callback to initialize device + * + * Allocates a struct net_device with private data area for driver use + * and performs basic initialization. + */ +struct net_device *alloc_netdev(int sizeof_priv, const char *name, + void (*setup)(struct net_device *)) +{ + void *p; + struct net_device *dev; + int alloc_size; + + BUG_ON(strlen(name) >= sizeof(dev->name)); + + /* ensure 32-byte alignment of both the device and private area */ + alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST; + alloc_size += sizeof_priv + NETDEV_ALIGN_CONST; + + p = kzalloc(alloc_size, GFP_KERNEL); + if (!p) { + printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n"); + return NULL; + } + + dev = (struct net_device *) + (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST); + dev->padded = (char *)dev - (char *)p; + dev->nd_net = &init_net; + + if (sizeof_priv) + dev->priv = netdev_priv(dev); + + dev->get_stats = internal_stats; + setup(dev); + strcpy(dev->name, name); + return dev; +} +EXPORT_SYMBOL(alloc_netdev); + +/** + * free_netdev - free network device + * @dev: device + * + * This function does the last stage of destroying an allocated device + * interface. The reference to the device object is released. + * If this is the last reference then it will be freed. + */ +void free_netdev(struct net_device *dev) +{ +#ifdef CONFIG_SYSFS + /* Compatibility with error handling in drivers */ + if (dev->reg_state == NETREG_UNINITIALIZED) { + kfree((char *)dev - dev->padded); + return; + } + + BUG_ON(dev->reg_state != NETREG_UNREGISTERED); + dev->reg_state = NETREG_RELEASED; + + /* will free via device release */ + put_device(&dev->dev); +#else + kfree((char *)dev - dev->padded); +#endif +} + +/* Synchronize with packet receive processing. */ +void synchronize_net(void) +{ + might_sleep(); + synchronize_rcu(); +} + +/** + * unregister_netdevice - remove device from the kernel + * @dev: device + * + * This function shuts down a device interface and removes it + * from the kernel tables. On success 0 is returned, on a failure + * a negative errno code is returned. + * + * Callers must hold the rtnl semaphore. You may want + * unregister_netdev() instead of this. + */ + +void unregister_netdevice(struct net_device *dev) +{ + BUG_ON(dev_boot_phase); + ASSERT_RTNL(); + + /* Some devices call without registering for initialization unwind. */ + if (dev->reg_state == NETREG_UNINITIALIZED) { + printk(KERN_DEBUG "unregister_netdevice: device %s/%p never " + "was registered\n", dev->name, dev); + + WARN_ON(1); + return; + } + + BUG_ON(dev->reg_state != NETREG_REGISTERED); + + /* If device is running, close it first. */ + if (dev->flags & IFF_UP) + dev_close(dev); + + /* And unlink it from device chain. */ + unlist_netdevice(dev); + + dev->reg_state = NETREG_UNREGISTERING; + + synchronize_net(); + + /* Shutdown queueing discipline. */ + dev_shutdown(dev); + + + /* Notify protocols, that we are about to destroy + this device. They should clean all the things. + */ + raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev); + + /* + * Flush the multicast chain + */ + dev_mc_discard(dev); + + if (dev->uninit) + dev->uninit(dev); + + /* Notifier chain MUST detach us from master device. */ + BUG_TRAP(!dev->master); + + /* Remove entries from sysfs */ + netdev_unregister_sysfs(dev); + + /* Finish processing unregister after unlock */ + net_set_todo(dev); + + synchronize_net(); + + dev_put(dev); +} + +/** + * unregister_netdev - remove device from the kernel + * @dev: device + * + * This function shuts down a device interface and removes it + * from the kernel tables. On success 0 is returned, on a failure + * a negative errno code is returned. + * + * This is just a wrapper for unregister_netdevice that takes + * the rtnl semaphore. In general you want to use this and not + * unregister_netdevice. + */ +void unregister_netdev(struct net_device *dev) +{ + rtnl_lock(); + unregister_netdevice(dev); + rtnl_unlock(); +} + +EXPORT_SYMBOL(unregister_netdev); + +/** + * dev_change_net_namespace - move device to different nethost namespace + * @dev: device + * @net: network namespace + * @pat: If not NULL name pattern to try if the current device name + * is already taken in the destination network namespace. + * + * This function shuts down a device interface and moves it + * to a new network namespace. On success 0 is returned, on + * a failure a netagive errno code is returned. + * + * Callers must hold the rtnl semaphore. + */ + +int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) +{ + char buf[IFNAMSIZ]; + const char *destname; + int err; + + ASSERT_RTNL(); + + /* Don't allow namespace local devices to be moved. */ + err = -EINVAL; + if (dev->features & NETIF_F_NETNS_LOCAL) + goto out; + + /* Ensure the device has been registrered */ + err = -EINVAL; + if (dev->reg_state != NETREG_REGISTERED) + goto out; + + /* Get out if there is nothing todo */ + err = 0; + if (dev->nd_net == net) + goto out; + + /* Pick the destination device name, and ensure + * we can use it in the destination network namespace. + */ + err = -EEXIST; + destname = dev->name; + if (__dev_get_by_name(net, destname)) { + /* We get here if we can't use the current device name */ + if (!pat) + goto out; + if (!dev_valid_name(pat)) + goto out; + if (strchr(pat, '%')) { + if (__dev_alloc_name(net, pat, buf) < 0) + goto out; + destname = buf; + } else + destname = pat; + if (__dev_get_by_name(net, destname)) + goto out; + } + + /* + * And now a mini version of register_netdevice unregister_netdevice. + */ + + /* If device is running close it first. */ + if (dev->flags & IFF_UP) + dev_close(dev); + + /* And unlink it from device chain */ + err = -ENODEV; + unlist_netdevice(dev); + + synchronize_net(); + + /* Shutdown queueing discipline. */ + dev_shutdown(dev); + + /* Notify protocols, that we are about to destroy + this device. They should clean all the things. + */ + call_netdevice_notifiers(NETDEV_UNREGISTER, dev); + + /* + * Flush the multicast chain + */ + dev_mc_discard(dev); + + /* Actually switch the network namespace */ + dev->nd_net = net; + + /* Assign the new device name */ + if (destname != dev->name) + strcpy(dev->name, destname); + + /* If there is an ifindex conflict assign a new one */ + if (__dev_get_by_index(net, dev->ifindex)) { + int iflink = (dev->iflink == dev->ifindex); + dev->ifindex = dev_new_index(net); + if (iflink) + dev->iflink = dev->ifindex; + } + + /* Fixup sysfs */ + err = device_rename(&dev->dev, dev->name); + BUG_ON(err); + + /* Add the device back in the hashes */ + list_netdevice(dev); + + /* Notify protocols, that a new device appeared. */ + call_netdevice_notifiers(NETDEV_REGISTER, dev); + + synchronize_net(); + err = 0; +out: + return err; +} + +static int dev_cpu_callback(struct notifier_block *nfb, + unsigned long action, + void *ocpu) +{ + struct sk_buff **list_skb; + struct net_device **list_net; + struct sk_buff *skb; + unsigned int cpu, oldcpu = (unsigned long)ocpu; + struct softnet_data *sd, *oldsd; + + if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) + return NOTIFY_OK; + + local_irq_disable(); + cpu = smp_processor_id(); + sd = &per_cpu(softnet_data, cpu); + oldsd = &per_cpu(softnet_data, oldcpu); + + /* Find end of our completion_queue. */ + list_skb = &sd->completion_queue; + while (*list_skb) + list_skb = &(*list_skb)->next; + /* Append completion queue from offline CPU. */ + *list_skb = oldsd->completion_queue; + oldsd->completion_queue = NULL; + + /* Find end of our output_queue. */ + list_net = &sd->output_queue; + while (*list_net) + list_net = &(*list_net)->next_sched; + /* Append output queue from offline CPU. */ + *list_net = oldsd->output_queue; + oldsd->output_queue = NULL; + + raise_softirq_irqoff(NET_TX_SOFTIRQ); + local_irq_enable(); + + /* Process offline CPU's input_pkt_queue */ + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) + netif_rx(skb); + + return NOTIFY_OK; +} + +#ifdef CONFIG_NET_DMA +/** + * net_dma_rebalance - + * This is called when the number of channels allocated to the net_dma_client + * changes. The net_dma_client tries to have one DMA channel per CPU. + */ + +static void net_dma_rebalance(struct net_dma *net_dma) +{ + unsigned int cpu, i, n, chan_idx; + struct dma_chan *chan; + + if (cpus_empty(net_dma->channel_mask)) { + for_each_online_cpu(cpu) + rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL); + return; + } + + i = 0; + cpu = first_cpu(cpu_online_map); + + for_each_cpu_mask(chan_idx, net_dma->channel_mask) { + chan = net_dma->channels[chan_idx]; + + n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask)) + + (i < (num_online_cpus() % + cpus_weight(net_dma->channel_mask)) ? 1 : 0)); + + while(n) { + per_cpu(softnet_data, cpu).net_dma = chan; + cpu = next_cpu(cpu, cpu_online_map); + n--; + } + i++; + } +} + +/** + * netdev_dma_event - event callback for the net_dma_client + * @client: should always be net_dma_client + * @chan: DMA channel for the event + * @event: event type + */ +static enum dma_state_client +netdev_dma_event(struct dma_client *client, struct dma_chan *chan, + enum dma_state state) +{ + int i, found = 0, pos = -1; + struct net_dma *net_dma = + container_of(client, struct net_dma, client); + enum dma_state_client ack = DMA_DUP; /* default: take no action */ + + spin_lock(&net_dma->lock); + switch (state) { + case DMA_RESOURCE_AVAILABLE: + for (i = 0; i < NR_CPUS; i++) + if (net_dma->channels[i] == chan) { + found = 1; + break; + } else if (net_dma->channels[i] == NULL && pos < 0) + pos = i; + + if (!found && pos >= 0) { + ack = DMA_ACK; + net_dma->channels[pos] = chan; + cpu_set(pos, net_dma->channel_mask); + net_dma_rebalance(net_dma); + } + break; + case DMA_RESOURCE_REMOVED: + for (i = 0; i < NR_CPUS; i++) + if (net_dma->channels[i] == chan) { + found = 1; + pos = i; + break; + } + + if (found) { + ack = DMA_ACK; + cpu_clear(pos, net_dma->channel_mask); + net_dma->channels[i] = NULL; + net_dma_rebalance(net_dma); + } + break; + default: + break; + } + spin_unlock(&net_dma->lock); + + return ack; +} + +/** + * netdev_dma_regiser - register the networking subsystem as a DMA client + */ +static int __init netdev_dma_register(void) +{ + spin_lock_init(&net_dma.lock); + dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask); + dma_async_client_register(&net_dma.client); + dma_async_client_chan_request(&net_dma.client); + return 0; +} + +#else +static int __init netdev_dma_register(void) { return -ENODEV; } +#endif /* CONFIG_NET_DMA */ + +/** + * netdev_compute_feature - compute conjunction of two feature sets + * @all: first feature set + * @one: second feature set + * + * Computes a new feature set after adding a device with feature set + * @one to the master device with current feature set @all. Returns + * the new feature set. + */ +int netdev_compute_features(unsigned long all, unsigned long one) +{ + /* if device needs checksumming, downgrade to hw checksumming */ + if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM)) + all ^= NETIF_F_NO_CSUM | NETIF_F_HW_CSUM; + + /* if device can't do all checksum, downgrade to ipv4 */ + if (all & NETIF_F_HW_CSUM && !(one & NETIF_F_HW_CSUM)) + all ^= NETIF_F_HW_CSUM | NETIF_F_IP_CSUM; + + if (one & NETIF_F_GSO) + one |= NETIF_F_GSO_SOFTWARE; + one |= NETIF_F_GSO; + + /* If even one device supports robust GSO, enable it for all. */ + if (one & NETIF_F_GSO_ROBUST) + all |= NETIF_F_GSO_ROBUST; + + all &= one | NETIF_F_LLTX; + + if (!(all & NETIF_F_ALL_CSUM)) + all &= ~NETIF_F_SG; + if (!(all & NETIF_F_SG)) + all &= ~NETIF_F_GSO_MASK; + + return all; +} +EXPORT_SYMBOL(netdev_compute_features); + +/* Initialize per network namespace state */ +static int netdev_init(struct net *net) +{ + int i; + INIT_LIST_HEAD(&net->dev_base_head); + rwlock_init(&dev_base_lock); + + net->dev_name_head = kmalloc( + sizeof(*net->dev_name_head)*NETDEV_HASHENTRIES, GFP_KERNEL); + if (!net->dev_name_head) + return -ENOMEM; + + net->dev_index_head = kmalloc( + sizeof(*net->dev_index_head)*NETDEV_HASHENTRIES, GFP_KERNEL); + if (!net->dev_index_head) { + kfree(net->dev_name_head); + return -ENOMEM; + } + + for (i = 0; i < NETDEV_HASHENTRIES; i++) + INIT_HLIST_HEAD(&net->dev_name_head[i]); + + for (i = 0; i < NETDEV_HASHENTRIES; i++) + INIT_HLIST_HEAD(&net->dev_index_head[i]); + + return 0; +} + +static void netdev_exit(struct net *net) +{ + kfree(net->dev_name_head); + kfree(net->dev_index_head); +} + +static struct pernet_operations netdev_net_ops = { + .init = netdev_init, + .exit = netdev_exit, +}; + +static void default_device_exit(struct net *net) +{ + struct net_device *dev, *next; + /* + * Push all migratable of the network devices back to the + * initial network namespace + */ + rtnl_lock(); + for_each_netdev_safe(net, dev, next) { + int err; + + /* Ignore unmoveable devices (i.e. loopback) */ + if (dev->features & NETIF_F_NETNS_LOCAL) + continue; + + /* Push remaing network devices to init_net */ + err = dev_change_net_namespace(dev, &init_net, "dev%d"); + if (err) { + printk(KERN_WARNING "%s: failed to move %s to init_net: %d\n", + __func__, dev->name, err); + unregister_netdevice(dev); + } + } + rtnl_unlock(); +} + +static struct pernet_operations default_device_ops = { + .exit = default_device_exit, +}; + +/* + * Initialize the DEV module. At boot time this walks the device list and + * unhooks any devices that fail to initialise (normally hardware not + * present) and leaves us with a valid list of present and active devices. + * + */ + +/* + * This is called single threaded during boot, so no need + * to take the rtnl semaphore. + */ +static int __init net_dev_init(void) +{ + int i, rc = -ENOMEM; + + BUG_ON(!dev_boot_phase); + + if (dev_proc_init()) + goto out; + + if (netdev_sysfs_init()) + goto out; + + INIT_LIST_HEAD(&ptype_all); + for (i = 0; i < 16; i++) + INIT_LIST_HEAD(&ptype_base[i]); + + if (register_pernet_subsys(&netdev_net_ops)) + goto out; + + if (register_pernet_device(&default_device_ops)) + goto out; + + /* + * Initialise the packet receive queues. + */ + + for_each_possible_cpu(i) { + struct softnet_data *queue; + + queue = &per_cpu(softnet_data, i); + skb_queue_head_init(&queue->input_pkt_queue); + queue->completion_queue = NULL; + INIT_LIST_HEAD(&queue->poll_list); + set_bit(__LINK_STATE_START, &queue->backlog_dev.state); + queue->backlog_dev.weight = weight_p; + queue->backlog_dev.poll = process_backlog; + atomic_set(&queue->backlog_dev.refcnt, 1); + } + + netdev_dma_register(); + + dev_boot_phase = 0; + + open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL); + open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL); + + hotcpu_notifier(dev_cpu_callback, 0); + dst_init(); + dev_mcast_init(); + rc = 0; +out: + return rc; +} + +subsys_initcall(net_dev_init); + +EXPORT_SYMBOL(__dev_get_by_index); +EXPORT_SYMBOL(__dev_get_by_name); +EXPORT_SYMBOL(__dev_remove_pack); +EXPORT_SYMBOL(dev_valid_name); +EXPORT_SYMBOL(dev_add_pack); +EXPORT_SYMBOL(dev_alloc_name); +EXPORT_SYMBOL(dev_close); +EXPORT_SYMBOL(dev_get_by_flags); +EXPORT_SYMBOL(dev_get_by_index); +EXPORT_SYMBOL(dev_get_by_name); +EXPORT_SYMBOL(dev_open); +EXPORT_SYMBOL(dev_queue_xmit); +EXPORT_SYMBOL(dev_remove_pack); +EXPORT_SYMBOL(dev_set_allmulti); +EXPORT_SYMBOL(dev_set_promiscuity); +EXPORT_SYMBOL(dev_change_flags); +EXPORT_SYMBOL(dev_set_mtu); +EXPORT_SYMBOL(dev_set_mac_address); +EXPORT_SYMBOL(free_netdev); +EXPORT_SYMBOL(netdev_boot_setup_check); +EXPORT_SYMBOL(netdev_set_master); +EXPORT_SYMBOL(netdev_state_change); +EXPORT_SYMBOL(netif_receive_skb); +EXPORT_SYMBOL(netif_rx); +EXPORT_SYMBOL(register_gifconf); +EXPORT_SYMBOL(register_netdevice); +EXPORT_SYMBOL(register_netdevice_notifier); +EXPORT_SYMBOL(skb_checksum_help); +EXPORT_SYMBOL(synchronize_net); +EXPORT_SYMBOL(unregister_netdevice); +EXPORT_SYMBOL(unregister_netdevice_notifier); +EXPORT_SYMBOL(net_enable_timestamp); +EXPORT_SYMBOL(net_disable_timestamp); +EXPORT_SYMBOL(dev_get_flags); + +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE) +EXPORT_SYMBOL(br_handle_frame_hook); +EXPORT_SYMBOL(br_fdb_get_hook); +EXPORT_SYMBOL(br_fdb_put_hook); +#endif + +#ifdef CONFIG_KMOD +EXPORT_SYMBOL(dev_load); +#endif + +EXPORT_PER_CPU_SYMBOL(softnet_data); diff -Nurb linux-2.6.22-594/net/core/net_namespace.c linux-2.6.22-595/net/core/net_namespace.c --- linux-2.6.22-594/net/core/net_namespace.c 2008-03-20 01:27:51.000000000 -0400 +++ linux-2.6.22-595/net/core/net_namespace.c 2008-03-20 01:28:00.000000000 -0400 @@ -112,10 +112,12 @@ ops = list_entry(ptr, struct pernet_operations, list); if (ops->init) { error = ops->init(net); - if (error < 0) + if (error < 0) { + printk(KERN_ALERT "Error setting up netns: %x\n", ops->init); goto out_undo; } } + } out: return error; out_undo: diff -Nurb linux-2.6.22-594/net/ipv4/af_inet.c.orig linux-2.6.22-595/net/ipv4/af_inet.c.orig --- linux-2.6.22-594/net/ipv4/af_inet.c.orig 2008-03-20 01:27:51.000000000 -0400 +++ linux-2.6.22-595/net/ipv4/af_inet.c.orig 1969-12-31 19:00:00.000000000 -0500 @@ -1,1522 +0,0 @@ -/* - * INET An implementation of the TCP/IP protocol suite for the LINUX - * operating system. INET is implemented using the BSD Socket - * interface as the means of communication with the user level. - * - * PF_INET protocol family socket handler. - * - * Version: $Id: af_inet.c,v 1.137 2002/02/01 22:01:03 davem Exp $ - * - * Authors: Ross Biro - * Fred N. van Kempen, - * Florian La Roche, - * Alan Cox, - * - * Changes (see also sock.c) - * - * piggy, - * Karl Knutson : Socket protocol table - * A.N.Kuznetsov : Socket death error in accept(). - * John Richardson : Fix non blocking error in connect() - * so sockets that fail to connect - * don't return -EINPROGRESS. - * Alan Cox : Asynchronous I/O support - * Alan Cox : Keep correct socket pointer on sock - * structures - * when accept() ed - * Alan Cox : Semantics of SO_LINGER aren't state - * moved to close when you look carefully. - * With this fixed and the accept bug fixed - * some RPC stuff seems happier. - * Niibe Yutaka : 4.4BSD style write async I/O - * Alan Cox, - * Tony Gale : Fixed reuse semantics. - * Alan Cox : bind() shouldn't abort existing but dead - * sockets. Stops FTP netin:.. I hope. - * Alan Cox : bind() works correctly for RAW sockets. - * Note that FreeBSD at least was broken - * in this respect so be careful with - * compatibility tests... - * Alan Cox : routing cache support - * Alan Cox : memzero the socket structure for - * compactness. - * Matt Day : nonblock connect error handler - * Alan Cox : Allow large numbers of pending sockets - * (eg for big web sites), but only if - * specifically application requested. - * Alan Cox : New buffering throughout IP. Used - * dumbly. - * Alan Cox : New buffering now used smartly. - * Alan Cox : BSD rather than common sense - * interpretation of listen. - * Germano Caronni : Assorted small races. - * Alan Cox : sendmsg/recvmsg basic support. - * Alan Cox : Only sendmsg/recvmsg now supported. - * Alan Cox : Locked down bind (see security list). - * Alan Cox : Loosened bind a little. - * Mike McLagan : ADD/DEL DLCI Ioctls - * Willy Konynenberg : Transparent proxying support. - * David S. Miller : New socket lookup architecture. - * Some other random speedups. - * Cyrus Durgin : Cleaned up file for kmod hacks. - * Andi Kleen : Fix inet_stream_connect TCP race. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version - * 2 of the License, or (at your option) any later version. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#ifdef CONFIG_IP_MROUTE -#include -#endif -#include - -DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly; - -extern void ip_mc_drop_socket(struct sock *sk); - -/* The inetsw table contains everything that inet_create needs to - * build a new socket. - */ -static struct list_head inetsw[SOCK_MAX]; -static DEFINE_SPINLOCK(inetsw_lock); - -/* New destruction routine */ - -void inet_sock_destruct(struct sock *sk) -{ - struct inet_sock *inet = inet_sk(sk); - - __skb_queue_purge(&sk->sk_receive_queue); - __skb_queue_purge(&sk->sk_error_queue); - - if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) { - printk("Attempt to release TCP socket in state %d %p\n", - sk->sk_state, sk); - return; - } - if (!sock_flag(sk, SOCK_DEAD)) { - printk("Attempt to release alive inet socket %p\n", sk); - return; - } - - BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc)); - BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc)); - BUG_TRAP(!sk->sk_wmem_queued); - BUG_TRAP(!sk->sk_forward_alloc); - - kfree(inet->opt); - dst_release(sk->sk_dst_cache); - sk_refcnt_debug_dec(sk); -} - -/* - * The routines beyond this point handle the behaviour of an AF_INET - * socket object. Mostly it punts to the subprotocols of IP to do - * the work. - */ - -/* - * Automatically bind an unbound socket. - */ - -static int inet_autobind(struct sock *sk) -{ - struct inet_sock *inet; - /* We may need to bind the socket. */ - lock_sock(sk); - inet = inet_sk(sk); - if (!inet->num) { - if (sk->sk_prot->get_port(sk, 0)) { - release_sock(sk); - return -EAGAIN; - } - inet->sport = htons(inet->num); - sk->sk_xid = vx_current_xid(); - sk->sk_nid = nx_current_nid(); - } - release_sock(sk); - return 0; -} - -/* - * Move a socket into listening state. - */ -int inet_listen(struct socket *sock, int backlog) -{ - struct sock *sk = sock->sk; - unsigned char old_state; - int err; - - lock_sock(sk); - - err = -EINVAL; - if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM) - goto out; - - old_state = sk->sk_state; - if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN))) - goto out; - - /* Really, if the socket is already in listen state - * we can only allow the backlog to be adjusted. - */ - if (old_state != TCP_LISTEN) { - err = inet_csk_listen_start(sk, backlog); - if (err) - goto out; - } - sk->sk_max_ack_backlog = backlog; - err = 0; - -out: - release_sock(sk); - return err; -} - -u32 inet_ehash_secret __read_mostly; -EXPORT_SYMBOL(inet_ehash_secret); - -/* - * inet_ehash_secret must be set exactly once - * Instead of using a dedicated spinlock, we (ab)use inetsw_lock - */ -void build_ehash_secret(void) -{ - u32 rnd; - do { - get_random_bytes(&rnd, sizeof(rnd)); - } while (rnd == 0); - spin_lock_bh(&inetsw_lock); - if (!inet_ehash_secret) - inet_ehash_secret = rnd; - spin_unlock_bh(&inetsw_lock); -} -EXPORT_SYMBOL(build_ehash_secret); - -/* - * Create an inet socket. - */ - -static int inet_create(struct socket *sock, int protocol) -{ - struct sock *sk; - struct list_head *p; - struct inet_protosw *answer; - struct inet_sock *inet; - struct proto *answer_prot; - unsigned char answer_flags; - char answer_no_check; - int try_loading_module = 0; - int err; - - if (sock->type != SOCK_RAW && - sock->type != SOCK_DGRAM && - !inet_ehash_secret) - build_ehash_secret(); - - sock->state = SS_UNCONNECTED; - - /* Look for the requested type/protocol pair. */ - answer = NULL; -lookup_protocol: - err = -ESOCKTNOSUPPORT; - rcu_read_lock(); - list_for_each_rcu(p, &inetsw[sock->type]) { - answer = list_entry(p, struct inet_protosw, list); - - /* Check the non-wild match. */ - if (protocol == answer->protocol) { - if (protocol != IPPROTO_IP) - break; - } else { - /* Check for the two wild cases. */ - if (IPPROTO_IP == protocol) { - protocol = answer->protocol; - break; - } - if (IPPROTO_IP == answer->protocol) - break; - } - err = -EPROTONOSUPPORT; - answer = NULL; - } - - if (unlikely(answer == NULL)) { - if (try_loading_module < 2) { - rcu_read_unlock(); - /* - * Be more specific, e.g. net-pf-2-proto-132-type-1 - * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM) - */ - if (++try_loading_module == 1) - request_module("net-pf-%d-proto-%d-type-%d", - PF_INET, protocol, sock->type); - /* - * Fall back to generic, e.g. net-pf-2-proto-132 - * (net-pf-PF_INET-proto-IPPROTO_SCTP) - */ - else - request_module("net-pf-%d-proto-%d", - PF_INET, protocol); - goto lookup_protocol; - } else - goto out_rcu_unlock; - } - - err = -EPERM; - if ((protocol == IPPROTO_ICMP) && - nx_capable(answer->capability, NXC_RAW_ICMP)) - goto override; - if (sock->type == SOCK_RAW && - nx_capable(answer->capability, NXC_RAW_SOCKET)) - goto override; - if (answer->capability > 0 && !capable(answer->capability)) - goto out_rcu_unlock; -override: - sock->ops = answer->ops; - answer_prot = answer->prot; - answer_no_check = answer->no_check; - answer_flags = answer->flags; - rcu_read_unlock(); - - BUG_TRAP(answer_prot->slab != NULL); - - err = -ENOBUFS; - sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1); - if (sk == NULL) - goto out; - - err = 0; - sk->sk_no_check = answer_no_check; - if (INET_PROTOSW_REUSE & answer_flags) - sk->sk_reuse = 1; - - inet = inet_sk(sk); - inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0; - - if (SOCK_RAW == sock->type) { - inet->num = protocol; - if (IPPROTO_RAW == protocol) - inet->hdrincl = 1; - } - - if (ipv4_config.no_pmtu_disc) - inet->pmtudisc = IP_PMTUDISC_DONT; - else - inet->pmtudisc = IP_PMTUDISC_WANT; - - inet->id = 0; - - sock_init_data(sock, sk); - - sk->sk_destruct = inet_sock_destruct; - sk->sk_family = PF_INET; - sk->sk_protocol = protocol; - sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; - - inet->uc_ttl = -1; - inet->mc_loop = 1; - inet->mc_ttl = 1; - inet->mc_index = 0; - inet->mc_list = NULL; - - sk_refcnt_debug_inc(sk); - - if (inet->num) { - /* It assumes that any protocol which allows - * the user to assign a number at socket - * creation time automatically - * shares. - */ - inet->sport = htons(inet->num); - /* Add to protocol hash chains. */ - sk->sk_prot->hash(sk); - } - - if (sk->sk_prot->init) { - err = sk->sk_prot->init(sk); - if (err) - sk_common_release(sk); - } -out: - return err; -out_rcu_unlock: - rcu_read_unlock(); - goto out; -} - - -/* - * The peer socket should always be NULL (or else). When we call this - * function we are destroying the object and from then on nobody - * should refer to it. - */ -int inet_release(struct socket *sock) -{ - struct sock *sk = sock->sk; - - if (sk) { - long timeout; - - /* Applications forget to leave groups before exiting */ - ip_mc_drop_socket(sk); - - /* If linger is set, we don't return until the close - * is complete. Otherwise we return immediately. The - * actually closing is done the same either way. - * - * If the close is due to the process exiting, we never - * linger.. - */ - timeout = 0; - if (sock_flag(sk, SOCK_LINGER) && - !(current->flags & PF_EXITING)) - timeout = sk->sk_lingertime; - sock->sk = NULL; - sk->sk_prot->close(sk, timeout); - } - return 0; -} - -/* It is off by default, see below. */ -int sysctl_ip_nonlocal_bind __read_mostly; - -int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) -{ - struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; - struct sock *sk = sock->sk; - struct inet_sock *inet = inet_sk(sk); - struct nx_v4_sock_addr nsa; - unsigned short snum; - int chk_addr_ret; - int err; - - /* If the socket has its own bind function then use it. (RAW) */ - if (sk->sk_prot->bind) { - err = sk->sk_prot->bind(sk, uaddr, addr_len); - goto out; - } - err = -EINVAL; - if (addr_len < sizeof(struct sockaddr_in)) - goto out; - - err = v4_map_sock_addr(inet, addr, &nsa); - if (err) - goto out; - - chk_addr_ret = inet_addr_type(nsa.saddr); - - /* Not specified by any standard per-se, however it breaks too - * many applications when removed. It is unfortunate since - * allowing applications to make a non-local bind solves - * several problems with systems using dynamic addressing. - * (ie. your servers still start up even if your ISDN link - * is temporarily down) - */ - err = -EADDRNOTAVAIL; - if (!sysctl_ip_nonlocal_bind && - !inet->freebind && - nsa.saddr != INADDR_ANY && - chk_addr_ret != RTN_LOCAL && - chk_addr_ret != RTN_MULTICAST && - chk_addr_ret != RTN_BROADCAST) - goto out; - - snum = ntohs(addr->sin_port); - err = -EACCES; - if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) - goto out; - - /* We keep a pair of addresses. rcv_saddr is the one - * used by hash lookups, and saddr is used for transmit. - * - * In the BSD API these are the same except where it - * would be illegal to use them (multicast/broadcast) in - * which case the sending device address is used. - */ - lock_sock(sk); - - /* Check these errors (active socket, double bind). */ - err = -EINVAL; - if (sk->sk_state != TCP_CLOSE || inet->num) - goto out_release_sock; - - v4_set_sock_addr(inet, &nsa); - if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) - inet->saddr = 0; /* Use device */ - - /* Make sure we are allowed to bind here. */ - if (sk->sk_prot->get_port(sk, snum)) { - inet->saddr = inet->rcv_saddr = 0; - err = -EADDRINUSE; - goto out_release_sock; - } - - if (inet->rcv_saddr) - sk->sk_userlocks |= SOCK_BINDADDR_LOCK; - if (snum) - sk->sk_userlocks |= SOCK_BINDPORT_LOCK; - inet->sport = htons(inet->num); - inet->daddr = 0; - inet->dport = 0; - sk_dst_reset(sk); - err = 0; -out_release_sock: - release_sock(sk); -out: - return err; -} - -int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr, - int addr_len, int flags) -{ - struct sock *sk = sock->sk; - - if (uaddr->sa_family == AF_UNSPEC) - return sk->sk_prot->disconnect(sk, flags); - - if (!inet_sk(sk)->num && inet_autobind(sk)) - return -EAGAIN; - return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len); -} - -static long inet_wait_for_connect(struct sock *sk, long timeo) -{ - DEFINE_WAIT(wait); - - prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); - - /* Basic assumption: if someone sets sk->sk_err, he _must_ - * change state of the socket from TCP_SYN_*. - * Connect() does not allow to get error notifications - * without closing the socket. - */ - while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { - release_sock(sk); - timeo = schedule_timeout(timeo); - lock_sock(sk); - if (signal_pending(current) || !timeo) - break; - prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); - } - finish_wait(sk->sk_sleep, &wait); - return timeo; -} - -/* - * Connect to a remote host. There is regrettably still a little - * TCP 'magic' in here. - */ -int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr, - int addr_len, int flags) -{ - struct sock *sk = sock->sk; - int err; - long timeo; - - lock_sock(sk); - - if (uaddr->sa_family == AF_UNSPEC) { - err = sk->sk_prot->disconnect(sk, flags); - sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; - goto out; - } - - switch (sock->state) { - default: - err = -EINVAL; - goto out; - case SS_CONNECTED: - err = -EISCONN; - goto out; - case SS_CONNECTING: - err = -EALREADY; - /* Fall out of switch with err, set for this state */ - break; - case SS_UNCONNECTED: - err = -EISCONN; - if (sk->sk_state != TCP_CLOSE) - goto out; - - err = sk->sk_prot->connect(sk, uaddr, addr_len); - if (err < 0) - goto out; - - sock->state = SS_CONNECTING; - - /* Just entered SS_CONNECTING state; the only - * difference is that return value in non-blocking - * case is EINPROGRESS, rather than EALREADY. - */ - err = -EINPROGRESS; - break; - } - - timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); - - if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { - /* Error code is set above */ - if (!timeo || !inet_wait_for_connect(sk, timeo)) - goto out; - - err = sock_intr_errno(timeo); - if (signal_pending(current)) - goto out; - } - - /* Connection was closed by RST, timeout, ICMP error - * or another process disconnected us. - */ - if (sk->sk_state == TCP_CLOSE) - goto sock_error; - - /* sk->sk_err may be not zero now, if RECVERR was ordered by user - * and error was received after socket entered established state. - * Hence, it is handled normally after connect() return successfully. - */ - - sock->state = SS_CONNECTED; - err = 0; -out: - release_sock(sk); - return err; - -sock_error: - err = sock_error(sk) ? : -ECONNABORTED; - sock->state = SS_UNCONNECTED; - if (sk->sk_prot->disconnect(sk, flags)) - sock->state = SS_DISCONNECTING; - goto out; -} - -/* - * Accept a pending connection. The TCP layer now gives BSD semantics. - */ - -int inet_accept(struct socket *sock, struct socket *newsock, int flags) -{ - struct sock *sk1 = sock->sk; - int err = -EINVAL; - struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err); - - if (!sk2) - goto do_err; - - lock_sock(sk2); - - BUG_TRAP((1 << sk2->sk_state) & - (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)); - - sock_graft(sk2, newsock); - - newsock->state = SS_CONNECTED; - err = 0; - release_sock(sk2); -do_err: - return err; -} - - -/* - * This does both peername and sockname. - */ -int inet_getname(struct socket *sock, struct sockaddr *uaddr, - int *uaddr_len, int peer) -{ - struct sock *sk = sock->sk; - struct inet_sock *inet = inet_sk(sk); - struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; - - sin->sin_family = AF_INET; - if (peer) { - if (!inet->dport || - (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && - peer == 1)) - return -ENOTCONN; - sin->sin_port = inet->dport; - sin->sin_addr.s_addr = - nx_map_sock_lback(sk->sk_nx_info, inet->daddr); - } else { - __be32 addr = inet->rcv_saddr; - if (!addr) - addr = inet->saddr; - addr = nx_map_sock_lback(sk->sk_nx_info, addr); - sin->sin_port = inet->sport; - sin->sin_addr.s_addr = addr; - } - memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); - *uaddr_len = sizeof(*sin); - return 0; -} - -int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, - size_t size) -{ - struct sock *sk = sock->sk; - - /* We may need to bind the socket. */ - if (!inet_sk(sk)->num && inet_autobind(sk)) - return -EAGAIN; - - return sk->sk_prot->sendmsg(iocb, sk, msg, size); -} - - -static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags) -{ - struct sock *sk = sock->sk; - - /* We may need to bind the socket. */ - if (!inet_sk(sk)->num && inet_autobind(sk)) - return -EAGAIN; - - if (sk->sk_prot->sendpage) - return sk->sk_prot->sendpage(sk, page, offset, size, flags); - return sock_no_sendpage(sock, page, offset, size, flags); -} - - -int inet_shutdown(struct socket *sock, int how) -{ - struct sock *sk = sock->sk; - int err = 0; - - /* This should really check to make sure - * the socket is a TCP socket. (WHY AC...) - */ - how++; /* maps 0->1 has the advantage of making bit 1 rcvs and - 1->2 bit 2 snds. - 2->3 */ - if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */ - return -EINVAL; - - lock_sock(sk); - if (sock->state == SS_CONNECTING) { - if ((1 << sk->sk_state) & - (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE)) - sock->state = SS_DISCONNECTING; - else - sock->state = SS_CONNECTED; - } - - switch (sk->sk_state) { - case TCP_CLOSE: - err = -ENOTCONN; - /* Hack to wake up other listeners, who can poll for - POLLHUP, even on eg. unconnected UDP sockets -- RR */ - default: - sk->sk_shutdown |= how; - if (sk->sk_prot->shutdown) - sk->sk_prot->shutdown(sk, how); - break; - - /* Remaining two branches are temporary solution for missing - * close() in multithreaded environment. It is _not_ a good idea, - * but we have no choice until close() is repaired at VFS level. - */ - case TCP_LISTEN: - if (!(how & RCV_SHUTDOWN)) - break; - /* Fall through */ - case TCP_SYN_SENT: - err = sk->sk_prot->disconnect(sk, O_NONBLOCK); - sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED; - break; - } - - /* Wake up anyone sleeping in poll. */ - sk->sk_state_change(sk); - release_sock(sk); - return err; -} - -/* - * ioctl() calls you can issue on an INET socket. Most of these are - * device configuration and stuff and very rarely used. Some ioctls - * pass on to the socket itself. - * - * NOTE: I like the idea of a module for the config stuff. ie ifconfig - * loads the devconfigure module does its configuring and unloads it. - * There's a good 20K of config code hanging around the kernel. - */ - -int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) -{ - struct sock *sk = sock->sk; - int err = 0; - - switch (cmd) { - case SIOCGSTAMP: - err = sock_get_timestamp(sk, (struct timeval __user *)arg); - break; - case SIOCGSTAMPNS: - err = sock_get_timestampns(sk, (struct timespec __user *)arg); - break; - case SIOCADDRT: - case SIOCDELRT: - case SIOCRTMSG: - err = ip_rt_ioctl(cmd, (void __user *)arg); - break; - case SIOCDARP: - case SIOCGARP: - case SIOCSARP: - err = arp_ioctl(cmd, (void __user *)arg); - break; - case SIOCGIFADDR: - case SIOCSIFADDR: - case SIOCGIFBRDADDR: - case SIOCSIFBRDADDR: - case SIOCGIFNETMASK: - case SIOCSIFNETMASK: - case SIOCGIFDSTADDR: - case SIOCSIFDSTADDR: - case SIOCSIFPFLAGS: - case SIOCGIFPFLAGS: - case SIOCSIFFLAGS: - err = devinet_ioctl(cmd, (void __user *)arg); - break; - default: - if (sk->sk_prot->ioctl) - err = sk->sk_prot->ioctl(sk, cmd, arg); - else - err = -ENOIOCTLCMD; - break; - } - return err; -} - -const struct proto_ops inet_stream_ops = { - .family = PF_INET, - .owner = THIS_MODULE, - .release = inet_release, - .bind = inet_bind, - .connect = inet_stream_connect, - .socketpair = sock_no_socketpair, - .accept = inet_accept, - .getname = inet_getname, - .poll = tcp_poll, - .ioctl = inet_ioctl, - .listen = inet_listen, - .shutdown = inet_shutdown, - .setsockopt = sock_common_setsockopt, - .getsockopt = sock_common_getsockopt, - .sendmsg = tcp_sendmsg, - .recvmsg = sock_common_recvmsg, - .mmap = sock_no_mmap, - .sendpage = tcp_sendpage, -#ifdef CONFIG_COMPAT - .compat_setsockopt = compat_sock_common_setsockopt, - .compat_getsockopt = compat_sock_common_getsockopt, -#endif -}; - -const struct proto_ops inet_dgram_ops = { - .family = PF_INET, - .owner = THIS_MODULE, - .release = inet_release, - .bind = inet_bind, - .connect = inet_dgram_connect, - .socketpair = sock_no_socketpair, - .accept = sock_no_accept, - .getname = inet_getname, - .poll = udp_poll, - .ioctl = inet_ioctl, - .listen = sock_no_listen, - .shutdown = inet_shutdown, - .setsockopt = sock_common_setsockopt, - .getsockopt = sock_common_getsockopt, - .sendmsg = inet_sendmsg, - .recvmsg = sock_common_recvmsg, - .mmap = sock_no_mmap, - .sendpage = inet_sendpage, -#ifdef CONFIG_COMPAT - .compat_setsockopt = compat_sock_common_setsockopt, - .compat_getsockopt = compat_sock_common_getsockopt, -#endif -}; - -/* - * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without - * udp_poll - */ -static const struct proto_ops inet_sockraw_ops = { - .family = PF_INET, - .owner = THIS_MODULE, - .release = inet_release, - .bind = inet_bind, - .connect = inet_dgram_connect, - .socketpair = sock_no_socketpair, - .accept = sock_no_accept, - .getname = inet_getname, - .poll = datagram_poll, - .ioctl = inet_ioctl, - .listen = sock_no_listen, - .shutdown = inet_shutdown, - .setsockopt = sock_common_setsockopt, - .getsockopt = sock_common_getsockopt, - .sendmsg = inet_sendmsg, - .recvmsg = sock_common_recvmsg, - .mmap = sock_no_mmap, - .sendpage = inet_sendpage, -#ifdef CONFIG_COMPAT - .compat_setsockopt = compat_sock_common_setsockopt, - .compat_getsockopt = compat_sock_common_getsockopt, -#endif -}; - -static struct net_proto_family inet_family_ops = { - .family = PF_INET, - .create = inet_create, - .owner = THIS_MODULE, -}; - -/* Upon startup we insert all the elements in inetsw_array[] into - * the linked list inetsw. - */ -static struct inet_protosw inetsw_array[] = -{ - { - .type = SOCK_STREAM, - .protocol = IPPROTO_TCP, - .prot = &tcp_prot, - .ops = &inet_stream_ops, - .capability = -1, - .no_check = 0, - .flags = INET_PROTOSW_PERMANENT | - INET_PROTOSW_ICSK, - }, - - { - .type = SOCK_DGRAM, - .protocol = IPPROTO_UDP, - .prot = &udp_prot, - .ops = &inet_dgram_ops, - .capability = -1, - .no_check = UDP_CSUM_DEFAULT, - .flags = INET_PROTOSW_PERMANENT, - }, - - - { - .type = SOCK_RAW, - .protocol = IPPROTO_IP, /* wild card */ - .prot = &raw_prot, - .ops = &inet_sockraw_ops, - .capability = CAP_NET_RAW, - .no_check = UDP_CSUM_DEFAULT, - .flags = INET_PROTOSW_REUSE, - } -}; - -#define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw)) - -void inet_register_protosw(struct inet_protosw *p) -{ - struct list_head *lh; - struct inet_protosw *answer; - int protocol = p->protocol; - struct list_head *last_perm; - - spin_lock_bh(&inetsw_lock); - - if (p->type >= SOCK_MAX) - goto out_illegal; - - /* If we are trying to override a permanent protocol, bail. */ - answer = NULL; - last_perm = &inetsw[p->type]; - list_for_each(lh, &inetsw[p->type]) { - answer = list_entry(lh, struct inet_protosw, list); - - /* Check only the non-wild match. */ - if (INET_PROTOSW_PERMANENT & answer->flags) { - if (protocol == answer->protocol) - break; - last_perm = lh; - } - - answer = NULL; - } - if (answer) - goto out_permanent; - - /* Add the new entry after the last permanent entry if any, so that - * the new entry does not override a permanent entry when matched with - * a wild-card protocol. But it is allowed to override any existing - * non-permanent entry. This means that when we remove this entry, the - * system automatically returns to the old behavior. - */ - list_add_rcu(&p->list, last_perm); -out: - spin_unlock_bh(&inetsw_lock); - - synchronize_net(); - - return; - -out_permanent: - printk(KERN_ERR "Attempt to override permanent protocol %d.\n", - protocol); - goto out; - -out_illegal: - printk(KERN_ERR - "Ignoring attempt to register invalid socket type %d.\n", - p->type); - goto out; -} - -void inet_unregister_protosw(struct inet_protosw *p) -{ - if (INET_PROTOSW_PERMANENT & p->flags) { - printk(KERN_ERR - "Attempt to unregister permanent protocol %d.\n", - p->protocol); - } else { - spin_lock_bh(&inetsw_lock); - list_del_rcu(&p->list); - spin_unlock_bh(&inetsw_lock); - - synchronize_net(); - } -} - -/* - * Shall we try to damage output packets if routing dev changes? - */ - -int sysctl_ip_dynaddr __read_mostly; - -static int inet_sk_reselect_saddr(struct sock *sk) -{ - struct inet_sock *inet = inet_sk(sk); - int err; - struct rtable *rt; - __be32 old_saddr = inet->saddr; - __be32 new_saddr; - __be32 daddr = inet->daddr; - - if (inet->opt && inet->opt->srr) - daddr = inet->opt->faddr; - - /* Query new route. */ - err = ip_route_connect(&rt, daddr, 0, - RT_CONN_FLAGS(sk), - sk->sk_bound_dev_if, - sk->sk_protocol, - inet->sport, inet->dport, sk, 0); - if (err) - return err; - - sk_setup_caps(sk, &rt->u.dst); - - new_saddr = rt->rt_src; - - if (new_saddr == old_saddr) - return 0; - - if (sysctl_ip_dynaddr > 1) { - printk(KERN_INFO "%s(): shifting inet->" - "saddr from %d.%d.%d.%d to %d.%d.%d.%d\n", - __FUNCTION__, - NIPQUAD(old_saddr), - NIPQUAD(new_saddr)); - } - - inet->saddr = inet->rcv_saddr = new_saddr; - - /* - * XXX The only one ugly spot where we need to - * XXX really change the sockets identity after - * XXX it has entered the hashes. -DaveM - * - * Besides that, it does not check for connection - * uniqueness. Wait for troubles. - */ - __sk_prot_rehash(sk); - return 0; -} - -int inet_sk_rebuild_header(struct sock *sk) -{ - struct inet_sock *inet = inet_sk(sk); - struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0); - __be32 daddr; - int err; - - /* Route is OK, nothing to do. */ - if (rt) - return 0; - - /* Reroute. */ - daddr = inet->daddr; - if (inet->opt && inet->opt->srr) - daddr = inet->opt->faddr; -{ - struct flowi fl = { - .oif = sk->sk_bound_dev_if, - .nl_u = { - .ip4_u = { - .daddr = daddr, - .saddr = inet->saddr, - .tos = RT_CONN_FLAGS(sk), - }, - }, - .proto = sk->sk_protocol, - .uli_u = { - .ports = { - .sport = inet->sport, - .dport = inet->dport, - }, - }, - }; - - security_sk_classify_flow(sk, &fl); - err = ip_route_output_flow(&rt, &fl, sk, 0); -} - if (!err) - sk_setup_caps(sk, &rt->u.dst); - else { - /* Routing failed... */ - sk->sk_route_caps = 0; - /* - * Other protocols have to map its equivalent state to TCP_SYN_SENT. - * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme - */ - if (!sysctl_ip_dynaddr || - sk->sk_state != TCP_SYN_SENT || - (sk->sk_userlocks & SOCK_BINDADDR_LOCK) || - (err = inet_sk_reselect_saddr(sk)) != 0) - sk->sk_err_soft = -err; - } - - return err; -} - -EXPORT_SYMBOL(inet_sk_rebuild_header); - -static int inet_gso_send_check(struct sk_buff *skb) -{ - struct iphdr *iph; - struct net_protocol *ops; - int proto; - int ihl; - int err = -EINVAL; - - if (unlikely(!pskb_may_pull(skb, sizeof(*iph)))) - goto out; - - iph = ip_hdr(skb); - ihl = iph->ihl * 4; - if (ihl < sizeof(*iph)) - goto out; - - if (unlikely(!pskb_may_pull(skb, ihl))) - goto out; - - __skb_pull(skb, ihl); - skb_reset_transport_header(skb); - iph = ip_hdr(skb); - proto = iph->protocol & (MAX_INET_PROTOS - 1); - err = -EPROTONOSUPPORT; - - rcu_read_lock(); - ops = rcu_dereference(inet_protos[proto]); - if (likely(ops && ops->gso_send_check)) - err = ops->gso_send_check(skb); - rcu_read_unlock(); - -out: - return err; -} - -static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features) -{ - struct sk_buff *segs = ERR_PTR(-EINVAL); - struct iphdr *iph; - struct net_protocol *ops; - int proto; - int ihl; - int id; - - if (unlikely(skb_shinfo(skb)->gso_type & - ~(SKB_GSO_TCPV4 | - SKB_GSO_UDP | - SKB_GSO_DODGY | - SKB_GSO_TCP_ECN | - 0))) - goto out; - - if (unlikely(!pskb_may_pull(skb, sizeof(*iph)))) - goto out; - - iph = ip_hdr(skb); - ihl = iph->ihl * 4; - if (ihl < sizeof(*iph)) - goto out; - - if (unlikely(!pskb_may_pull(skb, ihl))) - goto out; - - __skb_pull(skb, ihl); - skb_reset_transport_header(skb); - iph = ip_hdr(skb); - id = ntohs(iph->id); - proto = iph->protocol & (MAX_INET_PROTOS - 1); - segs = ERR_PTR(-EPROTONOSUPPORT); - - rcu_read_lock(); - ops = rcu_dereference(inet_protos[proto]); - if (likely(ops && ops->gso_segment)) - segs = ops->gso_segment(skb, features); - rcu_read_unlock(); - - if (!segs || unlikely(IS_ERR(segs))) - goto out; - - skb = segs; - do { - iph = ip_hdr(skb); - iph->id = htons(id++); - iph->tot_len = htons(skb->len - skb->mac_len); - iph->check = 0; - iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); - } while ((skb = skb->next)); - -out: - return segs; -} - -unsigned long snmp_fold_field(void *mib[], int offt) -{ - unsigned long res = 0; - int i; - - for_each_possible_cpu(i) { - res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt); - res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt); - } - return res; -} -EXPORT_SYMBOL_GPL(snmp_fold_field); - -int snmp_mib_init(void *ptr[2], size_t mibsize, size_t mibalign) -{ - BUG_ON(ptr == NULL); - ptr[0] = __alloc_percpu(mibsize); - if (!ptr[0]) - goto err0; - ptr[1] = __alloc_percpu(mibsize); - if (!ptr[1]) - goto err1; - return 0; -err1: - free_percpu(ptr[0]); - ptr[0] = NULL; -err0: - return -ENOMEM; -} -EXPORT_SYMBOL_GPL(snmp_mib_init); - -void snmp_mib_free(void *ptr[2]) -{ - BUG_ON(ptr == NULL); - free_percpu(ptr[0]); - free_percpu(ptr[1]); - ptr[0] = ptr[1] = NULL; -} -EXPORT_SYMBOL_GPL(snmp_mib_free); - -#ifdef CONFIG_IP_MULTICAST -static struct net_protocol igmp_protocol = { - .handler = igmp_rcv, -}; -#endif - -static struct net_protocol tcp_protocol = { - .handler = tcp_v4_rcv, - .err_handler = tcp_v4_err, - .gso_send_check = tcp_v4_gso_send_check, - .gso_segment = tcp_tso_segment, - .no_policy = 1, -}; - -static struct net_protocol udp_protocol = { - .handler = udp_rcv, - .err_handler = udp_err, - .no_policy = 1, -}; - -static struct net_protocol icmp_protocol = { - .handler = icmp_rcv, -}; - -static int __init init_ipv4_mibs(void) -{ - if (snmp_mib_init((void **)net_statistics, - sizeof(struct linux_mib), - __alignof__(struct linux_mib)) < 0) - goto err_net_mib; - if (snmp_mib_init((void **)ip_statistics, - sizeof(struct ipstats_mib), - __alignof__(struct ipstats_mib)) < 0) - goto err_ip_mib; - if (snmp_mib_init((void **)icmp_statistics, - sizeof(struct icmp_mib), - __alignof__(struct icmp_mib)) < 0) - goto err_icmp_mib; - if (snmp_mib_init((void **)tcp_statistics, - sizeof(struct tcp_mib), - __alignof__(struct tcp_mib)) < 0) - goto err_tcp_mib; - if (snmp_mib_init((void **)udp_statistics, - sizeof(struct udp_mib), - __alignof__(struct udp_mib)) < 0) - goto err_udp_mib; - if (snmp_mib_init((void **)udplite_statistics, - sizeof(struct udp_mib), - __alignof__(struct udp_mib)) < 0) - goto err_udplite_mib; - - tcp_mib_init(); - - return 0; - -err_udplite_mib: - snmp_mib_free((void **)udp_statistics); -err_udp_mib: - snmp_mib_free((void **)tcp_statistics); -err_tcp_mib: - snmp_mib_free((void **)icmp_statistics); -err_icmp_mib: - snmp_mib_free((void **)ip_statistics); -err_ip_mib: - snmp_mib_free((void **)net_statistics); -err_net_mib: - return -ENOMEM; -} - -static int ipv4_proc_init(void); - -/* - * IP protocol layer initialiser - */ - -static struct packet_type ip_packet_type = { - .type = __constant_htons(ETH_P_IP), - .func = ip_rcv, - .gso_send_check = inet_gso_send_check, - .gso_segment = inet_gso_segment, -}; - -static int __init inet_init(void) -{ - struct sk_buff *dummy_skb; - struct inet_protosw *q; - struct list_head *r; - int rc = -EINVAL; - - BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)); - - rc = proto_register(&tcp_prot, 1); - if (rc) - goto out; - - rc = proto_register(&udp_prot, 1); - if (rc) - goto out_unregister_tcp_proto; - - rc = proto_register(&raw_prot, 1); - if (rc) - goto out_unregister_udp_proto; - - /* - * Tell SOCKET that we are alive... - */ - - (void)sock_register(&inet_family_ops); - - /* - * Add all the base protocols. - */ - - if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0) - printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n"); - if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0) - printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n"); - if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0) - printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n"); -#ifdef CONFIG_IP_MULTICAST - if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0) - printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n"); -#endif - - /* Register the socket-side information for inet_create. */ - for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r) - INIT_LIST_HEAD(r); - - for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q) - inet_register_protosw(q); - - /* - * Set the ARP module up - */ - - arp_init(); - - /* - * Set the IP module up - */ - - ip_init(); - - tcp_v4_init(&inet_family_ops); - - /* Setup TCP slab cache for open requests. */ - tcp_init(); - - /* Add UDP-Lite (RFC 3828) */ - udplite4_register(); - - /* - * Set the ICMP layer up - */ - - icmp_init(&inet_family_ops); - - /* - * Initialise the multicast router - */ -#if defined(CONFIG_IP_MROUTE) - ip_mr_init(); -#endif - /* - * Initialise per-cpu ipv4 mibs - */ - - if (init_ipv4_mibs()) - printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ; - - ipv4_proc_init(); - - ipfrag_init(); - - dev_add_pack(&ip_packet_type); - - rc = 0; -out: - return rc; -out_unregister_udp_proto: - proto_unregister(&udp_prot); -out_unregister_tcp_proto: - proto_unregister(&tcp_prot); - goto out; -} - -fs_initcall(inet_init); - -/* ------------------------------------------------------------------------ */ - -#ifdef CONFIG_PROC_FS -static int __init ipv4_proc_init(void) -{ - int rc = 0; - - if (raw_proc_init()) - goto out_raw; - if (tcp4_proc_init()) - goto out_tcp; - if (udp4_proc_init()) - goto out_udp; - if (fib_proc_init()) - goto out_fib; - if (ip_misc_proc_init()) - goto out_misc; -out: - return rc; -out_misc: - fib_proc_exit(); -out_fib: - udp4_proc_exit(); -out_udp: - tcp4_proc_exit(); -out_tcp: - raw_proc_exit(); -out_raw: - rc = -ENOMEM; - goto out; -} - -#else /* CONFIG_PROC_FS */ -static int __init ipv4_proc_init(void) -{ - return 0; -} -#endif /* CONFIG_PROC_FS */ - -MODULE_ALIAS_NETPROTO(PF_INET); - -EXPORT_SYMBOL(inet_accept); -EXPORT_SYMBOL(inet_bind); -EXPORT_SYMBOL(inet_dgram_connect); -EXPORT_SYMBOL(inet_dgram_ops); -EXPORT_SYMBOL(inet_getname); -EXPORT_SYMBOL(inet_ioctl); -EXPORT_SYMBOL(inet_listen); -EXPORT_SYMBOL(inet_register_protosw); -EXPORT_SYMBOL(inet_release); -EXPORT_SYMBOL(inet_sendmsg); -EXPORT_SYMBOL(inet_shutdown); -EXPORT_SYMBOL(inet_sock_destruct); -EXPORT_SYMBOL(inet_stream_connect); -EXPORT_SYMBOL(inet_stream_ops); -EXPORT_SYMBOL(inet_unregister_protosw); -EXPORT_SYMBOL(net_statistics); -EXPORT_SYMBOL(sysctl_ip_nonlocal_bind); diff -Nurb linux-2.6.22-594/net/netfilter/xt_MARK.c.orig linux-2.6.22-595/net/netfilter/xt_MARK.c.orig --- linux-2.6.22-594/net/netfilter/xt_MARK.c.orig 2008-03-20 01:27:52.000000000 -0400 +++ linux-2.6.22-595/net/netfilter/xt_MARK.c.orig 1969-12-31 19:00:00.000000000 -0500 @@ -1,283 +0,0 @@ -/* This is a module which is used for setting the NFMARK field of an skb. */ - -/* (C) 1999-2001 Marc Boucher - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -MODULE_LICENSE("GPL"); -MODULE_AUTHOR("Marc Boucher "); -MODULE_DESCRIPTION("ip[6]tables MARK modification module"); -MODULE_ALIAS("ipt_MARK"); -MODULE_ALIAS("ip6t_MARK"); - -static inline u_int16_t -get_dst_port(struct nf_conntrack_tuple *tuple) -{ - switch (tuple->dst.protonum) { - case IPPROTO_GRE: - /* XXX Truncate 32-bit GRE key to 16 bits */ -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11) - return tuple->dst.u.gre.key; -#else - return htons(ntohl(tuple->dst.u.gre.key)); -#endif - case IPPROTO_ICMP: - /* Bind on ICMP echo ID */ - return tuple->src.u.icmp.id; - case IPPROTO_TCP: - return tuple->dst.u.tcp.port; - case IPPROTO_UDP: - return tuple->dst.u.udp.port; - default: - return tuple->dst.u.all; - } -} - -static inline u_int16_t -get_src_port(struct nf_conntrack_tuple *tuple) -{ - switch (tuple->dst.protonum) { - case IPPROTO_GRE: - /* XXX Truncate 32-bit GRE key to 16 bits */ - return htons(ntohl(tuple->src.u.gre.key)); - case IPPROTO_ICMP: - /* Bind on ICMP echo ID */ - return tuple->src.u.icmp.id; - case IPPROTO_TCP: - return tuple->src.u.tcp.port; - case IPPROTO_UDP: - return tuple->src.u.udp.port; - default: - return tuple->src.u.all; - } -} - -static unsigned int -target_v0(struct sk_buff **pskb, - const struct net_device *in, - const struct net_device *out, - unsigned int hooknum, - const struct xt_target *target, - const void *targinfo) -{ - const struct xt_mark_target_info *markinfo = targinfo; - - (*pskb)->mark = markinfo->mark; - return XT_CONTINUE; -} - -static unsigned int -target_v1(struct sk_buff **pskb, - const struct net_device *in, - const struct net_device *out, - unsigned int hooknum, - const struct xt_target *target, - const void *targinfo) -{ - const struct xt_mark_target_info_v1 *markinfo = targinfo; - int mark = -1; - - switch (markinfo->mode) { - case XT_MARK_SET: - mark = markinfo->mark; - break; - - case XT_MARK_AND: - mark = (*pskb)->mark & markinfo->mark; - break; - - case XT_MARK_OR: - mark = (*pskb)->mark | markinfo->mark; - break; - - case XT_MARK_COPYXID: { - enum ip_conntrack_info ctinfo; - struct sock *connection_sk=NULL; - int dif; - - struct nf_conn *ct = nf_ct_get((*pskb), &ctinfo); - extern struct inet_hashinfo tcp_hashinfo; - enum ip_conntrack_dir dir; - if (!ct) - break; - - dir = CTINFO2DIR(ctinfo); - u_int32_t src_ip = ct->tuplehash[dir].tuple.src.u3.ip; - u_int16_t src_port = get_src_port(&ct->tuplehash[dir].tuple); - u_int16_t proto = ct->tuplehash[dir].tuple.dst.protonum; - - u_int32_t ip; - u_int16_t port; - - dif = ((struct rtable *)(*pskb)->dst)->rt_iif; - ip = ct->tuplehash[dir].tuple.dst.u3.ip; - port = get_dst_port(&ct->tuplehash[dir].tuple); - - if (proto == 1 || proto == 17) { - if (((*pskb)->mark!=-1) && (*pskb)->mark) - ct->xid[0]=(*pskb)->mark; - if (ct->xid[0]) - mark = ct->xid[0]; - - } - else if (proto == 6) { - if ((*pskb)->sk) - connection_sk = (*pskb)->sk; - else { - connection_sk = inet_lookup(&tcp_hashinfo, src_ip, src_port, ip, port, dif); - } - - if (connection_sk) { - connection_sk->sk_peercred.gid = connection_sk->sk_peercred.uid = ct->xid[dir]; - ct->xid[!dir]=connection_sk->sk_xid; - if (connection_sk->sk_xid != 0) - mark = connection_sk->sk_xid; - if (connection_sk != (*pskb)->sk) - sock_put(connection_sk); - } - break; - } - } - } - - if (mark != -1) - (*pskb)->mark = mark; - return XT_CONTINUE; -} - - -static int -checkentry_v0(const char *tablename, - const void *entry, - const struct xt_target *target, - void *targinfo, - unsigned int hook_mask) -{ - struct xt_mark_target_info *markinfo = targinfo; - - if (markinfo->mark > 0xffffffff) { - printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n"); - return 0; - } - return 1; -} - -static int -checkentry_v1(const char *tablename, - const void *entry, - const struct xt_target *target, - void *targinfo, - unsigned int hook_mask) -{ - struct xt_mark_target_info_v1 *markinfo = targinfo; - - if (markinfo->mode != XT_MARK_SET - && markinfo->mode != XT_MARK_AND - && markinfo->mode != XT_MARK_OR - && markinfo->mode != XT_MARK_COPYXID) { - printk(KERN_WARNING "MARK: unknown mode %u\n", - markinfo->mode); - return 0; - } - if (markinfo->mark > 0xffffffff) { - printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n"); - return 0; - } - return 1; -} - -#ifdef CONFIG_COMPAT -struct compat_xt_mark_target_info_v1 { - compat_ulong_t mark; - u_int8_t mode; - u_int8_t __pad1; - u_int16_t __pad2; -}; - -static void compat_from_user_v1(void *dst, void *src) -{ - struct compat_xt_mark_target_info_v1 *cm = src; - struct xt_mark_target_info_v1 m = { - .mark = cm->mark, - .mode = cm->mode, - }; - memcpy(dst, &m, sizeof(m)); -} - -static int compat_to_user_v1(void __user *dst, void *src) -{ - struct xt_mark_target_info_v1 *m = src; - struct compat_xt_mark_target_info_v1 cm = { - .mark = m->mark, - .mode = m->mode, - }; - return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0; -} -#endif /* CONFIG_COMPAT */ - -static struct xt_target xt_mark_target[] = { - { - .name = "MARK", - .family = AF_INET, - .revision = 0, - .checkentry = checkentry_v0, - .target = target_v0, - .targetsize = sizeof(struct xt_mark_target_info), - .table = "mangle", - .me = THIS_MODULE, - }, - { - .name = "MARK", - .family = AF_INET, - .revision = 1, - .checkentry = checkentry_v1, - .target = target_v1, - .targetsize = sizeof(struct xt_mark_target_info_v1), -#ifdef CONFIG_COMPAT - .compatsize = sizeof(struct compat_xt_mark_target_info_v1), - .compat_from_user = compat_from_user_v1, - .compat_to_user = compat_to_user_v1, -#endif - .table = "mangle", - .me = THIS_MODULE, - }, - { - .name = "MARK", - .family = AF_INET6, - .revision = 0, - .checkentry = checkentry_v0, - .target = target_v0, - .targetsize = sizeof(struct xt_mark_target_info), - .table = "mangle", - .me = THIS_MODULE, - }, -}; - -static int __init xt_mark_init(void) -{ - return xt_register_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target)); -} - -static void __exit xt_mark_fini(void) -{ - xt_unregister_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target)); -} - -module_init(xt_mark_init); -module_exit(xt_mark_fini); diff -Nurb linux-2.6.22-594/net/packet/af_packet.c.orig linux-2.6.22-595/net/packet/af_packet.c.orig --- linux-2.6.22-594/net/packet/af_packet.c.orig 2008-03-20 01:27:52.000000000 -0400 +++ linux-2.6.22-595/net/packet/af_packet.c.orig 1969-12-31 19:00:00.000000000 -0500 @@ -1,1989 +0,0 @@ -/* - * INET An implementation of the TCP/IP protocol suite for the LINUX - * operating system. INET is implemented using the BSD Socket - * interface as the means of communication with the user level. - * - * PACKET - implements raw packet sockets. - * - * Version: $Id: af_packet.c,v 1.61 2002/02/08 03:57:19 davem Exp $ - * - * Authors: Ross Biro - * Fred N. van Kempen, - * Alan Cox, - * - * Fixes: - * Alan Cox : verify_area() now used correctly - * Alan Cox : new skbuff lists, look ma no backlogs! - * Alan Cox : tidied skbuff lists. - * Alan Cox : Now uses generic datagram routines I - * added. Also fixed the peek/read crash - * from all old Linux datagram code. - * Alan Cox : Uses the improved datagram code. - * Alan Cox : Added NULL's for socket options. - * Alan Cox : Re-commented the code. - * Alan Cox : Use new kernel side addressing - * Rob Janssen : Correct MTU usage. - * Dave Platt : Counter leaks caused by incorrect - * interrupt locking and some slightly - * dubious gcc output. Can you read - * compiler: it said _VOLATILE_ - * Richard Kooijman : Timestamp fixes. - * Alan Cox : New buffers. Use sk->mac.raw. - * Alan Cox : sendmsg/recvmsg support. - * Alan Cox : Protocol setting support - * Alexey Kuznetsov : Untied from IPv4 stack. - * Cyrus Durgin : Fixed kerneld for kmod. - * Michal Ostrowski : Module initialization cleanup. - * Ulises Alonso : Frame number limit removal and - * packet_set_ring memory leak. - * Eric Biederman : Allow for > 8 byte hardware addresses. - * The convention is that longer addresses - * will simply extend the hardware address - * byte arrays at the end of sockaddr_ll - * and packet_mreq. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version - * 2 of the License, or (at your option) any later version. - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef CONFIG_INET -#include -#endif - -/* - Assumptions: - - if device has no dev->hard_header routine, it adds and removes ll header - inside itself. In this case ll header is invisible outside of device, - but higher levels still should reserve dev->hard_header_len. - Some devices are enough clever to reallocate skb, when header - will not fit to reserved space (tunnel), another ones are silly - (PPP). - - packet socket receives packets with pulled ll header, - so that SOCK_RAW should push it back. - -On receive: ------------ - -Incoming, dev->hard_header!=NULL - mac_header -> ll header - data -> data - -Outgoing, dev->hard_header!=NULL - mac_header -> ll header - data -> ll header - -Incoming, dev->hard_header==NULL - mac_header -> UNKNOWN position. It is very likely, that it points to ll - header. PPP makes it, that is wrong, because introduce - assymetry between rx and tx paths. - data -> data - -Outgoing, dev->hard_header==NULL - mac_header -> data. ll header is still not built! - data -> data - -Resume - If dev->hard_header==NULL we are unlikely to restore sensible ll header. - - -On transmit: ------------- - -dev->hard_header != NULL - mac_header -> ll header - data -> ll header - -dev->hard_header == NULL (ll header is added by device, we cannot control it) - mac_header -> data - data -> data - - We should set nh.raw on output to correct posistion, - packet classifier depends on it. - */ - -/* List of all packet sockets. */ -static HLIST_HEAD(packet_sklist); -static DEFINE_RWLOCK(packet_sklist_lock); - -static atomic_t packet_socks_nr; - - -/* Private packet socket structures. */ - -struct packet_mclist -{ - struct packet_mclist *next; - int ifindex; - int count; - unsigned short type; - unsigned short alen; - unsigned char addr[MAX_ADDR_LEN]; -}; -/* identical to struct packet_mreq except it has - * a longer address field. - */ -struct packet_mreq_max -{ - int mr_ifindex; - unsigned short mr_type; - unsigned short mr_alen; - unsigned char mr_address[MAX_ADDR_LEN]; -}; - -#ifdef CONFIG_PACKET_MMAP -static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing); -#endif - -static void packet_flush_mclist(struct sock *sk); - -struct packet_sock { - /* struct sock has to be the first member of packet_sock */ - struct sock sk; - struct tpacket_stats stats; -#ifdef CONFIG_PACKET_MMAP - char * *pg_vec; - unsigned int head; - unsigned int frames_per_block; - unsigned int frame_size; - unsigned int frame_max; - int copy_thresh; -#endif - struct packet_type prot_hook; - spinlock_t bind_lock; - unsigned int running:1, /* prot_hook is attached*/ - auxdata:1, - origdev:1; - int ifindex; /* bound device */ - __be16 num; - struct packet_mclist *mclist; -#ifdef CONFIG_PACKET_MMAP - atomic_t mapped; - unsigned int pg_vec_order; - unsigned int pg_vec_pages; - unsigned int pg_vec_len; -#endif -}; - -struct packet_skb_cb { - unsigned int origlen; - union { - struct sockaddr_pkt pkt; - struct sockaddr_ll ll; - } sa; -}; - -#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb)) - -#ifdef CONFIG_PACKET_MMAP - -static inline struct tpacket_hdr *packet_lookup_frame(struct packet_sock *po, unsigned int position) -{ - unsigned int pg_vec_pos, frame_offset; - - pg_vec_pos = position / po->frames_per_block; - frame_offset = position % po->frames_per_block; - - return (struct tpacket_hdr *)(po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size)); -} -#endif - -static inline struct packet_sock *pkt_sk(struct sock *sk) -{ - return (struct packet_sock *)sk; -} - -static void packet_sock_destruct(struct sock *sk) -{ - BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc)); - BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc)); - - if (!sock_flag(sk, SOCK_DEAD)) { - printk("Attempt to release alive packet socket: %p\n", sk); - return; - } - - atomic_dec(&packet_socks_nr); -#ifdef PACKET_REFCNT_DEBUG - printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr)); -#endif -} - - -static const struct proto_ops packet_ops; - -static const struct proto_ops packet_ops_spkt; - -static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) -{ - struct sock *sk; - struct sockaddr_pkt *spkt; - - /* - * When we registered the protocol we saved the socket in the data - * field for just this event. - */ - - sk = pt->af_packet_priv; - - /* - * Yank back the headers [hope the device set this - * right or kerboom...] - * - * Incoming packets have ll header pulled, - * push it back. - * - * For outgoing ones skb->data == skb_mac_header(skb) - * so that this procedure is noop. - */ - - if (skb->pkt_type == PACKET_LOOPBACK) - goto out; - - if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) - goto oom; - - /* drop any routing info */ - dst_release(skb->dst); - skb->dst = NULL; - - /* drop conntrack reference */ - nf_reset(skb); - - spkt = &PACKET_SKB_CB(skb)->sa.pkt; - - skb_push(skb, skb->data - skb_mac_header(skb)); - - /* - * The SOCK_PACKET socket receives _all_ frames. - */ - - spkt->spkt_family = dev->type; - strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device)); - spkt->spkt_protocol = skb->protocol; - - /* - * Charge the memory to the socket. This is done specifically - * to prevent sockets using all the memory up. - */ - - if (sock_queue_rcv_skb(sk,skb) == 0) - return 0; - -out: - kfree_skb(skb); -oom: - return 0; -} - - -/* - * Output a raw packet to a device layer. This bypasses all the other - * protocol layers and you must therefore supply it with a complete frame - */ - -static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock, - struct msghdr *msg, size_t len) -{ - struct sock *sk = sock->sk; - struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name; - struct sk_buff *skb; - struct net_device *dev; - __be16 proto=0; - int err; - - if (!nx_capable(CAP_NET_RAW, NXC_RAW_SEND)) - return -EPERM; - - /* - * Get and verify the address. - */ - - if (saddr) - { - if (msg->msg_namelen < sizeof(struct sockaddr)) - return(-EINVAL); - if (msg->msg_namelen==sizeof(struct sockaddr_pkt)) - proto=saddr->spkt_protocol; - } - else - return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */ - - /* - * Find the device first to size check it - */ - - saddr->spkt_device[13] = 0; - dev = dev_get_by_name(saddr->spkt_device); - err = -ENODEV; - if (dev == NULL) - goto out_unlock; - - err = -ENETDOWN; - if (!(dev->flags & IFF_UP)) - goto out_unlock; - - /* - * You may not queue a frame bigger than the mtu. This is the lowest level - * raw protocol and you must do your own fragmentation at this level. - */ - - err = -EMSGSIZE; - if (len > dev->mtu + dev->hard_header_len) - goto out_unlock; - - err = -ENOBUFS; - skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL); - - /* - * If the write buffer is full, then tough. At this level the user gets to - * deal with the problem - do your own algorithmic backoffs. That's far - * more flexible. - */ - - if (skb == NULL) - goto out_unlock; - - /* - * Fill it in - */ - - /* FIXME: Save some space for broken drivers that write a - * hard header at transmission time by themselves. PPP is the - * notable one here. This should really be fixed at the driver level. - */ - skb_reserve(skb, LL_RESERVED_SPACE(dev)); - skb_reset_network_header(skb); - - /* Try to align data part correctly */ - if (dev->hard_header) { - skb->data -= dev->hard_header_len; - skb->tail -= dev->hard_header_len; - if (len < dev->hard_header_len) - skb_reset_network_header(skb); - } - - /* Returns -EFAULT on error */ - err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len); - skb->protocol = proto; - skb->dev = dev; - skb->priority = sk->sk_priority; - if (err) - goto out_free; - - /* - * Now send it - */ - - dev_queue_xmit(skb); - dev_put(dev); - return(len); - -out_free: - kfree_skb(skb); -out_unlock: - if (dev) - dev_put(dev); - return err; -} - -static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk, - unsigned int res) -{ - struct sk_filter *filter; - int tag = skb->skb_tag; - - if (sk->sk_nx_info && !(tag == 1 || sk->sk_nid == tag)) - return 0; - - rcu_read_lock_bh(); - filter = rcu_dereference(sk->sk_filter); - if (filter != NULL) - res = sk_run_filter(skb, filter->insns, filter->len); - rcu_read_unlock_bh(); - - return res; -} - -/* - This function makes lazy skb cloning in hope that most of packets - are discarded by BPF. - - Note tricky part: we DO mangle shared skb! skb->data, skb->len - and skb->cb are mangled. It works because (and until) packets - falling here are owned by current CPU. Output packets are cloned - by dev_queue_xmit_nit(), input packets are processed by net_bh - sequencially, so that if we return skb to original state on exit, - we will not harm anyone. - */ - -static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) -{ - struct sock *sk; - struct sockaddr_ll *sll; - struct packet_sock *po; - u8 * skb_head = skb->data; - int skb_len = skb->len; - unsigned int snaplen, res; - - if (skb->pkt_type == PACKET_LOOPBACK) - goto drop; - - sk = pt->af_packet_priv; - po = pkt_sk(sk); - - skb->dev = dev; - - if (dev->hard_header) { - /* The device has an explicit notion of ll header, - exported to higher levels. - - Otherwise, the device hides datails of it frame - structure, so that corresponding packet head - never delivered to user. - */ - if (sk->sk_type != SOCK_DGRAM) - skb_push(skb, skb->data - skb_mac_header(skb)); - else if (skb->pkt_type == PACKET_OUTGOING) { - /* Special case: outgoing packets have ll header at head */ - skb_pull(skb, skb_network_offset(skb)); - } - } - - snaplen = skb->len; - - res = run_filter(skb, sk, snaplen); - if (!res) - goto drop_n_restore; - if (snaplen > res) - snaplen = res; - - if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= - (unsigned)sk->sk_rcvbuf) - goto drop_n_acct; - - if (skb_shared(skb)) { - struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); - if (nskb == NULL) - goto drop_n_acct; - - if (skb_head != skb->data) { - skb->data = skb_head; - skb->len = skb_len; - } - kfree_skb(skb); - skb = nskb; - } - - BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 > - sizeof(skb->cb)); - - sll = &PACKET_SKB_CB(skb)->sa.ll; - sll->sll_family = AF_PACKET; - sll->sll_hatype = dev->type; - sll->sll_protocol = skb->protocol; - sll->sll_pkttype = skb->pkt_type; - if (unlikely(po->origdev) && skb->pkt_type == PACKET_HOST) - sll->sll_ifindex = orig_dev->ifindex; - else - sll->sll_ifindex = dev->ifindex; - sll->sll_halen = 0; - - if (dev->hard_header_parse) - sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr); - - PACKET_SKB_CB(skb)->origlen = skb->len; - - if (pskb_trim(skb, snaplen)) - goto drop_n_acct; - - skb_set_owner_r(skb, sk); - skb->dev = NULL; - dst_release(skb->dst); - skb->dst = NULL; - - /* drop conntrack reference */ - nf_reset(skb); - - spin_lock(&sk->sk_receive_queue.lock); - po->stats.tp_packets++; - __skb_queue_tail(&sk->sk_receive_queue, skb); - spin_unlock(&sk->sk_receive_queue.lock); - sk->sk_data_ready(sk, skb->len); - return 0; - -drop_n_acct: - spin_lock(&sk->sk_receive_queue.lock); - po->stats.tp_drops++; - spin_unlock(&sk->sk_receive_queue.lock); - -drop_n_restore: - if (skb_head != skb->data && skb_shared(skb)) { - skb->data = skb_head; - skb->len = skb_len; - } -drop: - kfree_skb(skb); - return 0; -} - -#ifdef CONFIG_PACKET_MMAP -static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) -{ - struct sock *sk; - struct packet_sock *po; - struct sockaddr_ll *sll; - struct tpacket_hdr *h; - u8 * skb_head = skb->data; - int skb_len = skb->len; - unsigned int snaplen, res; - unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER; - unsigned short macoff, netoff; - struct sk_buff *copy_skb = NULL; - struct timeval tv; - - if (skb->pkt_type == PACKET_LOOPBACK) - goto drop; - - sk = pt->af_packet_priv; - po = pkt_sk(sk); - - if (dev->hard_header) { - if (sk->sk_type != SOCK_DGRAM) - skb_push(skb, skb->data - skb_mac_header(skb)); - else if (skb->pkt_type == PACKET_OUTGOING) { - /* Special case: outgoing packets have ll header at head */ - skb_pull(skb, skb_network_offset(skb)); - } - } - - if (skb->ip_summed == CHECKSUM_PARTIAL) - status |= TP_STATUS_CSUMNOTREADY; - - snaplen = skb->len; - - res = run_filter(skb, sk, snaplen); - if (!res) - goto drop_n_restore; - if (snaplen > res) - snaplen = res; - - if (sk->sk_type == SOCK_DGRAM) { - macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16; - } else { - unsigned maclen = skb_network_offset(skb); - netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen)); - macoff = netoff - maclen; - } - - if (macoff + snaplen > po->frame_size) { - if (po->copy_thresh && - atomic_read(&sk->sk_rmem_alloc) + skb->truesize < - (unsigned)sk->sk_rcvbuf) { - if (skb_shared(skb)) { - copy_skb = skb_clone(skb, GFP_ATOMIC); - } else { - copy_skb = skb_get(skb); - skb_head = skb->data; - } - if (copy_skb) - skb_set_owner_r(copy_skb, sk); - } - snaplen = po->frame_size - macoff; - if ((int)snaplen < 0) - snaplen = 0; - } - - spin_lock(&sk->sk_receive_queue.lock); - h = packet_lookup_frame(po, po->head); - - if (h->tp_status) - goto ring_is_full; - po->head = po->head != po->frame_max ? po->head+1 : 0; - po->stats.tp_packets++; - if (copy_skb) { - status |= TP_STATUS_COPY; - __skb_queue_tail(&sk->sk_receive_queue, copy_skb); - } - if (!po->stats.tp_drops) - status &= ~TP_STATUS_LOSING; - spin_unlock(&sk->sk_receive_queue.lock); - - skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen); - - h->tp_len = skb->len; - h->tp_snaplen = snaplen; - h->tp_mac = macoff; - h->tp_net = netoff; - if (skb->tstamp.tv64 == 0) { - __net_timestamp(skb); - sock_enable_timestamp(sk); - } - tv = ktime_to_timeval(skb->tstamp); - h->tp_sec = tv.tv_sec; - h->tp_usec = tv.tv_usec; - - sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h))); - sll->sll_halen = 0; - if (dev->hard_header_parse) - sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr); - sll->sll_family = AF_PACKET; - sll->sll_hatype = dev->type; - sll->sll_protocol = skb->protocol; - sll->sll_pkttype = skb->pkt_type; - if (unlikely(po->origdev) && skb->pkt_type == PACKET_HOST) - sll->sll_ifindex = orig_dev->ifindex; - else - sll->sll_ifindex = dev->ifindex; - - h->tp_status = status; - smp_mb(); - - { - struct page *p_start, *p_end; - u8 *h_end = (u8 *)h + macoff + snaplen - 1; - - p_start = virt_to_page(h); - p_end = virt_to_page(h_end); - while (p_start <= p_end) { - flush_dcache_page(p_start); - p_start++; - } - } - - sk->sk_data_ready(sk, 0); - -drop_n_restore: - if (skb_head != skb->data && skb_shared(skb)) { - skb->data = skb_head; - skb->len = skb_len; - } -drop: - kfree_skb(skb); - return 0; - -ring_is_full: - po->stats.tp_drops++; - spin_unlock(&sk->sk_receive_queue.lock); - - sk->sk_data_ready(sk, 0); - if (copy_skb) - kfree_skb(copy_skb); - goto drop_n_restore; -} - -#endif - - -static int packet_sendmsg(struct kiocb *iocb, struct socket *sock, - struct msghdr *msg, size_t len) -{ - struct sock *sk = sock->sk; - struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name; - struct sk_buff *skb; - struct net_device *dev; - __be16 proto; - unsigned char *addr; - int ifindex, err, reserve = 0; - - if (!nx_capable(CAP_NET_RAW, NXC_RAW_SEND)) - return -EPERM; - - /* - * Get and verify the address. - */ - - if (saddr == NULL) { - struct packet_sock *po = pkt_sk(sk); - - ifindex = po->ifindex; - proto = po->num; - addr = NULL; - } else { - err = -EINVAL; - if (msg->msg_namelen < sizeof(struct sockaddr_ll)) - goto out; - if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) - goto out; - ifindex = saddr->sll_ifindex; - proto = saddr->sll_protocol; - addr = saddr->sll_addr; - } - - - dev = dev_get_by_index(ifindex); - err = -ENXIO; - if (dev == NULL) - goto out_unlock; - if (sock->type == SOCK_RAW) - reserve = dev->hard_header_len; - - err = -ENETDOWN; - if (!(dev->flags & IFF_UP)) - goto out_unlock; - - err = -EMSGSIZE; - if (len > dev->mtu+reserve) - goto out_unlock; - - skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev), - msg->msg_flags & MSG_DONTWAIT, &err); - if (skb==NULL) - goto out_unlock; - - skb_reserve(skb, LL_RESERVED_SPACE(dev)); - skb_reset_network_header(skb); - - if (dev->hard_header) { - int res; - err = -EINVAL; - res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len); - if (sock->type != SOCK_DGRAM) { - skb_reset_tail_pointer(skb); - skb->len = 0; - } else if (res < 0) - goto out_free; - } - - /* Returns -EFAULT on error */ - err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len); - if (err) - goto out_free; - - skb->protocol = proto; - skb->dev = dev; - skb->priority = sk->sk_priority; - - /* - * Now send it - */ - - err = dev_queue_xmit(skb); - if (err > 0 && (err = net_xmit_errno(err)) != 0) - goto out_unlock; - - dev_put(dev); - - return(len); - -out_free: - kfree_skb(skb); -out_unlock: - if (dev) - dev_put(dev); -out: - return err; -} - -/* - * Close a PACKET socket. This is fairly simple. We immediately go - * to 'closed' state and remove our protocol entry in the device list. - */ - -static int packet_release(struct socket *sock) -{ - struct sock *sk = sock->sk; - struct packet_sock *po; - - if (!sk) - return 0; - - po = pkt_sk(sk); - - write_lock_bh(&packet_sklist_lock); - sk_del_node_init(sk); - write_unlock_bh(&packet_sklist_lock); - - /* - * Unhook packet receive handler. - */ - - if (po->running) { - /* - * Remove the protocol hook - */ - dev_remove_pack(&po->prot_hook); - po->running = 0; - po->num = 0; - __sock_put(sk); - } - - packet_flush_mclist(sk); - -#ifdef CONFIG_PACKET_MMAP - if (po->pg_vec) { - struct tpacket_req req; - memset(&req, 0, sizeof(req)); - packet_set_ring(sk, &req, 1); - } -#endif - - /* - * Now the socket is dead. No more input will appear. - */ - - sock_orphan(sk); - sock->sk = NULL; - - /* Purge queues */ - - skb_queue_purge(&sk->sk_receive_queue); - - sock_put(sk); - return 0; -} - -/* - * Attach a packet hook. - */ - -static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol) -{ - struct packet_sock *po = pkt_sk(sk); - /* - * Detach an existing hook if present. - */ - - lock_sock(sk); - - spin_lock(&po->bind_lock); - if (po->running) { - __sock_put(sk); - po->running = 0; - po->num = 0; - spin_unlock(&po->bind_lock); - dev_remove_pack(&po->prot_hook); - spin_lock(&po->bind_lock); - } - - po->num = protocol; - po->prot_hook.type = protocol; - po->prot_hook.dev = dev; - - po->ifindex = dev ? dev->ifindex : 0; - - if (protocol == 0) - goto out_unlock; - - if (dev) { - if (dev->flags&IFF_UP) { - dev_add_pack(&po->prot_hook); - sock_hold(sk); - po->running = 1; - } else { - sk->sk_err = ENETDOWN; - if (!sock_flag(sk, SOCK_DEAD)) - sk->sk_error_report(sk); - } - } else { - dev_add_pack(&po->prot_hook); - sock_hold(sk); - po->running = 1; - } - -out_unlock: - spin_unlock(&po->bind_lock); - release_sock(sk); - return 0; -} - -/* - * Bind a packet socket to a device - */ - -static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len) -{ - struct sock *sk=sock->sk; - char name[15]; - struct net_device *dev; - int err = -ENODEV; - - /* - * Check legality - */ - - if (addr_len != sizeof(struct sockaddr)) - return -EINVAL; - strlcpy(name,uaddr->sa_data,sizeof(name)); - - dev = dev_get_by_name(name); - if (dev) { - err = packet_do_bind(sk, dev, pkt_sk(sk)->num); - dev_put(dev); - } - return err; -} - -static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) -{ - struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr; - struct sock *sk=sock->sk; - struct net_device *dev = NULL; - int err; - - - /* - * Check legality - */ - - if (addr_len < sizeof(struct sockaddr_ll)) - return -EINVAL; - if (sll->sll_family != AF_PACKET) - return -EINVAL; - - if (sll->sll_ifindex) { - err = -ENODEV; - dev = dev_get_by_index(sll->sll_ifindex); - if (dev == NULL) - goto out; - } - err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num); - if (dev) - dev_put(dev); - -out: - return err; -} - -static struct proto packet_proto = { - .name = "PACKET", - .owner = THIS_MODULE, - .obj_size = sizeof(struct packet_sock), -}; - -/* - * Create a packet of type SOCK_PACKET. - */ - -static int packet_create(struct socket *sock, int protocol) -{ - struct sock *sk; - struct packet_sock *po; - __be16 proto = (__force __be16)protocol; /* weird, but documented */ - int err; - - if (!nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET)) - return -EPERM; - if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW && - sock->type != SOCK_PACKET) - return -ESOCKTNOSUPPORT; - - sock->state = SS_UNCONNECTED; - - err = -ENOBUFS; - sk = sk_alloc(PF_PACKET, GFP_KERNEL, &packet_proto, 1); - if (sk == NULL) - goto out; - - sock->ops = &packet_ops; - if (sock->type == SOCK_PACKET) - sock->ops = &packet_ops_spkt; - - sock_init_data(sock, sk); - - po = pkt_sk(sk); - sk->sk_family = PF_PACKET; - po->num = proto; - - sk->sk_destruct = packet_sock_destruct; - atomic_inc(&packet_socks_nr); - - /* - * Attach a protocol block - */ - - spin_lock_init(&po->bind_lock); - po->prot_hook.func = packet_rcv; - - if (sock->type == SOCK_PACKET) - po->prot_hook.func = packet_rcv_spkt; - - po->prot_hook.af_packet_priv = sk; - - if (proto) { - po->prot_hook.type = proto; - dev_add_pack(&po->prot_hook); - sock_hold(sk); - po->running = 1; - } - - write_lock_bh(&packet_sklist_lock); - sk_add_node(sk, &packet_sklist); - write_unlock_bh(&packet_sklist_lock); - return(0); -out: - return err; -} - -/* - * Pull a packet from our receive queue and hand it to the user. - * If necessary we block. - */ - -static int packet_recvmsg(struct kiocb *iocb, struct socket *sock, - struct msghdr *msg, size_t len, int flags) -{ - struct sock *sk = sock->sk; - struct sk_buff *skb; - int copied, err; - struct sockaddr_ll *sll; - - err = -EINVAL; - if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT)) - goto out; - -#if 0 - /* What error should we return now? EUNATTACH? */ - if (pkt_sk(sk)->ifindex < 0) - return -ENODEV; -#endif - - /* - * Call the generic datagram receiver. This handles all sorts - * of horrible races and re-entrancy so we can forget about it - * in the protocol layers. - * - * Now it will return ENETDOWN, if device have just gone down, - * but then it will block. - */ - - skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err); - - /* - * An error occurred so return it. Because skb_recv_datagram() - * handles the blocking we don't see and worry about blocking - * retries. - */ - - if (skb == NULL) - goto out; - - /* - * If the address length field is there to be filled in, we fill - * it in now. - */ - - sll = &PACKET_SKB_CB(skb)->sa.ll; - if (sock->type == SOCK_PACKET) - msg->msg_namelen = sizeof(struct sockaddr_pkt); - else - msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr); - - /* - * You lose any data beyond the buffer you gave. If it worries a - * user program they can ask the device for its MTU anyway. - */ - - copied = skb->len; - if (copied > len) - { - copied=len; - msg->msg_flags|=MSG_TRUNC; - } - - err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); - if (err) - goto out_free; - - sock_recv_timestamp(msg, sk, skb); - - if (msg->msg_name) - memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, - msg->msg_namelen); - - if (pkt_sk(sk)->auxdata) { - struct tpacket_auxdata aux; - - aux.tp_status = TP_STATUS_USER; - if (skb->ip_summed == CHECKSUM_PARTIAL) - aux.tp_status |= TP_STATUS_CSUMNOTREADY; - aux.tp_len = PACKET_SKB_CB(skb)->origlen; - aux.tp_snaplen = skb->len; - aux.tp_mac = 0; - aux.tp_net = skb_network_offset(skb); - - put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux); - } - - /* - * Free or return the buffer as appropriate. Again this - * hides all the races and re-entrancy issues from us. - */ - err = (flags&MSG_TRUNC) ? skb->len : copied; - -out_free: - skb_free_datagram(sk, skb); -out: - return err; -} - -static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr, - int *uaddr_len, int peer) -{ - struct net_device *dev; - struct sock *sk = sock->sk; - - if (peer) - return -EOPNOTSUPP; - - uaddr->sa_family = AF_PACKET; - dev = dev_get_by_index(pkt_sk(sk)->ifindex); - if (dev) { - strlcpy(uaddr->sa_data, dev->name, 15); - dev_put(dev); - } else - memset(uaddr->sa_data, 0, 14); - *uaddr_len = sizeof(*uaddr); - - return 0; -} - -static int packet_getname(struct socket *sock, struct sockaddr *uaddr, - int *uaddr_len, int peer) -{ - struct net_device *dev; - struct sock *sk = sock->sk; - struct packet_sock *po = pkt_sk(sk); - struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr; - - if (peer) - return -EOPNOTSUPP; - - sll->sll_family = AF_PACKET; - sll->sll_ifindex = po->ifindex; - sll->sll_protocol = po->num; - dev = dev_get_by_index(po->ifindex); - if (dev) { - sll->sll_hatype = dev->type; - sll->sll_halen = dev->addr_len; - memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len); - dev_put(dev); - } else { - sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */ - sll->sll_halen = 0; - } - *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen; - - return 0; -} - -static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what) -{ - switch (i->type) { - case PACKET_MR_MULTICAST: - if (what > 0) - dev_mc_add(dev, i->addr, i->alen, 0); - else - dev_mc_delete(dev, i->addr, i->alen, 0); - break; - case PACKET_MR_PROMISC: - dev_set_promiscuity(dev, what); - break; - case PACKET_MR_ALLMULTI: - dev_set_allmulti(dev, what); - break; - default:; - } -} - -static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what) -{ - for ( ; i; i=i->next) { - if (i->ifindex == dev->ifindex) - packet_dev_mc(dev, i, what); - } -} - -static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq) -{ - struct packet_sock *po = pkt_sk(sk); - struct packet_mclist *ml, *i; - struct net_device *dev; - int err; - - rtnl_lock(); - - err = -ENODEV; - dev = __dev_get_by_index(mreq->mr_ifindex); - if (!dev) - goto done; - - err = -EINVAL; - if (mreq->mr_alen > dev->addr_len) - goto done; - - err = -ENOBUFS; - i = kmalloc(sizeof(*i), GFP_KERNEL); - if (i == NULL) - goto done; - - err = 0; - for (ml = po->mclist; ml; ml = ml->next) { - if (ml->ifindex == mreq->mr_ifindex && - ml->type == mreq->mr_type && - ml->alen == mreq->mr_alen && - memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { - ml->count++; - /* Free the new element ... */ - kfree(i); - goto done; - } - } - - i->type = mreq->mr_type; - i->ifindex = mreq->mr_ifindex; - i->alen = mreq->mr_alen; - memcpy(i->addr, mreq->mr_address, i->alen); - i->count = 1; - i->next = po->mclist; - po->mclist = i; - packet_dev_mc(dev, i, +1); - -done: - rtnl_unlock(); - return err; -} - -static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq) -{ - struct packet_mclist *ml, **mlp; - - rtnl_lock(); - - for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) { - if (ml->ifindex == mreq->mr_ifindex && - ml->type == mreq->mr_type && - ml->alen == mreq->mr_alen && - memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { - if (--ml->count == 0) { - struct net_device *dev; - *mlp = ml->next; - dev = dev_get_by_index(ml->ifindex); - if (dev) { - packet_dev_mc(dev, ml, -1); - dev_put(dev); - } - kfree(ml); - } - rtnl_unlock(); - return 0; - } - } - rtnl_unlock(); - return -EADDRNOTAVAIL; -} - -static void packet_flush_mclist(struct sock *sk) -{ - struct packet_sock *po = pkt_sk(sk); - struct packet_mclist *ml; - - if (!po->mclist) - return; - - rtnl_lock(); - while ((ml = po->mclist) != NULL) { - struct net_device *dev; - - po->mclist = ml->next; - if ((dev = dev_get_by_index(ml->ifindex)) != NULL) { - packet_dev_mc(dev, ml, -1); - dev_put(dev); - } - kfree(ml); - } - rtnl_unlock(); -} - -static int -packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen) -{ - struct sock *sk = sock->sk; - struct packet_sock *po = pkt_sk(sk); - int ret; - - if (level != SOL_PACKET) - return -ENOPROTOOPT; - - switch(optname) { - case PACKET_ADD_MEMBERSHIP: - case PACKET_DROP_MEMBERSHIP: - { - struct packet_mreq_max mreq; - int len = optlen; - memset(&mreq, 0, sizeof(mreq)); - if (len < sizeof(struct packet_mreq)) - return -EINVAL; - if (len > sizeof(mreq)) - len = sizeof(mreq); - if (copy_from_user(&mreq,optval,len)) - return -EFAULT; - if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address))) - return -EINVAL; - if (optname == PACKET_ADD_MEMBERSHIP) - ret = packet_mc_add(sk, &mreq); - else - ret = packet_mc_drop(sk, &mreq); - return ret; - } - -#ifdef CONFIG_PACKET_MMAP - case PACKET_RX_RING: - { - struct tpacket_req req; - - if (optlencopy_thresh = val; - return 0; - } -#endif - case PACKET_AUXDATA: - { - int val; - - if (optlen < sizeof(val)) - return -EINVAL; - if (copy_from_user(&val, optval, sizeof(val))) - return -EFAULT; - - po->auxdata = !!val; - return 0; - } - case PACKET_ORIGDEV: - { - int val; - - if (optlen < sizeof(val)) - return -EINVAL; - if (copy_from_user(&val, optval, sizeof(val))) - return -EFAULT; - - po->origdev = !!val; - return 0; - } - default: - return -ENOPROTOOPT; - } -} - -static int packet_getsockopt(struct socket *sock, int level, int optname, - char __user *optval, int __user *optlen) -{ - int len; - int val; - struct sock *sk = sock->sk; - struct packet_sock *po = pkt_sk(sk); - void *data; - struct tpacket_stats st; - - if (level != SOL_PACKET) - return -ENOPROTOOPT; - - if (get_user(len, optlen)) - return -EFAULT; - - if (len < 0) - return -EINVAL; - - switch(optname) { - case PACKET_STATISTICS: - if (len > sizeof(struct tpacket_stats)) - len = sizeof(struct tpacket_stats); - spin_lock_bh(&sk->sk_receive_queue.lock); - st = po->stats; - memset(&po->stats, 0, sizeof(st)); - spin_unlock_bh(&sk->sk_receive_queue.lock); - st.tp_packets += st.tp_drops; - - data = &st; - break; - case PACKET_AUXDATA: - if (len > sizeof(int)) - len = sizeof(int); - val = po->auxdata; - - data = &val; - break; - case PACKET_ORIGDEV: - if (len > sizeof(int)) - len = sizeof(int); - val = po->origdev; - - data = &val; - break; - default: - return -ENOPROTOOPT; - } - - if (put_user(len, optlen)) - return -EFAULT; - if (copy_to_user(optval, data, len)) - return -EFAULT; - return 0; -} - - -static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data) -{ - struct sock *sk; - struct hlist_node *node; - struct net_device *dev = data; - - read_lock(&packet_sklist_lock); - sk_for_each(sk, node, &packet_sklist) { - struct packet_sock *po = pkt_sk(sk); - - switch (msg) { - case NETDEV_UNREGISTER: - if (po->mclist) - packet_dev_mclist(dev, po->mclist, -1); - /* fallthrough */ - - case NETDEV_DOWN: - if (dev->ifindex == po->ifindex) { - spin_lock(&po->bind_lock); - if (po->running) { - __dev_remove_pack(&po->prot_hook); - __sock_put(sk); - po->running = 0; - sk->sk_err = ENETDOWN; - if (!sock_flag(sk, SOCK_DEAD)) - sk->sk_error_report(sk); - } - if (msg == NETDEV_UNREGISTER) { - po->ifindex = -1; - po->prot_hook.dev = NULL; - } - spin_unlock(&po->bind_lock); - } - break; - case NETDEV_UP: - spin_lock(&po->bind_lock); - if (dev->ifindex == po->ifindex && po->num && - !po->running) { - dev_add_pack(&po->prot_hook); - sock_hold(sk); - po->running = 1; - } - spin_unlock(&po->bind_lock); - break; - } - } - read_unlock(&packet_sklist_lock); - return NOTIFY_DONE; -} - - -static int packet_ioctl(struct socket *sock, unsigned int cmd, - unsigned long arg) -{ - struct sock *sk = sock->sk; - - switch(cmd) { - case SIOCOUTQ: - { - int amount = atomic_read(&sk->sk_wmem_alloc); - return put_user(amount, (int __user *)arg); - } - case SIOCINQ: - { - struct sk_buff *skb; - int amount = 0; - - spin_lock_bh(&sk->sk_receive_queue.lock); - skb = skb_peek(&sk->sk_receive_queue); - if (skb) - amount = skb->len; - spin_unlock_bh(&sk->sk_receive_queue.lock); - return put_user(amount, (int __user *)arg); - } - case SIOCGSTAMP: - return sock_get_timestamp(sk, (struct timeval __user *)arg); - case SIOCGSTAMPNS: - return sock_get_timestampns(sk, (struct timespec __user *)arg); - -#ifdef CONFIG_INET - case SIOCADDRT: - case SIOCDELRT: - case SIOCDARP: - case SIOCGARP: - case SIOCSARP: - case SIOCGIFADDR: - case SIOCSIFADDR: - case SIOCGIFBRDADDR: - case SIOCSIFBRDADDR: - case SIOCGIFNETMASK: - case SIOCSIFNETMASK: - case SIOCGIFDSTADDR: - case SIOCSIFDSTADDR: - case SIOCSIFFLAGS: - return inet_dgram_ops.ioctl(sock, cmd, arg); -#endif - - default: - return -ENOIOCTLCMD; - } - return 0; -} - -#ifndef CONFIG_PACKET_MMAP -#define packet_mmap sock_no_mmap -#define packet_poll datagram_poll -#else - -static unsigned int packet_poll(struct file * file, struct socket *sock, - poll_table *wait) -{ - struct sock *sk = sock->sk; - struct packet_sock *po = pkt_sk(sk); - unsigned int mask = datagram_poll(file, sock, wait); - - spin_lock_bh(&sk->sk_receive_queue.lock); - if (po->pg_vec) { - unsigned last = po->head ? po->head-1 : po->frame_max; - struct tpacket_hdr *h; - - h = packet_lookup_frame(po, last); - - if (h->tp_status) - mask |= POLLIN | POLLRDNORM; - } - spin_unlock_bh(&sk->sk_receive_queue.lock); - return mask; -} - - -/* Dirty? Well, I still did not learn better way to account - * for user mmaps. - */ - -static void packet_mm_open(struct vm_area_struct *vma) -{ - struct file *file = vma->vm_file; - struct socket * sock = file->private_data; - struct sock *sk = sock->sk; - - if (sk) - atomic_inc(&pkt_sk(sk)->mapped); -} - -static void packet_mm_close(struct vm_area_struct *vma) -{ - struct file *file = vma->vm_file; - struct socket * sock = file->private_data; - struct sock *sk = sock->sk; - - if (sk) - atomic_dec(&pkt_sk(sk)->mapped); -} - -static struct vm_operations_struct packet_mmap_ops = { - .open = packet_mm_open, - .close =packet_mm_close, -}; - -static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order) -{ - return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1); -} - -static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len) -{ - int i; - - for (i = 0; i < len; i++) { - if (likely(pg_vec[i])) - free_pages((unsigned long) pg_vec[i], order); - } - kfree(pg_vec); -} - -static inline char *alloc_one_pg_vec_page(unsigned long order) -{ - return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO, - order); -} - -static char **alloc_pg_vec(struct tpacket_req *req, int order) -{ - unsigned int block_nr = req->tp_block_nr; - char **pg_vec; - int i; - - pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL); - if (unlikely(!pg_vec)) - goto out; - - for (i = 0; i < block_nr; i++) { - pg_vec[i] = alloc_one_pg_vec_page(order); - if (unlikely(!pg_vec[i])) - goto out_free_pgvec; - } - -out: - return pg_vec; - -out_free_pgvec: - free_pg_vec(pg_vec, order, block_nr); - pg_vec = NULL; - goto out; -} - -static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing) -{ - char **pg_vec = NULL; - struct packet_sock *po = pkt_sk(sk); - int was_running, order = 0; - __be16 num; - int err = 0; - - if (req->tp_block_nr) { - int i, l; - - /* Sanity tests and some calculations */ - - if (unlikely(po->pg_vec)) - return -EBUSY; - - if (unlikely((int)req->tp_block_size <= 0)) - return -EINVAL; - if (unlikely(req->tp_block_size & (PAGE_SIZE - 1))) - return -EINVAL; - if (unlikely(req->tp_frame_size < TPACKET_HDRLEN)) - return -EINVAL; - if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1))) - return -EINVAL; - - po->frames_per_block = req->tp_block_size/req->tp_frame_size; - if (unlikely(po->frames_per_block <= 0)) - return -EINVAL; - if (unlikely((po->frames_per_block * req->tp_block_nr) != - req->tp_frame_nr)) - return -EINVAL; - - err = -ENOMEM; - order = get_order(req->tp_block_size); - pg_vec = alloc_pg_vec(req, order); - if (unlikely(!pg_vec)) - goto out; - - l = 0; - for (i = 0; i < req->tp_block_nr; i++) { - char *ptr = pg_vec[i]; - struct tpacket_hdr *header; - int k; - - for (k = 0; k < po->frames_per_block; k++) { - header = (struct tpacket_hdr *) ptr; - header->tp_status = TP_STATUS_KERNEL; - ptr += req->tp_frame_size; - } - } - /* Done */ - } else { - if (unlikely(req->tp_frame_nr)) - return -EINVAL; - } - - lock_sock(sk); - - /* Detach socket from network */ - spin_lock(&po->bind_lock); - was_running = po->running; - num = po->num; - if (was_running) { - __dev_remove_pack(&po->prot_hook); - po->num = 0; - po->running = 0; - __sock_put(sk); - } - spin_unlock(&po->bind_lock); - - synchronize_net(); - - err = -EBUSY; - if (closing || atomic_read(&po->mapped) == 0) { - err = 0; -#define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; }) - - spin_lock_bh(&sk->sk_receive_queue.lock); - pg_vec = XC(po->pg_vec, pg_vec); - po->frame_max = (req->tp_frame_nr - 1); - po->head = 0; - po->frame_size = req->tp_frame_size; - spin_unlock_bh(&sk->sk_receive_queue.lock); - - order = XC(po->pg_vec_order, order); - req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr); - - po->pg_vec_pages = req->tp_block_size/PAGE_SIZE; - po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv; - skb_queue_purge(&sk->sk_receive_queue); -#undef XC - if (atomic_read(&po->mapped)) - printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped)); - } - - spin_lock(&po->bind_lock); - if (was_running && !po->running) { - sock_hold(sk); - po->running = 1; - po->num = num; - dev_add_pack(&po->prot_hook); - } - spin_unlock(&po->bind_lock); - - release_sock(sk); - - if (pg_vec) - free_pg_vec(pg_vec, order, req->tp_block_nr); -out: - return err; -} - -static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma) -{ - struct sock *sk = sock->sk; - struct packet_sock *po = pkt_sk(sk); - unsigned long size; - unsigned long start; - int err = -EINVAL; - int i; - - if (vma->vm_pgoff) - return -EINVAL; - - size = vma->vm_end - vma->vm_start; - - lock_sock(sk); - if (po->pg_vec == NULL) - goto out; - if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE) - goto out; - - start = vma->vm_start; - for (i = 0; i < po->pg_vec_len; i++) { - struct page *page = virt_to_page(po->pg_vec[i]); - int pg_num; - - for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) { - err = vm_insert_page(vma, start, page); - if (unlikely(err)) - goto out; - start += PAGE_SIZE; - } - } - atomic_inc(&po->mapped); - vma->vm_ops = &packet_mmap_ops; - err = 0; - -out: - release_sock(sk); - return err; -} -#endif - - -static const struct proto_ops packet_ops_spkt = { - .family = PF_PACKET, - .owner = THIS_MODULE, - .release = packet_release, - .bind = packet_bind_spkt, - .connect = sock_no_connect, - .socketpair = sock_no_socketpair, - .accept = sock_no_accept, - .getname = packet_getname_spkt, - .poll = datagram_poll, - .ioctl = packet_ioctl, - .listen = sock_no_listen, - .shutdown = sock_no_shutdown, - .setsockopt = sock_no_setsockopt, - .getsockopt = sock_no_getsockopt, - .sendmsg = packet_sendmsg_spkt, - .recvmsg = packet_recvmsg, - .mmap = sock_no_mmap, - .sendpage = sock_no_sendpage, -}; - -static const struct proto_ops packet_ops = { - .family = PF_PACKET, - .owner = THIS_MODULE, - .release = packet_release, - .bind = packet_bind, - .connect = sock_no_connect, - .socketpair = sock_no_socketpair, - .accept = sock_no_accept, - .getname = packet_getname, - .poll = packet_poll, - .ioctl = packet_ioctl, - .listen = sock_no_listen, - .shutdown = sock_no_shutdown, - .setsockopt = packet_setsockopt, - .getsockopt = packet_getsockopt, - .sendmsg = packet_sendmsg, - .recvmsg = packet_recvmsg, - .mmap = packet_mmap, - .sendpage = sock_no_sendpage, -}; - -static struct net_proto_family packet_family_ops = { - .family = PF_PACKET, - .create = packet_create, - .owner = THIS_MODULE, -}; - -static struct notifier_block packet_netdev_notifier = { - .notifier_call =packet_notifier, -}; - -#ifdef CONFIG_PROC_FS -static inline struct sock *packet_seq_idx(loff_t off) -{ - struct sock *s; - struct hlist_node *node; - - sk_for_each(s, node, &packet_sklist) { - if (!off--) - return s; - } - return NULL; -} - -static void *packet_seq_start(struct seq_file *seq, loff_t *pos) -{ - read_lock(&packet_sklist_lock); - return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN; -} - -static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos) -{ - ++*pos; - return (v == SEQ_START_TOKEN) - ? sk_head(&packet_sklist) - : sk_next((struct sock*)v) ; -} - -static void packet_seq_stop(struct seq_file *seq, void *v) -{ - read_unlock(&packet_sklist_lock); -} - -static int packet_seq_show(struct seq_file *seq, void *v) -{ - if (v == SEQ_START_TOKEN) - seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n"); - else { - struct sock *s = v; - const struct packet_sock *po = pkt_sk(s); - - seq_printf(seq, - "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n", - s, - atomic_read(&s->sk_refcnt), - s->sk_type, - ntohs(po->num), - po->ifindex, - po->running, - atomic_read(&s->sk_rmem_alloc), - sock_i_uid(s), - sock_i_ino(s) ); - } - - return 0; -} - -static struct seq_operations packet_seq_ops = { - .start = packet_seq_start, - .next = packet_seq_next, - .stop = packet_seq_stop, - .show = packet_seq_show, -}; - -static int packet_seq_open(struct inode *inode, struct file *file) -{ - return seq_open(file, &packet_seq_ops); -} - -static const struct file_operations packet_seq_fops = { - .owner = THIS_MODULE, - .open = packet_seq_open, - .read = seq_read, - .llseek = seq_lseek, - .release = seq_release, -}; - -#endif - -static void __exit packet_exit(void) -{ - proc_net_remove("packet"); - unregister_netdevice_notifier(&packet_netdev_notifier); - sock_unregister(PF_PACKET); - proto_unregister(&packet_proto); -} - -static int __init packet_init(void) -{ - int rc = proto_register(&packet_proto, 0); - - if (rc != 0) - goto out; - - sock_register(&packet_family_ops); - register_netdevice_notifier(&packet_netdev_notifier); - proc_net_fops_create("packet", 0, &packet_seq_fops); -out: - return rc; -} - -module_init(packet_init); -module_exit(packet_exit); -MODULE_LICENSE("GPL"); -MODULE_ALIAS_NETPROTO(PF_PACKET); diff -Nurb linux-2.6.22-594/net/socket.c linux-2.6.22-595/net/socket.c --- linux-2.6.22-594/net/socket.c 2008-03-20 01:27:52.000000000 -0400 +++ linux-2.6.22-595/net/socket.c 2008-03-20 01:28:00.000000000 -0400 @@ -1122,12 +1122,17 @@ if (type < 0 || type >= SOCK_MAX) return -EINVAL; + /* + * Hack no. 2 - Sapan + * Clean this up later + * if (!nx_check(0, VS_ADMIN)) { if (family == PF_INET && !current_nx_info_has_v4()) return -EAFNOSUPPORT; if (family == PF_INET6 && !current_nx_info_has_v6()) return -EAFNOSUPPORT; } + */ /* Compatibility.