2 * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
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.
8 * Based on ipt_random and ipt_nth by Fabrice MARIE <fabrice@netfilter.org>.
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/skbuff.h>
14 #include <linux/net.h>
16 #include <linux/netfilter/xt_statistic.h>
17 #include <linux/netfilter/x_tables.h>
19 struct xt_statistic_priv
{
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
25 MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)");
26 MODULE_ALIAS("ipt_statistic");
27 MODULE_ALIAS("ip6t_statistic");
29 static DEFINE_SPINLOCK(nth_lock
);
32 match(const struct sk_buff
*skb
,
33 const struct net_device
*in
, const struct net_device
*out
,
34 const struct xt_match
*match
, const void *matchinfo
,
35 int offset
, unsigned int protoff
, int *hotdrop
)
37 const struct xt_statistic_info
*info
= matchinfo
;
38 int ret
= info
->flags
& XT_STATISTIC_INVERT
? 1 : 0;
41 case XT_STATISTIC_MODE_RANDOM
:
42 if ((net_random() & 0x7FFFFFFF) < info
->u
.random
.probability
)
45 case XT_STATISTIC_MODE_NTH
:
46 spin_lock_bh(&nth_lock
);
47 if (info
->master
->count
++ == info
->u
.nth
.every
) {
48 info
->master
->count
= 0;
51 spin_unlock_bh(&nth_lock
);
59 checkentry(const char *tablename
, const void *entry
,
60 const struct xt_match
*match
, void *matchinfo
,
61 unsigned int hook_mask
)
63 struct xt_statistic_info
*info
= (struct xt_statistic_info
*)matchinfo
;
65 if (info
->mode
> XT_STATISTIC_MODE_MAX
||
66 info
->flags
& ~XT_STATISTIC_MASK
)
69 info
->master
= kzalloc(sizeof(*info
->master
), GFP_KERNEL
);
70 if (info
->master
== NULL
) {
71 printk(KERN_ERR KBUILD_MODNAME
": Out of memory\n");
74 info
->master
->count
= info
->u
.nth
.count
;
79 static void statistic_mt_destroy(const struct xt_match
*match
, void *matchinfo
)
81 const struct xt_statistic_info
*info
= matchinfo
;
86 static struct xt_match xt_statistic_match
[] = {
90 .checkentry
= checkentry
,
92 .destroy
= statistic_mt_destroy
,
93 .matchsize
= sizeof(struct xt_statistic_info
),
99 .checkentry
= checkentry
,
101 .destroy
= statistic_mt_destroy
,
102 .matchsize
= sizeof(struct xt_statistic_info
),
107 static int __init
xt_statistic_init(void)
109 return xt_register_matches(xt_statistic_match
,
110 ARRAY_SIZE(xt_statistic_match
));
113 static void __exit
xt_statistic_fini(void)
115 xt_unregister_matches(xt_statistic_match
,
116 ARRAY_SIZE(xt_statistic_match
));
119 module_init(xt_statistic_init
);
120 module_exit(xt_statistic_fini
);