blob: d9418a26781202e5a2b927b211f9976a14375a10 [file] [log] [blame]
Jan Engelhardt09e410d2008-01-31 04:48:13 -08001/*
2 * xt_hashlimit - Netfilter module to limit the number of packets per time
3 * seperately for each hashbucket (sourceip/sourceport/dstip/dstport)
Patrick McHardy39b46fc2006-11-29 02:35:36 +01004 *
Jan Engelhardt09e410d2008-01-31 04:48:13 -08005 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
6 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
Patrick McHardy39b46fc2006-11-29 02:35:36 +01007 *
8 * Development of this code was funded by Astaro AG, http://www.astaro.com/
9 */
10#include <linux/module.h>
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>
16#include <linux/proc_fs.h>
17#include <linux/seq_file.h>
18#include <linux/list.h>
19#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050020#include <linux/mm.h>
Patrick McHardy39b46fc2006-11-29 02:35:36 +010021#include <linux/in.h>
22#include <linux/ip.h>
Eric Dumazet7b21e092007-12-17 22:45:28 -080023#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Patrick McHardy39b46fc2006-11-29 02:35:36 +010024#include <linux/ipv6.h>
Patrick McHardy193b23c2007-12-04 23:51:48 -080025#include <net/ipv6.h>
Eric Dumazet7b21e092007-12-17 22:45:28 -080026#endif
27
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028#include <net/net_namespace.h>
Patrick McHardy39b46fc2006-11-29 02:35:36 +010029
30#include <linux/netfilter/x_tables.h>
31#include <linux/netfilter_ipv4/ip_tables.h>
32#include <linux/netfilter_ipv6/ip6_tables.h>
33#include <linux/netfilter/xt_hashlimit.h>
34#include <linux/mutex.h>
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt09e410d2008-01-31 04:48:13 -080038MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080039MODULE_DESCRIPTION("Xtables: per hash-bucket rate-limit match");
Patrick McHardy39b46fc2006-11-29 02:35:36 +010040MODULE_ALIAS("ipt_hashlimit");
41MODULE_ALIAS("ip6t_hashlimit");
42
43/* need to declare this at the top */
44static struct proc_dir_entry *hashlimit_procdir4;
45static struct proc_dir_entry *hashlimit_procdir6;
Arjan van de Venda7071d2007-02-12 00:55:36 -080046static const struct file_operations dl_file_ops;
Patrick McHardy39b46fc2006-11-29 02:35:36 +010047
48/* hash table crap */
49struct dsthash_dst {
50 union {
51 struct {
52 __be32 src;
53 __be32 dst;
54 } ip;
Eric Dumazet7b21e092007-12-17 22:45:28 -080055#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Patrick McHardy39b46fc2006-11-29 02:35:36 +010056 struct {
57 __be32 src[4];
58 __be32 dst[4];
59 } ip6;
Eric Dumazet7b21e092007-12-17 22:45:28 -080060#endif
Jan Engelhardt09e410d2008-01-31 04:48:13 -080061 };
Patrick McHardy39b46fc2006-11-29 02:35:36 +010062 __be16 src_port;
63 __be16 dst_port;
64};
65
66struct dsthash_ent {
67 /* static / read-only parts in the beginning */
68 struct hlist_node node;
69 struct dsthash_dst dst;
70
71 /* modified structure members in the end */
72 unsigned long expires; /* precalculated expiry time */
73 struct {
74 unsigned long prev; /* last modification */
75 u_int32_t credit;
76 u_int32_t credit_cap, cost;
77 } rateinfo;
78};
79
80struct xt_hashlimit_htable {
81 struct hlist_node node; /* global list of all htables */
82 atomic_t use;
83 int family;
84
Jan Engelhardt09e410d2008-01-31 04:48:13 -080085 struct hashlimit_cfg1 cfg; /* config */
Patrick McHardy39b46fc2006-11-29 02:35:36 +010086
87 /* used internally */
88 spinlock_t lock; /* lock for list_head */
89 u_int32_t rnd; /* random seed for hash */
90 int rnd_initialized;
91 unsigned int count; /* number entries in table */
92 struct timer_list timer; /* timer for gc */
93
94 /* seq_file stuff */
95 struct proc_dir_entry *pde;
96
97 struct hlist_head hash[0]; /* hashtable itself */
98};
99
100static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */
101static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */
102static HLIST_HEAD(hashlimit_htables);
Christoph Lametere18b8902006-12-06 20:33:20 -0800103static struct kmem_cache *hashlimit_cachep __read_mostly;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100104
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700105static inline bool dst_cmp(const struct dsthash_ent *ent,
Jan Engelhardta47362a2007-07-07 22:16:55 -0700106 const struct dsthash_dst *b)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100107{
108 return !memcmp(&ent->dst, b, sizeof(ent->dst));
109}
110
111static u_int32_t
112hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
113{
Eric Dumazete2f82ac2007-12-17 22:45:13 -0800114 u_int32_t hash = jhash2((const u32 *)dst,
115 sizeof(*dst)/sizeof(u32),
116 ht->rnd);
117 /*
118 * Instead of returning hash % ht->cfg.size (implying a divide)
119 * we return the high 32 bits of the (hash * ht->cfg.size) that will
120 * give results between [0 and cfg.size-1] and same hash distribution,
121 * but using a multiply, less expensive than a divide
122 */
123 return ((u64)hash * ht->cfg.size) >> 32;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100124}
125
126static struct dsthash_ent *
Jan Engelhardta47362a2007-07-07 22:16:55 -0700127dsthash_find(const struct xt_hashlimit_htable *ht,
128 const struct dsthash_dst *dst)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100129{
130 struct dsthash_ent *ent;
131 struct hlist_node *pos;
132 u_int32_t hash = hash_dst(ht, dst);
133
134 if (!hlist_empty(&ht->hash[hash])) {
135 hlist_for_each_entry(ent, pos, &ht->hash[hash], node)
136 if (dst_cmp(ent, dst))
137 return ent;
138 }
139 return NULL;
140}
141
142/* allocate dsthash_ent, initialize dst, put in htable and lock it */
143static struct dsthash_ent *
Jan Engelhardta47362a2007-07-07 22:16:55 -0700144dsthash_alloc_init(struct xt_hashlimit_htable *ht,
145 const struct dsthash_dst *dst)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100146{
147 struct dsthash_ent *ent;
148
149 /* initialize hash with random val at the time we allocate
150 * the first hashtable entry */
151 if (!ht->rnd_initialized) {
152 get_random_bytes(&ht->rnd, 4);
153 ht->rnd_initialized = 1;
154 }
155
156 if (ht->cfg.max && ht->count >= ht->cfg.max) {
157 /* FIXME: do something. question is what.. */
158 if (net_ratelimit())
159 printk(KERN_WARNING
160 "xt_hashlimit: max count of %u reached\n",
161 ht->cfg.max);
162 return NULL;
163 }
164
165 ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
166 if (!ent) {
167 if (net_ratelimit())
168 printk(KERN_ERR
169 "xt_hashlimit: can't allocate dsthash_ent\n");
170 return NULL;
171 }
172 memcpy(&ent->dst, dst, sizeof(ent->dst));
173
174 hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]);
175 ht->count++;
176 return ent;
177}
178
179static inline void
180dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
181{
182 hlist_del(&ent->node);
183 kmem_cache_free(hashlimit_cachep, ent);
184 ht->count--;
185}
186static void htable_gc(unsigned long htlong);
187
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800188static int htable_create_v0(struct xt_hashlimit_info *minfo, int family)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100189{
190 struct xt_hashlimit_htable *hinfo;
191 unsigned int size;
192 unsigned int i;
193
194 if (minfo->cfg.size)
195 size = minfo->cfg.size;
196 else {
197 size = ((num_physpages << PAGE_SHIFT) / 16384) /
198 sizeof(struct list_head);
199 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
200 size = 8192;
201 if (size < 16)
202 size = 16;
203 }
204 /* FIXME: don't use vmalloc() here or anywhere else -HW */
205 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
206 sizeof(struct list_head) * size);
207 if (!hinfo) {
208 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n");
209 return -1;
210 }
211 minfo->hinfo = hinfo;
212
213 /* copy match config into hashtable config */
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800214 hinfo->cfg.mode = minfo->cfg.mode;
215 hinfo->cfg.avg = minfo->cfg.avg;
216 hinfo->cfg.burst = minfo->cfg.burst;
217 hinfo->cfg.max = minfo->cfg.max;
218 hinfo->cfg.gc_interval = minfo->cfg.gc_interval;
219 hinfo->cfg.expire = minfo->cfg.expire;
220
221 if (family == AF_INET)
222 hinfo->cfg.srcmask = hinfo->cfg.dstmask = 32;
223 else
224 hinfo->cfg.srcmask = hinfo->cfg.dstmask = 128;
225
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100226 hinfo->cfg.size = size;
227 if (!hinfo->cfg.max)
228 hinfo->cfg.max = 8 * hinfo->cfg.size;
229 else if (hinfo->cfg.max < hinfo->cfg.size)
230 hinfo->cfg.max = hinfo->cfg.size;
231
232 for (i = 0; i < hinfo->cfg.size; i++)
233 INIT_HLIST_HEAD(&hinfo->hash[i]);
234
235 atomic_set(&hinfo->use, 1);
236 hinfo->count = 0;
237 hinfo->family = family;
238 hinfo->rnd_initialized = 0;
239 spin_lock_init(&hinfo->lock);
Denis V. Lunev6e79d852008-05-02 02:45:42 -0700240 hinfo->pde =
241 proc_create_data(minfo->name, 0,
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700242 family == AF_INET ? hashlimit_procdir4 :
243 hashlimit_procdir6,
Denis V. Lunev6e79d852008-05-02 02:45:42 -0700244 &dl_file_ops, hinfo);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100245 if (!hinfo->pde) {
246 vfree(hinfo);
247 return -1;
248 }
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100249
Patrick McHardye6f689d2007-03-23 11:16:30 -0700250 setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100251 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100252 add_timer(&hinfo->timer);
253
254 spin_lock_bh(&hashlimit_lock);
255 hlist_add_head(&hinfo->node, &hashlimit_htables);
256 spin_unlock_bh(&hashlimit_lock);
257
258 return 0;
259}
260
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800261static int htable_create(struct xt_hashlimit_mtinfo1 *minfo,
262 unsigned int family)
263{
264 struct xt_hashlimit_htable *hinfo;
265 unsigned int size;
266 unsigned int i;
267
268 if (minfo->cfg.size) {
269 size = minfo->cfg.size;
270 } else {
271 size = (num_physpages << PAGE_SHIFT) / 16384 /
272 sizeof(struct list_head);
273 if (num_physpages > 1024 * 1024 * 1024 / PAGE_SIZE)
274 size = 8192;
275 if (size < 16)
276 size = 16;
277 }
278 /* FIXME: don't use vmalloc() here or anywhere else -HW */
279 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
280 sizeof(struct list_head) * size);
281 if (hinfo == NULL) {
282 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n");
283 return -1;
284 }
285 minfo->hinfo = hinfo;
286
287 /* copy match config into hashtable config */
288 memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
289 hinfo->cfg.size = size;
290 if (hinfo->cfg.max == 0)
291 hinfo->cfg.max = 8 * hinfo->cfg.size;
292 else if (hinfo->cfg.max < hinfo->cfg.size)
293 hinfo->cfg.max = hinfo->cfg.size;
294
295 for (i = 0; i < hinfo->cfg.size; i++)
296 INIT_HLIST_HEAD(&hinfo->hash[i]);
297
298 atomic_set(&hinfo->use, 1);
299 hinfo->count = 0;
300 hinfo->family = family;
301 hinfo->rnd_initialized = 0;
302 spin_lock_init(&hinfo->lock);
303
Denis V. Lunev6e79d852008-05-02 02:45:42 -0700304 hinfo->pde =
305 proc_create_data(minfo->name, 0,
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700306 family == AF_INET ? hashlimit_procdir4 :
307 hashlimit_procdir6,
Denis V. Lunev6e79d852008-05-02 02:45:42 -0700308 &dl_file_ops, hinfo);
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800309 if (hinfo->pde == NULL) {
310 vfree(hinfo);
311 return -1;
312 }
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800313
314 setup_timer(&hinfo->timer, htable_gc, (unsigned long)hinfo);
315 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
316 add_timer(&hinfo->timer);
317
318 spin_lock_bh(&hashlimit_lock);
319 hlist_add_head(&hinfo->node, &hashlimit_htables);
320 spin_unlock_bh(&hashlimit_lock);
321
322 return 0;
323}
324
Jan Engelhardta47362a2007-07-07 22:16:55 -0700325static bool select_all(const struct xt_hashlimit_htable *ht,
326 const struct dsthash_ent *he)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100327{
328 return 1;
329}
330
Jan Engelhardta47362a2007-07-07 22:16:55 -0700331static bool select_gc(const struct xt_hashlimit_htable *ht,
332 const struct dsthash_ent *he)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100333{
Eric Dumazetcbebc512007-12-12 11:11:28 -0800334 return time_after_eq(jiffies, he->expires);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100335}
336
337static void htable_selective_cleanup(struct xt_hashlimit_htable *ht,
Jan Engelhardta47362a2007-07-07 22:16:55 -0700338 bool (*select)(const struct xt_hashlimit_htable *ht,
339 const struct dsthash_ent *he))
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100340{
341 unsigned int i;
342
343 /* lock hash table and iterate over it */
344 spin_lock_bh(&ht->lock);
345 for (i = 0; i < ht->cfg.size; i++) {
346 struct dsthash_ent *dh;
347 struct hlist_node *pos, *n;
348 hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) {
349 if ((*select)(ht, dh))
350 dsthash_free(ht, dh);
351 }
352 }
353 spin_unlock_bh(&ht->lock);
354}
355
356/* hash table garbage collector, run by timer */
357static void htable_gc(unsigned long htlong)
358{
359 struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong;
360
361 htable_selective_cleanup(ht, select_gc);
362
363 /* re-add the timer accordingly */
364 ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval);
365 add_timer(&ht->timer);
366}
367
368static void htable_destroy(struct xt_hashlimit_htable *hinfo)
369{
Pavel Emelyanov967ab992008-07-31 00:38:52 -0700370 del_timer_sync(&hinfo->timer);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100371
372 /* remove proc entry */
373 remove_proc_entry(hinfo->pde->name,
374 hinfo->family == AF_INET ? hashlimit_procdir4 :
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800375 hashlimit_procdir6);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100376 htable_selective_cleanup(hinfo, select_all);
377 vfree(hinfo);
378}
379
Jan Engelhardta47362a2007-07-07 22:16:55 -0700380static struct xt_hashlimit_htable *htable_find_get(const char *name,
381 int family)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100382{
383 struct xt_hashlimit_htable *hinfo;
384 struct hlist_node *pos;
385
386 spin_lock_bh(&hashlimit_lock);
387 hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) {
388 if (!strcmp(name, hinfo->pde->name) &&
389 hinfo->family == family) {
390 atomic_inc(&hinfo->use);
391 spin_unlock_bh(&hashlimit_lock);
392 return hinfo;
393 }
394 }
395 spin_unlock_bh(&hashlimit_lock);
396 return NULL;
397}
398
399static void htable_put(struct xt_hashlimit_htable *hinfo)
400{
401 if (atomic_dec_and_test(&hinfo->use)) {
402 spin_lock_bh(&hashlimit_lock);
403 hlist_del(&hinfo->node);
404 spin_unlock_bh(&hashlimit_lock);
405 htable_destroy(hinfo);
406 }
407}
408
409/* The algorithm used is the Simple Token Bucket Filter (TBF)
410 * see net/sched/sch_tbf.c in the linux source tree
411 */
412
413/* Rusty: This is my (non-mathematically-inclined) understanding of
414 this algorithm. The `average rate' in jiffies becomes your initial
415 amount of credit `credit' and the most credit you can ever have
416 `credit_cap'. The `peak rate' becomes the cost of passing the
417 test, `cost'.
418
419 `prev' tracks the last packet hit: you gain one credit per jiffy.
420 If you get credit balance more than this, the extra credit is
421 discarded. Every time the match passes, you lose `cost' credits;
422 if you don't have that many, the test fails.
423
424 See Alexey's formal explanation in net/sched/sch_tbf.c.
425
426 To get the maximum range, we multiply by this factor (ie. you get N
427 credits per jiffy). We want to allow a rate as low as 1 per day
428 (slowest userspace tool allows), which means
429 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
430*/
431#define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
432
433/* Repeated shift and or gives us all 1s, final shift and add 1 gives
434 * us the power of 2 below the theoretical max, so GCC simply does a
435 * shift. */
436#define _POW2_BELOW2(x) ((x)|((x)>>1))
437#define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
438#define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
439#define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
440#define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
441#define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
442
443#define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
444
445/* Precision saver. */
446static inline u_int32_t
447user2credits(u_int32_t user)
448{
449 /* If multiplying would overflow... */
450 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
451 /* Divide first. */
452 return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
453
454 return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE;
455}
456
457static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
458{
459 dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY;
460 if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
461 dh->rateinfo.credit = dh->rateinfo.credit_cap;
462 dh->rateinfo.prev = now;
463}
464
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800465static inline __be32 maskl(__be32 a, unsigned int l)
466{
Patrick McHardy1b9b70e2008-04-09 15:14:18 -0700467 return l ? htonl(ntohl(a) & ~0 << (32 - l)) : 0;
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800468}
469
Pavel Emelyanov3ed5df42008-01-31 18:42:26 -0800470#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800471static void hashlimit_ipv6_mask(__be32 *i, unsigned int p)
472{
473 switch (p) {
Patrick McHardy1b9b70e2008-04-09 15:14:18 -0700474 case 0 ... 31:
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800475 i[0] = maskl(i[0], p);
476 i[1] = i[2] = i[3] = 0;
477 break;
Patrick McHardy1b9b70e2008-04-09 15:14:18 -0700478 case 32 ... 63:
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800479 i[1] = maskl(i[1], p - 32);
480 i[2] = i[3] = 0;
481 break;
Patrick McHardy1b9b70e2008-04-09 15:14:18 -0700482 case 64 ... 95:
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800483 i[2] = maskl(i[2], p - 64);
484 i[3] = 0;
Patrick McHardy1b9b70e2008-04-09 15:14:18 -0700485 case 96 ... 127:
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800486 i[3] = maskl(i[3], p - 96);
487 break;
488 case 128:
489 break;
490 }
491}
Pavel Emelyanov3ed5df42008-01-31 18:42:26 -0800492#endif
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800493
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100494static int
Jan Engelhardta47362a2007-07-07 22:16:55 -0700495hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo,
496 struct dsthash_dst *dst,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100497 const struct sk_buff *skb, unsigned int protoff)
498{
499 __be16 _ports[2], *ports;
Patrick McHardy193b23c2007-12-04 23:51:48 -0800500 u8 nexthdr;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100501
502 memset(dst, 0, sizeof(*dst));
503
504 switch (hinfo->family) {
505 case AF_INET:
506 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800507 dst->ip.dst = maskl(ip_hdr(skb)->daddr,
508 hinfo->cfg.dstmask);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100509 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800510 dst->ip.src = maskl(ip_hdr(skb)->saddr,
511 hinfo->cfg.srcmask);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100512
513 if (!(hinfo->cfg.mode &
514 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
515 return 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700516 nexthdr = ip_hdr(skb)->protocol;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100517 break;
Yasuyuki Kozakai02dba022006-12-02 22:19:01 -0800518#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100519 case AF_INET6:
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800520 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) {
521 memcpy(&dst->ip6.dst, &ipv6_hdr(skb)->daddr,
522 sizeof(dst->ip6.dst));
523 hashlimit_ipv6_mask(dst->ip6.dst, hinfo->cfg.dstmask);
524 }
525 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) {
526 memcpy(&dst->ip6.src, &ipv6_hdr(skb)->saddr,
527 sizeof(dst->ip6.src));
528 hashlimit_ipv6_mask(dst->ip6.src, hinfo->cfg.srcmask);
529 }
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100530
531 if (!(hinfo->cfg.mode &
532 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
533 return 0;
Patrick McHardy193b23c2007-12-04 23:51:48 -0800534 nexthdr = ipv6_hdr(skb)->nexthdr;
535 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr);
536 if ((int)protoff < 0)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100537 return -1;
538 break;
539#endif
540 default:
541 BUG();
542 return 0;
543 }
544
545 switch (nexthdr) {
546 case IPPROTO_TCP:
547 case IPPROTO_UDP:
Patrick McHardya8d0f952007-02-07 15:07:43 -0800548 case IPPROTO_UDPLITE:
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100549 case IPPROTO_SCTP:
550 case IPPROTO_DCCP:
551 ports = skb_header_pointer(skb, protoff, sizeof(_ports),
552 &_ports);
553 break;
554 default:
555 _ports[0] = _ports[1] = 0;
556 ports = _ports;
557 break;
558 }
559 if (!ports)
560 return -1;
561 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT)
562 dst->src_port = ports[0];
563 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT)
564 dst->dst_port = ports[1];
565 return 0;
566}
567
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700568static bool
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800569hashlimit_mt_v0(const struct sk_buff *skb, const struct net_device *in,
570 const struct net_device *out, const struct xt_match *match,
571 const void *matchinfo, int offset, unsigned int protoff,
572 bool *hotdrop)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100573{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700574 const struct xt_hashlimit_info *r =
575 ((const struct xt_hashlimit_info *)matchinfo)->u.master;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100576 struct xt_hashlimit_htable *hinfo = r->hinfo;
577 unsigned long now = jiffies;
578 struct dsthash_ent *dh;
579 struct dsthash_dst dst;
580
581 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0)
582 goto hotdrop;
583
584 spin_lock_bh(&hinfo->lock);
585 dh = dsthash_find(hinfo, &dst);
586 if (!dh) {
587 dh = dsthash_alloc_init(hinfo, &dst);
588 if (!dh) {
589 spin_unlock_bh(&hinfo->lock);
590 goto hotdrop;
591 }
592
593 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
594 dh->rateinfo.prev = jiffies;
595 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
596 hinfo->cfg.burst);
597 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
598 hinfo->cfg.burst);
599 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
600 } else {
601 /* update expiration timeout */
602 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
603 rateinfo_recalc(dh, now);
604 }
605
606 if (dh->rateinfo.credit >= dh->rateinfo.cost) {
607 /* We're underlimit. */
608 dh->rateinfo.credit -= dh->rateinfo.cost;
609 spin_unlock_bh(&hinfo->lock);
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700610 return true;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100611 }
612
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800613 spin_unlock_bh(&hinfo->lock);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100614
615 /* default case: we're overlimit, thus don't match */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700616 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100617
618hotdrop:
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700619 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700620 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100621}
622
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700623static bool
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800624hashlimit_mt(const struct sk_buff *skb, const struct net_device *in,
625 const struct net_device *out, const struct xt_match *match,
626 const void *matchinfo, int offset, unsigned int protoff,
627 bool *hotdrop)
628{
629 const struct xt_hashlimit_mtinfo1 *info = matchinfo;
630 struct xt_hashlimit_htable *hinfo = info->hinfo;
631 unsigned long now = jiffies;
632 struct dsthash_ent *dh;
633 struct dsthash_dst dst;
634
635 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0)
636 goto hotdrop;
637
638 spin_lock_bh(&hinfo->lock);
639 dh = dsthash_find(hinfo, &dst);
640 if (dh == NULL) {
641 dh = dsthash_alloc_init(hinfo, &dst);
642 if (dh == NULL) {
643 spin_unlock_bh(&hinfo->lock);
644 goto hotdrop;
645 }
646
647 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
648 dh->rateinfo.prev = jiffies;
649 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
650 hinfo->cfg.burst);
651 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
652 hinfo->cfg.burst);
653 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
654 } else {
655 /* update expiration timeout */
656 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
657 rateinfo_recalc(dh, now);
658 }
659
660 if (dh->rateinfo.credit >= dh->rateinfo.cost) {
661 /* below the limit */
662 dh->rateinfo.credit -= dh->rateinfo.cost;
663 spin_unlock_bh(&hinfo->lock);
664 return !(info->cfg.mode & XT_HASHLIMIT_INVERT);
665 }
666
667 spin_unlock_bh(&hinfo->lock);
668 /* default match is underlimit - so over the limit, we need to invert */
669 return info->cfg.mode & XT_HASHLIMIT_INVERT;
670
671 hotdrop:
672 *hotdrop = true;
673 return false;
674}
675
676static bool
677hashlimit_mt_check_v0(const char *tablename, const void *inf,
678 const struct xt_match *match, void *matchinfo,
679 unsigned int hook_mask)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100680{
681 struct xt_hashlimit_info *r = matchinfo;
682
683 /* Check for overflow. */
684 if (r->cfg.burst == 0 ||
685 user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) {
686 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n",
687 r->cfg.avg, r->cfg.burst);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700688 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100689 }
690 if (r->cfg.mode == 0 ||
691 r->cfg.mode > (XT_HASHLIMIT_HASH_DPT |
692 XT_HASHLIMIT_HASH_DIP |
693 XT_HASHLIMIT_HASH_SIP |
694 XT_HASHLIMIT_HASH_SPT))
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700695 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100696 if (!r->cfg.gc_interval)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700697 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100698 if (!r->cfg.expire)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700699 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100700 if (r->name[sizeof(r->name) - 1] != '\0')
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700701 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100702
703 /* This is the best we've got: We cannot release and re-grab lock,
704 * since checkentry() is called before x_tables.c grabs xt_mutex.
705 * We also cannot grab the hashtable spinlock, since htable_create will
706 * call vmalloc, and that can sleep. And we cannot just re-search
707 * the list of htable's in htable_create(), since then we would
708 * create duplicate proc files. -HW */
709 mutex_lock(&hlimit_mutex);
710 r->hinfo = htable_find_get(r->name, match->family);
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800711 if (!r->hinfo && htable_create_v0(r, match->family) != 0) {
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100712 mutex_unlock(&hlimit_mutex);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700713 return false;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100714 }
715 mutex_unlock(&hlimit_mutex);
716
717 /* Ugly hack: For SMP, we only want to use one set */
718 r->u.master = r;
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700719 return true;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100720}
721
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800722static bool
723hashlimit_mt_check(const char *tablename, const void *inf,
724 const struct xt_match *match, void *matchinfo,
725 unsigned int hook_mask)
726{
727 struct xt_hashlimit_mtinfo1 *info = matchinfo;
728
729 /* Check for overflow. */
730 if (info->cfg.burst == 0 ||
731 user2credits(info->cfg.avg * info->cfg.burst) <
732 user2credits(info->cfg.avg)) {
733 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n",
734 info->cfg.avg, info->cfg.burst);
735 return false;
736 }
737 if (info->cfg.gc_interval == 0 || info->cfg.expire == 0)
738 return false;
739 if (info->name[sizeof(info->name)-1] != '\0')
740 return false;
741 if (match->family == AF_INET) {
742 if (info->cfg.srcmask > 32 || info->cfg.dstmask > 32)
743 return false;
744 } else {
745 if (info->cfg.srcmask > 128 || info->cfg.dstmask > 128)
746 return false;
747 }
748
749 /* This is the best we've got: We cannot release and re-grab lock,
750 * since checkentry() is called before x_tables.c grabs xt_mutex.
751 * We also cannot grab the hashtable spinlock, since htable_create will
752 * call vmalloc, and that can sleep. And we cannot just re-search
753 * the list of htable's in htable_create(), since then we would
754 * create duplicate proc files. -HW */
755 mutex_lock(&hlimit_mutex);
756 info->hinfo = htable_find_get(info->name, match->family);
757 if (!info->hinfo && htable_create(info, match->family) != 0) {
758 mutex_unlock(&hlimit_mutex);
759 return false;
760 }
761 mutex_unlock(&hlimit_mutex);
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800762 return true;
763}
764
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100765static void
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800766hashlimit_mt_destroy_v0(const struct xt_match *match, void *matchinfo)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100767{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700768 const struct xt_hashlimit_info *r = matchinfo;
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100769
770 htable_put(r->hinfo);
771}
772
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800773static void
774hashlimit_mt_destroy(const struct xt_match *match, void *matchinfo)
775{
776 const struct xt_hashlimit_mtinfo1 *info = matchinfo;
777
778 htable_put(info->hinfo);
779}
780
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100781#ifdef CONFIG_COMPAT
782struct compat_xt_hashlimit_info {
783 char name[IFNAMSIZ];
784 struct hashlimit_cfg cfg;
785 compat_uptr_t hinfo;
786 compat_uptr_t master;
787};
788
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800789static void hashlimit_mt_compat_from_user(void *dst, void *src)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100790{
791 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
792
793 memcpy(dst, src, off);
794 memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off);
795}
796
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800797static int hashlimit_mt_compat_to_user(void __user *dst, void *src)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100798{
799 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
800
801 return copy_to_user(dst, src, off) ? -EFAULT : 0;
802}
803#endif
804
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800805static struct xt_match hashlimit_mt_reg[] __read_mostly = {
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100806 {
807 .name = "hashlimit",
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800808 .revision = 0,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100809 .family = AF_INET,
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800810 .match = hashlimit_mt_v0,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100811 .matchsize = sizeof(struct xt_hashlimit_info),
812#ifdef CONFIG_COMPAT
813 .compatsize = sizeof(struct compat_xt_hashlimit_info),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800814 .compat_from_user = hashlimit_mt_compat_from_user,
815 .compat_to_user = hashlimit_mt_compat_to_user,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100816#endif
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800817 .checkentry = hashlimit_mt_check_v0,
818 .destroy = hashlimit_mt_destroy_v0,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100819 .me = THIS_MODULE
820 },
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800821 {
822 .name = "hashlimit",
823 .revision = 1,
824 .family = AF_INET,
825 .match = hashlimit_mt,
826 .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
827 .checkentry = hashlimit_mt_check,
828 .destroy = hashlimit_mt_destroy,
829 .me = THIS_MODULE,
830 },
Eric Dumazet7b21e092007-12-17 22:45:28 -0800831#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100832 {
833 .name = "hashlimit",
834 .family = AF_INET6,
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800835 .match = hashlimit_mt_v0,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100836 .matchsize = sizeof(struct xt_hashlimit_info),
837#ifdef CONFIG_COMPAT
838 .compatsize = sizeof(struct compat_xt_hashlimit_info),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800839 .compat_from_user = hashlimit_mt_compat_from_user,
840 .compat_to_user = hashlimit_mt_compat_to_user,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100841#endif
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800842 .checkentry = hashlimit_mt_check_v0,
843 .destroy = hashlimit_mt_destroy_v0,
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100844 .me = THIS_MODULE
845 },
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800846 {
847 .name = "hashlimit",
848 .revision = 1,
849 .family = AF_INET6,
850 .match = hashlimit_mt,
851 .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
852 .checkentry = hashlimit_mt_check,
853 .destroy = hashlimit_mt_destroy,
854 .me = THIS_MODULE,
855 },
Eric Dumazet7b21e092007-12-17 22:45:28 -0800856#endif
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100857};
858
859/* PROC stuff */
860static void *dl_seq_start(struct seq_file *s, loff_t *pos)
Stephen Hemmingerf4f6fb72008-01-31 04:08:39 -0800861 __acquires(htable->lock)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100862{
863 struct proc_dir_entry *pde = s->private;
864 struct xt_hashlimit_htable *htable = pde->data;
865 unsigned int *bucket;
866
867 spin_lock_bh(&htable->lock);
868 if (*pos >= htable->cfg.size)
869 return NULL;
870
871 bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
872 if (!bucket)
873 return ERR_PTR(-ENOMEM);
874
875 *bucket = *pos;
876 return bucket;
877}
878
879static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
880{
881 struct proc_dir_entry *pde = s->private;
882 struct xt_hashlimit_htable *htable = pde->data;
883 unsigned int *bucket = (unsigned int *)v;
884
885 *pos = ++(*bucket);
886 if (*pos >= htable->cfg.size) {
887 kfree(v);
888 return NULL;
889 }
890 return bucket;
891}
892
893static void dl_seq_stop(struct seq_file *s, void *v)
Stephen Hemmingerf4f6fb72008-01-31 04:08:39 -0800894 __releases(htable->lock)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100895{
896 struct proc_dir_entry *pde = s->private;
897 struct xt_hashlimit_htable *htable = pde->data;
898 unsigned int *bucket = (unsigned int *)v;
899
900 kfree(bucket);
901 spin_unlock_bh(&htable->lock);
902}
903
904static int dl_seq_real_show(struct dsthash_ent *ent, int family,
905 struct seq_file *s)
906{
907 /* recalculate to show accurate numbers */
908 rateinfo_recalc(ent, jiffies);
909
910 switch (family) {
911 case AF_INET:
912 return seq_printf(s, "%ld %u.%u.%u.%u:%u->"
913 "%u.%u.%u.%u:%u %u %u %u\n",
914 (long)(ent->expires - jiffies)/HZ,
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800915 NIPQUAD(ent->dst.ip.src),
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100916 ntohs(ent->dst.src_port),
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800917 NIPQUAD(ent->dst.ip.dst),
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100918 ntohs(ent->dst.dst_port),
919 ent->rateinfo.credit, ent->rateinfo.credit_cap,
920 ent->rateinfo.cost);
Eric Dumazet7b21e092007-12-17 22:45:28 -0800921#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100922 case AF_INET6:
923 return seq_printf(s, "%ld " NIP6_FMT ":%u->"
924 NIP6_FMT ":%u %u %u %u\n",
925 (long)(ent->expires - jiffies)/HZ,
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800926 NIP6(*(struct in6_addr *)&ent->dst.ip6.src),
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100927 ntohs(ent->dst.src_port),
Jan Engelhardt09e410d2008-01-31 04:48:13 -0800928 NIP6(*(struct in6_addr *)&ent->dst.ip6.dst),
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100929 ntohs(ent->dst.dst_port),
930 ent->rateinfo.credit, ent->rateinfo.credit_cap,
931 ent->rateinfo.cost);
Eric Dumazet7b21e092007-12-17 22:45:28 -0800932#endif
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100933 default:
934 BUG();
935 return 0;
936 }
937}
938
939static int dl_seq_show(struct seq_file *s, void *v)
940{
941 struct proc_dir_entry *pde = s->private;
942 struct xt_hashlimit_htable *htable = pde->data;
943 unsigned int *bucket = (unsigned int *)v;
944 struct dsthash_ent *ent;
945 struct hlist_node *pos;
946
947 if (!hlist_empty(&htable->hash[*bucket])) {
948 hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node)
949 if (dl_seq_real_show(ent, htable->family, s))
950 return 1;
951 }
952 return 0;
953}
954
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700955static const struct seq_operations dl_seq_ops = {
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100956 .start = dl_seq_start,
957 .next = dl_seq_next,
958 .stop = dl_seq_stop,
959 .show = dl_seq_show
960};
961
962static int dl_proc_open(struct inode *inode, struct file *file)
963{
964 int ret = seq_open(file, &dl_seq_ops);
965
966 if (!ret) {
967 struct seq_file *sf = file->private_data;
968 sf->private = PDE(inode);
969 }
970 return ret;
971}
972
Arjan van de Venda7071d2007-02-12 00:55:36 -0800973static const struct file_operations dl_file_ops = {
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100974 .owner = THIS_MODULE,
975 .open = dl_proc_open,
976 .read = seq_read,
977 .llseek = seq_lseek,
978 .release = seq_release
979};
980
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800981static int __init hashlimit_mt_init(void)
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100982{
983 int err;
984
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800985 err = xt_register_matches(hashlimit_mt_reg,
986 ARRAY_SIZE(hashlimit_mt_reg));
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100987 if (err < 0)
988 goto err1;
989
990 err = -ENOMEM;
991 hashlimit_cachep = kmem_cache_create("xt_hashlimit",
992 sizeof(struct dsthash_ent), 0, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900993 NULL);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100994 if (!hashlimit_cachep) {
995 printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n");
996 goto err2;
997 }
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200998 hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", init_net.proc_net);
Patrick McHardy39b46fc2006-11-29 02:35:36 +0100999 if (!hashlimit_procdir4) {
1000 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
1001 "entry\n");
1002 goto err3;
1003 }
Eric Dumazet7b21e092007-12-17 22:45:28 -08001004 err = 0;
1005#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001006 hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", init_net.proc_net);
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001007 if (!hashlimit_procdir6) {
Alexey Dobriyan9c2440b2007-01-02 00:42:00 -08001008 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001009 "entry\n");
Eric Dumazet7b21e092007-12-17 22:45:28 -08001010 err = -ENOMEM;
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001011 }
Eric Dumazet7b21e092007-12-17 22:45:28 -08001012#endif
1013 if (!err)
1014 return 0;
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001015 remove_proc_entry("ipt_hashlimit", init_net.proc_net);
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001016err3:
1017 kmem_cache_destroy(hashlimit_cachep);
1018err2:
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -08001019 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001020err1:
1021 return err;
1022
1023}
1024
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -08001025static void __exit hashlimit_mt_exit(void)
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001026{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001027 remove_proc_entry("ipt_hashlimit", init_net.proc_net);
Eric Dumazet7b21e092007-12-17 22:45:28 -08001028#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001029 remove_proc_entry("ip6t_hashlimit", init_net.proc_net);
Eric Dumazet7b21e092007-12-17 22:45:28 -08001030#endif
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001031 kmem_cache_destroy(hashlimit_cachep);
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -08001032 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
Patrick McHardy39b46fc2006-11-29 02:35:36 +01001033}
1034
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -08001035module_init(hashlimit_mt_init);
1036module_exit(hashlimit_mt_exit);