blob: f263a77e57b729106a4b5948f74de7ccf566bae3 [file] [log] [blame]
Marcin Garskidb9551702007-10-19 23:22:11 +02001/* (C) 1999 Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr>
2 * (C) 1999 Hervé Eychenne <eychenne@info.enserb.u-bordeaux.fr>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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.
7 */
8
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/spinlock.h>
12#include <linux/interrupt.h>
13
Harald Welte2e4e6a12006-01-12 13:30:04 -080014#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter/xt_limit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17MODULE_LICENSE("GPL");
18MODULE_AUTHOR("Herve Eychenne <rv@wallfire.org>");
19MODULE_DESCRIPTION("iptables rate limit match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080020MODULE_ALIAS("ipt_limit");
21MODULE_ALIAS("ip6t_limit");
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* The algorithm used is the Simple Token Bucket Filter (TBF)
24 * see net/sched/sch_tbf.c in the linux source tree
25 */
26
27static DEFINE_SPINLOCK(limit_lock);
28
29/* Rusty: This is my (non-mathematically-inclined) understanding of
30 this algorithm. The `average rate' in jiffies becomes your initial
31 amount of credit `credit' and the most credit you can ever have
32 `credit_cap'. The `peak rate' becomes the cost of passing the
33 test, `cost'.
34
35 `prev' tracks the last packet hit: you gain one credit per jiffy.
36 If you get credit balance more than this, the extra credit is
37 discarded. Every time the match passes, you lose `cost' credits;
38 if you don't have that many, the test fails.
39
40 See Alexey's formal explanation in net/sched/sch_tbf.c.
41
42 To get the maxmum range, we multiply by this factor (ie. you get N
43 credits per jiffy). We want to allow a rate as low as 1 per day
44 (slowest userspace tool allows), which means
45 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32. ie. */
46#define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
47
48/* Repeated shift and or gives us all 1s, final shift and add 1 gives
49 * us the power of 2 below the theoretical max, so GCC simply does a
50 * shift. */
51#define _POW2_BELOW2(x) ((x)|((x)>>1))
52#define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
53#define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
54#define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
55#define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
56#define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
57
58#define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
59
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070060static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070061ipt_limit_match(const struct sk_buff *skb,
62 const struct net_device *in,
63 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080064 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 const void *matchinfo,
66 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080067 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070068 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jan Engelhardta47362a2007-07-07 22:16:55 -070070 struct xt_rateinfo *r =
71 ((const struct xt_rateinfo *)matchinfo)->master;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long now = jiffies;
73
74 spin_lock_bh(&limit_lock);
75 r->credit += (now - xchg(&r->prev, now)) * CREDITS_PER_JIFFY;
76 if (r->credit > r->credit_cap)
77 r->credit = r->credit_cap;
78
79 if (r->credit >= r->cost) {
80 /* We're not limited. */
81 r->credit -= r->cost;
82 spin_unlock_bh(&limit_lock);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070083 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080086 spin_unlock_bh(&limit_lock);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070087 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/* Precision saver. */
91static u_int32_t
92user2credits(u_int32_t user)
93{
94 /* If multiplying would overflow... */
95 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
96 /* Divide first. */
Harald Welte2e4e6a12006-01-12 13:30:04 -080097 return (user / XT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Harald Welte2e4e6a12006-01-12 13:30:04 -080099 return (user * HZ * CREDITS_PER_JIFFY) / XT_LIMIT_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700102static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103ipt_limit_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800104 const void *inf,
Patrick McHardyc4986732006-03-20 18:02:56 -0800105 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int hook_mask)
108{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800109 struct xt_rateinfo *r = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* Check for overflow. */
112 if (r->burst == 0
113 || user2credits(r->avg * r->burst) < user2credits(r->avg)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800114 printk("Overflow in xt_limit, try lower: %u/%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 r->avg, r->burst);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700116 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* For SMP, we only want to use one set of counters. */
120 r->master = r;
Patrick McHardy57dab5d2006-09-20 11:59:25 -0700121 if (r->cost == 0) {
122 /* User avg in seconds * XT_LIMIT_SCALE: convert to jiffies *
123 128. */
124 r->prev = jiffies;
125 r->credit = user2credits(r->avg * r->burst); /* Credits full. */
126 r->credit_cap = user2credits(r->avg * r->burst); /* Credits full. */
127 r->cost = user2credits(r->avg);
128 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700129 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Patrick McHardy02c63cf2006-09-20 12:07:06 -0700132#ifdef CONFIG_COMPAT
133struct compat_xt_rateinfo {
134 u_int32_t avg;
135 u_int32_t burst;
136
137 compat_ulong_t prev;
138 u_int32_t credit;
139 u_int32_t credit_cap, cost;
140
141 u_int32_t master;
142};
143
144/* To keep the full "prev" timestamp, the upper 32 bits are stored in the
145 * master pointer, which does not need to be preserved. */
146static void compat_from_user(void *dst, void *src)
147{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700148 const struct compat_xt_rateinfo *cm = src;
Patrick McHardy02c63cf2006-09-20 12:07:06 -0700149 struct xt_rateinfo m = {
150 .avg = cm->avg,
151 .burst = cm->burst,
152 .prev = cm->prev | (unsigned long)cm->master << 32,
153 .credit = cm->credit,
154 .credit_cap = cm->credit_cap,
155 .cost = cm->cost,
156 };
157 memcpy(dst, &m, sizeof(m));
158}
159
160static int compat_to_user(void __user *dst, void *src)
161{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700162 const struct xt_rateinfo *m = src;
Patrick McHardy02c63cf2006-09-20 12:07:06 -0700163 struct compat_xt_rateinfo cm = {
164 .avg = m->avg,
165 .burst = m->burst,
166 .prev = m->prev,
167 .credit = m->credit,
168 .credit_cap = m->credit_cap,
169 .cost = m->cost,
170 .master = m->prev >> 32,
171 };
172 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
173}
174#endif /* CONFIG_COMPAT */
175
Patrick McHardy9f15c532007-07-07 22:22:02 -0700176static struct xt_match xt_limit_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700177 {
178 .name = "limit",
179 .family = AF_INET,
180 .checkentry = ipt_limit_checkentry,
181 .match = ipt_limit_match,
182 .matchsize = sizeof(struct xt_rateinfo),
Patrick McHardy02c63cf2006-09-20 12:07:06 -0700183#ifdef CONFIG_COMPAT
184 .compatsize = sizeof(struct compat_xt_rateinfo),
185 .compat_from_user = compat_from_user,
186 .compat_to_user = compat_to_user,
187#endif
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700188 .me = THIS_MODULE,
189 },
190 {
191 .name = "limit",
192 .family = AF_INET6,
193 .checkentry = ipt_limit_checkentry,
194 .match = ipt_limit_match,
195 .matchsize = sizeof(struct xt_rateinfo),
196 .me = THIS_MODULE,
197 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800200static int __init xt_limit_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700202 return xt_register_matches(xt_limit_match, ARRAY_SIZE(xt_limit_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800205static void __exit xt_limit_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700207 xt_unregister_matches(xt_limit_match, ARRAY_SIZE(xt_limit_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800210module_init(xt_limit_init);
211module_exit(xt_limit_fini);