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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/types.h>
18 #include <linux/netfilter.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/skbuff.h>
22 #include <linux/proc_fs.h>
23 #include <linux/vmalloc.h>
24 #include <linux/stddef.h>
25 #include <linux/slab.h>
26 #include <linux/random.h>
27 #include <linux/jhash.h>
28 #include <linux/err.h>
29 #include <linux/percpu.h>
30 #include <linux/moduleparam.h>
31 #include <linux/notifier.h>
32 #include <linux/kernel.h>
33 #include <linux/netdevice.h>
34 #include <linux/socket.h>
36 #include <linux/nsproxy.h>
37 #include <linux/rculist_nulls.h>
39 #include <net/netfilter/nf_conntrack.h>
40 #include <net/netfilter/nf_conntrack_l3proto.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_expect.h>
43 #include <net/netfilter/nf_conntrack_helper.h>
44 #include <net/netfilter/nf_conntrack_seqadj.h>
45 #include <net/netfilter/nf_conntrack_core.h>
46 #include <net/netfilter/nf_conntrack_extend.h>
47 #include <net/netfilter/nf_conntrack_acct.h>
48 #include <net/netfilter/nf_conntrack_ecache.h>
49 #include <net/netfilter/nf_conntrack_zones.h>
50 #include <net/netfilter/nf_conntrack_timestamp.h>
51 #include <net/netfilter/nf_conntrack_timeout.h>
52 #include <net/netfilter/nf_conntrack_labels.h>
53 #include <net/netfilter/nf_conntrack_synproxy.h>
54 #include <net/netfilter/nf_nat.h>
55 #include <net/netfilter/nf_nat_core.h>
56 #include <net/netfilter/nf_nat_helper.h>
57 #include <net/netns/hash.h>
59 #define NF_CONNTRACK_VERSION "0.5.0"
61 int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
62 enum nf_nat_manip_type manip,
63 const struct nlattr *attr) __read_mostly;
64 EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
66 __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
67 EXPORT_SYMBOL_GPL(nf_conntrack_locks);
69 __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
70 EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
72 struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
73 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
75 static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
76 static __read_mostly seqcount_t nf_conntrack_generation;
77 static __read_mostly bool nf_conntrack_locks_all;
79 void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
82 while (unlikely(nf_conntrack_locks_all)) {
84 spin_unlock_wait(&nf_conntrack_locks_all_lock);
88 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
90 static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
92 h1 %= CONNTRACK_LOCKS;
93 h2 %= CONNTRACK_LOCKS;
94 spin_unlock(&nf_conntrack_locks[h1]);
96 spin_unlock(&nf_conntrack_locks[h2]);
99 /* return true if we need to recompute hashes (in case hash table was resized) */
100 static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
101 unsigned int h2, unsigned int sequence)
103 h1 %= CONNTRACK_LOCKS;
104 h2 %= CONNTRACK_LOCKS;
106 nf_conntrack_lock(&nf_conntrack_locks[h1]);
108 spin_lock_nested(&nf_conntrack_locks[h2],
109 SINGLE_DEPTH_NESTING);
111 nf_conntrack_lock(&nf_conntrack_locks[h2]);
112 spin_lock_nested(&nf_conntrack_locks[h1],
113 SINGLE_DEPTH_NESTING);
115 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
116 nf_conntrack_double_unlock(h1, h2);
122 static void nf_conntrack_all_lock(void)
126 spin_lock(&nf_conntrack_locks_all_lock);
127 nf_conntrack_locks_all = true;
129 for (i = 0; i < CONNTRACK_LOCKS; i++) {
130 spin_unlock_wait(&nf_conntrack_locks[i]);
134 static void nf_conntrack_all_unlock(void)
136 nf_conntrack_locks_all = false;
137 spin_unlock(&nf_conntrack_locks_all_lock);
140 unsigned int nf_conntrack_htable_size __read_mostly;
141 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
143 unsigned int nf_conntrack_max __read_mostly;
144 EXPORT_SYMBOL_GPL(nf_conntrack_max);
146 DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
147 EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
149 static unsigned int nf_conntrack_hash_rnd __read_mostly;
151 static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
152 const struct net *net)
157 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
159 /* The direction must be ignored, so we hash everything up to the
160 * destination ports (which is a multiple of 4) and treat the last
161 * three bytes manually.
163 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
164 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
165 return jhash2((u32 *)tuple, n, seed ^
166 (((__force __u16)tuple->dst.u.all << 16) |
167 tuple->dst.protonum));
170 static u32 scale_hash(u32 hash)
172 return reciprocal_scale(hash, nf_conntrack_htable_size);
175 static u32 __hash_conntrack(const struct net *net,
176 const struct nf_conntrack_tuple *tuple,
179 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
182 static u32 hash_conntrack(const struct net *net,
183 const struct nf_conntrack_tuple *tuple)
185 return scale_hash(hash_conntrack_raw(tuple, net));
189 nf_ct_get_tuple(const struct sk_buff *skb,
191 unsigned int dataoff,
195 struct nf_conntrack_tuple *tuple,
196 const struct nf_conntrack_l3proto *l3proto,
197 const struct nf_conntrack_l4proto *l4proto)
199 memset(tuple, 0, sizeof(*tuple));
201 tuple->src.l3num = l3num;
202 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
205 tuple->dst.protonum = protonum;
206 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
208 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
210 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
212 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
214 struct net *net, struct nf_conntrack_tuple *tuple)
216 struct nf_conntrack_l3proto *l3proto;
217 struct nf_conntrack_l4proto *l4proto;
218 unsigned int protoff;
224 l3proto = __nf_ct_l3proto_find(l3num);
225 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
226 if (ret != NF_ACCEPT) {
231 l4proto = __nf_ct_l4proto_find(l3num, protonum);
233 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
239 EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
242 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
243 const struct nf_conntrack_tuple *orig,
244 const struct nf_conntrack_l3proto *l3proto,
245 const struct nf_conntrack_l4proto *l4proto)
247 memset(inverse, 0, sizeof(*inverse));
249 inverse->src.l3num = orig->src.l3num;
250 if (l3proto->invert_tuple(inverse, orig) == 0)
253 inverse->dst.dir = !orig->dst.dir;
255 inverse->dst.protonum = orig->dst.protonum;
256 return l4proto->invert_tuple(inverse, orig);
258 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
261 clean_from_lists(struct nf_conn *ct)
263 pr_debug("clean_from_lists(%p)\n", ct);
264 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
265 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
267 /* Destroy all pending expectations */
268 nf_ct_remove_expectations(ct);
271 /* must be called with local_bh_disable */
272 static void nf_ct_add_to_dying_list(struct nf_conn *ct)
274 struct ct_pcpu *pcpu;
276 /* add this conntrack to the (per cpu) dying list */
277 ct->cpu = smp_processor_id();
278 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
280 spin_lock(&pcpu->lock);
281 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
283 spin_unlock(&pcpu->lock);
286 /* must be called with local_bh_disable */
287 static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
289 struct ct_pcpu *pcpu;
291 /* add this conntrack to the (per cpu) unconfirmed list */
292 ct->cpu = smp_processor_id();
293 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
295 spin_lock(&pcpu->lock);
296 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
298 spin_unlock(&pcpu->lock);
301 /* must be called with local_bh_disable */
302 static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
304 struct ct_pcpu *pcpu;
306 /* We overload first tuple to link into unconfirmed or dying list.*/
307 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
309 spin_lock(&pcpu->lock);
310 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
311 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
312 spin_unlock(&pcpu->lock);
315 /* Released via destroy_conntrack() */
316 struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
317 const struct nf_conntrack_zone *zone,
320 struct nf_conn *tmpl;
322 tmpl = kzalloc(sizeof(*tmpl), flags);
326 tmpl->status = IPS_TEMPLATE;
327 write_pnet(&tmpl->ct_net, net);
329 if (nf_ct_zone_add(tmpl, flags, zone) < 0)
332 atomic_set(&tmpl->ct_general.use, 0);
339 EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
341 void nf_ct_tmpl_free(struct nf_conn *tmpl)
343 nf_ct_ext_destroy(tmpl);
344 nf_ct_ext_free(tmpl);
347 EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
350 destroy_conntrack(struct nf_conntrack *nfct)
352 struct nf_conn *ct = (struct nf_conn *)nfct;
353 struct net *net = nf_ct_net(ct);
354 struct nf_conntrack_l4proto *l4proto;
356 pr_debug("destroy_conntrack(%p)\n", ct);
357 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
358 NF_CT_ASSERT(!timer_pending(&ct->timeout));
360 if (unlikely(nf_ct_is_template(ct))) {
365 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
366 if (l4proto->destroy)
367 l4proto->destroy(ct);
372 /* Expectations will have been removed in clean_from_lists,
373 * except TFTP can create an expectation on the first packet,
374 * before connection is in the list, so we need to clean here,
377 nf_ct_remove_expectations(ct);
379 nf_ct_del_from_dying_or_unconfirmed_list(ct);
381 NF_CT_STAT_INC(net, delete);
385 nf_ct_put(ct->master);
387 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
388 nf_conntrack_free(ct);
391 static void nf_ct_delete_from_lists(struct nf_conn *ct)
393 struct net *net = nf_ct_net(ct);
394 unsigned int hash, reply_hash;
395 unsigned int sequence;
397 nf_ct_helper_destroy(ct);
401 sequence = read_seqcount_begin(&nf_conntrack_generation);
402 hash = hash_conntrack(net,
403 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
404 reply_hash = hash_conntrack(net,
405 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
406 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
408 clean_from_lists(ct);
409 nf_conntrack_double_unlock(hash, reply_hash);
411 nf_ct_add_to_dying_list(ct);
413 NF_CT_STAT_INC(net, delete_list);
417 bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
419 struct nf_conn_tstamp *tstamp;
421 tstamp = nf_conn_tstamp_find(ct);
422 if (tstamp && tstamp->stop == 0)
423 tstamp->stop = ktime_get_real_ns();
425 if (nf_ct_is_dying(ct))
428 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
429 portid, report) < 0) {
430 /* destroy event was not delivered */
431 nf_ct_delete_from_lists(ct);
432 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
436 nf_conntrack_ecache_work(nf_ct_net(ct));
437 set_bit(IPS_DYING_BIT, &ct->status);
439 nf_ct_delete_from_lists(ct);
443 EXPORT_SYMBOL_GPL(nf_ct_delete);
445 static void death_by_timeout(unsigned long ul_conntrack)
447 nf_ct_delete((struct nf_conn *)ul_conntrack, 0, 0);
451 nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
452 const struct nf_conntrack_tuple *tuple,
453 const struct nf_conntrack_zone *zone,
454 const struct net *net)
456 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
458 /* A conntrack can be recreated with the equal tuple,
459 * so we need to check that the conntrack is confirmed
461 return nf_ct_tuple_equal(tuple, &h->tuple) &&
462 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
463 nf_ct_is_confirmed(ct) &&
464 net_eq(net, nf_ct_net(ct));
469 * - Caller must take a reference on returned object
470 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
472 static struct nf_conntrack_tuple_hash *
473 ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
474 const struct nf_conntrack_tuple *tuple, u32 hash)
476 struct nf_conntrack_tuple_hash *h;
477 struct hlist_nulls_head *ct_hash;
478 struct hlist_nulls_node *n;
479 unsigned int bucket, sequence;
483 sequence = read_seqcount_begin(&nf_conntrack_generation);
484 bucket = scale_hash(hash);
485 ct_hash = nf_conntrack_hash;
486 } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
488 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
489 if (nf_ct_key_equal(h, tuple, zone, net)) {
490 NF_CT_STAT_INC_ATOMIC(net, found);
493 NF_CT_STAT_INC_ATOMIC(net, searched);
496 * if the nulls value we got at the end of this lookup is
497 * not the expected one, we must restart lookup.
498 * We probably met an item that was moved to another chain.
500 if (get_nulls_value(n) != bucket) {
501 NF_CT_STAT_INC_ATOMIC(net, search_restart);
508 /* Find a connection corresponding to a tuple. */
509 static struct nf_conntrack_tuple_hash *
510 __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
511 const struct nf_conntrack_tuple *tuple, u32 hash)
513 struct nf_conntrack_tuple_hash *h;
518 h = ____nf_conntrack_find(net, zone, tuple, hash);
520 ct = nf_ct_tuplehash_to_ctrack(h);
521 if (unlikely(nf_ct_is_dying(ct) ||
522 !atomic_inc_not_zero(&ct->ct_general.use)))
525 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
536 struct nf_conntrack_tuple_hash *
537 nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
538 const struct nf_conntrack_tuple *tuple)
540 return __nf_conntrack_find_get(net, zone, tuple,
541 hash_conntrack_raw(tuple, net));
543 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
545 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
547 unsigned int reply_hash)
549 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
550 &nf_conntrack_hash[hash]);
551 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
552 &nf_conntrack_hash[reply_hash]);
556 nf_conntrack_hash_check_insert(struct nf_conn *ct)
558 const struct nf_conntrack_zone *zone;
559 struct net *net = nf_ct_net(ct);
560 unsigned int hash, reply_hash;
561 struct nf_conntrack_tuple_hash *h;
562 struct hlist_nulls_node *n;
563 unsigned int sequence;
565 zone = nf_ct_zone(ct);
569 sequence = read_seqcount_begin(&nf_conntrack_generation);
570 hash = hash_conntrack(net,
571 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
572 reply_hash = hash_conntrack(net,
573 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
574 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
576 /* See if there's one in the list already, including reverse */
577 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
578 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
582 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
583 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
587 add_timer(&ct->timeout);
589 /* The caller holds a reference to this object */
590 atomic_set(&ct->ct_general.use, 2);
591 __nf_conntrack_hash_insert(ct, hash, reply_hash);
592 nf_conntrack_double_unlock(hash, reply_hash);
593 NF_CT_STAT_INC(net, insert);
598 nf_conntrack_double_unlock(hash, reply_hash);
599 NF_CT_STAT_INC(net, insert_failed);
603 EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
605 static inline void nf_ct_acct_update(struct nf_conn *ct,
606 enum ip_conntrack_info ctinfo,
609 struct nf_conn_acct *acct;
611 acct = nf_conn_acct_find(ct);
613 struct nf_conn_counter *counter = acct->counter;
615 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
616 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
620 /* Confirm a connection given skb; places it in hash table */
622 __nf_conntrack_confirm(struct sk_buff *skb)
624 const struct nf_conntrack_zone *zone;
625 unsigned int hash, reply_hash;
626 struct nf_conntrack_tuple_hash *h;
628 struct nf_conn_help *help;
629 struct nf_conn_tstamp *tstamp;
630 struct hlist_nulls_node *n;
631 enum ip_conntrack_info ctinfo;
633 unsigned int sequence;
635 ct = nf_ct_get(skb, &ctinfo);
638 /* ipt_REJECT uses nf_conntrack_attach to attach related
639 ICMP/TCP RST packets in other direction. Actual packet
640 which created connection will be IP_CT_NEW or for an
641 expected connection, IP_CT_RELATED. */
642 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
645 zone = nf_ct_zone(ct);
649 sequence = read_seqcount_begin(&nf_conntrack_generation);
650 /* reuse the hash saved before */
651 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
652 hash = scale_hash(hash);
653 reply_hash = hash_conntrack(net,
654 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
656 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
658 /* We're not in hash table, and we refuse to set up related
659 * connections for unconfirmed conns. But packet copies and
660 * REJECT will give spurious warnings here.
662 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
664 /* No external references means no one else could have
667 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
668 pr_debug("Confirming conntrack %p\n", ct);
669 /* We have to check the DYING flag after unlink to prevent
670 * a race against nf_ct_get_next_corpse() possibly called from
671 * user context, else we insert an already 'dead' hash, blocking
672 * further use of that particular connection -JM.
674 nf_ct_del_from_dying_or_unconfirmed_list(ct);
676 if (unlikely(nf_ct_is_dying(ct)))
679 /* See if there's one in the list already, including reverse:
680 NAT could have grabbed it without realizing, since we're
681 not in the hash. If there is, we lost race. */
682 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
683 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
687 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
688 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
692 /* Timer relative to confirmation time, not original
693 setting time, otherwise we'd get timer wrap in
694 weird delay cases. */
695 ct->timeout.expires += jiffies;
696 add_timer(&ct->timeout);
697 atomic_inc(&ct->ct_general.use);
698 ct->status |= IPS_CONFIRMED;
700 /* set conntrack timestamp, if enabled. */
701 tstamp = nf_conn_tstamp_find(ct);
703 if (skb->tstamp.tv64 == 0)
704 __net_timestamp(skb);
706 tstamp->start = ktime_to_ns(skb->tstamp);
708 /* Since the lookup is lockless, hash insertion must be done after
709 * starting the timer and setting the CONFIRMED bit. The RCU barriers
710 * guarantee that no other CPU can find the conntrack before the above
711 * stores are visible.
713 __nf_conntrack_hash_insert(ct, hash, reply_hash);
714 nf_conntrack_double_unlock(hash, reply_hash);
715 NF_CT_STAT_INC(net, insert);
718 help = nfct_help(ct);
719 if (help && help->helper)
720 nf_conntrack_event_cache(IPCT_HELPER, ct);
722 nf_conntrack_event_cache(master_ct(ct) ?
723 IPCT_RELATED : IPCT_NEW, ct);
727 nf_ct_add_to_dying_list(ct);
728 nf_conntrack_double_unlock(hash, reply_hash);
729 NF_CT_STAT_INC(net, insert_failed);
733 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
735 /* Returns true if a connection correspondings to the tuple (required
738 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
739 const struct nf_conn *ignored_conntrack)
741 struct net *net = nf_ct_net(ignored_conntrack);
742 const struct nf_conntrack_zone *zone;
743 struct nf_conntrack_tuple_hash *h;
744 struct hlist_nulls_head *ct_hash;
745 unsigned int hash, sequence;
746 struct hlist_nulls_node *n;
749 zone = nf_ct_zone(ignored_conntrack);
753 sequence = read_seqcount_begin(&nf_conntrack_generation);
754 hash = hash_conntrack(net, tuple);
755 ct_hash = nf_conntrack_hash;
756 } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
758 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
759 ct = nf_ct_tuplehash_to_ctrack(h);
760 if (ct != ignored_conntrack &&
761 nf_ct_key_equal(h, tuple, zone, net)) {
762 NF_CT_STAT_INC_ATOMIC(net, found);
766 NF_CT_STAT_INC_ATOMIC(net, searched);
772 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
774 #define NF_CT_EVICTION_RANGE 8
776 /* There's a small race here where we may free a just-assured
777 connection. Too bad: we're in trouble anyway. */
778 static noinline int early_drop(struct net *net, unsigned int _hash)
780 /* Use oldest entry, which is roughly LRU */
781 struct nf_conntrack_tuple_hash *h;
783 struct hlist_nulls_node *n;
784 unsigned int i, hash, sequence;
785 struct nf_conn *ct = NULL;
793 sequence = read_seqcount_begin(&nf_conntrack_generation);
794 for (; i < NF_CT_EVICTION_RANGE; i++) {
795 hash = scale_hash(_hash++);
796 lockp = &nf_conntrack_locks[hash % CONNTRACK_LOCKS];
797 nf_conntrack_lock(lockp);
798 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
802 hlist_nulls_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash],
804 tmp = nf_ct_tuplehash_to_ctrack(h);
806 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
807 !net_eq(nf_ct_net(tmp), net) ||
811 if (atomic_inc_not_zero(&tmp->ct_general.use)) {
827 /* kill only if in same netns -- might have moved due to
828 * SLAB_DESTROY_BY_RCU rules
830 if (net_eq(nf_ct_net(ct), net) && del_timer(&ct->timeout)) {
831 if (nf_ct_delete(ct, 0, 0)) {
832 NF_CT_STAT_INC_ATOMIC(net, early_drop);
841 static struct nf_conn *
842 __nf_conntrack_alloc(struct net *net,
843 const struct nf_conntrack_zone *zone,
844 const struct nf_conntrack_tuple *orig,
845 const struct nf_conntrack_tuple *repl,
850 /* We don't want any race condition at early drop stage */
851 atomic_inc(&net->ct.count);
853 if (nf_conntrack_max &&
854 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
855 if (!early_drop(net, hash)) {
856 atomic_dec(&net->ct.count);
857 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
858 return ERR_PTR(-ENOMEM);
863 * Do not use kmem_cache_zalloc(), as this cache uses
864 * SLAB_DESTROY_BY_RCU.
866 ct = kmem_cache_alloc(net->ct.nf_conntrack_cachep, gfp);
870 spin_lock_init(&ct->lock);
871 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
872 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
873 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
874 /* save hash for reusing when confirming */
875 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
877 /* Don't set timer yet: wait for confirmation */
878 setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
879 write_pnet(&ct->ct_net, net);
880 memset(&ct->__nfct_init_offset[0], 0,
881 offsetof(struct nf_conn, proto) -
882 offsetof(struct nf_conn, __nfct_init_offset[0]));
884 if (zone && nf_ct_zone_add(ct, GFP_ATOMIC, zone) < 0)
887 /* Because we use RCU lookups, we set ct_general.use to zero before
888 * this is inserted in any list.
890 atomic_set(&ct->ct_general.use, 0);
893 kmem_cache_free(net->ct.nf_conntrack_cachep, ct);
895 atomic_dec(&net->ct.count);
896 return ERR_PTR(-ENOMEM);
899 struct nf_conn *nf_conntrack_alloc(struct net *net,
900 const struct nf_conntrack_zone *zone,
901 const struct nf_conntrack_tuple *orig,
902 const struct nf_conntrack_tuple *repl,
905 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
907 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
909 void nf_conntrack_free(struct nf_conn *ct)
911 struct net *net = nf_ct_net(ct);
913 /* A freed object has refcnt == 0, that's
914 * the golden rule for SLAB_DESTROY_BY_RCU
916 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
918 nf_ct_ext_destroy(ct);
920 kmem_cache_free(net->ct.nf_conntrack_cachep, ct);
921 smp_mb__before_atomic();
922 atomic_dec(&net->ct.count);
924 EXPORT_SYMBOL_GPL(nf_conntrack_free);
927 /* Allocate a new conntrack: we return -ENOMEM if classification
928 failed due to stress. Otherwise it really is unclassifiable. */
929 static struct nf_conntrack_tuple_hash *
930 init_conntrack(struct net *net, struct nf_conn *tmpl,
931 const struct nf_conntrack_tuple *tuple,
932 struct nf_conntrack_l3proto *l3proto,
933 struct nf_conntrack_l4proto *l4proto,
935 unsigned int dataoff, u32 hash)
938 struct nf_conn_help *help;
939 struct nf_conntrack_tuple repl_tuple;
940 struct nf_conntrack_ecache *ecache;
941 struct nf_conntrack_expect *exp = NULL;
942 const struct nf_conntrack_zone *zone;
943 struct nf_conn_timeout *timeout_ext;
944 struct nf_conntrack_zone tmp;
945 unsigned int *timeouts;
947 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
948 pr_debug("Can't invert tuple.\n");
952 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
953 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
956 return (struct nf_conntrack_tuple_hash *)ct;
958 if (tmpl && nfct_synproxy(tmpl)) {
959 nfct_seqadj_ext_add(ct);
960 nfct_synproxy_ext_add(ct);
963 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
965 timeouts = nf_ct_timeout_data(timeout_ext);
966 if (unlikely(!timeouts))
967 timeouts = l4proto->get_timeouts(net);
969 timeouts = l4proto->get_timeouts(net);
972 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
973 nf_conntrack_free(ct);
974 pr_debug("can't track with proto module\n");
979 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
982 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
983 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
984 nf_ct_labels_ext_add(ct);
986 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
987 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
988 ecache ? ecache->expmask : 0,
992 if (net->ct.expect_count) {
993 spin_lock(&nf_conntrack_expect_lock);
994 exp = nf_ct_find_expectation(net, zone, tuple);
996 pr_debug("expectation arrives ct=%p exp=%p\n",
998 /* Welcome, Mr. Bond. We've been expecting you... */
999 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1000 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1001 ct->master = exp->master;
1003 help = nf_ct_helper_ext_add(ct, exp->helper,
1006 rcu_assign_pointer(help->helper, exp->helper);
1009 #ifdef CONFIG_NF_CONNTRACK_MARK
1010 ct->mark = exp->master->mark;
1012 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1013 ct->secmark = exp->master->secmark;
1015 NF_CT_STAT_INC(net, expect_new);
1017 spin_unlock(&nf_conntrack_expect_lock);
1020 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
1021 NF_CT_STAT_INC(net, new);
1024 /* Now it is inserted into the unconfirmed list, bump refcount */
1025 nf_conntrack_get(&ct->ct_general);
1026 nf_ct_add_to_unconfirmed_list(ct);
1032 exp->expectfn(ct, exp);
1033 nf_ct_expect_put(exp);
1036 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
1039 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1040 static inline struct nf_conn *
1041 resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
1042 struct sk_buff *skb,
1043 unsigned int dataoff,
1046 struct nf_conntrack_l3proto *l3proto,
1047 struct nf_conntrack_l4proto *l4proto,
1049 enum ip_conntrack_info *ctinfo)
1051 const struct nf_conntrack_zone *zone;
1052 struct nf_conntrack_tuple tuple;
1053 struct nf_conntrack_tuple_hash *h;
1054 struct nf_conntrack_zone tmp;
1058 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
1059 dataoff, l3num, protonum, net, &tuple, l3proto,
1061 pr_debug("Can't get tuple\n");
1065 /* look for tuple match */
1066 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1067 hash = hash_conntrack_raw(&tuple, net);
1068 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
1070 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
1071 skb, dataoff, hash);
1077 ct = nf_ct_tuplehash_to_ctrack(h);
1079 /* It exists; we have (non-exclusive) reference. */
1080 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
1081 *ctinfo = IP_CT_ESTABLISHED_REPLY;
1082 /* Please set reply bit if this packet OK */
1085 /* Once we've had two way comms, always ESTABLISHED. */
1086 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1087 pr_debug("normal packet for %p\n", ct);
1088 *ctinfo = IP_CT_ESTABLISHED;
1089 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
1090 pr_debug("related packet for %p\n", ct);
1091 *ctinfo = IP_CT_RELATED;
1093 pr_debug("new packet for %p\n", ct);
1094 *ctinfo = IP_CT_NEW;
1098 skb->nfct = &ct->ct_general;
1099 skb->nfctinfo = *ctinfo;
1104 nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1105 struct sk_buff *skb)
1107 struct nf_conn *ct, *tmpl = NULL;
1108 enum ip_conntrack_info ctinfo;
1109 struct nf_conntrack_l3proto *l3proto;
1110 struct nf_conntrack_l4proto *l4proto;
1111 unsigned int *timeouts;
1112 unsigned int dataoff;
1118 /* Previously seen (loopback or untracked)? Ignore. */
1119 tmpl = (struct nf_conn *)skb->nfct;
1120 if (!nf_ct_is_template(tmpl)) {
1121 NF_CT_STAT_INC_ATOMIC(net, ignore);
1127 /* rcu_read_lock()ed by nf_hook_slow */
1128 l3proto = __nf_ct_l3proto_find(pf);
1129 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
1130 &dataoff, &protonum);
1132 pr_debug("not prepared to track yet or error occurred\n");
1133 NF_CT_STAT_INC_ATOMIC(net, error);
1134 NF_CT_STAT_INC_ATOMIC(net, invalid);
1139 l4proto = __nf_ct_l4proto_find(pf, protonum);
1141 /* It may be an special packet, error, unclean...
1142 * inverse of the return code tells to the netfilter
1143 * core what to do with the packet. */
1144 if (l4proto->error != NULL) {
1145 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1148 NF_CT_STAT_INC_ATOMIC(net, error);
1149 NF_CT_STAT_INC_ATOMIC(net, invalid);
1153 /* ICMP[v6] protocol trackers may assign one conntrack. */
1158 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
1159 l3proto, l4proto, &set_reply, &ctinfo);
1161 /* Not valid part of a connection */
1162 NF_CT_STAT_INC_ATOMIC(net, invalid);
1168 /* Too stressed to deal. */
1169 NF_CT_STAT_INC_ATOMIC(net, drop);
1174 NF_CT_ASSERT(skb->nfct);
1176 /* Decide what timeout policy we want to apply to this flow. */
1177 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
1179 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
1181 /* Invalid: inverse of the return code tells
1182 * the netfilter core what to do */
1183 pr_debug("nf_conntrack_in: Can't track with proto module\n");
1184 nf_conntrack_put(skb->nfct);
1186 NF_CT_STAT_INC_ATOMIC(net, invalid);
1187 if (ret == -NF_DROP)
1188 NF_CT_STAT_INC_ATOMIC(net, drop);
1193 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
1194 nf_conntrack_event_cache(IPCT_REPLY, ct);
1197 /* Special case: we have to repeat this hook, assign the
1198 * template again to this packet. We assume that this packet
1199 * has no conntrack assigned. This is used by nf_ct_tcp. */
1200 if (ret == NF_REPEAT)
1201 skb->nfct = (struct nf_conntrack *)tmpl;
1208 EXPORT_SYMBOL_GPL(nf_conntrack_in);
1210 bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1211 const struct nf_conntrack_tuple *orig)
1216 ret = nf_ct_invert_tuple(inverse, orig,
1217 __nf_ct_l3proto_find(orig->src.l3num),
1218 __nf_ct_l4proto_find(orig->src.l3num,
1219 orig->dst.protonum));
1223 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
1225 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
1226 implicitly racy: see __nf_conntrack_confirm */
1227 void nf_conntrack_alter_reply(struct nf_conn *ct,
1228 const struct nf_conntrack_tuple *newreply)
1230 struct nf_conn_help *help = nfct_help(ct);
1232 /* Should be unconfirmed, so not in hash table yet */
1233 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1235 pr_debug("Altering reply tuple of %p to ", ct);
1236 nf_ct_dump_tuple(newreply);
1238 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
1239 if (ct->master || (help && !hlist_empty(&help->expectations)))
1243 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1246 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
1248 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1249 void __nf_ct_refresh_acct(struct nf_conn *ct,
1250 enum ip_conntrack_info ctinfo,
1251 const struct sk_buff *skb,
1252 unsigned long extra_jiffies,
1255 NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
1258 /* Only update if this is not a fixed timeout */
1259 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1262 /* If not in hash table, timer will not be active yet */
1263 if (!nf_ct_is_confirmed(ct)) {
1264 ct->timeout.expires = extra_jiffies;
1266 unsigned long newtime = jiffies + extra_jiffies;
1268 /* Only update the timeout if the new timeout is at least
1269 HZ jiffies from the old timeout. Need del_timer for race
1270 avoidance (may already be dying). */
1271 if (newtime - ct->timeout.expires >= HZ)
1272 mod_timer_pending(&ct->timeout, newtime);
1277 nf_ct_acct_update(ct, ctinfo, skb->len);
1279 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
1281 bool __nf_ct_kill_acct(struct nf_conn *ct,
1282 enum ip_conntrack_info ctinfo,
1283 const struct sk_buff *skb,
1287 nf_ct_acct_update(ct, ctinfo, skb->len);
1289 if (del_timer(&ct->timeout)) {
1290 ct->timeout.function((unsigned long)ct);
1295 EXPORT_SYMBOL_GPL(__nf_ct_kill_acct);
1297 #ifdef CONFIG_NF_CONNTRACK_ZONES
1298 static struct nf_ct_ext_type nf_ct_zone_extend __read_mostly = {
1299 .len = sizeof(struct nf_conntrack_zone),
1300 .align = __alignof__(struct nf_conntrack_zone),
1301 .id = NF_CT_EXT_ZONE,
1305 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1307 #include <linux/netfilter/nfnetlink.h>
1308 #include <linux/netfilter/nfnetlink_conntrack.h>
1309 #include <linux/mutex.h>
1311 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1312 * in ip_conntrack_core, since we don't want the protocols to autoload
1313 * or depend on ctnetlink */
1314 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
1315 const struct nf_conntrack_tuple *tuple)
1317 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1318 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1319 goto nla_put_failure;
1325 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
1327 const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1328 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1329 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
1331 EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
1333 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
1334 struct nf_conntrack_tuple *t)
1336 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
1339 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1340 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
1344 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
1346 int nf_ct_port_nlattr_tuple_size(void)
1348 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1350 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
1353 /* Used by ipt_REJECT and ip6t_REJECT. */
1354 static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
1357 enum ip_conntrack_info ctinfo;
1359 /* This ICMP is in reverse direction to the packet which caused it */
1360 ct = nf_ct_get(skb, &ctinfo);
1361 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1362 ctinfo = IP_CT_RELATED_REPLY;
1364 ctinfo = IP_CT_RELATED;
1366 /* Attach to new skbuff, and increment count */
1367 nskb->nfct = &ct->ct_general;
1368 nskb->nfctinfo = ctinfo;
1369 nf_conntrack_get(nskb->nfct);
1372 /* Bring out ya dead! */
1373 static struct nf_conn *
1374 get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
1375 void *data, unsigned int *bucket)
1377 struct nf_conntrack_tuple_hash *h;
1379 struct hlist_nulls_node *n;
1383 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
1384 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1386 nf_conntrack_lock(lockp);
1387 if (*bucket < nf_conntrack_htable_size) {
1388 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
1389 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1391 ct = nf_ct_tuplehash_to_ctrack(h);
1392 if (net_eq(nf_ct_net(ct), net) &&
1402 for_each_possible_cpu(cpu) {
1403 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1405 spin_lock_bh(&pcpu->lock);
1406 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1407 ct = nf_ct_tuplehash_to_ctrack(h);
1409 set_bit(IPS_DYING_BIT, &ct->status);
1411 spin_unlock_bh(&pcpu->lock);
1416 atomic_inc(&ct->ct_general.use);
1422 void nf_ct_iterate_cleanup(struct net *net,
1423 int (*iter)(struct nf_conn *i, void *data),
1424 void *data, u32 portid, int report)
1427 unsigned int bucket = 0;
1431 if (atomic_read(&net->ct.count) == 0)
1434 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
1435 /* Time to push up daises... */
1436 if (del_timer(&ct->timeout))
1437 nf_ct_delete(ct, portid, report);
1439 /* ... else the timer will get him soon. */
1445 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
1447 static int kill_all(struct nf_conn *i, void *data)
1452 void nf_ct_free_hashtable(void *hash, unsigned int size)
1454 if (is_vmalloc_addr(hash))
1457 free_pages((unsigned long)hash,
1458 get_order(sizeof(struct hlist_head) * size));
1460 EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
1462 static int untrack_refs(void)
1466 for_each_possible_cpu(cpu) {
1467 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1469 cnt += atomic_read(&ct->ct_general.use) - 1;
1474 void nf_conntrack_cleanup_start(void)
1476 RCU_INIT_POINTER(ip_ct_attach, NULL);
1479 void nf_conntrack_cleanup_end(void)
1481 RCU_INIT_POINTER(nf_ct_destroy, NULL);
1482 while (untrack_refs() > 0)
1485 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1487 #ifdef CONFIG_NF_CONNTRACK_ZONES
1488 nf_ct_extend_unregister(&nf_ct_zone_extend);
1490 nf_conntrack_proto_fini();
1491 nf_conntrack_seqadj_fini();
1492 nf_conntrack_labels_fini();
1493 nf_conntrack_helper_fini();
1494 nf_conntrack_timeout_fini();
1495 nf_conntrack_ecache_fini();
1496 nf_conntrack_tstamp_fini();
1497 nf_conntrack_acct_fini();
1498 nf_conntrack_expect_fini();
1502 * Mishearing the voices in his head, our hero wonders how he's
1503 * supposed to kill the mall.
1505 void nf_conntrack_cleanup_net(struct net *net)
1509 list_add(&net->exit_list, &single);
1510 nf_conntrack_cleanup_net_list(&single);
1513 void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1519 * This makes sure all current packets have passed through
1520 * netfilter framework. Roll on, two-stage module
1526 list_for_each_entry(net, net_exit_list, exit_list) {
1527 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
1528 if (atomic_read(&net->ct.count) != 0)
1533 goto i_see_dead_people;
1536 list_for_each_entry(net, net_exit_list, exit_list) {
1537 nf_conntrack_proto_pernet_fini(net);
1538 nf_conntrack_helper_pernet_fini(net);
1539 nf_conntrack_ecache_pernet_fini(net);
1540 nf_conntrack_tstamp_pernet_fini(net);
1541 nf_conntrack_acct_pernet_fini(net);
1542 nf_conntrack_expect_pernet_fini(net);
1543 kmem_cache_destroy(net->ct.nf_conntrack_cachep);
1544 kfree(net->ct.slabname);
1545 free_percpu(net->ct.stat);
1546 free_percpu(net->ct.pcpu_lists);
1550 void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
1552 struct hlist_nulls_head *hash;
1553 unsigned int nr_slots, i;
1556 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1557 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
1558 sz = nr_slots * sizeof(struct hlist_nulls_head);
1559 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1565 for (i = 0; i < nr_slots; i++)
1566 INIT_HLIST_NULLS_HEAD(&hash[i], i);
1570 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
1572 int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1575 unsigned int hashsize, old_size;
1576 struct hlist_nulls_head *hash, *old_hash;
1577 struct nf_conntrack_tuple_hash *h;
1580 if (current->nsproxy->net_ns != &init_net)
1583 /* On boot, we can set this without any fancy locking. */
1584 if (!nf_conntrack_htable_size)
1585 return param_set_uint(val, kp);
1587 rc = kstrtouint(val, 0, &hashsize);
1593 hash = nf_ct_alloc_hashtable(&hashsize, 1);
1598 nf_conntrack_all_lock();
1599 write_seqcount_begin(&nf_conntrack_generation);
1601 /* Lookups in the old hash might happen in parallel, which means we
1602 * might get false negatives during connection lookup. New connections
1603 * created because of a false negative won't make it into the hash
1604 * though since that required taking the locks.
1607 for (i = 0; i < nf_conntrack_htable_size; i++) {
1608 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1609 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1610 struct nf_conntrack_tuple_hash, hnnode);
1611 ct = nf_ct_tuplehash_to_ctrack(h);
1612 hlist_nulls_del_rcu(&h->hnnode);
1613 bucket = __hash_conntrack(nf_ct_net(ct),
1614 &h->tuple, hashsize);
1615 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
1618 old_size = nf_conntrack_htable_size;
1619 old_hash = nf_conntrack_hash;
1621 nf_conntrack_hash = hash;
1622 nf_conntrack_htable_size = hashsize;
1624 write_seqcount_end(&nf_conntrack_generation);
1625 nf_conntrack_all_unlock();
1629 nf_ct_free_hashtable(old_hash, old_size);
1632 EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
1634 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
1635 &nf_conntrack_htable_size, 0600);
1637 void nf_ct_untracked_status_or(unsigned long bits)
1641 for_each_possible_cpu(cpu)
1642 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
1644 EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1646 int nf_conntrack_init_start(void)
1651 seqcount_init(&nf_conntrack_generation);
1653 for (i = 0; i < CONNTRACK_LOCKS; i++)
1654 spin_lock_init(&nf_conntrack_locks[i]);
1656 if (!nf_conntrack_htable_size) {
1657 /* Idea from tcp.c: use 1/16384 of memory.
1658 * On i386: 32MB machine has 512 buckets.
1659 * >= 1GB machines have 16384 buckets.
1660 * >= 4GB machines have 65536 buckets.
1662 nf_conntrack_htable_size
1663 = (((totalram_pages << PAGE_SHIFT) / 16384)
1664 / sizeof(struct hlist_head));
1665 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1666 nf_conntrack_htable_size = 65536;
1667 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
1668 nf_conntrack_htable_size = 16384;
1669 if (nf_conntrack_htable_size < 32)
1670 nf_conntrack_htable_size = 32;
1672 /* Use a max. factor of four by default to get the same max as
1673 * with the old struct list_heads. When a table size is given
1674 * we use the old value of 8 to avoid reducing the max.
1679 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1680 if (!nf_conntrack_hash)
1683 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
1685 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
1686 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1689 ret = nf_conntrack_expect_init();
1693 ret = nf_conntrack_acct_init();
1697 ret = nf_conntrack_tstamp_init();
1701 ret = nf_conntrack_ecache_init();
1705 ret = nf_conntrack_timeout_init();
1709 ret = nf_conntrack_helper_init();
1713 ret = nf_conntrack_labels_init();
1717 ret = nf_conntrack_seqadj_init();
1721 #ifdef CONFIG_NF_CONNTRACK_ZONES
1722 ret = nf_ct_extend_register(&nf_ct_zone_extend);
1726 ret = nf_conntrack_proto_init();
1730 /* Set up fake conntrack: to never be deleted, not in any hashes */
1731 for_each_possible_cpu(cpu) {
1732 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1733 write_pnet(&ct->ct_net, &init_net);
1734 atomic_set(&ct->ct_general.use, 1);
1736 /* - and look it like as a confirmed connection */
1737 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
1741 #ifdef CONFIG_NF_CONNTRACK_ZONES
1742 nf_ct_extend_unregister(&nf_ct_zone_extend);
1745 nf_conntrack_seqadj_fini();
1747 nf_conntrack_labels_fini();
1749 nf_conntrack_helper_fini();
1751 nf_conntrack_timeout_fini();
1753 nf_conntrack_ecache_fini();
1755 nf_conntrack_tstamp_fini();
1757 nf_conntrack_acct_fini();
1759 nf_conntrack_expect_fini();
1761 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
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);
1786 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
1787 if (!net->ct.pcpu_lists)
1790 for_each_possible_cpu(cpu) {
1791 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1793 spin_lock_init(&pcpu->lock);
1794 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
1795 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
1798 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
1800 goto err_pcpu_lists;
1802 net->ct.slabname = kasprintf(GFP_KERNEL, "nf_conntrack_%p", net);
1803 if (!net->ct.slabname)
1806 net->ct.nf_conntrack_cachep = kmem_cache_create(net->ct.slabname,
1807 sizeof(struct nf_conn), 0,
1808 SLAB_DESTROY_BY_RCU, NULL);
1809 if (!net->ct.nf_conntrack_cachep) {
1810 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1814 ret = nf_conntrack_expect_pernet_init(net);
1817 ret = nf_conntrack_acct_pernet_init(net);
1820 ret = nf_conntrack_tstamp_pernet_init(net);
1823 ret = nf_conntrack_ecache_pernet_init(net);
1826 ret = nf_conntrack_helper_pernet_init(net);
1829 ret = nf_conntrack_proto_pernet_init(net);
1835 nf_conntrack_helper_pernet_fini(net);
1837 nf_conntrack_ecache_pernet_fini(net);
1839 nf_conntrack_tstamp_pernet_fini(net);
1841 nf_conntrack_acct_pernet_fini(net);
1843 nf_conntrack_expect_pernet_fini(net);
1845 kmem_cache_destroy(net->ct.nf_conntrack_cachep);
1847 kfree(net->ct.slabname);
1849 free_percpu(net->ct.stat);
1851 free_percpu(net->ct.pcpu_lists);