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 unsigned int nf_conntrack_htable_size __read_mostly;
57 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
59 int nf_conntrack_max __read_mostly;
60 EXPORT_SYMBOL_GPL(nf_conntrack_max);
62 struct hlist_head *nf_conntrack_hash __read_mostly;
63 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
65 struct nf_conn nf_conntrack_untracked __read_mostly;
66 EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
68 unsigned int nf_ct_log_invalid __read_mostly;
69 HLIST_HEAD(unconfirmed);
70 static int nf_conntrack_vmalloc __read_mostly;
71 static struct kmem_cache *nf_conntrack_cachep __read_mostly;
72 static unsigned int nf_conntrack_next_id;
74 DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
75 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat);
77 static int nf_conntrack_hash_rnd_initted;
78 static unsigned int nf_conntrack_hash_rnd;
80 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
81 unsigned int size, unsigned int rnd)
85 a = jhash2(tuple->src.u3.all, ARRAY_SIZE(tuple->src.u3.all),
86 (tuple->src.l3num << 16) | tuple->dst.protonum);
87 b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
88 (tuple->src.u.all << 16) | tuple->dst.u.all);
90 return jhash_2words(a, b, rnd) % size;
93 static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
95 return __hash_conntrack(tuple, nf_conntrack_htable_size,
96 nf_conntrack_hash_rnd);
100 nf_ct_get_tuple(const struct sk_buff *skb,
102 unsigned int dataoff,
105 struct nf_conntrack_tuple *tuple,
106 const struct nf_conntrack_l3proto *l3proto,
107 const struct nf_conntrack_l4proto *l4proto)
109 NF_CT_TUPLE_U_BLANK(tuple);
111 tuple->src.l3num = l3num;
112 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
115 tuple->dst.protonum = protonum;
116 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
118 return l4proto->pkt_to_tuple(skb, dataoff, tuple);
120 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
123 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
124 const struct nf_conntrack_tuple *orig,
125 const struct nf_conntrack_l3proto *l3proto,
126 const struct nf_conntrack_l4proto *l4proto)
128 NF_CT_TUPLE_U_BLANK(inverse);
130 inverse->src.l3num = orig->src.l3num;
131 if (l3proto->invert_tuple(inverse, orig) == 0)
134 inverse->dst.dir = !orig->dst.dir;
136 inverse->dst.protonum = orig->dst.protonum;
137 return l4proto->invert_tuple(inverse, orig);
139 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
142 clean_from_lists(struct nf_conn *ct)
144 DEBUGP("clean_from_lists(%p)\n", ct);
145 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
146 hlist_del(&ct->tuplehash[IP_CT_DIR_REPLY].hnode);
148 /* Destroy all pending expectations */
149 nf_ct_remove_expectations(ct);
153 destroy_conntrack(struct nf_conntrack *nfct)
155 struct nf_conn *ct = (struct nf_conn *)nfct;
156 struct nf_conntrack_l4proto *l4proto;
158 DEBUGP("destroy_conntrack(%p)\n", ct);
159 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
160 NF_CT_ASSERT(!timer_pending(&ct->timeout));
162 nf_conntrack_event(IPCT_DESTROY, ct);
163 set_bit(IPS_DYING_BIT, &ct->status);
165 /* To make sure we don't get any weird locking issues here:
166 * destroy_conntrack() MUST NOT be called with a write lock
167 * to nf_conntrack_lock!!! -HW */
169 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.l3num,
170 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
171 if (l4proto && l4proto->destroy)
172 l4proto->destroy(ct);
174 nf_ct_ext_destroy(ct);
178 write_lock_bh(&nf_conntrack_lock);
179 /* Expectations will have been removed in clean_from_lists,
180 * except TFTP can create an expectation on the first packet,
181 * before connection is in the list, so we need to clean here,
183 nf_ct_remove_expectations(ct);
185 /* We overload first tuple to link into unconfirmed list. */
186 if (!nf_ct_is_confirmed(ct)) {
187 BUG_ON(hlist_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode));
188 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
191 NF_CT_STAT_INC(delete);
192 write_unlock_bh(&nf_conntrack_lock);
195 nf_ct_put(ct->master);
197 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
198 nf_conntrack_free(ct);
201 static void death_by_timeout(unsigned long ul_conntrack)
203 struct nf_conn *ct = (void *)ul_conntrack;
204 struct nf_conn_help *help = nfct_help(ct);
205 struct nf_conntrack_helper *helper;
209 helper = rcu_dereference(help->helper);
210 if (helper && helper->destroy)
215 write_lock_bh(&nf_conntrack_lock);
216 /* Inside lock so preempt is disabled on module removal path.
217 * Otherwise we can get spurious warnings. */
218 NF_CT_STAT_INC(delete_list);
219 clean_from_lists(ct);
220 write_unlock_bh(&nf_conntrack_lock);
224 struct nf_conntrack_tuple_hash *
225 __nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
226 const struct nf_conn *ignored_conntrack)
228 struct nf_conntrack_tuple_hash *h;
229 struct hlist_node *n;
230 unsigned int hash = hash_conntrack(tuple);
232 hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode) {
233 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
234 nf_ct_tuple_equal(tuple, &h->tuple)) {
235 NF_CT_STAT_INC(found);
238 NF_CT_STAT_INC(searched);
243 EXPORT_SYMBOL_GPL(__nf_conntrack_find);
245 /* Find a connection corresponding to a tuple. */
246 struct nf_conntrack_tuple_hash *
247 nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple)
249 struct nf_conntrack_tuple_hash *h;
251 read_lock_bh(&nf_conntrack_lock);
252 h = __nf_conntrack_find(tuple, NULL);
254 atomic_inc(&nf_ct_tuplehash_to_ctrack(h)->ct_general.use);
255 read_unlock_bh(&nf_conntrack_lock);
259 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
261 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
263 unsigned int repl_hash)
265 ct->id = ++nf_conntrack_next_id;
266 hlist_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode,
267 &nf_conntrack_hash[hash]);
268 hlist_add_head(&ct->tuplehash[IP_CT_DIR_REPLY].hnode,
269 &nf_conntrack_hash[repl_hash]);
272 void nf_conntrack_hash_insert(struct nf_conn *ct)
274 unsigned int hash, repl_hash;
276 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
277 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
279 write_lock_bh(&nf_conntrack_lock);
280 __nf_conntrack_hash_insert(ct, hash, repl_hash);
281 write_unlock_bh(&nf_conntrack_lock);
283 EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
285 /* Confirm a connection given skb; places it in hash table */
287 __nf_conntrack_confirm(struct sk_buff **pskb)
289 unsigned int hash, repl_hash;
290 struct nf_conntrack_tuple_hash *h;
292 struct nf_conn_help *help;
293 struct hlist_node *n;
294 enum ip_conntrack_info ctinfo;
296 ct = nf_ct_get(*pskb, &ctinfo);
298 /* ipt_REJECT uses nf_conntrack_attach to attach related
299 ICMP/TCP RST packets in other direction. Actual packet
300 which created connection will be IP_CT_NEW or for an
301 expected connection, IP_CT_RELATED. */
302 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
305 hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
306 repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
308 /* We're not in hash table, and we refuse to set up related
309 connections for unconfirmed conns. But packet copies and
310 REJECT will give spurious warnings here. */
311 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
313 /* No external references means noone else could have
315 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
316 DEBUGP("Confirming conntrack %p\n", ct);
318 write_lock_bh(&nf_conntrack_lock);
320 /* See if there's one in the list already, including reverse:
321 NAT could have grabbed it without realizing, since we're
322 not in the hash. If there is, we lost race. */
323 hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode)
324 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
327 hlist_for_each_entry(h, n, &nf_conntrack_hash[repl_hash], hnode)
328 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
332 /* Remove from unconfirmed list */
333 hlist_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode);
335 __nf_conntrack_hash_insert(ct, hash, repl_hash);
336 /* Timer relative to confirmation time, not original
337 setting time, otherwise we'd get timer wrap in
338 weird delay cases. */
339 ct->timeout.expires += jiffies;
340 add_timer(&ct->timeout);
341 atomic_inc(&ct->ct_general.use);
342 set_bit(IPS_CONFIRMED_BIT, &ct->status);
343 NF_CT_STAT_INC(insert);
344 write_unlock_bh(&nf_conntrack_lock);
345 help = nfct_help(ct);
346 if (help && help->helper)
347 nf_conntrack_event_cache(IPCT_HELPER, *pskb);
348 #ifdef CONFIG_NF_NAT_NEEDED
349 if (test_bit(IPS_SRC_NAT_DONE_BIT, &ct->status) ||
350 test_bit(IPS_DST_NAT_DONE_BIT, &ct->status))
351 nf_conntrack_event_cache(IPCT_NATINFO, *pskb);
353 nf_conntrack_event_cache(master_ct(ct) ?
354 IPCT_RELATED : IPCT_NEW, *pskb);
358 NF_CT_STAT_INC(insert_failed);
359 write_unlock_bh(&nf_conntrack_lock);
362 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
364 /* Returns true if a connection correspondings to the tuple (required
367 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
368 const struct nf_conn *ignored_conntrack)
370 struct nf_conntrack_tuple_hash *h;
372 read_lock_bh(&nf_conntrack_lock);
373 h = __nf_conntrack_find(tuple, ignored_conntrack);
374 read_unlock_bh(&nf_conntrack_lock);
378 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
380 #define NF_CT_EVICTION_RANGE 8
382 /* There's a small race here where we may free a just-assured
383 connection. Too bad: we're in trouble anyway. */
384 static int early_drop(unsigned int hash)
386 /* Use oldest entry, which is roughly LRU */
387 struct nf_conntrack_tuple_hash *h;
388 struct nf_conn *ct = NULL, *tmp;
389 struct hlist_node *n;
390 unsigned int i, cnt = 0;
393 read_lock_bh(&nf_conntrack_lock);
394 for (i = 0; i < nf_conntrack_htable_size; i++) {
395 hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode) {
396 tmp = nf_ct_tuplehash_to_ctrack(h);
397 if (!test_bit(IPS_ASSURED_BIT, &tmp->status))
401 if (ct || cnt >= NF_CT_EVICTION_RANGE)
403 hash = (hash + 1) % nf_conntrack_htable_size;
406 atomic_inc(&ct->ct_general.use);
407 read_unlock_bh(&nf_conntrack_lock);
412 if (del_timer(&ct->timeout)) {
413 death_by_timeout((unsigned long)ct);
415 NF_CT_STAT_INC_ATOMIC(early_drop);
421 struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
422 const struct nf_conntrack_tuple *repl)
424 struct nf_conn *conntrack = NULL;
426 if (unlikely(!nf_conntrack_hash_rnd_initted)) {
427 get_random_bytes(&nf_conntrack_hash_rnd, 4);
428 nf_conntrack_hash_rnd_initted = 1;
431 /* We don't want any race condition at early drop stage */
432 atomic_inc(&nf_conntrack_count);
435 && atomic_read(&nf_conntrack_count) > nf_conntrack_max) {
436 unsigned int hash = hash_conntrack(orig);
437 if (!early_drop(hash)) {
438 atomic_dec(&nf_conntrack_count);
441 "nf_conntrack: table full, dropping"
443 return ERR_PTR(-ENOMEM);
447 conntrack = kmem_cache_zalloc(nf_conntrack_cachep, GFP_ATOMIC);
448 if (conntrack == NULL) {
449 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack.\n");
450 atomic_dec(&nf_conntrack_count);
451 return ERR_PTR(-ENOMEM);
454 atomic_set(&conntrack->ct_general.use, 1);
455 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
456 conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
457 /* Don't set timer yet: wait for confirmation */
458 setup_timer(&conntrack->timeout, death_by_timeout,
459 (unsigned long)conntrack);
463 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
465 void nf_conntrack_free(struct nf_conn *conntrack)
467 nf_ct_ext_free(conntrack);
468 kmem_cache_free(nf_conntrack_cachep, conntrack);
469 atomic_dec(&nf_conntrack_count);
471 EXPORT_SYMBOL_GPL(nf_conntrack_free);
473 /* Allocate a new conntrack: we return -ENOMEM if classification
474 failed due to stress. Otherwise it really is unclassifiable. */
475 static struct nf_conntrack_tuple_hash *
476 init_conntrack(const struct nf_conntrack_tuple *tuple,
477 struct nf_conntrack_l3proto *l3proto,
478 struct nf_conntrack_l4proto *l4proto,
480 unsigned int dataoff)
482 struct nf_conn *conntrack;
483 struct nf_conn_help *help;
484 struct nf_conntrack_tuple repl_tuple;
485 struct nf_conntrack_expect *exp;
487 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
488 DEBUGP("Can't invert tuple.\n");
492 conntrack = nf_conntrack_alloc(tuple, &repl_tuple);
493 if (conntrack == NULL || IS_ERR(conntrack)) {
494 DEBUGP("Can't allocate conntrack.\n");
495 return (struct nf_conntrack_tuple_hash *)conntrack;
498 if (!l4proto->new(conntrack, skb, dataoff)) {
499 nf_conntrack_free(conntrack);
500 DEBUGP("init conntrack: can't track with proto module\n");
504 write_lock_bh(&nf_conntrack_lock);
505 exp = nf_ct_find_expectation(tuple);
507 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
509 /* Welcome, Mr. Bond. We've been expecting you... */
510 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
511 conntrack->master = exp->master;
513 help = nf_ct_helper_ext_add(conntrack, GFP_ATOMIC);
515 rcu_assign_pointer(help->helper, exp->helper);
518 #ifdef CONFIG_NF_CONNTRACK_MARK
519 conntrack->mark = exp->master->mark;
521 #ifdef CONFIG_NF_CONNTRACK_SECMARK
522 conntrack->secmark = exp->master->secmark;
524 nf_conntrack_get(&conntrack->master->ct_general);
525 NF_CT_STAT_INC(expect_new);
527 struct nf_conntrack_helper *helper;
529 helper = __nf_ct_helper_find(&repl_tuple);
531 help = nf_ct_helper_ext_add(conntrack, GFP_ATOMIC);
533 rcu_assign_pointer(help->helper, helper);
538 /* Overload tuple linked list to put us in unconfirmed list. */
539 hlist_add_head(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].hnode,
542 write_unlock_bh(&nf_conntrack_lock);
546 exp->expectfn(conntrack, exp);
547 nf_ct_expect_put(exp);
550 return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
553 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
554 static inline struct nf_conn *
555 resolve_normal_ct(struct sk_buff *skb,
556 unsigned int dataoff,
559 struct nf_conntrack_l3proto *l3proto,
560 struct nf_conntrack_l4proto *l4proto,
562 enum ip_conntrack_info *ctinfo)
564 struct nf_conntrack_tuple tuple;
565 struct nf_conntrack_tuple_hash *h;
568 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
569 dataoff, l3num, protonum, &tuple, l3proto,
571 DEBUGP("resolve_normal_ct: Can't get tuple\n");
575 /* look for tuple match */
576 h = nf_conntrack_find_get(&tuple);
578 h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
584 ct = nf_ct_tuplehash_to_ctrack(h);
586 /* It exists; we have (non-exclusive) reference. */
587 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
588 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
589 /* Please set reply bit if this packet OK */
592 /* Once we've had two way comms, always ESTABLISHED. */
593 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
594 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct);
595 *ctinfo = IP_CT_ESTABLISHED;
596 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
597 DEBUGP("nf_conntrack_in: related packet for %p\n", ct);
598 *ctinfo = IP_CT_RELATED;
600 DEBUGP("nf_conntrack_in: new packet for %p\n", ct);
605 skb->nfct = &ct->ct_general;
606 skb->nfctinfo = *ctinfo;
611 nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff **pskb)
614 enum ip_conntrack_info ctinfo;
615 struct nf_conntrack_l3proto *l3proto;
616 struct nf_conntrack_l4proto *l4proto;
617 unsigned int dataoff;
622 /* Previously seen (loopback or untracked)? Ignore. */
624 NF_CT_STAT_INC_ATOMIC(ignore);
628 /* rcu_read_lock()ed by nf_hook_slow */
629 l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
631 if ((ret = l3proto->prepare(pskb, hooknum, &dataoff, &protonum)) <= 0) {
632 DEBUGP("not prepared to track yet or error occured\n");
636 l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
638 /* It may be an special packet, error, unclean...
639 * inverse of the return code tells to the netfilter
640 * core what to do with the packet. */
641 if (l4proto->error != NULL &&
642 (ret = l4proto->error(*pskb, dataoff, &ctinfo, pf, hooknum)) <= 0) {
643 NF_CT_STAT_INC_ATOMIC(error);
644 NF_CT_STAT_INC_ATOMIC(invalid);
648 ct = resolve_normal_ct(*pskb, dataoff, pf, protonum, l3proto, l4proto,
649 &set_reply, &ctinfo);
651 /* Not valid part of a connection */
652 NF_CT_STAT_INC_ATOMIC(invalid);
657 /* Too stressed to deal. */
658 NF_CT_STAT_INC_ATOMIC(drop);
662 NF_CT_ASSERT((*pskb)->nfct);
664 ret = l4proto->packet(ct, *pskb, dataoff, ctinfo, pf, hooknum);
666 /* Invalid: inverse of the return code tells
667 * the netfilter core what to do */
668 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
669 nf_conntrack_put((*pskb)->nfct);
670 (*pskb)->nfct = NULL;
671 NF_CT_STAT_INC_ATOMIC(invalid);
675 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
676 nf_conntrack_event_cache(IPCT_STATUS, *pskb);
680 EXPORT_SYMBOL_GPL(nf_conntrack_in);
682 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
683 const struct nf_conntrack_tuple *orig)
688 ret = nf_ct_invert_tuple(inverse, orig,
689 __nf_ct_l3proto_find(orig->src.l3num),
690 __nf_ct_l4proto_find(orig->src.l3num,
691 orig->dst.protonum));
695 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
697 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
698 implicitly racy: see __nf_conntrack_confirm */
699 void nf_conntrack_alter_reply(struct nf_conn *ct,
700 const struct nf_conntrack_tuple *newreply)
702 struct nf_conn_help *help = nfct_help(ct);
703 struct nf_conntrack_helper *helper;
705 write_lock_bh(&nf_conntrack_lock);
706 /* Should be unconfirmed, so not in hash table yet */
707 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
709 DEBUGP("Altering reply tuple of %p to ", ct);
710 NF_CT_DUMP_TUPLE(newreply);
712 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
713 if (ct->master || (help && help->expecting != 0))
716 helper = __nf_ct_helper_find(newreply);
717 if (helper == NULL) {
719 rcu_assign_pointer(help->helper, NULL);
724 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
728 memset(&help->help, 0, sizeof(help->help));
731 rcu_assign_pointer(help->helper, helper);
733 write_unlock_bh(&nf_conntrack_lock);
735 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
737 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
738 void __nf_ct_refresh_acct(struct nf_conn *ct,
739 enum ip_conntrack_info ctinfo,
740 const struct sk_buff *skb,
741 unsigned long extra_jiffies,
746 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
749 write_lock_bh(&nf_conntrack_lock);
751 /* Only update if this is not a fixed timeout */
752 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) {
753 write_unlock_bh(&nf_conntrack_lock);
757 /* If not in hash table, timer will not be active yet */
758 if (!nf_ct_is_confirmed(ct)) {
759 ct->timeout.expires = extra_jiffies;
760 event = IPCT_REFRESH;
762 unsigned long newtime = jiffies + extra_jiffies;
764 /* Only update the timeout if the new timeout is at least
765 HZ jiffies from the old timeout. Need del_timer for race
766 avoidance (may already be dying). */
767 if (newtime - ct->timeout.expires >= HZ
768 && del_timer(&ct->timeout)) {
769 ct->timeout.expires = newtime;
770 add_timer(&ct->timeout);
771 event = IPCT_REFRESH;
775 #ifdef CONFIG_NF_CT_ACCT
777 ct->counters[CTINFO2DIR(ctinfo)].packets++;
778 ct->counters[CTINFO2DIR(ctinfo)].bytes +=
779 skb->len - skb_network_offset(skb);
781 if ((ct->counters[CTINFO2DIR(ctinfo)].packets & 0x80000000)
782 || (ct->counters[CTINFO2DIR(ctinfo)].bytes & 0x80000000))
783 event |= IPCT_COUNTER_FILLING;
787 write_unlock_bh(&nf_conntrack_lock);
789 /* must be unlocked when calling event cache */
791 nf_conntrack_event_cache(event, skb);
793 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
795 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
797 #include <linux/netfilter/nfnetlink.h>
798 #include <linux/netfilter/nfnetlink_conntrack.h>
799 #include <linux/mutex.h>
802 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
803 * in ip_conntrack_core, since we don't want the protocols to autoload
804 * or depend on ctnetlink */
805 int nf_ct_port_tuple_to_nfattr(struct sk_buff *skb,
806 const struct nf_conntrack_tuple *tuple)
808 NFA_PUT(skb, CTA_PROTO_SRC_PORT, sizeof(u_int16_t),
809 &tuple->src.u.tcp.port);
810 NFA_PUT(skb, CTA_PROTO_DST_PORT, sizeof(u_int16_t),
811 &tuple->dst.u.tcp.port);
817 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr);
819 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
820 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
821 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t)
824 int nf_ct_port_nfattr_to_tuple(struct nfattr *tb[],
825 struct nf_conntrack_tuple *t)
827 if (!tb[CTA_PROTO_SRC_PORT-1] || !tb[CTA_PROTO_DST_PORT-1])
830 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
833 t->src.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_SRC_PORT-1]);
834 t->dst.u.tcp.port = *(__be16 *)NFA_DATA(tb[CTA_PROTO_DST_PORT-1]);
838 EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple);
841 /* Used by ipt_REJECT and ip6t_REJECT. */
842 void __nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
845 enum ip_conntrack_info ctinfo;
847 /* This ICMP is in reverse direction to the packet which caused it */
848 ct = nf_ct_get(skb, &ctinfo);
849 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
850 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
852 ctinfo = IP_CT_RELATED;
854 /* Attach to new skbuff, and increment count */
855 nskb->nfct = &ct->ct_general;
856 nskb->nfctinfo = ctinfo;
857 nf_conntrack_get(nskb->nfct);
859 EXPORT_SYMBOL_GPL(__nf_conntrack_attach);
862 do_iter(const struct nf_conntrack_tuple_hash *i,
863 int (*iter)(struct nf_conn *i, void *data),
866 return iter(nf_ct_tuplehash_to_ctrack(i), data);
869 /* Bring out ya dead! */
870 static struct nf_conn *
871 get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
872 void *data, unsigned int *bucket)
874 struct nf_conntrack_tuple_hash *h;
876 struct hlist_node *n;
878 write_lock_bh(&nf_conntrack_lock);
879 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
880 hlist_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnode) {
881 ct = nf_ct_tuplehash_to_ctrack(h);
886 hlist_for_each_entry(h, n, &unconfirmed, hnode) {
887 ct = nf_ct_tuplehash_to_ctrack(h);
889 set_bit(IPS_DYING_BIT, &ct->status);
891 write_unlock_bh(&nf_conntrack_lock);
894 atomic_inc(&ct->ct_general.use);
895 write_unlock_bh(&nf_conntrack_lock);
900 nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
903 unsigned int bucket = 0;
905 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
906 /* Time to push up daises... */
907 if (del_timer(&ct->timeout))
908 death_by_timeout((unsigned long)ct);
909 /* ... else the timer will get him soon. */
914 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
916 static int kill_all(struct nf_conn *i, void *data)
921 void nf_ct_free_hashtable(struct hlist_head *hash, int vmalloced, int size)
926 free_pages((unsigned long)hash,
927 get_order(sizeof(struct hlist_head) * size));
929 EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
931 void nf_conntrack_flush(void)
933 nf_ct_iterate_cleanup(kill_all, NULL);
935 EXPORT_SYMBOL_GPL(nf_conntrack_flush);
937 /* Mishearing the voices in his head, our hero wonders how he's
938 supposed to kill the mall. */
939 void nf_conntrack_cleanup(void)
941 rcu_assign_pointer(ip_ct_attach, NULL);
943 /* This makes sure all current packets have passed through
944 netfilter framework. Roll on, two-stage module
948 nf_ct_event_cache_flush();
950 nf_conntrack_flush();
951 if (atomic_read(&nf_conntrack_count) != 0) {
953 goto i_see_dead_people;
955 /* wait until all references to nf_conntrack_untracked are dropped */
956 while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
959 rcu_assign_pointer(nf_ct_destroy, NULL);
961 kmem_cache_destroy(nf_conntrack_cachep);
962 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
963 nf_conntrack_htable_size);
965 nf_conntrack_proto_fini();
966 nf_conntrack_helper_fini();
967 nf_conntrack_expect_fini();
970 struct hlist_head *nf_ct_alloc_hashtable(int *sizep, int *vmalloced)
972 struct hlist_head *hash;
973 unsigned int size, i;
977 size = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_head));
978 hash = (void*)__get_free_pages(GFP_KERNEL,
979 get_order(sizeof(struct hlist_head)
983 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
984 hash = vmalloc(sizeof(struct hlist_head) * size);
988 for (i = 0; i < size; i++)
989 INIT_HLIST_HEAD(&hash[i]);
993 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
995 int set_hashsize(const char *val, struct kernel_param *kp)
997 int i, bucket, hashsize, vmalloced;
998 int old_vmalloced, old_size;
1000 struct hlist_head *hash, *old_hash;
1001 struct nf_conntrack_tuple_hash *h;
1003 /* On boot, we can set this without any fancy locking. */
1004 if (!nf_conntrack_htable_size)
1005 return param_set_uint(val, kp);
1007 hashsize = simple_strtol(val, NULL, 0);
1011 hash = nf_ct_alloc_hashtable(&hashsize, &vmalloced);
1015 /* We have to rehahs for the new table anyway, so we also can
1016 * use a newrandom seed */
1017 get_random_bytes(&rnd, 4);
1019 write_lock_bh(&nf_conntrack_lock);
1020 for (i = 0; i < nf_conntrack_htable_size; i++) {
1021 while (!hlist_empty(&nf_conntrack_hash[i])) {
1022 h = hlist_entry(nf_conntrack_hash[i].first,
1023 struct nf_conntrack_tuple_hash, hnode);
1024 hlist_del(&h->hnode);
1025 bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1026 hlist_add_head(&h->hnode, &hash[bucket]);
1029 old_size = nf_conntrack_htable_size;
1030 old_vmalloced = nf_conntrack_vmalloc;
1031 old_hash = nf_conntrack_hash;
1033 nf_conntrack_htable_size = hashsize;
1034 nf_conntrack_vmalloc = vmalloced;
1035 nf_conntrack_hash = hash;
1036 nf_conntrack_hash_rnd = rnd;
1037 write_unlock_bh(&nf_conntrack_lock);
1039 nf_ct_free_hashtable(old_hash, old_vmalloced, old_size);
1043 module_param_call(hashsize, set_hashsize, param_get_uint,
1044 &nf_conntrack_htable_size, 0600);
1046 int __init nf_conntrack_init(void)
1051 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1052 * machine has 512 buckets. >= 1GB machines have 16384 buckets. */
1053 if (!nf_conntrack_htable_size) {
1054 nf_conntrack_htable_size
1055 = (((num_physpages << PAGE_SHIFT) / 16384)
1056 / sizeof(struct hlist_head));
1057 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1058 nf_conntrack_htable_size = 16384;
1059 if (nf_conntrack_htable_size < 32)
1060 nf_conntrack_htable_size = 32;
1062 /* Use a max. factor of four by default to get the same max as
1063 * with the old struct list_heads. When a table size is given
1064 * we use the old value of 8 to avoid reducing the max.
1068 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
1069 &nf_conntrack_vmalloc);
1070 if (!nf_conntrack_hash) {
1071 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1075 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
1077 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1078 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1081 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1082 sizeof(struct nf_conn),
1084 if (!nf_conntrack_cachep) {
1085 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1089 ret = nf_conntrack_proto_init();
1091 goto err_free_conntrack_slab;
1093 ret = nf_conntrack_expect_init();
1095 goto out_fini_proto;
1097 ret = nf_conntrack_helper_init();
1099 goto out_fini_expect;
1101 /* For use by REJECT target */
1102 rcu_assign_pointer(ip_ct_attach, __nf_conntrack_attach);
1103 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
1105 /* Set up fake conntrack:
1106 - to never be deleted, not in any hashes */
1107 atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1108 /* - and look it like as a confirmed connection */
1109 set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1114 nf_conntrack_expect_fini();
1116 nf_conntrack_proto_fini();
1117 err_free_conntrack_slab:
1118 kmem_cache_destroy(nf_conntrack_cachep);
1120 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
1121 nf_conntrack_htable_size);