Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * netfilter module to limit the number of parallel tcp |
| 3 | * connections per IP address. |
| 4 | * (c) 2000 Gerd Knorr <kraxel@bytesex.org> |
| 5 | * Nov 2002: Martin Bene <martin.bene@icomedias.com>: |
| 6 | * only ignore TIME_WAIT or gone connections |
Jan Engelhardt | ba5dc27 | 2007-11-05 20:35:56 -0800 | [diff] [blame] | 7 | * (C) CC Computer Consultants GmbH, 2007 |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 8 | * |
| 9 | * based on ... |
| 10 | * |
| 11 | * Kernel module to match connection tracking information. |
| 12 | * GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au). |
| 13 | */ |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 15 | |
Jeremy Sowden | 40d102c | 2019-09-13 09:13:05 +0100 | [diff] [blame] | 16 | #include <linux/ip.h> |
| 17 | #include <linux/ipv6.h> |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 19 | #include <linux/skbuff.h> |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 20 | #include <linux/netfilter/x_tables.h> |
| 21 | #include <linux/netfilter/xt_connlimit.h> |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 22 | |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 23 | #include <net/netfilter/nf_conntrack.h> |
| 24 | #include <net/netfilter/nf_conntrack_core.h> |
| 25 | #include <net/netfilter/nf_conntrack_tuple.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 26 | #include <net/netfilter/nf_conntrack_zones.h> |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 27 | #include <net/netfilter/nf_conntrack_count.h> |
Florian Westphal | 15cfd52 | 2014-03-07 14:37:09 +0100 | [diff] [blame] | 28 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 29 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 30 | connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 31 | { |
Pablo Neira Ayuso | 613dbd9 | 2016-11-03 10:56:21 +0100 | [diff] [blame] | 32 | struct net *net = xt_net(par); |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 33 | const struct xt_connlimit_info *info = par->matchinfo; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 34 | struct nf_conntrack_tuple tuple; |
| 35 | const struct nf_conntrack_tuple *tuple_ptr = &tuple; |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 36 | const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 37 | enum ip_conntrack_info ctinfo; |
| 38 | const struct nf_conn *ct; |
Florian Westphal | 7d08487 | 2014-03-12 23:49:51 +0100 | [diff] [blame] | 39 | unsigned int connections; |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 40 | u32 key[5]; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 41 | |
| 42 | ct = nf_ct_get(skb, &ctinfo); |
Florian Westphal | e59ea3d | 2014-11-14 13:21:48 +0100 | [diff] [blame] | 43 | if (ct != NULL) { |
Changli Gao | 8183e3a | 2011-03-15 13:23:28 +0100 | [diff] [blame] | 44 | tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
Florian Westphal | e59ea3d | 2014-11-14 13:21:48 +0100 | [diff] [blame] | 45 | zone = nf_ct_zone(ct); |
| 46 | } else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), |
Pablo Neira Ayuso | 613dbd9 | 2016-11-03 10:56:21 +0100 | [diff] [blame] | 47 | xt_family(par), net, &tuple)) { |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 48 | goto hotdrop; |
Florian Westphal | e59ea3d | 2014-11-14 13:21:48 +0100 | [diff] [blame] | 49 | } |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 50 | |
Pablo Neira Ayuso | 613dbd9 | 2016-11-03 10:56:21 +0100 | [diff] [blame] | 51 | if (xt_family(par) == NFPROTO_IPV6) { |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 52 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 53 | union nf_inet_addr addr; |
Florian Westphal | b1fc137 | 2017-11-02 12:50:38 +0100 | [diff] [blame] | 54 | unsigned int i; |
| 55 | |
Jan Engelhardt | cc4fc02 | 2011-01-18 17:32:40 +0100 | [diff] [blame] | 56 | memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ? |
| 57 | &iph->daddr : &iph->saddr, sizeof(addr.ip6)); |
Florian Westphal | b1fc137 | 2017-11-02 12:50:38 +0100 | [diff] [blame] | 58 | |
| 59 | for (i = 0; i < ARRAY_SIZE(addr.ip6); ++i) |
| 60 | addr.ip6[i] &= info->mask.ip6[i]; |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 61 | memcpy(key, &addr, sizeof(addr.ip6)); |
| 62 | key[4] = zone->id; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 63 | } else { |
| 64 | const struct iphdr *iph = ip_hdr(skb); |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 65 | key[0] = (info->flags & XT_CONNLIMIT_DADDR) ? |
Jan Engelhardt | cc4fc02 | 2011-01-18 17:32:40 +0100 | [diff] [blame] | 66 | iph->daddr : iph->saddr; |
Florian Westphal | b1fc137 | 2017-11-02 12:50:38 +0100 | [diff] [blame] | 67 | |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 68 | key[0] &= info->mask.ip; |
| 69 | key[1] = zone->id; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Yi-Hung Wei | 6aec208 | 2018-03-04 15:29:51 -0800 | [diff] [blame] | 72 | connections = nf_conncount_count(net, info->data, key, tuple_ptr, |
| 73 | zone); |
Florian Westphal | 7d08487 | 2014-03-12 23:49:51 +0100 | [diff] [blame] | 74 | if (connections == 0) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 75 | /* kmalloc failed, drop it entirely */ |
Richard Weinberger | 1cc34c3 | 2011-01-18 01:36:57 +0100 | [diff] [blame] | 76 | goto hotdrop; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 77 | |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 78 | return (connections > info->limit) ^ !!(info->flags & XT_CONNLIMIT_INVERT); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 79 | |
| 80 | hotdrop: |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame] | 81 | par->hotdrop = true; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 85 | static int connlimit_mt_check(const struct xt_mtchk_param *par) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 86 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 87 | struct xt_connlimit_info *info = par->matchinfo; |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 88 | unsigned int keylen; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 89 | |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 90 | keylen = sizeof(u32); |
| 91 | if (par->family == NFPROTO_IPV6) |
| 92 | keylen += sizeof(struct in6_addr); |
| 93 | else |
| 94 | keylen += sizeof(struct in_addr); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 95 | |
| 96 | /* init private data */ |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 97 | info->data = nf_conncount_init(par->net, par->family, keylen); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 98 | |
YueHaibing | 33b78aa | 2018-07-19 21:20:09 +0800 | [diff] [blame] | 99 | return PTR_ERR_OR_ZERO(info->data); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 102 | static void connlimit_mt_destroy(const struct xt_mtdtor_param *par) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 103 | { |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 104 | const struct xt_connlimit_info *info = par->matchinfo; |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 105 | |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 106 | nf_conncount_destroy(par->net, par->family, info->data); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Cong Wang | 68c07cb | 2012-05-19 04:39:01 +0000 | [diff] [blame] | 109 | static struct xt_match connlimit_mt_reg __read_mostly = { |
| 110 | .name = "connlimit", |
| 111 | .revision = 1, |
| 112 | .family = NFPROTO_UNSPEC, |
| 113 | .checkentry = connlimit_mt_check, |
| 114 | .match = connlimit_mt, |
| 115 | .matchsize = sizeof(struct xt_connlimit_info), |
Willem de Bruijn | ec23189 | 2017-01-02 17:19:46 -0500 | [diff] [blame] | 116 | .usersize = offsetof(struct xt_connlimit_info, data), |
Cong Wang | 68c07cb | 2012-05-19 04:39:01 +0000 | [diff] [blame] | 117 | .destroy = connlimit_mt_destroy, |
| 118 | .me = THIS_MODULE, |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 121 | static int __init connlimit_mt_init(void) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 122 | { |
Florian Westphal | 625c556 | 2017-12-09 21:01:08 +0100 | [diff] [blame] | 123 | return xt_register_match(&connlimit_mt_reg); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 126 | static void __exit connlimit_mt_exit(void) |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 127 | { |
Cong Wang | 68c07cb | 2012-05-19 04:39:01 +0000 | [diff] [blame] | 128 | xt_unregister_match(&connlimit_mt_reg); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 131 | module_init(connlimit_mt_init); |
| 132 | module_exit(connlimit_mt_exit); |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 133 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 134 | MODULE_DESCRIPTION("Xtables: Number of connections matching"); |
Jan Engelhardt | 370786f | 2007-07-14 20:47:26 -0700 | [diff] [blame] | 135 | MODULE_LICENSE("GPL"); |
| 136 | MODULE_ALIAS("ipt_connlimit"); |
| 137 | MODULE_ALIAS("ip6t_connlimit"); |