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