blob: fbc66bb250d54de3d8cac6254110fedf1af7dd01 [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>
Florian Westphal7d084872014-03-12 23:49:51 +010022#include <linux/rbtree.h>
Jan Engelhardt370786f2007-07-14 20:47:26 -070023#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>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010033#include <net/netfilter/nf_conntrack_zones.h>
Jan Engelhardt370786f2007-07-14 20:47:26 -070034
Florian Westphale00b437b2014-03-20 11:53:39 +010035#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
Florian Westphal7d084872014-03-12 23:49:51 +010043#define CONNLIMIT_GC_MAX_NODES 8
Florian Westphal1442e752014-03-12 23:49:49 +010044
Jan Engelhardt370786f2007-07-14 20:47:26 -070045/* we will save the tuples of all connections we care about */
46struct xt_connlimit_conn {
Changli Gao3e0d5142011-03-15 13:25:42 +010047 struct hlist_node node;
Changli Gao8183e3a2011-03-15 13:23:28 +010048 struct nf_conntrack_tuple tuple;
49 union nf_inet_addr addr;
Jan Engelhardt370786f2007-07-14 20:47:26 -070050};
51
Florian Westphal7d084872014-03-12 23:49:51 +010052struct xt_connlimit_rb {
53 struct rb_node node;
54 struct hlist_head hhead; /* connections/hosts in same subnet */
55 union nf_inet_addr addr; /* search key */
56};
57
Florian Westphale00b437b2014-03-20 11:53:39 +010058static spinlock_t xt_connlimit_locks[CONNLIMIT_LOCK_SLOTS] __cacheline_aligned_in_smp;
59
Jan Engelhardt370786f2007-07-14 20:47:26 -070060struct xt_connlimit_data {
Florian Westphal7d084872014-03-12 23:49:51 +010061 struct rb_root climit_root4[CONNLIMIT_SLOTS];
62 struct rb_root climit_root6[CONNLIMIT_SLOTS];
Jan Engelhardt370786f2007-07-14 20:47:26 -070063};
64
Jan Engelhardt294188a2010-01-04 16:28:38 +010065static u_int32_t connlimit_rnd __read_mostly;
Florian Westphal7d084872014-03-12 23:49:51 +010066static struct kmem_cache *connlimit_rb_cachep __read_mostly;
Florian Westphal14e1a972014-03-07 14:37:12 +010067static struct kmem_cache *connlimit_conn_cachep __read_mostly;
Jan Engelhardt370786f2007-07-14 20:47:26 -070068
Al Viroa34c4582007-07-26 17:33:19 +010069static inline unsigned int connlimit_iphash(__be32 addr)
Jan Engelhardt370786f2007-07-14 20:47:26 -070070{
Florian Westphal1442e752014-03-12 23:49:49 +010071 return jhash_1word((__force __u32)addr,
72 connlimit_rnd) % CONNLIMIT_SLOTS;
Jan Engelhardt370786f2007-07-14 20:47:26 -070073}
74
75static inline unsigned int
Jan Engelhardt643a2c12007-12-17 22:43:50 -080076connlimit_iphash6(const union nf_inet_addr *addr,
77 const union nf_inet_addr *mask)
Jan Engelhardt370786f2007-07-14 20:47:26 -070078{
Jan Engelhardt643a2c12007-12-17 22:43:50 -080079 union nf_inet_addr res;
Jan Engelhardt370786f2007-07-14 20:47:26 -070080 unsigned int i;
81
Jan Engelhardt370786f2007-07-14 20:47:26 -070082 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i)
83 res.ip6[i] = addr->ip6[i] & mask->ip6[i];
84
Florian Westphal1442e752014-03-12 23:49:49 +010085 return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6),
86 connlimit_rnd) % CONNLIMIT_SLOTS;
Jan Engelhardt370786f2007-07-14 20:47:26 -070087}
88
89static inline bool already_closed(const struct nf_conn *conn)
90{
Patrick McHardy5e8fbe22008-04-14 11:15:52 +020091 if (nf_ct_protonum(conn) == IPPROTO_TCP)
Dong Weid2ee3f22008-06-04 09:57:51 -070092 return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
93 conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
Jan Engelhardt370786f2007-07-14 20:47:26 -070094 else
95 return 0;
96}
97
Florian Westphal50e0e9b2014-03-12 23:49:50 +010098static int
Jan Engelhardt643a2c12007-12-17 22:43:50 -080099same_source_net(const union nf_inet_addr *addr,
100 const union nf_inet_addr *mask,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200101 const union nf_inet_addr *u3, u_int8_t family)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700102{
Jan Engelhardtee999d82008-10-08 11:35:01 +0200103 if (family == NFPROTO_IPV4) {
Florian Westphal50e0e9b2014-03-12 23:49:50 +0100104 return ntohl(addr->ip & mask->ip) -
105 ntohl(u3->ip & mask->ip);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700106 } else {
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800107 union nf_inet_addr lh, rh;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700108 unsigned int i;
109
110 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) {
111 lh.ip6[i] = addr->ip6[i] & mask->ip6[i];
112 rh.ip6[i] = u3->ip6[i] & mask->ip6[i];
113 }
114
Florian Westphal50e0e9b2014-03-12 23:49:50 +0100115 return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6));
Jan Engelhardt370786f2007-07-14 20:47:26 -0700116 }
117}
118
Florian Westphal7d084872014-03-12 23:49:51 +0100119static bool add_hlist(struct hlist_head *head,
120 const struct nf_conntrack_tuple *tuple,
121 const union nf_inet_addr *addr)
122{
123 struct xt_connlimit_conn *conn;
124
125 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
126 if (conn == NULL)
127 return false;
128 conn->tuple = *tuple;
129 conn->addr = *addr;
130 hlist_add_head(&conn->node, head);
131 return true;
132}
133
134static unsigned int check_hlist(struct net *net,
135 struct hlist_head *head,
136 const struct nf_conntrack_tuple *tuple,
137 bool *addit)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700138{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200139 const struct nf_conntrack_tuple_hash *found;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700140 struct xt_connlimit_conn *conn;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800141 struct hlist_node *n;
Eric Dumazetea781f12009-03-25 21:05:46 +0100142 struct nf_conn *found_ct;
Florian Westphal7d084872014-03-12 23:49:51 +0100143 unsigned int length = 0;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700144
Florian Westphal7d084872014-03-12 23:49:51 +0100145 *addit = true;
Patrick McHardy76507f62008-01-31 04:38:38 -0800146 rcu_read_lock();
Jan Engelhardt370786f2007-07-14 20:47:26 -0700147
148 /* check the saved connections */
Florian Westphal15cfd522014-03-07 14:37:09 +0100149 hlist_for_each_entry_safe(conn, n, head, node) {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100150 found = nf_conntrack_find_get(net, NF_CT_DEFAULT_ZONE,
151 &conn->tuple);
Florian Westphald9ec4f12014-03-07 14:37:10 +0100152 if (found == NULL) {
153 hlist_del(&conn->node);
Florian Westphal14e1a972014-03-07 14:37:12 +0100154 kmem_cache_free(connlimit_conn_cachep, conn);
Florian Westphald9ec4f12014-03-07 14:37:10 +0100155 continue;
156 }
Jan Engelhardt370786f2007-07-14 20:47:26 -0700157
Florian Westphald9ec4f12014-03-07 14:37:10 +0100158 found_ct = nf_ct_tuplehash_to_ctrack(found);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700159
Florian Westphald9ec4f12014-03-07 14:37:10 +0100160 if (nf_ct_tuple_equal(&conn->tuple, tuple)) {
Jan Engelhardt370786f2007-07-14 20:47:26 -0700161 /*
162 * Just to be sure we have it only once in the list.
163 * We should not see tuples twice unless someone hooks
164 * this into a table without "-p tcp --syn".
165 */
Florian Westphal3bcc5fd2014-03-07 14:37:11 +0100166 *addit = false;
Florian Westphald9ec4f12014-03-07 14:37:10 +0100167 } else if (already_closed(found_ct)) {
Jan Engelhardt370786f2007-07-14 20:47:26 -0700168 /*
169 * we do not care about connections which are
170 * closed already -> ditch it
171 */
Eric Dumazetea781f12009-03-25 21:05:46 +0100172 nf_ct_put(found_ct);
Changli Gao3e0d5142011-03-15 13:25:42 +0100173 hlist_del(&conn->node);
Florian Westphal14e1a972014-03-07 14:37:12 +0100174 kmem_cache_free(connlimit_conn_cachep, conn);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700175 continue;
176 }
177
Eric Dumazetea781f12009-03-25 21:05:46 +0100178 nf_ct_put(found_ct);
Florian Westphal7d084872014-03-12 23:49:51 +0100179 length++;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700180 }
181
Patrick McHardy76507f62008-01-31 04:38:38 -0800182 rcu_read_unlock();
Jan Engelhardt370786f2007-07-14 20:47:26 -0700183
Florian Westphal7d084872014-03-12 23:49:51 +0100184 return length;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700185}
186
Florian Westphal7d084872014-03-12 23:49:51 +0100187static void tree_nodes_free(struct rb_root *root,
188 struct xt_connlimit_rb *gc_nodes[],
189 unsigned int gc_count)
Florian Westphal3bcc5fd2014-03-07 14:37:11 +0100190{
Florian Westphal7d084872014-03-12 23:49:51 +0100191 struct xt_connlimit_rb *rbconn;
192
193 while (gc_count) {
194 rbconn = gc_nodes[--gc_count];
195 rb_erase(&rbconn->node, root);
196 kmem_cache_free(connlimit_rb_cachep, rbconn);
197 }
198}
199
200static unsigned int
201count_tree(struct net *net, struct rb_root *root,
202 const struct nf_conntrack_tuple *tuple,
203 const union nf_inet_addr *addr, const union nf_inet_addr *mask,
204 u8 family)
205{
206 struct xt_connlimit_rb *gc_nodes[CONNLIMIT_GC_MAX_NODES];
207 struct rb_node **rbnode, *parent;
208 struct xt_connlimit_rb *rbconn;
Florian Westphal14e1a972014-03-07 14:37:12 +0100209 struct xt_connlimit_conn *conn;
Florian Westphal7d084872014-03-12 23:49:51 +0100210 unsigned int gc_count;
211 bool no_gc = false;
212
213 restart:
214 gc_count = 0;
215 parent = NULL;
216 rbnode = &(root->rb_node);
217 while (*rbnode) {
218 int diff;
219 bool addit;
220
221 rbconn = container_of(*rbnode, struct xt_connlimit_rb, node);
222
223 parent = *rbnode;
224 diff = same_source_net(addr, mask, &rbconn->addr, family);
225 if (diff < 0) {
226 rbnode = &((*rbnode)->rb_left);
227 } else if (diff > 0) {
228 rbnode = &((*rbnode)->rb_right);
229 } else {
230 /* same source network -> be counted! */
231 unsigned int count;
232 count = check_hlist(net, &rbconn->hhead, tuple, &addit);
233
234 tree_nodes_free(root, gc_nodes, gc_count);
235 if (!addit)
236 return count;
237
238 if (!add_hlist(&rbconn->hhead, tuple, addr))
239 return 0; /* hotdrop */
240
241 return count + 1;
242 }
243
244 if (no_gc || gc_count >= ARRAY_SIZE(gc_nodes))
245 continue;
246
247 /* only used for GC on hhead, retval and 'addit' ignored */
248 check_hlist(net, &rbconn->hhead, tuple, &addit);
249 if (hlist_empty(&rbconn->hhead))
250 gc_nodes[gc_count++] = rbconn;
251 }
252
253 if (gc_count) {
254 no_gc = true;
255 tree_nodes_free(root, gc_nodes, gc_count);
256 /* tree_node_free before new allocation permits
257 * allocator to re-use newly free'd object.
258 *
259 * This is a rare event; in most cases we will find
260 * existing node to re-use. (or gc_count is 0).
261 */
262 goto restart;
263 }
264
265 /* no match, need to insert new node */
266 rbconn = kmem_cache_alloc(connlimit_rb_cachep, GFP_ATOMIC);
267 if (rbconn == NULL)
268 return 0;
Florian Westphal14e1a972014-03-07 14:37:12 +0100269
270 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
Florian Westphal7d084872014-03-12 23:49:51 +0100271 if (conn == NULL) {
272 kmem_cache_free(connlimit_rb_cachep, rbconn);
273 return 0;
274 }
275
Florian Westphal3bcc5fd2014-03-07 14:37:11 +0100276 conn->tuple = *tuple;
277 conn->addr = *addr;
Florian Westphal7d084872014-03-12 23:49:51 +0100278 rbconn->addr = *addr;
279
280 INIT_HLIST_HEAD(&rbconn->hhead);
281 hlist_add_head(&conn->node, &rbconn->hhead);
282
283 rb_link_node(&rbconn->node, parent, rbnode);
284 rb_insert_color(&rbconn->node, root);
285 return 1;
Florian Westphal3bcc5fd2014-03-07 14:37:11 +0100286}
287
Florian Westphal15cfd522014-03-07 14:37:09 +0100288static int count_them(struct net *net,
289 struct xt_connlimit_data *data,
290 const struct nf_conntrack_tuple *tuple,
291 const union nf_inet_addr *addr,
292 const union nf_inet_addr *mask,
293 u_int8_t family)
294{
Florian Westphal7d084872014-03-12 23:49:51 +0100295 struct rb_root *root;
Florian Westphal15cfd522014-03-07 14:37:09 +0100296 int count;
297 u32 hash;
298
Florian Westphal7d084872014-03-12 23:49:51 +0100299 if (family == NFPROTO_IPV6) {
Florian Westphal15cfd522014-03-07 14:37:09 +0100300 hash = connlimit_iphash6(addr, mask);
Florian Westphal7d084872014-03-12 23:49:51 +0100301 root = &data->climit_root6[hash];
302 } else {
Florian Westphal15cfd522014-03-07 14:37:09 +0100303 hash = connlimit_iphash(addr->ip & mask->ip);
Florian Westphal7d084872014-03-12 23:49:51 +0100304 root = &data->climit_root4[hash];
305 }
Florian Westphal15cfd522014-03-07 14:37:09 +0100306
Florian Westphale00b437b2014-03-20 11:53:39 +0100307 spin_lock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
Florian Westphal7d084872014-03-12 23:49:51 +0100308
309 count = count_tree(net, root, tuple, addr, mask, family);
310
Florian Westphale00b437b2014-03-20 11:53:39 +0100311 spin_unlock_bh(&xt_connlimit_locks[hash % CONNLIMIT_LOCK_SLOTS]);
Florian Westphal15cfd522014-03-07 14:37:09 +0100312
313 return count;
314}
315
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800316static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200317connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700318{
Alexey Dobriyan83fc8102010-01-18 08:07:50 +0100319 struct net *net = dev_net(par->in ? par->in : par->out);
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200320 const struct xt_connlimit_info *info = par->matchinfo;
Jan Engelhardt22c2d8b2007-12-17 22:44:47 -0800321 union nf_inet_addr addr;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700322 struct nf_conntrack_tuple tuple;
323 const struct nf_conntrack_tuple *tuple_ptr = &tuple;
324 enum ip_conntrack_info ctinfo;
325 const struct nf_conn *ct;
Florian Westphal7d084872014-03-12 23:49:51 +0100326 unsigned int connections;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700327
328 ct = nf_ct_get(skb, &ctinfo);
Changli Gao8183e3a2011-03-15 13:23:28 +0100329 if (ct != NULL)
330 tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
331 else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
332 par->family, &tuple))
Jan Engelhardt370786f2007-07-14 20:47:26 -0700333 goto hotdrop;
334
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200335 if (par->family == NFPROTO_IPV6) {
Jan Engelhardt370786f2007-07-14 20:47:26 -0700336 const struct ipv6hdr *iph = ipv6_hdr(skb);
Jan Engelhardtcc4fc022011-01-18 17:32:40 +0100337 memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
338 &iph->daddr : &iph->saddr, sizeof(addr.ip6));
Jan Engelhardt370786f2007-07-14 20:47:26 -0700339 } else {
340 const struct iphdr *iph = ip_hdr(skb);
Jan Engelhardtcc4fc022011-01-18 17:32:40 +0100341 addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ?
342 iph->daddr : iph->saddr;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700343 }
344
Alexey Dobriyan83fc8102010-01-18 08:07:50 +0100345 connections = count_them(net, info->data, tuple_ptr, &addr,
Stefan Berger20b79752011-02-14 16:54:33 +0100346 &info->mask, par->family);
Florian Westphal7d084872014-03-12 23:49:51 +0100347 if (connections == 0)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700348 /* kmalloc failed, drop it entirely */
Richard Weinberger1cc34c32011-01-18 01:36:57 +0100349 goto hotdrop;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700350
Jan Engelhardtcc4fc022011-01-18 17:32:40 +0100351 return (connections > info->limit) ^
352 !!(info->flags & XT_CONNLIMIT_INVERT);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700353
354 hotdrop:
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200355 par->hotdrop = true;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700356 return false;
357}
358
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100359static int connlimit_mt_check(const struct xt_mtchk_param *par)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700360{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200361 struct xt_connlimit_info *info = par->matchinfo;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700362 unsigned int i;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100363 int ret;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700364
Changli Gao4656c4d2011-03-15 13:26:32 +0100365 if (unlikely(!connlimit_rnd)) {
366 u_int32_t rand;
367
368 do {
369 get_random_bytes(&rand, sizeof(rand));
370 } while (!rand);
371 cmpxchg(&connlimit_rnd, 0, rand);
Jan Engelhardt294188a2010-01-04 16:28:38 +0100372 }
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100373 ret = nf_ct_l3proto_try_module_get(par->family);
374 if (ret < 0) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100375 pr_info("cannot load conntrack support for "
376 "address family %u\n", par->family);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100377 return ret;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700378 }
379
380 /* init private data */
381 info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
382 if (info->data == NULL) {
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200383 nf_ct_l3proto_module_put(par->family);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100384 return -ENOMEM;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700385 }
386
Florian Westphal7d084872014-03-12 23:49:51 +0100387 for (i = 0; i < ARRAY_SIZE(info->data->climit_root4); ++i)
388 info->data->climit_root4[i] = RB_ROOT;
389 for (i = 0; i < ARRAY_SIZE(info->data->climit_root6); ++i)
390 info->data->climit_root6[i] = RB_ROOT;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700391
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100392 return 0;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700393}
394
Florian Westphal7d084872014-03-12 23:49:51 +0100395static void destroy_tree(struct rb_root *r)
396{
397 struct xt_connlimit_conn *conn;
398 struct xt_connlimit_rb *rbconn;
399 struct hlist_node *n;
400 struct rb_node *node;
401
402 while ((node = rb_first(r)) != NULL) {
403 rbconn = container_of(node, struct xt_connlimit_rb, node);
404
405 rb_erase(node, r);
406
407 hlist_for_each_entry_safe(conn, n, &rbconn->hhead, node)
408 kmem_cache_free(connlimit_conn_cachep, conn);
409
410 kmem_cache_free(connlimit_rb_cachep, rbconn);
411 }
412}
413
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200414static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700415{
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200416 const struct xt_connlimit_info *info = par->matchinfo;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700417 unsigned int i;
418
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200419 nf_ct_l3proto_module_put(par->family);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700420
Florian Westphal7d084872014-03-12 23:49:51 +0100421 for (i = 0; i < ARRAY_SIZE(info->data->climit_root4); ++i)
422 destroy_tree(&info->data->climit_root4[i]);
423 for (i = 0; i < ARRAY_SIZE(info->data->climit_root6); ++i)
424 destroy_tree(&info->data->climit_root6[i]);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700425
426 kfree(info->data);
427}
428
Cong Wang68c07cb2012-05-19 04:39:01 +0000429static struct xt_match connlimit_mt_reg __read_mostly = {
430 .name = "connlimit",
431 .revision = 1,
432 .family = NFPROTO_UNSPEC,
433 .checkentry = connlimit_mt_check,
434 .match = connlimit_mt,
435 .matchsize = sizeof(struct xt_connlimit_info),
436 .destroy = connlimit_mt_destroy,
437 .me = THIS_MODULE,
Jan Engelhardt370786f2007-07-14 20:47:26 -0700438};
439
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800440static int __init connlimit_mt_init(void)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700441{
Florian Westphale00b437b2014-03-20 11:53:39 +0100442 int ret, i;
Florian Westphal1442e752014-03-12 23:49:49 +0100443
444 BUILD_BUG_ON(CONNLIMIT_LOCK_SLOTS > CONNLIMIT_SLOTS);
445 BUILD_BUG_ON((CONNLIMIT_SLOTS % CONNLIMIT_LOCK_SLOTS) != 0);
446
Florian Westphale00b437b2014-03-20 11:53:39 +0100447 for (i = 0; i < CONNLIMIT_LOCK_SLOTS; ++i)
448 spin_lock_init(&xt_connlimit_locks[i]);
449
Florian Westphal14e1a972014-03-07 14:37:12 +0100450 connlimit_conn_cachep = kmem_cache_create("xt_connlimit_conn",
451 sizeof(struct xt_connlimit_conn),
452 0, 0, NULL);
453 if (!connlimit_conn_cachep)
454 return -ENOMEM;
455
Florian Westphal7d084872014-03-12 23:49:51 +0100456 connlimit_rb_cachep = kmem_cache_create("xt_connlimit_rb",
457 sizeof(struct xt_connlimit_rb),
458 0, 0, NULL);
459 if (!connlimit_rb_cachep) {
Florian Westphal14e1a972014-03-07 14:37:12 +0100460 kmem_cache_destroy(connlimit_conn_cachep);
Florian Westphal7d084872014-03-12 23:49:51 +0100461 return -ENOMEM;
462 }
463 ret = xt_register_match(&connlimit_mt_reg);
464 if (ret != 0) {
465 kmem_cache_destroy(connlimit_conn_cachep);
466 kmem_cache_destroy(connlimit_rb_cachep);
467 }
Florian Westphal14e1a972014-03-07 14:37:12 +0100468 return ret;
Jan Engelhardt370786f2007-07-14 20:47:26 -0700469}
470
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800471static void __exit connlimit_mt_exit(void)
Jan Engelhardt370786f2007-07-14 20:47:26 -0700472{
Cong Wang68c07cb2012-05-19 04:39:01 +0000473 xt_unregister_match(&connlimit_mt_reg);
Florian Westphal14e1a972014-03-07 14:37:12 +0100474 kmem_cache_destroy(connlimit_conn_cachep);
Florian Westphal7d084872014-03-12 23:49:51 +0100475 kmem_cache_destroy(connlimit_rb_cachep);
Jan Engelhardt370786f2007-07-14 20:47:26 -0700476}
477
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800478module_init(connlimit_mt_init);
479module_exit(connlimit_mt_exit);
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +0200480MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -0800481MODULE_DESCRIPTION("Xtables: Number of connections matching");
Jan Engelhardt370786f2007-07-14 20:47:26 -0700482MODULE_LICENSE("GPL");
483MODULE_ALIAS("ipt_connlimit");
484MODULE_ALIAS("ip6t_connlimit");