netfilter: connlimit: use keyed locks
[deliverable/linux.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
JE
21#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>
5d0aa2cc 32#include <net/netfilter/nf_conntrack_zones.h>
370786f9 33
1442e750
FW
34#define CONNLIMIT_SLOTS 256
35#define CONNLIMIT_LOCK_SLOTS 32
36
370786f9
JE
37/* we will save the tuples of all connections we care about */
38struct xt_connlimit_conn {
3e0d5149 39 struct hlist_node node;
8183e3a8
CG
40 struct nf_conntrack_tuple tuple;
41 union nf_inet_addr addr;
370786f9
JE
42};
43
44struct xt_connlimit_data {
1442e750
FW
45 struct hlist_head iphash[CONNLIMIT_SLOTS];
46 spinlock_t locks[CONNLIMIT_LOCK_SLOTS];
370786f9
JE
47};
48
294188ae 49static u_int32_t connlimit_rnd __read_mostly;
14e1a977 50static struct kmem_cache *connlimit_conn_cachep __read_mostly;
370786f9 51
a34c4589 52static inline unsigned int connlimit_iphash(__be32 addr)
370786f9 53{
1442e750
FW
54 return jhash_1word((__force __u32)addr,
55 connlimit_rnd) % CONNLIMIT_SLOTS;
370786f9
JE
56}
57
58static inline unsigned int
643a2c15
JE
59connlimit_iphash6(const union nf_inet_addr *addr,
60 const union nf_inet_addr *mask)
370786f9 61{
643a2c15 62 union nf_inet_addr res;
370786f9
JE
63 unsigned int i;
64
370786f9
JE
65 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i)
66 res.ip6[i] = addr->ip6[i] & mask->ip6[i];
67
1442e750
FW
68 return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6),
69 connlimit_rnd) % CONNLIMIT_SLOTS;
370786f9
JE
70}
71
72static inline bool already_closed(const struct nf_conn *conn)
73{
5e8fbe2a 74 if (nf_ct_protonum(conn) == IPPROTO_TCP)
d2ee3f2c
DW
75 return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
76 conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
370786f9
JE
77 else
78 return 0;
79}
80
81static inline unsigned int
643a2c15
JE
82same_source_net(const union nf_inet_addr *addr,
83 const union nf_inet_addr *mask,
76108cea 84 const union nf_inet_addr *u3, u_int8_t family)
370786f9 85{
ee999d8b 86 if (family == NFPROTO_IPV4) {
370786f9
JE
87 return (addr->ip & mask->ip) == (u3->ip & mask->ip);
88 } else {
643a2c15 89 union nf_inet_addr lh, rh;
370786f9
JE
90 unsigned int i;
91
92 for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) {
93 lh.ip6[i] = addr->ip6[i] & mask->ip6[i];
94 rh.ip6[i] = u3->ip6[i] & mask->ip6[i];
95 }
96
97 return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6)) == 0;
98 }
99}
100
15cfd528
FW
101static int count_hlist(struct net *net,
102 struct hlist_head *head,
103 const struct nf_conntrack_tuple *tuple,
104 const union nf_inet_addr *addr,
105 const union nf_inet_addr *mask,
3bcc5fdf 106 u_int8_t family, bool *addit)
370786f9 107{
3cf93c96 108 const struct nf_conntrack_tuple_hash *found;
370786f9 109 struct xt_connlimit_conn *conn;
b67bfe0d 110 struct hlist_node *n;
ea781f19 111 struct nf_conn *found_ct;
370786f9
JE
112 int matches = 0;
113
76507f69 114 rcu_read_lock();
370786f9
JE
115
116 /* check the saved connections */
15cfd528 117 hlist_for_each_entry_safe(conn, n, head, node) {
5d0aa2cc
PM
118 found = nf_conntrack_find_get(net, NF_CT_DEFAULT_ZONE,
119 &conn->tuple);
d9ec4f1e
FW
120 if (found == NULL) {
121 hlist_del(&conn->node);
14e1a977 122 kmem_cache_free(connlimit_conn_cachep, conn);
d9ec4f1e
FW
123 continue;
124 }
370786f9 125
d9ec4f1e 126 found_ct = nf_ct_tuplehash_to_ctrack(found);
370786f9 127
d9ec4f1e 128 if (nf_ct_tuple_equal(&conn->tuple, tuple)) {
370786f9
JE
129 /*
130 * Just to be sure we have it only once in the list.
131 * We should not see tuples twice unless someone hooks
132 * this into a table without "-p tcp --syn".
133 */
3bcc5fdf 134 *addit = false;
d9ec4f1e 135 } else if (already_closed(found_ct)) {
370786f9
JE
136 /*
137 * we do not care about connections which are
138 * closed already -> ditch it
139 */
ea781f19 140 nf_ct_put(found_ct);
3e0d5149 141 hlist_del(&conn->node);
14e1a977 142 kmem_cache_free(connlimit_conn_cachep, conn);
370786f9
JE
143 continue;
144 }
145
8183e3a8 146 if (same_source_net(addr, mask, &conn->addr, family))
370786f9
JE
147 /* same source network -> be counted! */
148 ++matches;
ea781f19 149 nf_ct_put(found_ct);
370786f9
JE
150 }
151
76507f69 152 rcu_read_unlock();
370786f9 153
370786f9
JE
154 return matches;
155}
156
3bcc5fdf
FW
157static bool add_hlist(struct hlist_head *head,
158 const struct nf_conntrack_tuple *tuple,
159 const union nf_inet_addr *addr)
160{
14e1a977
FW
161 struct xt_connlimit_conn *conn;
162
163 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
3bcc5fdf
FW
164 if (conn == NULL)
165 return false;
166 conn->tuple = *tuple;
167 conn->addr = *addr;
168 hlist_add_head(&conn->node, head);
169 return true;
170}
171
15cfd528
FW
172static int count_them(struct net *net,
173 struct xt_connlimit_data *data,
174 const struct nf_conntrack_tuple *tuple,
175 const union nf_inet_addr *addr,
176 const union nf_inet_addr *mask,
177 u_int8_t family)
178{
179 struct hlist_head *hhead;
180 int count;
181 u32 hash;
3bcc5fdf 182 bool addit = true;
15cfd528
FW
183
184 if (family == NFPROTO_IPV6)
185 hash = connlimit_iphash6(addr, mask);
186 else
187 hash = connlimit_iphash(addr->ip & mask->ip);
188
189 hhead = &data->iphash[hash];
190
1442e750 191 spin_lock_bh(&data->locks[hash % CONNLIMIT_LOCK_SLOTS]);
3bcc5fdf
FW
192 count = count_hlist(net, hhead, tuple, addr, mask, family, &addit);
193 if (addit) {
194 if (add_hlist(hhead, tuple, addr))
195 count++;
196 else
197 count = -ENOMEM;
198 }
1442e750 199 spin_unlock_bh(&data->locks[hash % CONNLIMIT_LOCK_SLOTS]);
15cfd528
FW
200
201 return count;
202}
203
d3c5ee6d 204static bool
62fc8051 205connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
370786f9 206{
83fc8102 207 struct net *net = dev_net(par->in ? par->in : par->out);
f7108a20 208 const struct xt_connlimit_info *info = par->matchinfo;
22c2d8bc 209 union nf_inet_addr addr;
370786f9
JE
210 struct nf_conntrack_tuple tuple;
211 const struct nf_conntrack_tuple *tuple_ptr = &tuple;
212 enum ip_conntrack_info ctinfo;
213 const struct nf_conn *ct;
214 int connections;
215
216 ct = nf_ct_get(skb, &ctinfo);
8183e3a8
CG
217 if (ct != NULL)
218 tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
219 else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
220 par->family, &tuple))
370786f9
JE
221 goto hotdrop;
222
92f3b2b1 223 if (par->family == NFPROTO_IPV6) {
370786f9 224 const struct ipv6hdr *iph = ipv6_hdr(skb);
cc4fc022
JE
225 memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
226 &iph->daddr : &iph->saddr, sizeof(addr.ip6));
370786f9
JE
227 } else {
228 const struct iphdr *iph = ip_hdr(skb);
cc4fc022
JE
229 addr.ip = (info->flags & XT_CONNLIMIT_DADDR) ?
230 iph->daddr : iph->saddr;
370786f9
JE
231 }
232
83fc8102 233 connections = count_them(net, info->data, tuple_ptr, &addr,
20b7975e 234 &info->mask, par->family);
1cc34c30 235 if (connections < 0)
370786f9 236 /* kmalloc failed, drop it entirely */
1cc34c30 237 goto hotdrop;
370786f9 238
cc4fc022
JE
239 return (connections > info->limit) ^
240 !!(info->flags & XT_CONNLIMIT_INVERT);
370786f9
JE
241
242 hotdrop:
b4ba2611 243 par->hotdrop = true;
370786f9
JE
244 return false;
245}
246
b0f38452 247static int connlimit_mt_check(const struct xt_mtchk_param *par)
370786f9 248{
9b4fce7a 249 struct xt_connlimit_info *info = par->matchinfo;
370786f9 250 unsigned int i;
4a5a5c73 251 int ret;
370786f9 252
4656c4d6
CG
253 if (unlikely(!connlimit_rnd)) {
254 u_int32_t rand;
255
256 do {
257 get_random_bytes(&rand, sizeof(rand));
258 } while (!rand);
259 cmpxchg(&connlimit_rnd, 0, rand);
294188ae 260 }
4a5a5c73
JE
261 ret = nf_ct_l3proto_try_module_get(par->family);
262 if (ret < 0) {
8bee4bad
JE
263 pr_info("cannot load conntrack support for "
264 "address family %u\n", par->family);
4a5a5c73 265 return ret;
370786f9
JE
266 }
267
268 /* init private data */
269 info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
270 if (info->data == NULL) {
92f3b2b1 271 nf_ct_l3proto_module_put(par->family);
4a5a5c73 272 return -ENOMEM;
370786f9
JE
273 }
274
1442e750
FW
275 for (i = 0; i < ARRAY_SIZE(info->data->locks); ++i)
276 spin_lock_init(&info->data->locks[i]);
277
370786f9 278 for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i)
3e0d5149 279 INIT_HLIST_HEAD(&info->data->iphash[i]);
370786f9 280
bd414ee6 281 return 0;
370786f9
JE
282}
283
6be3d859 284static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
370786f9 285{
6be3d859 286 const struct xt_connlimit_info *info = par->matchinfo;
370786f9 287 struct xt_connlimit_conn *conn;
b67bfe0d 288 struct hlist_node *n;
3e0d5149 289 struct hlist_head *hash = info->data->iphash;
370786f9
JE
290 unsigned int i;
291
92f3b2b1 292 nf_ct_l3proto_module_put(par->family);
370786f9
JE
293
294 for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i) {
b67bfe0d 295 hlist_for_each_entry_safe(conn, n, &hash[i], node) {
3e0d5149 296 hlist_del(&conn->node);
14e1a977 297 kmem_cache_free(connlimit_conn_cachep, conn);
370786f9
JE
298 }
299 }
300
301 kfree(info->data);
302}
303
68c07cb6
CW
304static struct xt_match connlimit_mt_reg __read_mostly = {
305 .name = "connlimit",
306 .revision = 1,
307 .family = NFPROTO_UNSPEC,
308 .checkentry = connlimit_mt_check,
309 .match = connlimit_mt,
310 .matchsize = sizeof(struct xt_connlimit_info),
311 .destroy = connlimit_mt_destroy,
312 .me = THIS_MODULE,
370786f9
JE
313};
314
d3c5ee6d 315static int __init connlimit_mt_init(void)
370786f9 316{
14e1a977 317 int ret;
1442e750
FW
318
319 BUILD_BUG_ON(CONNLIMIT_LOCK_SLOTS > CONNLIMIT_SLOTS);
320 BUILD_BUG_ON((CONNLIMIT_SLOTS % CONNLIMIT_LOCK_SLOTS) != 0);
321
14e1a977
FW
322 connlimit_conn_cachep = kmem_cache_create("xt_connlimit_conn",
323 sizeof(struct xt_connlimit_conn),
324 0, 0, NULL);
325 if (!connlimit_conn_cachep)
326 return -ENOMEM;
327
328 ret = xt_register_match(&connlimit_mt_reg);
329 if (ret != 0)
330 kmem_cache_destroy(connlimit_conn_cachep);
331 return ret;
370786f9
JE
332}
333
d3c5ee6d 334static void __exit connlimit_mt_exit(void)
370786f9 335{
68c07cb6 336 xt_unregister_match(&connlimit_mt_reg);
14e1a977 337 kmem_cache_destroy(connlimit_conn_cachep);
370786f9
JE
338}
339
d3c5ee6d
JE
340module_init(connlimit_mt_init);
341module_exit(connlimit_mt_exit);
92f3b2b1 342MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
2ae15b64 343MODULE_DESCRIPTION("Xtables: Number of connections matching");
370786f9
JE
344MODULE_LICENSE("GPL");
345MODULE_ALIAS("ipt_connlimit");
346MODULE_ALIAS("ip6t_connlimit");
This page took 0.713167 seconds and 5 git commands to generate.