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 /* This rwlock protects the main hash table, protocol/helper/expected
51 registrations, conntrack timers*/
52 #define ASSERT_READ_LOCK(x)
53 #define ASSERT_WRITE_LOCK(x)
55 #include <net/netfilter/nf_conntrack.h>
56 #include <net/netfilter/nf_conntrack_l3proto.h>
57 #include <net/netfilter/nf_conntrack_l4proto.h>
58 #include <net/netfilter/nf_conntrack_expect.h>
59 #include <net/netfilter/nf_conntrack_helper.h>
60 #include <net/netfilter/nf_conntrack_core.h>
62 #define NF_CONNTRACK_VERSION "0.5.0"
67 #define DEBUGP(format, args...)
70 DEFINE_RWLOCK(nf_conntrack_lock
);
72 /* nf_conntrack_standalone needs this */
73 atomic_t nf_conntrack_count
= ATOMIC_INIT(0);
75 void (*nf_conntrack_destroyed
)(struct nf_conn
*conntrack
) = NULL
;
76 unsigned int nf_conntrack_htable_size __read_mostly
;
77 int nf_conntrack_max __read_mostly
;
78 struct list_head
*nf_conntrack_hash __read_mostly
;
79 struct nf_conn nf_conntrack_untracked __read_mostly
;
80 unsigned int nf_ct_log_invalid __read_mostly
;
81 LIST_HEAD(unconfirmed
);
82 static int nf_conntrack_vmalloc __read_mostly
;
84 static unsigned int nf_conntrack_next_id
;
86 DEFINE_PER_CPU(struct ip_conntrack_stat
, nf_conntrack_stat
);
87 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat
);
90 * This scheme offers various size of "struct nf_conn" dependent on
91 * features(helper, nat, ...)
94 #define NF_CT_FEATURES_NAMELEN 256
96 /* name of slab cache. printed in /proc/slabinfo */
99 /* size of slab cache */
102 /* slab cache pointer */
103 kmem_cache_t
*cachep
;
105 /* allocated slab cache + modules which uses this slab cache */
108 } nf_ct_cache
[NF_CT_F_NUM
];
110 /* protect members of nf_ct_cache except of "use" */
111 DEFINE_RWLOCK(nf_ct_cache_lock
);
113 /* This avoids calling kmem_cache_create() with same name simultaneously */
114 static DEFINE_MUTEX(nf_ct_cache_mutex
);
116 static int nf_conntrack_hash_rnd_initted
;
117 static unsigned int nf_conntrack_hash_rnd
;
119 static u_int32_t
__hash_conntrack(const struct nf_conntrack_tuple
*tuple
,
120 unsigned int size
, unsigned int rnd
)
123 a
= jhash((void *)tuple
->src
.u3
.all
, sizeof(tuple
->src
.u3
.all
),
124 ((tuple
->src
.l3num
) << 16) | tuple
->dst
.protonum
);
125 b
= jhash((void *)tuple
->dst
.u3
.all
, sizeof(tuple
->dst
.u3
.all
),
126 (tuple
->src
.u
.all
<< 16) | tuple
->dst
.u
.all
);
128 return jhash_2words(a
, b
, rnd
) % size
;
131 static inline u_int32_t
hash_conntrack(const struct nf_conntrack_tuple
*tuple
)
133 return __hash_conntrack(tuple
, nf_conntrack_htable_size
,
134 nf_conntrack_hash_rnd
);
137 int nf_conntrack_register_cache(u_int32_t features
, const char *name
,
142 kmem_cache_t
*cachep
;
144 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
145 features
, name
, size
);
147 if (features
< NF_CT_F_BASIC
|| features
>= NF_CT_F_NUM
) {
148 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
153 mutex_lock(&nf_ct_cache_mutex
);
155 write_lock_bh(&nf_ct_cache_lock
);
156 /* e.g: multiple helpers are loaded */
157 if (nf_ct_cache
[features
].use
> 0) {
158 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
159 if ((!strncmp(nf_ct_cache
[features
].name
, name
,
160 NF_CT_FEATURES_NAMELEN
))
161 && nf_ct_cache
[features
].size
== size
) {
162 DEBUGP("nf_conntrack_register_cache: reusing.\n");
163 nf_ct_cache
[features
].use
++;
168 write_unlock_bh(&nf_ct_cache_lock
);
169 mutex_unlock(&nf_ct_cache_mutex
);
172 write_unlock_bh(&nf_ct_cache_lock
);
175 * The memory space for name of slab cache must be alive until
176 * cache is destroyed.
178 cache_name
= kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN
, GFP_ATOMIC
);
179 if (cache_name
== NULL
) {
180 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
185 if (strlcpy(cache_name
, name
, NF_CT_FEATURES_NAMELEN
)
186 >= NF_CT_FEATURES_NAMELEN
) {
187 printk("nf_conntrack_register_cache: name too long\n");
192 cachep
= kmem_cache_create(cache_name
, size
, 0, 0,
195 printk("nf_conntrack_register_cache: Can't create slab cache "
196 "for the features = 0x%x\n", features
);
201 write_lock_bh(&nf_ct_cache_lock
);
202 nf_ct_cache
[features
].use
= 1;
203 nf_ct_cache
[features
].size
= size
;
204 nf_ct_cache
[features
].cachep
= cachep
;
205 nf_ct_cache
[features
].name
= cache_name
;
206 write_unlock_bh(&nf_ct_cache_lock
);
213 mutex_unlock(&nf_ct_cache_mutex
);
217 /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
218 void nf_conntrack_unregister_cache(u_int32_t features
)
220 kmem_cache_t
*cachep
;
224 * This assures that kmem_cache_create() isn't called before destroying
227 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features
);
228 mutex_lock(&nf_ct_cache_mutex
);
230 write_lock_bh(&nf_ct_cache_lock
);
231 if (--nf_ct_cache
[features
].use
> 0) {
232 write_unlock_bh(&nf_ct_cache_lock
);
233 mutex_unlock(&nf_ct_cache_mutex
);
236 cachep
= nf_ct_cache
[features
].cachep
;
237 name
= nf_ct_cache
[features
].name
;
238 nf_ct_cache
[features
].cachep
= NULL
;
239 nf_ct_cache
[features
].name
= NULL
;
240 nf_ct_cache
[features
].size
= 0;
241 write_unlock_bh(&nf_ct_cache_lock
);
245 kmem_cache_destroy(cachep
);
248 mutex_unlock(&nf_ct_cache_mutex
);
252 nf_ct_get_tuple(const struct sk_buff
*skb
,
254 unsigned int dataoff
,
257 struct nf_conntrack_tuple
*tuple
,
258 const struct nf_conntrack_l3proto
*l3proto
,
259 const struct nf_conntrack_l4proto
*l4proto
)
261 NF_CT_TUPLE_U_BLANK(tuple
);
263 tuple
->src
.l3num
= l3num
;
264 if (l3proto
->pkt_to_tuple(skb
, nhoff
, tuple
) == 0)
267 tuple
->dst
.protonum
= protonum
;
268 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
270 return l4proto
->pkt_to_tuple(skb
, dataoff
, tuple
);
274 nf_ct_invert_tuple(struct nf_conntrack_tuple
*inverse
,
275 const struct nf_conntrack_tuple
*orig
,
276 const struct nf_conntrack_l3proto
*l3proto
,
277 const struct nf_conntrack_l4proto
*l4proto
)
279 NF_CT_TUPLE_U_BLANK(inverse
);
281 inverse
->src
.l3num
= orig
->src
.l3num
;
282 if (l3proto
->invert_tuple(inverse
, orig
) == 0)
285 inverse
->dst
.dir
= !orig
->dst
.dir
;
287 inverse
->dst
.protonum
= orig
->dst
.protonum
;
288 return l4proto
->invert_tuple(inverse
, orig
);
292 clean_from_lists(struct nf_conn
*ct
)
294 DEBUGP("clean_from_lists(%p)\n", ct
);
295 ASSERT_WRITE_LOCK(&nf_conntrack_lock
);
296 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
297 list_del(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
);
299 /* Destroy all pending expectations */
300 nf_ct_remove_expectations(ct
);
304 destroy_conntrack(struct nf_conntrack
*nfct
)
306 struct nf_conn
*ct
= (struct nf_conn
*)nfct
;
307 struct nf_conntrack_l3proto
*l3proto
;
308 struct nf_conntrack_l4proto
*l4proto
;
310 DEBUGP("destroy_conntrack(%p)\n", ct
);
311 NF_CT_ASSERT(atomic_read(&nfct
->use
) == 0);
312 NF_CT_ASSERT(!timer_pending(&ct
->timeout
));
314 nf_conntrack_event(IPCT_DESTROY
, ct
);
315 set_bit(IPS_DYING_BIT
, &ct
->status
);
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 ASSERT_READ_LOCK(&nf_conntrack_lock
);
375 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
) {
376 if (nf_ct_tuplehash_to_ctrack(h
) != ignored_conntrack
&&
377 nf_ct_tuple_equal(tuple
, &h
->tuple
)) {
378 NF_CT_STAT_INC(found
);
381 NF_CT_STAT_INC(searched
);
387 /* Find a connection corresponding to a tuple. */
388 struct nf_conntrack_tuple_hash
*
389 nf_conntrack_find_get(const struct nf_conntrack_tuple
*tuple
,
390 const struct nf_conn
*ignored_conntrack
)
392 struct nf_conntrack_tuple_hash
*h
;
394 read_lock_bh(&nf_conntrack_lock
);
395 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
397 atomic_inc(&nf_ct_tuplehash_to_ctrack(h
)->ct_general
.use
);
398 read_unlock_bh(&nf_conntrack_lock
);
403 static void __nf_conntrack_hash_insert(struct nf_conn
*ct
,
405 unsigned int repl_hash
)
407 ct
->id
= ++nf_conntrack_next_id
;
408 list_add(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
,
409 &nf_conntrack_hash
[hash
]);
410 list_add(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
,
411 &nf_conntrack_hash
[repl_hash
]);
414 void nf_conntrack_hash_insert(struct nf_conn
*ct
)
416 unsigned int hash
, repl_hash
;
418 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
419 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
421 write_lock_bh(&nf_conntrack_lock
);
422 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
423 write_unlock_bh(&nf_conntrack_lock
);
426 /* Confirm a connection given skb; places it in hash table */
428 __nf_conntrack_confirm(struct sk_buff
**pskb
)
430 unsigned int hash
, repl_hash
;
431 struct nf_conntrack_tuple_hash
*h
;
433 struct nf_conn_help
*help
;
434 enum ip_conntrack_info ctinfo
;
436 ct
= nf_ct_get(*pskb
, &ctinfo
);
438 /* ipt_REJECT uses nf_conntrack_attach to attach related
439 ICMP/TCP RST packets in other direction. Actual packet
440 which created connection will be IP_CT_NEW or for an
441 expected connection, IP_CT_RELATED. */
442 if (CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
)
445 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
446 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
448 /* We're not in hash table, and we refuse to set up related
449 connections for unconfirmed conns. But packet copies and
450 REJECT will give spurious warnings here. */
451 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
453 /* No external references means noone else could have
455 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
456 DEBUGP("Confirming conntrack %p\n", ct
);
458 write_lock_bh(&nf_conntrack_lock
);
460 /* See if there's one in the list already, including reverse:
461 NAT could have grabbed it without realizing, since we're
462 not in the hash. If there is, we lost race. */
463 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
)
464 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
467 list_for_each_entry(h
, &nf_conntrack_hash
[repl_hash
], list
)
468 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
472 /* Remove from unconfirmed list */
473 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
475 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
476 /* Timer relative to confirmation time, not original
477 setting time, otherwise we'd get timer wrap in
478 weird delay cases. */
479 ct
->timeout
.expires
+= jiffies
;
480 add_timer(&ct
->timeout
);
481 atomic_inc(&ct
->ct_general
.use
);
482 set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
483 NF_CT_STAT_INC(insert
);
484 write_unlock_bh(&nf_conntrack_lock
);
485 help
= nfct_help(ct
);
486 if (help
&& help
->helper
)
487 nf_conntrack_event_cache(IPCT_HELPER
, *pskb
);
488 #ifdef CONFIG_NF_NAT_NEEDED
489 if (test_bit(IPS_SRC_NAT_DONE_BIT
, &ct
->status
) ||
490 test_bit(IPS_DST_NAT_DONE_BIT
, &ct
->status
))
491 nf_conntrack_event_cache(IPCT_NATINFO
, *pskb
);
493 nf_conntrack_event_cache(master_ct(ct
) ?
494 IPCT_RELATED
: IPCT_NEW
, *pskb
);
498 NF_CT_STAT_INC(insert_failed
);
499 write_unlock_bh(&nf_conntrack_lock
);
503 /* Returns true if a connection correspondings to the tuple (required
506 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple
*tuple
,
507 const struct nf_conn
*ignored_conntrack
)
509 struct nf_conntrack_tuple_hash
*h
;
511 read_lock_bh(&nf_conntrack_lock
);
512 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
513 read_unlock_bh(&nf_conntrack_lock
);
518 /* There's a small race here where we may free a just-assured
519 connection. Too bad: we're in trouble anyway. */
520 static int early_drop(struct list_head
*chain
)
522 /* Traverse backwards: gives us oldest, which is roughly LRU */
523 struct nf_conntrack_tuple_hash
*h
;
524 struct nf_conn
*ct
= NULL
, *tmp
;
527 read_lock_bh(&nf_conntrack_lock
);
528 list_for_each_entry_reverse(h
, chain
, list
) {
529 tmp
= nf_ct_tuplehash_to_ctrack(h
);
530 if (!test_bit(IPS_ASSURED_BIT
, &tmp
->status
)) {
532 atomic_inc(&ct
->ct_general
.use
);
536 read_unlock_bh(&nf_conntrack_lock
);
541 if (del_timer(&ct
->timeout
)) {
542 death_by_timeout((unsigned long)ct
);
544 NF_CT_STAT_INC(early_drop
);
550 static struct nf_conn
*
551 __nf_conntrack_alloc(const struct nf_conntrack_tuple
*orig
,
552 const struct nf_conntrack_tuple
*repl
,
553 const struct nf_conntrack_l3proto
*l3proto
)
555 struct nf_conn
*conntrack
= NULL
;
556 u_int32_t features
= 0;
557 struct nf_conntrack_helper
*helper
;
559 if (unlikely(!nf_conntrack_hash_rnd_initted
)) {
560 get_random_bytes(&nf_conntrack_hash_rnd
, 4);
561 nf_conntrack_hash_rnd_initted
= 1;
564 /* We don't want any race condition at early drop stage */
565 atomic_inc(&nf_conntrack_count
);
568 && atomic_read(&nf_conntrack_count
) > nf_conntrack_max
) {
569 unsigned int hash
= hash_conntrack(orig
);
570 /* Try dropping from this hash chain. */
571 if (!early_drop(&nf_conntrack_hash
[hash
])) {
572 atomic_dec(&nf_conntrack_count
);
575 "nf_conntrack: table full, dropping"
577 return ERR_PTR(-ENOMEM
);
581 /* find features needed by this conntrack. */
582 features
= l3proto
->get_features(orig
);
584 /* FIXME: protect helper list per RCU */
585 read_lock_bh(&nf_conntrack_lock
);
586 helper
= __nf_ct_helper_find(repl
);
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
);
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
;
658 if (!nf_ct_invert_tuple(&repl_tuple
, tuple
, l3proto
, l4proto
)) {
659 DEBUGP("Can't invert tuple.\n");
663 conntrack
= __nf_conntrack_alloc(tuple
, &repl_tuple
, l3proto
);
664 if (conntrack
== NULL
|| IS_ERR(conntrack
)) {
665 DEBUGP("Can't allocate conntrack.\n");
666 return (struct nf_conntrack_tuple_hash
*)conntrack
;
669 if (!l4proto
->new(conntrack
, skb
, dataoff
)) {
670 nf_conntrack_free(conntrack
);
671 DEBUGP("init conntrack: can't track with proto module\n");
675 write_lock_bh(&nf_conntrack_lock
);
676 exp
= find_expectation(tuple
);
679 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
681 /* Welcome, Mr. Bond. We've been expecting you... */
682 __set_bit(IPS_EXPECTED_BIT
, &conntrack
->status
);
683 conntrack
->master
= exp
->master
;
684 #ifdef CONFIG_NF_CONNTRACK_MARK
685 conntrack
->mark
= exp
->master
->mark
;
687 #ifdef CONFIG_NF_CONNTRACK_SECMARK
688 conntrack
->secmark
= exp
->master
->secmark
;
690 nf_conntrack_get(&conntrack
->master
->ct_general
);
691 NF_CT_STAT_INC(expect_new
);
693 struct nf_conn_help
*help
= nfct_help(conntrack
);
696 help
->helper
= __nf_ct_helper_find(&repl_tuple
);
700 /* Overload tuple linked list to put us in unconfirmed list. */
701 list_add(&conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
, &unconfirmed
);
703 write_unlock_bh(&nf_conntrack_lock
);
707 exp
->expectfn(conntrack
, exp
);
708 nf_conntrack_expect_put(exp
);
711 return &conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
];
714 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
715 static inline struct nf_conn
*
716 resolve_normal_ct(struct sk_buff
*skb
,
717 unsigned int dataoff
,
720 struct nf_conntrack_l3proto
*l3proto
,
721 struct nf_conntrack_l4proto
*l4proto
,
723 enum ip_conntrack_info
*ctinfo
)
725 struct nf_conntrack_tuple tuple
;
726 struct nf_conntrack_tuple_hash
*h
;
729 if (!nf_ct_get_tuple(skb
, (unsigned int)(skb
->nh
.raw
- skb
->data
),
730 dataoff
, l3num
, protonum
, &tuple
, l3proto
,
732 DEBUGP("resolve_normal_ct: Can't get tuple\n");
736 /* look for tuple match */
737 h
= nf_conntrack_find_get(&tuple
, NULL
);
739 h
= init_conntrack(&tuple
, l3proto
, l4proto
, skb
, dataoff
);
745 ct
= nf_ct_tuplehash_to_ctrack(h
);
747 /* It exists; we have (non-exclusive) reference. */
748 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
) {
749 *ctinfo
= IP_CT_ESTABLISHED
+ IP_CT_IS_REPLY
;
750 /* Please set reply bit if this packet OK */
753 /* Once we've had two way comms, always ESTABLISHED. */
754 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
755 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct
);
756 *ctinfo
= IP_CT_ESTABLISHED
;
757 } else if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
)) {
758 DEBUGP("nf_conntrack_in: related packet for %p\n", ct
);
759 *ctinfo
= IP_CT_RELATED
;
761 DEBUGP("nf_conntrack_in: new packet for %p\n", ct
);
766 skb
->nfct
= &ct
->ct_general
;
767 skb
->nfctinfo
= *ctinfo
;
772 nf_conntrack_in(int pf
, unsigned int hooknum
, struct sk_buff
**pskb
)
775 enum ip_conntrack_info ctinfo
;
776 struct nf_conntrack_l3proto
*l3proto
;
777 struct nf_conntrack_l4proto
*l4proto
;
778 unsigned int dataoff
;
783 /* Previously seen (loopback or untracked)? Ignore. */
785 NF_CT_STAT_INC(ignore
);
789 l3proto
= __nf_ct_l3proto_find((u_int16_t
)pf
);
790 if ((ret
= l3proto
->prepare(pskb
, hooknum
, &dataoff
, &protonum
)) <= 0) {
791 DEBUGP("not prepared to track yet or error occured\n");
795 l4proto
= __nf_ct_l4proto_find((u_int16_t
)pf
, protonum
);
797 /* It may be an special packet, error, unclean...
798 * inverse of the return code tells to the netfilter
799 * core what to do with the packet. */
800 if (l4proto
->error
!= NULL
&&
801 (ret
= l4proto
->error(*pskb
, dataoff
, &ctinfo
, pf
, hooknum
)) <= 0) {
802 NF_CT_STAT_INC(error
);
803 NF_CT_STAT_INC(invalid
);
807 ct
= resolve_normal_ct(*pskb
, dataoff
, pf
, protonum
, l3proto
, l4proto
,
808 &set_reply
, &ctinfo
);
810 /* Not valid part of a connection */
811 NF_CT_STAT_INC(invalid
);
816 /* Too stressed to deal. */
817 NF_CT_STAT_INC(drop
);
821 NF_CT_ASSERT((*pskb
)->nfct
);
823 ret
= l4proto
->packet(ct
, *pskb
, dataoff
, ctinfo
, pf
, hooknum
);
825 /* Invalid: inverse of the return code tells
826 * the netfilter core what to do */
827 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
828 nf_conntrack_put((*pskb
)->nfct
);
829 (*pskb
)->nfct
= NULL
;
830 NF_CT_STAT_INC(invalid
);
834 if (set_reply
&& !test_and_set_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
835 nf_conntrack_event_cache(IPCT_STATUS
, *pskb
);
840 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple
*inverse
,
841 const struct nf_conntrack_tuple
*orig
)
843 return nf_ct_invert_tuple(inverse
, orig
,
844 __nf_ct_l3proto_find(orig
->src
.l3num
),
845 __nf_ct_l4proto_find(orig
->src
.l3num
,
846 orig
->dst
.protonum
));
849 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
850 void __nf_ct_refresh_acct(struct nf_conn
*ct
,
851 enum ip_conntrack_info ctinfo
,
852 const struct sk_buff
*skb
,
853 unsigned long extra_jiffies
,
858 NF_CT_ASSERT(ct
->timeout
.data
== (unsigned long)ct
);
861 write_lock_bh(&nf_conntrack_lock
);
863 /* Only update if this is not a fixed timeout */
864 if (test_bit(IPS_FIXED_TIMEOUT_BIT
, &ct
->status
)) {
865 write_unlock_bh(&nf_conntrack_lock
);
869 /* If not in hash table, timer will not be active yet */
870 if (!nf_ct_is_confirmed(ct
)) {
871 ct
->timeout
.expires
= extra_jiffies
;
872 event
= IPCT_REFRESH
;
874 /* Need del_timer for race avoidance (may already be dying). */
875 if (del_timer(&ct
->timeout
)) {
876 ct
->timeout
.expires
= jiffies
+ extra_jiffies
;
877 add_timer(&ct
->timeout
);
878 event
= IPCT_REFRESH
;
882 #ifdef CONFIG_NF_CT_ACCT
884 ct
->counters
[CTINFO2DIR(ctinfo
)].packets
++;
885 ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
+=
886 skb
->len
- (unsigned int)(skb
->nh
.raw
- skb
->data
);
887 if ((ct
->counters
[CTINFO2DIR(ctinfo
)].packets
& 0x80000000)
888 || (ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
& 0x80000000))
889 event
|= IPCT_COUNTER_FILLING
;
893 write_unlock_bh(&nf_conntrack_lock
);
895 /* must be unlocked when calling event cache */
897 nf_conntrack_event_cache(event
, skb
);
900 #if defined(CONFIG_NF_CT_NETLINK) || \
901 defined(CONFIG_NF_CT_NETLINK_MODULE)
903 #include <linux/netfilter/nfnetlink.h>
904 #include <linux/netfilter/nfnetlink_conntrack.h>
905 #include <linux/mutex.h>
908 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
909 * in ip_conntrack_core, since we don't want the protocols to autoload
910 * or depend on ctnetlink */
911 int nf_ct_port_tuple_to_nfattr(struct sk_buff
*skb
,
912 const struct nf_conntrack_tuple
*tuple
)
914 NFA_PUT(skb
, CTA_PROTO_SRC_PORT
, sizeof(u_int16_t
),
915 &tuple
->src
.u
.tcp
.port
);
916 NFA_PUT(skb
, CTA_PROTO_DST_PORT
, sizeof(u_int16_t
),
917 &tuple
->dst
.u
.tcp
.port
);
924 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
925 [CTA_PROTO_SRC_PORT
-1] = sizeof(u_int16_t
),
926 [CTA_PROTO_DST_PORT
-1] = sizeof(u_int16_t
)
929 int nf_ct_port_nfattr_to_tuple(struct nfattr
*tb
[],
930 struct nf_conntrack_tuple
*t
)
932 if (!tb
[CTA_PROTO_SRC_PORT
-1] || !tb
[CTA_PROTO_DST_PORT
-1])
935 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
939 *(u_int16_t
*)NFA_DATA(tb
[CTA_PROTO_SRC_PORT
-1]);
941 *(u_int16_t
*)NFA_DATA(tb
[CTA_PROTO_DST_PORT
-1]);
947 /* Used by ipt_REJECT and ip6t_REJECT. */
948 void __nf_conntrack_attach(struct sk_buff
*nskb
, struct sk_buff
*skb
)
951 enum ip_conntrack_info ctinfo
;
953 /* This ICMP is in reverse direction to the packet which caused it */
954 ct
= nf_ct_get(skb
, &ctinfo
);
955 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
)
956 ctinfo
= IP_CT_RELATED
+ IP_CT_IS_REPLY
;
958 ctinfo
= IP_CT_RELATED
;
960 /* Attach to new skbuff, and increment count */
961 nskb
->nfct
= &ct
->ct_general
;
962 nskb
->nfctinfo
= ctinfo
;
963 nf_conntrack_get(nskb
->nfct
);
967 do_iter(const struct nf_conntrack_tuple_hash
*i
,
968 int (*iter
)(struct nf_conn
*i
, void *data
),
971 return iter(nf_ct_tuplehash_to_ctrack(i
), data
);
974 /* Bring out ya dead! */
975 static struct nf_conn
*
976 get_next_corpse(int (*iter
)(struct nf_conn
*i
, void *data
),
977 void *data
, unsigned int *bucket
)
979 struct nf_conntrack_tuple_hash
*h
;
982 write_lock_bh(&nf_conntrack_lock
);
983 for (; *bucket
< nf_conntrack_htable_size
; (*bucket
)++) {
984 list_for_each_entry(h
, &nf_conntrack_hash
[*bucket
], list
) {
985 ct
= nf_ct_tuplehash_to_ctrack(h
);
990 list_for_each_entry(h
, &unconfirmed
, list
) {
991 ct
= nf_ct_tuplehash_to_ctrack(h
);
995 write_unlock_bh(&nf_conntrack_lock
);
998 atomic_inc(&ct
->ct_general
.use
);
999 write_unlock_bh(&nf_conntrack_lock
);
1004 nf_ct_iterate_cleanup(int (*iter
)(struct nf_conn
*i
, void *data
), void *data
)
1007 unsigned int bucket
= 0;
1009 while ((ct
= get_next_corpse(iter
, data
, &bucket
)) != NULL
) {
1010 /* Time to push up daises... */
1011 if (del_timer(&ct
->timeout
))
1012 death_by_timeout((unsigned long)ct
);
1013 /* ... else the timer will get him soon. */
1019 static int kill_all(struct nf_conn
*i
, void *data
)
1024 static void free_conntrack_hash(struct list_head
*hash
, int vmalloced
, int size
)
1029 free_pages((unsigned long)hash
,
1030 get_order(sizeof(struct list_head
) * size
));
1033 void nf_conntrack_flush()
1035 nf_ct_iterate_cleanup(kill_all
, NULL
);
1038 /* Mishearing the voices in his head, our hero wonders how he's
1039 supposed to kill the mall. */
1040 void nf_conntrack_cleanup(void)
1044 ip_ct_attach
= NULL
;
1046 /* This makes sure all current packets have passed through
1047 netfilter framework. Roll on, two-stage module
1051 nf_ct_event_cache_flush();
1053 nf_conntrack_flush();
1054 if (atomic_read(&nf_conntrack_count
) != 0) {
1056 goto i_see_dead_people
;
1058 /* wait until all references to nf_conntrack_untracked are dropped */
1059 while (atomic_read(&nf_conntrack_untracked
.ct_general
.use
) > 1)
1062 for (i
= 0; i
< NF_CT_F_NUM
; i
++) {
1063 if (nf_ct_cache
[i
].use
== 0)
1066 NF_CT_ASSERT(nf_ct_cache
[i
].use
== 1);
1067 nf_ct_cache
[i
].use
= 1;
1068 nf_conntrack_unregister_cache(i
);
1070 kmem_cache_destroy(nf_conntrack_expect_cachep
);
1071 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1072 nf_conntrack_htable_size
);
1074 /* free l3proto protocol tables */
1075 for (i
= 0; i
< PF_MAX
; i
++)
1076 if (nf_ct_protos
[i
]) {
1077 kfree(nf_ct_protos
[i
]);
1078 nf_ct_protos
[i
] = NULL
;
1082 static struct list_head
*alloc_hashtable(int size
, int *vmalloced
)
1084 struct list_head
*hash
;
1088 hash
= (void*)__get_free_pages(GFP_KERNEL
,
1089 get_order(sizeof(struct list_head
)
1093 printk(KERN_WARNING
"nf_conntrack: falling back to vmalloc.\n");
1094 hash
= vmalloc(sizeof(struct list_head
) * size
);
1098 for (i
= 0; i
< size
; i
++)
1099 INIT_LIST_HEAD(&hash
[i
]);
1104 int set_hashsize(const char *val
, struct kernel_param
*kp
)
1106 int i
, bucket
, hashsize
, vmalloced
;
1107 int old_vmalloced
, old_size
;
1109 struct list_head
*hash
, *old_hash
;
1110 struct nf_conntrack_tuple_hash
*h
;
1112 /* On boot, we can set this without any fancy locking. */
1113 if (!nf_conntrack_htable_size
)
1114 return param_set_uint(val
, kp
);
1116 hashsize
= simple_strtol(val
, NULL
, 0);
1120 hash
= alloc_hashtable(hashsize
, &vmalloced
);
1124 /* We have to rehahs for the new table anyway, so we also can
1125 * use a newrandom seed */
1126 get_random_bytes(&rnd
, 4);
1128 write_lock_bh(&nf_conntrack_lock
);
1129 for (i
= 0; i
< nf_conntrack_htable_size
; i
++) {
1130 while (!list_empty(&nf_conntrack_hash
[i
])) {
1131 h
= list_entry(nf_conntrack_hash
[i
].next
,
1132 struct nf_conntrack_tuple_hash
, list
);
1134 bucket
= __hash_conntrack(&h
->tuple
, hashsize
, rnd
);
1135 list_add_tail(&h
->list
, &hash
[bucket
]);
1138 old_size
= nf_conntrack_htable_size
;
1139 old_vmalloced
= nf_conntrack_vmalloc
;
1140 old_hash
= nf_conntrack_hash
;
1142 nf_conntrack_htable_size
= hashsize
;
1143 nf_conntrack_vmalloc
= vmalloced
;
1144 nf_conntrack_hash
= hash
;
1145 nf_conntrack_hash_rnd
= rnd
;
1146 write_unlock_bh(&nf_conntrack_lock
);
1148 free_conntrack_hash(old_hash
, old_vmalloced
, old_size
);
1152 module_param_call(hashsize
, set_hashsize
, param_get_uint
,
1153 &nf_conntrack_htable_size
, 0600);
1155 int __init
nf_conntrack_init(void)
1160 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1161 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1162 if (!nf_conntrack_htable_size
) {
1163 nf_conntrack_htable_size
1164 = (((num_physpages
<< PAGE_SHIFT
) / 16384)
1165 / sizeof(struct list_head
));
1166 if (num_physpages
> (1024 * 1024 * 1024 / PAGE_SIZE
))
1167 nf_conntrack_htable_size
= 8192;
1168 if (nf_conntrack_htable_size
< 16)
1169 nf_conntrack_htable_size
= 16;
1171 nf_conntrack_max
= 8 * nf_conntrack_htable_size
;
1173 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1174 NF_CONNTRACK_VERSION
, nf_conntrack_htable_size
,
1177 nf_conntrack_hash
= alloc_hashtable(nf_conntrack_htable_size
,
1178 &nf_conntrack_vmalloc
);
1179 if (!nf_conntrack_hash
) {
1180 printk(KERN_ERR
"Unable to create nf_conntrack_hash\n");
1184 ret
= nf_conntrack_register_cache(NF_CT_F_BASIC
, "nf_conntrack:basic",
1185 sizeof(struct nf_conn
));
1187 printk(KERN_ERR
"Unable to create nf_conn slab cache\n");
1191 nf_conntrack_expect_cachep
= kmem_cache_create("nf_conntrack_expect",
1192 sizeof(struct nf_conntrack_expect
),
1194 if (!nf_conntrack_expect_cachep
) {
1195 printk(KERN_ERR
"Unable to create nf_expect slab cache\n");
1196 goto err_free_conntrack_slab
;
1199 /* Don't NEED lock here, but good form anyway. */
1200 write_lock_bh(&nf_conntrack_lock
);
1201 for (i
= 0; i
< AF_MAX
; i
++)
1202 nf_ct_l3protos
[i
] = &nf_conntrack_l3proto_generic
;
1203 write_unlock_bh(&nf_conntrack_lock
);
1205 /* For use by REJECT target */
1206 ip_ct_attach
= __nf_conntrack_attach
;
1208 /* Set up fake conntrack:
1209 - to never be deleted, not in any hashes */
1210 atomic_set(&nf_conntrack_untracked
.ct_general
.use
, 1);
1211 /* - and look it like as a confirmed connection */
1212 set_bit(IPS_CONFIRMED_BIT
, &nf_conntrack_untracked
.status
);
1216 err_free_conntrack_slab
:
1217 nf_conntrack_unregister_cache(NF_CT_F_BASIC
);
1219 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1220 nf_conntrack_htable_size
);