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/siphash.h>
29 #include <linux/err.h>
30 #include <linux/percpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/notifier.h>
33 #include <linux/kernel.h>
34 #include <linux/netdevice.h>
35 #include <linux/socket.h>
37 #include <linux/nsproxy.h>
38 #include <linux/rculist_nulls.h>
40 #include <net/netfilter/nf_conntrack.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>
60 #include "nf_internals.h"
62 __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
63 EXPORT_SYMBOL_GPL(nf_conntrack_locks);
65 __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
66 EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
68 struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
69 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
71 struct conntrack_gc_work {
72 struct delayed_work dwork;
78 static __read_mostly struct kmem_cache *nf_conntrack_cachep;
79 static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
80 static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
81 static __read_mostly bool nf_conntrack_locks_all;
83 #define GC_SCAN_INTERVAL (120u * HZ)
84 #define GC_SCAN_MAX_DURATION msecs_to_jiffies(10)
86 static struct conntrack_gc_work conntrack_gc_work;
88 void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
90 /* 1) Acquire the lock */
93 /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics
94 * It pairs with the smp_store_release() in nf_conntrack_all_unlock()
96 if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
99 /* fast path failed, unlock */
102 /* Slow path 1) get global lock */
103 spin_lock(&nf_conntrack_locks_all_lock);
105 /* Slow path 2) get the lock we want */
108 /* Slow path 3) release the global lock */
109 spin_unlock(&nf_conntrack_locks_all_lock);
111 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
113 static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
115 h1 %= CONNTRACK_LOCKS;
116 h2 %= CONNTRACK_LOCKS;
117 spin_unlock(&nf_conntrack_locks[h1]);
119 spin_unlock(&nf_conntrack_locks[h2]);
122 /* return true if we need to recompute hashes (in case hash table was resized) */
123 static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
124 unsigned int h2, unsigned int sequence)
126 h1 %= CONNTRACK_LOCKS;
127 h2 %= CONNTRACK_LOCKS;
129 nf_conntrack_lock(&nf_conntrack_locks[h1]);
131 spin_lock_nested(&nf_conntrack_locks[h2],
132 SINGLE_DEPTH_NESTING);
134 nf_conntrack_lock(&nf_conntrack_locks[h2]);
135 spin_lock_nested(&nf_conntrack_locks[h1],
136 SINGLE_DEPTH_NESTING);
138 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
139 nf_conntrack_double_unlock(h1, h2);
145 static void nf_conntrack_all_lock(void)
149 spin_lock(&nf_conntrack_locks_all_lock);
151 nf_conntrack_locks_all = true;
153 for (i = 0; i < CONNTRACK_LOCKS; i++) {
154 spin_lock(&nf_conntrack_locks[i]);
156 /* This spin_unlock provides the "release" to ensure that
157 * nf_conntrack_locks_all==true is visible to everyone that
158 * acquired spin_lock(&nf_conntrack_locks[]).
160 spin_unlock(&nf_conntrack_locks[i]);
164 static void nf_conntrack_all_unlock(void)
166 /* All prior stores must be complete before we clear
167 * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock()
168 * might observe the false value but not the entire
170 * It pairs with the smp_load_acquire() in nf_conntrack_lock()
172 smp_store_release(&nf_conntrack_locks_all, false);
173 spin_unlock(&nf_conntrack_locks_all_lock);
176 unsigned int nf_conntrack_htable_size __read_mostly;
177 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
179 unsigned int nf_conntrack_max __read_mostly;
180 EXPORT_SYMBOL_GPL(nf_conntrack_max);
181 seqcount_t nf_conntrack_generation __read_mostly;
182 static unsigned int nf_conntrack_hash_rnd __read_mostly;
184 static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
185 const struct net *net)
190 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
192 /* The direction must be ignored, so we hash everything up to the
193 * destination ports (which is a multiple of 4) and treat the last
194 * three bytes manually.
196 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
197 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
198 return jhash2((u32 *)tuple, n, seed ^
199 (((__force __u16)tuple->dst.u.all << 16) |
200 tuple->dst.protonum));
203 static u32 scale_hash(u32 hash)
205 return reciprocal_scale(hash, nf_conntrack_htable_size);
208 static u32 __hash_conntrack(const struct net *net,
209 const struct nf_conntrack_tuple *tuple,
212 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
215 static u32 hash_conntrack(const struct net *net,
216 const struct nf_conntrack_tuple *tuple)
218 return scale_hash(hash_conntrack_raw(tuple, net));
222 nf_ct_get_tuple(const struct sk_buff *skb,
224 unsigned int dataoff,
228 struct nf_conntrack_tuple *tuple,
229 const struct nf_conntrack_l4proto *l4proto)
237 } _inet_hdr, *inet_hdr;
239 memset(tuple, 0, sizeof(*tuple));
241 tuple->src.l3num = l3num;
244 nhoff += offsetof(struct iphdr, saddr);
245 size = 2 * sizeof(__be32);
248 nhoff += offsetof(struct ipv6hdr, saddr);
249 size = sizeof(_addrs);
255 ap = skb_header_pointer(skb, nhoff, size, _addrs);
261 tuple->src.u3.ip = ap[0];
262 tuple->dst.u3.ip = ap[1];
265 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
266 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
270 tuple->dst.protonum = protonum;
271 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
273 if (unlikely(l4proto->pkt_to_tuple))
274 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
276 /* Actually only need first 4 bytes to get ports. */
277 inet_hdr = skb_header_pointer(skb, dataoff, sizeof(_inet_hdr), &_inet_hdr);
281 tuple->src.u.udp.port = inet_hdr->sport;
282 tuple->dst.u.udp.port = inet_hdr->dport;
286 static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
290 const struct iphdr *iph;
293 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
297 /* Conntrack defragments packets, we might still see fragments
298 * inside ICMP packets though.
300 if (iph->frag_off & htons(IP_OFFSET))
303 dataoff = nhoff + (iph->ihl << 2);
304 *protonum = iph->protocol;
306 /* Check bogus IP headers */
307 if (dataoff > skb->len) {
308 pr_debug("bogus IPv4 packet: nhoff %u, ihl %u, skblen %u\n",
309 nhoff, iph->ihl << 2, skb->len);
315 #if IS_ENABLED(CONFIG_IPV6)
316 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
320 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
324 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
325 &nexthdr, sizeof(nexthdr)) != 0) {
326 pr_debug("can't get nexthdr\n");
329 protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off);
331 * (protoff == skb->len) means the packet has not data, just
332 * IPv6 and possibly extensions headers, but it is tracked anyway
334 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
335 pr_debug("can't find proto in pkt\n");
344 static int get_l4proto(const struct sk_buff *skb,
345 unsigned int nhoff, u8 pf, u8 *l4num)
349 return ipv4_get_l4proto(skb, nhoff, l4num);
350 #if IS_ENABLED(CONFIG_IPV6)
352 return ipv6_get_l4proto(skb, nhoff, l4num);
361 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
363 struct net *net, struct nf_conntrack_tuple *tuple)
365 const struct nf_conntrack_l4proto *l4proto;
372 protoff = get_l4proto(skb, nhoff, l3num, &protonum);
378 l4proto = __nf_ct_l4proto_find(l3num, protonum);
380 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
386 EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
389 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
390 const struct nf_conntrack_tuple *orig,
391 const struct nf_conntrack_l4proto *l4proto)
393 memset(inverse, 0, sizeof(*inverse));
395 inverse->src.l3num = orig->src.l3num;
397 switch (orig->src.l3num) {
399 inverse->src.u3.ip = orig->dst.u3.ip;
400 inverse->dst.u3.ip = orig->src.u3.ip;
403 inverse->src.u3.in6 = orig->dst.u3.in6;
404 inverse->dst.u3.in6 = orig->src.u3.in6;
410 inverse->dst.dir = !orig->dst.dir;
412 inverse->dst.protonum = orig->dst.protonum;
414 if (unlikely(l4proto->invert_tuple))
415 return l4proto->invert_tuple(inverse, orig);
417 inverse->src.u.all = orig->dst.u.all;
418 inverse->dst.u.all = orig->src.u.all;
421 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
423 /* Generate a almost-unique pseudo-id for a given conntrack.
425 * intentionally doesn't re-use any of the seeds used for hash
426 * table location, we assume id gets exposed to userspace.
428 * Following nf_conn items do not change throughout lifetime
432 * 2. nf_conn->master address (normally NULL)
433 * 3. the associated net namespace
434 * 4. the original direction tuple
436 u32 nf_ct_get_id(const struct nf_conn *ct)
438 static __read_mostly siphash_key_t ct_id_seed;
439 unsigned long a, b, c, d;
441 net_get_random_once(&ct_id_seed, sizeof(ct_id_seed));
443 a = (unsigned long)ct;
444 b = (unsigned long)ct->master;
445 c = (unsigned long)nf_ct_net(ct);
446 d = (unsigned long)siphash(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
447 sizeof(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple),
450 return siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &ct_id_seed);
452 return siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &ct_id_seed);
455 EXPORT_SYMBOL_GPL(nf_ct_get_id);
458 clean_from_lists(struct nf_conn *ct)
460 pr_debug("clean_from_lists(%p)\n", ct);
461 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
462 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
464 /* Destroy all pending expectations */
465 nf_ct_remove_expectations(ct);
468 /* must be called with local_bh_disable */
469 static void nf_ct_add_to_dying_list(struct nf_conn *ct)
471 struct ct_pcpu *pcpu;
473 /* add this conntrack to the (per cpu) dying list */
474 ct->cpu = smp_processor_id();
475 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
477 spin_lock(&pcpu->lock);
478 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
480 spin_unlock(&pcpu->lock);
483 /* must be called with local_bh_disable */
484 static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
486 struct ct_pcpu *pcpu;
488 /* add this conntrack to the (per cpu) unconfirmed list */
489 ct->cpu = smp_processor_id();
490 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
492 spin_lock(&pcpu->lock);
493 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
495 spin_unlock(&pcpu->lock);
498 /* must be called with local_bh_disable */
499 static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
501 struct ct_pcpu *pcpu;
503 /* We overload first tuple to link into unconfirmed or dying list.*/
504 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
506 spin_lock(&pcpu->lock);
507 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
508 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
509 spin_unlock(&pcpu->lock);
512 #define NFCT_ALIGN(len) (((len) + NFCT_INFOMASK) & ~NFCT_INFOMASK)
514 /* Released via destroy_conntrack() */
515 struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
516 const struct nf_conntrack_zone *zone,
519 struct nf_conn *tmpl, *p;
521 if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK) {
522 tmpl = kzalloc(sizeof(*tmpl) + NFCT_INFOMASK, flags);
527 tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p);
529 tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p);
530 tmpl->proto.tmpl_padto = (char *)tmpl - (char *)p;
533 tmpl = kzalloc(sizeof(*tmpl), flags);
538 tmpl->status = IPS_TEMPLATE;
539 write_pnet(&tmpl->ct_net, net);
540 nf_ct_zone_add(tmpl, zone);
541 atomic_set(&tmpl->ct_general.use, 0);
545 EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
547 void nf_ct_tmpl_free(struct nf_conn *tmpl)
549 nf_ct_ext_destroy(tmpl);
550 nf_ct_ext_free(tmpl);
552 if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK)
553 kfree((char *)tmpl - tmpl->proto.tmpl_padto);
557 EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
560 destroy_conntrack(struct nf_conntrack *nfct)
562 struct nf_conn *ct = (struct nf_conn *)nfct;
563 const struct nf_conntrack_l4proto *l4proto;
565 pr_debug("destroy_conntrack(%p)\n", ct);
566 WARN_ON(atomic_read(&nfct->use) != 0);
568 if (unlikely(nf_ct_is_template(ct))) {
572 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
573 if (l4proto->destroy)
574 l4proto->destroy(ct);
577 /* Expectations will have been removed in clean_from_lists,
578 * except TFTP can create an expectation on the first packet,
579 * before connection is in the list, so we need to clean here,
582 nf_ct_remove_expectations(ct);
584 nf_ct_del_from_dying_or_unconfirmed_list(ct);
589 nf_ct_put(ct->master);
591 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
592 nf_conntrack_free(ct);
595 static void nf_ct_delete_from_lists(struct nf_conn *ct)
597 struct net *net = nf_ct_net(ct);
598 unsigned int hash, reply_hash;
599 unsigned int sequence;
601 nf_ct_helper_destroy(ct);
605 sequence = read_seqcount_begin(&nf_conntrack_generation);
606 hash = hash_conntrack(net,
607 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
608 reply_hash = hash_conntrack(net,
609 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
610 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
612 clean_from_lists(ct);
613 nf_conntrack_double_unlock(hash, reply_hash);
615 nf_ct_add_to_dying_list(ct);
620 bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
622 struct nf_conn_tstamp *tstamp;
624 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
627 tstamp = nf_conn_tstamp_find(ct);
629 s32 timeout = ct->timeout - nfct_time_stamp;
631 tstamp->stop = ktime_get_real_ns();
633 tstamp->stop -= jiffies_to_nsecs(-timeout);
636 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
637 portid, report) < 0) {
638 /* destroy event was not delivered. nf_ct_put will
639 * be done by event cache worker on redelivery.
641 nf_ct_delete_from_lists(ct);
642 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
646 nf_conntrack_ecache_work(nf_ct_net(ct));
647 nf_ct_delete_from_lists(ct);
651 EXPORT_SYMBOL_GPL(nf_ct_delete);
654 nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
655 const struct nf_conntrack_tuple *tuple,
656 const struct nf_conntrack_zone *zone,
657 const struct net *net)
659 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
661 /* A conntrack can be recreated with the equal tuple,
662 * so we need to check that the conntrack is confirmed
664 return nf_ct_tuple_equal(tuple, &h->tuple) &&
665 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
666 nf_ct_is_confirmed(ct) &&
667 net_eq(net, nf_ct_net(ct));
671 nf_ct_match(const struct nf_conn *ct1, const struct nf_conn *ct2)
673 return nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
674 &ct2->tuplehash[IP_CT_DIR_ORIGINAL].tuple) &&
675 nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_REPLY].tuple,
676 &ct2->tuplehash[IP_CT_DIR_REPLY].tuple) &&
677 nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_ORIGINAL) &&
678 nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_REPLY) &&
679 net_eq(nf_ct_net(ct1), nf_ct_net(ct2));
682 /* caller must hold rcu readlock and none of the nf_conntrack_locks */
683 static void nf_ct_gc_expired(struct nf_conn *ct)
685 if (!atomic_inc_not_zero(&ct->ct_general.use))
688 if (nf_ct_should_gc(ct))
696 * - Caller must take a reference on returned object
697 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
699 static struct nf_conntrack_tuple_hash *
700 ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
701 const struct nf_conntrack_tuple *tuple, u32 hash)
703 struct nf_conntrack_tuple_hash *h;
704 struct hlist_nulls_head *ct_hash;
705 struct hlist_nulls_node *n;
706 unsigned int bucket, hsize;
709 nf_conntrack_get_ht(&ct_hash, &hsize);
710 bucket = reciprocal_scale(hash, hsize);
712 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
715 ct = nf_ct_tuplehash_to_ctrack(h);
716 if (nf_ct_is_expired(ct)) {
717 nf_ct_gc_expired(ct);
721 if (nf_ct_is_dying(ct))
724 if (nf_ct_key_equal(h, tuple, zone, net))
728 * if the nulls value we got at the end of this lookup is
729 * not the expected one, we must restart lookup.
730 * We probably met an item that was moved to another chain.
732 if (get_nulls_value(n) != bucket) {
733 NF_CT_STAT_INC_ATOMIC(net, search_restart);
740 /* Find a connection corresponding to a tuple. */
741 static struct nf_conntrack_tuple_hash *
742 __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
743 const struct nf_conntrack_tuple *tuple, u32 hash)
745 struct nf_conntrack_tuple_hash *h;
750 h = ____nf_conntrack_find(net, zone, tuple, hash);
752 ct = nf_ct_tuplehash_to_ctrack(h);
753 if (unlikely(nf_ct_is_dying(ct) ||
754 !atomic_inc_not_zero(&ct->ct_general.use)))
757 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
768 struct nf_conntrack_tuple_hash *
769 nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
770 const struct nf_conntrack_tuple *tuple)
772 return __nf_conntrack_find_get(net, zone, tuple,
773 hash_conntrack_raw(tuple, net));
775 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
777 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
779 unsigned int reply_hash)
781 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
782 &nf_conntrack_hash[hash]);
783 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
784 &nf_conntrack_hash[reply_hash]);
788 nf_conntrack_hash_check_insert(struct nf_conn *ct)
790 const struct nf_conntrack_zone *zone;
791 struct net *net = nf_ct_net(ct);
792 unsigned int hash, reply_hash;
793 struct nf_conntrack_tuple_hash *h;
794 struct hlist_nulls_node *n;
795 unsigned int sequence;
797 zone = nf_ct_zone(ct);
801 sequence = read_seqcount_begin(&nf_conntrack_generation);
802 hash = hash_conntrack(net,
803 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
804 reply_hash = hash_conntrack(net,
805 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
806 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
808 /* See if there's one in the list already, including reverse */
809 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
810 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
814 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
815 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
820 /* The caller holds a reference to this object */
821 atomic_set(&ct->ct_general.use, 2);
822 __nf_conntrack_hash_insert(ct, hash, reply_hash);
823 nf_conntrack_double_unlock(hash, reply_hash);
824 NF_CT_STAT_INC(net, insert);
829 nf_conntrack_double_unlock(hash, reply_hash);
830 NF_CT_STAT_INC(net, insert_failed);
834 EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
836 static inline void nf_ct_acct_update(struct nf_conn *ct,
837 enum ip_conntrack_info ctinfo,
840 struct nf_conn_acct *acct;
842 acct = nf_conn_acct_find(ct);
844 struct nf_conn_counter *counter = acct->counter;
846 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
847 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
851 static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
852 const struct nf_conn *loser_ct)
854 struct nf_conn_acct *acct;
856 acct = nf_conn_acct_find(loser_ct);
858 struct nf_conn_counter *counter = acct->counter;
861 /* u32 should be fine since we must have seen one packet. */
862 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
863 nf_ct_acct_update(ct, ctinfo, bytes);
867 /* Resolve race on insertion if this protocol allows this. */
868 static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
869 enum ip_conntrack_info ctinfo,
870 struct nf_conntrack_tuple_hash *h)
872 /* This is the conntrack entry already in hashes that won race. */
873 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
874 const struct nf_conntrack_l4proto *l4proto;
875 enum ip_conntrack_info oldinfo;
876 struct nf_conn *loser_ct = nf_ct_get(skb, &oldinfo);
878 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
879 if (l4proto->allow_clash &&
880 !nf_ct_is_dying(ct) &&
881 atomic_inc_not_zero(&ct->ct_general.use)) {
882 if (((ct->status & IPS_NAT_DONE_MASK) == 0) ||
883 nf_ct_match(ct, loser_ct)) {
884 nf_ct_acct_merge(ct, ctinfo, loser_ct);
885 nf_conntrack_put(&loser_ct->ct_general);
886 nf_ct_set(skb, ct, oldinfo);
891 NF_CT_STAT_INC(net, drop);
895 /* Confirm a connection given skb; places it in hash table */
897 __nf_conntrack_confirm(struct sk_buff *skb)
899 const struct nf_conntrack_zone *zone;
900 unsigned int hash, reply_hash;
901 struct nf_conntrack_tuple_hash *h;
903 struct nf_conn_help *help;
904 struct nf_conn_tstamp *tstamp;
905 struct hlist_nulls_node *n;
906 enum ip_conntrack_info ctinfo;
908 unsigned int sequence;
911 ct = nf_ct_get(skb, &ctinfo);
914 /* ipt_REJECT uses nf_conntrack_attach to attach related
915 ICMP/TCP RST packets in other direction. Actual packet
916 which created connection will be IP_CT_NEW or for an
917 expected connection, IP_CT_RELATED. */
918 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
921 zone = nf_ct_zone(ct);
925 sequence = read_seqcount_begin(&nf_conntrack_generation);
926 /* reuse the hash saved before */
927 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
928 hash = scale_hash(hash);
929 reply_hash = hash_conntrack(net,
930 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
932 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
934 /* We're not in hash table, and we refuse to set up related
935 * connections for unconfirmed conns. But packet copies and
936 * REJECT will give spurious warnings here.
939 /* Another skb with the same unconfirmed conntrack may
940 * win the race. This may happen for bridge(br_flood)
941 * or broadcast/multicast packets do skb_clone with
942 * unconfirmed conntrack.
944 if (unlikely(nf_ct_is_confirmed(ct))) {
946 nf_conntrack_double_unlock(hash, reply_hash);
951 pr_debug("Confirming conntrack %p\n", ct);
952 /* We have to check the DYING flag after unlink to prevent
953 * a race against nf_ct_get_next_corpse() possibly called from
954 * user context, else we insert an already 'dead' hash, blocking
955 * further use of that particular connection -JM.
957 nf_ct_del_from_dying_or_unconfirmed_list(ct);
959 if (unlikely(nf_ct_is_dying(ct))) {
960 nf_ct_add_to_dying_list(ct);
964 /* See if there's one in the list already, including reverse:
965 NAT could have grabbed it without realizing, since we're
966 not in the hash. If there is, we lost race. */
967 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
968 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
972 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
973 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
977 /* Timer relative to confirmation time, not original
978 setting time, otherwise we'd get timer wrap in
979 weird delay cases. */
980 ct->timeout += nfct_time_stamp;
981 atomic_inc(&ct->ct_general.use);
982 ct->status |= IPS_CONFIRMED;
984 /* set conntrack timestamp, if enabled. */
985 tstamp = nf_conn_tstamp_find(ct);
987 if (skb->tstamp == 0)
988 __net_timestamp(skb);
990 tstamp->start = ktime_to_ns(skb->tstamp);
992 /* Since the lookup is lockless, hash insertion must be done after
993 * starting the timer and setting the CONFIRMED bit. The RCU barriers
994 * guarantee that no other CPU can find the conntrack before the above
995 * stores are visible.
997 __nf_conntrack_hash_insert(ct, hash, reply_hash);
998 nf_conntrack_double_unlock(hash, reply_hash);
1001 help = nfct_help(ct);
1002 if (help && help->helper)
1003 nf_conntrack_event_cache(IPCT_HELPER, ct);
1005 nf_conntrack_event_cache(master_ct(ct) ?
1006 IPCT_RELATED : IPCT_NEW, ct);
1010 nf_ct_add_to_dying_list(ct);
1011 ret = nf_ct_resolve_clash(net, skb, ctinfo, h);
1013 nf_conntrack_double_unlock(hash, reply_hash);
1014 NF_CT_STAT_INC(net, insert_failed);
1018 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
1020 /* Returns true if a connection correspondings to the tuple (required
1023 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
1024 const struct nf_conn *ignored_conntrack)
1026 struct net *net = nf_ct_net(ignored_conntrack);
1027 const struct nf_conntrack_zone *zone;
1028 struct nf_conntrack_tuple_hash *h;
1029 struct hlist_nulls_head *ct_hash;
1030 unsigned int hash, hsize;
1031 struct hlist_nulls_node *n;
1034 zone = nf_ct_zone(ignored_conntrack);
1038 nf_conntrack_get_ht(&ct_hash, &hsize);
1039 hash = __hash_conntrack(net, tuple, hsize);
1041 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
1042 ct = nf_ct_tuplehash_to_ctrack(h);
1044 if (ct == ignored_conntrack)
1047 if (nf_ct_is_expired(ct)) {
1048 nf_ct_gc_expired(ct);
1052 if (nf_ct_key_equal(h, tuple, zone, net)) {
1053 /* Tuple is taken already, so caller will need to find
1054 * a new source port to use.
1057 * If the *original tuples* are identical, then both
1058 * conntracks refer to the same flow.
1059 * This is a rare situation, it can occur e.g. when
1060 * more than one UDP packet is sent from same socket
1061 * in different threads.
1063 * Let nf_ct_resolve_clash() deal with this later.
1065 if (nf_ct_tuple_equal(&ignored_conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1066 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple) &&
1067 nf_ct_zone_equal(ct, zone, IP_CT_DIR_ORIGINAL))
1070 NF_CT_STAT_INC_ATOMIC(net, found);
1076 if (get_nulls_value(n) != hash) {
1077 NF_CT_STAT_INC_ATOMIC(net, search_restart);
1085 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
1087 #define NF_CT_EVICTION_RANGE 8
1089 /* There's a small race here where we may free a just-assured
1090 connection. Too bad: we're in trouble anyway. */
1091 static unsigned int early_drop_list(struct net *net,
1092 struct hlist_nulls_head *head)
1094 struct nf_conntrack_tuple_hash *h;
1095 struct hlist_nulls_node *n;
1096 unsigned int drops = 0;
1097 struct nf_conn *tmp;
1099 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
1100 tmp = nf_ct_tuplehash_to_ctrack(h);
1102 if (test_bit(IPS_OFFLOAD_BIT, &tmp->status))
1105 if (nf_ct_is_expired(tmp)) {
1106 nf_ct_gc_expired(tmp);
1110 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
1111 !net_eq(nf_ct_net(tmp), net) ||
1112 nf_ct_is_dying(tmp))
1115 if (!atomic_inc_not_zero(&tmp->ct_general.use))
1118 /* kill only if still in same netns -- might have moved due to
1119 * SLAB_TYPESAFE_BY_RCU rules.
1121 * We steal the timer reference. If that fails timer has
1122 * already fired or someone else deleted it. Just drop ref
1123 * and move to next entry.
1125 if (net_eq(nf_ct_net(tmp), net) &&
1126 nf_ct_is_confirmed(tmp) &&
1127 nf_ct_delete(tmp, 0, 0))
1136 static noinline int early_drop(struct net *net, unsigned int hash)
1138 unsigned int i, bucket;
1140 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
1141 struct hlist_nulls_head *ct_hash;
1142 unsigned int hsize, drops;
1145 nf_conntrack_get_ht(&ct_hash, &hsize);
1147 bucket = reciprocal_scale(hash, hsize);
1149 bucket = (bucket + 1) % hsize;
1151 drops = early_drop_list(net, &ct_hash[bucket]);
1155 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
1163 static bool gc_worker_skip_ct(const struct nf_conn *ct)
1165 return !nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct);
1168 static bool gc_worker_can_early_drop(const struct nf_conn *ct)
1170 const struct nf_conntrack_l4proto *l4proto;
1172 if (!test_bit(IPS_ASSURED_BIT, &ct->status))
1175 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1176 if (l4proto->can_early_drop && l4proto->can_early_drop(ct))
1182 #define DAY (86400 * HZ)
1184 /* Set an arbitrary timeout large enough not to ever expire, this save
1185 * us a check for the IPS_OFFLOAD_BIT from the packet path via
1186 * nf_ct_is_expired().
1188 static void nf_ct_offload_timeout(struct nf_conn *ct)
1190 if (nf_ct_expires(ct) < DAY / 2)
1191 ct->timeout = nfct_time_stamp + DAY;
1194 static void gc_worker(struct work_struct *work)
1196 unsigned long end_time = jiffies + GC_SCAN_MAX_DURATION;
1197 unsigned int i, hashsz, nf_conntrack_max95 = 0;
1198 unsigned long next_run = GC_SCAN_INTERVAL;
1199 struct conntrack_gc_work *gc_work;
1200 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
1202 i = gc_work->next_bucket;
1203 if (gc_work->early_drop)
1204 nf_conntrack_max95 = nf_conntrack_max / 100u * 95u;
1207 struct nf_conntrack_tuple_hash *h;
1208 struct hlist_nulls_head *ct_hash;
1209 struct hlist_nulls_node *n;
1210 struct nf_conn *tmp;
1214 nf_conntrack_get_ht(&ct_hash, &hashsz);
1220 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
1223 tmp = nf_ct_tuplehash_to_ctrack(h);
1225 if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) {
1226 nf_ct_offload_timeout(tmp);
1230 if (nf_ct_is_expired(tmp)) {
1231 nf_ct_gc_expired(tmp);
1235 if (nf_conntrack_max95 == 0 || gc_worker_skip_ct(tmp))
1238 net = nf_ct_net(tmp);
1239 if (atomic_read(&net->ct.count) < nf_conntrack_max95)
1242 /* need to take reference to avoid possible races */
1243 if (!atomic_inc_not_zero(&tmp->ct_general.use))
1246 if (gc_worker_skip_ct(tmp)) {
1251 if (gc_worker_can_early_drop(tmp))
1257 /* could check get_nulls_value() here and restart if ct
1258 * was moved to another chain. But given gc is best-effort
1259 * we will just continue with next hash slot.
1265 if (time_after(jiffies, end_time) && i < hashsz) {
1266 gc_work->next_bucket = i;
1270 } while (i < hashsz);
1272 if (gc_work->exiting)
1276 * Eviction will normally happen from the packet path, and not
1277 * from this gc worker.
1279 * This worker is only here to reap expired entries when system went
1280 * idle after a busy period.
1283 gc_work->early_drop = false;
1284 gc_work->next_bucket = 0;
1286 queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run);
1289 static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
1291 INIT_DEFERRABLE_WORK(&gc_work->dwork, gc_worker);
1292 gc_work->exiting = false;
1295 static struct nf_conn *
1296 __nf_conntrack_alloc(struct net *net,
1297 const struct nf_conntrack_zone *zone,
1298 const struct nf_conntrack_tuple *orig,
1299 const struct nf_conntrack_tuple *repl,
1300 gfp_t gfp, u32 hash)
1304 /* We don't want any race condition at early drop stage */
1305 atomic_inc(&net->ct.count);
1307 if (nf_conntrack_max &&
1308 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
1309 if (!early_drop(net, hash)) {
1310 if (!conntrack_gc_work.early_drop)
1311 conntrack_gc_work.early_drop = true;
1312 atomic_dec(&net->ct.count);
1313 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
1314 return ERR_PTR(-ENOMEM);
1319 * Do not use kmem_cache_zalloc(), as this cache uses
1320 * SLAB_TYPESAFE_BY_RCU.
1322 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
1326 spin_lock_init(&ct->lock);
1327 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
1328 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
1329 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
1330 /* save hash for reusing when confirming */
1331 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
1333 write_pnet(&ct->ct_net, net);
1334 memset(&ct->__nfct_init_offset, 0,
1335 offsetof(struct nf_conn, proto) -
1336 offsetof(struct nf_conn, __nfct_init_offset));
1338 nf_ct_zone_add(ct, zone);
1340 /* Because we use RCU lookups, we set ct_general.use to zero before
1341 * this is inserted in any list.
1343 atomic_set(&ct->ct_general.use, 0);
1346 atomic_dec(&net->ct.count);
1347 return ERR_PTR(-ENOMEM);
1350 struct nf_conn *nf_conntrack_alloc(struct net *net,
1351 const struct nf_conntrack_zone *zone,
1352 const struct nf_conntrack_tuple *orig,
1353 const struct nf_conntrack_tuple *repl,
1356 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1358 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
1360 void nf_conntrack_free(struct nf_conn *ct)
1362 struct net *net = nf_ct_net(ct);
1364 /* A freed object has refcnt == 0, that's
1365 * the golden rule for SLAB_TYPESAFE_BY_RCU
1367 WARN_ON(atomic_read(&ct->ct_general.use) != 0);
1369 nf_ct_ext_destroy(ct);
1371 kmem_cache_free(nf_conntrack_cachep, ct);
1372 smp_mb__before_atomic();
1373 atomic_dec(&net->ct.count);
1375 EXPORT_SYMBOL_GPL(nf_conntrack_free);
1378 /* Allocate a new conntrack: we return -ENOMEM if classification
1379 failed due to stress. Otherwise it really is unclassifiable. */
1380 static noinline struct nf_conntrack_tuple_hash *
1381 init_conntrack(struct net *net, struct nf_conn *tmpl,
1382 const struct nf_conntrack_tuple *tuple,
1383 const struct nf_conntrack_l4proto *l4proto,
1384 struct sk_buff *skb,
1385 unsigned int dataoff, u32 hash)
1388 struct nf_conn_help *help;
1389 struct nf_conntrack_tuple repl_tuple;
1390 struct nf_conntrack_ecache *ecache;
1391 struct nf_conntrack_expect *exp = NULL;
1392 const struct nf_conntrack_zone *zone;
1393 struct nf_conn_timeout *timeout_ext;
1394 struct nf_conntrack_zone tmp;
1396 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l4proto)) {
1397 pr_debug("Can't invert tuple.\n");
1401 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1402 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1405 return (struct nf_conntrack_tuple_hash *)ct;
1407 if (!nf_ct_add_synproxy(ct, tmpl)) {
1408 nf_conntrack_free(ct);
1409 return ERR_PTR(-ENOMEM);
1412 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
1414 if (!l4proto->new(ct, skb, dataoff)) {
1415 nf_conntrack_free(ct);
1416 pr_debug("can't track with proto module\n");
1421 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1424 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1425 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1426 nf_ct_labels_ext_add(ct);
1428 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1429 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1430 ecache ? ecache->expmask : 0,
1434 if (net->ct.expect_count) {
1435 spin_lock(&nf_conntrack_expect_lock);
1436 exp = nf_ct_find_expectation(net, zone, tuple);
1438 pr_debug("expectation arrives ct=%p exp=%p\n",
1440 /* Welcome, Mr. Bond. We've been expecting you... */
1441 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1442 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1443 ct->master = exp->master;
1445 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1447 rcu_assign_pointer(help->helper, exp->helper);
1450 #ifdef CONFIG_NF_CONNTRACK_MARK
1451 ct->mark = exp->master->mark;
1453 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1454 ct->secmark = exp->master->secmark;
1456 NF_CT_STAT_INC(net, expect_new);
1458 spin_unlock(&nf_conntrack_expect_lock);
1461 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
1463 /* Now it is inserted into the unconfirmed list, bump refcount */
1464 nf_conntrack_get(&ct->ct_general);
1465 nf_ct_add_to_unconfirmed_list(ct);
1471 exp->expectfn(ct, exp);
1472 nf_ct_expect_put(exp);
1475 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
1478 /* On success, returns 0, sets skb->_nfct | ctinfo */
1480 resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
1481 struct sk_buff *skb,
1482 unsigned int dataoff,
1485 const struct nf_conntrack_l4proto *l4proto)
1487 const struct nf_conntrack_zone *zone;
1488 struct nf_conntrack_tuple tuple;
1489 struct nf_conntrack_tuple_hash *h;
1490 enum ip_conntrack_info ctinfo;
1491 struct nf_conntrack_zone tmp;
1495 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
1496 dataoff, l3num, protonum, net, &tuple, l4proto)) {
1497 pr_debug("Can't get tuple\n");
1501 /* look for tuple match */
1502 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1503 hash = hash_conntrack_raw(&tuple, net);
1504 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
1506 h = init_conntrack(net, tmpl, &tuple, l4proto,
1507 skb, dataoff, hash);
1513 ct = nf_ct_tuplehash_to_ctrack(h);
1515 /* It exists; we have (non-exclusive) reference. */
1516 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
1517 ctinfo = IP_CT_ESTABLISHED_REPLY;
1519 /* Once we've had two way comms, always ESTABLISHED. */
1520 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1521 pr_debug("normal packet for %p\n", ct);
1522 ctinfo = IP_CT_ESTABLISHED;
1523 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
1524 pr_debug("related packet for %p\n", ct);
1525 ctinfo = IP_CT_RELATED;
1527 pr_debug("new packet for %p\n", ct);
1531 nf_ct_set(skb, ct, ctinfo);
1536 nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1537 struct sk_buff *skb)
1539 const struct nf_conntrack_l4proto *l4proto;
1540 struct nf_conn *ct, *tmpl;
1541 enum ip_conntrack_info ctinfo;
1545 tmpl = nf_ct_get(skb, &ctinfo);
1546 if (tmpl || ctinfo == IP_CT_UNTRACKED) {
1547 /* Previously seen (loopback or untracked)? Ignore. */
1548 if ((tmpl && !nf_ct_is_template(tmpl)) ||
1549 ctinfo == IP_CT_UNTRACKED) {
1550 NF_CT_STAT_INC_ATOMIC(net, ignore);
1556 /* rcu_read_lock()ed by nf_hook_thresh */
1557 dataoff = get_l4proto(skb, skb_network_offset(skb), pf, &protonum);
1559 pr_debug("not prepared to track yet or error occurred\n");
1560 NF_CT_STAT_INC_ATOMIC(net, error);
1561 NF_CT_STAT_INC_ATOMIC(net, invalid);
1566 l4proto = __nf_ct_l4proto_find(pf, protonum);
1568 /* It may be an special packet, error, unclean...
1569 * inverse of the return code tells to the netfilter
1570 * core what to do with the packet. */
1571 if (l4proto->error != NULL) {
1572 ret = l4proto->error(net, tmpl, skb, dataoff, pf, hooknum);
1574 NF_CT_STAT_INC_ATOMIC(net, error);
1575 NF_CT_STAT_INC_ATOMIC(net, invalid);
1579 /* ICMP[v6] protocol trackers may assign one conntrack. */
1584 ret = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum, l4proto);
1586 /* Too stressed to deal. */
1587 NF_CT_STAT_INC_ATOMIC(net, drop);
1592 ct = nf_ct_get(skb, &ctinfo);
1594 /* Not valid part of a connection */
1595 NF_CT_STAT_INC_ATOMIC(net, invalid);
1600 ret = l4proto->packet(ct, skb, dataoff, ctinfo);
1602 /* Invalid: inverse of the return code tells
1603 * the netfilter core what to do */
1604 pr_debug("nf_conntrack_in: Can't track with proto module\n");
1605 nf_conntrack_put(&ct->ct_general);
1607 NF_CT_STAT_INC_ATOMIC(net, invalid);
1608 if (ret == -NF_DROP)
1609 NF_CT_STAT_INC_ATOMIC(net, drop);
1610 /* Special case: TCP tracker reports an attempt to reopen a
1611 * closed/aborted connection. We have to go back and create a
1614 if (ret == -NF_REPEAT)
1620 if (ctinfo == IP_CT_ESTABLISHED_REPLY &&
1621 !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
1622 nf_conntrack_event_cache(IPCT_REPLY, ct);
1629 EXPORT_SYMBOL_GPL(nf_conntrack_in);
1631 bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1632 const struct nf_conntrack_tuple *orig)
1637 ret = nf_ct_invert_tuple(inverse, orig,
1638 __nf_ct_l4proto_find(orig->src.l3num,
1639 orig->dst.protonum));
1643 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
1645 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
1646 implicitly racy: see __nf_conntrack_confirm */
1647 void nf_conntrack_alter_reply(struct nf_conn *ct,
1648 const struct nf_conntrack_tuple *newreply)
1650 struct nf_conn_help *help = nfct_help(ct);
1652 /* Should be unconfirmed, so not in hash table yet */
1653 WARN_ON(nf_ct_is_confirmed(ct));
1655 pr_debug("Altering reply tuple of %p to ", ct);
1656 nf_ct_dump_tuple(newreply);
1658 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
1659 if (ct->master || (help && !hlist_empty(&help->expectations)))
1663 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1666 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
1668 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1669 void __nf_ct_refresh_acct(struct nf_conn *ct,
1670 enum ip_conntrack_info ctinfo,
1671 const struct sk_buff *skb,
1672 unsigned long extra_jiffies,
1677 /* Only update if this is not a fixed timeout */
1678 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1681 /* If not in hash table, timer will not be active yet */
1682 if (nf_ct_is_confirmed(ct))
1683 extra_jiffies += nfct_time_stamp;
1685 ct->timeout = extra_jiffies;
1688 nf_ct_acct_update(ct, ctinfo, skb->len);
1690 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
1692 bool nf_ct_kill_acct(struct nf_conn *ct,
1693 enum ip_conntrack_info ctinfo,
1694 const struct sk_buff *skb)
1696 nf_ct_acct_update(ct, ctinfo, skb->len);
1698 return nf_ct_delete(ct, 0, 0);
1700 EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
1702 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1704 #include <linux/netfilter/nfnetlink.h>
1705 #include <linux/netfilter/nfnetlink_conntrack.h>
1706 #include <linux/mutex.h>
1708 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1709 * in ip_conntrack_core, since we don't want the protocols to autoload
1710 * or depend on ctnetlink */
1711 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
1712 const struct nf_conntrack_tuple *tuple)
1714 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1715 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1716 goto nla_put_failure;
1722 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
1724 const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1725 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1726 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
1728 EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
1730 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
1731 struct nf_conntrack_tuple *t)
1733 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
1736 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1737 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
1741 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
1743 unsigned int nf_ct_port_nlattr_tuple_size(void)
1745 static unsigned int size __read_mostly;
1748 size = nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1752 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
1755 /* Used by ipt_REJECT and ip6t_REJECT. */
1756 static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
1759 enum ip_conntrack_info ctinfo;
1761 /* This ICMP is in reverse direction to the packet which caused it */
1762 ct = nf_ct_get(skb, &ctinfo);
1763 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1764 ctinfo = IP_CT_RELATED_REPLY;
1766 ctinfo = IP_CT_RELATED;
1768 /* Attach to new skbuff, and increment count */
1769 nf_ct_set(nskb, ct, ctinfo);
1770 nf_conntrack_get(skb_nfct(nskb));
1773 static int nf_conntrack_update(struct net *net, struct sk_buff *skb)
1775 const struct nf_conntrack_l4proto *l4proto;
1776 struct nf_conntrack_tuple_hash *h;
1777 struct nf_conntrack_tuple tuple;
1778 enum ip_conntrack_info ctinfo;
1779 struct nf_nat_hook *nat_hook;
1780 unsigned int status;
1786 ct = nf_ct_get(skb, &ctinfo);
1787 if (!ct || nf_ct_is_confirmed(ct))
1790 l3num = nf_ct_l3num(ct);
1792 dataoff = get_l4proto(skb, skb_network_offset(skb), l3num, &l4num);
1796 l4proto = nf_ct_l4proto_find_get(l3num, l4num);
1798 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
1799 l4num, net, &tuple, l4proto))
1802 if (ct->status & IPS_SRC_NAT) {
1803 memcpy(tuple.src.u3.all,
1804 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.all,
1805 sizeof(tuple.src.u3.all));
1807 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all;
1810 if (ct->status & IPS_DST_NAT) {
1811 memcpy(tuple.dst.u3.all,
1812 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.all,
1813 sizeof(tuple.dst.u3.all));
1815 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all;
1818 h = nf_conntrack_find_get(net, nf_ct_zone(ct), &tuple);
1822 /* Store status bits of the conntrack that is clashing to re-do NAT
1823 * mangling according to what it has been done already to this packet.
1825 status = ct->status;
1828 ct = nf_ct_tuplehash_to_ctrack(h);
1829 nf_ct_set(skb, ct, ctinfo);
1831 nat_hook = rcu_dereference(nf_nat_hook);
1835 if (status & IPS_SRC_NAT &&
1836 nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_SRC,
1837 IP_CT_DIR_ORIGINAL) == NF_DROP)
1840 if (status & IPS_DST_NAT &&
1841 nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_DST,
1842 IP_CT_DIR_ORIGINAL) == NF_DROP)
1848 static bool nf_conntrack_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
1849 const struct sk_buff *skb)
1851 const struct nf_conntrack_tuple *src_tuple;
1852 const struct nf_conntrack_tuple_hash *hash;
1853 struct nf_conntrack_tuple srctuple;
1854 enum ip_conntrack_info ctinfo;
1857 ct = nf_ct_get(skb, &ctinfo);
1859 src_tuple = nf_ct_tuple(ct, CTINFO2DIR(ctinfo));
1860 memcpy(dst_tuple, src_tuple, sizeof(*dst_tuple));
1864 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
1865 NFPROTO_IPV4, dev_net(skb->dev),
1869 hash = nf_conntrack_find_get(dev_net(skb->dev),
1875 ct = nf_ct_tuplehash_to_ctrack(hash);
1876 src_tuple = nf_ct_tuple(ct, !hash->tuple.dst.dir);
1877 memcpy(dst_tuple, src_tuple, sizeof(*dst_tuple));
1883 /* Bring out ya dead! */
1884 static struct nf_conn *
1885 get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
1886 void *data, unsigned int *bucket)
1888 struct nf_conntrack_tuple_hash *h;
1890 struct hlist_nulls_node *n;
1893 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
1894 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1896 nf_conntrack_lock(lockp);
1897 if (*bucket < nf_conntrack_htable_size) {
1898 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
1899 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1901 ct = nf_ct_tuplehash_to_ctrack(h);
1913 atomic_inc(&ct->ct_general.use);
1919 static void nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data),
1920 void *data, u32 portid, int report)
1922 unsigned int bucket = 0, sequence;
1928 sequence = read_seqcount_begin(&nf_conntrack_generation);
1930 while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
1931 /* Time to push up daises... */
1933 nf_ct_delete(ct, portid, report);
1938 if (!read_seqcount_retry(&nf_conntrack_generation, sequence))
1945 int (*iter)(struct nf_conn *i, void *data);
1950 static int iter_net_only(struct nf_conn *i, void *data)
1952 struct iter_data *d = data;
1954 if (!net_eq(d->net, nf_ct_net(i)))
1957 return d->iter(i, d->data);
1961 __nf_ct_unconfirmed_destroy(struct net *net)
1965 for_each_possible_cpu(cpu) {
1966 struct nf_conntrack_tuple_hash *h;
1967 struct hlist_nulls_node *n;
1968 struct ct_pcpu *pcpu;
1970 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1972 spin_lock_bh(&pcpu->lock);
1973 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1976 ct = nf_ct_tuplehash_to_ctrack(h);
1978 /* we cannot call iter() on unconfirmed list, the
1979 * owning cpu can reallocate ct->ext at any time.
1981 set_bit(IPS_DYING_BIT, &ct->status);
1983 spin_unlock_bh(&pcpu->lock);
1988 void nf_ct_unconfirmed_destroy(struct net *net)
1992 if (atomic_read(&net->ct.count) > 0) {
1993 __nf_ct_unconfirmed_destroy(net);
1994 nf_queue_nf_hook_drop(net);
1998 EXPORT_SYMBOL_GPL(nf_ct_unconfirmed_destroy);
2000 void nf_ct_iterate_cleanup_net(struct net *net,
2001 int (*iter)(struct nf_conn *i, void *data),
2002 void *data, u32 portid, int report)
2008 if (atomic_read(&net->ct.count) == 0)
2015 nf_ct_iterate_cleanup(iter_net_only, &d, portid, report);
2017 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup_net);
2020 * nf_ct_iterate_destroy - destroy unconfirmed conntracks and iterate table
2021 * @iter: callback to invoke for each conntrack
2022 * @data: data to pass to @iter
2024 * Like nf_ct_iterate_cleanup, but first marks conntracks on the
2025 * unconfirmed list as dying (so they will not be inserted into
2028 * Can only be called in module exit path.
2031 nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
2035 down_read(&net_rwsem);
2037 if (atomic_read(&net->ct.count) == 0)
2039 __nf_ct_unconfirmed_destroy(net);
2040 nf_queue_nf_hook_drop(net);
2042 up_read(&net_rwsem);
2044 /* Need to wait for netns cleanup worker to finish, if its
2045 * running -- it might have deleted a net namespace from
2046 * the global list, so our __nf_ct_unconfirmed_destroy() might
2047 * not have affected all namespaces.
2051 /* a conntrack could have been unlinked from unconfirmed list
2052 * before we grabbed pcpu lock in __nf_ct_unconfirmed_destroy().
2053 * This makes sure its inserted into conntrack table.
2057 nf_ct_iterate_cleanup(iter, data, 0, 0);
2059 EXPORT_SYMBOL_GPL(nf_ct_iterate_destroy);
2061 static int kill_all(struct nf_conn *i, void *data)
2063 return net_eq(nf_ct_net(i), data);
2066 void nf_conntrack_cleanup_start(void)
2068 conntrack_gc_work.exiting = true;
2069 RCU_INIT_POINTER(ip_ct_attach, NULL);
2072 void nf_conntrack_cleanup_end(void)
2074 RCU_INIT_POINTER(nf_ct_hook, NULL);
2075 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
2076 kvfree(nf_conntrack_hash);
2078 nf_conntrack_proto_fini();
2079 nf_conntrack_seqadj_fini();
2080 nf_conntrack_labels_fini();
2081 nf_conntrack_helper_fini();
2082 nf_conntrack_timeout_fini();
2083 nf_conntrack_ecache_fini();
2084 nf_conntrack_tstamp_fini();
2085 nf_conntrack_acct_fini();
2086 nf_conntrack_expect_fini();
2088 kmem_cache_destroy(nf_conntrack_cachep);
2092 * Mishearing the voices in his head, our hero wonders how he's
2093 * supposed to kill the mall.
2095 void nf_conntrack_cleanup_net(struct net *net)
2099 list_add(&net->exit_list, &single);
2100 nf_conntrack_cleanup_net_list(&single);
2103 void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
2109 * This makes sure all current packets have passed through
2110 * netfilter framework. Roll on, two-stage module
2116 list_for_each_entry(net, net_exit_list, exit_list) {
2117 nf_ct_iterate_cleanup(kill_all, net, 0, 0);
2118 if (atomic_read(&net->ct.count) != 0)
2123 goto i_see_dead_people;
2126 list_for_each_entry(net, net_exit_list, exit_list) {
2127 nf_conntrack_proto_pernet_fini(net);
2128 nf_conntrack_helper_pernet_fini(net);
2129 nf_conntrack_ecache_pernet_fini(net);
2130 nf_conntrack_tstamp_pernet_fini(net);
2131 nf_conntrack_acct_pernet_fini(net);
2132 nf_conntrack_expect_pernet_fini(net);
2133 free_percpu(net->ct.stat);
2134 free_percpu(net->ct.pcpu_lists);
2138 void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
2140 struct hlist_nulls_head *hash;
2141 unsigned int nr_slots, i;
2143 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
2146 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
2147 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
2149 hash = kvmalloc_array(nr_slots, sizeof(struct hlist_nulls_head),
2150 GFP_KERNEL | __GFP_ZERO);
2153 for (i = 0; i < nr_slots; i++)
2154 INIT_HLIST_NULLS_HEAD(&hash[i], i);
2158 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
2160 int nf_conntrack_hash_resize(unsigned int hashsize)
2163 unsigned int old_size;
2164 struct hlist_nulls_head *hash, *old_hash;
2165 struct nf_conntrack_tuple_hash *h;
2171 hash = nf_ct_alloc_hashtable(&hashsize, 1);
2175 old_size = nf_conntrack_htable_size;
2176 if (old_size == hashsize) {
2182 nf_conntrack_all_lock();
2183 write_seqcount_begin(&nf_conntrack_generation);
2185 /* Lookups in the old hash might happen in parallel, which means we
2186 * might get false negatives during connection lookup. New connections
2187 * created because of a false negative won't make it into the hash
2188 * though since that required taking the locks.
2191 for (i = 0; i < nf_conntrack_htable_size; i++) {
2192 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
2193 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
2194 struct nf_conntrack_tuple_hash, hnnode);
2195 ct = nf_ct_tuplehash_to_ctrack(h);
2196 hlist_nulls_del_rcu(&h->hnnode);
2197 bucket = __hash_conntrack(nf_ct_net(ct),
2198 &h->tuple, hashsize);
2199 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
2202 old_size = nf_conntrack_htable_size;
2203 old_hash = nf_conntrack_hash;
2205 nf_conntrack_hash = hash;
2206 nf_conntrack_htable_size = hashsize;
2208 write_seqcount_end(&nf_conntrack_generation);
2209 nf_conntrack_all_unlock();
2217 int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp)
2219 unsigned int hashsize;
2222 if (current->nsproxy->net_ns != &init_net)
2225 /* On boot, we can set this without any fancy locking. */
2226 if (!nf_conntrack_hash)
2227 return param_set_uint(val, kp);
2229 rc = kstrtouint(val, 0, &hashsize);
2233 return nf_conntrack_hash_resize(hashsize);
2235 EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
2237 static __always_inline unsigned int total_extension_size(void)
2239 /* remember to add new extensions below */
2240 BUILD_BUG_ON(NF_CT_EXT_NUM > 9);
2242 return sizeof(struct nf_ct_ext) +
2243 sizeof(struct nf_conn_help)
2244 #if IS_ENABLED(CONFIG_NF_NAT)
2245 + sizeof(struct nf_conn_nat)
2247 + sizeof(struct nf_conn_seqadj)
2248 + sizeof(struct nf_conn_acct)
2249 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2250 + sizeof(struct nf_conntrack_ecache)
2252 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
2253 + sizeof(struct nf_conn_tstamp)
2255 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
2256 + sizeof(struct nf_conn_timeout)
2258 #ifdef CONFIG_NF_CONNTRACK_LABELS
2259 + sizeof(struct nf_conn_labels)
2261 #if IS_ENABLED(CONFIG_NETFILTER_SYNPROXY)
2262 + sizeof(struct nf_conn_synproxy)
2267 int nf_conntrack_init_start(void)
2273 /* struct nf_ct_ext uses u8 to store offsets/size */
2274 BUILD_BUG_ON(total_extension_size() > 255u);
2276 seqcount_init(&nf_conntrack_generation);
2278 for (i = 0; i < CONNTRACK_LOCKS; i++)
2279 spin_lock_init(&nf_conntrack_locks[i]);
2281 if (!nf_conntrack_htable_size) {
2282 /* Idea from tcp.c: use 1/16384 of memory.
2283 * On i386: 32MB machine has 512 buckets.
2284 * >= 1GB machines have 16384 buckets.
2285 * >= 4GB machines have 65536 buckets.
2287 nf_conntrack_htable_size
2288 = (((totalram_pages << PAGE_SHIFT) / 16384)
2289 / sizeof(struct hlist_head));
2290 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
2291 nf_conntrack_htable_size = 65536;
2292 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
2293 nf_conntrack_htable_size = 16384;
2294 if (nf_conntrack_htable_size < 32)
2295 nf_conntrack_htable_size = 32;
2297 /* Use a max. factor of four by default to get the same max as
2298 * with the old struct list_heads. When a table size is given
2299 * we use the old value of 8 to avoid reducing the max.
2304 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
2305 if (!nf_conntrack_hash)
2308 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
2310 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
2311 sizeof(struct nf_conn),
2313 SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
2314 if (!nf_conntrack_cachep)
2317 ret = nf_conntrack_expect_init();
2321 ret = nf_conntrack_acct_init();
2325 ret = nf_conntrack_tstamp_init();
2329 ret = nf_conntrack_ecache_init();
2333 ret = nf_conntrack_timeout_init();
2337 ret = nf_conntrack_helper_init();
2341 ret = nf_conntrack_labels_init();
2345 ret = nf_conntrack_seqadj_init();
2349 ret = nf_conntrack_proto_init();
2353 conntrack_gc_work_init(&conntrack_gc_work);
2354 queue_delayed_work(system_power_efficient_wq, &conntrack_gc_work.dwork, HZ);
2359 nf_conntrack_seqadj_fini();
2361 nf_conntrack_labels_fini();
2363 nf_conntrack_helper_fini();
2365 nf_conntrack_timeout_fini();
2367 nf_conntrack_ecache_fini();
2369 nf_conntrack_tstamp_fini();
2371 nf_conntrack_acct_fini();
2373 nf_conntrack_expect_fini();
2375 kmem_cache_destroy(nf_conntrack_cachep);
2377 kvfree(nf_conntrack_hash);
2381 static struct nf_ct_hook nf_conntrack_hook = {
2382 .update = nf_conntrack_update,
2383 .destroy = destroy_conntrack,
2384 .get_tuple_skb = nf_conntrack_get_tuple_skb,
2387 void nf_conntrack_init_end(void)
2389 /* For use by REJECT target */
2390 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
2391 RCU_INIT_POINTER(nf_ct_hook, &nf_conntrack_hook);
2395 * We need to use special "null" values, not used in hash table
2397 #define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
2398 #define DYING_NULLS_VAL ((1<<30)+1)
2399 #define TEMPLATE_NULLS_VAL ((1<<30)+2)
2401 int nf_conntrack_init_net(struct net *net)
2406 BUILD_BUG_ON(IP_CT_UNTRACKED == IP_CT_NUMBER);
2407 atomic_set(&net->ct.count, 0);
2409 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
2410 if (!net->ct.pcpu_lists)
2413 for_each_possible_cpu(cpu) {
2414 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
2416 spin_lock_init(&pcpu->lock);
2417 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
2418 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
2421 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
2423 goto err_pcpu_lists;
2425 ret = nf_conntrack_expect_pernet_init(net);
2428 ret = nf_conntrack_acct_pernet_init(net);
2431 ret = nf_conntrack_tstamp_pernet_init(net);
2434 ret = nf_conntrack_ecache_pernet_init(net);
2437 ret = nf_conntrack_helper_pernet_init(net);
2440 ret = nf_conntrack_proto_pernet_init(net);
2446 nf_conntrack_helper_pernet_fini(net);
2448 nf_conntrack_ecache_pernet_fini(net);
2450 nf_conntrack_tstamp_pernet_fini(net);
2452 nf_conntrack_acct_pernet_fini(net);
2454 nf_conntrack_expect_pernet_fini(net);
2456 free_percpu(net->ct.stat);
2458 free_percpu(net->ct.pcpu_lists);