]>
Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* Connection state tracking for netfilter. This is separated from, |
2 | but required by, the NAT layer; it can also be used by an iptables | |
3 | extension. */ | |
4 | ||
5 | /* (C) 1999-2001 Paul `Rusty' Russell | |
dc808fe2 | 6 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
9fb9cbb1 YK |
7 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
9fb9cbb1 YK |
12 | */ |
13 | ||
9fb9cbb1 YK |
14 | #include <linux/types.h> |
15 | #include <linux/netfilter.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/skbuff.h> | |
18 | #include <linux/proc_fs.h> | |
19 | #include <linux/vmalloc.h> | |
20 | #include <linux/stddef.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/random.h> | |
23 | #include <linux/jhash.h> | |
24 | #include <linux/err.h> | |
25 | #include <linux/percpu.h> | |
26 | #include <linux/moduleparam.h> | |
27 | #include <linux/notifier.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/netdevice.h> | |
30 | #include <linux/socket.h> | |
d7fe0f24 | 31 | #include <linux/mm.h> |
9fb9cbb1 | 32 | |
9fb9cbb1 YK |
33 | #include <net/netfilter/nf_conntrack.h> |
34 | #include <net/netfilter/nf_conntrack_l3proto.h> | |
605dcad6 | 35 | #include <net/netfilter/nf_conntrack_l4proto.h> |
77ab9cff | 36 | #include <net/netfilter/nf_conntrack_expect.h> |
9fb9cbb1 YK |
37 | #include <net/netfilter/nf_conntrack_helper.h> |
38 | #include <net/netfilter/nf_conntrack_core.h> | |
ecfab2c9 | 39 | #include <net/netfilter/nf_conntrack_extend.h> |
9fb9cbb1 | 40 | |
dc808fe2 | 41 | #define NF_CONNTRACK_VERSION "0.5.0" |
9fb9cbb1 YK |
42 | |
43 | #if 0 | |
44 | #define DEBUGP printk | |
45 | #else | |
46 | #define DEBUGP(format, args...) | |
47 | #endif | |
48 | ||
49 | DEFINE_RWLOCK(nf_conntrack_lock); | |
13b18339 | 50 | EXPORT_SYMBOL_GPL(nf_conntrack_lock); |
9fb9cbb1 YK |
51 | |
52 | /* nf_conntrack_standalone needs this */ | |
53 | atomic_t nf_conntrack_count = ATOMIC_INIT(0); | |
a999e683 | 54 | EXPORT_SYMBOL_GPL(nf_conntrack_count); |
9fb9cbb1 | 55 | |
e2b7606c | 56 | unsigned int nf_conntrack_htable_size __read_mostly; |
13b18339 PM |
57 | EXPORT_SYMBOL_GPL(nf_conntrack_htable_size); |
58 | ||
94aec08e | 59 | int nf_conntrack_max __read_mostly; |
a999e683 | 60 | EXPORT_SYMBOL_GPL(nf_conntrack_max); |
13b18339 | 61 | |
f205c5e0 | 62 | struct hlist_head *nf_conntrack_hash __read_mostly; |
13b18339 PM |
63 | EXPORT_SYMBOL_GPL(nf_conntrack_hash); |
64 | ||
e2b7606c | 65 | struct nf_conn nf_conntrack_untracked __read_mostly; |
13b18339 PM |
66 | EXPORT_SYMBOL_GPL(nf_conntrack_untracked); |
67 | ||
94aec08e | 68 | unsigned int nf_ct_log_invalid __read_mostly; |
f205c5e0 | 69 | HLIST_HEAD(unconfirmed); |
1192e403 | 70 | static int nf_conntrack_vmalloc __read_mostly; |
dacd2a1a | 71 | static struct kmem_cache *nf_conntrack_cachep __read_mostly; |
4e3882f7 | 72 | static unsigned int nf_conntrack_next_id; |
77ab9cff | 73 | |
9fb9cbb1 YK |
74 | DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat); |
75 | EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat); | |
76 | ||
9fb9cbb1 YK |
77 | static int nf_conntrack_hash_rnd_initted; |
78 | static unsigned int nf_conntrack_hash_rnd; | |
79 | ||
80 | static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple, | |
81 | unsigned int size, unsigned int rnd) | |
82 | { | |
83 | unsigned int a, b; | |
9b887909 SF |
84 | |
85 | a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all), | |
86 | (tuple->src.l3num << 16) | tuple->dst.protonum); | |
87 | b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all), | |
88 | (tuple->src.u.all << 16) | tuple->dst.u.all); | |
9fb9cbb1 YK |
89 | |
90 | return jhash_2words(a, b, rnd) % size; | |
91 | } | |
92 | ||
93 | static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple) | |
94 | { | |
95 | return __hash_conntrack(tuple, nf_conntrack_htable_size, | |
96 | nf_conntrack_hash_rnd); | |
97 | } | |
98 | ||
9fb9cbb1 YK |
99 | int |
100 | nf_ct_get_tuple(const struct sk_buff *skb, | |
101 | unsigned int nhoff, | |
102 | unsigned int dataoff, | |
103 | u_int16_t l3num, | |
104 | u_int8_t protonum, | |
105 | struct nf_conntrack_tuple *tuple, | |
106 | const struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 107 | const struct nf_conntrack_l4proto *l4proto) |
9fb9cbb1 YK |
108 | { |
109 | NF_CT_TUPLE_U_BLANK(tuple); | |
110 | ||
111 | tuple->src.l3num = l3num; | |
112 | if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0) | |
113 | return 0; | |
114 | ||
115 | tuple->dst.protonum = protonum; | |
116 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
117 | ||
605dcad6 | 118 | return l4proto->pkt_to_tuple(skb, dataoff, tuple); |
9fb9cbb1 | 119 | } |
13b18339 | 120 | EXPORT_SYMBOL_GPL(nf_ct_get_tuple); |
9fb9cbb1 YK |
121 | |
122 | int | |
123 | nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse, | |
124 | const struct nf_conntrack_tuple *orig, | |
125 | const struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 126 | const struct nf_conntrack_l4proto *l4proto) |
9fb9cbb1 YK |
127 | { |
128 | NF_CT_TUPLE_U_BLANK(inverse); | |
129 | ||
130 | inverse->src.l3num = orig->src.l3num; | |
131 | if (l3proto->invert_tuple(inverse, orig) == 0) | |
132 | return 0; | |
133 | ||
134 | inverse->dst.dir = !orig->dst.dir; | |
135 | ||
136 | inverse->dst.protonum = orig->dst.protonum; | |
605dcad6 | 137 | return l4proto->invert_tuple(inverse, orig); |
9fb9cbb1 | 138 | } |
13b18339 | 139 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuple); |
9fb9cbb1 | 140 | |
9fb9cbb1 YK |
141 | static void |
142 | clean_from_lists(struct nf_conn *ct) | |
143 | { | |
9fb9cbb1 | 144 | DEBUGP("clean_from_lists(%p)\n", ct); |
f205c5e0 PM |
145 | hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode); |
146 | hlist_del(&ct->tuplehash[IP_CT_DIR_REPLY].hnode); | |
9fb9cbb1 YK |
147 | |
148 | /* Destroy all pending expectations */ | |
c1d10adb | 149 | nf_ct_remove_expectations(ct); |
9fb9cbb1 YK |
150 | } |
151 | ||
152 | static void | |
153 | destroy_conntrack(struct nf_conntrack *nfct) | |
154 | { | |
155 | struct nf_conn *ct = (struct nf_conn *)nfct; | |
605dcad6 | 156 | struct nf_conntrack_l4proto *l4proto; |
9fb9cbb1 YK |
157 | |
158 | DEBUGP("destroy_conntrack(%p)\n", ct); | |
159 | NF_CT_ASSERT(atomic_read(&nfct->use) == 0); | |
160 | NF_CT_ASSERT(!timer_pending(&ct->timeout)); | |
161 | ||
162 | nf_conntrack_event(IPCT_DESTROY, ct); | |
163 | set_bit(IPS_DYING_BIT, &ct->status); | |
164 | ||
165 | /* To make sure we don't get any weird locking issues here: | |
166 | * destroy_conntrack() MUST NOT be called with a write lock | |
167 | * to nf_conntrack_lock!!! -HW */ | |
923f4902 | 168 | rcu_read_lock(); |
923f4902 PM |
169 | l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num, |
170 | ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum); | |
605dcad6 MJ |
171 | if (l4proto && l4proto->destroy) |
172 | l4proto->destroy(ct); | |
9fb9cbb1 | 173 | |
ecfab2c9 YK |
174 | nf_ct_ext_destroy(ct); |
175 | ||
982d9a9c | 176 | rcu_read_unlock(); |
9fb9cbb1 YK |
177 | |
178 | write_lock_bh(&nf_conntrack_lock); | |
179 | /* Expectations will have been removed in clean_from_lists, | |
180 | * except TFTP can create an expectation on the first packet, | |
181 | * before connection is in the list, so we need to clean here, | |
182 | * too. */ | |
c1d10adb | 183 | nf_ct_remove_expectations(ct); |
9fb9cbb1 YK |
184 | |
185 | /* We overload first tuple to link into unconfirmed list. */ | |
186 | if (!nf_ct_is_confirmed(ct)) { | |
f205c5e0 PM |
187 | BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode)); |
188 | hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode); | |
9fb9cbb1 YK |
189 | } |
190 | ||
191 | NF_CT_STAT_INC(delete); | |
192 | write_unlock_bh(&nf_conntrack_lock); | |
193 | ||
194 | if (ct->master) | |
195 | nf_ct_put(ct->master); | |
196 | ||
197 | DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct); | |
198 | nf_conntrack_free(ct); | |
199 | } | |
200 | ||
201 | static void death_by_timeout(unsigned long ul_conntrack) | |
202 | { | |
203 | struct nf_conn *ct = (void *)ul_conntrack; | |
5397e97d | 204 | struct nf_conn_help *help = nfct_help(ct); |
3c158f7f | 205 | struct nf_conntrack_helper *helper; |
5397e97d | 206 | |
3c158f7f PM |
207 | if (help) { |
208 | rcu_read_lock(); | |
209 | helper = rcu_dereference(help->helper); | |
210 | if (helper && helper->destroy) | |
211 | helper->destroy(ct); | |
212 | rcu_read_unlock(); | |
213 | } | |
9fb9cbb1 YK |
214 | |
215 | write_lock_bh(&nf_conntrack_lock); | |
216 | /* Inside lock so preempt is disabled on module removal path. | |
217 | * Otherwise we can get spurious warnings. */ | |
218 | NF_CT_STAT_INC(delete_list); | |
219 | clean_from_lists(ct); | |
220 | write_unlock_bh(&nf_conntrack_lock); | |
221 | nf_ct_put(ct); | |
222 | } | |
223 | ||
c1d10adb | 224 | struct nf_conntrack_tuple_hash * |
9fb9cbb1 YK |
225 | __nf_conntrack_find(const struct nf_conntrack_tuple *tuple, |
226 | const struct nf_conn *ignored_conntrack) | |
227 | { | |
228 | struct nf_conntrack_tuple_hash *h; | |
f205c5e0 | 229 | struct hlist_node *n; |
9fb9cbb1 YK |
230 | unsigned int hash = hash_conntrack(tuple); |
231 | ||
f205c5e0 | 232 | hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode) { |
df0933dc PM |
233 | if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack && |
234 | nf_ct_tuple_equal(tuple, &h->tuple)) { | |
9fb9cbb1 YK |
235 | NF_CT_STAT_INC(found); |
236 | return h; | |
237 | } | |
238 | NF_CT_STAT_INC(searched); | |
239 | } | |
240 | ||
241 | return NULL; | |
242 | } | |
13b18339 | 243 | EXPORT_SYMBOL_GPL(__nf_conntrack_find); |
9fb9cbb1 YK |
244 | |
245 | /* Find a connection corresponding to a tuple. */ | |
246 | struct nf_conntrack_tuple_hash * | |
330f7db5 | 247 | nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple) |
9fb9cbb1 YK |
248 | { |
249 | struct nf_conntrack_tuple_hash *h; | |
250 | ||
251 | read_lock_bh(&nf_conntrack_lock); | |
330f7db5 | 252 | h = __nf_conntrack_find(tuple, NULL); |
9fb9cbb1 YK |
253 | if (h) |
254 | atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use); | |
255 | read_unlock_bh(&nf_conntrack_lock); | |
256 | ||
257 | return h; | |
258 | } | |
13b18339 | 259 | EXPORT_SYMBOL_GPL(nf_conntrack_find_get); |
9fb9cbb1 | 260 | |
c1d10adb PNA |
261 | static void __nf_conntrack_hash_insert(struct nf_conn *ct, |
262 | unsigned int hash, | |
601e68e1 | 263 | unsigned int repl_hash) |
c1d10adb PNA |
264 | { |
265 | ct->id = ++nf_conntrack_next_id; | |
f205c5e0 PM |
266 | hlist_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode, |
267 | &nf_conntrack_hash[hash]); | |
268 | hlist_add_head(&ct->tuplehash[IP_CT_DIR_REPLY].hnode, | |
269 | &nf_conntrack_hash[repl_hash]); | |
c1d10adb PNA |
270 | } |
271 | ||
272 | void nf_conntrack_hash_insert(struct nf_conn *ct) | |
273 | { | |
274 | unsigned int hash, repl_hash; | |
275 | ||
276 | hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); | |
277 | repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple); | |
278 | ||
279 | write_lock_bh(&nf_conntrack_lock); | |
280 | __nf_conntrack_hash_insert(ct, hash, repl_hash); | |
281 | write_unlock_bh(&nf_conntrack_lock); | |
282 | } | |
13b18339 | 283 | EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert); |
c1d10adb | 284 | |
9fb9cbb1 YK |
285 | /* Confirm a connection given skb; places it in hash table */ |
286 | int | |
287 | __nf_conntrack_confirm(struct sk_buff **pskb) | |
288 | { | |
289 | unsigned int hash, repl_hash; | |
df0933dc | 290 | struct nf_conntrack_tuple_hash *h; |
9fb9cbb1 | 291 | struct nf_conn *ct; |
df0933dc | 292 | struct nf_conn_help *help; |
f205c5e0 | 293 | struct hlist_node *n; |
9fb9cbb1 YK |
294 | enum ip_conntrack_info ctinfo; |
295 | ||
296 | ct = nf_ct_get(*pskb, &ctinfo); | |
297 | ||
298 | /* ipt_REJECT uses nf_conntrack_attach to attach related | |
299 | ICMP/TCP RST packets in other direction. Actual packet | |
300 | which created connection will be IP_CT_NEW or for an | |
301 | expected connection, IP_CT_RELATED. */ | |
302 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) | |
303 | return NF_ACCEPT; | |
304 | ||
305 | hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); | |
306 | repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple); | |
307 | ||
308 | /* We're not in hash table, and we refuse to set up related | |
309 | connections for unconfirmed conns. But packet copies and | |
310 | REJECT will give spurious warnings here. */ | |
311 | /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */ | |
312 | ||
313 | /* No external references means noone else could have | |
314 | confirmed us. */ | |
315 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); | |
316 | DEBUGP("Confirming conntrack %p\n", ct); | |
317 | ||
318 | write_lock_bh(&nf_conntrack_lock); | |
319 | ||
320 | /* See if there's one in the list already, including reverse: | |
321 | NAT could have grabbed it without realizing, since we're | |
322 | not in the hash. If there is, we lost race. */ | |
f205c5e0 | 323 | hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode) |
df0933dc PM |
324 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
325 | &h->tuple)) | |
326 | goto out; | |
f205c5e0 | 327 | hlist_for_each_entry(h, n, &nf_conntrack_hash[repl_hash], hnode) |
df0933dc PM |
328 | if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple, |
329 | &h->tuple)) | |
330 | goto out; | |
9fb9cbb1 | 331 | |
df0933dc | 332 | /* Remove from unconfirmed list */ |
f205c5e0 | 333 | hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode); |
df0933dc PM |
334 | |
335 | __nf_conntrack_hash_insert(ct, hash, repl_hash); | |
336 | /* Timer relative to confirmation time, not original | |
337 | setting time, otherwise we'd get timer wrap in | |
338 | weird delay cases. */ | |
339 | ct->timeout.expires += jiffies; | |
340 | add_timer(&ct->timeout); | |
341 | atomic_inc(&ct->ct_general.use); | |
342 | set_bit(IPS_CONFIRMED_BIT, &ct->status); | |
343 | NF_CT_STAT_INC(insert); | |
344 | write_unlock_bh(&nf_conntrack_lock); | |
345 | help = nfct_help(ct); | |
346 | if (help && help->helper) | |
347 | nf_conntrack_event_cache(IPCT_HELPER, *pskb); | |
9fb9cbb1 | 348 | #ifdef CONFIG_NF_NAT_NEEDED |
df0933dc PM |
349 | if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) || |
350 | test_bit(IPS_DST_NAT_DONE_BIT, &ct->status)) | |
351 | nf_conntrack_event_cache(IPCT_NATINFO, *pskb); | |
9fb9cbb1 | 352 | #endif |
df0933dc PM |
353 | nf_conntrack_event_cache(master_ct(ct) ? |
354 | IPCT_RELATED : IPCT_NEW, *pskb); | |
355 | return NF_ACCEPT; | |
9fb9cbb1 | 356 | |
df0933dc | 357 | out: |
9fb9cbb1 YK |
358 | NF_CT_STAT_INC(insert_failed); |
359 | write_unlock_bh(&nf_conntrack_lock); | |
360 | return NF_DROP; | |
361 | } | |
13b18339 | 362 | EXPORT_SYMBOL_GPL(__nf_conntrack_confirm); |
9fb9cbb1 YK |
363 | |
364 | /* Returns true if a connection correspondings to the tuple (required | |
365 | for NAT). */ | |
366 | int | |
367 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, | |
368 | const struct nf_conn *ignored_conntrack) | |
369 | { | |
370 | struct nf_conntrack_tuple_hash *h; | |
371 | ||
372 | read_lock_bh(&nf_conntrack_lock); | |
373 | h = __nf_conntrack_find(tuple, ignored_conntrack); | |
374 | read_unlock_bh(&nf_conntrack_lock); | |
375 | ||
376 | return h != NULL; | |
377 | } | |
13b18339 | 378 | EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken); |
9fb9cbb1 YK |
379 | |
380 | /* There's a small race here where we may free a just-assured | |
381 | connection. Too bad: we're in trouble anyway. */ | |
f205c5e0 | 382 | static int early_drop(struct hlist_head *chain) |
9fb9cbb1 | 383 | { |
f205c5e0 | 384 | /* Use oldest entry, which is roughly LRU */ |
9fb9cbb1 | 385 | struct nf_conntrack_tuple_hash *h; |
df0933dc | 386 | struct nf_conn *ct = NULL, *tmp; |
f205c5e0 | 387 | struct hlist_node *n; |
9fb9cbb1 YK |
388 | int dropped = 0; |
389 | ||
390 | read_lock_bh(&nf_conntrack_lock); | |
f205c5e0 | 391 | hlist_for_each_entry(h, n, chain, hnode) { |
df0933dc | 392 | tmp = nf_ct_tuplehash_to_ctrack(h); |
f205c5e0 | 393 | if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) |
df0933dc | 394 | ct = tmp; |
9fb9cbb1 | 395 | } |
f205c5e0 PM |
396 | if (ct) |
397 | atomic_inc(&ct->ct_general.use); | |
9fb9cbb1 YK |
398 | read_unlock_bh(&nf_conntrack_lock); |
399 | ||
400 | if (!ct) | |
401 | return dropped; | |
402 | ||
403 | if (del_timer(&ct->timeout)) { | |
404 | death_by_timeout((unsigned long)ct); | |
405 | dropped = 1; | |
c0e912d7 | 406 | NF_CT_STAT_INC_ATOMIC(early_drop); |
9fb9cbb1 YK |
407 | } |
408 | nf_ct_put(ct); | |
409 | return dropped; | |
410 | } | |
411 | ||
dacd2a1a YK |
412 | struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig, |
413 | const struct nf_conntrack_tuple *repl) | |
9fb9cbb1 YK |
414 | { |
415 | struct nf_conn *conntrack = NULL; | |
9fb9cbb1 | 416 | |
dc808fe2 | 417 | if (unlikely(!nf_conntrack_hash_rnd_initted)) { |
9fb9cbb1 YK |
418 | get_random_bytes(&nf_conntrack_hash_rnd, 4); |
419 | nf_conntrack_hash_rnd_initted = 1; | |
420 | } | |
421 | ||
5251e2d2 PNA |
422 | /* We don't want any race condition at early drop stage */ |
423 | atomic_inc(&nf_conntrack_count); | |
424 | ||
9fb9cbb1 | 425 | if (nf_conntrack_max |
5251e2d2 | 426 | && atomic_read(&nf_conntrack_count) > nf_conntrack_max) { |
9fb9cbb1 YK |
427 | unsigned int hash = hash_conntrack(orig); |
428 | /* Try dropping from this hash chain. */ | |
429 | if (!early_drop(&nf_conntrack_hash[hash])) { | |
5251e2d2 | 430 | atomic_dec(&nf_conntrack_count); |
9fb9cbb1 YK |
431 | if (net_ratelimit()) |
432 | printk(KERN_WARNING | |
433 | "nf_conntrack: table full, dropping" | |
434 | " packet.\n"); | |
435 | return ERR_PTR(-ENOMEM); | |
436 | } | |
437 | } | |
438 | ||
dacd2a1a | 439 | conntrack = kmem_cache_zalloc(nf_conntrack_cachep, GFP_ATOMIC); |
9fb9cbb1 | 440 | if (conntrack == NULL) { |
dacd2a1a YK |
441 | DEBUGP("nf_conntrack_alloc: Can't alloc conntrack.\n"); |
442 | atomic_dec(&nf_conntrack_count); | |
443 | return ERR_PTR(-ENOMEM); | |
9fb9cbb1 YK |
444 | } |
445 | ||
9fb9cbb1 | 446 | atomic_set(&conntrack->ct_general.use, 1); |
9fb9cbb1 YK |
447 | conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig; |
448 | conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl; | |
449 | /* Don't set timer yet: wait for confirmation */ | |
e6f689db PM |
450 | setup_timer(&conntrack->timeout, death_by_timeout, |
451 | (unsigned long)conntrack); | |
9fb9cbb1 | 452 | |
9fb9cbb1 YK |
453 | return conntrack; |
454 | } | |
13b18339 | 455 | EXPORT_SYMBOL_GPL(nf_conntrack_alloc); |
9fb9cbb1 YK |
456 | |
457 | void nf_conntrack_free(struct nf_conn *conntrack) | |
458 | { | |
ecfab2c9 | 459 | nf_ct_ext_free(conntrack); |
dacd2a1a | 460 | kmem_cache_free(nf_conntrack_cachep, conntrack); |
9fb9cbb1 YK |
461 | atomic_dec(&nf_conntrack_count); |
462 | } | |
13b18339 | 463 | EXPORT_SYMBOL_GPL(nf_conntrack_free); |
9fb9cbb1 YK |
464 | |
465 | /* Allocate a new conntrack: we return -ENOMEM if classification | |
466 | failed due to stress. Otherwise it really is unclassifiable. */ | |
467 | static struct nf_conntrack_tuple_hash * | |
468 | init_conntrack(const struct nf_conntrack_tuple *tuple, | |
469 | struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 470 | struct nf_conntrack_l4proto *l4proto, |
9fb9cbb1 YK |
471 | struct sk_buff *skb, |
472 | unsigned int dataoff) | |
473 | { | |
474 | struct nf_conn *conntrack; | |
3c158f7f | 475 | struct nf_conn_help *help; |
9fb9cbb1 YK |
476 | struct nf_conntrack_tuple repl_tuple; |
477 | struct nf_conntrack_expect *exp; | |
478 | ||
605dcad6 | 479 | if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) { |
9fb9cbb1 YK |
480 | DEBUGP("Can't invert tuple.\n"); |
481 | return NULL; | |
482 | } | |
483 | ||
dacd2a1a | 484 | conntrack = nf_conntrack_alloc(tuple, &repl_tuple); |
9fb9cbb1 YK |
485 | if (conntrack == NULL || IS_ERR(conntrack)) { |
486 | DEBUGP("Can't allocate conntrack.\n"); | |
487 | return (struct nf_conntrack_tuple_hash *)conntrack; | |
488 | } | |
489 | ||
605dcad6 | 490 | if (!l4proto->new(conntrack, skb, dataoff)) { |
9fb9cbb1 YK |
491 | nf_conntrack_free(conntrack); |
492 | DEBUGP("init conntrack: can't track with proto module\n"); | |
493 | return NULL; | |
494 | } | |
495 | ||
496 | write_lock_bh(&nf_conntrack_lock); | |
497 | exp = find_expectation(tuple); | |
9fb9cbb1 YK |
498 | if (exp) { |
499 | DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n", | |
500 | conntrack, exp); | |
501 | /* Welcome, Mr. Bond. We've been expecting you... */ | |
502 | __set_bit(IPS_EXPECTED_BIT, &conntrack->status); | |
503 | conntrack->master = exp->master; | |
ceceae1b YK |
504 | if (exp->helper) { |
505 | help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER, | |
506 | GFP_ATOMIC); | |
507 | if (help) | |
508 | rcu_assign_pointer(help->helper, exp->helper); | |
509 | else | |
510 | DEBUGP("failed to add helper extension area"); | |
511 | } | |
512 | ||
9fb9cbb1 YK |
513 | #ifdef CONFIG_NF_CONNTRACK_MARK |
514 | conntrack->mark = exp->master->mark; | |
7c9728c3 JM |
515 | #endif |
516 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
517 | conntrack->secmark = exp->master->secmark; | |
9fb9cbb1 YK |
518 | #endif |
519 | nf_conntrack_get(&conntrack->master->ct_general); | |
520 | NF_CT_STAT_INC(expect_new); | |
22e7410b | 521 | } else { |
ceceae1b YK |
522 | struct nf_conntrack_helper *helper; |
523 | ||
524 | helper = __nf_ct_helper_find(&repl_tuple); | |
525 | if (helper) { | |
526 | help = nf_ct_ext_add(conntrack, NF_CT_EXT_HELPER, | |
527 | GFP_ATOMIC); | |
528 | if (help) | |
529 | /* not in hash table yet, so not strictly | |
530 | necessary */ | |
531 | rcu_assign_pointer(help->helper, helper); | |
532 | else | |
533 | DEBUGP("failed to add helper extension area"); | |
3c158f7f | 534 | } |
9fb9cbb1 | 535 | NF_CT_STAT_INC(new); |
22e7410b | 536 | } |
9fb9cbb1 YK |
537 | |
538 | /* Overload tuple linked list to put us in unconfirmed list. */ | |
f205c5e0 PM |
539 | hlist_add_head(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].hnode, |
540 | &unconfirmed); | |
9fb9cbb1 YK |
541 | |
542 | write_unlock_bh(&nf_conntrack_lock); | |
543 | ||
544 | if (exp) { | |
545 | if (exp->expectfn) | |
546 | exp->expectfn(conntrack, exp); | |
547 | nf_conntrack_expect_put(exp); | |
548 | } | |
549 | ||
550 | return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL]; | |
551 | } | |
552 | ||
553 | /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */ | |
554 | static inline struct nf_conn * | |
555 | resolve_normal_ct(struct sk_buff *skb, | |
556 | unsigned int dataoff, | |
557 | u_int16_t l3num, | |
558 | u_int8_t protonum, | |
559 | struct nf_conntrack_l3proto *l3proto, | |
605dcad6 | 560 | struct nf_conntrack_l4proto *l4proto, |
9fb9cbb1 YK |
561 | int *set_reply, |
562 | enum ip_conntrack_info *ctinfo) | |
563 | { | |
564 | struct nf_conntrack_tuple tuple; | |
565 | struct nf_conntrack_tuple_hash *h; | |
566 | struct nf_conn *ct; | |
567 | ||
bbe735e4 | 568 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), |
9fb9cbb1 | 569 | dataoff, l3num, protonum, &tuple, l3proto, |
605dcad6 | 570 | l4proto)) { |
9fb9cbb1 YK |
571 | DEBUGP("resolve_normal_ct: Can't get tuple\n"); |
572 | return NULL; | |
573 | } | |
574 | ||
575 | /* look for tuple match */ | |
330f7db5 | 576 | h = nf_conntrack_find_get(&tuple); |
9fb9cbb1 | 577 | if (!h) { |
605dcad6 | 578 | h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff); |
9fb9cbb1 YK |
579 | if (!h) |
580 | return NULL; | |
581 | if (IS_ERR(h)) | |
582 | return (void *)h; | |
583 | } | |
584 | ct = nf_ct_tuplehash_to_ctrack(h); | |
585 | ||
586 | /* It exists; we have (non-exclusive) reference. */ | |
587 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) { | |
588 | *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY; | |
589 | /* Please set reply bit if this packet OK */ | |
590 | *set_reply = 1; | |
591 | } else { | |
592 | /* Once we've had two way comms, always ESTABLISHED. */ | |
593 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { | |
594 | DEBUGP("nf_conntrack_in: normal packet for %p\n", ct); | |
595 | *ctinfo = IP_CT_ESTABLISHED; | |
596 | } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) { | |
597 | DEBUGP("nf_conntrack_in: related packet for %p\n", ct); | |
598 | *ctinfo = IP_CT_RELATED; | |
599 | } else { | |
600 | DEBUGP("nf_conntrack_in: new packet for %p\n", ct); | |
601 | *ctinfo = IP_CT_NEW; | |
602 | } | |
603 | *set_reply = 0; | |
604 | } | |
605 | skb->nfct = &ct->ct_general; | |
606 | skb->nfctinfo = *ctinfo; | |
607 | return ct; | |
608 | } | |
609 | ||
610 | unsigned int | |
611 | nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb) | |
612 | { | |
613 | struct nf_conn *ct; | |
614 | enum ip_conntrack_info ctinfo; | |
615 | struct nf_conntrack_l3proto *l3proto; | |
605dcad6 | 616 | struct nf_conntrack_l4proto *l4proto; |
9fb9cbb1 YK |
617 | unsigned int dataoff; |
618 | u_int8_t protonum; | |
619 | int set_reply = 0; | |
620 | int ret; | |
621 | ||
622 | /* Previously seen (loopback or untracked)? Ignore. */ | |
623 | if ((*pskb)->nfct) { | |
c0e912d7 | 624 | NF_CT_STAT_INC_ATOMIC(ignore); |
9fb9cbb1 YK |
625 | return NF_ACCEPT; |
626 | } | |
627 | ||
923f4902 | 628 | /* rcu_read_lock()ed by nf_hook_slow */ |
c1d10adb | 629 | l3proto = __nf_ct_l3proto_find((u_int16_t)pf); |
923f4902 | 630 | |
9fb9cbb1 YK |
631 | if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) { |
632 | DEBUGP("not prepared to track yet or error occured\n"); | |
633 | return -ret; | |
634 | } | |
635 | ||
605dcad6 | 636 | l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum); |
9fb9cbb1 YK |
637 | |
638 | /* It may be an special packet, error, unclean... | |
639 | * inverse of the return code tells to the netfilter | |
640 | * core what to do with the packet. */ | |
605dcad6 MJ |
641 | if (l4proto->error != NULL && |
642 | (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) { | |
c0e912d7 PM |
643 | NF_CT_STAT_INC_ATOMIC(error); |
644 | NF_CT_STAT_INC_ATOMIC(invalid); | |
9fb9cbb1 YK |
645 | return -ret; |
646 | } | |
647 | ||
605dcad6 | 648 | ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto, |
9fb9cbb1 YK |
649 | &set_reply, &ctinfo); |
650 | if (!ct) { | |
651 | /* Not valid part of a connection */ | |
c0e912d7 | 652 | NF_CT_STAT_INC_ATOMIC(invalid); |
9fb9cbb1 YK |
653 | return NF_ACCEPT; |
654 | } | |
655 | ||
656 | if (IS_ERR(ct)) { | |
657 | /* Too stressed to deal. */ | |
c0e912d7 | 658 | NF_CT_STAT_INC_ATOMIC(drop); |
9fb9cbb1 YK |
659 | return NF_DROP; |
660 | } | |
661 | ||
662 | NF_CT_ASSERT((*pskb)->nfct); | |
663 | ||
605dcad6 | 664 | ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum); |
9fb9cbb1 YK |
665 | if (ret < 0) { |
666 | /* Invalid: inverse of the return code tells | |
667 | * the netfilter core what to do */ | |
668 | DEBUGP("nf_conntrack_in: Can't track with proto module\n"); | |
669 | nf_conntrack_put((*pskb)->nfct); | |
670 | (*pskb)->nfct = NULL; | |
c0e912d7 | 671 | NF_CT_STAT_INC_ATOMIC(invalid); |
9fb9cbb1 YK |
672 | return -ret; |
673 | } | |
674 | ||
675 | if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status)) | |
676 | nf_conntrack_event_cache(IPCT_STATUS, *pskb); | |
677 | ||
678 | return ret; | |
679 | } | |
13b18339 | 680 | EXPORT_SYMBOL_GPL(nf_conntrack_in); |
9fb9cbb1 YK |
681 | |
682 | int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse, | |
683 | const struct nf_conntrack_tuple *orig) | |
684 | { | |
923f4902 PM |
685 | int ret; |
686 | ||
687 | rcu_read_lock(); | |
688 | ret = nf_ct_invert_tuple(inverse, orig, | |
689 | __nf_ct_l3proto_find(orig->src.l3num), | |
690 | __nf_ct_l4proto_find(orig->src.l3num, | |
691 | orig->dst.protonum)); | |
692 | rcu_read_unlock(); | |
693 | return ret; | |
9fb9cbb1 | 694 | } |
13b18339 | 695 | EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr); |
9fb9cbb1 | 696 | |
5b1158e9 JK |
697 | /* Alter reply tuple (maybe alter helper). This is for NAT, and is |
698 | implicitly racy: see __nf_conntrack_confirm */ | |
699 | void nf_conntrack_alter_reply(struct nf_conn *ct, | |
700 | const struct nf_conntrack_tuple *newreply) | |
701 | { | |
702 | struct nf_conn_help *help = nfct_help(ct); | |
ceceae1b | 703 | struct nf_conntrack_helper *helper; |
5b1158e9 JK |
704 | |
705 | write_lock_bh(&nf_conntrack_lock); | |
706 | /* Should be unconfirmed, so not in hash table yet */ | |
707 | NF_CT_ASSERT(!nf_ct_is_confirmed(ct)); | |
708 | ||
709 | DEBUGP("Altering reply tuple of %p to ", ct); | |
710 | NF_CT_DUMP_TUPLE(newreply); | |
711 | ||
712 | ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply; | |
ceceae1b YK |
713 | if (ct->master || (help && help->expecting != 0)) |
714 | goto out; | |
715 | ||
716 | helper = __nf_ct_helper_find(newreply); | |
717 | if (helper == NULL) { | |
718 | if (help) | |
719 | rcu_assign_pointer(help->helper, NULL); | |
720 | goto out; | |
5d78a849 | 721 | } |
ceceae1b YK |
722 | |
723 | if (help == NULL) { | |
724 | help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, GFP_ATOMIC); | |
725 | if (help == NULL) { | |
726 | DEBUGP("failed to add helper extension area"); | |
727 | goto out; | |
728 | } | |
729 | } else { | |
730 | memset(&help->help, 0, sizeof(help->help)); | |
731 | } | |
732 | ||
733 | rcu_assign_pointer(help->helper, helper); | |
734 | out: | |
5b1158e9 JK |
735 | write_unlock_bh(&nf_conntrack_lock); |
736 | } | |
13b18339 | 737 | EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply); |
5b1158e9 | 738 | |
9fb9cbb1 YK |
739 | /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ |
740 | void __nf_ct_refresh_acct(struct nf_conn *ct, | |
741 | enum ip_conntrack_info ctinfo, | |
742 | const struct sk_buff *skb, | |
743 | unsigned long extra_jiffies, | |
744 | int do_acct) | |
745 | { | |
746 | int event = 0; | |
747 | ||
748 | NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct); | |
749 | NF_CT_ASSERT(skb); | |
750 | ||
751 | write_lock_bh(&nf_conntrack_lock); | |
752 | ||
997ae831 EL |
753 | /* Only update if this is not a fixed timeout */ |
754 | if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) { | |
755 | write_unlock_bh(&nf_conntrack_lock); | |
756 | return; | |
757 | } | |
758 | ||
9fb9cbb1 YK |
759 | /* If not in hash table, timer will not be active yet */ |
760 | if (!nf_ct_is_confirmed(ct)) { | |
761 | ct->timeout.expires = extra_jiffies; | |
762 | event = IPCT_REFRESH; | |
763 | } else { | |
be00c8e4 MJ |
764 | unsigned long newtime = jiffies + extra_jiffies; |
765 | ||
766 | /* Only update the timeout if the new timeout is at least | |
767 | HZ jiffies from the old timeout. Need del_timer for race | |
768 | avoidance (may already be dying). */ | |
769 | if (newtime - ct->timeout.expires >= HZ | |
770 | && del_timer(&ct->timeout)) { | |
771 | ct->timeout.expires = newtime; | |
9fb9cbb1 YK |
772 | add_timer(&ct->timeout); |
773 | event = IPCT_REFRESH; | |
774 | } | |
775 | } | |
776 | ||
777 | #ifdef CONFIG_NF_CT_ACCT | |
778 | if (do_acct) { | |
779 | ct->counters[CTINFO2DIR(ctinfo)].packets++; | |
780 | ct->counters[CTINFO2DIR(ctinfo)].bytes += | |
bbe735e4 | 781 | skb->len - skb_network_offset(skb); |
3ffd5eeb MJ |
782 | |
783 | if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000) | |
784 | || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000)) | |
785 | event |= IPCT_COUNTER_FILLING; | |
9fb9cbb1 YK |
786 | } |
787 | #endif | |
788 | ||
789 | write_unlock_bh(&nf_conntrack_lock); | |
790 | ||
791 | /* must be unlocked when calling event cache */ | |
792 | if (event) | |
793 | nf_conntrack_event_cache(event, skb); | |
794 | } | |
13b18339 | 795 | EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct); |
9fb9cbb1 | 796 | |
e281db5c | 797 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
c1d10adb PNA |
798 | |
799 | #include <linux/netfilter/nfnetlink.h> | |
800 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
57b47a53 IM |
801 | #include <linux/mutex.h> |
802 | ||
c1d10adb PNA |
803 | |
804 | /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be | |
805 | * in ip_conntrack_core, since we don't want the protocols to autoload | |
806 | * or depend on ctnetlink */ | |
807 | int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb, | |
808 | const struct nf_conntrack_tuple *tuple) | |
809 | { | |
810 | NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t), | |
811 | &tuple->src.u.tcp.port); | |
812 | NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t), | |
813 | &tuple->dst.u.tcp.port); | |
814 | return 0; | |
815 | ||
816 | nfattr_failure: | |
817 | return -1; | |
818 | } | |
13b18339 | 819 | EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr); |
c1d10adb PNA |
820 | |
821 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { | |
822 | [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t), | |
823 | [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t) | |
824 | }; | |
825 | ||
826 | int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[], | |
827 | struct nf_conntrack_tuple *t) | |
828 | { | |
829 | if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1]) | |
830 | return -EINVAL; | |
831 | ||
832 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) | |
833 | return -EINVAL; | |
834 | ||
bff9a89b PM |
835 | t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]); |
836 | t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]); | |
c1d10adb PNA |
837 | |
838 | return 0; | |
839 | } | |
13b18339 | 840 | EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple); |
c1d10adb PNA |
841 | #endif |
842 | ||
9fb9cbb1 YK |
843 | /* Used by ipt_REJECT and ip6t_REJECT. */ |
844 | void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb) | |
845 | { | |
846 | struct nf_conn *ct; | |
847 | enum ip_conntrack_info ctinfo; | |
848 | ||
849 | /* This ICMP is in reverse direction to the packet which caused it */ | |
850 | ct = nf_ct_get(skb, &ctinfo); | |
851 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) | |
852 | ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY; | |
853 | else | |
854 | ctinfo = IP_CT_RELATED; | |
855 | ||
856 | /* Attach to new skbuff, and increment count */ | |
857 | nskb->nfct = &ct->ct_general; | |
858 | nskb->nfctinfo = ctinfo; | |
859 | nf_conntrack_get(nskb->nfct); | |
860 | } | |
13b18339 | 861 | EXPORT_SYMBOL_GPL(__nf_conntrack_attach); |
9fb9cbb1 YK |
862 | |
863 | static inline int | |
864 | do_iter(const struct nf_conntrack_tuple_hash *i, | |
865 | int (*iter)(struct nf_conn *i, void *data), | |
866 | void *data) | |
867 | { | |
868 | return iter(nf_ct_tuplehash_to_ctrack(i), data); | |
869 | } | |
870 | ||
871 | /* Bring out ya dead! */ | |
df0933dc | 872 | static struct nf_conn * |
9fb9cbb1 YK |
873 | get_next_corpse(int (*iter)(struct nf_conn *i, void *data), |
874 | void *data, unsigned int *bucket) | |
875 | { | |
df0933dc PM |
876 | struct nf_conntrack_tuple_hash *h; |
877 | struct nf_conn *ct; | |
f205c5e0 | 878 | struct hlist_node *n; |
9fb9cbb1 YK |
879 | |
880 | write_lock_bh(&nf_conntrack_lock); | |
881 | for (; *bucket < nf_conntrack_htable_size; (*bucket)++) { | |
f205c5e0 | 882 | hlist_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnode) { |
df0933dc PM |
883 | ct = nf_ct_tuplehash_to_ctrack(h); |
884 | if (iter(ct, data)) | |
885 | goto found; | |
886 | } | |
601e68e1 | 887 | } |
f205c5e0 | 888 | hlist_for_each_entry(h, n, &unconfirmed, hnode) { |
df0933dc PM |
889 | ct = nf_ct_tuplehash_to_ctrack(h); |
890 | if (iter(ct, data)) | |
ec68e97d | 891 | set_bit(IPS_DYING_BIT, &ct->status); |
df0933dc | 892 | } |
c073e3fa | 893 | write_unlock_bh(&nf_conntrack_lock); |
df0933dc PM |
894 | return NULL; |
895 | found: | |
c073e3fa | 896 | atomic_inc(&ct->ct_general.use); |
9fb9cbb1 | 897 | write_unlock_bh(&nf_conntrack_lock); |
df0933dc | 898 | return ct; |
9fb9cbb1 YK |
899 | } |
900 | ||
901 | void | |
902 | nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data) | |
903 | { | |
df0933dc | 904 | struct nf_conn *ct; |
9fb9cbb1 YK |
905 | unsigned int bucket = 0; |
906 | ||
df0933dc | 907 | while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) { |
9fb9cbb1 YK |
908 | /* Time to push up daises... */ |
909 | if (del_timer(&ct->timeout)) | |
910 | death_by_timeout((unsigned long)ct); | |
911 | /* ... else the timer will get him soon. */ | |
912 | ||
913 | nf_ct_put(ct); | |
914 | } | |
915 | } | |
13b18339 | 916 | EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup); |
9fb9cbb1 YK |
917 | |
918 | static int kill_all(struct nf_conn *i, void *data) | |
919 | { | |
920 | return 1; | |
921 | } | |
922 | ||
f205c5e0 PM |
923 | static void free_conntrack_hash(struct hlist_head *hash, int vmalloced, |
924 | int size) | |
9fb9cbb1 YK |
925 | { |
926 | if (vmalloced) | |
927 | vfree(hash); | |
928 | else | |
601e68e1 | 929 | free_pages((unsigned long)hash, |
f205c5e0 | 930 | get_order(sizeof(struct hlist_head) * size)); |
9fb9cbb1 YK |
931 | } |
932 | ||
272491ef | 933 | void nf_conntrack_flush(void) |
c1d10adb PNA |
934 | { |
935 | nf_ct_iterate_cleanup(kill_all, NULL); | |
936 | } | |
13b18339 | 937 | EXPORT_SYMBOL_GPL(nf_conntrack_flush); |
c1d10adb | 938 | |
9fb9cbb1 YK |
939 | /* Mishearing the voices in his head, our hero wonders how he's |
940 | supposed to kill the mall. */ | |
941 | void nf_conntrack_cleanup(void) | |
942 | { | |
c3a47ab3 | 943 | rcu_assign_pointer(ip_ct_attach, NULL); |
7d3cdc6b | 944 | |
9fb9cbb1 YK |
945 | /* This makes sure all current packets have passed through |
946 | netfilter framework. Roll on, two-stage module | |
947 | delete... */ | |
948 | synchronize_net(); | |
949 | ||
950 | nf_ct_event_cache_flush(); | |
951 | i_see_dead_people: | |
c1d10adb | 952 | nf_conntrack_flush(); |
9fb9cbb1 YK |
953 | if (atomic_read(&nf_conntrack_count) != 0) { |
954 | schedule(); | |
955 | goto i_see_dead_people; | |
956 | } | |
6636568c PM |
957 | /* wait until all references to nf_conntrack_untracked are dropped */ |
958 | while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1) | |
959 | schedule(); | |
9fb9cbb1 | 960 | |
de6e05c4 YK |
961 | rcu_assign_pointer(nf_ct_destroy, NULL); |
962 | ||
dacd2a1a | 963 | kmem_cache_destroy(nf_conntrack_cachep); |
9fb9cbb1 YK |
964 | kmem_cache_destroy(nf_conntrack_expect_cachep); |
965 | free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc, | |
966 | nf_conntrack_htable_size); | |
5a6f294e | 967 | |
ac5357eb | 968 | nf_conntrack_proto_fini(); |
ceceae1b | 969 | nf_conntrack_helper_fini(); |
9fb9cbb1 YK |
970 | } |
971 | ||
f205c5e0 | 972 | static struct hlist_head *alloc_hashtable(int *sizep, int *vmalloced) |
9fb9cbb1 | 973 | { |
f205c5e0 | 974 | struct hlist_head *hash; |
8e5105a0 | 975 | unsigned int size, i; |
9fb9cbb1 | 976 | |
601e68e1 | 977 | *vmalloced = 0; |
8e5105a0 | 978 | |
f205c5e0 | 979 | size = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_head)); |
601e68e1 | 980 | hash = (void*)__get_free_pages(GFP_KERNEL, |
f205c5e0 | 981 | get_order(sizeof(struct hlist_head) |
9fb9cbb1 | 982 | * size)); |
601e68e1 | 983 | if (!hash) { |
9fb9cbb1 YK |
984 | *vmalloced = 1; |
985 | printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n"); | |
f205c5e0 | 986 | hash = vmalloc(sizeof(struct hlist_head) * size); |
9fb9cbb1 YK |
987 | } |
988 | ||
989 | if (hash) | |
601e68e1 | 990 | for (i = 0; i < size; i++) |
f205c5e0 | 991 | INIT_HLIST_HEAD(&hash[i]); |
9fb9cbb1 YK |
992 | |
993 | return hash; | |
994 | } | |
995 | ||
996 | int set_hashsize(const char *val, struct kernel_param *kp) | |
997 | { | |
998 | int i, bucket, hashsize, vmalloced; | |
999 | int old_vmalloced, old_size; | |
1000 | int rnd; | |
f205c5e0 | 1001 | struct hlist_head *hash, *old_hash; |
9fb9cbb1 YK |
1002 | struct nf_conntrack_tuple_hash *h; |
1003 | ||
1004 | /* On boot, we can set this without any fancy locking. */ | |
1005 | if (!nf_conntrack_htable_size) | |
1006 | return param_set_uint(val, kp); | |
1007 | ||
1008 | hashsize = simple_strtol(val, NULL, 0); | |
1009 | if (!hashsize) | |
1010 | return -EINVAL; | |
1011 | ||
8e5105a0 | 1012 | hash = alloc_hashtable(&hashsize, &vmalloced); |
9fb9cbb1 YK |
1013 | if (!hash) |
1014 | return -ENOMEM; | |
1015 | ||
1016 | /* We have to rehahs for the new table anyway, so we also can | |
1017 | * use a newrandom seed */ | |
1018 | get_random_bytes(&rnd, 4); | |
1019 | ||
1020 | write_lock_bh(&nf_conntrack_lock); | |
1021 | for (i = 0; i < nf_conntrack_htable_size; i++) { | |
f205c5e0 PM |
1022 | while (!hlist_empty(&nf_conntrack_hash[i])) { |
1023 | h = hlist_entry(nf_conntrack_hash[i].first, | |
1024 | struct nf_conntrack_tuple_hash, hnode); | |
1025 | hlist_del(&h->hnode); | |
9fb9cbb1 | 1026 | bucket = __hash_conntrack(&h->tuple, hashsize, rnd); |
f205c5e0 | 1027 | hlist_add_head(&h->hnode, &hash[bucket]); |
9fb9cbb1 YK |
1028 | } |
1029 | } | |
1030 | old_size = nf_conntrack_htable_size; | |
1031 | old_vmalloced = nf_conntrack_vmalloc; | |
1032 | old_hash = nf_conntrack_hash; | |
1033 | ||
1034 | nf_conntrack_htable_size = hashsize; | |
1035 | nf_conntrack_vmalloc = vmalloced; | |
1036 | nf_conntrack_hash = hash; | |
1037 | nf_conntrack_hash_rnd = rnd; | |
1038 | write_unlock_bh(&nf_conntrack_lock); | |
1039 | ||
1040 | free_conntrack_hash(old_hash, old_vmalloced, old_size); | |
1041 | return 0; | |
1042 | } | |
1043 | ||
1044 | module_param_call(hashsize, set_hashsize, param_get_uint, | |
1045 | &nf_conntrack_htable_size, 0600); | |
1046 | ||
1047 | int __init nf_conntrack_init(void) | |
1048 | { | |
f205c5e0 | 1049 | int max_factor = 8; |
9fb9cbb1 YK |
1050 | int ret; |
1051 | ||
1052 | /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB | |
f205c5e0 | 1053 | * machine has 512 buckets. >= 1GB machines have 16384 buckets. */ |
9fb9cbb1 YK |
1054 | if (!nf_conntrack_htable_size) { |
1055 | nf_conntrack_htable_size | |
1056 | = (((num_physpages << PAGE_SHIFT) / 16384) | |
f205c5e0 | 1057 | / sizeof(struct hlist_head)); |
9fb9cbb1 | 1058 | if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE)) |
f205c5e0 PM |
1059 | nf_conntrack_htable_size = 16384; |
1060 | if (nf_conntrack_htable_size < 32) | |
1061 | nf_conntrack_htable_size = 32; | |
1062 | ||
1063 | /* Use a max. factor of four by default to get the same max as | |
1064 | * with the old struct list_heads. When a table size is given | |
1065 | * we use the old value of 8 to avoid reducing the max. | |
1066 | * entries. */ | |
1067 | max_factor = 4; | |
9fb9cbb1 | 1068 | } |
8e5105a0 | 1069 | nf_conntrack_hash = alloc_hashtable(&nf_conntrack_htable_size, |
9fb9cbb1 YK |
1070 | &nf_conntrack_vmalloc); |
1071 | if (!nf_conntrack_hash) { | |
1072 | printk(KERN_ERR "Unable to create nf_conntrack_hash\n"); | |
1073 | goto err_out; | |
1074 | } | |
1075 | ||
f205c5e0 | 1076 | nf_conntrack_max = max_factor * nf_conntrack_htable_size; |
8e5105a0 PM |
1077 | |
1078 | printk("nf_conntrack version %s (%u buckets, %d max)\n", | |
1079 | NF_CONNTRACK_VERSION, nf_conntrack_htable_size, | |
1080 | nf_conntrack_max); | |
1081 | ||
dacd2a1a YK |
1082 | nf_conntrack_cachep = kmem_cache_create("nf_conntrack", |
1083 | sizeof(struct nf_conn), | |
1084 | 0, 0, NULL, NULL); | |
1085 | if (!nf_conntrack_cachep) { | |
9fb9cbb1 YK |
1086 | printk(KERN_ERR "Unable to create nf_conn slab cache\n"); |
1087 | goto err_free_hash; | |
1088 | } | |
1089 | ||
1090 | nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect", | |
1091 | sizeof(struct nf_conntrack_expect), | |
1092 | 0, 0, NULL, NULL); | |
1093 | if (!nf_conntrack_expect_cachep) { | |
1094 | printk(KERN_ERR "Unable to create nf_expect slab cache\n"); | |
1095 | goto err_free_conntrack_slab; | |
1096 | } | |
1097 | ||
ac5357eb | 1098 | ret = nf_conntrack_proto_init(); |
933a41e7 PM |
1099 | if (ret < 0) |
1100 | goto out_free_expect_slab; | |
1101 | ||
ceceae1b YK |
1102 | ret = nf_conntrack_helper_init(); |
1103 | if (ret < 0) | |
1104 | goto out_fini_proto; | |
1105 | ||
7d3cdc6b | 1106 | /* For use by REJECT target */ |
c3a47ab3 | 1107 | rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach); |
de6e05c4 | 1108 | rcu_assign_pointer(nf_ct_destroy, destroy_conntrack); |
7d3cdc6b | 1109 | |
9fb9cbb1 YK |
1110 | /* Set up fake conntrack: |
1111 | - to never be deleted, not in any hashes */ | |
1112 | atomic_set(&nf_conntrack_untracked.ct_general.use, 1); | |
1113 | /* - and look it like as a confirmed connection */ | |
1114 | set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status); | |
1115 | ||
1116 | return ret; | |
1117 | ||
ceceae1b YK |
1118 | out_fini_proto: |
1119 | nf_conntrack_proto_fini(); | |
933a41e7 PM |
1120 | out_free_expect_slab: |
1121 | kmem_cache_destroy(nf_conntrack_expect_cachep); | |
9fb9cbb1 | 1122 | err_free_conntrack_slab: |
dacd2a1a | 1123 | kmem_cache_destroy(nf_conntrack_cachep); |
9fb9cbb1 YK |
1124 | err_free_hash: |
1125 | free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc, | |
1126 | nf_conntrack_htable_size); | |
1127 | err_out: | |
1128 | return -ENOMEM; | |
1129 | } |