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);
70 void (*nf_conntrack_destroyed
)(struct nf_conn
*conntrack
) = NULL
;
71 unsigned int nf_conntrack_htable_size __read_mostly
;
72 int nf_conntrack_max __read_mostly
;
73 struct list_head
*nf_conntrack_hash __read_mostly
;
74 struct nf_conn nf_conntrack_untracked __read_mostly
;
75 unsigned int nf_ct_log_invalid __read_mostly
;
76 LIST_HEAD(unconfirmed
);
77 static int nf_conntrack_vmalloc __read_mostly
;
79 static unsigned int nf_conntrack_next_id
;
81 DEFINE_PER_CPU(struct ip_conntrack_stat
, nf_conntrack_stat
);
82 EXPORT_PER_CPU_SYMBOL(nf_conntrack_stat
);
85 * This scheme offers various size of "struct nf_conn" dependent on
86 * features(helper, nat, ...)
89 #define NF_CT_FEATURES_NAMELEN 256
91 /* name of slab cache. printed in /proc/slabinfo */
94 /* size of slab cache */
97 /* slab cache pointer */
100 /* allocated slab cache + modules which uses this slab cache */
103 } nf_ct_cache
[NF_CT_F_NUM
];
105 /* protect members of nf_ct_cache except of "use" */
106 DEFINE_RWLOCK(nf_ct_cache_lock
);
108 /* This avoids calling kmem_cache_create() with same name simultaneously */
109 static DEFINE_MUTEX(nf_ct_cache_mutex
);
111 static int nf_conntrack_hash_rnd_initted
;
112 static unsigned int nf_conntrack_hash_rnd
;
114 static u_int32_t
__hash_conntrack(const struct nf_conntrack_tuple
*tuple
,
115 unsigned int size
, unsigned int rnd
)
118 a
= jhash((void *)tuple
->src
.u3
.all
, sizeof(tuple
->src
.u3
.all
),
119 ((tuple
->src
.l3num
) << 16) | tuple
->dst
.protonum
);
120 b
= jhash((void *)tuple
->dst
.u3
.all
, sizeof(tuple
->dst
.u3
.all
),
121 (tuple
->src
.u
.all
<< 16) | tuple
->dst
.u
.all
);
123 return jhash_2words(a
, b
, rnd
) % size
;
126 static inline u_int32_t
hash_conntrack(const struct nf_conntrack_tuple
*tuple
)
128 return __hash_conntrack(tuple
, nf_conntrack_htable_size
,
129 nf_conntrack_hash_rnd
);
132 int nf_conntrack_register_cache(u_int32_t features
, const char *name
,
137 kmem_cache_t
*cachep
;
139 DEBUGP("nf_conntrack_register_cache: features=0x%x, name=%s, size=%d\n",
140 features
, name
, size
);
142 if (features
< NF_CT_F_BASIC
|| features
>= NF_CT_F_NUM
) {
143 DEBUGP("nf_conntrack_register_cache: invalid features.: 0x%x\n",
148 mutex_lock(&nf_ct_cache_mutex
);
150 write_lock_bh(&nf_ct_cache_lock
);
151 /* e.g: multiple helpers are loaded */
152 if (nf_ct_cache
[features
].use
> 0) {
153 DEBUGP("nf_conntrack_register_cache: already resisterd.\n");
154 if ((!strncmp(nf_ct_cache
[features
].name
, name
,
155 NF_CT_FEATURES_NAMELEN
))
156 && nf_ct_cache
[features
].size
== size
) {
157 DEBUGP("nf_conntrack_register_cache: reusing.\n");
158 nf_ct_cache
[features
].use
++;
163 write_unlock_bh(&nf_ct_cache_lock
);
164 mutex_unlock(&nf_ct_cache_mutex
);
167 write_unlock_bh(&nf_ct_cache_lock
);
170 * The memory space for name of slab cache must be alive until
171 * cache is destroyed.
173 cache_name
= kmalloc(sizeof(char)*NF_CT_FEATURES_NAMELEN
, GFP_ATOMIC
);
174 if (cache_name
== NULL
) {
175 DEBUGP("nf_conntrack_register_cache: can't alloc cache_name\n");
180 if (strlcpy(cache_name
, name
, NF_CT_FEATURES_NAMELEN
)
181 >= NF_CT_FEATURES_NAMELEN
) {
182 printk("nf_conntrack_register_cache: name too long\n");
187 cachep
= kmem_cache_create(cache_name
, size
, 0, 0,
190 printk("nf_conntrack_register_cache: Can't create slab cache "
191 "for the features = 0x%x\n", features
);
196 write_lock_bh(&nf_ct_cache_lock
);
197 nf_ct_cache
[features
].use
= 1;
198 nf_ct_cache
[features
].size
= size
;
199 nf_ct_cache
[features
].cachep
= cachep
;
200 nf_ct_cache
[features
].name
= cache_name
;
201 write_unlock_bh(&nf_ct_cache_lock
);
208 mutex_unlock(&nf_ct_cache_mutex
);
212 /* FIXME: In the current, only nf_conntrack_cleanup() can call this function. */
213 void nf_conntrack_unregister_cache(u_int32_t features
)
215 kmem_cache_t
*cachep
;
219 * This assures that kmem_cache_create() isn't called before destroying
222 DEBUGP("nf_conntrack_unregister_cache: 0x%04x\n", features
);
223 mutex_lock(&nf_ct_cache_mutex
);
225 write_lock_bh(&nf_ct_cache_lock
);
226 if (--nf_ct_cache
[features
].use
> 0) {
227 write_unlock_bh(&nf_ct_cache_lock
);
228 mutex_unlock(&nf_ct_cache_mutex
);
231 cachep
= nf_ct_cache
[features
].cachep
;
232 name
= nf_ct_cache
[features
].name
;
233 nf_ct_cache
[features
].cachep
= NULL
;
234 nf_ct_cache
[features
].name
= NULL
;
235 nf_ct_cache
[features
].size
= 0;
236 write_unlock_bh(&nf_ct_cache_lock
);
240 kmem_cache_destroy(cachep
);
243 mutex_unlock(&nf_ct_cache_mutex
);
247 nf_ct_get_tuple(const struct sk_buff
*skb
,
249 unsigned int dataoff
,
252 struct nf_conntrack_tuple
*tuple
,
253 const struct nf_conntrack_l3proto
*l3proto
,
254 const struct nf_conntrack_l4proto
*l4proto
)
256 NF_CT_TUPLE_U_BLANK(tuple
);
258 tuple
->src
.l3num
= l3num
;
259 if (l3proto
->pkt_to_tuple(skb
, nhoff
, tuple
) == 0)
262 tuple
->dst
.protonum
= protonum
;
263 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
265 return l4proto
->pkt_to_tuple(skb
, dataoff
, tuple
);
269 nf_ct_invert_tuple(struct nf_conntrack_tuple
*inverse
,
270 const struct nf_conntrack_tuple
*orig
,
271 const struct nf_conntrack_l3proto
*l3proto
,
272 const struct nf_conntrack_l4proto
*l4proto
)
274 NF_CT_TUPLE_U_BLANK(inverse
);
276 inverse
->src
.l3num
= orig
->src
.l3num
;
277 if (l3proto
->invert_tuple(inverse
, orig
) == 0)
280 inverse
->dst
.dir
= !orig
->dst
.dir
;
282 inverse
->dst
.protonum
= orig
->dst
.protonum
;
283 return l4proto
->invert_tuple(inverse
, orig
);
287 clean_from_lists(struct nf_conn
*ct
)
289 DEBUGP("clean_from_lists(%p)\n", ct
);
290 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
291 list_del(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
);
293 /* Destroy all pending expectations */
294 nf_ct_remove_expectations(ct
);
298 destroy_conntrack(struct nf_conntrack
*nfct
)
300 struct nf_conn
*ct
= (struct nf_conn
*)nfct
;
301 struct nf_conntrack_l3proto
*l3proto
;
302 struct nf_conntrack_l4proto
*l4proto
;
304 DEBUGP("destroy_conntrack(%p)\n", ct
);
305 NF_CT_ASSERT(atomic_read(&nfct
->use
) == 0);
306 NF_CT_ASSERT(!timer_pending(&ct
->timeout
));
308 nf_conntrack_event(IPCT_DESTROY
, ct
);
309 set_bit(IPS_DYING_BIT
, &ct
->status
);
311 /* To make sure we don't get any weird locking issues here:
312 * destroy_conntrack() MUST NOT be called with a write lock
313 * to nf_conntrack_lock!!! -HW */
314 l3proto
= __nf_ct_l3proto_find(ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.l3num
);
315 if (l3proto
&& l3proto
->destroy
)
316 l3proto
->destroy(ct
);
318 l4proto
= __nf_ct_l4proto_find(ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.l3num
, ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.protonum
);
319 if (l4proto
&& l4proto
->destroy
)
320 l4proto
->destroy(ct
);
322 if (nf_conntrack_destroyed
)
323 nf_conntrack_destroyed(ct
);
325 write_lock_bh(&nf_conntrack_lock
);
326 /* Expectations will have been removed in clean_from_lists,
327 * except TFTP can create an expectation on the first packet,
328 * before connection is in the list, so we need to clean here,
330 nf_ct_remove_expectations(ct
);
332 /* We overload first tuple to link into unconfirmed list. */
333 if (!nf_ct_is_confirmed(ct
)) {
334 BUG_ON(list_empty(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
));
335 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
338 NF_CT_STAT_INC(delete);
339 write_unlock_bh(&nf_conntrack_lock
);
342 nf_ct_put(ct
->master
);
344 DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct
);
345 nf_conntrack_free(ct
);
348 static void death_by_timeout(unsigned long ul_conntrack
)
350 struct nf_conn
*ct
= (void *)ul_conntrack
;
352 write_lock_bh(&nf_conntrack_lock
);
353 /* Inside lock so preempt is disabled on module removal path.
354 * Otherwise we can get spurious warnings. */
355 NF_CT_STAT_INC(delete_list
);
356 clean_from_lists(ct
);
357 write_unlock_bh(&nf_conntrack_lock
);
361 struct nf_conntrack_tuple_hash
*
362 __nf_conntrack_find(const struct nf_conntrack_tuple
*tuple
,
363 const struct nf_conn
*ignored_conntrack
)
365 struct nf_conntrack_tuple_hash
*h
;
366 unsigned int hash
= hash_conntrack(tuple
);
368 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
) {
369 if (nf_ct_tuplehash_to_ctrack(h
) != ignored_conntrack
&&
370 nf_ct_tuple_equal(tuple
, &h
->tuple
)) {
371 NF_CT_STAT_INC(found
);
374 NF_CT_STAT_INC(searched
);
380 /* Find a connection corresponding to a tuple. */
381 struct nf_conntrack_tuple_hash
*
382 nf_conntrack_find_get(const struct nf_conntrack_tuple
*tuple
,
383 const struct nf_conn
*ignored_conntrack
)
385 struct nf_conntrack_tuple_hash
*h
;
387 read_lock_bh(&nf_conntrack_lock
);
388 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
390 atomic_inc(&nf_ct_tuplehash_to_ctrack(h
)->ct_general
.use
);
391 read_unlock_bh(&nf_conntrack_lock
);
396 static void __nf_conntrack_hash_insert(struct nf_conn
*ct
,
398 unsigned int repl_hash
)
400 ct
->id
= ++nf_conntrack_next_id
;
401 list_add(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
,
402 &nf_conntrack_hash
[hash
]);
403 list_add(&ct
->tuplehash
[IP_CT_DIR_REPLY
].list
,
404 &nf_conntrack_hash
[repl_hash
]);
407 void nf_conntrack_hash_insert(struct nf_conn
*ct
)
409 unsigned int hash
, repl_hash
;
411 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
412 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
414 write_lock_bh(&nf_conntrack_lock
);
415 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
416 write_unlock_bh(&nf_conntrack_lock
);
419 /* Confirm a connection given skb; places it in hash table */
421 __nf_conntrack_confirm(struct sk_buff
**pskb
)
423 unsigned int hash
, repl_hash
;
424 struct nf_conntrack_tuple_hash
*h
;
426 struct nf_conn_help
*help
;
427 enum ip_conntrack_info ctinfo
;
429 ct
= nf_ct_get(*pskb
, &ctinfo
);
431 /* ipt_REJECT uses nf_conntrack_attach to attach related
432 ICMP/TCP RST packets in other direction. Actual packet
433 which created connection will be IP_CT_NEW or for an
434 expected connection, IP_CT_RELATED. */
435 if (CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
)
438 hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
439 repl_hash
= hash_conntrack(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
441 /* We're not in hash table, and we refuse to set up related
442 connections for unconfirmed conns. But packet copies and
443 REJECT will give spurious warnings here. */
444 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
446 /* No external references means noone else could have
448 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
449 DEBUGP("Confirming conntrack %p\n", ct
);
451 write_lock_bh(&nf_conntrack_lock
);
453 /* See if there's one in the list already, including reverse:
454 NAT could have grabbed it without realizing, since we're
455 not in the hash. If there is, we lost race. */
456 list_for_each_entry(h
, &nf_conntrack_hash
[hash
], list
)
457 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
460 list_for_each_entry(h
, &nf_conntrack_hash
[repl_hash
], list
)
461 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
465 /* Remove from unconfirmed list */
466 list_del(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
);
468 __nf_conntrack_hash_insert(ct
, hash
, repl_hash
);
469 /* Timer relative to confirmation time, not original
470 setting time, otherwise we'd get timer wrap in
471 weird delay cases. */
472 ct
->timeout
.expires
+= jiffies
;
473 add_timer(&ct
->timeout
);
474 atomic_inc(&ct
->ct_general
.use
);
475 set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
476 NF_CT_STAT_INC(insert
);
477 write_unlock_bh(&nf_conntrack_lock
);
478 help
= nfct_help(ct
);
479 if (help
&& help
->helper
)
480 nf_conntrack_event_cache(IPCT_HELPER
, *pskb
);
481 #ifdef CONFIG_NF_NAT_NEEDED
482 if (test_bit(IPS_SRC_NAT_DONE_BIT
, &ct
->status
) ||
483 test_bit(IPS_DST_NAT_DONE_BIT
, &ct
->status
))
484 nf_conntrack_event_cache(IPCT_NATINFO
, *pskb
);
486 nf_conntrack_event_cache(master_ct(ct
) ?
487 IPCT_RELATED
: IPCT_NEW
, *pskb
);
491 NF_CT_STAT_INC(insert_failed
);
492 write_unlock_bh(&nf_conntrack_lock
);
496 /* Returns true if a connection correspondings to the tuple (required
499 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple
*tuple
,
500 const struct nf_conn
*ignored_conntrack
)
502 struct nf_conntrack_tuple_hash
*h
;
504 read_lock_bh(&nf_conntrack_lock
);
505 h
= __nf_conntrack_find(tuple
, ignored_conntrack
);
506 read_unlock_bh(&nf_conntrack_lock
);
511 /* There's a small race here where we may free a just-assured
512 connection. Too bad: we're in trouble anyway. */
513 static int early_drop(struct list_head
*chain
)
515 /* Traverse backwards: gives us oldest, which is roughly LRU */
516 struct nf_conntrack_tuple_hash
*h
;
517 struct nf_conn
*ct
= NULL
, *tmp
;
520 read_lock_bh(&nf_conntrack_lock
);
521 list_for_each_entry_reverse(h
, chain
, list
) {
522 tmp
= nf_ct_tuplehash_to_ctrack(h
);
523 if (!test_bit(IPS_ASSURED_BIT
, &tmp
->status
)) {
525 atomic_inc(&ct
->ct_general
.use
);
529 read_unlock_bh(&nf_conntrack_lock
);
534 if (del_timer(&ct
->timeout
)) {
535 death_by_timeout((unsigned long)ct
);
537 NF_CT_STAT_INC(early_drop
);
543 static struct nf_conn
*
544 __nf_conntrack_alloc(const struct nf_conntrack_tuple
*orig
,
545 const struct nf_conntrack_tuple
*repl
,
546 const struct nf_conntrack_l3proto
*l3proto
)
548 struct nf_conn
*conntrack
= NULL
;
549 u_int32_t features
= 0;
550 struct nf_conntrack_helper
*helper
;
552 if (unlikely(!nf_conntrack_hash_rnd_initted
)) {
553 get_random_bytes(&nf_conntrack_hash_rnd
, 4);
554 nf_conntrack_hash_rnd_initted
= 1;
557 /* We don't want any race condition at early drop stage */
558 atomic_inc(&nf_conntrack_count
);
561 && atomic_read(&nf_conntrack_count
) > nf_conntrack_max
) {
562 unsigned int hash
= hash_conntrack(orig
);
563 /* Try dropping from this hash chain. */
564 if (!early_drop(&nf_conntrack_hash
[hash
])) {
565 atomic_dec(&nf_conntrack_count
);
568 "nf_conntrack: table full, dropping"
570 return ERR_PTR(-ENOMEM
);
574 /* find features needed by this conntrack. */
575 features
= l3proto
->get_features(orig
);
577 /* FIXME: protect helper list per RCU */
578 read_lock_bh(&nf_conntrack_lock
);
579 helper
= __nf_ct_helper_find(repl
);
581 features
|= NF_CT_F_HELP
;
582 read_unlock_bh(&nf_conntrack_lock
);
584 DEBUGP("nf_conntrack_alloc: features=0x%x\n", features
);
586 read_lock_bh(&nf_ct_cache_lock
);
588 if (unlikely(!nf_ct_cache
[features
].use
)) {
589 DEBUGP("nf_conntrack_alloc: not supported features = 0x%x\n",
594 conntrack
= kmem_cache_alloc(nf_ct_cache
[features
].cachep
, GFP_ATOMIC
);
595 if (conntrack
== NULL
) {
596 DEBUGP("nf_conntrack_alloc: Can't alloc conntrack from cache\n");
600 memset(conntrack
, 0, nf_ct_cache
[features
].size
);
601 conntrack
->features
= features
;
602 atomic_set(&conntrack
->ct_general
.use
, 1);
603 conntrack
->ct_general
.destroy
= destroy_conntrack
;
604 conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
= *orig
;
605 conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
= *repl
;
606 /* Don't set timer yet: wait for confirmation */
607 init_timer(&conntrack
->timeout
);
608 conntrack
->timeout
.data
= (unsigned long)conntrack
;
609 conntrack
->timeout
.function
= death_by_timeout
;
610 read_unlock_bh(&nf_ct_cache_lock
);
614 read_unlock_bh(&nf_ct_cache_lock
);
615 atomic_dec(&nf_conntrack_count
);
619 struct nf_conn
*nf_conntrack_alloc(const struct nf_conntrack_tuple
*orig
,
620 const struct nf_conntrack_tuple
*repl
)
622 struct nf_conntrack_l3proto
*l3proto
;
624 l3proto
= __nf_ct_l3proto_find(orig
->src
.l3num
);
625 return __nf_conntrack_alloc(orig
, repl
, l3proto
);
628 void nf_conntrack_free(struct nf_conn
*conntrack
)
630 u_int32_t features
= conntrack
->features
;
631 NF_CT_ASSERT(features
>= NF_CT_F_BASIC
&& features
< NF_CT_F_NUM
);
632 DEBUGP("nf_conntrack_free: features = 0x%x, conntrack=%p\n", features
,
634 kmem_cache_free(nf_ct_cache
[features
].cachep
, conntrack
);
635 atomic_dec(&nf_conntrack_count
);
638 /* Allocate a new conntrack: we return -ENOMEM if classification
639 failed due to stress. Otherwise it really is unclassifiable. */
640 static struct nf_conntrack_tuple_hash
*
641 init_conntrack(const struct nf_conntrack_tuple
*tuple
,
642 struct nf_conntrack_l3proto
*l3proto
,
643 struct nf_conntrack_l4proto
*l4proto
,
645 unsigned int dataoff
)
647 struct nf_conn
*conntrack
;
648 struct nf_conntrack_tuple repl_tuple
;
649 struct nf_conntrack_expect
*exp
;
651 if (!nf_ct_invert_tuple(&repl_tuple
, tuple
, l3proto
, l4proto
)) {
652 DEBUGP("Can't invert tuple.\n");
656 conntrack
= __nf_conntrack_alloc(tuple
, &repl_tuple
, l3proto
);
657 if (conntrack
== NULL
|| IS_ERR(conntrack
)) {
658 DEBUGP("Can't allocate conntrack.\n");
659 return (struct nf_conntrack_tuple_hash
*)conntrack
;
662 if (!l4proto
->new(conntrack
, skb
, dataoff
)) {
663 nf_conntrack_free(conntrack
);
664 DEBUGP("init conntrack: can't track with proto module\n");
668 write_lock_bh(&nf_conntrack_lock
);
669 exp
= find_expectation(tuple
);
672 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
674 /* Welcome, Mr. Bond. We've been expecting you... */
675 __set_bit(IPS_EXPECTED_BIT
, &conntrack
->status
);
676 conntrack
->master
= exp
->master
;
677 #ifdef CONFIG_NF_CONNTRACK_MARK
678 conntrack
->mark
= exp
->master
->mark
;
680 #ifdef CONFIG_NF_CONNTRACK_SECMARK
681 conntrack
->secmark
= exp
->master
->secmark
;
683 nf_conntrack_get(&conntrack
->master
->ct_general
);
684 NF_CT_STAT_INC(expect_new
);
686 struct nf_conn_help
*help
= nfct_help(conntrack
);
689 help
->helper
= __nf_ct_helper_find(&repl_tuple
);
693 /* Overload tuple linked list to put us in unconfirmed list. */
694 list_add(&conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
].list
, &unconfirmed
);
696 write_unlock_bh(&nf_conntrack_lock
);
700 exp
->expectfn(conntrack
, exp
);
701 nf_conntrack_expect_put(exp
);
704 return &conntrack
->tuplehash
[IP_CT_DIR_ORIGINAL
];
707 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
708 static inline struct nf_conn
*
709 resolve_normal_ct(struct sk_buff
*skb
,
710 unsigned int dataoff
,
713 struct nf_conntrack_l3proto
*l3proto
,
714 struct nf_conntrack_l4proto
*l4proto
,
716 enum ip_conntrack_info
*ctinfo
)
718 struct nf_conntrack_tuple tuple
;
719 struct nf_conntrack_tuple_hash
*h
;
722 if (!nf_ct_get_tuple(skb
, (unsigned int)(skb
->nh
.raw
- skb
->data
),
723 dataoff
, l3num
, protonum
, &tuple
, l3proto
,
725 DEBUGP("resolve_normal_ct: Can't get tuple\n");
729 /* look for tuple match */
730 h
= nf_conntrack_find_get(&tuple
, NULL
);
732 h
= init_conntrack(&tuple
, l3proto
, l4proto
, skb
, dataoff
);
738 ct
= nf_ct_tuplehash_to_ctrack(h
);
740 /* It exists; we have (non-exclusive) reference. */
741 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
) {
742 *ctinfo
= IP_CT_ESTABLISHED
+ IP_CT_IS_REPLY
;
743 /* Please set reply bit if this packet OK */
746 /* Once we've had two way comms, always ESTABLISHED. */
747 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
748 DEBUGP("nf_conntrack_in: normal packet for %p\n", ct
);
749 *ctinfo
= IP_CT_ESTABLISHED
;
750 } else if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
)) {
751 DEBUGP("nf_conntrack_in: related packet for %p\n", ct
);
752 *ctinfo
= IP_CT_RELATED
;
754 DEBUGP("nf_conntrack_in: new packet for %p\n", ct
);
759 skb
->nfct
= &ct
->ct_general
;
760 skb
->nfctinfo
= *ctinfo
;
765 nf_conntrack_in(int pf
, unsigned int hooknum
, struct sk_buff
**pskb
)
768 enum ip_conntrack_info ctinfo
;
769 struct nf_conntrack_l3proto
*l3proto
;
770 struct nf_conntrack_l4proto
*l4proto
;
771 unsigned int dataoff
;
776 /* Previously seen (loopback or untracked)? Ignore. */
778 NF_CT_STAT_INC(ignore
);
782 l3proto
= __nf_ct_l3proto_find((u_int16_t
)pf
);
783 if ((ret
= l3proto
->prepare(pskb
, hooknum
, &dataoff
, &protonum
)) <= 0) {
784 DEBUGP("not prepared to track yet or error occured\n");
788 l4proto
= __nf_ct_l4proto_find((u_int16_t
)pf
, protonum
);
790 /* It may be an special packet, error, unclean...
791 * inverse of the return code tells to the netfilter
792 * core what to do with the packet. */
793 if (l4proto
->error
!= NULL
&&
794 (ret
= l4proto
->error(*pskb
, dataoff
, &ctinfo
, pf
, hooknum
)) <= 0) {
795 NF_CT_STAT_INC(error
);
796 NF_CT_STAT_INC(invalid
);
800 ct
= resolve_normal_ct(*pskb
, dataoff
, pf
, protonum
, l3proto
, l4proto
,
801 &set_reply
, &ctinfo
);
803 /* Not valid part of a connection */
804 NF_CT_STAT_INC(invalid
);
809 /* Too stressed to deal. */
810 NF_CT_STAT_INC(drop
);
814 NF_CT_ASSERT((*pskb
)->nfct
);
816 ret
= l4proto
->packet(ct
, *pskb
, dataoff
, ctinfo
, pf
, hooknum
);
818 /* Invalid: inverse of the return code tells
819 * the netfilter core what to do */
820 DEBUGP("nf_conntrack_in: Can't track with proto module\n");
821 nf_conntrack_put((*pskb
)->nfct
);
822 (*pskb
)->nfct
= NULL
;
823 NF_CT_STAT_INC(invalid
);
827 if (set_reply
&& !test_and_set_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
828 nf_conntrack_event_cache(IPCT_STATUS
, *pskb
);
833 int nf_ct_invert_tuplepr(struct nf_conntrack_tuple
*inverse
,
834 const struct nf_conntrack_tuple
*orig
)
836 return nf_ct_invert_tuple(inverse
, orig
,
837 __nf_ct_l3proto_find(orig
->src
.l3num
),
838 __nf_ct_l4proto_find(orig
->src
.l3num
,
839 orig
->dst
.protonum
));
842 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
843 void __nf_ct_refresh_acct(struct nf_conn
*ct
,
844 enum ip_conntrack_info ctinfo
,
845 const struct sk_buff
*skb
,
846 unsigned long extra_jiffies
,
851 NF_CT_ASSERT(ct
->timeout
.data
== (unsigned long)ct
);
854 write_lock_bh(&nf_conntrack_lock
);
856 /* Only update if this is not a fixed timeout */
857 if (test_bit(IPS_FIXED_TIMEOUT_BIT
, &ct
->status
)) {
858 write_unlock_bh(&nf_conntrack_lock
);
862 /* If not in hash table, timer will not be active yet */
863 if (!nf_ct_is_confirmed(ct
)) {
864 ct
->timeout
.expires
= extra_jiffies
;
865 event
= IPCT_REFRESH
;
867 /* Need del_timer for race avoidance (may already be dying). */
868 if (del_timer(&ct
->timeout
)) {
869 ct
->timeout
.expires
= jiffies
+ extra_jiffies
;
870 add_timer(&ct
->timeout
);
871 event
= IPCT_REFRESH
;
875 #ifdef CONFIG_NF_CT_ACCT
877 ct
->counters
[CTINFO2DIR(ctinfo
)].packets
++;
878 ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
+=
879 skb
->len
- (unsigned int)(skb
->nh
.raw
- skb
->data
);
880 if ((ct
->counters
[CTINFO2DIR(ctinfo
)].packets
& 0x80000000)
881 || (ct
->counters
[CTINFO2DIR(ctinfo
)].bytes
& 0x80000000))
882 event
|= IPCT_COUNTER_FILLING
;
886 write_unlock_bh(&nf_conntrack_lock
);
888 /* must be unlocked when calling event cache */
890 nf_conntrack_event_cache(event
, skb
);
893 #if defined(CONFIG_NF_CT_NETLINK) || \
894 defined(CONFIG_NF_CT_NETLINK_MODULE)
896 #include <linux/netfilter/nfnetlink.h>
897 #include <linux/netfilter/nfnetlink_conntrack.h>
898 #include <linux/mutex.h>
901 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
902 * in ip_conntrack_core, since we don't want the protocols to autoload
903 * or depend on ctnetlink */
904 int nf_ct_port_tuple_to_nfattr(struct sk_buff
*skb
,
905 const struct nf_conntrack_tuple
*tuple
)
907 NFA_PUT(skb
, CTA_PROTO_SRC_PORT
, sizeof(u_int16_t
),
908 &tuple
->src
.u
.tcp
.port
);
909 NFA_PUT(skb
, CTA_PROTO_DST_PORT
, sizeof(u_int16_t
),
910 &tuple
->dst
.u
.tcp
.port
);
917 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
918 [CTA_PROTO_SRC_PORT
-1] = sizeof(u_int16_t
),
919 [CTA_PROTO_DST_PORT
-1] = sizeof(u_int16_t
)
922 int nf_ct_port_nfattr_to_tuple(struct nfattr
*tb
[],
923 struct nf_conntrack_tuple
*t
)
925 if (!tb
[CTA_PROTO_SRC_PORT
-1] || !tb
[CTA_PROTO_DST_PORT
-1])
928 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
932 *(u_int16_t
*)NFA_DATA(tb
[CTA_PROTO_SRC_PORT
-1]);
934 *(u_int16_t
*)NFA_DATA(tb
[CTA_PROTO_DST_PORT
-1]);
940 /* Used by ipt_REJECT and ip6t_REJECT. */
941 void __nf_conntrack_attach(struct sk_buff
*nskb
, struct sk_buff
*skb
)
944 enum ip_conntrack_info ctinfo
;
946 /* This ICMP is in reverse direction to the packet which caused it */
947 ct
= nf_ct_get(skb
, &ctinfo
);
948 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
)
949 ctinfo
= IP_CT_RELATED
+ IP_CT_IS_REPLY
;
951 ctinfo
= IP_CT_RELATED
;
953 /* Attach to new skbuff, and increment count */
954 nskb
->nfct
= &ct
->ct_general
;
955 nskb
->nfctinfo
= ctinfo
;
956 nf_conntrack_get(nskb
->nfct
);
960 do_iter(const struct nf_conntrack_tuple_hash
*i
,
961 int (*iter
)(struct nf_conn
*i
, void *data
),
964 return iter(nf_ct_tuplehash_to_ctrack(i
), data
);
967 /* Bring out ya dead! */
968 static struct nf_conn
*
969 get_next_corpse(int (*iter
)(struct nf_conn
*i
, void *data
),
970 void *data
, unsigned int *bucket
)
972 struct nf_conntrack_tuple_hash
*h
;
975 write_lock_bh(&nf_conntrack_lock
);
976 for (; *bucket
< nf_conntrack_htable_size
; (*bucket
)++) {
977 list_for_each_entry(h
, &nf_conntrack_hash
[*bucket
], list
) {
978 ct
= nf_ct_tuplehash_to_ctrack(h
);
983 list_for_each_entry(h
, &unconfirmed
, list
) {
984 ct
= nf_ct_tuplehash_to_ctrack(h
);
988 write_unlock_bh(&nf_conntrack_lock
);
991 atomic_inc(&ct
->ct_general
.use
);
992 write_unlock_bh(&nf_conntrack_lock
);
997 nf_ct_iterate_cleanup(int (*iter
)(struct nf_conn
*i
, void *data
), void *data
)
1000 unsigned int bucket
= 0;
1002 while ((ct
= get_next_corpse(iter
, data
, &bucket
)) != NULL
) {
1003 /* Time to push up daises... */
1004 if (del_timer(&ct
->timeout
))
1005 death_by_timeout((unsigned long)ct
);
1006 /* ... else the timer will get him soon. */
1012 static int kill_all(struct nf_conn
*i
, void *data
)
1017 static void free_conntrack_hash(struct list_head
*hash
, int vmalloced
, int size
)
1022 free_pages((unsigned long)hash
,
1023 get_order(sizeof(struct list_head
) * size
));
1026 void nf_conntrack_flush()
1028 nf_ct_iterate_cleanup(kill_all
, NULL
);
1031 /* Mishearing the voices in his head, our hero wonders how he's
1032 supposed to kill the mall. */
1033 void nf_conntrack_cleanup(void)
1037 ip_ct_attach
= NULL
;
1039 /* This makes sure all current packets have passed through
1040 netfilter framework. Roll on, two-stage module
1044 nf_ct_event_cache_flush();
1046 nf_conntrack_flush();
1047 if (atomic_read(&nf_conntrack_count
) != 0) {
1049 goto i_see_dead_people
;
1051 /* wait until all references to nf_conntrack_untracked are dropped */
1052 while (atomic_read(&nf_conntrack_untracked
.ct_general
.use
) > 1)
1055 for (i
= 0; i
< NF_CT_F_NUM
; i
++) {
1056 if (nf_ct_cache
[i
].use
== 0)
1059 NF_CT_ASSERT(nf_ct_cache
[i
].use
== 1);
1060 nf_ct_cache
[i
].use
= 1;
1061 nf_conntrack_unregister_cache(i
);
1063 kmem_cache_destroy(nf_conntrack_expect_cachep
);
1064 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1065 nf_conntrack_htable_size
);
1067 /* free l3proto protocol tables */
1068 for (i
= 0; i
< PF_MAX
; i
++)
1069 if (nf_ct_protos
[i
]) {
1070 kfree(nf_ct_protos
[i
]);
1071 nf_ct_protos
[i
] = NULL
;
1075 static struct list_head
*alloc_hashtable(int size
, int *vmalloced
)
1077 struct list_head
*hash
;
1081 hash
= (void*)__get_free_pages(GFP_KERNEL
,
1082 get_order(sizeof(struct list_head
)
1086 printk(KERN_WARNING
"nf_conntrack: falling back to vmalloc.\n");
1087 hash
= vmalloc(sizeof(struct list_head
) * size
);
1091 for (i
= 0; i
< size
; i
++)
1092 INIT_LIST_HEAD(&hash
[i
]);
1097 int set_hashsize(const char *val
, struct kernel_param
*kp
)
1099 int i
, bucket
, hashsize
, vmalloced
;
1100 int old_vmalloced
, old_size
;
1102 struct list_head
*hash
, *old_hash
;
1103 struct nf_conntrack_tuple_hash
*h
;
1105 /* On boot, we can set this without any fancy locking. */
1106 if (!nf_conntrack_htable_size
)
1107 return param_set_uint(val
, kp
);
1109 hashsize
= simple_strtol(val
, NULL
, 0);
1113 hash
= alloc_hashtable(hashsize
, &vmalloced
);
1117 /* We have to rehahs for the new table anyway, so we also can
1118 * use a newrandom seed */
1119 get_random_bytes(&rnd
, 4);
1121 write_lock_bh(&nf_conntrack_lock
);
1122 for (i
= 0; i
< nf_conntrack_htable_size
; i
++) {
1123 while (!list_empty(&nf_conntrack_hash
[i
])) {
1124 h
= list_entry(nf_conntrack_hash
[i
].next
,
1125 struct nf_conntrack_tuple_hash
, list
);
1127 bucket
= __hash_conntrack(&h
->tuple
, hashsize
, rnd
);
1128 list_add_tail(&h
->list
, &hash
[bucket
]);
1131 old_size
= nf_conntrack_htable_size
;
1132 old_vmalloced
= nf_conntrack_vmalloc
;
1133 old_hash
= nf_conntrack_hash
;
1135 nf_conntrack_htable_size
= hashsize
;
1136 nf_conntrack_vmalloc
= vmalloced
;
1137 nf_conntrack_hash
= hash
;
1138 nf_conntrack_hash_rnd
= rnd
;
1139 write_unlock_bh(&nf_conntrack_lock
);
1141 free_conntrack_hash(old_hash
, old_vmalloced
, old_size
);
1145 module_param_call(hashsize
, set_hashsize
, param_get_uint
,
1146 &nf_conntrack_htable_size
, 0600);
1148 int __init
nf_conntrack_init(void)
1153 /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
1154 * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
1155 if (!nf_conntrack_htable_size
) {
1156 nf_conntrack_htable_size
1157 = (((num_physpages
<< PAGE_SHIFT
) / 16384)
1158 / sizeof(struct list_head
));
1159 if (num_physpages
> (1024 * 1024 * 1024 / PAGE_SIZE
))
1160 nf_conntrack_htable_size
= 8192;
1161 if (nf_conntrack_htable_size
< 16)
1162 nf_conntrack_htable_size
= 16;
1164 nf_conntrack_max
= 8 * nf_conntrack_htable_size
;
1166 printk("nf_conntrack version %s (%u buckets, %d max)\n",
1167 NF_CONNTRACK_VERSION
, nf_conntrack_htable_size
,
1170 nf_conntrack_hash
= alloc_hashtable(nf_conntrack_htable_size
,
1171 &nf_conntrack_vmalloc
);
1172 if (!nf_conntrack_hash
) {
1173 printk(KERN_ERR
"Unable to create nf_conntrack_hash\n");
1177 ret
= nf_conntrack_register_cache(NF_CT_F_BASIC
, "nf_conntrack:basic",
1178 sizeof(struct nf_conn
));
1180 printk(KERN_ERR
"Unable to create nf_conn slab cache\n");
1184 nf_conntrack_expect_cachep
= kmem_cache_create("nf_conntrack_expect",
1185 sizeof(struct nf_conntrack_expect
),
1187 if (!nf_conntrack_expect_cachep
) {
1188 printk(KERN_ERR
"Unable to create nf_expect slab cache\n");
1189 goto err_free_conntrack_slab
;
1192 /* Don't NEED lock here, but good form anyway. */
1193 write_lock_bh(&nf_conntrack_lock
);
1194 for (i
= 0; i
< AF_MAX
; i
++)
1195 nf_ct_l3protos
[i
] = &nf_conntrack_l3proto_generic
;
1196 write_unlock_bh(&nf_conntrack_lock
);
1198 /* For use by REJECT target */
1199 ip_ct_attach
= __nf_conntrack_attach
;
1201 /* Set up fake conntrack:
1202 - to never be deleted, not in any hashes */
1203 atomic_set(&nf_conntrack_untracked
.ct_general
.use
, 1);
1204 /* - and look it like as a confirmed connection */
1205 set_bit(IPS_CONFIRMED_BIT
, &nf_conntrack_untracked
.status
);
1209 err_free_conntrack_slab
:
1210 nf_conntrack_unregister_cache(NF_CT_F_BASIC
);
1212 free_conntrack_hash(nf_conntrack_hash
, nf_conntrack_vmalloc
,
1213 nf_conntrack_htable_size
);