]> git.ipfire.org Git - people/arne_f/kernel.git/blame - net/netfilter/xt_connlimit.c
KVM: VMX: Don't freeze guest when event delivery causes an APIC-access exit
[people/arne_f/kernel.git] / net / netfilter / xt_connlimit.c
CommitLineData
370786f9
JE
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
ba5dc275 7 * (C) CC Computer Consultants GmbH, 2007
370786f9
JE
8 *
9 * based on ...
10 *
11 * Kernel module to match connection tracking information.
12 * GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au).
13 */
8bee4bad 14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
370786f9
JE
15#include <linux/in.h>
16#include <linux/in6.h>
17#include <linux/ip.h>
18#include <linux/ipv6.h>
19#include <linux/jhash.h>
5a0e3ad6 20#include <linux/slab.h>
370786f9 21#include <linux/list.h>
7d084877 22#include <linux/rbtree.h>
370786f9
JE
23#include <linux/module.h>
24#include <linux/random.h>
25#include <linux/skbuff.h>
26#include <linux/spinlock.h>
27#include <linux/netfilter/nf_conntrack_tcp.h>
28#include <linux/netfilter/x_tables.h>
29#include <linux/netfilter/xt_connlimit.h>
30#include <net/netfilter/nf_conntrack.h>
31#include <net/netfilter/nf_conntrack_core.h>
32#include <net/netfilter/nf_conntrack_tuple.h>
5d0aa2cc 33#include <net/netfilter/nf_conntrack_zones.h>
370786f9 34
e00b437b
FW
35#define CONNLIMIT_SLOTS 256U
36
37#ifdef CONFIG_LOCKDEP
38#define CONNLIMIT_LOCK_SLOTS 8U
39#else
40#define CONNLIMIT_LOCK_SLOTS 256U
41#endif
42
7d084877 43#define CONNLIMIT_GC_MAX_NODES 8
1442e750 44
370786f9
JE
45/* we will save the tuples of all connections we care about */
46struct xt_connlimit_conn {
3e0d5149 47 struct hlist_node node;
8183e3a8 48 struct nf_conntrack_tuple tuple;
525e1dff 49 struct nf_conntrack_zone zone;
75af3d78
FW
50 int cpu;
51 u32 jiffies32;
370786f9
JE
52};
53
7d084877
FW
54struct xt_connlimit_rb {
55 struct rb_node node;
56 struct hlist_head hhead; /* connections/hosts in same subnet */
57 union nf_inet_addr addr; /* search key */
58};
59
e00b437b
FW
60static spinlock_t xt_connlimit_locks[CONNLIMIT_LOCK_SLOTS] __cacheline_aligned_in_smp;
61
370786f9 62struct xt_connlimit_data {
a2acc543 63 struct rb_root climit_root[CONNLIMIT_SLOTS];
370786f9
JE
64};
65
294188ae 66static u_int32_t connlimit_rnd __read_mostly;
7d084877 67static struct kmem_cache *connlimit_rb_cachep __read_mostly;
14e1a977 68static struct kmem_cache *connlimit_conn_cachep __read_mostly;
370786f9 69
a34c4589 70static inline unsigned int connlimit_iphash(__be32 addr)
370786f9 71{
1442e750
FW
72 return jhash_1word((__force __u32)addr,
73 connlimit_rnd) % CONNLIMIT_SLOTS;
370786f9
JE
74}
75
76static inline unsigned int
643a2c15
JE
77connlimit_iphash6(const union nf_inet_addr *addr,
78 const union nf_inet_addr *mask)
370786f9 79{
643a2c15 80 union nf_inet_addr res;
370786f9
JE
81 unsigned int i;
82
370786f9
JE
83 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i)
84 res.ip6[i] = addr->ip6[i] & mask->ip6[i];
85
1442e750
FW
86 return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6),
87 connlimit_rnd) % CONNLIMIT_SLOTS;
370786f9
JE
88}
89
90static inline bool already_closed(const struct nf_conn *conn)
91{
5e8fbe2a 92 if (nf_ct_protonum(conn) == IPPROTO_TCP)
d2ee3f2c
DW
93 return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
94 conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
370786f9
JE
95 else
96 return 0;
97}
98
50e0e9b1 99static int
643a2c15
JE
100same_source_net(const union nf_inet_addr *addr,
101 const union nf_inet_addr *mask,
76108cea 102 const union nf_inet_addr *u3, u_int8_t family)
370786f9 103{
ee999d8b 104 if (family == NFPROTO_IPV4) {
50e0e9b1
FW
105 return ntohl(addr->ip & mask->ip) -
106 ntohl(u3->ip & mask->ip);
370786f9 107 } else {
643a2c15 108 union nf_inet_addr lh, rh;
370786f9
JE
109 unsigned int i;
110
111 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) {
112 lh.ip6[i] = addr->ip6[i] & mask->ip6[i];
113 rh.ip6[i] = u3->ip6[i] & mask->ip6[i];
114 }
115
50e0e9b1 116 return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6));
370786f9
JE
117 }
118}
119
15ee3595 120bool nf_conncount_add(struct hlist_head *head,
525e1dff
YHW
121 const struct nf_conntrack_tuple *tuple,
122 const struct nf_conntrack_zone *zone)
7d084877
FW
123{
124 struct xt_connlimit_conn *conn;
125
126 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
127 if (conn == NULL)
128 return false;
129 conn->tuple = *tuple;
525e1dff 130 conn->zone = *zone;
75af3d78
FW
131 conn->cpu = raw_smp_processor_id();
132 conn->jiffies32 = (u32)jiffies;
7d084877
FW
133 hlist_add_head(&conn->node, head);
134 return true;
135}
15ee3595 136EXPORT_SYMBOL_GPL(nf_conncount_add);
7d084877 137
75af3d78
FW
138static const struct nf_conntrack_tuple_hash *
139find_or_evict(struct net *net, struct xt_connlimit_conn *conn)
140{
141 const struct nf_conntrack_tuple_hash *found;
142 unsigned long a, b;
143 int cpu = raw_smp_processor_id();
052ccb86 144 u32 age;
75af3d78
FW
145
146 found = nf_conntrack_find_get(net, &conn->zone, &conn->tuple);
147 if (found)
148 return found;
149 b = conn->jiffies32;
150 a = (u32)jiffies;
151
152 /* conn might have been added just before by another cpu and
153 * might still be unconfirmed. In this case, nf_conntrack_find()
154 * returns no result. Thus only evict if this cpu added the
155 * stale entry or if the entry is older than two jiffies.
156 */
157 age = a - b;
158 if (conn->cpu == cpu || age >= 2) {
159 hlist_del(&conn->node);
160 kmem_cache_free(connlimit_conn_cachep, conn);
161 return ERR_PTR(-ENOENT);
162 }
163
164 return ERR_PTR(-EAGAIN);
165}
166
15ee3595
PNA
167unsigned int nf_conncount_lookup(struct net *net, struct hlist_head *head,
168 const struct nf_conntrack_tuple *tuple,
169 const struct nf_conntrack_zone *zone,
170 bool *addit)
370786f9 171{
3cf93c96 172 const struct nf_conntrack_tuple_hash *found;
370786f9 173 struct xt_connlimit_conn *conn;
ea781f19 174 struct nf_conn *found_ct;
75af3d78 175 struct hlist_node *n;
7d084877 176 unsigned int length = 0;
370786f9 177
7d084877 178 *addit = true;
370786f9
JE
179
180 /* check the saved connections */
15cfd528 181 hlist_for_each_entry_safe(conn, n, head, node) {
75af3d78
FW
182 found = find_or_evict(net, conn);
183 if (IS_ERR(found)) {
184 /* Not found, but might be about to be confirmed */
185 if (PTR_ERR(found) == -EAGAIN) {
186 length++;
187 if (!tuple)
188 continue;
189
190 if (nf_ct_tuple_equal(&conn->tuple, tuple) &&
191 nf_ct_zone_id(&conn->zone, conn->zone.dir) ==
192 nf_ct_zone_id(zone, zone->dir))
193 *addit = false;
194 }
d9ec4f1e
FW
195 continue;
196 }
370786f9 197
d9ec4f1e 198 found_ct = nf_ct_tuplehash_to_ctrack(found);
370786f9 199
525e1dff
YHW
200 if (nf_ct_tuple_equal(&conn->tuple, tuple) &&
201 nf_ct_zone_equal(found_ct, zone, zone->dir)) {
370786f9
JE
202 /*
203 * Just to be sure we have it only once in the list.
204 * We should not see tuples twice unless someone hooks
205 * this into a table without "-p tcp --syn".
206 */
3bcc5fdf 207 *addit = false;
d9ec4f1e 208 } else if (already_closed(found_ct)) {
370786f9
JE
209 /*
210 * we do not care about connections which are
211 * closed already -> ditch it
212 */
ea781f19 213 nf_ct_put(found_ct);
3e0d5149 214 hlist_del(&conn->node);
14e1a977 215 kmem_cache_free(connlimit_conn_cachep, conn);
370786f9
JE
216 continue;
217 }
218
ea781f19 219 nf_ct_put(found_ct);
7d084877 220 length++;
370786f9
JE
221 }
222
7d084877 223 return length;
370786f9 224}
15ee3595 225EXPORT_SYMBOL_GPL(nf_conncount_lookup);
370786f9 226
7d084877
FW
227static void tree_nodes_free(struct rb_root *root,
228 struct xt_connlimit_rb *gc_nodes[],
229 unsigned int gc_count)
230{
231 struct xt_connlimit_rb *rbconn;
232
233 while (gc_count) {
234 rbconn = gc_nodes[--gc_count];
235 rb_erase(&rbconn->node, root);
236 kmem_cache_free(connlimit_rb_cachep, rbconn);
237 }
238}
239
240static unsigned int
241count_tree(struct net *net, struct rb_root *root,
242 const struct nf_conntrack_tuple *tuple,
243 const union nf_inet_addr *addr, const union nf_inet_addr *mask,
308ac914 244 u8 family, const struct nf_conntrack_zone *zone)
3bcc5fdf 245{
7d084877
FW
246 struct xt_connlimit_rb *gc_nodes[CONNLIMIT_GC_MAX_NODES];
247 struct rb_node **rbnode, *parent;
248 struct xt_connlimit_rb *rbconn;
14e1a977 249 struct xt_connlimit_conn *conn;
7d084877
FW
250 unsigned int gc_count;
251 bool no_gc = false;
252
253 restart:
254 gc_count = 0;
255 parent = NULL;
256 rbnode = &(root->rb_node);
257 while (*rbnode) {
258 int diff;
259 bool addit;
260
4cc4b72c 261 rbconn = rb_entry(*rbnode, struct xt_connlimit_rb, node);
7d084877
FW
262
263 parent = *rbnode;
264 diff = same_source_net(addr, mask, &rbconn->addr, family);
265 if (diff < 0) {
266 rbnode = &((*rbnode)->rb_left);
267 } else if (diff > 0) {
268 rbnode = &((*rbnode)->rb_right);
269 } else {
270 /* same source network -> be counted! */
271 unsigned int count;
15ee3595
PNA
272
273 count = nf_conncount_lookup(net, &rbconn->hhead, tuple,
274 zone, &addit);
7d084877
FW
275
276 tree_nodes_free(root, gc_nodes, gc_count);
277 if (!addit)
278 return count;
279
525e1dff 280 if (!nf_conncount_add(&rbconn->hhead, tuple, zone))
7d084877
FW
281 return 0; /* hotdrop */
282
283 return count + 1;
284 }
285
286 if (no_gc || gc_count >= ARRAY_SIZE(gc_nodes))
287 continue;
288
289 /* only used for GC on hhead, retval and 'addit' ignored */
15ee3595 290 nf_conncount_lookup(net, &rbconn->hhead, tuple, zone, &addit);
7d084877
FW
291 if (hlist_empty(&rbconn->hhead))
292 gc_nodes[gc_count++] = rbconn;
293 }
294
295 if (gc_count) {
296 no_gc = true;
297 tree_nodes_free(root, gc_nodes, gc_count);
298 /* tree_node_free before new allocation permits
299 * allocator to re-use newly free'd object.
300 *
301 * This is a rare event; in most cases we will find
302 * existing node to re-use. (or gc_count is 0).
303 */
304 goto restart;
305 }
306
307 /* no match, need to insert new node */
308 rbconn = kmem_cache_alloc(connlimit_rb_cachep, GFP_ATOMIC);
309 if (rbconn == NULL)
310 return 0;
14e1a977
FW
311
312 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
7d084877
FW
313 if (conn == NULL) {
314 kmem_cache_free(connlimit_rb_cachep, rbconn);
315 return 0;
316 }
317
3bcc5fdf 318 conn->tuple = *tuple;
525e1dff 319 conn->zone = *zone;
7d084877
FW
320 rbconn->addr = *addr;
321
322 INIT_HLIST_HEAD(&rbconn->hhead);
323 hlist_add_head(&conn->node, &rbconn->hhead);
324
325 rb_link_node(&rbconn->node, parent, rbnode);
326 rb_insert_color(&rbconn->node, root);
327 return 1;
3bcc5fdf
FW
328}
329
15cfd528
FW
330static int count_them(struct net *net,
331 struct xt_connlimit_data *data,
332 const struct nf_conntrack_tuple *tuple,
333 const union nf_inet_addr *addr,
334 const union nf_inet_addr *mask,
308ac914
DB
335 u_int8_t family,
336 const struct nf_conntrack_zone *zone)
15cfd528 337{
7d084877 338 struct rb_root *root;
15cfd528
FW
339 int count;
340 u32 hash;
341
a2acc543 342 if (family == NFPROTO_IPV6)
15cfd528 343 hash = connlimit_iphash6(addr, mask);
a2acc543 344 else
15cfd528 345 hash = connlimit_iphash(addr->ip & mask->ip);
a2acc543 346 root = &data->climit_root[hash];
15cfd528 347
e00b437b 348 spin_lock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
7d084877 349
e59ea3df 350 count = count_tree(net, root, tuple, addr, mask, family, zone);
7d084877 351
e00b437b 352 spin_unlock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
15cfd528
FW
353
354 return count;
355}
356
d3c5ee6d 357static bool
62fc8051 358connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
370786f9 359{
613dbd95 360 struct net *net = xt_net(par);
f7108a20 361 const struct xt_connlimit_info *info = par->matchinfo;
22c2d8bc 362 union nf_inet_addr addr;
370786f9
JE
363 struct nf_conntrack_tuple tuple;
364 const struct nf_conntrack_tuple *tuple_ptr = &tuple;
308ac914 365 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
370786f9
JE
366 enum ip_conntrack_info ctinfo;
367 const struct nf_conn *ct;
7d084877 368 unsigned int connections;
370786f9
JE
369
370 ct = nf_ct_get(skb, &ctinfo);
e59ea3df 371 if (ct != NULL) {
8183e3a8 372 tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
e59ea3df
FW
373 zone = nf_ct_zone(ct);
374 } else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
613dbd95 375 xt_family(par), net, &tuple)) {
370786f9 376 goto hotdrop;
e59ea3df 377 }
370786f9 378
613dbd95 379 if (xt_family(par) == NFPROTO_IPV6) {
370786f9 380 const struct ipv6hdr *iph = ipv6_hdr(skb);
cc4fc022
JE
381 memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
382 &iph->daddr : &iph->saddr, sizeof(addr.ip6));
370786f9
JE
383 } else {
384 const struct iphdr *iph = ip_hdr(skb);
cc4fc022
JE
385 addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ?
386 iph->daddr : iph->saddr;
370786f9
JE
387 }
388
83fc8102 389 connections = count_them(net, info->data, tuple_ptr, &addr,
613dbd95 390 &info->mask, xt_family(par), zone);
7d084877 391 if (connections == 0)
370786f9 392 /* kmalloc failed, drop it entirely */
1cc34c30 393 goto hotdrop;
370786f9 394
cc4fc022
JE
395 return (connections > info->limit) ^
396 !!(info->flags & XT_CONNLIMIT_INVERT);
370786f9
JE
397
398 hotdrop:
b4ba2611 399 par->hotdrop = true;
370786f9
JE
400 return false;
401}
402
b0f38452 403static int connlimit_mt_check(const struct xt_mtchk_param *par)
370786f9 404{
9b4fce7a 405 struct xt_connlimit_info *info = par->matchinfo;
370786f9 406 unsigned int i;
4a5a5c73 407 int ret;
370786f9 408
7bdc6624 409 net_get_random_once(&connlimit_rnd, sizeof(connlimit_rnd));
4656c4d6 410
ecb2421b 411 ret = nf_ct_netns_get(par->net, par->family);
4a5a5c73 412 if (ret < 0) {
8bee4bad
JE
413 pr_info("cannot load conntrack support for "
414 "address family %u\n", par->family);
4a5a5c73 415 return ret;
370786f9
JE
416 }
417
418 /* init private data */
419 info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
420 if (info->data == NULL) {
ecb2421b 421 nf_ct_netns_put(par->net, par->family);
4a5a5c73 422 return -ENOMEM;
370786f9
JE
423 }
424
a2acc543
TY
425 for (i = 0; i < ARRAY_SIZE(info->data->climit_root); ++i)
426 info->data->climit_root[i] = RB_ROOT;
370786f9 427
bd414ee6 428 return 0;
370786f9
JE
429}
430
15ee3595 431void nf_conncount_cache_free(struct hlist_head *hhead)
370786f9 432{
370786f9 433 struct xt_connlimit_conn *conn;
b67bfe0d 434 struct hlist_node *n;
15ee3595
PNA
435
436 hlist_for_each_entry_safe(conn, n, hhead, node)
437 kmem_cache_free(connlimit_conn_cachep, conn);
438}
439EXPORT_SYMBOL_GPL(nf_conncount_cache_free);
440
441static void destroy_tree(struct rb_root *r)
442{
443 struct xt_connlimit_rb *rbconn;
7d084877 444 struct rb_node *node;
370786f9 445
7d084877 446 while ((node = rb_first(r)) != NULL) {
4cc4b72c 447 rbconn = rb_entry(node, struct xt_connlimit_rb, node);
370786f9 448
7d084877
FW
449 rb_erase(node, r);
450
15ee3595 451 nf_conncount_cache_free(&rbconn->hhead);
7d084877
FW
452
453 kmem_cache_free(connlimit_rb_cachep, rbconn);
370786f9 454 }
7d084877
FW
455}
456
457static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
458{
459 const struct xt_connlimit_info *info = par->matchinfo;
460 unsigned int i;
461
ecb2421b 462 nf_ct_netns_put(par->net, par->family);
7d084877 463
a2acc543
TY
464 for (i = 0; i < ARRAY_SIZE(info->data->climit_root); ++i)
465 destroy_tree(&info->data->climit_root[i]);
370786f9
JE
466
467 kfree(info->data);
468}
469
68c07cb6
CW
470static struct xt_match connlimit_mt_reg __read_mostly = {
471 .name = "connlimit",
472 .revision = 1,
473 .family = NFPROTO_UNSPEC,
474 .checkentry = connlimit_mt_check,
475 .match = connlimit_mt,
476 .matchsize = sizeof(struct xt_connlimit_info),
ec231890 477 .usersize = offsetof(struct xt_connlimit_info, data),
68c07cb6
CW
478 .destroy = connlimit_mt_destroy,
479 .me = THIS_MODULE,
370786f9
JE
480};
481
d3c5ee6d 482static int __init connlimit_mt_init(void)
370786f9 483{
e00b437b 484 int ret, i;
1442e750
FW
485
486 BUILD_BUG_ON(CONNLIMIT_LOCK_SLOTS > CONNLIMIT_SLOTS);
487 BUILD_BUG_ON((CONNLIMIT_SLOTS % CONNLIMIT_LOCK_SLOTS) != 0);
488
e00b437b
FW
489 for (i = 0; i < CONNLIMIT_LOCK_SLOTS; ++i)
490 spin_lock_init(&xt_connlimit_locks[i]);
491
14e1a977
FW
492 connlimit_conn_cachep = kmem_cache_create("xt_connlimit_conn",
493 sizeof(struct xt_connlimit_conn),
494 0, 0, NULL);
495 if (!connlimit_conn_cachep)
496 return -ENOMEM;
497
7d084877
FW
498 connlimit_rb_cachep = kmem_cache_create("xt_connlimit_rb",
499 sizeof(struct xt_connlimit_rb),
500 0, 0, NULL);
501 if (!connlimit_rb_cachep) {
502 kmem_cache_destroy(connlimit_conn_cachep);
503 return -ENOMEM;
504 }
14e1a977 505 ret = xt_register_match(&connlimit_mt_reg);
7d084877 506 if (ret != 0) {
14e1a977 507 kmem_cache_destroy(connlimit_conn_cachep);
7d084877
FW
508 kmem_cache_destroy(connlimit_rb_cachep);
509 }
14e1a977 510 return ret;
370786f9
JE
511}
512
d3c5ee6d 513static void __exit connlimit_mt_exit(void)
370786f9 514{
68c07cb6 515 xt_unregister_match(&connlimit_mt_reg);
14e1a977 516 kmem_cache_destroy(connlimit_conn_cachep);
7d084877 517 kmem_cache_destroy(connlimit_rb_cachep);
370786f9
JE
518}
519
d3c5ee6d
JE
520module_init(connlimit_mt_init);
521module_exit(connlimit_mt_exit);
92f3b2b1 522MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
2ae15b64 523MODULE_DESCRIPTION("Xtables: Number of connections matching");
370786f9
JE
524MODULE_LICENSE("GPL");
525MODULE_ALIAS("ipt_connlimit");
526MODULE_ALIAS("ip6t_connlimit");