]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_hashlimit.c
[ROSE] rose_add_loopback_node: propagate -E
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / xt_hashlimit.c
CommitLineData
1da177e4
LT
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/
1da177e4
LT
9 */
10#include <linux/module.h>
1da177e4
LT
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>
1da177e4
LT
16#include <linux/proc_fs.h>
17#include <linux/seq_file.h>
18#include <linux/list.h>
39b46fc6
PM
19#include <linux/skbuff.h>
20#include <linux/in.h>
21#include <linux/ip.h>
22#include <linux/ipv6.h>
1da177e4 23
39b46fc6 24#include <linux/netfilter/x_tables.h>
1da177e4 25#include <linux/netfilter_ipv4/ip_tables.h>
39b46fc6
PM
26#include <linux/netfilter_ipv6/ip6_tables.h>
27#include <linux/netfilter/xt_hashlimit.h>
14cc3e2b 28#include <linux/mutex.h>
1da177e4
LT
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
32MODULE_DESCRIPTION("iptables match for limiting per hash-bucket");
39b46fc6
PM
33MODULE_ALIAS("ipt_hashlimit");
34MODULE_ALIAS("ip6t_hashlimit");
1da177e4
LT
35
36/* need to declare this at the top */
39b46fc6
PM
37static struct proc_dir_entry *hashlimit_procdir4;
38static struct proc_dir_entry *hashlimit_procdir6;
1da177e4
LT
39static struct file_operations dl_file_ops;
40
41/* hash table crap */
1da177e4 42struct dsthash_dst {
39b46fc6
PM
43 union {
44 struct {
45 __be32 src;
46 __be32 dst;
47 } ip;
48 struct {
49 __be32 src[4];
50 __be32 dst[4];
51 } ip6;
52 } addr;
6a19d614
AV
53 __be16 src_port;
54 __be16 dst_port;
1da177e4
LT
55};
56
57struct dsthash_ent {
58 /* static / read-only parts in the beginning */
59 struct hlist_node node;
60 struct dsthash_dst dst;
61
62 /* modified structure members in the end */
63 unsigned long expires; /* precalculated expiry time */
64 struct {
65 unsigned long prev; /* last modification */
66 u_int32_t credit;
67 u_int32_t credit_cap, cost;
68 } rateinfo;
69};
70
39b46fc6 71struct xt_hashlimit_htable {
1da177e4
LT
72 struct hlist_node node; /* global list of all htables */
73 atomic_t use;
39b46fc6 74 int family;
1da177e4
LT
75
76 struct hashlimit_cfg cfg; /* config */
77
78 /* used internally */
79 spinlock_t lock; /* lock for list_head */
80 u_int32_t rnd; /* random seed for hash */
bf0857ea 81 int rnd_initialized;
39b46fc6 82 unsigned int count; /* number entries in table */
1da177e4 83 struct timer_list timer; /* timer for gc */
1da177e4
LT
84
85 /* seq_file stuff */
86 struct proc_dir_entry *pde;
87
88 struct hlist_head hash[0]; /* hashtable itself */
89};
90
e45b1be8 91static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */
14cc3e2b 92static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */
1da177e4 93static HLIST_HEAD(hashlimit_htables);
ba89966c 94static kmem_cache_t *hashlimit_cachep __read_mostly;
1da177e4
LT
95
96static inline int dst_cmp(const struct dsthash_ent *ent, struct dsthash_dst *b)
97{
39b46fc6 98 return !memcmp(&ent->dst, b, sizeof(ent->dst));
1da177e4
LT
99}
100
39b46fc6
PM
101static u_int32_t
102hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
1da177e4 103{
39b46fc6 104 return jhash(dst, sizeof(*dst), ht->rnd) % ht->cfg.size;
1da177e4
LT
105}
106
39b46fc6
PM
107static struct dsthash_ent *
108dsthash_find(const struct xt_hashlimit_htable *ht, struct dsthash_dst *dst)
1da177e4
LT
109{
110 struct dsthash_ent *ent;
111 struct hlist_node *pos;
112 u_int32_t hash = hash_dst(ht, dst);
113
39b46fc6
PM
114 if (!hlist_empty(&ht->hash[hash])) {
115 hlist_for_each_entry(ent, pos, &ht->hash[hash], node)
116 if (dst_cmp(ent, dst))
1da177e4 117 return ent;
39b46fc6 118 }
1da177e4
LT
119 return NULL;
120}
121
122/* allocate dsthash_ent, initialize dst, put in htable and lock it */
123static struct dsthash_ent *
39b46fc6 124dsthash_alloc_init(struct xt_hashlimit_htable *ht, struct dsthash_dst *dst)
1da177e4
LT
125{
126 struct dsthash_ent *ent;
127
128 /* initialize hash with random val at the time we allocate
129 * the first hashtable entry */
bf0857ea 130 if (!ht->rnd_initialized) {
1da177e4 131 get_random_bytes(&ht->rnd, 4);
bf0857ea
PM
132 ht->rnd_initialized = 1;
133 }
1da177e4 134
39b46fc6 135 if (ht->cfg.max && ht->count >= ht->cfg.max) {
1da177e4
LT
136 /* FIXME: do something. question is what.. */
137 if (net_ratelimit())
39b46fc6
PM
138 printk(KERN_WARNING
139 "xt_hashlimit: max count of %u reached\n",
1da177e4
LT
140 ht->cfg.max);
141 return NULL;
142 }
143
144 ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
145 if (!ent) {
146 if (net_ratelimit())
39b46fc6
PM
147 printk(KERN_ERR
148 "xt_hashlimit: can't allocate dsthash_ent\n");
1da177e4
LT
149 return NULL;
150 }
39b46fc6 151 memcpy(&ent->dst, dst, sizeof(ent->dst));
1da177e4
LT
152
153 hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]);
39b46fc6 154 ht->count++;
1da177e4
LT
155 return ent;
156}
157
39b46fc6
PM
158static inline void
159dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
1da177e4
LT
160{
161 hlist_del(&ent->node);
162 kmem_cache_free(hashlimit_cachep, ent);
39b46fc6 163 ht->count--;
1da177e4
LT
164}
165static void htable_gc(unsigned long htlong);
166
39b46fc6 167static int htable_create(struct xt_hashlimit_info *minfo, int family)
1da177e4 168{
39b46fc6 169 struct xt_hashlimit_htable *hinfo;
1da177e4 170 unsigned int size;
39b46fc6 171 unsigned int i;
1da177e4
LT
172
173 if (minfo->cfg.size)
174 size = minfo->cfg.size;
175 else {
39b46fc6
PM
176 size = ((num_physpages << PAGE_SHIFT) / 16384) /
177 sizeof(struct list_head);
1da177e4
LT
178 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
179 size = 8192;
180 if (size < 16)
181 size = 16;
182 }
183 /* FIXME: don't use vmalloc() here or anywhere else -HW */
39b46fc6
PM
184 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
185 sizeof(struct list_head) * size);
1da177e4 186 if (!hinfo) {
39b46fc6 187 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n");
1da177e4
LT
188 return -1;
189 }
190 minfo->hinfo = hinfo;
191
192 /* copy match config into hashtable config */
193 memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
194 hinfo->cfg.size = size;
195 if (!hinfo->cfg.max)
196 hinfo->cfg.max = 8 * hinfo->cfg.size;
197 else if (hinfo->cfg.max < hinfo->cfg.size)
198 hinfo->cfg.max = hinfo->cfg.size;
199
200 for (i = 0; i < hinfo->cfg.size; i++)
201 INIT_HLIST_HEAD(&hinfo->hash[i]);
202
1da177e4 203 atomic_set(&hinfo->use, 1);
39b46fc6
PM
204 hinfo->count = 0;
205 hinfo->family = family;
bf0857ea 206 hinfo->rnd_initialized = 0;
1da177e4 207 spin_lock_init(&hinfo->lock);
39b46fc6
PM
208 hinfo->pde = create_proc_entry(minfo->name, 0,
209 family == AF_INET ? hashlimit_procdir4 :
210 hashlimit_procdir6);
1da177e4
LT
211 if (!hinfo->pde) {
212 vfree(hinfo);
213 return -1;
214 }
215 hinfo->pde->proc_fops = &dl_file_ops;
216 hinfo->pde->data = hinfo;
217
218 init_timer(&hinfo->timer);
219 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
220 hinfo->timer.data = (unsigned long )hinfo;
221 hinfo->timer.function = htable_gc;
222 add_timer(&hinfo->timer);
223
e45b1be8 224 spin_lock_bh(&hashlimit_lock);
1da177e4 225 hlist_add_head(&hinfo->node, &hashlimit_htables);
e45b1be8 226 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
227
228 return 0;
229}
230
39b46fc6 231static int select_all(struct xt_hashlimit_htable *ht, struct dsthash_ent *he)
1da177e4
LT
232{
233 return 1;
234}
235
39b46fc6 236static int select_gc(struct xt_hashlimit_htable *ht, struct dsthash_ent *he)
1da177e4
LT
237{
238 return (jiffies >= he->expires);
239}
240
39b46fc6
PM
241static void htable_selective_cleanup(struct xt_hashlimit_htable *ht,
242 int (*select)(struct xt_hashlimit_htable *ht,
1da177e4
LT
243 struct dsthash_ent *he))
244{
39b46fc6 245 unsigned int i;
1da177e4
LT
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))
39b46fc6 254 dsthash_free(ht, dh);
1da177e4
LT
255 }
256 }
257 spin_unlock_bh(&ht->lock);
258}
259
260/* hash table garbage collector, run by timer */
261static void htable_gc(unsigned long htlong)
262{
39b46fc6 263 struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong;
1da177e4
LT
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
39b46fc6 272static void htable_destroy(struct xt_hashlimit_htable *hinfo)
1da177e4
LT
273{
274 /* remove timer, if it is pending */
275 if (timer_pending(&hinfo->timer))
276 del_timer(&hinfo->timer);
277
278 /* remove proc entry */
39b46fc6
PM
279 remove_proc_entry(hinfo->pde->name,
280 hinfo->family == AF_INET ? hashlimit_procdir4 :
281 hashlimit_procdir6);
1da177e4
LT
282 htable_selective_cleanup(hinfo, select_all);
283 vfree(hinfo);
284}
285
39b46fc6 286static struct xt_hashlimit_htable *htable_find_get(char *name, int family)
1da177e4 287{
39b46fc6 288 struct xt_hashlimit_htable *hinfo;
1da177e4
LT
289 struct hlist_node *pos;
290
e45b1be8 291 spin_lock_bh(&hashlimit_lock);
1da177e4 292 hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) {
39b46fc6
PM
293 if (!strcmp(name, hinfo->pde->name) &&
294 hinfo->family == family) {
1da177e4 295 atomic_inc(&hinfo->use);
e45b1be8 296 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
297 return hinfo;
298 }
299 }
e45b1be8 300 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
301 return NULL;
302}
303
39b46fc6 304static void htable_put(struct xt_hashlimit_htable *hinfo)
1da177e4
LT
305{
306 if (atomic_dec_and_test(&hinfo->use)) {
e45b1be8 307 spin_lock_bh(&hashlimit_lock);
1da177e4 308 hlist_del(&hinfo->node);
e45b1be8 309 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
310 htable_destroy(hinfo);
311 }
312}
313
1da177e4
LT
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. */
351static inline u_int32_t
352user2credits(u_int32_t user)
353{
354 /* If multiplying would overflow... */
355 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
356 /* Divide first. */
39b46fc6 357 return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
1da177e4 358
39b46fc6 359 return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE;
1da177e4
LT
360}
361
362static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
363{
39b46fc6 364 dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY;
1da177e4
LT
365 if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
366 dh->rateinfo.credit = dh->rateinfo.credit_cap;
39b46fc6
PM
367 dh->rateinfo.prev = now;
368}
369
370static int
371hashlimit_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 = skb->nh.iph->daddr;
383 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
384 dst->addr.ip.src = skb->nh.iph->saddr;
385
386 if (!(hinfo->cfg.mode &
387 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
388 return 0;
389 nexthdr = skb->nh.iph->protocol;
390 break;
391#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
392 case AF_INET6:
393 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
394 memcpy(&dst->addr.ip6.dst, &skb->nh.ipv6h->daddr,
395 sizeof(dst->addr.ip6.dst));
396 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
397 memcpy(&dst->addr.ip6.src, &skb->nh.ipv6h->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_SCTP:
417 case IPPROTO_DCCP:
418 ports = skb_header_pointer(skb, protoff, sizeof(_ports),
419 &_ports);
420 break;
421 default:
422 _ports[0] = _ports[1] = 0;
423 ports = _ports;
424 break;
425 }
426 if (!ports)
427 return -1;
428 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT)
429 dst->src_port = ports[0];
430 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT)
431 dst->dst_port = ports[1];
432 return 0;
1da177e4
LT
433}
434
1da177e4
LT
435static int
436hashlimit_match(const struct sk_buff *skb,
437 const struct net_device *in,
438 const struct net_device *out,
c4986734 439 const struct xt_match *match,
1da177e4
LT
440 const void *matchinfo,
441 int offset,
2e4e6a17 442 unsigned int protoff,
1da177e4
LT
443 int *hotdrop)
444{
39b46fc6
PM
445 struct xt_hashlimit_info *r =
446 ((struct xt_hashlimit_info *)matchinfo)->u.master;
447 struct xt_hashlimit_htable *hinfo = r->hinfo;
1da177e4
LT
448 unsigned long now = jiffies;
449 struct dsthash_ent *dh;
450 struct dsthash_dst dst;
451
39b46fc6
PM
452 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0)
453 goto hotdrop;
1da177e4
LT
454
455 spin_lock_bh(&hinfo->lock);
39b46fc6 456 dh = dsthash_find(hinfo, &dst);
1da177e4 457 if (!dh) {
39b46fc6 458 dh = dsthash_alloc_init(hinfo, &dst);
1da177e4 459 if (!dh) {
1da177e4 460 spin_unlock_bh(&hinfo->lock);
39b46fc6 461 goto hotdrop;
1da177e4
LT
462 }
463
464 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
1da177e4 465 dh->rateinfo.prev = jiffies;
39b46fc6
PM
466 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
467 hinfo->cfg.burst);
468 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
469 hinfo->cfg.burst);
1da177e4 470 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
1c7628bd
PM
471 } else {
472 /* update expiration timeout */
473 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
474 rateinfo_recalc(dh, now);
1da177e4
LT
475 }
476
1da177e4
LT
477 if (dh->rateinfo.credit >= dh->rateinfo.cost) {
478 /* We're underlimit. */
479 dh->rateinfo.credit -= dh->rateinfo.cost;
480 spin_unlock_bh(&hinfo->lock);
481 return 1;
482 }
483
484 spin_unlock_bh(&hinfo->lock);
485
486 /* default case: we're overlimit, thus don't match */
487 return 0;
39b46fc6
PM
488
489hotdrop:
490 *hotdrop = 1;
491 return 0;
1da177e4
LT
492}
493
494static int
495hashlimit_checkentry(const char *tablename,
2e4e6a17 496 const void *inf,
c4986734 497 const struct xt_match *match,
1da177e4 498 void *matchinfo,
1da177e4
LT
499 unsigned int hook_mask)
500{
39b46fc6 501 struct xt_hashlimit_info *r = matchinfo;
1da177e4 502
1da177e4 503 /* Check for overflow. */
39b46fc6
PM
504 if (r->cfg.burst == 0 ||
505 user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) {
506 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n",
1da177e4
LT
507 r->cfg.avg, r->cfg.burst);
508 return 0;
509 }
39b46fc6
PM
510 if (r->cfg.mode == 0 ||
511 r->cfg.mode > (XT_HASHLIMIT_HASH_DPT |
512 XT_HASHLIMIT_HASH_DIP |
513 XT_HASHLIMIT_HASH_SIP |
514 XT_HASHLIMIT_HASH_SPT))
1da177e4 515 return 0;
1da177e4
LT
516 if (!r->cfg.gc_interval)
517 return 0;
1da177e4
LT
518 if (!r->cfg.expire)
519 return 0;
3ab72088
PM
520 if (r->name[sizeof(r->name) - 1] != '\0')
521 return 0;
522
1da177e4 523 /* This is the best we've got: We cannot release and re-grab lock,
39b46fc6
PM
524 * since checkentry() is called before x_tables.c grabs xt_mutex.
525 * We also cannot grab the hashtable spinlock, since htable_create will
1da177e4
LT
526 * call vmalloc, and that can sleep. And we cannot just re-search
527 * the list of htable's in htable_create(), since then we would
528 * create duplicate proc files. -HW */
14cc3e2b 529 mutex_lock(&hlimit_mutex);
39b46fc6
PM
530 r->hinfo = htable_find_get(r->name, match->family);
531 if (!r->hinfo && htable_create(r, match->family) != 0) {
14cc3e2b 532 mutex_unlock(&hlimit_mutex);
1da177e4
LT
533 return 0;
534 }
14cc3e2b 535 mutex_unlock(&hlimit_mutex);
1da177e4
LT
536
537 /* Ugly hack: For SMP, we only want to use one set */
538 r->u.master = r;
1da177e4
LT
539 return 1;
540}
541
542static void
efa74165 543hashlimit_destroy(const struct xt_match *match, void *matchinfo)
1da177e4 544{
39b46fc6 545 struct xt_hashlimit_info *r = matchinfo;
1da177e4
LT
546
547 htable_put(r->hinfo);
548}
549
127f15dd 550#ifdef CONFIG_COMPAT
39b46fc6 551struct compat_xt_hashlimit_info {
127f15dd
PM
552 char name[IFNAMSIZ];
553 struct hashlimit_cfg cfg;
554 compat_uptr_t hinfo;
555 compat_uptr_t master;
556};
557
558static void compat_from_user(void *dst, void *src)
559{
39b46fc6 560 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
127f15dd
PM
561
562 memcpy(dst, src, off);
39b46fc6 563 memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off);
127f15dd
PM
564}
565
566static int compat_to_user(void __user *dst, void *src)
567{
39b46fc6 568 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
127f15dd
PM
569
570 return copy_to_user(dst, src, off) ? -EFAULT : 0;
571}
572#endif
573
39b46fc6
PM
574static struct xt_match xt_hashlimit[] = {
575 {
576 .name = "hashlimit",
577 .family = AF_INET,
578 .match = hashlimit_match,
579 .matchsize = sizeof(struct xt_hashlimit_info),
580#ifdef CONFIG_COMPAT
581 .compatsize = sizeof(struct compat_xt_hashlimit_info),
582 .compat_from_user = compat_from_user,
583 .compat_to_user = compat_to_user,
584#endif
585 .checkentry = hashlimit_checkentry,
586 .destroy = hashlimit_destroy,
587 .me = THIS_MODULE
588 },
589 {
590 .name = "hashlimit",
591 .family = AF_INET6,
592 .match = hashlimit_match,
593 .matchsize = sizeof(struct xt_hashlimit_info),
127f15dd 594#ifdef CONFIG_COMPAT
39b46fc6
PM
595 .compatsize = sizeof(struct compat_xt_hashlimit_info),
596 .compat_from_user = compat_from_user,
597 .compat_to_user = compat_to_user,
127f15dd 598#endif
39b46fc6
PM
599 .checkentry = hashlimit_checkentry,
600 .destroy = hashlimit_destroy,
601 .me = THIS_MODULE
602 },
1da177e4
LT
603};
604
605/* PROC stuff */
1da177e4
LT
606static void *dl_seq_start(struct seq_file *s, loff_t *pos)
607{
608 struct proc_dir_entry *pde = s->private;
39b46fc6 609 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
610 unsigned int *bucket;
611
612 spin_lock_bh(&htable->lock);
613 if (*pos >= htable->cfg.size)
614 return NULL;
615
616 bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
617 if (!bucket)
618 return ERR_PTR(-ENOMEM);
619
620 *bucket = *pos;
621 return bucket;
622}
623
624static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
625{
626 struct proc_dir_entry *pde = s->private;
39b46fc6 627 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
628 unsigned int *bucket = (unsigned int *)v;
629
630 *pos = ++(*bucket);
631 if (*pos >= htable->cfg.size) {
632 kfree(v);
633 return NULL;
634 }
635 return bucket;
636}
637
638static void dl_seq_stop(struct seq_file *s, void *v)
639{
640 struct proc_dir_entry *pde = s->private;
39b46fc6 641 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
642 unsigned int *bucket = (unsigned int *)v;
643
644 kfree(bucket);
1da177e4
LT
645 spin_unlock_bh(&htable->lock);
646}
647
39b46fc6
PM
648static int dl_seq_real_show(struct dsthash_ent *ent, int family,
649 struct seq_file *s)
1da177e4
LT
650{
651 /* recalculate to show accurate numbers */
652 rateinfo_recalc(ent, jiffies);
653
39b46fc6
PM
654 switch (family) {
655 case AF_INET:
656 return seq_printf(s, "%ld %u.%u.%u.%u:%u->"
657 "%u.%u.%u.%u:%u %u %u %u\n",
658 (long)(ent->expires - jiffies)/HZ,
659 NIPQUAD(ent->dst.addr.ip.src),
660 ntohs(ent->dst.src_port),
661 NIPQUAD(ent->dst.addr.ip.dst),
662 ntohs(ent->dst.dst_port),
663 ent->rateinfo.credit, ent->rateinfo.credit_cap,
664 ent->rateinfo.cost);
665 case AF_INET6:
666 return seq_printf(s, "%ld " NIP6_FMT ":%u->"
667 NIP6_FMT ":%u %u %u %u\n",
668 (long)(ent->expires - jiffies)/HZ,
669 NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.src),
670 ntohs(ent->dst.src_port),
671 NIP6(*(struct in6_addr *)&ent->dst.addr.ip6.dst),
672 ntohs(ent->dst.dst_port),
673 ent->rateinfo.credit, ent->rateinfo.credit_cap,
674 ent->rateinfo.cost);
675 default:
676 BUG();
677 return 0;
678 }
1da177e4
LT
679}
680
681static int dl_seq_show(struct seq_file *s, void *v)
682{
683 struct proc_dir_entry *pde = s->private;
39b46fc6 684 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
685 unsigned int *bucket = (unsigned int *)v;
686 struct dsthash_ent *ent;
687 struct hlist_node *pos;
688
39b46fc6
PM
689 if (!hlist_empty(&htable->hash[*bucket])) {
690 hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node)
691 if (dl_seq_real_show(ent, htable->family, s))
1da177e4 692 return 1;
39b46fc6 693 }
1da177e4
LT
694 return 0;
695}
696
697static struct seq_operations dl_seq_ops = {
698 .start = dl_seq_start,
699 .next = dl_seq_next,
700 .stop = dl_seq_stop,
701 .show = dl_seq_show
702};
703
704static int dl_proc_open(struct inode *inode, struct file *file)
705{
706 int ret = seq_open(file, &dl_seq_ops);
707
708 if (!ret) {
709 struct seq_file *sf = file->private_data;
710 sf->private = PDE(inode);
711 }
712 return ret;
713}
714
715static struct file_operations dl_file_ops = {
716 .owner = THIS_MODULE,
717 .open = dl_proc_open,
718 .read = seq_read,
719 .llseek = seq_lseek,
720 .release = seq_release
721};
722
39b46fc6 723static int __init xt_hashlimit_init(void)
1da177e4 724{
39b46fc6 725 int err;
1da177e4 726
39b46fc6
PM
727 err = xt_register_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
728 if (err < 0)
729 goto err1;
1da177e4 730
39b46fc6
PM
731 err = -ENOMEM;
732 hashlimit_cachep = kmem_cache_create("xt_hashlimit",
733 sizeof(struct dsthash_ent), 0, 0,
734 NULL, NULL);
1da177e4 735 if (!hashlimit_cachep) {
39b46fc6
PM
736 printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n");
737 goto err2;
1da177e4 738 }
39b46fc6
PM
739 hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", proc_net);
740 if (!hashlimit_procdir4) {
741 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
742 "entry\n");
743 goto err3;
1da177e4 744 }
39b46fc6
PM
745 hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", proc_net);
746 if (!hashlimit_procdir6) {
747 printk(KERN_ERR "xt_hashlimit: tnable to create proc dir "
748 "entry\n");
749 goto err4;
750 }
751 return 0;
752err4:
1da177e4 753 remove_proc_entry("ipt_hashlimit", proc_net);
39b46fc6 754err3:
1da177e4 755 kmem_cache_destroy(hashlimit_cachep);
39b46fc6
PM
756err2:
757 xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
758err1:
759 return err;
1da177e4 760
1da177e4
LT
761}
762
39b46fc6 763static void __exit xt_hashlimit_fini(void)
1da177e4 764{
39b46fc6
PM
765 remove_proc_entry("ipt_hashlimit", proc_net);
766 remove_proc_entry("ip6t_hashlimit", proc_net);
767 kmem_cache_destroy(hashlimit_cachep);
768 xt_unregister_matches(xt_hashlimit, ARRAY_SIZE(xt_hashlimit));
1da177e4
LT
769}
770
39b46fc6
PM
771module_init(xt_hashlimit_init);
772module_exit(xt_hashlimit_fini);