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
);
67 /* nf_conntrack_standalone needs this */
68 atomic_t nf_conntrack_count
= ATOMIC_INIT(0);
69 EXPORT_SYMBOL_GPL(nf_conntrack_count
);
71 void (*nf_conntrack_destroyed
)(struct nf_conn
*conntrack
) = NULL
;
72 unsigned int nf_conntrack_htable_size __read_mostly
;
73 int nf_conntrack_max __read_mostly
;
74 EXPORT_SYMBOL_GPL(nf_conntrack_max
);
75 struct list_head
*nf_conntrack_hash __read_mostly
;
76 struct nf_conn nf_conntrack_untracked __read_mostly
;
77 unsigned int nf_ct_log_invalid __read_mostly
;
78 LIST_HEAD(unconfirmed
);
79 static int nf_conntrack_vmalloc __read_mostly
;
81 static unsigned int nf_conntrack_next_id
;
83 DEFINE_PER_CPU(struct ip_conntrack_stat
, nf_conntrack_stat
);
84 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat
);
87 * This scheme offers various size of "struct nf_conn" dependent on
88 * features(helper, nat, ...)
91 #define NF_CT_FEATURES_NAMELEN 256
93 /* name of slab cache. printed in /proc/slabinfo */
96 /* size of slab cache */
99 /* slab cache pointer */
100 kmem_cache_t
*cachep
;
102 /* allocated slab cache + modules which uses this slab cache */
105 } nf_ct_cache
[NF_CT_F_NUM
];
107 /* protect members of nf_ct_cache except of "use" */
108 DEFINE_RWLOCK(nf_ct_cache_lock
);
110 /* This avoids calling kmem_cache_create() with same name simultaneously */
111 static DEFINE_MUTEX(nf_ct_cache_mutex
);
113 static int nf_conntrack_hash_rnd_initted
;
114 static unsigned int nf_conntrack_hash_rnd
;
116 static u_int32_t
__hash_conntrack(const struct nf_conntrack_tuple
*tuple
,
117 unsigned int size
, unsigned int rnd
)
120 a
= jhash((void *)tuple
->src
.u3
.all
, sizeof(tuple
->src
.u3
.all
),
121 ((tuple
->src
.l3num
) << 16) | tuple
->dst
.protonum
);
122 b
= jhash((void *)tuple
->dst
.u3
.all
, sizeof(tuple
->dst
.u3
.all
),
123 (tuple
->src
.u
.all
<< 16) | tuple
->dst
.u
.all
);
125 return jhash_2words(a
, b
, rnd
) % size
;
128 static inline u_int32_t
hash_conntrack(const struct nf_conntrack_tuple
*tuple
)
130 return __hash_conntrack(tuple
, nf_conntrack_htable_size
,
131 nf_conntrack_hash_rnd
);
134 int nf_conntrack_register_cache(u_int32_t features
, const char *name
,
139 kmem_cache_t
*cachep
;
141 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
142 features
, name
, size
);
144 if (features
< NF_CT_F_BASIC
|| features
>= NF_CT_F_NUM
) {
145 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
150 mutex_lock(&nf_ct_cache_mutex
);
152 write_lock_bh(&nf_ct_cache_lock
);
153 /* e.g: multiple helpers are loaded */
154 if (nf_ct_cache
[features
].use
> 0) {
155 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
156 if ((!strncmp(nf_ct_cache
[features
].name
, name
,
157 NF_CT_FEATURES_NAMELEN
))
158 && nf_ct_cache
[features
].size
== size
) {
159 DEBUGP("nf_conntrack_register_cache: reusing.\n");
160 nf_ct_cache
[features
].use
++;
165 write_unlock_bh(&nf_ct_cache_lock
);
166 mutex_unlock(&nf_ct_cache_mutex
);
169 write_unlock_bh(&nf_ct_cache_lock
);
172 * The memory space for name of slab cache must be alive until
173 * cache is destroyed.
175 cache_name
= kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN
, GFP_ATOMIC
);
176 if (cache_name
== NULL
) {
177 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
182 if (strlcpy(cache_name
, name
, NF_CT_FEATURES_NAMELEN
)
183 >= NF_CT_FEATURES_NAMELEN
) {
184 printk("nf_conntrack_register_cache: name too long\n");
189 cachep
= kmem_cache_create(cache_name
, size
, 0, 0,
192 printk("nf_conntrack_register_cache: Can't create slab cache "
193 "for the features = 0x%x\n", features
);
198 write_lock_bh(&nf_ct_cache_lock
);
199 nf_ct_cache
[features
].use
= 1;
200 nf_ct_cache
[features
].size
= size
;
201 nf_ct_cache
[features
].cachep
= cachep
;
202 nf_ct_cache
[features
].name
= cache_name
;
203 write_unlock_bh(&nf_ct_cache_lock
);
210 mutex_unlock(&nf_ct_cache_mutex
);
214 /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
215 void nf_conntrack_unregister_cache(u_int32_t features
)
217 kmem_cache_t
*cachep
;
221 * This assures that kmem_cache_create() isn't called before destroying
224 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features
);
225 mutex_lock(&nf_ct_cache_mutex
);
227 write_lock_bh(&nf_ct_cache_lock
);
228 if (--nf_ct_cache
[features
].use
> 0) {
229 write_unlock_bh(&nf_ct_cache_lock
);
230 mutex_unlock(&nf_ct_cache_mutex
);
233 cachep
= nf_ct_cache
[features
].cachep
;
234 name
= nf_ct_cache
[features
].name
;
235 nf_ct_cache
[features
].cachep
= NULL
;
236 nf_ct_cache
[features
].name
= NULL
;
237 nf_ct_cache
[features
].size
= 0;
238 write_unlock_bh(&nf_ct_cache_lock
);
242 kmem_cache_destroy(cachep
);
245 mutex_unlock(&nf_ct_cache_mutex
);
249 nf_ct_get_tuple(const struct sk_buff
*skb
,
251 unsigned int dataoff
,
254 struct nf_conntrack_tuple
*tuple
,
255 const struct nf_conntrack_l3proto
*l3proto
,
256 const struct nf_conntrack_l4proto
*l4proto
)
258 NF_CT_TUPLE_U_BLANK(tuple
);
260 tuple
->src
.l3num
= l3num
;
261 if (l3proto
->pkt_to_tuple(skb
, nhoff
, tuple
) == 0)
264 tuple
->dst
.protonum
= protonum
;
265 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
267 return l4proto
->pkt_to_tuple(skb
, dataoff
, tuple
);
271 nf_ct_invert_tuple(struct nf_conntrack_tuple
*inverse
,
272 const struct nf_conntrack_tuple
*orig
,
273 const struct nf_conntrack_l3proto
*l3proto
,
274 const struct nf_conntrack_l4proto
*l4proto
)
276 NF_CT_TUPLE_U_BLANK(inverse
);
278 inverse
->src
.l3num
= orig
->src
.l3num
;
279 if (l3proto
->invert_tuple(inverse
, orig
) == 0)
282 inverse
->dst
.dir
= !orig
->dst
.dir
;
284 inverse
->dst
.protonum
= orig
->dst
.protonum
;
285 return l4proto
->invert_tuple(inverse
, orig
);
289 clean_from_lists(struct nf_conn
*ct
)
291 DEBUGP("clean_from_lists(%p)\n", ct
);
292 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
293 list_del(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
);
295 /* Destroy all pending expectations */
296 nf_ct_remove_expectations(ct
);
300 destroy_conntrack(struct nf_conntrack
*nfct
)
302 struct nf_conn
*ct
= (struct nf_conn
*)nfct
;
303 struct nf_conn_help
*help
= nfct_help(ct
);
304 struct nf_conntrack_l3proto
*l3proto
;
305 struct nf_conntrack_l4proto
*l4proto
;
307 DEBUGP("destroy_conntrack(%p)\n", ct
);
308 NF_CT_ASSERT(atomic_read(&nfct
->use
) == 0);
309 NF_CT_ASSERT(!timer_pending(&ct
->timeout
));
311 nf_conntrack_event(IPCT_DESTROY
, ct
);
312 set_bit(IPS_DYING_BIT
, &ct
->status
);
314 if (help
&& help
->helper
&& help
->helper
->destroy
)
315 help
->helper
->destroy(ct
);
317 /* To make sure we don't get any weird locking issues here:
318 * destroy_conntrack() MUST NOT be called with a write lock
319 * 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
, ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.protonum
);
325 if (l4proto
&& l4proto
->destroy
)
326 l4proto
->destroy(ct
);
328 if (nf_conntrack_destroyed
)
329 nf_conntrack_destroyed(ct
);
331 write_lock_bh(&nf_conntrack_lock
);
332 /* Expectations will have been removed in clean_from_lists,
333 * except TFTP can create an expectation on the first packet,
334 * before connection is in the list, so we need to clean here,
336 nf_ct_remove_expectations(ct
);
338 /* We overload first tuple to link into unconfirmed list. */
339 if (!nf_ct_is_confirmed(ct
)) {
340 BUG_ON(list_empty(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
));
341 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
344 NF_CT_STAT_INC(delete);
345 write_unlock_bh(&nf_conntrack_lock
);
348 nf_ct_put(ct
->master
);
350 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct
);
351 nf_conntrack_free(ct
);
354 static void death_by_timeout(unsigned long ul_conntrack
)
356 struct nf_conn
*ct
= (void *)ul_conntrack
;
358 write_lock_bh(&nf_conntrack_lock
);
359 /* Inside lock so preempt is disabled on module removal path.
360 * Otherwise we can get spurious warnings. */
361 NF_CT_STAT_INC(delete_list
);
362 clean_from_lists(ct
);
363 write_unlock_bh(&nf_conntrack_lock
);
367 struct nf_conntrack_tuple_hash
*
368 __nf_conntrack_find(const struct nf_conntrack_tuple
*tuple
,
369 const struct nf_conn
*ignored_conntrack
)
371 struct nf_conntrack_tuple_hash
*h
;
372 unsigned int hash
= hash_conntrack(tuple
);
374 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
) {
375 if (nf_ct_tuplehash_to_ctrack(h
) != ignored_conntrack
&&
376 nf_ct_tuple_equal(tuple
, &h
->tuple
)) {
377 NF_CT_STAT_INC(found
);
380 NF_CT_STAT_INC(searched
);
386 /* Find a connection corresponding to a tuple. */
387 struct nf_conntrack_tuple_hash
*
388 nf_conntrack_find_get(const struct nf_conntrack_tuple
*tuple
,
389 const struct nf_conn
*ignored_conntrack
)
391 struct nf_conntrack_tuple_hash
*h
;
393 read_lock_bh(&nf_conntrack_lock
);
394 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
396 atomic_inc(&nf_ct_tuplehash_to_ctrack(h
)->ct_general
.use
);
397 read_unlock_bh(&nf_conntrack_lock
);
402 static void __nf_conntrack_hash_insert(struct nf_conn
*ct
,
404 unsigned int repl_hash
)
406 ct
->id
= ++nf_conntrack_next_id
;
407 list_add(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
,
408 &nf_conntrack_hash
[hash
]);
409 list_add(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
,
410 &nf_conntrack_hash
[repl_hash
]);
413 void nf_conntrack_hash_insert(struct nf_conn
*ct
)
415 unsigned int hash
, repl_hash
;
417 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
418 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
420 write_lock_bh(&nf_conntrack_lock
);
421 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
422 write_unlock_bh(&nf_conntrack_lock
);
425 /* Confirm a connection given skb; places it in hash table */
427 __nf_conntrack_confirm(struct sk_buff
**pskb
)
429 unsigned int hash
, repl_hash
;
430 struct nf_conntrack_tuple_hash
*h
;
432 struct nf_conn_help
*help
;
433 enum ip_conntrack_info ctinfo
;
435 ct
= nf_ct_get(*pskb
, &ctinfo
);
437 /* ipt_REJECT uses nf_conntrack_attach to attach related
438 ICMP/TCP RST packets in other direction. Actual packet
439 which created connection will be IP_CT_NEW or for an
440 expected connection, IP_CT_RELATED. */
441 if (CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
)
444 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
445 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
447 /* We're not in hash table, and we refuse to set up related
448 connections for unconfirmed conns. But packet copies and
449 REJECT will give spurious warnings here. */
450 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
452 /* No external references means noone else could have
454 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
455 DEBUGP("Confirming conntrack %p\n", ct
);
457 write_lock_bh(&nf_conntrack_lock
);
459 /* See if there's one in the list already, including reverse:
460 NAT could have grabbed it without realizing, since we're
461 not in the hash. If there is, we lost race. */
462 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
)
463 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
466 list_for_each_entry(h
, &nf_conntrack_hash
[repl_hash
], list
)
467 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
471 /* Remove from unconfirmed list */
472 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
474 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
475 /* Timer relative to confirmation time, not original
476 setting time, otherwise we'd get timer wrap in
477 weird delay cases. */
478 ct
->timeout
.expires
+= jiffies
;
479 add_timer(&ct
->timeout
);
480 atomic_inc(&ct
->ct_general
.use
);
481 set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
482 NF_CT_STAT_INC(insert
);
483 write_unlock_bh(&nf_conntrack_lock
);
484 help
= nfct_help(ct
);
485 if (help
&& help
->helper
)
486 nf_conntrack_event_cache(IPCT_HELPER
, *pskb
);
487 #ifdef CONFIG_NF_NAT_NEEDED
488 if (test_bit(IPS_SRC_NAT_DONE_BIT
, &ct
->status
) ||
489 test_bit(IPS_DST_NAT_DONE_BIT
, &ct
->status
))
490 nf_conntrack_event_cache(IPCT_NATINFO
, *pskb
);
492 nf_conntrack_event_cache(master_ct(ct
) ?
493 IPCT_RELATED
: IPCT_NEW
, *pskb
);
497 NF_CT_STAT_INC(insert_failed
);
498 write_unlock_bh(&nf_conntrack_lock
);
502 /* Returns true if a connection correspondings to the tuple (required
505 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple
*tuple
,
506 const struct nf_conn
*ignored_conntrack
)
508 struct nf_conntrack_tuple_hash
*h
;
510 read_lock_bh(&nf_conntrack_lock
);
511 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
512 read_unlock_bh(&nf_conntrack_lock
);
517 /* There's a small race here where we may free a just-assured
518 connection. Too bad: we're in trouble anyway. */
519 static int early_drop(struct list_head
*chain
)
521 /* Traverse backwards: gives us oldest, which is roughly LRU */
522 struct nf_conntrack_tuple_hash
*h
;
523 struct nf_conn
*ct
= NULL
, *tmp
;
526 read_lock_bh(&nf_conntrack_lock
);
527 list_for_each_entry_reverse(h
, chain
, list
) {
528 tmp
= nf_ct_tuplehash_to_ctrack(h
);
529 if (!test_bit(IPS_ASSURED_BIT
, &tmp
->status
)) {
531 atomic_inc(&ct
->ct_general
.use
);
535 read_unlock_bh(&nf_conntrack_lock
);
540 if (del_timer(&ct
->timeout
)) {
541 death_by_timeout((unsigned long)ct
);
543 NF_CT_STAT_INC(early_drop
);
549 static struct nf_conn
*
550 __nf_conntrack_alloc(const struct nf_conntrack_tuple
*orig
,
551 const struct nf_conntrack_tuple
*repl
,
552 const struct nf_conntrack_l3proto
*l3proto
,
555 struct nf_conn
*conntrack
= NULL
;
556 struct nf_conntrack_helper
*helper
;
558 if (unlikely(!nf_conntrack_hash_rnd_initted
)) {
559 get_random_bytes(&nf_conntrack_hash_rnd
, 4);
560 nf_conntrack_hash_rnd_initted
= 1;
563 /* We don't want any race condition at early drop stage */
564 atomic_inc(&nf_conntrack_count
);
567 && atomic_read(&nf_conntrack_count
) > nf_conntrack_max
) {
568 unsigned int hash
= hash_conntrack(orig
);
569 /* Try dropping from this hash chain. */
570 if (!early_drop(&nf_conntrack_hash
[hash
])) {
571 atomic_dec(&nf_conntrack_count
);
574 "nf_conntrack: table full, dropping"
576 return ERR_PTR(-ENOMEM
);
580 /* find features needed by this conntrack. */
581 features
|= l3proto
->get_features(orig
);
583 /* FIXME: protect helper list per RCU */
584 read_lock_bh(&nf_conntrack_lock
);
585 helper
= __nf_ct_helper_find(repl
);
586 /* NAT might want to assign a helper later */
587 if (helper
|| features
& NF_CT_F_NAT
)
588 features
|= NF_CT_F_HELP
;
589 read_unlock_bh(&nf_conntrack_lock
);
591 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features
);
593 read_lock_bh(&nf_ct_cache_lock
);
595 if (unlikely(!nf_ct_cache
[features
].use
)) {
596 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
601 conntrack
= kmem_cache_alloc(nf_ct_cache
[features
].cachep
, GFP_ATOMIC
);
602 if (conntrack
== NULL
) {
603 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
607 memset(conntrack
, 0, nf_ct_cache
[features
].size
);
608 conntrack
->features
= features
;
609 atomic_set(&conntrack
->ct_general
.use
, 1);
610 conntrack
->ct_general
.destroy
= destroy_conntrack
;
611 conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
= *orig
;
612 conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
= *repl
;
613 /* Don't set timer yet: wait for confirmation */
614 init_timer(&conntrack
->timeout
);
615 conntrack
->timeout
.data
= (unsigned long)conntrack
;
616 conntrack
->timeout
.function
= death_by_timeout
;
617 read_unlock_bh(&nf_ct_cache_lock
);
621 read_unlock_bh(&nf_ct_cache_lock
);
622 atomic_dec(&nf_conntrack_count
);
626 struct nf_conn
*nf_conntrack_alloc(const struct nf_conntrack_tuple
*orig
,
627 const struct nf_conntrack_tuple
*repl
)
629 struct nf_conntrack_l3proto
*l3proto
;
631 l3proto
= __nf_ct_l3proto_find(orig
->src
.l3num
);
632 return __nf_conntrack_alloc(orig
, repl
, l3proto
, 0);
635 void nf_conntrack_free(struct nf_conn
*conntrack
)
637 u_int32_t features
= conntrack
->features
;
638 NF_CT_ASSERT(features
>= NF_CT_F_BASIC
&& features
< NF_CT_F_NUM
);
639 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features
,
641 kmem_cache_free(nf_ct_cache
[features
].cachep
, conntrack
);
642 atomic_dec(&nf_conntrack_count
);
645 /* Allocate a new conntrack: we return -ENOMEM if classification
646 failed due to stress. Otherwise it really is unclassifiable. */
647 static struct nf_conntrack_tuple_hash
*
648 init_conntrack(const struct nf_conntrack_tuple
*tuple
,
649 struct nf_conntrack_l3proto
*l3proto
,
650 struct nf_conntrack_l4proto
*l4proto
,
652 unsigned int dataoff
)
654 struct nf_conn
*conntrack
;
655 struct nf_conntrack_tuple repl_tuple
;
656 struct nf_conntrack_expect
*exp
;
657 u_int32_t features
= 0;
659 if (!nf_ct_invert_tuple(&repl_tuple
, tuple
, l3proto
, l4proto
)) {
660 DEBUGP("Can't invert tuple.\n");
664 read_lock_bh(&nf_conntrack_lock
);
665 exp
= __nf_conntrack_expect_find(tuple
);
666 if (exp
&& exp
->helper
)
667 features
= NF_CT_F_HELP
;
668 read_unlock_bh(&nf_conntrack_lock
);
670 conntrack
= __nf_conntrack_alloc(tuple
, &repl_tuple
, l3proto
, features
);
671 if (conntrack
== NULL
|| IS_ERR(conntrack
)) {
672 DEBUGP("Can't allocate conntrack.\n");
673 return (struct nf_conntrack_tuple_hash
*)conntrack
;
676 if (!l4proto
->new(conntrack
, skb
, dataoff
)) {
677 nf_conntrack_free(conntrack
);
678 DEBUGP("init conntrack: can't track with proto module\n");
682 write_lock_bh(&nf_conntrack_lock
);
683 exp
= find_expectation(tuple
);
686 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
688 /* Welcome, Mr. Bond. We've been expecting you... */
689 __set_bit(IPS_EXPECTED_BIT
, &conntrack
->status
);
690 conntrack
->master
= exp
->master
;
692 nfct_help(conntrack
)->helper
= exp
->helper
;
693 #ifdef CONFIG_NF_CONNTRACK_MARK
694 conntrack
->mark
= exp
->master
->mark
;
696 #ifdef CONFIG_NF_CONNTRACK_SECMARK
697 conntrack
->secmark
= exp
->master
->secmark
;
699 nf_conntrack_get(&conntrack
->master
->ct_general
);
700 NF_CT_STAT_INC(expect_new
);
702 struct nf_conn_help
*help
= nfct_help(conntrack
);
705 help
->helper
= __nf_ct_helper_find(&repl_tuple
);
709 /* Overload tuple linked list to put us in unconfirmed list. */
710 list_add(&conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
, &unconfirmed
);
712 write_unlock_bh(&nf_conntrack_lock
);
716 exp
->expectfn(conntrack
, exp
);
717 nf_conntrack_expect_put(exp
);
720 return &conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
];
723 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
724 static inline struct nf_conn
*
725 resolve_normal_ct(struct sk_buff
*skb
,
726 unsigned int dataoff
,
729 struct nf_conntrack_l3proto
*l3proto
,
730 struct nf_conntrack_l4proto
*l4proto
,
732 enum ip_conntrack_info
*ctinfo
)
734 struct nf_conntrack_tuple tuple
;
735 struct nf_conntrack_tuple_hash
*h
;
738 if (!nf_ct_get_tuple(skb
, (unsigned int)(skb
->nh
.raw
- skb
->data
),
739 dataoff
, l3num
, protonum
, &tuple
, l3proto
,
741 DEBUGP("resolve_normal_ct: Can't get tuple\n");
745 /* look for tuple match */
746 h
= nf_conntrack_find_get(&tuple
, NULL
);
748 h
= init_conntrack(&tuple
, l3proto
, l4proto
, skb
, dataoff
);
754 ct
= nf_ct_tuplehash_to_ctrack(h
);
756 /* It exists; we have (non-exclusive) reference. */
757 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
) {
758 *ctinfo
= IP_CT_ESTABLISHED
+ IP_CT_IS_REPLY
;
759 /* Please set reply bit if this packet OK */
762 /* Once we've had two way comms, always ESTABLISHED. */
763 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
764 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct
);
765 *ctinfo
= IP_CT_ESTABLISHED
;
766 } else if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
)) {
767 DEBUGP("nf_conntrack_in: related packet for %p\n", ct
);
768 *ctinfo
= IP_CT_RELATED
;
770 DEBUGP("nf_conntrack_in: new packet for %p\n", ct
);
775 skb
->nfct
= &ct
->ct_general
;
776 skb
->nfctinfo
= *ctinfo
;
781 nf_conntrack_in(int pf
, unsigned int hooknum
, struct sk_buff
**pskb
)
784 enum ip_conntrack_info ctinfo
;
785 struct nf_conntrack_l3proto
*l3proto
;
786 struct nf_conntrack_l4proto
*l4proto
;
787 unsigned int dataoff
;
792 /* Previously seen (loopback or untracked)? Ignore. */
794 NF_CT_STAT_INC(ignore
);
798 l3proto
= __nf_ct_l3proto_find((u_int16_t
)pf
);
799 if ((ret
= l3proto
->prepare(pskb
, hooknum
, &dataoff
, &protonum
)) <= 0) {
800 DEBUGP("not prepared to track yet or error occured\n");
804 l4proto
= __nf_ct_l4proto_find((u_int16_t
)pf
, protonum
);
806 /* It may be an special packet, error, unclean...
807 * inverse of the return code tells to the netfilter
808 * core what to do with the packet. */
809 if (l4proto
->error
!= NULL
&&
810 (ret
= l4proto
->error(*pskb
, dataoff
, &ctinfo
, pf
, hooknum
)) <= 0) {
811 NF_CT_STAT_INC(error
);
812 NF_CT_STAT_INC(invalid
);
816 ct
= resolve_normal_ct(*pskb
, dataoff
, pf
, protonum
, l3proto
, l4proto
,
817 &set_reply
, &ctinfo
);
819 /* Not valid part of a connection */
820 NF_CT_STAT_INC(invalid
);
825 /* Too stressed to deal. */
826 NF_CT_STAT_INC(drop
);
830 NF_CT_ASSERT((*pskb
)->nfct
);
832 ret
= l4proto
->packet(ct
, *pskb
, dataoff
, ctinfo
, pf
, hooknum
);
834 /* Invalid: inverse of the return code tells
835 * the netfilter core what to do */
836 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
837 nf_conntrack_put((*pskb
)->nfct
);
838 (*pskb
)->nfct
= NULL
;
839 NF_CT_STAT_INC(invalid
);
843 if (set_reply
&& !test_and_set_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
844 nf_conntrack_event_cache(IPCT_STATUS
, *pskb
);
849 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple
*inverse
,
850 const struct nf_conntrack_tuple
*orig
)
852 return nf_ct_invert_tuple(inverse
, orig
,
853 __nf_ct_l3proto_find(orig
->src
.l3num
),
854 __nf_ct_l4proto_find(orig
->src
.l3num
,
855 orig
->dst
.protonum
));
858 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
859 implicitly racy: see __nf_conntrack_confirm */
860 void nf_conntrack_alter_reply(struct nf_conn
*ct
,
861 const struct nf_conntrack_tuple
*newreply
)
863 struct nf_conn_help
*help
= nfct_help(ct
);
865 write_lock_bh(&nf_conntrack_lock
);
866 /* Should be unconfirmed, so not in hash table yet */
867 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
869 DEBUGP("Altering reply tuple of %p to ", ct
);
870 NF_CT_DUMP_TUPLE(newreply
);
872 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
= *newreply
;
873 if (!ct
->master
&& help
&& help
->expecting
== 0)
874 help
->helper
= __nf_ct_helper_find(newreply
);
875 write_unlock_bh(&nf_conntrack_lock
);
878 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
879 void __nf_ct_refresh_acct(struct nf_conn
*ct
,
880 enum ip_conntrack_info ctinfo
,
881 const struct sk_buff
*skb
,
882 unsigned long extra_jiffies
,
887 NF_CT_ASSERT(ct
->timeout
.data
== (unsigned long)ct
);
890 write_lock_bh(&nf_conntrack_lock
);
892 /* Only update if this is not a fixed timeout */
893 if (test_bit(IPS_FIXED_TIMEOUT_BIT
, &ct
->status
)) {
894 write_unlock_bh(&nf_conntrack_lock
);
898 /* If not in hash table, timer will not be active yet */
899 if (!nf_ct_is_confirmed(ct
)) {
900 ct
->timeout
.expires
= extra_jiffies
;
901 event
= IPCT_REFRESH
;
903 unsigned long newtime
= jiffies
+ extra_jiffies
;
905 /* Only update the timeout if the new timeout is at least
906 HZ jiffies from the old timeout. Need del_timer for race
907 avoidance (may already be dying). */
908 if (newtime
- ct
->timeout
.expires
>= HZ
909 && del_timer(&ct
->timeout
)) {
910 ct
->timeout
.expires
= newtime
;
911 add_timer(&ct
->timeout
);
912 event
= IPCT_REFRESH
;
916 #ifdef CONFIG_NF_CT_ACCT
918 ct
->counters
[CTINFO2DIR(ctinfo
)].packets
++;
919 ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
+=
920 skb
->len
- (unsigned int)(skb
->nh
.raw
- skb
->data
);
922 if ((ct
->counters
[CTINFO2DIR(ctinfo
)].packets
& 0x80000000)
923 || (ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
& 0x80000000))
924 event
|= IPCT_COUNTER_FILLING
;
928 write_unlock_bh(&nf_conntrack_lock
);
930 /* must be unlocked when calling event cache */
932 nf_conntrack_event_cache(event
, skb
);
935 #if defined(CONFIG_NF_CT_NETLINK) || \
936 defined(CONFIG_NF_CT_NETLINK_MODULE)
938 #include <linux/netfilter/nfnetlink.h>
939 #include <linux/netfilter/nfnetlink_conntrack.h>
940 #include <linux/mutex.h>
943 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
944 * in ip_conntrack_core, since we don't want the protocols to autoload
945 * or depend on ctnetlink */
946 int nf_ct_port_tuple_to_nfattr(struct sk_buff
*skb
,
947 const struct nf_conntrack_tuple
*tuple
)
949 NFA_PUT(skb
, CTA_PROTO_SRC_PORT
, sizeof(u_int16_t
),
950 &tuple
->src
.u
.tcp
.port
);
951 NFA_PUT(skb
, CTA_PROTO_DST_PORT
, sizeof(u_int16_t
),
952 &tuple
->dst
.u
.tcp
.port
);
959 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
960 [CTA_PROTO_SRC_PORT
-1] = sizeof(u_int16_t
),
961 [CTA_PROTO_DST_PORT
-1] = sizeof(u_int16_t
)
964 int nf_ct_port_nfattr_to_tuple(struct nfattr
*tb
[],
965 struct nf_conntrack_tuple
*t
)
967 if (!tb
[CTA_PROTO_SRC_PORT
-1] || !tb
[CTA_PROTO_DST_PORT
-1])
970 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
973 t
->src
.u
.tcp
.port
= *(__be16
*)NFA_DATA(tb
[CTA_PROTO_SRC_PORT
-1]);
974 t
->dst
.u
.tcp
.port
= *(__be16
*)NFA_DATA(tb
[CTA_PROTO_DST_PORT
-1]);
980 /* Used by ipt_REJECT and ip6t_REJECT. */
981 void __nf_conntrack_attach(struct sk_buff
*nskb
, struct sk_buff
*skb
)
984 enum ip_conntrack_info ctinfo
;
986 /* This ICMP is in reverse direction to the packet which caused it */
987 ct
= nf_ct_get(skb
, &ctinfo
);
988 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
)
989 ctinfo
= IP_CT_RELATED
+ IP_CT_IS_REPLY
;
991 ctinfo
= IP_CT_RELATED
;
993 /* Attach to new skbuff, and increment count */
994 nskb
->nfct
= &ct
->ct_general
;
995 nskb
->nfctinfo
= ctinfo
;
996 nf_conntrack_get(nskb
->nfct
);
1000 do_iter(const struct nf_conntrack_tuple_hash
*i
,
1001 int (*iter
)(struct nf_conn
*i
, void *data
),
1004 return iter(nf_ct_tuplehash_to_ctrack(i
), data
);
1007 /* Bring out ya dead! */
1008 static struct nf_conn
*
1009 get_next_corpse(int (*iter
)(struct nf_conn
*i
, void *data
),
1010 void *data
, unsigned int *bucket
)
1012 struct nf_conntrack_tuple_hash
*h
;
1015 write_lock_bh(&nf_conntrack_lock
);
1016 for (; *bucket
< nf_conntrack_htable_size
; (*bucket
)++) {
1017 list_for_each_entry(h
, &nf_conntrack_hash
[*bucket
], list
) {
1018 ct
= nf_ct_tuplehash_to_ctrack(h
);
1023 list_for_each_entry(h
, &unconfirmed
, list
) {
1024 ct
= nf_ct_tuplehash_to_ctrack(h
);
1028 write_unlock_bh(&nf_conntrack_lock
);
1031 atomic_inc(&ct
->ct_general
.use
);
1032 write_unlock_bh(&nf_conntrack_lock
);
1037 nf_ct_iterate_cleanup(int (*iter
)(struct nf_conn
*i
, void *data
), void *data
)
1040 unsigned int bucket
= 0;
1042 while ((ct
= get_next_corpse(iter
, data
, &bucket
)) != NULL
) {
1043 /* Time to push up daises... */
1044 if (del_timer(&ct
->timeout
))
1045 death_by_timeout((unsigned long)ct
);
1046 /* ... else the timer will get him soon. */
1052 static int kill_all(struct nf_conn
*i
, void *data
)
1057 static void free_conntrack_hash(struct list_head
*hash
, int vmalloced
, int size
)
1062 free_pages((unsigned long)hash
,
1063 get_order(sizeof(struct list_head
) * size
));
1066 void nf_conntrack_flush()
1068 nf_ct_iterate_cleanup(kill_all
, NULL
);
1071 /* Mishearing the voices in his head, our hero wonders how he's
1072 supposed to kill the mall. */
1073 void nf_conntrack_cleanup(void)
1077 ip_ct_attach
= NULL
;
1079 /* This makes sure all current packets have passed through
1080 netfilter framework. Roll on, two-stage module
1084 nf_ct_event_cache_flush();
1086 nf_conntrack_flush();
1087 if (atomic_read(&nf_conntrack_count
) != 0) {
1089 goto i_see_dead_people
;
1091 /* wait until all references to nf_conntrack_untracked are dropped */
1092 while (atomic_read(&nf_conntrack_untracked
.ct_general
.use
) > 1)
1095 for (i
= 0; i
< NF_CT_F_NUM
; i
++) {
1096 if (nf_ct_cache
[i
].use
== 0)
1099 NF_CT_ASSERT(nf_ct_cache
[i
].use
== 1);
1100 nf_ct_cache
[i
].use
= 1;
1101 nf_conntrack_unregister_cache(i
);
1103 kmem_cache_destroy(nf_conntrack_expect_cachep
);
1104 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1105 nf_conntrack_htable_size
);
1107 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_generic
);
1109 /* free l3proto protocol tables */
1110 for (i
= 0; i
< PF_MAX
; i
++)
1111 if (nf_ct_protos
[i
]) {
1112 kfree(nf_ct_protos
[i
]);
1113 nf_ct_protos
[i
] = NULL
;
1117 static struct list_head
*alloc_hashtable(int size
, int *vmalloced
)
1119 struct list_head
*hash
;
1123 hash
= (void*)__get_free_pages(GFP_KERNEL
,
1124 get_order(sizeof(struct list_head
)
1128 printk(KERN_WARNING
"nf_conntrack: falling back to vmalloc.\n");
1129 hash
= vmalloc(sizeof(struct list_head
) * size
);
1133 for (i
= 0; i
< size
; i
++)
1134 INIT_LIST_HEAD(&hash
[i
]);
1139 int set_hashsize(const char *val
, struct kernel_param
*kp
)
1141 int i
, bucket
, hashsize
, vmalloced
;
1142 int old_vmalloced
, old_size
;
1144 struct list_head
*hash
, *old_hash
;
1145 struct nf_conntrack_tuple_hash
*h
;
1147 /* On boot, we can set this without any fancy locking. */
1148 if (!nf_conntrack_htable_size
)
1149 return param_set_uint(val
, kp
);
1151 hashsize
= simple_strtol(val
, NULL
, 0);
1155 hash
= alloc_hashtable(hashsize
, &vmalloced
);
1159 /* We have to rehahs for the new table anyway, so we also can
1160 * use a newrandom seed */
1161 get_random_bytes(&rnd
, 4);
1163 write_lock_bh(&nf_conntrack_lock
);
1164 for (i
= 0; i
< nf_conntrack_htable_size
; i
++) {
1165 while (!list_empty(&nf_conntrack_hash
[i
])) {
1166 h
= list_entry(nf_conntrack_hash
[i
].next
,
1167 struct nf_conntrack_tuple_hash
, list
);
1169 bucket
= __hash_conntrack(&h
->tuple
, hashsize
, rnd
);
1170 list_add_tail(&h
->list
, &hash
[bucket
]);
1173 old_size
= nf_conntrack_htable_size
;
1174 old_vmalloced
= nf_conntrack_vmalloc
;
1175 old_hash
= nf_conntrack_hash
;
1177 nf_conntrack_htable_size
= hashsize
;
1178 nf_conntrack_vmalloc
= vmalloced
;
1179 nf_conntrack_hash
= hash
;
1180 nf_conntrack_hash_rnd
= rnd
;
1181 write_unlock_bh(&nf_conntrack_lock
);
1183 free_conntrack_hash(old_hash
, old_vmalloced
, old_size
);
1187 module_param_call(hashsize
, set_hashsize
, param_get_uint
,
1188 &nf_conntrack_htable_size
, 0600);
1190 int __init
nf_conntrack_init(void)
1195 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1196 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1197 if (!nf_conntrack_htable_size
) {
1198 nf_conntrack_htable_size
1199 = (((num_physpages
<< PAGE_SHIFT
) / 16384)
1200 / sizeof(struct list_head
));
1201 if (num_physpages
> (1024 * 1024 * 1024 / PAGE_SIZE
))
1202 nf_conntrack_htable_size
= 8192;
1203 if (nf_conntrack_htable_size
< 16)
1204 nf_conntrack_htable_size
= 16;
1206 nf_conntrack_max
= 8 * nf_conntrack_htable_size
;
1208 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1209 NF_CONNTRACK_VERSION
, nf_conntrack_htable_size
,
1212 nf_conntrack_hash
= alloc_hashtable(nf_conntrack_htable_size
,
1213 &nf_conntrack_vmalloc
);
1214 if (!nf_conntrack_hash
) {
1215 printk(KERN_ERR
"Unable to create nf_conntrack_hash\n");
1219 ret
= nf_conntrack_register_cache(NF_CT_F_BASIC
, "nf_conntrack:basic",
1220 sizeof(struct nf_conn
));
1222 printk(KERN_ERR
"Unable to create nf_conn slab cache\n");
1226 nf_conntrack_expect_cachep
= kmem_cache_create("nf_conntrack_expect",
1227 sizeof(struct nf_conntrack_expect
),
1229 if (!nf_conntrack_expect_cachep
) {
1230 printk(KERN_ERR
"Unable to create nf_expect slab cache\n");
1231 goto err_free_conntrack_slab
;
1234 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_generic
);
1236 goto out_free_expect_slab
;
1238 /* Don't NEED lock here, but good form anyway. */
1239 write_lock_bh(&nf_conntrack_lock
);
1240 for (i
= 0; i
< AF_MAX
; i
++)
1241 nf_ct_l3protos
[i
] = &nf_conntrack_l3proto_generic
;
1242 write_unlock_bh(&nf_conntrack_lock
);
1244 /* For use by REJECT target */
1245 ip_ct_attach
= __nf_conntrack_attach
;
1247 /* Set up fake conntrack:
1248 - to never be deleted, not in any hashes */
1249 atomic_set(&nf_conntrack_untracked
.ct_general
.use
, 1);
1250 /* - and look it like as a confirmed connection */
1251 set_bit(IPS_CONFIRMED_BIT
, &nf_conntrack_untracked
.status
);
1255 out_free_expect_slab
:
1256 kmem_cache_destroy(nf_conntrack_expect_cachep
);
1257 err_free_conntrack_slab
:
1258 nf_conntrack_unregister_cache(NF_CT_F_BASIC
);
1260 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1261 nf_conntrack_htable_size
);