blob: 11de55e7a868950c6b9ad5bbfa0a0561eb8787d7 [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>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040019#include <linux/module.h>
Patrick McHardyf3389802006-05-29 18:21:00 -070020
Jan Engelhardtacc738f2009-03-16 15:35:29 +010021struct xt_statistic_priv {
Eric Dumazetfabf3a82010-06-01 12:00:41 +020022 atomic_t count;
23} ____cacheline_aligned_in_smp;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010024
Patrick McHardyf3389802006-05-29 18:21:00 -070025MODULE_LICENSE("GPL");
26MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080027MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)");
Patrick McHardyf3389802006-05-29 18:21:00 -070028MODULE_ALIAS("ipt_statistic");
29MODULE_ALIAS("ip6t_statistic");
30
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070031static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020032statistic_mt(const struct sk_buff *skb, struct xt_action_param *par)
Patrick McHardyf3389802006-05-29 18:21:00 -070033{
Jan Engelhardtacc738f2009-03-16 15:35:29 +010034 const struct xt_statistic_info *info = par->matchinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070035 bool ret = info->flags & XT_STATISTIC_INVERT;
Eric Dumazetfabf3a82010-06-01 12:00:41 +020036 int nval, oval;
Patrick McHardyf3389802006-05-29 18:21:00 -070037
38 switch (info->mode) {
39 case XT_STATISTIC_MODE_RANDOM:
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -050040 if ((prandom_u32() & 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:
Eric Dumazetfabf3a82010-06-01 12:00:41 +020044 do {
45 oval = atomic_read(&info->master->count);
46 nval = (oval == info->u.nth.every) ? 0 : oval + 1;
47 } while (atomic_cmpxchg(&info->master->count, oval, nval) != oval);
48 if (nval == 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070049 ret = !ret;
Patrick McHardyf3389802006-05-29 18:21:00 -070050 break;
51 }
52
53 return ret;
54}
55
Jan Engelhardtb0f38452010-03-19 17:16:42 +010056static int 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 Engelhardtbd414ee2010-03-23 16:35:56 +010062 return -EINVAL;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010063
64 info->master = kzalloc(sizeof(*info->master), GFP_KERNEL);
Jan Engelhardt85bc3f32010-03-18 00:27:03 +010065 if (info->master == NULL)
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010066 return -ENOMEM;
Eric Dumazetfabf3a82010-06-01 12:00:41 +020067 atomic_set(&info->master->count, info->u.nth.count);
Jan Engelhardtacc738f2009-03-16 15:35:29 +010068
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010069 return 0;
Patrick McHardyf3389802006-05-29 18:21:00 -070070}
71
Jan Engelhardtacc738f2009-03-16 15:35:29 +010072static void statistic_mt_destroy(const struct xt_mtdtor_param *par)
73{
74 const struct xt_statistic_info *info = par->matchinfo;
75
76 kfree(info->master);
77}
78
Jan Engelhardt55b69e92008-10-08 11:35:01 +020079static struct xt_match xt_statistic_mt_reg __read_mostly = {
80 .name = "statistic",
81 .revision = 0,
82 .family = NFPROTO_UNSPEC,
83 .match = statistic_mt,
84 .checkentry = statistic_mt_check,
Jan Engelhardtacc738f2009-03-16 15:35:29 +010085 .destroy = statistic_mt_destroy,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020086 .matchsize = sizeof(struct xt_statistic_info),
87 .me = THIS_MODULE,
Patrick McHardyf3389802006-05-29 18:21:00 -070088};
89
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080090static int __init statistic_mt_init(void)
Patrick McHardyf3389802006-05-29 18:21:00 -070091{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020092 return xt_register_match(&xt_statistic_mt_reg);
Patrick McHardyf3389802006-05-29 18:21:00 -070093}
94
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080095static void __exit statistic_mt_exit(void)
Patrick McHardyf3389802006-05-29 18:21:00 -070096{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020097 xt_unregister_match(&xt_statistic_mt_reg);
Patrick McHardyf3389802006-05-29 18:21:00 -070098}
99
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800100module_init(statistic_mt_init);
101module_exit(statistic_mt_exit);