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