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>
8 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/types.h>
16 #include <linux/netfilter.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/skbuff.h>
20 #include <linux/proc_fs.h>
21 #include <linux/vmalloc.h>
22 #include <linux/stddef.h>
23 #include <linux/slab.h>
24 #include <linux/random.h>
25 #include <linux/jhash.h>
26 #include <linux/err.h>
27 #include <linux/percpu.h>
28 #include <linux/moduleparam.h>
29 #include <linux/notifier.h>
30 #include <linux/kernel.h>
31 #include <linux/netdevice.h>
32 #include <linux/socket.h>
34 #include <linux/nsproxy.h>
35 #include <linux/rculist_nulls.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_l4proto.h>
40 #include <net/netfilter/nf_conntrack_expect.h>
41 #include <net/netfilter/nf_conntrack_helper.h>
42 #include <net/netfilter/nf_conntrack_seqadj.h>
43 #include <net/netfilter/nf_conntrack_core.h>
44 #include <net/netfilter/nf_conntrack_extend.h>
45 #include <net/netfilter/nf_conntrack_acct.h>
46 #include <net/netfilter/nf_conntrack_ecache.h>
47 #include <net/netfilter/nf_conntrack_zones.h>
48 #include <net/netfilter/nf_conntrack_timestamp.h>
49 #include <net/netfilter/nf_conntrack_timeout.h>
50 #include <net/netfilter/nf_conntrack_labels.h>
51 #include <net/netfilter/nf_conntrack_synproxy.h>
52 #include <net/netfilter/nf_nat.h>
53 #include <net/netfilter/nf_nat_core.h>
54 #include <net/netfilter/nf_nat_helper.h>
56 #define NF_CONNTRACK_VERSION "0.5.0"
58 int (*nfnetlink_parse_nat_setup_hook
)(struct nf_conn
*ct
,
59 enum nf_nat_manip_type manip
,
60 const struct nlattr
*attr
) __read_mostly
;
61 EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook
);
63 __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks
[CONNTRACK_LOCKS
];
64 EXPORT_SYMBOL_GPL(nf_conntrack_locks
);
66 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(nf_conntrack_expect_lock
);
67 EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock
);
69 static __read_mostly spinlock_t nf_conntrack_locks_all_lock
;
70 static __read_mostly
bool nf_conntrack_locks_all
;
72 void nf_conntrack_lock(spinlock_t
*lock
) __acquires(lock
)
75 while (unlikely(nf_conntrack_locks_all
)) {
77 spin_unlock_wait(&nf_conntrack_locks_all_lock
);
81 EXPORT_SYMBOL_GPL(nf_conntrack_lock
);
83 static void nf_conntrack_double_unlock(unsigned int h1
, unsigned int h2
)
85 h1
%= CONNTRACK_LOCKS
;
86 h2
%= CONNTRACK_LOCKS
;
87 spin_unlock(&nf_conntrack_locks
[h1
]);
89 spin_unlock(&nf_conntrack_locks
[h2
]);
92 /* return true if we need to recompute hashes (in case hash table was resized) */
93 static bool nf_conntrack_double_lock(struct net
*net
, unsigned int h1
,
94 unsigned int h2
, unsigned int sequence
)
96 h1
%= CONNTRACK_LOCKS
;
97 h2
%= CONNTRACK_LOCKS
;
99 nf_conntrack_lock(&nf_conntrack_locks
[h1
]);
101 spin_lock_nested(&nf_conntrack_locks
[h2
],
102 SINGLE_DEPTH_NESTING
);
104 nf_conntrack_lock(&nf_conntrack_locks
[h2
]);
105 spin_lock_nested(&nf_conntrack_locks
[h1
],
106 SINGLE_DEPTH_NESTING
);
108 if (read_seqcount_retry(&net
->ct
.generation
, sequence
)) {
109 nf_conntrack_double_unlock(h1
, h2
);
115 static void nf_conntrack_all_lock(void)
119 spin_lock(&nf_conntrack_locks_all_lock
);
120 nf_conntrack_locks_all
= true;
122 for (i
= 0; i
< CONNTRACK_LOCKS
; i
++) {
123 spin_unlock_wait(&nf_conntrack_locks
[i
]);
127 static void nf_conntrack_all_unlock(void)
129 nf_conntrack_locks_all
= false;
130 spin_unlock(&nf_conntrack_locks_all_lock
);
133 unsigned int nf_conntrack_htable_size __read_mostly
;
134 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size
);
136 unsigned int nf_conntrack_max __read_mostly
;
137 EXPORT_SYMBOL_GPL(nf_conntrack_max
);
139 DEFINE_PER_CPU(struct nf_conn
, nf_conntrack_untracked
);
140 EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked
);
142 unsigned int nf_conntrack_hash_rnd __read_mostly
;
143 EXPORT_SYMBOL_GPL(nf_conntrack_hash_rnd
);
145 static u32
hash_conntrack_raw(const struct nf_conntrack_tuple
*tuple
)
149 /* The direction must be ignored, so we hash everything up to the
150 * destination ports (which is a multiple of 4) and treat the last
151 * three bytes manually.
153 n
= (sizeof(tuple
->src
) + sizeof(tuple
->dst
.u3
)) / sizeof(u32
);
154 return jhash2((u32
*)tuple
, n
, nf_conntrack_hash_rnd
^
155 (((__force __u16
)tuple
->dst
.u
.all
<< 16) |
156 tuple
->dst
.protonum
));
159 static u32
__hash_bucket(u32 hash
, unsigned int size
)
161 return reciprocal_scale(hash
, size
);
164 static u32
hash_bucket(u32 hash
, const struct net
*net
)
166 return __hash_bucket(hash
, net
->ct
.htable_size
);
169 static u_int32_t
__hash_conntrack(const struct nf_conntrack_tuple
*tuple
,
172 return __hash_bucket(hash_conntrack_raw(tuple
), size
);
175 static inline u_int32_t
hash_conntrack(const struct net
*net
,
176 const struct nf_conntrack_tuple
*tuple
)
178 return __hash_conntrack(tuple
, net
->ct
.htable_size
);
182 nf_ct_get_tuple(const struct sk_buff
*skb
,
184 unsigned int dataoff
,
188 struct nf_conntrack_tuple
*tuple
,
189 const struct nf_conntrack_l3proto
*l3proto
,
190 const struct nf_conntrack_l4proto
*l4proto
)
192 memset(tuple
, 0, sizeof(*tuple
));
194 tuple
->src
.l3num
= l3num
;
195 if (l3proto
->pkt_to_tuple(skb
, nhoff
, tuple
) == 0)
198 tuple
->dst
.protonum
= protonum
;
199 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
201 return l4proto
->pkt_to_tuple(skb
, dataoff
, net
, tuple
);
203 EXPORT_SYMBOL_GPL(nf_ct_get_tuple
);
205 bool nf_ct_get_tuplepr(const struct sk_buff
*skb
, unsigned int nhoff
,
207 struct net
*net
, struct nf_conntrack_tuple
*tuple
)
209 struct nf_conntrack_l3proto
*l3proto
;
210 struct nf_conntrack_l4proto
*l4proto
;
211 unsigned int protoff
;
217 l3proto
= __nf_ct_l3proto_find(l3num
);
218 ret
= l3proto
->get_l4proto(skb
, nhoff
, &protoff
, &protonum
);
219 if (ret
!= NF_ACCEPT
) {
224 l4proto
= __nf_ct_l4proto_find(l3num
, protonum
);
226 ret
= nf_ct_get_tuple(skb
, nhoff
, protoff
, l3num
, protonum
, net
, tuple
,
232 EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr
);
235 nf_ct_invert_tuple(struct nf_conntrack_tuple
*inverse
,
236 const struct nf_conntrack_tuple
*orig
,
237 const struct nf_conntrack_l3proto
*l3proto
,
238 const struct nf_conntrack_l4proto
*l4proto
)
240 memset(inverse
, 0, sizeof(*inverse
));
242 inverse
->src
.l3num
= orig
->src
.l3num
;
243 if (l3proto
->invert_tuple(inverse
, orig
) == 0)
246 inverse
->dst
.dir
= !orig
->dst
.dir
;
248 inverse
->dst
.protonum
= orig
->dst
.protonum
;
249 return l4proto
->invert_tuple(inverse
, orig
);
251 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple
);
254 clean_from_lists(struct nf_conn
*ct
)
256 pr_debug("clean_from_lists(%p)\n", ct
);
257 hlist_nulls_del_rcu(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
);
258 hlist_nulls_del_rcu(&ct
->tuplehash
[IP_CT_DIR_REPLY
].hnnode
);
260 /* Destroy all pending expectations */
261 nf_ct_remove_expectations(ct
);
264 /* must be called with local_bh_disable */
265 static void nf_ct_add_to_dying_list(struct nf_conn
*ct
)
267 struct ct_pcpu
*pcpu
;
269 /* add this conntrack to the (per cpu) dying list */
270 ct
->cpu
= smp_processor_id();
271 pcpu
= per_cpu_ptr(nf_ct_net(ct
)->ct
.pcpu_lists
, ct
->cpu
);
273 spin_lock(&pcpu
->lock
);
274 hlist_nulls_add_head(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
,
276 spin_unlock(&pcpu
->lock
);
279 /* must be called with local_bh_disable */
280 static void nf_ct_add_to_unconfirmed_list(struct nf_conn
*ct
)
282 struct ct_pcpu
*pcpu
;
284 /* add this conntrack to the (per cpu) unconfirmed list */
285 ct
->cpu
= smp_processor_id();
286 pcpu
= per_cpu_ptr(nf_ct_net(ct
)->ct
.pcpu_lists
, ct
->cpu
);
288 spin_lock(&pcpu
->lock
);
289 hlist_nulls_add_head(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
,
291 spin_unlock(&pcpu
->lock
);
294 /* must be called with local_bh_disable */
295 static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn
*ct
)
297 struct ct_pcpu
*pcpu
;
299 /* We overload first tuple to link into unconfirmed or dying list.*/
300 pcpu
= per_cpu_ptr(nf_ct_net(ct
)->ct
.pcpu_lists
, ct
->cpu
);
302 spin_lock(&pcpu
->lock
);
303 BUG_ON(hlist_nulls_unhashed(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
));
304 hlist_nulls_del_rcu(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
);
305 spin_unlock(&pcpu
->lock
);
308 /* Released via destroy_conntrack() */
309 struct nf_conn
*nf_ct_tmpl_alloc(struct net
*net
,
310 const struct nf_conntrack_zone
*zone
,
313 struct nf_conn
*tmpl
;
315 tmpl
= kzalloc(sizeof(*tmpl
), flags
);
319 tmpl
->status
= IPS_TEMPLATE
;
320 write_pnet(&tmpl
->ct_net
, net
);
322 if (nf_ct_zone_add(tmpl
, flags
, zone
) < 0)
325 atomic_set(&tmpl
->ct_general
.use
, 0);
332 EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc
);
334 void nf_ct_tmpl_free(struct nf_conn
*tmpl
)
336 nf_ct_ext_destroy(tmpl
);
337 nf_ct_ext_free(tmpl
);
340 EXPORT_SYMBOL_GPL(nf_ct_tmpl_free
);
343 destroy_conntrack(struct nf_conntrack
*nfct
)
345 struct nf_conn
*ct
= (struct nf_conn
*)nfct
;
346 struct net
*net
= nf_ct_net(ct
);
347 struct nf_conntrack_l4proto
*l4proto
;
349 pr_debug("destroy_conntrack(%p)\n", ct
);
350 NF_CT_ASSERT(atomic_read(&nfct
->use
) == 0);
351 NF_CT_ASSERT(!timer_pending(&ct
->timeout
));
353 if (unlikely(nf_ct_is_template(ct
))) {
358 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
359 if (l4proto
&& l4proto
->destroy
)
360 l4proto
->destroy(ct
);
365 /* Expectations will have been removed in clean_from_lists,
366 * except TFTP can create an expectation on the first packet,
367 * before connection is in the list, so we need to clean here,
370 nf_ct_remove_expectations(ct
);
372 nf_ct_del_from_dying_or_unconfirmed_list(ct
);
374 NF_CT_STAT_INC(net
, delete);
378 nf_ct_put(ct
->master
);
380 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct
);
381 nf_conntrack_free(ct
);
384 static void nf_ct_delete_from_lists(struct nf_conn
*ct
)
386 struct net
*net
= nf_ct_net(ct
);
387 unsigned int hash
, reply_hash
;
388 unsigned int sequence
;
390 nf_ct_helper_destroy(ct
);
394 sequence
= read_seqcount_begin(&net
->ct
.generation
);
395 hash
= hash_conntrack(net
,
396 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
397 reply_hash
= hash_conntrack(net
,
398 &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
399 } while (nf_conntrack_double_lock(net
, hash
, reply_hash
, sequence
));
401 clean_from_lists(ct
);
402 nf_conntrack_double_unlock(hash
, reply_hash
);
404 nf_ct_add_to_dying_list(ct
);
406 NF_CT_STAT_INC(net
, delete_list
);
410 bool nf_ct_delete(struct nf_conn
*ct
, u32 portid
, int report
)
412 struct nf_conn_tstamp
*tstamp
;
414 tstamp
= nf_conn_tstamp_find(ct
);
415 if (tstamp
&& tstamp
->stop
== 0)
416 tstamp
->stop
= ktime_get_real_ns();
418 if (nf_ct_is_dying(ct
))
421 if (nf_conntrack_event_report(IPCT_DESTROY
, ct
,
422 portid
, report
) < 0) {
423 /* destroy event was not delivered */
424 nf_ct_delete_from_lists(ct
);
425 nf_conntrack_ecache_delayed_work(nf_ct_net(ct
));
429 nf_conntrack_ecache_work(nf_ct_net(ct
));
430 set_bit(IPS_DYING_BIT
, &ct
->status
);
432 nf_ct_delete_from_lists(ct
);
436 EXPORT_SYMBOL_GPL(nf_ct_delete
);
438 static void death_by_timeout(unsigned long ul_conntrack
)
440 nf_ct_delete((struct nf_conn
*)ul_conntrack
, 0, 0);
444 nf_ct_key_equal(struct nf_conntrack_tuple_hash
*h
,
445 const struct nf_conntrack_tuple
*tuple
,
446 const struct nf_conntrack_zone
*zone
)
448 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
450 /* A conntrack can be recreated with the equal tuple,
451 * so we need to check that the conntrack is confirmed
453 return nf_ct_tuple_equal(tuple
, &h
->tuple
) &&
454 nf_ct_zone_equal(ct
, zone
, NF_CT_DIRECTION(h
)) &&
455 nf_ct_is_confirmed(ct
);
460 * - Caller must take a reference on returned object
461 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
463 static struct nf_conntrack_tuple_hash
*
464 ____nf_conntrack_find(struct net
*net
, const struct nf_conntrack_zone
*zone
,
465 const struct nf_conntrack_tuple
*tuple
, u32 hash
)
467 struct nf_conntrack_tuple_hash
*h
;
468 struct hlist_nulls_node
*n
;
469 unsigned int bucket
= hash_bucket(hash
, net
);
471 /* Disable BHs the entire time since we normally need to disable them
472 * at least once for the stats anyway.
476 hlist_nulls_for_each_entry_rcu(h
, n
, &net
->ct
.hash
[bucket
], hnnode
) {
477 if (nf_ct_key_equal(h
, tuple
, zone
)) {
478 NF_CT_STAT_INC(net
, found
);
482 NF_CT_STAT_INC(net
, searched
);
485 * if the nulls value we got at the end of this lookup is
486 * not the expected one, we must restart lookup.
487 * We probably met an item that was moved to another chain.
489 if (get_nulls_value(n
) != bucket
) {
490 NF_CT_STAT_INC(net
, search_restart
);
498 /* Find a connection corresponding to a tuple. */
499 static struct nf_conntrack_tuple_hash
*
500 __nf_conntrack_find_get(struct net
*net
, const struct nf_conntrack_zone
*zone
,
501 const struct nf_conntrack_tuple
*tuple
, u32 hash
)
503 struct nf_conntrack_tuple_hash
*h
;
508 h
= ____nf_conntrack_find(net
, zone
, tuple
, hash
);
510 ct
= nf_ct_tuplehash_to_ctrack(h
);
511 if (unlikely(nf_ct_is_dying(ct
) ||
512 !atomic_inc_not_zero(&ct
->ct_general
.use
)))
515 if (unlikely(!nf_ct_key_equal(h
, tuple
, zone
))) {
526 struct nf_conntrack_tuple_hash
*
527 nf_conntrack_find_get(struct net
*net
, const struct nf_conntrack_zone
*zone
,
528 const struct nf_conntrack_tuple
*tuple
)
530 return __nf_conntrack_find_get(net
, zone
, tuple
,
531 hash_conntrack_raw(tuple
));
533 EXPORT_SYMBOL_GPL(nf_conntrack_find_get
);
535 static void __nf_conntrack_hash_insert(struct nf_conn
*ct
,
537 unsigned int reply_hash
)
539 struct net
*net
= nf_ct_net(ct
);
541 hlist_nulls_add_head_rcu(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
,
542 &net
->ct
.hash
[hash
]);
543 hlist_nulls_add_head_rcu(&ct
->tuplehash
[IP_CT_DIR_REPLY
].hnnode
,
544 &net
->ct
.hash
[reply_hash
]);
548 nf_conntrack_hash_check_insert(struct nf_conn
*ct
)
550 const struct nf_conntrack_zone
*zone
;
551 struct net
*net
= nf_ct_net(ct
);
552 unsigned int hash
, reply_hash
;
553 struct nf_conntrack_tuple_hash
*h
;
554 struct hlist_nulls_node
*n
;
555 unsigned int sequence
;
557 zone
= nf_ct_zone(ct
);
561 sequence
= read_seqcount_begin(&net
->ct
.generation
);
562 hash
= hash_conntrack(net
,
563 &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
564 reply_hash
= hash_conntrack(net
,
565 &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
566 } while (nf_conntrack_double_lock(net
, hash
, reply_hash
, sequence
));
568 /* See if there's one in the list already, including reverse */
569 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[hash
], hnnode
)
570 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
572 nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h
), zone
,
575 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[reply_hash
], hnnode
)
576 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
578 nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h
), zone
,
582 add_timer(&ct
->timeout
);
584 /* The caller holds a reference to this object */
585 atomic_set(&ct
->ct_general
.use
, 2);
586 __nf_conntrack_hash_insert(ct
, hash
, reply_hash
);
587 nf_conntrack_double_unlock(hash
, reply_hash
);
588 NF_CT_STAT_INC(net
, insert
);
593 nf_conntrack_double_unlock(hash
, reply_hash
);
594 NF_CT_STAT_INC(net
, insert_failed
);
598 EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert
);
600 /* Confirm a connection given skb; places it in hash table */
602 __nf_conntrack_confirm(struct sk_buff
*skb
)
604 const struct nf_conntrack_zone
*zone
;
605 unsigned int hash
, reply_hash
;
606 struct nf_conntrack_tuple_hash
*h
;
608 struct nf_conn_help
*help
;
609 struct nf_conn_tstamp
*tstamp
;
610 struct hlist_nulls_node
*n
;
611 enum ip_conntrack_info ctinfo
;
613 unsigned int sequence
;
615 ct
= nf_ct_get(skb
, &ctinfo
);
618 /* ipt_REJECT uses nf_conntrack_attach to attach related
619 ICMP/TCP RST packets in other direction. Actual packet
620 which created connection will be IP_CT_NEW or for an
621 expected connection, IP_CT_RELATED. */
622 if (CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
)
625 zone
= nf_ct_zone(ct
);
629 sequence
= read_seqcount_begin(&net
->ct
.generation
);
630 /* reuse the hash saved before */
631 hash
= *(unsigned long *)&ct
->tuplehash
[IP_CT_DIR_REPLY
].hnnode
.pprev
;
632 hash
= hash_bucket(hash
, net
);
633 reply_hash
= hash_conntrack(net
,
634 &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
636 } while (nf_conntrack_double_lock(net
, hash
, reply_hash
, sequence
));
638 /* We're not in hash table, and we refuse to set up related
639 * connections for unconfirmed conns. But packet copies and
640 * REJECT will give spurious warnings here.
642 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
644 /* No external references means no one else could have
647 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
648 pr_debug("Confirming conntrack %p\n", ct
);
649 /* We have to check the DYING flag after unlink to prevent
650 * a race against nf_ct_get_next_corpse() possibly called from
651 * user context, else we insert an already 'dead' hash, blocking
652 * further use of that particular connection -JM.
654 nf_ct_del_from_dying_or_unconfirmed_list(ct
);
656 if (unlikely(nf_ct_is_dying(ct
)))
659 /* See if there's one in the list already, including reverse:
660 NAT could have grabbed it without realizing, since we're
661 not in the hash. If there is, we lost race. */
662 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[hash
], hnnode
)
663 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
665 nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h
), zone
,
668 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[reply_hash
], hnnode
)
669 if (nf_ct_tuple_equal(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
671 nf_ct_zone_equal(nf_ct_tuplehash_to_ctrack(h
), zone
,
675 /* Timer relative to confirmation time, not original
676 setting time, otherwise we'd get timer wrap in
677 weird delay cases. */
678 ct
->timeout
.expires
+= jiffies
;
679 add_timer(&ct
->timeout
);
680 atomic_inc(&ct
->ct_general
.use
);
681 ct
->status
|= IPS_CONFIRMED
;
683 /* set conntrack timestamp, if enabled. */
684 tstamp
= nf_conn_tstamp_find(ct
);
686 if (skb
->tstamp
.tv64
== 0)
687 __net_timestamp(skb
);
689 tstamp
->start
= ktime_to_ns(skb
->tstamp
);
691 /* Since the lookup is lockless, hash insertion must be done after
692 * starting the timer and setting the CONFIRMED bit. The RCU barriers
693 * guarantee that no other CPU can find the conntrack before the above
694 * stores are visible.
696 __nf_conntrack_hash_insert(ct
, hash
, reply_hash
);
697 nf_conntrack_double_unlock(hash
, reply_hash
);
698 NF_CT_STAT_INC(net
, insert
);
701 help
= nfct_help(ct
);
702 if (help
&& help
->helper
)
703 nf_conntrack_event_cache(IPCT_HELPER
, ct
);
705 nf_conntrack_event_cache(master_ct(ct
) ?
706 IPCT_RELATED
: IPCT_NEW
, ct
);
710 nf_ct_add_to_dying_list(ct
);
711 nf_conntrack_double_unlock(hash
, reply_hash
);
712 NF_CT_STAT_INC(net
, insert_failed
);
716 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm
);
718 /* Returns true if a connection correspondings to the tuple (required
721 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple
*tuple
,
722 const struct nf_conn
*ignored_conntrack
)
724 struct net
*net
= nf_ct_net(ignored_conntrack
);
725 const struct nf_conntrack_zone
*zone
;
726 struct nf_conntrack_tuple_hash
*h
;
727 struct hlist_nulls_node
*n
;
731 zone
= nf_ct_zone(ignored_conntrack
);
732 hash
= hash_conntrack(net
, tuple
);
734 /* Disable BHs the entire time since we need to disable them at
735 * least once for the stats anyway.
738 hlist_nulls_for_each_entry_rcu(h
, n
, &net
->ct
.hash
[hash
], hnnode
) {
739 ct
= nf_ct_tuplehash_to_ctrack(h
);
740 if (ct
!= ignored_conntrack
&&
741 nf_ct_tuple_equal(tuple
, &h
->tuple
) &&
742 nf_ct_zone_equal(ct
, zone
, NF_CT_DIRECTION(h
))) {
743 NF_CT_STAT_INC(net
, found
);
744 rcu_read_unlock_bh();
747 NF_CT_STAT_INC(net
, searched
);
749 rcu_read_unlock_bh();
753 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken
);
755 #define NF_CT_EVICTION_RANGE 8
757 /* There's a small race here where we may free a just-assured
758 connection. Too bad: we're in trouble anyway. */
759 static noinline
int early_drop(struct net
*net
, unsigned int _hash
)
761 /* Use oldest entry, which is roughly LRU */
762 struct nf_conntrack_tuple_hash
*h
;
763 struct nf_conn
*ct
= NULL
, *tmp
;
764 struct hlist_nulls_node
*n
;
765 unsigned int i
= 0, cnt
= 0;
767 unsigned int hash
, sequence
;
772 sequence
= read_seqcount_begin(&net
->ct
.generation
);
773 hash
= hash_bucket(_hash
, net
);
774 for (; i
< net
->ct
.htable_size
; i
++) {
775 lockp
= &nf_conntrack_locks
[hash
% CONNTRACK_LOCKS
];
776 nf_conntrack_lock(lockp
);
777 if (read_seqcount_retry(&net
->ct
.generation
, sequence
)) {
781 hlist_nulls_for_each_entry_rcu(h
, n
, &net
->ct
.hash
[hash
],
783 tmp
= nf_ct_tuplehash_to_ctrack(h
);
784 if (!test_bit(IPS_ASSURED_BIT
, &tmp
->status
) &&
785 !nf_ct_is_dying(tmp
) &&
786 atomic_inc_not_zero(&tmp
->ct_general
.use
)) {
793 hash
= (hash
+ 1) % net
->ct
.htable_size
;
796 if (ct
|| cnt
>= NF_CT_EVICTION_RANGE
)
805 if (del_timer(&ct
->timeout
)) {
806 if (nf_ct_delete(ct
, 0, 0)) {
808 NF_CT_STAT_INC_ATOMIC(net
, early_drop
);
815 void init_nf_conntrack_hash_rnd(void)
820 * Why not initialize nf_conntrack_rnd in a "init()" function ?
821 * Because there isn't enough entropy when system initializing,
822 * and we initialize it as late as possible.
825 get_random_bytes(&rand
, sizeof(rand
));
827 cmpxchg(&nf_conntrack_hash_rnd
, 0, rand
);
830 static struct nf_conn
*
831 __nf_conntrack_alloc(struct net
*net
,
832 const struct nf_conntrack_zone
*zone
,
833 const struct nf_conntrack_tuple
*orig
,
834 const struct nf_conntrack_tuple
*repl
,
839 if (unlikely(!nf_conntrack_hash_rnd
)) {
840 init_nf_conntrack_hash_rnd();
841 /* recompute the hash as nf_conntrack_hash_rnd is initialized */
842 hash
= hash_conntrack_raw(orig
);
845 /* We don't want any race condition at early drop stage */
846 atomic_inc(&net
->ct
.count
);
848 if (nf_conntrack_max
&&
849 unlikely(atomic_read(&net
->ct
.count
) > nf_conntrack_max
)) {
850 if (!early_drop(net
, hash
)) {
851 atomic_dec(&net
->ct
.count
);
852 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
853 return ERR_PTR(-ENOMEM
);
858 * Do not use kmem_cache_zalloc(), as this cache uses
859 * SLAB_DESTROY_BY_RCU.
861 ct
= kmem_cache_alloc(net
->ct
.nf_conntrack_cachep
, gfp
);
865 spin_lock_init(&ct
->lock
);
866 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
= *orig
;
867 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
.pprev
= NULL
;
868 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
= *repl
;
869 /* save hash for reusing when confirming */
870 *(unsigned long *)(&ct
->tuplehash
[IP_CT_DIR_REPLY
].hnnode
.pprev
) = hash
;
872 /* Don't set timer yet: wait for confirmation */
873 setup_timer(&ct
->timeout
, death_by_timeout
, (unsigned long)ct
);
874 write_pnet(&ct
->ct_net
, net
);
875 memset(&ct
->__nfct_init_offset
[0], 0,
876 offsetof(struct nf_conn
, proto
) -
877 offsetof(struct nf_conn
, __nfct_init_offset
[0]));
879 if (zone
&& nf_ct_zone_add(ct
, GFP_ATOMIC
, zone
) < 0)
882 /* Because we use RCU lookups, we set ct_general.use to zero before
883 * this is inserted in any list.
885 atomic_set(&ct
->ct_general
.use
, 0);
888 kmem_cache_free(net
->ct
.nf_conntrack_cachep
, ct
);
890 atomic_dec(&net
->ct
.count
);
891 return ERR_PTR(-ENOMEM
);
894 struct nf_conn
*nf_conntrack_alloc(struct net
*net
,
895 const struct nf_conntrack_zone
*zone
,
896 const struct nf_conntrack_tuple
*orig
,
897 const struct nf_conntrack_tuple
*repl
,
900 return __nf_conntrack_alloc(net
, zone
, orig
, repl
, gfp
, 0);
902 EXPORT_SYMBOL_GPL(nf_conntrack_alloc
);
904 void nf_conntrack_free(struct nf_conn
*ct
)
906 struct net
*net
= nf_ct_net(ct
);
908 /* A freed object has refcnt == 0, that's
909 * the golden rule for SLAB_DESTROY_BY_RCU
911 NF_CT_ASSERT(atomic_read(&ct
->ct_general
.use
) == 0);
913 nf_ct_ext_destroy(ct
);
915 kmem_cache_free(net
->ct
.nf_conntrack_cachep
, ct
);
916 smp_mb__before_atomic();
917 atomic_dec(&net
->ct
.count
);
919 EXPORT_SYMBOL_GPL(nf_conntrack_free
);
922 /* Allocate a new conntrack: we return -ENOMEM if classification
923 failed due to stress. Otherwise it really is unclassifiable. */
924 static struct nf_conntrack_tuple_hash
*
925 init_conntrack(struct net
*net
, struct nf_conn
*tmpl
,
926 const struct nf_conntrack_tuple
*tuple
,
927 struct nf_conntrack_l3proto
*l3proto
,
928 struct nf_conntrack_l4proto
*l4proto
,
930 unsigned int dataoff
, u32 hash
)
933 struct nf_conn_help
*help
;
934 struct nf_conntrack_tuple repl_tuple
;
935 struct nf_conntrack_ecache
*ecache
;
936 struct nf_conntrack_expect
*exp
= NULL
;
937 const struct nf_conntrack_zone
*zone
;
938 struct nf_conn_timeout
*timeout_ext
;
939 struct nf_conntrack_zone tmp
;
940 unsigned int *timeouts
;
942 if (!nf_ct_invert_tuple(&repl_tuple
, tuple
, l3proto
, l4proto
)) {
943 pr_debug("Can't invert tuple.\n");
947 zone
= nf_ct_zone_tmpl(tmpl
, skb
, &tmp
);
948 ct
= __nf_conntrack_alloc(net
, zone
, tuple
, &repl_tuple
, GFP_ATOMIC
,
951 return (struct nf_conntrack_tuple_hash
*)ct
;
953 if (tmpl
&& nfct_synproxy(tmpl
)) {
954 nfct_seqadj_ext_add(ct
);
955 nfct_synproxy_ext_add(ct
);
958 timeout_ext
= tmpl
? nf_ct_timeout_find(tmpl
) : NULL
;
960 timeouts
= nf_ct_timeout_data(timeout_ext
);
961 if (unlikely(!timeouts
))
962 timeouts
= l4proto
->get_timeouts(net
);
964 timeouts
= l4proto
->get_timeouts(net
);
967 if (!l4proto
->new(ct
, skb
, dataoff
, timeouts
)) {
968 nf_conntrack_free(ct
);
969 pr_debug("init conntrack: can't track with proto module\n");
974 nf_ct_timeout_ext_add(ct
, rcu_dereference(timeout_ext
->timeout
),
977 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
978 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
979 nf_ct_labels_ext_add(ct
);
981 ecache
= tmpl
? nf_ct_ecache_find(tmpl
) : NULL
;
982 nf_ct_ecache_ext_add(ct
, ecache
? ecache
->ctmask
: 0,
983 ecache
? ecache
->expmask
: 0,
987 if (net
->ct
.expect_count
) {
988 spin_lock(&nf_conntrack_expect_lock
);
989 exp
= nf_ct_find_expectation(net
, zone
, tuple
);
991 pr_debug("conntrack: expectation arrives ct=%p exp=%p\n",
993 /* Welcome, Mr. Bond. We've been expecting you... */
994 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
995 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
996 ct
->master
= exp
->master
;
998 help
= nf_ct_helper_ext_add(ct
, exp
->helper
,
1001 rcu_assign_pointer(help
->helper
, exp
->helper
);
1004 #ifdef CONFIG_NF_CONNTRACK_MARK
1005 ct
->mark
= exp
->master
->mark
;
1007 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1008 ct
->secmark
= exp
->master
->secmark
;
1010 NF_CT_STAT_INC(net
, expect_new
);
1012 spin_unlock(&nf_conntrack_expect_lock
);
1015 __nf_ct_try_assign_helper(ct
, tmpl
, GFP_ATOMIC
);
1016 NF_CT_STAT_INC(net
, new);
1019 /* Now it is inserted into the unconfirmed list, bump refcount */
1020 nf_conntrack_get(&ct
->ct_general
);
1021 nf_ct_add_to_unconfirmed_list(ct
);
1027 exp
->expectfn(ct
, exp
);
1028 nf_ct_expect_put(exp
);
1031 return &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
];
1034 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1035 static inline struct nf_conn
*
1036 resolve_normal_ct(struct net
*net
, struct nf_conn
*tmpl
,
1037 struct sk_buff
*skb
,
1038 unsigned int dataoff
,
1041 struct nf_conntrack_l3proto
*l3proto
,
1042 struct nf_conntrack_l4proto
*l4proto
,
1044 enum ip_conntrack_info
*ctinfo
)
1046 const struct nf_conntrack_zone
*zone
;
1047 struct nf_conntrack_tuple tuple
;
1048 struct nf_conntrack_tuple_hash
*h
;
1049 struct nf_conntrack_zone tmp
;
1053 if (!nf_ct_get_tuple(skb
, skb_network_offset(skb
),
1054 dataoff
, l3num
, protonum
, net
, &tuple
, l3proto
,
1056 pr_debug("resolve_normal_ct: Can't get tuple\n");
1060 /* look for tuple match */
1061 zone
= nf_ct_zone_tmpl(tmpl
, skb
, &tmp
);
1062 hash
= hash_conntrack_raw(&tuple
);
1063 h
= __nf_conntrack_find_get(net
, zone
, &tuple
, hash
);
1065 h
= init_conntrack(net
, tmpl
, &tuple
, l3proto
, l4proto
,
1066 skb
, dataoff
, hash
);
1072 ct
= nf_ct_tuplehash_to_ctrack(h
);
1074 /* It exists; we have (non-exclusive) reference. */
1075 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
) {
1076 *ctinfo
= IP_CT_ESTABLISHED_REPLY
;
1077 /* Please set reply bit if this packet OK */
1080 /* Once we've had two way comms, always ESTABLISHED. */
1081 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
1082 pr_debug("nf_conntrack_in: normal packet for %p\n", ct
);
1083 *ctinfo
= IP_CT_ESTABLISHED
;
1084 } else if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
)) {
1085 pr_debug("nf_conntrack_in: related packet for %p\n",
1087 *ctinfo
= IP_CT_RELATED
;
1089 pr_debug("nf_conntrack_in: new packet for %p\n", ct
);
1090 *ctinfo
= IP_CT_NEW
;
1094 skb
->nfct
= &ct
->ct_general
;
1095 skb
->nfctinfo
= *ctinfo
;
1100 nf_conntrack_in(struct net
*net
, u_int8_t pf
, unsigned int hooknum
,
1101 struct sk_buff
*skb
)
1103 struct nf_conn
*ct
, *tmpl
= NULL
;
1104 enum ip_conntrack_info ctinfo
;
1105 struct nf_conntrack_l3proto
*l3proto
;
1106 struct nf_conntrack_l4proto
*l4proto
;
1107 unsigned int *timeouts
;
1108 unsigned int dataoff
;
1114 /* Previously seen (loopback or untracked)? Ignore. */
1115 tmpl
= (struct nf_conn
*)skb
->nfct
;
1116 if (!nf_ct_is_template(tmpl
)) {
1117 NF_CT_STAT_INC_ATOMIC(net
, ignore
);
1123 /* rcu_read_lock()ed by nf_hook_slow */
1124 l3proto
= __nf_ct_l3proto_find(pf
);
1125 ret
= l3proto
->get_l4proto(skb
, skb_network_offset(skb
),
1126 &dataoff
, &protonum
);
1128 pr_debug("not prepared to track yet or error occurred\n");
1129 NF_CT_STAT_INC_ATOMIC(net
, error
);
1130 NF_CT_STAT_INC_ATOMIC(net
, invalid
);
1135 l4proto
= __nf_ct_l4proto_find(pf
, protonum
);
1137 /* It may be an special packet, error, unclean...
1138 * inverse of the return code tells to the netfilter
1139 * core what to do with the packet. */
1140 if (l4proto
->error
!= NULL
) {
1141 ret
= l4proto
->error(net
, tmpl
, skb
, dataoff
, &ctinfo
,
1144 NF_CT_STAT_INC_ATOMIC(net
, error
);
1145 NF_CT_STAT_INC_ATOMIC(net
, invalid
);
1149 /* ICMP[v6] protocol trackers may assign one conntrack. */
1154 ct
= resolve_normal_ct(net
, tmpl
, skb
, dataoff
, pf
, protonum
,
1155 l3proto
, l4proto
, &set_reply
, &ctinfo
);
1157 /* Not valid part of a connection */
1158 NF_CT_STAT_INC_ATOMIC(net
, invalid
);
1164 /* Too stressed to deal. */
1165 NF_CT_STAT_INC_ATOMIC(net
, drop
);
1170 NF_CT_ASSERT(skb
->nfct
);
1172 /* Decide what timeout policy we want to apply to this flow. */
1173 timeouts
= nf_ct_timeout_lookup(net
, ct
, l4proto
);
1175 ret
= l4proto
->packet(ct
, skb
, dataoff
, ctinfo
, pf
, hooknum
, timeouts
);
1177 /* Invalid: inverse of the return code tells
1178 * the netfilter core what to do */
1179 pr_debug("nf_conntrack_in: Can't track with proto module\n");
1180 nf_conntrack_put(skb
->nfct
);
1182 NF_CT_STAT_INC_ATOMIC(net
, invalid
);
1183 if (ret
== -NF_DROP
)
1184 NF_CT_STAT_INC_ATOMIC(net
, drop
);
1189 if (set_reply
&& !test_and_set_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
))
1190 nf_conntrack_event_cache(IPCT_REPLY
, ct
);
1193 /* Special case: we have to repeat this hook, assign the
1194 * template again to this packet. We assume that this packet
1195 * has no conntrack assigned. This is used by nf_ct_tcp. */
1196 if (ret
== NF_REPEAT
)
1197 skb
->nfct
= (struct nf_conntrack
*)tmpl
;
1204 EXPORT_SYMBOL_GPL(nf_conntrack_in
);
1206 bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple
*inverse
,
1207 const struct nf_conntrack_tuple
*orig
)
1212 ret
= nf_ct_invert_tuple(inverse
, orig
,
1213 __nf_ct_l3proto_find(orig
->src
.l3num
),
1214 __nf_ct_l4proto_find(orig
->src
.l3num
,
1215 orig
->dst
.protonum
));
1219 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr
);
1221 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
1222 implicitly racy: see __nf_conntrack_confirm */
1223 void nf_conntrack_alter_reply(struct nf_conn
*ct
,
1224 const struct nf_conntrack_tuple
*newreply
)
1226 struct nf_conn_help
*help
= nfct_help(ct
);
1228 /* Should be unconfirmed, so not in hash table yet */
1229 NF_CT_ASSERT(!nf_ct_is_confirmed(ct
));
1231 pr_debug("Altering reply tuple of %p to ", ct
);
1232 nf_ct_dump_tuple(newreply
);
1234 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
= *newreply
;
1235 if (ct
->master
|| (help
&& !hlist_empty(&help
->expectations
)))
1239 __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
1242 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply
);
1244 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1245 void __nf_ct_refresh_acct(struct nf_conn
*ct
,
1246 enum ip_conntrack_info ctinfo
,
1247 const struct sk_buff
*skb
,
1248 unsigned long extra_jiffies
,
1251 NF_CT_ASSERT(ct
->timeout
.data
== (unsigned long)ct
);
1254 /* Only update if this is not a fixed timeout */
1255 if (test_bit(IPS_FIXED_TIMEOUT_BIT
, &ct
->status
))
1258 /* If not in hash table, timer will not be active yet */
1259 if (!nf_ct_is_confirmed(ct
)) {
1260 ct
->timeout
.expires
= extra_jiffies
;
1262 unsigned long newtime
= jiffies
+ extra_jiffies
;
1264 /* Only update the timeout if the new timeout is at least
1265 HZ jiffies from the old timeout. Need del_timer for race
1266 avoidance (may already be dying). */
1267 if (newtime
- ct
->timeout
.expires
>= HZ
)
1268 mod_timer_pending(&ct
->timeout
, newtime
);
1273 struct nf_conn_acct
*acct
;
1275 acct
= nf_conn_acct_find(ct
);
1277 struct nf_conn_counter
*counter
= acct
->counter
;
1279 atomic64_inc(&counter
[CTINFO2DIR(ctinfo
)].packets
);
1280 atomic64_add(skb
->len
, &counter
[CTINFO2DIR(ctinfo
)].bytes
);
1284 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct
);
1286 bool __nf_ct_kill_acct(struct nf_conn
*ct
,
1287 enum ip_conntrack_info ctinfo
,
1288 const struct sk_buff
*skb
,
1292 struct nf_conn_acct
*acct
;
1294 acct
= nf_conn_acct_find(ct
);
1296 struct nf_conn_counter
*counter
= acct
->counter
;
1298 atomic64_inc(&counter
[CTINFO2DIR(ctinfo
)].packets
);
1299 atomic64_add(skb
->len
- skb_network_offset(skb
),
1300 &counter
[CTINFO2DIR(ctinfo
)].bytes
);
1304 if (del_timer(&ct
->timeout
)) {
1305 ct
->timeout
.function((unsigned long)ct
);
1310 EXPORT_SYMBOL_GPL(__nf_ct_kill_acct
);
1312 #ifdef CONFIG_NF_CONNTRACK_ZONES
1313 static struct nf_ct_ext_type nf_ct_zone_extend __read_mostly
= {
1314 .len
= sizeof(struct nf_conntrack_zone
),
1315 .align
= __alignof__(struct nf_conntrack_zone
),
1316 .id
= NF_CT_EXT_ZONE
,
1320 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1322 #include <linux/netfilter/nfnetlink.h>
1323 #include <linux/netfilter/nfnetlink_conntrack.h>
1324 #include <linux/mutex.h>
1326 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1327 * in ip_conntrack_core, since we don't want the protocols to autoload
1328 * or depend on ctnetlink */
1329 int nf_ct_port_tuple_to_nlattr(struct sk_buff
*skb
,
1330 const struct nf_conntrack_tuple
*tuple
)
1332 if (nla_put_be16(skb
, CTA_PROTO_SRC_PORT
, tuple
->src
.u
.tcp
.port
) ||
1333 nla_put_be16(skb
, CTA_PROTO_DST_PORT
, tuple
->dst
.u
.tcp
.port
))
1334 goto nla_put_failure
;
1340 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr
);
1342 const struct nla_policy nf_ct_port_nla_policy
[CTA_PROTO_MAX
+1] = {
1343 [CTA_PROTO_SRC_PORT
] = { .type
= NLA_U16
},
1344 [CTA_PROTO_DST_PORT
] = { .type
= NLA_U16
},
1346 EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy
);
1348 int nf_ct_port_nlattr_to_tuple(struct nlattr
*tb
[],
1349 struct nf_conntrack_tuple
*t
)
1351 if (!tb
[CTA_PROTO_SRC_PORT
] || !tb
[CTA_PROTO_DST_PORT
])
1354 t
->src
.u
.tcp
.port
= nla_get_be16(tb
[CTA_PROTO_SRC_PORT
]);
1355 t
->dst
.u
.tcp
.port
= nla_get_be16(tb
[CTA_PROTO_DST_PORT
]);
1359 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple
);
1361 int nf_ct_port_nlattr_tuple_size(void)
1363 return nla_policy_len(nf_ct_port_nla_policy
, CTA_PROTO_MAX
+ 1);
1365 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size
);
1368 /* Used by ipt_REJECT and ip6t_REJECT. */
1369 static void nf_conntrack_attach(struct sk_buff
*nskb
, const struct sk_buff
*skb
)
1372 enum ip_conntrack_info ctinfo
;
1374 /* This ICMP is in reverse direction to the packet which caused it */
1375 ct
= nf_ct_get(skb
, &ctinfo
);
1376 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
)
1377 ctinfo
= IP_CT_RELATED_REPLY
;
1379 ctinfo
= IP_CT_RELATED
;
1381 /* Attach to new skbuff, and increment count */
1382 nskb
->nfct
= &ct
->ct_general
;
1383 nskb
->nfctinfo
= ctinfo
;
1384 nf_conntrack_get(nskb
->nfct
);
1387 /* Bring out ya dead! */
1388 static struct nf_conn
*
1389 get_next_corpse(struct net
*net
, int (*iter
)(struct nf_conn
*i
, void *data
),
1390 void *data
, unsigned int *bucket
)
1392 struct nf_conntrack_tuple_hash
*h
;
1394 struct hlist_nulls_node
*n
;
1398 for (; *bucket
< net
->ct
.htable_size
; (*bucket
)++) {
1399 lockp
= &nf_conntrack_locks
[*bucket
% CONNTRACK_LOCKS
];
1401 nf_conntrack_lock(lockp
);
1402 if (*bucket
< net
->ct
.htable_size
) {
1403 hlist_nulls_for_each_entry(h
, n
, &net
->ct
.hash
[*bucket
], hnnode
) {
1404 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
1406 ct
= nf_ct_tuplehash_to_ctrack(h
);
1416 for_each_possible_cpu(cpu
) {
1417 struct ct_pcpu
*pcpu
= per_cpu_ptr(net
->ct
.pcpu_lists
, cpu
);
1419 spin_lock_bh(&pcpu
->lock
);
1420 hlist_nulls_for_each_entry(h
, n
, &pcpu
->unconfirmed
, hnnode
) {
1421 ct
= nf_ct_tuplehash_to_ctrack(h
);
1423 set_bit(IPS_DYING_BIT
, &ct
->status
);
1425 spin_unlock_bh(&pcpu
->lock
);
1430 atomic_inc(&ct
->ct_general
.use
);
1436 void nf_ct_iterate_cleanup(struct net
*net
,
1437 int (*iter
)(struct nf_conn
*i
, void *data
),
1438 void *data
, u32 portid
, int report
)
1441 unsigned int bucket
= 0;
1445 while ((ct
= get_next_corpse(net
, iter
, data
, &bucket
)) != NULL
) {
1446 /* Time to push up daises... */
1447 if (del_timer(&ct
->timeout
))
1448 nf_ct_delete(ct
, portid
, report
);
1450 /* ... else the timer will get him soon. */
1456 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup
);
1458 static int kill_all(struct nf_conn
*i
, void *data
)
1463 void nf_ct_free_hashtable(void *hash
, unsigned int size
)
1465 if (is_vmalloc_addr(hash
))
1468 free_pages((unsigned long)hash
,
1469 get_order(sizeof(struct hlist_head
) * size
));
1471 EXPORT_SYMBOL_GPL(nf_ct_free_hashtable
);
1473 static int untrack_refs(void)
1477 for_each_possible_cpu(cpu
) {
1478 struct nf_conn
*ct
= &per_cpu(nf_conntrack_untracked
, cpu
);
1480 cnt
+= atomic_read(&ct
->ct_general
.use
) - 1;
1485 void nf_conntrack_cleanup_start(void)
1487 RCU_INIT_POINTER(ip_ct_attach
, NULL
);
1490 void nf_conntrack_cleanup_end(void)
1492 RCU_INIT_POINTER(nf_ct_destroy
, NULL
);
1493 while (untrack_refs() > 0)
1496 #ifdef CONFIG_NF_CONNTRACK_ZONES
1497 nf_ct_extend_unregister(&nf_ct_zone_extend
);
1499 nf_conntrack_proto_fini();
1500 nf_conntrack_seqadj_fini();
1501 nf_conntrack_labels_fini();
1502 nf_conntrack_helper_fini();
1503 nf_conntrack_timeout_fini();
1504 nf_conntrack_ecache_fini();
1505 nf_conntrack_tstamp_fini();
1506 nf_conntrack_acct_fini();
1507 nf_conntrack_expect_fini();
1511 * Mishearing the voices in his head, our hero wonders how he's
1512 * supposed to kill the mall.
1514 void nf_conntrack_cleanup_net(struct net
*net
)
1518 list_add(&net
->exit_list
, &single
);
1519 nf_conntrack_cleanup_net_list(&single
);
1522 void nf_conntrack_cleanup_net_list(struct list_head
*net_exit_list
)
1528 * This makes sure all current packets have passed through
1529 * netfilter framework. Roll on, two-stage module
1535 list_for_each_entry(net
, net_exit_list
, exit_list
) {
1536 nf_ct_iterate_cleanup(net
, kill_all
, NULL
, 0, 0);
1537 if (atomic_read(&net
->ct
.count
) != 0)
1542 goto i_see_dead_people
;
1545 list_for_each_entry(net
, net_exit_list
, exit_list
) {
1546 nf_ct_free_hashtable(net
->ct
.hash
, net
->ct
.htable_size
);
1547 nf_conntrack_proto_pernet_fini(net
);
1548 nf_conntrack_helper_pernet_fini(net
);
1549 nf_conntrack_ecache_pernet_fini(net
);
1550 nf_conntrack_tstamp_pernet_fini(net
);
1551 nf_conntrack_acct_pernet_fini(net
);
1552 nf_conntrack_expect_pernet_fini(net
);
1553 kmem_cache_destroy(net
->ct
.nf_conntrack_cachep
);
1554 kfree(net
->ct
.slabname
);
1555 free_percpu(net
->ct
.stat
);
1556 free_percpu(net
->ct
.pcpu_lists
);
1560 void *nf_ct_alloc_hashtable(unsigned int *sizep
, int nulls
)
1562 struct hlist_nulls_head
*hash
;
1563 unsigned int nr_slots
, i
;
1566 BUILD_BUG_ON(sizeof(struct hlist_nulls_head
) != sizeof(struct hlist_head
));
1567 nr_slots
= *sizep
= roundup(*sizep
, PAGE_SIZE
/ sizeof(struct hlist_nulls_head
));
1568 sz
= nr_slots
* sizeof(struct hlist_nulls_head
);
1569 hash
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_NOWARN
| __GFP_ZERO
,
1575 for (i
= 0; i
< nr_slots
; i
++)
1576 INIT_HLIST_NULLS_HEAD(&hash
[i
], i
);
1580 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable
);
1582 int nf_conntrack_set_hashsize(const char *val
, struct kernel_param
*kp
)
1585 unsigned int hashsize
, old_size
;
1586 struct hlist_nulls_head
*hash
, *old_hash
;
1587 struct nf_conntrack_tuple_hash
*h
;
1590 if (current
->nsproxy
->net_ns
!= &init_net
)
1593 /* On boot, we can set this without any fancy locking. */
1594 if (!nf_conntrack_htable_size
)
1595 return param_set_uint(val
, kp
);
1597 rc
= kstrtouint(val
, 0, &hashsize
);
1603 hash
= nf_ct_alloc_hashtable(&hashsize
, 1);
1608 nf_conntrack_all_lock();
1609 write_seqcount_begin(&init_net
.ct
.generation
);
1611 /* Lookups in the old hash might happen in parallel, which means we
1612 * might get false negatives during connection lookup. New connections
1613 * created because of a false negative won't make it into the hash
1614 * though since that required taking the locks.
1617 for (i
= 0; i
< init_net
.ct
.htable_size
; i
++) {
1618 while (!hlist_nulls_empty(&init_net
.ct
.hash
[i
])) {
1619 h
= hlist_nulls_entry(init_net
.ct
.hash
[i
].first
,
1620 struct nf_conntrack_tuple_hash
, hnnode
);
1621 ct
= nf_ct_tuplehash_to_ctrack(h
);
1622 hlist_nulls_del_rcu(&h
->hnnode
);
1623 bucket
= __hash_conntrack(&h
->tuple
, hashsize
);
1624 hlist_nulls_add_head_rcu(&h
->hnnode
, &hash
[bucket
]);
1627 old_size
= init_net
.ct
.htable_size
;
1628 old_hash
= init_net
.ct
.hash
;
1630 init_net
.ct
.htable_size
= nf_conntrack_htable_size
= hashsize
;
1631 init_net
.ct
.hash
= hash
;
1633 write_seqcount_end(&init_net
.ct
.generation
);
1634 nf_conntrack_all_unlock();
1637 nf_ct_free_hashtable(old_hash
, old_size
);
1640 EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize
);
1642 module_param_call(hashsize
, nf_conntrack_set_hashsize
, param_get_uint
,
1643 &nf_conntrack_htable_size
, 0600);
1645 void nf_ct_untracked_status_or(unsigned long bits
)
1649 for_each_possible_cpu(cpu
)
1650 per_cpu(nf_conntrack_untracked
, cpu
).status
|= bits
;
1652 EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or
);
1654 int nf_conntrack_init_start(void)
1659 for (i
= 0; i
< CONNTRACK_LOCKS
; i
++)
1660 spin_lock_init(&nf_conntrack_locks
[i
]);
1662 if (!nf_conntrack_htable_size
) {
1663 /* Idea from tcp.c: use 1/16384 of memory.
1664 * On i386: 32MB machine has 512 buckets.
1665 * >= 1GB machines have 16384 buckets.
1666 * >= 4GB machines have 65536 buckets.
1668 nf_conntrack_htable_size
1669 = (((totalram_pages
<< PAGE_SHIFT
) / 16384)
1670 / sizeof(struct hlist_head
));
1671 if (totalram_pages
> (4 * (1024 * 1024 * 1024 / PAGE_SIZE
)))
1672 nf_conntrack_htable_size
= 65536;
1673 else if (totalram_pages
> (1024 * 1024 * 1024 / PAGE_SIZE
))
1674 nf_conntrack_htable_size
= 16384;
1675 if (nf_conntrack_htable_size
< 32)
1676 nf_conntrack_htable_size
= 32;
1678 /* Use a max. factor of four by default to get the same max as
1679 * with the old struct list_heads. When a table size is given
1680 * we use the old value of 8 to avoid reducing the max.
1684 nf_conntrack_max
= max_factor
* nf_conntrack_htable_size
;
1686 printk(KERN_INFO
"nf_conntrack version %s (%u buckets, %d max)\n",
1687 NF_CONNTRACK_VERSION
, nf_conntrack_htable_size
,
1690 ret
= nf_conntrack_expect_init();
1694 ret
= nf_conntrack_acct_init();
1698 ret
= nf_conntrack_tstamp_init();
1702 ret
= nf_conntrack_ecache_init();
1706 ret
= nf_conntrack_timeout_init();
1710 ret
= nf_conntrack_helper_init();
1714 ret
= nf_conntrack_labels_init();
1718 ret
= nf_conntrack_seqadj_init();
1722 #ifdef CONFIG_NF_CONNTRACK_ZONES
1723 ret
= nf_ct_extend_register(&nf_ct_zone_extend
);
1727 ret
= nf_conntrack_proto_init();
1731 /* Set up fake conntrack: to never be deleted, not in any hashes */
1732 for_each_possible_cpu(cpu
) {
1733 struct nf_conn
*ct
= &per_cpu(nf_conntrack_untracked
, cpu
);
1734 write_pnet(&ct
->ct_net
, &init_net
);
1735 atomic_set(&ct
->ct_general
.use
, 1);
1737 /* - and look it like as a confirmed connection */
1738 nf_ct_untracked_status_or(IPS_CONFIRMED
| IPS_UNTRACKED
);
1742 #ifdef CONFIG_NF_CONNTRACK_ZONES
1743 nf_ct_extend_unregister(&nf_ct_zone_extend
);
1746 nf_conntrack_seqadj_fini();
1748 nf_conntrack_labels_fini();
1750 nf_conntrack_helper_fini();
1752 nf_conntrack_timeout_fini();
1754 nf_conntrack_ecache_fini();
1756 nf_conntrack_tstamp_fini();
1758 nf_conntrack_acct_fini();
1760 nf_conntrack_expect_fini();
1765 void nf_conntrack_init_end(void)
1767 /* For use by REJECT target */
1768 RCU_INIT_POINTER(ip_ct_attach
, nf_conntrack_attach
);
1769 RCU_INIT_POINTER(nf_ct_destroy
, destroy_conntrack
);
1773 * We need to use special "null" values, not used in hash table
1775 #define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
1776 #define DYING_NULLS_VAL ((1<<30)+1)
1777 #define TEMPLATE_NULLS_VAL ((1<<30)+2)
1779 int nf_conntrack_init_net(struct net
*net
)
1784 atomic_set(&net
->ct
.count
, 0);
1785 seqcount_init(&net
->ct
.generation
);
1787 net
->ct
.pcpu_lists
= alloc_percpu(struct ct_pcpu
);
1788 if (!net
->ct
.pcpu_lists
)
1791 for_each_possible_cpu(cpu
) {
1792 struct ct_pcpu
*pcpu
= per_cpu_ptr(net
->ct
.pcpu_lists
, cpu
);
1794 spin_lock_init(&pcpu
->lock
);
1795 INIT_HLIST_NULLS_HEAD(&pcpu
->unconfirmed
, UNCONFIRMED_NULLS_VAL
);
1796 INIT_HLIST_NULLS_HEAD(&pcpu
->dying
, DYING_NULLS_VAL
);
1799 net
->ct
.stat
= alloc_percpu(struct ip_conntrack_stat
);
1801 goto err_pcpu_lists
;
1803 net
->ct
.slabname
= kasprintf(GFP_KERNEL
, "nf_conntrack_%p", net
);
1804 if (!net
->ct
.slabname
)
1807 net
->ct
.nf_conntrack_cachep
= kmem_cache_create(net
->ct
.slabname
,
1808 sizeof(struct nf_conn
), 0,
1809 SLAB_DESTROY_BY_RCU
, NULL
);
1810 if (!net
->ct
.nf_conntrack_cachep
) {
1811 printk(KERN_ERR
"Unable to create nf_conn slab cache\n");
1815 net
->ct
.htable_size
= nf_conntrack_htable_size
;
1816 net
->ct
.hash
= nf_ct_alloc_hashtable(&net
->ct
.htable_size
, 1);
1817 if (!net
->ct
.hash
) {
1818 printk(KERN_ERR
"Unable to create nf_conntrack_hash\n");
1821 ret
= nf_conntrack_expect_pernet_init(net
);
1824 ret
= nf_conntrack_acct_pernet_init(net
);
1827 ret
= nf_conntrack_tstamp_pernet_init(net
);
1830 ret
= nf_conntrack_ecache_pernet_init(net
);
1833 ret
= nf_conntrack_helper_pernet_init(net
);
1836 ret
= nf_conntrack_proto_pernet_init(net
);
1842 nf_conntrack_helper_pernet_fini(net
);
1844 nf_conntrack_ecache_pernet_fini(net
);
1846 nf_conntrack_tstamp_pernet_fini(net
);
1848 nf_conntrack_acct_pernet_fini(net
);
1850 nf_conntrack_expect_pernet_fini(net
);
1852 nf_ct_free_hashtable(net
->ct
.hash
, net
->ct
.htable_size
);
1854 kmem_cache_destroy(net
->ct
.nf_conntrack_cachep
);
1856 kfree(net
->ct
.slabname
);
1858 free_percpu(net
->ct
.stat
);
1860 free_percpu(net
->ct
.pcpu_lists
);