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.
13 * 23 Apr 2001: Harald Welte <laforge@gnumonks.org>
14 * - new API and handling of conntrack/nat helpers
15 * - now capable of multiple expectations for one master
16 * 16 Jul 2002: Harald Welte <laforge@gnumonks.org>
17 * - add usage/reference counts to ip_conntrack_expect
18 * - export ip_conntrack[_expect]_{find_get,put} functions
19 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
20 * - generalize L3 protocol denendent part.
21 * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
22 * - add support various size of conntrack structures.
23 * 26 Jan 2006: Harald Welte <laforge@netfilter.org>
24 * - restructure nf_conn (introduce nf_conn_help)
25 * - redesign 'features' how they were originally intended
26 * 26 Feb 2006: Pablo Neira Ayuso <pablo@eurodev.net>
27 * - add support for L3 protocol module load on demand.
29 * Derived from net/ipv4/netfilter/ip_conntrack_core.c
32 #include <linux/types.h>
33 #include <linux/netfilter.h>
34 #include <linux/module.h>
35 #include <linux/skbuff.h>
36 #include <linux/proc_fs.h>
37 #include <linux/vmalloc.h>
38 #include <linux/stddef.h>
39 #include <linux/slab.h>
40 #include <linux/random.h>
41 #include <linux/jhash.h>
42 #include <linux/err.h>
43 #include <linux/percpu.h>
44 #include <linux/moduleparam.h>
45 #include <linux/notifier.h>
46 #include <linux/kernel.h>
47 #include <linux/netdevice.h>
48 #include <linux/socket.h>
50 #include <net/netfilter/nf_conntrack.h>
51 #include <net/netfilter/nf_conntrack_l3proto.h>
52 #include <net/netfilter/nf_conntrack_l4proto.h>
53 #include <net/netfilter/nf_conntrack_expect.h>
54 #include <net/netfilter/nf_conntrack_helper.h>
55 #include <net/netfilter/nf_conntrack_core.h>
57 #define NF_CONNTRACK_VERSION "0.5.0"
62 #define DEBUGP(format, args...)
65 DEFINE_RWLOCK(nf_conntrack_lock
);
66 EXPORT_SYMBOL_GPL(nf_conntrack_lock
);
68 /* nf_conntrack_standalone needs this */
69 atomic_t nf_conntrack_count
= ATOMIC_INIT(0);
70 EXPORT_SYMBOL_GPL(nf_conntrack_count
);
72 void (*nf_conntrack_destroyed
)(struct nf_conn
*conntrack
);
73 EXPORT_SYMBOL_GPL(nf_conntrack_destroyed
);
75 unsigned int nf_conntrack_htable_size __read_mostly
;
76 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size
);
78 int nf_conntrack_max __read_mostly
;
79 EXPORT_SYMBOL_GPL(nf_conntrack_max
);
81 struct list_head
*nf_conntrack_hash __read_mostly
;
82 EXPORT_SYMBOL_GPL(nf_conntrack_hash
);
84 struct nf_conn nf_conntrack_untracked __read_mostly
;
85 EXPORT_SYMBOL_GPL(nf_conntrack_untracked
);
87 unsigned int nf_ct_log_invalid __read_mostly
;
88 LIST_HEAD(unconfirmed
);
89 static int nf_conntrack_vmalloc __read_mostly
;
91 static unsigned int nf_conntrack_next_id
;
93 DEFINE_PER_CPU(struct ip_conntrack_stat
, nf_conntrack_stat
);
94 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat
);
97 * This scheme offers various size of "struct nf_conn" dependent on
98 * features(helper, nat, ...)
101 #define NF_CT_FEATURES_NAMELEN 256
103 /* name of slab cache. printed in /proc/slabinfo */
106 /* size of slab cache */
109 /* slab cache pointer */
110 kmem_cache_t
*cachep
;
112 /* allocated slab cache + modules which uses this slab cache */
115 } nf_ct_cache
[NF_CT_F_NUM
];
117 /* protect members of nf_ct_cache except of "use" */
118 DEFINE_RWLOCK(nf_ct_cache_lock
);
120 /* This avoids calling kmem_cache_create() with same name simultaneously */
121 static DEFINE_MUTEX(nf_ct_cache_mutex
);
123 static int nf_conntrack_hash_rnd_initted
;
124 static unsigned int nf_conntrack_hash_rnd
;
126 static u_int32_t
__hash_conntrack(const struct nf_conntrack_tuple
*tuple
,
127 unsigned int size
, unsigned int rnd
)
130 a
= jhash((void *)tuple
->src
.u3
.all
, sizeof(tuple
->src
.u3
.all
),
131 ((tuple
->src
.l3num
) << 16) | tuple
->dst
.protonum
);
132 b
= jhash((void *)tuple
->dst
.u3
.all
, sizeof(tuple
->dst
.u3
.all
),
133 (tuple
->src
.u
.all
<< 16) | tuple
->dst
.u
.all
);
135 return jhash_2words(a
, b
, rnd
) % size
;
138 static inline u_int32_t
hash_conntrack(const struct nf_conntrack_tuple
*tuple
)
140 return __hash_conntrack(tuple
, nf_conntrack_htable_size
,
141 nf_conntrack_hash_rnd
);
144 int nf_conntrack_register_cache(u_int32_t features
, const char *name
,
149 kmem_cache_t
*cachep
;
151 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
152 features
, name
, size
);
154 if (features
< NF_CT_F_BASIC
|| features
>= NF_CT_F_NUM
) {
155 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
160 mutex_lock(&nf_ct_cache_mutex
);
162 write_lock_bh(&nf_ct_cache_lock
);
163 /* e.g: multiple helpers are loaded */
164 if (nf_ct_cache
[features
].use
> 0) {
165 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
166 if ((!strncmp(nf_ct_cache
[features
].name
, name
,
167 NF_CT_FEATURES_NAMELEN
))
168 && nf_ct_cache
[features
].size
== size
) {
169 DEBUGP("nf_conntrack_register_cache: reusing.\n");
170 nf_ct_cache
[features
].use
++;
175 write_unlock_bh(&nf_ct_cache_lock
);
176 mutex_unlock(&nf_ct_cache_mutex
);
179 write_unlock_bh(&nf_ct_cache_lock
);
182 * The memory space for name of slab cache must be alive until
183 * cache is destroyed.
185 cache_name
= kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN
, GFP_ATOMIC
);
186 if (cache_name
== NULL
) {
187 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
192 if (strlcpy(cache_name
, name
, NF_CT_FEATURES_NAMELEN
)
193 >= NF_CT_FEATURES_NAMELEN
) {
194 printk("nf_conntrack_register_cache: name too long\n");
199 cachep
= kmem_cache_create(cache_name
, size
, 0, 0,
202 printk("nf_conntrack_register_cache: Can't create slab cache "
203 "for the features = 0x%x\n", features
);
208 write_lock_bh(&nf_ct_cache_lock
);
209 nf_ct_cache
[features
].use
= 1;
210 nf_ct_cache
[features
].size
= size
;
211 nf_ct_cache
[features
].cachep
= cachep
;
212 nf_ct_cache
[features
].name
= cache_name
;
213 write_unlock_bh(&nf_ct_cache_lock
);
220 mutex_unlock(&nf_ct_cache_mutex
);
223 EXPORT_SYMBOL_GPL(nf_conntrack_register_cache
);
225 /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
226 void nf_conntrack_unregister_cache(u_int32_t features
)
228 kmem_cache_t
*cachep
;
232 * This assures that kmem_cache_create() isn't called before destroying
235 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features
);
236 mutex_lock(&nf_ct_cache_mutex
);
238 write_lock_bh(&nf_ct_cache_lock
);
239 if (--nf_ct_cache
[features
].use
> 0) {
240 write_unlock_bh(&nf_ct_cache_lock
);
241 mutex_unlock(&nf_ct_cache_mutex
);
244 cachep
= nf_ct_cache
[features
].cachep
;
245 name
= nf_ct_cache
[features
].name
;
246 nf_ct_cache
[features
].cachep
= NULL
;
247 nf_ct_cache
[features
].name
= NULL
;
248 nf_ct_cache
[features
].size
= 0;
249 write_unlock_bh(&nf_ct_cache_lock
);
253 kmem_cache_destroy(cachep
);
256 mutex_unlock(&nf_ct_cache_mutex
);
258 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_cache
);
261 nf_ct_get_tuple(const struct sk_buff
*skb
,
263 unsigned int dataoff
,
266 struct nf_conntrack_tuple
*tuple
,
267 const struct nf_conntrack_l3proto
*l3proto
,
268 const struct nf_conntrack_l4proto
*l4proto
)
270 NF_CT_TUPLE_U_BLANK(tuple
);
272 tuple
->src
.l3num
= l3num
;
273 if (l3proto
->pkt_to_tuple(skb
, nhoff
, tuple
) == 0)
276 tuple
->dst
.protonum
= protonum
;
277 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
279 return l4proto
->pkt_to_tuple(skb
, dataoff
, tuple
);
281 EXPORT_SYMBOL_GPL(nf_ct_get_tuple
);
284 nf_ct_invert_tuple(struct nf_conntrack_tuple
*inverse
,
285 const struct nf_conntrack_tuple
*orig
,
286 const struct nf_conntrack_l3proto
*l3proto
,
287 const struct nf_conntrack_l4proto
*l4proto
)
289 NF_CT_TUPLE_U_BLANK(inverse
);
291 inverse
->src
.l3num
= orig
->src
.l3num
;
292 if (l3proto
->invert_tuple(inverse
, orig
) == 0)
295 inverse
->dst
.dir
= !orig
->dst
.dir
;
297 inverse
->dst
.protonum
= orig
->dst
.protonum
;
298 return l4proto
->invert_tuple(inverse
, orig
);
300 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple
);
303 clean_from_lists(struct nf_conn
*ct
)
305 DEBUGP("clean_from_lists(%p)\n", ct
);
306 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
307 list_del(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
);
309 /* Destroy all pending expectations */
310 nf_ct_remove_expectations(ct
);
314 destroy_conntrack(struct nf_conntrack
*nfct
)
316 struct nf_conn
*ct
= (struct nf_conn
*)nfct
;
317 struct nf_conn_help
*help
= nfct_help(ct
);
318 struct nf_conntrack_l3proto
*l3proto
;
319 struct nf_conntrack_l4proto
*l4proto
;
321 DEBUGP("destroy_conntrack(%p)\n", ct
);
322 NF_CT_ASSERT(atomic_read(&nfct
->use
) == 0);
323 NF_CT_ASSERT(!timer_pending(&ct
->timeout
));
325 nf_conntrack_event(IPCT_DESTROY
, ct
);
326 set_bit(IPS_DYING_BIT
, &ct
->status
);
328 if (help
&& help
->helper
&& help
->helper
->destroy
)
329 help
->helper
->destroy(ct
);
331 /* To make sure we don't get any weird locking issues here:
332 * destroy_conntrack() MUST NOT be called with a write lock
333 * to nf_conntrack_lock!!! -HW */
334 l3proto
= __nf_ct_l3proto_find(ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.l3num
);
335 if (l3proto
&& l3proto
->destroy
)
336 l3proto
->destroy(ct
);
338 l4proto
= __nf_ct_l4proto_find(ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.l3num
, ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.protonum
);
339 if (l4proto
&& l4proto
->destroy
)
340 l4proto
->destroy(ct
);
342 if (nf_conntrack_destroyed
)
343 nf_conntrack_destroyed(ct
);
345 write_lock_bh(&nf_conntrack_lock
);
346 /* Expectations will have been removed in clean_from_lists,
347 * except TFTP can create an expectation on the first packet,
348 * before connection is in the list, so we need to clean here,
350 nf_ct_remove_expectations(ct
);
352 /* We overload first tuple to link into unconfirmed list. */
353 if (!nf_ct_is_confirmed(ct
)) {
354 BUG_ON(list_empty(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
));
355 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
358 NF_CT_STAT_INC(delete);
359 write_unlock_bh(&nf_conntrack_lock
);
362 nf_ct_put(ct
->master
);
364 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct
);
365 nf_conntrack_free(ct
);
368 static void death_by_timeout(unsigned long ul_conntrack
)
370 struct nf_conn
*ct
= (void *)ul_conntrack
;
372 write_lock_bh(&nf_conntrack_lock
);
373 /* Inside lock so preempt is disabled on module removal path.
374 * Otherwise we can get spurious warnings. */
375 NF_CT_STAT_INC(delete_list
);
376 clean_from_lists(ct
);
377 write_unlock_bh(&nf_conntrack_lock
);
381 struct nf_conntrack_tuple_hash
*
382 __nf_conntrack_find(const struct nf_conntrack_tuple
*tuple
,
383 const struct nf_conn
*ignored_conntrack
)
385 struct nf_conntrack_tuple_hash
*h
;
386 unsigned int hash
= hash_conntrack(tuple
);
388 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
) {
389 if (nf_ct_tuplehash_to_ctrack(h
) != ignored_conntrack
&&
390 nf_ct_tuple_equal(tuple
, &h
->tuple
)) {
391 NF_CT_STAT_INC(found
);
394 NF_CT_STAT_INC(searched
);
399 EXPORT_SYMBOL_GPL(__nf_conntrack_find
);
401 /* Find a connection corresponding to a tuple. */
402 struct nf_conntrack_tuple_hash
*
403 nf_conntrack_find_get(const struct nf_conntrack_tuple
*tuple
,
404 const struct nf_conn
*ignored_conntrack
)
406 struct nf_conntrack_tuple_hash
*h
;
408 read_lock_bh(&nf_conntrack_lock
);
409 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
411 atomic_inc(&nf_ct_tuplehash_to_ctrack(h
)->ct_general
.use
);
412 read_unlock_bh(&nf_conntrack_lock
);
416 EXPORT_SYMBOL_GPL(nf_conntrack_find_get
);
418 static void __nf_conntrack_hash_insert(struct nf_conn
*ct
,
420 unsigned int repl_hash
)
422 ct
->id
= ++nf_conntrack_next_id
;
423 list_add(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
,
424 &nf_conntrack_hash
[hash
]);
425 list_add(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
,
426 &nf_conntrack_hash
[repl_hash
]);
429 void nf_conntrack_hash_insert(struct nf_conn
*ct
)
431 unsigned int hash
, repl_hash
;
433 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
434 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
436 write_lock_bh(&nf_conntrack_lock
);
437 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
438 write_unlock_bh(&nf_conntrack_lock
);
440 EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert
);
442 /* Confirm a connection given skb; places it in hash table */
444 __nf_conntrack_confirm(struct sk_buff
**pskb
)
446 unsigned int hash
, repl_hash
;
447 struct nf_conntrack_tuple_hash
*h
;
449 struct nf_conn_help
*help
;
450 enum ip_conntrack_info ctinfo
;
452 ct
= nf_ct_get(*pskb
, &ctinfo
);
454 /* ipt_REJECT uses nf_conntrack_attach to attach related
455 ICMP/TCP RST packets in other direction. Actual packet
456 which created connection will be IP_CT_NEW or for an
457 expected connection, IP_CT_RELATED. */
458 if (CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
)
461 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
462 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
464 /* We're not in hash table, and we refuse to set up related
465 connections for unconfirmed conns. But packet copies and
466 REJECT will give spurious warnings here. */
467 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
469 /* No external references means noone else could have
471 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
472 DEBUGP("Confirming conntrack %p\n", ct
);
474 write_lock_bh(&nf_conntrack_lock
);
476 /* See if there's one in the list already, including reverse:
477 NAT could have grabbed it without realizing, since we're
478 not in the hash. If there is, we lost race. */
479 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
)
480 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
483 list_for_each_entry(h
, &nf_conntrack_hash
[repl_hash
], list
)
484 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
488 /* Remove from unconfirmed list */
489 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
491 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
492 /* Timer relative to confirmation time, not original
493 setting time, otherwise we'd get timer wrap in
494 weird delay cases. */
495 ct
->timeout
.expires
+= jiffies
;
496 add_timer(&ct
->timeout
);
497 atomic_inc(&ct
->ct_general
.use
);
498 set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
499 NF_CT_STAT_INC(insert
);
500 write_unlock_bh(&nf_conntrack_lock
);
501 help
= nfct_help(ct
);
502 if (help
&& help
->helper
)
503 nf_conntrack_event_cache(IPCT_HELPER
, *pskb
);
504 #ifdef CONFIG_NF_NAT_NEEDED
505 if (test_bit(IPS_SRC_NAT_DONE_BIT
, &ct
->status
) ||
506 test_bit(IPS_DST_NAT_DONE_BIT
, &ct
->status
))
507 nf_conntrack_event_cache(IPCT_NATINFO
, *pskb
);
509 nf_conntrack_event_cache(master_ct(ct
) ?
510 IPCT_RELATED
: IPCT_NEW
, *pskb
);
514 NF_CT_STAT_INC(insert_failed
);
515 write_unlock_bh(&nf_conntrack_lock
);
518 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm
);
520 /* Returns true if a connection correspondings to the tuple (required
523 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple
*tuple
,
524 const struct nf_conn
*ignored_conntrack
)
526 struct nf_conntrack_tuple_hash
*h
;
528 read_lock_bh(&nf_conntrack_lock
);
529 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
530 read_unlock_bh(&nf_conntrack_lock
);
534 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken
);
536 /* There's a small race here where we may free a just-assured
537 connection. Too bad: we're in trouble anyway. */
538 static int early_drop(struct list_head
*chain
)
540 /* Traverse backwards: gives us oldest, which is roughly LRU */
541 struct nf_conntrack_tuple_hash
*h
;
542 struct nf_conn
*ct
= NULL
, *tmp
;
545 read_lock_bh(&nf_conntrack_lock
);
546 list_for_each_entry_reverse(h
, chain
, list
) {
547 tmp
= nf_ct_tuplehash_to_ctrack(h
);
548 if (!test_bit(IPS_ASSURED_BIT
, &tmp
->status
)) {
550 atomic_inc(&ct
->ct_general
.use
);
554 read_unlock_bh(&nf_conntrack_lock
);
559 if (del_timer(&ct
->timeout
)) {
560 death_by_timeout((unsigned long)ct
);
562 NF_CT_STAT_INC(early_drop
);
568 static struct nf_conn
*
569 __nf_conntrack_alloc(const struct nf_conntrack_tuple
*orig
,
570 const struct nf_conntrack_tuple
*repl
,
571 const struct nf_conntrack_l3proto
*l3proto
,
574 struct nf_conn
*conntrack
= NULL
;
575 struct nf_conntrack_helper
*helper
;
577 if (unlikely(!nf_conntrack_hash_rnd_initted
)) {
578 get_random_bytes(&nf_conntrack_hash_rnd
, 4);
579 nf_conntrack_hash_rnd_initted
= 1;
582 /* We don't want any race condition at early drop stage */
583 atomic_inc(&nf_conntrack_count
);
586 && atomic_read(&nf_conntrack_count
) > nf_conntrack_max
) {
587 unsigned int hash
= hash_conntrack(orig
);
588 /* Try dropping from this hash chain. */
589 if (!early_drop(&nf_conntrack_hash
[hash
])) {
590 atomic_dec(&nf_conntrack_count
);
593 "nf_conntrack: table full, dropping"
595 return ERR_PTR(-ENOMEM
);
599 /* find features needed by this conntrack. */
600 features
|= l3proto
->get_features(orig
);
602 /* FIXME: protect helper list per RCU */
603 read_lock_bh(&nf_conntrack_lock
);
604 helper
= __nf_ct_helper_find(repl
);
605 /* NAT might want to assign a helper later */
606 if (helper
|| features
& NF_CT_F_NAT
)
607 features
|= NF_CT_F_HELP
;
608 read_unlock_bh(&nf_conntrack_lock
);
610 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features
);
612 read_lock_bh(&nf_ct_cache_lock
);
614 if (unlikely(!nf_ct_cache
[features
].use
)) {
615 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
620 conntrack
= kmem_cache_alloc(nf_ct_cache
[features
].cachep
, GFP_ATOMIC
);
621 if (conntrack
== NULL
) {
622 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
626 memset(conntrack
, 0, nf_ct_cache
[features
].size
);
627 conntrack
->features
= features
;
628 atomic_set(&conntrack
->ct_general
.use
, 1);
629 conntrack
->ct_general
.destroy
= destroy_conntrack
;
630 conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
= *orig
;
631 conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
= *repl
;
632 /* Don't set timer yet: wait for confirmation */
633 init_timer(&conntrack
->timeout
);
634 conntrack
->timeout
.data
= (unsigned long)conntrack
;
635 conntrack
->timeout
.function
= death_by_timeout
;
636 read_unlock_bh(&nf_ct_cache_lock
);
640 read_unlock_bh(&nf_ct_cache_lock
);
641 atomic_dec(&nf_conntrack_count
);
645 struct nf_conn
*nf_conntrack_alloc(const struct nf_conntrack_tuple
*orig
,
646 const struct nf_conntrack_tuple
*repl
)
648 struct nf_conntrack_l3proto
*l3proto
;
650 l3proto
= __nf_ct_l3proto_find(orig
->src
.l3num
);
651 return __nf_conntrack_alloc(orig
, repl
, l3proto
, 0);
653 EXPORT_SYMBOL_GPL(nf_conntrack_alloc
);
655 void nf_conntrack_free(struct nf_conn
*conntrack
)
657 u_int32_t features
= conntrack
->features
;
658 NF_CT_ASSERT(features
>= NF_CT_F_BASIC
&& features
< NF_CT_F_NUM
);
659 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features
,
661 kmem_cache_free(nf_ct_cache
[features
].cachep
, conntrack
);
662 atomic_dec(&nf_conntrack_count
);
664 EXPORT_SYMBOL_GPL(nf_conntrack_free
);
666 /* Allocate a new conntrack: we return -ENOMEM if classification
667 failed due to stress. Otherwise it really is unclassifiable. */
668 static struct nf_conntrack_tuple_hash
*
669 init_conntrack(const struct nf_conntrack_tuple
*tuple
,
670 struct nf_conntrack_l3proto
*l3proto
,
671 struct nf_conntrack_l4proto
*l4proto
,
673 unsigned int dataoff
)
675 struct nf_conn
*conntrack
;
676 struct nf_conntrack_tuple repl_tuple
;
677 struct nf_conntrack_expect
*exp
;
678 u_int32_t features
= 0;
680 if (!nf_ct_invert_tuple(&repl_tuple
, tuple
, l3proto
, l4proto
)) {
681 DEBUGP("Can't invert tuple.\n");
685 read_lock_bh(&nf_conntrack_lock
);
686 exp
= __nf_conntrack_expect_find(tuple
);
687 if (exp
&& exp
->helper
)
688 features
= NF_CT_F_HELP
;
689 read_unlock_bh(&nf_conntrack_lock
);
691 conntrack
= __nf_conntrack_alloc(tuple
, &repl_tuple
, l3proto
, features
);
692 if (conntrack
== NULL
|| IS_ERR(conntrack
)) {
693 DEBUGP("Can't allocate conntrack.\n");
694 return (struct nf_conntrack_tuple_hash
*)conntrack
;
697 if (!l4proto
->new(conntrack
, skb
, dataoff
)) {
698 nf_conntrack_free(conntrack
);
699 DEBUGP("init conntrack: can't track with proto module\n");
703 write_lock_bh(&nf_conntrack_lock
);
704 exp
= find_expectation(tuple
);
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 nfct_help(conntrack
)->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
);
723 struct nf_conn_help
*help
= nfct_help(conntrack
);
726 help
->helper
= __nf_ct_helper_find(&repl_tuple
);
730 /* Overload tuple linked list to put us in unconfirmed list. */
731 list_add(&conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
, &unconfirmed
);
733 write_unlock_bh(&nf_conntrack_lock
);
737 exp
->expectfn(conntrack
, exp
);
738 nf_conntrack_expect_put(exp
);
741 return &conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
];
744 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
745 static inline struct nf_conn
*
746 resolve_normal_ct(struct sk_buff
*skb
,
747 unsigned int dataoff
,
750 struct nf_conntrack_l3proto
*l3proto
,
751 struct nf_conntrack_l4proto
*l4proto
,
753 enum ip_conntrack_info
*ctinfo
)
755 struct nf_conntrack_tuple tuple
;
756 struct nf_conntrack_tuple_hash
*h
;
759 if (!nf_ct_get_tuple(skb
, (unsigned int)(skb
->nh
.raw
- skb
->data
),
760 dataoff
, l3num
, protonum
, &tuple
, l3proto
,
762 DEBUGP("resolve_normal_ct: Can't get tuple\n");
766 /* look for tuple match */
767 h
= nf_conntrack_find_get(&tuple
, NULL
);
769 h
= init_conntrack(&tuple
, l3proto
, l4proto
, skb
, dataoff
);
775 ct
= nf_ct_tuplehash_to_ctrack(h
);
777 /* It exists; we have (non-exclusive) reference. */
778 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
) {
779 *ctinfo
= IP_CT_ESTABLISHED
+ IP_CT_IS_REPLY
;
780 /* Please set reply bit if this packet OK */
783 /* Once we've had two way comms, always ESTABLISHED. */
784 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
785 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct
);
786 *ctinfo
= IP_CT_ESTABLISHED
;
787 } else if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
)) {
788 DEBUGP("nf_conntrack_in: related packet for %p\n", ct
);
789 *ctinfo
= IP_CT_RELATED
;
791 DEBUGP("nf_conntrack_in: new packet for %p\n", ct
);
796 skb
->nfct
= &ct
->ct_general
;
797 skb
->nfctinfo
= *ctinfo
;
802 nf_conntrack_in(int pf
, unsigned int hooknum
, struct sk_buff
**pskb
)
805 enum ip_conntrack_info ctinfo
;
806 struct nf_conntrack_l3proto
*l3proto
;
807 struct nf_conntrack_l4proto
*l4proto
;
808 unsigned int dataoff
;
813 /* Previously seen (loopback or untracked)? Ignore. */
815 NF_CT_STAT_INC(ignore
);
819 l3proto
= __nf_ct_l3proto_find((u_int16_t
)pf
);
820 if ((ret
= l3proto
->prepare(pskb
, hooknum
, &dataoff
, &protonum
)) <= 0) {
821 DEBUGP("not prepared to track yet or error occured\n");
825 l4proto
= __nf_ct_l4proto_find((u_int16_t
)pf
, protonum
);
827 /* It may be an special packet, error, unclean...
828 * inverse of the return code tells to the netfilter
829 * core what to do with the packet. */
830 if (l4proto
->error
!= NULL
&&
831 (ret
= l4proto
->error(*pskb
, dataoff
, &ctinfo
, pf
, hooknum
)) <= 0) {
832 NF_CT_STAT_INC(error
);
833 NF_CT_STAT_INC(invalid
);
837 ct
= resolve_normal_ct(*pskb
, dataoff
, pf
, protonum
, l3proto
, l4proto
,
838 &set_reply
, &ctinfo
);
840 /* Not valid part of a connection */
841 NF_CT_STAT_INC(invalid
);
846 /* Too stressed to deal. */
847 NF_CT_STAT_INC(drop
);
851 NF_CT_ASSERT((*pskb
)->nfct
);
853 ret
= l4proto
->packet(ct
, *pskb
, dataoff
, ctinfo
, pf
, hooknum
);
855 /* Invalid: inverse of the return code tells
856 * the netfilter core what to do */
857 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
858 nf_conntrack_put((*pskb
)->nfct
);
859 (*pskb
)->nfct
= NULL
;
860 NF_CT_STAT_INC(invalid
);
864 if (set_reply
&& !test_and_set_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
865 nf_conntrack_event_cache(IPCT_STATUS
, *pskb
);
869 EXPORT_SYMBOL_GPL(nf_conntrack_in
);
871 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple
*inverse
,
872 const struct nf_conntrack_tuple
*orig
)
874 return 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
));
879 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr
);
881 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
882 implicitly racy: see __nf_conntrack_confirm */
883 void nf_conntrack_alter_reply(struct nf_conn
*ct
,
884 const struct nf_conntrack_tuple
*newreply
)
886 struct nf_conn_help
*help
= nfct_help(ct
);
888 write_lock_bh(&nf_conntrack_lock
);
889 /* Should be unconfirmed, so not in hash table yet */
890 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
892 DEBUGP("Altering reply tuple of %p to ", ct
);
893 NF_CT_DUMP_TUPLE(newreply
);
895 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
= *newreply
;
896 if (!ct
->master
&& help
&& help
->expecting
== 0)
897 help
->helper
= __nf_ct_helper_find(newreply
);
898 write_unlock_bh(&nf_conntrack_lock
);
900 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply
);
902 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
903 void __nf_ct_refresh_acct(struct nf_conn
*ct
,
904 enum ip_conntrack_info ctinfo
,
905 const struct sk_buff
*skb
,
906 unsigned long extra_jiffies
,
911 NF_CT_ASSERT(ct
->timeout
.data
== (unsigned long)ct
);
914 write_lock_bh(&nf_conntrack_lock
);
916 /* Only update if this is not a fixed timeout */
917 if (test_bit(IPS_FIXED_TIMEOUT_BIT
, &ct
->status
)) {
918 write_unlock_bh(&nf_conntrack_lock
);
922 /* If not in hash table, timer will not be active yet */
923 if (!nf_ct_is_confirmed(ct
)) {
924 ct
->timeout
.expires
= extra_jiffies
;
925 event
= IPCT_REFRESH
;
927 unsigned long newtime
= jiffies
+ extra_jiffies
;
929 /* Only update the timeout if the new timeout is at least
930 HZ jiffies from the old timeout. Need del_timer for race
931 avoidance (may already be dying). */
932 if (newtime
- ct
->timeout
.expires
>= HZ
933 && del_timer(&ct
->timeout
)) {
934 ct
->timeout
.expires
= newtime
;
935 add_timer(&ct
->timeout
);
936 event
= IPCT_REFRESH
;
940 #ifdef CONFIG_NF_CT_ACCT
942 ct
->counters
[CTINFO2DIR(ctinfo
)].packets
++;
943 ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
+=
944 skb
->len
- (unsigned int)(skb
->nh
.raw
- skb
->data
);
946 if ((ct
->counters
[CTINFO2DIR(ctinfo
)].packets
& 0x80000000)
947 || (ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
& 0x80000000))
948 event
|= IPCT_COUNTER_FILLING
;
952 write_unlock_bh(&nf_conntrack_lock
);
954 /* must be unlocked when calling event cache */
956 nf_conntrack_event_cache(event
, skb
);
958 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct
);
960 #if defined(CONFIG_NF_CT_NETLINK) || \
961 defined(CONFIG_NF_CT_NETLINK_MODULE)
963 #include <linux/netfilter/nfnetlink.h>
964 #include <linux/netfilter/nfnetlink_conntrack.h>
965 #include <linux/mutex.h>
968 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
969 * in ip_conntrack_core, since we don't want the protocols to autoload
970 * or depend on ctnetlink */
971 int nf_ct_port_tuple_to_nfattr(struct sk_buff
*skb
,
972 const struct nf_conntrack_tuple
*tuple
)
974 NFA_PUT(skb
, CTA_PROTO_SRC_PORT
, sizeof(u_int16_t
),
975 &tuple
->src
.u
.tcp
.port
);
976 NFA_PUT(skb
, CTA_PROTO_DST_PORT
, sizeof(u_int16_t
),
977 &tuple
->dst
.u
.tcp
.port
);
983 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nfattr
);
985 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
986 [CTA_PROTO_SRC_PORT
-1] = sizeof(u_int16_t
),
987 [CTA_PROTO_DST_PORT
-1] = sizeof(u_int16_t
)
990 int nf_ct_port_nfattr_to_tuple(struct nfattr
*tb
[],
991 struct nf_conntrack_tuple
*t
)
993 if (!tb
[CTA_PROTO_SRC_PORT
-1] || !tb
[CTA_PROTO_DST_PORT
-1])
996 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
999 t
->src
.u
.tcp
.port
= *(__be16
*)NFA_DATA(tb
[CTA_PROTO_SRC_PORT
-1]);
1000 t
->dst
.u
.tcp
.port
= *(__be16
*)NFA_DATA(tb
[CTA_PROTO_DST_PORT
-1]);
1004 EXPORT_SYMBOL_GPL(nf_ct_port_nfattr_to_tuple
);
1007 /* Used by ipt_REJECT and ip6t_REJECT. */
1008 void __nf_conntrack_attach(struct sk_buff
*nskb
, struct sk_buff
*skb
)
1011 enum ip_conntrack_info ctinfo
;
1013 /* This ICMP is in reverse direction to the packet which caused it */
1014 ct
= nf_ct_get(skb
, &ctinfo
);
1015 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
)
1016 ctinfo
= IP_CT_RELATED
+ IP_CT_IS_REPLY
;
1018 ctinfo
= IP_CT_RELATED
;
1020 /* Attach to new skbuff, and increment count */
1021 nskb
->nfct
= &ct
->ct_general
;
1022 nskb
->nfctinfo
= ctinfo
;
1023 nf_conntrack_get(nskb
->nfct
);
1025 EXPORT_SYMBOL_GPL(__nf_conntrack_attach
);
1028 do_iter(const struct nf_conntrack_tuple_hash
*i
,
1029 int (*iter
)(struct nf_conn
*i
, void *data
),
1032 return iter(nf_ct_tuplehash_to_ctrack(i
), data
);
1035 /* Bring out ya dead! */
1036 static struct nf_conn
*
1037 get_next_corpse(int (*iter
)(struct nf_conn
*i
, void *data
),
1038 void *data
, unsigned int *bucket
)
1040 struct nf_conntrack_tuple_hash
*h
;
1043 write_lock_bh(&nf_conntrack_lock
);
1044 for (; *bucket
< nf_conntrack_htable_size
; (*bucket
)++) {
1045 list_for_each_entry(h
, &nf_conntrack_hash
[*bucket
], list
) {
1046 ct
= nf_ct_tuplehash_to_ctrack(h
);
1051 list_for_each_entry(h
, &unconfirmed
, list
) {
1052 ct
= nf_ct_tuplehash_to_ctrack(h
);
1056 write_unlock_bh(&nf_conntrack_lock
);
1059 atomic_inc(&ct
->ct_general
.use
);
1060 write_unlock_bh(&nf_conntrack_lock
);
1065 nf_ct_iterate_cleanup(int (*iter
)(struct nf_conn
*i
, void *data
), void *data
)
1068 unsigned int bucket
= 0;
1070 while ((ct
= get_next_corpse(iter
, data
, &bucket
)) != NULL
) {
1071 /* Time to push up daises... */
1072 if (del_timer(&ct
->timeout
))
1073 death_by_timeout((unsigned long)ct
);
1074 /* ... else the timer will get him soon. */
1079 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup
);
1081 static int kill_all(struct nf_conn
*i
, void *data
)
1086 static void free_conntrack_hash(struct list_head
*hash
, int vmalloced
, int size
)
1091 free_pages((unsigned long)hash
,
1092 get_order(sizeof(struct list_head
) * size
));
1095 void nf_conntrack_flush()
1097 nf_ct_iterate_cleanup(kill_all
, NULL
);
1099 EXPORT_SYMBOL_GPL(nf_conntrack_flush
);
1101 /* Mishearing the voices in his head, our hero wonders how he's
1102 supposed to kill the mall. */
1103 void nf_conntrack_cleanup(void)
1107 ip_ct_attach
= NULL
;
1109 /* This makes sure all current packets have passed through
1110 netfilter framework. Roll on, two-stage module
1114 nf_ct_event_cache_flush();
1116 nf_conntrack_flush();
1117 if (atomic_read(&nf_conntrack_count
) != 0) {
1119 goto i_see_dead_people
;
1121 /* wait until all references to nf_conntrack_untracked are dropped */
1122 while (atomic_read(&nf_conntrack_untracked
.ct_general
.use
) > 1)
1125 for (i
= 0; i
< NF_CT_F_NUM
; i
++) {
1126 if (nf_ct_cache
[i
].use
== 0)
1129 NF_CT_ASSERT(nf_ct_cache
[i
].use
== 1);
1130 nf_ct_cache
[i
].use
= 1;
1131 nf_conntrack_unregister_cache(i
);
1133 kmem_cache_destroy(nf_conntrack_expect_cachep
);
1134 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1135 nf_conntrack_htable_size
);
1137 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_generic
);
1139 /* free l3proto protocol tables */
1140 for (i
= 0; i
< PF_MAX
; i
++)
1141 if (nf_ct_protos
[i
]) {
1142 kfree(nf_ct_protos
[i
]);
1143 nf_ct_protos
[i
] = NULL
;
1147 static struct list_head
*alloc_hashtable(int size
, int *vmalloced
)
1149 struct list_head
*hash
;
1153 hash
= (void*)__get_free_pages(GFP_KERNEL
,
1154 get_order(sizeof(struct list_head
)
1158 printk(KERN_WARNING
"nf_conntrack: falling back to vmalloc.\n");
1159 hash
= vmalloc(sizeof(struct list_head
) * size
);
1163 for (i
= 0; i
< size
; i
++)
1164 INIT_LIST_HEAD(&hash
[i
]);
1169 int set_hashsize(const char *val
, struct kernel_param
*kp
)
1171 int i
, bucket
, hashsize
, vmalloced
;
1172 int old_vmalloced
, old_size
;
1174 struct list_head
*hash
, *old_hash
;
1175 struct nf_conntrack_tuple_hash
*h
;
1177 /* On boot, we can set this without any fancy locking. */
1178 if (!nf_conntrack_htable_size
)
1179 return param_set_uint(val
, kp
);
1181 hashsize
= simple_strtol(val
, NULL
, 0);
1185 hash
= alloc_hashtable(hashsize
, &vmalloced
);
1189 /* We have to rehahs for the new table anyway, so we also can
1190 * use a newrandom seed */
1191 get_random_bytes(&rnd
, 4);
1193 write_lock_bh(&nf_conntrack_lock
);
1194 for (i
= 0; i
< nf_conntrack_htable_size
; i
++) {
1195 while (!list_empty(&nf_conntrack_hash
[i
])) {
1196 h
= list_entry(nf_conntrack_hash
[i
].next
,
1197 struct nf_conntrack_tuple_hash
, list
);
1199 bucket
= __hash_conntrack(&h
->tuple
, hashsize
, rnd
);
1200 list_add_tail(&h
->list
, &hash
[bucket
]);
1203 old_size
= nf_conntrack_htable_size
;
1204 old_vmalloced
= nf_conntrack_vmalloc
;
1205 old_hash
= nf_conntrack_hash
;
1207 nf_conntrack_htable_size
= hashsize
;
1208 nf_conntrack_vmalloc
= vmalloced
;
1209 nf_conntrack_hash
= hash
;
1210 nf_conntrack_hash_rnd
= rnd
;
1211 write_unlock_bh(&nf_conntrack_lock
);
1213 free_conntrack_hash(old_hash
, old_vmalloced
, old_size
);
1217 module_param_call(hashsize
, set_hashsize
, param_get_uint
,
1218 &nf_conntrack_htable_size
, 0600);
1220 int __init
nf_conntrack_init(void)
1225 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1226 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1227 if (!nf_conntrack_htable_size
) {
1228 nf_conntrack_htable_size
1229 = (((num_physpages
<< PAGE_SHIFT
) / 16384)
1230 / sizeof(struct list_head
));
1231 if (num_physpages
> (1024 * 1024 * 1024 / PAGE_SIZE
))
1232 nf_conntrack_htable_size
= 8192;
1233 if (nf_conntrack_htable_size
< 16)
1234 nf_conntrack_htable_size
= 16;
1236 nf_conntrack_max
= 8 * nf_conntrack_htable_size
;
1238 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1239 NF_CONNTRACK_VERSION
, nf_conntrack_htable_size
,
1242 nf_conntrack_hash
= alloc_hashtable(nf_conntrack_htable_size
,
1243 &nf_conntrack_vmalloc
);
1244 if (!nf_conntrack_hash
) {
1245 printk(KERN_ERR
"Unable to create nf_conntrack_hash\n");
1249 ret
= nf_conntrack_register_cache(NF_CT_F_BASIC
, "nf_conntrack:basic",
1250 sizeof(struct nf_conn
));
1252 printk(KERN_ERR
"Unable to create nf_conn slab cache\n");
1256 nf_conntrack_expect_cachep
= kmem_cache_create("nf_conntrack_expect",
1257 sizeof(struct nf_conntrack_expect
),
1259 if (!nf_conntrack_expect_cachep
) {
1260 printk(KERN_ERR
"Unable to create nf_expect slab cache\n");
1261 goto err_free_conntrack_slab
;
1264 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_generic
);
1266 goto out_free_expect_slab
;
1268 /* Don't NEED lock here, but good form anyway. */
1269 write_lock_bh(&nf_conntrack_lock
);
1270 for (i
= 0; i
< AF_MAX
; i
++)
1271 nf_ct_l3protos
[i
] = &nf_conntrack_l3proto_generic
;
1272 write_unlock_bh(&nf_conntrack_lock
);
1274 /* For use by REJECT target */
1275 ip_ct_attach
= __nf_conntrack_attach
;
1277 /* Set up fake conntrack:
1278 - to never be deleted, not in any hashes */
1279 atomic_set(&nf_conntrack_untracked
.ct_general
.use
, 1);
1280 /* - and look it like as a confirmed connection */
1281 set_bit(IPS_CONFIRMED_BIT
, &nf_conntrack_untracked
.status
);
1285 out_free_expect_slab
:
1286 kmem_cache_destroy(nf_conntrack_expect_cachep
);
1287 err_free_conntrack_slab
:
1288 nf_conntrack_unregister_cache(NF_CT_F_BASIC
);
1290 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1291 nf_conntrack_htable_size
);