[NETFILTER]: x_tables: remove unused size argument to check/destroy functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / xt_limit.c
blobb9c9ff3a06ea1d73a01bdf98ca8f21d03c4f9d65
1 /* Kernel module to control the rate
3 * 2 September 1999: Changed from the target RATE to the match
4 * `limit', removed logging. Did I mention that
5 * Alexey is a fucking genius?
6 * Rusty Russell (rusty@rustcorp.com.au). */
8 /* (C) 1999 Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr>
9 * (C) 1999 Hervé Eychenne <eychenne@info.enserb.u-bordeaux.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/xt_limit.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Herve Eychenne <rv@wallfire.org>");
26 MODULE_DESCRIPTION("iptables rate limit match");
27 MODULE_ALIAS("ipt_limit");
28 MODULE_ALIAS("ip6t_limit");
30 /* The algorithm used is the Simple Token Bucket Filter (TBF)
31 * see net/sched/sch_tbf.c in the linux source tree
34 static DEFINE_SPINLOCK(limit_lock);
36 /* Rusty: This is my (non-mathematically-inclined) understanding of
37 this algorithm. The `average rate' in jiffies becomes your initial
38 amount of credit `credit' and the most credit you can ever have
39 `credit_cap'. The `peak rate' becomes the cost of passing the
40 test, `cost'.
42 `prev' tracks the last packet hit: you gain one credit per jiffy.
43 If you get credit balance more than this, the extra credit is
44 discarded. Every time the match passes, you lose `cost' credits;
45 if you don't have that many, the test fails.
47 See Alexey's formal explanation in net/sched/sch_tbf.c.
49 To get the maxmum range, we multiply by this factor (ie. you get N
50 credits per jiffy). We want to allow a rate as low as 1 per day
51 (slowest userspace tool allows), which means
52 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32. ie. */
53 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
55 /* Repeated shift and or gives us all 1s, final shift and add 1 gives
56 * us the power of 2 below the theoretical max, so GCC simply does a
57 * shift. */
58 #define _POW2_BELOW2(x) ((x)|((x)>>1))
59 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
60 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
61 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
62 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
63 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
65 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
67 static int
68 ipt_limit_match(const struct sk_buff *skb,
69 const struct net_device *in,
70 const struct net_device *out,
71 const struct xt_match *match,
72 const void *matchinfo,
73 int offset,
74 unsigned int protoff,
75 int *hotdrop)
77 struct xt_rateinfo *r = ((struct xt_rateinfo *)matchinfo)->master;
78 unsigned long now = jiffies;
80 spin_lock_bh(&limit_lock);
81 r->credit += (now - xchg(&r->prev, now)) * CREDITS_PER_JIFFY;
82 if (r->credit > r->credit_cap)
83 r->credit = r->credit_cap;
85 if (r->credit >= r->cost) {
86 /* We're not limited. */
87 r->credit -= r->cost;
88 spin_unlock_bh(&limit_lock);
89 return 1;
92 spin_unlock_bh(&limit_lock);
93 return 0;
96 /* Precision saver. */
97 static u_int32_t
98 user2credits(u_int32_t user)
100 /* If multiplying would overflow... */
101 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
102 /* Divide first. */
103 return (user / XT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
105 return (user * HZ * CREDITS_PER_JIFFY) / XT_LIMIT_SCALE;
108 static int
109 ipt_limit_checkentry(const char *tablename,
110 const void *inf,
111 const struct xt_match *match,
112 void *matchinfo,
113 unsigned int hook_mask)
115 struct xt_rateinfo *r = matchinfo;
117 /* Check for overflow. */
118 if (r->burst == 0
119 || user2credits(r->avg * r->burst) < user2credits(r->avg)) {
120 printk("Overflow in xt_limit, try lower: %u/%u\n",
121 r->avg, r->burst);
122 return 0;
125 /* User avg in seconds * XT_LIMIT_SCALE: convert to jiffies *
126 128. */
127 r->prev = jiffies;
128 r->credit = user2credits(r->avg * r->burst); /* Credits full. */
129 r->credit_cap = user2credits(r->avg * r->burst); /* Credits full. */
130 r->cost = user2credits(r->avg);
132 /* For SMP, we only want to use one set of counters. */
133 r->master = r;
135 return 1;
138 static struct xt_match xt_limit_match[] = {
140 .name = "limit",
141 .family = AF_INET,
142 .checkentry = ipt_limit_checkentry,
143 .match = ipt_limit_match,
144 .matchsize = sizeof(struct xt_rateinfo),
145 .me = THIS_MODULE,
148 .name = "limit",
149 .family = AF_INET6,
150 .checkentry = ipt_limit_checkentry,
151 .match = ipt_limit_match,
152 .matchsize = sizeof(struct xt_rateinfo),
153 .me = THIS_MODULE,
157 static int __init xt_limit_init(void)
159 return xt_register_matches(xt_limit_match, ARRAY_SIZE(xt_limit_match));
162 static void __exit xt_limit_fini(void)
164 xt_unregister_matches(xt_limit_match, ARRAY_SIZE(xt_limit_match));
167 module_init(xt_limit_init);
168 module_exit(xt_limit_fini);