[NETFILTER]: x_tables: switch xt_match->checkentry to bool
[safe/jmp/linux-2.6] / net / netfilter / xt_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 #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>
24
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>
30
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");
36
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;
41
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;
56 };
57
58 struct dsthash_ent {
59         /* static / read-only parts in the beginning */
60         struct hlist_node node;
61         struct dsthash_dst dst;
62
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;
70 };
71
72 struct xt_hashlimit_htable {
73         struct hlist_node node;         /* global list of all htables */
74         atomic_t use;
75         int family;
76
77         struct hashlimit_cfg cfg;       /* config */
78
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 */
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 struct kmem_cache *hashlimit_cachep __read_mostly;
96
97 static inline bool dst_cmp(const struct dsthash_ent *ent,
98                            struct dsthash_dst *b)
99 {
100         return !memcmp(&ent->dst, b, sizeof(ent->dst));
101 }
102
103 static u_int32_t
104 hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
105 {
106         return jhash(dst, sizeof(*dst), ht->rnd) % ht->cfg.size;
107 }
108
109 static struct dsthash_ent *
110 dsthash_find(const struct xt_hashlimit_htable *ht, struct dsthash_dst *dst)
111 {
112         struct dsthash_ent *ent;
113         struct hlist_node *pos;
114         u_int32_t hash = hash_dst(ht, dst);
115
116         if (!hlist_empty(&ht->hash[hash])) {
117                 hlist_for_each_entry(ent, pos, &ht->hash[hash], node)
118                         if (dst_cmp(ent, dst))
119                                 return ent;
120         }
121         return NULL;
122 }
123
124 /* allocate dsthash_ent, initialize dst, put in htable and lock it */
125 static struct dsthash_ent *
126 dsthash_alloc_init(struct xt_hashlimit_htable *ht, struct dsthash_dst *dst)
127 {
128         struct dsthash_ent *ent;
129
130         /* initialize hash with random val at the time we allocate
131          * the first hashtable entry */
132         if (!ht->rnd_initialized) {
133                 get_random_bytes(&ht->rnd, 4);
134                 ht->rnd_initialized = 1;
135         }
136
137         if (ht->cfg.max && ht->count >= ht->cfg.max) {
138                 /* FIXME: do something. question is what.. */
139                 if (net_ratelimit())
140                         printk(KERN_WARNING
141                                 "xt_hashlimit: max count of %u reached\n",
142                                 ht->cfg.max);
143                 return NULL;
144         }
145
146         ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
147         if (!ent) {
148                 if (net_ratelimit())
149                         printk(KERN_ERR
150                                 "xt_hashlimit: can't allocate dsthash_ent\n");
151                 return NULL;
152         }
153         memcpy(&ent->dst, dst, sizeof(ent->dst));
154
155         hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]);
156         ht->count++;
157         return ent;
158 }
159
160 static inline void
161 dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
162 {
163         hlist_del(&ent->node);
164         kmem_cache_free(hashlimit_cachep, ent);
165         ht->count--;
166 }
167 static void htable_gc(unsigned long htlong);
168
169 static int htable_create(struct xt_hashlimit_info *minfo, int family)
170 {
171         struct xt_hashlimit_htable *hinfo;
172         unsigned int size;
173         unsigned int i;
174
175         if (minfo->cfg.size)
176                 size = minfo->cfg.size;
177         else {
178                 size = ((num_physpages << PAGE_SHIFT) / 16384) /
179                        sizeof(struct list_head);
180                 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
181                         size = 8192;
182                 if (size < 16)
183                         size = 16;
184         }
185         /* FIXME: don't use vmalloc() here or anywhere else -HW */
186         hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
187                         sizeof(struct list_head) * size);
188         if (!hinfo) {
189                 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n");
190                 return -1;
191         }
192         minfo->hinfo = hinfo;
193
194         /* copy match config into hashtable config */
195         memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
196         hinfo->cfg.size = size;
197         if (!hinfo->cfg.max)
198                 hinfo->cfg.max = 8 * hinfo->cfg.size;
199         else if (hinfo->cfg.max < hinfo->cfg.size)
200                 hinfo->cfg.max = hinfo->cfg.size;
201
202         for (i = 0; i < hinfo->cfg.size; i++)
203                 INIT_HLIST_HEAD(&hinfo->hash[i]);
204
205         atomic_set(&hinfo->use, 1);
206         hinfo->count = 0;
207         hinfo->family = family;
208         hinfo->rnd_initialized = 0;
209         spin_lock_init(&hinfo->lock);
210         hinfo->pde = create_proc_entry(minfo->name, 0,
211                                        family == AF_INET ? hashlimit_procdir4 :
212                                                            hashlimit_procdir6);
213         if (!hinfo->pde) {
214                 vfree(hinfo);
215                 return -1;
216         }
217         hinfo->pde->proc_fops = &dl_file_ops;
218         hinfo->pde->data = hinfo;
219
220         setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo);
221         hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
222         add_timer(&hinfo->timer);
223
224         spin_lock_bh(&hashlimit_lock);
225         hlist_add_head(&hinfo->node, &hashlimit_htables);
226         spin_unlock_bh(&hashlimit_lock);
227
228         return 0;
229 }
230
231 static bool select_all(struct xt_hashlimit_htable *ht, struct dsthash_ent *he)
232 {
233         return 1;
234 }
235
236 static bool select_gc(struct xt_hashlimit_htable *ht, struct dsthash_ent *he)
237 {
238         return (jiffies >= he->expires);
239 }
240
241 static void htable_selective_cleanup(struct xt_hashlimit_htable *ht,
242                                 bool (*select)(struct xt_hashlimit_htable *ht,
243                                               struct dsthash_ent *he))
244 {
245         unsigned int i;
246
247         /* lock hash table and iterate over it */
248         spin_lock_bh(&ht->lock);
249         for (i = 0; i < ht->cfg.size; i++) {
250                 struct dsthash_ent *dh;
251                 struct hlist_node *pos, *n;
252                 hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) {
253                         if ((*select)(ht, dh))
254                                 dsthash_free(ht, dh);
255                 }
256         }
257         spin_unlock_bh(&ht->lock);
258 }
259
260 /* hash table garbage collector, run by timer */
261 static void htable_gc(unsigned long htlong)
262 {
263         struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong;
264
265         htable_selective_cleanup(ht, select_gc);
266
267         /* re-add the timer accordingly */
268         ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval);
269         add_timer(&ht->timer);
270 }
271
272 static void htable_destroy(struct xt_hashlimit_htable *hinfo)
273 {
274         /* remove timer, if it is pending */
275         if (timer_pending(&hinfo->timer))
276                 del_timer(&hinfo->timer);
277
278         /* remove proc entry */
279         remove_proc_entry(hinfo->pde->name,
280                           hinfo->family == AF_INET ? hashlimit_procdir4 :
281                                                      hashlimit_procdir6);
282         htable_selective_cleanup(hinfo, select_all);
283         vfree(hinfo);
284 }
285
286 static struct xt_hashlimit_htable *htable_find_get(char *name, int family)
287 {
288         struct xt_hashlimit_htable *hinfo;
289         struct hlist_node *pos;
290
291         spin_lock_bh(&hashlimit_lock);
292         hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) {
293                 if (!strcmp(name, hinfo->pde->name) &&
294                     hinfo->family == family) {
295                         atomic_inc(&hinfo->use);
296                         spin_unlock_bh(&hashlimit_lock);
297                         return hinfo;
298                 }
299         }
300         spin_unlock_bh(&hashlimit_lock);
301         return NULL;
302 }
303
304 static void htable_put(struct xt_hashlimit_htable *hinfo)
305 {
306         if (atomic_dec_and_test(&hinfo->use)) {
307                 spin_lock_bh(&hashlimit_lock);
308                 hlist_del(&hinfo->node);
309                 spin_unlock_bh(&hashlimit_lock);
310                 htable_destroy(hinfo);
311         }
312 }
313
314 /* The algorithm used is the Simple Token Bucket Filter (TBF)
315  * see net/sched/sch_tbf.c in the linux source tree
316  */
317
318 /* Rusty: This is my (non-mathematically-inclined) understanding of
319    this algorithm.  The `average rate' in jiffies becomes your initial
320    amount of credit `credit' and the most credit you can ever have
321    `credit_cap'.  The `peak rate' becomes the cost of passing the
322    test, `cost'.
323
324    `prev' tracks the last packet hit: you gain one credit per jiffy.
325    If you get credit balance more than this, the extra credit is
326    discarded.  Every time the match passes, you lose `cost' credits;
327    if you don't have that many, the test fails.
328
329    See Alexey's formal explanation in net/sched/sch_tbf.c.
330
331    To get the maximum range, we multiply by this factor (ie. you get N
332    credits per jiffy).  We want to allow a rate as low as 1 per day
333    (slowest userspace tool allows), which means
334    CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
335 */
336 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
337
338 /* Repeated shift and or gives us all 1s, final shift and add 1 gives
339  * us the power of 2 below the theoretical max, so GCC simply does a
340  * shift. */
341 #define _POW2_BELOW2(x) ((x)|((x)>>1))
342 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
343 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
344 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
345 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
346 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
347
348 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
349
350 /* Precision saver. */
351 static inline u_int32_t
352 user2credits(u_int32_t user)
353 {
354         /* If multiplying would overflow... */
355         if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
356                 /* Divide first. */
357                 return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
358
359         return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE;
360 }
361
362 static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
363 {
364         dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY;
365         if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
366                 dh->rateinfo.credit = dh->rateinfo.credit_cap;
367         dh->rateinfo.prev = now;
368 }
369
370 static int
371 hashlimit_init_dst(struct xt_hashlimit_htable *hinfo, struct dsthash_dst *dst,
372                    const struct sk_buff *skb, unsigned int protoff)
373 {
374         __be16 _ports[2], *ports;
375         int nexthdr;
376
377         memset(dst, 0, sizeof(*dst));
378
379         switch (hinfo->family) {
380         case AF_INET:
381                 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
382                         dst->addr.ip.dst = ip_hdr(skb)->daddr;
383                 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
384                         dst->addr.ip.src = ip_hdr(skb)->saddr;
385
386                 if (!(hinfo->cfg.mode &
387                       (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
388                         return 0;
389                 nexthdr = ip_hdr(skb)->protocol;
390                 break;
391 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
392         case AF_INET6:
393                 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
394                         memcpy(&dst->addr.ip6.dst, &ipv6_hdr(skb)->daddr,
395                                sizeof(dst->addr.ip6.dst));
396                 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
397                         memcpy(&dst->addr.ip6.src, &ipv6_hdr(skb)->saddr,
398                                sizeof(dst->addr.ip6.src));
399
400                 if (!(hinfo->cfg.mode &
401                       (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
402                         return 0;
403                 nexthdr = ipv6_find_hdr(skb, &protoff, -1, NULL);
404                 if (nexthdr < 0)
405                         return -1;
406                 break;
407 #endif
408         default:
409                 BUG();
410                 return 0;
411         }
412
413         switch (nexthdr) {
414         case IPPROTO_TCP:
415         case IPPROTO_UDP:
416         case IPPROTO_UDPLITE:
417         case IPPROTO_SCTP:
418         case IPPROTO_DCCP:
419                 ports = skb_header_pointer(skb, protoff, sizeof(_ports),
420                                            &_ports);
421                 break;
422         default:
423                 _ports[0] = _ports[1] = 0;
424                 ports = _ports;
425                 break;
426         }
427         if (!ports)
428                 return -1;
429         if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT)
430                 dst->src_port = ports[0];
431         if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT)
432                 dst->dst_port = ports[1];
433         return 0;
434 }
435
436 static bool
437 hashlimit_match(const struct sk_buff *skb,
438                 const struct net_device *in,
439                 const struct net_device *out,
440                 const struct xt_match *match,
441                 const void *matchinfo,
442                 int offset,
443                 unsigned int protoff,
444                 bool *hotdrop)
445 {
446         struct xt_hashlimit_info *r =
447                 ((struct xt_hashlimit_info *)matchinfo)->u.master;
448         struct xt_hashlimit_htable *hinfo = r->hinfo;
449         unsigned long now = jiffies;
450         struct dsthash_ent *dh;
451         struct dsthash_dst dst;
452
453         if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0)
454                 goto hotdrop;
455
456         spin_lock_bh(&hinfo->lock);
457         dh = dsthash_find(hinfo, &dst);
458         if (!dh) {
459                 dh = dsthash_alloc_init(hinfo, &dst);
460                 if (!dh) {
461                         spin_unlock_bh(&hinfo->lock);
462                         goto hotdrop;
463                 }
464
465                 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
466                 dh->rateinfo.prev = jiffies;
467                 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
468                                                    hinfo->cfg.burst);
469                 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
470                                                        hinfo->cfg.burst);
471                 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
472         } else {
473                 /* update expiration timeout */
474                 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
475                 rateinfo_recalc(dh, now);
476         }
477
478         if (dh->rateinfo.credit >= dh->rateinfo.cost) {
479                 /* We're underlimit. */
480                 dh->rateinfo.credit -= dh->rateinfo.cost;
481                 spin_unlock_bh(&hinfo->lock);
482                 return true;
483         }
484
485         spin_unlock_bh(&hinfo->lock);
486
487         /* default case: we're overlimit, thus don't match */
488         return false;
489
490 hotdrop:
491         *hotdrop = true;
492         return false;
493 }
494
495 static bool
496 hashlimit_checkentry(const char *tablename,
497                      const void *inf,
498                      const struct xt_match *match,
499                      void *matchinfo,
500                      unsigned int hook_mask)
501 {
502         struct xt_hashlimit_info *r = matchinfo;
503
504         /* Check for overflow. */
505         if (r->cfg.burst == 0 ||
506             user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) {
507                 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n",
508                        r->cfg.avg, r->cfg.burst);
509                 return false;
510         }
511         if (r->cfg.mode == 0 ||
512             r->cfg.mode > (XT_HASHLIMIT_HASH_DPT |
513                            XT_HASHLIMIT_HASH_DIP |
514                            XT_HASHLIMIT_HASH_SIP |
515                            XT_HASHLIMIT_HASH_SPT))
516                 return false;
517         if (!r->cfg.gc_interval)
518                 return false;
519         if (!r->cfg.expire)
520                 return false;
521         if (r->name[sizeof(r->name) - 1] != '\0')
522                 return false;
523
524         /* This is the best we've got: We cannot release and re-grab lock,
525          * since checkentry() is called before x_tables.c grabs xt_mutex.
526          * We also cannot grab the hashtable spinlock, since htable_create will
527          * call vmalloc, and that can sleep.  And we cannot just re-search
528          * the list of htable's in htable_create(), since then we would
529          * create duplicate proc files. -HW */
530         mutex_lock(&hlimit_mutex);
531         r->hinfo = htable_find_get(r->name, match->family);
532         if (!r->hinfo && htable_create(r, match->family) != 0) {
533                 mutex_unlock(&hlimit_mutex);
534                 return false;
535         }
536         mutex_unlock(&hlimit_mutex);
537
538         /* Ugly hack: For SMP, we only want to use one set */
539         r->u.master = r;
540         return true;
541 }
542
543 static void
544 hashlimit_destroy(const struct xt_match *match, void *matchinfo)
545 {
546         struct xt_hashlimit_info *r = matchinfo;
547
548         htable_put(r->hinfo);
549 }
550
551 #ifdef CONFIG_COMPAT
552 struct compat_xt_hashlimit_info {
553         char name[IFNAMSIZ];
554         struct hashlimit_cfg cfg;
555         compat_uptr_t hinfo;
556         compat_uptr_t master;
557 };
558
559 static void compat_from_user(void *dst, void *src)
560 {
561         int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
562
563         memcpy(dst, src, off);
564         memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off);
565 }
566
567 static int compat_to_user(void __user *dst, void *src)
568 {
569         int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
570
571         return copy_to_user(dst, src, off) ? -EFAULT : 0;
572 }
573 #endif
574
575 static struct xt_match xt_hashlimit[] = {
576         {
577                 .name           = "hashlimit",
578                 .family         = AF_INET,
579                 .match          = hashlimit_match,
580                 .matchsize      = sizeof(struct xt_hashlimit_info),
581 #ifdef CONFIG_COMPAT
582                 .compatsize     = sizeof(struct compat_xt_hashlimit_info),
583                 .compat_from_user = compat_from_user,
584                 .compat_to_user = compat_to_user,
585 #endif
586                 .checkentry     = hashlimit_checkentry,
587                 .destroy        = hashlimit_destroy,
588                 .me             = THIS_MODULE
589         },
590         {
591                 .name           = "hashlimit",
592                 .family         = AF_INET6,
593                 .match          = hashlimit_match,
594                 .matchsize      = sizeof(struct xt_hashlimit_info),
595 #ifdef CONFIG_COMPAT
596                 .compatsize     = sizeof(struct compat_xt_hashlimit_info),
597                 .compat_from_user = compat_from_user,
598                 .compat_to_user = compat_to_user,
599 #endif
600                 .checkentry     = hashlimit_checkentry,
601                 .destroy        = hashlimit_destroy,
602                 .me             = THIS_MODULE
603         },
604 };
605
606 /* PROC stuff */
607 static void *dl_seq_start(struct seq_file *s, loff_t *pos)
608 {
609         struct proc_dir_entry *pde = s->private;
610         struct xt_hashlimit_htable *htable = pde->data;
611         unsigned int *bucket;
612
613         spin_lock_bh(&htable->lock);
614         if (*pos >= htable->cfg.size)
615                 return NULL;
616
617         bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
618         if (!bucket)
619                 return ERR_PTR(-ENOMEM);
620
621         *bucket = *pos;
622         return bucket;
623 }
624
625 static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
626 {
627         struct proc_dir_entry *pde = s->private;
628         struct xt_hashlimit_htable *htable = pde->data;
629         unsigned int *bucket = (unsigned int *)v;
630
631         *pos = ++(*bucket);
632         if (*pos >= htable->cfg.size) {
633                 kfree(v);
634                 return NULL;
635         }
636         return bucket;
637 }
638
639 static void dl_seq_stop(struct seq_file *s, void *v)
640 {
641         struct proc_dir_entry *pde = s->private;
642         struct xt_hashlimit_htable *htable = pde->data;
643         unsigned int *bucket = (unsigned int *)v;
644
645         kfree(bucket);
646         spin_unlock_bh(&htable->lock);
647 }
648
649 static int dl_seq_real_show(struct dsthash_ent *ent, int family,
650                                    struct seq_file *s)
651 {
652         /* recalculate to show accurate numbers */
653         rateinfo_recalc(ent, jiffies);
654
655         switch (family) {
656         case AF_INET:
657                 return seq_printf(s, "%ld %u.%u.%u.%u:%u->"
658                                      "%u.%u.%u.%u:%u %u %u %u\n",
659                                  (long)(ent->expires - jiffies)/HZ,
660                                  NIPQUAD(ent->dst.addr.ip.src),
661                                  ntohs(ent->dst.src_port),
662                                  NIPQUAD(ent->dst.addr.ip.dst),
663                                  ntohs(ent->dst.dst_port),
664                                  ent->rateinfo.credit, ent->rateinfo.credit_cap,
665                                  ent->rateinfo.cost);
666         case AF_INET6:
667                 return seq_printf(s, "%ld " NIP6_FMT ":%u->"
668                                      NIP6_FMT ":%u %u %u %u\n",
669                                  (long)(ent->expires - jiffies)/HZ,
670                                  NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.src),
671                                  ntohs(ent->dst.src_port),
672                                  NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.dst),
673                                  ntohs(ent->dst.dst_port),
674                                  ent->rateinfo.credit, ent->rateinfo.credit_cap,
675                                  ent->rateinfo.cost);
676         default:
677                 BUG();
678                 return 0;
679         }
680 }
681
682 static int dl_seq_show(struct seq_file *s, void *v)
683 {
684         struct proc_dir_entry *pde = s->private;
685         struct xt_hashlimit_htable *htable = pde->data;
686         unsigned int *bucket = (unsigned int *)v;
687         struct dsthash_ent *ent;
688         struct hlist_node *pos;
689
690         if (!hlist_empty(&htable->hash[*bucket])) {
691                 hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node)
692                         if (dl_seq_real_show(ent, htable->family, s))
693                                 return 1;
694         }
695         return 0;
696 }
697
698 static struct seq_operations dl_seq_ops = {
699         .start = dl_seq_start,
700         .next  = dl_seq_next,
701         .stop  = dl_seq_stop,
702         .show  = dl_seq_show
703 };
704
705 static int dl_proc_open(struct inode *inode, struct file *file)
706 {
707         int ret = seq_open(file, &dl_seq_ops);
708
709         if (!ret) {
710                 struct seq_file *sf = file->private_data;
711                 sf->private = PDE(inode);
712         }
713         return ret;
714 }
715
716 static const struct file_operations dl_file_ops = {
717         .owner   = THIS_MODULE,
718         .open    = dl_proc_open,
719         .read    = seq_read,
720         .llseek  = seq_lseek,
721         .release = seq_release
722 };
723
724 static int __init xt_hashlimit_init(void)
725 {
726         int err;
727
728         err = xt_register_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
729         if (err < 0)
730                 goto err1;
731
732         err = -ENOMEM;
733         hashlimit_cachep = kmem_cache_create("xt_hashlimit",
734                                             sizeof(struct dsthash_ent), 0, 0,
735                                             NULL, NULL);
736         if (!hashlimit_cachep) {
737                 printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n");
738                 goto err2;
739         }
740         hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", proc_net);
741         if (!hashlimit_procdir4) {
742                 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
743                                 "entry\n");
744                 goto err3;
745         }
746         hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", proc_net);
747         if (!hashlimit_procdir6) {
748                 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
749                                 "entry\n");
750                 goto err4;
751         }
752         return 0;
753 err4:
754         remove_proc_entry("ipt_hashlimit", proc_net);
755 err3:
756         kmem_cache_destroy(hashlimit_cachep);
757 err2:
758         xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
759 err1:
760         return err;
761
762 }
763
764 static void __exit xt_hashlimit_fini(void)
765 {
766         remove_proc_entry("ipt_hashlimit", proc_net);
767         remove_proc_entry("ip6t_hashlimit", proc_net);
768         kmem_cache_destroy(hashlimit_cachep);
769         xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
770 }
771
772 module_init(xt_hashlimit_init);
773 module_exit(xt_hashlimit_fini);