blob: 937ce0633e99c7c926144a882fea550d2b232a3a [file] [log] [blame]
Patrick McHardyf3389802006-05-29 18:21:00 -07001/*
2 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Based on ipt_random and ipt_nth by Fabrice MARIE <fabrice@netfilter.org>.
9 */
10
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/skbuff.h>
14#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Patrick McHardyf3389802006-05-29 18:21:00 -070016
17#include <linux/netfilter/xt_statistic.h>
18#include <linux/netfilter/x_tables.h>
19
Jan Engelhardtacc738f2009-03-16 15:35:29 +010020struct xt_statistic_priv {
21 uint32_t count;
22};
23
Patrick McHardyf3389802006-05-29 18:21:00 -070024MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080026MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)");
Patrick McHardyf3389802006-05-29 18:21:00 -070027MODULE_ALIAS("ipt_statistic");
28MODULE_ALIAS("ip6t_statistic");
29
30static DEFINE_SPINLOCK(nth_lock);
31
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070032static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020033statistic_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Patrick McHardyf3389802006-05-29 18:21:00 -070034{
Jan Engelhardtacc738f2009-03-16 15:35:29 +010035 const struct xt_statistic_info *info = par->matchinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070036 bool ret = info->flags & XT_STATISTIC_INVERT;
Patrick McHardyf3389802006-05-29 18:21:00 -070037
38 switch (info->mode) {
39 case XT_STATISTIC_MODE_RANDOM:
40 if ((net_random() & 0x7FFFFFFF) < info->u.random.probability)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 ret = !ret;
Patrick McHardyf3389802006-05-29 18:21:00 -070042 break;
43 case XT_STATISTIC_MODE_NTH:
Patrick McHardyf3389802006-05-29 18:21:00 -070044 spin_lock_bh(&nth_lock);
Jan Engelhardtacc738f2009-03-16 15:35:29 +010045 if (info->master->count++ == info->u.nth.every) {
46 info->master->count = 0;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070047 ret = !ret;
Patrick McHardyf3389802006-05-29 18:21:00 -070048 }
49 spin_unlock_bh(&nth_lock);
50 break;
51 }
52
53 return ret;
54}
55
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020056static bool statistic_mt_check(const struct xt_mtchk_param *par)
Patrick McHardyf3389802006-05-29 18:21:00 -070057{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020058 struct xt_statistic_info *info = par->matchinfo;
Patrick McHardyf3389802006-05-29 18:21:00 -070059
60 if (info->mode > XT_STATISTIC_MODE_MAX ||
61 info->flags & ~XT_STATISTIC_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070062 return false;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010063
64 info->master = kzalloc(sizeof(*info->master), GFP_KERNEL);
65 if (info->master == NULL) {
66 printk(KERN_ERR KBUILD_MODNAME ": Out of memory\n");
67 return false;
68 }
69 info->master->count = info->u.nth.count;
70
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070071 return true;
Patrick McHardyf3389802006-05-29 18:21:00 -070072}
73
Jan Engelhardtacc738f2009-03-16 15:35:29 +010074static void statistic_mt_destroy(const struct xt_mtdtor_param *par)
75{
76 const struct xt_statistic_info *info = par->matchinfo;
77
78 kfree(info->master);
79}
80
Jan Engelhardt55b69e92008-10-08 11:35:01 +020081static struct xt_match xt_statistic_mt_reg __read_mostly = {
82 .name = "statistic",
83 .revision = 0,
84 .family = NFPROTO_UNSPEC,
85 .match = statistic_mt,
86 .checkentry = statistic_mt_check,
Jan Engelhardtacc738f2009-03-16 15:35:29 +010087 .destroy = statistic_mt_destroy,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020088 .matchsize = sizeof(struct xt_statistic_info),
89 .me = THIS_MODULE,
Patrick McHardyf3389802006-05-29 18:21:00 -070090};
91
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080092static int __init statistic_mt_init(void)
Patrick McHardyf3389802006-05-29 18:21:00 -070093{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020094 return xt_register_match(&xt_statistic_mt_reg);
Patrick McHardyf3389802006-05-29 18:21:00 -070095}
96
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080097static void __exit statistic_mt_exit(void)
Patrick McHardyf3389802006-05-29 18:21:00 -070098{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020099 xt_unregister_match(&xt_statistic_mt_reg);
Patrick McHardyf3389802006-05-29 18:21:00 -0700100}
101
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800102module_init(statistic_mt_init);
103module_exit(statistic_mt_exit);