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