Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* iptables match extension to limit the number of packets per second |
2 | * seperately for each hashbucket (sourceip/sourceport/dstip/dstport) | |
3 | * | |
4 | * (C) 2003-2004 by Harald Welte <laforge@netfilter.org> | |
5 | * | |
6 | * $Id: ipt_hashlimit.c 3244 2004-10-20 16:24:29Z laforge@netfilter.org $ | |
7 | * | |
8 | * Development of this code was funded by Astaro AG, http://www.astaro.com/ | |
1da177e4 LT |
9 | */ |
10 | #include <linux/module.h> | |
1da177e4 LT |
11 | #include <linux/spinlock.h> |
12 | #include <linux/random.h> | |
13 | #include <linux/jhash.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/vmalloc.h> | |
1da177e4 LT |
16 | #include <linux/proc_fs.h> |
17 | #include <linux/seq_file.h> | |
18 | #include <linux/list.h> | |
39b46fc6 | 19 | #include <linux/skbuff.h> |
d7fe0f24 | 20 | #include <linux/mm.h> |
39b46fc6 PM |
21 | #include <linux/in.h> |
22 | #include <linux/ip.h> | |
23 | #include <linux/ipv6.h> | |
457c4cbc | 24 | #include <net/net_namespace.h> |
1da177e4 | 25 | |
39b46fc6 | 26 | #include <linux/netfilter/x_tables.h> |
1da177e4 | 27 | #include <linux/netfilter_ipv4/ip_tables.h> |
39b46fc6 PM |
28 | #include <linux/netfilter_ipv6/ip6_tables.h> |
29 | #include <linux/netfilter/xt_hashlimit.h> | |
14cc3e2b | 30 | #include <linux/mutex.h> |
1da177e4 LT |
31 | |
32 | MODULE_LICENSE("GPL"); | |
33 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
34 | MODULE_DESCRIPTION("iptables match for limiting per hash-bucket"); | |
39b46fc6 PM |
35 | MODULE_ALIAS("ipt_hashlimit"); |
36 | MODULE_ALIAS("ip6t_hashlimit"); | |
1da177e4 LT |
37 | |
38 | /* need to declare this at the top */ | |
39b46fc6 PM |
39 | static struct proc_dir_entry *hashlimit_procdir4; |
40 | static struct proc_dir_entry *hashlimit_procdir6; | |
da7071d7 | 41 | static const struct file_operations dl_file_ops; |
1da177e4 LT |
42 | |
43 | /* hash table crap */ | |
1da177e4 | 44 | struct dsthash_dst { |
39b46fc6 PM |
45 | union { |
46 | struct { | |
47 | __be32 src; | |
48 | __be32 dst; | |
49 | } ip; | |
50 | struct { | |
51 | __be32 src[4]; | |
52 | __be32 dst[4]; | |
53 | } ip6; | |
54 | } addr; | |
6a19d614 AV |
55 | __be16 src_port; |
56 | __be16 dst_port; | |
1da177e4 LT |
57 | }; |
58 | ||
59 | struct dsthash_ent { | |
60 | /* static / read-only parts in the beginning */ | |
61 | struct hlist_node node; | |
62 | struct dsthash_dst dst; | |
63 | ||
64 | /* modified structure members in the end */ | |
65 | unsigned long expires; /* precalculated expiry time */ | |
66 | struct { | |
67 | unsigned long prev; /* last modification */ | |
68 | u_int32_t credit; | |
69 | u_int32_t credit_cap, cost; | |
70 | } rateinfo; | |
71 | }; | |
72 | ||
39b46fc6 | 73 | struct xt_hashlimit_htable { |
1da177e4 LT |
74 | struct hlist_node node; /* global list of all htables */ |
75 | atomic_t use; | |
39b46fc6 | 76 | int family; |
1da177e4 LT |
77 | |
78 | struct hashlimit_cfg cfg; /* config */ | |
79 | ||
80 | /* used internally */ | |
81 | spinlock_t lock; /* lock for list_head */ | |
82 | u_int32_t rnd; /* random seed for hash */ | |
bf0857ea | 83 | int rnd_initialized; |
39b46fc6 | 84 | unsigned int count; /* number entries in table */ |
1da177e4 | 85 | struct timer_list timer; /* timer for gc */ |
1da177e4 LT |
86 | |
87 | /* seq_file stuff */ | |
88 | struct proc_dir_entry *pde; | |
89 | ||
90 | struct hlist_head hash[0]; /* hashtable itself */ | |
91 | }; | |
92 | ||
e45b1be8 | 93 | static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */ |
14cc3e2b | 94 | static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */ |
1da177e4 | 95 | static HLIST_HEAD(hashlimit_htables); |
e18b890b | 96 | static struct kmem_cache *hashlimit_cachep __read_mostly; |
1da177e4 | 97 | |
1d93a9cb | 98 | static inline bool dst_cmp(const struct dsthash_ent *ent, |
a47362a2 | 99 | const struct dsthash_dst *b) |
1da177e4 | 100 | { |
39b46fc6 | 101 | return !memcmp(&ent->dst, b, sizeof(ent->dst)); |
1da177e4 LT |
102 | } |
103 | ||
39b46fc6 PM |
104 | static u_int32_t |
105 | hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst) | |
1da177e4 | 106 | { |
39b46fc6 | 107 | return jhash(dst, sizeof(*dst), ht->rnd) % ht->cfg.size; |
1da177e4 LT |
108 | } |
109 | ||
39b46fc6 | 110 | static struct dsthash_ent * |
a47362a2 JE |
111 | dsthash_find(const struct xt_hashlimit_htable *ht, |
112 | const struct dsthash_dst *dst) | |
1da177e4 LT |
113 | { |
114 | struct dsthash_ent *ent; | |
115 | struct hlist_node *pos; | |
116 | u_int32_t hash = hash_dst(ht, dst); | |
117 | ||
39b46fc6 PM |
118 | if (!hlist_empty(&ht->hash[hash])) { |
119 | hlist_for_each_entry(ent, pos, &ht->hash[hash], node) | |
120 | if (dst_cmp(ent, dst)) | |
1da177e4 | 121 | return ent; |
39b46fc6 | 122 | } |
1da177e4 LT |
123 | return NULL; |
124 | } | |
125 | ||
126 | /* allocate dsthash_ent, initialize dst, put in htable and lock it */ | |
127 | static struct dsthash_ent * | |
a47362a2 JE |
128 | dsthash_alloc_init(struct xt_hashlimit_htable *ht, |
129 | const struct dsthash_dst *dst) | |
1da177e4 LT |
130 | { |
131 | struct dsthash_ent *ent; | |
132 | ||
133 | /* initialize hash with random val at the time we allocate | |
134 | * the first hashtable entry */ | |
bf0857ea | 135 | if (!ht->rnd_initialized) { |
1da177e4 | 136 | get_random_bytes(&ht->rnd, 4); |
bf0857ea PM |
137 | ht->rnd_initialized = 1; |
138 | } | |
1da177e4 | 139 | |
39b46fc6 | 140 | if (ht->cfg.max && ht->count >= ht->cfg.max) { |
1da177e4 LT |
141 | /* FIXME: do something. question is what.. */ |
142 | if (net_ratelimit()) | |
39b46fc6 PM |
143 | printk(KERN_WARNING |
144 | "xt_hashlimit: max count of %u reached\n", | |
1da177e4 LT |
145 | ht->cfg.max); |
146 | return NULL; | |
147 | } | |
148 | ||
149 | ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC); | |
150 | if (!ent) { | |
151 | if (net_ratelimit()) | |
39b46fc6 PM |
152 | printk(KERN_ERR |
153 | "xt_hashlimit: can't allocate dsthash_ent\n"); | |
1da177e4 LT |
154 | return NULL; |
155 | } | |
39b46fc6 | 156 | memcpy(&ent->dst, dst, sizeof(ent->dst)); |
1da177e4 LT |
157 | |
158 | hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]); | |
39b46fc6 | 159 | ht->count++; |
1da177e4 LT |
160 | return ent; |
161 | } | |
162 | ||
39b46fc6 PM |
163 | static inline void |
164 | dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent) | |
1da177e4 LT |
165 | { |
166 | hlist_del(&ent->node); | |
167 | kmem_cache_free(hashlimit_cachep, ent); | |
39b46fc6 | 168 | ht->count--; |
1da177e4 LT |
169 | } |
170 | static void htable_gc(unsigned long htlong); | |
171 | ||
39b46fc6 | 172 | static int htable_create(struct xt_hashlimit_info *minfo, int family) |
1da177e4 | 173 | { |
39b46fc6 | 174 | struct xt_hashlimit_htable *hinfo; |
1da177e4 | 175 | unsigned int size; |
39b46fc6 | 176 | unsigned int i; |
1da177e4 LT |
177 | |
178 | if (minfo->cfg.size) | |
179 | size = minfo->cfg.size; | |
180 | else { | |
39b46fc6 PM |
181 | size = ((num_physpages << PAGE_SHIFT) / 16384) / |
182 | sizeof(struct list_head); | |
1da177e4 LT |
183 | if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
184 | size = 8192; | |
185 | if (size < 16) | |
186 | size = 16; | |
187 | } | |
188 | /* FIXME: don't use vmalloc() here or anywhere else -HW */ | |
39b46fc6 PM |
189 | hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) + |
190 | sizeof(struct list_head) * size); | |
1da177e4 | 191 | if (!hinfo) { |
39b46fc6 | 192 | printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n"); |
1da177e4 LT |
193 | return -1; |
194 | } | |
195 | minfo->hinfo = hinfo; | |
196 | ||
197 | /* copy match config into hashtable config */ | |
198 | memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg)); | |
199 | hinfo->cfg.size = size; | |
200 | if (!hinfo->cfg.max) | |
201 | hinfo->cfg.max = 8 * hinfo->cfg.size; | |
202 | else if (hinfo->cfg.max < hinfo->cfg.size) | |
203 | hinfo->cfg.max = hinfo->cfg.size; | |
204 | ||
205 | for (i = 0; i < hinfo->cfg.size; i++) | |
206 | INIT_HLIST_HEAD(&hinfo->hash[i]); | |
207 | ||
1da177e4 | 208 | atomic_set(&hinfo->use, 1); |
39b46fc6 PM |
209 | hinfo->count = 0; |
210 | hinfo->family = family; | |
bf0857ea | 211 | hinfo->rnd_initialized = 0; |
1da177e4 | 212 | spin_lock_init(&hinfo->lock); |
39b46fc6 PM |
213 | hinfo->pde = create_proc_entry(minfo->name, 0, |
214 | family == AF_INET ? hashlimit_procdir4 : | |
601e68e1 | 215 | hashlimit_procdir6); |
1da177e4 LT |
216 | if (!hinfo->pde) { |
217 | vfree(hinfo); | |
218 | return -1; | |
219 | } | |
220 | hinfo->pde->proc_fops = &dl_file_ops; | |
221 | hinfo->pde->data = hinfo; | |
222 | ||
e6f689db | 223 | setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo); |
1da177e4 | 224 | hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval); |
1da177e4 LT |
225 | add_timer(&hinfo->timer); |
226 | ||
e45b1be8 | 227 | spin_lock_bh(&hashlimit_lock); |
1da177e4 | 228 | hlist_add_head(&hinfo->node, &hashlimit_htables); |
e45b1be8 | 229 | spin_unlock_bh(&hashlimit_lock); |
1da177e4 LT |
230 | |
231 | return 0; | |
232 | } | |
233 | ||
a47362a2 JE |
234 | static bool select_all(const struct xt_hashlimit_htable *ht, |
235 | const struct dsthash_ent *he) | |
1da177e4 LT |
236 | { |
237 | return 1; | |
238 | } | |
239 | ||
a47362a2 JE |
240 | static bool select_gc(const struct xt_hashlimit_htable *ht, |
241 | const struct dsthash_ent *he) | |
1da177e4 | 242 | { |
cbebc51f | 243 | return time_after_eq(jiffies, he->expires); |
1da177e4 LT |
244 | } |
245 | ||
39b46fc6 | 246 | static void htable_selective_cleanup(struct xt_hashlimit_htable *ht, |
a47362a2 JE |
247 | bool (*select)(const struct xt_hashlimit_htable *ht, |
248 | const struct dsthash_ent *he)) | |
1da177e4 | 249 | { |
39b46fc6 | 250 | unsigned int i; |
1da177e4 LT |
251 | |
252 | /* lock hash table and iterate over it */ | |
253 | spin_lock_bh(&ht->lock); | |
254 | for (i = 0; i < ht->cfg.size; i++) { | |
255 | struct dsthash_ent *dh; | |
256 | struct hlist_node *pos, *n; | |
257 | hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) { | |
258 | if ((*select)(ht, dh)) | |
39b46fc6 | 259 | dsthash_free(ht, dh); |
1da177e4 LT |
260 | } |
261 | } | |
262 | spin_unlock_bh(&ht->lock); | |
263 | } | |
264 | ||
265 | /* hash table garbage collector, run by timer */ | |
266 | static void htable_gc(unsigned long htlong) | |
267 | { | |
39b46fc6 | 268 | struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong; |
1da177e4 LT |
269 | |
270 | htable_selective_cleanup(ht, select_gc); | |
271 | ||
272 | /* re-add the timer accordingly */ | |
273 | ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval); | |
274 | add_timer(&ht->timer); | |
275 | } | |
276 | ||
39b46fc6 | 277 | static void htable_destroy(struct xt_hashlimit_htable *hinfo) |
1da177e4 LT |
278 | { |
279 | /* remove timer, if it is pending */ | |
280 | if (timer_pending(&hinfo->timer)) | |
281 | del_timer(&hinfo->timer); | |
282 | ||
283 | /* remove proc entry */ | |
39b46fc6 PM |
284 | remove_proc_entry(hinfo->pde->name, |
285 | hinfo->family == AF_INET ? hashlimit_procdir4 : | |
601e68e1 | 286 | hashlimit_procdir6); |
1da177e4 LT |
287 | htable_selective_cleanup(hinfo, select_all); |
288 | vfree(hinfo); | |
289 | } | |
290 | ||
a47362a2 JE |
291 | static struct xt_hashlimit_htable *htable_find_get(const char *name, |
292 | int family) | |
1da177e4 | 293 | { |
39b46fc6 | 294 | struct xt_hashlimit_htable *hinfo; |
1da177e4 LT |
295 | struct hlist_node *pos; |
296 | ||
e45b1be8 | 297 | spin_lock_bh(&hashlimit_lock); |
1da177e4 | 298 | hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) { |
39b46fc6 PM |
299 | if (!strcmp(name, hinfo->pde->name) && |
300 | hinfo->family == family) { | |
1da177e4 | 301 | atomic_inc(&hinfo->use); |
e45b1be8 | 302 | spin_unlock_bh(&hashlimit_lock); |
1da177e4 LT |
303 | return hinfo; |
304 | } | |
305 | } | |
e45b1be8 | 306 | spin_unlock_bh(&hashlimit_lock); |
1da177e4 LT |
307 | return NULL; |
308 | } | |
309 | ||
39b46fc6 | 310 | static void htable_put(struct xt_hashlimit_htable *hinfo) |
1da177e4 LT |
311 | { |
312 | if (atomic_dec_and_test(&hinfo->use)) { | |
e45b1be8 | 313 | spin_lock_bh(&hashlimit_lock); |
1da177e4 | 314 | hlist_del(&hinfo->node); |
e45b1be8 | 315 | spin_unlock_bh(&hashlimit_lock); |
1da177e4 LT |
316 | htable_destroy(hinfo); |
317 | } | |
318 | } | |
319 | ||
1da177e4 LT |
320 | /* The algorithm used is the Simple Token Bucket Filter (TBF) |
321 | * see net/sched/sch_tbf.c in the linux source tree | |
322 | */ | |
323 | ||
324 | /* Rusty: This is my (non-mathematically-inclined) understanding of | |
325 | this algorithm. The `average rate' in jiffies becomes your initial | |
326 | amount of credit `credit' and the most credit you can ever have | |
327 | `credit_cap'. The `peak rate' becomes the cost of passing the | |
328 | test, `cost'. | |
329 | ||
330 | `prev' tracks the last packet hit: you gain one credit per jiffy. | |
331 | If you get credit balance more than this, the extra credit is | |
332 | discarded. Every time the match passes, you lose `cost' credits; | |
333 | if you don't have that many, the test fails. | |
334 | ||
335 | See Alexey's formal explanation in net/sched/sch_tbf.c. | |
336 | ||
337 | To get the maximum range, we multiply by this factor (ie. you get N | |
338 | credits per jiffy). We want to allow a rate as low as 1 per day | |
339 | (slowest userspace tool allows), which means | |
340 | CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie. | |
341 | */ | |
342 | #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24)) | |
343 | ||
344 | /* Repeated shift and or gives us all 1s, final shift and add 1 gives | |
345 | * us the power of 2 below the theoretical max, so GCC simply does a | |
346 | * shift. */ | |
347 | #define _POW2_BELOW2(x) ((x)|((x)>>1)) | |
348 | #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2)) | |
349 | #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4)) | |
350 | #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8)) | |
351 | #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16)) | |
352 | #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1) | |
353 | ||
354 | #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ) | |
355 | ||
356 | /* Precision saver. */ | |
357 | static inline u_int32_t | |
358 | user2credits(u_int32_t user) | |
359 | { | |
360 | /* If multiplying would overflow... */ | |
361 | if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY)) | |
362 | /* Divide first. */ | |
39b46fc6 | 363 | return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY; |
1da177e4 | 364 | |
39b46fc6 | 365 | return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE; |
1da177e4 LT |
366 | } |
367 | ||
368 | static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now) | |
369 | { | |
39b46fc6 | 370 | dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY; |
1da177e4 LT |
371 | if (dh->rateinfo.credit > dh->rateinfo.credit_cap) |
372 | dh->rateinfo.credit = dh->rateinfo.credit_cap; | |
39b46fc6 PM |
373 | dh->rateinfo.prev = now; |
374 | } | |
375 | ||
376 | static int | |
a47362a2 JE |
377 | hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo, |
378 | struct dsthash_dst *dst, | |
39b46fc6 PM |
379 | const struct sk_buff *skb, unsigned int protoff) |
380 | { | |
381 | __be16 _ports[2], *ports; | |
382 | int nexthdr; | |
383 | ||
384 | memset(dst, 0, sizeof(*dst)); | |
385 | ||
386 | switch (hinfo->family) { | |
387 | case AF_INET: | |
388 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) | |
eddc9ec5 | 389 | dst->addr.ip.dst = ip_hdr(skb)->daddr; |
39b46fc6 | 390 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) |
eddc9ec5 | 391 | dst->addr.ip.src = ip_hdr(skb)->saddr; |
39b46fc6 PM |
392 | |
393 | if (!(hinfo->cfg.mode & | |
394 | (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) | |
395 | return 0; | |
eddc9ec5 | 396 | nexthdr = ip_hdr(skb)->protocol; |
39b46fc6 | 397 | break; |
02dba025 | 398 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
39b46fc6 PM |
399 | case AF_INET6: |
400 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) | |
0660e03f | 401 | memcpy(&dst->addr.ip6.dst, &ipv6_hdr(skb)->daddr, |
39b46fc6 PM |
402 | sizeof(dst->addr.ip6.dst)); |
403 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) | |
0660e03f | 404 | memcpy(&dst->addr.ip6.src, &ipv6_hdr(skb)->saddr, |
39b46fc6 PM |
405 | sizeof(dst->addr.ip6.src)); |
406 | ||
407 | if (!(hinfo->cfg.mode & | |
408 | (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT))) | |
409 | return 0; | |
410 | nexthdr = ipv6_find_hdr(skb, &protoff, -1, NULL); | |
411 | if (nexthdr < 0) | |
412 | return -1; | |
413 | break; | |
414 | #endif | |
415 | default: | |
416 | BUG(); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | switch (nexthdr) { | |
421 | case IPPROTO_TCP: | |
422 | case IPPROTO_UDP: | |
a8d0f952 | 423 | case IPPROTO_UDPLITE: |
39b46fc6 PM |
424 | case IPPROTO_SCTP: |
425 | case IPPROTO_DCCP: | |
426 | ports = skb_header_pointer(skb, protoff, sizeof(_ports), | |
427 | &_ports); | |
428 | break; | |
429 | default: | |
430 | _ports[0] = _ports[1] = 0; | |
431 | ports = _ports; | |
432 | break; | |
433 | } | |
434 | if (!ports) | |
435 | return -1; | |
436 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT) | |
437 | dst->src_port = ports[0]; | |
438 | if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT) | |
439 | dst->dst_port = ports[1]; | |
440 | return 0; | |
1da177e4 LT |
441 | } |
442 | ||
1d93a9cb | 443 | static bool |
d3c5ee6d JE |
444 | hashlimit_mt(const struct sk_buff *skb, const struct net_device *in, |
445 | const struct net_device *out, const struct xt_match *match, | |
446 | const void *matchinfo, int offset, unsigned int protoff, | |
447 | bool *hotdrop) | |
1da177e4 | 448 | { |
a47362a2 JE |
449 | const struct xt_hashlimit_info *r = |
450 | ((const struct xt_hashlimit_info *)matchinfo)->u.master; | |
39b46fc6 | 451 | struct xt_hashlimit_htable *hinfo = r->hinfo; |
1da177e4 LT |
452 | unsigned long now = jiffies; |
453 | struct dsthash_ent *dh; | |
454 | struct dsthash_dst dst; | |
455 | ||
39b46fc6 PM |
456 | if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0) |
457 | goto hotdrop; | |
1da177e4 LT |
458 | |
459 | spin_lock_bh(&hinfo->lock); | |
39b46fc6 | 460 | dh = dsthash_find(hinfo, &dst); |
1da177e4 | 461 | if (!dh) { |
39b46fc6 | 462 | dh = dsthash_alloc_init(hinfo, &dst); |
1da177e4 | 463 | if (!dh) { |
1da177e4 | 464 | spin_unlock_bh(&hinfo->lock); |
39b46fc6 | 465 | goto hotdrop; |
1da177e4 LT |
466 | } |
467 | ||
468 | dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire); | |
1da177e4 | 469 | dh->rateinfo.prev = jiffies; |
39b46fc6 PM |
470 | dh->rateinfo.credit = user2credits(hinfo->cfg.avg * |
471 | hinfo->cfg.burst); | |
472 | dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * | |
473 | hinfo->cfg.burst); | |
1da177e4 | 474 | dh->rateinfo.cost = user2credits(hinfo->cfg.avg); |
1c7628bd PM |
475 | } else { |
476 | /* update expiration timeout */ | |
477 | dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire); | |
478 | rateinfo_recalc(dh, now); | |
1da177e4 LT |
479 | } |
480 | ||
1da177e4 LT |
481 | if (dh->rateinfo.credit >= dh->rateinfo.cost) { |
482 | /* We're underlimit. */ | |
483 | dh->rateinfo.credit -= dh->rateinfo.cost; | |
484 | spin_unlock_bh(&hinfo->lock); | |
1d93a9cb | 485 | return true; |
1da177e4 LT |
486 | } |
487 | ||
601e68e1 | 488 | spin_unlock_bh(&hinfo->lock); |
1da177e4 LT |
489 | |
490 | /* default case: we're overlimit, thus don't match */ | |
1d93a9cb | 491 | return false; |
39b46fc6 PM |
492 | |
493 | hotdrop: | |
cff533ac | 494 | *hotdrop = true; |
1d93a9cb | 495 | return false; |
1da177e4 LT |
496 | } |
497 | ||
ccb79bdc | 498 | static bool |
d3c5ee6d JE |
499 | hashlimit_mt_check(const char *tablename, const void *inf, |
500 | const struct xt_match *match, void *matchinfo, | |
501 | unsigned int hook_mask) | |
1da177e4 | 502 | { |
39b46fc6 | 503 | struct xt_hashlimit_info *r = matchinfo; |
1da177e4 | 504 | |
1da177e4 | 505 | /* Check for overflow. */ |
39b46fc6 PM |
506 | if (r->cfg.burst == 0 || |
507 | user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) { | |
508 | printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n", | |
1da177e4 | 509 | r->cfg.avg, r->cfg.burst); |
ccb79bdc | 510 | return false; |
1da177e4 | 511 | } |
39b46fc6 PM |
512 | if (r->cfg.mode == 0 || |
513 | r->cfg.mode > (XT_HASHLIMIT_HASH_DPT | | |
514 | XT_HASHLIMIT_HASH_DIP | | |
515 | XT_HASHLIMIT_HASH_SIP | | |
516 | XT_HASHLIMIT_HASH_SPT)) | |
ccb79bdc | 517 | return false; |
1da177e4 | 518 | if (!r->cfg.gc_interval) |
ccb79bdc | 519 | return false; |
1da177e4 | 520 | if (!r->cfg.expire) |
ccb79bdc | 521 | return false; |
3ab72088 | 522 | if (r->name[sizeof(r->name) - 1] != '\0') |
ccb79bdc | 523 | return false; |
3ab72088 | 524 | |
1da177e4 | 525 | /* This is the best we've got: We cannot release and re-grab lock, |
39b46fc6 PM |
526 | * since checkentry() is called before x_tables.c grabs xt_mutex. |
527 | * We also cannot grab the hashtable spinlock, since htable_create will | |
1da177e4 LT |
528 | * call vmalloc, and that can sleep. And we cannot just re-search |
529 | * the list of htable's in htable_create(), since then we would | |
530 | * create duplicate proc files. -HW */ | |
14cc3e2b | 531 | mutex_lock(&hlimit_mutex); |
39b46fc6 PM |
532 | r->hinfo = htable_find_get(r->name, match->family); |
533 | if (!r->hinfo && htable_create(r, match->family) != 0) { | |
14cc3e2b | 534 | mutex_unlock(&hlimit_mutex); |
ccb79bdc | 535 | return false; |
1da177e4 | 536 | } |
14cc3e2b | 537 | mutex_unlock(&hlimit_mutex); |
1da177e4 LT |
538 | |
539 | /* Ugly hack: For SMP, we only want to use one set */ | |
540 | r->u.master = r; | |
ccb79bdc | 541 | return true; |
1da177e4 LT |
542 | } |
543 | ||
544 | static void | |
d3c5ee6d | 545 | hashlimit_mt_destroy(const struct xt_match *match, void *matchinfo) |
1da177e4 | 546 | { |
a47362a2 | 547 | const struct xt_hashlimit_info *r = matchinfo; |
1da177e4 LT |
548 | |
549 | htable_put(r->hinfo); | |
550 | } | |
551 | ||
127f15dd | 552 | #ifdef CONFIG_COMPAT |
39b46fc6 | 553 | struct compat_xt_hashlimit_info { |
127f15dd PM |
554 | char name[IFNAMSIZ]; |
555 | struct hashlimit_cfg cfg; | |
556 | compat_uptr_t hinfo; | |
557 | compat_uptr_t master; | |
558 | }; | |
559 | ||
d3c5ee6d | 560 | static void hashlimit_mt_compat_from_user(void *dst, void *src) |
127f15dd | 561 | { |
39b46fc6 | 562 | int off = offsetof(struct compat_xt_hashlimit_info, hinfo); |
127f15dd PM |
563 | |
564 | memcpy(dst, src, off); | |
39b46fc6 | 565 | memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off); |
127f15dd PM |
566 | } |
567 | ||
d3c5ee6d | 568 | static int hashlimit_mt_compat_to_user(void __user *dst, void *src) |
127f15dd | 569 | { |
39b46fc6 | 570 | int off = offsetof(struct compat_xt_hashlimit_info, hinfo); |
127f15dd PM |
571 | |
572 | return copy_to_user(dst, src, off) ? -EFAULT : 0; | |
573 | } | |
574 | #endif | |
575 | ||
d3c5ee6d | 576 | static struct xt_match hashlimit_mt_reg[] __read_mostly = { |
39b46fc6 PM |
577 | { |
578 | .name = "hashlimit", | |
579 | .family = AF_INET, | |
d3c5ee6d | 580 | .match = hashlimit_mt, |
39b46fc6 PM |
581 | .matchsize = sizeof(struct xt_hashlimit_info), |
582 | #ifdef CONFIG_COMPAT | |
583 | .compatsize = sizeof(struct compat_xt_hashlimit_info), | |
d3c5ee6d JE |
584 | .compat_from_user = hashlimit_mt_compat_from_user, |
585 | .compat_to_user = hashlimit_mt_compat_to_user, | |
39b46fc6 | 586 | #endif |
d3c5ee6d JE |
587 | .checkentry = hashlimit_mt_check, |
588 | .destroy = hashlimit_mt_destroy, | |
39b46fc6 PM |
589 | .me = THIS_MODULE |
590 | }, | |
591 | { | |
592 | .name = "hashlimit", | |
593 | .family = AF_INET6, | |
d3c5ee6d | 594 | .match = hashlimit_mt, |
39b46fc6 | 595 | .matchsize = sizeof(struct xt_hashlimit_info), |
127f15dd | 596 | #ifdef CONFIG_COMPAT |
39b46fc6 | 597 | .compatsize = sizeof(struct compat_xt_hashlimit_info), |
d3c5ee6d JE |
598 | .compat_from_user = hashlimit_mt_compat_from_user, |
599 | .compat_to_user = hashlimit_mt_compat_to_user, | |
127f15dd | 600 | #endif |
d3c5ee6d JE |
601 | .checkentry = hashlimit_mt_check, |
602 | .destroy = hashlimit_mt_destroy, | |
39b46fc6 PM |
603 | .me = THIS_MODULE |
604 | }, | |
1da177e4 LT |
605 | }; |
606 | ||
607 | /* PROC stuff */ | |
1da177e4 LT |
608 | static void *dl_seq_start(struct seq_file *s, loff_t *pos) |
609 | { | |
610 | struct proc_dir_entry *pde = s->private; | |
39b46fc6 | 611 | struct xt_hashlimit_htable *htable = pde->data; |
1da177e4 LT |
612 | unsigned int *bucket; |
613 | ||
614 | spin_lock_bh(&htable->lock); | |
615 | if (*pos >= htable->cfg.size) | |
616 | return NULL; | |
617 | ||
618 | bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC); | |
619 | if (!bucket) | |
620 | return ERR_PTR(-ENOMEM); | |
621 | ||
622 | *bucket = *pos; | |
623 | return bucket; | |
624 | } | |
625 | ||
626 | static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
627 | { | |
628 | struct proc_dir_entry *pde = s->private; | |
39b46fc6 | 629 | struct xt_hashlimit_htable *htable = pde->data; |
1da177e4 LT |
630 | unsigned int *bucket = (unsigned int *)v; |
631 | ||
632 | *pos = ++(*bucket); | |
633 | if (*pos >= htable->cfg.size) { | |
634 | kfree(v); | |
635 | return NULL; | |
636 | } | |
637 | return bucket; | |
638 | } | |
639 | ||
640 | static void dl_seq_stop(struct seq_file *s, void *v) | |
641 | { | |
642 | struct proc_dir_entry *pde = s->private; | |
39b46fc6 | 643 | struct xt_hashlimit_htable *htable = pde->data; |
1da177e4 LT |
644 | unsigned int *bucket = (unsigned int *)v; |
645 | ||
646 | kfree(bucket); | |
1da177e4 LT |
647 | spin_unlock_bh(&htable->lock); |
648 | } | |
649 | ||
39b46fc6 PM |
650 | static int dl_seq_real_show(struct dsthash_ent *ent, int family, |
651 | struct seq_file *s) | |
1da177e4 LT |
652 | { |
653 | /* recalculate to show accurate numbers */ | |
654 | rateinfo_recalc(ent, jiffies); | |
655 | ||
39b46fc6 PM |
656 | switch (family) { |
657 | case AF_INET: | |
658 | return seq_printf(s, "%ld %u.%u.%u.%u:%u->" | |
659 | "%u.%u.%u.%u:%u %u %u %u\n", | |
660 | (long)(ent->expires - jiffies)/HZ, | |
661 | NIPQUAD(ent->dst.addr.ip.src), | |
662 | ntohs(ent->dst.src_port), | |
663 | NIPQUAD(ent->dst.addr.ip.dst), | |
664 | ntohs(ent->dst.dst_port), | |
665 | ent->rateinfo.credit, ent->rateinfo.credit_cap, | |
666 | ent->rateinfo.cost); | |
667 | case AF_INET6: | |
668 | return seq_printf(s, "%ld " NIP6_FMT ":%u->" | |
669 | NIP6_FMT ":%u %u %u %u\n", | |
670 | (long)(ent->expires - jiffies)/HZ, | |
671 | NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.src), | |
672 | ntohs(ent->dst.src_port), | |
673 | NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.dst), | |
674 | ntohs(ent->dst.dst_port), | |
675 | ent->rateinfo.credit, ent->rateinfo.credit_cap, | |
676 | ent->rateinfo.cost); | |
677 | default: | |
678 | BUG(); | |
679 | return 0; | |
680 | } | |
1da177e4 LT |
681 | } |
682 | ||
683 | static int dl_seq_show(struct seq_file *s, void *v) | |
684 | { | |
685 | struct proc_dir_entry *pde = s->private; | |
39b46fc6 | 686 | struct xt_hashlimit_htable *htable = pde->data; |
1da177e4 LT |
687 | unsigned int *bucket = (unsigned int *)v; |
688 | struct dsthash_ent *ent; | |
689 | struct hlist_node *pos; | |
690 | ||
39b46fc6 PM |
691 | if (!hlist_empty(&htable->hash[*bucket])) { |
692 | hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node) | |
693 | if (dl_seq_real_show(ent, htable->family, s)) | |
1da177e4 | 694 | return 1; |
39b46fc6 | 695 | } |
1da177e4 LT |
696 | return 0; |
697 | } | |
698 | ||
56b3d975 | 699 | static const struct seq_operations dl_seq_ops = { |
1da177e4 LT |
700 | .start = dl_seq_start, |
701 | .next = dl_seq_next, | |
702 | .stop = dl_seq_stop, | |
703 | .show = dl_seq_show | |
704 | }; | |
705 | ||
706 | static int dl_proc_open(struct inode *inode, struct file *file) | |
707 | { | |
708 | int ret = seq_open(file, &dl_seq_ops); | |
709 | ||
710 | if (!ret) { | |
711 | struct seq_file *sf = file->private_data; | |
712 | sf->private = PDE(inode); | |
713 | } | |
714 | return ret; | |
715 | } | |
716 | ||
da7071d7 | 717 | static const struct file_operations dl_file_ops = { |
1da177e4 LT |
718 | .owner = THIS_MODULE, |
719 | .open = dl_proc_open, | |
720 | .read = seq_read, | |
721 | .llseek = seq_lseek, | |
722 | .release = seq_release | |
723 | }; | |
724 | ||
d3c5ee6d | 725 | static int __init hashlimit_mt_init(void) |
1da177e4 | 726 | { |
39b46fc6 | 727 | int err; |
1da177e4 | 728 | |
d3c5ee6d JE |
729 | err = xt_register_matches(hashlimit_mt_reg, |
730 | ARRAY_SIZE(hashlimit_mt_reg)); | |
39b46fc6 PM |
731 | if (err < 0) |
732 | goto err1; | |
1da177e4 | 733 | |
39b46fc6 PM |
734 | err = -ENOMEM; |
735 | hashlimit_cachep = kmem_cache_create("xt_hashlimit", | |
736 | sizeof(struct dsthash_ent), 0, 0, | |
20c2df83 | 737 | NULL); |
1da177e4 | 738 | if (!hashlimit_cachep) { |
39b46fc6 PM |
739 | printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n"); |
740 | goto err2; | |
1da177e4 | 741 | } |
457c4cbc | 742 | hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", init_net.proc_net); |
39b46fc6 PM |
743 | if (!hashlimit_procdir4) { |
744 | printk(KERN_ERR "xt_hashlimit: unable to create proc dir " | |
745 | "entry\n"); | |
746 | goto err3; | |
1da177e4 | 747 | } |
457c4cbc | 748 | hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", init_net.proc_net); |
39b46fc6 | 749 | if (!hashlimit_procdir6) { |
9c2440bb | 750 | printk(KERN_ERR "xt_hashlimit: unable to create proc dir " |
39b46fc6 PM |
751 | "entry\n"); |
752 | goto err4; | |
753 | } | |
754 | return 0; | |
755 | err4: | |
457c4cbc | 756 | remove_proc_entry("ipt_hashlimit", init_net.proc_net); |
39b46fc6 | 757 | err3: |
1da177e4 | 758 | kmem_cache_destroy(hashlimit_cachep); |
39b46fc6 | 759 | err2: |
d3c5ee6d | 760 | xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg)); |
39b46fc6 PM |
761 | err1: |
762 | return err; | |
1da177e4 | 763 | |
1da177e4 LT |
764 | } |
765 | ||
d3c5ee6d | 766 | static void __exit hashlimit_mt_exit(void) |
1da177e4 | 767 | { |
457c4cbc EB |
768 | remove_proc_entry("ipt_hashlimit", init_net.proc_net); |
769 | remove_proc_entry("ip6t_hashlimit", init_net.proc_net); | |
39b46fc6 | 770 | kmem_cache_destroy(hashlimit_cachep); |
d3c5ee6d | 771 | xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg)); |
1da177e4 LT |
772 | } |
773 | ||
d3c5ee6d JE |
774 | module_init(hashlimit_mt_init); |
775 | module_exit(hashlimit_mt_exit); |