blob: d4c6db1af8ef223a50a650251f86b12e244d03f1 [file] [log] [blame]
Jan Engelhardt370786f2007-07-14 20:47:26 -07001/*
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 Engelhardtba5dc272007-11-05 20:35:56 -08007 * (C) CC Computer Consultants GmbH, 2007
Jan Engelhardt370786f2007-07-14 20:47:26 -07008 *
9 * based on ...
10 *
11 * Kernel module to match connection tracking information.
12 * GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au).
13 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Jan Engelhardt370786f2007-07-14 20:47:26 -070015#include <linux/in.h>
16#include <linux/in6.h>
17#include <linux/ip.h>
18#include <linux/ipv6.h>
19#include <linux/jhash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Engelhardt370786f2007-07-14 20:47:26 -070021#include <linux/list.h>
22#include <linux/module.h>
23#include <linux/random.h>
24#include <linux/skbuff.h>
25#include <linux/spinlock.h>
26#include <linux/netfilter/nf_conntrack_tcp.h>
27#include <linux/netfilter/x_tables.h>
28#include <linux/netfilter/xt_connlimit.h>
29#include <net/netfilter/nf_conntrack.h>
30#include <net/netfilter/nf_conntrack_core.h>
31#include <net/netfilter/nf_conntrack_tuple.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010032#include <net/netfilter/nf_conntrack_zones.h>
Jan Engelhardt370786f2007-07-14 20:47:26 -070033
34/* we will save the tuples of all connections we care about */
35struct xt_connlimit_conn {
Changli Gao3e0d5142011-03-15 13:25:42 +010036 struct hlist_node node;
Changli Gao8183e3a2011-03-15 13:23:28 +010037 struct nf_conntrack_tuple tuple;
38 union nf_inet_addr addr;
Jan Engelhardt370786f2007-07-14 20:47:26 -070039};
40
41struct xt_connlimit_data {
Changli Gao3e0d5142011-03-15 13:25:42 +010042 struct hlist_head iphash[256];
43 spinlock_t lock;
Jan Engelhardt370786f2007-07-14 20:47:26 -070044};
45
Jan Engelhardt294188a2010-01-04 16:28:38 +010046static u_int32_t connlimit_rnd __read_mostly;
Jan Engelhardt370786f2007-07-14 20:47:26 -070047
Al Viroa34c4582007-07-26 17:33:19 +010048static inline unsigned int connlimit_iphash(__be32 addr)
Jan Engelhardt370786f2007-07-14 20:47:26 -070049{
Al Viroa34c4582007-07-26 17:33:19 +010050 return jhash_1word((__force __u32)addr, connlimit_rnd) & 0xFF;
Jan Engelhardt370786f2007-07-14 20:47:26 -070051}
52
53static inline unsigned int
Jan Engelhardt643a2c12007-12-17 22:43:50 -080054connlimit_iphash6(const union nf_inet_addr *addr,
55 const union nf_inet_addr *mask)
Jan Engelhardt370786f2007-07-14 20:47:26 -070056{
Jan Engelhardt643a2c12007-12-17 22:43:50 -080057 union nf_inet_addr res;
Jan Engelhardt370786f2007-07-14 20:47:26 -070058 unsigned int i;
59
Jan Engelhardt370786f2007-07-14 20:47:26 -070060 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i)
61 res.ip6[i] = addr->ip6[i] & mask->ip6[i];
62
Al Viroa34c4582007-07-26 17:33:19 +010063 return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6), connlimit_rnd) & 0xFF;
Jan Engelhardt370786f2007-07-14 20:47:26 -070064}
65
66static inline bool already_closed(const struct nf_conn *conn)
67{
Patrick McHardy5e8fbe22008-04-14 11:15:52 +020068 if (nf_ct_protonum(conn) == IPPROTO_TCP)
Dong Weid2ee3f22008-06-04 09:57:51 -070069 return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
70 conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
Jan Engelhardt370786f2007-07-14 20:47:26 -070071 else
72 return 0;
73}
74
75static inline unsigned int
Jan Engelhardt643a2c12007-12-17 22:43:50 -080076same_source_net(const union nf_inet_addr *addr,
77 const union nf_inet_addr *mask,
Jan Engelhardt76108ce2008-10-08 11:35:00 +020078 const union nf_inet_addr *u3, u_int8_t family)
Jan Engelhardt370786f2007-07-14 20:47:26 -070079{
Jan Engelhardtee999d82008-10-08 11:35:01 +020080 if (family == NFPROTO_IPV4) {
Jan Engelhardt370786f2007-07-14 20:47:26 -070081 return (addr->ip & mask->ip) == (u3->ip & mask->ip);
82 } else {
Jan Engelhardt643a2c12007-12-17 22:43:50 -080083 union nf_inet_addr lh, rh;
Jan Engelhardt370786f2007-07-14 20:47:26 -070084 unsigned int i;
85
86 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) {
87 lh.ip6[i] = addr->ip6[i] & mask->ip6[i];
88 rh.ip6[i] = u3->ip6[i] & mask->ip6[i];
89 }
90
91 return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6)) == 0;
92 }
93}
94
Florian Westphal15cfd522014-03-07 14:37:09 +010095static int count_hlist(struct net *net,
96 struct hlist_head *head,
97 const struct nf_conntrack_tuple *tuple,
98 const union nf_inet_addr *addr,
99 const union nf_inet_addr *mask,
100 u_int8_t family)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700101{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200102 const struct nf_conntrack_tuple_hash *found;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700103 struct xt_connlimit_conn *conn;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800104 struct hlist_node *n;
Eric Dumazetea781f12009-03-25 21:05:46 +0100105 struct nf_conn *found_ct;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700106 bool addit = true;
107 int matches = 0;
108
Patrick McHardy76507f62008-01-31 04:38:38 -0800109 rcu_read_lock();
Jan Engelhardt370786f2007-07-14 20:47:26 -0700110
111 /* check the saved connections */
Florian Westphal15cfd522014-03-07 14:37:09 +0100112 hlist_for_each_entry_safe(conn, n, head, node) {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100113 found = nf_conntrack_find_get(net, NF_CT_DEFAULT_ZONE,
114 &conn->tuple);
Florian Westphald9ec4f12014-03-07 14:37:10 +0100115 if (found == NULL) {
116 hlist_del(&conn->node);
117 kfree(conn);
118 continue;
119 }
Jan Engelhardt370786f2007-07-14 20:47:26 -0700120
Florian Westphald9ec4f12014-03-07 14:37:10 +0100121 found_ct = nf_ct_tuplehash_to_ctrack(found);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700122
Florian Westphald9ec4f12014-03-07 14:37:10 +0100123 if (nf_ct_tuple_equal(&conn->tuple, tuple)) {
Jan Engelhardt370786f2007-07-14 20:47:26 -0700124 /*
125 * Just to be sure we have it only once in the list.
126 * We should not see tuples twice unless someone hooks
127 * this into a table without "-p tcp --syn".
128 */
129 addit = false;
Florian Westphald9ec4f12014-03-07 14:37:10 +0100130 } else if (already_closed(found_ct)) {
Jan Engelhardt370786f2007-07-14 20:47:26 -0700131 /*
132 * we do not care about connections which are
133 * closed already -> ditch it
134 */
Eric Dumazetea781f12009-03-25 21:05:46 +0100135 nf_ct_put(found_ct);
Changli Gao3e0d5142011-03-15 13:25:42 +0100136 hlist_del(&conn->node);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700137 kfree(conn);
138 continue;
139 }
140
Changli Gao8183e3a2011-03-15 13:23:28 +0100141 if (same_source_net(addr, mask, &conn->addr, family))
Jan Engelhardt370786f2007-07-14 20:47:26 -0700142 /* same source network -> be counted! */
143 ++matches;
Eric Dumazetea781f12009-03-25 21:05:46 +0100144 nf_ct_put(found_ct);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700145 }
146
Patrick McHardy76507f62008-01-31 04:38:38 -0800147 rcu_read_unlock();
Jan Engelhardt370786f2007-07-14 20:47:26 -0700148
149 if (addit) {
150 /* save the new connection in our list */
Changli Gao0e23ca12011-03-15 13:24:56 +0100151 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700152 if (conn == NULL)
153 return -ENOMEM;
154 conn->tuple = *tuple;
Changli Gao8183e3a2011-03-15 13:23:28 +0100155 conn->addr = *addr;
Florian Westphal15cfd522014-03-07 14:37:09 +0100156 hlist_add_head(&conn->node, head);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700157 ++matches;
158 }
159
160 return matches;
161}
162
Florian Westphal15cfd522014-03-07 14:37:09 +0100163static int count_them(struct net *net,
164 struct xt_connlimit_data *data,
165 const struct nf_conntrack_tuple *tuple,
166 const union nf_inet_addr *addr,
167 const union nf_inet_addr *mask,
168 u_int8_t family)
169{
170 struct hlist_head *hhead;
171 int count;
172 u32 hash;
173
174 if (family == NFPROTO_IPV6)
175 hash = connlimit_iphash6(addr, mask);
176 else
177 hash = connlimit_iphash(addr->ip & mask->ip);
178
179 hhead = &data->iphash[hash];
180
181 spin_lock_bh(&data->lock);
182 count = count_hlist(net, hhead, tuple, addr, mask, family);
183 spin_unlock_bh(&data->lock);
184
185 return count;
186}
187
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800188static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200189connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700190{
Alexey Dobriyan83fc8102010-01-18 08:07:50 +0100191 struct net *net = dev_net(par->in ? par->in : par->out);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200192 const struct xt_connlimit_info *info = par->matchinfo;
Jan Engelhardt22c2d8b2007-12-17 22:44:47 -0800193 union nf_inet_addr addr;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700194 struct nf_conntrack_tuple tuple;
195 const struct nf_conntrack_tuple *tuple_ptr = &tuple;
196 enum ip_conntrack_info ctinfo;
197 const struct nf_conn *ct;
198 int connections;
199
200 ct = nf_ct_get(skb, &ctinfo);
Changli Gao8183e3a2011-03-15 13:23:28 +0100201 if (ct != NULL)
202 tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
203 else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
204 par->family, &tuple))
Jan Engelhardt370786f2007-07-14 20:47:26 -0700205 goto hotdrop;
206
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200207 if (par->family == NFPROTO_IPV6) {
Jan Engelhardt370786f2007-07-14 20:47:26 -0700208 const struct ipv6hdr *iph = ipv6_hdr(skb);
Jan Engelhardtcc4fc022011-01-18 17:32:40 +0100209 memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
210 &iph->daddr : &iph->saddr, sizeof(addr.ip6));
Jan Engelhardt370786f2007-07-14 20:47:26 -0700211 } else {
212 const struct iphdr *iph = ip_hdr(skb);
Jan Engelhardtcc4fc022011-01-18 17:32:40 +0100213 addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ?
214 iph->daddr : iph->saddr;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700215 }
216
Alexey Dobriyan83fc8102010-01-18 08:07:50 +0100217 connections = count_them(net, info->data, tuple_ptr, &addr,
Stefan Berger20b79752011-02-14 16:54:33 +0100218 &info->mask, par->family);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700219
Richard Weinberger1cc34c32011-01-18 01:36:57 +0100220 if (connections < 0)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700221 /* kmalloc failed, drop it entirely */
Richard Weinberger1cc34c32011-01-18 01:36:57 +0100222 goto hotdrop;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700223
Jan Engelhardtcc4fc022011-01-18 17:32:40 +0100224 return (connections > info->limit) ^
225 !!(info->flags & XT_CONNLIMIT_INVERT);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700226
227 hotdrop:
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200228 par->hotdrop = true;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700229 return false;
230}
231
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100232static int connlimit_mt_check(const struct xt_mtchk_param *par)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700233{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200234 struct xt_connlimit_info *info = par->matchinfo;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700235 unsigned int i;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100236 int ret;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700237
Changli Gao4656c4d2011-03-15 13:26:32 +0100238 if (unlikely(!connlimit_rnd)) {
239 u_int32_t rand;
240
241 do {
242 get_random_bytes(&rand, sizeof(rand));
243 } while (!rand);
244 cmpxchg(&connlimit_rnd, 0, rand);
Jan Engelhardt294188a2010-01-04 16:28:38 +0100245 }
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100246 ret = nf_ct_l3proto_try_module_get(par->family);
247 if (ret < 0) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100248 pr_info("cannot load conntrack support for "
249 "address family %u\n", par->family);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100250 return ret;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700251 }
252
253 /* init private data */
254 info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
255 if (info->data == NULL) {
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200256 nf_ct_l3proto_module_put(par->family);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100257 return -ENOMEM;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700258 }
259
260 spin_lock_init(&info->data->lock);
261 for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i)
Changli Gao3e0d5142011-03-15 13:25:42 +0100262 INIT_HLIST_HEAD(&info->data->iphash[i]);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700263
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100264 return 0;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700265}
266
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200267static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700268{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200269 const struct xt_connlimit_info *info = par->matchinfo;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700270 struct xt_connlimit_conn *conn;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800271 struct hlist_node *n;
Changli Gao3e0d5142011-03-15 13:25:42 +0100272 struct hlist_head *hash = info->data->iphash;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700273 unsigned int i;
274
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200275 nf_ct_l3proto_module_put(par->family);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700276
277 for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800278 hlist_for_each_entry_safe(conn, n, &hash[i], node) {
Changli Gao3e0d5142011-03-15 13:25:42 +0100279 hlist_del(&conn->node);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700280 kfree(conn);
281 }
282 }
283
284 kfree(info->data);
285}
286
Cong Wang68c07cb2012-05-19 04:39:01 +0000287static struct xt_match connlimit_mt_reg __read_mostly = {
288 .name = "connlimit",
289 .revision = 1,
290 .family = NFPROTO_UNSPEC,
291 .checkentry = connlimit_mt_check,
292 .match = connlimit_mt,
293 .matchsize = sizeof(struct xt_connlimit_info),
294 .destroy = connlimit_mt_destroy,
295 .me = THIS_MODULE,
Jan Engelhardt370786f2007-07-14 20:47:26 -0700296};
297
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800298static int __init connlimit_mt_init(void)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700299{
Cong Wang68c07cb2012-05-19 04:39:01 +0000300 return xt_register_match(&connlimit_mt_reg);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700301}
302
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800303static void __exit connlimit_mt_exit(void)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700304{
Cong Wang68c07cb2012-05-19 04:39:01 +0000305 xt_unregister_match(&connlimit_mt_reg);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700306}
307
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800308module_init(connlimit_mt_init);
309module_exit(connlimit_mt_exit);
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200310MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -0800311MODULE_DESCRIPTION("Xtables: Number of connections matching");
Jan Engelhardt370786f2007-07-14 20:47:26 -0700312MODULE_LICENSE("GPL");
313MODULE_ALIAS("ipt_connlimit");
314MODULE_ALIAS("ip6t_connlimit");