1 /* Kernel module to control the rate
2 *
3 * J�r�me de Vivie <devivie@info.enserb.u-bordeaux.fr>
4 * Herv� Eychenne <eychenne@info.enserb.u-bordeaux.fr>
5 *
6 * 2 September 1999: Changed from the target RATE to the match
7 * `limit', removed logging. Did I mention that
8 * Alexey is a fucking genius?
9 * Rusty Russell (rusty@rustcorp.com.au). */
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/spinlock.h>
13 #include <linux/interrupt.h>
14
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netfilter_ipv4/ipt_limit.h>
17
18 /* The algorithm used is the Simple Token Bucket Filter (TBF)
19 * see net/sched/sch_tbf.c in the linux source tree
20 */
21
22 static spinlock_t limit_lock = SPIN_LOCK_UNLOCKED;
23
24 /* Rusty: This is my (non-mathematically-inclined) understanding of
25 this algorithm. The `average rate' in jiffies becomes your initial
26 amount of credit `credit' and the most credit you can ever have
27 `credit_cap'. The `peak rate' becomes the cost of passing the
28 test, `cost'.
29
30 `prev' tracks the last packet hit: you gain one credit per jiffy.
31 If you get credit balance more than this, the extra credit is
32 discarded. Every time the match passes, you lose `cost' credits;
33 if you don't have that many, the test fails.
34
35 See Alexey's formal explanation in net/sched/sch_tbf.c.
36
37 To get the maxmum range, we multiply by this factor (ie. you get N
38 credits per jiffy). We want to allow a rate as low as 1 per day
39 (slowest userspace tool allows), which means
40 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32. ie. */
41 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
42
43 /* Repeated shift and or gives us all 1s, final shift and add 1 gives
44 * us the power of 2 below the theoretical max, so GCC simply does a
45 * shift. */
46 #define _POW2_BELOW2(x) ((x)|((x)>>1))
47 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
48 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
49 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
50 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
51 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
52
53 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
54
55 static int
ipt_limit_match(const struct sk_buff * skb,const struct net_device * in,const struct net_device * out,const void * matchinfo,int offset,const void * hdr,u_int16_t datalen,int * hotdrop)56 ipt_limit_match(const struct sk_buff *skb,
57 const struct net_device *in,
58 const struct net_device *out,
59 const void *matchinfo,
60 int offset,
61 const void *hdr,
62 u_int16_t datalen,
63 int *hotdrop)
64 {
65 struct ipt_rateinfo *r = ((struct ipt_rateinfo *)matchinfo)->master;
66 unsigned long now = jiffies;
67
68 spin_lock_bh(&limit_lock);
69 r->credit += (now - xchg(&r->prev, now)) * CREDITS_PER_JIFFY;
70 if (r->credit > r->credit_cap)
71 r->credit = r->credit_cap;
72
73 if (r->credit >= r->cost) {
74 /* We're not limited. */
75 r->credit -= r->cost;
76 spin_unlock_bh(&limit_lock);
77 return 1;
78 }
79
80 spin_unlock_bh(&limit_lock);
81 return 0;
82 }
83
84 /* Precision saver. */
85 static u_int32_t
user2credits(u_int32_t user)86 user2credits(u_int32_t user)
87 {
88 /* If multiplying would overflow... */
89 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
90 /* Divide first. */
91 return (user / IPT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
92
93 return (user * HZ * CREDITS_PER_JIFFY) / IPT_LIMIT_SCALE;
94 }
95
96 static int
ipt_limit_checkentry(const char * tablename,const struct ipt_ip * ip,void * matchinfo,unsigned int matchsize,unsigned int hook_mask)97 ipt_limit_checkentry(const char *tablename,
98 const struct ipt_ip *ip,
99 void *matchinfo,
100 unsigned int matchsize,
101 unsigned int hook_mask)
102 {
103 struct ipt_rateinfo *r = matchinfo;
104
105 if (matchsize != IPT_ALIGN(sizeof(struct ipt_rateinfo)))
106 return 0;
107
108 /* Check for overflow. */
109 if (r->burst == 0
110 || user2credits(r->avg * r->burst) < user2credits(r->avg)) {
111 printk("Overflow in ipt_limit, try lower: %u/%u\n",
112 r->avg, r->burst);
113 return 0;
114 }
115
116 /* User avg in seconds * IPT_LIMIT_SCALE: convert to jiffies *
117 128. */
118 r->prev = jiffies;
119 r->credit = user2credits(r->avg * r->burst); /* Credits full. */
120 r->credit_cap = user2credits(r->avg * r->burst); /* Credits full. */
121 r->cost = user2credits(r->avg);
122
123 /* For SMP, we only want to use one set of counters. */
124 r->master = r;
125
126 return 1;
127 }
128
129 static struct ipt_match ipt_limit_reg
130 = { { NULL, NULL }, "limit", ipt_limit_match, ipt_limit_checkentry, NULL,
131 THIS_MODULE };
132
init(void)133 static int __init init(void)
134 {
135 if (ipt_register_match(&ipt_limit_reg))
136 return -EINVAL;
137 return 0;
138 }
139
fini(void)140 static void __exit fini(void)
141 {
142 ipt_unregister_match(&ipt_limit_reg);
143 }
144
145 module_init(init);
146 module_exit(fini);
147 MODULE_LICENSE("GPL");
148