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
5 /* (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
7 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
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.
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>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_l3proto.h>
35 #include <net/netfilter/nf_conntrack_l4proto.h>
36 #include <net/netfilter/nf_conntrack_expect.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_extend.h>
41 #define NF_CONNTRACK_VERSION "0.5.0"
46 #define DEBUGP(format, args...)
49 DEFINE_RWLOCK(nf_conntrack_lock);
50 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
52 /* nf_conntrack_standalone needs this */
53 atomic_t nf_conntrack_count = ATOMIC_INIT(0);
54 EXPORT_SYMBOL_GPL(nf_conntrack_count);
56 void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
57 EXPORT_SYMBOL_GPL(nf_conntrack_destroyed);
59 unsigned int nf_conntrack_htable_size __read_mostly;
60 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
62 int nf_conntrack_max __read_mostly;
63 EXPORT_SYMBOL_GPL(nf_conntrack_max);
65 struct list_head *nf_conntrack_hash __read_mostly;
66 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
68 struct nf_conn nf_conntrack_untracked __read_mostly;
69 EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
71 unsigned int nf_ct_log_invalid __read_mostly;
72 LIST_HEAD(unconfirmed);
73 static int nf_conntrack_vmalloc __read_mostly;
75 static unsigned int nf_conntrack_next_id;
77 DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
78 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
81 * This scheme offers various size of "struct nf_conn" dependent on
82 * features(helper, nat, ...)
85 #define NF_CT_FEATURES_NAMELEN 256
87 /* name of slab cache. printed in /proc/slabinfo */
90 /* size of slab cache */
93 /* slab cache pointer */
94 struct kmem_cache *cachep;
96 /* allocated slab cache + modules which uses this slab cache */
99 } nf_ct_cache[NF_CT_F_NUM];
101 /* protect members of nf_ct_cache except of "use" */
102 DEFINE_RWLOCK(nf_ct_cache_lock);
104 /* This avoids calling kmem_cache_create() with same name simultaneously */
105 static DEFINE_MUTEX(nf_ct_cache_mutex);
107 static int nf_conntrack_hash_rnd_initted;
108 static unsigned int nf_conntrack_hash_rnd;
110 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
111 unsigned int size, unsigned int rnd)
115 a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all),
116 (tuple->src.l3num << 16) | tuple->dst.protonum);
117 b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
118 (tuple->src.u.all << 16) | tuple->dst.u.all);
120 return jhash_2words(a, b, rnd) % size;
123 static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
125 return __hash_conntrack(tuple, nf_conntrack_htable_size,
126 nf_conntrack_hash_rnd);
129 int nf_conntrack_register_cache(u_int32_t features, const char *name,
134 struct kmem_cache *cachep;
136 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
137 features, name, size);
139 if (features < NF_CT_F_BASIC || features >= NF_CT_F_NUM) {
140 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
145 mutex_lock(&nf_ct_cache_mutex);
147 write_lock_bh(&nf_ct_cache_lock);
148 /* e.g: multiple helpers are loaded */
149 if (nf_ct_cache[features].use > 0) {
150 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
151 if ((!strncmp(nf_ct_cache[features].name, name,
152 NF_CT_FEATURES_NAMELEN))
153 && nf_ct_cache[features].size == size) {
154 DEBUGP("nf_conntrack_register_cache: reusing.\n");
155 nf_ct_cache[features].use++;
160 write_unlock_bh(&nf_ct_cache_lock);
161 mutex_unlock(&nf_ct_cache_mutex);
164 write_unlock_bh(&nf_ct_cache_lock);
167 * The memory space for name of slab cache must be alive until
168 * cache is destroyed.
170 cache_name = kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN, GFP_ATOMIC);
171 if (cache_name == NULL) {
172 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
177 if (strlcpy(cache_name, name, NF_CT_FEATURES_NAMELEN)
178 >= NF_CT_FEATURES_NAMELEN) {
179 printk("nf_conntrack_register_cache: name too long\n");
184 cachep = kmem_cache_create(cache_name, size, 0, 0,
187 printk("nf_conntrack_register_cache: Can't create slab cache "
188 "for the features = 0x%x\n", features);
193 write_lock_bh(&nf_ct_cache_lock);
194 nf_ct_cache[features].use = 1;
195 nf_ct_cache[features].size = size;
196 nf_ct_cache[features].cachep = cachep;
197 nf_ct_cache[features].name = cache_name;
198 write_unlock_bh(&nf_ct_cache_lock);
205 mutex_unlock(&nf_ct_cache_mutex);
208 EXPORT_SYMBOL_GPL(nf_conntrack_register_cache);
210 /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
211 void nf_conntrack_unregister_cache(u_int32_t features)
213 struct kmem_cache *cachep;
217 * This assures that kmem_cache_create() isn't called before destroying
220 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features);
221 mutex_lock(&nf_ct_cache_mutex);
223 write_lock_bh(&nf_ct_cache_lock);
224 if (--nf_ct_cache[features].use > 0) {
225 write_unlock_bh(&nf_ct_cache_lock);
226 mutex_unlock(&nf_ct_cache_mutex);
229 cachep = nf_ct_cache[features].cachep;
230 name = nf_ct_cache[features].name;
231 nf_ct_cache[features].cachep = NULL;
232 nf_ct_cache[features].name = NULL;
233 nf_ct_cache[features].size = 0;
234 write_unlock_bh(&nf_ct_cache_lock);
238 kmem_cache_destroy(cachep);
241 mutex_unlock(&nf_ct_cache_mutex);
243 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_cache);
246 nf_ct_get_tuple(const struct sk_buff *skb,
248 unsigned int dataoff,
251 struct nf_conntrack_tuple *tuple,
252 const struct nf_conntrack_l3proto *l3proto,
253 const struct nf_conntrack_l4proto *l4proto)
255 NF_CT_TUPLE_U_BLANK(tuple);
257 tuple->src.l3num = l3num;
258 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
261 tuple->dst.protonum = protonum;
262 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
264 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
266 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
269 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
270 const struct nf_conntrack_tuple *orig,
271 const struct nf_conntrack_l3proto *l3proto,
272 const struct nf_conntrack_l4proto *l4proto)
274 NF_CT_TUPLE_U_BLANK(inverse);
276 inverse->src.l3num = orig->src.l3num;
277 if (l3proto->invert_tuple(inverse, orig) == 0)
280 inverse->dst.dir = !orig->dst.dir;
282 inverse->dst.protonum = orig->dst.protonum;
283 return l4proto->invert_tuple(inverse, orig);
285 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
288 clean_from_lists(struct nf_conn *ct)
290 DEBUGP("clean_from_lists(%p)\n", ct);
291 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
292 list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list);
294 /* Destroy all pending expectations */
295 nf_ct_remove_expectations(ct);
299 destroy_conntrack(struct nf_conntrack *nfct)
301 struct nf_conn *ct = (struct nf_conn *)nfct;
302 struct nf_conntrack_l4proto *l4proto;
303 typeof(nf_conntrack_destroyed) destroyed;
305 DEBUGP("destroy_conntrack(%p)\n", ct);
306 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
307 NF_CT_ASSERT(!timer_pending(&ct->timeout));
309 nf_conntrack_event(IPCT_DESTROY, ct);
310 set_bit(IPS_DYING_BIT, &ct->status);
312 /* To make sure we don't get any weird locking issues here:
313 * destroy_conntrack() MUST NOT be called with a write lock
314 * to nf_conntrack_lock!!! -HW */
316 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
317 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
318 if (l4proto && l4proto->destroy)
319 l4proto->destroy(ct);
321 nf_ct_ext_destroy(ct);
323 destroyed = rcu_dereference(nf_conntrack_destroyed);
329 write_lock_bh(&nf_conntrack_lock);
330 /* Expectations will have been removed in clean_from_lists,
331 * except TFTP can create an expectation on the first packet,
332 * before connection is in the list, so we need to clean here,
334 nf_ct_remove_expectations(ct);
336 /* We overload first tuple to link into unconfirmed list. */
337 if (!nf_ct_is_confirmed(ct)) {
338 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
339 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
342 NF_CT_STAT_INC(delete);
343 write_unlock_bh(&nf_conntrack_lock);
346 nf_ct_put(ct->master);
348 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
349 nf_conntrack_free(ct);
352 static void death_by_timeout(unsigned long ul_conntrack)
354 struct nf_conn *ct = (void *)ul_conntrack;
355 struct nf_conn_help *help = nfct_help(ct);
356 struct nf_conntrack_helper *helper;
360 helper = rcu_dereference(help->helper);
361 if (helper && helper->destroy)
366 write_lock_bh(&nf_conntrack_lock);
367 /* Inside lock so preempt is disabled on module removal path.
368 * Otherwise we can get spurious warnings. */
369 NF_CT_STAT_INC(delete_list);
370 clean_from_lists(ct);
371 write_unlock_bh(&nf_conntrack_lock);
375 struct nf_conntrack_tuple_hash *
376 __nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
377 const struct nf_conn *ignored_conntrack)
379 struct nf_conntrack_tuple_hash *h;
380 unsigned int hash = hash_conntrack(tuple);
382 list_for_each_entry(h, &nf_conntrack_hash[hash], list) {
383 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
384 nf_ct_tuple_equal(tuple, &h->tuple)) {
385 NF_CT_STAT_INC(found);
388 NF_CT_STAT_INC(searched);
393 EXPORT_SYMBOL_GPL(__nf_conntrack_find);
395 /* Find a connection corresponding to a tuple. */
396 struct nf_conntrack_tuple_hash *
397 nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple,
398 const struct nf_conn *ignored_conntrack)
400 struct nf_conntrack_tuple_hash *h;
402 read_lock_bh(&nf_conntrack_lock);
403 h = __nf_conntrack_find(tuple, ignored_conntrack);
405 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
406 read_unlock_bh(&nf_conntrack_lock);
410 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
412 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
414 unsigned int repl_hash)
416 ct->id = ++nf_conntrack_next_id;
417 list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list,
418 &nf_conntrack_hash[hash]);
419 list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list,
420 &nf_conntrack_hash[repl_hash]);
423 void nf_conntrack_hash_insert(struct nf_conn *ct)
425 unsigned int hash, repl_hash;
427 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
428 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
430 write_lock_bh(&nf_conntrack_lock);
431 __nf_conntrack_hash_insert(ct, hash, repl_hash);
432 write_unlock_bh(&nf_conntrack_lock);
434 EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
436 /* Confirm a connection given skb; places it in hash table */
438 __nf_conntrack_confirm(struct sk_buff **pskb)
440 unsigned int hash, repl_hash;
441 struct nf_conntrack_tuple_hash *h;
443 struct nf_conn_help *help;
444 enum ip_conntrack_info ctinfo;
446 ct = nf_ct_get(*pskb, &ctinfo);
448 /* ipt_REJECT uses nf_conntrack_attach to attach related
449 ICMP/TCP RST packets in other direction. Actual packet
450 which created connection will be IP_CT_NEW or for an
451 expected connection, IP_CT_RELATED. */
452 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
455 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
456 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
458 /* We're not in hash table, and we refuse to set up related
459 connections for unconfirmed conns. But packet copies and
460 REJECT will give spurious warnings here. */
461 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
463 /* No external references means noone else could have
465 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
466 DEBUGP("Confirming conntrack %p\n", ct);
468 write_lock_bh(&nf_conntrack_lock);
470 /* See if there's one in the list already, including reverse:
471 NAT could have grabbed it without realizing, since we're
472 not in the hash. If there is, we lost race. */
473 list_for_each_entry(h, &nf_conntrack_hash[hash], list)
474 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
477 list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list)
478 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
482 /* Remove from unconfirmed list */
483 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
485 __nf_conntrack_hash_insert(ct, hash, repl_hash);
486 /* Timer relative to confirmation time, not original
487 setting time, otherwise we'd get timer wrap in
488 weird delay cases. */
489 ct->timeout.expires += jiffies;
490 add_timer(&ct->timeout);
491 atomic_inc(&ct->ct_general.use);
492 set_bit(IPS_CONFIRMED_BIT, &ct->status);
493 NF_CT_STAT_INC(insert);
494 write_unlock_bh(&nf_conntrack_lock);
495 help = nfct_help(ct);
496 if (help && help->helper)
497 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
498 #ifdef CONFIG_NF_NAT_NEEDED
499 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
500 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
501 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
503 nf_conntrack_event_cache(master_ct(ct) ?
504 IPCT_RELATED : IPCT_NEW, *pskb);
508 NF_CT_STAT_INC(insert_failed);
509 write_unlock_bh(&nf_conntrack_lock);
512 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
514 /* Returns true if a connection correspondings to the tuple (required
517 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
518 const struct nf_conn *ignored_conntrack)
520 struct nf_conntrack_tuple_hash *h;
522 read_lock_bh(&nf_conntrack_lock);
523 h = __nf_conntrack_find(tuple, ignored_conntrack);
524 read_unlock_bh(&nf_conntrack_lock);
528 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
530 /* There's a small race here where we may free a just-assured
531 connection. Too bad: we're in trouble anyway. */
532 static int early_drop(struct list_head *chain)
534 /* Traverse backwards: gives us oldest, which is roughly LRU */
535 struct nf_conntrack_tuple_hash *h;
536 struct nf_conn *ct = NULL, *tmp;
539 read_lock_bh(&nf_conntrack_lock);
540 list_for_each_entry_reverse(h, chain, list) {
541 tmp = nf_ct_tuplehash_to_ctrack(h);
542 if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) {
544 atomic_inc(&ct->ct_general.use);
548 read_unlock_bh(&nf_conntrack_lock);
553 if (del_timer(&ct->timeout)) {
554 death_by_timeout((unsigned long)ct);
556 NF_CT_STAT_INC_ATOMIC(early_drop);
562 static struct nf_conn *
563 __nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
564 const struct nf_conntrack_tuple *repl,
565 const struct nf_conntrack_l3proto *l3proto,
568 struct nf_conn *conntrack = NULL;
569 struct nf_conntrack_helper *helper;
571 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
572 get_random_bytes(&nf_conntrack_hash_rnd, 4);
573 nf_conntrack_hash_rnd_initted = 1;
576 /* We don't want any race condition at early drop stage */
577 atomic_inc(&nf_conntrack_count);
580 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
581 unsigned int hash = hash_conntrack(orig);
582 /* Try dropping from this hash chain. */
583 if (!early_drop(&nf_conntrack_hash[hash])) {
584 atomic_dec(&nf_conntrack_count);
587 "nf_conntrack: table full, dropping"
589 return ERR_PTR(-ENOMEM);
593 /* find features needed by this conntrack. */
594 features |= l3proto->get_features(orig);
596 /* FIXME: protect helper list per RCU */
597 read_lock_bh(&nf_conntrack_lock);
598 helper = __nf_ct_helper_find(repl);
599 /* NAT might want to assign a helper later */
600 if (helper || features & NF_CT_F_NAT)
601 features |= NF_CT_F_HELP;
602 read_unlock_bh(&nf_conntrack_lock);
604 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features);
606 read_lock_bh(&nf_ct_cache_lock);
608 if (unlikely(!nf_ct_cache[features].use)) {
609 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
614 conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC);
615 if (conntrack == NULL) {
616 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
620 memset(conntrack, 0, nf_ct_cache[features].size);
621 conntrack->features = features;
622 atomic_set(&conntrack->ct_general.use, 1);
623 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
624 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
625 /* Don't set timer yet: wait for confirmation */
626 setup_timer(&conntrack->timeout, death_by_timeout,
627 (unsigned long)conntrack);
628 read_unlock_bh(&nf_ct_cache_lock);
632 read_unlock_bh(&nf_ct_cache_lock);
633 atomic_dec(&nf_conntrack_count);
637 struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
638 const struct nf_conntrack_tuple *repl)
640 struct nf_conntrack_l3proto *l3proto;
644 l3proto = __nf_ct_l3proto_find(orig->src.l3num);
645 ct = __nf_conntrack_alloc(orig, repl, l3proto, 0);
650 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
652 void nf_conntrack_free(struct nf_conn *conntrack)
654 u_int32_t features = conntrack->features;
655 NF_CT_ASSERT(features >= NF_CT_F_BASIC && features < NF_CT_F_NUM);
656 nf_ct_ext_free(conntrack);
657 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features,
659 kmem_cache_free(nf_ct_cache[features].cachep, conntrack);
660 atomic_dec(&nf_conntrack_count);
662 EXPORT_SYMBOL_GPL(nf_conntrack_free);
664 /* Allocate a new conntrack: we return -ENOMEM if classification
665 failed due to stress. Otherwise it really is unclassifiable. */
666 static struct nf_conntrack_tuple_hash *
667 init_conntrack(const struct nf_conntrack_tuple *tuple,
668 struct nf_conntrack_l3proto *l3proto,
669 struct nf_conntrack_l4proto *l4proto,
671 unsigned int dataoff)
673 struct nf_conn *conntrack;
674 struct nf_conn_help *help;
675 struct nf_conntrack_tuple repl_tuple;
676 struct nf_conntrack_expect *exp;
677 u_int32_t features = 0;
679 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
680 DEBUGP("Can't invert tuple.\n");
684 read_lock_bh(&nf_conntrack_lock);
685 exp = __nf_conntrack_expect_find(tuple);
686 if (exp && exp->helper)
687 features = NF_CT_F_HELP;
688 read_unlock_bh(&nf_conntrack_lock);
690 conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto, features);
691 if (conntrack == NULL || IS_ERR(conntrack)) {
692 DEBUGP("Can't allocate conntrack.\n");
693 return (struct nf_conntrack_tuple_hash *)conntrack;
696 if (!l4proto->new(conntrack, skb, dataoff)) {
697 nf_conntrack_free(conntrack);
698 DEBUGP("init conntrack: can't track with proto module\n");
702 write_lock_bh(&nf_conntrack_lock);
703 exp = find_expectation(tuple);
705 help = nfct_help(conntrack);
707 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
709 /* Welcome, Mr. Bond. We've been expecting you... */
710 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
711 conntrack->master = exp->master;
713 rcu_assign_pointer(help->helper, exp->helper);
714 #ifdef CONFIG_NF_CONNTRACK_MARK
715 conntrack->mark = exp->master->mark;
717 #ifdef CONFIG_NF_CONNTRACK_SECMARK
718 conntrack->secmark = exp->master->secmark;
720 nf_conntrack_get(&conntrack->master->ct_general);
721 NF_CT_STAT_INC(expect_new);
724 /* not in hash table yet, so not strictly necessary */
725 rcu_assign_pointer(help->helper,
726 __nf_ct_helper_find(&repl_tuple));
731 /* Overload tuple linked list to put us in unconfirmed list. */
732 list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
734 write_unlock_bh(&nf_conntrack_lock);
738 exp->expectfn(conntrack, exp);
739 nf_conntrack_expect_put(exp);
742 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
745 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
746 static inline struct nf_conn *
747 resolve_normal_ct(struct sk_buff *skb,
748 unsigned int dataoff,
751 struct nf_conntrack_l3proto *l3proto,
752 struct nf_conntrack_l4proto *l4proto,
754 enum ip_conntrack_info *ctinfo)
756 struct nf_conntrack_tuple tuple;
757 struct nf_conntrack_tuple_hash *h;
760 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
761 dataoff, l3num, protonum, &tuple, l3proto,
763 DEBUGP("resolve_normal_ct: Can't get tuple\n");
767 /* look for tuple match */
768 h = nf_conntrack_find_get(&tuple, NULL);
770 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
776 ct = nf_ct_tuplehash_to_ctrack(h);
778 /* It exists; we have (non-exclusive) reference. */
779 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
780 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
781 /* Please set reply bit if this packet OK */
784 /* Once we've had two way comms, always ESTABLISHED. */
785 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
786 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
787 *ctinfo = IP_CT_ESTABLISHED;
788 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
789 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
790 *ctinfo = IP_CT_RELATED;
792 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
797 skb->nfct = &ct->ct_general;
798 skb->nfctinfo = *ctinfo;
803 nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
806 enum ip_conntrack_info ctinfo;
807 struct nf_conntrack_l3proto *l3proto;
808 struct nf_conntrack_l4proto *l4proto;
809 unsigned int dataoff;
814 /* Previously seen (loopback or untracked)? Ignore. */
816 NF_CT_STAT_INC_ATOMIC(ignore);
820 /* rcu_read_lock()ed by nf_hook_slow */
821 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
823 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
824 DEBUGP("not prepared to track yet or error occured\n");
828 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
830 /* It may be an special packet, error, unclean...
831 * inverse of the return code tells to the netfilter
832 * core what to do with the packet. */
833 if (l4proto->error != NULL &&
834 (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
835 NF_CT_STAT_INC_ATOMIC(error);
836 NF_CT_STAT_INC_ATOMIC(invalid);
840 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto,
841 &set_reply, &ctinfo);
843 /* Not valid part of a connection */
844 NF_CT_STAT_INC_ATOMIC(invalid);
849 /* Too stressed to deal. */
850 NF_CT_STAT_INC_ATOMIC(drop);
854 NF_CT_ASSERT((*pskb)->nfct);
856 ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
858 /* Invalid: inverse of the return code tells
859 * the netfilter core what to do */
860 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
861 nf_conntrack_put((*pskb)->nfct);
862 (*pskb)->nfct = NULL;
863 NF_CT_STAT_INC_ATOMIC(invalid);
867 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
868 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
872 EXPORT_SYMBOL_GPL(nf_conntrack_in);
874 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
875 const struct nf_conntrack_tuple *orig)
880 ret = nf_ct_invert_tuple(inverse, orig,
881 __nf_ct_l3proto_find(orig->src.l3num),
882 __nf_ct_l4proto_find(orig->src.l3num,
883 orig->dst.protonum));
887 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
889 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
890 implicitly racy: see __nf_conntrack_confirm */
891 void nf_conntrack_alter_reply(struct nf_conn *ct,
892 const struct nf_conntrack_tuple *newreply)
894 struct nf_conn_help *help = nfct_help(ct);
896 write_lock_bh(&nf_conntrack_lock);
897 /* Should be unconfirmed, so not in hash table yet */
898 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
900 DEBUGP("Altering reply tuple of %p to ", ct);
901 NF_CT_DUMP_TUPLE(newreply);
903 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
904 if (!ct->master && help && help->expecting == 0) {
905 struct nf_conntrack_helper *helper;
906 helper = __nf_ct_helper_find(newreply);
908 memset(&help->help, 0, sizeof(help->help));
909 /* not in hash table yet, so not strictly necessary */
910 rcu_assign_pointer(help->helper, helper);
912 write_unlock_bh(&nf_conntrack_lock);
914 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
916 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
917 void __nf_ct_refresh_acct(struct nf_conn *ct,
918 enum ip_conntrack_info ctinfo,
919 const struct sk_buff *skb,
920 unsigned long extra_jiffies,
925 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
928 write_lock_bh(&nf_conntrack_lock);
930 /* Only update if this is not a fixed timeout */
931 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
932 write_unlock_bh(&nf_conntrack_lock);
936 /* If not in hash table, timer will not be active yet */
937 if (!nf_ct_is_confirmed(ct)) {
938 ct->timeout.expires = extra_jiffies;
939 event = IPCT_REFRESH;
941 unsigned long newtime = jiffies + extra_jiffies;
943 /* Only update the timeout if the new timeout is at least
944 HZ jiffies from the old timeout. Need del_timer for race
945 avoidance (may already be dying). */
946 if (newtime - ct->timeout.expires >= HZ
947 && del_timer(&ct->timeout)) {
948 ct->timeout.expires = newtime;
949 add_timer(&ct->timeout);
950 event = IPCT_REFRESH;
954 #ifdef CONFIG_NF_CT_ACCT
956 ct->counters[CTINFO2DIR(ctinfo)].packets++;
957 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
958 skb->len - skb_network_offset(skb);
960 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
961 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
962 event |= IPCT_COUNTER_FILLING;
966 write_unlock_bh(&nf_conntrack_lock);
968 /* must be unlocked when calling event cache */
970 nf_conntrack_event_cache(event, skb);
972 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
974 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
976 #include <linux/netfilter/nfnetlink.h>
977 #include <linux/netfilter/nfnetlink_conntrack.h>
978 #include <linux/mutex.h>
981 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
982 * in ip_conntrack_core, since we don't want the protocols to autoload
983 * or depend on ctnetlink */
984 int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
985 const struct nf_conntrack_tuple *tuple)
987 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
988 &tuple->src.u.tcp.port);
989 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
990 &tuple->dst.u.tcp.port);
996 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
998 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
999 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
1000 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
1003 int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
1004 struct nf_conntrack_tuple *t)
1006 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
1009 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
1012 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
1013 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
1017 EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
1020 /* Used by ipt_REJECT and ip6t_REJECT. */
1021 void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
1024 enum ip_conntrack_info ctinfo;
1026 /* This ICMP is in reverse direction to the packet which caused it */
1027 ct = nf_ct_get(skb, &ctinfo);
1028 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1029 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
1031 ctinfo = IP_CT_RELATED;
1033 /* Attach to new skbuff, and increment count */
1034 nskb->nfct = &ct->ct_general;
1035 nskb->nfctinfo = ctinfo;
1036 nf_conntrack_get(nskb->nfct);
1038 EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
1041 do_iter(const struct nf_conntrack_tuple_hash *i,
1042 int (*iter)(struct nf_conn *i, void *data),
1045 return iter(nf_ct_tuplehash_to_ctrack(i), data);
1048 /* Bring out ya dead! */
1049 static struct nf_conn *
1050 get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
1051 void *data, unsigned int *bucket)
1053 struct nf_conntrack_tuple_hash *h;
1056 write_lock_bh(&nf_conntrack_lock);
1057 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
1058 list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) {
1059 ct = nf_ct_tuplehash_to_ctrack(h);
1064 list_for_each_entry(h, &unconfirmed, list) {
1065 ct = nf_ct_tuplehash_to_ctrack(h);
1067 set_bit(IPS_DYING_BIT, &ct->status);
1069 write_unlock_bh(&nf_conntrack_lock);
1072 atomic_inc(&ct->ct_general.use);
1073 write_unlock_bh(&nf_conntrack_lock);
1078 nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
1081 unsigned int bucket = 0;
1083 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
1084 /* Time to push up daises... */
1085 if (del_timer(&ct->timeout))
1086 death_by_timeout((unsigned long)ct);
1087 /* ... else the timer will get him soon. */
1092 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
1094 static int kill_all(struct nf_conn *i, void *data)
1099 static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size)
1104 free_pages((unsigned long)hash,
1105 get_order(sizeof(struct list_head) * size));
1108 void nf_conntrack_flush(void)
1110 nf_ct_iterate_cleanup(kill_all, NULL);
1112 EXPORT_SYMBOL_GPL(nf_conntrack_flush);
1114 /* Mishearing the voices in his head, our hero wonders how he's
1115 supposed to kill the mall. */
1116 void nf_conntrack_cleanup(void)
1120 rcu_assign_pointer(ip_ct_attach, NULL);
1122 /* This makes sure all current packets have passed through
1123 netfilter framework. Roll on, two-stage module
1127 nf_ct_event_cache_flush();
1129 nf_conntrack_flush();
1130 if (atomic_read(&nf_conntrack_count) != 0) {
1132 goto i_see_dead_people;
1134 /* wait until all references to nf_conntrack_untracked are dropped */
1135 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1138 rcu_assign_pointer(nf_ct_destroy, NULL);
1140 for (i = 0; i < NF_CT_F_NUM; i++) {
1141 if (nf_ct_cache[i].use == 0)
1144 NF_CT_ASSERT(nf_ct_cache[i].use == 1);
1145 nf_ct_cache[i].use = 1;
1146 nf_conntrack_unregister_cache(i);
1148 kmem_cache_destroy(nf_conntrack_expect_cachep);
1149 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1150 nf_conntrack_htable_size);
1152 nf_conntrack_proto_fini();
1155 static struct list_head *alloc_hashtable(int size, int *vmalloced)
1157 struct list_head *hash;
1161 hash = (void*)__get_free_pages(GFP_KERNEL,
1162 get_order(sizeof(struct list_head)
1166 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
1167 hash = vmalloc(sizeof(struct list_head) * size);
1171 for (i = 0; i < size; i++)
1172 INIT_LIST_HEAD(&hash[i]);
1177 int set_hashsize(const char *val, struct kernel_param *kp)
1179 int i, bucket, hashsize, vmalloced;
1180 int old_vmalloced, old_size;
1182 struct list_head *hash, *old_hash;
1183 struct nf_conntrack_tuple_hash *h;
1185 /* On boot, we can set this without any fancy locking. */
1186 if (!nf_conntrack_htable_size)
1187 return param_set_uint(val, kp);
1189 hashsize = simple_strtol(val, NULL, 0);
1193 hash = alloc_hashtable(hashsize, &vmalloced);
1197 /* We have to rehahs for the new table anyway, so we also can
1198 * use a newrandom seed */
1199 get_random_bytes(&rnd, 4);
1201 write_lock_bh(&nf_conntrack_lock);
1202 for (i = 0; i < nf_conntrack_htable_size; i++) {
1203 while (!list_empty(&nf_conntrack_hash[i])) {
1204 h = list_entry(nf_conntrack_hash[i].next,
1205 struct nf_conntrack_tuple_hash, list);
1207 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1208 list_add_tail(&h->list, &hash[bucket]);
1211 old_size = nf_conntrack_htable_size;
1212 old_vmalloced = nf_conntrack_vmalloc;
1213 old_hash = nf_conntrack_hash;
1215 nf_conntrack_htable_size = hashsize;
1216 nf_conntrack_vmalloc = vmalloced;
1217 nf_conntrack_hash = hash;
1218 nf_conntrack_hash_rnd = rnd;
1219 write_unlock_bh(&nf_conntrack_lock);
1221 free_conntrack_hash(old_hash, old_vmalloced, old_size);
1225 module_param_call(hashsize, set_hashsize, param_get_uint,
1226 &nf_conntrack_htable_size, 0600);
1228 int __init nf_conntrack_init(void)
1232 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1233 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1234 if (!nf_conntrack_htable_size) {
1235 nf_conntrack_htable_size
1236 = (((num_physpages << PAGE_SHIFT) / 16384)
1237 / sizeof(struct list_head));
1238 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1239 nf_conntrack_htable_size = 8192;
1240 if (nf_conntrack_htable_size < 16)
1241 nf_conntrack_htable_size = 16;
1243 nf_conntrack_max = 8 * nf_conntrack_htable_size;
1245 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1246 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1249 nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size,
1250 &nf_conntrack_vmalloc);
1251 if (!nf_conntrack_hash) {
1252 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1256 ret = nf_conntrack_register_cache(NF_CT_F_BASIC, "nf_conntrack:basic",
1257 sizeof(struct nf_conn));
1259 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1263 nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect",
1264 sizeof(struct nf_conntrack_expect),
1266 if (!nf_conntrack_expect_cachep) {
1267 printk(KERN_ERR "Unable to create nf_expect slab cache\n");
1268 goto err_free_conntrack_slab;
1271 ret = nf_conntrack_proto_init();
1273 goto out_free_expect_slab;
1275 /* For use by REJECT target */
1276 rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach);
1277 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
1279 /* Set up fake conntrack:
1280 - to never be deleted, not in any hashes */
1281 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1282 /* - and look it like as a confirmed connection */
1283 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1287 out_free_expect_slab:
1288 kmem_cache_destroy(nf_conntrack_expect_cachep);
1289 err_free_conntrack_slab:
1290 nf_conntrack_unregister_cache(NF_CT_F_BASIC);
1292 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1293 nf_conntrack_htable_size);