Blame view
net/netfilter/xt_statistic.c
2.53 KB
f3389805e [NETFILTER]: x_ta... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* * 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> |
5a0e3ad6a include cleanup: ... |
15 |
#include <linux/slab.h> |
f3389805e [NETFILTER]: x_ta... |
16 17 18 |
#include <linux/netfilter/xt_statistic.h> #include <linux/netfilter/x_tables.h> |
3a9a231d9 net: Fix files ex... |
19 |
#include <linux/module.h> |
f3389805e [NETFILTER]: x_ta... |
20 |
|
acc738fec netfilter: xtable... |
21 |
struct xt_statistic_priv { |
fabf3a85a netfilter: xt_sta... |
22 23 |
atomic_t count; } ____cacheline_aligned_in_smp; |
acc738fec netfilter: xtable... |
24 |
|
f3389805e [NETFILTER]: x_ta... |
25 26 |
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
2ae15b64e [NETFILTER]: Upda... |
27 |
MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)"); |
f3389805e [NETFILTER]: x_ta... |
28 29 |
MODULE_ALIAS("ipt_statistic"); MODULE_ALIAS("ip6t_statistic"); |
1d93a9cba [NETFILTER]: x_ta... |
30 |
static bool |
62fc80510 netfilter: xtable... |
31 |
statistic_mt(const struct sk_buff *skb, struct xt_action_param *par) |
f3389805e [NETFILTER]: x_ta... |
32 |
{ |
acc738fec netfilter: xtable... |
33 |
const struct xt_statistic_info *info = par->matchinfo; |
1d93a9cba [NETFILTER]: x_ta... |
34 |
bool ret = info->flags & XT_STATISTIC_INVERT; |
fabf3a85a netfilter: xt_sta... |
35 |
int nval, oval; |
f3389805e [NETFILTER]: x_ta... |
36 37 38 39 |
switch (info->mode) { case XT_STATISTIC_MODE_RANDOM: if ((net_random() & 0x7FFFFFFF) < info->u.random.probability) |
1d93a9cba [NETFILTER]: x_ta... |
40 |
ret = !ret; |
f3389805e [NETFILTER]: x_ta... |
41 42 |
break; case XT_STATISTIC_MODE_NTH: |
fabf3a85a netfilter: xt_sta... |
43 44 45 46 47 |
do { oval = atomic_read(&info->master->count); nval = (oval == info->u.nth.every) ? 0 : oval + 1; } while (atomic_cmpxchg(&info->master->count, oval, nval) != oval); if (nval == 0) |
1d93a9cba [NETFILTER]: x_ta... |
48 |
ret = !ret; |
f3389805e [NETFILTER]: x_ta... |
49 50 51 52 53 |
break; } return ret; } |
b0f38452f netfilter: xtable... |
54 |
static int statistic_mt_check(const struct xt_mtchk_param *par) |
f3389805e [NETFILTER]: x_ta... |
55 |
{ |
9b4fce7a3 netfilter: xtable... |
56 |
struct xt_statistic_info *info = par->matchinfo; |
f3389805e [NETFILTER]: x_ta... |
57 58 59 |
if (info->mode > XT_STATISTIC_MODE_MAX || info->flags & ~XT_STATISTIC_MASK) |
bd414ee60 netfilter: xtable... |
60 |
return -EINVAL; |
acc738fec netfilter: xtable... |
61 62 |
info->master = kzalloc(sizeof(*info->master), GFP_KERNEL); |
85bc3f381 netfilter: xtable... |
63 |
if (info->master == NULL) |
4a5a5c73b netfilter: xtable... |
64 |
return -ENOMEM; |
fabf3a85a netfilter: xt_sta... |
65 |
atomic_set(&info->master->count, info->u.nth.count); |
acc738fec netfilter: xtable... |
66 |
|
bd414ee60 netfilter: xtable... |
67 |
return 0; |
f3389805e [NETFILTER]: x_ta... |
68 |
} |
acc738fec netfilter: xtable... |
69 70 71 72 73 74 |
static void statistic_mt_destroy(const struct xt_mtdtor_param *par) { const struct xt_statistic_info *info = par->matchinfo; kfree(info->master); } |
55b69e910 netfilter: implem... |
75 76 77 78 79 80 |
static struct xt_match xt_statistic_mt_reg __read_mostly = { .name = "statistic", .revision = 0, .family = NFPROTO_UNSPEC, .match = statistic_mt, .checkentry = statistic_mt_check, |
acc738fec netfilter: xtable... |
81 |
.destroy = statistic_mt_destroy, |
55b69e910 netfilter: implem... |
82 83 |
.matchsize = sizeof(struct xt_statistic_info), .me = THIS_MODULE, |
f3389805e [NETFILTER]: x_ta... |
84 |
}; |
d3c5ee6d5 [NETFILTER]: x_ta... |
85 |
static int __init statistic_mt_init(void) |
f3389805e [NETFILTER]: x_ta... |
86 |
{ |
55b69e910 netfilter: implem... |
87 |
return xt_register_match(&xt_statistic_mt_reg); |
f3389805e [NETFILTER]: x_ta... |
88 |
} |
d3c5ee6d5 [NETFILTER]: x_ta... |
89 |
static void __exit statistic_mt_exit(void) |
f3389805e [NETFILTER]: x_ta... |
90 |
{ |
55b69e910 netfilter: implem... |
91 |
xt_unregister_match(&xt_statistic_mt_reg); |
f3389805e [NETFILTER]: x_ta... |
92 |
} |
d3c5ee6d5 [NETFILTER]: x_ta... |
93 94 |
module_init(statistic_mt_init); module_exit(statistic_mt_exit); |