vserver 1.9.5.x5
[linux-2.6.git] / net / ipv4 / netfilter / ipt_hashlimit.c
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/
9  *
10  * based on ipt_limit.c by:
11  * Jérôme de Vivie      <devivie@info.enserb.u-bordeaux.fr>
12  * Hervé Eychenne       <eychenne@info.enserb.u-bordeaux.fr>
13  * Rusty Russell        <rusty@rustcorp.com.au>
14  *
15  * The general idea is to create a hash table for every dstip and have a
16  * seperate limit counter per tuple.  This way you can do something like 'limit
17  * the number of syn packets for each of my internal addresses.
18  *
19  * Ideally this would just be implemented as a general 'hash' match, which would
20  * allow us to attach any iptables target to it's hash buckets.  But this is
21  * not possible in the current iptables architecture.  As always, pkttables for
22  * 2.7.x will help ;)
23  */
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/spinlock.h>
27 #include <linux/random.h>
28 #include <linux/jhash.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/tcp.h>
32 #include <linux/udp.h>
33 #include <linux/sctp.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36
37 #define ASSERT_READ_LOCK(x) 
38 #define ASSERT_WRITE_LOCK(x) 
39 #include <linux/netfilter_ipv4/lockhelp.h>
40 #include <linux/netfilter_ipv4/listhelp.h>
41
42 #include <linux/netfilter_ipv4/ip_tables.h>
43 #include <linux/netfilter_ipv4/ipt_hashlimit.h>
44
45 /* FIXME: this is just for IP_NF_ASSERRT */
46 #include <linux/netfilter_ipv4/ip_conntrack.h>
47
48 #define MS2JIFFIES(x) ((x*HZ)/1000)
49
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
52 MODULE_DESCRIPTION("iptables match for limiting per hash-bucket");
53
54 /* need to declare this at the top */
55 static struct proc_dir_entry *hashlimit_procdir;
56 static struct file_operations dl_file_ops;
57
58 /* hash table crap */
59
60 struct dsthash_dst {
61         u_int32_t src_ip;
62         u_int32_t dst_ip;
63         /* ports have to be consecutive !!! */
64         u_int16_t src_port;
65         u_int16_t dst_port;
66 };
67
68 struct dsthash_ent {
69         /* static / read-only parts in the beginning */
70         struct list_head list;
71         struct dsthash_dst dst;
72
73         /* modified structure members in the end */
74         unsigned long expires;          /* precalculated expiry time */
75         struct {
76                 unsigned long prev;     /* last modification */
77                 u_int32_t credit;
78                 u_int32_t credit_cap, cost;
79         } rateinfo;
80 };
81
82 struct ipt_hashlimit_htable {
83         struct list_head list;          /* global list of all htables */
84         atomic_t use;
85
86         struct hashlimit_cfg cfg;       /* config */
87
88         /* used internally */
89         spinlock_t lock;                /* lock for list_head */
90         u_int32_t rnd;                  /* random seed for hash */
91         struct timer_list timer;        /* timer for gc */
92         atomic_t count;                 /* number entries in table */
93
94         /* seq_file stuff */
95         struct proc_dir_entry *pde;
96
97         struct list_head hash[0];       /* hashtable itself */
98 };
99
100 static DECLARE_RWLOCK(hashlimit_lock);  /* protects htables list */
101 static DECLARE_MUTEX(hlimit_mutex);     /* additional checkentry protection */
102 static LIST_HEAD(hashlimit_htables);
103 static kmem_cache_t *hashlimit_cachep;
104
105 static inline int dst_cmp(const struct dsthash_ent *ent, struct dsthash_dst *b)
106 {
107         return (ent->dst.dst_ip == b->dst_ip 
108                 && ent->dst.dst_port == b->dst_port
109                 && ent->dst.src_port == b->src_port
110                 && ent->dst.src_ip == b->src_ip);
111 }
112
113 static inline u_int32_t
114 hash_dst(const struct ipt_hashlimit_htable *ht, const struct dsthash_dst *dst)
115 {
116         return (jhash_3words(dst->dst_ip, (dst->dst_port<<16 & dst->src_port), 
117                              dst->src_ip, ht->rnd) % ht->cfg.size);
118 }
119
120 static inline struct dsthash_ent *
121 __dsthash_find(const struct ipt_hashlimit_htable *ht, struct dsthash_dst *dst)
122 {
123         struct dsthash_ent *ent;
124         u_int32_t hash = hash_dst(ht, dst);
125         ent = LIST_FIND(&ht->hash[hash], dst_cmp, struct dsthash_ent *, dst);
126         return ent;
127 }
128
129 /* allocate dsthash_ent, initialize dst, put in htable and lock it */
130 static struct dsthash_ent *
131 __dsthash_alloc_init(struct ipt_hashlimit_htable *ht, struct dsthash_dst *dst)
132 {
133         struct dsthash_ent *ent;
134
135         /* initialize hash with random val at the time we allocate
136          * the first hashtable entry */
137         if (!ht->rnd)
138                 get_random_bytes(&ht->rnd, 4);
139
140         if (ht->cfg.max &&
141             atomic_read(&ht->count) >= ht->cfg.max) {
142                 /* FIXME: do something. question is what.. */
143                 if (net_ratelimit())
144                         printk(KERN_WARNING 
145                                 "ipt_hashlimit: max count of %u reached\n", 
146                                 ht->cfg.max);
147                 return NULL;
148         }
149
150         ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
151         if (!ent) {
152                 if (net_ratelimit())
153                         printk(KERN_ERR 
154                                 "ipt_hashlimit: can't allocate dsthash_ent\n");
155                 return NULL;
156         }
157
158         atomic_inc(&ht->count);
159
160         ent->dst.dst_ip = dst->dst_ip;
161         ent->dst.dst_port = dst->dst_port;
162         ent->dst.src_ip = dst->src_ip;
163         ent->dst.src_port = dst->src_port;
164
165         list_add(&ent->list, &ht->hash[hash_dst(ht, dst)]);
166
167         return ent;
168 }
169
170 static inline void 
171 __dsthash_free(struct ipt_hashlimit_htable *ht, struct dsthash_ent *ent)
172 {
173         list_del(&ent->list);
174         kmem_cache_free(hashlimit_cachep, ent);
175         atomic_dec(&ht->count);
176 }
177 static void htable_gc(unsigned long htlong);
178
179 static int htable_create(struct ipt_hashlimit_info *minfo)
180 {
181         int i;
182         unsigned int size;
183         struct ipt_hashlimit_htable *hinfo;
184
185         if (minfo->cfg.size)
186                 size = minfo->cfg.size;
187         else {
188                 size = (((num_physpages << PAGE_SHIFT) / 16384)
189                          / sizeof(struct list_head));
190                 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
191                         size = 8192;
192                 if (size < 16)
193                         size = 16;
194         }
195         /* FIXME: don't use vmalloc() here or anywhere else -HW */
196         hinfo = vmalloc(sizeof(struct ipt_hashlimit_htable)
197                         + (sizeof(struct list_head) * size));
198         if (!hinfo) {
199                 printk(KERN_ERR "ipt_hashlimit: Unable to create hashtable\n");
200                 return -1;
201         }
202         minfo->hinfo = hinfo;
203
204         /* copy match config into hashtable config */
205         memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
206         hinfo->cfg.size = size;
207         if (!hinfo->cfg.max)
208                 hinfo->cfg.max = 8 * hinfo->cfg.size;
209         else if (hinfo->cfg.max < hinfo->cfg.size)
210                 hinfo->cfg.max = hinfo->cfg.size;
211
212         for (i = 0; i < hinfo->cfg.size; i++)
213                 INIT_LIST_HEAD(&hinfo->hash[i]);
214
215         atomic_set(&hinfo->count, 0);
216         atomic_set(&hinfo->use, 1);
217         hinfo->rnd = 0;
218         spin_lock_init(&hinfo->lock);
219         hinfo->pde = create_proc_entry(minfo->name, 0, hashlimit_procdir);
220         if (!hinfo->pde) {
221                 vfree(hinfo);
222                 return -1;
223         }
224         hinfo->pde->proc_fops = &dl_file_ops;
225         hinfo->pde->data = hinfo;
226
227         init_timer(&hinfo->timer);
228         hinfo->timer.expires = jiffies + MS2JIFFIES(hinfo->cfg.gc_interval);
229         hinfo->timer.data = (unsigned long )hinfo;
230         hinfo->timer.function = htable_gc;
231         add_timer(&hinfo->timer);
232
233         WRITE_LOCK(&hashlimit_lock);
234         list_add(&hinfo->list, &hashlimit_htables);
235         WRITE_UNLOCK(&hashlimit_lock);
236
237         return 0;
238 }
239
240 static int select_all(struct ipt_hashlimit_htable *ht, struct dsthash_ent *he)
241 {
242         return 1;
243 }
244
245 static int select_gc(struct ipt_hashlimit_htable *ht, struct dsthash_ent *he)
246 {
247         return (jiffies >= he->expires);
248 }
249
250 static void htable_selective_cleanup(struct ipt_hashlimit_htable *ht,
251                                 int (*select)(struct ipt_hashlimit_htable *ht, 
252                                               struct dsthash_ent *he))
253 {
254         int i;
255
256         IP_NF_ASSERT(ht->cfg.size && ht->cfg.max);
257
258         /* lock hash table and iterate over it */
259         spin_lock_bh(&ht->lock);
260         for (i = 0; i < ht->cfg.size; i++) {
261                 struct dsthash_ent *dh, *n;
262                 list_for_each_entry_safe(dh, n, &ht->hash[i], list) {
263                         if ((*select)(ht, dh))
264                                 __dsthash_free(ht, dh);
265                 }
266         }
267         spin_unlock_bh(&ht->lock);
268 }
269
270 /* hash table garbage collector, run by timer */
271 static void htable_gc(unsigned long htlong)
272 {
273         struct ipt_hashlimit_htable *ht = (struct ipt_hashlimit_htable *)htlong;
274
275         htable_selective_cleanup(ht, select_gc);
276
277         /* re-add the timer accordingly */
278         ht->timer.expires = jiffies + MS2JIFFIES(ht->cfg.gc_interval);
279         add_timer(&ht->timer);
280 }
281
282 static void htable_destroy(struct ipt_hashlimit_htable *hinfo)
283 {
284         /* remove timer, if it is pending */
285         if (timer_pending(&hinfo->timer))
286                 del_timer(&hinfo->timer);
287
288         /* remove proc entry */
289         remove_proc_entry(hinfo->pde->name, hashlimit_procdir);
290
291         htable_selective_cleanup(hinfo, select_all);
292         vfree(hinfo);
293 }
294
295 static struct ipt_hashlimit_htable *htable_find_get(char *name)
296 {
297         struct ipt_hashlimit_htable *hinfo;
298
299         READ_LOCK(&hashlimit_lock);
300         list_for_each_entry(hinfo, &hashlimit_htables, list) {
301                 if (!strcmp(name, hinfo->pde->name)) {
302                         atomic_inc(&hinfo->use);
303                         READ_UNLOCK(&hashlimit_lock);
304                         return hinfo;
305                 }
306         }
307         READ_UNLOCK(&hashlimit_lock);
308
309         return NULL;
310 }
311
312 static void htable_put(struct ipt_hashlimit_htable *hinfo)
313 {
314         if (atomic_dec_and_test(&hinfo->use)) {
315                 WRITE_LOCK(&hashlimit_lock);
316                 list_del(&hinfo->list);
317                 WRITE_UNLOCK(&hashlimit_lock);
318                 htable_destroy(hinfo);
319         }
320 }
321
322
323 /* The algorithm used is the Simple Token Bucket Filter (TBF)
324  * see net/sched/sch_tbf.c in the linux source tree
325  */
326
327 /* Rusty: This is my (non-mathematically-inclined) understanding of
328    this algorithm.  The `average rate' in jiffies becomes your initial
329    amount of credit `credit' and the most credit you can ever have
330    `credit_cap'.  The `peak rate' becomes the cost of passing the
331    test, `cost'.
332
333    `prev' tracks the last packet hit: you gain one credit per jiffy.
334    If you get credit balance more than this, the extra credit is
335    discarded.  Every time the match passes, you lose `cost' credits;
336    if you don't have that many, the test fails.
337
338    See Alexey's formal explanation in net/sched/sch_tbf.c.
339
340    To get the maximum range, we multiply by this factor (ie. you get N
341    credits per jiffy).  We want to allow a rate as low as 1 per day
342    (slowest userspace tool allows), which means
343    CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
344 */
345 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
346
347 /* Repeated shift and or gives us all 1s, final shift and add 1 gives
348  * us the power of 2 below the theoretical max, so GCC simply does a
349  * shift. */
350 #define _POW2_BELOW2(x) ((x)|((x)>>1))
351 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
352 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
353 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
354 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
355 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
356
357 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
358
359 /* Precision saver. */
360 static inline u_int32_t
361 user2credits(u_int32_t user)
362 {
363         /* If multiplying would overflow... */
364         if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
365                 /* Divide first. */
366                 return (user / IPT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
367
368         return (user * HZ * CREDITS_PER_JIFFY) / IPT_HASHLIMIT_SCALE;
369 }
370
371 static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
372 {
373         dh->rateinfo.credit += (now - xchg(&dh->rateinfo.prev, now)) 
374                                         * CREDITS_PER_JIFFY;
375         if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
376                 dh->rateinfo.credit = dh->rateinfo.credit_cap;
377 }
378
379 static inline int get_ports(const struct sk_buff *skb, int offset, 
380                             u16 ports[2])
381 {
382         union {
383                 struct tcphdr th;
384                 struct udphdr uh;
385                 sctp_sctphdr_t sctph;
386         } hdr_u, *ptr_u;
387
388         /* Must not be a fragment. */
389         if (offset)
390                 return 1;
391
392         /* Must be big enough to read ports (both UDP and TCP have
393            them at the start). */
394         ptr_u = skb_header_pointer(skb, skb->nh.iph->ihl*4, 8, &hdr_u); 
395         if (!ptr_u)
396                 return 1;
397
398         switch (skb->nh.iph->protocol) {
399                 case IPPROTO_TCP:
400                         ports[0] = ptr_u->th.source;
401                         ports[1] = ptr_u->th.dest;
402                         break;
403                 case IPPROTO_UDP:
404                         ports[0] = ptr_u->uh.source;
405                         ports[1] = ptr_u->uh.dest;
406                         break;
407                 case IPPROTO_SCTP:
408                         ports[0] = ptr_u->sctph.source;
409                         ports[1] = ptr_u->sctph.dest;
410                         break;
411                 default:
412                         /* all other protocols don't supprot per-port hash
413                          * buckets */
414                         ports[0] = ports[1] = 0;
415                         break;
416         }
417
418         return 0;
419 }
420
421
422 static int
423 hashlimit_match(const struct sk_buff *skb,
424                 const struct net_device *in,
425                 const struct net_device *out,
426                 const void *matchinfo,
427                 int offset,
428                 int *hotdrop)
429 {
430         struct ipt_hashlimit_info *r = 
431                 ((struct ipt_hashlimit_info *)matchinfo)->u.master;
432         struct ipt_hashlimit_htable *hinfo = r->hinfo;
433         unsigned long now = jiffies;
434         struct dsthash_ent *dh;
435         struct dsthash_dst dst;
436
437         /* build 'dst' according to hinfo->cfg and current packet */
438         memset(&dst, 0, sizeof(dst));
439         if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_DIP)
440                 dst.dst_ip = skb->nh.iph->daddr;
441         if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_SIP)
442                 dst.src_ip = skb->nh.iph->saddr;
443         if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_DPT
444             ||hinfo->cfg.mode & IPT_HASHLIMIT_HASH_SPT) {
445                 u_int16_t ports[2];
446                 if (get_ports(skb, offset, ports)) {
447                         /* We've been asked to examine this packet, and we
448                           can't.  Hence, no choice but to drop. */
449                         *hotdrop = 1;
450                         return 0;
451                 }
452                 if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_SPT)
453                         dst.src_port = ports[0];
454                 if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_DPT)
455                         dst.dst_port = ports[1];
456         } 
457
458         spin_lock_bh(&hinfo->lock);
459         dh = __dsthash_find(hinfo, &dst);
460         if (!dh) {
461                 dh = __dsthash_alloc_init(hinfo, &dst);
462
463                 if (!dh) {
464                         /* enomem... don't match == DROP */
465                         if (net_ratelimit())
466                                 printk(KERN_ERR "%s: ENOMEM\n", __FUNCTION__);
467                         spin_unlock_bh(&hinfo->lock);
468                         return 0;
469                 }
470
471                 dh->expires = jiffies + MS2JIFFIES(hinfo->cfg.expire);
472
473                 dh->rateinfo.prev = jiffies;
474                 dh->rateinfo.credit = user2credits(hinfo->cfg.avg * 
475                                                         hinfo->cfg.burst);
476                 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * 
477                                                         hinfo->cfg.burst);
478                 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
479
480                 spin_unlock_bh(&hinfo->lock);
481                 return 1;
482         }
483
484         /* update expiration timeout */
485         dh->expires = now + MS2JIFFIES(hinfo->cfg.expire);
486
487         rateinfo_recalc(dh, now);
488         if (dh->rateinfo.credit >= dh->rateinfo.cost) {
489                 /* We're underlimit. */
490                 dh->rateinfo.credit -= dh->rateinfo.cost;
491                 spin_unlock_bh(&hinfo->lock);
492                 return 1;
493         }
494
495         spin_unlock_bh(&hinfo->lock);
496
497         /* default case: we're overlimit, thus don't match */
498         return 0;
499 }
500
501 static int
502 hashlimit_checkentry(const char *tablename,
503                      const struct ipt_ip *ip,
504                      void *matchinfo,
505                      unsigned int matchsize,
506                      unsigned int hook_mask)
507 {
508         struct ipt_hashlimit_info *r = matchinfo;
509
510         if (matchsize != IPT_ALIGN(sizeof(struct ipt_hashlimit_info)))
511                 return 0;
512
513         /* Check for overflow. */
514         if (r->cfg.burst == 0
515             || user2credits(r->cfg.avg * r->cfg.burst) < 
516                                         user2credits(r->cfg.avg)) {
517                 printk(KERN_ERR "ipt_hashlimit: Overflow, try lower: %u/%u\n",
518                        r->cfg.avg, r->cfg.burst);
519                 return 0;
520         }
521
522         if (r->cfg.mode == 0 
523             || r->cfg.mode > (IPT_HASHLIMIT_HASH_DPT
524                           |IPT_HASHLIMIT_HASH_DIP
525                           |IPT_HASHLIMIT_HASH_SIP
526                           |IPT_HASHLIMIT_HASH_SPT))
527                 return 0;
528
529         if (!r->cfg.gc_interval)
530                 return 0;
531         
532         if (!r->cfg.expire)
533                 return 0;
534
535         /* This is the best we've got: We cannot release and re-grab lock,
536          * since checkentry() is called before ip_tables.c grabs ipt_mutex.  
537          * We also cannot grab the hashtable spinlock, since htable_create will 
538          * call vmalloc, and that can sleep.  And we cannot just re-search
539          * the list of htable's in htable_create(), since then we would
540          * create duplicate proc files. -HW */
541         down(&hlimit_mutex);
542         r->hinfo = htable_find_get(r->name);
543         if (!r->hinfo && (htable_create(r) != 0)) {
544                 up(&hlimit_mutex);
545                 return 0;
546         }
547         up(&hlimit_mutex);
548
549         /* Ugly hack: For SMP, we only want to use one set */
550         r->u.master = r;
551
552         return 1;
553 }
554
555 static void
556 hashlimit_destroy(void *matchinfo, unsigned int matchsize)
557 {
558         struct ipt_hashlimit_info *r = (struct ipt_hashlimit_info *) matchinfo;
559
560         htable_put(r->hinfo);
561 }
562
563 static struct ipt_match ipt_hashlimit = { 
564         .name = "hashlimit", 
565         .match = hashlimit_match, 
566         .checkentry = hashlimit_checkentry, 
567         .destroy = hashlimit_destroy,
568         .me = THIS_MODULE 
569 };
570
571 /* PROC stuff */
572
573 static void *dl_seq_start(struct seq_file *s, loff_t *pos)
574 {
575         struct proc_dir_entry *pde = s->private;
576         struct ipt_hashlimit_htable *htable = pde->data;
577         unsigned int *bucket;
578
579         spin_lock_bh(&htable->lock);
580         if (*pos >= htable->cfg.size)
581                 return NULL;
582
583         bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
584         if (!bucket)
585                 return ERR_PTR(-ENOMEM);
586
587         *bucket = *pos;
588         return bucket;
589 }
590
591 static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
592 {
593         struct proc_dir_entry *pde = s->private;
594         struct ipt_hashlimit_htable *htable = pde->data;
595         unsigned int *bucket = (unsigned int *)v;
596
597         *pos = ++(*bucket);
598         if (*pos >= htable->cfg.size) {
599                 kfree(v);
600                 return NULL;
601         }
602         return bucket;
603 }
604
605 static void dl_seq_stop(struct seq_file *s, void *v)
606 {
607         struct proc_dir_entry *pde = s->private;
608         struct ipt_hashlimit_htable *htable = pde->data;
609         unsigned int *bucket = (unsigned int *)v;
610
611         kfree(bucket);
612
613         spin_unlock_bh(&htable->lock);
614 }
615
616 static inline int dl_seq_real_show(struct dsthash_ent *ent, struct seq_file *s)
617 {
618         /* recalculate to show accurate numbers */
619         rateinfo_recalc(ent, jiffies);
620
621         return seq_printf(s, "%ld %u.%u.%u.%u:%u->%u.%u.%u.%u:%u %u %u %u\n",
622                         (ent->expires - jiffies)/HZ,
623                         NIPQUAD(ent->dst.src_ip), ntohs(ent->dst.src_port),
624                         NIPQUAD(ent->dst.dst_ip), ntohs(ent->dst.dst_port),
625                         ent->rateinfo.credit, ent->rateinfo.credit_cap,
626                         ent->rateinfo.cost);
627 }
628
629 static int dl_seq_show(struct seq_file *s, void *v)
630 {
631         struct proc_dir_entry *pde = s->private;
632         struct ipt_hashlimit_htable *htable = pde->data;
633         unsigned int *bucket = (unsigned int *)v;
634
635         if (LIST_FIND_W(&htable->hash[*bucket], dl_seq_real_show,
636                       struct dsthash_ent *, s)) {
637                 /* buffer was filled and unable to print that tuple */
638                 return 1;
639         }
640         return 0;
641 }
642
643 static struct seq_operations dl_seq_ops = {
644         .start = dl_seq_start,
645         .next  = dl_seq_next,
646         .stop  = dl_seq_stop,
647         .show  = dl_seq_show
648 };
649
650 static int dl_proc_open(struct inode *inode, struct file *file)
651 {
652         int ret = seq_open(file, &dl_seq_ops);
653
654         if (!ret) {
655                 struct seq_file *sf = file->private_data;
656                 sf->private = PDE(inode);
657         }
658         return ret;
659 }
660
661 static struct file_operations dl_file_ops = {
662         .owner   = THIS_MODULE,
663         .open    = dl_proc_open,
664         .read    = seq_read,
665         .llseek  = seq_lseek,
666         .release = seq_release
667 };
668
669 static int init_or_fini(int fini)
670 {
671         int ret = 0;
672
673         if (fini)
674                 goto cleanup;
675
676         if (ipt_register_match(&ipt_hashlimit)) {
677                 ret = -EINVAL;
678                 goto cleanup_nothing;
679         }
680
681         hashlimit_cachep = kmem_cache_create("ipt_hashlimit",
682                                             sizeof(struct dsthash_ent), 0,
683                                             0, NULL, NULL);
684         if (!hashlimit_cachep) {
685                 printk(KERN_ERR "Unable to create ipt_hashlimit slab cache\n");
686                 ret = -ENOMEM;
687                 goto cleanup_unreg_match;
688         }
689
690         hashlimit_procdir = proc_mkdir("ipt_hashlimit", proc_net);
691         if (!hashlimit_procdir) {
692                 printk(KERN_ERR "Unable to create proc dir entry\n");
693                 ret = -ENOMEM;
694                 goto cleanup_free_slab;
695         }
696
697         return ret;
698
699 cleanup:
700         remove_proc_entry("ipt_hashlimit", proc_net);
701 cleanup_free_slab:
702         kmem_cache_destroy(hashlimit_cachep);
703 cleanup_unreg_match:
704         ipt_unregister_match(&ipt_hashlimit);
705 cleanup_nothing:
706         return ret;
707         
708 }
709
710 static int __init init(void)
711 {
712         return init_or_fini(0);
713 }
714
715 static void __exit fini(void)
716 {
717         init_or_fini(1);
718 }
719
720 module_init(init);
721 module_exit(fini);