10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
13 #include <linux/random.h>
15 #include <linux/slab.h>
19 #include <linux/list.h>
24 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
25 #include <linux/ipv6.h>
32 #include <linux/netfilter/x_tables.h>
33 #include <linux/netfilter_ipv4/ip_tables.h>
34 #include <linux/netfilter_ipv6/ip6_tables.h>
35 #include <linux/netfilter/xt_hashlimit.h>
51 static int hashlimit_net_id;
67 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
128 sizeof(*dst)/
sizeof(
u32),
136 return ((
u64)hash * ht->
cfg.size) >> 32;
147 if (!hlist_empty(&ht->
hash[hash])) {
148 hlist_for_each_entry_rcu(ent, pos, &ht->
hash[hash],
node)
149 if (dst_cmp(ent, dst)) {
150 spin_lock(&ent->lock);
164 spin_lock(&ht->
lock);
182 spin_lock(&ent->
lock);
183 hlist_add_head_rcu(&ent->
node, &ht->
hash[hash_dst(ht, dst)]);
186 spin_unlock(&ht->
lock);
200 hlist_del_rcu(&ent->
node);
204 static void htable_gc(
unsigned long htlong);
214 if (minfo->
cfg.size) {
215 size = minfo->
cfg.size;
217 size = (totalram_pages <<
PAGE_SHIFT) / 16384 /
219 if (totalram_pages > 1024 * 1024 * 1024 /
PAGE_SIZE)
229 minfo->hinfo = hinfo;
234 if (hinfo->
cfg.max == 0)
235 hinfo->
cfg.max = 8 * hinfo->
cfg.size;
236 else if (hinfo->
cfg.max < hinfo->
cfg.size)
237 hinfo->
cfg.max = hinfo->
cfg.size;
239 for (i = 0; i < hinfo->
cfg.size; i++)
251 &dl_file_ops, hinfo);
262 hlist_add_head(&hinfo->
node, &hashlimit_net->
htables);
286 spin_lock_bh(&ht->
lock);
287 for (i = 0; i < ht->
cfg.size; i++) {
292 dsthash_free(ht, dh);
295 spin_unlock_bh(&ht->
lock);
299 static void htable_gc(
unsigned long htlong)
303 htable_selective_cleanup(ht, select_gc);
312 struct hashlimit_net *hashlimit_net = hashlimit_pernet(hinfo->
net);
322 htable_selective_cleanup(hinfo, select_all);
330 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
336 hinfo->
family == family) {
347 if (--hinfo->
use == 0) {
348 hlist_del(&hinfo->
node);
349 htable_destroy(hinfo);
376 #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
381 #define _POW2_BELOW2(x) ((x)|((x)>>1))
382 #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
383 #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
384 #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
385 #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
386 #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
388 #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
394 #define MAX_CPJ_BYTES (0xFFFFFFFF / HZ)
395 #define CREDITS_PER_JIFFY_BYTES POW2_BELOW32(MAX_CPJ_BYTES)
397 static u32 xt_hashlimit_len_to_chunks(
u32 len)
413 static u32 user2credits_byte(
u32 user)
417 return (
u32) (us >> 32);
433 cap = CREDITS_PER_JIFFY_BYTES *
HZ;
450 if (hinfo->
cfg.mode & XT_HASHLIMIT_BYTES) {
451 dh->
rateinfo.credit = CREDITS_PER_JIFFY_BYTES *
HZ;
452 dh->
rateinfo.cost = user2credits_byte(hinfo->
cfg.avg);
455 dh->
rateinfo.credit = user2credits(hinfo->
cfg.avg *
464 return l ?
htonl(
ntohl(a) & ~0 << (32 - l)) : 0;
467 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
468 static void hashlimit_ipv6_mask(
__be32 *i,
unsigned int p)
472 i[0] = maskl(i[0], p);
473 i[1] = i[2] = i[3] = 0;
476 i[1] = maskl(i[1], p - 32);
480 i[2] = maskl(i[2], p - 64);
484 i[3] = maskl(i[3], p - 96);
495 const struct sk_buff *
skb,
unsigned int protoff)
501 memset(dst, 0,
sizeof(*dst));
506 dst->
ip.dst = maskl(ip_hdr(skb)->
daddr,
509 dst->
ip.src = maskl(ip_hdr(skb)->
saddr,
512 if (!(hinfo->
cfg.mode &
515 nexthdr = ip_hdr(skb)->protocol;
517 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
524 sizeof(dst->ip6.
dst));
525 hashlimit_ipv6_mask(dst->ip6.
dst, hinfo->
cfg.dstmask);
529 sizeof(dst->ip6.
src));
530 hashlimit_ipv6_mask(dst->ip6.
src, hinfo->
cfg.srcmask);
533 if (!(hinfo->
cfg.mode &
536 nexthdr = ipv6_hdr(skb)->nexthdr;
538 if ((
int)protoff < 0)
548 poff = proto_ports_offset(nexthdr);
550 ports = skb_header_pointer(skb, protoff + poff,
sizeof(_ports),
553 _ports[0] = _ports[1] = 0;
565 static u32 hashlimit_byte_cost(
unsigned int len,
struct dsthash_ent *dh)
567 u64 tmp = xt_hashlimit_len_to_chunks(len);
570 if (
unlikely(tmp > CREDITS_PER_JIFFY_BYTES * HZ))
571 tmp = CREDITS_PER_JIFFY_BYTES *
HZ;
575 dh->
rateinfo.credit = CREDITS_PER_JIFFY_BYTES *
HZ;
590 if (hashlimit_init_dst(hinfo, &dst, skb, par->
thoff) < 0)
594 dh = dsthash_find(hinfo, &dst);
596 dh = dsthash_alloc_init(hinfo, &dst);
598 rcu_read_unlock_bh();
602 rateinfo_init(dh, hinfo);
606 rateinfo_recalc(dh, now, hinfo->
cfg.mode);
609 if (info->
cfg.mode & XT_HASHLIMIT_BYTES)
610 cost = hashlimit_byte_cost(skb->
len, dh);
617 spin_unlock(&dh->
lock);
618 rcu_read_unlock_bh();
622 spin_unlock(&dh->
lock);
623 rcu_read_unlock_bh();
634 struct net *net = par->
net;
638 if (info->
cfg.gc_interval == 0 || info->
cfg.expire == 0)
640 if (info->
name[
sizeof(info->
name)-1] !=
'\0')
643 if (info->
cfg.srcmask > 32 || info->
cfg.dstmask > 32)
646 if (info->
cfg.srcmask > 128 || info->
cfg.dstmask > 128)
651 pr_info(
"Unknown mode mask %X, kernel too old?\n",
657 if (info->
cfg.mode & XT_HASHLIMIT_BYTES) {
658 if (user2credits_byte(info->
cfg.avg) == 0) {
659 pr_info(
"overflow, rate too high: %u\n", info->
cfg.avg);
662 }
else if (info->
cfg.burst == 0 ||
663 user2credits(info->
cfg.avg * info->
cfg.burst) <
664 user2credits(info->
cfg.avg)) {
665 pr_info(
"overflow, try lower: %u/%u\n",
666 info->
cfg.avg, info->
cfg.burst);
671 info->hinfo = htable_find_get(net, info->
name, par->
family);
672 if (info->hinfo ==
NULL) {
673 ret = htable_create(net, info, par->
family);
687 htable_put(info->hinfo);
695 .match = hashlimit_mt,
697 .checkentry = hashlimit_mt_check,
698 .destroy = hashlimit_mt_destroy,
701 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
706 .match = hashlimit_mt,
708 .checkentry = hashlimit_mt_check,
709 .destroy = hashlimit_mt_destroy,
716 static void *dl_seq_start(
struct seq_file *
s, loff_t *pos)
720 unsigned int *bucket;
722 spin_lock_bh(&htable->
lock);
723 if (*pos >= htable->
cfg.size)
734 static void *dl_seq_next(
struct seq_file *
s,
void *
v, loff_t *pos)
737 unsigned int *bucket = (
unsigned int *)v;
740 if (*pos >= htable->
cfg.size) {
747 static void dl_seq_stop(
struct seq_file *s,
void *v)
751 unsigned int *bucket = (
unsigned int *)v;
755 spin_unlock_bh(&htable->
lock);
764 spin_lock(&ent->
lock);
766 rateinfo_recalc(ent, jiffies, ht->
cfg.mode);
770 res =
seq_printf(s,
"%ld %pI4:%u->%pI4:%u %u %u %u\n",
771 (
long)(ent->
expires - jiffies)/HZ,
779 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
781 res =
seq_printf(s,
"%ld %pI6:%u->%pI6:%u %u %u %u\n",
782 (
long)(ent->
expires - jiffies)/HZ,
795 spin_unlock(&ent->
lock);
799 static int dl_seq_show(
struct seq_file *s,
void *v)
802 unsigned int *bucket = (
unsigned int *)v;
806 if (!hlist_empty(&htable->
hash[*bucket])) {
808 if (dl_seq_real_show(ent, htable->family, s))
815 .
start = dl_seq_start,
823 int ret =
seq_open(file, &dl_seq_ops);
827 sf->
private = PDE(inode)->data;
834 .open = dl_proc_open,
840 static int __net_init hashlimit_proc_net_init(
struct net *net)
842 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
847 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
857 static void __net_exit hashlimit_proc_net_exit(
struct net *net)
860 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
865 static int __net_init hashlimit_net_init(
struct net *net)
867 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
870 return hashlimit_proc_net_init(net);
873 static void __net_exit hashlimit_net_exit(
struct net *net)
875 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
878 hashlimit_proc_net_exit(net);
882 .init = hashlimit_net_init,
883 .exit = hashlimit_net_exit,
884 .id = &hashlimit_net_id,
885 .size =
sizeof(
struct hashlimit_net),
888 static int __init hashlimit_mt_init(
void)
904 if (!hashlimit_cachep) {
918 static void __exit hashlimit_mt_exit(
void)