blob: 203e24ae472c2a1bd0f67eaf2ce92639a6aebca4 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Patrick McHardyf3389802006-05-29 18:21:00 -07002/*
3 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
4 *
Patrick McHardyf3389802006-05-29 18:21:00 -07005 * Based on ipt_random and ipt_nth by Fabrice MARIE <fabrice@netfilter.org>.
6 */
7
8#include <linux/init.h>
9#include <linux/spinlock.h>
10#include <linux/skbuff.h>
11#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Patrick McHardyf3389802006-05-29 18:21:00 -070013
14#include <linux/netfilter/xt_statistic.h>
15#include <linux/netfilter/x_tables.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040016#include <linux/module.h>
Patrick McHardyf3389802006-05-29 18:21:00 -070017
Jan Engelhardtacc738f2009-03-16 15:35:29 +010018struct xt_statistic_priv {
Eric Dumazetfabf3a82010-06-01 12:00:41 +020019 atomic_t count;
20} ____cacheline_aligned_in_smp;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010021
Patrick McHardyf3389802006-05-29 18:21:00 -070022MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080024MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)");
Patrick McHardyf3389802006-05-29 18:21:00 -070025MODULE_ALIAS("ipt_statistic");
26MODULE_ALIAS("ip6t_statistic");
27
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070028static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020029statistic_mt(const struct sk_buff *skb, struct xt_action_param *par)
Patrick McHardyf3389802006-05-29 18:21:00 -070030{
Jan Engelhardtacc738f2009-03-16 15:35:29 +010031 const struct xt_statistic_info *info = par->matchinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070032 bool ret = info->flags & XT_STATISTIC_INVERT;
Eric Dumazetfabf3a82010-06-01 12:00:41 +020033 int nval, oval;
Patrick McHardyf3389802006-05-29 18:21:00 -070034
35 switch (info->mode) {
36 case XT_STATISTIC_MODE_RANDOM:
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -050037 if ((prandom_u32() & 0x7FFFFFFF) < info->u.random.probability)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070038 ret = !ret;
Patrick McHardyf3389802006-05-29 18:21:00 -070039 break;
40 case XT_STATISTIC_MODE_NTH:
Eric Dumazetfabf3a82010-06-01 12:00:41 +020041 do {
42 oval = atomic_read(&info->master->count);
43 nval = (oval == info->u.nth.every) ? 0 : oval + 1;
44 } while (atomic_cmpxchg(&info->master->count, oval, nval) != oval);
45 if (nval == 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070046 ret = !ret;
Patrick McHardyf3389802006-05-29 18:21:00 -070047 break;
48 }
49
50 return ret;
51}
52
Jan Engelhardtb0f38452010-03-19 17:16:42 +010053static int statistic_mt_check(const struct xt_mtchk_param *par)
Patrick McHardyf3389802006-05-29 18:21:00 -070054{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020055 struct xt_statistic_info *info = par->matchinfo;
Patrick McHardyf3389802006-05-29 18:21:00 -070056
57 if (info->mode > XT_STATISTIC_MODE_MAX ||
58 info->flags & ~XT_STATISTIC_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010059 return -EINVAL;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010060
61 info->master = kzalloc(sizeof(*info->master), GFP_KERNEL);
Jan Engelhardt85bc3f32010-03-18 00:27:03 +010062 if (info->master == NULL)
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010063 return -ENOMEM;
Eric Dumazetfabf3a82010-06-01 12:00:41 +020064 atomic_set(&info->master->count, info->u.nth.count);
Jan Engelhardtacc738f2009-03-16 15:35:29 +010065
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010066 return 0;
Patrick McHardyf3389802006-05-29 18:21:00 -070067}
68
Jan Engelhardtacc738f2009-03-16 15:35:29 +010069static void statistic_mt_destroy(const struct xt_mtdtor_param *par)
70{
71 const struct xt_statistic_info *info = par->matchinfo;
72
73 kfree(info->master);
74}
75
Jan Engelhardt55b69e92008-10-08 11:35:01 +020076static struct xt_match xt_statistic_mt_reg __read_mostly = {
77 .name = "statistic",
78 .revision = 0,
79 .family = NFPROTO_UNSPEC,
80 .match = statistic_mt,
81 .checkentry = statistic_mt_check,
Jan Engelhardtacc738f2009-03-16 15:35:29 +010082 .destroy = statistic_mt_destroy,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020083 .matchsize = sizeof(struct xt_statistic_info),
Dmitry Vyukov1e98ffe2018-01-29 13:21:20 +010084 .usersize = offsetof(struct xt_statistic_info, master),
Jan Engelhardt55b69e92008-10-08 11:35:01 +020085 .me = THIS_MODULE,
Patrick McHardyf3389802006-05-29 18:21:00 -070086};
87
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080088static int __init statistic_mt_init(void)
Patrick McHardyf3389802006-05-29 18:21:00 -070089{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020090 return xt_register_match(&xt_statistic_mt_reg);
Patrick McHardyf3389802006-05-29 18:21:00 -070091}
92
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080093static void __exit statistic_mt_exit(void)
Patrick McHardyf3389802006-05-29 18:21:00 -070094{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020095 xt_unregister_match(&xt_statistic_mt_reg);
Patrick McHardyf3389802006-05-29 18:21:00 -070096}
97
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080098module_init(statistic_mt_init);
99module_exit(statistic_mt_exit);