2006-05-30 03:21:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Based on ipt_random and ipt_nth by Fabrice MARIE <fabrice@netfilter.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
|
|
|
|
#include <linux/netfilter/xt_statistic.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
|
2009-03-16 15:35:29 +01:00
|
|
|
struct xt_statistic_priv {
|
|
|
|
uint32_t count;
|
|
|
|
};
|
|
|
|
|
2006-05-30 03:21:00 +02:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
|
2008-01-15 08:42:28 +01:00
|
|
|
MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)");
|
2006-05-30 03:21:00 +02:00
|
|
|
MODULE_ALIAS("ipt_statistic");
|
|
|
|
MODULE_ALIAS("ip6t_statistic");
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(nth_lock);
|
|
|
|
|
2007-07-08 07:15:35 +02:00
|
|
|
static bool
|
2008-10-08 11:35:18 +02:00
|
|
|
statistic_mt(const struct sk_buff *skb, const struct xt_match_param *par)
|
2006-05-30 03:21:00 +02:00
|
|
|
{
|
2009-03-16 15:35:29 +01:00
|
|
|
const struct xt_statistic_info *info = par->matchinfo;
|
2007-07-08 07:15:35 +02:00
|
|
|
bool ret = info->flags & XT_STATISTIC_INVERT;
|
2006-05-30 03:21:00 +02:00
|
|
|
|
|
|
|
switch (info->mode) {
|
|
|
|
case XT_STATISTIC_MODE_RANDOM:
|
|
|
|
if ((net_random() & 0x7FFFFFFF) < info->u.random.probability)
|
2007-07-08 07:15:35 +02:00
|
|
|
ret = !ret;
|
2006-05-30 03:21:00 +02:00
|
|
|
break;
|
|
|
|
case XT_STATISTIC_MODE_NTH:
|
|
|
|
spin_lock_bh(&nth_lock);
|
2009-03-16 15:35:29 +01:00
|
|
|
if (info->master->count++ == info->u.nth.every) {
|
|
|
|
info->master->count = 0;
|
2007-07-08 07:15:35 +02:00
|
|
|
ret = !ret;
|
2006-05-30 03:21:00 +02:00
|
|
|
}
|
|
|
|
spin_unlock_bh(&nth_lock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:35:18 +02:00
|
|
|
static bool statistic_mt_check(const struct xt_mtchk_param *par)
|
2006-05-30 03:21:00 +02:00
|
|
|
{
|
2008-10-08 11:35:18 +02:00
|
|
|
struct xt_statistic_info *info = par->matchinfo;
|
2006-05-30 03:21:00 +02:00
|
|
|
|
|
|
|
if (info->mode > XT_STATISTIC_MODE_MAX ||
|
|
|
|
info->flags & ~XT_STATISTIC_MASK)
|
2007-07-08 07:16:00 +02:00
|
|
|
return false;
|
2009-03-16 15:35:29 +01:00
|
|
|
|
|
|
|
info->master = kzalloc(sizeof(*info->master), GFP_KERNEL);
|
2010-03-18 00:27:03 +01:00
|
|
|
if (info->master == NULL)
|
2009-03-16 15:35:29 +01:00
|
|
|
return false;
|
|
|
|
info->master->count = info->u.nth.count;
|
|
|
|
|
2007-07-08 07:16:00 +02:00
|
|
|
return true;
|
2006-05-30 03:21:00 +02:00
|
|
|
}
|
|
|
|
|
2009-03-16 15:35:29 +01:00
|
|
|
static void statistic_mt_destroy(const struct xt_mtdtor_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_statistic_info *info = par->matchinfo;
|
|
|
|
|
|
|
|
kfree(info->master);
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:35:01 +02:00
|
|
|
static struct xt_match xt_statistic_mt_reg __read_mostly = {
|
|
|
|
.name = "statistic",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.match = statistic_mt,
|
|
|
|
.checkentry = statistic_mt_check,
|
2009-03-16 15:35:29 +01:00
|
|
|
.destroy = statistic_mt_destroy,
|
2008-10-08 11:35:01 +02:00
|
|
|
.matchsize = sizeof(struct xt_statistic_info),
|
|
|
|
.me = THIS_MODULE,
|
2006-05-30 03:21:00 +02:00
|
|
|
};
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static int __init statistic_mt_init(void)
|
2006-05-30 03:21:00 +02:00
|
|
|
{
|
2008-10-08 11:35:01 +02:00
|
|
|
return xt_register_match(&xt_statistic_mt_reg);
|
2006-05-30 03:21:00 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static void __exit statistic_mt_exit(void)
|
2006-05-30 03:21:00 +02:00
|
|
|
{
|
2008-10-08 11:35:01 +02:00
|
|
|
xt_unregister_match(&xt_statistic_mt_reg);
|
2006-05-30 03:21:00 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
module_init(statistic_mt_init);
|
|
|
|
module_exit(statistic_mt_exit);
|