2006-05-29 19:21:34 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
|
2008-10-08 03:35:00 -06:00
|
|
|
* Copyright © CC Computer Consultants GmbH, 2007 - 2008
|
2006-05-29 19:21:34 -06:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This is a replacement of the old ipt_recent module, which carried the
|
|
|
|
* following copyright notice:
|
|
|
|
*
|
|
|
|
* Author: Stephen Frost <sfrost@snowman.net>
|
|
|
|
* Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
|
|
|
|
*/
|
|
|
|
#include <linux/init.h>
|
2007-02-07 16:11:19 -07:00
|
|
|
#include <linux/ip.h>
|
2008-10-08 03:35:00 -06:00
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#include <linux/module.h>
|
2006-05-29 19:21:34 -06:00
|
|
|
#include <linux/moduleparam.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/proc_fs.h>
|
2006-05-29 19:21:34 -06:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/string.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/ctype.h>
|
2006-05-29 19:21:34 -06:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/jhash.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/inet.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 02:04:11 -06:00
|
|
|
#include <linux/slab.h>
|
2007-09-12 04:01:34 -06:00
|
|
|
#include <net/net_namespace.h>
|
2010-01-18 00:31:00 -07:00
|
|
|
#include <net/netns/generic.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-02-07 16:11:19 -07:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2008-10-08 03:35:00 -06:00
|
|
|
#include <linux/netfilter/xt_recent.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
|
2008-10-08 03:35:00 -06:00
|
|
|
MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
|
2008-01-15 00:42:28 -07:00
|
|
|
MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4");
|
2006-05-29 19:21:34 -06:00
|
|
|
MODULE_LICENSE("GPL");
|
2008-10-08 03:35:00 -06:00
|
|
|
MODULE_ALIAS("ipt_recent");
|
2008-10-08 03:35:00 -06:00
|
|
|
MODULE_ALIAS("ip6t_recent");
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-01-05 13:19:46 -07:00
|
|
|
static unsigned int ip_list_tot = 100;
|
|
|
|
static unsigned int ip_pkt_list_tot = 20;
|
|
|
|
static unsigned int ip_list_hash_size = 0;
|
|
|
|
static unsigned int ip_list_perms = 0644;
|
2006-08-22 01:30:55 -06:00
|
|
|
static unsigned int ip_list_uid = 0;
|
|
|
|
static unsigned int ip_list_gid = 0;
|
2006-01-05 13:19:46 -07:00
|
|
|
module_param(ip_list_tot, uint, 0400);
|
|
|
|
module_param(ip_pkt_list_tot, uint, 0400);
|
|
|
|
module_param(ip_list_hash_size, uint, 0400);
|
|
|
|
module_param(ip_list_perms, uint, 0400);
|
2006-08-22 01:30:55 -06:00
|
|
|
module_param(ip_list_uid, uint, 0400);
|
|
|
|
module_param(ip_list_gid, uint, 0400);
|
2006-05-29 19:21:34 -06:00
|
|
|
MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
|
2010-02-15 08:31:35 -07:00
|
|
|
MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
|
2006-05-29 19:21:34 -06:00
|
|
|
MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
|
2008-10-08 03:35:00 -06:00
|
|
|
MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
|
|
|
|
MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/xt_recent/* files");
|
|
|
|
MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/xt_recent/* files");
|
2006-05-29 19:21:34 -06:00
|
|
|
|
|
|
|
struct recent_entry {
|
|
|
|
struct list_head list;
|
|
|
|
struct list_head lru_list;
|
2008-10-08 03:35:00 -06:00
|
|
|
union nf_inet_addr addr;
|
|
|
|
u_int16_t family;
|
2006-05-29 19:21:34 -06:00
|
|
|
u_int8_t ttl;
|
|
|
|
u_int8_t index;
|
|
|
|
u_int16_t nstamps;
|
|
|
|
unsigned long stamps[0];
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
struct recent_table {
|
|
|
|
struct list_head list;
|
2008-10-08 03:35:00 -06:00
|
|
|
char name[XT_RECENT_NAME_LEN];
|
2006-05-29 19:21:34 -06:00
|
|
|
unsigned int refcnt;
|
|
|
|
unsigned int entries;
|
|
|
|
struct list_head lru_list;
|
|
|
|
struct list_head iphash[0];
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
2010-01-18 00:31:00 -07:00
|
|
|
struct recent_net {
|
|
|
|
struct list_head tables;
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct proc_dir_entry *xt_recent;
|
|
|
|
#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
|
|
|
|
struct proc_dir_entry *ipt_recent;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static int recent_net_id;
|
|
|
|
static inline struct recent_net *recent_pernet(struct net *net)
|
|
|
|
{
|
|
|
|
return net_generic(net, recent_net_id);
|
|
|
|
}
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
static DEFINE_SPINLOCK(recent_lock);
|
2006-06-09 13:17:41 -06:00
|
|
|
static DEFINE_MUTEX(recent_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-10-08 03:35:00 -06:00
|
|
|
static const struct file_operations recent_old_fops, recent_mt_fops;
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif
|
|
|
|
|
2010-01-04 08:28:38 -07:00
|
|
|
static u_int32_t hash_rnd __read_mostly;
|
|
|
|
static bool hash_rnd_inited __read_mostly;
|
2008-10-08 03:35:00 -06:00
|
|
|
|
2010-01-04 08:28:38 -07:00
|
|
|
static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
|
2008-10-08 03:35:00 -06:00
|
|
|
{
|
|
|
|
return jhash_1word((__force u32)addr->ip, hash_rnd) &
|
|
|
|
(ip_list_hash_size - 1);
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-01-04 08:28:38 -07:00
|
|
|
static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:00 -06:00
|
|
|
return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
|
|
|
|
(ip_list_hash_size - 1);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static struct recent_entry *
|
2008-10-08 03:35:00 -06:00
|
|
|
recent_entry_lookup(const struct recent_table *table,
|
|
|
|
const union nf_inet_addr *addrp, u_int16_t family,
|
|
|
|
u_int8_t ttl)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2006-05-29 19:21:34 -06:00
|
|
|
struct recent_entry *e;
|
|
|
|
unsigned int h;
|
|
|
|
|
2008-10-08 03:35:01 -06:00
|
|
|
if (family == NFPROTO_IPV4)
|
2008-10-08 03:35:00 -06:00
|
|
|
h = recent_entry_hash4(addrp);
|
|
|
|
else
|
|
|
|
h = recent_entry_hash6(addrp);
|
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
list_for_each_entry(e, &table->iphash[h], list)
|
2008-10-08 03:35:00 -06:00
|
|
|
if (e->family == family &&
|
|
|
|
memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
|
|
|
|
(ttl == e->ttl || ttl == 0 || e->ttl == 0))
|
2006-05-29 19:21:34 -06:00
|
|
|
return e;
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
|
|
|
|
{
|
|
|
|
list_del(&e->list);
|
|
|
|
list_del(&e->lru_list);
|
|
|
|
kfree(e);
|
|
|
|
t->entries--;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static struct recent_entry *
|
2008-10-08 03:35:00 -06:00
|
|
|
recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
|
|
|
|
u_int16_t family, u_int8_t ttl)
|
2006-05-29 19:21:34 -06:00
|
|
|
{
|
|
|
|
struct recent_entry *e;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
if (t->entries >= ip_list_tot) {
|
|
|
|
e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
|
|
|
|
recent_entry_remove(t, e);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-05-29 19:21:34 -06:00
|
|
|
e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
|
|
|
|
GFP_ATOMIC);
|
|
|
|
if (e == NULL)
|
|
|
|
return NULL;
|
2008-10-08 03:35:00 -06:00
|
|
|
memcpy(&e->addr, addr, sizeof(e->addr));
|
2006-05-29 19:21:34 -06:00
|
|
|
e->ttl = ttl;
|
|
|
|
e->stamps[0] = jiffies;
|
|
|
|
e->nstamps = 1;
|
|
|
|
e->index = 1;
|
2008-10-08 03:35:00 -06:00
|
|
|
e->family = family;
|
2008-10-08 03:35:01 -06:00
|
|
|
if (family == NFPROTO_IPV4)
|
2008-10-08 03:35:00 -06:00
|
|
|
list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
|
|
|
|
else
|
|
|
|
list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
|
2006-05-29 19:21:34 -06:00
|
|
|
list_add_tail(&e->lru_list, &t->lru_list);
|
|
|
|
t->entries++;
|
|
|
|
return e;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
|
|
|
|
{
|
2010-02-23 06:55:21 -07:00
|
|
|
e->index %= ip_pkt_list_tot;
|
2006-05-29 19:21:34 -06:00
|
|
|
e->stamps[e->index++] = jiffies;
|
|
|
|
if (e->index > e->nstamps)
|
|
|
|
e->nstamps = e->index;
|
|
|
|
list_move_tail(&e->lru_list, &t->lru_list);
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-01-18 00:31:00 -07:00
|
|
|
static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
|
|
|
|
const char *name)
|
2006-05-29 19:21:34 -06:00
|
|
|
{
|
|
|
|
struct recent_table *t;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-01-18 00:31:00 -07:00
|
|
|
list_for_each_entry(t, &recent_net->tables, list)
|
2006-05-29 19:21:34 -06:00
|
|
|
if (!strcmp(t->name, name))
|
|
|
|
return t;
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static void recent_table_flush(struct recent_table *t)
|
|
|
|
{
|
|
|
|
struct recent_entry *e, *next;
|
|
|
|
unsigned int i;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-07-07 23:19:08 -06:00
|
|
|
for (i = 0; i < ip_list_hash_size; i++)
|
2006-05-29 19:21:34 -06:00
|
|
|
list_for_each_entry_safe(e, next, &t->iphash[i], list)
|
|
|
|
recent_entry_remove(t, e);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-07-07 23:15:35 -06:00
|
|
|
static bool
|
2008-10-08 03:35:18 -06:00
|
|
|
recent_mt(const struct sk_buff *skb, const struct xt_match_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2010-01-18 00:31:00 -07:00
|
|
|
struct net *net = dev_net(par->in ? par->in : par->out);
|
|
|
|
struct recent_net *recent_net = recent_pernet(net);
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct xt_recent_mtinfo *info = par->matchinfo;
|
2006-05-29 19:21:34 -06:00
|
|
|
struct recent_table *t;
|
|
|
|
struct recent_entry *e;
|
2008-10-08 03:35:00 -06:00
|
|
|
union nf_inet_addr addr = {};
|
2006-05-29 19:21:34 -06:00
|
|
|
u_int8_t ttl;
|
2007-07-07 23:15:35 -06:00
|
|
|
bool ret = info->invert;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-08 03:35:18 -06:00
|
|
|
if (par->match->family == NFPROTO_IPV4) {
|
2008-10-08 03:35:00 -06:00
|
|
|
const struct iphdr *iph = ip_hdr(skb);
|
|
|
|
|
|
|
|
if (info->side == XT_RECENT_DEST)
|
|
|
|
addr.ip = iph->daddr;
|
|
|
|
else
|
|
|
|
addr.ip = iph->saddr;
|
|
|
|
|
|
|
|
ttl = iph->ttl;
|
|
|
|
} else {
|
|
|
|
const struct ipv6hdr *iph = ipv6_hdr(skb);
|
|
|
|
|
|
|
|
if (info->side == XT_RECENT_DEST)
|
|
|
|
memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
|
|
|
|
else
|
|
|
|
memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
|
|
|
|
|
|
|
|
ttl = iph->hop_limit;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
/* use TTL as seen before forwarding */
|
2008-10-08 03:35:18 -06:00
|
|
|
if (par->out != NULL && skb->sk == NULL)
|
2006-05-29 19:21:34 -06:00
|
|
|
ttl++;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
spin_lock_bh(&recent_lock);
|
2010-01-18 00:31:00 -07:00
|
|
|
t = recent_table_lookup(recent_net, info->name);
|
2008-10-08 03:35:18 -06:00
|
|
|
e = recent_entry_lookup(t, &addr, par->match->family,
|
2008-10-08 03:35:00 -06:00
|
|
|
(info->check_set & XT_RECENT_TTL) ? ttl : 0);
|
2006-05-29 19:21:34 -06:00
|
|
|
if (e == NULL) {
|
2008-10-08 03:35:00 -06:00
|
|
|
if (!(info->check_set & XT_RECENT_SET))
|
2006-05-29 19:21:34 -06:00
|
|
|
goto out;
|
2008-10-08 03:35:18 -06:00
|
|
|
e = recent_entry_init(t, &addr, par->match->family, ttl);
|
2006-05-29 19:21:34 -06:00
|
|
|
if (e == NULL)
|
2008-10-08 03:35:18 -06:00
|
|
|
*par->hotdrop = true;
|
2007-07-07 23:15:35 -06:00
|
|
|
ret = !ret;
|
2006-05-29 19:21:34 -06:00
|
|
|
goto out;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:00 -06:00
|
|
|
if (info->check_set & XT_RECENT_SET)
|
2007-07-07 23:15:35 -06:00
|
|
|
ret = !ret;
|
2008-10-08 03:35:00 -06:00
|
|
|
else if (info->check_set & XT_RECENT_REMOVE) {
|
2006-05-29 19:21:34 -06:00
|
|
|
recent_entry_remove(t, e);
|
2007-07-07 23:15:35 -06:00
|
|
|
ret = !ret;
|
2008-10-08 03:35:00 -06:00
|
|
|
} else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
|
2008-01-31 05:09:46 -07:00
|
|
|
unsigned long time = jiffies - info->seconds * HZ;
|
2006-05-29 19:21:34 -06:00
|
|
|
unsigned int i, hits = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < e->nstamps; i++) {
|
2008-01-31 05:09:46 -07:00
|
|
|
if (info->seconds && time_after(time, e->stamps[i]))
|
2006-05-29 19:21:34 -06:00
|
|
|
continue;
|
2010-03-22 11:25:20 -06:00
|
|
|
if (!info->hit_count || ++hits >= info->hit_count) {
|
2007-07-07 23:15:35 -06:00
|
|
|
ret = !ret;
|
2006-05-29 19:21:34 -06:00
|
|
|
break;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:00 -06:00
|
|
|
if (info->check_set & XT_RECENT_SET ||
|
|
|
|
(info->check_set & XT_RECENT_UPDATE && ret)) {
|
2006-05-29 19:21:34 -06:00
|
|
|
recent_entry_update(t, e);
|
|
|
|
e->ttl = ttl;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
spin_unlock_bh(&recent_lock);
|
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:18 -06:00
|
|
|
static bool recent_mt_check(const struct xt_mtchk_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2010-01-18 00:31:00 -07:00
|
|
|
struct recent_net *recent_net = recent_pernet(par->net);
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct xt_recent_mtinfo *info = par->matchinfo;
|
2006-05-29 19:21:34 -06:00
|
|
|
struct recent_table *t;
|
2008-11-20 01:57:01 -07:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct proc_dir_entry *pde;
|
|
|
|
#endif
|
2006-05-29 19:21:34 -06:00
|
|
|
unsigned i;
|
2007-07-07 23:16:00 -06:00
|
|
|
bool ret = false;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-01-04 08:28:38 -07:00
|
|
|
if (unlikely(!hash_rnd_inited)) {
|
|
|
|
get_random_bytes(&hash_rnd, sizeof(hash_rnd));
|
|
|
|
hash_rnd_inited = true;
|
|
|
|
}
|
2006-05-29 19:21:34 -06:00
|
|
|
if (hweight8(info->check_set &
|
2008-10-08 03:35:00 -06:00
|
|
|
(XT_RECENT_SET | XT_RECENT_REMOVE |
|
|
|
|
XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
|
2007-07-07 23:16:00 -06:00
|
|
|
return false;
|
2008-10-08 03:35:00 -06:00
|
|
|
if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
|
2006-05-29 19:21:34 -06:00
|
|
|
(info->seconds || info->hit_count))
|
2007-07-07 23:16:00 -06:00
|
|
|
return false;
|
2010-02-15 08:31:35 -07:00
|
|
|
if (info->hit_count > ip_pkt_list_tot) {
|
|
|
|
pr_info(KBUILD_MODNAME ": hitcount (%u) is larger than "
|
|
|
|
"packets to be remembered (%u)\n",
|
|
|
|
info->hit_count, ip_pkt_list_tot);
|
2008-03-20 16:07:10 -06:00
|
|
|
return false;
|
2010-02-15 08:31:35 -07:00
|
|
|
}
|
2006-05-29 19:21:34 -06:00
|
|
|
if (info->name[0] == '\0' ||
|
2008-10-08 03:35:00 -06:00
|
|
|
strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
|
2007-07-07 23:16:00 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-06-09 13:17:41 -06:00
|
|
|
mutex_lock(&recent_mutex);
|
2010-01-18 00:31:00 -07:00
|
|
|
t = recent_table_lookup(recent_net, info->name);
|
2006-05-29 19:21:34 -06:00
|
|
|
if (t != NULL) {
|
|
|
|
t->refcnt++;
|
2007-07-07 23:16:00 -06:00
|
|
|
ret = true;
|
2006-05-29 19:21:34 -06:00
|
|
|
goto out;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
|
2006-06-09 13:17:41 -06:00
|
|
|
GFP_KERNEL);
|
2006-05-29 19:21:34 -06:00
|
|
|
if (t == NULL)
|
|
|
|
goto out;
|
2006-06-09 13:18:17 -06:00
|
|
|
t->refcnt = 1;
|
2006-05-29 19:21:34 -06:00
|
|
|
strcpy(t->name, info->name);
|
|
|
|
INIT_LIST_HEAD(&t->lru_list);
|
|
|
|
for (i = 0; i < ip_list_hash_size; i++)
|
|
|
|
INIT_LIST_HEAD(&t->iphash[i]);
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2010-01-18 00:31:00 -07:00
|
|
|
pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
|
2008-10-20 04:33:49 -06:00
|
|
|
&recent_mt_fops, t);
|
2008-11-20 01:57:01 -07:00
|
|
|
if (pde == NULL) {
|
2006-05-29 19:21:34 -06:00
|
|
|
kfree(t);
|
|
|
|
goto out;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-11-20 01:57:01 -07:00
|
|
|
pde->uid = ip_list_uid;
|
|
|
|
pde->gid = ip_list_gid;
|
2008-10-08 03:35:00 -06:00
|
|
|
#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
|
2010-01-18 00:31:00 -07:00
|
|
|
pde = proc_create_data(t->name, ip_list_perms, recent_net->ipt_recent,
|
2008-10-20 04:33:49 -06:00
|
|
|
&recent_old_fops, t);
|
2008-11-20 01:57:01 -07:00
|
|
|
if (pde == NULL) {
|
2010-01-18 00:31:00 -07:00
|
|
|
remove_proc_entry(t->name, recent_net->xt_recent);
|
2008-10-08 03:35:00 -06:00
|
|
|
kfree(t);
|
|
|
|
goto out;
|
|
|
|
}
|
2008-11-20 01:57:01 -07:00
|
|
|
pde->uid = ip_list_uid;
|
|
|
|
pde->gid = ip_list_gid;
|
2008-10-08 03:35:00 -06:00
|
|
|
#endif
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif
|
2006-06-09 13:17:41 -06:00
|
|
|
spin_lock_bh(&recent_lock);
|
2010-01-18 00:31:00 -07:00
|
|
|
list_add_tail(&t->list, &recent_net->tables);
|
2006-06-09 13:17:41 -06:00
|
|
|
spin_unlock_bh(&recent_lock);
|
2007-07-07 23:16:00 -06:00
|
|
|
ret = true;
|
2006-05-29 19:21:34 -06:00
|
|
|
out:
|
2006-06-09 13:17:41 -06:00
|
|
|
mutex_unlock(&recent_mutex);
|
2006-05-29 19:21:34 -06:00
|
|
|
return ret;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-08 03:35:19 -06:00
|
|
|
static void recent_mt_destroy(const struct xt_mtdtor_param *par)
|
2006-05-29 19:21:34 -06:00
|
|
|
{
|
2010-01-18 00:31:00 -07:00
|
|
|
struct recent_net *recent_net = recent_pernet(par->net);
|
2008-10-08 03:35:19 -06:00
|
|
|
const struct xt_recent_mtinfo *info = par->matchinfo;
|
2006-05-29 19:21:34 -06:00
|
|
|
struct recent_table *t;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-06-09 13:17:41 -06:00
|
|
|
mutex_lock(&recent_mutex);
|
2010-01-18 00:31:00 -07:00
|
|
|
t = recent_table_lookup(recent_net, info->name);
|
2006-05-29 19:21:34 -06:00
|
|
|
if (--t->refcnt == 0) {
|
2006-06-09 13:17:41 -06:00
|
|
|
spin_lock_bh(&recent_lock);
|
2006-05-29 19:21:34 -06:00
|
|
|
list_del(&t->list);
|
2006-06-09 13:17:41 -06:00
|
|
|
spin_unlock_bh(&recent_lock);
|
2006-05-29 19:21:34 -06:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-10-08 03:35:00 -06:00
|
|
|
#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
|
2010-01-18 00:31:00 -07:00
|
|
|
remove_proc_entry(t->name, recent_net->ipt_recent);
|
2008-10-08 03:35:00 -06:00
|
|
|
#endif
|
2010-01-18 00:31:00 -07:00
|
|
|
remove_proc_entry(t->name, recent_net->xt_recent);
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif
|
2008-07-31 01:38:31 -06:00
|
|
|
recent_table_flush(t);
|
2006-05-29 19:21:34 -06:00
|
|
|
kfree(t);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-06-09 13:17:41 -06:00
|
|
|
mutex_unlock(&recent_mutex);
|
2006-05-29 19:21:34 -06:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct recent_iter_state {
|
2008-10-08 03:35:00 -06:00
|
|
|
const struct recent_table *table;
|
2006-05-29 19:21:34 -06:00
|
|
|
unsigned int bucket;
|
|
|
|
};
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
|
2008-01-31 05:09:46 -07:00
|
|
|
__acquires(recent_lock)
|
2006-05-29 19:21:34 -06:00
|
|
|
{
|
|
|
|
struct recent_iter_state *st = seq->private;
|
2007-07-07 23:16:55 -06:00
|
|
|
const struct recent_table *t = st->table;
|
2006-05-29 19:21:34 -06:00
|
|
|
struct recent_entry *e;
|
|
|
|
loff_t p = *pos;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
spin_lock_bh(&recent_lock);
|
|
|
|
|
2007-07-07 23:19:08 -06:00
|
|
|
for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
|
|
|
|
list_for_each_entry(e, &t->iphash[st->bucket], list)
|
2006-05-29 19:21:34 -06:00
|
|
|
if (p-- == 0)
|
|
|
|
return e;
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct recent_iter_state *st = seq->private;
|
2008-04-14 01:56:05 -06:00
|
|
|
const struct recent_table *t = st->table;
|
2008-10-08 03:35:00 -06:00
|
|
|
const struct recent_entry *e = v;
|
|
|
|
const struct list_head *head = e->list.next;
|
2006-05-29 19:21:34 -06:00
|
|
|
|
|
|
|
while (head == &t->iphash[st->bucket]) {
|
|
|
|
if (++st->bucket >= ip_list_hash_size)
|
|
|
|
return NULL;
|
|
|
|
head = t->iphash[st->bucket].next;
|
|
|
|
}
|
|
|
|
(*pos)++;
|
|
|
|
return list_entry(head, struct recent_entry, list);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static void recent_seq_stop(struct seq_file *s, void *v)
|
2008-01-31 05:09:46 -07:00
|
|
|
__releases(recent_lock)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2006-05-29 19:21:34 -06:00
|
|
|
spin_unlock_bh(&recent_lock);
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static int recent_seq_show(struct seq_file *seq, void *v)
|
|
|
|
{
|
2008-04-14 01:56:05 -06:00
|
|
|
const struct recent_entry *e = v;
|
2006-05-29 19:21:34 -06:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
i = (e->index - 1) % ip_pkt_list_tot;
|
2008-10-08 03:35:01 -06:00
|
|
|
if (e->family == NFPROTO_IPV4)
|
2008-10-31 01:54:29 -06:00
|
|
|
seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
|
|
|
|
&e->addr.ip, e->ttl, e->stamps[i], e->index);
|
2008-10-08 03:35:00 -06:00
|
|
|
else
|
2008-10-29 13:52:50 -06:00
|
|
|
seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
|
2008-10-28 17:08:13 -06:00
|
|
|
&e->addr.in6, e->ttl, e->stamps[i], e->index);
|
2006-05-29 19:21:34 -06:00
|
|
|
for (i = 0; i < e->nstamps; i++)
|
|
|
|
seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
|
|
|
|
seq_printf(seq, "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-07-11 00:07:31 -06:00
|
|
|
static const struct seq_operations recent_seq_ops = {
|
2006-05-29 19:21:34 -06:00
|
|
|
.start = recent_seq_start,
|
|
|
|
.next = recent_seq_next,
|
|
|
|
.stop = recent_seq_stop,
|
|
|
|
.show = recent_seq_show,
|
|
|
|
};
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
static int recent_seq_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct proc_dir_entry *pde = PDE(inode);
|
|
|
|
struct recent_iter_state *st;
|
|
|
|
|
2007-10-10 03:29:58 -06:00
|
|
|
st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
|
2006-05-29 19:21:34 -06:00
|
|
|
if (st == NULL)
|
|
|
|
return -ENOMEM;
|
2007-08-07 19:10:54 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
st->table = pde->data;
|
2007-10-10 03:29:58 -06:00
|
|
|
return 0;
|
2006-05-29 19:21:34 -06:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-08 03:35:00 -06:00
|
|
|
#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
|
|
|
|
static int recent_old_seq_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
static bool warned_of_old;
|
|
|
|
|
|
|
|
if (unlikely(!warned_of_old)) {
|
|
|
|
printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent"
|
|
|
|
" is deprecated; use /proc/net/xt_recent.\n");
|
|
|
|
warned_of_old = true;
|
|
|
|
}
|
|
|
|
return recent_seq_open(inode, filp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t recent_old_proc_write(struct file *file,
|
|
|
|
const char __user *input,
|
|
|
|
size_t size, loff_t *loff)
|
2006-05-29 19:21:34 -06:00
|
|
|
{
|
2008-04-14 01:56:05 -06:00
|
|
|
const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
|
2006-05-29 19:21:34 -06:00
|
|
|
struct recent_table *t = pde->data;
|
|
|
|
struct recent_entry *e;
|
|
|
|
char buf[sizeof("+255.255.255.255")], *c = buf;
|
2009-04-24 09:05:21 -06:00
|
|
|
union nf_inet_addr addr = {};
|
2006-05-29 19:21:34 -06:00
|
|
|
int add;
|
|
|
|
|
|
|
|
if (size > sizeof(buf))
|
|
|
|
size = sizeof(buf);
|
|
|
|
if (copy_from_user(buf, input, size))
|
|
|
|
return -EFAULT;
|
2008-10-08 03:35:00 -06:00
|
|
|
|
2009-12-14 19:01:06 -07:00
|
|
|
c = skip_spaces(c);
|
2006-05-29 19:21:34 -06:00
|
|
|
|
|
|
|
if (size - (c - buf) < 5)
|
|
|
|
return c - buf;
|
|
|
|
if (!strncmp(c, "clear", 5)) {
|
|
|
|
c += 5;
|
|
|
|
spin_lock_bh(&recent_lock);
|
|
|
|
recent_table_flush(t);
|
2005-04-16 16:20:36 -06:00
|
|
|
spin_unlock_bh(&recent_lock);
|
2006-05-29 19:21:34 -06:00
|
|
|
return c - buf;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
switch (*c) {
|
|
|
|
case '-':
|
|
|
|
add = 0;
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
c++;
|
|
|
|
default:
|
|
|
|
add = 1;
|
|
|
|
break;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2009-04-24 09:05:21 -06:00
|
|
|
addr.ip = in_aton(c);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
spin_lock_bh(&recent_lock);
|
2009-04-24 09:05:21 -06:00
|
|
|
e = recent_entry_lookup(t, &addr, NFPROTO_IPV4, 0);
|
2006-05-29 19:21:34 -06:00
|
|
|
if (e == NULL) {
|
|
|
|
if (add)
|
2009-04-24 09:05:21 -06:00
|
|
|
recent_entry_init(t, &addr, NFPROTO_IPV4, 0);
|
2006-05-29 19:21:34 -06:00
|
|
|
} else {
|
|
|
|
if (add)
|
|
|
|
recent_entry_update(t, e);
|
|
|
|
else
|
|
|
|
recent_entry_remove(t, e);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
spin_unlock_bh(&recent_lock);
|
2006-05-29 19:21:34 -06:00
|
|
|
return size;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-08 03:35:00 -06:00
|
|
|
static const struct file_operations recent_old_fops = {
|
|
|
|
.open = recent_old_seq_open,
|
2006-05-29 19:21:34 -06:00
|
|
|
.read = seq_read,
|
2008-10-08 03:35:00 -06:00
|
|
|
.write = recent_old_proc_write,
|
2006-05-29 19:21:34 -06:00
|
|
|
.release = seq_release_private,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
2008-10-08 03:35:00 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
recent_mt_proc_write(struct file *file, const char __user *input,
|
|
|
|
size_t size, loff_t *loff)
|
|
|
|
{
|
|
|
|
const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
|
|
|
|
struct recent_table *t = pde->data;
|
|
|
|
struct recent_entry *e;
|
|
|
|
char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
|
|
|
|
const char *c = buf;
|
netfilter: xt_recent: fix proc-file addition/removal of IPv4 addresses
Fix regression introduded by commit 079aa88 (netfilter: xt_recent: IPv6 support):
From http://bugzilla.kernel.org/show_bug.cgi?id=12753:
Problem Description:
An uninitialized buffer causes IPv4 addresses added manually (via the +IP
command to the proc interface) to never match any packets. Similarly, the -IP
command fails to remove IPv4 addresses.
Details:
In the function recent_entry_lookup, the xt_recent module does comparisons of
the entire nf_inet_addr union value, both for IPv4 and IPv6 addresses. For
addresses initialized from actual packets the remaining 12 bytes not occupied
by the IPv4 are zeroed so this works correctly. However when setting the
nf_inet_addr addr variable in the recent_mt_proc_write function, only the IPv4
bytes are initialized and the remaining 12 bytes contain garbage.
Hence addresses added in this way never match any packets, unless these
uninitialized 12 bytes happened to be zero by coincidence. Similarly, addresses
cannot consistently be removed using the proc interface due to mismatch of the
garbage bytes (although it will sometimes work to remove an address that was
added manually).
Reading the /proc/net/xt_recent/ entries hides this problem because this only
uses the first 4 bytes when displaying IPv4 addresses.
Steps to reproduce:
$ iptables -I INPUT -m recent --rcheck -j LOG
$ echo +169.254.156.239 > /proc/net/xt_recent/DEFAULT
$ cat /proc/net/xt_recent/DEFAULT
src=169.254.156.239 ttl: 0 last_seen: 119910 oldest_pkt: 1 119910
[At this point no packets from 169.254.156.239 are being logged.]
$ iptables -I INPUT -s 169.254.156.239 -m recent --set
$ cat /proc/net/xt_recent/DEFAULT
src=169.254.156.239 ttl: 0 last_seen: 119910 oldest_pkt: 1 119910
src=169.254.156.239 ttl: 255 last_seen: 126184 oldest_pkt: 4 125434, 125684, 125934, 126184
[At this point, adding the address via an iptables rule, packets are being
logged correctly.]
$ echo -169.254.156.239 > /proc/net/xt_recent/DEFAULT
$ cat /proc/net/xt_recent/DEFAULT
src=169.254.156.239 ttl: 0 last_seen: 119910 oldest_pkt: 1 119910
src=169.254.156.239 ttl: 255 last_seen: 126992 oldest_pkt: 10 125434, 125684, 125934, 126184, 126434, 126684, 126934, 126991, 126991, 126992
$ echo -169.254.156.239 > /proc/net/xt_recent/DEFAULT
$ cat /proc/net/xt_recent/DEFAULT
src=169.254.156.239 ttl: 0 last_seen: 119910 oldest_pkt: 1 119910
src=169.254.156.239 ttl: 255 last_seen: 126992 oldest_pkt: 10 125434, 125684, 125934, 126184, 126434, 126684, 126934, 126991, 126991, 126992
[Removing the address via /proc interface failed evidently.]
Possible solutions:
- initialize the addr variable in recent_mt_proc_write
- compare only 4 bytes for IPv4 addresses in recent_entry_lookup
Signed-off-by: Patrick McHardy <kaber@trash.net>
2009-02-24 06:53:12 -07:00
|
|
|
union nf_inet_addr addr = {};
|
2008-10-08 03:35:00 -06:00
|
|
|
u_int16_t family;
|
|
|
|
bool add, succ;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
if (size > sizeof(buf))
|
|
|
|
size = sizeof(buf);
|
|
|
|
if (copy_from_user(buf, input, size) != 0)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/* Strict protocol! */
|
|
|
|
if (*loff != 0)
|
|
|
|
return -ESPIPE;
|
|
|
|
switch (*c) {
|
|
|
|
case '/': /* flush table */
|
|
|
|
spin_lock_bh(&recent_lock);
|
|
|
|
recent_table_flush(t);
|
|
|
|
spin_unlock_bh(&recent_lock);
|
|
|
|
return size;
|
|
|
|
case '-': /* remove address */
|
|
|
|
add = false;
|
|
|
|
break;
|
|
|
|
case '+': /* add address */
|
|
|
|
add = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
++c;
|
|
|
|
--size;
|
|
|
|
if (strnchr(c, size, ':') != NULL) {
|
2008-10-08 03:35:01 -06:00
|
|
|
family = NFPROTO_IPV6;
|
2008-10-08 03:35:00 -06:00
|
|
|
succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
|
|
|
|
} else {
|
2008-10-08 03:35:01 -06:00
|
|
|
family = NFPROTO_IPV4;
|
2008-10-08 03:35:00 -06:00
|
|
|
succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!succ) {
|
|
|
|
printk(KERN_INFO KBUILD_MODNAME ": illegal address written "
|
|
|
|
"to procfs\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_bh(&recent_lock);
|
|
|
|
e = recent_entry_lookup(t, &addr, family, 0);
|
|
|
|
if (e == NULL) {
|
|
|
|
if (add)
|
|
|
|
recent_entry_init(t, &addr, family, 0);
|
|
|
|
} else {
|
|
|
|
if (add)
|
|
|
|
recent_entry_update(t, e);
|
|
|
|
else
|
|
|
|
recent_entry_remove(t, e);
|
|
|
|
}
|
|
|
|
spin_unlock_bh(&recent_lock);
|
|
|
|
/* Note we removed one above */
|
|
|
|
*loff += size + 1;
|
|
|
|
return size + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations recent_mt_fops = {
|
|
|
|
.open = recent_seq_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.write = recent_mt_proc_write,
|
|
|
|
.release = seq_release_private,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
2010-01-18 00:31:00 -07:00
|
|
|
|
|
|
|
static int __net_init recent_proc_net_init(struct net *net)
|
|
|
|
{
|
|
|
|
struct recent_net *recent_net = recent_pernet(net);
|
|
|
|
|
|
|
|
recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
|
|
|
|
if (!recent_net->xt_recent)
|
|
|
|
return -ENOMEM;
|
|
|
|
#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
|
|
|
|
recent_net->ipt_recent = proc_mkdir("ipt_recent", net->proc_net);
|
|
|
|
if (!recent_net->ipt_recent) {
|
|
|
|
proc_net_remove(net, "xt_recent");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __net_exit recent_proc_net_exit(struct net *net)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT
|
|
|
|
proc_net_remove(net, "ipt_recent");
|
|
|
|
#endif
|
|
|
|
proc_net_remove(net, "xt_recent");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int recent_proc_net_init(struct net *net)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void recent_proc_net_exit(struct net *net)
|
|
|
|
{
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif /* CONFIG_PROC_FS */
|
|
|
|
|
2010-01-18 00:31:00 -07:00
|
|
|
static int __net_init recent_net_init(struct net *net)
|
|
|
|
{
|
|
|
|
struct recent_net *recent_net = recent_pernet(net);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&recent_net->tables);
|
|
|
|
return recent_proc_net_init(net);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __net_exit recent_net_exit(struct net *net)
|
|
|
|
{
|
|
|
|
struct recent_net *recent_net = recent_pernet(net);
|
|
|
|
|
|
|
|
BUG_ON(!list_empty(&recent_net->tables));
|
|
|
|
recent_proc_net_exit(net);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations recent_net_ops = {
|
|
|
|
.init = recent_net_init,
|
|
|
|
.exit = recent_net_exit,
|
|
|
|
.id = &recent_net_id,
|
|
|
|
.size = sizeof(struct recent_net),
|
|
|
|
};
|
|
|
|
|
2008-10-08 03:35:00 -06:00
|
|
|
static struct xt_match recent_mt_reg[] __read_mostly = {
|
|
|
|
{
|
|
|
|
.name = "recent",
|
|
|
|
.revision = 0,
|
2008-10-08 03:35:01 -06:00
|
|
|
.family = NFPROTO_IPV4,
|
2008-10-08 03:35:00 -06:00
|
|
|
.match = recent_mt,
|
|
|
|
.matchsize = sizeof(struct xt_recent_mtinfo),
|
|
|
|
.checkentry = recent_mt_check,
|
|
|
|
.destroy = recent_mt_destroy,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "recent",
|
|
|
|
.revision = 0,
|
2008-10-08 03:35:01 -06:00
|
|
|
.family = NFPROTO_IPV6,
|
2008-10-08 03:35:00 -06:00
|
|
|
.match = recent_mt,
|
|
|
|
.matchsize = sizeof(struct xt_recent_mtinfo),
|
|
|
|
.checkentry = recent_mt_check,
|
|
|
|
.destroy = recent_mt_destroy,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static int __init recent_mt_init(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2006-05-29 19:21:34 -06:00
|
|
|
int err;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-05-29 19:21:34 -06:00
|
|
|
if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
|
|
|
|
return -EINVAL;
|
|
|
|
ip_list_hash_size = 1 << fls(ip_list_tot);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-01-18 00:31:00 -07:00
|
|
|
err = register_pernet_subsys(&recent_net_ops);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (err)
|
2006-05-29 19:21:34 -06:00
|
|
|
return err;
|
2010-01-18 00:31:00 -07:00
|
|
|
err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
|
|
|
|
if (err)
|
|
|
|
unregister_pernet_subsys(&recent_net_ops);
|
2005-04-16 16:20:36 -06:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static void __exit recent_mt_exit(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:00 -06:00
|
|
|
xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
|
2010-01-18 00:31:00 -07:00
|
|
|
unregister_pernet_subsys(&recent_net_ops);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
module_init(recent_mt_init);
|
|
|
|
module_exit(recent_mt_exit);
|