1 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
2 --- linux-2.6.22-594/include/linux/vserver/network.h.orig.orig 2008-03-20 01:27:27.000000000 -0400
3 +++ linux-2.6.22-595/include/linux/vserver/network.h.orig.orig 1969-12-31 19:00:00.000000000 -0500
8 -#include <linux/types.h>
11 -#define MAX_N_CONTEXT 65535 /* Arbitrary limit */
16 -#define NXF_INFO_PRIVATE 0x00000008
18 -#define NXF_SINGLE_IP 0x00000100
19 -#define NXF_LBACK_REMAP 0x00000200
21 -#define NXF_HIDE_NETIF 0x02000000
22 -#define NXF_HIDE_LBACK 0x04000000
24 -#define NXF_STATE_SETUP (1ULL << 32)
25 -#define NXF_STATE_ADMIN (1ULL << 34)
27 -#define NXF_SC_HELPER (1ULL << 36)
28 -#define NXF_PERSISTENT (1ULL << 38)
30 -#define NXF_ONE_TIME (0x0005ULL << 32)
33 -#define NXF_INIT_SET (__nxf_init_set())
35 -static inline uint64_t __nxf_init_set(void) {
36 - return NXF_STATE_ADMIN
37 -#ifdef CONFIG_VSERVER_AUTO_LBACK
41 -#ifdef CONFIG_VSERVER_AUTO_SINGLE
50 -#define NXC_RAW_ICMP 0x00000100
55 -#define NXA_TYPE_IPV4 0x0001
56 -#define NXA_TYPE_IPV6 0x0002
58 -#define NXA_TYPE_NONE 0x0000
59 -#define NXA_TYPE_ANY 0x00FF
61 -#define NXA_TYPE_ADDR 0x0010
62 -#define NXA_TYPE_MASK 0x0020
63 -#define NXA_TYPE_RANGE 0x0040
65 -#define NXA_MASK_ALL (NXA_TYPE_ADDR | NXA_TYPE_MASK | NXA_TYPE_RANGE)
67 -#define NXA_MOD_BCAST 0x0100
68 -#define NXA_MOD_LBACK 0x0200
70 -#define NXA_LOOPBACK 0x1000
72 -#define NXA_MASK_BIND (NXA_MASK_ALL | NXA_MOD_BCAST | NXA_MOD_LBACK)
73 -#define NXA_MASK_SHOW (NXA_MASK_ALL | NXA_LOOPBACK)
77 -#include <linux/list.h>
78 -#include <linux/spinlock.h>
79 -#include <linux/rcupdate.h>
80 -#include <linux/in.h>
81 -#include <linux/in6.h>
82 -#include <asm/atomic.h>
85 - struct nx_addr_v4 *next;
86 - struct in_addr ip[2];
87 - struct in_addr mask;
93 - struct nx_addr_v6 *next;
95 - struct in6_addr mask;
102 - struct hlist_node nx_hlist; /* linked list of nxinfos */
103 - nid_t nx_id; /* vnet id */
104 - atomic_t nx_usecnt; /* usage count */
105 - atomic_t nx_tasks; /* tasks count */
106 - int nx_state; /* context state */
108 - uint64_t nx_flags; /* network flag word */
109 - uint64_t nx_ncaps; /* network capabilities */
111 - struct in_addr v4_lback; /* Loopback address */
112 - struct in_addr v4_bcast; /* Broadcast address */
113 - struct nx_addr_v4 v4; /* First/Single ipv4 address */
115 - struct nx_addr_v6 v6; /* First/Single ipv6 address */
117 - char nx_name[65]; /* network context name */
123 -#define NXS_HASHED 0x0001
124 -#define NXS_SHUTDOWN 0x0100
125 -#define NXS_RELEASED 0x8000
127 -extern struct nx_info *lookup_nx_info(int);
129 -extern int get_nid_list(int, unsigned int *, int);
130 -extern int nid_is_hashed(nid_t);
132 -extern int nx_migrate_task(struct task_struct *, struct nx_info *);
134 -extern long vs_net_change(struct nx_info *, unsigned int);
139 -#define NX_IPV4(n) ((n)->v4.type != NXA_TYPE_NONE)
141 -#define NX_IPV6(n) ((n)->v6.type != NXA_TYPE_NONE)
143 -#define NX_IPV6(n) (0)
146 -#endif /* __KERNEL__ */
147 -#endif /* _VX_NETWORK_H */
148 diff -Nurb linux-2.6.22-594/kernel/nsproxy.c.orig linux-2.6.22-595/kernel/nsproxy.c.orig
149 --- linux-2.6.22-594/kernel/nsproxy.c.orig 2008-03-20 01:27:50.000000000 -0400
150 +++ linux-2.6.22-595/kernel/nsproxy.c.orig 1969-12-31 19:00:00.000000000 -0500
153 - * Copyright (C) 2006 IBM Corporation
155 - * Author: Serge Hallyn <serue@us.ibm.com>
157 - * This program is free software; you can redistribute it and/or
158 - * modify it under the terms of the GNU General Public License as
159 - * published by the Free Software Foundation, version 2 of the
162 - * Jun 2006 - namespaces support
163 - * OpenVZ, SWsoft Inc.
164 - * Pavel Emelianov <xemul@openvz.org>
167 -#include <linux/module.h>
168 -#include <linux/version.h>
169 -#include <linux/nsproxy.h>
170 -#include <linux/init_task.h>
171 -#include <linux/mnt_namespace.h>
172 -#include <linux/utsname.h>
173 -#include <net/net_namespace.h>
174 -#include <linux/pid_namespace.h>
175 -#include <linux/vserver/global.h>
176 -#include <linux/vserver/debug.h>
178 -static struct kmem_cache *nsproxy_cachep;
180 -struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
182 -void get_task_namespaces(struct task_struct *tsk)
184 - struct nsproxy *ns = tsk->nsproxy;
191 - * creates a copy of "orig" with refcount 1.
193 -static inline struct nsproxy *clone_nsproxy(struct nsproxy *orig)
195 - struct nsproxy *ns;
197 - ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL);
199 - atomic_set(&ns->count, 1);
200 - vxdprintk(VXD_CBIT(space, 2), "clone_nsproxy(%p[%u] = %p[1]",
201 - orig, atomic_read(&orig->count), ns);
202 - atomic_inc(&vs_global_nsproxy);
207 - * Create new nsproxy and all of its the associated namespaces.
208 - * Return the newly created nsproxy. Do not attach this to the task,
209 - * leave it to the caller to do proper locking and attach it to task.
211 -static struct nsproxy *unshare_namespaces(int flags, struct nsproxy *orig,
212 - struct fs_struct *new_fs)
214 - struct nsproxy *new_nsp;
217 - vxdprintk(VXD_CBIT(space, 4),
218 - "unshare_namespaces(0x%08x,%p,%p)",
219 - flags, orig, new_fs);
221 - new_nsp = clone_nsproxy(orig);
223 - return ERR_PTR(-ENOMEM);
225 - new_nsp->mnt_ns = copy_mnt_ns(flags, orig->mnt_ns, new_fs);
226 - if (IS_ERR(new_nsp->mnt_ns))
229 - new_nsp->uts_ns = copy_utsname(flags, orig->uts_ns);
230 - if (IS_ERR(new_nsp->uts_ns))
233 - new_nsp->ipc_ns = copy_ipcs(flags, orig->ipc_ns);
234 - if (IS_ERR(new_nsp->ipc_ns))
237 - new_nsp->pid_ns = copy_pid_ns(flags, orig->pid_ns);
238 - if (IS_ERR(new_nsp->pid_ns))
241 - new_nsp->user_ns = copy_user_ns(flags, orig->user_ns);
242 - if (IS_ERR(new_nsp->user_ns))
245 - new_nsp->net_ns = copy_net_ns(flags, orig->net_ns);
246 - if (IS_ERR(new_nsp->net_ns))
252 - if (new_nsp->user_ns)
253 - put_user_ns(new_nsp->user_ns);
254 - if (new_nsp->net_ns)
255 - put_net(new_nsp->net_ns);
257 - if (new_nsp->pid_ns)
258 - put_pid_ns(new_nsp->pid_ns);
260 - if (new_nsp->ipc_ns)
261 - put_ipc_ns(new_nsp->ipc_ns);
263 - if (new_nsp->uts_ns)
264 - put_uts_ns(new_nsp->uts_ns);
266 - if (new_nsp->mnt_ns)
267 - put_mnt_ns(new_nsp->mnt_ns);
269 - kmem_cache_free(nsproxy_cachep, new_nsp);
270 - return ERR_PTR(err);
273 -static struct nsproxy *create_new_namespaces(unsigned long flags, struct task_struct *tsk,
274 - struct fs_struct *new_fs)
276 - return unshare_namespaces(flags, tsk->nsproxy, new_fs);
280 - * copies the nsproxy, setting refcount to 1, and grabbing a
281 - * reference to all contained namespaces.
283 -struct nsproxy *copy_nsproxy(struct nsproxy *orig)
285 - struct nsproxy *ns = clone_nsproxy(orig);
289 - get_mnt_ns(ns->mnt_ns);
291 - get_uts_ns(ns->uts_ns);
293 - get_ipc_ns(ns->ipc_ns);
295 - get_pid_ns(ns->pid_ns);
301 - * called from clone. This now handles copy for nsproxy and all
302 - * namespaces therein.
304 -int copy_namespaces(unsigned long flags, struct task_struct *tsk)
306 - struct nsproxy *old_ns = tsk->nsproxy;
307 - struct nsproxy *new_ns = NULL;
310 - vxdprintk(VXD_CBIT(space, 7), "copy_namespaces(0x%08x,%p[%p])",
311 - flags, tsk, old_ns);
316 - get_nsproxy(old_ns);
319 - if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWNET)))
322 - #ifndef CONFIG_NET_NS
323 - if (unshare_flags & CLONE_NEWNET)
328 - if (!capable(CAP_SYS_ADMIN)) {
333 - new_ns = create_new_namespaces(flags, tsk, tsk->fs);
334 - if (IS_ERR(new_ns)) {
335 - err = PTR_ERR(new_ns);
339 - err = ns_container_clone(tsk);
341 - put_nsproxy(new_ns);
345 - tsk->nsproxy = new_ns;
348 - put_nsproxy(old_ns);
349 - vxdprintk(VXD_CBIT(space, 3),
350 - "copy_namespaces(0x%08x,%p[%p]) = %d [%p]",
351 - flags, tsk, old_ns, err, new_ns);
355 -void free_nsproxy(struct nsproxy *ns)
358 - put_mnt_ns(ns->mnt_ns);
360 - put_uts_ns(ns->uts_ns);
362 - put_ipc_ns(ns->ipc_ns);
364 - put_pid_ns(ns->pid_ns);
365 - atomic_dec(&vs_global_nsproxy);
370 - * Called from unshare. Unshare all the namespaces part of nsproxy.
371 - * On success, returns the new nsproxy.
373 -int unshare_nsproxy_namespaces(unsigned long unshare_flags,
374 - struct nsproxy **new_nsp, struct fs_struct *new_fs)
378 - vxdprintk(VXD_CBIT(space, 4),
379 - "unshare_nsproxy_namespaces(0x%08lx,[%p])",
380 - unshare_flags, current->nsproxy);
382 - if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
383 - CLONE_NEWUSER | CLONE_NEWNET)))
386 -#ifndef CONFIG_NET_NS
387 - if (unshare_flags & CLONE_NEWNET)
390 - if (!capable(CAP_SYS_ADMIN))
393 - *new_nsp = create_new_namespaces(unshare_flags, current,
394 - new_fs ? new_fs : current->fs);
395 - if (IS_ERR(*new_nsp)) {
396 - err = PTR_ERR(*new_nsp);
400 - err = ns_container_clone(current);
402 - put_nsproxy(*new_nsp);
408 -static int __init nsproxy_cache_init(void)
410 - nsproxy_cachep = kmem_cache_create("nsproxy", sizeof(struct nsproxy),
411 - 0, SLAB_PANIC, NULL, NULL);
415 -module_init(nsproxy_cache_init);
416 diff -Nurb linux-2.6.22-594/kernel/user.c.orig linux-2.6.22-595/kernel/user.c.orig
417 --- linux-2.6.22-594/kernel/user.c.orig 2008-03-20 01:27:50.000000000 -0400
418 +++ linux-2.6.22-595/kernel/user.c.orig 1969-12-31 19:00:00.000000000 -0500
421 - * The "user cache".
423 - * (C) Copyright 1991-2000 Linus Torvalds
425 - * We have a per-user structure to keep track of how many
426 - * processes, files etc the user has claimed, in order to be
427 - * able to have per-user limits for system resources.
430 -#include <linux/init.h>
431 -#include <linux/sched.h>
432 -#include <linux/slab.h>
433 -#include <linux/bitops.h>
434 -#include <linux/key.h>
435 -#include <linux/interrupt.h>
436 -#include <linux/module.h>
437 -#include <linux/user_namespace.h>
440 - * UID task count cache, to get fast user lookup in "alloc_uid"
441 - * when changing user ID's (ie setuid() and friends).
444 -#define UIDHASH_MASK (UIDHASH_SZ - 1)
445 -#define __uidhashfn(xid,uid) ((((uid) >> UIDHASH_BITS) + ((uid)^(xid))) & UIDHASH_MASK)
446 -#define uidhashentry(ns, xid, uid) ((ns)->uidhash_table + __uidhashfn(xid, uid))
448 -static struct kmem_cache *uid_cachep;
449 -static struct list_head uidhash_table[UIDHASH_SZ];
452 - * The uidhash_lock is mostly taken from process context, but it is
453 - * occasionally also taken from softirq/tasklet context, when
454 - * task-structs get RCU-freed. Hence all locking must be softirq-safe.
455 - * But free_uid() is also called with local interrupts disabled, and running
456 - * local_bh_enable() with local interrupts disabled is an error - we'll run
457 - * softirq callbacks, and they can unconditionally enable interrupts, and
458 - * the caller of free_uid() didn't expect that..
460 -static DEFINE_SPINLOCK(uidhash_lock);
462 -struct user_struct root_user = {
463 - .__count = ATOMIC_INIT(1),
464 - .processes = ATOMIC_INIT(1),
465 - .files = ATOMIC_INIT(0),
466 - .sigpending = ATOMIC_INIT(0),
470 - .uid_keyring = &root_user_keyring,
471 - .session_keyring = &root_session_keyring,
476 - * These routines must be called with the uidhash spinlock held!
478 -static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
480 - list_add(&up->uidhash_list, hashent);
483 -static inline void uid_hash_remove(struct user_struct *up)
485 - list_del(&up->uidhash_list);
488 -static inline struct user_struct *uid_hash_find(xid_t xid, uid_t uid, struct list_head *hashent)
490 - struct list_head *up;
492 - list_for_each(up, hashent) {
493 - struct user_struct *user;
495 - user = list_entry(up, struct user_struct, uidhash_list);
497 - if(user->uid == uid && user->xid == xid) {
498 - atomic_inc(&user->__count);
507 - * Locate the user_struct for the passed UID. If found, take a ref on it. The
508 - * caller must undo that ref with free_uid().
510 - * If the user_struct could not be found, return NULL.
512 -struct user_struct *find_user(xid_t xid, uid_t uid)
514 - struct user_struct *ret;
515 - unsigned long flags;
516 - struct user_namespace *ns = current->nsproxy->user_ns;
518 - spin_lock_irqsave(&uidhash_lock, flags);
519 - ret = uid_hash_find(xid, uid, uidhashentry(ns, xid, uid));
520 - spin_unlock_irqrestore(&uidhash_lock, flags);
524 -void free_uid(struct user_struct *up)
526 - unsigned long flags;
531 - local_irq_save(flags);
532 - if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
533 - uid_hash_remove(up);
534 - spin_unlock_irqrestore(&uidhash_lock, flags);
535 - key_put(up->uid_keyring);
536 - key_put(up->session_keyring);
537 - kmem_cache_free(uid_cachep, up);
539 - local_irq_restore(flags);
543 -struct user_struct * alloc_uid(xid_t xid, uid_t uid)
545 - struct user_namespace *ns = current->nsproxy->user_ns;
546 - struct list_head *hashent = uidhashentry(ns,xid, uid);
547 - struct user_struct *up;
549 - spin_lock_irq(&uidhash_lock);
550 - up = uid_hash_find(xid, uid, hashent);
551 - spin_unlock_irq(&uidhash_lock);
554 - struct user_struct *new;
556 - new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
561 - atomic_set(&new->__count, 1);
562 - atomic_set(&new->processes, 0);
563 - atomic_set(&new->files, 0);
564 - atomic_set(&new->sigpending, 0);
565 -#ifdef CONFIG_INOTIFY_USER
566 - atomic_set(&new->inotify_watches, 0);
567 - atomic_set(&new->inotify_devs, 0);
571 - new->locked_shm = 0;
573 - if (alloc_uid_keyring(new, current) < 0) {
574 - kmem_cache_free(uid_cachep, new);
579 - * Before adding this, check whether we raced
580 - * on adding the same user already..
582 - spin_lock_irq(&uidhash_lock);
583 - up = uid_hash_find(xid, uid, hashent);
585 - key_put(new->uid_keyring);
586 - key_put(new->session_keyring);
587 - kmem_cache_free(uid_cachep, new);
589 - uid_hash_insert(new, hashent);
592 - spin_unlock_irq(&uidhash_lock);
598 -void switch_uid(struct user_struct *new_user)
600 - struct user_struct *old_user;
602 - /* What if a process setreuid()'s and this brings the
603 - * new uid over his NPROC rlimit? We can check this now
604 - * cheaply with the new uid cache, so if it matters
605 - * we should be checking for it. -DaveM
607 - old_user = current->user;
608 - atomic_inc(&new_user->processes);
609 - atomic_dec(&old_user->processes);
610 - switch_uid_keyring(new_user);
611 - current->user = new_user;
614 - * We need to synchronize with __sigqueue_alloc()
615 - * doing a get_uid(p->user).. If that saw the old
616 - * user value, we need to wait until it has exited
617 - * its critical region before we can free the old
621 - spin_unlock_wait(¤t->sighand->siglock);
623 - free_uid(old_user);
624 - suid_keys(current);
628 -static int __init uid_cache_init(void)
632 - uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
633 - 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
635 - for(n = 0; n < UIDHASH_SZ; ++n)
636 - INIT_LIST_HEAD(init_user_ns.uidhash_table + n);
638 - /* Insert the root user immediately (init already runs as root) */
639 - spin_lock_irq(&uidhash_lock);
640 - uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0, 0));
641 - spin_unlock_irq(&uidhash_lock);
646 -module_init(uid_cache_init);
647 diff -Nurb linux-2.6.22-594/kernel/vserver/context.c linux-2.6.22-595/kernel/vserver/context.c
648 --- linux-2.6.22-594/kernel/vserver/context.c 2008-03-20 01:27:21.000000000 -0400
649 +++ linux-2.6.22-595/kernel/vserver/context.c 2008-03-20 01:28:00.000000000 -0400
650 @@ -589,13 +589,13 @@
651 struct nsproxy *old_nsp, *new_nsp;
653 ret = unshare_nsproxy_namespaces(
654 - CLONE_NEWUTS | CLONE_NEWIPC,
655 + CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWNET,
660 old_nsp = xchg(&p->nsproxy, new_nsp);
661 - vx_set_space(vxi, CLONE_NEWUTS | CLONE_NEWIPC);
662 + vx_set_space(vxi, CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWNET);
663 put_nsproxy(old_nsp);
667 if (vs_state_change(new_vxi, VSC_STARTUP))
670 - ret = vx_migrate_task(current, new_vxi, (!data));
671 + ret = vx_migrate_task(current, new_vxi, 1 /*(!data) Hack no. 1 - Sapan*/);
675 diff -Nurb linux-2.6.22-594/kernel/vserver/space.c linux-2.6.22-595/kernel/vserver/space.c
676 --- linux-2.6.22-594/kernel/vserver/space.c 2008-03-20 01:27:54.000000000 -0400
677 +++ linux-2.6.22-595/kernel/vserver/space.c 2008-03-20 01:28:00.000000000 -0400
679 #include <linux/utsname.h>
680 #include <linux/nsproxy.h>
681 #include <linux/err.h>
682 +#include <net/net_namespace.h>
683 #include <asm/uaccess.h>
685 #include <linux/vs_context.h>
687 struct mnt_namespace *old_ns;
688 struct uts_namespace *old_uts;
689 struct ipc_namespace *old_ipc;
690 + struct net *old_net;
691 struct nsproxy *nsproxy;
693 nsproxy = copy_nsproxy(old_nsproxy);
698 + if (mask & CLONE_NEWNET) {
699 + old_net = nsproxy->net_ns;
700 + nsproxy->net_ns = new_nsproxy->net_ns;
701 + if (nsproxy->net_ns) {
702 + get_net(nsproxy->net_ns);
703 + printk(KERN_ALERT "Cloning network namespace\n");
723 int vc_enter_space(struct vx_info *vxi, void __user *data)
725 + /* Ask dhozac how to pass this flag from user space - Sapan*/
726 struct vcmd_space_mask vc_data = { .mask = 0 };
728 if (data && copy_from_user(&vc_data, data, sizeof(vc_data)))
729 diff -Nurb linux-2.6.22-594/net/core/dev.c linux-2.6.22-595/net/core/dev.c
730 --- linux-2.6.22-594/net/core/dev.c 2008-03-20 01:27:55.000000000 -0400
731 +++ linux-2.6.22-595/net/core/dev.c 2008-03-20 01:32:22.000000000 -0400
732 @@ -2207,7 +2207,7 @@
735 for_each_netdev(net, dev) {
736 - if (!nx_dev_visible(current->nx_info, dev))
737 + if (net==&init_net && !nx_dev_visible(current->nx_info, dev))
739 for (i = 0; i < NPROTO; i++) {
740 if (gifconf_list[i]) {
741 @@ -2274,8 +2274,9 @@
742 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
744 struct net_device_stats *stats = dev->get_stats(dev);
745 + struct net *net = seq->private;
747 - if (!nx_dev_visible(current->nx_info, dev))
748 + if (net==&init_net && !nx_dev_visible(current->nx_info, dev))
751 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
752 diff -Nurb linux-2.6.22-594/net/core/dev.c.orig linux-2.6.22-595/net/core/dev.c.orig
753 --- linux-2.6.22-594/net/core/dev.c.orig 1969-12-31 19:00:00.000000000 -0500
754 +++ linux-2.6.22-595/net/core/dev.c.orig 2008-03-20 01:27:55.000000000 -0400
757 + * NET3 Protocol independent device support routines.
759 + * This program is free software; you can redistribute it and/or
760 + * modify it under the terms of the GNU General Public License
761 + * as published by the Free Software Foundation; either version
762 + * 2 of the License, or (at your option) any later version.
764 + * Derived from the non IP parts of dev.c 1.0.19
765 + * Authors: Ross Biro
766 + * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
767 + * Mark Evans, <evansmp@uhura.aston.ac.uk>
769 + * Additional Authors:
770 + * Florian la Roche <rzsfl@rz.uni-sb.de>
771 + * Alan Cox <gw4pts@gw4pts.ampr.org>
772 + * David Hinds <dahinds@users.sourceforge.net>
773 + * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
774 + * Adam Sulmicki <adam@cfar.umd.edu>
775 + * Pekka Riikonen <priikone@poesidon.pspt.fi>
778 + * D.J. Barrow : Fixed bug where dev->refcnt gets set
779 + * to 2 if register_netdev gets called
780 + * before net_dev_init & also removed a
781 + * few lines of code in the process.
782 + * Alan Cox : device private ioctl copies fields back.
783 + * Alan Cox : Transmit queue code does relevant
784 + * stunts to keep the queue safe.
785 + * Alan Cox : Fixed double lock.
786 + * Alan Cox : Fixed promisc NULL pointer trap
787 + * ???????? : Support the full private ioctl range
788 + * Alan Cox : Moved ioctl permission check into
790 + * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
791 + * Alan Cox : 100 backlog just doesn't cut it when
792 + * you start doing multicast video 8)
793 + * Alan Cox : Rewrote net_bh and list manager.
794 + * Alan Cox : Fix ETH_P_ALL echoback lengths.
795 + * Alan Cox : Took out transmit every packet pass
796 + * Saved a few bytes in the ioctl handler
797 + * Alan Cox : Network driver sets packet type before
798 + * calling netif_rx. Saves a function
800 + * Alan Cox : Hashed net_bh()
801 + * Richard Kooijman: Timestamp fixes.
802 + * Alan Cox : Wrong field in SIOCGIFDSTADDR
803 + * Alan Cox : Device lock protection.
804 + * Alan Cox : Fixed nasty side effect of device close
806 + * Rudi Cilibrasi : Pass the right thing to
807 + * set_mac_address()
808 + * Dave Miller : 32bit quantity for the device lock to
809 + * make it work out on a Sparc.
810 + * Bjorn Ekwall : Added KERNELD hack.
811 + * Alan Cox : Cleaned up the backlog initialise.
812 + * Craig Metz : SIOCGIFCONF fix if space for under
814 + * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
815 + * is no device open function.
816 + * Andi Kleen : Fix error reporting for SIOCGIFCONF
817 + * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
818 + * Cyrus Durgin : Cleaned for KMOD
819 + * Adam Sulmicki : Bug Fix : Network Device Unload
820 + * A network device unload needs to purge
821 + * the backlog queue.
822 + * Paul Rusty Russell : SIOCSIFNAME
823 + * Pekka Riikonen : Netdev boot-time settings code
824 + * Andrew Morton : Make unregister_netdevice wait
825 + * indefinitely on dev->refcnt
826 + * J Hadi Salim : - Backlog queue sampling
827 + * - netif_rx() feedback
830 +#include <asm/uaccess.h>
831 +#include <asm/system.h>
832 +#include <linux/bitops.h>
833 +#include <linux/capability.h>
834 +#include <linux/cpu.h>
835 +#include <linux/types.h>
836 +#include <linux/kernel.h>
837 +#include <linux/sched.h>
838 +#include <linux/mutex.h>
839 +#include <linux/string.h>
840 +#include <linux/mm.h>
841 +#include <linux/socket.h>
842 +#include <linux/sockios.h>
843 +#include <linux/errno.h>
844 +#include <linux/interrupt.h>
845 +#include <linux/if_ether.h>
846 +#include <linux/netdevice.h>
847 +#include <linux/etherdevice.h>
848 +#include <linux/notifier.h>
849 +#include <linux/skbuff.h>
850 +#include <net/sock.h>
851 +#include <linux/rtnetlink.h>
852 +#include <linux/proc_fs.h>
853 +#include <linux/seq_file.h>
854 +#include <linux/stat.h>
855 +#include <linux/if_bridge.h>
856 +#include <net/dst.h>
857 +#include <net/pkt_sched.h>
858 +#include <net/checksum.h>
859 +#include <linux/highmem.h>
860 +#include <linux/init.h>
861 +#include <linux/kmod.h>
862 +#include <linux/module.h>
863 +#include <linux/kallsyms.h>
864 +#include <linux/netpoll.h>
865 +#include <linux/rcupdate.h>
866 +#include <linux/delay.h>
867 +#include <net/wext.h>
868 +#include <net/iw_handler.h>
869 +#include <asm/current.h>
870 +#include <linux/audit.h>
871 +#include <linux/dmaengine.h>
872 +#include <linux/err.h>
873 +#include <linux/ctype.h>
874 +#include <net/net_namespace.h>
875 +#include <linux/if_arp.h>
876 +#include <linux/vs_inet.h>
879 + * The list of packet types we will receive (as opposed to discard)
880 + * and the routines to invoke.
882 + * Why 16. Because with 16 the only overlap we get on a hash of the
883 + * low nibble of the protocol value is RARP/SNAP/X.25.
885 + * NOTE: That is no longer true with the addition of VLAN tags. Not
886 + * sure which should go first, but I bet it won't make much
887 + * difference if we are running VLANs. The good news is that
888 + * this protocol won't be in the list unless compiled in, so
889 + * the average user (w/out VLANs) will not be adversely affected.
906 +static DEFINE_SPINLOCK(ptype_lock);
907 +static struct list_head ptype_base[16] __read_mostly; /* 16 way hashed list */
908 +static struct list_head ptype_all __read_mostly; /* Taps */
910 +#ifdef CONFIG_NET_DMA
912 + struct dma_client client;
914 + cpumask_t channel_mask;
915 + struct dma_chan *channels[NR_CPUS];
918 +static enum dma_state_client
919 +netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
920 + enum dma_state state);
922 +static struct net_dma net_dma = {
924 + .event_callback = netdev_dma_event,
930 + * The @dev_base_head list is protected by @dev_base_lock and the rtnl
933 + * Pure readers hold dev_base_lock for reading.
935 + * Writers must hold the rtnl semaphore while they loop through the
936 + * dev_base_head list, and hold dev_base_lock for writing when they do the
937 + * actual updates. This allows pure readers to access the list even
938 + * while a writer is preparing to update it.
940 + * To put it another way, dev_base_lock is held for writing only to
941 + * protect against pure readers; the rtnl semaphore provides the
942 + * protection against other writers.
944 + * See, for example usages, register_netdevice() and
945 + * unregister_netdevice(), which must be called with the rtnl
948 +DEFINE_RWLOCK(dev_base_lock);
950 +EXPORT_SYMBOL(dev_base_lock);
952 +#define NETDEV_HASHBITS 8
953 +#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
955 +static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
957 + unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
958 + return &net->dev_name_head[hash & ((1 << NETDEV_HASHBITS) - 1)];
961 +static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
963 + return &net->dev_index_head[ifindex & ((1 << NETDEV_HASHBITS) - 1)];
966 +/* Device list insertion */
967 +static int list_netdevice(struct net_device *dev)
969 + struct net *net = dev->nd_net;
973 + write_lock_bh(&dev_base_lock);
974 + list_add_tail(&dev->dev_list, &net->dev_base_head);
975 + hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
976 + hlist_add_head(&dev->index_hlist, dev_index_hash(net, dev->ifindex));
977 + write_unlock_bh(&dev_base_lock);
981 +/* Device list removal */
982 +static void unlist_netdevice(struct net_device *dev)
986 + /* Unlink dev from the device chain */
987 + write_lock_bh(&dev_base_lock);
988 + list_del(&dev->dev_list);
989 + hlist_del(&dev->name_hlist);
990 + hlist_del(&dev->index_hlist);
991 + write_unlock_bh(&dev_base_lock);
995 + * Our notifier list
998 +static RAW_NOTIFIER_HEAD(netdev_chain);
1001 + * Device drivers call our routines to queue packets here. We empty the
1002 + * queue in the local softnet handler.
1004 +DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
1006 +#ifdef CONFIG_SYSFS
1007 +extern int netdev_sysfs_init(void);
1008 +extern int netdev_register_sysfs(struct net_device *);
1009 +extern void netdev_unregister_sysfs(struct net_device *);
1011 +#define netdev_sysfs_init() (0)
1012 +#define netdev_register_sysfs(dev) (0)
1013 +#define netdev_unregister_sysfs(dev) do { } while(0)
1016 +#ifdef CONFIG_DEBUG_LOCK_ALLOC
1018 + * register_netdevice() inits dev->_xmit_lock and sets lockdep class
1019 + * according to dev->type
1021 +static const unsigned short netdev_lock_type[] =
1022 + {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
1023 + ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
1024 + ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
1025 + ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
1026 + ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
1027 + ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
1028 + ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
1029 + ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
1030 + ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
1031 + ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
1032 + ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
1033 + ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
1034 + ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
1035 + ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
1038 +static const char *netdev_lock_name[] =
1039 + {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
1040 + "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
1041 + "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
1042 + "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
1043 + "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
1044 + "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
1045 + "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
1046 + "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
1047 + "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
1048 + "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
1049 + "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
1050 + "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
1051 + "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
1052 + "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
1055 +static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
1057 +static inline unsigned short netdev_lock_pos(unsigned short dev_type)
1061 + for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
1062 + if (netdev_lock_type[i] == dev_type)
1064 + /* the last key is used by default */
1065 + return ARRAY_SIZE(netdev_lock_type) - 1;
1068 +static inline void netdev_set_lockdep_class(spinlock_t *lock,
1069 + unsigned short dev_type)
1073 + i = netdev_lock_pos(dev_type);
1074 + lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
1075 + netdev_lock_name[i]);
1078 +static inline void netdev_set_lockdep_class(spinlock_t *lock,
1079 + unsigned short dev_type)
1084 +/*******************************************************************************
1086 + Protocol management and registration routines
1088 +*******************************************************************************/
1091 + * Add a protocol ID to the list. Now that the input handler is
1092 + * smarter we can dispense with all the messy stuff that used to be
1095 + * BEWARE!!! Protocol handlers, mangling input packets,
1096 + * MUST BE last in hash buckets and checking protocol handlers
1097 + * MUST start from promiscuous ptype_all chain in net_bh.
1098 + * It is true now, do not change it.
1099 + * Explanation follows: if protocol handler, mangling packet, will
1100 + * be the first on list, it is not able to sense, that packet
1101 + * is cloned and should be copied-on-write, so that it will
1102 + * change it and subsequent readers will get broken packet.
1107 + * dev_add_pack - add packet handler
1108 + * @pt: packet type declaration
1110 + * Add a protocol handler to the networking stack. The passed &packet_type
1111 + * is linked into kernel lists and may not be freed until it has been
1112 + * removed from the kernel lists.
1114 + * This call does not sleep therefore it can not
1115 + * guarantee all CPU's that are in middle of receiving packets
1116 + * will see the new packet type (until the next received packet).
1119 +void dev_add_pack(struct packet_type *pt)
1123 + spin_lock_bh(&ptype_lock);
1124 + if (pt->type == htons(ETH_P_ALL))
1125 + list_add_rcu(&pt->list, &ptype_all);
1127 + hash = ntohs(pt->type) & 15;
1128 + list_add_rcu(&pt->list, &ptype_base[hash]);
1130 + spin_unlock_bh(&ptype_lock);
1134 + * __dev_remove_pack - remove packet handler
1135 + * @pt: packet type declaration
1137 + * Remove a protocol handler that was previously added to the kernel
1138 + * protocol handlers by dev_add_pack(). The passed &packet_type is removed
1139 + * from the kernel lists and can be freed or reused once this function
1142 + * The packet type might still be in use by receivers
1143 + * and must not be freed until after all the CPU's have gone
1144 + * through a quiescent state.
1146 +void __dev_remove_pack(struct packet_type *pt)
1148 + struct list_head *head;
1149 + struct packet_type *pt1;
1151 + spin_lock_bh(&ptype_lock);
1153 + if (pt->type == htons(ETH_P_ALL))
1154 + head = &ptype_all;
1156 + head = &ptype_base[ntohs(pt->type) & 15];
1158 + list_for_each_entry(pt1, head, list) {
1160 + list_del_rcu(&pt->list);
1165 + printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
1167 + spin_unlock_bh(&ptype_lock);
1170 + * dev_remove_pack - remove packet handler
1171 + * @pt: packet type declaration
1173 + * Remove a protocol handler that was previously added to the kernel
1174 + * protocol handlers by dev_add_pack(). The passed &packet_type is removed
1175 + * from the kernel lists and can be freed or reused once this function
1178 + * This call sleeps to guarantee that no CPU is looking at the packet
1179 + * type after return.
1181 +void dev_remove_pack(struct packet_type *pt)
1183 + __dev_remove_pack(pt);
1185 + synchronize_net();
1188 +/******************************************************************************
1190 + Device Boot-time Settings Routines
1192 +*******************************************************************************/
1194 +/* Boot time configuration table */
1195 +static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
1198 + * netdev_boot_setup_add - add new setup entry
1199 + * @name: name of the device
1200 + * @map: configured settings for the device
1202 + * Adds new setup entry to the dev_boot_setup list. The function
1203 + * returns 0 on error and 1 on success. This is a generic routine to
1206 +static int netdev_boot_setup_add(char *name, struct ifmap *map)
1208 + struct netdev_boot_setup *s;
1211 + s = dev_boot_setup;
1212 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
1213 + if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
1214 + memset(s[i].name, 0, sizeof(s[i].name));
1215 + strcpy(s[i].name, name);
1216 + memcpy(&s[i].map, map, sizeof(s[i].map));
1221 + return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
1225 + * netdev_boot_setup_check - check boot time settings
1226 + * @dev: the netdevice
1228 + * Check boot time settings for the device.
1229 + * The found settings are set for the device to be used
1230 + * later in the device probing.
1231 + * Returns 0 if no settings found, 1 if they are.
1233 +int netdev_boot_setup_check(struct net_device *dev)
1235 + struct netdev_boot_setup *s = dev_boot_setup;
1238 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
1239 + if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
1240 + !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
1241 + dev->irq = s[i].map.irq;
1242 + dev->base_addr = s[i].map.base_addr;
1243 + dev->mem_start = s[i].map.mem_start;
1244 + dev->mem_end = s[i].map.mem_end;
1253 + * netdev_boot_base - get address from boot time settings
1254 + * @prefix: prefix for network device
1255 + * @unit: id for network device
1257 + * Check boot time settings for the base address of device.
1258 + * The found settings are set for the device to be used
1259 + * later in the device probing.
1260 + * Returns 0 if no settings found.
1262 +unsigned long netdev_boot_base(const char *prefix, int unit)
1264 + const struct netdev_boot_setup *s = dev_boot_setup;
1265 + char name[IFNAMSIZ];
1268 + sprintf(name, "%s%d", prefix, unit);
1271 + * If device already registered then return base of 1
1272 + * to indicate not to probe for this interface
1274 + if (__dev_get_by_name(&init_net, name))
1277 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
1278 + if (!strcmp(name, s[i].name))
1279 + return s[i].map.base_addr;
1284 + * Saves at boot time configured settings for any netdevice.
1286 +int __init netdev_boot_setup(char *str)
1291 + str = get_options(str, ARRAY_SIZE(ints), ints);
1292 + if (!str || !*str)
1295 + /* Save settings */
1296 + memset(&map, 0, sizeof(map));
1298 + map.irq = ints[1];
1300 + map.base_addr = ints[2];
1302 + map.mem_start = ints[3];
1304 + map.mem_end = ints[4];
1306 + /* Add new entry to the list */
1307 + return netdev_boot_setup_add(str, &map);
1310 +__setup("netdev=", netdev_boot_setup);
1312 +/*******************************************************************************
1314 + Device Interface Subroutines
1316 +*******************************************************************************/
1319 + * __dev_get_by_name - find a device by its name
1320 + * @name: name to find
1322 + * Find an interface by name. Must be called under RTNL semaphore
1323 + * or @dev_base_lock. If the name is found a pointer to the device
1324 + * is returned. If the name is not found then %NULL is returned. The
1325 + * reference counters are not incremented so the caller must be
1326 + * careful with locks.
1329 +struct net_device *__dev_get_by_name(struct net *net, const char *name)
1331 + struct hlist_node *p;
1333 + hlist_for_each(p, dev_name_hash(net, name)) {
1334 + struct net_device *dev
1335 + = hlist_entry(p, struct net_device, name_hlist);
1336 + if (!strncmp(dev->name, name, IFNAMSIZ))
1343 + * dev_get_by_name - find a device by its name
1344 + * @name: name to find
1346 + * Find an interface by name. This can be called from any
1347 + * context and does its own locking. The returned handle has
1348 + * the usage count incremented and the caller must use dev_put() to
1349 + * release it when it is no longer needed. %NULL is returned if no
1350 + * matching device is found.
1353 +struct net_device *dev_get_by_name(struct net *net, const char *name)
1355 + struct net_device *dev;
1357 + read_lock(&dev_base_lock);
1358 + dev = __dev_get_by_name(net, name);
1361 + read_unlock(&dev_base_lock);
1366 + * __dev_get_by_index - find a device by its ifindex
1367 + * @ifindex: index of device
1369 + * Search for an interface by index. Returns %NULL if the device
1370 + * is not found or a pointer to the device. The device has not
1371 + * had its reference counter increased so the caller must be careful
1372 + * about locking. The caller must hold either the RTNL semaphore
1373 + * or @dev_base_lock.
1376 +struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1378 + struct hlist_node *p;
1380 + hlist_for_each(p, dev_index_hash(net, ifindex)) {
1381 + struct net_device *dev
1382 + = hlist_entry(p, struct net_device, index_hlist);
1383 + if (dev->ifindex == ifindex)
1391 + * dev_get_by_index - find a device by its ifindex
1392 + * @ifindex: index of device
1394 + * Search for an interface by index. Returns NULL if the device
1395 + * is not found or a pointer to the device. The device returned has
1396 + * had a reference added and the pointer is safe until the user calls
1397 + * dev_put to indicate they have finished with it.
1400 +struct net_device *dev_get_by_index(struct net *net, int ifindex)
1402 + struct net_device *dev;
1404 + read_lock(&dev_base_lock);
1405 + dev = __dev_get_by_index(net, ifindex);
1408 + read_unlock(&dev_base_lock);
1413 + * dev_getbyhwaddr - find a device by its hardware address
1414 + * @type: media type of device
1415 + * @ha: hardware address
1417 + * Search for an interface by MAC address. Returns NULL if the device
1418 + * is not found or a pointer to the device. The caller must hold the
1419 + * rtnl semaphore. The returned device has not had its ref count increased
1420 + * and the caller must therefore be careful about locking
1423 + * If the API was consistent this would be __dev_get_by_hwaddr
1426 +struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
1428 + struct net_device *dev;
1432 + for_each_netdev(&init_net, dev)
1433 + if (dev->type == type &&
1434 + !memcmp(dev->dev_addr, ha, dev->addr_len))
1440 +EXPORT_SYMBOL(dev_getbyhwaddr);
1442 +struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
1444 + struct net_device *dev;
1447 + for_each_netdev(net, dev)
1448 + if (dev->type == type)
1454 +EXPORT_SYMBOL(__dev_getfirstbyhwtype);
1456 +struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
1458 + struct net_device *dev;
1461 + dev = __dev_getfirstbyhwtype(net, type);
1468 +EXPORT_SYMBOL(dev_getfirstbyhwtype);
1471 + * dev_get_by_flags - find any device with given flags
1472 + * @if_flags: IFF_* values
1473 + * @mask: bitmask of bits in if_flags to check
1475 + * Search for any interface with the given flags. Returns NULL if a device
1476 + * is not found or a pointer to the device. The device returned has
1477 + * had a reference added and the pointer is safe until the user calls
1478 + * dev_put to indicate they have finished with it.
1481 +struct net_device * dev_get_by_flags(struct net *net, unsigned short if_flags, unsigned short mask)
1483 + struct net_device *dev, *ret;
1486 + read_lock(&dev_base_lock);
1487 + for_each_netdev(net, dev) {
1488 + if (((dev->flags ^ if_flags) & mask) == 0) {
1494 + read_unlock(&dev_base_lock);
1499 + * dev_valid_name - check if name is okay for network device
1500 + * @name: name string
1502 + * Network device names need to be valid file names to
1503 + * to allow sysfs to work. We also disallow any kind of
1506 +int dev_valid_name(const char *name)
1508 + if (*name == '\0')
1510 + if (strlen(name) >= IFNAMSIZ)
1512 + if (!strcmp(name, ".") || !strcmp(name, ".."))
1516 + if (*name == '/' || isspace(*name))
1524 + * __dev_alloc_name - allocate a name for a device
1525 + * @net: network namespace to allocate the device name in
1526 + * @name: name format string
1527 + * @buf: scratch buffer and result name string
1529 + * Passed a format string - eg "lt%d" it will try and find a suitable
1530 + * id. It scans list of devices to build up a free map, then chooses
1531 + * the first empty slot. The caller must hold the dev_base or rtnl lock
1532 + * while allocating the name and adding the device in order to avoid
1534 + * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1535 + * Returns the number of the unit assigned or a negative errno code.
1538 +static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1542 + const int max_netdevices = 8*PAGE_SIZE;
1544 + struct net_device *d;
1546 + p = strnchr(name, IFNAMSIZ-1, '%');
1549 + * Verify the string as this thing may have come from
1550 + * the user. There must be either one "%d" and no other "%"
1553 + if (p[1] != 'd' || strchr(p + 2, '%'))
1556 + /* Use one page as a bit array of possible slots */
1557 + inuse = (long *) get_zeroed_page(GFP_ATOMIC);
1561 + for_each_netdev(net, d) {
1562 + if (!sscanf(d->name, name, &i))
1564 + if (i < 0 || i >= max_netdevices)
1567 + /* avoid cases where sscanf is not exact inverse of printf */
1568 + snprintf(buf, IFNAMSIZ, name, i);
1569 + if (!strncmp(buf, d->name, IFNAMSIZ))
1570 + set_bit(i, inuse);
1573 + i = find_first_zero_bit(inuse, max_netdevices);
1574 + free_page((unsigned long) inuse);
1577 + snprintf(buf, IFNAMSIZ, name, i);
1578 + if (!__dev_get_by_name(net, buf))
1581 + /* It is possible to run out of possible slots
1582 + * when the name is long and there isn't enough space left
1583 + * for the digits, or if all bits are used.
1589 + * dev_alloc_name - allocate a name for a device
1591 + * @name: name format string
1593 + * Passed a format string - eg "lt%d" it will try and find a suitable
1594 + * id. It scans list of devices to build up a free map, then chooses
1595 + * the first empty slot. The caller must hold the dev_base or rtnl lock
1596 + * while allocating the name and adding the device in order to avoid
1598 + * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1599 + * Returns the number of the unit assigned or a negative errno code.
1602 +int dev_alloc_name(struct net_device *dev, const char *name)
1604 + char buf[IFNAMSIZ];
1608 + BUG_ON(!dev->nd_net);
1609 + net = dev->nd_net;
1610 + ret = __dev_alloc_name(net, name, buf);
1612 + strlcpy(dev->name, buf, IFNAMSIZ);
1618 + * dev_change_name - change name of a device
1620 + * @newname: name (or format string) must be at least IFNAMSIZ
1622 + * Change name of a device, can pass format strings "eth%d".
1623 + * for wildcarding.
1625 +int dev_change_name(struct net_device *dev, char *newname)
1631 + BUG_ON(!dev->nd_net);
1633 + net = dev->nd_net;
1634 + if (dev->flags & IFF_UP)
1637 + if (!dev_valid_name(newname))
1640 + if (strchr(newname, '%')) {
1641 + err = dev_alloc_name(dev, newname);
1644 + strcpy(newname, dev->name);
1646 + else if (__dev_get_by_name(net, newname))
1649 + if (strncmp(newname, dev->name, IFNAMSIZ))
1650 + printk(KERN_INFO "%s renamed to %s\n",
1651 + dev->name, newname);
1652 + strlcpy(dev->name, newname, IFNAMSIZ);
1655 + device_rename(&dev->dev, dev->name);
1656 + hlist_del(&dev->name_hlist);
1657 + hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
1658 + raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
1664 + * netdev_features_change - device changes features
1665 + * @dev: device to cause notification
1667 + * Called to indicate a device has changed features.
1669 +void netdev_features_change(struct net_device *dev)
1671 + raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
1673 +EXPORT_SYMBOL(netdev_features_change);
1676 + * netdev_state_change - device changes state
1677 + * @dev: device to cause notification
1679 + * Called to indicate a device has changed state. This function calls
1680 + * the notifier chains for netdev_chain and sends a NEWLINK message
1681 + * to the routing socket.
1683 +void netdev_state_change(struct net_device *dev)
1685 + if (dev->flags & IFF_UP) {
1686 + raw_notifier_call_chain(&netdev_chain,
1687 + NETDEV_CHANGE, dev);
1688 + rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1693 + * dev_load - load a network module
1694 + * @name: name of interface
1696 + * If a network interface is not present and the process has suitable
1697 + * privileges this function loads the module. If module loading is not
1698 + * available in this kernel then it becomes a nop.
1701 +void dev_load(struct net *net, const char *name)
1703 + struct net_device *dev;
1705 + read_lock(&dev_base_lock);
1706 + dev = __dev_get_by_name(net, name);
1707 + read_unlock(&dev_base_lock);
1709 + if (!dev && capable(CAP_SYS_MODULE))
1710 + request_module("%s", name);
1713 +static int default_rebuild_header(struct sk_buff *skb)
1715 + printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
1716 + skb->dev ? skb->dev->name : "NULL!!!");
1722 + * dev_open - prepare an interface for use.
1723 + * @dev: device to open
1725 + * Takes a device from down to up state. The device's private open
1726 + * function is invoked and then the multicast lists are loaded. Finally
1727 + * the device is moved into the up state and a %NETDEV_UP message is
1728 + * sent to the netdev notifier chain.
1730 + * Calling this function on an active interface is a nop. On a failure
1731 + * a negative errno code is returned.
1733 +int dev_open(struct net_device *dev)
1738 + * Is it already up?
1741 + if (dev->flags & IFF_UP)
1745 + * Is it even present?
1747 + if (!netif_device_present(dev))
1751 + * Call device private open method
1753 + set_bit(__LINK_STATE_START, &dev->state);
1755 + ret = dev->open(dev);
1757 + clear_bit(__LINK_STATE_START, &dev->state);
1761 + * If it went open OK then:
1768 + dev->flags |= IFF_UP;
1771 + * Initialize multicasting status
1773 + dev_mc_upload(dev);
1776 + * Wakeup transmit queue engine
1778 + dev_activate(dev);
1781 + * ... and announce new interface.
1783 + raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
1789 + * dev_close - shutdown an interface.
1790 + * @dev: device to shutdown
1792 + * This function moves an active device into down state. A
1793 + * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1794 + * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1797 +int dev_close(struct net_device *dev)
1799 + if (!(dev->flags & IFF_UP))
1803 + * Tell people we are going down, so that they can
1804 + * prepare to death, when device is still operating.
1806 + raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
1808 + dev_deactivate(dev);
1810 + clear_bit(__LINK_STATE_START, &dev->state);
1812 + /* Synchronize to scheduled poll. We cannot touch poll list,
1813 + * it can be even on different cpu. So just clear netif_running(),
1814 + * and wait when poll really will happen. Actually, the best place
1815 + * for this is inside dev->stop() after device stopped its irq
1816 + * engine, but this requires more changes in devices. */
1818 + smp_mb__after_clear_bit(); /* Commit netif_running(). */
1819 + while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
1825 + * Call the device specific close. This cannot fail.
1826 + * Only if device is UP
1828 + * We allow it to be called even after a DETACH hot-plug
1835 + * Device is now down.
1838 + dev->flags &= ~IFF_UP;
1841 + * Tell people we are down
1843 + raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
1849 +static int dev_boot_phase = 1;
1852 + * Device change register/unregister. These are not inline or static
1853 + * as we export them to the world.
1857 + * register_netdevice_notifier - register a network notifier block
1860 + * Register a notifier to be called when network device events occur.
1861 + * The notifier passed is linked into the kernel structures and must
1862 + * not be reused until it has been unregistered. A negative errno code
1863 + * is returned on a failure.
1865 + * When registered all registration and up events are replayed
1866 + * to the new notifier to allow device to have a race free
1867 + * view of the network device list.
1870 +int register_netdevice_notifier(struct notifier_block *nb)
1872 + struct net_device *dev;
1876 + err = raw_notifier_chain_register(&netdev_chain, nb);
1877 + if (!err && !dev_boot_phase) {
1879 + for_each_net(net) {
1880 + for_each_netdev(net, dev) {
1881 + nb->notifier_call(nb, NETDEV_REGISTER, dev);
1883 + if (dev->flags & IFF_UP)
1884 + nb->notifier_call(nb, NETDEV_UP, dev);
1893 + * unregister_netdevice_notifier - unregister a network notifier block
1896 + * Unregister a notifier previously registered by
1897 + * register_netdevice_notifier(). The notifier is unlinked into the
1898 + * kernel structures and may then be reused. A negative errno code
1899 + * is returned on a failure.
1902 +int unregister_netdevice_notifier(struct notifier_block *nb)
1907 + err = raw_notifier_chain_unregister(&netdev_chain, nb);
1913 + * call_netdevice_notifiers - call all network notifier blocks
1914 + * @val: value passed unmodified to notifier function
1915 + * @v: pointer passed unmodified to notifier function
1917 + * Call all network notifier blocks. Parameters and return value
1918 + * are as for raw_notifier_call_chain().
1921 +int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1923 + return raw_notifier_call_chain(&netdev_chain, val, dev);
1926 +/* When > 0 there are consumers of rx skb time stamps */
1927 +static atomic_t netstamp_needed = ATOMIC_INIT(0);
1929 +void net_enable_timestamp(void)
1931 + atomic_inc(&netstamp_needed);
1934 +void net_disable_timestamp(void)
1936 + atomic_dec(&netstamp_needed);
1939 +static inline void net_timestamp(struct sk_buff *skb)
1941 + if (atomic_read(&netstamp_needed))
1942 + __net_timestamp(skb);
1944 + skb->tstamp.tv64 = 0;
1948 + * Support routine. Sends outgoing frames to any network
1949 + * taps currently in use.
1952 +static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1954 + struct packet_type *ptype;
1956 + net_timestamp(skb);
1959 + list_for_each_entry_rcu(ptype, &ptype_all, list) {
1960 + /* Never send packets back to the socket
1961 + * they originated from - MvS (miquels@drinkel.ow.org)
1963 + if ((ptype->dev == dev || !ptype->dev) &&
1964 + (ptype->af_packet_priv == NULL ||
1965 + (struct sock *)ptype->af_packet_priv != skb->sk)) {
1966 + struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1970 + /* skb->nh should be correctly
1971 + set by sender, so that the second statement is
1972 + just protection against buggy protocols.
1974 + skb_reset_mac_header(skb2);
1976 + if (skb_network_header(skb2) < skb2->data ||
1977 + skb2->network_header > skb2->tail) {
1978 + if (net_ratelimit())
1979 + printk(KERN_CRIT "protocol %04x is "
1980 + "buggy, dev %s\n",
1981 + skb2->protocol, dev->name);
1982 + skb_reset_network_header(skb2);
1985 + skb2->transport_header = skb2->network_header;
1986 + skb2->pkt_type = PACKET_OUTGOING;
1987 + ptype->func(skb2, skb->dev, ptype, skb->dev);
1990 + rcu_read_unlock();
1994 +void __netif_schedule(struct net_device *dev)
1996 + if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1997 + unsigned long flags;
1998 + struct softnet_data *sd;
2000 + local_irq_save(flags);
2001 + sd = &__get_cpu_var(softnet_data);
2002 + dev->next_sched = sd->output_queue;
2003 + sd->output_queue = dev;
2004 + raise_softirq_irqoff(NET_TX_SOFTIRQ);
2005 + local_irq_restore(flags);
2008 +EXPORT_SYMBOL(__netif_schedule);
2010 +void __netif_rx_schedule(struct net_device *dev)
2012 + unsigned long flags;
2014 + local_irq_save(flags);
2016 + list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
2017 + if (dev->quota < 0)
2018 + dev->quota += dev->weight;
2020 + dev->quota = dev->weight;
2021 + __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2022 + local_irq_restore(flags);
2024 +EXPORT_SYMBOL(__netif_rx_schedule);
2026 +void dev_kfree_skb_any(struct sk_buff *skb)
2028 + if (in_irq() || irqs_disabled())
2029 + dev_kfree_skb_irq(skb);
2031 + dev_kfree_skb(skb);
2033 +EXPORT_SYMBOL(dev_kfree_skb_any);
2036 +/* Hot-plugging. */
2037 +void netif_device_detach(struct net_device *dev)
2039 + if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2040 + netif_running(dev)) {
2041 + netif_stop_queue(dev);
2044 +EXPORT_SYMBOL(netif_device_detach);
2046 +void netif_device_attach(struct net_device *dev)
2048 + if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2049 + netif_running(dev)) {
2050 + netif_wake_queue(dev);
2051 + __netdev_watchdog_up(dev);
2054 +EXPORT_SYMBOL(netif_device_attach);
2058 + * Invalidate hardware checksum when packet is to be mangled, and
2059 + * complete checksum manually on outgoing path.
2061 +int skb_checksum_help(struct sk_buff *skb)
2064 + int ret = 0, offset;
2066 + if (skb->ip_summed == CHECKSUM_COMPLETE)
2067 + goto out_set_summed;
2069 + if (unlikely(skb_shinfo(skb)->gso_size)) {
2070 + /* Let GSO fix up the checksum. */
2071 + goto out_set_summed;
2074 + if (skb_cloned(skb)) {
2075 + ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2080 + offset = skb->csum_start - skb_headroom(skb);
2081 + BUG_ON(offset > (int)skb->len);
2082 + csum = skb_checksum(skb, offset, skb->len-offset, 0);
2084 + offset = skb_headlen(skb) - offset;
2085 + BUG_ON(offset <= 0);
2086 + BUG_ON(skb->csum_offset + 2 > offset);
2088 + *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) =
2091 + skb->ip_summed = CHECKSUM_NONE;
2097 + * skb_gso_segment - Perform segmentation on skb.
2098 + * @skb: buffer to segment
2099 + * @features: features for the output path (see dev->features)
2101 + * This function segments the given skb and returns a list of segments.
2103 + * It may return NULL if the skb requires no segmentation. This is
2104 + * only possible when GSO is used for verifying header integrity.
2106 +struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
2108 + struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2109 + struct packet_type *ptype;
2110 + __be16 type = skb->protocol;
2113 + BUG_ON(skb_shinfo(skb)->frag_list);
2115 + skb_reset_mac_header(skb);
2116 + skb->mac_len = skb->network_header - skb->mac_header;
2117 + __skb_pull(skb, skb->mac_len);
2119 + if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
2120 + if (skb_header_cloned(skb) &&
2121 + (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
2122 + return ERR_PTR(err);
2126 + list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
2127 + if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
2128 + if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
2129 + err = ptype->gso_send_check(skb);
2130 + segs = ERR_PTR(err);
2131 + if (err || skb_gso_ok(skb, features))
2133 + __skb_push(skb, (skb->data -
2134 + skb_network_header(skb)));
2136 + segs = ptype->gso_segment(skb, features);
2140 + rcu_read_unlock();
2142 + __skb_push(skb, skb->data - skb_mac_header(skb));
2147 +EXPORT_SYMBOL(skb_gso_segment);
2149 +/* Take action when hardware reception checksum errors are detected. */
2151 +void netdev_rx_csum_fault(struct net_device *dev)
2153 + if (net_ratelimit()) {
2154 + printk(KERN_ERR "%s: hw csum failure.\n",
2155 + dev ? dev->name : "<unknown>");
2159 +EXPORT_SYMBOL(netdev_rx_csum_fault);
2162 +/* Actually, we should eliminate this check as soon as we know, that:
2163 + * 1. IOMMU is present and allows to map all the memory.
2164 + * 2. No high memory really exists on this machine.
2167 +static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
2169 +#ifdef CONFIG_HIGHMEM
2172 + if (dev->features & NETIF_F_HIGHDMA)
2175 + for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2176 + if (PageHighMem(skb_shinfo(skb)->frags[i].page))
2183 +struct dev_gso_cb {
2184 + void (*destructor)(struct sk_buff *skb);
2187 +#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
2189 +static void dev_gso_skb_destructor(struct sk_buff *skb)
2191 + struct dev_gso_cb *cb;
2194 + struct sk_buff *nskb = skb->next;
2196 + skb->next = nskb->next;
2197 + nskb->next = NULL;
2199 + } while (skb->next);
2201 + cb = DEV_GSO_CB(skb);
2202 + if (cb->destructor)
2203 + cb->destructor(skb);
2207 + * dev_gso_segment - Perform emulated hardware segmentation on skb.
2208 + * @skb: buffer to segment
2210 + * This function segments the given skb and stores the list of segments
2213 +static int dev_gso_segment(struct sk_buff *skb)
2215 + struct net_device *dev = skb->dev;
2216 + struct sk_buff *segs;
2217 + int features = dev->features & ~(illegal_highdma(dev, skb) ?
2220 + segs = skb_gso_segment(skb, features);
2222 + /* Verifying header integrity only. */
2226 + if (unlikely(IS_ERR(segs)))
2227 + return PTR_ERR(segs);
2230 + DEV_GSO_CB(skb)->destructor = skb->destructor;
2231 + skb->destructor = dev_gso_skb_destructor;
2236 +int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
2238 + if (likely(!skb->next)) {
2239 + if (!list_empty(&ptype_all))
2240 + dev_queue_xmit_nit(skb, dev);
2242 + if (netif_needs_gso(dev, skb)) {
2243 + if (unlikely(dev_gso_segment(skb)))
2244 + goto out_kfree_skb;
2249 + return dev->hard_start_xmit(skb, skb->dev);
2254 + struct sk_buff *nskb = skb->next;
2257 + skb->next = nskb->next;
2258 + nskb->next = NULL;
2259 + rc = dev->hard_start_xmit(nskb, dev);
2260 + if (unlikely(rc)) {
2261 + nskb->next = skb->next;
2265 + if (unlikely(netif_queue_stopped(dev) && skb->next))
2266 + return NETDEV_TX_BUSY;
2267 + } while (skb->next);
2269 + skb->destructor = DEV_GSO_CB(skb)->destructor;
2276 +#define HARD_TX_LOCK(dev, cpu) { \
2277 + if ((dev->features & NETIF_F_LLTX) == 0) { \
2278 + netif_tx_lock(dev); \
2282 +#define HARD_TX_UNLOCK(dev) { \
2283 + if ((dev->features & NETIF_F_LLTX) == 0) { \
2284 + netif_tx_unlock(dev); \
2289 + * dev_queue_xmit - transmit a buffer
2290 + * @skb: buffer to transmit
2292 + * Queue a buffer for transmission to a network device. The caller must
2293 + * have set the device and priority and built the buffer before calling
2294 + * this function. The function can be called from an interrupt.
2296 + * A negative errno code is returned on a failure. A success does not
2297 + * guarantee the frame will be transmitted as it may be dropped due
2298 + * to congestion or traffic shaping.
2300 + * -----------------------------------------------------------------------------------
2301 + * I notice this method can also return errors from the queue disciplines,
2302 + * including NET_XMIT_DROP, which is a positive value. So, errors can also
2305 + * Regardless of the return value, the skb is consumed, so it is currently
2306 + * difficult to retry a send to this method. (You can bump the ref count
2307 + * before sending to hold a reference for retry if you are careful.)
2309 + * When calling this method, interrupts MUST be enabled. This is because
2310 + * the BH enable code must have IRQs enabled so that it will not deadlock.
2314 +unsigned int sb_hook (struct sk_buff *skb, struct packet_type *pt,struct net_device *orig_dev);
2316 +int dev_queue_xmit(struct sk_buff *skb)
2318 + struct net_device *dev = skb->dev;
2322 + sb_hook(skb, NULL, NULL);
2324 + /* GSO will handle the following emulations directly. */
2325 + if (netif_needs_gso(dev, skb))
2328 + if (skb_shinfo(skb)->frag_list &&
2329 + !(dev->features & NETIF_F_FRAGLIST) &&
2330 + __skb_linearize(skb))
2331 + goto out_kfree_skb;
2333 + /* Fragmented skb is linearized if device does not support SG,
2334 + * or if at least one of fragments is in highmem and device
2335 + * does not support DMA from it.
2337 + if (skb_shinfo(skb)->nr_frags &&
2338 + (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
2339 + __skb_linearize(skb))
2340 + goto out_kfree_skb;
2342 + /* If packet is not checksummed and device does not support
2343 + * checksumming for this protocol, complete checksumming here.
2345 + if (skb->ip_summed == CHECKSUM_PARTIAL) {
2346 + skb_set_transport_header(skb, skb->csum_start -
2347 + skb_headroom(skb));
2349 + if (!(dev->features & NETIF_F_GEN_CSUM)
2350 + || ((dev->features & NETIF_F_IP_CSUM)
2351 + && skb->protocol == htons(ETH_P_IP))
2352 + || ((dev->features & NETIF_F_IPV6_CSUM)
2353 + && skb->protocol == htons(ETH_P_IPV6)))
2354 + if (skb_checksum_help(skb))
2355 + goto out_kfree_skb;
2359 + spin_lock_prefetch(&dev->queue_lock);
2361 + /* Disable soft irqs for various locks below. Also
2362 + * stops preemption for RCU.
2364 + rcu_read_lock_bh();
2366 + /* Updates of qdisc are serialized by queue_lock.
2367 + * The struct Qdisc which is pointed to by qdisc is now a
2368 + * rcu structure - it may be accessed without acquiring
2369 + * a lock (but the structure may be stale.) The freeing of the
2370 + * qdisc will be deferred until it's known that there are no
2371 + * more references to it.
2373 + * If the qdisc has an enqueue function, we still need to
2374 + * hold the queue_lock before calling it, since queue_lock
2375 + * also serializes access to the device queue.
2378 + q = rcu_dereference(dev->qdisc);
2379 +#ifdef CONFIG_NET_CLS_ACT
2380 + skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
2383 + /* Grab device queue */
2384 + spin_lock(&dev->queue_lock);
2387 + rc = q->enqueue(skb, q);
2389 + spin_unlock(&dev->queue_lock);
2391 + rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
2394 + spin_unlock(&dev->queue_lock);
2397 + /* The device has no queue. Common case for software devices:
2398 + loopback, all the sorts of tunnels...
2400 + Really, it is unlikely that netif_tx_lock protection is necessary
2401 + here. (f.e. loopback and IP tunnels are clean ignoring statistics
2403 + However, it is possible, that they rely on protection
2406 + Check this and shot the lock. It is not prone from deadlocks.
2407 + Either shot noqueue qdisc, it is even simpler 8)
2409 + if (dev->flags & IFF_UP) {
2410 + int cpu = smp_processor_id(); /* ok because BHs are off */
2412 + if (dev->xmit_lock_owner != cpu) {
2414 + HARD_TX_LOCK(dev, cpu);
2416 + if (!netif_queue_stopped(dev)) {
2418 + if (!dev_hard_start_xmit(skb, dev)) {
2419 + HARD_TX_UNLOCK(dev);
2423 + HARD_TX_UNLOCK(dev);
2424 + if (net_ratelimit())
2425 + printk(KERN_CRIT "Virtual device %s asks to "
2426 + "queue packet!\n", dev->name);
2428 + /* Recursion is detected! It is possible,
2429 + * unfortunately */
2430 + if (net_ratelimit())
2431 + printk(KERN_CRIT "Dead loop on virtual device "
2432 + "%s, fix it urgently!\n", dev->name);
2437 + rcu_read_unlock_bh();
2443 + rcu_read_unlock_bh();
2448 +/*=======================================================================
2450 + =======================================================================*/
2452 +int netdev_max_backlog __read_mostly = 1000;
2453 +int netdev_budget __read_mostly = 300;
2454 +int weight_p __read_mostly = 64; /* old backlog weight */
2456 +DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
2460 + * netif_rx - post buffer to the network code
2461 + * @skb: buffer to post
2463 + * This function receives a packet from a device driver and queues it for
2464 + * the upper (protocol) levels to process. It always succeeds. The buffer
2465 + * may be dropped during processing for congestion control or by the
2466 + * protocol layers.
2469 + * NET_RX_SUCCESS (no congestion)
2470 + * NET_RX_CN_LOW (low congestion)
2471 + * NET_RX_CN_MOD (moderate congestion)
2472 + * NET_RX_CN_HIGH (high congestion)
2473 + * NET_RX_DROP (packet was dropped)
2477 +int netif_rx(struct sk_buff *skb)
2479 + struct softnet_data *queue;
2480 + unsigned long flags;
2482 + /* if netpoll wants it, pretend we never saw it */
2483 + if (netpoll_rx(skb))
2484 + return NET_RX_DROP;
2486 + if (!skb->tstamp.tv64)
2487 + net_timestamp(skb);
2490 + * The code is rearranged so that the path is the most
2491 + * short when CPU is congested, but is still operating.
2493 + local_irq_save(flags);
2494 + queue = &__get_cpu_var(softnet_data);
2496 + __get_cpu_var(netdev_rx_stat).total++;
2497 + if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
2498 + if (queue->input_pkt_queue.qlen) {
2500 + dev_hold(skb->dev);
2501 + __skb_queue_tail(&queue->input_pkt_queue, skb);
2502 + local_irq_restore(flags);
2503 + return NET_RX_SUCCESS;
2506 + netif_rx_schedule(&queue->backlog_dev);
2510 + __get_cpu_var(netdev_rx_stat).dropped++;
2511 + local_irq_restore(flags);
2514 + return NET_RX_DROP;
2517 +int netif_rx_ni(struct sk_buff *skb)
2521 + preempt_disable();
2522 + err = netif_rx(skb);
2523 + if (local_softirq_pending())
2530 +EXPORT_SYMBOL(netif_rx_ni);
2532 +static inline struct net_device *skb_bond(struct sk_buff *skb)
2534 + struct net_device *dev = skb->dev;
2536 + if (dev->master) {
2537 + if (skb_bond_should_drop(skb)) {
2541 + skb->dev = dev->master;
2547 +static void net_tx_action(struct softirq_action *h)
2549 + struct softnet_data *sd = &__get_cpu_var(softnet_data);
2551 + if (sd->completion_queue) {
2552 + struct sk_buff *clist;
2554 + local_irq_disable();
2555 + clist = sd->completion_queue;
2556 + sd->completion_queue = NULL;
2557 + local_irq_enable();
2560 + struct sk_buff *skb = clist;
2561 + clist = clist->next;
2563 + BUG_TRAP(!atomic_read(&skb->users));
2568 + if (sd->output_queue) {
2569 + struct net_device *head;
2571 + local_irq_disable();
2572 + head = sd->output_queue;
2573 + sd->output_queue = NULL;
2574 + local_irq_enable();
2577 + struct net_device *dev = head;
2578 + head = head->next_sched;
2580 + smp_mb__before_clear_bit();
2581 + clear_bit(__LINK_STATE_SCHED, &dev->state);
2583 + if (spin_trylock(&dev->queue_lock)) {
2585 + spin_unlock(&dev->queue_lock);
2587 + netif_schedule(dev);
2593 +static inline int deliver_skb(struct sk_buff *skb,
2594 + struct packet_type *pt_prev,
2595 + struct net_device *orig_dev)
2597 + atomic_inc(&skb->users);
2598 + return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2601 +#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
2602 +/* These hooks defined here for ATM */
2604 +struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
2605 + unsigned char *addr);
2606 +void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
2609 + * If bridge module is loaded call bridging hook.
2610 + * returns NULL if packet was consumed.
2612 +struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
2613 + struct sk_buff *skb) __read_mostly;
2614 +static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
2615 + struct packet_type **pt_prev, int *ret,
2616 + struct net_device *orig_dev)
2618 + struct net_bridge_port *port;
2620 + if (skb->pkt_type == PACKET_LOOPBACK ||
2621 + (port = rcu_dereference(skb->dev->br_port)) == NULL)
2625 + *ret = deliver_skb(skb, *pt_prev, orig_dev);
2629 + return br_handle_frame_hook(port, skb);
2632 +#define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
2635 +#ifdef CONFIG_NET_CLS_ACT
2636 +/* TODO: Maybe we should just force sch_ingress to be compiled in
2637 + * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
2638 + * a compare and 2 stores extra right now if we dont have it on
2639 + * but have CONFIG_NET_CLS_ACT
2640 + * NOTE: This doesnt stop any functionality; if you dont have
2641 + * the ingress scheduler, you just cant add policies on ingress.
2644 +static int ing_filter(struct sk_buff *skb)
2647 + struct net_device *dev = skb->dev;
2648 + int result = TC_ACT_OK;
2650 + if (dev->qdisc_ingress) {
2651 + __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
2652 + if (MAX_RED_LOOP < ttl++) {
2653 + printk(KERN_WARNING "Redir loop detected Dropping packet (%d->%d)\n",
2654 + skb->iif, skb->dev->ifindex);
2655 + return TC_ACT_SHOT;
2658 + skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
2660 + skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
2662 + spin_lock(&dev->ingress_lock);
2663 + if ((q = dev->qdisc_ingress) != NULL)
2664 + result = q->enqueue(skb, q);
2665 + spin_unlock(&dev->ingress_lock);
2673 +int netif_receive_skb(struct sk_buff *skb)
2675 + struct packet_type *ptype, *pt_prev;
2676 + struct net_device *orig_dev;
2677 + int ret = NET_RX_DROP;
2680 + /* if we've gotten here through NAPI, check netpoll */
2681 + if (skb->dev->poll && netpoll_rx(skb))
2682 + return NET_RX_DROP;
2684 + if (!skb->tstamp.tv64)
2685 + net_timestamp(skb);
2688 + skb->iif = skb->dev->ifindex;
2690 + orig_dev = skb_bond(skb);
2693 + return NET_RX_DROP;
2695 + __get_cpu_var(netdev_rx_stat).total++;
2697 + skb_reset_network_header(skb);
2698 + skb_reset_transport_header(skb);
2699 + skb->mac_len = skb->network_header - skb->mac_header;
2705 + sb_hook(skb,pt_prev,skb->dev);
2707 +#ifdef CONFIG_NET_CLS_ACT
2708 + if (skb->tc_verd & TC_NCLS) {
2709 + skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
2714 + list_for_each_entry_rcu(ptype, &ptype_all, list) {
2715 + if (!ptype->dev || ptype->dev == skb->dev) {
2717 + ret = deliver_skb(skb, pt_prev, skb->dev);
2722 +#ifdef CONFIG_NET_CLS_ACT
2724 + ret = deliver_skb(skb, pt_prev, skb->dev);
2725 + pt_prev = NULL; /* noone else should process this after*/
2727 + skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
2730 + ret = ing_filter(skb);
2732 + if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
2741 + skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
2745 + type = skb->protocol;
2746 + list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
2747 + if (ptype->type == type &&
2748 + (!ptype->dev || ptype->dev == skb->dev)) {
2750 + ret = deliver_skb(skb, pt_prev, skb->dev);
2756 + ret = pt_prev->func(skb, skb->dev, pt_prev, skb->dev);
2759 + /* Jamal, now you will not able to escape explaining
2760 + * me how you were going to use this. :-)
2762 + ret = NET_RX_DROP;
2766 + rcu_read_unlock();
2770 +static int process_backlog(struct net_device *backlog_dev, int *budget)
2773 + int quota = min(backlog_dev->quota, *budget);
2774 + struct softnet_data *queue = &__get_cpu_var(softnet_data);
2775 + unsigned long start_time = jiffies;
2777 + backlog_dev->weight = weight_p;
2779 + struct sk_buff *skb;
2780 + struct net_device *dev;
2782 + local_irq_disable();
2783 + skb = __skb_dequeue(&queue->input_pkt_queue);
2786 + local_irq_enable();
2790 + netif_receive_skb(skb);
2796 + if (work >= quota || jiffies - start_time > 1)
2801 + backlog_dev->quota -= work;
2806 + backlog_dev->quota -= work;
2809 + list_del(&backlog_dev->poll_list);
2810 + smp_mb__before_clear_bit();
2811 + netif_poll_enable(backlog_dev);
2813 + local_irq_enable();
2817 +static void net_rx_action(struct softirq_action *h)
2819 + struct softnet_data *queue = &__get_cpu_var(softnet_data);
2820 + unsigned long start_time = jiffies;
2821 + int budget = netdev_budget;
2824 + local_irq_disable();
2826 + while (!list_empty(&queue->poll_list)) {
2827 + struct net_device *dev;
2829 + if (budget <= 0 || jiffies - start_time > 1)
2830 + goto softnet_break;
2832 + local_irq_enable();
2834 + dev = list_entry(queue->poll_list.next,
2835 + struct net_device, poll_list);
2836 + have = netpoll_poll_lock(dev);
2838 + if (dev->quota <= 0 || dev->poll(dev, &budget)) {
2839 + netpoll_poll_unlock(have);
2840 + local_irq_disable();
2841 + list_move_tail(&dev->poll_list, &queue->poll_list);
2842 + if (dev->quota < 0)
2843 + dev->quota += dev->weight;
2845 + dev->quota = dev->weight;
2847 + netpoll_poll_unlock(have);
2849 + local_irq_disable();
2853 + local_irq_enable();
2854 +#ifdef CONFIG_NET_DMA
2856 + * There may not be any more sk_buffs coming right now, so push
2857 + * any pending DMA copies to hardware
2859 + if (!cpus_empty(net_dma.channel_mask)) {
2861 + for_each_cpu_mask(chan_idx, net_dma.channel_mask) {
2862 + struct dma_chan *chan = net_dma.channels[chan_idx];
2864 + dma_async_memcpy_issue_pending(chan);
2871 + __get_cpu_var(netdev_rx_stat).time_squeeze++;
2872 + __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2876 +static gifconf_func_t * gifconf_list [NPROTO];
2879 + * register_gifconf - register a SIOCGIF handler
2880 + * @family: Address family
2881 + * @gifconf: Function handler
2883 + * Register protocol dependent address dumping routines. The handler
2884 + * that is passed must not be freed or reused until it has been replaced
2885 + * by another handler.
2887 +int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2889 + if (family >= NPROTO)
2891 + gifconf_list[family] = gifconf;
2897 + * Map an interface index to its name (SIOCGIFNAME)
2901 + * We need this ioctl for efficient implementation of the
2902 + * if_indextoname() function required by the IPv6 API. Without
2903 + * it, we would have to search all the interfaces to find a
2907 +static int dev_ifname(struct net *net, struct ifreq __user *arg)
2909 + struct net_device *dev;
2913 + * Fetch the caller's info block.
2916 + if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2919 + read_lock(&dev_base_lock);
2920 + dev = __dev_get_by_index(net, ifr.ifr_ifindex);
2922 + read_unlock(&dev_base_lock);
2926 + strcpy(ifr.ifr_name, dev->name);
2927 + read_unlock(&dev_base_lock);
2929 + if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2935 + * Perform a SIOCGIFCONF call. This structure will change
2936 + * size eventually, and there is nothing I can do about it.
2937 + * Thus we will need a 'compatibility mode'.
2940 +static int dev_ifconf(struct net *net, char __user *arg)
2942 + struct ifconf ifc;
2943 + struct net_device *dev;
2950 + * Fetch the caller's info block.
2953 + if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2956 + pos = ifc.ifc_buf;
2957 + len = ifc.ifc_len;
2960 + * Loop over the interfaces, and write an info block for each.
2964 + for_each_netdev(net, dev) {
2965 + if (!nx_dev_visible(current->nx_info, dev))
2967 + for (i = 0; i < NPROTO; i++) {
2968 + if (gifconf_list[i]) {
2971 + done = gifconf_list[i](dev, NULL, 0);
2973 + done = gifconf_list[i](dev, pos + total,
2983 + * All done. Write the updated control block back to the caller.
2985 + ifc.ifc_len = total;
2988 + * Both BSD and Solaris return 0 here, so we do too.
2990 + return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2993 +#ifdef CONFIG_PROC_FS
2995 + * This is invoked by the /proc filesystem handler to display a device
2998 +void *dev_seq_start(struct seq_file *seq, loff_t *pos)
3000 + struct net *net = seq->private;
3002 + struct net_device *dev;
3004 + read_lock(&dev_base_lock);
3006 + return SEQ_START_TOKEN;
3009 + for_each_netdev(net, dev)
3010 + if (off++ == *pos)
3016 +void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3018 + struct net *net = seq->private;
3020 + return v == SEQ_START_TOKEN ?
3021 + first_net_device(net) : next_net_device((struct net_device *)v);
3024 +void dev_seq_stop(struct seq_file *seq, void *v)
3026 + read_unlock(&dev_base_lock);
3029 +static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
3031 + struct net_device_stats *stats = dev->get_stats(dev);
3033 + if (!nx_dev_visible(current->nx_info, dev))
3036 + seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
3037 + "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
3038 + dev->name, stats->rx_bytes, stats->rx_packets,
3040 + stats->rx_dropped + stats->rx_missed_errors,
3041 + stats->rx_fifo_errors,
3042 + stats->rx_length_errors + stats->rx_over_errors +
3043 + stats->rx_crc_errors + stats->rx_frame_errors,
3044 + stats->rx_compressed, stats->multicast,
3045 + stats->tx_bytes, stats->tx_packets,
3046 + stats->tx_errors, stats->tx_dropped,
3047 + stats->tx_fifo_errors, stats->collisions,
3048 + stats->tx_carrier_errors +
3049 + stats->tx_aborted_errors +
3050 + stats->tx_window_errors +
3051 + stats->tx_heartbeat_errors,
3052 + stats->tx_compressed);
3056 + * Called from the PROCfs module. This now uses the new arbitrary sized
3057 + * /proc/net interface to create /proc/net/dev
3059 +static int dev_seq_show(struct seq_file *seq, void *v)
3061 + if (v == SEQ_START_TOKEN)
3062 + seq_puts(seq, "Inter-| Receive "
3064 + " face |bytes packets errs drop fifo frame "
3065 + "compressed multicast|bytes packets errs "
3066 + "drop fifo colls carrier compressed\n");
3068 + dev_seq_printf_stats(seq, v);
3072 +static struct netif_rx_stats *softnet_get_online(loff_t *pos)
3074 + struct netif_rx_stats *rc = NULL;
3076 + while (*pos < NR_CPUS)
3077 + if (cpu_online(*pos)) {
3078 + rc = &per_cpu(netdev_rx_stat, *pos);
3085 +static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
3087 + return softnet_get_online(pos);
3090 +static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3093 + return softnet_get_online(pos);
3096 +static void softnet_seq_stop(struct seq_file *seq, void *v)
3100 +static int softnet_seq_show(struct seq_file *seq, void *v)
3102 + struct netif_rx_stats *s = v;
3104 + seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
3105 + s->total, s->dropped, s->time_squeeze, 0,
3106 + 0, 0, 0, 0, /* was fastroute */
3107 + s->cpu_collision );
3111 +static const struct seq_operations dev_seq_ops = {
3112 + .start = dev_seq_start,
3113 + .next = dev_seq_next,
3114 + .stop = dev_seq_stop,
3115 + .show = dev_seq_show,
3118 +static int dev_seq_open(struct inode *inode, struct file *file)
3120 + struct seq_file *seq;
3122 + res = seq_open(file, &dev_seq_ops);
3124 + seq = file->private_data;
3125 + seq->private = get_net(PROC_NET(inode));
3130 +static int dev_seq_release(struct inode *inode, struct file *file)
3132 + struct seq_file *seq = file->private_data;
3133 + struct net *net = seq->private;
3135 + return seq_release(inode, file);
3138 +static const struct file_operations dev_seq_fops = {
3139 + .owner = THIS_MODULE,
3140 + .open = dev_seq_open,
3142 + .llseek = seq_lseek,
3143 + .release = dev_seq_release,
3146 +static const struct seq_operations softnet_seq_ops = {
3147 + .start = softnet_seq_start,
3148 + .next = softnet_seq_next,
3149 + .stop = softnet_seq_stop,
3150 + .show = softnet_seq_show,
3153 +static int softnet_seq_open(struct inode *inode, struct file *file)
3155 + return seq_open(file, &softnet_seq_ops);
3158 +static const struct file_operations softnet_seq_fops = {
3159 + .owner = THIS_MODULE,
3160 + .open = softnet_seq_open,
3162 + .llseek = seq_lseek,
3163 + .release = seq_release,
3166 +static void *ptype_get_idx(loff_t pos)
3168 + struct packet_type *pt = NULL;
3172 + list_for_each_entry_rcu(pt, &ptype_all, list) {
3178 + for (t = 0; t < 16; t++) {
3179 + list_for_each_entry_rcu(pt, &ptype_base[t], list) {
3188 +static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
3191 + return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
3194 +static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3196 + struct packet_type *pt;
3197 + struct list_head *nxt;
3201 + if (v == SEQ_START_TOKEN)
3202 + return ptype_get_idx(0);
3205 + nxt = pt->list.next;
3206 + if (pt->type == htons(ETH_P_ALL)) {
3207 + if (nxt != &ptype_all)
3210 + nxt = ptype_base[0].next;
3212 + hash = ntohs(pt->type) & 15;
3214 + while (nxt == &ptype_base[hash]) {
3217 + nxt = ptype_base[hash].next;
3220 + return list_entry(nxt, struct packet_type, list);
3223 +static void ptype_seq_stop(struct seq_file *seq, void *v)
3225 + rcu_read_unlock();
3228 +static void ptype_seq_decode(struct seq_file *seq, void *sym)
3230 +#ifdef CONFIG_KALLSYMS
3231 + unsigned long offset = 0, symsize;
3232 + const char *symname;
3234 + char namebuf[128];
3236 + symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
3237 + &modname, namebuf);
3240 + char *delim = ":";
3243 + modname = delim = "";
3244 + seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
3250 + seq_printf(seq, "[%p]", sym);
3253 +static int ptype_seq_show(struct seq_file *seq, void *v)
3255 + struct packet_type *pt = v;
3257 + if (v == SEQ_START_TOKEN)
3258 + seq_puts(seq, "Type Device Function\n");
3260 + if (pt->type == htons(ETH_P_ALL))
3261 + seq_puts(seq, "ALL ");
3263 + seq_printf(seq, "%04x", ntohs(pt->type));
3265 + seq_printf(seq, " %-8s ",
3266 + pt->dev ? pt->dev->name : "");
3267 + ptype_seq_decode(seq, pt->func);
3268 + seq_putc(seq, '\n');
3274 +static const struct seq_operations ptype_seq_ops = {
3275 + .start = ptype_seq_start,
3276 + .next = ptype_seq_next,
3277 + .stop = ptype_seq_stop,
3278 + .show = ptype_seq_show,
3281 +static int ptype_seq_open(struct inode *inode, struct file *file)
3283 + return seq_open(file, &ptype_seq_ops);
3286 +static const struct file_operations ptype_seq_fops = {
3287 + .owner = THIS_MODULE,
3288 + .open = ptype_seq_open,
3290 + .llseek = seq_lseek,
3291 + .release = seq_release,
3295 +static int dev_proc_net_init(struct net *net)
3299 + if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
3301 + if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
3303 + if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
3306 + if (wext_proc_init(net))
3312 + proc_net_remove(net, "ptype");
3314 + proc_net_remove(net, "softnet_stat");
3316 + proc_net_remove(net, "dev");
3320 +static void dev_proc_net_exit(struct net *net)
3322 + wext_proc_exit(net);
3324 + proc_net_remove(net, "ptype");
3325 + proc_net_remove(net, "softnet_stat");
3326 + proc_net_remove(net, "dev");
3329 +static struct pernet_operations dev_proc_ops = {
3330 + .init = dev_proc_net_init,
3331 + .exit = dev_proc_net_exit,
3334 +static int __init dev_proc_init(void)
3336 + return register_pernet_subsys(&dev_proc_ops);
3339 +#define dev_proc_init() 0
3340 +#endif /* CONFIG_PROC_FS */
3344 + * netdev_set_master - set up master/slave pair
3345 + * @slave: slave device
3346 + * @master: new master device
3348 + * Changes the master device of the slave. Pass %NULL to break the
3349 + * bonding. The caller must hold the RTNL semaphore. On a failure
3350 + * a negative errno code is returned. On success the reference counts
3351 + * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
3352 + * function returns zero.
3354 +int netdev_set_master(struct net_device *slave, struct net_device *master)
3356 + struct net_device *old = slave->master;
3366 + slave->master = master;
3368 + synchronize_net();
3374 + slave->flags |= IFF_SLAVE;
3376 + slave->flags &= ~IFF_SLAVE;
3378 + rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
3383 + * dev_set_promiscuity - update promiscuity count on a device
3387 + * Add or remove promiscuity from a device. While the count in the device
3388 + * remains above zero the interface remains promiscuous. Once it hits zero
3389 + * the device reverts back to normal filtering operation. A negative inc
3390 + * value is used to drop promiscuity on the device.
3392 +void dev_set_promiscuity(struct net_device *dev, int inc)
3394 + unsigned short old_flags = dev->flags;
3396 + if ((dev->promiscuity += inc) == 0)
3397 + dev->flags &= ~IFF_PROMISC;
3399 + dev->flags |= IFF_PROMISC;
3400 + if (dev->flags != old_flags) {
3401 + dev_mc_upload(dev);
3402 + printk(KERN_INFO "device %s %s promiscuous mode\n",
3403 + dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
3405 + audit_log(current->audit_context, GFP_ATOMIC,
3406 + AUDIT_ANOM_PROMISCUOUS,
3407 + "dev=%s prom=%d old_prom=%d auid=%u",
3408 + dev->name, (dev->flags & IFF_PROMISC),
3409 + (old_flags & IFF_PROMISC),
3410 + audit_get_loginuid(current->audit_context));
3415 + * dev_set_allmulti - update allmulti count on a device
3419 + * Add or remove reception of all multicast frames to a device. While the
3420 + * count in the device remains above zero the interface remains listening
3421 + * to all interfaces. Once it hits zero the device reverts back to normal
3422 + * filtering operation. A negative @inc value is used to drop the counter
3423 + * when releasing a resource needing all multicasts.
3426 +void dev_set_allmulti(struct net_device *dev, int inc)
3428 + unsigned short old_flags = dev->flags;
3430 + dev->flags |= IFF_ALLMULTI;
3431 + if ((dev->allmulti += inc) == 0)
3432 + dev->flags &= ~IFF_ALLMULTI;
3433 + if (dev->flags ^ old_flags)
3434 + dev_mc_upload(dev);
3437 +unsigned dev_get_flags(const struct net_device *dev)
3441 + flags = (dev->flags & ~(IFF_PROMISC |
3446 + (dev->gflags & (IFF_PROMISC |
3449 + if (netif_running(dev)) {
3450 + if (netif_oper_up(dev))
3451 + flags |= IFF_RUNNING;
3452 + if (netif_carrier_ok(dev))
3453 + flags |= IFF_LOWER_UP;
3454 + if (netif_dormant(dev))
3455 + flags |= IFF_DORMANT;
3461 +int dev_change_flags(struct net_device *dev, unsigned flags)
3464 + int old_flags = dev->flags;
3467 + * Set the flags on our device.
3470 + dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
3471 + IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
3473 + (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
3477 + * Load in the correct multicast list now the flags have changed.
3480 + dev_mc_upload(dev);
3483 + * Have we downed the interface. We handle IFF_UP ourselves
3484 + * according to user attempts to set it, rather than blindly
3489 + if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
3490 + ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
3493 + dev_mc_upload(dev);
3496 + if (dev->flags & IFF_UP &&
3497 + ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
3499 + raw_notifier_call_chain(&netdev_chain,
3500 + NETDEV_CHANGE, dev);
3502 + if ((flags ^ dev->gflags) & IFF_PROMISC) {
3503 + int inc = (flags & IFF_PROMISC) ? +1 : -1;
3504 + dev->gflags ^= IFF_PROMISC;
3505 + dev_set_promiscuity(dev, inc);
3508 + /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
3509 + is important. Some (broken) drivers set IFF_PROMISC, when
3510 + IFF_ALLMULTI is requested not asking us and not reporting.
3512 + if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
3513 + int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
3514 + dev->gflags ^= IFF_ALLMULTI;
3515 + dev_set_allmulti(dev, inc);
3518 + /* Exclude state transition flags, already notified */
3519 + changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
3521 + rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
3526 +int dev_set_mtu(struct net_device *dev, int new_mtu)
3530 + if (new_mtu == dev->mtu)
3533 + /* MTU must be positive. */
3537 + if (!netif_device_present(dev))
3541 + if (dev->change_mtu)
3542 + err = dev->change_mtu(dev, new_mtu);
3544 + dev->mtu = new_mtu;
3545 + if (!err && dev->flags & IFF_UP)
3546 + raw_notifier_call_chain(&netdev_chain,
3547 + NETDEV_CHANGEMTU, dev);
3551 +int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
3555 + if (!dev->set_mac_address)
3556 + return -EOPNOTSUPP;
3557 + if (sa->sa_family != dev->type)
3559 + if (!netif_device_present(dev))
3561 + err = dev->set_mac_address(dev, sa);
3563 + raw_notifier_call_chain(&netdev_chain,
3564 + NETDEV_CHANGEADDR, dev);
3569 + * Perform the SIOCxIFxxx calls.
3571 +static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
3574 + struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
3580 + case SIOCGIFFLAGS: /* Get interface flags */
3581 + ifr->ifr_flags = dev_get_flags(dev);
3584 + case SIOCSIFFLAGS: /* Set interface flags */
3585 + return dev_change_flags(dev, ifr->ifr_flags);
3587 + case SIOCGIFMETRIC: /* Get the metric on the interface
3588 + (currently unused) */
3589 + ifr->ifr_metric = 0;
3592 + case SIOCSIFMETRIC: /* Set the metric on the interface
3593 + (currently unused) */
3594 + return -EOPNOTSUPP;
3596 + case SIOCGIFMTU: /* Get the MTU of a device */
3597 + ifr->ifr_mtu = dev->mtu;
3600 + case SIOCSIFMTU: /* Set the MTU of a device */
3601 + return dev_set_mtu(dev, ifr->ifr_mtu);
3603 + case SIOCGIFHWADDR:
3604 + if (!dev->addr_len)
3605 + memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
3607 + memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
3608 + min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
3609 + ifr->ifr_hwaddr.sa_family = dev->type;
3612 + case SIOCSIFHWADDR:
3613 + return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
3615 + case SIOCSIFHWBROADCAST:
3616 + if (ifr->ifr_hwaddr.sa_family != dev->type)
3618 + memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
3619 + min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
3620 + raw_notifier_call_chain(&netdev_chain,
3621 + NETDEV_CHANGEADDR, dev);
3625 + ifr->ifr_map.mem_start = dev->mem_start;
3626 + ifr->ifr_map.mem_end = dev->mem_end;
3627 + ifr->ifr_map.base_addr = dev->base_addr;
3628 + ifr->ifr_map.irq = dev->irq;
3629 + ifr->ifr_map.dma = dev->dma;
3630 + ifr->ifr_map.port = dev->if_port;
3634 + if (dev->set_config) {
3635 + if (!netif_device_present(dev))
3637 + return dev->set_config(dev, &ifr->ifr_map);
3639 + return -EOPNOTSUPP;
3641 + case SIOCADDMULTI:
3642 + if (!dev->set_multicast_list ||
3643 + ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3645 + if (!netif_device_present(dev))
3647 + return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
3648 + dev->addr_len, 1);
3650 + case SIOCDELMULTI:
3651 + if (!dev->set_multicast_list ||
3652 + ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3654 + if (!netif_device_present(dev))
3656 + return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
3657 + dev->addr_len, 1);
3659 + case SIOCGIFINDEX:
3660 + ifr->ifr_ifindex = dev->ifindex;
3663 + case SIOCGIFTXQLEN:
3664 + ifr->ifr_qlen = dev->tx_queue_len;
3667 + case SIOCSIFTXQLEN:
3668 + if (ifr->ifr_qlen < 0)
3670 + dev->tx_queue_len = ifr->ifr_qlen;
3674 + ifr->ifr_newname[IFNAMSIZ-1] = '\0';
3675 + return dev_change_name(dev, ifr->ifr_newname);
3678 + * Unknown or private ioctl
3682 + if ((cmd >= SIOCDEVPRIVATE &&
3683 + cmd <= SIOCDEVPRIVATE + 15) ||
3684 + cmd == SIOCBONDENSLAVE ||
3685 + cmd == SIOCBONDRELEASE ||
3686 + cmd == SIOCBONDSETHWADDR ||
3687 + cmd == SIOCBONDSLAVEINFOQUERY ||
3688 + cmd == SIOCBONDINFOQUERY ||
3689 + cmd == SIOCBONDCHANGEACTIVE ||
3690 + cmd == SIOCGMIIPHY ||
3691 + cmd == SIOCGMIIREG ||
3692 + cmd == SIOCSMIIREG ||
3693 + cmd == SIOCBRADDIF ||
3694 + cmd == SIOCBRDELIF ||
3695 + cmd == SIOCWANDEV) {
3696 + err = -EOPNOTSUPP;
3697 + if (dev->do_ioctl) {
3698 + if (netif_device_present(dev))
3699 + err = dev->do_ioctl(dev, ifr,
3712 + * This function handles all "interface"-type I/O control requests. The actual
3713 + * 'doing' part of this is dev_ifsioc above.
3717 + * dev_ioctl - network device ioctl
3718 + * @cmd: command to issue
3719 + * @arg: pointer to a struct ifreq in user space
3721 + * Issue ioctl functions to devices. This is normally called by the
3722 + * user space syscall interfaces but can sometimes be useful for
3723 + * other purposes. The return value is the return from the syscall if
3724 + * positive or a negative errno code on error.
3727 +int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
3733 + /* One special case: SIOCGIFCONF takes ifconf argument
3734 + and requires shared lock, because it sleeps writing
3738 + if (cmd == SIOCGIFCONF) {
3740 + ret = dev_ifconf(net, (char __user *) arg);
3744 + if (cmd == SIOCGIFNAME)
3745 + return dev_ifname(net, (struct ifreq __user *)arg);
3747 + if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3750 + ifr.ifr_name[IFNAMSIZ-1] = 0;
3752 + colon = strchr(ifr.ifr_name, ':');
3757 + * See which interface the caller is talking about.
3762 + * These ioctl calls:
3763 + * - can be done by all.
3764 + * - atomic and do not require locking.
3765 + * - return a value
3767 + case SIOCGIFFLAGS:
3768 + case SIOCGIFMETRIC:
3770 + case SIOCGIFHWADDR:
3771 + case SIOCGIFSLAVE:
3773 + case SIOCGIFINDEX:
3774 + case SIOCGIFTXQLEN:
3775 + dev_load(net, ifr.ifr_name);
3776 + read_lock(&dev_base_lock);
3777 + ret = dev_ifsioc(net, &ifr, cmd);
3778 + read_unlock(&dev_base_lock);
3782 + if (copy_to_user(arg, &ifr,
3783 + sizeof(struct ifreq)))
3789 + dev_load(net, ifr.ifr_name);
3791 + ret = dev_ethtool(net, &ifr);
3796 + if (copy_to_user(arg, &ifr,
3797 + sizeof(struct ifreq)))
3803 + * These ioctl calls:
3804 + * - require superuser power.
3805 + * - require strict serialization.
3806 + * - return a value
3811 + if (!capable(CAP_NET_ADMIN))
3813 + dev_load(net, ifr.ifr_name);
3815 + ret = dev_ifsioc(net, &ifr, cmd);
3820 + if (copy_to_user(arg, &ifr,
3821 + sizeof(struct ifreq)))
3827 + * These ioctl calls:
3828 + * - require superuser power.
3829 + * - require strict serialization.
3830 + * - do not return a value
3832 + case SIOCSIFFLAGS:
3833 + case SIOCSIFMETRIC:
3836 + case SIOCSIFHWADDR:
3837 + case SIOCSIFSLAVE:
3838 + case SIOCADDMULTI:
3839 + case SIOCDELMULTI:
3840 + case SIOCSIFHWBROADCAST:
3841 + case SIOCSIFTXQLEN:
3843 + case SIOCBONDENSLAVE:
3844 + case SIOCBONDRELEASE:
3845 + case SIOCBONDSETHWADDR:
3846 + case SIOCBONDCHANGEACTIVE:
3849 + if (!capable(CAP_NET_ADMIN))
3851 + /* fall through */
3852 + case SIOCBONDSLAVEINFOQUERY:
3853 + case SIOCBONDINFOQUERY:
3854 + dev_load(net, ifr.ifr_name);
3856 + ret = dev_ifsioc(net, &ifr, cmd);
3861 + /* Get the per device memory space. We can add this but
3862 + * currently do not support it */
3864 + /* Set the per device memory buffer space.
3865 + * Not applicable in our case */
3870 + * Unknown or private ioctl.
3873 + if (cmd == SIOCWANDEV ||
3874 + (cmd >= SIOCDEVPRIVATE &&
3875 + cmd <= SIOCDEVPRIVATE + 15)) {
3876 + dev_load(net, ifr.ifr_name);
3878 + ret = dev_ifsioc(net, &ifr, cmd);
3880 + if (!ret && copy_to_user(arg, &ifr,
3881 + sizeof(struct ifreq)))
3885 + /* Take care of Wireless Extensions */
3886 + if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
3887 + return wext_handle_ioctl(net, &ifr, cmd, arg);
3894 + * dev_new_index - allocate an ifindex
3896 + * Returns a suitable unique value for a new device interface
3897 + * number. The caller must hold the rtnl semaphore or the
3898 + * dev_base_lock to be sure it remains unique.
3900 +static int dev_new_index(struct net *net)
3902 + static int ifindex;
3904 + if (++ifindex <= 0)
3906 + if (!__dev_get_by_index(net, ifindex))
3911 +/* Delayed registration/unregisteration */
3912 +static DEFINE_SPINLOCK(net_todo_list_lock);
3913 +static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3915 +static void net_set_todo(struct net_device *dev)
3917 + spin_lock(&net_todo_list_lock);
3918 + list_add_tail(&dev->todo_list, &net_todo_list);
3919 + spin_unlock(&net_todo_list_lock);
3923 + * register_netdevice - register a network device
3924 + * @dev: device to register
3926 + * Take a completed network device structure and add it to the kernel
3927 + * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3928 + * chain. 0 is returned on success. A negative errno code is returned
3929 + * on a failure to set up the device, or if the name is a duplicate.
3931 + * Callers must hold the rtnl semaphore. You may want
3932 + * register_netdev() instead of this.
3935 + * The locking appears insufficient to guarantee two parallel registers
3936 + * will not get the same name.
3939 +int register_netdevice(struct net_device *dev)
3941 + struct hlist_head *head;
3942 + struct hlist_node *p;
3946 + BUG_ON(dev_boot_phase);
3951 + /* When net_device's are persistent, this will be fatal. */
3952 + BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3953 + BUG_ON(!dev->nd_net);
3954 + net = dev->nd_net;
3956 + spin_lock_init(&dev->queue_lock);
3957 + spin_lock_init(&dev->_xmit_lock);
3958 + netdev_set_lockdep_class(&dev->_xmit_lock, dev->type);
3959 + dev->xmit_lock_owner = -1;
3960 + spin_lock_init(&dev->ingress_lock);
3964 + /* Init, if this function is available */
3966 + ret = dev->init(dev);
3974 + if (!dev_valid_name(dev->name)) {
3979 + dev->ifindex = dev_new_index(net);
3980 + if (dev->iflink == -1)
3981 + dev->iflink = dev->ifindex;
3983 + /* Check for existence of name */
3984 + head = dev_name_hash(net, dev->name);
3985 + hlist_for_each(p, head) {
3986 + struct net_device *d
3987 + = hlist_entry(p, struct net_device, name_hlist);
3988 + if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3994 + /* Fix illegal checksum combinations */
3995 + if ((dev->features & NETIF_F_HW_CSUM) &&
3996 + (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3997 + printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3999 + dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
4002 + if ((dev->features & NETIF_F_NO_CSUM) &&
4003 + (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
4004 + printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
4006 + dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
4010 + /* Fix illegal SG+CSUM combinations. */
4011 + if ((dev->features & NETIF_F_SG) &&
4012 + !(dev->features & NETIF_F_ALL_CSUM)) {
4013 + printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
4015 + dev->features &= ~NETIF_F_SG;
4018 + /* TSO requires that SG is present as well. */
4019 + if ((dev->features & NETIF_F_TSO) &&
4020 + !(dev->features & NETIF_F_SG)) {
4021 + printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
4023 + dev->features &= ~NETIF_F_TSO;
4025 + if (dev->features & NETIF_F_UFO) {
4026 + if (!(dev->features & NETIF_F_HW_CSUM)) {
4027 + printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
4028 + "NETIF_F_HW_CSUM feature.\n",
4030 + dev->features &= ~NETIF_F_UFO;
4032 + if (!(dev->features & NETIF_F_SG)) {
4033 + printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
4034 + "NETIF_F_SG feature.\n",
4036 + dev->features &= ~NETIF_F_UFO;
4041 + * nil rebuild_header routine,
4042 + * that should be never called and used as just bug trap.
4045 + if (!dev->rebuild_header)
4046 + dev->rebuild_header = default_rebuild_header;
4048 + ret = netdev_register_sysfs(dev);
4051 + dev->reg_state = NETREG_REGISTERED;
4054 + * Default initial state at registry is that the
4055 + * device is present.
4058 + set_bit(__LINK_STATE_PRESENT, &dev->state);
4060 + dev_init_scheduler(dev);
4062 + list_netdevice(dev);
4064 + /* Notify protocols, that a new device appeared. */
4065 + raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
4074 + * register_netdev - register a network device
4075 + * @dev: device to register
4077 + * Take a completed network device structure and add it to the kernel
4078 + * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
4079 + * chain. 0 is returned on success. A negative errno code is returned
4080 + * on a failure to set up the device, or if the name is a duplicate.
4082 + * This is a wrapper around register_netdevice that takes the rtnl semaphore
4083 + * and expands the device name if you passed a format string to
4086 +int register_netdev(struct net_device *dev)
4093 + * If the name is a format string the caller wants us to do a
4094 + * name allocation.
4096 + if (strchr(dev->name, '%')) {
4097 + err = dev_alloc_name(dev, dev->name);
4102 + err = register_netdevice(dev);
4107 +EXPORT_SYMBOL(register_netdev);
4110 + * netdev_wait_allrefs - wait until all references are gone.
4112 + * This is called when unregistering network devices.
4114 + * Any protocol or device that holds a reference should register
4115 + * for netdevice notification, and cleanup and put back the
4116 + * reference if they receive an UNREGISTER event.
4117 + * We can get stuck here if buggy protocols don't correctly
4120 +static void netdev_wait_allrefs(struct net_device *dev)
4122 + unsigned long rebroadcast_time, warning_time;
4124 + rebroadcast_time = warning_time = jiffies;
4125 + while (atomic_read(&dev->refcnt) != 0) {
4126 + if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
4129 + /* Rebroadcast unregister notification */
4130 + raw_notifier_call_chain(&netdev_chain,
4131 + NETDEV_UNREGISTER, dev);
4133 + if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
4135 + /* We must not have linkwatch events
4136 + * pending on unregister. If this
4137 + * happens, we simply run the queue
4138 + * unscheduled, resulting in a noop
4139 + * for this device.
4141 + linkwatch_run_queue();
4146 + rebroadcast_time = jiffies;
4151 + if (time_after(jiffies, warning_time + 10 * HZ)) {
4152 + printk(KERN_EMERG "unregister_netdevice: "
4153 + "waiting for %s to become free. Usage "
4155 + dev->name, atomic_read(&dev->refcnt));
4156 + warning_time = jiffies;
4161 +/* The sequence is:
4165 + * register_netdevice(x1);
4166 + * register_netdevice(x2);
4168 + * unregister_netdevice(y1);
4169 + * unregister_netdevice(y2);
4172 + * free_netdev(y1);
4173 + * free_netdev(y2);
4175 + * We are invoked by rtnl_unlock() after it drops the semaphore.
4176 + * This allows us to deal with problems:
4177 + * 1) We can delete sysfs objects which invoke hotplug
4178 + * without deadlocking with linkwatch via keventd.
4179 + * 2) Since we run with the RTNL semaphore not held, we can sleep
4180 + * safely in order to wait for the netdev refcnt to drop to zero.
4182 +static DEFINE_MUTEX(net_todo_run_mutex);
4183 +void netdev_run_todo(void)
4185 + struct list_head list;
4187 + /* Need to guard against multiple cpu's getting out of order. */
4188 + mutex_lock(&net_todo_run_mutex);
4190 + /* Not safe to do outside the semaphore. We must not return
4191 + * until all unregister events invoked by the local processor
4192 + * have been completed (either by this todo run, or one on
4195 + if (list_empty(&net_todo_list))
4198 + /* Snapshot list, allow later requests */
4199 + spin_lock(&net_todo_list_lock);
4200 + list_replace_init(&net_todo_list, &list);
4201 + spin_unlock(&net_todo_list_lock);
4203 + while (!list_empty(&list)) {
4204 + struct net_device *dev
4205 + = list_entry(list.next, struct net_device, todo_list);
4206 + list_del(&dev->todo_list);
4208 + if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
4209 + printk(KERN_ERR "network todo '%s' but state %d\n",
4210 + dev->name, dev->reg_state);
4215 + dev->reg_state = NETREG_UNREGISTERED;
4217 + netdev_wait_allrefs(dev);
4220 + BUG_ON(atomic_read(&dev->refcnt));
4221 + BUG_TRAP(!dev->ip_ptr);
4222 + BUG_TRAP(!dev->ip6_ptr);
4223 + BUG_TRAP(!dev->dn_ptr);
4225 + if (dev->destructor)
4226 + dev->destructor(dev);
4228 + /* Free network device */
4229 + kobject_put(&dev->dev.kobj);
4233 + mutex_unlock(&net_todo_run_mutex);
4236 +static struct net_device_stats *internal_stats(struct net_device *dev)
4238 + return &dev->stats;
4242 + * alloc_netdev - allocate network device
4243 + * @sizeof_priv: size of private data to allocate space for
4244 + * @name: device name format string
4245 + * @setup: callback to initialize device
4247 + * Allocates a struct net_device with private data area for driver use
4248 + * and performs basic initialization.
4250 +struct net_device *alloc_netdev(int sizeof_priv, const char *name,
4251 + void (*setup)(struct net_device *))
4254 + struct net_device *dev;
4257 + BUG_ON(strlen(name) >= sizeof(dev->name));
4259 + /* ensure 32-byte alignment of both the device and private area */
4260 + alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
4261 + alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
4263 + p = kzalloc(alloc_size, GFP_KERNEL);
4265 + printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
4269 + dev = (struct net_device *)
4270 + (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
4271 + dev->padded = (char *)dev - (char *)p;
4272 + dev->nd_net = &init_net;
4275 + dev->priv = netdev_priv(dev);
4277 + dev->get_stats = internal_stats;
4279 + strcpy(dev->name, name);
4282 +EXPORT_SYMBOL(alloc_netdev);
4285 + * free_netdev - free network device
4288 + * This function does the last stage of destroying an allocated device
4289 + * interface. The reference to the device object is released.
4290 + * If this is the last reference then it will be freed.
4292 +void free_netdev(struct net_device *dev)
4294 +#ifdef CONFIG_SYSFS
4295 + /* Compatibility with error handling in drivers */
4296 + if (dev->reg_state == NETREG_UNINITIALIZED) {
4297 + kfree((char *)dev - dev->padded);
4301 + BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
4302 + dev->reg_state = NETREG_RELEASED;
4304 + /* will free via device release */
4305 + put_device(&dev->dev);
4307 + kfree((char *)dev - dev->padded);
4311 +/* Synchronize with packet receive processing. */
4312 +void synchronize_net(void)
4315 + synchronize_rcu();
4319 + * unregister_netdevice - remove device from the kernel
4322 + * This function shuts down a device interface and removes it
4323 + * from the kernel tables. On success 0 is returned, on a failure
4324 + * a negative errno code is returned.
4326 + * Callers must hold the rtnl semaphore. You may want
4327 + * unregister_netdev() instead of this.
4330 +void unregister_netdevice(struct net_device *dev)
4332 + BUG_ON(dev_boot_phase);
4335 + /* Some devices call without registering for initialization unwind. */
4336 + if (dev->reg_state == NETREG_UNINITIALIZED) {
4337 + printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
4338 + "was registered\n", dev->name, dev);
4344 + BUG_ON(dev->reg_state != NETREG_REGISTERED);
4346 + /* If device is running, close it first. */
4347 + if (dev->flags & IFF_UP)
4350 + /* And unlink it from device chain. */
4351 + unlist_netdevice(dev);
4353 + dev->reg_state = NETREG_UNREGISTERING;
4355 + synchronize_net();
4357 + /* Shutdown queueing discipline. */
4358 + dev_shutdown(dev);
4361 + /* Notify protocols, that we are about to destroy
4362 + this device. They should clean all the things.
4364 + raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
4367 + * Flush the multicast chain
4369 + dev_mc_discard(dev);
4374 + /* Notifier chain MUST detach us from master device. */
4375 + BUG_TRAP(!dev->master);
4377 + /* Remove entries from sysfs */
4378 + netdev_unregister_sysfs(dev);
4380 + /* Finish processing unregister after unlock */
4381 + net_set_todo(dev);
4383 + synchronize_net();
4389 + * unregister_netdev - remove device from the kernel
4392 + * This function shuts down a device interface and removes it
4393 + * from the kernel tables. On success 0 is returned, on a failure
4394 + * a negative errno code is returned.
4396 + * This is just a wrapper for unregister_netdevice that takes
4397 + * the rtnl semaphore. In general you want to use this and not
4398 + * unregister_netdevice.
4400 +void unregister_netdev(struct net_device *dev)
4403 + unregister_netdevice(dev);
4407 +EXPORT_SYMBOL(unregister_netdev);
4410 + * dev_change_net_namespace - move device to different nethost namespace
4412 + * @net: network namespace
4413 + * @pat: If not NULL name pattern to try if the current device name
4414 + * is already taken in the destination network namespace.
4416 + * This function shuts down a device interface and moves it
4417 + * to a new network namespace. On success 0 is returned, on
4418 + * a failure a netagive errno code is returned.
4420 + * Callers must hold the rtnl semaphore.
4423 +int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
4425 + char buf[IFNAMSIZ];
4426 + const char *destname;
4431 + /* Don't allow namespace local devices to be moved. */
4433 + if (dev->features & NETIF_F_NETNS_LOCAL)
4436 + /* Ensure the device has been registrered */
4438 + if (dev->reg_state != NETREG_REGISTERED)
4441 + /* Get out if there is nothing todo */
4443 + if (dev->nd_net == net)
4446 + /* Pick the destination device name, and ensure
4447 + * we can use it in the destination network namespace.
4450 + destname = dev->name;
4451 + if (__dev_get_by_name(net, destname)) {
4452 + /* We get here if we can't use the current device name */
4455 + if (!dev_valid_name(pat))
4457 + if (strchr(pat, '%')) {
4458 + if (__dev_alloc_name(net, pat, buf) < 0)
4463 + if (__dev_get_by_name(net, destname))
4468 + * And now a mini version of register_netdevice unregister_netdevice.
4471 + /* If device is running close it first. */
4472 + if (dev->flags & IFF_UP)
4475 + /* And unlink it from device chain */
4477 + unlist_netdevice(dev);
4479 + synchronize_net();
4481 + /* Shutdown queueing discipline. */
4482 + dev_shutdown(dev);
4484 + /* Notify protocols, that we are about to destroy
4485 + this device. They should clean all the things.
4487 + call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
4490 + * Flush the multicast chain
4492 + dev_mc_discard(dev);
4494 + /* Actually switch the network namespace */
4495 + dev->nd_net = net;
4497 + /* Assign the new device name */
4498 + if (destname != dev->name)
4499 + strcpy(dev->name, destname);
4501 + /* If there is an ifindex conflict assign a new one */
4502 + if (__dev_get_by_index(net, dev->ifindex)) {
4503 + int iflink = (dev->iflink == dev->ifindex);
4504 + dev->ifindex = dev_new_index(net);
4506 + dev->iflink = dev->ifindex;
4510 + err = device_rename(&dev->dev, dev->name);
4513 + /* Add the device back in the hashes */
4514 + list_netdevice(dev);
4516 + /* Notify protocols, that a new device appeared. */
4517 + call_netdevice_notifiers(NETDEV_REGISTER, dev);
4519 + synchronize_net();
4525 +static int dev_cpu_callback(struct notifier_block *nfb,
4526 + unsigned long action,
4529 + struct sk_buff **list_skb;
4530 + struct net_device **list_net;
4531 + struct sk_buff *skb;
4532 + unsigned int cpu, oldcpu = (unsigned long)ocpu;
4533 + struct softnet_data *sd, *oldsd;
4535 + if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
4538 + local_irq_disable();
4539 + cpu = smp_processor_id();
4540 + sd = &per_cpu(softnet_data, cpu);
4541 + oldsd = &per_cpu(softnet_data, oldcpu);
4543 + /* Find end of our completion_queue. */
4544 + list_skb = &sd->completion_queue;
4546 + list_skb = &(*list_skb)->next;
4547 + /* Append completion queue from offline CPU. */
4548 + *list_skb = oldsd->completion_queue;
4549 + oldsd->completion_queue = NULL;
4551 + /* Find end of our output_queue. */
4552 + list_net = &sd->output_queue;
4554 + list_net = &(*list_net)->next_sched;
4555 + /* Append output queue from offline CPU. */
4556 + *list_net = oldsd->output_queue;
4557 + oldsd->output_queue = NULL;
4559 + raise_softirq_irqoff(NET_TX_SOFTIRQ);
4560 + local_irq_enable();
4562 + /* Process offline CPU's input_pkt_queue */
4563 + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
4569 +#ifdef CONFIG_NET_DMA
4571 + * net_dma_rebalance -
4572 + * This is called when the number of channels allocated to the net_dma_client
4573 + * changes. The net_dma_client tries to have one DMA channel per CPU.
4576 +static void net_dma_rebalance(struct net_dma *net_dma)
4578 + unsigned int cpu, i, n, chan_idx;
4579 + struct dma_chan *chan;
4581 + if (cpus_empty(net_dma->channel_mask)) {
4582 + for_each_online_cpu(cpu)
4583 + rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
4588 + cpu = first_cpu(cpu_online_map);
4590 + for_each_cpu_mask(chan_idx, net_dma->channel_mask) {
4591 + chan = net_dma->channels[chan_idx];
4593 + n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask))
4594 + + (i < (num_online_cpus() %
4595 + cpus_weight(net_dma->channel_mask)) ? 1 : 0));
4598 + per_cpu(softnet_data, cpu).net_dma = chan;
4599 + cpu = next_cpu(cpu, cpu_online_map);
4607 + * netdev_dma_event - event callback for the net_dma_client
4608 + * @client: should always be net_dma_client
4609 + * @chan: DMA channel for the event
4610 + * @event: event type
4612 +static enum dma_state_client
4613 +netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
4614 + enum dma_state state)
4616 + int i, found = 0, pos = -1;
4617 + struct net_dma *net_dma =
4618 + container_of(client, struct net_dma, client);
4619 + enum dma_state_client ack = DMA_DUP; /* default: take no action */
4621 + spin_lock(&net_dma->lock);
4623 + case DMA_RESOURCE_AVAILABLE:
4624 + for (i = 0; i < NR_CPUS; i++)
4625 + if (net_dma->channels[i] == chan) {
4628 + } else if (net_dma->channels[i] == NULL && pos < 0)
4631 + if (!found && pos >= 0) {
4633 + net_dma->channels[pos] = chan;
4634 + cpu_set(pos, net_dma->channel_mask);
4635 + net_dma_rebalance(net_dma);
4638 + case DMA_RESOURCE_REMOVED:
4639 + for (i = 0; i < NR_CPUS; i++)
4640 + if (net_dma->channels[i] == chan) {
4648 + cpu_clear(pos, net_dma->channel_mask);
4649 + net_dma->channels[i] = NULL;
4650 + net_dma_rebalance(net_dma);
4656 + spin_unlock(&net_dma->lock);
4662 + * netdev_dma_regiser - register the networking subsystem as a DMA client
4664 +static int __init netdev_dma_register(void)
4666 + spin_lock_init(&net_dma.lock);
4667 + dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask);
4668 + dma_async_client_register(&net_dma.client);
4669 + dma_async_client_chan_request(&net_dma.client);
4674 +static int __init netdev_dma_register(void) { return -ENODEV; }
4675 +#endif /* CONFIG_NET_DMA */
4678 + * netdev_compute_feature - compute conjunction of two feature sets
4679 + * @all: first feature set
4680 + * @one: second feature set
4682 + * Computes a new feature set after adding a device with feature set
4683 + * @one to the master device with current feature set @all. Returns
4684 + * the new feature set.
4686 +int netdev_compute_features(unsigned long all, unsigned long one)
4688 + /* if device needs checksumming, downgrade to hw checksumming */
4689 + if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
4690 + all ^= NETIF_F_NO_CSUM | NETIF_F_HW_CSUM;
4692 + /* if device can't do all checksum, downgrade to ipv4 */
4693 + if (all & NETIF_F_HW_CSUM && !(one & NETIF_F_HW_CSUM))
4694 + all ^= NETIF_F_HW_CSUM | NETIF_F_IP_CSUM;
4696 + if (one & NETIF_F_GSO)
4697 + one |= NETIF_F_GSO_SOFTWARE;
4698 + one |= NETIF_F_GSO;
4700 + /* If even one device supports robust GSO, enable it for all. */
4701 + if (one & NETIF_F_GSO_ROBUST)
4702 + all |= NETIF_F_GSO_ROBUST;
4704 + all &= one | NETIF_F_LLTX;
4706 + if (!(all & NETIF_F_ALL_CSUM))
4707 + all &= ~NETIF_F_SG;
4708 + if (!(all & NETIF_F_SG))
4709 + all &= ~NETIF_F_GSO_MASK;
4713 +EXPORT_SYMBOL(netdev_compute_features);
4715 +/* Initialize per network namespace state */
4716 +static int netdev_init(struct net *net)
4719 + INIT_LIST_HEAD(&net->dev_base_head);
4720 + rwlock_init(&dev_base_lock);
4722 + net->dev_name_head = kmalloc(
4723 + sizeof(*net->dev_name_head)*NETDEV_HASHENTRIES, GFP_KERNEL);
4724 + if (!net->dev_name_head)
4727 + net->dev_index_head = kmalloc(
4728 + sizeof(*net->dev_index_head)*NETDEV_HASHENTRIES, GFP_KERNEL);
4729 + if (!net->dev_index_head) {
4730 + kfree(net->dev_name_head);
4734 + for (i = 0; i < NETDEV_HASHENTRIES; i++)
4735 + INIT_HLIST_HEAD(&net->dev_name_head[i]);
4737 + for (i = 0; i < NETDEV_HASHENTRIES; i++)
4738 + INIT_HLIST_HEAD(&net->dev_index_head[i]);
4743 +static void netdev_exit(struct net *net)
4745 + kfree(net->dev_name_head);
4746 + kfree(net->dev_index_head);
4749 +static struct pernet_operations netdev_net_ops = {
4750 + .init = netdev_init,
4751 + .exit = netdev_exit,
4754 +static void default_device_exit(struct net *net)
4756 + struct net_device *dev, *next;
4758 + * Push all migratable of the network devices back to the
4759 + * initial network namespace
4762 + for_each_netdev_safe(net, dev, next) {
4765 + /* Ignore unmoveable devices (i.e. loopback) */
4766 + if (dev->features & NETIF_F_NETNS_LOCAL)
4769 + /* Push remaing network devices to init_net */
4770 + err = dev_change_net_namespace(dev, &init_net, "dev%d");
4772 + printk(KERN_WARNING "%s: failed to move %s to init_net: %d\n",
4773 + __func__, dev->name, err);
4774 + unregister_netdevice(dev);
4780 +static struct pernet_operations default_device_ops = {
4781 + .exit = default_device_exit,
4785 + * Initialize the DEV module. At boot time this walks the device list and
4786 + * unhooks any devices that fail to initialise (normally hardware not
4787 + * present) and leaves us with a valid list of present and active devices.
4792 + * This is called single threaded during boot, so no need
4793 + * to take the rtnl semaphore.
4795 +static int __init net_dev_init(void)
4797 + int i, rc = -ENOMEM;
4799 + BUG_ON(!dev_boot_phase);
4801 + if (dev_proc_init())
4804 + if (netdev_sysfs_init())
4807 + INIT_LIST_HEAD(&ptype_all);
4808 + for (i = 0; i < 16; i++)
4809 + INIT_LIST_HEAD(&ptype_base[i]);
4811 + if (register_pernet_subsys(&netdev_net_ops))
4814 + if (register_pernet_device(&default_device_ops))
4818 + * Initialise the packet receive queues.
4821 + for_each_possible_cpu(i) {
4822 + struct softnet_data *queue;
4824 + queue = &per_cpu(softnet_data, i);
4825 + skb_queue_head_init(&queue->input_pkt_queue);
4826 + queue->completion_queue = NULL;
4827 + INIT_LIST_HEAD(&queue->poll_list);
4828 + set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
4829 + queue->backlog_dev.weight = weight_p;
4830 + queue->backlog_dev.poll = process_backlog;
4831 + atomic_set(&queue->backlog_dev.refcnt, 1);
4834 + netdev_dma_register();
4836 + dev_boot_phase = 0;
4838 + open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
4839 + open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
4841 + hotcpu_notifier(dev_cpu_callback, 0);
4849 +subsys_initcall(net_dev_init);
4851 +EXPORT_SYMBOL(__dev_get_by_index);
4852 +EXPORT_SYMBOL(__dev_get_by_name);
4853 +EXPORT_SYMBOL(__dev_remove_pack);
4854 +EXPORT_SYMBOL(dev_valid_name);
4855 +EXPORT_SYMBOL(dev_add_pack);
4856 +EXPORT_SYMBOL(dev_alloc_name);
4857 +EXPORT_SYMBOL(dev_close);
4858 +EXPORT_SYMBOL(dev_get_by_flags);
4859 +EXPORT_SYMBOL(dev_get_by_index);
4860 +EXPORT_SYMBOL(dev_get_by_name);
4861 +EXPORT_SYMBOL(dev_open);
4862 +EXPORT_SYMBOL(dev_queue_xmit);
4863 +EXPORT_SYMBOL(dev_remove_pack);
4864 +EXPORT_SYMBOL(dev_set_allmulti);
4865 +EXPORT_SYMBOL(dev_set_promiscuity);
4866 +EXPORT_SYMBOL(dev_change_flags);
4867 +EXPORT_SYMBOL(dev_set_mtu);
4868 +EXPORT_SYMBOL(dev_set_mac_address);
4869 +EXPORT_SYMBOL(free_netdev);
4870 +EXPORT_SYMBOL(netdev_boot_setup_check);
4871 +EXPORT_SYMBOL(netdev_set_master);
4872 +EXPORT_SYMBOL(netdev_state_change);
4873 +EXPORT_SYMBOL(netif_receive_skb);
4874 +EXPORT_SYMBOL(netif_rx);
4875 +EXPORT_SYMBOL(register_gifconf);
4876 +EXPORT_SYMBOL(register_netdevice);
4877 +EXPORT_SYMBOL(register_netdevice_notifier);
4878 +EXPORT_SYMBOL(skb_checksum_help);
4879 +EXPORT_SYMBOL(synchronize_net);
4880 +EXPORT_SYMBOL(unregister_netdevice);
4881 +EXPORT_SYMBOL(unregister_netdevice_notifier);
4882 +EXPORT_SYMBOL(net_enable_timestamp);
4883 +EXPORT_SYMBOL(net_disable_timestamp);
4884 +EXPORT_SYMBOL(dev_get_flags);
4886 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4887 +EXPORT_SYMBOL(br_handle_frame_hook);
4888 +EXPORT_SYMBOL(br_fdb_get_hook);
4889 +EXPORT_SYMBOL(br_fdb_put_hook);
4893 +EXPORT_SYMBOL(dev_load);
4896 +EXPORT_PER_CPU_SYMBOL(softnet_data);
4897 diff -Nurb linux-2.6.22-594/net/core/net_namespace.c linux-2.6.22-595/net/core/net_namespace.c
4898 --- linux-2.6.22-594/net/core/net_namespace.c 2008-03-20 01:27:51.000000000 -0400
4899 +++ linux-2.6.22-595/net/core/net_namespace.c 2008-03-20 01:28:00.000000000 -0400
4900 @@ -112,10 +112,12 @@
4901 ops = list_entry(ptr, struct pernet_operations, list);
4903 error = ops->init(net);
4906 + printk(KERN_ALERT "Error setting up netns: %x\n", ops->init);
4914 diff -Nurb linux-2.6.22-594/net/ipv4/af_inet.c.orig linux-2.6.22-595/net/ipv4/af_inet.c.orig
4915 --- linux-2.6.22-594/net/ipv4/af_inet.c.orig 2008-03-20 01:27:51.000000000 -0400
4916 +++ linux-2.6.22-595/net/ipv4/af_inet.c.orig 1969-12-31 19:00:00.000000000 -0500
4919 - * INET An implementation of the TCP/IP protocol suite for the LINUX
4920 - * operating system. INET is implemented using the BSD Socket
4921 - * interface as the means of communication with the user level.
4923 - * PF_INET protocol family socket handler.
4925 - * Version: $Id: af_inet.c,v 1.137 2002/02/01 22:01:03 davem Exp $
4927 - * Authors: Ross Biro
4928 - * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
4929 - * Florian La Roche, <flla@stud.uni-sb.de>
4930 - * Alan Cox, <A.Cox@swansea.ac.uk>
4932 - * Changes (see also sock.c)
4935 - * Karl Knutson : Socket protocol table
4936 - * A.N.Kuznetsov : Socket death error in accept().
4937 - * John Richardson : Fix non blocking error in connect()
4938 - * so sockets that fail to connect
4939 - * don't return -EINPROGRESS.
4940 - * Alan Cox : Asynchronous I/O support
4941 - * Alan Cox : Keep correct socket pointer on sock
4943 - * when accept() ed
4944 - * Alan Cox : Semantics of SO_LINGER aren't state
4945 - * moved to close when you look carefully.
4946 - * With this fixed and the accept bug fixed
4947 - * some RPC stuff seems happier.
4948 - * Niibe Yutaka : 4.4BSD style write async I/O
4950 - * Tony Gale : Fixed reuse semantics.
4951 - * Alan Cox : bind() shouldn't abort existing but dead
4952 - * sockets. Stops FTP netin:.. I hope.
4953 - * Alan Cox : bind() works correctly for RAW sockets.
4954 - * Note that FreeBSD at least was broken
4955 - * in this respect so be careful with
4956 - * compatibility tests...
4957 - * Alan Cox : routing cache support
4958 - * Alan Cox : memzero the socket structure for
4960 - * Matt Day : nonblock connect error handler
4961 - * Alan Cox : Allow large numbers of pending sockets
4962 - * (eg for big web sites), but only if
4963 - * specifically application requested.
4964 - * Alan Cox : New buffering throughout IP. Used
4966 - * Alan Cox : New buffering now used smartly.
4967 - * Alan Cox : BSD rather than common sense
4968 - * interpretation of listen.
4969 - * Germano Caronni : Assorted small races.
4970 - * Alan Cox : sendmsg/recvmsg basic support.
4971 - * Alan Cox : Only sendmsg/recvmsg now supported.
4972 - * Alan Cox : Locked down bind (see security list).
4973 - * Alan Cox : Loosened bind a little.
4974 - * Mike McLagan : ADD/DEL DLCI Ioctls
4975 - * Willy Konynenberg : Transparent proxying support.
4976 - * David S. Miller : New socket lookup architecture.
4977 - * Some other random speedups.
4978 - * Cyrus Durgin : Cleaned up file for kmod hacks.
4979 - * Andi Kleen : Fix inet_stream_connect TCP race.
4981 - * This program is free software; you can redistribute it and/or
4982 - * modify it under the terms of the GNU General Public License
4983 - * as published by the Free Software Foundation; either version
4984 - * 2 of the License, or (at your option) any later version.
4987 -#include <linux/err.h>
4988 -#include <linux/errno.h>
4989 -#include <linux/types.h>
4990 -#include <linux/socket.h>
4991 -#include <linux/in.h>
4992 -#include <linux/kernel.h>
4993 -#include <linux/module.h>
4994 -#include <linux/sched.h>
4995 -#include <linux/timer.h>
4996 -#include <linux/string.h>
4997 -#include <linux/sockios.h>
4998 -#include <linux/net.h>
4999 -#include <linux/capability.h>
5000 -#include <linux/fcntl.h>
5001 -#include <linux/mm.h>
5002 -#include <linux/interrupt.h>
5003 -#include <linux/stat.h>
5004 -#include <linux/init.h>
5005 -#include <linux/poll.h>
5006 -#include <linux/netfilter_ipv4.h>
5007 -#include <linux/random.h>
5009 -#include <asm/uaccess.h>
5010 -#include <asm/system.h>
5012 -#include <linux/inet.h>
5013 -#include <linux/igmp.h>
5014 -#include <linux/inetdevice.h>
5015 -#include <linux/netdevice.h>
5016 -#include <net/ip.h>
5017 -#include <net/protocol.h>
5018 -#include <net/arp.h>
5019 -#include <net/route.h>
5020 -#include <net/ip_fib.h>
5021 -#include <net/inet_connection_sock.h>
5022 -#include <net/tcp.h>
5023 -#include <net/udp.h>
5024 -#include <net/udplite.h>
5025 -#include <linux/skbuff.h>
5026 -#include <net/sock.h>
5027 -#include <net/raw.h>
5028 -#include <net/icmp.h>
5029 -#include <net/ipip.h>
5030 -#include <net/inet_common.h>
5031 -#include <net/xfrm.h>
5032 -#ifdef CONFIG_IP_MROUTE
5033 -#include <linux/mroute.h>
5035 -#include <linux/vs_limit.h>
5037 -DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly;
5039 -extern void ip_mc_drop_socket(struct sock *sk);
5041 -/* The inetsw table contains everything that inet_create needs to
5042 - * build a new socket.
5044 -static struct list_head inetsw[SOCK_MAX];
5045 -static DEFINE_SPINLOCK(inetsw_lock);
5047 -/* New destruction routine */
5049 -void inet_sock_destruct(struct sock *sk)
5051 - struct inet_sock *inet = inet_sk(sk);
5053 - __skb_queue_purge(&sk->sk_receive_queue);
5054 - __skb_queue_purge(&sk->sk_error_queue);
5056 - if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
5057 - printk("Attempt to release TCP socket in state %d %p\n",
5058 - sk->sk_state, sk);
5061 - if (!sock_flag(sk, SOCK_DEAD)) {
5062 - printk("Attempt to release alive inet socket %p\n", sk);
5066 - BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
5067 - BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
5068 - BUG_TRAP(!sk->sk_wmem_queued);
5069 - BUG_TRAP(!sk->sk_forward_alloc);
5072 - dst_release(sk->sk_dst_cache);
5073 - sk_refcnt_debug_dec(sk);
5077 - * The routines beyond this point handle the behaviour of an AF_INET
5078 - * socket object. Mostly it punts to the subprotocols of IP to do
5083 - * Automatically bind an unbound socket.
5086 -static int inet_autobind(struct sock *sk)
5088 - struct inet_sock *inet;
5089 - /* We may need to bind the socket. */
5091 - inet = inet_sk(sk);
5093 - if (sk->sk_prot->get_port(sk, 0)) {
5097 - inet->sport = htons(inet->num);
5098 - sk->sk_xid = vx_current_xid();
5099 - sk->sk_nid = nx_current_nid();
5106 - * Move a socket into listening state.
5108 -int inet_listen(struct socket *sock, int backlog)
5110 - struct sock *sk = sock->sk;
5111 - unsigned char old_state;
5117 - if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
5120 - old_state = sk->sk_state;
5121 - if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
5124 - /* Really, if the socket is already in listen state
5125 - * we can only allow the backlog to be adjusted.
5127 - if (old_state != TCP_LISTEN) {
5128 - err = inet_csk_listen_start(sk, backlog);
5132 - sk->sk_max_ack_backlog = backlog;
5140 -u32 inet_ehash_secret __read_mostly;
5141 -EXPORT_SYMBOL(inet_ehash_secret);
5144 - * inet_ehash_secret must be set exactly once
5145 - * Instead of using a dedicated spinlock, we (ab)use inetsw_lock
5147 -void build_ehash_secret(void)
5151 - get_random_bytes(&rnd, sizeof(rnd));
5152 - } while (rnd == 0);
5153 - spin_lock_bh(&inetsw_lock);
5154 - if (!inet_ehash_secret)
5155 - inet_ehash_secret = rnd;
5156 - spin_unlock_bh(&inetsw_lock);
5158 -EXPORT_SYMBOL(build_ehash_secret);
5161 - * Create an inet socket.
5164 -static int inet_create(struct socket *sock, int protocol)
5167 - struct list_head *p;
5168 - struct inet_protosw *answer;
5169 - struct inet_sock *inet;
5170 - struct proto *answer_prot;
5171 - unsigned char answer_flags;
5172 - char answer_no_check;
5173 - int try_loading_module = 0;
5176 - if (sock->type != SOCK_RAW &&
5177 - sock->type != SOCK_DGRAM &&
5178 - !inet_ehash_secret)
5179 - build_ehash_secret();
5181 - sock->state = SS_UNCONNECTED;
5183 - /* Look for the requested type/protocol pair. */
5186 - err = -ESOCKTNOSUPPORT;
5188 - list_for_each_rcu(p, &inetsw[sock->type]) {
5189 - answer = list_entry(p, struct inet_protosw, list);
5191 - /* Check the non-wild match. */
5192 - if (protocol == answer->protocol) {
5193 - if (protocol != IPPROTO_IP)
5196 - /* Check for the two wild cases. */
5197 - if (IPPROTO_IP == protocol) {
5198 - protocol = answer->protocol;
5201 - if (IPPROTO_IP == answer->protocol)
5204 - err = -EPROTONOSUPPORT;
5208 - if (unlikely(answer == NULL)) {
5209 - if (try_loading_module < 2) {
5210 - rcu_read_unlock();
5212 - * Be more specific, e.g. net-pf-2-proto-132-type-1
5213 - * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
5215 - if (++try_loading_module == 1)
5216 - request_module("net-pf-%d-proto-%d-type-%d",
5217 - PF_INET, protocol, sock->type);
5219 - * Fall back to generic, e.g. net-pf-2-proto-132
5220 - * (net-pf-PF_INET-proto-IPPROTO_SCTP)
5223 - request_module("net-pf-%d-proto-%d",
5224 - PF_INET, protocol);
5225 - goto lookup_protocol;
5227 - goto out_rcu_unlock;
5231 - if ((protocol == IPPROTO_ICMP) &&
5232 - nx_capable(answer->capability, NXC_RAW_ICMP))
5234 - if (sock->type == SOCK_RAW &&
5235 - nx_capable(answer->capability, NXC_RAW_SOCKET))
5237 - if (answer->capability > 0 && !capable(answer->capability))
5238 - goto out_rcu_unlock;
5240 - sock->ops = answer->ops;
5241 - answer_prot = answer->prot;
5242 - answer_no_check = answer->no_check;
5243 - answer_flags = answer->flags;
5244 - rcu_read_unlock();
5246 - BUG_TRAP(answer_prot->slab != NULL);
5249 - sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);
5254 - sk->sk_no_check = answer_no_check;
5255 - if (INET_PROTOSW_REUSE & answer_flags)
5258 - inet = inet_sk(sk);
5259 - inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
5261 - if (SOCK_RAW == sock->type) {
5262 - inet->num = protocol;
5263 - if (IPPROTO_RAW == protocol)
5264 - inet->hdrincl = 1;
5267 - if (ipv4_config.no_pmtu_disc)
5268 - inet->pmtudisc = IP_PMTUDISC_DONT;
5270 - inet->pmtudisc = IP_PMTUDISC_WANT;
5274 - sock_init_data(sock, sk);
5276 - sk->sk_destruct = inet_sock_destruct;
5277 - sk->sk_family = PF_INET;
5278 - sk->sk_protocol = protocol;
5279 - sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
5281 - inet->uc_ttl = -1;
5282 - inet->mc_loop = 1;
5284 - inet->mc_index = 0;
5285 - inet->mc_list = NULL;
5287 - sk_refcnt_debug_inc(sk);
5290 - /* It assumes that any protocol which allows
5291 - * the user to assign a number at socket
5292 - * creation time automatically
5295 - inet->sport = htons(inet->num);
5296 - /* Add to protocol hash chains. */
5297 - sk->sk_prot->hash(sk);
5300 - if (sk->sk_prot->init) {
5301 - err = sk->sk_prot->init(sk);
5303 - sk_common_release(sk);
5308 - rcu_read_unlock();
5314 - * The peer socket should always be NULL (or else). When we call this
5315 - * function we are destroying the object and from then on nobody
5316 - * should refer to it.
5318 -int inet_release(struct socket *sock)
5320 - struct sock *sk = sock->sk;
5325 - /* Applications forget to leave groups before exiting */
5326 - ip_mc_drop_socket(sk);
5328 - /* If linger is set, we don't return until the close
5329 - * is complete. Otherwise we return immediately. The
5330 - * actually closing is done the same either way.
5332 - * If the close is due to the process exiting, we never
5336 - if (sock_flag(sk, SOCK_LINGER) &&
5337 - !(current->flags & PF_EXITING))
5338 - timeout = sk->sk_lingertime;
5340 - sk->sk_prot->close(sk, timeout);
5345 -/* It is off by default, see below. */
5346 -int sysctl_ip_nonlocal_bind __read_mostly;
5348 -int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
5350 - struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
5351 - struct sock *sk = sock->sk;
5352 - struct inet_sock *inet = inet_sk(sk);
5353 - struct nx_v4_sock_addr nsa;
5354 - unsigned short snum;
5358 - /* If the socket has its own bind function then use it. (RAW) */
5359 - if (sk->sk_prot->bind) {
5360 - err = sk->sk_prot->bind(sk, uaddr, addr_len);
5364 - if (addr_len < sizeof(struct sockaddr_in))
5367 - err = v4_map_sock_addr(inet, addr, &nsa);
5371 - chk_addr_ret = inet_addr_type(nsa.saddr);
5373 - /* Not specified by any standard per-se, however it breaks too
5374 - * many applications when removed. It is unfortunate since
5375 - * allowing applications to make a non-local bind solves
5376 - * several problems with systems using dynamic addressing.
5377 - * (ie. your servers still start up even if your ISDN link
5378 - * is temporarily down)
5380 - err = -EADDRNOTAVAIL;
5381 - if (!sysctl_ip_nonlocal_bind &&
5382 - !inet->freebind &&
5383 - nsa.saddr != INADDR_ANY &&
5384 - chk_addr_ret != RTN_LOCAL &&
5385 - chk_addr_ret != RTN_MULTICAST &&
5386 - chk_addr_ret != RTN_BROADCAST)
5389 - snum = ntohs(addr->sin_port);
5391 - if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
5394 - /* We keep a pair of addresses. rcv_saddr is the one
5395 - * used by hash lookups, and saddr is used for transmit.
5397 - * In the BSD API these are the same except where it
5398 - * would be illegal to use them (multicast/broadcast) in
5399 - * which case the sending device address is used.
5403 - /* Check these errors (active socket, double bind). */
5405 - if (sk->sk_state != TCP_CLOSE || inet->num)
5406 - goto out_release_sock;
5408 - v4_set_sock_addr(inet, &nsa);
5409 - if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
5410 - inet->saddr = 0; /* Use device */
5412 - /* Make sure we are allowed to bind here. */
5413 - if (sk->sk_prot->get_port(sk, snum)) {
5414 - inet->saddr = inet->rcv_saddr = 0;
5415 - err = -EADDRINUSE;
5416 - goto out_release_sock;
5419 - if (inet->rcv_saddr)
5420 - sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
5422 - sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
5423 - inet->sport = htons(inet->num);
5434 -int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
5435 - int addr_len, int flags)
5437 - struct sock *sk = sock->sk;
5439 - if (uaddr->sa_family == AF_UNSPEC)
5440 - return sk->sk_prot->disconnect(sk, flags);
5442 - if (!inet_sk(sk)->num && inet_autobind(sk))
5444 - return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
5447 -static long inet_wait_for_connect(struct sock *sk, long timeo)
5449 - DEFINE_WAIT(wait);
5451 - prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5453 - /* Basic assumption: if someone sets sk->sk_err, he _must_
5454 - * change state of the socket from TCP_SYN_*.
5455 - * Connect() does not allow to get error notifications
5456 - * without closing the socket.
5458 - while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
5460 - timeo = schedule_timeout(timeo);
5462 - if (signal_pending(current) || !timeo)
5464 - prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5466 - finish_wait(sk->sk_sleep, &wait);
5471 - * Connect to a remote host. There is regrettably still a little
5472 - * TCP 'magic' in here.
5474 -int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
5475 - int addr_len, int flags)
5477 - struct sock *sk = sock->sk;
5483 - if (uaddr->sa_family == AF_UNSPEC) {
5484 - err = sk->sk_prot->disconnect(sk, flags);
5485 - sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
5489 - switch (sock->state) {
5493 - case SS_CONNECTED:
5496 - case SS_CONNECTING:
5498 - /* Fall out of switch with err, set for this state */
5500 - case SS_UNCONNECTED:
5502 - if (sk->sk_state != TCP_CLOSE)
5505 - err = sk->sk_prot->connect(sk, uaddr, addr_len);
5509 - sock->state = SS_CONNECTING;
5511 - /* Just entered SS_CONNECTING state; the only
5512 - * difference is that return value in non-blocking
5513 - * case is EINPROGRESS, rather than EALREADY.
5515 - err = -EINPROGRESS;
5519 - timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
5521 - if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
5522 - /* Error code is set above */
5523 - if (!timeo || !inet_wait_for_connect(sk, timeo))
5526 - err = sock_intr_errno(timeo);
5527 - if (signal_pending(current))
5531 - /* Connection was closed by RST, timeout, ICMP error
5532 - * or another process disconnected us.
5534 - if (sk->sk_state == TCP_CLOSE)
5537 - /* sk->sk_err may be not zero now, if RECVERR was ordered by user
5538 - * and error was received after socket entered established state.
5539 - * Hence, it is handled normally after connect() return successfully.
5542 - sock->state = SS_CONNECTED;
5549 - err = sock_error(sk) ? : -ECONNABORTED;
5550 - sock->state = SS_UNCONNECTED;
5551 - if (sk->sk_prot->disconnect(sk, flags))
5552 - sock->state = SS_DISCONNECTING;
5557 - * Accept a pending connection. The TCP layer now gives BSD semantics.
5560 -int inet_accept(struct socket *sock, struct socket *newsock, int flags)
5562 - struct sock *sk1 = sock->sk;
5563 - int err = -EINVAL;
5564 - struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
5571 - BUG_TRAP((1 << sk2->sk_state) &
5572 - (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
5574 - sock_graft(sk2, newsock);
5576 - newsock->state = SS_CONNECTED;
5578 - release_sock(sk2);
5585 - * This does both peername and sockname.
5587 -int inet_getname(struct socket *sock, struct sockaddr *uaddr,
5588 - int *uaddr_len, int peer)
5590 - struct sock *sk = sock->sk;
5591 - struct inet_sock *inet = inet_sk(sk);
5592 - struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
5594 - sin->sin_family = AF_INET;
5596 - if (!inet->dport ||
5597 - (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
5600 - sin->sin_port = inet->dport;
5601 - sin->sin_addr.s_addr =
5602 - nx_map_sock_lback(sk->sk_nx_info, inet->daddr);
5604 - __be32 addr = inet->rcv_saddr;
5606 - addr = inet->saddr;
5607 - addr = nx_map_sock_lback(sk->sk_nx_info, addr);
5608 - sin->sin_port = inet->sport;
5609 - sin->sin_addr.s_addr = addr;
5611 - memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
5612 - *uaddr_len = sizeof(*sin);
5616 -int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
5619 - struct sock *sk = sock->sk;
5621 - /* We may need to bind the socket. */
5622 - if (!inet_sk(sk)->num && inet_autobind(sk))
5625 - return sk->sk_prot->sendmsg(iocb, sk, msg, size);
5629 -static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
5631 - struct sock *sk = sock->sk;
5633 - /* We may need to bind the socket. */
5634 - if (!inet_sk(sk)->num && inet_autobind(sk))
5637 - if (sk->sk_prot->sendpage)
5638 - return sk->sk_prot->sendpage(sk, page, offset, size, flags);
5639 - return sock_no_sendpage(sock, page, offset, size, flags);
5643 -int inet_shutdown(struct socket *sock, int how)
5645 - struct sock *sk = sock->sk;
5648 - /* This should really check to make sure
5649 - * the socket is a TCP socket. (WHY AC...)
5651 - how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
5654 - if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
5658 - if (sock->state == SS_CONNECTING) {
5659 - if ((1 << sk->sk_state) &
5660 - (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
5661 - sock->state = SS_DISCONNECTING;
5663 - sock->state = SS_CONNECTED;
5666 - switch (sk->sk_state) {
5669 - /* Hack to wake up other listeners, who can poll for
5670 - POLLHUP, even on eg. unconnected UDP sockets -- RR */
5672 - sk->sk_shutdown |= how;
5673 - if (sk->sk_prot->shutdown)
5674 - sk->sk_prot->shutdown(sk, how);
5677 - /* Remaining two branches are temporary solution for missing
5678 - * close() in multithreaded environment. It is _not_ a good idea,
5679 - * but we have no choice until close() is repaired at VFS level.
5682 - if (!(how & RCV_SHUTDOWN))
5684 - /* Fall through */
5685 - case TCP_SYN_SENT:
5686 - err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
5687 - sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
5691 - /* Wake up anyone sleeping in poll. */
5692 - sk->sk_state_change(sk);
5698 - * ioctl() calls you can issue on an INET socket. Most of these are
5699 - * device configuration and stuff and very rarely used. Some ioctls
5700 - * pass on to the socket itself.
5702 - * NOTE: I like the idea of a module for the config stuff. ie ifconfig
5703 - * loads the devconfigure module does its configuring and unloads it.
5704 - * There's a good 20K of config code hanging around the kernel.
5707 -int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
5709 - struct sock *sk = sock->sk;
5714 - err = sock_get_timestamp(sk, (struct timeval __user *)arg);
5716 - case SIOCGSTAMPNS:
5717 - err = sock_get_timestampns(sk, (struct timespec __user *)arg);
5722 - err = ip_rt_ioctl(cmd, (void __user *)arg);
5727 - err = arp_ioctl(cmd, (void __user *)arg);
5731 - case SIOCGIFBRDADDR:
5732 - case SIOCSIFBRDADDR:
5733 - case SIOCGIFNETMASK:
5734 - case SIOCSIFNETMASK:
5735 - case SIOCGIFDSTADDR:
5736 - case SIOCSIFDSTADDR:
5737 - case SIOCSIFPFLAGS:
5738 - case SIOCGIFPFLAGS:
5739 - case SIOCSIFFLAGS:
5740 - err = devinet_ioctl(cmd, (void __user *)arg);
5743 - if (sk->sk_prot->ioctl)
5744 - err = sk->sk_prot->ioctl(sk, cmd, arg);
5746 - err = -ENOIOCTLCMD;
5752 -const struct proto_ops inet_stream_ops = {
5753 - .family = PF_INET,
5754 - .owner = THIS_MODULE,
5755 - .release = inet_release,
5756 - .bind = inet_bind,
5757 - .connect = inet_stream_connect,
5758 - .socketpair = sock_no_socketpair,
5759 - .accept = inet_accept,
5760 - .getname = inet_getname,
5762 - .ioctl = inet_ioctl,
5763 - .listen = inet_listen,
5764 - .shutdown = inet_shutdown,
5765 - .setsockopt = sock_common_setsockopt,
5766 - .getsockopt = sock_common_getsockopt,
5767 - .sendmsg = tcp_sendmsg,
5768 - .recvmsg = sock_common_recvmsg,
5769 - .mmap = sock_no_mmap,
5770 - .sendpage = tcp_sendpage,
5771 -#ifdef CONFIG_COMPAT
5772 - .compat_setsockopt = compat_sock_common_setsockopt,
5773 - .compat_getsockopt = compat_sock_common_getsockopt,
5777 -const struct proto_ops inet_dgram_ops = {
5778 - .family = PF_INET,
5779 - .owner = THIS_MODULE,
5780 - .release = inet_release,
5781 - .bind = inet_bind,
5782 - .connect = inet_dgram_connect,
5783 - .socketpair = sock_no_socketpair,
5784 - .accept = sock_no_accept,
5785 - .getname = inet_getname,
5787 - .ioctl = inet_ioctl,
5788 - .listen = sock_no_listen,
5789 - .shutdown = inet_shutdown,
5790 - .setsockopt = sock_common_setsockopt,
5791 - .getsockopt = sock_common_getsockopt,
5792 - .sendmsg = inet_sendmsg,
5793 - .recvmsg = sock_common_recvmsg,
5794 - .mmap = sock_no_mmap,
5795 - .sendpage = inet_sendpage,
5796 -#ifdef CONFIG_COMPAT
5797 - .compat_setsockopt = compat_sock_common_setsockopt,
5798 - .compat_getsockopt = compat_sock_common_getsockopt,
5803 - * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
5806 -static const struct proto_ops inet_sockraw_ops = {
5807 - .family = PF_INET,
5808 - .owner = THIS_MODULE,
5809 - .release = inet_release,
5810 - .bind = inet_bind,
5811 - .connect = inet_dgram_connect,
5812 - .socketpair = sock_no_socketpair,
5813 - .accept = sock_no_accept,
5814 - .getname = inet_getname,
5815 - .poll = datagram_poll,
5816 - .ioctl = inet_ioctl,
5817 - .listen = sock_no_listen,
5818 - .shutdown = inet_shutdown,
5819 - .setsockopt = sock_common_setsockopt,
5820 - .getsockopt = sock_common_getsockopt,
5821 - .sendmsg = inet_sendmsg,
5822 - .recvmsg = sock_common_recvmsg,
5823 - .mmap = sock_no_mmap,
5824 - .sendpage = inet_sendpage,
5825 -#ifdef CONFIG_COMPAT
5826 - .compat_setsockopt = compat_sock_common_setsockopt,
5827 - .compat_getsockopt = compat_sock_common_getsockopt,
5831 -static struct net_proto_family inet_family_ops = {
5832 - .family = PF_INET,
5833 - .create = inet_create,
5834 - .owner = THIS_MODULE,
5837 -/* Upon startup we insert all the elements in inetsw_array[] into
5838 - * the linked list inetsw.
5840 -static struct inet_protosw inetsw_array[] =
5843 - .type = SOCK_STREAM,
5844 - .protocol = IPPROTO_TCP,
5845 - .prot = &tcp_prot,
5846 - .ops = &inet_stream_ops,
5849 - .flags = INET_PROTOSW_PERMANENT |
5850 - INET_PROTOSW_ICSK,
5854 - .type = SOCK_DGRAM,
5855 - .protocol = IPPROTO_UDP,
5856 - .prot = &udp_prot,
5857 - .ops = &inet_dgram_ops,
5859 - .no_check = UDP_CSUM_DEFAULT,
5860 - .flags = INET_PROTOSW_PERMANENT,
5866 - .protocol = IPPROTO_IP, /* wild card */
5867 - .prot = &raw_prot,
5868 - .ops = &inet_sockraw_ops,
5869 - .capability = CAP_NET_RAW,
5870 - .no_check = UDP_CSUM_DEFAULT,
5871 - .flags = INET_PROTOSW_REUSE,
5875 -#define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
5877 -void inet_register_protosw(struct inet_protosw *p)
5879 - struct list_head *lh;
5880 - struct inet_protosw *answer;
5881 - int protocol = p->protocol;
5882 - struct list_head *last_perm;
5884 - spin_lock_bh(&inetsw_lock);
5886 - if (p->type >= SOCK_MAX)
5889 - /* If we are trying to override a permanent protocol, bail. */
5891 - last_perm = &inetsw[p->type];
5892 - list_for_each(lh, &inetsw[p->type]) {
5893 - answer = list_entry(lh, struct inet_protosw, list);
5895 - /* Check only the non-wild match. */
5896 - if (INET_PROTOSW_PERMANENT & answer->flags) {
5897 - if (protocol == answer->protocol)
5905 - goto out_permanent;
5907 - /* Add the new entry after the last permanent entry if any, so that
5908 - * the new entry does not override a permanent entry when matched with
5909 - * a wild-card protocol. But it is allowed to override any existing
5910 - * non-permanent entry. This means that when we remove this entry, the
5911 - * system automatically returns to the old behavior.
5913 - list_add_rcu(&p->list, last_perm);
5915 - spin_unlock_bh(&inetsw_lock);
5917 - synchronize_net();
5922 - printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
5928 - "Ignoring attempt to register invalid socket type %d.\n",
5933 -void inet_unregister_protosw(struct inet_protosw *p)
5935 - if (INET_PROTOSW_PERMANENT & p->flags) {
5937 - "Attempt to unregister permanent protocol %d.\n",
5940 - spin_lock_bh(&inetsw_lock);
5941 - list_del_rcu(&p->list);
5942 - spin_unlock_bh(&inetsw_lock);
5944 - synchronize_net();
5949 - * Shall we try to damage output packets if routing dev changes?
5952 -int sysctl_ip_dynaddr __read_mostly;
5954 -static int inet_sk_reselect_saddr(struct sock *sk)
5956 - struct inet_sock *inet = inet_sk(sk);
5958 - struct rtable *rt;
5959 - __be32 old_saddr = inet->saddr;
5961 - __be32 daddr = inet->daddr;
5963 - if (inet->opt && inet->opt->srr)
5964 - daddr = inet->opt->faddr;
5966 - /* Query new route. */
5967 - err = ip_route_connect(&rt, daddr, 0,
5968 - RT_CONN_FLAGS(sk),
5969 - sk->sk_bound_dev_if,
5971 - inet->sport, inet->dport, sk, 0);
5975 - sk_setup_caps(sk, &rt->u.dst);
5977 - new_saddr = rt->rt_src;
5979 - if (new_saddr == old_saddr)
5982 - if (sysctl_ip_dynaddr > 1) {
5983 - printk(KERN_INFO "%s(): shifting inet->"
5984 - "saddr from %d.%d.%d.%d to %d.%d.%d.%d\n",
5986 - NIPQUAD(old_saddr),
5987 - NIPQUAD(new_saddr));
5990 - inet->saddr = inet->rcv_saddr = new_saddr;
5993 - * XXX The only one ugly spot where we need to
5994 - * XXX really change the sockets identity after
5995 - * XXX it has entered the hashes. -DaveM
5997 - * Besides that, it does not check for connection
5998 - * uniqueness. Wait for troubles.
6000 - __sk_prot_rehash(sk);
6004 -int inet_sk_rebuild_header(struct sock *sk)
6006 - struct inet_sock *inet = inet_sk(sk);
6007 - struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
6011 - /* Route is OK, nothing to do. */
6016 - daddr = inet->daddr;
6017 - if (inet->opt && inet->opt->srr)
6018 - daddr = inet->opt->faddr;
6020 - struct flowi fl = {
6021 - .oif = sk->sk_bound_dev_if,
6025 - .saddr = inet->saddr,
6026 - .tos = RT_CONN_FLAGS(sk),
6029 - .proto = sk->sk_protocol,
6032 - .sport = inet->sport,
6033 - .dport = inet->dport,
6038 - security_sk_classify_flow(sk, &fl);
6039 - err = ip_route_output_flow(&rt, &fl, sk, 0);
6042 - sk_setup_caps(sk, &rt->u.dst);
6044 - /* Routing failed... */
6045 - sk->sk_route_caps = 0;
6047 - * Other protocols have to map its equivalent state to TCP_SYN_SENT.
6048 - * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
6050 - if (!sysctl_ip_dynaddr ||
6051 - sk->sk_state != TCP_SYN_SENT ||
6052 - (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
6053 - (err = inet_sk_reselect_saddr(sk)) != 0)
6054 - sk->sk_err_soft = -err;
6060 -EXPORT_SYMBOL(inet_sk_rebuild_header);
6062 -static int inet_gso_send_check(struct sk_buff *skb)
6064 - struct iphdr *iph;
6065 - struct net_protocol *ops;
6068 - int err = -EINVAL;
6070 - if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
6073 - iph = ip_hdr(skb);
6074 - ihl = iph->ihl * 4;
6075 - if (ihl < sizeof(*iph))
6078 - if (unlikely(!pskb_may_pull(skb, ihl)))
6081 - __skb_pull(skb, ihl);
6082 - skb_reset_transport_header(skb);
6083 - iph = ip_hdr(skb);
6084 - proto = iph->protocol & (MAX_INET_PROTOS - 1);
6085 - err = -EPROTONOSUPPORT;
6088 - ops = rcu_dereference(inet_protos[proto]);
6089 - if (likely(ops && ops->gso_send_check))
6090 - err = ops->gso_send_check(skb);
6091 - rcu_read_unlock();
6097 -static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
6099 - struct sk_buff *segs = ERR_PTR(-EINVAL);
6100 - struct iphdr *iph;
6101 - struct net_protocol *ops;
6106 - if (unlikely(skb_shinfo(skb)->gso_type &
6114 - if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
6117 - iph = ip_hdr(skb);
6118 - ihl = iph->ihl * 4;
6119 - if (ihl < sizeof(*iph))
6122 - if (unlikely(!pskb_may_pull(skb, ihl)))
6125 - __skb_pull(skb, ihl);
6126 - skb_reset_transport_header(skb);
6127 - iph = ip_hdr(skb);
6128 - id = ntohs(iph->id);
6129 - proto = iph->protocol & (MAX_INET_PROTOS - 1);
6130 - segs = ERR_PTR(-EPROTONOSUPPORT);
6133 - ops = rcu_dereference(inet_protos[proto]);
6134 - if (likely(ops && ops->gso_segment))
6135 - segs = ops->gso_segment(skb, features);
6136 - rcu_read_unlock();
6138 - if (!segs || unlikely(IS_ERR(segs)))
6143 - iph = ip_hdr(skb);
6144 - iph->id = htons(id++);
6145 - iph->tot_len = htons(skb->len - skb->mac_len);
6147 - iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
6148 - } while ((skb = skb->next));
6154 -unsigned long snmp_fold_field(void *mib[], int offt)
6156 - unsigned long res = 0;
6159 - for_each_possible_cpu(i) {
6160 - res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
6161 - res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
6165 -EXPORT_SYMBOL_GPL(snmp_fold_field);
6167 -int snmp_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
6169 - BUG_ON(ptr == NULL);
6170 - ptr[0] = __alloc_percpu(mibsize);
6173 - ptr[1] = __alloc_percpu(mibsize);
6178 - free_percpu(ptr[0]);
6183 -EXPORT_SYMBOL_GPL(snmp_mib_init);
6185 -void snmp_mib_free(void *ptr[2])
6187 - BUG_ON(ptr == NULL);
6188 - free_percpu(ptr[0]);
6189 - free_percpu(ptr[1]);
6190 - ptr[0] = ptr[1] = NULL;
6192 -EXPORT_SYMBOL_GPL(snmp_mib_free);
6194 -#ifdef CONFIG_IP_MULTICAST
6195 -static struct net_protocol igmp_protocol = {
6196 - .handler = igmp_rcv,
6200 -static struct net_protocol tcp_protocol = {
6201 - .handler = tcp_v4_rcv,
6202 - .err_handler = tcp_v4_err,
6203 - .gso_send_check = tcp_v4_gso_send_check,
6204 - .gso_segment = tcp_tso_segment,
6208 -static struct net_protocol udp_protocol = {
6209 - .handler = udp_rcv,
6210 - .err_handler = udp_err,
6214 -static struct net_protocol icmp_protocol = {
6215 - .handler = icmp_rcv,
6218 -static int __init init_ipv4_mibs(void)
6220 - if (snmp_mib_init((void **)net_statistics,
6221 - sizeof(struct linux_mib),
6222 - __alignof__(struct linux_mib)) < 0)
6224 - if (snmp_mib_init((void **)ip_statistics,
6225 - sizeof(struct ipstats_mib),
6226 - __alignof__(struct ipstats_mib)) < 0)
6228 - if (snmp_mib_init((void **)icmp_statistics,
6229 - sizeof(struct icmp_mib),
6230 - __alignof__(struct icmp_mib)) < 0)
6231 - goto err_icmp_mib;
6232 - if (snmp_mib_init((void **)tcp_statistics,
6233 - sizeof(struct tcp_mib),
6234 - __alignof__(struct tcp_mib)) < 0)
6236 - if (snmp_mib_init((void **)udp_statistics,
6237 - sizeof(struct udp_mib),
6238 - __alignof__(struct udp_mib)) < 0)
6240 - if (snmp_mib_init((void **)udplite_statistics,
6241 - sizeof(struct udp_mib),
6242 - __alignof__(struct udp_mib)) < 0)
6243 - goto err_udplite_mib;
6250 - snmp_mib_free((void **)udp_statistics);
6252 - snmp_mib_free((void **)tcp_statistics);
6254 - snmp_mib_free((void **)icmp_statistics);
6256 - snmp_mib_free((void **)ip_statistics);
6258 - snmp_mib_free((void **)net_statistics);
6263 -static int ipv4_proc_init(void);
6266 - * IP protocol layer initialiser
6269 -static struct packet_type ip_packet_type = {
6270 - .type = __constant_htons(ETH_P_IP),
6272 - .gso_send_check = inet_gso_send_check,
6273 - .gso_segment = inet_gso_segment,
6276 -static int __init inet_init(void)
6278 - struct sk_buff *dummy_skb;
6279 - struct inet_protosw *q;
6280 - struct list_head *r;
6283 - BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
6285 - rc = proto_register(&tcp_prot, 1);
6289 - rc = proto_register(&udp_prot, 1);
6291 - goto out_unregister_tcp_proto;
6293 - rc = proto_register(&raw_prot, 1);
6295 - goto out_unregister_udp_proto;
6298 - * Tell SOCKET that we are alive...
6301 - (void)sock_register(&inet_family_ops);
6304 - * Add all the base protocols.
6307 - if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
6308 - printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
6309 - if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
6310 - printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
6311 - if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
6312 - printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
6313 -#ifdef CONFIG_IP_MULTICAST
6314 - if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
6315 - printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
6318 - /* Register the socket-side information for inet_create. */
6319 - for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
6320 - INIT_LIST_HEAD(r);
6322 - for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
6323 - inet_register_protosw(q);
6326 - * Set the ARP module up
6332 - * Set the IP module up
6337 - tcp_v4_init(&inet_family_ops);
6339 - /* Setup TCP slab cache for open requests. */
6342 - /* Add UDP-Lite (RFC 3828) */
6343 - udplite4_register();
6346 - * Set the ICMP layer up
6349 - icmp_init(&inet_family_ops);
6352 - * Initialise the multicast router
6354 -#if defined(CONFIG_IP_MROUTE)
6358 - * Initialise per-cpu ipv4 mibs
6361 - if (init_ipv4_mibs())
6362 - printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
6368 - dev_add_pack(&ip_packet_type);
6373 -out_unregister_udp_proto:
6374 - proto_unregister(&udp_prot);
6375 -out_unregister_tcp_proto:
6376 - proto_unregister(&tcp_prot);
6380 -fs_initcall(inet_init);
6382 -/* ------------------------------------------------------------------------ */
6384 -#ifdef CONFIG_PROC_FS
6385 -static int __init ipv4_proc_init(void)
6389 - if (raw_proc_init())
6391 - if (tcp4_proc_init())
6393 - if (udp4_proc_init())
6395 - if (fib_proc_init())
6397 - if (ip_misc_proc_init())
6414 -#else /* CONFIG_PROC_FS */
6415 -static int __init ipv4_proc_init(void)
6419 -#endif /* CONFIG_PROC_FS */
6421 -MODULE_ALIAS_NETPROTO(PF_INET);
6423 -EXPORT_SYMBOL(inet_accept);
6424 -EXPORT_SYMBOL(inet_bind);
6425 -EXPORT_SYMBOL(inet_dgram_connect);
6426 -EXPORT_SYMBOL(inet_dgram_ops);
6427 -EXPORT_SYMBOL(inet_getname);
6428 -EXPORT_SYMBOL(inet_ioctl);
6429 -EXPORT_SYMBOL(inet_listen);
6430 -EXPORT_SYMBOL(inet_register_protosw);
6431 -EXPORT_SYMBOL(inet_release);
6432 -EXPORT_SYMBOL(inet_sendmsg);
6433 -EXPORT_SYMBOL(inet_shutdown);
6434 -EXPORT_SYMBOL(inet_sock_destruct);
6435 -EXPORT_SYMBOL(inet_stream_connect);
6436 -EXPORT_SYMBOL(inet_stream_ops);
6437 -EXPORT_SYMBOL(inet_unregister_protosw);
6438 -EXPORT_SYMBOL(net_statistics);
6439 -EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
6440 diff -Nurb linux-2.6.22-594/net/netfilter/xt_MARK.c.orig linux-2.6.22-595/net/netfilter/xt_MARK.c.orig
6441 --- linux-2.6.22-594/net/netfilter/xt_MARK.c.orig 2008-03-20 01:27:52.000000000 -0400
6442 +++ linux-2.6.22-595/net/netfilter/xt_MARK.c.orig 1969-12-31 19:00:00.000000000 -0500
6444 -/* This is a module which is used for setting the NFMARK field of an skb. */
6446 -/* (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
6448 - * This program is free software; you can redistribute it and/or modify
6449 - * it under the terms of the GNU General Public License version 2 as
6450 - * published by the Free Software Foundation.
6454 -#include <linux/module.h>
6455 -#include <linux/version.h>
6456 -#include <linux/skbuff.h>
6457 -#include <linux/ip.h>
6458 -#include <net/checksum.h>
6459 -#include <net/route.h>
6460 -#include <net/inet_hashtables.h>
6462 -#include <net/netfilter/nf_conntrack.h>
6463 -#include <linux/netfilter/x_tables.h>
6464 -#include <linux/netfilter/xt_MARK.h>
6466 -MODULE_LICENSE("GPL");
6467 -MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
6468 -MODULE_DESCRIPTION("ip[6]tables MARK modification module");
6469 -MODULE_ALIAS("ipt_MARK");
6470 -MODULE_ALIAS("ip6t_MARK");
6472 -static inline u_int16_t
6473 -get_dst_port(struct nf_conntrack_tuple *tuple)
6475 - switch (tuple->dst.protonum) {
6477 - /* XXX Truncate 32-bit GRE key to 16 bits */
6478 -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
6479 - return tuple->dst.u.gre.key;
6481 - return htons(ntohl(tuple->dst.u.gre.key));
6483 - case IPPROTO_ICMP:
6484 - /* Bind on ICMP echo ID */
6485 - return tuple->src.u.icmp.id;
6487 - return tuple->dst.u.tcp.port;
6489 - return tuple->dst.u.udp.port;
6491 - return tuple->dst.u.all;
6495 -static inline u_int16_t
6496 -get_src_port(struct nf_conntrack_tuple *tuple)
6498 - switch (tuple->dst.protonum) {
6500 - /* XXX Truncate 32-bit GRE key to 16 bits */
6501 - return htons(ntohl(tuple->src.u.gre.key));
6502 - case IPPROTO_ICMP:
6503 - /* Bind on ICMP echo ID */
6504 - return tuple->src.u.icmp.id;
6506 - return tuple->src.u.tcp.port;
6508 - return tuple->src.u.udp.port;
6510 - return tuple->src.u.all;
6514 -static unsigned int
6515 -target_v0(struct sk_buff **pskb,
6516 - const struct net_device *in,
6517 - const struct net_device *out,
6518 - unsigned int hooknum,
6519 - const struct xt_target *target,
6520 - const void *targinfo)
6522 - const struct xt_mark_target_info *markinfo = targinfo;
6524 - (*pskb)->mark = markinfo->mark;
6525 - return XT_CONTINUE;
6528 -static unsigned int
6529 -target_v1(struct sk_buff **pskb,
6530 - const struct net_device *in,
6531 - const struct net_device *out,
6532 - unsigned int hooknum,
6533 - const struct xt_target *target,
6534 - const void *targinfo)
6536 - const struct xt_mark_target_info_v1 *markinfo = targinfo;
6539 - switch (markinfo->mode) {
6541 - mark = markinfo->mark;
6545 - mark = (*pskb)->mark & markinfo->mark;
6549 - mark = (*pskb)->mark | markinfo->mark;
6552 - case XT_MARK_COPYXID: {
6553 - enum ip_conntrack_info ctinfo;
6554 - struct sock *connection_sk=NULL;
6557 - struct nf_conn *ct = nf_ct_get((*pskb), &ctinfo);
6558 - extern struct inet_hashinfo tcp_hashinfo;
6559 - enum ip_conntrack_dir dir;
6563 - dir = CTINFO2DIR(ctinfo);
6564 - u_int32_t src_ip = ct->tuplehash[dir].tuple.src.u3.ip;
6565 - u_int16_t src_port = get_src_port(&ct->tuplehash[dir].tuple);
6566 - u_int16_t proto = ct->tuplehash[dir].tuple.dst.protonum;
6571 - dif = ((struct rtable *)(*pskb)->dst)->rt_iif;
6572 - ip = ct->tuplehash[dir].tuple.dst.u3.ip;
6573 - port = get_dst_port(&ct->tuplehash[dir].tuple);
6575 - if (proto == 1 || proto == 17) {
6576 - if (((*pskb)->mark!=-1) && (*pskb)->mark)
6577 - ct->xid[0]=(*pskb)->mark;
6579 - mark = ct->xid[0];
6582 - else if (proto == 6) {
6584 - connection_sk = (*pskb)->sk;
6586 - connection_sk = inet_lookup(&tcp_hashinfo, src_ip, src_port, ip, port, dif);
6589 - if (connection_sk) {
6590 - connection_sk->sk_peercred.gid = connection_sk->sk_peercred.uid = ct->xid[dir];
6591 - ct->xid[!dir]=connection_sk->sk_xid;
6592 - if (connection_sk->sk_xid != 0)
6593 - mark = connection_sk->sk_xid;
6594 - if (connection_sk != (*pskb)->sk)
6595 - sock_put(connection_sk);
6603 - (*pskb)->mark = mark;
6604 - return XT_CONTINUE;
6609 -checkentry_v0(const char *tablename,
6610 - const void *entry,
6611 - const struct xt_target *target,
6613 - unsigned int hook_mask)
6615 - struct xt_mark_target_info *markinfo = targinfo;
6617 - if (markinfo->mark > 0xffffffff) {
6618 - printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
6625 -checkentry_v1(const char *tablename,
6626 - const void *entry,
6627 - const struct xt_target *target,
6629 - unsigned int hook_mask)
6631 - struct xt_mark_target_info_v1 *markinfo = targinfo;
6633 - if (markinfo->mode != XT_MARK_SET
6634 - && markinfo->mode != XT_MARK_AND
6635 - && markinfo->mode != XT_MARK_OR
6636 - && markinfo->mode != XT_MARK_COPYXID) {
6637 - printk(KERN_WARNING "MARK: unknown mode %u\n",
6641 - if (markinfo->mark > 0xffffffff) {
6642 - printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
6648 -#ifdef CONFIG_COMPAT
6649 -struct compat_xt_mark_target_info_v1 {
6650 - compat_ulong_t mark;
6656 -static void compat_from_user_v1(void *dst, void *src)
6658 - struct compat_xt_mark_target_info_v1 *cm = src;
6659 - struct xt_mark_target_info_v1 m = {
6663 - memcpy(dst, &m, sizeof(m));
6666 -static int compat_to_user_v1(void __user *dst, void *src)
6668 - struct xt_mark_target_info_v1 *m = src;
6669 - struct compat_xt_mark_target_info_v1 cm = {
6673 - return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
6675 -#endif /* CONFIG_COMPAT */
6677 -static struct xt_target xt_mark_target[] = {
6680 - .family = AF_INET,
6682 - .checkentry = checkentry_v0,
6683 - .target = target_v0,
6684 - .targetsize = sizeof(struct xt_mark_target_info),
6685 - .table = "mangle",
6686 - .me = THIS_MODULE,
6690 - .family = AF_INET,
6692 - .checkentry = checkentry_v1,
6693 - .target = target_v1,
6694 - .targetsize = sizeof(struct xt_mark_target_info_v1),
6695 -#ifdef CONFIG_COMPAT
6696 - .compatsize = sizeof(struct compat_xt_mark_target_info_v1),
6697 - .compat_from_user = compat_from_user_v1,
6698 - .compat_to_user = compat_to_user_v1,
6700 - .table = "mangle",
6701 - .me = THIS_MODULE,
6705 - .family = AF_INET6,
6707 - .checkentry = checkentry_v0,
6708 - .target = target_v0,
6709 - .targetsize = sizeof(struct xt_mark_target_info),
6710 - .table = "mangle",
6711 - .me = THIS_MODULE,
6715 -static int __init xt_mark_init(void)
6717 - return xt_register_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
6720 -static void __exit xt_mark_fini(void)
6722 - xt_unregister_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
6725 -module_init(xt_mark_init);
6726 -module_exit(xt_mark_fini);
6727 diff -Nurb linux-2.6.22-594/net/packet/af_packet.c.orig linux-2.6.22-595/net/packet/af_packet.c.orig
6728 --- linux-2.6.22-594/net/packet/af_packet.c.orig 2008-03-20 01:27:52.000000000 -0400
6729 +++ linux-2.6.22-595/net/packet/af_packet.c.orig 1969-12-31 19:00:00.000000000 -0500
6732 - * INET An implementation of the TCP/IP protocol suite for the LINUX
6733 - * operating system. INET is implemented using the BSD Socket
6734 - * interface as the means of communication with the user level.
6736 - * PACKET - implements raw packet sockets.
6738 - * Version: $Id: af_packet.c,v 1.61 2002/02/08 03:57:19 davem Exp $
6740 - * Authors: Ross Biro
6741 - * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
6742 - * Alan Cox, <gw4pts@gw4pts.ampr.org>
6745 - * Alan Cox : verify_area() now used correctly
6746 - * Alan Cox : new skbuff lists, look ma no backlogs!
6747 - * Alan Cox : tidied skbuff lists.
6748 - * Alan Cox : Now uses generic datagram routines I
6749 - * added. Also fixed the peek/read crash
6750 - * from all old Linux datagram code.
6751 - * Alan Cox : Uses the improved datagram code.
6752 - * Alan Cox : Added NULL's for socket options.
6753 - * Alan Cox : Re-commented the code.
6754 - * Alan Cox : Use new kernel side addressing
6755 - * Rob Janssen : Correct MTU usage.
6756 - * Dave Platt : Counter leaks caused by incorrect
6757 - * interrupt locking and some slightly
6758 - * dubious gcc output. Can you read
6759 - * compiler: it said _VOLATILE_
6760 - * Richard Kooijman : Timestamp fixes.
6761 - * Alan Cox : New buffers. Use sk->mac.raw.
6762 - * Alan Cox : sendmsg/recvmsg support.
6763 - * Alan Cox : Protocol setting support
6764 - * Alexey Kuznetsov : Untied from IPv4 stack.
6765 - * Cyrus Durgin : Fixed kerneld for kmod.
6766 - * Michal Ostrowski : Module initialization cleanup.
6767 - * Ulises Alonso : Frame number limit removal and
6768 - * packet_set_ring memory leak.
6769 - * Eric Biederman : Allow for > 8 byte hardware addresses.
6770 - * The convention is that longer addresses
6771 - * will simply extend the hardware address
6772 - * byte arrays at the end of sockaddr_ll
6773 - * and packet_mreq.
6775 - * This program is free software; you can redistribute it and/or
6776 - * modify it under the terms of the GNU General Public License
6777 - * as published by the Free Software Foundation; either version
6778 - * 2 of the License, or (at your option) any later version.
6782 -#include <linux/types.h>
6783 -#include <linux/mm.h>
6784 -#include <linux/capability.h>
6785 -#include <linux/fcntl.h>
6786 -#include <linux/socket.h>
6787 -#include <linux/in.h>
6788 -#include <linux/inet.h>
6789 -#include <linux/netdevice.h>
6790 -#include <linux/if_packet.h>
6791 -#include <linux/wireless.h>
6792 -#include <linux/kernel.h>
6793 -#include <linux/kmod.h>
6794 -#include <net/ip.h>
6795 -#include <net/protocol.h>
6796 -#include <linux/skbuff.h>
6797 -#include <net/sock.h>
6798 -#include <linux/errno.h>
6799 -#include <linux/timer.h>
6800 -#include <asm/system.h>
6801 -#include <asm/uaccess.h>
6802 -#include <asm/ioctls.h>
6803 -#include <asm/page.h>
6804 -#include <asm/cacheflush.h>
6805 -#include <asm/io.h>
6806 -#include <linux/proc_fs.h>
6807 -#include <linux/seq_file.h>
6808 -#include <linux/poll.h>
6809 -#include <linux/module.h>
6810 -#include <linux/init.h>
6811 -#include <linux/vs_network.h>
6814 -#include <net/inet_common.h>
6819 - - if device has no dev->hard_header routine, it adds and removes ll header
6820 - inside itself. In this case ll header is invisible outside of device,
6821 - but higher levels still should reserve dev->hard_header_len.
6822 - Some devices are enough clever to reallocate skb, when header
6823 - will not fit to reserved space (tunnel), another ones are silly
6825 - - packet socket receives packets with pulled ll header,
6826 - so that SOCK_RAW should push it back.
6831 -Incoming, dev->hard_header!=NULL
6832 - mac_header -> ll header
6835 -Outgoing, dev->hard_header!=NULL
6836 - mac_header -> ll header
6839 -Incoming, dev->hard_header==NULL
6840 - mac_header -> UNKNOWN position. It is very likely, that it points to ll
6841 - header. PPP makes it, that is wrong, because introduce
6842 - assymetry between rx and tx paths.
6845 -Outgoing, dev->hard_header==NULL
6846 - mac_header -> data. ll header is still not built!
6850 - If dev->hard_header==NULL we are unlikely to restore sensible ll header.
6856 -dev->hard_header != NULL
6857 - mac_header -> ll header
6860 -dev->hard_header == NULL (ll header is added by device, we cannot control it)
6861 - mac_header -> data
6864 - We should set nh.raw on output to correct posistion,
6865 - packet classifier depends on it.
6868 -/* List of all packet sockets. */
6869 -static HLIST_HEAD(packet_sklist);
6870 -static DEFINE_RWLOCK(packet_sklist_lock);
6872 -static atomic_t packet_socks_nr;
6875 -/* Private packet socket structures. */
6877 -struct packet_mclist
6879 - struct packet_mclist *next;
6882 - unsigned short type;
6883 - unsigned short alen;
6884 - unsigned char addr[MAX_ADDR_LEN];
6886 -/* identical to struct packet_mreq except it has
6887 - * a longer address field.
6889 -struct packet_mreq_max
6892 - unsigned short mr_type;
6893 - unsigned short mr_alen;
6894 - unsigned char mr_address[MAX_ADDR_LEN];
6897 -#ifdef CONFIG_PACKET_MMAP
6898 -static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);
6901 -static void packet_flush_mclist(struct sock *sk);
6903 -struct packet_sock {
6904 - /* struct sock has to be the first member of packet_sock */
6906 - struct tpacket_stats stats;
6907 -#ifdef CONFIG_PACKET_MMAP
6909 - unsigned int head;
6910 - unsigned int frames_per_block;
6911 - unsigned int frame_size;
6912 - unsigned int frame_max;
6915 - struct packet_type prot_hook;
6916 - spinlock_t bind_lock;
6917 - unsigned int running:1, /* prot_hook is attached*/
6920 - int ifindex; /* bound device */
6922 - struct packet_mclist *mclist;
6923 -#ifdef CONFIG_PACKET_MMAP
6925 - unsigned int pg_vec_order;
6926 - unsigned int pg_vec_pages;
6927 - unsigned int pg_vec_len;
6931 -struct packet_skb_cb {
6932 - unsigned int origlen;
6934 - struct sockaddr_pkt pkt;
6935 - struct sockaddr_ll ll;
6939 -#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
6941 -#ifdef CONFIG_PACKET_MMAP
6943 -static inline struct tpacket_hdr *packet_lookup_frame(struct packet_sock *po, unsigned int position)
6945 - unsigned int pg_vec_pos, frame_offset;
6947 - pg_vec_pos = position / po->frames_per_block;
6948 - frame_offset = position % po->frames_per_block;
6950 - return (struct tpacket_hdr *)(po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size));
6954 -static inline struct packet_sock *pkt_sk(struct sock *sk)
6956 - return (struct packet_sock *)sk;
6959 -static void packet_sock_destruct(struct sock *sk)
6961 - BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
6962 - BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
6964 - if (!sock_flag(sk, SOCK_DEAD)) {
6965 - printk("Attempt to release alive packet socket: %p\n", sk);
6969 - atomic_dec(&packet_socks_nr);
6970 -#ifdef PACKET_REFCNT_DEBUG
6971 - printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));
6976 -static const struct proto_ops packet_ops;
6978 -static const struct proto_ops packet_ops_spkt;
6980 -static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
6983 - struct sockaddr_pkt *spkt;
6986 - * When we registered the protocol we saved the socket in the data
6987 - * field for just this event.
6990 - sk = pt->af_packet_priv;
6993 - * Yank back the headers [hope the device set this
6994 - * right or kerboom...]
6996 - * Incoming packets have ll header pulled,
6999 - * For outgoing ones skb->data == skb_mac_header(skb)
7000 - * so that this procedure is noop.
7003 - if (skb->pkt_type == PACKET_LOOPBACK)
7006 - if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
7009 - /* drop any routing info */
7010 - dst_release(skb->dst);
7013 - /* drop conntrack reference */
7016 - spkt = &PACKET_SKB_CB(skb)->sa.pkt;
7018 - skb_push(skb, skb->data - skb_mac_header(skb));
7021 - * The SOCK_PACKET socket receives _all_ frames.
7024 - spkt->spkt_family = dev->type;
7025 - strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
7026 - spkt->spkt_protocol = skb->protocol;
7029 - * Charge the memory to the socket. This is done specifically
7030 - * to prevent sockets using all the memory up.
7033 - if (sock_queue_rcv_skb(sk,skb) == 0)
7044 - * Output a raw packet to a device layer. This bypasses all the other
7045 - * protocol layers and you must therefore supply it with a complete frame
7048 -static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
7049 - struct msghdr *msg, size_t len)
7051 - struct sock *sk = sock->sk;
7052 - struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
7053 - struct sk_buff *skb;
7054 - struct net_device *dev;
7058 - if (!nx_capable(CAP_NET_RAW, NXC_RAW_SEND))
7062 - * Get and verify the address.
7067 - if (msg->msg_namelen < sizeof(struct sockaddr))
7069 - if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
7070 - proto=saddr->spkt_protocol;
7073 - return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
7076 - * Find the device first to size check it
7079 - saddr->spkt_device[13] = 0;
7080 - dev = dev_get_by_name(saddr->spkt_device);
7086 - if (!(dev->flags & IFF_UP))
7090 - * You may not queue a frame bigger than the mtu. This is the lowest level
7091 - * raw protocol and you must do your own fragmentation at this level.
7095 - if (len > dev->mtu + dev->hard_header_len)
7099 - skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
7102 - * If the write buffer is full, then tough. At this level the user gets to
7103 - * deal with the problem - do your own algorithmic backoffs. That's far
7114 - /* FIXME: Save some space for broken drivers that write a
7115 - * hard header at transmission time by themselves. PPP is the
7116 - * notable one here. This should really be fixed at the driver level.
7118 - skb_reserve(skb, LL_RESERVED_SPACE(dev));
7119 - skb_reset_network_header(skb);
7121 - /* Try to align data part correctly */
7122 - if (dev->hard_header) {
7123 - skb->data -= dev->hard_header_len;
7124 - skb->tail -= dev->hard_header_len;
7125 - if (len < dev->hard_header_len)
7126 - skb_reset_network_header(skb);
7129 - /* Returns -EFAULT on error */
7130 - err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
7131 - skb->protocol = proto;
7133 - skb->priority = sk->sk_priority;
7141 - dev_queue_xmit(skb);
7153 -static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
7156 - struct sk_filter *filter;
7157 - int tag = skb->skb_tag;
7159 - if (sk->sk_nx_info && !(tag == 1 || sk->sk_nid == tag))
7162 - rcu_read_lock_bh();
7163 - filter = rcu_dereference(sk->sk_filter);
7164 - if (filter != NULL)
7165 - res = sk_run_filter(skb, filter->insns, filter->len);
7166 - rcu_read_unlock_bh();
7172 - This function makes lazy skb cloning in hope that most of packets
7173 - are discarded by BPF.
7175 - Note tricky part: we DO mangle shared skb! skb->data, skb->len
7176 - and skb->cb are mangled. It works because (and until) packets
7177 - falling here are owned by current CPU. Output packets are cloned
7178 - by dev_queue_xmit_nit(), input packets are processed by net_bh
7179 - sequencially, so that if we return skb to original state on exit,
7180 - we will not harm anyone.
7183 -static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
7186 - struct sockaddr_ll *sll;
7187 - struct packet_sock *po;
7188 - u8 * skb_head = skb->data;
7189 - int skb_len = skb->len;
7190 - unsigned int snaplen, res;
7192 - if (skb->pkt_type == PACKET_LOOPBACK)
7195 - sk = pt->af_packet_priv;
7200 - if (dev->hard_header) {
7201 - /* The device has an explicit notion of ll header,
7202 - exported to higher levels.
7204 - Otherwise, the device hides datails of it frame
7205 - structure, so that corresponding packet head
7206 - never delivered to user.
7208 - if (sk->sk_type != SOCK_DGRAM)
7209 - skb_push(skb, skb->data - skb_mac_header(skb));
7210 - else if (skb->pkt_type == PACKET_OUTGOING) {
7211 - /* Special case: outgoing packets have ll header at head */
7212 - skb_pull(skb, skb_network_offset(skb));
7216 - snaplen = skb->len;
7218 - res = run_filter(skb, sk, snaplen);
7220 - goto drop_n_restore;
7221 - if (snaplen > res)
7224 - if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
7225 - (unsigned)sk->sk_rcvbuf)
7228 - if (skb_shared(skb)) {
7229 - struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
7233 - if (skb_head != skb->data) {
7234 - skb->data = skb_head;
7235 - skb->len = skb_len;
7241 - BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
7244 - sll = &PACKET_SKB_CB(skb)->sa.ll;
7245 - sll->sll_family = AF_PACKET;
7246 - sll->sll_hatype = dev->type;
7247 - sll->sll_protocol = skb->protocol;
7248 - sll->sll_pkttype = skb->pkt_type;
7249 - if (unlikely(po->origdev) && skb->pkt_type == PACKET_HOST)
7250 - sll->sll_ifindex = orig_dev->ifindex;
7252 - sll->sll_ifindex = dev->ifindex;
7253 - sll->sll_halen = 0;
7255 - if (dev->hard_header_parse)
7256 - sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
7258 - PACKET_SKB_CB(skb)->origlen = skb->len;
7260 - if (pskb_trim(skb, snaplen))
7263 - skb_set_owner_r(skb, sk);
7265 - dst_release(skb->dst);
7268 - /* drop conntrack reference */
7271 - spin_lock(&sk->sk_receive_queue.lock);
7272 - po->stats.tp_packets++;
7273 - __skb_queue_tail(&sk->sk_receive_queue, skb);
7274 - spin_unlock(&sk->sk_receive_queue.lock);
7275 - sk->sk_data_ready(sk, skb->len);
7279 - spin_lock(&sk->sk_receive_queue.lock);
7280 - po->stats.tp_drops++;
7281 - spin_unlock(&sk->sk_receive_queue.lock);
7284 - if (skb_head != skb->data && skb_shared(skb)) {
7285 - skb->data = skb_head;
7286 - skb->len = skb_len;
7293 -#ifdef CONFIG_PACKET_MMAP
7294 -static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
7297 - struct packet_sock *po;
7298 - struct sockaddr_ll *sll;
7299 - struct tpacket_hdr *h;
7300 - u8 * skb_head = skb->data;
7301 - int skb_len = skb->len;
7302 - unsigned int snaplen, res;
7303 - unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
7304 - unsigned short macoff, netoff;
7305 - struct sk_buff *copy_skb = NULL;
7306 - struct timeval tv;
7308 - if (skb->pkt_type == PACKET_LOOPBACK)
7311 - sk = pt->af_packet_priv;
7314 - if (dev->hard_header) {
7315 - if (sk->sk_type != SOCK_DGRAM)
7316 - skb_push(skb, skb->data - skb_mac_header(skb));
7317 - else if (skb->pkt_type == PACKET_OUTGOING) {
7318 - /* Special case: outgoing packets have ll header at head */
7319 - skb_pull(skb, skb_network_offset(skb));
7323 - if (skb->ip_summed == CHECKSUM_PARTIAL)
7324 - status |= TP_STATUS_CSUMNOTREADY;
7326 - snaplen = skb->len;
7328 - res = run_filter(skb, sk, snaplen);
7330 - goto drop_n_restore;
7331 - if (snaplen > res)
7334 - if (sk->sk_type == SOCK_DGRAM) {
7335 - macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
7337 - unsigned maclen = skb_network_offset(skb);
7338 - netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
7339 - macoff = netoff - maclen;
7342 - if (macoff + snaplen > po->frame_size) {
7343 - if (po->copy_thresh &&
7344 - atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
7345 - (unsigned)sk->sk_rcvbuf) {
7346 - if (skb_shared(skb)) {
7347 - copy_skb = skb_clone(skb, GFP_ATOMIC);
7349 - copy_skb = skb_get(skb);
7350 - skb_head = skb->data;
7353 - skb_set_owner_r(copy_skb, sk);
7355 - snaplen = po->frame_size - macoff;
7356 - if ((int)snaplen < 0)
7360 - spin_lock(&sk->sk_receive_queue.lock);
7361 - h = packet_lookup_frame(po, po->head);
7364 - goto ring_is_full;
7365 - po->head = po->head != po->frame_max ? po->head+1 : 0;
7366 - po->stats.tp_packets++;
7368 - status |= TP_STATUS_COPY;
7369 - __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
7371 - if (!po->stats.tp_drops)
7372 - status &= ~TP_STATUS_LOSING;
7373 - spin_unlock(&sk->sk_receive_queue.lock);
7375 - skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen);
7377 - h->tp_len = skb->len;
7378 - h->tp_snaplen = snaplen;
7379 - h->tp_mac = macoff;
7380 - h->tp_net = netoff;
7381 - if (skb->tstamp.tv64 == 0) {
7382 - __net_timestamp(skb);
7383 - sock_enable_timestamp(sk);
7385 - tv = ktime_to_timeval(skb->tstamp);
7386 - h->tp_sec = tv.tv_sec;
7387 - h->tp_usec = tv.tv_usec;
7389 - sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
7390 - sll->sll_halen = 0;
7391 - if (dev->hard_header_parse)
7392 - sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
7393 - sll->sll_family = AF_PACKET;
7394 - sll->sll_hatype = dev->type;
7395 - sll->sll_protocol = skb->protocol;
7396 - sll->sll_pkttype = skb->pkt_type;
7397 - if (unlikely(po->origdev) && skb->pkt_type == PACKET_HOST)
7398 - sll->sll_ifindex = orig_dev->ifindex;
7400 - sll->sll_ifindex = dev->ifindex;
7402 - h->tp_status = status;
7406 - struct page *p_start, *p_end;
7407 - u8 *h_end = (u8 *)h + macoff + snaplen - 1;
7409 - p_start = virt_to_page(h);
7410 - p_end = virt_to_page(h_end);
7411 - while (p_start <= p_end) {
7412 - flush_dcache_page(p_start);
7417 - sk->sk_data_ready(sk, 0);
7420 - if (skb_head != skb->data && skb_shared(skb)) {
7421 - skb->data = skb_head;
7422 - skb->len = skb_len;
7429 - po->stats.tp_drops++;
7430 - spin_unlock(&sk->sk_receive_queue.lock);
7432 - sk->sk_data_ready(sk, 0);
7434 - kfree_skb(copy_skb);
7435 - goto drop_n_restore;
7441 -static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
7442 - struct msghdr *msg, size_t len)
7444 - struct sock *sk = sock->sk;
7445 - struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
7446 - struct sk_buff *skb;
7447 - struct net_device *dev;
7449 - unsigned char *addr;
7450 - int ifindex, err, reserve = 0;
7452 - if (!nx_capable(CAP_NET_RAW, NXC_RAW_SEND))
7456 - * Get and verify the address.
7459 - if (saddr == NULL) {
7460 - struct packet_sock *po = pkt_sk(sk);
7462 - ifindex = po->ifindex;
7467 - if (msg->msg_namelen < sizeof(struct sockaddr_ll))
7469 - if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
7471 - ifindex = saddr->sll_ifindex;
7472 - proto = saddr->sll_protocol;
7473 - addr = saddr->sll_addr;
7477 - dev = dev_get_by_index(ifindex);
7481 - if (sock->type == SOCK_RAW)
7482 - reserve = dev->hard_header_len;
7485 - if (!(dev->flags & IFF_UP))
7489 - if (len > dev->mtu+reserve)
7492 - skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
7493 - msg->msg_flags & MSG_DONTWAIT, &err);
7497 - skb_reserve(skb, LL_RESERVED_SPACE(dev));
7498 - skb_reset_network_header(skb);
7500 - if (dev->hard_header) {
7503 - res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
7504 - if (sock->type != SOCK_DGRAM) {
7505 - skb_reset_tail_pointer(skb);
7507 - } else if (res < 0)
7511 - /* Returns -EFAULT on error */
7512 - err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
7516 - skb->protocol = proto;
7518 - skb->priority = sk->sk_priority;
7524 - err = dev_queue_xmit(skb);
7525 - if (err > 0 && (err = net_xmit_errno(err)) != 0)
7542 - * Close a PACKET socket. This is fairly simple. We immediately go
7543 - * to 'closed' state and remove our protocol entry in the device list.
7546 -static int packet_release(struct socket *sock)
7548 - struct sock *sk = sock->sk;
7549 - struct packet_sock *po;
7556 - write_lock_bh(&packet_sklist_lock);
7557 - sk_del_node_init(sk);
7558 - write_unlock_bh(&packet_sklist_lock);
7561 - * Unhook packet receive handler.
7564 - if (po->running) {
7566 - * Remove the protocol hook
7568 - dev_remove_pack(&po->prot_hook);
7574 - packet_flush_mclist(sk);
7576 -#ifdef CONFIG_PACKET_MMAP
7578 - struct tpacket_req req;
7579 - memset(&req, 0, sizeof(req));
7580 - packet_set_ring(sk, &req, 1);
7585 - * Now the socket is dead. No more input will appear.
7591 - /* Purge queues */
7593 - skb_queue_purge(&sk->sk_receive_queue);
7600 - * Attach a packet hook.
7603 -static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
7605 - struct packet_sock *po = pkt_sk(sk);
7607 - * Detach an existing hook if present.
7612 - spin_lock(&po->bind_lock);
7613 - if (po->running) {
7617 - spin_unlock(&po->bind_lock);
7618 - dev_remove_pack(&po->prot_hook);
7619 - spin_lock(&po->bind_lock);
7622 - po->num = protocol;
7623 - po->prot_hook.type = protocol;
7624 - po->prot_hook.dev = dev;
7626 - po->ifindex = dev ? dev->ifindex : 0;
7628 - if (protocol == 0)
7632 - if (dev->flags&IFF_UP) {
7633 - dev_add_pack(&po->prot_hook);
7637 - sk->sk_err = ENETDOWN;
7638 - if (!sock_flag(sk, SOCK_DEAD))
7639 - sk->sk_error_report(sk);
7642 - dev_add_pack(&po->prot_hook);
7648 - spin_unlock(&po->bind_lock);
7654 - * Bind a packet socket to a device
7657 -static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
7659 - struct sock *sk=sock->sk;
7661 - struct net_device *dev;
7662 - int err = -ENODEV;
7668 - if (addr_len != sizeof(struct sockaddr))
7670 - strlcpy(name,uaddr->sa_data,sizeof(name));
7672 - dev = dev_get_by_name(name);
7674 - err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
7680 -static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
7682 - struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
7683 - struct sock *sk=sock->sk;
7684 - struct net_device *dev = NULL;
7692 - if (addr_len < sizeof(struct sockaddr_ll))
7694 - if (sll->sll_family != AF_PACKET)
7697 - if (sll->sll_ifindex) {
7699 - dev = dev_get_by_index(sll->sll_ifindex);
7703 - err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
7711 -static struct proto packet_proto = {
7713 - .owner = THIS_MODULE,
7714 - .obj_size = sizeof(struct packet_sock),
7718 - * Create a packet of type SOCK_PACKET.
7721 -static int packet_create(struct socket *sock, int protocol)
7724 - struct packet_sock *po;
7725 - __be16 proto = (__force __be16)protocol; /* weird, but documented */
7728 - if (!nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET))
7730 - if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
7731 - sock->type != SOCK_PACKET)
7732 - return -ESOCKTNOSUPPORT;
7734 - sock->state = SS_UNCONNECTED;
7737 - sk = sk_alloc(PF_PACKET, GFP_KERNEL, &packet_proto, 1);
7741 - sock->ops = &packet_ops;
7742 - if (sock->type == SOCK_PACKET)
7743 - sock->ops = &packet_ops_spkt;
7745 - sock_init_data(sock, sk);
7748 - sk->sk_family = PF_PACKET;
7751 - sk->sk_destruct = packet_sock_destruct;
7752 - atomic_inc(&packet_socks_nr);
7755 - * Attach a protocol block
7758 - spin_lock_init(&po->bind_lock);
7759 - po->prot_hook.func = packet_rcv;
7761 - if (sock->type == SOCK_PACKET)
7762 - po->prot_hook.func = packet_rcv_spkt;
7764 - po->prot_hook.af_packet_priv = sk;
7767 - po->prot_hook.type = proto;
7768 - dev_add_pack(&po->prot_hook);
7773 - write_lock_bh(&packet_sklist_lock);
7774 - sk_add_node(sk, &packet_sklist);
7775 - write_unlock_bh(&packet_sklist_lock);
7782 - * Pull a packet from our receive queue and hand it to the user.
7783 - * If necessary we block.
7786 -static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
7787 - struct msghdr *msg, size_t len, int flags)
7789 - struct sock *sk = sock->sk;
7790 - struct sk_buff *skb;
7792 - struct sockaddr_ll *sll;
7795 - if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
7799 - /* What error should we return now? EUNATTACH? */
7800 - if (pkt_sk(sk)->ifindex < 0)
7805 - * Call the generic datagram receiver. This handles all sorts
7806 - * of horrible races and re-entrancy so we can forget about it
7807 - * in the protocol layers.
7809 - * Now it will return ENETDOWN, if device have just gone down,
7810 - * but then it will block.
7813 - skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
7816 - * An error occurred so return it. Because skb_recv_datagram()
7817 - * handles the blocking we don't see and worry about blocking
7825 - * If the address length field is there to be filled in, we fill
7829 - sll = &PACKET_SKB_CB(skb)->sa.ll;
7830 - if (sock->type == SOCK_PACKET)
7831 - msg->msg_namelen = sizeof(struct sockaddr_pkt);
7833 - msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
7836 - * You lose any data beyond the buffer you gave. If it worries a
7837 - * user program they can ask the device for its MTU anyway.
7840 - copied = skb->len;
7844 - msg->msg_flags|=MSG_TRUNC;
7847 - err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
7851 - sock_recv_timestamp(msg, sk, skb);
7853 - if (msg->msg_name)
7854 - memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
7855 - msg->msg_namelen);
7857 - if (pkt_sk(sk)->auxdata) {
7858 - struct tpacket_auxdata aux;
7860 - aux.tp_status = TP_STATUS_USER;
7861 - if (skb->ip_summed == CHECKSUM_PARTIAL)
7862 - aux.tp_status |= TP_STATUS_CSUMNOTREADY;
7863 - aux.tp_len = PACKET_SKB_CB(skb)->origlen;
7864 - aux.tp_snaplen = skb->len;
7866 - aux.tp_net = skb_network_offset(skb);
7868 - put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
7872 - * Free or return the buffer as appropriate. Again this
7873 - * hides all the races and re-entrancy issues from us.
7875 - err = (flags&MSG_TRUNC) ? skb->len : copied;
7878 - skb_free_datagram(sk, skb);
7883 -static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
7884 - int *uaddr_len, int peer)
7886 - struct net_device *dev;
7887 - struct sock *sk = sock->sk;
7890 - return -EOPNOTSUPP;
7892 - uaddr->sa_family = AF_PACKET;
7893 - dev = dev_get_by_index(pkt_sk(sk)->ifindex);
7895 - strlcpy(uaddr->sa_data, dev->name, 15);
7898 - memset(uaddr->sa_data, 0, 14);
7899 - *uaddr_len = sizeof(*uaddr);
7904 -static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
7905 - int *uaddr_len, int peer)
7907 - struct net_device *dev;
7908 - struct sock *sk = sock->sk;
7909 - struct packet_sock *po = pkt_sk(sk);
7910 - struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
7913 - return -EOPNOTSUPP;
7915 - sll->sll_family = AF_PACKET;
7916 - sll->sll_ifindex = po->ifindex;
7917 - sll->sll_protocol = po->num;
7918 - dev = dev_get_by_index(po->ifindex);
7920 - sll->sll_hatype = dev->type;
7921 - sll->sll_halen = dev->addr_len;
7922 - memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
7925 - sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
7926 - sll->sll_halen = 0;
7928 - *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
7933 -static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
7935 - switch (i->type) {
7936 - case PACKET_MR_MULTICAST:
7938 - dev_mc_add(dev, i->addr, i->alen, 0);
7940 - dev_mc_delete(dev, i->addr, i->alen, 0);
7942 - case PACKET_MR_PROMISC:
7943 - dev_set_promiscuity(dev, what);
7945 - case PACKET_MR_ALLMULTI:
7946 - dev_set_allmulti(dev, what);
7952 -static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
7954 - for ( ; i; i=i->next) {
7955 - if (i->ifindex == dev->ifindex)
7956 - packet_dev_mc(dev, i, what);
7960 -static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
7962 - struct packet_sock *po = pkt_sk(sk);
7963 - struct packet_mclist *ml, *i;
7964 - struct net_device *dev;
7970 - dev = __dev_get_by_index(mreq->mr_ifindex);
7975 - if (mreq->mr_alen > dev->addr_len)
7979 - i = kmalloc(sizeof(*i), GFP_KERNEL);
7984 - for (ml = po->mclist; ml; ml = ml->next) {
7985 - if (ml->ifindex == mreq->mr_ifindex &&
7986 - ml->type == mreq->mr_type &&
7987 - ml->alen == mreq->mr_alen &&
7988 - memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
7990 - /* Free the new element ... */
7996 - i->type = mreq->mr_type;
7997 - i->ifindex = mreq->mr_ifindex;
7998 - i->alen = mreq->mr_alen;
7999 - memcpy(i->addr, mreq->mr_address, i->alen);
8001 - i->next = po->mclist;
8003 - packet_dev_mc(dev, i, +1);
8010 -static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
8012 - struct packet_mclist *ml, **mlp;
8016 - for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
8017 - if (ml->ifindex == mreq->mr_ifindex &&
8018 - ml->type == mreq->mr_type &&
8019 - ml->alen == mreq->mr_alen &&
8020 - memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
8021 - if (--ml->count == 0) {
8022 - struct net_device *dev;
8024 - dev = dev_get_by_index(ml->ifindex);
8026 - packet_dev_mc(dev, ml, -1);
8036 - return -EADDRNOTAVAIL;
8039 -static void packet_flush_mclist(struct sock *sk)
8041 - struct packet_sock *po = pkt_sk(sk);
8042 - struct packet_mclist *ml;
8048 - while ((ml = po->mclist) != NULL) {
8049 - struct net_device *dev;
8051 - po->mclist = ml->next;
8052 - if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
8053 - packet_dev_mc(dev, ml, -1);
8062 -packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
8064 - struct sock *sk = sock->sk;
8065 - struct packet_sock *po = pkt_sk(sk);
8068 - if (level != SOL_PACKET)
8069 - return -ENOPROTOOPT;
8072 - case PACKET_ADD_MEMBERSHIP:
8073 - case PACKET_DROP_MEMBERSHIP:
8075 - struct packet_mreq_max mreq;
8077 - memset(&mreq, 0, sizeof(mreq));
8078 - if (len < sizeof(struct packet_mreq))
8080 - if (len > sizeof(mreq))
8081 - len = sizeof(mreq);
8082 - if (copy_from_user(&mreq,optval,len))
8084 - if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
8086 - if (optname == PACKET_ADD_MEMBERSHIP)
8087 - ret = packet_mc_add(sk, &mreq);
8089 - ret = packet_mc_drop(sk, &mreq);
8093 -#ifdef CONFIG_PACKET_MMAP
8094 - case PACKET_RX_RING:
8096 - struct tpacket_req req;
8098 - if (optlen<sizeof(req))
8100 - if (copy_from_user(&req,optval,sizeof(req)))
8102 - return packet_set_ring(sk, &req, 0);
8104 - case PACKET_COPY_THRESH:
8108 - if (optlen!=sizeof(val))
8110 - if (copy_from_user(&val,optval,sizeof(val)))
8113 - pkt_sk(sk)->copy_thresh = val;
8117 - case PACKET_AUXDATA:
8121 - if (optlen < sizeof(val))
8123 - if (copy_from_user(&val, optval, sizeof(val)))
8126 - po->auxdata = !!val;
8129 - case PACKET_ORIGDEV:
8133 - if (optlen < sizeof(val))
8135 - if (copy_from_user(&val, optval, sizeof(val)))
8138 - po->origdev = !!val;
8142 - return -ENOPROTOOPT;
8146 -static int packet_getsockopt(struct socket *sock, int level, int optname,
8147 - char __user *optval, int __user *optlen)
8151 - struct sock *sk = sock->sk;
8152 - struct packet_sock *po = pkt_sk(sk);
8154 - struct tpacket_stats st;
8156 - if (level != SOL_PACKET)
8157 - return -ENOPROTOOPT;
8159 - if (get_user(len, optlen))
8166 - case PACKET_STATISTICS:
8167 - if (len > sizeof(struct tpacket_stats))
8168 - len = sizeof(struct tpacket_stats);
8169 - spin_lock_bh(&sk->sk_receive_queue.lock);
8171 - memset(&po->stats, 0, sizeof(st));
8172 - spin_unlock_bh(&sk->sk_receive_queue.lock);
8173 - st.tp_packets += st.tp_drops;
8177 - case PACKET_AUXDATA:
8178 - if (len > sizeof(int))
8179 - len = sizeof(int);
8180 - val = po->auxdata;
8184 - case PACKET_ORIGDEV:
8185 - if (len > sizeof(int))
8186 - len = sizeof(int);
8187 - val = po->origdev;
8192 - return -ENOPROTOOPT;
8195 - if (put_user(len, optlen))
8197 - if (copy_to_user(optval, data, len))
8203 -static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
8206 - struct hlist_node *node;
8207 - struct net_device *dev = data;
8209 - read_lock(&packet_sklist_lock);
8210 - sk_for_each(sk, node, &packet_sklist) {
8211 - struct packet_sock *po = pkt_sk(sk);
8214 - case NETDEV_UNREGISTER:
8216 - packet_dev_mclist(dev, po->mclist, -1);
8220 - if (dev->ifindex == po->ifindex) {
8221 - spin_lock(&po->bind_lock);
8222 - if (po->running) {
8223 - __dev_remove_pack(&po->prot_hook);
8226 - sk->sk_err = ENETDOWN;
8227 - if (!sock_flag(sk, SOCK_DEAD))
8228 - sk->sk_error_report(sk);
8230 - if (msg == NETDEV_UNREGISTER) {
8232 - po->prot_hook.dev = NULL;
8234 - spin_unlock(&po->bind_lock);
8238 - spin_lock(&po->bind_lock);
8239 - if (dev->ifindex == po->ifindex && po->num &&
8241 - dev_add_pack(&po->prot_hook);
8245 - spin_unlock(&po->bind_lock);
8249 - read_unlock(&packet_sklist_lock);
8250 - return NOTIFY_DONE;
8254 -static int packet_ioctl(struct socket *sock, unsigned int cmd,
8255 - unsigned long arg)
8257 - struct sock *sk = sock->sk;
8262 - int amount = atomic_read(&sk->sk_wmem_alloc);
8263 - return put_user(amount, (int __user *)arg);
8267 - struct sk_buff *skb;
8270 - spin_lock_bh(&sk->sk_receive_queue.lock);
8271 - skb = skb_peek(&sk->sk_receive_queue);
8273 - amount = skb->len;
8274 - spin_unlock_bh(&sk->sk_receive_queue.lock);
8275 - return put_user(amount, (int __user *)arg);
8278 - return sock_get_timestamp(sk, (struct timeval __user *)arg);
8279 - case SIOCGSTAMPNS:
8280 - return sock_get_timestampns(sk, (struct timespec __user *)arg);
8290 - case SIOCGIFBRDADDR:
8291 - case SIOCSIFBRDADDR:
8292 - case SIOCGIFNETMASK:
8293 - case SIOCSIFNETMASK:
8294 - case SIOCGIFDSTADDR:
8295 - case SIOCSIFDSTADDR:
8296 - case SIOCSIFFLAGS:
8297 - return inet_dgram_ops.ioctl(sock, cmd, arg);
8301 - return -ENOIOCTLCMD;
8306 -#ifndef CONFIG_PACKET_MMAP
8307 -#define packet_mmap sock_no_mmap
8308 -#define packet_poll datagram_poll
8311 -static unsigned int packet_poll(struct file * file, struct socket *sock,
8314 - struct sock *sk = sock->sk;
8315 - struct packet_sock *po = pkt_sk(sk);
8316 - unsigned int mask = datagram_poll(file, sock, wait);
8318 - spin_lock_bh(&sk->sk_receive_queue.lock);
8320 - unsigned last = po->head ? po->head-1 : po->frame_max;
8321 - struct tpacket_hdr *h;
8323 - h = packet_lookup_frame(po, last);
8326 - mask |= POLLIN | POLLRDNORM;
8328 - spin_unlock_bh(&sk->sk_receive_queue.lock);
8333 -/* Dirty? Well, I still did not learn better way to account
8337 -static void packet_mm_open(struct vm_area_struct *vma)
8339 - struct file *file = vma->vm_file;
8340 - struct socket * sock = file->private_data;
8341 - struct sock *sk = sock->sk;
8344 - atomic_inc(&pkt_sk(sk)->mapped);
8347 -static void packet_mm_close(struct vm_area_struct *vma)
8349 - struct file *file = vma->vm_file;
8350 - struct socket * sock = file->private_data;
8351 - struct sock *sk = sock->sk;
8354 - atomic_dec(&pkt_sk(sk)->mapped);
8357 -static struct vm_operations_struct packet_mmap_ops = {
8358 - .open = packet_mm_open,
8359 - .close =packet_mm_close,
8362 -static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order)
8364 - return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1);
8367 -static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len)
8371 - for (i = 0; i < len; i++) {
8372 - if (likely(pg_vec[i]))
8373 - free_pages((unsigned long) pg_vec[i], order);
8378 -static inline char *alloc_one_pg_vec_page(unsigned long order)
8380 - return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
8384 -static char **alloc_pg_vec(struct tpacket_req *req, int order)
8386 - unsigned int block_nr = req->tp_block_nr;
8390 - pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL);
8391 - if (unlikely(!pg_vec))
8394 - for (i = 0; i < block_nr; i++) {
8395 - pg_vec[i] = alloc_one_pg_vec_page(order);
8396 - if (unlikely(!pg_vec[i]))
8397 - goto out_free_pgvec;
8404 - free_pg_vec(pg_vec, order, block_nr);
8409 -static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
8411 - char **pg_vec = NULL;
8412 - struct packet_sock *po = pkt_sk(sk);
8413 - int was_running, order = 0;
8417 - if (req->tp_block_nr) {
8420 - /* Sanity tests and some calculations */
8422 - if (unlikely(po->pg_vec))
8425 - if (unlikely((int)req->tp_block_size <= 0))
8427 - if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
8429 - if (unlikely(req->tp_frame_size < TPACKET_HDRLEN))
8431 - if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
8434 - po->frames_per_block = req->tp_block_size/req->tp_frame_size;
8435 - if (unlikely(po->frames_per_block <= 0))
8437 - if (unlikely((po->frames_per_block * req->tp_block_nr) !=
8438 - req->tp_frame_nr))
8442 - order = get_order(req->tp_block_size);
8443 - pg_vec = alloc_pg_vec(req, order);
8444 - if (unlikely(!pg_vec))
8448 - for (i = 0; i < req->tp_block_nr; i++) {
8449 - char *ptr = pg_vec[i];
8450 - struct tpacket_hdr *header;
8453 - for (k = 0; k < po->frames_per_block; k++) {
8454 - header = (struct tpacket_hdr *) ptr;
8455 - header->tp_status = TP_STATUS_KERNEL;
8456 - ptr += req->tp_frame_size;
8461 - if (unlikely(req->tp_frame_nr))
8467 - /* Detach socket from network */
8468 - spin_lock(&po->bind_lock);
8469 - was_running = po->running;
8471 - if (was_running) {
8472 - __dev_remove_pack(&po->prot_hook);
8477 - spin_unlock(&po->bind_lock);
8479 - synchronize_net();
8482 - if (closing || atomic_read(&po->mapped) == 0) {
8484 -#define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
8486 - spin_lock_bh(&sk->sk_receive_queue.lock);
8487 - pg_vec = XC(po->pg_vec, pg_vec);
8488 - po->frame_max = (req->tp_frame_nr - 1);
8490 - po->frame_size = req->tp_frame_size;
8491 - spin_unlock_bh(&sk->sk_receive_queue.lock);
8493 - order = XC(po->pg_vec_order, order);
8494 - req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
8496 - po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
8497 - po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
8498 - skb_queue_purge(&sk->sk_receive_queue);
8500 - if (atomic_read(&po->mapped))
8501 - printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
8504 - spin_lock(&po->bind_lock);
8505 - if (was_running && !po->running) {
8509 - dev_add_pack(&po->prot_hook);
8511 - spin_unlock(&po->bind_lock);
8516 - free_pg_vec(pg_vec, order, req->tp_block_nr);
8521 -static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
8523 - struct sock *sk = sock->sk;
8524 - struct packet_sock *po = pkt_sk(sk);
8525 - unsigned long size;
8526 - unsigned long start;
8527 - int err = -EINVAL;
8530 - if (vma->vm_pgoff)
8533 - size = vma->vm_end - vma->vm_start;
8536 - if (po->pg_vec == NULL)
8538 - if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
8541 - start = vma->vm_start;
8542 - for (i = 0; i < po->pg_vec_len; i++) {
8543 - struct page *page = virt_to_page(po->pg_vec[i]);
8546 - for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) {
8547 - err = vm_insert_page(vma, start, page);
8548 - if (unlikely(err))
8550 - start += PAGE_SIZE;
8553 - atomic_inc(&po->mapped);
8554 - vma->vm_ops = &packet_mmap_ops;
8564 -static const struct proto_ops packet_ops_spkt = {
8565 - .family = PF_PACKET,
8566 - .owner = THIS_MODULE,
8567 - .release = packet_release,
8568 - .bind = packet_bind_spkt,
8569 - .connect = sock_no_connect,
8570 - .socketpair = sock_no_socketpair,
8571 - .accept = sock_no_accept,
8572 - .getname = packet_getname_spkt,
8573 - .poll = datagram_poll,
8574 - .ioctl = packet_ioctl,
8575 - .listen = sock_no_listen,
8576 - .shutdown = sock_no_shutdown,
8577 - .setsockopt = sock_no_setsockopt,
8578 - .getsockopt = sock_no_getsockopt,
8579 - .sendmsg = packet_sendmsg_spkt,
8580 - .recvmsg = packet_recvmsg,
8581 - .mmap = sock_no_mmap,
8582 - .sendpage = sock_no_sendpage,
8585 -static const struct proto_ops packet_ops = {
8586 - .family = PF_PACKET,
8587 - .owner = THIS_MODULE,
8588 - .release = packet_release,
8589 - .bind = packet_bind,
8590 - .connect = sock_no_connect,
8591 - .socketpair = sock_no_socketpair,
8592 - .accept = sock_no_accept,
8593 - .getname = packet_getname,
8594 - .poll = packet_poll,
8595 - .ioctl = packet_ioctl,
8596 - .listen = sock_no_listen,
8597 - .shutdown = sock_no_shutdown,
8598 - .setsockopt = packet_setsockopt,
8599 - .getsockopt = packet_getsockopt,
8600 - .sendmsg = packet_sendmsg,
8601 - .recvmsg = packet_recvmsg,
8602 - .mmap = packet_mmap,
8603 - .sendpage = sock_no_sendpage,
8606 -static struct net_proto_family packet_family_ops = {
8607 - .family = PF_PACKET,
8608 - .create = packet_create,
8609 - .owner = THIS_MODULE,
8612 -static struct notifier_block packet_netdev_notifier = {
8613 - .notifier_call =packet_notifier,
8616 -#ifdef CONFIG_PROC_FS
8617 -static inline struct sock *packet_seq_idx(loff_t off)
8620 - struct hlist_node *node;
8622 - sk_for_each(s, node, &packet_sklist) {
8629 -static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
8631 - read_lock(&packet_sklist_lock);
8632 - return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN;
8635 -static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8638 - return (v == SEQ_START_TOKEN)
8639 - ? sk_head(&packet_sklist)
8640 - : sk_next((struct sock*)v) ;
8643 -static void packet_seq_stop(struct seq_file *seq, void *v)
8645 - read_unlock(&packet_sklist_lock);
8648 -static int packet_seq_show(struct seq_file *seq, void *v)
8650 - if (v == SEQ_START_TOKEN)
8651 - seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
8653 - struct sock *s = v;
8654 - const struct packet_sock *po = pkt_sk(s);
8657 - "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
8659 - atomic_read(&s->sk_refcnt),
8664 - atomic_read(&s->sk_rmem_alloc),
8672 -static struct seq_operations packet_seq_ops = {
8673 - .start = packet_seq_start,
8674 - .next = packet_seq_next,
8675 - .stop = packet_seq_stop,
8676 - .show = packet_seq_show,
8679 -static int packet_seq_open(struct inode *inode, struct file *file)
8681 - return seq_open(file, &packet_seq_ops);
8684 -static const struct file_operations packet_seq_fops = {
8685 - .owner = THIS_MODULE,
8686 - .open = packet_seq_open,
8688 - .llseek = seq_lseek,
8689 - .release = seq_release,
8694 -static void __exit packet_exit(void)
8696 - proc_net_remove("packet");
8697 - unregister_netdevice_notifier(&packet_netdev_notifier);
8698 - sock_unregister(PF_PACKET);
8699 - proto_unregister(&packet_proto);
8702 -static int __init packet_init(void)
8704 - int rc = proto_register(&packet_proto, 0);
8709 - sock_register(&packet_family_ops);
8710 - register_netdevice_notifier(&packet_netdev_notifier);
8711 - proc_net_fops_create("packet", 0, &packet_seq_fops);
8716 -module_init(packet_init);
8717 -module_exit(packet_exit);
8718 -MODULE_LICENSE("GPL");
8719 -MODULE_ALIAS_NETPROTO(PF_PACKET);
8720 diff -Nurb linux-2.6.22-594/net/socket.c linux-2.6.22-595/net/socket.c
8721 --- linux-2.6.22-594/net/socket.c 2008-03-20 01:27:52.000000000 -0400
8722 +++ linux-2.6.22-595/net/socket.c 2008-03-20 01:28:00.000000000 -0400
8723 @@ -1122,12 +1122,17 @@
8724 if (type < 0 || type >= SOCK_MAX)
8728 + * Hack no. 2 - Sapan
8729 + * Clean this up later
8731 if (!nx_check(0, VS_ADMIN)) {
8732 if (family == PF_INET && !current_nx_info_has_v4())
8733 return -EAFNOSUPPORT;
8734 if (family == PF_INET6 && !current_nx_info_has_v6())
8735 return -EAFNOSUPPORT;