blob: 6fd8347c0058f410531cf14a052f47f3a274ccc4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel module to control the rate
2 *
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). */
7
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>
10 *
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.
14 */
15
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/spinlock.h>
19#include <linux/interrupt.h>
20
Harald Welte2e4e6a12006-01-12 13:30:04 -080021#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_limit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Herve Eychenne <rv@wallfire.org>");
26MODULE_DESCRIPTION("iptables rate limit match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080027MODULE_ALIAS("ipt_limit");
28MODULE_ALIAS("ip6t_limit");
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* The algorithm used is the Simple Token Bucket Filter (TBF)
31 * see net/sched/sch_tbf.c in the linux source tree
32 */
33
34static DEFINE_SPINLOCK(limit_lock);
35
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'.
41
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.
46
47 See Alexey's formal explanation in net/sched/sch_tbf.c.
48
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))
54
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)
64
65#define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
66
67static int
68ipt_limit_match(const struct sk_buff *skb,
69 const struct net_device *in,
70 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080071 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 const void *matchinfo,
73 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080074 unsigned int protoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int *hotdrop)
76{
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 struct xt_rateinfo *r = ((struct xt_rateinfo *)matchinfo)->master;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned long now = jiffies;
79
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;
84
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;
90 }
91
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080092 spin_unlock_bh(&limit_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94}
95
96/* Precision saver. */
97static u_int32_t
98user2credits(u_int32_t user)
99{
100 /* If multiplying would overflow... */
101 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
102 /* Divide first. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800103 return (user / XT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Harald Welte2e4e6a12006-01-12 13:30:04 -0800105 return (user * HZ * CREDITS_PER_JIFFY) / XT_LIMIT_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108static int
109ipt_limit_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800110 const void *inf,
Patrick McHardyc4986732006-03-20 18:02:56 -0800111 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int hook_mask)
114{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800115 struct xt_rateinfo *r = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* Check for overflow. */
118 if (r->burst == 0
119 || user2credits(r->avg * r->burst) < user2credits(r->avg)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800120 printk("Overflow in xt_limit, try lower: %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 r->avg, r->burst);
122 return 0;
123 }
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* For SMP, we only want to use one set of counters. */
126 r->master = r;
Patrick McHardy57dab5d2006-09-20 11:59:25 -0700127 if (r->cost == 0) {
128 /* User avg in seconds * XT_LIMIT_SCALE: convert to jiffies *
129 128. */
130 r->prev = jiffies;
131 r->credit = user2credits(r->avg * r->burst); /* Credits full. */
132 r->credit_cap = user2credits(r->avg * r->burst); /* Credits full. */
133 r->cost = user2credits(r->avg);
134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 1;
136}
137
Patrick McHardy02c63cf2006-09-20 12:07:06 -0700138#ifdef CONFIG_COMPAT
139struct compat_xt_rateinfo {
140 u_int32_t avg;
141 u_int32_t burst;
142
143 compat_ulong_t prev;
144 u_int32_t credit;
145 u_int32_t credit_cap, cost;
146
147 u_int32_t master;
148};
149
150/* To keep the full "prev" timestamp, the upper 32 bits are stored in the
151 * master pointer, which does not need to be preserved. */
152static void compat_from_user(void *dst, void *src)
153{
154 struct compat_xt_rateinfo *cm = src;
155 struct xt_rateinfo m = {
156 .avg = cm->avg,
157 .burst = cm->burst,
158 .prev = cm->prev | (unsigned long)cm->master << 32,
159 .credit = cm->credit,
160 .credit_cap = cm->credit_cap,
161 .cost = cm->cost,
162 };
163 memcpy(dst, &m, sizeof(m));
164}
165
166static int compat_to_user(void __user *dst, void *src)
167{
168 struct xt_rateinfo *m = src;
169 struct compat_xt_rateinfo cm = {
170 .avg = m->avg,
171 .burst = m->burst,
172 .prev = m->prev,
173 .credit = m->credit,
174 .credit_cap = m->credit_cap,
175 .cost = m->cost,
176 .master = m->prev >> 32,
177 };
178 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
179}
180#endif /* CONFIG_COMPAT */
181
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700182static struct xt_match xt_limit_match[] = {
183 {
184 .name = "limit",
185 .family = AF_INET,
186 .checkentry = ipt_limit_checkentry,
187 .match = ipt_limit_match,
188 .matchsize = sizeof(struct xt_rateinfo),
Patrick McHardy02c63cf2006-09-20 12:07:06 -0700189#ifdef CONFIG_COMPAT
190 .compatsize = sizeof(struct compat_xt_rateinfo),
191 .compat_from_user = compat_from_user,
192 .compat_to_user = compat_to_user,
193#endif
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700194 .me = THIS_MODULE,
195 },
196 {
197 .name = "limit",
198 .family = AF_INET6,
199 .checkentry = ipt_limit_checkentry,
200 .match = ipt_limit_match,
201 .matchsize = sizeof(struct xt_rateinfo),
202 .me = THIS_MODULE,
203 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800206static int __init xt_limit_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700208 return xt_register_matches(xt_limit_match, ARRAY_SIZE(xt_limit_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800211static void __exit xt_limit_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700213 xt_unregister_matches(xt_limit_match, ARRAY_SIZE(xt_limit_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800216module_init(xt_limit_init);
217module_exit(xt_limit_fini);