inotify: fix race
[linux-2.6.22.y-op.git] / net / netfilter / xt_hashlimit.c
blobd3043fa32ebc9c30715485299438060e33371fa0
1 /* iptables match extension to limit the number of packets per second
2 * seperately for each hashbucket (sourceip/sourceport/dstip/dstport)
4 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
6 * $Id: ipt_hashlimit.c 3244 2004-10-20 16:24:29Z laforge@netfilter.org $
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>
20 #include <linux/mm.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <linux/ipv6.h>
25 #include <linux/netfilter/x_tables.h>
26 #include <linux/netfilter_ipv4/ip_tables.h>
27 #include <linux/netfilter_ipv6/ip6_tables.h>
28 #include <linux/netfilter/xt_hashlimit.h>
29 #include <linux/mutex.h>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
33 MODULE_DESCRIPTION("iptables match for limiting per hash-bucket");
34 MODULE_ALIAS("ipt_hashlimit");
35 MODULE_ALIAS("ip6t_hashlimit");
37 /* need to declare this at the top */
38 static struct proc_dir_entry *hashlimit_procdir4;
39 static struct proc_dir_entry *hashlimit_procdir6;
40 static const struct file_operations dl_file_ops;
42 /* hash table crap */
43 struct dsthash_dst {
44 union {
45 struct {
46 __be32 src;
47 __be32 dst;
48 } ip;
49 struct {
50 __be32 src[4];
51 __be32 dst[4];
52 } ip6;
53 } addr;
54 __be16 src_port;
55 __be16 dst_port;
58 struct dsthash_ent {
59 /* static / read-only parts in the beginning */
60 struct hlist_node node;
61 struct dsthash_dst dst;
63 /* modified structure members in the end */
64 unsigned long expires; /* precalculated expiry time */
65 struct {
66 unsigned long prev; /* last modification */
67 u_int32_t credit;
68 u_int32_t credit_cap, cost;
69 } rateinfo;
72 struct xt_hashlimit_htable {
73 struct hlist_node node; /* global list of all htables */
74 atomic_t use;
75 int family;
77 struct hashlimit_cfg cfg; /* config */
79 /* used internally */
80 spinlock_t lock; /* lock for list_head */
81 u_int32_t rnd; /* random seed for hash */
82 int rnd_initialized;
83 unsigned int count; /* number entries in table */
84 struct timer_list timer; /* timer for gc */
86 /* seq_file stuff */
87 struct proc_dir_entry *pde;
89 struct hlist_head hash[0]; /* hashtable itself */
92 static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */
93 static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */
94 static HLIST_HEAD(hashlimit_htables);
95 static struct kmem_cache *hashlimit_cachep __read_mostly;
97 static inline int dst_cmp(const struct dsthash_ent *ent, struct dsthash_dst *b)
99 return !memcmp(&ent->dst, b, sizeof(ent->dst));
102 static u_int32_t
103 hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
105 return jhash(dst, sizeof(*dst), ht->rnd) % ht->cfg.size;
108 static struct dsthash_ent *
109 dsthash_find(const struct xt_hashlimit_htable *ht, struct dsthash_dst *dst)
111 struct dsthash_ent *ent;
112 struct hlist_node *pos;
113 u_int32_t hash = hash_dst(ht, dst);
115 if (!hlist_empty(&ht->hash[hash])) {
116 hlist_for_each_entry(ent, pos, &ht->hash[hash], node)
117 if (dst_cmp(ent, dst))
118 return ent;
120 return NULL;
123 /* allocate dsthash_ent, initialize dst, put in htable and lock it */
124 static struct dsthash_ent *
125 dsthash_alloc_init(struct xt_hashlimit_htable *ht, struct dsthash_dst *dst)
127 struct dsthash_ent *ent;
129 /* initialize hash with random val at the time we allocate
130 * the first hashtable entry */
131 if (!ht->rnd_initialized) {
132 get_random_bytes(&ht->rnd, 4);
133 ht->rnd_initialized = 1;
136 if (ht->cfg.max && ht->count >= ht->cfg.max) {
137 /* FIXME: do something. question is what.. */
138 if (net_ratelimit())
139 printk(KERN_WARNING
140 "xt_hashlimit: max count of %u reached\n",
141 ht->cfg.max);
142 return NULL;
145 ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
146 if (!ent) {
147 if (net_ratelimit())
148 printk(KERN_ERR
149 "xt_hashlimit: can't allocate dsthash_ent\n");
150 return NULL;
152 memcpy(&ent->dst, dst, sizeof(ent->dst));
154 hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]);
155 ht->count++;
156 return ent;
159 static inline void
160 dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
162 hlist_del(&ent->node);
163 kmem_cache_free(hashlimit_cachep, ent);
164 ht->count--;
166 static void htable_gc(unsigned long htlong);
168 static int htable_create(struct xt_hashlimit_info *minfo, int family)
170 struct xt_hashlimit_htable *hinfo;
171 unsigned int size;
172 unsigned int i;
174 if (minfo->cfg.size)
175 size = minfo->cfg.size;
176 else {
177 size = ((num_physpages << PAGE_SHIFT) / 16384) /
178 sizeof(struct list_head);
179 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
180 size = 8192;
181 if (size < 16)
182 size = 16;
184 /* FIXME: don't use vmalloc() here or anywhere else -HW */
185 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
186 sizeof(struct list_head) * size);
187 if (!hinfo) {
188 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n");
189 return -1;
191 minfo->hinfo = hinfo;
193 /* copy match config into hashtable config */
194 memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
195 hinfo->cfg.size = size;
196 if (!hinfo->cfg.max)
197 hinfo->cfg.max = 8 * hinfo->cfg.size;
198 else if (hinfo->cfg.max < hinfo->cfg.size)
199 hinfo->cfg.max = hinfo->cfg.size;
201 for (i = 0; i < hinfo->cfg.size; i++)
202 INIT_HLIST_HEAD(&hinfo->hash[i]);
204 atomic_set(&hinfo->use, 1);
205 hinfo->count = 0;
206 hinfo->family = family;
207 hinfo->rnd_initialized = 0;
208 spin_lock_init(&hinfo->lock);
209 hinfo->pde = create_proc_entry(minfo->name, 0,
210 family == AF_INET ? hashlimit_procdir4 :
211 hashlimit_procdir6);
212 if (!hinfo->pde) {
213 vfree(hinfo);
214 return -1;
216 hinfo->pde->proc_fops = &dl_file_ops;
217 hinfo->pde->data = hinfo;
219 setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo);
220 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
221 add_timer(&hinfo->timer);
223 spin_lock_bh(&hashlimit_lock);
224 hlist_add_head(&hinfo->node, &hashlimit_htables);
225 spin_unlock_bh(&hashlimit_lock);
227 return 0;
230 static int select_all(struct xt_hashlimit_htable *ht, struct dsthash_ent *he)
232 return 1;
235 static int select_gc(struct xt_hashlimit_htable *ht, struct dsthash_ent *he)
237 return (jiffies >= he->expires);
240 static void htable_selective_cleanup(struct xt_hashlimit_htable *ht,
241 int (*select)(struct xt_hashlimit_htable *ht,
242 struct dsthash_ent *he))
244 unsigned int i;
246 /* lock hash table and iterate over it */
247 spin_lock_bh(&ht->lock);
248 for (i = 0; i < ht->cfg.size; i++) {
249 struct dsthash_ent *dh;
250 struct hlist_node *pos, *n;
251 hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) {
252 if ((*select)(ht, dh))
253 dsthash_free(ht, dh);
256 spin_unlock_bh(&ht->lock);
259 /* hash table garbage collector, run by timer */
260 static void htable_gc(unsigned long htlong)
262 struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong;
264 htable_selective_cleanup(ht, select_gc);
266 /* re-add the timer accordingly */
267 ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval);
268 add_timer(&ht->timer);
271 static void htable_destroy(struct xt_hashlimit_htable *hinfo)
273 /* remove timer, if it is pending */
274 if (timer_pending(&hinfo->timer))
275 del_timer(&hinfo->timer);
277 /* remove proc entry */
278 remove_proc_entry(hinfo->pde->name,
279 hinfo->family == AF_INET ? hashlimit_procdir4 :
280 hashlimit_procdir6);
281 htable_selective_cleanup(hinfo, select_all);
282 vfree(hinfo);
285 static struct xt_hashlimit_htable *htable_find_get(char *name, int family)
287 struct xt_hashlimit_htable *hinfo;
288 struct hlist_node *pos;
290 spin_lock_bh(&hashlimit_lock);
291 hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) {
292 if (!strcmp(name, hinfo->pde->name) &&
293 hinfo->family == family) {
294 atomic_inc(&hinfo->use);
295 spin_unlock_bh(&hashlimit_lock);
296 return hinfo;
299 spin_unlock_bh(&hashlimit_lock);
300 return NULL;
303 static void htable_put(struct xt_hashlimit_htable *hinfo)
305 if (atomic_dec_and_test(&hinfo->use)) {
306 spin_lock_bh(&hashlimit_lock);
307 hlist_del(&hinfo->node);
308 spin_unlock_bh(&hashlimit_lock);
309 htable_destroy(hinfo);
313 /* The algorithm used is the Simple Token Bucket Filter (TBF)
314 * see net/sched/sch_tbf.c in the linux source tree
317 /* Rusty: This is my (non-mathematically-inclined) understanding of
318 this algorithm. The `average rate' in jiffies becomes your initial
319 amount of credit `credit' and the most credit you can ever have
320 `credit_cap'. The `peak rate' becomes the cost of passing the
321 test, `cost'.
323 `prev' tracks the last packet hit: you gain one credit per jiffy.
324 If you get credit balance more than this, the extra credit is
325 discarded. Every time the match passes, you lose `cost' credits;
326 if you don't have that many, the test fails.
328 See Alexey's formal explanation in net/sched/sch_tbf.c.
330 To get the maximum range, we multiply by this factor (ie. you get N
331 credits per jiffy). We want to allow a rate as low as 1 per day
332 (slowest userspace tool allows), which means
333 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
335 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
337 /* Repeated shift and or gives us all 1s, final shift and add 1 gives
338 * us the power of 2 below the theoretical max, so GCC simply does a
339 * shift. */
340 #define _POW2_BELOW2(x) ((x)|((x)>>1))
341 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
342 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
343 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
344 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
345 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
347 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
349 /* Precision saver. */
350 static inline u_int32_t
351 user2credits(u_int32_t user)
353 /* If multiplying would overflow... */
354 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
355 /* Divide first. */
356 return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
358 return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE;
361 static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
363 dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY;
364 if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
365 dh->rateinfo.credit = dh->rateinfo.credit_cap;
366 dh->rateinfo.prev = now;
369 static int
370 hashlimit_init_dst(struct xt_hashlimit_htable *hinfo, struct dsthash_dst *dst,
371 const struct sk_buff *skb, unsigned int protoff)
373 __be16 _ports[2], *ports;
374 int nexthdr;
376 memset(dst, 0, sizeof(*dst));
378 switch (hinfo->family) {
379 case AF_INET:
380 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
381 dst->addr.ip.dst = ip_hdr(skb)->daddr;
382 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
383 dst->addr.ip.src = ip_hdr(skb)->saddr;
385 if (!(hinfo->cfg.mode &
386 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
387 return 0;
388 nexthdr = ip_hdr(skb)->protocol;
389 break;
390 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
391 case AF_INET6:
392 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
393 memcpy(&dst->addr.ip6.dst, &ipv6_hdr(skb)->daddr,
394 sizeof(dst->addr.ip6.dst));
395 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
396 memcpy(&dst->addr.ip6.src, &ipv6_hdr(skb)->saddr,
397 sizeof(dst->addr.ip6.src));
399 if (!(hinfo->cfg.mode &
400 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
401 return 0;
402 nexthdr = ipv6_find_hdr(skb, &protoff, -1, NULL);
403 if (nexthdr < 0)
404 return -1;
405 break;
406 #endif
407 default:
408 BUG();
409 return 0;
412 switch (nexthdr) {
413 case IPPROTO_TCP:
414 case IPPROTO_UDP:
415 case IPPROTO_UDPLITE:
416 case IPPROTO_SCTP:
417 case IPPROTO_DCCP:
418 ports = skb_header_pointer(skb, protoff, sizeof(_ports),
419 &_ports);
420 break;
421 default:
422 _ports[0] = _ports[1] = 0;
423 ports = _ports;
424 break;
426 if (!ports)
427 return -1;
428 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT)
429 dst->src_port = ports[0];
430 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT)
431 dst->dst_port = ports[1];
432 return 0;
435 static int
436 hashlimit_match(const struct sk_buff *skb,
437 const struct net_device *in,
438 const struct net_device *out,
439 const struct xt_match *match,
440 const void *matchinfo,
441 int offset,
442 unsigned int protoff,
443 int *hotdrop)
445 struct xt_hashlimit_info *r =
446 ((struct xt_hashlimit_info *)matchinfo)->u.master;
447 struct xt_hashlimit_htable *hinfo = r->hinfo;
448 unsigned long now = jiffies;
449 struct dsthash_ent *dh;
450 struct dsthash_dst dst;
452 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0)
453 goto hotdrop;
455 spin_lock_bh(&hinfo->lock);
456 dh = dsthash_find(hinfo, &dst);
457 if (!dh) {
458 dh = dsthash_alloc_init(hinfo, &dst);
459 if (!dh) {
460 spin_unlock_bh(&hinfo->lock);
461 goto hotdrop;
464 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
465 dh->rateinfo.prev = jiffies;
466 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
467 hinfo->cfg.burst);
468 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
469 hinfo->cfg.burst);
470 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
471 } else {
472 /* update expiration timeout */
473 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
474 rateinfo_recalc(dh, now);
477 if (dh->rateinfo.credit >= dh->rateinfo.cost) {
478 /* We're underlimit. */
479 dh->rateinfo.credit -= dh->rateinfo.cost;
480 spin_unlock_bh(&hinfo->lock);
481 return 1;
484 spin_unlock_bh(&hinfo->lock);
486 /* default case: we're overlimit, thus don't match */
487 return 0;
489 hotdrop:
490 *hotdrop = 1;
491 return 0;
494 static int
495 hashlimit_checkentry(const char *tablename,
496 const void *inf,
497 const struct xt_match *match,
498 void *matchinfo,
499 unsigned int hook_mask)
501 struct xt_hashlimit_info *r = matchinfo;
503 /* Check for overflow. */
504 if (r->cfg.burst == 0 ||
505 user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) {
506 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n",
507 r->cfg.avg, r->cfg.burst);
508 return 0;
510 if (r->cfg.mode == 0 ||
511 r->cfg.mode > (XT_HASHLIMIT_HASH_DPT |
512 XT_HASHLIMIT_HASH_DIP |
513 XT_HASHLIMIT_HASH_SIP |
514 XT_HASHLIMIT_HASH_SPT))
515 return 0;
516 if (!r->cfg.gc_interval)
517 return 0;
518 if (!r->cfg.expire)
519 return 0;
520 if (r->name[sizeof(r->name) - 1] != '\0')
521 return 0;
523 /* This is the best we've got: We cannot release and re-grab lock,
524 * since checkentry() is called before x_tables.c grabs xt_mutex.
525 * We also cannot grab the hashtable spinlock, since htable_create will
526 * call vmalloc, and that can sleep. And we cannot just re-search
527 * the list of htable's in htable_create(), since then we would
528 * create duplicate proc files. -HW */
529 mutex_lock(&hlimit_mutex);
530 r->hinfo = htable_find_get(r->name, match->family);
531 if (!r->hinfo && htable_create(r, match->family) != 0) {
532 mutex_unlock(&hlimit_mutex);
533 return 0;
535 mutex_unlock(&hlimit_mutex);
537 /* Ugly hack: For SMP, we only want to use one set */
538 r->u.master = r;
539 return 1;
542 static void
543 hashlimit_destroy(const struct xt_match *match, void *matchinfo)
545 struct xt_hashlimit_info *r = matchinfo;
547 htable_put(r->hinfo);
550 #ifdef CONFIG_COMPAT
551 struct compat_xt_hashlimit_info {
552 char name[IFNAMSIZ];
553 struct hashlimit_cfg cfg;
554 compat_uptr_t hinfo;
555 compat_uptr_t master;
558 static void compat_from_user(void *dst, void *src)
560 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
562 memcpy(dst, src, off);
563 memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off);
566 static int compat_to_user(void __user *dst, void *src)
568 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
570 return copy_to_user(dst, src, off) ? -EFAULT : 0;
572 #endif
574 static struct xt_match xt_hashlimit[] = {
576 .name = "hashlimit",
577 .family = AF_INET,
578 .match = hashlimit_match,
579 .matchsize = sizeof(struct xt_hashlimit_info),
580 #ifdef CONFIG_COMPAT
581 .compatsize = sizeof(struct compat_xt_hashlimit_info),
582 .compat_from_user = compat_from_user,
583 .compat_to_user = compat_to_user,
584 #endif
585 .checkentry = hashlimit_checkentry,
586 .destroy = hashlimit_destroy,
587 .me = THIS_MODULE
590 .name = "hashlimit",
591 .family = AF_INET6,
592 .match = hashlimit_match,
593 .matchsize = sizeof(struct xt_hashlimit_info),
594 #ifdef CONFIG_COMPAT
595 .compatsize = sizeof(struct compat_xt_hashlimit_info),
596 .compat_from_user = compat_from_user,
597 .compat_to_user = compat_to_user,
598 #endif
599 .checkentry = hashlimit_checkentry,
600 .destroy = hashlimit_destroy,
601 .me = THIS_MODULE
605 /* PROC stuff */
606 static void *dl_seq_start(struct seq_file *s, loff_t *pos)
608 struct proc_dir_entry *pde = s->private;
609 struct xt_hashlimit_htable *htable = pde->data;
610 unsigned int *bucket;
612 spin_lock_bh(&htable->lock);
613 if (*pos >= htable->cfg.size)
614 return NULL;
616 bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
617 if (!bucket)
618 return ERR_PTR(-ENOMEM);
620 *bucket = *pos;
621 return bucket;
624 static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
626 struct proc_dir_entry *pde = s->private;
627 struct xt_hashlimit_htable *htable = pde->data;
628 unsigned int *bucket = (unsigned int *)v;
630 *pos = ++(*bucket);
631 if (*pos >= htable->cfg.size) {
632 kfree(v);
633 return NULL;
635 return bucket;
638 static void dl_seq_stop(struct seq_file *s, void *v)
640 struct proc_dir_entry *pde = s->private;
641 struct xt_hashlimit_htable *htable = pde->data;
642 unsigned int *bucket = (unsigned int *)v;
644 kfree(bucket);
645 spin_unlock_bh(&htable->lock);
648 static int dl_seq_real_show(struct dsthash_ent *ent, int family,
649 struct seq_file *s)
651 /* recalculate to show accurate numbers */
652 rateinfo_recalc(ent, jiffies);
654 switch (family) {
655 case AF_INET:
656 return seq_printf(s, "%ld %u.%u.%u.%u:%u->"
657 "%u.%u.%u.%u:%u %u %u %u\n",
658 (long)(ent->expires - jiffies)/HZ,
659 NIPQUAD(ent->dst.addr.ip.src),
660 ntohs(ent->dst.src_port),
661 NIPQUAD(ent->dst.addr.ip.dst),
662 ntohs(ent->dst.dst_port),
663 ent->rateinfo.credit, ent->rateinfo.credit_cap,
664 ent->rateinfo.cost);
665 case AF_INET6:
666 return seq_printf(s, "%ld " NIP6_FMT ":%u->"
667 NIP6_FMT ":%u %u %u %u\n",
668 (long)(ent->expires - jiffies)/HZ,
669 NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.src),
670 ntohs(ent->dst.src_port),
671 NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.dst),
672 ntohs(ent->dst.dst_port),
673 ent->rateinfo.credit, ent->rateinfo.credit_cap,
674 ent->rateinfo.cost);
675 default:
676 BUG();
677 return 0;
681 static int dl_seq_show(struct seq_file *s, void *v)
683 struct proc_dir_entry *pde = s->private;
684 struct xt_hashlimit_htable *htable = pde->data;
685 unsigned int *bucket = (unsigned int *)v;
686 struct dsthash_ent *ent;
687 struct hlist_node *pos;
689 if (!hlist_empty(&htable->hash[*bucket])) {
690 hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node)
691 if (dl_seq_real_show(ent, htable->family, s))
692 return 1;
694 return 0;
697 static struct seq_operations dl_seq_ops = {
698 .start = dl_seq_start,
699 .next = dl_seq_next,
700 .stop = dl_seq_stop,
701 .show = dl_seq_show
704 static int dl_proc_open(struct inode *inode, struct file *file)
706 int ret = seq_open(file, &dl_seq_ops);
708 if (!ret) {
709 struct seq_file *sf = file->private_data;
710 sf->private = PDE(inode);
712 return ret;
715 static const struct file_operations dl_file_ops = {
716 .owner = THIS_MODULE,
717 .open = dl_proc_open,
718 .read = seq_read,
719 .llseek = seq_lseek,
720 .release = seq_release
723 static int __init xt_hashlimit_init(void)
725 int err;
727 err = xt_register_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
728 if (err < 0)
729 goto err1;
731 err = -ENOMEM;
732 hashlimit_cachep = kmem_cache_create("xt_hashlimit",
733 sizeof(struct dsthash_ent), 0, 0,
734 NULL, NULL);
735 if (!hashlimit_cachep) {
736 printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n");
737 goto err2;
739 hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", proc_net);
740 if (!hashlimit_procdir4) {
741 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
742 "entry\n");
743 goto err3;
745 hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", proc_net);
746 if (!hashlimit_procdir6) {
747 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
748 "entry\n");
749 goto err4;
751 return 0;
752 err4:
753 remove_proc_entry("ipt_hashlimit", proc_net);
754 err3:
755 kmem_cache_destroy(hashlimit_cachep);
756 err2:
757 xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
758 err1:
759 return err;
763 static void __exit xt_hashlimit_fini(void)
765 remove_proc_entry("ipt_hashlimit", proc_net);
766 remove_proc_entry("ip6t_hashlimit", proc_net);
767 kmem_cache_destroy(hashlimit_cachep);
768 xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
771 module_init(xt_hashlimit_init);
772 module_exit(xt_hashlimit_fini);