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