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>
40 #define NF_CONNTRACK_VERSION "0.5.0"
45 #define DEBUGP(format, args...)
48 DEFINE_RWLOCK(nf_conntrack_lock);
49 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
51 /* nf_conntrack_standalone needs this */
52 atomic_t nf_conntrack_count = ATOMIC_INIT(0);
53 EXPORT_SYMBOL_GPL(nf_conntrack_count);
55 void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
56 EXPORT_SYMBOL_GPL(nf_conntrack_destroyed);
58 unsigned int nf_conntrack_htable_size __read_mostly;
59 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
61 int nf_conntrack_max __read_mostly;
62 EXPORT_SYMBOL_GPL(nf_conntrack_max);
64 struct list_head *nf_conntrack_hash __read_mostly;
65 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
67 struct nf_conn nf_conntrack_untracked __read_mostly;
68 EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
70 unsigned int nf_ct_log_invalid __read_mostly;
71 LIST_HEAD(unconfirmed);
72 static int nf_conntrack_vmalloc __read_mostly;
74 static unsigned int nf_conntrack_next_id;
76 DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
77 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
80 * This scheme offers various size of "struct nf_conn" dependent on
81 * features(helper, nat, ...)
84 #define NF_CT_FEATURES_NAMELEN 256
86 /* name of slab cache. printed in /proc/slabinfo */
89 /* size of slab cache */
92 /* slab cache pointer */
93 struct kmem_cache *cachep;
95 /* allocated slab cache + modules which uses this slab cache */
98 } nf_ct_cache[NF_CT_F_NUM];
100 /* protect members of nf_ct_cache except of "use" */
101 DEFINE_RWLOCK(nf_ct_cache_lock);
103 /* This avoids calling kmem_cache_create() with same name simultaneously */
104 static DEFINE_MUTEX(nf_ct_cache_mutex);
106 static int nf_conntrack_hash_rnd_initted;
107 static unsigned int nf_conntrack_hash_rnd;
109 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
110 unsigned int size, unsigned int rnd)
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);
119 return jhash_2words(a, b, rnd) % size;
122 static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
124 return __hash_conntrack(tuple, nf_conntrack_htable_size,
125 nf_conntrack_hash_rnd);
128 int nf_conntrack_register_cache(u_int32_t features, const char *name,
133 struct kmem_cache *cachep;
135 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
136 features, name, size);
138 if (features < NF_CT_F_BASIC || features >= NF_CT_F_NUM) {
139 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
144 mutex_lock(&nf_ct_cache_mutex);
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))
152 && nf_ct_cache[features].size == size) {
153 DEBUGP("nf_conntrack_register_cache: reusing.\n");
154 nf_ct_cache[features].use++;
159 write_unlock_bh(&nf_ct_cache_lock);
160 mutex_unlock(&nf_ct_cache_mutex);
163 write_unlock_bh(&nf_ct_cache_lock);
166 * The memory space for name of slab cache must be alive until
167 * cache is destroyed.
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");
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");
183 cachep = kmem_cache_create(cache_name, size, 0, 0,
186 printk("nf_conntrack_register_cache: Can't create slab cache "
187 "for the features = 0x%x\n", features);
192 write_lock_bh(&nf_ct_cache_lock);
193 nf_ct_cache[features].use = 1;
194 nf_ct_cache[features].size = size;
195 nf_ct_cache[features].cachep = cachep;
196 nf_ct_cache[features].name = cache_name;
197 write_unlock_bh(&nf_ct_cache_lock);
204 mutex_unlock(&nf_ct_cache_mutex);
207 EXPORT_SYMBOL_GPL(nf_conntrack_register_cache);
209 /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
210 void nf_conntrack_unregister_cache(u_int32_t features)
212 struct kmem_cache *cachep;
216 * This assures that kmem_cache_create() isn't called before destroying
219 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features);
220 mutex_lock(&nf_ct_cache_mutex);
222 write_lock_bh(&nf_ct_cache_lock);
223 if (--nf_ct_cache[features].use > 0) {
224 write_unlock_bh(&nf_ct_cache_lock);
225 mutex_unlock(&nf_ct_cache_mutex);
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;
232 nf_ct_cache[features].size = 0;
233 write_unlock_bh(&nf_ct_cache_lock);
237 kmem_cache_destroy(cachep);
240 mutex_unlock(&nf_ct_cache_mutex);
242 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_cache);
245 nf_ct_get_tuple(const struct sk_buff *skb,
247 unsigned int dataoff,
250 struct nf_conntrack_tuple *tuple,
251 const struct nf_conntrack_l3proto *l3proto,
252 const struct nf_conntrack_l4proto *l4proto)
254 NF_CT_TUPLE_U_BLANK(tuple);
256 tuple->src.l3num = l3num;
257 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
260 tuple->dst.protonum = protonum;
261 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
263 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
265 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
268 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
269 const struct nf_conntrack_tuple *orig,
270 const struct nf_conntrack_l3proto *l3proto,
271 const struct nf_conntrack_l4proto *l4proto)
273 NF_CT_TUPLE_U_BLANK(inverse);
275 inverse->src.l3num = orig->src.l3num;
276 if (l3proto->invert_tuple(inverse, orig) == 0)
279 inverse->dst.dir = !orig->dst.dir;
281 inverse->dst.protonum = orig->dst.protonum;
282 return l4proto->invert_tuple(inverse, orig);
284 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
287 clean_from_lists(struct nf_conn *ct)
289 DEBUGP("clean_from_lists(%p)\n", ct);
290 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
291 list_del(&ct->tuplehash[IP_CT_DIR_REPLY].list);
293 /* Destroy all pending expectations */
294 nf_ct_remove_expectations(ct);
298 destroy_conntrack(struct nf_conntrack *nfct)
300 struct nf_conn *ct = (struct nf_conn *)nfct;
301 struct nf_conn_help *help = nfct_help(ct);
302 struct nf_conntrack_l3proto *l3proto;
303 struct nf_conntrack_l4proto *l4proto;
304 typeof(nf_conntrack_destroyed) destroyed;
306 DEBUGP("destroy_conntrack(%p)\n", ct);
307 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
308 NF_CT_ASSERT(!timer_pending(&ct->timeout));
310 nf_conntrack_event(IPCT_DESTROY, ct);
311 set_bit(IPS_DYING_BIT, &ct->status);
313 if (help && help->helper && help->helper->destroy)
314 help->helper->destroy(ct);
316 /* To make sure we don't get any weird locking issues here:
317 * destroy_conntrack() MUST NOT be called with a write lock
318 * to nf_conntrack_lock!!! -HW */
320 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num);
321 if (l3proto && l3proto->destroy)
322 l3proto->destroy(ct);
324 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
325 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
326 if (l4proto && l4proto->destroy)
327 l4proto->destroy(ct);
329 destroyed = rcu_dereference(nf_conntrack_destroyed);
335 write_lock_bh(&nf_conntrack_lock);
336 /* Expectations will have been removed in clean_from_lists,
337 * except TFTP can create an expectation on the first packet,
338 * before connection is in the list, so we need to clean here,
340 nf_ct_remove_expectations(ct);
342 /* We overload first tuple to link into unconfirmed list. */
343 if (!nf_ct_is_confirmed(ct)) {
344 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
345 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
348 NF_CT_STAT_INC(delete);
349 write_unlock_bh(&nf_conntrack_lock);
352 nf_ct_put(ct->master);
354 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
355 nf_conntrack_free(ct);
358 static void death_by_timeout(unsigned long ul_conntrack)
360 struct nf_conn *ct = (void *)ul_conntrack;
362 write_lock_bh(&nf_conntrack_lock);
363 /* Inside lock so preempt is disabled on module removal path.
364 * Otherwise we can get spurious warnings. */
365 NF_CT_STAT_INC(delete_list);
366 clean_from_lists(ct);
367 write_unlock_bh(&nf_conntrack_lock);
371 struct nf_conntrack_tuple_hash *
372 __nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
373 const struct nf_conn *ignored_conntrack)
375 struct nf_conntrack_tuple_hash *h;
376 unsigned int hash = hash_conntrack(tuple);
378 list_for_each_entry(h, &nf_conntrack_hash[hash], list) {
379 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
380 nf_ct_tuple_equal(tuple, &h->tuple)) {
381 NF_CT_STAT_INC(found);
384 NF_CT_STAT_INC(searched);
389 EXPORT_SYMBOL_GPL(__nf_conntrack_find);
391 /* Find a connection corresponding to a tuple. */
392 struct nf_conntrack_tuple_hash *
393 nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple,
394 const struct nf_conn *ignored_conntrack)
396 struct nf_conntrack_tuple_hash *h;
398 read_lock_bh(&nf_conntrack_lock);
399 h = __nf_conntrack_find(tuple, ignored_conntrack);
401 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
402 read_unlock_bh(&nf_conntrack_lock);
406 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
408 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
410 unsigned int repl_hash)
412 ct->id = ++nf_conntrack_next_id;
413 list_add(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list,
414 &nf_conntrack_hash[hash]);
415 list_add(&ct->tuplehash[IP_CT_DIR_REPLY].list,
416 &nf_conntrack_hash[repl_hash]);
419 void nf_conntrack_hash_insert(struct nf_conn *ct)
421 unsigned int hash, repl_hash;
423 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
424 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
426 write_lock_bh(&nf_conntrack_lock);
427 __nf_conntrack_hash_insert(ct, hash, repl_hash);
428 write_unlock_bh(&nf_conntrack_lock);
430 EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
432 /* Confirm a connection given skb; places it in hash table */
434 __nf_conntrack_confirm(struct sk_buff **pskb)
436 unsigned int hash, repl_hash;
437 struct nf_conntrack_tuple_hash *h;
439 struct nf_conn_help *help;
440 enum ip_conntrack_info ctinfo;
442 ct = nf_ct_get(*pskb, &ctinfo);
444 /* ipt_REJECT uses nf_conntrack_attach to attach related
445 ICMP/TCP RST packets in other direction. Actual packet
446 which created connection will be IP_CT_NEW or for an
447 expected connection, IP_CT_RELATED. */
448 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
451 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
452 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
454 /* We're not in hash table, and we refuse to set up related
455 connections for unconfirmed conns. But packet copies and
456 REJECT will give spurious warnings here. */
457 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
459 /* No external references means noone else could have
461 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
462 DEBUGP("Confirming conntrack %p\n", ct);
464 write_lock_bh(&nf_conntrack_lock);
466 /* See if there's one in the list already, including reverse:
467 NAT could have grabbed it without realizing, since we're
468 not in the hash. If there is, we lost race. */
469 list_for_each_entry(h, &nf_conntrack_hash[hash], list)
470 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
473 list_for_each_entry(h, &nf_conntrack_hash[repl_hash], list)
474 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
478 /* Remove from unconfirmed list */
479 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);
481 __nf_conntrack_hash_insert(ct, hash, repl_hash);
482 /* Timer relative to confirmation time, not original
483 setting time, otherwise we'd get timer wrap in
484 weird delay cases. */
485 ct->timeout.expires += jiffies;
486 add_timer(&ct->timeout);
487 atomic_inc(&ct->ct_general.use);
488 set_bit(IPS_CONFIRMED_BIT, &ct->status);
489 NF_CT_STAT_INC(insert);
490 write_unlock_bh(&nf_conntrack_lock);
491 help = nfct_help(ct);
492 if (help && help->helper)
493 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
494 #ifdef CONFIG_NF_NAT_NEEDED
495 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
496 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
497 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
499 nf_conntrack_event_cache(master_ct(ct) ?
500 IPCT_RELATED : IPCT_NEW, *pskb);
504 NF_CT_STAT_INC(insert_failed);
505 write_unlock_bh(&nf_conntrack_lock);
508 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
510 /* Returns true if a connection correspondings to the tuple (required
513 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
514 const struct nf_conn *ignored_conntrack)
516 struct nf_conntrack_tuple_hash *h;
518 read_lock_bh(&nf_conntrack_lock);
519 h = __nf_conntrack_find(tuple, ignored_conntrack);
520 read_unlock_bh(&nf_conntrack_lock);
524 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
526 /* There's a small race here where we may free a just-assured
527 connection. Too bad: we're in trouble anyway. */
528 static int early_drop(struct list_head *chain)
530 /* Traverse backwards: gives us oldest, which is roughly LRU */
531 struct nf_conntrack_tuple_hash *h;
532 struct nf_conn *ct = NULL, *tmp;
535 read_lock_bh(&nf_conntrack_lock);
536 list_for_each_entry_reverse(h, chain, list) {
537 tmp = nf_ct_tuplehash_to_ctrack(h);
538 if (!test_bit(IPS_ASSURED_BIT, &tmp->status)) {
540 atomic_inc(&ct->ct_general.use);
544 read_unlock_bh(&nf_conntrack_lock);
549 if (del_timer(&ct->timeout)) {
550 death_by_timeout((unsigned long)ct);
552 NF_CT_STAT_INC_ATOMIC(early_drop);
558 static struct nf_conn *
559 __nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
560 const struct nf_conntrack_tuple *repl,
561 const struct nf_conntrack_l3proto *l3proto,
564 struct nf_conn *conntrack = NULL;
565 struct nf_conntrack_helper *helper;
567 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
568 get_random_bytes(&nf_conntrack_hash_rnd, 4);
569 nf_conntrack_hash_rnd_initted = 1;
572 /* We don't want any race condition at early drop stage */
573 atomic_inc(&nf_conntrack_count);
576 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
577 unsigned int hash = hash_conntrack(orig);
578 /* Try dropping from this hash chain. */
579 if (!early_drop(&nf_conntrack_hash[hash])) {
580 atomic_dec(&nf_conntrack_count);
583 "nf_conntrack: table full, dropping"
585 return ERR_PTR(-ENOMEM);
589 /* find features needed by this conntrack. */
590 features |= l3proto->get_features(orig);
592 /* FIXME: protect helper list per RCU */
593 read_lock_bh(&nf_conntrack_lock);
594 helper = __nf_ct_helper_find(repl);
595 /* NAT might want to assign a helper later */
596 if (helper || features & NF_CT_F_NAT)
597 features |= NF_CT_F_HELP;
598 read_unlock_bh(&nf_conntrack_lock);
600 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features);
602 read_lock_bh(&nf_ct_cache_lock);
604 if (unlikely(!nf_ct_cache[features].use)) {
605 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
610 conntrack = kmem_cache_alloc(nf_ct_cache[features].cachep, GFP_ATOMIC);
611 if (conntrack == NULL) {
612 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
616 memset(conntrack, 0, nf_ct_cache[features].size);
617 conntrack->features = features;
618 atomic_set(&conntrack->ct_general.use, 1);
619 conntrack->ct_general.destroy = destroy_conntrack;
620 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
621 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
622 /* Don't set timer yet: wait for confirmation */
623 init_timer(&conntrack->timeout);
624 conntrack->timeout.data = (unsigned long)conntrack;
625 conntrack->timeout.function = death_by_timeout;
626 read_unlock_bh(&nf_ct_cache_lock);
630 read_unlock_bh(&nf_ct_cache_lock);
631 atomic_dec(&nf_conntrack_count);
635 struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
636 const struct nf_conntrack_tuple *repl)
638 struct nf_conntrack_l3proto *l3proto;
642 l3proto = __nf_ct_l3proto_find(orig->src.l3num);
643 ct = __nf_conntrack_alloc(orig, repl, l3proto, 0);
648 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
650 void nf_conntrack_free(struct nf_conn *conntrack)
652 u_int32_t features = conntrack->features;
653 NF_CT_ASSERT(features >= NF_CT_F_BASIC && features < NF_CT_F_NUM);
654 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features,
656 kmem_cache_free(nf_ct_cache[features].cachep, conntrack);
657 atomic_dec(&nf_conntrack_count);
659 EXPORT_SYMBOL_GPL(nf_conntrack_free);
661 /* Allocate a new conntrack: we return -ENOMEM if classification
662 failed due to stress. Otherwise it really is unclassifiable. */
663 static struct nf_conntrack_tuple_hash *
664 init_conntrack(const struct nf_conntrack_tuple *tuple,
665 struct nf_conntrack_l3proto *l3proto,
666 struct nf_conntrack_l4proto *l4proto,
668 unsigned int dataoff)
670 struct nf_conn *conntrack;
671 struct nf_conntrack_tuple repl_tuple;
672 struct nf_conntrack_expect *exp;
673 u_int32_t features = 0;
675 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
676 DEBUGP("Can't invert tuple.\n");
680 read_lock_bh(&nf_conntrack_lock);
681 exp = __nf_conntrack_expect_find(tuple);
682 if (exp && exp->helper)
683 features = NF_CT_F_HELP;
684 read_unlock_bh(&nf_conntrack_lock);
686 conntrack = __nf_conntrack_alloc(tuple, &repl_tuple, l3proto, features);
687 if (conntrack == NULL || IS_ERR(conntrack)) {
688 DEBUGP("Can't allocate conntrack.\n");
689 return (struct nf_conntrack_tuple_hash *)conntrack;
692 if (!l4proto->new(conntrack, skb, dataoff)) {
693 nf_conntrack_free(conntrack);
694 DEBUGP("init conntrack: can't track with proto module\n");
698 write_lock_bh(&nf_conntrack_lock);
699 exp = find_expectation(tuple);
702 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
704 /* Welcome, Mr. Bond. We've been expecting you... */
705 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
706 conntrack->master = exp->master;
708 nfct_help(conntrack)->helper = exp->helper;
709 #ifdef CONFIG_NF_CONNTRACK_MARK
710 conntrack->mark = exp->master->mark;
712 #ifdef CONFIG_NF_CONNTRACK_SECMARK
713 conntrack->secmark = exp->master->secmark;
715 nf_conntrack_get(&conntrack->master->ct_general);
716 NF_CT_STAT_INC(expect_new);
718 struct nf_conn_help *help = nfct_help(conntrack);
721 help->helper = __nf_ct_helper_find(&repl_tuple);
725 /* Overload tuple linked list to put us in unconfirmed list. */
726 list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
728 write_unlock_bh(&nf_conntrack_lock);
732 exp->expectfn(conntrack, exp);
733 nf_conntrack_expect_put(exp);
736 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
739 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
740 static inline struct nf_conn *
741 resolve_normal_ct(struct sk_buff *skb,
742 unsigned int dataoff,
745 struct nf_conntrack_l3proto *l3proto,
746 struct nf_conntrack_l4proto *l4proto,
748 enum ip_conntrack_info *ctinfo)
750 struct nf_conntrack_tuple tuple;
751 struct nf_conntrack_tuple_hash *h;
754 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
755 dataoff, l3num, protonum, &tuple, l3proto,
757 DEBUGP("resolve_normal_ct: Can't get tuple\n");
761 /* look for tuple match */
762 h = nf_conntrack_find_get(&tuple, NULL);
764 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
770 ct = nf_ct_tuplehash_to_ctrack(h);
772 /* It exists; we have (non-exclusive) reference. */
773 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
774 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
775 /* Please set reply bit if this packet OK */
778 /* Once we've had two way comms, always ESTABLISHED. */
779 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
780 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
781 *ctinfo = IP_CT_ESTABLISHED;
782 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
783 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
784 *ctinfo = IP_CT_RELATED;
786 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
791 skb->nfct = &ct->ct_general;
792 skb->nfctinfo = *ctinfo;
797 nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
800 enum ip_conntrack_info ctinfo;
801 struct nf_conntrack_l3proto *l3proto;
802 struct nf_conntrack_l4proto *l4proto;
803 unsigned int dataoff;
808 /* Previously seen (loopback or untracked)? Ignore. */
810 NF_CT_STAT_INC_ATOMIC(ignore);
814 /* rcu_read_lock()ed by nf_hook_slow */
815 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
817 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
818 DEBUGP("not prepared to track yet or error occured\n");
822 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
824 /* It may be an special packet, error, unclean...
825 * inverse of the return code tells to the netfilter
826 * core what to do with the packet. */
827 if (l4proto->error != NULL &&
828 (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
829 NF_CT_STAT_INC_ATOMIC(error);
830 NF_CT_STAT_INC_ATOMIC(invalid);
834 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto,
835 &set_reply, &ctinfo);
837 /* Not valid part of a connection */
838 NF_CT_STAT_INC_ATOMIC(invalid);
843 /* Too stressed to deal. */
844 NF_CT_STAT_INC_ATOMIC(drop);
848 NF_CT_ASSERT((*pskb)->nfct);
850 ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
852 /* Invalid: inverse of the return code tells
853 * the netfilter core what to do */
854 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
855 nf_conntrack_put((*pskb)->nfct);
856 (*pskb)->nfct = NULL;
857 NF_CT_STAT_INC_ATOMIC(invalid);
861 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
862 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
866 EXPORT_SYMBOL_GPL(nf_conntrack_in);
868 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
869 const struct nf_conntrack_tuple *orig)
874 ret = nf_ct_invert_tuple(inverse, orig,
875 __nf_ct_l3proto_find(orig->src.l3num),
876 __nf_ct_l4proto_find(orig->src.l3num,
877 orig->dst.protonum));
881 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
883 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
884 implicitly racy: see __nf_conntrack_confirm */
885 void nf_conntrack_alter_reply(struct nf_conn *ct,
886 const struct nf_conntrack_tuple *newreply)
888 struct nf_conn_help *help = nfct_help(ct);
890 write_lock_bh(&nf_conntrack_lock);
891 /* Should be unconfirmed, so not in hash table yet */
892 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
894 DEBUGP("Altering reply tuple of %p to ", ct);
895 NF_CT_DUMP_TUPLE(newreply);
897 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
898 if (!ct->master && help && help->expecting == 0)
899 help->helper = __nf_ct_helper_find(newreply);
900 write_unlock_bh(&nf_conntrack_lock);
902 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
904 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
905 void __nf_ct_refresh_acct(struct nf_conn *ct,
906 enum ip_conntrack_info ctinfo,
907 const struct sk_buff *skb,
908 unsigned long extra_jiffies,
913 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
916 write_lock_bh(&nf_conntrack_lock);
918 /* Only update if this is not a fixed timeout */
919 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
920 write_unlock_bh(&nf_conntrack_lock);
924 /* If not in hash table, timer will not be active yet */
925 if (!nf_ct_is_confirmed(ct)) {
926 ct->timeout.expires = extra_jiffies;
927 event = IPCT_REFRESH;
929 unsigned long newtime = jiffies + extra_jiffies;
931 /* Only update the timeout if the new timeout is at least
932 HZ jiffies from the old timeout. Need del_timer for race
933 avoidance (may already be dying). */
934 if (newtime - ct->timeout.expires >= HZ
935 && del_timer(&ct->timeout)) {
936 ct->timeout.expires = newtime;
937 add_timer(&ct->timeout);
938 event = IPCT_REFRESH;
942 #ifdef CONFIG_NF_CT_ACCT
944 ct->counters[CTINFO2DIR(ctinfo)].packets++;
945 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
946 skb->len - skb_network_offset(skb);
948 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
949 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
950 event |= IPCT_COUNTER_FILLING;
954 write_unlock_bh(&nf_conntrack_lock);
956 /* must be unlocked when calling event cache */
958 nf_conntrack_event_cache(event, skb);
960 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
962 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
964 #include <linux/netfilter/nfnetlink.h>
965 #include <linux/netfilter/nfnetlink_conntrack.h>
966 #include <linux/mutex.h>
969 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
970 * in ip_conntrack_core, since we don't want the protocols to autoload
971 * or depend on ctnetlink */
972 int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
973 const struct nf_conntrack_tuple *tuple)
975 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
976 &tuple->src.u.tcp.port);
977 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
978 &tuple->dst.u.tcp.port);
984 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
986 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
987 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
988 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
991 int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
992 struct nf_conntrack_tuple *t)
994 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
997 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
1000 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
1001 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
1005 EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
1008 /* Used by ipt_REJECT and ip6t_REJECT. */
1009 void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
1012 enum ip_conntrack_info ctinfo;
1014 /* This ICMP is in reverse direction to the packet which caused it */
1015 ct = nf_ct_get(skb, &ctinfo);
1016 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1017 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
1019 ctinfo = IP_CT_RELATED;
1021 /* Attach to new skbuff, and increment count */
1022 nskb->nfct = &ct->ct_general;
1023 nskb->nfctinfo = ctinfo;
1024 nf_conntrack_get(nskb->nfct);
1026 EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
1029 do_iter(const struct nf_conntrack_tuple_hash *i,
1030 int (*iter)(struct nf_conn *i, void *data),
1033 return iter(nf_ct_tuplehash_to_ctrack(i), data);
1036 /* Bring out ya dead! */
1037 static struct nf_conn *
1038 get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
1039 void *data, unsigned int *bucket)
1041 struct nf_conntrack_tuple_hash *h;
1044 write_lock_bh(&nf_conntrack_lock);
1045 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
1046 list_for_each_entry(h, &nf_conntrack_hash[*bucket], list) {
1047 ct = nf_ct_tuplehash_to_ctrack(h);
1052 list_for_each_entry(h, &unconfirmed, list) {
1053 ct = nf_ct_tuplehash_to_ctrack(h);
1055 set_bit(IPS_DYING_BIT, &ct->status);
1057 write_unlock_bh(&nf_conntrack_lock);
1060 atomic_inc(&ct->ct_general.use);
1061 write_unlock_bh(&nf_conntrack_lock);
1066 nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
1069 unsigned int bucket = 0;
1071 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
1072 /* Time to push up daises... */
1073 if (del_timer(&ct->timeout))
1074 death_by_timeout((unsigned long)ct);
1075 /* ... else the timer will get him soon. */
1080 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
1082 static int kill_all(struct nf_conn *i, void *data)
1087 static void free_conntrack_hash(struct list_head *hash, int vmalloced, int size)
1092 free_pages((unsigned long)hash,
1093 get_order(sizeof(struct list_head) * size));
1096 void nf_conntrack_flush(void)
1098 nf_ct_iterate_cleanup(kill_all, NULL);
1100 EXPORT_SYMBOL_GPL(nf_conntrack_flush);
1102 /* Mishearing the voices in his head, our hero wonders how he's
1103 supposed to kill the mall. */
1104 void nf_conntrack_cleanup(void)
1108 rcu_assign_pointer(ip_ct_attach, NULL);
1110 /* This makes sure all current packets have passed through
1111 netfilter framework. Roll on, two-stage module
1115 nf_ct_event_cache_flush();
1117 nf_conntrack_flush();
1118 if (atomic_read(&nf_conntrack_count) != 0) {
1120 goto i_see_dead_people;
1122 /* wait until all references to nf_conntrack_untracked are dropped */
1123 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1126 for (i = 0; i < NF_CT_F_NUM; i++) {
1127 if (nf_ct_cache[i].use == 0)
1130 NF_CT_ASSERT(nf_ct_cache[i].use == 1);
1131 nf_ct_cache[i].use = 1;
1132 nf_conntrack_unregister_cache(i);
1134 kmem_cache_destroy(nf_conntrack_expect_cachep);
1135 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1136 nf_conntrack_htable_size);
1138 nf_conntrack_proto_fini();
1141 static struct list_head *alloc_hashtable(int size, int *vmalloced)
1143 struct list_head *hash;
1147 hash = (void*)__get_free_pages(GFP_KERNEL,
1148 get_order(sizeof(struct list_head)
1152 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
1153 hash = vmalloc(sizeof(struct list_head) * size);
1157 for (i = 0; i < size; i++)
1158 INIT_LIST_HEAD(&hash[i]);
1163 int set_hashsize(const char *val, struct kernel_param *kp)
1165 int i, bucket, hashsize, vmalloced;
1166 int old_vmalloced, old_size;
1168 struct list_head *hash, *old_hash;
1169 struct nf_conntrack_tuple_hash *h;
1171 /* On boot, we can set this without any fancy locking. */
1172 if (!nf_conntrack_htable_size)
1173 return param_set_uint(val, kp);
1175 hashsize = simple_strtol(val, NULL, 0);
1179 hash = alloc_hashtable(hashsize, &vmalloced);
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);
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);
1193 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1194 list_add_tail(&h->list, &hash[bucket]);
1197 old_size = nf_conntrack_htable_size;
1198 old_vmalloced = nf_conntrack_vmalloc;
1199 old_hash = nf_conntrack_hash;
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);
1207 free_conntrack_hash(old_hash, old_vmalloced, old_size);
1211 module_param_call(hashsize, set_hashsize, param_get_uint,
1212 &nf_conntrack_htable_size, 0600);
1214 int __init nf_conntrack_init(void)
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;
1229 nf_conntrack_max = 8 * nf_conntrack_htable_size;
1231 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1232 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
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");
1242 ret = nf_conntrack_register_cache(NF_CT_F_BASIC, "nf_conntrack:basic",
1243 sizeof(struct nf_conn));
1245 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1249 nf_conntrack_expect_cachep = kmem_cache_create("nf_conntrack_expect",
1250 sizeof(struct nf_conntrack_expect),
1252 if (!nf_conntrack_expect_cachep) {
1253 printk(KERN_ERR "Unable to create nf_expect slab cache\n");
1254 goto err_free_conntrack_slab;
1257 ret = nf_conntrack_proto_init();
1259 goto out_free_expect_slab;
1261 /* For use by REJECT target */
1262 rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach);
1264 /* Set up fake conntrack:
1265 - to never be deleted, not in any hashes */
1266 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1267 /* - and look it like as a confirmed connection */
1268 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1272 out_free_expect_slab:
1273 kmem_cache_destroy(nf_conntrack_expect_cachep);
1274 err_free_conntrack_slab:
1275 nf_conntrack_unregister_cache(NF_CT_F_BASIC);
1277 free_conntrack_hash(nf_conntrack_hash, nf_conntrack_vmalloc,
1278 nf_conntrack_htable_size);