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