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