2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* INET An implementation of the TCP/IP protocol suite for the LINUX
|
|
|
|
* operating system. INET is implemented using the BSD Socket
|
|
|
|
* interface as the means of communication with the user level.
|
|
|
|
*
|
|
|
|
* IPv4 FIB: lookup engine and maintenance routines.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <asm/system.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/inet.h>
|
2005-12-27 05:43:12 +01:00
|
|
|
#include <linux/inetdevice.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/init.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 09:04:11 +01:00
|
|
|
#include <linux/slab.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-09-12 12:01:34 +02:00
|
|
|
#include <net/net_namespace.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <net/ip.h>
|
|
|
|
#include <net/protocol.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/tcp.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/ip_fib.h>
|
|
|
|
|
|
|
|
#include "fib_lookup.h"
|
|
|
|
|
2006-12-07 05:33:20 +01:00
|
|
|
static struct kmem_cache *fn_hash_kmem __read_mostly;
|
|
|
|
static struct kmem_cache *fn_alias_kmem __read_mostly;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
struct fib_node {
|
|
|
|
struct hlist_node fn_hash;
|
|
|
|
struct list_head fn_alias;
|
2006-09-27 07:20:01 +02:00
|
|
|
__be32 fn_key;
|
2008-01-18 12:33:26 +01:00
|
|
|
struct fib_alias fn_embedded_alias;
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
|
2010-10-14 22:53:04 +02:00
|
|
|
#define EMBEDDED_HASH_SIZE (L1_CACHE_BYTES / sizeof(struct hlist_head))
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fn_zone {
|
2010-10-14 22:53:34 +02:00
|
|
|
struct fn_zone __rcu *fz_next; /* Next not empty zone */
|
2010-10-14 22:56:39 +02:00
|
|
|
struct hlist_head __rcu *fz_hash; /* Hash table pointer */
|
|
|
|
seqlock_t fz_lock;
|
2005-04-17 00:20:36 +02:00
|
|
|
u32 fz_hashmask; /* (fz_divisor - 1) */
|
|
|
|
|
2010-10-14 22:53:04 +02:00
|
|
|
u8 fz_order; /* Zone order (0..32) */
|
|
|
|
u8 fz_revorder; /* 32 - fz_order */
|
|
|
|
__be32 fz_mask; /* inet_make_mask(order) */
|
2005-04-17 00:20:36 +02:00
|
|
|
#define FZ_MASK(fz) ((fz)->fz_mask)
|
|
|
|
|
2010-10-14 22:53:04 +02:00
|
|
|
struct hlist_head fz_embedded_hash[EMBEDDED_HASH_SIZE];
|
|
|
|
|
|
|
|
int fz_nent; /* Number of entries */
|
|
|
|
int fz_divisor; /* Hash size (mask+1) */
|
|
|
|
};
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
struct fn_hash {
|
2010-10-14 22:53:34 +02:00
|
|
|
struct fn_zone *fn_zones[33];
|
|
|
|
struct fn_zone __rcu *fn_zone_list;
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
|
2006-09-27 07:20:01 +02:00
|
|
|
static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2010-10-14 22:53:04 +02:00
|
|
|
u32 h = ntohl(key) >> fz->fz_revorder;
|
2005-04-17 00:20:36 +02:00
|
|
|
h ^= (h>>20);
|
|
|
|
h ^= (h>>10);
|
|
|
|
h ^= (h>>5);
|
2010-10-14 22:53:04 +02:00
|
|
|
h &= fz->fz_hashmask;
|
2005-04-17 00:20:36 +02:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2006-09-27 07:20:01 +02:00
|
|
|
static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
return dst & FZ_MASK(fz);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int fib_hash_genid;
|
|
|
|
|
|
|
|
#define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
|
|
|
|
|
|
|
|
static struct hlist_head *fz_hash_alloc(int divisor)
|
|
|
|
{
|
|
|
|
unsigned long size = divisor * sizeof(struct hlist_head);
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
if (size <= PAGE_SIZE)
|
2007-11-26 16:31:24 +01:00
|
|
|
return kzalloc(size, GFP_KERNEL);
|
2010-10-14 22:56:39 +02:00
|
|
|
|
|
|
|
return (struct hlist_head *)
|
|
|
|
__get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(size));
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The fib hash lock must be held when this is called. */
|
|
|
|
static inline void fn_rebuild_zone(struct fn_zone *fz,
|
|
|
|
struct hlist_head *old_ht,
|
|
|
|
int old_divisor)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < old_divisor; i++) {
|
|
|
|
struct hlist_node *node, *n;
|
|
|
|
struct fib_node *f;
|
|
|
|
|
|
|
|
hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
|
2010-10-26 05:24:16 +02:00
|
|
|
struct hlist_head *new_head;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_del_rcu(&f->fn_hash);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-26 05:24:16 +02:00
|
|
|
new_head = rcu_dereference_protected(fz->fz_hash, 1) +
|
|
|
|
fn_hash(f->fn_key, fz);
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_add_head_rcu(&f->fn_hash, new_head);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fz_hash_free(struct hlist_head *hash, int divisor)
|
|
|
|
{
|
|
|
|
unsigned long size = divisor * sizeof(struct hlist_head);
|
|
|
|
|
|
|
|
if (size <= PAGE_SIZE)
|
|
|
|
kfree(hash);
|
|
|
|
else
|
|
|
|
free_pages((unsigned long)hash, get_order(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fn_rehash_zone(struct fn_zone *fz)
|
|
|
|
{
|
|
|
|
struct hlist_head *ht, *old_ht;
|
|
|
|
int old_divisor, new_divisor;
|
|
|
|
u32 new_hashmask;
|
2007-02-09 15:24:47 +01:00
|
|
|
|
2010-10-14 22:53:04 +02:00
|
|
|
new_divisor = old_divisor = fz->fz_divisor;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
switch (old_divisor) {
|
2010-10-14 22:53:04 +02:00
|
|
|
case EMBEDDED_HASH_SIZE:
|
|
|
|
new_divisor *= EMBEDDED_HASH_SIZE;
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
2010-10-14 22:53:04 +02:00
|
|
|
case EMBEDDED_HASH_SIZE*EMBEDDED_HASH_SIZE:
|
|
|
|
new_divisor *= (EMBEDDED_HASH_SIZE/2);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
|
|
|
|
printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new_divisor = (old_divisor << 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_hashmask = (new_divisor - 1);
|
|
|
|
|
|
|
|
#if RT_CACHE_DEBUG >= 2
|
2008-01-13 05:58:35 +01:00
|
|
|
printk(KERN_DEBUG "fn_rehash_zone: hash for zone %d grows from %d\n",
|
|
|
|
fz->fz_order, old_divisor);
|
2005-04-17 00:20:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ht = fz_hash_alloc(new_divisor);
|
|
|
|
|
|
|
|
if (ht) {
|
2010-10-14 22:56:39 +02:00
|
|
|
struct fn_zone nfz;
|
|
|
|
|
|
|
|
memcpy(&nfz, fz, sizeof(nfz));
|
|
|
|
|
|
|
|
write_seqlock_bh(&fz->fz_lock);
|
2010-10-26 05:24:16 +02:00
|
|
|
old_ht = rcu_dereference_protected(fz->fz_hash, 1);
|
|
|
|
RCU_INIT_POINTER(nfz.fz_hash, ht);
|
2010-10-14 22:56:39 +02:00
|
|
|
nfz.fz_hashmask = new_hashmask;
|
|
|
|
nfz.fz_divisor = new_divisor;
|
|
|
|
fn_rebuild_zone(&nfz, old_ht, old_divisor);
|
|
|
|
fib_hash_genid++;
|
|
|
|
rcu_assign_pointer(fz->fz_hash, ht);
|
2005-04-17 00:20:36 +02:00
|
|
|
fz->fz_hashmask = new_hashmask;
|
|
|
|
fz->fz_divisor = new_divisor;
|
2010-10-14 22:56:39 +02:00
|
|
|
write_sequnlock_bh(&fz->fz_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
if (old_ht != fz->fz_embedded_hash) {
|
|
|
|
synchronize_rcu();
|
2010-10-14 22:53:04 +02:00
|
|
|
fz_hash_free(old_ht, old_divisor);
|
2010-10-14 22:56:39 +02:00
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
static void fn_free_node_rcu(struct rcu_head *head)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2010-10-14 22:56:39 +02:00
|
|
|
struct fib_node *f = container_of(head, struct fib_node, fn_embedded_alias.rcu);
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
kmem_cache_free(fn_hash_kmem, f);
|
|
|
|
}
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
static inline void fn_free_node(struct fib_node *f)
|
|
|
|
{
|
|
|
|
call_rcu(&f->fn_embedded_alias.rcu, fn_free_node_rcu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fn_free_alias_rcu(struct rcu_head *head)
|
|
|
|
{
|
|
|
|
struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
|
|
|
|
|
|
|
|
kmem_cache_free(fn_alias_kmem, fa);
|
|
|
|
}
|
|
|
|
|
2008-01-18 12:33:26 +01:00
|
|
|
static inline void fn_free_alias(struct fib_alias *fa, struct fib_node *f)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
fib_release_info(fa->fa_info);
|
2008-01-18 12:33:26 +01:00
|
|
|
if (fa == &f->fn_embedded_alias)
|
|
|
|
fa->fa_info = NULL;
|
|
|
|
else
|
2010-10-14 22:56:39 +02:00
|
|
|
call_rcu(&fa->rcu, fn_free_alias_rcu);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct fn_zone *
|
|
|
|
fn_new_zone(struct fn_hash *table, int z)
|
|
|
|
{
|
|
|
|
int i;
|
2006-07-21 23:51:30 +02:00
|
|
|
struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!fz)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
seqlock_init(&fz->fz_lock);
|
2010-10-14 22:53:04 +02:00
|
|
|
fz->fz_divisor = z ? EMBEDDED_HASH_SIZE : 1;
|
|
|
|
fz->fz_hashmask = fz->fz_divisor - 1;
|
2010-10-26 05:24:16 +02:00
|
|
|
RCU_INIT_POINTER(fz->fz_hash, fz->fz_embedded_hash);
|
2005-04-17 00:20:36 +02:00
|
|
|
fz->fz_order = z;
|
2010-10-14 22:53:04 +02:00
|
|
|
fz->fz_revorder = 32 - z;
|
2005-04-17 00:20:36 +02:00
|
|
|
fz->fz_mask = inet_make_mask(z);
|
|
|
|
|
|
|
|
/* Find the first not empty zone with more specific mask */
|
2010-10-14 22:53:34 +02:00
|
|
|
for (i = z + 1; i <= 32; i++)
|
2005-04-17 00:20:36 +02:00
|
|
|
if (table->fn_zones[i])
|
|
|
|
break;
|
2010-10-14 22:53:34 +02:00
|
|
|
if (i > 32) {
|
2005-04-17 00:20:36 +02:00
|
|
|
/* No more specific masks, we are the first. */
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_assign_pointer(fz->fz_next,
|
|
|
|
rtnl_dereference(table->fn_zone_list));
|
|
|
|
rcu_assign_pointer(table->fn_zone_list, fz);
|
2005-04-17 00:20:36 +02:00
|
|
|
} else {
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_assign_pointer(fz->fz_next,
|
|
|
|
rtnl_dereference(table->fn_zones[i]->fz_next));
|
|
|
|
rcu_assign_pointer(table->fn_zones[i]->fz_next, fz);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
table->fn_zones[z] = fz;
|
|
|
|
fib_hash_genid++;
|
|
|
|
return fz;
|
|
|
|
}
|
|
|
|
|
2009-09-20 12:35:36 +02:00
|
|
|
int fib_table_lookup(struct fib_table *tb,
|
2010-10-05 12:41:36 +02:00
|
|
|
const struct flowi *flp, struct fib_result *res,
|
|
|
|
int fib_flags)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct fn_zone *fz;
|
2008-11-03 09:25:16 +01:00
|
|
|
struct fn_hash *t = (struct fn_hash *)tb->tb_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_read_lock();
|
|
|
|
for (fz = rcu_dereference(t->fn_zone_list);
|
|
|
|
fz != NULL;
|
|
|
|
fz = rcu_dereference(fz->fz_next)) {
|
2010-10-26 05:24:16 +02:00
|
|
|
struct hlist_head *head;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct hlist_node *node;
|
|
|
|
struct fib_node *f;
|
2010-10-14 22:56:39 +02:00
|
|
|
__be32 k;
|
|
|
|
unsigned int seq;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
do {
|
|
|
|
seq = read_seqbegin(&fz->fz_lock);
|
|
|
|
k = fz_key(flp->fl4_dst, fz);
|
|
|
|
|
2010-10-26 05:24:16 +02:00
|
|
|
head = rcu_dereference(fz->fz_hash) + fn_hash(k, fz);
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_for_each_entry_rcu(f, node, head, fn_hash) {
|
|
|
|
if (f->fn_key != k)
|
|
|
|
continue;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
err = fib_semantic_match(&f->fn_alias,
|
2005-04-17 00:20:36 +02:00
|
|
|
flp, res,
|
2010-10-05 12:41:36 +02:00
|
|
|
fz->fz_order, fib_flags);
|
2010-10-14 22:56:39 +02:00
|
|
|
if (err <= 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} while (read_seqretry(&fz->fz_lock, seq));
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
err = 1;
|
|
|
|
out:
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_read_unlock();
|
2005-04-17 00:20:36 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-09-20 12:35:36 +02:00
|
|
|
void fib_table_select_default(struct fib_table *tb,
|
|
|
|
const struct flowi *flp, struct fib_result *res)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int order, last_idx;
|
|
|
|
struct hlist_node *node;
|
|
|
|
struct fib_node *f;
|
|
|
|
struct fib_info *fi = NULL;
|
|
|
|
struct fib_info *last_resort;
|
2008-11-03 09:25:16 +01:00
|
|
|
struct fn_hash *t = (struct fn_hash *)tb->tb_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fn_zone *fz = t->fn_zones[0];
|
2010-10-26 05:24:16 +02:00
|
|
|
struct hlist_head *head;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (fz == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
last_idx = -1;
|
|
|
|
last_resort = NULL;
|
|
|
|
order = -1;
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
rcu_read_lock();
|
2010-10-26 05:24:16 +02:00
|
|
|
head = rcu_dereference(fz->fz_hash);
|
|
|
|
hlist_for_each_entry_rcu(f, node, head, fn_hash) {
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fib_alias *fa;
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fib_info *next_fi = fa->fa_info;
|
|
|
|
|
|
|
|
if (fa->fa_scope != res->scope ||
|
|
|
|
fa->fa_type != RTN_UNICAST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (next_fi->fib_priority > res->fi->fib_priority)
|
|
|
|
break;
|
|
|
|
if (!next_fi->fib_nh[0].nh_gw ||
|
|
|
|
next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
|
|
|
|
continue;
|
2010-10-21 00:03:38 +02:00
|
|
|
|
|
|
|
fib_alias_accessed(fa);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (fi == NULL) {
|
|
|
|
if (next_fi != res->fi)
|
|
|
|
break;
|
|
|
|
} else if (!fib_detect_death(fi, order, &last_resort,
|
2007-12-08 09:32:23 +01:00
|
|
|
&last_idx, tb->tb_default)) {
|
2007-12-08 09:31:44 +01:00
|
|
|
fib_result_assign(res, fi);
|
2007-12-08 09:32:23 +01:00
|
|
|
tb->tb_default = order;
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
fi = next_fi;
|
|
|
|
order++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (order <= 0 || fi == NULL) {
|
2007-12-08 09:32:23 +01:00
|
|
|
tb->tb_default = -1;
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-12-08 09:32:23 +01:00
|
|
|
if (!fib_detect_death(fi, order, &last_resort, &last_idx,
|
|
|
|
tb->tb_default)) {
|
2007-12-08 09:31:44 +01:00
|
|
|
fib_result_assign(res, fi);
|
2007-12-08 09:32:23 +01:00
|
|
|
tb->tb_default = order;
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-12-08 09:31:44 +01:00
|
|
|
if (last_idx >= 0)
|
|
|
|
fib_result_assign(res, last_resort);
|
2007-12-08 09:32:23 +01:00
|
|
|
tb->tb_default = last_idx;
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
2010-10-14 22:56:39 +02:00
|
|
|
rcu_read_unlock();
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert node F to FZ. */
|
|
|
|
static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
|
|
|
|
{
|
2010-10-26 05:24:16 +02:00
|
|
|
struct hlist_head *head = rtnl_dereference(fz->fz_hash) + fn_hash(f->fn_key, fz);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_add_head_rcu(&f->fn_hash, head);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the node in FZ matching KEY. */
|
2006-09-27 07:20:01 +02:00
|
|
|
static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2010-10-26 05:24:16 +02:00
|
|
|
struct hlist_head *head = rtnl_dereference(fz->fz_hash) + fn_hash(key, fz);
|
2005-04-17 00:20:36 +02:00
|
|
|
struct hlist_node *node;
|
|
|
|
struct fib_node *f;
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_for_each_entry_rcu(f, node, head, fn_hash) {
|
2005-04-17 00:20:36 +02:00
|
|
|
if (f->fn_key == key)
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
|
|
|
|
static struct fib_alias *fib_fast_alloc(struct fib_node *f)
|
|
|
|
{
|
|
|
|
struct fib_alias *fa = &f->fn_embedded_alias;
|
|
|
|
|
|
|
|
if (fa->fa_info != NULL)
|
|
|
|
fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
|
|
|
|
return fa;
|
|
|
|
}
|
|
|
|
|
2010-10-14 22:53:34 +02:00
|
|
|
/* Caller must hold RTNL. */
|
2009-09-20 12:35:36 +02:00
|
|
|
int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct fn_hash *table = (struct fn_hash *) tb->tb_data;
|
2008-02-20 01:28:54 +01:00
|
|
|
struct fib_node *new_f = NULL;
|
|
|
|
struct fib_node *f;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fib_alias *fa, *new_fa;
|
|
|
|
struct fn_zone *fz;
|
|
|
|
struct fib_info *fi;
|
2006-08-18 03:14:52 +02:00
|
|
|
u8 tos = cfg->fc_tos;
|
2006-09-27 07:20:01 +02:00
|
|
|
__be32 key;
|
2005-04-17 00:20:36 +02:00
|
|
|
int err;
|
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
if (cfg->fc_dst_len > 32)
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
2006-08-18 03:14:52 +02:00
|
|
|
|
|
|
|
fz = table->fn_zones[cfg->fc_dst_len];
|
|
|
|
if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -ENOBUFS;
|
|
|
|
|
|
|
|
key = 0;
|
2006-08-18 03:14:52 +02:00
|
|
|
if (cfg->fc_dst) {
|
|
|
|
if (cfg->fc_dst & ~FZ_MASK(fz))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
2006-08-18 03:14:52 +02:00
|
|
|
key = fz_key(cfg->fc_dst, fz);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
fi = fib_create_info(cfg);
|
|
|
|
if (IS_ERR(fi))
|
|
|
|
return PTR_ERR(fi);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (fz->fz_nent > (fz->fz_divisor<<1) &&
|
|
|
|
fz->fz_divisor < FZ_MAX_DIVISOR &&
|
2006-08-18 03:14:52 +02:00
|
|
|
(cfg->fc_dst_len == 32 ||
|
|
|
|
(1 << cfg->fc_dst_len) > fz->fz_divisor))
|
2005-04-17 00:20:36 +02:00
|
|
|
fn_rehash_zone(fz);
|
|
|
|
|
|
|
|
f = fib_find_node(fz, key);
|
|
|
|
|
|
|
|
if (!f)
|
|
|
|
fa = NULL;
|
|
|
|
else
|
|
|
|
fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
|
|
|
|
|
|
|
|
/* Now fa, if non-NULL, points to the first fib alias
|
|
|
|
* with the same keys [prefix,tos,priority], if such key already
|
|
|
|
* exists or to the node before which we will insert new one.
|
|
|
|
*
|
|
|
|
* If fa is NULL, we will need to allocate a new one and
|
|
|
|
* insert to the head of f.
|
|
|
|
*
|
|
|
|
* If f is NULL, no fib node matched the destination key
|
|
|
|
* and we need to allocate a new one of those as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fa && fa->fa_tos == tos &&
|
|
|
|
fa->fa_info->fib_priority == fi->fib_priority) {
|
2008-01-29 06:14:10 +01:00
|
|
|
struct fib_alias *fa_first, *fa_match;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
err = -EEXIST;
|
2006-08-18 03:14:52 +02:00
|
|
|
if (cfg->fc_nlflags & NLM_F_EXCL)
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out;
|
|
|
|
|
2008-01-29 06:14:10 +01:00
|
|
|
/* We have 2 goals:
|
|
|
|
* 1. Find exact match for type, scope, fib_info to avoid
|
|
|
|
* duplicate routes
|
|
|
|
* 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
|
|
|
|
*/
|
|
|
|
fa_match = NULL;
|
|
|
|
fa_first = fa;
|
|
|
|
fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
|
|
|
|
list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
|
|
|
|
if (fa->fa_tos != tos)
|
|
|
|
break;
|
|
|
|
if (fa->fa_info->fib_priority != fi->fib_priority)
|
|
|
|
break;
|
|
|
|
if (fa->fa_type == cfg->fc_type &&
|
|
|
|
fa->fa_scope == cfg->fc_scope &&
|
|
|
|
fa->fa_info == fi) {
|
|
|
|
fa_match = fa;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
if (cfg->fc_nlflags & NLM_F_REPLACE) {
|
2005-04-17 00:20:36 +02:00
|
|
|
u8 state;
|
|
|
|
|
2008-01-29 06:14:10 +01:00
|
|
|
fa = fa_first;
|
|
|
|
if (fa_match) {
|
|
|
|
if (fa == fa_match)
|
|
|
|
err = 0;
|
2008-01-18 12:44:48 +01:00
|
|
|
goto out;
|
2008-01-29 06:14:10 +01:00
|
|
|
}
|
2010-10-14 22:56:39 +02:00
|
|
|
err = -ENOBUFS;
|
|
|
|
new_fa = fib_fast_alloc(f);
|
|
|
|
if (new_fa == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
new_fa->fa_tos = fa->fa_tos;
|
|
|
|
new_fa->fa_info = fi;
|
|
|
|
new_fa->fa_type = cfg->fc_type;
|
|
|
|
new_fa->fa_scope = cfg->fc_scope;
|
2005-04-17 00:20:36 +02:00
|
|
|
state = fa->fa_state;
|
2010-10-14 22:56:39 +02:00
|
|
|
new_fa->fa_state = state & ~FA_S_ACCESSED;
|
2005-04-17 00:20:36 +02:00
|
|
|
fib_hash_genid++;
|
2010-10-14 22:56:39 +02:00
|
|
|
list_replace_rcu(&fa->fa_list, &new_fa->fa_list);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
fn_free_alias(fa, f);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (state & FA_S_ACCESSED)
|
2008-07-06 04:00:44 +02:00
|
|
|
rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
|
2010-10-14 22:56:39 +02:00
|
|
|
rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len,
|
|
|
|
tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
|
2005-04-17 00:20:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error if we find a perfect match which
|
|
|
|
* uses the same scope, type, and nexthop
|
|
|
|
* information.
|
|
|
|
*/
|
2008-01-29 06:14:10 +01:00
|
|
|
if (fa_match)
|
|
|
|
goto out;
|
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
if (!(cfg->fc_nlflags & NLM_F_APPEND))
|
2008-01-29 06:14:10 +01:00
|
|
|
fa = fa_first;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = -ENOENT;
|
2006-08-18 03:14:52 +02:00
|
|
|
if (!(cfg->fc_nlflags & NLM_F_CREATE))
|
2005-04-17 00:20:36 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = -ENOBUFS;
|
|
|
|
|
|
|
|
if (!f) {
|
2008-01-18 12:33:26 +01:00
|
|
|
new_f = kmem_cache_zalloc(fn_hash_kmem, GFP_KERNEL);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (new_f == NULL)
|
2008-01-18 12:33:26 +01:00
|
|
|
goto out;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
INIT_HLIST_NODE(&new_f->fn_hash);
|
|
|
|
INIT_LIST_HEAD(&new_f->fn_alias);
|
|
|
|
new_f->fn_key = key;
|
|
|
|
f = new_f;
|
|
|
|
}
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
new_fa = fib_fast_alloc(f);
|
|
|
|
if (new_fa == NULL)
|
|
|
|
goto out;
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
new_fa->fa_info = fi;
|
|
|
|
new_fa->fa_tos = tos;
|
2006-08-18 03:14:52 +02:00
|
|
|
new_fa->fa_type = cfg->fc_type;
|
|
|
|
new_fa->fa_scope = cfg->fc_scope;
|
2005-04-17 00:20:36 +02:00
|
|
|
new_fa->fa_state = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert new entry to the list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (new_f)
|
|
|
|
fib_insert_node(fz, new_f);
|
2010-10-14 22:56:39 +02:00
|
|
|
list_add_tail_rcu(&new_fa->fa_list,
|
2005-04-17 00:20:36 +02:00
|
|
|
(fa ? &fa->fa_list : &f->fn_alias));
|
|
|
|
fib_hash_genid++;
|
|
|
|
|
|
|
|
if (new_f)
|
|
|
|
fz->fz_nent++;
|
2008-07-06 04:00:44 +02:00
|
|
|
rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
|
2007-05-23 23:55:06 +02:00
|
|
|
&cfg->fc_nlinfo, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
2008-02-20 01:28:54 +01:00
|
|
|
if (new_f)
|
|
|
|
kmem_cache_free(fn_hash_kmem, new_f);
|
2005-04-17 00:20:36 +02:00
|
|
|
fib_release_info(fi);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-09-20 12:35:36 +02:00
|
|
|
int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-11-03 09:25:16 +01:00
|
|
|
struct fn_hash *table = (struct fn_hash *)tb->tb_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fib_node *f;
|
|
|
|
struct fib_alias *fa, *fa_to_delete;
|
|
|
|
struct fn_zone *fz;
|
2006-09-27 07:20:01 +02:00
|
|
|
__be32 key;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
if (cfg->fc_dst_len > 32)
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
2006-08-18 03:14:52 +02:00
|
|
|
|
|
|
|
if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
|
2005-04-17 00:20:36 +02:00
|
|
|
return -ESRCH;
|
|
|
|
|
|
|
|
key = 0;
|
2006-08-18 03:14:52 +02:00
|
|
|
if (cfg->fc_dst) {
|
|
|
|
if (cfg->fc_dst & ~FZ_MASK(fz))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
2006-08-18 03:14:52 +02:00
|
|
|
key = fz_key(cfg->fc_dst, fz);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f = fib_find_node(fz, key);
|
|
|
|
|
|
|
|
if (!f)
|
|
|
|
fa = NULL;
|
|
|
|
else
|
2006-08-18 03:14:52 +02:00
|
|
|
fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!fa)
|
|
|
|
return -ESRCH;
|
|
|
|
|
|
|
|
fa_to_delete = NULL;
|
|
|
|
fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
|
|
|
|
list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
|
|
|
|
struct fib_info *fi = fa->fa_info;
|
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
if (fa->fa_tos != cfg->fc_tos)
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
|
2006-08-18 03:14:52 +02:00
|
|
|
if ((!cfg->fc_type ||
|
|
|
|
fa->fa_type == cfg->fc_type) &&
|
|
|
|
(cfg->fc_scope == RT_SCOPE_NOWHERE ||
|
|
|
|
fa->fa_scope == cfg->fc_scope) &&
|
|
|
|
(!cfg->fc_protocol ||
|
|
|
|
fi->fib_protocol == cfg->fc_protocol) &&
|
|
|
|
fib_nh_match(cfg, fi) == 0) {
|
2005-04-17 00:20:36 +02:00
|
|
|
fa_to_delete = fa;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fa_to_delete) {
|
|
|
|
int kill_fn;
|
|
|
|
|
|
|
|
fa = fa_to_delete;
|
2006-08-18 03:14:52 +02:00
|
|
|
rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
|
2007-05-23 23:55:06 +02:00
|
|
|
tb->tb_id, &cfg->fc_nlinfo, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
kill_fn = 0;
|
2010-10-14 22:56:39 +02:00
|
|
|
list_del_rcu(&fa->fa_list);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (list_empty(&f->fn_alias)) {
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_del_rcu(&f->fn_hash);
|
2005-04-17 00:20:36 +02:00
|
|
|
kill_fn = 1;
|
|
|
|
}
|
|
|
|
fib_hash_genid++;
|
|
|
|
|
|
|
|
if (fa->fa_state & FA_S_ACCESSED)
|
2008-07-06 04:00:44 +02:00
|
|
|
rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
|
2008-01-18 12:33:26 +01:00
|
|
|
fn_free_alias(fa, f);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (kill_fn) {
|
|
|
|
fn_free_node(f);
|
|
|
|
fz->fz_nent--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fn_flush_list(struct fn_zone *fz, int idx)
|
|
|
|
{
|
2010-10-26 05:24:16 +02:00
|
|
|
struct hlist_head *head = rtnl_dereference(fz->fz_hash) + idx;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct hlist_node *node, *n;
|
|
|
|
struct fib_node *f;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
|
|
|
|
struct fib_alias *fa, *fa_node;
|
|
|
|
int kill_f;
|
|
|
|
|
|
|
|
kill_f = 0;
|
|
|
|
list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
|
|
|
|
struct fib_info *fi = fa->fa_info;
|
|
|
|
|
|
|
|
if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
|
2010-10-14 22:56:39 +02:00
|
|
|
list_del_rcu(&fa->fa_list);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (list_empty(&f->fn_alias)) {
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_del_rcu(&f->fn_hash);
|
2005-04-17 00:20:36 +02:00
|
|
|
kill_f = 1;
|
|
|
|
}
|
|
|
|
fib_hash_genid++;
|
|
|
|
|
2008-01-18 12:33:26 +01:00
|
|
|
fn_free_alias(fa, f);
|
2005-04-17 00:20:36 +02:00
|
|
|
found++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kill_f) {
|
|
|
|
fn_free_node(f);
|
|
|
|
fz->fz_nent--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2010-10-14 22:53:34 +02:00
|
|
|
/* caller must hold RTNL. */
|
2009-09-20 12:35:36 +02:00
|
|
|
int fib_table_flush(struct fib_table *tb)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct fn_hash *table = (struct fn_hash *) tb->tb_data;
|
|
|
|
struct fn_zone *fz;
|
|
|
|
int found = 0;
|
|
|
|
|
2010-10-14 22:53:34 +02:00
|
|
|
for (fz = rtnl_dereference(table->fn_zone_list);
|
|
|
|
fz != NULL;
|
|
|
|
fz = rtnl_dereference(fz->fz_next)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = fz->fz_divisor - 1; i >= 0; i--)
|
|
|
|
found += fn_flush_list(fz, i);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2010-10-28 04:00:43 +02:00
|
|
|
void fib_free_table(struct fib_table *tb)
|
|
|
|
{
|
|
|
|
struct fn_hash *table = (struct fn_hash *) tb->tb_data;
|
|
|
|
struct fn_zone *fz, *next;
|
|
|
|
|
|
|
|
next = table->fn_zone_list;
|
|
|
|
while (next != NULL) {
|
|
|
|
fz = next;
|
|
|
|
next = fz->fz_next;
|
|
|
|
|
|
|
|
if (fz->fz_hash != fz->fz_embedded_hash)
|
|
|
|
fz_hash_free(fz->fz_hash, fz->fz_divisor);
|
|
|
|
|
|
|
|
kfree(fz);
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(tb);
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
|
|
|
|
struct fib_table *tb,
|
|
|
|
struct fn_zone *fz,
|
|
|
|
struct hlist_head *head)
|
|
|
|
{
|
|
|
|
struct hlist_node *node;
|
|
|
|
struct fib_node *f;
|
|
|
|
int i, s_i;
|
|
|
|
|
2006-08-11 08:10:46 +02:00
|
|
|
s_i = cb->args[4];
|
2005-04-17 00:20:36 +02:00
|
|
|
i = 0;
|
2010-10-14 22:56:39 +02:00
|
|
|
hlist_for_each_entry_rcu(f, node, head, fn_hash) {
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fib_alias *fa;
|
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
|
2005-04-17 00:20:36 +02:00
|
|
|
if (i < s_i)
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
|
|
|
|
cb->nlh->nlmsg_seq,
|
|
|
|
RTM_NEWROUTE,
|
|
|
|
tb->tb_id,
|
|
|
|
fa->fa_type,
|
|
|
|
fa->fa_scope,
|
2006-08-18 03:15:17 +02:00
|
|
|
f->fn_key,
|
2005-04-17 00:20:36 +02:00
|
|
|
fz->fz_order,
|
|
|
|
fa->fa_tos,
|
2005-06-19 07:54:12 +02:00
|
|
|
fa->fa_info,
|
|
|
|
NLM_F_MULTI) < 0) {
|
2006-08-11 08:10:46 +02:00
|
|
|
cb->args[4] = i;
|
2005-04-17 00:20:36 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2010-10-14 22:56:39 +02:00
|
|
|
next:
|
2005-04-17 00:20:36 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2006-08-11 08:10:46 +02:00
|
|
|
cb->args[4] = i;
|
2005-04-17 00:20:36 +02:00
|
|
|
return skb->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
|
|
|
|
struct fib_table *tb,
|
|
|
|
struct fn_zone *fz)
|
|
|
|
{
|
|
|
|
int h, s_h;
|
2010-10-26 05:24:16 +02:00
|
|
|
struct hlist_head *head = rcu_dereference(fz->fz_hash);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-10-26 05:24:16 +02:00
|
|
|
if (head == NULL)
|
2008-01-18 13:30:21 +01:00
|
|
|
return skb->len;
|
2006-08-11 08:10:46 +02:00
|
|
|
s_h = cb->args[3];
|
2008-01-18 13:30:21 +01:00
|
|
|
for (h = s_h; h < fz->fz_divisor; h++) {
|
2010-10-26 05:24:16 +02:00
|
|
|
if (hlist_empty(head + h))
|
2005-04-17 00:20:36 +02:00
|
|
|
continue;
|
2010-10-26 05:24:16 +02:00
|
|
|
if (fn_hash_dump_bucket(skb, cb, tb, fz, head + h) < 0) {
|
2006-08-11 08:10:46 +02:00
|
|
|
cb->args[3] = h;
|
2005-04-17 00:20:36 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-01-18 13:30:21 +01:00
|
|
|
memset(&cb->args[4], 0,
|
|
|
|
sizeof(cb->args) - 4*sizeof(cb->args[0]));
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2006-08-11 08:10:46 +02:00
|
|
|
cb->args[3] = h;
|
2005-04-17 00:20:36 +02:00
|
|
|
return skb->len;
|
|
|
|
}
|
|
|
|
|
2009-09-20 12:35:36 +02:00
|
|
|
int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
|
|
|
|
struct netlink_callback *cb)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2010-10-14 22:53:34 +02:00
|
|
|
int m = 0, s_m;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fn_zone *fz;
|
2008-11-03 09:25:16 +01:00
|
|
|
struct fn_hash *table = (struct fn_hash *)tb->tb_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-08-11 08:10:46 +02:00
|
|
|
s_m = cb->args[2];
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_read_lock();
|
|
|
|
for (fz = rcu_dereference(table->fn_zone_list);
|
|
|
|
fz != NULL;
|
|
|
|
fz = rcu_dereference(fz->fz_next), m++) {
|
|
|
|
if (m < s_m)
|
|
|
|
continue;
|
2005-04-17 00:20:36 +02:00
|
|
|
if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
|
2006-08-11 08:10:46 +02:00
|
|
|
cb->args[2] = m;
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_read_unlock();
|
2005-04-17 00:20:36 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-01-18 13:30:21 +01:00
|
|
|
memset(&cb->args[3], 0,
|
|
|
|
sizeof(cb->args) - 3*sizeof(cb->args[0]));
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_read_unlock();
|
2006-08-11 08:10:46 +02:00
|
|
|
cb->args[2] = m;
|
2005-04-17 00:20:36 +02:00
|
|
|
return skb->len;
|
|
|
|
}
|
|
|
|
|
2008-01-15 08:14:20 +01:00
|
|
|
void __init fib_hash_init(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-01-15 08:14:20 +01:00
|
|
|
fn_hash_kmem = kmem_cache_create("ip_fib_hash", sizeof(struct fib_node),
|
2008-01-18 12:33:26 +01:00
|
|
|
0, SLAB_PANIC, NULL);
|
2008-01-15 08:14:20 +01:00
|
|
|
|
|
|
|
fn_alias_kmem = kmem_cache_create("ip_fib_alias", sizeof(struct fib_alias),
|
2008-01-18 12:33:26 +01:00
|
|
|
0, SLAB_PANIC, NULL);
|
2008-01-15 08:14:20 +01:00
|
|
|
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-01-15 08:14:20 +01:00
|
|
|
struct fib_table *fib_hash_table(u32 id)
|
|
|
|
{
|
|
|
|
struct fib_table *tb;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (tb == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tb->tb_id = id;
|
2007-12-08 09:32:23 +01:00
|
|
|
tb->tb_default = -1;
|
2009-09-20 12:35:36 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
memset(tb->tb_data, 0, sizeof(struct fn_hash));
|
|
|
|
return tb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
|
|
|
|
struct fib_iter_state {
|
2008-01-10 12:26:50 +01:00
|
|
|
struct seq_net_private p;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fn_zone *zone;
|
|
|
|
int bucket;
|
|
|
|
struct hlist_head *hash_head;
|
|
|
|
struct fib_node *fn;
|
|
|
|
struct fib_alias *fa;
|
|
|
|
loff_t pos;
|
|
|
|
unsigned int genid;
|
|
|
|
int valid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct fib_alias *fib_get_first(struct seq_file *seq)
|
|
|
|
{
|
|
|
|
struct fib_iter_state *iter = seq->private;
|
2008-01-10 12:26:50 +01:00
|
|
|
struct fib_table *main_table;
|
|
|
|
struct fn_hash *table;
|
|
|
|
|
2008-03-25 18:36:06 +01:00
|
|
|
main_table = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
|
2008-01-10 12:26:50 +01:00
|
|
|
table = (struct fn_hash *)main_table->tb_data;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
iter->bucket = 0;
|
|
|
|
iter->hash_head = NULL;
|
|
|
|
iter->fn = NULL;
|
|
|
|
iter->fa = NULL;
|
|
|
|
iter->pos = 0;
|
|
|
|
iter->genid = fib_hash_genid;
|
|
|
|
iter->valid = 1;
|
|
|
|
|
2010-10-14 22:53:34 +02:00
|
|
|
for (iter->zone = rcu_dereference(table->fn_zone_list);
|
|
|
|
iter->zone != NULL;
|
|
|
|
iter->zone = rcu_dereference(iter->zone->fz_next)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
int maxslot;
|
|
|
|
|
|
|
|
if (!iter->zone->fz_nent)
|
|
|
|
continue;
|
|
|
|
|
2010-10-26 05:24:16 +02:00
|
|
|
iter->hash_head = rcu_dereference(iter->zone->fz_hash);
|
2005-04-17 00:20:36 +02:00
|
|
|
maxslot = iter->zone->fz_divisor;
|
|
|
|
|
|
|
|
for (iter->bucket = 0; iter->bucket < maxslot;
|
|
|
|
++iter->bucket, ++iter->hash_head) {
|
|
|
|
struct hlist_node *node;
|
|
|
|
struct fib_node *fn;
|
|
|
|
|
2008-11-03 09:25:16 +01:00
|
|
|
hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
|
2005-04-17 00:20:36 +02:00
|
|
|
struct fib_alias *fa;
|
|
|
|
|
2008-11-03 09:25:16 +01:00
|
|
|
list_for_each_entry(fa, &fn->fn_alias, fa_list) {
|
2005-04-17 00:20:36 +02:00
|
|
|
iter->fn = fn;
|
|
|
|
iter->fa = fa;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return iter->fa;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct fib_alias *fib_get_next(struct seq_file *seq)
|
|
|
|
{
|
|
|
|
struct fib_iter_state *iter = seq->private;
|
|
|
|
struct fib_node *fn;
|
|
|
|
struct fib_alias *fa;
|
|
|
|
|
|
|
|
/* Advance FA, if any. */
|
|
|
|
fn = iter->fn;
|
|
|
|
fa = iter->fa;
|
|
|
|
if (fa) {
|
|
|
|
BUG_ON(!fn);
|
|
|
|
list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
|
|
|
|
iter->fa = fa;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fa = iter->fa = NULL;
|
|
|
|
|
|
|
|
/* Advance FN. */
|
|
|
|
if (fn) {
|
|
|
|
struct hlist_node *node = &fn->fn_hash;
|
|
|
|
hlist_for_each_entry_continue(fn, node, fn_hash) {
|
|
|
|
iter->fn = fn;
|
|
|
|
|
|
|
|
list_for_each_entry(fa, &fn->fn_alias, fa_list) {
|
|
|
|
iter->fa = fa;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn = iter->fn = NULL;
|
|
|
|
|
|
|
|
/* Advance hash chain. */
|
|
|
|
if (!iter->zone)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct hlist_node *node;
|
|
|
|
int maxslot;
|
|
|
|
|
|
|
|
maxslot = iter->zone->fz_divisor;
|
|
|
|
|
|
|
|
while (++iter->bucket < maxslot) {
|
|
|
|
iter->hash_head++;
|
|
|
|
|
|
|
|
hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
|
|
|
|
list_for_each_entry(fa, &fn->fn_alias, fa_list) {
|
|
|
|
iter->fn = fn;
|
|
|
|
iter->fa = fa;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-14 22:53:34 +02:00
|
|
|
iter->zone = rcu_dereference(iter->zone->fz_next);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (!iter->zone)
|
|
|
|
goto out;
|
2007-02-09 15:24:47 +01:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
iter->bucket = 0;
|
2010-10-26 05:24:16 +02:00
|
|
|
iter->hash_head = rcu_dereference(iter->zone->fz_hash);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
|
|
|
|
list_for_each_entry(fa, &fn->fn_alias, fa_list) {
|
|
|
|
iter->fn = fn;
|
|
|
|
iter->fa = fa;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
iter->pos++;
|
|
|
|
return fa;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
|
|
|
|
{
|
|
|
|
struct fib_iter_state *iter = seq->private;
|
|
|
|
struct fib_alias *fa;
|
2007-02-09 15:24:47 +01:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
|
|
|
|
fa = iter->fa;
|
|
|
|
pos -= iter->pos;
|
|
|
|
} else
|
|
|
|
fa = fib_get_first(seq);
|
|
|
|
|
|
|
|
if (fa)
|
|
|
|
while (pos && (fa = fib_get_next(seq)))
|
|
|
|
--pos;
|
|
|
|
return pos ? NULL : fa;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
|
2010-10-14 22:53:34 +02:00
|
|
|
__acquires(RCU)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
void *v = NULL;
|
|
|
|
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_read_lock();
|
2008-03-25 18:36:06 +01:00
|
|
|
if (fib_get_table(seq_file_net(seq), RT_TABLE_MAIN))
|
2005-04-17 00:20:36 +02:00
|
|
|
v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
++*pos;
|
|
|
|
return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fib_seq_stop(struct seq_file *seq, void *v)
|
2010-10-14 22:53:34 +02:00
|
|
|
__releases(RCU)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2010-10-14 22:53:34 +02:00
|
|
|
rcu_read_unlock();
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2006-09-27 07:20:01 +02:00
|
|
|
static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2005-11-30 01:21:38 +01:00
|
|
|
static const unsigned type2flags[RTN_MAX + 1] = {
|
2010-10-14 22:56:39 +02:00
|
|
|
[7] = RTF_REJECT,
|
|
|
|
[8] = RTF_REJECT,
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
unsigned flags = type2flags[type];
|
|
|
|
|
|
|
|
if (fi && fi->fib_nh->nh_gw)
|
|
|
|
flags |= RTF_GATEWAY;
|
2006-09-27 07:20:01 +02:00
|
|
|
if (mask == htonl(0xFFFFFFFF))
|
2005-04-17 00:20:36 +02:00
|
|
|
flags |= RTF_HOST;
|
|
|
|
flags |= RTF_UP;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2007-02-09 15:24:47 +01:00
|
|
|
/*
|
2005-04-17 00:20:36 +02:00
|
|
|
* This outputs /proc/net/route.
|
|
|
|
*
|
|
|
|
* It always works in backward compatibility mode.
|
|
|
|
* The format of the file is not supposed to be changed.
|
|
|
|
*/
|
|
|
|
static int fib_seq_show(struct seq_file *seq, void *v)
|
|
|
|
{
|
|
|
|
struct fib_iter_state *iter;
|
2008-04-24 10:02:16 +02:00
|
|
|
int len;
|
2006-09-27 07:20:01 +02:00
|
|
|
__be32 prefix, mask;
|
2005-04-17 00:20:36 +02:00
|
|
|
unsigned flags;
|
|
|
|
struct fib_node *f;
|
|
|
|
struct fib_alias *fa;
|
|
|
|
struct fib_info *fi;
|
|
|
|
|
|
|
|
if (v == SEQ_START_TOKEN) {
|
|
|
|
seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
|
|
|
|
"\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
|
|
|
|
"\tWindow\tIRTT");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = seq->private;
|
|
|
|
f = iter->fn;
|
|
|
|
fa = iter->fa;
|
|
|
|
fi = fa->fa_info;
|
|
|
|
prefix = f->fn_key;
|
|
|
|
mask = FZ_MASK(iter->zone);
|
|
|
|
flags = fib_flag_trans(fa->fa_type, mask, fi);
|
|
|
|
if (fi)
|
2008-04-24 10:02:16 +02:00
|
|
|
seq_printf(seq,
|
|
|
|
"%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
|
2005-04-17 00:20:36 +02:00
|
|
|
fi->fib_dev ? fi->fib_dev->name : "*", prefix,
|
|
|
|
fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
|
|
|
|
mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
|
|
|
|
fi->fib_window,
|
2008-04-24 10:02:16 +02:00
|
|
|
fi->fib_rtt >> 3, &len);
|
2005-04-17 00:20:36 +02:00
|
|
|
else
|
2008-04-24 10:02:16 +02:00
|
|
|
seq_printf(seq,
|
|
|
|
"*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
|
|
|
|
prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0, &len);
|
|
|
|
|
|
|
|
seq_printf(seq, "%*s\n", 127 - len, "");
|
2005-04-17 00:20:36 +02:00
|
|
|
out:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-12 22:34:29 +01:00
|
|
|
static const struct seq_operations fib_seq_ops = {
|
2005-04-17 00:20:36 +02:00
|
|
|
.start = fib_seq_start,
|
|
|
|
.next = fib_seq_next,
|
|
|
|
.stop = fib_seq_stop,
|
|
|
|
.show = fib_seq_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int fib_seq_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2008-01-10 12:26:50 +01:00
|
|
|
return seq_open_net(inode, file, &fib_seq_ops,
|
|
|
|
sizeof(struct fib_iter_state));
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2007-02-12 09:55:35 +01:00
|
|
|
static const struct file_operations fib_seq_fops = {
|
2005-04-17 00:20:36 +02:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = fib_seq_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
2008-01-10 12:26:50 +01:00
|
|
|
.release = seq_release_net,
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
|
2008-01-10 12:21:09 +01:00
|
|
|
int __net_init fib_proc_init(struct net *net)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-01-10 12:21:09 +01:00
|
|
|
if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops))
|
2005-04-17 00:20:36 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-10 12:21:09 +01:00
|
|
|
void __net_exit fib_proc_exit(struct net *net)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-01-10 12:21:09 +01:00
|
|
|
proc_net_remove(net, "route");
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_PROC_FS */
|