[NETFILTER]: x_tables: add SCTP/DCCP support where missing
[safe/jmp/linux-2.6] / 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         struct timer_list timer;        /* timer for gc */
84         atomic_t count;                 /* number entries in table */
85
86         /* seq_file stuff */
87         struct proc_dir_entry *pde;
88
89         struct hlist_head hash[0];      /* hashtable itself */
90 };
91
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 kmem_cache_t *hashlimit_cachep __read_mostly;
96
97 static inline int dst_cmp(const struct dsthash_ent *ent, struct dsthash_dst *b)
98 {
99         return (ent->dst.dst_ip == b->dst_ip 
100                 && ent->dst.dst_port == b->dst_port
101                 && ent->dst.src_port == b->src_port
102                 && ent->dst.src_ip == b->src_ip);
103 }
104
105 static inline u_int32_t
106 hash_dst(const struct ipt_hashlimit_htable *ht, const struct dsthash_dst *dst)
107 {
108         return (jhash_3words(dst->dst_ip, (dst->dst_port<<16 | dst->src_port), 
109                              dst->src_ip, ht->rnd) % ht->cfg.size);
110 }
111
112 static inline struct dsthash_ent *
113 __dsthash_find(const struct ipt_hashlimit_htable *ht, struct dsthash_dst *dst)
114 {
115         struct dsthash_ent *ent;
116         struct hlist_node *pos;
117         u_int32_t hash = hash_dst(ht, dst);
118
119         if (!hlist_empty(&ht->hash[hash]))
120                 hlist_for_each_entry(ent, pos, &ht->hash[hash], node) {
121                         if (dst_cmp(ent, dst)) {
122                                 return ent;
123                         }
124                 }
125         
126         return NULL;
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         hlist_add_head(&ent->node, &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         hlist_del(&ent->node);
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_HLIST_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 + msecs_to_jiffies(hinfo->cfg.gc_interval);
229         hinfo->timer.data = (unsigned long )hinfo;
230         hinfo->timer.function = htable_gc;
231         add_timer(&hinfo->timer);
232
233         spin_lock_bh(&hashlimit_lock);
234         hlist_add_head(&hinfo->node, &hashlimit_htables);
235         spin_unlock_bh(&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;
262                 struct hlist_node *pos, *n;
263                 hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) {
264                         if ((*select)(ht, dh))
265                                 __dsthash_free(ht, dh);
266                 }
267         }
268         spin_unlock_bh(&ht->lock);
269 }
270
271 /* hash table garbage collector, run by timer */
272 static void htable_gc(unsigned long htlong)
273 {
274         struct ipt_hashlimit_htable *ht = (struct ipt_hashlimit_htable *)htlong;
275
276         htable_selective_cleanup(ht, select_gc);
277
278         /* re-add the timer accordingly */
279         ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval);
280         add_timer(&ht->timer);
281 }
282
283 static void htable_destroy(struct ipt_hashlimit_htable *hinfo)
284 {
285         /* remove timer, if it is pending */
286         if (timer_pending(&hinfo->timer))
287                 del_timer(&hinfo->timer);
288
289         /* remove proc entry */
290         remove_proc_entry(hinfo->pde->name, hashlimit_procdir);
291
292         htable_selective_cleanup(hinfo, select_all);
293         vfree(hinfo);
294 }
295
296 static struct ipt_hashlimit_htable *htable_find_get(char *name)
297 {
298         struct ipt_hashlimit_htable *hinfo;
299         struct hlist_node *pos;
300
301         spin_lock_bh(&hashlimit_lock);
302         hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) {
303                 if (!strcmp(name, hinfo->pde->name)) {
304                         atomic_inc(&hinfo->use);
305                         spin_unlock_bh(&hashlimit_lock);
306                         return hinfo;
307                 }
308         }
309         spin_unlock_bh(&hashlimit_lock);
310
311         return NULL;
312 }
313
314 static void htable_put(struct ipt_hashlimit_htable *hinfo)
315 {
316         if (atomic_dec_and_test(&hinfo->use)) {
317                 spin_lock_bh(&hashlimit_lock);
318                 hlist_del(&hinfo->node);
319                 spin_unlock_bh(&hashlimit_lock);
320                 htable_destroy(hinfo);
321         }
322 }
323
324
325 /* The algorithm used is the Simple Token Bucket Filter (TBF)
326  * see net/sched/sch_tbf.c in the linux source tree
327  */
328
329 /* Rusty: This is my (non-mathematically-inclined) understanding of
330    this algorithm.  The `average rate' in jiffies becomes your initial
331    amount of credit `credit' and the most credit you can ever have
332    `credit_cap'.  The `peak rate' becomes the cost of passing the
333    test, `cost'.
334
335    `prev' tracks the last packet hit: you gain one credit per jiffy.
336    If you get credit balance more than this, the extra credit is
337    discarded.  Every time the match passes, you lose `cost' credits;
338    if you don't have that many, the test fails.
339
340    See Alexey's formal explanation in net/sched/sch_tbf.c.
341
342    To get the maximum range, we multiply by this factor (ie. you get N
343    credits per jiffy).  We want to allow a rate as low as 1 per day
344    (slowest userspace tool allows), which means
345    CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
346 */
347 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
348
349 /* Repeated shift and or gives us all 1s, final shift and add 1 gives
350  * us the power of 2 below the theoretical max, so GCC simply does a
351  * shift. */
352 #define _POW2_BELOW2(x) ((x)|((x)>>1))
353 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
354 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
355 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
356 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
357 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
358
359 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
360
361 /* Precision saver. */
362 static inline u_int32_t
363 user2credits(u_int32_t user)
364 {
365         /* If multiplying would overflow... */
366         if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
367                 /* Divide first. */
368                 return (user / IPT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
369
370         return (user * HZ * CREDITS_PER_JIFFY) / IPT_HASHLIMIT_SCALE;
371 }
372
373 static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
374 {
375         dh->rateinfo.credit += (now - xchg(&dh->rateinfo.prev, now)) 
376                                         * CREDITS_PER_JIFFY;
377         if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
378                 dh->rateinfo.credit = dh->rateinfo.credit_cap;
379 }
380
381 static int
382 hashlimit_match(const struct sk_buff *skb,
383                 const struct net_device *in,
384                 const struct net_device *out,
385                 const struct xt_match *match,
386                 const void *matchinfo,
387                 int offset,
388                 unsigned int protoff,
389                 int *hotdrop)
390 {
391         struct ipt_hashlimit_info *r = 
392                 ((struct ipt_hashlimit_info *)matchinfo)->u.master;
393         struct ipt_hashlimit_htable *hinfo = r->hinfo;
394         unsigned long now = jiffies;
395         struct dsthash_ent *dh;
396         struct dsthash_dst dst;
397
398         /* build 'dst' according to hinfo->cfg and current packet */
399         memset(&dst, 0, sizeof(dst));
400         if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_DIP)
401                 dst.dst_ip = skb->nh.iph->daddr;
402         if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_SIP)
403                 dst.src_ip = skb->nh.iph->saddr;
404         if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_DPT
405             ||hinfo->cfg.mode & IPT_HASHLIMIT_HASH_SPT) {
406                 u_int16_t _ports[2], *ports;
407
408                 switch (skb->nh.iph->protocol) {
409                 case IPPROTO_TCP:
410                 case IPPROTO_UDP:
411                 case IPPROTO_SCTP:
412                 case IPPROTO_DCCP:
413                         ports = skb_header_pointer(skb, skb->nh.iph->ihl*4,
414                                                    sizeof(_ports), &_ports);
415                         break;
416                 default:
417                         _ports[0] = _ports[1] = 0;
418                         ports = _ports;
419                         break;
420                 }
421                 if (!ports) {
422                         /* We've been asked to examine this packet, and we
423                           can't.  Hence, no choice but to drop. */
424                         *hotdrop = 1;
425                         return 0;
426                 }
427                 if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_SPT)
428                         dst.src_port = ports[0];
429                 if (hinfo->cfg.mode & IPT_HASHLIMIT_HASH_DPT)
430                         dst.dst_port = ports[1];
431         } 
432
433         spin_lock_bh(&hinfo->lock);
434         dh = __dsthash_find(hinfo, &dst);
435         if (!dh) {
436                 dh = __dsthash_alloc_init(hinfo, &dst);
437
438                 if (!dh) {
439                         /* enomem... don't match == DROP */
440                         if (net_ratelimit())
441                                 printk(KERN_ERR "%s: ENOMEM\n", __FUNCTION__);
442                         spin_unlock_bh(&hinfo->lock);
443                         return 0;
444                 }
445
446                 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
447
448                 dh->rateinfo.prev = jiffies;
449                 dh->rateinfo.credit = user2credits(hinfo->cfg.avg * 
450                                                         hinfo->cfg.burst);
451                 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg * 
452                                                         hinfo->cfg.burst);
453                 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
454
455                 spin_unlock_bh(&hinfo->lock);
456                 return 1;
457         }
458
459         /* update expiration timeout */
460         dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
461
462         rateinfo_recalc(dh, now);
463         if (dh->rateinfo.credit >= dh->rateinfo.cost) {
464                 /* We're underlimit. */
465                 dh->rateinfo.credit -= dh->rateinfo.cost;
466                 spin_unlock_bh(&hinfo->lock);
467                 return 1;
468         }
469
470         spin_unlock_bh(&hinfo->lock);
471
472         /* default case: we're overlimit, thus don't match */
473         return 0;
474 }
475
476 static int
477 hashlimit_checkentry(const char *tablename,
478                      const void *inf,
479                      const struct xt_match *match,
480                      void *matchinfo,
481                      unsigned int matchsize,
482                      unsigned int hook_mask)
483 {
484         struct ipt_hashlimit_info *r = matchinfo;
485
486         /* Check for overflow. */
487         if (r->cfg.burst == 0
488             || user2credits(r->cfg.avg * r->cfg.burst) < 
489                                         user2credits(r->cfg.avg)) {
490                 printk(KERN_ERR "ipt_hashlimit: Overflow, try lower: %u/%u\n",
491                        r->cfg.avg, r->cfg.burst);
492                 return 0;
493         }
494
495         if (r->cfg.mode == 0 
496             || r->cfg.mode > (IPT_HASHLIMIT_HASH_DPT
497                           |IPT_HASHLIMIT_HASH_DIP
498                           |IPT_HASHLIMIT_HASH_SIP
499                           |IPT_HASHLIMIT_HASH_SPT))
500                 return 0;
501
502         if (!r->cfg.gc_interval)
503                 return 0;
504         
505         if (!r->cfg.expire)
506                 return 0;
507
508         /* This is the best we've got: We cannot release and re-grab lock,
509          * since checkentry() is called before ip_tables.c grabs ipt_mutex.  
510          * We also cannot grab the hashtable spinlock, since htable_create will 
511          * call vmalloc, and that can sleep.  And we cannot just re-search
512          * the list of htable's in htable_create(), since then we would
513          * create duplicate proc files. -HW */
514         mutex_lock(&hlimit_mutex);
515         r->hinfo = htable_find_get(r->name);
516         if (!r->hinfo && (htable_create(r) != 0)) {
517                 mutex_unlock(&hlimit_mutex);
518                 return 0;
519         }
520         mutex_unlock(&hlimit_mutex);
521
522         /* Ugly hack: For SMP, we only want to use one set */
523         r->u.master = r;
524
525         return 1;
526 }
527
528 static void
529 hashlimit_destroy(const struct xt_match *match, void *matchinfo,
530                   unsigned int matchsize)
531 {
532         struct ipt_hashlimit_info *r = matchinfo;
533
534         htable_put(r->hinfo);
535 }
536
537 static struct ipt_match ipt_hashlimit = {
538         .name           = "hashlimit",
539         .match          = hashlimit_match,
540         .matchsize      = sizeof(struct ipt_hashlimit_info),
541         .checkentry     = hashlimit_checkentry,
542         .destroy        = hashlimit_destroy,
543         .me             = THIS_MODULE
544 };
545
546 /* PROC stuff */
547
548 static void *dl_seq_start(struct seq_file *s, loff_t *pos)
549 {
550         struct proc_dir_entry *pde = s->private;
551         struct ipt_hashlimit_htable *htable = pde->data;
552         unsigned int *bucket;
553
554         spin_lock_bh(&htable->lock);
555         if (*pos >= htable->cfg.size)
556                 return NULL;
557
558         bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
559         if (!bucket)
560                 return ERR_PTR(-ENOMEM);
561
562         *bucket = *pos;
563         return bucket;
564 }
565
566 static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
567 {
568         struct proc_dir_entry *pde = s->private;
569         struct ipt_hashlimit_htable *htable = pde->data;
570         unsigned int *bucket = (unsigned int *)v;
571
572         *pos = ++(*bucket);
573         if (*pos >= htable->cfg.size) {
574                 kfree(v);
575                 return NULL;
576         }
577         return bucket;
578 }
579
580 static void dl_seq_stop(struct seq_file *s, void *v)
581 {
582         struct proc_dir_entry *pde = s->private;
583         struct ipt_hashlimit_htable *htable = pde->data;
584         unsigned int *bucket = (unsigned int *)v;
585
586         kfree(bucket);
587
588         spin_unlock_bh(&htable->lock);
589 }
590
591 static inline int dl_seq_real_show(struct dsthash_ent *ent, struct seq_file *s)
592 {
593         /* recalculate to show accurate numbers */
594         rateinfo_recalc(ent, jiffies);
595
596         return seq_printf(s, "%ld %u.%u.%u.%u:%u->%u.%u.%u.%u:%u %u %u %u\n",
597                         (long)(ent->expires - jiffies)/HZ,
598                         NIPQUAD(ent->dst.src_ip), ntohs(ent->dst.src_port),
599                         NIPQUAD(ent->dst.dst_ip), ntohs(ent->dst.dst_port),
600                         ent->rateinfo.credit, ent->rateinfo.credit_cap,
601                         ent->rateinfo.cost);
602 }
603
604 static int dl_seq_show(struct seq_file *s, void *v)
605 {
606         struct proc_dir_entry *pde = s->private;
607         struct ipt_hashlimit_htable *htable = pde->data;
608         unsigned int *bucket = (unsigned int *)v;
609         struct dsthash_ent *ent;
610         struct hlist_node *pos;
611
612         if (!hlist_empty(&htable->hash[*bucket]))
613                 hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node) {
614                         if (dl_seq_real_show(ent, s)) {
615                                 /* buffer was filled and unable to print that tuple */
616                                 return 1;
617                         }
618                 }
619         
620         return 0;
621 }
622
623 static struct seq_operations dl_seq_ops = {
624         .start = dl_seq_start,
625         .next  = dl_seq_next,
626         .stop  = dl_seq_stop,
627         .show  = dl_seq_show
628 };
629
630 static int dl_proc_open(struct inode *inode, struct file *file)
631 {
632         int ret = seq_open(file, &dl_seq_ops);
633
634         if (!ret) {
635                 struct seq_file *sf = file->private_data;
636                 sf->private = PDE(inode);
637         }
638         return ret;
639 }
640
641 static struct file_operations dl_file_ops = {
642         .owner   = THIS_MODULE,
643         .open    = dl_proc_open,
644         .read    = seq_read,
645         .llseek  = seq_lseek,
646         .release = seq_release
647 };
648
649 static int init_or_fini(int fini)
650 {
651         int ret = 0;
652
653         if (fini)
654                 goto cleanup;
655
656         if (ipt_register_match(&ipt_hashlimit)) {
657                 ret = -EINVAL;
658                 goto cleanup_nothing;
659         }
660
661         hashlimit_cachep = kmem_cache_create("ipt_hashlimit",
662                                             sizeof(struct dsthash_ent), 0,
663                                             0, NULL, NULL);
664         if (!hashlimit_cachep) {
665                 printk(KERN_ERR "Unable to create ipt_hashlimit slab cache\n");
666                 ret = -ENOMEM;
667                 goto cleanup_unreg_match;
668         }
669
670         hashlimit_procdir = proc_mkdir("ipt_hashlimit", proc_net);
671         if (!hashlimit_procdir) {
672                 printk(KERN_ERR "Unable to create proc dir entry\n");
673                 ret = -ENOMEM;
674                 goto cleanup_free_slab;
675         }
676
677         return ret;
678
679 cleanup:
680         remove_proc_entry("ipt_hashlimit", proc_net);
681 cleanup_free_slab:
682         kmem_cache_destroy(hashlimit_cachep);
683 cleanup_unreg_match:
684         ipt_unregister_match(&ipt_hashlimit);
685 cleanup_nothing:
686         return ret;
687         
688 }
689
690 static int __init ipt_hashlimit_init(void)
691 {
692         return init_or_fini(0);
693 }
694
695 static void __exit ipt_hashlimit_fini(void)
696 {
697         init_or_fini(1);
698 }
699
700 module_init(ipt_hashlimit_init);
701 module_exit(ipt_hashlimit_fini);