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