]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_hashlimit.c
netfilter: core: remove erroneous warn_on
[mirror_ubuntu-bionic-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 5 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
f229f6ce 6 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
09e410de 7 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
1da177e4
LT
8 *
9 * Development of this code was funded by Astaro AG, http://www.astaro.com/
1da177e4 10 */
8bee4bad 11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4 12#include <linux/module.h>
1da177e4
LT
13#include <linux/spinlock.h>
14#include <linux/random.h>
15#include <linux/jhash.h>
16#include <linux/slab.h>
17#include <linux/vmalloc.h>
1da177e4
LT
18#include <linux/proc_fs.h>
19#include <linux/seq_file.h>
20#include <linux/list.h>
39b46fc6 21#include <linux/skbuff.h>
d7fe0f24 22#include <linux/mm.h>
39b46fc6
PM
23#include <linux/in.h>
24#include <linux/ip.h>
c0cd1156 25#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
39b46fc6 26#include <linux/ipv6.h>
193b23c5 27#include <net/ipv6.h>
7b21e09d
ED
28#endif
29
457c4cbc 30#include <net/net_namespace.h>
e89fc3f1 31#include <net/netns/generic.h>
1da177e4 32
39b46fc6 33#include <linux/netfilter/x_tables.h>
1da177e4 34#include <linux/netfilter_ipv4/ip_tables.h>
39b46fc6
PM
35#include <linux/netfilter_ipv6/ip6_tables.h>
36#include <linux/netfilter/xt_hashlimit.h>
14cc3e2b 37#include <linux/mutex.h>
1da177e4
LT
38
39MODULE_LICENSE("GPL");
40MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
408ffaa4 41MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
2ae15b64 42MODULE_DESCRIPTION("Xtables: per hash-bucket rate-limit match");
39b46fc6
PM
43MODULE_ALIAS("ipt_hashlimit");
44MODULE_ALIAS("ip6t_hashlimit");
1da177e4 45
e89fc3f1
AD
46struct hashlimit_net {
47 struct hlist_head htables;
48 struct proc_dir_entry *ipt_hashlimit;
49 struct proc_dir_entry *ip6t_hashlimit;
50};
51
c7d03a00 52static unsigned int hashlimit_net_id;
e89fc3f1
AD
53static inline struct hashlimit_net *hashlimit_pernet(struct net *net)
54{
55 return net_generic(net, hashlimit_net_id);
56}
57
1da177e4 58/* need to declare this at the top */
bea74641 59static const struct file_operations dl_file_ops_v2;
0dc60a45 60static const struct file_operations dl_file_ops_v1;
11d5f157 61static const struct file_operations dl_file_ops;
1da177e4
LT
62
63/* hash table crap */
1da177e4 64struct dsthash_dst {
39b46fc6
PM
65 union {
66 struct {
67 __be32 src;
68 __be32 dst;
69 } ip;
c0cd1156 70#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
39b46fc6
PM
71 struct {
72 __be32 src[4];
73 __be32 dst[4];
74 } ip6;
7b21e09d 75#endif
09e410de 76 };
6a19d614
AV
77 __be16 src_port;
78 __be16 dst_port;
1da177e4
LT
79};
80
81struct dsthash_ent {
82 /* static / read-only parts in the beginning */
83 struct hlist_node node;
84 struct dsthash_dst dst;
85
86 /* modified structure members in the end */
02e4eb75 87 spinlock_t lock;
1da177e4
LT
88 unsigned long expires; /* precalculated expiry time */
89 struct {
90 unsigned long prev; /* last modification */
bea74641
VP
91 union {
92 struct {
93 u_int64_t credit;
94 u_int64_t credit_cap;
95 u_int64_t cost;
96 };
97 struct {
98 u_int32_t interval, prev_window;
99 u_int64_t current_rate;
100 u_int64_t rate;
101 int64_t burst;
102 };
103 };
1da177e4 104 } rateinfo;
02e4eb75 105 struct rcu_head rcu;
1da177e4
LT
106};
107
39b46fc6 108struct xt_hashlimit_htable {
1da177e4 109 struct hlist_node node; /* global list of all htables */
2eff25c1 110 int use;
76108cea 111 u_int8_t family;
89bc7a0f 112 bool rnd_initialized;
1da177e4 113
bea74641 114 struct hashlimit_cfg3 cfg; /* config */
1da177e4
LT
115
116 /* used internally */
117 spinlock_t lock; /* lock for list_head */
118 u_int32_t rnd; /* random seed for hash */
39b46fc6 119 unsigned int count; /* number entries in table */
7bd8490e 120 struct delayed_work gc_work;
1da177e4
LT
121
122 /* seq_file stuff */
123 struct proc_dir_entry *pde;
14b872f0 124 const char *name;
e89fc3f1 125 struct net *net;
1da177e4
LT
126
127 struct hlist_head hash[0]; /* hashtable itself */
128};
129
11d5f157 130static int
bea74641 131cfg_copy(struct hashlimit_cfg3 *to, const void *from, int revision)
11d5f157
VP
132{
133 if (revision == 1) {
bea74641 134 struct hashlimit_cfg1 *cfg = (struct hashlimit_cfg1 *)from;
11d5f157
VP
135
136 to->mode = cfg->mode;
137 to->avg = cfg->avg;
138 to->burst = cfg->burst;
139 to->size = cfg->size;
140 to->max = cfg->max;
141 to->gc_interval = cfg->gc_interval;
142 to->expire = cfg->expire;
143 to->srcmask = cfg->srcmask;
144 to->dstmask = cfg->dstmask;
145 } else if (revision == 2) {
bea74641
VP
146 struct hashlimit_cfg2 *cfg = (struct hashlimit_cfg2 *)from;
147
148 to->mode = cfg->mode;
149 to->avg = cfg->avg;
150 to->burst = cfg->burst;
151 to->size = cfg->size;
152 to->max = cfg->max;
153 to->gc_interval = cfg->gc_interval;
154 to->expire = cfg->expire;
155 to->srcmask = cfg->srcmask;
156 to->dstmask = cfg->dstmask;
157 } else if (revision == 3) {
158 memcpy(to, from, sizeof(struct hashlimit_cfg3));
11d5f157
VP
159 } else {
160 return -EINVAL;
161 }
162
163 return 0;
164}
165
2eff25c1 166static DEFINE_MUTEX(hashlimit_mutex); /* protects htables list */
e18b890b 167static struct kmem_cache *hashlimit_cachep __read_mostly;
1da177e4 168
1d93a9cb 169static inline bool dst_cmp(const struct dsthash_ent *ent,
a47362a2 170 const struct dsthash_dst *b)
1da177e4 171{
39b46fc6 172 return !memcmp(&ent->dst, b, sizeof(ent->dst));
1da177e4
LT
173}
174
39b46fc6
PM
175static u_int32_t
176hash_dst(const struct xt_hashlimit_htable *ht, const struct dsthash_dst *dst)
1da177e4 177{
e2f82ac3
ED
178 u_int32_t hash = jhash2((const u32 *)dst,
179 sizeof(*dst)/sizeof(u32),
180 ht->rnd);
181 /*
182 * Instead of returning hash % ht->cfg.size (implying a divide)
183 * we return the high 32 bits of the (hash * ht->cfg.size) that will
184 * give results between [0 and cfg.size-1] and same hash distribution,
185 * but using a multiply, less expensive than a divide
186 */
8fc54f68 187 return reciprocal_scale(hash, ht->cfg.size);
1da177e4
LT
188}
189
39b46fc6 190static struct dsthash_ent *
a47362a2
JE
191dsthash_find(const struct xt_hashlimit_htable *ht,
192 const struct dsthash_dst *dst)
1da177e4
LT
193{
194 struct dsthash_ent *ent;
1da177e4
LT
195 u_int32_t hash = hash_dst(ht, dst);
196
39b46fc6 197 if (!hlist_empty(&ht->hash[hash])) {
b67bfe0d 198 hlist_for_each_entry_rcu(ent, &ht->hash[hash], node)
02e4eb75
ED
199 if (dst_cmp(ent, dst)) {
200 spin_lock(&ent->lock);
1da177e4 201 return ent;
02e4eb75 202 }
39b46fc6 203 }
1da177e4
LT
204 return NULL;
205}
206
207/* allocate dsthash_ent, initialize dst, put in htable and lock it */
208static struct dsthash_ent *
a47362a2 209dsthash_alloc_init(struct xt_hashlimit_htable *ht,
09181842 210 const struct dsthash_dst *dst, bool *race)
1da177e4
LT
211{
212 struct dsthash_ent *ent;
213
02e4eb75 214 spin_lock(&ht->lock);
09181842
PNA
215
216 /* Two or more packets may race to create the same entry in the
217 * hashtable, double check if this packet lost race.
218 */
219 ent = dsthash_find(ht, dst);
220 if (ent != NULL) {
221 spin_unlock(&ht->lock);
222 *race = true;
223 return ent;
224 }
225
1da177e4
LT
226 /* initialize hash with random val at the time we allocate
227 * the first hashtable entry */
02e4eb75 228 if (unlikely(!ht->rnd_initialized)) {
af07d241 229 get_random_bytes(&ht->rnd, sizeof(ht->rnd));
89bc7a0f 230 ht->rnd_initialized = true;
bf0857ea 231 }
1da177e4 232
39b46fc6 233 if (ht->cfg.max && ht->count >= ht->cfg.max) {
1da177e4 234 /* FIXME: do something. question is what.. */
e87cc472 235 net_err_ratelimited("max count of %u reached\n", ht->cfg.max);
02e4eb75
ED
236 ent = NULL;
237 } else
238 ent = kmem_cache_alloc(hashlimit_cachep, GFP_ATOMIC);
0a9ee813 239 if (ent) {
02e4eb75
ED
240 memcpy(&ent->dst, dst, sizeof(ent->dst));
241 spin_lock_init(&ent->lock);
1da177e4 242
02e4eb75
ED
243 spin_lock(&ent->lock);
244 hlist_add_head_rcu(&ent->node, &ht->hash[hash_dst(ht, dst)]);
245 ht->count++;
246 }
247 spin_unlock(&ht->lock);
1da177e4
LT
248 return ent;
249}
250
02e4eb75
ED
251static void dsthash_free_rcu(struct rcu_head *head)
252{
253 struct dsthash_ent *ent = container_of(head, struct dsthash_ent, rcu);
254
255 kmem_cache_free(hashlimit_cachep, ent);
256}
257
39b46fc6
PM
258static inline void
259dsthash_free(struct xt_hashlimit_htable *ht, struct dsthash_ent *ent)
1da177e4 260{
02e4eb75
ED
261 hlist_del_rcu(&ent->node);
262 call_rcu_bh(&ent->rcu, dsthash_free_rcu);
39b46fc6 263 ht->count--;
1da177e4 264}
7bd8490e 265static void htable_gc(struct work_struct *work);
1da177e4 266
bea74641 267static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg,
11d5f157
VP
268 const char *name, u_int8_t family,
269 struct xt_hashlimit_htable **out_hinfo,
270 int revision)
09e410de 271{
e89fc3f1 272 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
09e410de 273 struct xt_hashlimit_htable *hinfo;
bea74641 274 const struct file_operations *fops;
11d5f157
VP
275 unsigned int size, i;
276 int ret;
09e410de 277
11d5f157
VP
278 if (cfg->size) {
279 size = cfg->size;
09e410de 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);
85bc3f38 291 if (hinfo == NULL)
4a5a5c73 292 return -ENOMEM;
11d5f157 293 *out_hinfo = hinfo;
09e410de
JE
294
295 /* copy match config into hashtable config */
bea74641 296 ret = cfg_copy(&hinfo->cfg, (void *)cfg, 3);
11d5f157
VP
297
298 if (ret)
299 return ret;
300
09e410de
JE
301 hinfo->cfg.size = size;
302 if (hinfo->cfg.max == 0)
303 hinfo->cfg.max = 8 * hinfo->cfg.size;
304 else if (hinfo->cfg.max < hinfo->cfg.size)
305 hinfo->cfg.max = hinfo->cfg.size;
306
307 for (i = 0; i < hinfo->cfg.size; i++)
308 INIT_HLIST_HEAD(&hinfo->hash[i]);
309
2eff25c1 310 hinfo->use = 1;
09e410de
JE
311 hinfo->count = 0;
312 hinfo->family = family;
89bc7a0f 313 hinfo->rnd_initialized = false;
11d5f157 314 hinfo->name = kstrdup(name, GFP_KERNEL);
14b872f0
AV
315 if (!hinfo->name) {
316 vfree(hinfo);
317 return -ENOMEM;
318 }
09e410de
JE
319 spin_lock_init(&hinfo->lock);
320
bea74641
VP
321 switch (revision) {
322 case 1:
323 fops = &dl_file_ops_v1;
324 break;
325 case 2:
326 fops = &dl_file_ops_v2;
327 break;
328 default:
329 fops = &dl_file_ops;
330 }
331
11d5f157 332 hinfo->pde = proc_create_data(name, 0,
ee999d8b 333 (family == NFPROTO_IPV4) ?
e89fc3f1 334 hashlimit_net->ipt_hashlimit : hashlimit_net->ip6t_hashlimit,
bea74641 335 fops, hinfo);
09e410de 336 if (hinfo->pde == NULL) {
14b872f0 337 kfree(hinfo->name);
09e410de 338 vfree(hinfo);
4a5a5c73 339 return -ENOMEM;
09e410de 340 }
e89fc3f1 341 hinfo->net = net;
09e410de 342
7bd8490e
ED
343 INIT_DEFERRABLE_WORK(&hinfo->gc_work, htable_gc);
344 queue_delayed_work(system_power_efficient_wq, &hinfo->gc_work,
345 msecs_to_jiffies(hinfo->cfg.gc_interval));
09e410de 346
e89fc3f1 347 hlist_add_head(&hinfo->node, &hashlimit_net->htables);
09e410de
JE
348
349 return 0;
350}
351
a47362a2
JE
352static bool select_all(const struct xt_hashlimit_htable *ht,
353 const struct dsthash_ent *he)
1da177e4
LT
354{
355 return 1;
356}
357
a47362a2
JE
358static bool select_gc(const struct xt_hashlimit_htable *ht,
359 const struct dsthash_ent *he)
1da177e4 360{
cbebc51f 361 return time_after_eq(jiffies, he->expires);
1da177e4
LT
362}
363
39b46fc6 364static void htable_selective_cleanup(struct xt_hashlimit_htable *ht,
a47362a2
JE
365 bool (*select)(const struct xt_hashlimit_htable *ht,
366 const struct dsthash_ent *he))
1da177e4 367{
39b46fc6 368 unsigned int i;
1da177e4 369
1da177e4
LT
370 for (i = 0; i < ht->cfg.size; i++) {
371 struct dsthash_ent *dh;
b67bfe0d 372 struct hlist_node *n;
7bd8490e
ED
373
374 spin_lock_bh(&ht->lock);
b67bfe0d 375 hlist_for_each_entry_safe(dh, n, &ht->hash[i], node) {
1da177e4 376 if ((*select)(ht, dh))
39b46fc6 377 dsthash_free(ht, dh);
1da177e4 378 }
7bd8490e
ED
379 spin_unlock_bh(&ht->lock);
380 cond_resched();
1da177e4 381 }
1da177e4
LT
382}
383
7bd8490e 384static void htable_gc(struct work_struct *work)
1da177e4 385{
7bd8490e
ED
386 struct xt_hashlimit_htable *ht;
387
388 ht = container_of(work, struct xt_hashlimit_htable, gc_work.work);
1da177e4
LT
389
390 htable_selective_cleanup(ht, select_gc);
391
7bd8490e
ED
392 queue_delayed_work(system_power_efficient_wq,
393 &ht->gc_work, msecs_to_jiffies(ht->cfg.gc_interval));
1da177e4
LT
394}
395
b4ef4ce0 396static void htable_remove_proc_entry(struct xt_hashlimit_htable *hinfo)
1da177e4 397{
e89fc3f1
AD
398 struct hashlimit_net *hashlimit_net = hashlimit_pernet(hinfo->net);
399 struct proc_dir_entry *parent;
400
e89fc3f1
AD
401 if (hinfo->family == NFPROTO_IPV4)
402 parent = hashlimit_net->ipt_hashlimit;
403 else
404 parent = hashlimit_net->ip6t_hashlimit;
32263dd1 405
b4ef4ce0 406 if (parent != NULL)
14b872f0 407 remove_proc_entry(hinfo->name, parent);
b4ef4ce0 408}
32263dd1 409
b4ef4ce0
SP
410static void htable_destroy(struct xt_hashlimit_htable *hinfo)
411{
7bd8490e 412 cancel_delayed_work_sync(&hinfo->gc_work);
b4ef4ce0 413 htable_remove_proc_entry(hinfo);
1da177e4 414 htable_selective_cleanup(hinfo, select_all);
14b872f0 415 kfree(hinfo->name);
1da177e4
LT
416 vfree(hinfo);
417}
418
e89fc3f1
AD
419static struct xt_hashlimit_htable *htable_find_get(struct net *net,
420 const char *name,
76108cea 421 u_int8_t family)
1da177e4 422{
e89fc3f1 423 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
39b46fc6 424 struct xt_hashlimit_htable *hinfo;
1da177e4 425
b67bfe0d 426 hlist_for_each_entry(hinfo, &hashlimit_net->htables, node) {
14b872f0 427 if (!strcmp(name, hinfo->name) &&
39b46fc6 428 hinfo->family == family) {
2eff25c1 429 hinfo->use++;
1da177e4
LT
430 return hinfo;
431 }
432 }
1da177e4
LT
433 return NULL;
434}
435
39b46fc6 436static void htable_put(struct xt_hashlimit_htable *hinfo)
1da177e4 437{
2eff25c1
PM
438 mutex_lock(&hashlimit_mutex);
439 if (--hinfo->use == 0) {
1da177e4 440 hlist_del(&hinfo->node);
1da177e4
LT
441 htable_destroy(hinfo);
442 }
2eff25c1 443 mutex_unlock(&hashlimit_mutex);
1da177e4
LT
444}
445
1da177e4
LT
446/* The algorithm used is the Simple Token Bucket Filter (TBF)
447 * see net/sched/sch_tbf.c in the linux source tree
448 */
449
450/* Rusty: This is my (non-mathematically-inclined) understanding of
451 this algorithm. The `average rate' in jiffies becomes your initial
452 amount of credit `credit' and the most credit you can ever have
453 `credit_cap'. The `peak rate' becomes the cost of passing the
454 test, `cost'.
455
456 `prev' tracks the last packet hit: you gain one credit per jiffy.
457 If you get credit balance more than this, the extra credit is
458 discarded. Every time the match passes, you lose `cost' credits;
459 if you don't have that many, the test fails.
460
461 See Alexey's formal explanation in net/sched/sch_tbf.c.
462
463 To get the maximum range, we multiply by this factor (ie. you get N
464 credits per jiffy). We want to allow a rate as low as 1 per day
465 (slowest userspace tool allows), which means
466 CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
467*/
0dc60a45 468#define MAX_CPJ_v1 (0xFFFFFFFF / (HZ*60*60*24))
1b203c13 469#define MAX_CPJ (0xFFFFFFFFFFFFFFFFULL / (HZ*60*60*24))
1da177e4
LT
470
471/* Repeated shift and or gives us all 1s, final shift and add 1 gives
472 * us the power of 2 below the theoretical max, so GCC simply does a
473 * shift. */
474#define _POW2_BELOW2(x) ((x)|((x)>>1))
475#define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
476#define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
477#define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
478#define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
11d5f157 479#define _POW2_BELOW64(x) (_POW2_BELOW32(x)|_POW2_BELOW32((x)>>32))
1da177e4 480#define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
11d5f157 481#define POW2_BELOW64(x) ((_POW2_BELOW64(x)>>1) + 1)
1da177e4 482
11d5f157 483#define CREDITS_PER_JIFFY POW2_BELOW64(MAX_CPJ)
0dc60a45 484#define CREDITS_PER_JIFFY_v1 POW2_BELOW32(MAX_CPJ_v1)
1da177e4 485
0197dee7
FW
486/* in byte mode, the lowest possible rate is one packet/second.
487 * credit_cap is used as a counter that tells us how many times we can
488 * refill the "credits available" counter when it becomes empty.
489 */
490#define MAX_CPJ_BYTES (0xFFFFFFFF / HZ)
491#define CREDITS_PER_JIFFY_BYTES POW2_BELOW32(MAX_CPJ_BYTES)
492
493static u32 xt_hashlimit_len_to_chunks(u32 len)
494{
495 return (len >> XT_HASHLIMIT_BYTE_SHIFT) + 1;
496}
497
1da177e4 498/* Precision saver. */
11d5f157 499static u64 user2credits(u64 user, int revision)
1da177e4 500{
ad5b5576
AB
501 u64 scale = (revision == 1) ?
502 XT_HASHLIMIT_SCALE : XT_HASHLIMIT_SCALE_v2;
503 u64 cpj = (revision == 1) ?
504 CREDITS_PER_JIFFY_v1 : CREDITS_PER_JIFFY;
1da177e4 505
ad5b5576
AB
506 /* Avoid overflow: divide the constant operands first */
507 if (scale >= HZ * cpj)
508 return div64_u64(user, div64_u64(scale, HZ * cpj));
509
510 return user * div64_u64(HZ * cpj, scale);
1da177e4
LT
511}
512
0197dee7 513static u32 user2credits_byte(u32 user)
1da177e4 514{
0197dee7
FW
515 u64 us = user;
516 us *= HZ * CREDITS_PER_JIFFY_BYTES;
517 return (u32) (us >> 32);
518}
519
bea74641
VP
520static u64 user2rate(u64 user)
521{
522 if (user != 0) {
523 return div64_u64(XT_HASHLIMIT_SCALE_v2, user);
524 } else {
525 pr_warn("invalid rate from userspace: %llu\n", user);
526 return 0;
527 }
528}
529
530static u64 user2rate_bytes(u64 user)
531{
532 u64 r;
533
534 r = user ? 0xFFFFFFFFULL / user : 0xFFFFFFFFULL;
535 r = (r - 1) << 4;
536 return r;
537}
538
11d5f157
VP
539static void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now,
540 u32 mode, int revision)
0197dee7
FW
541{
542 unsigned long delta = now - dh->rateinfo.prev;
11d5f157 543 u64 cap, cpj;
0197dee7
FW
544
545 if (delta == 0)
546 return;
547
bea74641
VP
548 if (revision >= 3 && mode & XT_HASHLIMIT_RATE_MATCH) {
549 u64 interval = dh->rateinfo.interval * HZ;
550
551 if (delta < interval)
552 return;
553
554 dh->rateinfo.prev = now;
555 dh->rateinfo.prev_window =
556 ((dh->rateinfo.current_rate * interval) >
557 (delta * dh->rateinfo.rate));
558 dh->rateinfo.current_rate = 0;
559
560 return;
561 }
562
39b46fc6 563 dh->rateinfo.prev = now;
0197dee7
FW
564
565 if (mode & XT_HASHLIMIT_BYTES) {
11d5f157 566 u64 tmp = dh->rateinfo.credit;
0197dee7
FW
567 dh->rateinfo.credit += CREDITS_PER_JIFFY_BYTES * delta;
568 cap = CREDITS_PER_JIFFY_BYTES * HZ;
569 if (tmp >= dh->rateinfo.credit) {/* overflow */
570 dh->rateinfo.credit = cap;
571 return;
572 }
573 } else {
11d5f157
VP
574 cpj = (revision == 1) ?
575 CREDITS_PER_JIFFY_v1 : CREDITS_PER_JIFFY;
576 dh->rateinfo.credit += delta * cpj;
0197dee7
FW
577 cap = dh->rateinfo.credit_cap;
578 }
579 if (dh->rateinfo.credit > cap)
580 dh->rateinfo.credit = cap;
39b46fc6
PM
581}
582
817e076f 583static void rateinfo_init(struct dsthash_ent *dh,
11d5f157 584 struct xt_hashlimit_htable *hinfo, int revision)
817e076f
FW
585{
586 dh->rateinfo.prev = jiffies;
bea74641
VP
587 if (revision >= 3 && hinfo->cfg.mode & XT_HASHLIMIT_RATE_MATCH) {
588 dh->rateinfo.prev_window = 0;
589 dh->rateinfo.current_rate = 0;
590 if (hinfo->cfg.mode & XT_HASHLIMIT_BYTES) {
591 dh->rateinfo.rate = user2rate_bytes(hinfo->cfg.avg);
592 if (hinfo->cfg.burst)
593 dh->rateinfo.burst =
594 hinfo->cfg.burst * dh->rateinfo.rate;
595 else
596 dh->rateinfo.burst = dh->rateinfo.rate;
597 } else {
598 dh->rateinfo.rate = user2rate(hinfo->cfg.avg);
599 dh->rateinfo.burst =
600 hinfo->cfg.burst + dh->rateinfo.rate;
601 }
602 dh->rateinfo.interval = hinfo->cfg.interval;
603 } else if (hinfo->cfg.mode & XT_HASHLIMIT_BYTES) {
0197dee7
FW
604 dh->rateinfo.credit = CREDITS_PER_JIFFY_BYTES * HZ;
605 dh->rateinfo.cost = user2credits_byte(hinfo->cfg.avg);
606 dh->rateinfo.credit_cap = hinfo->cfg.burst;
607 } else {
608 dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
11d5f157
VP
609 hinfo->cfg.burst, revision);
610 dh->rateinfo.cost = user2credits(hinfo->cfg.avg, revision);
0197dee7
FW
611 dh->rateinfo.credit_cap = dh->rateinfo.credit;
612 }
817e076f
FW
613}
614
09e410de
JE
615static inline __be32 maskl(__be32 a, unsigned int l)
616{
1b9b70ea 617 return l ? htonl(ntohl(a) & ~0 << (32 - l)) : 0;
09e410de
JE
618}
619
c0cd1156 620#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
09e410de
JE
621static void hashlimit_ipv6_mask(__be32 *i, unsigned int p)
622{
623 switch (p) {
1b9b70ea 624 case 0 ... 31:
09e410de
JE
625 i[0] = maskl(i[0], p);
626 i[1] = i[2] = i[3] = 0;
627 break;
1b9b70ea 628 case 32 ... 63:
09e410de
JE
629 i[1] = maskl(i[1], p - 32);
630 i[2] = i[3] = 0;
631 break;
1b9b70ea 632 case 64 ... 95:
09e410de
JE
633 i[2] = maskl(i[2], p - 64);
634 i[3] = 0;
8f599229 635 break;
1b9b70ea 636 case 96 ... 127:
09e410de
JE
637 i[3] = maskl(i[3], p - 96);
638 break;
639 case 128:
640 break;
641 }
642}
3ed5df44 643#endif
09e410de 644
39b46fc6 645static int
a47362a2
JE
646hashlimit_init_dst(const struct xt_hashlimit_htable *hinfo,
647 struct dsthash_dst *dst,
39b46fc6
PM
648 const struct sk_buff *skb, unsigned int protoff)
649{
650 __be16 _ports[2], *ports;
193b23c5 651 u8 nexthdr;
aca071c1 652 int poff;
39b46fc6
PM
653
654 memset(dst, 0, sizeof(*dst));
655
656 switch (hinfo->family) {
ee999d8b 657 case NFPROTO_IPV4:
39b46fc6 658 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP)
09e410de
JE
659 dst->ip.dst = maskl(ip_hdr(skb)->daddr,
660 hinfo->cfg.dstmask);
39b46fc6 661 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP)
09e410de
JE
662 dst->ip.src = maskl(ip_hdr(skb)->saddr,
663 hinfo->cfg.srcmask);
39b46fc6
PM
664
665 if (!(hinfo->cfg.mode &
666 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
667 return 0;
eddc9ec5 668 nexthdr = ip_hdr(skb)->protocol;
39b46fc6 669 break;
c0cd1156 670#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
ee999d8b 671 case NFPROTO_IPV6:
412662d2
SR
672 {
673 __be16 frag_off;
674
09e410de
JE
675 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DIP) {
676 memcpy(&dst->ip6.dst, &ipv6_hdr(skb)->daddr,
677 sizeof(dst->ip6.dst));
678 hashlimit_ipv6_mask(dst->ip6.dst, hinfo->cfg.dstmask);
679 }
680 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SIP) {
681 memcpy(&dst->ip6.src, &ipv6_hdr(skb)->saddr,
682 sizeof(dst->ip6.src));
683 hashlimit_ipv6_mask(dst->ip6.src, hinfo->cfg.srcmask);
684 }
39b46fc6
PM
685
686 if (!(hinfo->cfg.mode &
687 (XT_HASHLIMIT_HASH_DPT | XT_HASHLIMIT_HASH_SPT)))
688 return 0;
193b23c5 689 nexthdr = ipv6_hdr(skb)->nexthdr;
75f2811c 690 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, &frag_off);
193b23c5 691 if ((int)protoff < 0)
39b46fc6
PM
692 return -1;
693 break;
412662d2 694 }
39b46fc6
PM
695#endif
696 default:
697 BUG();
698 return 0;
699 }
700
aca071c1
CG
701 poff = proto_ports_offset(nexthdr);
702 if (poff >= 0) {
703 ports = skb_header_pointer(skb, protoff + poff, sizeof(_ports),
39b46fc6 704 &_ports);
aca071c1 705 } else {
39b46fc6
PM
706 _ports[0] = _ports[1] = 0;
707 ports = _ports;
39b46fc6
PM
708 }
709 if (!ports)
710 return -1;
711 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_SPT)
712 dst->src_port = ports[0];
713 if (hinfo->cfg.mode & XT_HASHLIMIT_HASH_DPT)
714 dst->dst_port = ports[1];
715 return 0;
1da177e4
LT
716}
717
0197dee7
FW
718static u32 hashlimit_byte_cost(unsigned int len, struct dsthash_ent *dh)
719{
720 u64 tmp = xt_hashlimit_len_to_chunks(len);
721 tmp = tmp * dh->rateinfo.cost;
722
723 if (unlikely(tmp > CREDITS_PER_JIFFY_BYTES * HZ))
724 tmp = CREDITS_PER_JIFFY_BYTES * HZ;
725
726 if (dh->rateinfo.credit < tmp && dh->rateinfo.credit_cap) {
727 dh->rateinfo.credit_cap--;
728 dh->rateinfo.credit = CREDITS_PER_JIFFY_BYTES * HZ;
729 }
730 return (u32) tmp;
731}
732
ccb79bdc 733static bool
11d5f157
VP
734hashlimit_mt_common(const struct sk_buff *skb, struct xt_action_param *par,
735 struct xt_hashlimit_htable *hinfo,
bea74641 736 const struct hashlimit_cfg3 *cfg, int revision)
09e410de 737{
09e410de
JE
738 unsigned long now = jiffies;
739 struct dsthash_ent *dh;
740 struct dsthash_dst dst;
09181842 741 bool race = false;
11d5f157 742 u64 cost;
09e410de 743
f7108a20 744 if (hashlimit_init_dst(hinfo, &dst, skb, par->thoff) < 0)
09e410de
JE
745 goto hotdrop;
746
0b35f603 747 local_bh_disable();
09e410de
JE
748 dh = dsthash_find(hinfo, &dst);
749 if (dh == NULL) {
09181842 750 dh = dsthash_alloc_init(hinfo, &dst, &race);
09e410de 751 if (dh == NULL) {
0b35f603 752 local_bh_enable();
09e410de 753 goto hotdrop;
09181842
PNA
754 } else if (race) {
755 /* Already got an entry, update expiration timeout */
756 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
11d5f157 757 rateinfo_recalc(dh, now, hinfo->cfg.mode, revision);
09181842
PNA
758 } else {
759 dh->expires = jiffies + msecs_to_jiffies(hinfo->cfg.expire);
11d5f157 760 rateinfo_init(dh, hinfo, revision);
09e410de 761 }
09e410de
JE
762 } else {
763 /* update expiration timeout */
764 dh->expires = now + msecs_to_jiffies(hinfo->cfg.expire);
11d5f157 765 rateinfo_recalc(dh, now, hinfo->cfg.mode, revision);
09e410de
JE
766 }
767
bea74641
VP
768 if (cfg->mode & XT_HASHLIMIT_RATE_MATCH) {
769 cost = (cfg->mode & XT_HASHLIMIT_BYTES) ? skb->len : 1;
770 dh->rateinfo.current_rate += cost;
771
772 if (!dh->rateinfo.prev_window &&
773 (dh->rateinfo.current_rate <= dh->rateinfo.burst)) {
774 spin_unlock(&dh->lock);
775 rcu_read_unlock_bh();
776 return !(cfg->mode & XT_HASHLIMIT_INVERT);
777 } else {
778 goto overlimit;
779 }
780 }
781
11d5f157 782 if (cfg->mode & XT_HASHLIMIT_BYTES)
0197dee7
FW
783 cost = hashlimit_byte_cost(skb->len, dh);
784 else
785 cost = dh->rateinfo.cost;
786
787 if (dh->rateinfo.credit >= cost) {
09e410de 788 /* below the limit */
0197dee7 789 dh->rateinfo.credit -= cost;
02e4eb75 790 spin_unlock(&dh->lock);
0b35f603 791 local_bh_enable();
11d5f157 792 return !(cfg->mode & XT_HASHLIMIT_INVERT);
09e410de
JE
793 }
794
bea74641 795overlimit:
02e4eb75 796 spin_unlock(&dh->lock);
0b35f603 797 local_bh_enable();
09e410de 798 /* default match is underlimit - so over the limit, we need to invert */
11d5f157 799 return cfg->mode & XT_HASHLIMIT_INVERT;
09e410de
JE
800
801 hotdrop:
b4ba2611 802 par->hotdrop = true;
09e410de
JE
803 return false;
804}
805
11d5f157
VP
806static bool
807hashlimit_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
808{
809 const struct xt_hashlimit_mtinfo1 *info = par->matchinfo;
810 struct xt_hashlimit_htable *hinfo = info->hinfo;
bea74641 811 struct hashlimit_cfg3 cfg = {};
11d5f157
VP
812 int ret;
813
814 ret = cfg_copy(&cfg, (void *)&info->cfg, 1);
815
816 if (ret)
817 return ret;
818
819 return hashlimit_mt_common(skb, par, hinfo, &cfg, 1);
820}
821
822static bool
bea74641 823hashlimit_mt_v2(const struct sk_buff *skb, struct xt_action_param *par)
11d5f157
VP
824{
825 const struct xt_hashlimit_mtinfo2 *info = par->matchinfo;
826 struct xt_hashlimit_htable *hinfo = info->hinfo;
bea74641
VP
827 struct hashlimit_cfg3 cfg = {};
828 int ret;
829
830 ret = cfg_copy(&cfg, (void *)&info->cfg, 2);
831
832 if (ret)
833 return ret;
834
835 return hashlimit_mt_common(skb, par, hinfo, &cfg, 2);
836}
837
838static bool
839hashlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
840{
841 const struct xt_hashlimit_mtinfo3 *info = par->matchinfo;
842 struct xt_hashlimit_htable *hinfo = info->hinfo;
11d5f157 843
bea74641 844 return hashlimit_mt_common(skb, par, hinfo, &info->cfg, 3);
11d5f157
VP
845}
846
847static int hashlimit_mt_check_common(const struct xt_mtchk_param *par,
848 struct xt_hashlimit_htable **hinfo,
bea74641 849 struct hashlimit_cfg3 *cfg,
11d5f157 850 const char *name, int revision)
09e410de 851{
e89fc3f1 852 struct net *net = par->net;
4a5a5c73 853 int ret;
09e410de 854
11d5f157 855 if (cfg->gc_interval == 0 || cfg->expire == 0)
bd414ee6 856 return -EINVAL;
aa5fa318 857 if (par->family == NFPROTO_IPV4) {
11d5f157 858 if (cfg->srcmask > 32 || cfg->dstmask > 32)
bd414ee6 859 return -EINVAL;
09e410de 860 } else {
11d5f157 861 if (cfg->srcmask > 128 || cfg->dstmask > 128)
bd414ee6 862 return -EINVAL;
09e410de
JE
863 }
864
11d5f157 865 if (cfg->mode & ~XT_HASHLIMIT_ALL) {
0197dee7 866 pr_info("Unknown mode mask %X, kernel too old?\n",
11d5f157 867 cfg->mode);
0197dee7
FW
868 return -EINVAL;
869 }
870
871 /* Check for overflow. */
bea74641
VP
872 if (revision >= 3 && cfg->mode & XT_HASHLIMIT_RATE_MATCH) {
873 if (cfg->avg == 0) {
874 pr_info("hashlimit invalid rate\n");
875 return -ERANGE;
876 }
877
878 if (cfg->interval == 0) {
879 pr_info("hashlimit invalid interval\n");
880 return -EINVAL;
881 }
882 } else if (cfg->mode & XT_HASHLIMIT_BYTES) {
11d5f157
VP
883 if (user2credits_byte(cfg->avg) == 0) {
884 pr_info("overflow, rate too high: %llu\n", cfg->avg);
0197dee7
FW
885 return -EINVAL;
886 }
11d5f157
VP
887 } else if (cfg->burst == 0 ||
888 user2credits(cfg->avg * cfg->burst, revision) <
889 user2credits(cfg->avg, revision)) {
890 pr_info("overflow, try lower: %llu/%llu\n",
891 cfg->avg, cfg->burst);
0197dee7
FW
892 return -ERANGE;
893 }
894
2eff25c1 895 mutex_lock(&hashlimit_mutex);
11d5f157
VP
896 *hinfo = htable_find_get(net, name, par->family);
897 if (*hinfo == NULL) {
898 ret = htable_create(net, cfg, name, par->family,
899 hinfo, revision);
4a5a5c73
JE
900 if (ret < 0) {
901 mutex_unlock(&hashlimit_mutex);
902 return ret;
903 }
09e410de 904 }
2eff25c1 905 mutex_unlock(&hashlimit_mutex);
11d5f157 906
bd414ee6 907 return 0;
09e410de
JE
908}
909
11d5f157
VP
910static int hashlimit_mt_check_v1(const struct xt_mtchk_param *par)
911{
912 struct xt_hashlimit_mtinfo1 *info = par->matchinfo;
bea74641 913 struct hashlimit_cfg3 cfg = {};
11d5f157
VP
914 int ret;
915
916 if (info->name[sizeof(info->name) - 1] != '\0')
917 return -EINVAL;
918
919 ret = cfg_copy(&cfg, (void *)&info->cfg, 1);
920
921 if (ret)
922 return ret;
923
924 return hashlimit_mt_check_common(par, &info->hinfo,
925 &cfg, info->name, 1);
926}
927
bea74641 928static int hashlimit_mt_check_v2(const struct xt_mtchk_param *par)
11d5f157
VP
929{
930 struct xt_hashlimit_mtinfo2 *info = par->matchinfo;
bea74641
VP
931 struct hashlimit_cfg3 cfg = {};
932 int ret;
933
934 if (info->name[sizeof(info->name) - 1] != '\0')
935 return -EINVAL;
936
937 ret = cfg_copy(&cfg, (void *)&info->cfg, 2);
938
939 if (ret)
940 return ret;
941
942 return hashlimit_mt_check_common(par, &info->hinfo,
943 &cfg, info->name, 2);
944}
945
946static int hashlimit_mt_check(const struct xt_mtchk_param *par)
947{
948 struct xt_hashlimit_mtinfo3 *info = par->matchinfo;
11d5f157
VP
949
950 if (info->name[sizeof(info->name) - 1] != '\0')
951 return -EINVAL;
952
953 return hashlimit_mt_check_common(par, &info->hinfo, &info->cfg,
bea74641
VP
954 info->name, 3);
955}
956
957static void hashlimit_mt_destroy_v2(const struct xt_mtdtor_param *par)
958{
959 const struct xt_hashlimit_mtinfo2 *info = par->matchinfo;
960
961 htable_put(info->hinfo);
11d5f157
VP
962}
963
0dc60a45 964static void hashlimit_mt_destroy_v1(const struct xt_mtdtor_param *par)
09e410de 965{
6be3d859 966 const struct xt_hashlimit_mtinfo1 *info = par->matchinfo;
09e410de
JE
967
968 htable_put(info->hinfo);
969}
970
11d5f157
VP
971static void hashlimit_mt_destroy(const struct xt_mtdtor_param *par)
972{
bea74641 973 const struct xt_hashlimit_mtinfo3 *info = par->matchinfo;
11d5f157
VP
974
975 htable_put(info->hinfo);
976}
977
d3c5ee6d 978static struct xt_match hashlimit_mt_reg[] __read_mostly = {
09e410de
JE
979 {
980 .name = "hashlimit",
981 .revision = 1,
ee999d8b 982 .family = NFPROTO_IPV4,
0dc60a45 983 .match = hashlimit_mt_v1,
09e410de 984 .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
ec231890 985 .usersize = offsetof(struct xt_hashlimit_mtinfo1, hinfo),
0dc60a45
VP
986 .checkentry = hashlimit_mt_check_v1,
987 .destroy = hashlimit_mt_destroy_v1,
09e410de
JE
988 .me = THIS_MODULE,
989 },
11d5f157
VP
990 {
991 .name = "hashlimit",
992 .revision = 2,
993 .family = NFPROTO_IPV4,
bea74641 994 .match = hashlimit_mt_v2,
11d5f157 995 .matchsize = sizeof(struct xt_hashlimit_mtinfo2),
ec231890 996 .usersize = offsetof(struct xt_hashlimit_mtinfo2, hinfo),
bea74641
VP
997 .checkentry = hashlimit_mt_check_v2,
998 .destroy = hashlimit_mt_destroy_v2,
999 .me = THIS_MODULE,
1000 },
1001 {
1002 .name = "hashlimit",
1003 .revision = 3,
1004 .family = NFPROTO_IPV4,
1005 .match = hashlimit_mt,
1006 .matchsize = sizeof(struct xt_hashlimit_mtinfo3),
1007 .usersize = offsetof(struct xt_hashlimit_mtinfo3, hinfo),
11d5f157
VP
1008 .checkentry = hashlimit_mt_check,
1009 .destroy = hashlimit_mt_destroy,
1010 .me = THIS_MODULE,
1011 },
c0cd1156 1012#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
09e410de
JE
1013 {
1014 .name = "hashlimit",
1015 .revision = 1,
ee999d8b 1016 .family = NFPROTO_IPV6,
0dc60a45 1017 .match = hashlimit_mt_v1,
09e410de 1018 .matchsize = sizeof(struct xt_hashlimit_mtinfo1),
ec231890 1019 .usersize = offsetof(struct xt_hashlimit_mtinfo1, hinfo),
0dc60a45
VP
1020 .checkentry = hashlimit_mt_check_v1,
1021 .destroy = hashlimit_mt_destroy_v1,
09e410de
JE
1022 .me = THIS_MODULE,
1023 },
11d5f157
VP
1024 {
1025 .name = "hashlimit",
1026 .revision = 2,
1027 .family = NFPROTO_IPV6,
bea74641 1028 .match = hashlimit_mt_v2,
11d5f157 1029 .matchsize = sizeof(struct xt_hashlimit_mtinfo2),
ec231890 1030 .usersize = offsetof(struct xt_hashlimit_mtinfo2, hinfo),
bea74641
VP
1031 .checkentry = hashlimit_mt_check_v2,
1032 .destroy = hashlimit_mt_destroy_v2,
1033 .me = THIS_MODULE,
1034 },
1035 {
1036 .name = "hashlimit",
1037 .revision = 3,
1038 .family = NFPROTO_IPV6,
1039 .match = hashlimit_mt,
1040 .matchsize = sizeof(struct xt_hashlimit_mtinfo3),
1041 .usersize = offsetof(struct xt_hashlimit_mtinfo3, hinfo),
11d5f157
VP
1042 .checkentry = hashlimit_mt_check,
1043 .destroy = hashlimit_mt_destroy,
1044 .me = THIS_MODULE,
1045 },
7b21e09d 1046#endif
1da177e4
LT
1047};
1048
1049/* PROC stuff */
1da177e4 1050static void *dl_seq_start(struct seq_file *s, loff_t *pos)
f4f6fb71 1051 __acquires(htable->lock)
1da177e4 1052{
a1004d8e 1053 struct xt_hashlimit_htable *htable = s->private;
1da177e4
LT
1054 unsigned int *bucket;
1055
1056 spin_lock_bh(&htable->lock);
1057 if (*pos >= htable->cfg.size)
1058 return NULL;
1059
1060 bucket = kmalloc(sizeof(unsigned int), GFP_ATOMIC);
1061 if (!bucket)
1062 return ERR_PTR(-ENOMEM);
1063
1064 *bucket = *pos;
1065 return bucket;
1066}
1067
1068static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
1069{
a1004d8e 1070 struct xt_hashlimit_htable *htable = s->private;
68ad546a 1071 unsigned int *bucket = v;
1da177e4
LT
1072
1073 *pos = ++(*bucket);
1074 if (*pos >= htable->cfg.size) {
1075 kfree(v);
1076 return NULL;
1077 }
1078 return bucket;
1079}
1080
1081static void dl_seq_stop(struct seq_file *s, void *v)
f4f6fb71 1082 __releases(htable->lock)
1da177e4 1083{
a1004d8e 1084 struct xt_hashlimit_htable *htable = s->private;
68ad546a 1085 unsigned int *bucket = v;
1da177e4 1086
55e0d7cf
ED
1087 if (!IS_ERR(bucket))
1088 kfree(bucket);
1da177e4
LT
1089 spin_unlock_bh(&htable->lock);
1090}
1091
11d5f157
VP
1092static void dl_seq_print(struct dsthash_ent *ent, u_int8_t family,
1093 struct seq_file *s)
1da177e4 1094{
39b46fc6 1095 switch (family) {
ee999d8b 1096 case NFPROTO_IPV4:
11d5f157 1097 seq_printf(s, "%ld %pI4:%u->%pI4:%u %llu %llu %llu\n",
e71456ae
SRRH
1098 (long)(ent->expires - jiffies)/HZ,
1099 &ent->dst.ip.src,
1100 ntohs(ent->dst.src_port),
1101 &ent->dst.ip.dst,
1102 ntohs(ent->dst.dst_port),
1103 ent->rateinfo.credit, ent->rateinfo.credit_cap,
1104 ent->rateinfo.cost);
02e4eb75 1105 break;
c0cd1156 1106#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
ee999d8b 1107 case NFPROTO_IPV6:
11d5f157 1108 seq_printf(s, "%ld %pI6:%u->%pI6:%u %llu %llu %llu\n",
e71456ae
SRRH
1109 (long)(ent->expires - jiffies)/HZ,
1110 &ent->dst.ip6.src,
1111 ntohs(ent->dst.src_port),
1112 &ent->dst.ip6.dst,
1113 ntohs(ent->dst.dst_port),
1114 ent->rateinfo.credit, ent->rateinfo.credit_cap,
1115 ent->rateinfo.cost);
02e4eb75 1116 break;
7b21e09d 1117#endif
39b46fc6
PM
1118 default:
1119 BUG();
39b46fc6 1120 }
11d5f157
VP
1121}
1122
bea74641
VP
1123static int dl_seq_real_show_v2(struct dsthash_ent *ent, u_int8_t family,
1124 struct seq_file *s)
1125{
1126 const struct xt_hashlimit_htable *ht = s->private;
1127
1128 spin_lock(&ent->lock);
1129 /* recalculate to show accurate numbers */
1130 rateinfo_recalc(ent, jiffies, ht->cfg.mode, 2);
1131
1132 dl_seq_print(ent, family, s);
1133
1134 spin_unlock(&ent->lock);
1135 return seq_has_overflowed(s);
1136}
1137
11d5f157
VP
1138static int dl_seq_real_show_v1(struct dsthash_ent *ent, u_int8_t family,
1139 struct seq_file *s)
1140{
1141 const struct xt_hashlimit_htable *ht = s->private;
1142
1143 spin_lock(&ent->lock);
1144 /* recalculate to show accurate numbers */
1145 rateinfo_recalc(ent, jiffies, ht->cfg.mode, 1);
1146
1147 dl_seq_print(ent, family, s);
1148
1149 spin_unlock(&ent->lock);
1150 return seq_has_overflowed(s);
1151}
1152
1153static int dl_seq_real_show(struct dsthash_ent *ent, u_int8_t family,
1154 struct seq_file *s)
1155{
1156 const struct xt_hashlimit_htable *ht = s->private;
1157
1158 spin_lock(&ent->lock);
1159 /* recalculate to show accurate numbers */
bea74641 1160 rateinfo_recalc(ent, jiffies, ht->cfg.mode, 3);
11d5f157
VP
1161
1162 dl_seq_print(ent, family, s);
1163
02e4eb75 1164 spin_unlock(&ent->lock);
e71456ae 1165 return seq_has_overflowed(s);
1da177e4
LT
1166}
1167
bea74641
VP
1168static int dl_seq_show_v2(struct seq_file *s, void *v)
1169{
1170 struct xt_hashlimit_htable *htable = s->private;
1171 unsigned int *bucket = (unsigned int *)v;
1172 struct dsthash_ent *ent;
1173
1174 if (!hlist_empty(&htable->hash[*bucket])) {
1175 hlist_for_each_entry(ent, &htable->hash[*bucket], node)
1176 if (dl_seq_real_show_v2(ent, htable->family, s))
1177 return -1;
1178 }
1179 return 0;
1180}
1181
0dc60a45 1182static int dl_seq_show_v1(struct seq_file *s, void *v)
1da177e4 1183{
a1004d8e 1184 struct xt_hashlimit_htable *htable = s->private;
68ad546a 1185 unsigned int *bucket = v;
1da177e4 1186 struct dsthash_ent *ent;
1da177e4 1187
39b46fc6 1188 if (!hlist_empty(&htable->hash[*bucket])) {
b67bfe0d 1189 hlist_for_each_entry(ent, &htable->hash[*bucket], node)
0dc60a45 1190 if (dl_seq_real_show_v1(ent, htable->family, s))
683a04ce 1191 return -1;
39b46fc6 1192 }
1da177e4
LT
1193 return 0;
1194}
1195
11d5f157
VP
1196static int dl_seq_show(struct seq_file *s, void *v)
1197{
1198 struct xt_hashlimit_htable *htable = s->private;
68ad546a 1199 unsigned int *bucket = v;
11d5f157
VP
1200 struct dsthash_ent *ent;
1201
1202 if (!hlist_empty(&htable->hash[*bucket])) {
1203 hlist_for_each_entry(ent, &htable->hash[*bucket], node)
1204 if (dl_seq_real_show(ent, htable->family, s))
1205 return -1;
1206 }
1207 return 0;
1208}
1209
0dc60a45 1210static const struct seq_operations dl_seq_ops_v1 = {
1da177e4
LT
1211 .start = dl_seq_start,
1212 .next = dl_seq_next,
1213 .stop = dl_seq_stop,
0dc60a45 1214 .show = dl_seq_show_v1
1da177e4
LT
1215};
1216
bea74641
VP
1217static const struct seq_operations dl_seq_ops_v2 = {
1218 .start = dl_seq_start,
1219 .next = dl_seq_next,
1220 .stop = dl_seq_stop,
1221 .show = dl_seq_show_v2
1222};
1223
11d5f157
VP
1224static const struct seq_operations dl_seq_ops = {
1225 .start = dl_seq_start,
1226 .next = dl_seq_next,
1227 .stop = dl_seq_stop,
1228 .show = dl_seq_show
1229};
1230
bea74641
VP
1231static int dl_proc_open_v2(struct inode *inode, struct file *file)
1232{
1233 int ret = seq_open(file, &dl_seq_ops_v2);
1234
1235 if (!ret) {
1236 struct seq_file *sf = file->private_data;
1237
1238 sf->private = PDE_DATA(inode);
1239 }
1240 return ret;
1241}
1242
0dc60a45 1243static int dl_proc_open_v1(struct inode *inode, struct file *file)
1da177e4 1244{
0dc60a45 1245 int ret = seq_open(file, &dl_seq_ops_v1);
1da177e4
LT
1246
1247 if (!ret) {
1248 struct seq_file *sf = file->private_data;
d9dda78b 1249 sf->private = PDE_DATA(inode);
1da177e4
LT
1250 }
1251 return ret;
1252}
1253
11d5f157
VP
1254static int dl_proc_open(struct inode *inode, struct file *file)
1255{
1256 int ret = seq_open(file, &dl_seq_ops);
1257
1258 if (!ret) {
1259 struct seq_file *sf = file->private_data;
1260
1261 sf->private = PDE_DATA(inode);
1262 }
1263 return ret;
1264}
1265
bea74641
VP
1266static const struct file_operations dl_file_ops_v2 = {
1267 .owner = THIS_MODULE,
1268 .open = dl_proc_open_v2,
1269 .read = seq_read,
1270 .llseek = seq_lseek,
1271 .release = seq_release
1272};
1273
0dc60a45 1274static const struct file_operations dl_file_ops_v1 = {
1da177e4 1275 .owner = THIS_MODULE,
0dc60a45 1276 .open = dl_proc_open_v1,
1da177e4
LT
1277 .read = seq_read,
1278 .llseek = seq_lseek,
1279 .release = seq_release
1280};
1281
11d5f157
VP
1282static const struct file_operations dl_file_ops = {
1283 .owner = THIS_MODULE,
1284 .open = dl_proc_open,
1285 .read = seq_read,
1286 .llseek = seq_lseek,
1287 .release = seq_release
1288};
1289
e89fc3f1
AD
1290static int __net_init hashlimit_proc_net_init(struct net *net)
1291{
1292 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
1293
1294 hashlimit_net->ipt_hashlimit = proc_mkdir("ipt_hashlimit", net->proc_net);
1295 if (!hashlimit_net->ipt_hashlimit)
1296 return -ENOMEM;
c0cd1156 1297#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
e89fc3f1
AD
1298 hashlimit_net->ip6t_hashlimit = proc_mkdir("ip6t_hashlimit", net->proc_net);
1299 if (!hashlimit_net->ip6t_hashlimit) {
ece31ffd 1300 remove_proc_entry("ipt_hashlimit", net->proc_net);
e89fc3f1
AD
1301 return -ENOMEM;
1302 }
1303#endif
1304 return 0;
1305}
1306
1307static void __net_exit hashlimit_proc_net_exit(struct net *net)
1308{
32263dd1 1309 struct xt_hashlimit_htable *hinfo;
32263dd1
VL
1310 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
1311
b4ef4ce0
SP
1312 /* hashlimit_net_exit() is called before hashlimit_mt_destroy().
1313 * Make sure that the parent ipt_hashlimit and ip6t_hashlimit proc
1314 * entries is empty before trying to remove it.
32263dd1
VL
1315 */
1316 mutex_lock(&hashlimit_mutex);
b67bfe0d 1317 hlist_for_each_entry(hinfo, &hashlimit_net->htables, node)
b4ef4ce0 1318 htable_remove_proc_entry(hinfo);
32263dd1
VL
1319 hashlimit_net->ipt_hashlimit = NULL;
1320 hashlimit_net->ip6t_hashlimit = NULL;
1321 mutex_unlock(&hashlimit_mutex);
1322
ece31ffd 1323 remove_proc_entry("ipt_hashlimit", net->proc_net);
c0cd1156 1324#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
ece31ffd 1325 remove_proc_entry("ip6t_hashlimit", net->proc_net);
e89fc3f1
AD
1326#endif
1327}
1328
1329static int __net_init hashlimit_net_init(struct net *net)
1330{
1331 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
1332
1333 INIT_HLIST_HEAD(&hashlimit_net->htables);
1334 return hashlimit_proc_net_init(net);
1335}
1336
1337static void __net_exit hashlimit_net_exit(struct net *net)
1338{
e89fc3f1
AD
1339 hashlimit_proc_net_exit(net);
1340}
1341
1342static struct pernet_operations hashlimit_net_ops = {
1343 .init = hashlimit_net_init,
1344 .exit = hashlimit_net_exit,
1345 .id = &hashlimit_net_id,
1346 .size = sizeof(struct hashlimit_net),
1347};
1348
d3c5ee6d 1349static int __init hashlimit_mt_init(void)
1da177e4 1350{
39b46fc6 1351 int err;
1da177e4 1352
e89fc3f1
AD
1353 err = register_pernet_subsys(&hashlimit_net_ops);
1354 if (err < 0)
1355 return err;
d3c5ee6d
JE
1356 err = xt_register_matches(hashlimit_mt_reg,
1357 ARRAY_SIZE(hashlimit_mt_reg));
39b46fc6
PM
1358 if (err < 0)
1359 goto err1;
1da177e4 1360
39b46fc6
PM
1361 err = -ENOMEM;
1362 hashlimit_cachep = kmem_cache_create("xt_hashlimit",
1363 sizeof(struct dsthash_ent), 0, 0,
20c2df83 1364 NULL);
1da177e4 1365 if (!hashlimit_cachep) {
b167a37c 1366 pr_warn("unable to create slab cache\n");
39b46fc6 1367 goto err2;
1da177e4 1368 }
e89fc3f1
AD
1369 return 0;
1370
39b46fc6 1371err2:
d3c5ee6d 1372 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
39b46fc6 1373err1:
e89fc3f1 1374 unregister_pernet_subsys(&hashlimit_net_ops);
39b46fc6 1375 return err;
1da177e4 1376
1da177e4
LT
1377}
1378
d3c5ee6d 1379static void __exit hashlimit_mt_exit(void)
1da177e4 1380{
d3c5ee6d 1381 xt_unregister_matches(hashlimit_mt_reg, ARRAY_SIZE(hashlimit_mt_reg));
e89fc3f1 1382 unregister_pernet_subsys(&hashlimit_net_ops);
02e4eb75
ED
1383
1384 rcu_barrier_bh();
1385 kmem_cache_destroy(hashlimit_cachep);
1da177e4
LT
1386}
1387
d3c5ee6d
JE
1388module_init(hashlimit_mt_init);
1389module_exit(hashlimit_mt_exit);