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