]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/xt_hashlimit.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / xt_hashlimit.c
CommitLineData
09e410de
JE
1/*
2 * xt_hashlimit - Netfilter module to limit the number of packets per time
3 * seperately for each hashbucket (sourceip/sourceport/dstip/dstport)
1da177e4 4 *
09e410de
JE
5 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
6 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
1da177e4
LT
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 19#include <linux/skbuff.h>
d7fe0f24 20#include <linux/mm.h>
39b46fc6
PM
21#include <linux/in.h>
22#include <linux/ip.h>
7b21e09d 23#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
39b46fc6 24#include <linux/ipv6.h>
193b23c5 25#include <net/ipv6.h>
7b21e09d
ED
26#endif
27
457c4cbc 28#include <net/net_namespace.h>
1da177e4 29
39b46fc6 30#include <linux/netfilter/x_tables.h>
1da177e4 31#include <linux/netfilter_ipv4/ip_tables.h>
39b46fc6
PM
32#include <linux/netfilter_ipv6/ip6_tables.h>
33#include <linux/netfilter/xt_hashlimit.h>
14cc3e2b 34#include <linux/mutex.h>
1da177e4
LT
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
09e410de 38MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
2ae15b64 39MODULE_DESCRIPTION("Xtables: per hash-bucket rate-limit match");
39b46fc6
PM
40MODULE_ALIAS("ipt_hashlimit");
41MODULE_ALIAS("ip6t_hashlimit");
1da177e4
LT
42
43/* need to declare this at the top */
39b46fc6
PM
44static struct proc_dir_entry *hashlimit_procdir4;
45static struct proc_dir_entry *hashlimit_procdir6;
da7071d7 46static const struct file_operations dl_file_ops;
1da177e4
LT
47
48/* hash table crap */
1da177e4 49struct dsthash_dst {
39b46fc6
PM
50 union {
51 struct {
52 __be32 src;
53 __be32 dst;
54 } ip;
7b21e09d 55#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
39b46fc6
PM
56 struct {
57 __be32 src[4];
58 __be32 dst[4];
59 } ip6;
7b21e09d 60#endif
09e410de 61 };
6a19d614
AV
62 __be16 src_port;
63 __be16 dst_port;
1da177e4
LT
64};
65
66struct dsthash_ent {
67 /* static / read-only parts in the beginning */
68 struct hlist_node node;
69 struct dsthash_dst dst;
70
71 /* modified structure members in the end */
72 unsigned long expires; /* precalculated expiry time */
73 struct {
74 unsigned long prev; /* last modification */
75 u_int32_t credit;
76 u_int32_t credit_cap, cost;
77 } rateinfo;
78};
79
39b46fc6 80struct xt_hashlimit_htable {
1da177e4
LT
81 struct hlist_node node; /* global list of all htables */
82 atomic_t use;
39b46fc6 83 int family;
1da177e4 84
09e410de 85 struct hashlimit_cfg1 cfg; /* config */
1da177e4
LT
86
87 /* used internally */
88 spinlock_t lock; /* lock for list_head */
89 u_int32_t rnd; /* random seed for hash */
bf0857ea 90 int rnd_initialized;
39b46fc6 91 unsigned int count; /* number entries in table */
1da177e4 92 struct timer_list timer; /* timer for gc */
1da177e4
LT
93
94 /* seq_file stuff */
95 struct proc_dir_entry *pde;
96
97 struct hlist_head hash[0]; /* hashtable itself */
98};
99
e45b1be8 100static DEFINE_SPINLOCK(hashlimit_lock); /* protects htables list */
14cc3e2b 101static DEFINE_MUTEX(hlimit_mutex); /* additional checkentry protection */
1da177e4 102static HLIST_HEAD(hashlimit_htables);
e18b890b 103static struct kmem_cache *hashlimit_cachep __read_mostly;
1da177e4 104
1d93a9cb 105static inline bool dst_cmp(const struct dsthash_ent *ent,
a47362a2 106 const struct dsthash_dst *b)
1da177e4 107{
39b46fc6 108 return !memcmp(&ent->dst, b, sizeof(ent->dst));
1da177e4
LT
109}
110
39b46fc6
PM
111static u_int32_t
112hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
1da177e4 113{
e2f82ac3
ED
114 u_int32_t hash = jhash2((const u32 *)dst,
115 sizeof(*dst)/sizeof(u32),
116 ht->rnd);
117 /*
118 * Instead of returning hash % ht->cfg.size (implying a divide)
119 * we return the high 32 bits of the (hash * ht->cfg.size) that will
120 * give results between [0 and cfg.size-1] and same hash distribution,
121 * but using a multiply, less expensive than a divide
122 */
123 return ((u64)hash * ht->cfg.size) >> 32;
1da177e4
LT
124}
125
39b46fc6 126static struct dsthash_ent *
a47362a2
JE
127dsthash_find(const struct xt_hashlimit_htable *ht,
128 const struct dsthash_dst *dst)
1da177e4
LT
129{
130 struct dsthash_ent *ent;
131 struct hlist_node *pos;
132 u_int32_t hash = hash_dst(ht, dst);
133
39b46fc6
PM
134 if (!hlist_empty(&ht->hash[hash])) {
135 hlist_for_each_entry(ent, pos, &ht->hash[hash], node)
136 if (dst_cmp(ent, dst))
1da177e4 137 return ent;
39b46fc6 138 }
1da177e4
LT
139 return NULL;
140}
141
142/* allocate dsthash_ent, initialize dst, put in htable and lock it */
143static struct dsthash_ent *
a47362a2
JE
144dsthash_alloc_init(struct xt_hashlimit_htable *ht,
145 const struct dsthash_dst *dst)
1da177e4
LT
146{
147 struct dsthash_ent *ent;
148
149 /* initialize hash with random val at the time we allocate
150 * the first hashtable entry */
bf0857ea 151 if (!ht->rnd_initialized) {
1da177e4 152 get_random_bytes(&ht->rnd, 4);
bf0857ea
PM
153 ht->rnd_initialized = 1;
154 }
1da177e4 155
39b46fc6 156 if (ht->cfg.max && ht->count >= ht->cfg.max) {
1da177e4
LT
157 /* FIXME: do something. question is what.. */
158 if (net_ratelimit())
39b46fc6
PM
159 printk(KERN_WARNING
160 "xt_hashlimit: max count of %u reached\n",
1da177e4
LT
161 ht->cfg.max);
162 return NULL;
163 }
164
165 ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
166 if (!ent) {
167 if (net_ratelimit())
39b46fc6
PM
168 printk(KERN_ERR
169 "xt_hashlimit: can't allocate dsthash_ent\n");
1da177e4
LT
170 return NULL;
171 }
39b46fc6 172 memcpy(&ent->dst, dst, sizeof(ent->dst));
1da177e4
LT
173
174 hlist_add_head(&ent->node, &ht->hash[hash_dst(ht, dst)]);
39b46fc6 175 ht->count++;
1da177e4
LT
176 return ent;
177}
178
39b46fc6
PM
179static inline void
180dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
1da177e4
LT
181{
182 hlist_del(&ent->node);
183 kmem_cache_free(hashlimit_cachep, ent);
39b46fc6 184 ht->count--;
1da177e4
LT
185}
186static void htable_gc(unsigned long htlong);
187
09e410de 188static int htable_create_v0(struct xt_hashlimit_info *minfo, int family)
1da177e4 189{
39b46fc6 190 struct xt_hashlimit_htable *hinfo;
1da177e4 191 unsigned int size;
39b46fc6 192 unsigned int i;
1da177e4
LT
193
194 if (minfo->cfg.size)
195 size = minfo->cfg.size;
196 else {
39b46fc6
PM
197 size = ((num_physpages << PAGE_SHIFT) / 16384) /
198 sizeof(struct list_head);
1da177e4
LT
199 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
200 size = 8192;
201 if (size < 16)
202 size = 16;
203 }
204 /* FIXME: don't use vmalloc() here or anywhere else -HW */
39b46fc6
PM
205 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
206 sizeof(struct list_head) * size);
1da177e4 207 if (!hinfo) {
39b46fc6 208 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n");
1da177e4
LT
209 return -1;
210 }
211 minfo->hinfo = hinfo;
212
213 /* copy match config into hashtable config */
09e410de
JE
214 hinfo->cfg.mode = minfo->cfg.mode;
215 hinfo->cfg.avg = minfo->cfg.avg;
216 hinfo->cfg.burst = minfo->cfg.burst;
217 hinfo->cfg.max = minfo->cfg.max;
218 hinfo->cfg.gc_interval = minfo->cfg.gc_interval;
219 hinfo->cfg.expire = minfo->cfg.expire;
220
221 if (family == AF_INET)
222 hinfo->cfg.srcmask = hinfo->cfg.dstmask = 32;
223 else
224 hinfo->cfg.srcmask = hinfo->cfg.dstmask = 128;
225
1da177e4
LT
226 hinfo->cfg.size = size;
227 if (!hinfo->cfg.max)
228 hinfo->cfg.max = 8 * hinfo->cfg.size;
229 else if (hinfo->cfg.max < hinfo->cfg.size)
230 hinfo->cfg.max = hinfo->cfg.size;
231
232 for (i = 0; i < hinfo->cfg.size; i++)
233 INIT_HLIST_HEAD(&hinfo->hash[i]);
234
1da177e4 235 atomic_set(&hinfo->use, 1);
39b46fc6
PM
236 hinfo->count = 0;
237 hinfo->family = family;
bf0857ea 238 hinfo->rnd_initialized = 0;
1da177e4 239 spin_lock_init(&hinfo->lock);
8eeee8b1
DL
240 hinfo->pde = proc_create(minfo->name, 0,
241 family == AF_INET ? hashlimit_procdir4 :
242 hashlimit_procdir6,
243 &dl_file_ops);
1da177e4
LT
244 if (!hinfo->pde) {
245 vfree(hinfo);
246 return -1;
247 }
1da177e4
LT
248 hinfo->pde->data = hinfo;
249
e6f689db 250 setup_timer(&hinfo->timer, htable_gc, (unsigned long )hinfo);
1da177e4 251 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
1da177e4
LT
252 add_timer(&hinfo->timer);
253
e45b1be8 254 spin_lock_bh(&hashlimit_lock);
1da177e4 255 hlist_add_head(&hinfo->node, &hashlimit_htables);
e45b1be8 256 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
257
258 return 0;
259}
260
09e410de
JE
261static int htable_create(struct xt_hashlimit_mtinfo1 *minfo,
262 unsigned int family)
263{
264 struct xt_hashlimit_htable *hinfo;
265 unsigned int size;
266 unsigned int i;
267
268 if (minfo->cfg.size) {
269 size = minfo->cfg.size;
270 } else {
271 size = (num_physpages << PAGE_SHIFT) / 16384 /
272 sizeof(struct list_head);
273 if (num_physpages > 1024 * 1024 * 1024 / PAGE_SIZE)
274 size = 8192;
275 if (size < 16)
276 size = 16;
277 }
278 /* FIXME: don't use vmalloc() here or anywhere else -HW */
279 hinfo = vmalloc(sizeof(struct xt_hashlimit_htable) +
280 sizeof(struct list_head) * size);
281 if (hinfo == NULL) {
282 printk(KERN_ERR "xt_hashlimit: unable to create hashtable\n");
283 return -1;
284 }
285 minfo->hinfo = hinfo;
286
287 /* copy match config into hashtable config */
288 memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
289 hinfo->cfg.size = size;
290 if (hinfo->cfg.max == 0)
291 hinfo->cfg.max = 8 * hinfo->cfg.size;
292 else if (hinfo->cfg.max < hinfo->cfg.size)
293 hinfo->cfg.max = hinfo->cfg.size;
294
295 for (i = 0; i < hinfo->cfg.size; i++)
296 INIT_HLIST_HEAD(&hinfo->hash[i]);
297
298 atomic_set(&hinfo->use, 1);
299 hinfo->count = 0;
300 hinfo->family = family;
301 hinfo->rnd_initialized = 0;
302 spin_lock_init(&hinfo->lock);
303
8eeee8b1
DL
304 hinfo->pde = proc_create(minfo->name, 0,
305 family == AF_INET ? hashlimit_procdir4 :
306 hashlimit_procdir6,
307 &dl_file_ops);
09e410de
JE
308 if (hinfo->pde == NULL) {
309 vfree(hinfo);
310 return -1;
311 }
09e410de
JE
312 hinfo->pde->data = hinfo;
313
314 setup_timer(&hinfo->timer, htable_gc, (unsigned long)hinfo);
315 hinfo->timer.expires = jiffies + msecs_to_jiffies(hinfo->cfg.gc_interval);
316 add_timer(&hinfo->timer);
317
318 spin_lock_bh(&hashlimit_lock);
319 hlist_add_head(&hinfo->node, &hashlimit_htables);
320 spin_unlock_bh(&hashlimit_lock);
321
322 return 0;
323}
324
a47362a2
JE
325static bool select_all(const struct xt_hashlimit_htable *ht,
326 const struct dsthash_ent *he)
1da177e4
LT
327{
328 return 1;
329}
330
a47362a2
JE
331static bool select_gc(const struct xt_hashlimit_htable *ht,
332 const struct dsthash_ent *he)
1da177e4 333{
cbebc51f 334 return time_after_eq(jiffies, he->expires);
1da177e4
LT
335}
336
39b46fc6 337static void htable_selective_cleanup(struct xt_hashlimit_htable *ht,
a47362a2
JE
338 bool (*select)(const struct xt_hashlimit_htable *ht,
339 const struct dsthash_ent *he))
1da177e4 340{
39b46fc6 341 unsigned int i;
1da177e4
LT
342
343 /* lock hash table and iterate over it */
344 spin_lock_bh(&ht->lock);
345 for (i = 0; i < ht->cfg.size; i++) {
346 struct dsthash_ent *dh;
347 struct hlist_node *pos, *n;
348 hlist_for_each_entry_safe(dh, pos, n, &ht->hash[i], node) {
349 if ((*select)(ht, dh))
39b46fc6 350 dsthash_free(ht, dh);
1da177e4
LT
351 }
352 }
353 spin_unlock_bh(&ht->lock);
354}
355
356/* hash table garbage collector, run by timer */
357static void htable_gc(unsigned long htlong)
358{
39b46fc6 359 struct xt_hashlimit_htable *ht = (struct xt_hashlimit_htable *)htlong;
1da177e4
LT
360
361 htable_selective_cleanup(ht, select_gc);
362
363 /* re-add the timer accordingly */
364 ht->timer.expires = jiffies + msecs_to_jiffies(ht->cfg.gc_interval);
365 add_timer(&ht->timer);
366}
367
39b46fc6 368static void htable_destroy(struct xt_hashlimit_htable *hinfo)
1da177e4
LT
369{
370 /* remove timer, if it is pending */
371 if (timer_pending(&hinfo->timer))
372 del_timer(&hinfo->timer);
373
374 /* remove proc entry */
39b46fc6
PM
375 remove_proc_entry(hinfo->pde->name,
376 hinfo->family == AF_INET ? hashlimit_procdir4 :
601e68e1 377 hashlimit_procdir6);
1da177e4
LT
378 htable_selective_cleanup(hinfo, select_all);
379 vfree(hinfo);
380}
381
a47362a2
JE
382static struct xt_hashlimit_htable *htable_find_get(const char *name,
383 int family)
1da177e4 384{
39b46fc6 385 struct xt_hashlimit_htable *hinfo;
1da177e4
LT
386 struct hlist_node *pos;
387
e45b1be8 388 spin_lock_bh(&hashlimit_lock);
1da177e4 389 hlist_for_each_entry(hinfo, pos, &hashlimit_htables, node) {
39b46fc6
PM
390 if (!strcmp(name, hinfo->pde->name) &&
391 hinfo->family == family) {
1da177e4 392 atomic_inc(&hinfo->use);
e45b1be8 393 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
394 return hinfo;
395 }
396 }
e45b1be8 397 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
398 return NULL;
399}
400
39b46fc6 401static void htable_put(struct xt_hashlimit_htable *hinfo)
1da177e4
LT
402{
403 if (atomic_dec_and_test(&hinfo->use)) {
e45b1be8 404 spin_lock_bh(&hashlimit_lock);
1da177e4 405 hlist_del(&hinfo->node);
e45b1be8 406 spin_unlock_bh(&hashlimit_lock);
1da177e4
LT
407 htable_destroy(hinfo);
408 }
409}
410
1da177e4
LT
411/* The algorithm used is the Simple Token Bucket Filter (TBF)
412 * see net/sched/sch_tbf.c in the linux source tree
413 */
414
415/* Rusty: This is my (non-mathematically-inclined) understanding of
416 this algorithm. The `average rate' in jiffies becomes your initial
417 amount of credit `credit' and the most credit you can ever have
418 `credit_cap'. The `peak rate' becomes the cost of passing the
419 test, `cost'.
420
421 `prev' tracks the last packet hit: you gain one credit per jiffy.
422 If you get credit balance more than this, the extra credit is
423 discarded. Every time the match passes, you lose `cost' credits;
424 if you don't have that many, the test fails.
425
426 See Alexey's formal explanation in net/sched/sch_tbf.c.
427
428 To get the maximum range, we multiply by this factor (ie. you get N
429 credits per jiffy). We want to allow a rate as low as 1 per day
430 (slowest userspace tool allows), which means
431 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
432*/
433#define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
434
435/* Repeated shift and or gives us all 1s, final shift and add 1 gives
436 * us the power of 2 below the theoretical max, so GCC simply does a
437 * shift. */
438#define _POW2_BELOW2(x) ((x)|((x)>>1))
439#define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
440#define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
441#define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
442#define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
443#define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
444
445#define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
446
447/* Precision saver. */
448static inline u_int32_t
449user2credits(u_int32_t user)
450{
451 /* If multiplying would overflow... */
452 if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
453 /* Divide first. */
39b46fc6 454 return (user / XT_HASHLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
1da177e4 455
39b46fc6 456 return (user * HZ * CREDITS_PER_JIFFY) / XT_HASHLIMIT_SCALE;
1da177e4
LT
457}
458
459static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
460{
39b46fc6 461 dh->rateinfo.credit += (now - dh->rateinfo.prev) * CREDITS_PER_JIFFY;
1da177e4
LT
462 if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
463 dh->rateinfo.credit = dh->rateinfo.credit_cap;
39b46fc6
PM
464 dh->rateinfo.prev = now;
465}
466
09e410de
JE
467static inline __be32 maskl(__be32 a, unsigned int l)
468{
469 return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
470}
471
3ed5df44 472#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
09e410de
JE
473static void hashlimit_ipv6_mask(__be32 *i, unsigned int p)
474{
475 switch (p) {
476 case 0:
477 i[0] = i[1] = 0;
478 i[2] = i[3] = 0;
479 break;
480 case 1 ... 31:
481 i[0] = maskl(i[0], p);
482 i[1] = i[2] = i[3] = 0;
483 break;
484 case 32:
485 i[1] = i[2] = i[3] = 0;
486 break;
487 case 33 ... 63:
488 i[1] = maskl(i[1], p - 32);
489 i[2] = i[3] = 0;
490 break;
491 case 64:
492 i[2] = i[3] = 0;
493 break;
494 case 65 ... 95:
495 i[2] = maskl(i[2], p - 64);
496 i[3] = 0;
497 case 96:
498 i[3] = 0;
499 break;
500 case 97 ... 127:
501 i[3] = maskl(i[3], p - 96);
502 break;
503 case 128:
504 break;
505 }
506}
3ed5df44 507#endif
09e410de 508
39b46fc6 509static int
a47362a2
JE
510hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo,
511 struct dsthash_dst *dst,
39b46fc6
PM
512 const struct sk_buff *skb, unsigned int protoff)
513{
514 __be16 _ports[2], *ports;
193b23c5 515 u8 nexthdr;
39b46fc6
PM
516
517 memset(dst, 0, sizeof(*dst));
518
519 switch (hinfo->family) {
520 case AF_INET:
521 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
09e410de
JE
522 dst->ip.dst = maskl(ip_hdr(skb)->daddr,
523 hinfo->cfg.dstmask);
39b46fc6 524 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
09e410de
JE
525 dst->ip.src = maskl(ip_hdr(skb)->saddr,
526 hinfo->cfg.srcmask);
39b46fc6
PM
527
528 if (!(hinfo->cfg.mode &
529 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
530 return 0;
eddc9ec5 531 nexthdr = ip_hdr(skb)->protocol;
39b46fc6 532 break;
02dba025 533#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
39b46fc6 534 case AF_INET6:
09e410de
JE
535 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) {
536 memcpy(&dst->ip6.dst, &ipv6_hdr(skb)->daddr,
537 sizeof(dst->ip6.dst));
538 hashlimit_ipv6_mask(dst->ip6.dst, hinfo->cfg.dstmask);
539 }
540 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) {
541 memcpy(&dst->ip6.src, &ipv6_hdr(skb)->saddr,
542 sizeof(dst->ip6.src));
543 hashlimit_ipv6_mask(dst->ip6.src, hinfo->cfg.srcmask);
544 }
39b46fc6
PM
545
546 if (!(hinfo->cfg.mode &
547 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
548 return 0;
193b23c5
PM
549 nexthdr = ipv6_hdr(skb)->nexthdr;
550 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr);
551 if ((int)protoff < 0)
39b46fc6
PM
552 return -1;
553 break;
554#endif
555 default:
556 BUG();
557 return 0;
558 }
559
560 switch (nexthdr) {
561 case IPPROTO_TCP:
562 case IPPROTO_UDP:
a8d0f952 563 case IPPROTO_UDPLITE:
39b46fc6
PM
564 case IPPROTO_SCTP:
565 case IPPROTO_DCCP:
566 ports = skb_header_pointer(skb, protoff, sizeof(_ports),
567 &_ports);
568 break;
569 default:
570 _ports[0] = _ports[1] = 0;
571 ports = _ports;
572 break;
573 }
574 if (!ports)
575 return -1;
576 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT)
577 dst->src_port = ports[0];
578 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT)
579 dst->dst_port = ports[1];
580 return 0;
1da177e4
LT
581}
582
1d93a9cb 583static bool
09e410de
JE
584hashlimit_mt_v0(const struct sk_buff *skb, const struct net_device *in,
585 const struct net_device *out, const struct xt_match *match,
586 const void *matchinfo, int offset, unsigned int protoff,
587 bool *hotdrop)
1da177e4 588{
a47362a2
JE
589 const struct xt_hashlimit_info *r =
590 ((const struct xt_hashlimit_info *)matchinfo)->u.master;
39b46fc6 591 struct xt_hashlimit_htable *hinfo = r->hinfo;
1da177e4
LT
592 unsigned long now = jiffies;
593 struct dsthash_ent *dh;
594 struct dsthash_dst dst;
595
39b46fc6
PM
596 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0)
597 goto hotdrop;
1da177e4
LT
598
599 spin_lock_bh(&hinfo->lock);
39b46fc6 600 dh = dsthash_find(hinfo, &dst);
1da177e4 601 if (!dh) {
39b46fc6 602 dh = dsthash_alloc_init(hinfo, &dst);
1da177e4 603 if (!dh) {
1da177e4 604 spin_unlock_bh(&hinfo->lock);
39b46fc6 605 goto hotdrop;
1da177e4
LT
606 }
607
608 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
1da177e4 609 dh->rateinfo.prev = jiffies;
39b46fc6
PM
610 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
611 hinfo->cfg.burst);
612 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
613 hinfo->cfg.burst);
1da177e4 614 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
1c7628bd
PM
615 } else {
616 /* update expiration timeout */
617 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
618 rateinfo_recalc(dh, now);
1da177e4
LT
619 }
620
1da177e4
LT
621 if (dh->rateinfo.credit >= dh->rateinfo.cost) {
622 /* We're underlimit. */
623 dh->rateinfo.credit -= dh->rateinfo.cost;
624 spin_unlock_bh(&hinfo->lock);
1d93a9cb 625 return true;
1da177e4
LT
626 }
627
601e68e1 628 spin_unlock_bh(&hinfo->lock);
1da177e4
LT
629
630 /* default case: we're overlimit, thus don't match */
1d93a9cb 631 return false;
39b46fc6
PM
632
633hotdrop:
cff533ac 634 *hotdrop = true;
1d93a9cb 635 return false;
1da177e4
LT
636}
637
ccb79bdc 638static bool
09e410de
JE
639hashlimit_mt(const struct sk_buff *skb, const struct net_device *in,
640 const struct net_device *out, const struct xt_match *match,
641 const void *matchinfo, int offset, unsigned int protoff,
642 bool *hotdrop)
643{
644 const struct xt_hashlimit_mtinfo1 *info = matchinfo;
645 struct xt_hashlimit_htable *hinfo = info->hinfo;
646 unsigned long now = jiffies;
647 struct dsthash_ent *dh;
648 struct dsthash_dst dst;
649
650 if (hashlimit_init_dst(hinfo, &dst, skb, protoff) < 0)
651 goto hotdrop;
652
653 spin_lock_bh(&hinfo->lock);
654 dh = dsthash_find(hinfo, &dst);
655 if (dh == NULL) {
656 dh = dsthash_alloc_init(hinfo, &dst);
657 if (dh == NULL) {
658 spin_unlock_bh(&hinfo->lock);
659 goto hotdrop;
660 }
661
662 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
663 dh->rateinfo.prev = jiffies;
664 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
665 hinfo->cfg.burst);
666 dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
667 hinfo->cfg.burst);
668 dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
669 } else {
670 /* update expiration timeout */
671 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
672 rateinfo_recalc(dh, now);
673 }
674
675 if (dh->rateinfo.credit >= dh->rateinfo.cost) {
676 /* below the limit */
677 dh->rateinfo.credit -= dh->rateinfo.cost;
678 spin_unlock_bh(&hinfo->lock);
679 return !(info->cfg.mode & XT_HASHLIMIT_INVERT);
680 }
681
682 spin_unlock_bh(&hinfo->lock);
683 /* default match is underlimit - so over the limit, we need to invert */
684 return info->cfg.mode & XT_HASHLIMIT_INVERT;
685
686 hotdrop:
687 *hotdrop = true;
688 return false;
689}
690
691static bool
692hashlimit_mt_check_v0(const char *tablename, const void *inf,
693 const struct xt_match *match, void *matchinfo,
694 unsigned int hook_mask)
1da177e4 695{
39b46fc6 696 struct xt_hashlimit_info *r = matchinfo;
1da177e4 697
1da177e4 698 /* Check for overflow. */
39b46fc6
PM
699 if (r->cfg.burst == 0 ||
700 user2credits(r->cfg.avg * r->cfg.burst) < user2credits(r->cfg.avg)) {
701 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n",
1da177e4 702 r->cfg.avg, r->cfg.burst);
ccb79bdc 703 return false;
1da177e4 704 }
39b46fc6
PM
705 if (r->cfg.mode == 0 ||
706 r->cfg.mode > (XT_HASHLIMIT_HASH_DPT |
707 XT_HASHLIMIT_HASH_DIP |
708 XT_HASHLIMIT_HASH_SIP |
709 XT_HASHLIMIT_HASH_SPT))
ccb79bdc 710 return false;
1da177e4 711 if (!r->cfg.gc_interval)
ccb79bdc 712 return false;
1da177e4 713 if (!r->cfg.expire)
ccb79bdc 714 return false;
3ab72088 715 if (r->name[sizeof(r->name) - 1] != '\0')
ccb79bdc 716 return false;
3ab72088 717
1da177e4 718 /* This is the best we've got: We cannot release and re-grab lock,
39b46fc6
PM
719 * since checkentry() is called before x_tables.c grabs xt_mutex.
720 * We also cannot grab the hashtable spinlock, since htable_create will
1da177e4
LT
721 * call vmalloc, and that can sleep. And we cannot just re-search
722 * the list of htable's in htable_create(), since then we would
723 * create duplicate proc files. -HW */
14cc3e2b 724 mutex_lock(&hlimit_mutex);
39b46fc6 725 r->hinfo = htable_find_get(r->name, match->family);
09e410de 726 if (!r->hinfo && htable_create_v0(r, match->family) != 0) {
14cc3e2b 727 mutex_unlock(&hlimit_mutex);
ccb79bdc 728 return false;
1da177e4 729 }
14cc3e2b 730 mutex_unlock(&hlimit_mutex);
1da177e4
LT
731
732 /* Ugly hack: For SMP, we only want to use one set */
733 r->u.master = r;
ccb79bdc 734 return true;
1da177e4
LT
735}
736
09e410de
JE
737static bool
738hashlimit_mt_check(const char *tablename, const void *inf,
739 const struct xt_match *match, void *matchinfo,
740 unsigned int hook_mask)
741{
742 struct xt_hashlimit_mtinfo1 *info = matchinfo;
743
744 /* Check for overflow. */
745 if (info->cfg.burst == 0 ||
746 user2credits(info->cfg.avg * info->cfg.burst) <
747 user2credits(info->cfg.avg)) {
748 printk(KERN_ERR "xt_hashlimit: overflow, try lower: %u/%u\n",
749 info->cfg.avg, info->cfg.burst);
750 return false;
751 }
752 if (info->cfg.gc_interval == 0 || info->cfg.expire == 0)
753 return false;
754 if (info->name[sizeof(info->name)-1] != '\0')
755 return false;
756 if (match->family == AF_INET) {
757 if (info->cfg.srcmask > 32 || info->cfg.dstmask > 32)
758 return false;
759 } else {
760 if (info->cfg.srcmask > 128 || info->cfg.dstmask > 128)
761 return false;
762 }
763
764 /* This is the best we've got: We cannot release and re-grab lock,
765 * since checkentry() is called before x_tables.c grabs xt_mutex.
766 * We also cannot grab the hashtable spinlock, since htable_create will
767 * call vmalloc, and that can sleep. And we cannot just re-search
768 * the list of htable's in htable_create(), since then we would
769 * create duplicate proc files. -HW */
770 mutex_lock(&hlimit_mutex);
771 info->hinfo = htable_find_get(info->name, match->family);
772 if (!info->hinfo && htable_create(info, match->family) != 0) {
773 mutex_unlock(&hlimit_mutex);
774 return false;
775 }
776 mutex_unlock(&hlimit_mutex);
09e410de
JE
777 return true;
778}
779
1da177e4 780static void
09e410de 781hashlimit_mt_destroy_v0(const struct xt_match *match, void *matchinfo)
1da177e4 782{
a47362a2 783 const struct xt_hashlimit_info *r = matchinfo;
1da177e4
LT
784
785 htable_put(r->hinfo);
786}
787
09e410de
JE
788static void
789hashlimit_mt_destroy(const struct xt_match *match, void *matchinfo)
790{
791 const struct xt_hashlimit_mtinfo1 *info = matchinfo;
792
793 htable_put(info->hinfo);
794}
795
127f15dd 796#ifdef CONFIG_COMPAT
39b46fc6 797struct compat_xt_hashlimit_info {
127f15dd
PM
798 char name[IFNAMSIZ];
799 struct hashlimit_cfg cfg;
800 compat_uptr_t hinfo;
801 compat_uptr_t master;
802};
803
d3c5ee6d 804static void hashlimit_mt_compat_from_user(void *dst, void *src)
127f15dd 805{
39b46fc6 806 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
127f15dd
PM
807
808 memcpy(dst, src, off);
39b46fc6 809 memset(dst + off, 0, sizeof(struct compat_xt_hashlimit_info) - off);
127f15dd
PM
810}
811
d3c5ee6d 812static int hashlimit_mt_compat_to_user(void __user *dst, void *src)
127f15dd 813{
39b46fc6 814 int off = offsetof(struct compat_xt_hashlimit_info, hinfo);
127f15dd
PM
815
816 return copy_to_user(dst, src, off) ? -EFAULT : 0;
817}
818#endif
819
d3c5ee6d 820static struct xt_match hashlimit_mt_reg[] __read_mostly = {
39b46fc6
PM
821 {
822 .name = "hashlimit",
09e410de 823 .revision = 0,
39b46fc6 824 .family = AF_INET,
09e410de 825 .match = hashlimit_mt_v0,
39b46fc6
PM
826 .matchsize = sizeof(struct xt_hashlimit_info),
827#ifdef CONFIG_COMPAT
828 .compatsize = sizeof(struct compat_xt_hashlimit_info),
d3c5ee6d
JE
829 .compat_from_user = hashlimit_mt_compat_from_user,
830 .compat_to_user = hashlimit_mt_compat_to_user,
39b46fc6 831#endif
09e410de
JE
832 .checkentry = hashlimit_mt_check_v0,
833 .destroy = hashlimit_mt_destroy_v0,
39b46fc6
PM
834 .me = THIS_MODULE
835 },
09e410de
JE
836 {
837 .name = "hashlimit",
838 .revision = 1,
839 .family = AF_INET,
840 .match = hashlimit_mt,
841 .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
842 .checkentry = hashlimit_mt_check,
843 .destroy = hashlimit_mt_destroy,
844 .me = THIS_MODULE,
845 },
7b21e09d 846#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
39b46fc6
PM
847 {
848 .name = "hashlimit",
849 .family = AF_INET6,
09e410de 850 .match = hashlimit_mt_v0,
39b46fc6 851 .matchsize = sizeof(struct xt_hashlimit_info),
127f15dd 852#ifdef CONFIG_COMPAT
39b46fc6 853 .compatsize = sizeof(struct compat_xt_hashlimit_info),
d3c5ee6d
JE
854 .compat_from_user = hashlimit_mt_compat_from_user,
855 .compat_to_user = hashlimit_mt_compat_to_user,
127f15dd 856#endif
09e410de
JE
857 .checkentry = hashlimit_mt_check_v0,
858 .destroy = hashlimit_mt_destroy_v0,
39b46fc6
PM
859 .me = THIS_MODULE
860 },
09e410de
JE
861 {
862 .name = "hashlimit",
863 .revision = 1,
864 .family = AF_INET6,
865 .match = hashlimit_mt,
866 .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
867 .checkentry = hashlimit_mt_check,
868 .destroy = hashlimit_mt_destroy,
869 .me = THIS_MODULE,
870 },
7b21e09d 871#endif
1da177e4
LT
872};
873
874/* PROC stuff */
1da177e4 875static void *dl_seq_start(struct seq_file *s, loff_t *pos)
f4f6fb71 876 __acquires(htable->lock)
1da177e4
LT
877{
878 struct proc_dir_entry *pde = s->private;
39b46fc6 879 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
880 unsigned int *bucket;
881
882 spin_lock_bh(&htable->lock);
883 if (*pos >= htable->cfg.size)
884 return NULL;
885
886 bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
887 if (!bucket)
888 return ERR_PTR(-ENOMEM);
889
890 *bucket = *pos;
891 return bucket;
892}
893
894static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
895{
896 struct proc_dir_entry *pde = s->private;
39b46fc6 897 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
898 unsigned int *bucket = (unsigned int *)v;
899
900 *pos = ++(*bucket);
901 if (*pos >= htable->cfg.size) {
902 kfree(v);
903 return NULL;
904 }
905 return bucket;
906}
907
908static void dl_seq_stop(struct seq_file *s, void *v)
f4f6fb71 909 __releases(htable->lock)
1da177e4
LT
910{
911 struct proc_dir_entry *pde = s->private;
39b46fc6 912 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
913 unsigned int *bucket = (unsigned int *)v;
914
915 kfree(bucket);
1da177e4
LT
916 spin_unlock_bh(&htable->lock);
917}
918
39b46fc6
PM
919static int dl_seq_real_show(struct dsthash_ent *ent, int family,
920 struct seq_file *s)
1da177e4
LT
921{
922 /* recalculate to show accurate numbers */
923 rateinfo_recalc(ent, jiffies);
924
39b46fc6
PM
925 switch (family) {
926 case AF_INET:
927 return seq_printf(s, "%ld %u.%u.%u.%u:%u->"
928 "%u.%u.%u.%u:%u %u %u %u\n",
929 (long)(ent->expires - jiffies)/HZ,
09e410de 930 NIPQUAD(ent->dst.ip.src),
39b46fc6 931 ntohs(ent->dst.src_port),
09e410de 932 NIPQUAD(ent->dst.ip.dst),
39b46fc6
PM
933 ntohs(ent->dst.dst_port),
934 ent->rateinfo.credit, ent->rateinfo.credit_cap,
935 ent->rateinfo.cost);
7b21e09d 936#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
39b46fc6
PM
937 case AF_INET6:
938 return seq_printf(s, "%ld " NIP6_FMT ":%u->"
939 NIP6_FMT ":%u %u %u %u\n",
940 (long)(ent->expires - jiffies)/HZ,
09e410de 941 NIP6(*(struct in6_addr *)&ent->dst.ip6.src),
39b46fc6 942 ntohs(ent->dst.src_port),
09e410de 943 NIP6(*(struct in6_addr *)&ent->dst.ip6.dst),
39b46fc6
PM
944 ntohs(ent->dst.dst_port),
945 ent->rateinfo.credit, ent->rateinfo.credit_cap,
946 ent->rateinfo.cost);
7b21e09d 947#endif
39b46fc6
PM
948 default:
949 BUG();
950 return 0;
951 }
1da177e4
LT
952}
953
954static int dl_seq_show(struct seq_file *s, void *v)
955{
956 struct proc_dir_entry *pde = s->private;
39b46fc6 957 struct xt_hashlimit_htable *htable = pde->data;
1da177e4
LT
958 unsigned int *bucket = (unsigned int *)v;
959 struct dsthash_ent *ent;
960 struct hlist_node *pos;
961
39b46fc6
PM
962 if (!hlist_empty(&htable->hash[*bucket])) {
963 hlist_for_each_entry(ent, pos, &htable->hash[*bucket], node)
964 if (dl_seq_real_show(ent, htable->family, s))
1da177e4 965 return 1;
39b46fc6 966 }
1da177e4
LT
967 return 0;
968}
969
56b3d975 970static const struct seq_operations dl_seq_ops = {
1da177e4
LT
971 .start = dl_seq_start,
972 .next = dl_seq_next,
973 .stop = dl_seq_stop,
974 .show = dl_seq_show
975};
976
977static int dl_proc_open(struct inode *inode, struct file *file)
978{
979 int ret = seq_open(file, &dl_seq_ops);
980
981 if (!ret) {
982 struct seq_file *sf = file->private_data;
983 sf->private = PDE(inode);
984 }
985 return ret;
986}
987
da7071d7 988static const struct file_operations dl_file_ops = {
1da177e4
LT
989 .owner = THIS_MODULE,
990 .open = dl_proc_open,
991 .read = seq_read,
992 .llseek = seq_lseek,
993 .release = seq_release
994};
995
d3c5ee6d 996static int __init hashlimit_mt_init(void)
1da177e4 997{
39b46fc6 998 int err;
1da177e4 999
d3c5ee6d
JE
1000 err = xt_register_matches(hashlimit_mt_reg,
1001 ARRAY_SIZE(hashlimit_mt_reg));
39b46fc6
PM
1002 if (err < 0)
1003 goto err1;
1da177e4 1004
39b46fc6
PM
1005 err = -ENOMEM;
1006 hashlimit_cachep = kmem_cache_create("xt_hashlimit",
1007 sizeof(struct dsthash_ent), 0, 0,
20c2df83 1008 NULL);
1da177e4 1009 if (!hashlimit_cachep) {
39b46fc6
PM
1010 printk(KERN_ERR "xt_hashlimit: unable to create slab cache\n");
1011 goto err2;
1da177e4 1012 }
457c4cbc 1013 hashlimit_procdir4 = proc_mkdir("ipt_hashlimit", init_net.proc_net);
39b46fc6
PM
1014 if (!hashlimit_procdir4) {
1015 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
1016 "entry\n");
1017 goto err3;
1da177e4 1018 }
7b21e09d
ED
1019 err = 0;
1020#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
457c4cbc 1021 hashlimit_procdir6 = proc_mkdir("ip6t_hashlimit", init_net.proc_net);
39b46fc6 1022 if (!hashlimit_procdir6) {
9c2440bb 1023 printk(KERN_ERR "xt_hashlimit: unable to create proc dir "
39b46fc6 1024 "entry\n");
7b21e09d 1025 err = -ENOMEM;
39b46fc6 1026 }
7b21e09d
ED
1027#endif
1028 if (!err)
1029 return 0;
457c4cbc 1030 remove_proc_entry("ipt_hashlimit", init_net.proc_net);
39b46fc6 1031err3:
1da177e4 1032 kmem_cache_destroy(hashlimit_cachep);
39b46fc6 1033err2:
d3c5ee6d 1034 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
39b46fc6
PM
1035err1:
1036 return err;
1da177e4 1037
1da177e4
LT
1038}
1039
d3c5ee6d 1040static void __exit hashlimit_mt_exit(void)
1da177e4 1041{
457c4cbc 1042 remove_proc_entry("ipt_hashlimit", init_net.proc_net);
7b21e09d 1043#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
457c4cbc 1044 remove_proc_entry("ip6t_hashlimit", init_net.proc_net);
7b21e09d 1045#endif
39b46fc6 1046 kmem_cache_destroy(hashlimit_cachep);
d3c5ee6d 1047 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
1da177e4
LT
1048}
1049
d3c5ee6d
JE
1050module_init(hashlimit_mt_init);
1051module_exit(hashlimit_mt_exit);