]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_connlimit.c
netfilter: nf_conncount: expose connection list interface
[mirror_ubuntu-bionic-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;
370786f9
JE
49};
50
7d084877
FW
51struct xt_connlimit_rb {
52 struct rb_node node;
53 struct hlist_head hhead; /* connections/hosts in same subnet */
54 union nf_inet_addr addr; /* search key */
55};
56
e00b437b
FW
57static spinlock_t xt_connlimit_locks[CONNLIMIT_LOCK_SLOTS] __cacheline_aligned_in_smp;
58
370786f9 59struct xt_connlimit_data {
a2acc543 60 struct rb_root climit_root[CONNLIMIT_SLOTS];
370786f9
JE
61};
62
294188ae 63static u_int32_t connlimit_rnd __read_mostly;
7d084877 64static struct kmem_cache *connlimit_rb_cachep __read_mostly;
14e1a977 65static struct kmem_cache *connlimit_conn_cachep __read_mostly;
370786f9 66
a34c4589 67static inline unsigned int connlimit_iphash(__be32 addr)
370786f9 68{
1442e750
FW
69 return jhash_1word((__force __u32)addr,
70 connlimit_rnd) % CONNLIMIT_SLOTS;
370786f9
JE
71}
72
73static inline unsigned int
b1fc1372 74connlimit_iphash6(const union nf_inet_addr *addr)
370786f9 75{
b1fc1372 76 return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6),
1442e750 77 connlimit_rnd) % CONNLIMIT_SLOTS;
370786f9
JE
78}
79
80static inline bool already_closed(const struct nf_conn *conn)
81{
5e8fbe2a 82 if (nf_ct_protonum(conn) == IPPROTO_TCP)
d2ee3f2c
DW
83 return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
84 conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
370786f9
JE
85 else
86 return 0;
87}
88
50e0e9b1 89static int
b1fc1372
FW
90same_source(const union nf_inet_addr *addr,
91 const union nf_inet_addr *u3, u_int8_t family)
370786f9 92{
b1fc1372
FW
93 if (family == NFPROTO_IPV4)
94 return ntohl(addr->ip) - ntohl(u3->ip);
370786f9 95
b1fc1372 96 return memcmp(addr->ip6, u3->ip6, sizeof(addr->ip6));
370786f9
JE
97}
98
cbe56e6b 99bool nf_conncount_add(struct hlist_head *head,
8b0e6589 100 const struct nf_conntrack_tuple *tuple)
7d084877
FW
101{
102 struct xt_connlimit_conn *conn;
103
104 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
105 if (conn == NULL)
106 return false;
107 conn->tuple = *tuple;
7d084877
FW
108 hlist_add_head(&conn->node, head);
109 return true;
110}
cbe56e6b 111EXPORT_SYMBOL_GPL(nf_conncount_add);
7d084877 112
cbe56e6b
PNA
113unsigned int nf_conncount_lookup(struct net *net, struct hlist_head *head,
114 const struct nf_conntrack_tuple *tuple,
115 const struct nf_conntrack_zone *zone,
116 bool *addit)
370786f9 117{
3cf93c96 118 const struct nf_conntrack_tuple_hash *found;
370786f9 119 struct xt_connlimit_conn *conn;
b67bfe0d 120 struct hlist_node *n;
ea781f19 121 struct nf_conn *found_ct;
7d084877 122 unsigned int length = 0;
370786f9 123
7d084877 124 *addit = true;
370786f9
JE
125
126 /* check the saved connections */
15cfd528 127 hlist_for_each_entry_safe(conn, n, head, node) {
e59ea3df 128 found = nf_conntrack_find_get(net, zone, &conn->tuple);
d9ec4f1e
FW
129 if (found == NULL) {
130 hlist_del(&conn->node);
14e1a977 131 kmem_cache_free(connlimit_conn_cachep, conn);
d9ec4f1e
FW
132 continue;
133 }
370786f9 134
d9ec4f1e 135 found_ct = nf_ct_tuplehash_to_ctrack(found);
370786f9 136
d9ec4f1e 137 if (nf_ct_tuple_equal(&conn->tuple, tuple)) {
370786f9
JE
138 /*
139 * Just to be sure we have it only once in the list.
140 * We should not see tuples twice unless someone hooks
141 * this into a table without "-p tcp --syn".
142 */
3bcc5fdf 143 *addit = false;
d9ec4f1e 144 } else if (already_closed(found_ct)) {
370786f9
JE
145 /*
146 * we do not care about connections which are
147 * closed already -> ditch it
148 */
ea781f19 149 nf_ct_put(found_ct);
3e0d5149 150 hlist_del(&conn->node);
14e1a977 151 kmem_cache_free(connlimit_conn_cachep, conn);
370786f9
JE
152 continue;
153 }
154
ea781f19 155 nf_ct_put(found_ct);
7d084877 156 length++;
370786f9
JE
157 }
158
7d084877 159 return length;
370786f9 160}
cbe56e6b 161EXPORT_SYMBOL_GPL(nf_conncount_lookup);
370786f9 162
7d084877
FW
163static void tree_nodes_free(struct rb_root *root,
164 struct xt_connlimit_rb *gc_nodes[],
165 unsigned int gc_count)
166{
167 struct xt_connlimit_rb *rbconn;
168
169 while (gc_count) {
170 rbconn = gc_nodes[--gc_count];
171 rb_erase(&rbconn->node, root);
172 kmem_cache_free(connlimit_rb_cachep, rbconn);
173 }
174}
175
176static unsigned int
177count_tree(struct net *net, struct rb_root *root,
178 const struct nf_conntrack_tuple *tuple,
b1fc1372 179 const union nf_inet_addr *addr,
308ac914 180 u8 family, const struct nf_conntrack_zone *zone)
3bcc5fdf 181{
7d084877
FW
182 struct xt_connlimit_rb *gc_nodes[CONNLIMIT_GC_MAX_NODES];
183 struct rb_node **rbnode, *parent;
184 struct xt_connlimit_rb *rbconn;
14e1a977 185 struct xt_connlimit_conn *conn;
7d084877
FW
186 unsigned int gc_count;
187 bool no_gc = false;
188
189 restart:
190 gc_count = 0;
191 parent = NULL;
192 rbnode = &(root->rb_node);
193 while (*rbnode) {
194 int diff;
195 bool addit;
196
4cc4b72c 197 rbconn = rb_entry(*rbnode, struct xt_connlimit_rb, node);
7d084877
FW
198
199 parent = *rbnode;
b1fc1372 200 diff = same_source(addr, &rbconn->addr, family);
7d084877
FW
201 if (diff < 0) {
202 rbnode = &((*rbnode)->rb_left);
203 } else if (diff > 0) {
204 rbnode = &((*rbnode)->rb_right);
205 } else {
206 /* same source network -> be counted! */
207 unsigned int count;
cbe56e6b
PNA
208
209 count = nf_conncount_lookup(net, &rbconn->hhead, tuple,
210 zone, &addit);
7d084877
FW
211
212 tree_nodes_free(root, gc_nodes, gc_count);
213 if (!addit)
214 return count;
215
cbe56e6b 216 if (!nf_conncount_add(&rbconn->hhead, tuple))
7d084877
FW
217 return 0; /* hotdrop */
218
219 return count + 1;
220 }
221
222 if (no_gc || gc_count >= ARRAY_SIZE(gc_nodes))
223 continue;
224
225 /* only used for GC on hhead, retval and 'addit' ignored */
cbe56e6b 226 nf_conncount_lookup(net, &rbconn->hhead, tuple, zone, &addit);
7d084877
FW
227 if (hlist_empty(&rbconn->hhead))
228 gc_nodes[gc_count++] = rbconn;
229 }
230
231 if (gc_count) {
232 no_gc = true;
233 tree_nodes_free(root, gc_nodes, gc_count);
234 /* tree_node_free before new allocation permits
235 * allocator to re-use newly free'd object.
236 *
237 * This is a rare event; in most cases we will find
238 * existing node to re-use. (or gc_count is 0).
239 */
240 goto restart;
241 }
242
243 /* no match, need to insert new node */
244 rbconn = kmem_cache_alloc(connlimit_rb_cachep, GFP_ATOMIC);
245 if (rbconn == NULL)
246 return 0;
14e1a977
FW
247
248 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
7d084877
FW
249 if (conn == NULL) {
250 kmem_cache_free(connlimit_rb_cachep, rbconn);
251 return 0;
252 }
253
3bcc5fdf 254 conn->tuple = *tuple;
7d084877
FW
255 rbconn->addr = *addr;
256
257 INIT_HLIST_HEAD(&rbconn->hhead);
258 hlist_add_head(&conn->node, &rbconn->hhead);
259
260 rb_link_node(&rbconn->node, parent, rbnode);
261 rb_insert_color(&rbconn->node, root);
262 return 1;
3bcc5fdf
FW
263}
264
15cfd528
FW
265static int count_them(struct net *net,
266 struct xt_connlimit_data *data,
267 const struct nf_conntrack_tuple *tuple,
268 const union nf_inet_addr *addr,
308ac914
DB
269 u_int8_t family,
270 const struct nf_conntrack_zone *zone)
15cfd528 271{
7d084877 272 struct rb_root *root;
15cfd528
FW
273 int count;
274 u32 hash;
275
a2acc543 276 if (family == NFPROTO_IPV6)
b1fc1372 277 hash = connlimit_iphash6(addr);
a2acc543 278 else
b1fc1372 279 hash = connlimit_iphash(addr->ip);
a2acc543 280 root = &data->climit_root[hash];
15cfd528 281
e00b437b 282 spin_lock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
7d084877 283
b1fc1372 284 count = count_tree(net, root, tuple, addr, family, zone);
7d084877 285
e00b437b 286 spin_unlock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
15cfd528
FW
287
288 return count;
289}
290
d3c5ee6d 291static bool
62fc8051 292connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
370786f9 293{
613dbd95 294 struct net *net = xt_net(par);
f7108a20 295 const struct xt_connlimit_info *info = par->matchinfo;
22c2d8bc 296 union nf_inet_addr addr;
370786f9
JE
297 struct nf_conntrack_tuple tuple;
298 const struct nf_conntrack_tuple *tuple_ptr = &tuple;
308ac914 299 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
370786f9
JE
300 enum ip_conntrack_info ctinfo;
301 const struct nf_conn *ct;
7d084877 302 unsigned int connections;
370786f9
JE
303
304 ct = nf_ct_get(skb, &ctinfo);
e59ea3df 305 if (ct != NULL) {
8183e3a8 306 tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
e59ea3df
FW
307 zone = nf_ct_zone(ct);
308 } else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
613dbd95 309 xt_family(par), net, &tuple)) {
370786f9 310 goto hotdrop;
e59ea3df 311 }
370786f9 312
613dbd95 313 if (xt_family(par) == NFPROTO_IPV6) {
370786f9 314 const struct ipv6hdr *iph = ipv6_hdr(skb);
b1fc1372
FW
315 unsigned int i;
316
cc4fc022
JE
317 memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
318 &iph->daddr : &iph->saddr, sizeof(addr.ip6));
b1fc1372
FW
319
320 for (i = 0; i < ARRAY_SIZE(addr.ip6); ++i)
321 addr.ip6[i] &= info->mask.ip6[i];
370786f9
JE
322 } else {
323 const struct iphdr *iph = ip_hdr(skb);
cc4fc022
JE
324 addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ?
325 iph->daddr : iph->saddr;
b1fc1372
FW
326
327 addr.ip &= info->mask.ip;
370786f9
JE
328 }
329
83fc8102 330 connections = count_them(net, info->data, tuple_ptr, &addr,
b1fc1372 331 xt_family(par), zone);
7d084877 332 if (connections == 0)
370786f9 333 /* kmalloc failed, drop it entirely */
1cc34c30 334 goto hotdrop;
370786f9 335
cc4fc022
JE
336 return (connections > info->limit) ^
337 !!(info->flags & XT_CONNLIMIT_INVERT);
370786f9
JE
338
339 hotdrop:
b4ba2611 340 par->hotdrop = true;
370786f9
JE
341 return false;
342}
343
b0f38452 344static int connlimit_mt_check(const struct xt_mtchk_param *par)
370786f9 345{
9b4fce7a 346 struct xt_connlimit_info *info = par->matchinfo;
370786f9 347 unsigned int i;
4a5a5c73 348 int ret;
370786f9 349
7bdc6624 350 net_get_random_once(&connlimit_rnd, sizeof(connlimit_rnd));
4656c4d6 351
ecb2421b 352 ret = nf_ct_netns_get(par->net, par->family);
4a5a5c73 353 if (ret < 0) {
8bee4bad
JE
354 pr_info("cannot load conntrack support for "
355 "address family %u\n", par->family);
4a5a5c73 356 return ret;
370786f9
JE
357 }
358
359 /* init private data */
360 info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
361 if (info->data == NULL) {
ecb2421b 362 nf_ct_netns_put(par->net, par->family);
4a5a5c73 363 return -ENOMEM;
370786f9
JE
364 }
365
a2acc543
TY
366 for (i = 0; i < ARRAY_SIZE(info->data->climit_root); ++i)
367 info->data->climit_root[i] = RB_ROOT;
370786f9 368
bd414ee6 369 return 0;
370786f9
JE
370}
371
cbe56e6b 372void nf_conncount_cache_free(struct hlist_head *hhead)
370786f9 373{
370786f9 374 struct xt_connlimit_conn *conn;
b67bfe0d 375 struct hlist_node *n;
cbe56e6b
PNA
376
377 hlist_for_each_entry_safe(conn, n, hhead, node)
378 kmem_cache_free(connlimit_conn_cachep, conn);
379}
380EXPORT_SYMBOL_GPL(nf_conncount_cache_free);
381
382static void destroy_tree(struct rb_root *r)
383{
384 struct xt_connlimit_rb *rbconn;
7d084877 385 struct rb_node *node;
370786f9 386
7d084877 387 while ((node = rb_first(r)) != NULL) {
4cc4b72c 388 rbconn = rb_entry(node, struct xt_connlimit_rb, node);
370786f9 389
7d084877
FW
390 rb_erase(node, r);
391
cbe56e6b 392 nf_conncount_cache_free(&rbconn->hhead);
7d084877
FW
393
394 kmem_cache_free(connlimit_rb_cachep, rbconn);
370786f9 395 }
7d084877
FW
396}
397
398static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
399{
400 const struct xt_connlimit_info *info = par->matchinfo;
401 unsigned int i;
402
ecb2421b 403 nf_ct_netns_put(par->net, par->family);
7d084877 404
a2acc543
TY
405 for (i = 0; i < ARRAY_SIZE(info->data->climit_root); ++i)
406 destroy_tree(&info->data->climit_root[i]);
370786f9
JE
407
408 kfree(info->data);
409}
410
68c07cb6
CW
411static struct xt_match connlimit_mt_reg __read_mostly = {
412 .name = "connlimit",
413 .revision = 1,
414 .family = NFPROTO_UNSPEC,
415 .checkentry = connlimit_mt_check,
416 .match = connlimit_mt,
417 .matchsize = sizeof(struct xt_connlimit_info),
ec231890 418 .usersize = offsetof(struct xt_connlimit_info, data),
68c07cb6
CW
419 .destroy = connlimit_mt_destroy,
420 .me = THIS_MODULE,
370786f9
JE
421};
422
d3c5ee6d 423static int __init connlimit_mt_init(void)
370786f9 424{
e00b437b 425 int ret, i;
1442e750
FW
426
427 BUILD_BUG_ON(CONNLIMIT_LOCK_SLOTS > CONNLIMIT_SLOTS);
428 BUILD_BUG_ON((CONNLIMIT_SLOTS % CONNLIMIT_LOCK_SLOTS) != 0);
429
e00b437b
FW
430 for (i = 0; i < CONNLIMIT_LOCK_SLOTS; ++i)
431 spin_lock_init(&xt_connlimit_locks[i]);
432
14e1a977
FW
433 connlimit_conn_cachep = kmem_cache_create("xt_connlimit_conn",
434 sizeof(struct xt_connlimit_conn),
435 0, 0, NULL);
436 if (!connlimit_conn_cachep)
437 return -ENOMEM;
438
7d084877
FW
439 connlimit_rb_cachep = kmem_cache_create("xt_connlimit_rb",
440 sizeof(struct xt_connlimit_rb),
441 0, 0, NULL);
442 if (!connlimit_rb_cachep) {
443 kmem_cache_destroy(connlimit_conn_cachep);
444 return -ENOMEM;
445 }
14e1a977 446 ret = xt_register_match(&connlimit_mt_reg);
7d084877 447 if (ret != 0) {
14e1a977 448 kmem_cache_destroy(connlimit_conn_cachep);
7d084877
FW
449 kmem_cache_destroy(connlimit_rb_cachep);
450 }
14e1a977 451 return ret;
370786f9
JE
452}
453
d3c5ee6d 454static void __exit connlimit_mt_exit(void)
370786f9 455{
68c07cb6 456 xt_unregister_match(&connlimit_mt_reg);
14e1a977 457 kmem_cache_destroy(connlimit_conn_cachep);
7d084877 458 kmem_cache_destroy(connlimit_rb_cachep);
370786f9
JE
459}
460
d3c5ee6d
JE
461module_init(connlimit_mt_init);
462module_exit(connlimit_mt_exit);
92f3b2b1 463MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
2ae15b64 464MODULE_DESCRIPTION("Xtables: Number of connections matching");
370786f9
JE
465MODULE_LICENSE("GPL");
466MODULE_ALIAS("ipt_connlimit");
467MODULE_ALIAS("ip6t_connlimit");