allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netfilter / xt_statistic.c
blob3abfa70fc31b6cee552ba51e08e6257a57dee8a7
1 /*
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>.
9 */
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 {
20 uint32_t count;
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);
31 static int
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;
40 switch (info->mode) {
41 case XT_STATISTIC_MODE_RANDOM:
42 if ((net_random() & 0x7FFFFFFF) < info->u.random.probability)
43 ret ^= 1;
44 break;
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;
49 ret ^= 1;
51 spin_unlock_bh(&nth_lock);
52 break;
55 return ret;
58 static int
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)
67 return 0;
69 info->master = kzalloc(sizeof(*info->master), GFP_KERNEL);
70 if (info->master == NULL) {
71 printk(KERN_ERR KBUILD_MODNAME ": Out of memory\n");
72 return 0;
74 info->master->count = info->u.nth.count;
76 return 1;
79 static void statistic_mt_destroy(const struct xt_match *match, void *matchinfo)
81 const struct xt_statistic_info *info = matchinfo;
83 kfree(info->master);
86 static struct xt_match xt_statistic_match[] = {
88 .name = "statistic",
89 .family = AF_INET,
90 .checkentry = checkentry,
91 .match = match,
92 .destroy = statistic_mt_destroy,
93 .matchsize = sizeof(struct xt_statistic_info),
94 .me = THIS_MODULE,
97 .name = "statistic",
98 .family = AF_INET6,
99 .checkentry = checkentry,
100 .match = match,
101 .destroy = statistic_mt_destroy,
102 .matchsize = sizeof(struct xt_statistic_info),
103 .me = THIS_MODULE,
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);