1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Connection state tracking for netfilter. This is separated from,
3 but required by, the NAT layer; it can also be used by an iptables
6 /* (C) 1999-2001 Paul `Rusty' Russell
7 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
8 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
9 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/types.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/skbuff.h>
19 #include <linux/proc_fs.h>
20 #include <linux/vmalloc.h>
21 #include <linux/stddef.h>
22 #include <linux/slab.h>
23 #include <linux/random.h>
24 #include <linux/siphash.h>
25 #include <linux/err.h>
26 #include <linux/percpu.h>
27 #include <linux/moduleparam.h>
28 #include <linux/notifier.h>
29 #include <linux/kernel.h>
30 #include <linux/netdevice.h>
31 #include <linux/socket.h>
33 #include <linux/nsproxy.h>
34 #include <linux/rculist_nulls.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_bpf.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_core.h>
42 #include <net/netfilter/nf_conntrack_extend.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_ecache.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_timeout.h>
48 #include <net/netfilter/nf_conntrack_labels.h>
49 #include <net/netfilter/nf_conntrack_synproxy.h>
50 #include <net/netfilter/nf_nat.h>
51 #include <net/netfilter/nf_nat_helper.h>
52 #include <net/netns/hash.h>
55 #include "nf_internals.h"
57 __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
58 EXPORT_SYMBOL_GPL(nf_conntrack_locks);
60 __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
61 EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
63 struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
64 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
66 struct conntrack_gc_work {
67 struct delayed_work dwork;
76 static __read_mostly struct kmem_cache *nf_conntrack_cachep;
77 static DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
78 static __read_mostly bool nf_conntrack_locks_all;
80 /* serialize hash resizes and nf_ct_iterate_cleanup */
81 static DEFINE_MUTEX(nf_conntrack_mutex);
83 #define GC_SCAN_INTERVAL_MAX (60ul * HZ)
84 #define GC_SCAN_INTERVAL_MIN (1ul * HZ)
86 /* clamp timeouts to this value (TCP unacked) */
87 #define GC_SCAN_INTERVAL_CLAMP (300ul * HZ)
89 /* Initial bias pretending we have 100 entries at the upper bound so we don't
90 * wakeup often just because we have three entries with a 1s timeout while still
91 * allowing non-idle machines to wakeup more often when needed.
93 #define GC_SCAN_INITIAL_COUNT 100
94 #define GC_SCAN_INTERVAL_INIT GC_SCAN_INTERVAL_MAX
96 #define GC_SCAN_MAX_DURATION msecs_to_jiffies(10)
97 #define GC_SCAN_EXPIRED_MAX (64000u / HZ)
99 #define MIN_CHAINLEN 8u
100 #define MAX_CHAINLEN (32u - MIN_CHAINLEN)
102 static struct conntrack_gc_work conntrack_gc_work;
104 void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
106 /* 1) Acquire the lock */
109 /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics
110 * It pairs with the smp_store_release() in nf_conntrack_all_unlock()
112 if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
115 /* fast path failed, unlock */
118 /* Slow path 1) get global lock */
119 spin_lock(&nf_conntrack_locks_all_lock);
121 /* Slow path 2) get the lock we want */
124 /* Slow path 3) release the global lock */
125 spin_unlock(&nf_conntrack_locks_all_lock);
127 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
129 static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
131 h1 %= CONNTRACK_LOCKS;
132 h2 %= CONNTRACK_LOCKS;
133 spin_unlock(&nf_conntrack_locks[h1]);
135 spin_unlock(&nf_conntrack_locks[h2]);
138 /* return true if we need to recompute hashes (in case hash table was resized) */
139 static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
140 unsigned int h2, unsigned int sequence)
142 h1 %= CONNTRACK_LOCKS;
143 h2 %= CONNTRACK_LOCKS;
145 nf_conntrack_lock(&nf_conntrack_locks[h1]);
147 spin_lock_nested(&nf_conntrack_locks[h2],
148 SINGLE_DEPTH_NESTING);
150 nf_conntrack_lock(&nf_conntrack_locks[h2]);
151 spin_lock_nested(&nf_conntrack_locks[h1],
152 SINGLE_DEPTH_NESTING);
154 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
155 nf_conntrack_double_unlock(h1, h2);
161 static void nf_conntrack_all_lock(void)
162 __acquires(&nf_conntrack_locks_all_lock)
166 spin_lock(&nf_conntrack_locks_all_lock);
168 /* For nf_contrack_locks_all, only the latest time when another
169 * CPU will see an update is controlled, by the "release" of the
171 * The earliest time is not controlled, an thus KCSAN could detect
172 * a race when nf_conntract_lock() reads the variable.
173 * WRITE_ONCE() is used to ensure the compiler will not
174 * optimize the write.
176 WRITE_ONCE(nf_conntrack_locks_all, true);
178 for (i = 0; i < CONNTRACK_LOCKS; i++) {
179 spin_lock(&nf_conntrack_locks[i]);
181 /* This spin_unlock provides the "release" to ensure that
182 * nf_conntrack_locks_all==true is visible to everyone that
183 * acquired spin_lock(&nf_conntrack_locks[]).
185 spin_unlock(&nf_conntrack_locks[i]);
189 static void nf_conntrack_all_unlock(void)
190 __releases(&nf_conntrack_locks_all_lock)
192 /* All prior stores must be complete before we clear
193 * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock()
194 * might observe the false value but not the entire
196 * It pairs with the smp_load_acquire() in nf_conntrack_lock()
198 smp_store_release(&nf_conntrack_locks_all, false);
199 spin_unlock(&nf_conntrack_locks_all_lock);
202 unsigned int nf_conntrack_htable_size __read_mostly;
203 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
205 unsigned int nf_conntrack_max __read_mostly;
206 EXPORT_SYMBOL_GPL(nf_conntrack_max);
207 seqcount_spinlock_t nf_conntrack_generation __read_mostly;
208 static siphash_aligned_key_t nf_conntrack_hash_rnd;
210 static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
212 const struct net *net)
215 struct nf_conntrack_man src;
216 union nf_inet_addr dst_addr;
221 } __aligned(SIPHASH_ALIGNMENT) combined;
223 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
225 memset(&combined, 0, sizeof(combined));
227 /* The direction must be ignored, so handle usable members manually. */
228 combined.src = tuple->src;
229 combined.dst_addr = tuple->dst.u3;
230 combined.zone = zoneid;
231 combined.net_mix = net_hash_mix(net);
232 combined.dport = (__force __u16)tuple->dst.u.all;
233 combined.proto = tuple->dst.protonum;
235 return (u32)siphash(&combined, sizeof(combined), &nf_conntrack_hash_rnd);
238 static u32 scale_hash(u32 hash)
240 return reciprocal_scale(hash, nf_conntrack_htable_size);
243 static u32 __hash_conntrack(const struct net *net,
244 const struct nf_conntrack_tuple *tuple,
248 return reciprocal_scale(hash_conntrack_raw(tuple, zoneid, net), size);
251 static u32 hash_conntrack(const struct net *net,
252 const struct nf_conntrack_tuple *tuple,
255 return scale_hash(hash_conntrack_raw(tuple, zoneid, net));
258 static bool nf_ct_get_tuple_ports(const struct sk_buff *skb,
259 unsigned int dataoff,
260 struct nf_conntrack_tuple *tuple)
264 } _inet_hdr, *inet_hdr;
266 /* Actually only need first 4 bytes to get ports. */
267 inet_hdr = skb_header_pointer(skb, dataoff, sizeof(_inet_hdr), &_inet_hdr);
271 tuple->src.u.udp.port = inet_hdr->sport;
272 tuple->dst.u.udp.port = inet_hdr->dport;
277 nf_ct_get_tuple(const struct sk_buff *skb,
279 unsigned int dataoff,
283 struct nf_conntrack_tuple *tuple)
289 memset(tuple, 0, sizeof(*tuple));
291 tuple->src.l3num = l3num;
294 nhoff += offsetof(struct iphdr, saddr);
295 size = 2 * sizeof(__be32);
298 nhoff += offsetof(struct ipv6hdr, saddr);
299 size = sizeof(_addrs);
305 ap = skb_header_pointer(skb, nhoff, size, _addrs);
311 tuple->src.u3.ip = ap[0];
312 tuple->dst.u3.ip = ap[1];
315 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
316 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
320 tuple->dst.protonum = protonum;
321 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
324 #if IS_ENABLED(CONFIG_IPV6)
326 return icmpv6_pkt_to_tuple(skb, dataoff, net, tuple);
329 return icmp_pkt_to_tuple(skb, dataoff, net, tuple);
330 #ifdef CONFIG_NF_CT_PROTO_GRE
332 return gre_pkt_to_tuple(skb, dataoff, net, tuple);
336 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
337 case IPPROTO_UDPLITE:
339 #ifdef CONFIG_NF_CT_PROTO_SCTP
342 #ifdef CONFIG_NF_CT_PROTO_DCCP
346 return nf_ct_get_tuple_ports(skb, dataoff, tuple);
354 static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
358 const struct iphdr *iph;
361 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
365 /* Conntrack defragments packets, we might still see fragments
366 * inside ICMP packets though.
368 if (iph->frag_off & htons(IP_OFFSET))
371 dataoff = nhoff + (iph->ihl << 2);
372 *protonum = iph->protocol;
374 /* Check bogus IP headers */
375 if (dataoff > skb->len) {
376 pr_debug("bogus IPv4 packet: nhoff %u, ihl %u, skblen %u\n",
377 nhoff, iph->ihl << 2, skb->len);
383 #if IS_ENABLED(CONFIG_IPV6)
384 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
388 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
392 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
393 &nexthdr, sizeof(nexthdr)) != 0) {
394 pr_debug("can't get nexthdr\n");
397 protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off);
399 * (protoff == skb->len) means the packet has not data, just
400 * IPv6 and possibly extensions headers, but it is tracked anyway
402 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
403 pr_debug("can't find proto in pkt\n");
412 static int get_l4proto(const struct sk_buff *skb,
413 unsigned int nhoff, u8 pf, u8 *l4num)
417 return ipv4_get_l4proto(skb, nhoff, l4num);
418 #if IS_ENABLED(CONFIG_IPV6)
420 return ipv6_get_l4proto(skb, nhoff, l4num);
429 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
431 struct net *net, struct nf_conntrack_tuple *tuple)
436 protoff = get_l4proto(skb, nhoff, l3num, &protonum);
440 return nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple);
442 EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
445 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
446 const struct nf_conntrack_tuple *orig)
448 memset(inverse, 0, sizeof(*inverse));
450 inverse->src.l3num = orig->src.l3num;
452 switch (orig->src.l3num) {
454 inverse->src.u3.ip = orig->dst.u3.ip;
455 inverse->dst.u3.ip = orig->src.u3.ip;
458 inverse->src.u3.in6 = orig->dst.u3.in6;
459 inverse->dst.u3.in6 = orig->src.u3.in6;
465 inverse->dst.dir = !orig->dst.dir;
467 inverse->dst.protonum = orig->dst.protonum;
469 switch (orig->dst.protonum) {
471 return nf_conntrack_invert_icmp_tuple(inverse, orig);
472 #if IS_ENABLED(CONFIG_IPV6)
474 return nf_conntrack_invert_icmpv6_tuple(inverse, orig);
478 inverse->src.u.all = orig->dst.u.all;
479 inverse->dst.u.all = orig->src.u.all;
482 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
484 /* Generate a almost-unique pseudo-id for a given conntrack.
486 * intentionally doesn't re-use any of the seeds used for hash
487 * table location, we assume id gets exposed to userspace.
489 * Following nf_conn items do not change throughout lifetime
493 * 2. nf_conn->master address (normally NULL)
494 * 3. the associated net namespace
495 * 4. the original direction tuple
497 u32 nf_ct_get_id(const struct nf_conn *ct)
499 static siphash_aligned_key_t ct_id_seed;
500 unsigned long a, b, c, d;
502 net_get_random_once(&ct_id_seed, sizeof(ct_id_seed));
504 a = (unsigned long)ct;
505 b = (unsigned long)ct->master;
506 c = (unsigned long)nf_ct_net(ct);
507 d = (unsigned long)siphash(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
508 sizeof(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple),
511 return siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &ct_id_seed);
513 return siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &ct_id_seed);
516 EXPORT_SYMBOL_GPL(nf_ct_get_id);
519 clean_from_lists(struct nf_conn *ct)
521 pr_debug("clean_from_lists(%p)\n", ct);
522 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
523 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
525 /* Destroy all pending expectations */
526 nf_ct_remove_expectations(ct);
529 #define NFCT_ALIGN(len) (((len) + NFCT_INFOMASK) & ~NFCT_INFOMASK)
531 /* Released via nf_ct_destroy() */
532 struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
533 const struct nf_conntrack_zone *zone,
536 struct nf_conn *tmpl, *p;
538 if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK) {
539 tmpl = kzalloc(sizeof(*tmpl) + NFCT_INFOMASK, flags);
544 tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p);
546 tmpl = (struct nf_conn *)NFCT_ALIGN((unsigned long)p);
547 tmpl->proto.tmpl_padto = (char *)tmpl - (char *)p;
550 tmpl = kzalloc(sizeof(*tmpl), flags);
555 tmpl->status = IPS_TEMPLATE;
556 write_pnet(&tmpl->ct_net, net);
557 nf_ct_zone_add(tmpl, zone);
558 refcount_set(&tmpl->ct_general.use, 1);
562 EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
564 void nf_ct_tmpl_free(struct nf_conn *tmpl)
568 if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK)
569 kfree((char *)tmpl - tmpl->proto.tmpl_padto);
573 EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
575 static void destroy_gre_conntrack(struct nf_conn *ct)
577 #ifdef CONFIG_NF_CT_PROTO_GRE
578 struct nf_conn *master = ct->master;
581 nf_ct_gre_keymap_destroy(master);
585 void nf_ct_destroy(struct nf_conntrack *nfct)
587 struct nf_conn *ct = (struct nf_conn *)nfct;
589 pr_debug("%s(%p)\n", __func__, ct);
590 WARN_ON(refcount_read(&nfct->use) != 0);
592 if (unlikely(nf_ct_is_template(ct))) {
597 if (unlikely(nf_ct_protonum(ct) == IPPROTO_GRE))
598 destroy_gre_conntrack(ct);
600 /* Expectations will have been removed in clean_from_lists,
601 * except TFTP can create an expectation on the first packet,
602 * before connection is in the list, so we need to clean here,
605 nf_ct_remove_expectations(ct);
608 nf_ct_put(ct->master);
610 pr_debug("%s: returning ct=%p to slab\n", __func__, ct);
611 nf_conntrack_free(ct);
613 EXPORT_SYMBOL(nf_ct_destroy);
615 static void __nf_ct_delete_from_lists(struct nf_conn *ct)
617 struct net *net = nf_ct_net(ct);
618 unsigned int hash, reply_hash;
619 unsigned int sequence;
622 sequence = read_seqcount_begin(&nf_conntrack_generation);
623 hash = hash_conntrack(net,
624 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
625 nf_ct_zone_id(nf_ct_zone(ct), IP_CT_DIR_ORIGINAL));
626 reply_hash = hash_conntrack(net,
627 &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
628 nf_ct_zone_id(nf_ct_zone(ct), IP_CT_DIR_REPLY));
629 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
631 clean_from_lists(ct);
632 nf_conntrack_double_unlock(hash, reply_hash);
635 static void nf_ct_delete_from_lists(struct nf_conn *ct)
637 nf_ct_helper_destroy(ct);
640 __nf_ct_delete_from_lists(ct);
645 static void nf_ct_add_to_ecache_list(struct nf_conn *ct)
647 #ifdef CONFIG_NF_CONNTRACK_EVENTS
648 struct nf_conntrack_net *cnet = nf_ct_pernet(nf_ct_net(ct));
650 spin_lock(&cnet->ecache.dying_lock);
651 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
652 &cnet->ecache.dying_list);
653 spin_unlock(&cnet->ecache.dying_lock);
657 bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
659 struct nf_conn_tstamp *tstamp;
662 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
665 tstamp = nf_conn_tstamp_find(ct);
667 s32 timeout = READ_ONCE(ct->timeout) - nfct_time_stamp;
669 tstamp->stop = ktime_get_real_ns();
671 tstamp->stop -= jiffies_to_nsecs(-timeout);
674 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
675 portid, report) < 0) {
676 /* destroy event was not delivered. nf_ct_put will
677 * be done by event cache worker on redelivery.
679 nf_ct_helper_destroy(ct);
681 __nf_ct_delete_from_lists(ct);
682 nf_ct_add_to_ecache_list(ct);
685 nf_conntrack_ecache_work(nf_ct_net(ct), NFCT_ECACHE_DESTROY_FAIL);
690 if (nf_conntrack_ecache_dwork_pending(net))
691 nf_conntrack_ecache_work(net, NFCT_ECACHE_DESTROY_SENT);
692 nf_ct_delete_from_lists(ct);
696 EXPORT_SYMBOL_GPL(nf_ct_delete);
699 nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
700 const struct nf_conntrack_tuple *tuple,
701 const struct nf_conntrack_zone *zone,
702 const struct net *net)
704 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
706 /* A conntrack can be recreated with the equal tuple,
707 * so we need to check that the conntrack is confirmed
709 return nf_ct_tuple_equal(tuple, &h->tuple) &&
710 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
711 nf_ct_is_confirmed(ct) &&
712 net_eq(net, nf_ct_net(ct));
716 nf_ct_match(const struct nf_conn *ct1, const struct nf_conn *ct2)
718 return nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
719 &ct2->tuplehash[IP_CT_DIR_ORIGINAL].tuple) &&
720 nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_REPLY].tuple,
721 &ct2->tuplehash[IP_CT_DIR_REPLY].tuple) &&
722 nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_ORIGINAL) &&
723 nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_REPLY) &&
724 net_eq(nf_ct_net(ct1), nf_ct_net(ct2));
727 /* caller must hold rcu readlock and none of the nf_conntrack_locks */
728 static void nf_ct_gc_expired(struct nf_conn *ct)
730 if (!refcount_inc_not_zero(&ct->ct_general.use))
733 /* load ->status after refcount increase */
734 smp_acquire__after_ctrl_dep();
736 if (nf_ct_should_gc(ct))
744 * - Caller must take a reference on returned object
745 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
747 static struct nf_conntrack_tuple_hash *
748 ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
749 const struct nf_conntrack_tuple *tuple, u32 hash)
751 struct nf_conntrack_tuple_hash *h;
752 struct hlist_nulls_head *ct_hash;
753 struct hlist_nulls_node *n;
754 unsigned int bucket, hsize;
757 nf_conntrack_get_ht(&ct_hash, &hsize);
758 bucket = reciprocal_scale(hash, hsize);
760 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
763 ct = nf_ct_tuplehash_to_ctrack(h);
764 if (nf_ct_is_expired(ct)) {
765 nf_ct_gc_expired(ct);
769 if (nf_ct_key_equal(h, tuple, zone, net))
773 * if the nulls value we got at the end of this lookup is
774 * not the expected one, we must restart lookup.
775 * We probably met an item that was moved to another chain.
777 if (get_nulls_value(n) != bucket) {
778 NF_CT_STAT_INC_ATOMIC(net, search_restart);
785 /* Find a connection corresponding to a tuple. */
786 static struct nf_conntrack_tuple_hash *
787 __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
788 const struct nf_conntrack_tuple *tuple, u32 hash)
790 struct nf_conntrack_tuple_hash *h;
795 h = ____nf_conntrack_find(net, zone, tuple, hash);
797 /* We have a candidate that matches the tuple we're interested
798 * in, try to obtain a reference and re-check tuple
800 ct = nf_ct_tuplehash_to_ctrack(h);
801 if (likely(refcount_inc_not_zero(&ct->ct_general.use))) {
802 /* re-check key after refcount */
803 smp_acquire__after_ctrl_dep();
805 if (likely(nf_ct_key_equal(h, tuple, zone, net)))
808 /* TYPESAFE_BY_RCU recycled the candidate */
820 struct nf_conntrack_tuple_hash *
821 nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
822 const struct nf_conntrack_tuple *tuple)
824 unsigned int rid, zone_id = nf_ct_zone_id(zone, IP_CT_DIR_ORIGINAL);
825 struct nf_conntrack_tuple_hash *thash;
827 thash = __nf_conntrack_find_get(net, zone, tuple,
828 hash_conntrack_raw(tuple, zone_id, net));
833 rid = nf_ct_zone_id(zone, IP_CT_DIR_REPLY);
835 return __nf_conntrack_find_get(net, zone, tuple,
836 hash_conntrack_raw(tuple, rid, net));
839 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
841 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
843 unsigned int reply_hash)
845 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
846 &nf_conntrack_hash[hash]);
847 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
848 &nf_conntrack_hash[reply_hash]);
851 static bool nf_ct_ext_valid_pre(const struct nf_ct_ext *ext)
853 /* if ext->gen_id is not equal to nf_conntrack_ext_genid, some extensions
854 * may contain stale pointers to e.g. helper that has been removed.
856 * The helper can't clear this because the nf_conn object isn't in
857 * any hash and synchronize_rcu() isn't enough because associated skb
858 * might sit in a queue.
860 return !ext || ext->gen_id == atomic_read(&nf_conntrack_ext_genid);
863 static bool nf_ct_ext_valid_post(struct nf_ct_ext *ext)
868 if (ext->gen_id != atomic_read(&nf_conntrack_ext_genid))
871 /* inserted into conntrack table, nf_ct_iterate_cleanup()
872 * will find it. Disable nf_ct_ext_find() id check.
874 WRITE_ONCE(ext->gen_id, 0);
879 nf_conntrack_hash_check_insert(struct nf_conn *ct)
881 const struct nf_conntrack_zone *zone;
882 struct net *net = nf_ct_net(ct);
883 unsigned int hash, reply_hash;
884 struct nf_conntrack_tuple_hash *h;
885 struct hlist_nulls_node *n;
886 unsigned int max_chainlen;
887 unsigned int chainlen = 0;
888 unsigned int sequence;
891 zone = nf_ct_zone(ct);
893 if (!nf_ct_ext_valid_pre(ct->ext)) {
894 NF_CT_STAT_INC(net, insert_failed);
900 sequence = read_seqcount_begin(&nf_conntrack_generation);
901 hash = hash_conntrack(net,
902 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
903 nf_ct_zone_id(nf_ct_zone(ct), IP_CT_DIR_ORIGINAL));
904 reply_hash = hash_conntrack(net,
905 &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
906 nf_ct_zone_id(nf_ct_zone(ct), IP_CT_DIR_REPLY));
907 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
909 max_chainlen = MIN_CHAINLEN + prandom_u32_max(MAX_CHAINLEN);
911 /* See if there's one in the list already, including reverse */
912 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode) {
913 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
917 if (chainlen++ > max_chainlen)
923 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode) {
924 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
927 if (chainlen++ > max_chainlen)
932 /* The caller holds a reference to this object */
933 refcount_set(&ct->ct_general.use, 2);
934 __nf_conntrack_hash_insert(ct, hash, reply_hash);
935 nf_conntrack_double_unlock(hash, reply_hash);
936 NF_CT_STAT_INC(net, insert);
939 if (!nf_ct_ext_valid_post(ct->ext)) {
941 NF_CT_STAT_INC(net, drop);
947 NF_CT_STAT_INC(net, chaintoolong);
950 nf_conntrack_double_unlock(hash, reply_hash);
954 EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
956 void nf_ct_acct_add(struct nf_conn *ct, u32 dir, unsigned int packets,
959 struct nf_conn_acct *acct;
961 acct = nf_conn_acct_find(ct);
963 struct nf_conn_counter *counter = acct->counter;
965 atomic64_add(packets, &counter[dir].packets);
966 atomic64_add(bytes, &counter[dir].bytes);
969 EXPORT_SYMBOL_GPL(nf_ct_acct_add);
971 static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
972 const struct nf_conn *loser_ct)
974 struct nf_conn_acct *acct;
976 acct = nf_conn_acct_find(loser_ct);
978 struct nf_conn_counter *counter = acct->counter;
981 /* u32 should be fine since we must have seen one packet. */
982 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
983 nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), bytes);
987 static void __nf_conntrack_insert_prepare(struct nf_conn *ct)
989 struct nf_conn_tstamp *tstamp;
991 refcount_inc(&ct->ct_general.use);
993 /* set conntrack timestamp, if enabled. */
994 tstamp = nf_conn_tstamp_find(ct);
996 tstamp->start = ktime_get_real_ns();
999 /* caller must hold locks to prevent concurrent changes */
1000 static int __nf_ct_resolve_clash(struct sk_buff *skb,
1001 struct nf_conntrack_tuple_hash *h)
1003 /* This is the conntrack entry already in hashes that won race. */
1004 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1005 enum ip_conntrack_info ctinfo;
1006 struct nf_conn *loser_ct;
1008 loser_ct = nf_ct_get(skb, &ctinfo);
1010 if (nf_ct_is_dying(ct))
1013 if (((ct->status & IPS_NAT_DONE_MASK) == 0) ||
1014 nf_ct_match(ct, loser_ct)) {
1015 struct net *net = nf_ct_net(ct);
1017 nf_conntrack_get(&ct->ct_general);
1019 nf_ct_acct_merge(ct, ctinfo, loser_ct);
1020 nf_ct_put(loser_ct);
1021 nf_ct_set(skb, ct, ctinfo);
1023 NF_CT_STAT_INC(net, clash_resolve);
1031 * nf_ct_resolve_clash_harder - attempt to insert clashing conntrack entry
1033 * @skb: skb that causes the collision
1034 * @repl_idx: hash slot for reply direction
1036 * Called when origin or reply direction had a clash.
1037 * The skb can be handled without packet drop provided the reply direction
1038 * is unique or there the existing entry has the identical tuple in both
1041 * Caller must hold conntrack table locks to prevent concurrent updates.
1043 * Returns NF_DROP if the clash could not be handled.
1045 static int nf_ct_resolve_clash_harder(struct sk_buff *skb, u32 repl_idx)
1047 struct nf_conn *loser_ct = (struct nf_conn *)skb_nfct(skb);
1048 const struct nf_conntrack_zone *zone;
1049 struct nf_conntrack_tuple_hash *h;
1050 struct hlist_nulls_node *n;
1053 zone = nf_ct_zone(loser_ct);
1054 net = nf_ct_net(loser_ct);
1056 /* Reply direction must never result in a clash, unless both origin
1057 * and reply tuples are identical.
1059 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[repl_idx], hnnode) {
1060 if (nf_ct_key_equal(h,
1061 &loser_ct->tuplehash[IP_CT_DIR_REPLY].tuple,
1063 return __nf_ct_resolve_clash(skb, h);
1066 /* We want the clashing entry to go away real soon: 1 second timeout. */
1067 WRITE_ONCE(loser_ct->timeout, nfct_time_stamp + HZ);
1069 /* IPS_NAT_CLASH removes the entry automatically on the first
1070 * reply. Also prevents UDP tracker from moving the entry to
1071 * ASSURED state, i.e. the entry can always be evicted under
1074 loser_ct->status |= IPS_FIXED_TIMEOUT | IPS_NAT_CLASH;
1076 __nf_conntrack_insert_prepare(loser_ct);
1078 /* fake add for ORIGINAL dir: we want lookups to only find the entry
1079 * already in the table. This also hides the clashing entry from
1080 * ctnetlink iteration, i.e. conntrack -L won't show them.
1082 hlist_nulls_add_fake(&loser_ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
1084 hlist_nulls_add_head_rcu(&loser_ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
1085 &nf_conntrack_hash[repl_idx]);
1087 NF_CT_STAT_INC(net, clash_resolve);
1092 * nf_ct_resolve_clash - attempt to handle clash without packet drop
1094 * @skb: skb that causes the clash
1095 * @h: tuplehash of the clashing entry already in table
1096 * @reply_hash: hash slot for reply direction
1098 * A conntrack entry can be inserted to the connection tracking table
1099 * if there is no existing entry with an identical tuple.
1101 * If there is one, @skb (and the assocated, unconfirmed conntrack) has
1102 * to be dropped. In case @skb is retransmitted, next conntrack lookup
1103 * will find the already-existing entry.
1105 * The major problem with such packet drop is the extra delay added by
1106 * the packet loss -- it will take some time for a retransmit to occur
1107 * (or the sender to time out when waiting for a reply).
1109 * This function attempts to handle the situation without packet drop.
1111 * If @skb has no NAT transformation or if the colliding entries are
1112 * exactly the same, only the to-be-confirmed conntrack entry is discarded
1113 * and @skb is associated with the conntrack entry already in the table.
1115 * Failing that, the new, unconfirmed conntrack is still added to the table
1116 * provided that the collision only occurs in the ORIGINAL direction.
1117 * The new entry will be added only in the non-clashing REPLY direction,
1118 * so packets in the ORIGINAL direction will continue to match the existing
1119 * entry. The new entry will also have a fixed timeout so it expires --
1120 * due to the collision, it will only see reply traffic.
1122 * Returns NF_DROP if the clash could not be resolved.
1124 static __cold noinline int
1125 nf_ct_resolve_clash(struct sk_buff *skb, struct nf_conntrack_tuple_hash *h,
1128 /* This is the conntrack entry already in hashes that won race. */
1129 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1130 const struct nf_conntrack_l4proto *l4proto;
1131 enum ip_conntrack_info ctinfo;
1132 struct nf_conn *loser_ct;
1136 loser_ct = nf_ct_get(skb, &ctinfo);
1137 net = nf_ct_net(loser_ct);
1139 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
1140 if (!l4proto->allow_clash)
1143 ret = __nf_ct_resolve_clash(skb, h);
1144 if (ret == NF_ACCEPT)
1147 ret = nf_ct_resolve_clash_harder(skb, reply_hash);
1148 if (ret == NF_ACCEPT)
1152 NF_CT_STAT_INC(net, drop);
1153 NF_CT_STAT_INC(net, insert_failed);
1157 /* Confirm a connection given skb; places it in hash table */
1159 __nf_conntrack_confirm(struct sk_buff *skb)
1161 unsigned int chainlen = 0, sequence, max_chainlen;
1162 const struct nf_conntrack_zone *zone;
1163 unsigned int hash, reply_hash;
1164 struct nf_conntrack_tuple_hash *h;
1166 struct nf_conn_help *help;
1167 struct hlist_nulls_node *n;
1168 enum ip_conntrack_info ctinfo;
1172 ct = nf_ct_get(skb, &ctinfo);
1173 net = nf_ct_net(ct);
1175 /* ipt_REJECT uses nf_conntrack_attach to attach related
1176 ICMP/TCP RST packets in other direction. Actual packet
1177 which created connection will be IP_CT_NEW or for an
1178 expected connection, IP_CT_RELATED. */
1179 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
1182 zone = nf_ct_zone(ct);
1186 sequence = read_seqcount_begin(&nf_conntrack_generation);
1187 /* reuse the hash saved before */
1188 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
1189 hash = scale_hash(hash);
1190 reply_hash = hash_conntrack(net,
1191 &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
1192 nf_ct_zone_id(nf_ct_zone(ct), IP_CT_DIR_REPLY));
1193 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
1195 /* We're not in hash table, and we refuse to set up related
1196 * connections for unconfirmed conns. But packet copies and
1197 * REJECT will give spurious warnings here.
1200 /* Another skb with the same unconfirmed conntrack may
1201 * win the race. This may happen for bridge(br_flood)
1202 * or broadcast/multicast packets do skb_clone with
1203 * unconfirmed conntrack.
1205 if (unlikely(nf_ct_is_confirmed(ct))) {
1207 nf_conntrack_double_unlock(hash, reply_hash);
1212 if (!nf_ct_ext_valid_pre(ct->ext)) {
1213 NF_CT_STAT_INC(net, insert_failed);
1217 pr_debug("Confirming conntrack %p\n", ct);
1218 /* We have to check the DYING flag after unlink to prevent
1219 * a race against nf_ct_get_next_corpse() possibly called from
1220 * user context, else we insert an already 'dead' hash, blocking
1221 * further use of that particular connection -JM.
1223 ct->status |= IPS_CONFIRMED;
1225 if (unlikely(nf_ct_is_dying(ct))) {
1226 NF_CT_STAT_INC(net, insert_failed);
1230 max_chainlen = MIN_CHAINLEN + prandom_u32_max(MAX_CHAINLEN);
1231 /* See if there's one in the list already, including reverse:
1232 NAT could have grabbed it without realizing, since we're
1233 not in the hash. If there is, we lost race. */
1234 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode) {
1235 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1238 if (chainlen++ > max_chainlen)
1243 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode) {
1244 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
1247 if (chainlen++ > max_chainlen) {
1249 NF_CT_STAT_INC(net, chaintoolong);
1250 NF_CT_STAT_INC(net, insert_failed);
1256 /* Timer relative to confirmation time, not original
1257 setting time, otherwise we'd get timer wrap in
1258 weird delay cases. */
1259 ct->timeout += nfct_time_stamp;
1261 __nf_conntrack_insert_prepare(ct);
1263 /* Since the lookup is lockless, hash insertion must be done after
1264 * starting the timer and setting the CONFIRMED bit. The RCU barriers
1265 * guarantee that no other CPU can find the conntrack before the above
1266 * stores are visible.
1268 __nf_conntrack_hash_insert(ct, hash, reply_hash);
1269 nf_conntrack_double_unlock(hash, reply_hash);
1272 /* ext area is still valid (rcu read lock is held,
1273 * but will go out of scope soon, we need to remove
1274 * this conntrack again.
1276 if (!nf_ct_ext_valid_post(ct->ext)) {
1278 NF_CT_STAT_INC(net, drop);
1282 help = nfct_help(ct);
1283 if (help && help->helper)
1284 nf_conntrack_event_cache(IPCT_HELPER, ct);
1286 nf_conntrack_event_cache(master_ct(ct) ?
1287 IPCT_RELATED : IPCT_NEW, ct);
1291 ret = nf_ct_resolve_clash(skb, h, reply_hash);
1293 nf_conntrack_double_unlock(hash, reply_hash);
1297 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
1299 /* Returns true if a connection correspondings to the tuple (required
1302 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
1303 const struct nf_conn *ignored_conntrack)
1305 struct net *net = nf_ct_net(ignored_conntrack);
1306 const struct nf_conntrack_zone *zone;
1307 struct nf_conntrack_tuple_hash *h;
1308 struct hlist_nulls_head *ct_hash;
1309 unsigned int hash, hsize;
1310 struct hlist_nulls_node *n;
1313 zone = nf_ct_zone(ignored_conntrack);
1317 nf_conntrack_get_ht(&ct_hash, &hsize);
1318 hash = __hash_conntrack(net, tuple, nf_ct_zone_id(zone, IP_CT_DIR_REPLY), hsize);
1320 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
1321 ct = nf_ct_tuplehash_to_ctrack(h);
1323 if (ct == ignored_conntrack)
1326 if (nf_ct_is_expired(ct)) {
1327 nf_ct_gc_expired(ct);
1331 if (nf_ct_key_equal(h, tuple, zone, net)) {
1332 /* Tuple is taken already, so caller will need to find
1333 * a new source port to use.
1336 * If the *original tuples* are identical, then both
1337 * conntracks refer to the same flow.
1338 * This is a rare situation, it can occur e.g. when
1339 * more than one UDP packet is sent from same socket
1340 * in different threads.
1342 * Let nf_ct_resolve_clash() deal with this later.
1344 if (nf_ct_tuple_equal(&ignored_conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1345 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple) &&
1346 nf_ct_zone_equal(ct, zone, IP_CT_DIR_ORIGINAL))
1349 NF_CT_STAT_INC_ATOMIC(net, found);
1355 if (get_nulls_value(n) != hash) {
1356 NF_CT_STAT_INC_ATOMIC(net, search_restart);
1364 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
1366 #define NF_CT_EVICTION_RANGE 8
1368 /* There's a small race here where we may free a just-assured
1369 connection. Too bad: we're in trouble anyway. */
1370 static unsigned int early_drop_list(struct net *net,
1371 struct hlist_nulls_head *head)
1373 struct nf_conntrack_tuple_hash *h;
1374 struct hlist_nulls_node *n;
1375 unsigned int drops = 0;
1376 struct nf_conn *tmp;
1378 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
1379 tmp = nf_ct_tuplehash_to_ctrack(h);
1381 if (test_bit(IPS_OFFLOAD_BIT, &tmp->status))
1384 if (nf_ct_is_expired(tmp)) {
1385 nf_ct_gc_expired(tmp);
1389 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
1390 !net_eq(nf_ct_net(tmp), net) ||
1391 nf_ct_is_dying(tmp))
1394 if (!refcount_inc_not_zero(&tmp->ct_general.use))
1397 /* load ->ct_net and ->status after refcount increase */
1398 smp_acquire__after_ctrl_dep();
1400 /* kill only if still in same netns -- might have moved due to
1401 * SLAB_TYPESAFE_BY_RCU rules.
1403 * We steal the timer reference. If that fails timer has
1404 * already fired or someone else deleted it. Just drop ref
1405 * and move to next entry.
1407 if (net_eq(nf_ct_net(tmp), net) &&
1408 nf_ct_is_confirmed(tmp) &&
1409 nf_ct_delete(tmp, 0, 0))
1418 static noinline int early_drop(struct net *net, unsigned int hash)
1420 unsigned int i, bucket;
1422 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
1423 struct hlist_nulls_head *ct_hash;
1424 unsigned int hsize, drops;
1427 nf_conntrack_get_ht(&ct_hash, &hsize);
1429 bucket = reciprocal_scale(hash, hsize);
1431 bucket = (bucket + 1) % hsize;
1433 drops = early_drop_list(net, &ct_hash[bucket]);
1437 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
1445 static bool gc_worker_skip_ct(const struct nf_conn *ct)
1447 return !nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct);
1450 static bool gc_worker_can_early_drop(const struct nf_conn *ct)
1452 const struct nf_conntrack_l4proto *l4proto;
1454 if (!test_bit(IPS_ASSURED_BIT, &ct->status))
1457 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
1458 if (l4proto->can_early_drop && l4proto->can_early_drop(ct))
1464 static void gc_worker(struct work_struct *work)
1466 unsigned int i, hashsz, nf_conntrack_max95 = 0;
1467 u32 end_time, start_time = nfct_time_stamp;
1468 struct conntrack_gc_work *gc_work;
1469 unsigned int expired_count = 0;
1470 unsigned long next_run;
1474 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
1476 i = gc_work->next_bucket;
1477 if (gc_work->early_drop)
1478 nf_conntrack_max95 = nf_conntrack_max / 100u * 95u;
1481 gc_work->avg_timeout = GC_SCAN_INTERVAL_INIT;
1482 gc_work->count = GC_SCAN_INITIAL_COUNT;
1483 gc_work->start_time = start_time;
1486 next_run = gc_work->avg_timeout;
1487 count = gc_work->count;
1489 end_time = start_time + GC_SCAN_MAX_DURATION;
1492 struct nf_conntrack_tuple_hash *h;
1493 struct hlist_nulls_head *ct_hash;
1494 struct hlist_nulls_node *n;
1495 struct nf_conn *tmp;
1499 nf_conntrack_get_ht(&ct_hash, &hashsz);
1505 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
1506 struct nf_conntrack_net *cnet;
1510 tmp = nf_ct_tuplehash_to_ctrack(h);
1512 if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) {
1513 nf_ct_offload_timeout(tmp);
1517 if (expired_count > GC_SCAN_EXPIRED_MAX) {
1520 gc_work->next_bucket = i;
1521 gc_work->avg_timeout = next_run;
1522 gc_work->count = count;
1524 delta_time = nfct_time_stamp - gc_work->start_time;
1526 /* re-sched immediately if total cycle time is exceeded */
1527 next_run = delta_time < (s32)GC_SCAN_INTERVAL_MAX;
1531 if (nf_ct_is_expired(tmp)) {
1532 nf_ct_gc_expired(tmp);
1537 expires = clamp(nf_ct_expires(tmp), GC_SCAN_INTERVAL_MIN, GC_SCAN_INTERVAL_CLAMP);
1538 expires = (expires - (long)next_run) / ++count;
1539 next_run += expires;
1541 if (nf_conntrack_max95 == 0 || gc_worker_skip_ct(tmp))
1544 net = nf_ct_net(tmp);
1545 cnet = nf_ct_pernet(net);
1546 if (atomic_read(&cnet->count) < nf_conntrack_max95)
1549 /* need to take reference to avoid possible races */
1550 if (!refcount_inc_not_zero(&tmp->ct_general.use))
1553 /* load ->status after refcount increase */
1554 smp_acquire__after_ctrl_dep();
1556 if (gc_worker_skip_ct(tmp)) {
1561 if (gc_worker_can_early_drop(tmp)) {
1569 /* could check get_nulls_value() here and restart if ct
1570 * was moved to another chain. But given gc is best-effort
1571 * we will just continue with next hash slot.
1577 delta_time = nfct_time_stamp - end_time;
1578 if (delta_time > 0 && i < hashsz) {
1579 gc_work->avg_timeout = next_run;
1580 gc_work->count = count;
1581 gc_work->next_bucket = i;
1585 } while (i < hashsz);
1587 gc_work->next_bucket = 0;
1589 next_run = clamp(next_run, GC_SCAN_INTERVAL_MIN, GC_SCAN_INTERVAL_MAX);
1591 delta_time = max_t(s32, nfct_time_stamp - gc_work->start_time, 1);
1592 if (next_run > (unsigned long)delta_time)
1593 next_run -= delta_time;
1598 if (gc_work->exiting)
1602 gc_work->early_drop = false;
1604 queue_delayed_work(system_power_efficient_wq, &gc_work->dwork, next_run);
1607 static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
1609 INIT_DELAYED_WORK(&gc_work->dwork, gc_worker);
1610 gc_work->exiting = false;
1613 static struct nf_conn *
1614 __nf_conntrack_alloc(struct net *net,
1615 const struct nf_conntrack_zone *zone,
1616 const struct nf_conntrack_tuple *orig,
1617 const struct nf_conntrack_tuple *repl,
1618 gfp_t gfp, u32 hash)
1620 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1621 unsigned int ct_count;
1624 /* We don't want any race condition at early drop stage */
1625 ct_count = atomic_inc_return(&cnet->count);
1627 if (nf_conntrack_max && unlikely(ct_count > nf_conntrack_max)) {
1628 if (!early_drop(net, hash)) {
1629 if (!conntrack_gc_work.early_drop)
1630 conntrack_gc_work.early_drop = true;
1631 atomic_dec(&cnet->count);
1632 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
1633 return ERR_PTR(-ENOMEM);
1638 * Do not use kmem_cache_zalloc(), as this cache uses
1639 * SLAB_TYPESAFE_BY_RCU.
1641 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
1645 spin_lock_init(&ct->lock);
1646 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
1647 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
1648 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
1649 /* save hash for reusing when confirming */
1650 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
1652 WRITE_ONCE(ct->timeout, 0);
1653 write_pnet(&ct->ct_net, net);
1654 memset_after(ct, 0, __nfct_init_offset);
1656 nf_ct_zone_add(ct, zone);
1658 /* Because we use RCU lookups, we set ct_general.use to zero before
1659 * this is inserted in any list.
1661 refcount_set(&ct->ct_general.use, 0);
1664 atomic_dec(&cnet->count);
1665 return ERR_PTR(-ENOMEM);
1668 struct nf_conn *nf_conntrack_alloc(struct net *net,
1669 const struct nf_conntrack_zone *zone,
1670 const struct nf_conntrack_tuple *orig,
1671 const struct nf_conntrack_tuple *repl,
1674 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1676 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
1678 void nf_conntrack_free(struct nf_conn *ct)
1680 struct net *net = nf_ct_net(ct);
1681 struct nf_conntrack_net *cnet;
1683 /* A freed object has refcnt == 0, that's
1684 * the golden rule for SLAB_TYPESAFE_BY_RCU
1686 WARN_ON(refcount_read(&ct->ct_general.use) != 0);
1688 if (ct->status & IPS_SRC_NAT_DONE) {
1689 const struct nf_nat_hook *nat_hook;
1692 nat_hook = rcu_dereference(nf_nat_hook);
1694 nat_hook->remove_nat_bysrc(ct);
1699 kmem_cache_free(nf_conntrack_cachep, ct);
1700 cnet = nf_ct_pernet(net);
1702 smp_mb__before_atomic();
1703 atomic_dec(&cnet->count);
1705 EXPORT_SYMBOL_GPL(nf_conntrack_free);
1708 /* Allocate a new conntrack: we return -ENOMEM if classification
1709 failed due to stress. Otherwise it really is unclassifiable. */
1710 static noinline struct nf_conntrack_tuple_hash *
1711 init_conntrack(struct net *net, struct nf_conn *tmpl,
1712 const struct nf_conntrack_tuple *tuple,
1713 struct sk_buff *skb,
1714 unsigned int dataoff, u32 hash)
1717 struct nf_conn_help *help;
1718 struct nf_conntrack_tuple repl_tuple;
1719 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1720 struct nf_conntrack_ecache *ecache;
1722 struct nf_conntrack_expect *exp = NULL;
1723 const struct nf_conntrack_zone *zone;
1724 struct nf_conn_timeout *timeout_ext;
1725 struct nf_conntrack_zone tmp;
1726 struct nf_conntrack_net *cnet;
1728 if (!nf_ct_invert_tuple(&repl_tuple, tuple)) {
1729 pr_debug("Can't invert tuple.\n");
1733 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1734 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1737 return (struct nf_conntrack_tuple_hash *)ct;
1739 if (!nf_ct_add_synproxy(ct, tmpl)) {
1740 nf_conntrack_free(ct);
1741 return ERR_PTR(-ENOMEM);
1744 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
1747 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1750 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1751 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1752 nf_ct_labels_ext_add(ct);
1754 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1755 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1757 if ((ecache || net->ct.sysctl_events) &&
1758 !nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1759 ecache ? ecache->expmask : 0,
1761 nf_conntrack_free(ct);
1762 return ERR_PTR(-ENOMEM);
1766 cnet = nf_ct_pernet(net);
1767 if (cnet->expect_count) {
1768 spin_lock_bh(&nf_conntrack_expect_lock);
1769 exp = nf_ct_find_expectation(net, zone, tuple);
1771 pr_debug("expectation arrives ct=%p exp=%p\n",
1773 /* Welcome, Mr. Bond. We've been expecting you... */
1774 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1775 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1776 ct->master = exp->master;
1778 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1780 rcu_assign_pointer(help->helper, exp->helper);
1783 #ifdef CONFIG_NF_CONNTRACK_MARK
1784 ct->mark = exp->master->mark;
1786 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1787 ct->secmark = exp->master->secmark;
1789 NF_CT_STAT_INC(net, expect_new);
1791 spin_unlock_bh(&nf_conntrack_expect_lock);
1794 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
1796 /* Other CPU might have obtained a pointer to this object before it was
1797 * released. Because refcount is 0, refcount_inc_not_zero() will fail.
1799 * After refcount_set(1) it will succeed; ensure that zeroing of
1800 * ct->status and the correct ct->net pointer are visible; else other
1801 * core might observe CONFIRMED bit which means the entry is valid and
1802 * in the hash table, but its not (anymore).
1806 /* Now it is going to be associated with an sk_buff, set refcount to 1. */
1807 refcount_set(&ct->ct_general.use, 1);
1811 exp->expectfn(ct, exp);
1812 nf_ct_expect_put(exp);
1815 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
1818 /* On success, returns 0, sets skb->_nfct | ctinfo */
1820 resolve_normal_ct(struct nf_conn *tmpl,
1821 struct sk_buff *skb,
1822 unsigned int dataoff,
1824 const struct nf_hook_state *state)
1826 const struct nf_conntrack_zone *zone;
1827 struct nf_conntrack_tuple tuple;
1828 struct nf_conntrack_tuple_hash *h;
1829 enum ip_conntrack_info ctinfo;
1830 struct nf_conntrack_zone tmp;
1831 u32 hash, zone_id, rid;
1834 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
1835 dataoff, state->pf, protonum, state->net,
1837 pr_debug("Can't get tuple\n");
1841 /* look for tuple match */
1842 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1844 zone_id = nf_ct_zone_id(zone, IP_CT_DIR_ORIGINAL);
1845 hash = hash_conntrack_raw(&tuple, zone_id, state->net);
1846 h = __nf_conntrack_find_get(state->net, zone, &tuple, hash);
1849 rid = nf_ct_zone_id(zone, IP_CT_DIR_REPLY);
1850 if (zone_id != rid) {
1851 u32 tmp = hash_conntrack_raw(&tuple, rid, state->net);
1853 h = __nf_conntrack_find_get(state->net, zone, &tuple, tmp);
1858 h = init_conntrack(state->net, tmpl, &tuple,
1859 skb, dataoff, hash);
1865 ct = nf_ct_tuplehash_to_ctrack(h);
1867 /* It exists; we have (non-exclusive) reference. */
1868 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
1869 ctinfo = IP_CT_ESTABLISHED_REPLY;
1871 /* Once we've had two way comms, always ESTABLISHED. */
1872 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1873 pr_debug("normal packet for %p\n", ct);
1874 ctinfo = IP_CT_ESTABLISHED;
1875 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
1876 pr_debug("related packet for %p\n", ct);
1877 ctinfo = IP_CT_RELATED;
1879 pr_debug("new packet for %p\n", ct);
1883 nf_ct_set(skb, ct, ctinfo);
1888 * icmp packets need special treatment to handle error messages that are
1889 * related to a connection.
1891 * Callers need to check if skb has a conntrack assigned when this
1892 * helper returns; in such case skb belongs to an already known connection.
1894 static unsigned int __cold
1895 nf_conntrack_handle_icmp(struct nf_conn *tmpl,
1896 struct sk_buff *skb,
1897 unsigned int dataoff,
1899 const struct nf_hook_state *state)
1903 if (state->pf == NFPROTO_IPV4 && protonum == IPPROTO_ICMP)
1904 ret = nf_conntrack_icmpv4_error(tmpl, skb, dataoff, state);
1905 #if IS_ENABLED(CONFIG_IPV6)
1906 else if (state->pf == NFPROTO_IPV6 && protonum == IPPROTO_ICMPV6)
1907 ret = nf_conntrack_icmpv6_error(tmpl, skb, dataoff, state);
1913 NF_CT_STAT_INC_ATOMIC(state->net, error);
1918 static int generic_packet(struct nf_conn *ct, struct sk_buff *skb,
1919 enum ip_conntrack_info ctinfo)
1921 const unsigned int *timeout = nf_ct_timeout_lookup(ct);
1924 timeout = &nf_generic_pernet(nf_ct_net(ct))->timeout;
1926 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
1930 /* Returns verdict for packet, or -1 for invalid. */
1931 static int nf_conntrack_handle_packet(struct nf_conn *ct,
1932 struct sk_buff *skb,
1933 unsigned int dataoff,
1934 enum ip_conntrack_info ctinfo,
1935 const struct nf_hook_state *state)
1937 switch (nf_ct_protonum(ct)) {
1939 return nf_conntrack_tcp_packet(ct, skb, dataoff,
1942 return nf_conntrack_udp_packet(ct, skb, dataoff,
1945 return nf_conntrack_icmp_packet(ct, skb, ctinfo, state);
1946 #if IS_ENABLED(CONFIG_IPV6)
1947 case IPPROTO_ICMPV6:
1948 return nf_conntrack_icmpv6_packet(ct, skb, ctinfo, state);
1950 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
1951 case IPPROTO_UDPLITE:
1952 return nf_conntrack_udplite_packet(ct, skb, dataoff,
1955 #ifdef CONFIG_NF_CT_PROTO_SCTP
1957 return nf_conntrack_sctp_packet(ct, skb, dataoff,
1960 #ifdef CONFIG_NF_CT_PROTO_DCCP
1962 return nf_conntrack_dccp_packet(ct, skb, dataoff,
1965 #ifdef CONFIG_NF_CT_PROTO_GRE
1967 return nf_conntrack_gre_packet(ct, skb, dataoff,
1972 return generic_packet(ct, skb, ctinfo);
1976 nf_conntrack_in(struct sk_buff *skb, const struct nf_hook_state *state)
1978 enum ip_conntrack_info ctinfo;
1979 struct nf_conn *ct, *tmpl;
1983 tmpl = nf_ct_get(skb, &ctinfo);
1984 if (tmpl || ctinfo == IP_CT_UNTRACKED) {
1985 /* Previously seen (loopback or untracked)? Ignore. */
1986 if ((tmpl && !nf_ct_is_template(tmpl)) ||
1987 ctinfo == IP_CT_UNTRACKED)
1992 /* rcu_read_lock()ed by nf_hook_thresh */
1993 dataoff = get_l4proto(skb, skb_network_offset(skb), state->pf, &protonum);
1995 pr_debug("not prepared to track yet or error occurred\n");
1996 NF_CT_STAT_INC_ATOMIC(state->net, invalid);
2001 if (protonum == IPPROTO_ICMP || protonum == IPPROTO_ICMPV6) {
2002 ret = nf_conntrack_handle_icmp(tmpl, skb, dataoff,
2008 /* ICMP[v6] protocol trackers may assign one conntrack. */
2013 ret = resolve_normal_ct(tmpl, skb, dataoff,
2016 /* Too stressed to deal. */
2017 NF_CT_STAT_INC_ATOMIC(state->net, drop);
2022 ct = nf_ct_get(skb, &ctinfo);
2024 /* Not valid part of a connection */
2025 NF_CT_STAT_INC_ATOMIC(state->net, invalid);
2030 ret = nf_conntrack_handle_packet(ct, skb, dataoff, ctinfo, state);
2032 /* Invalid: inverse of the return code tells
2033 * the netfilter core what to do */
2034 pr_debug("nf_conntrack_in: Can't track with proto module\n");
2037 /* Special case: TCP tracker reports an attempt to reopen a
2038 * closed/aborted connection. We have to go back and create a
2041 if (ret == -NF_REPEAT)
2044 NF_CT_STAT_INC_ATOMIC(state->net, invalid);
2045 if (ret == -NF_DROP)
2046 NF_CT_STAT_INC_ATOMIC(state->net, drop);
2052 if (ctinfo == IP_CT_ESTABLISHED_REPLY &&
2053 !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
2054 nf_conntrack_event_cache(IPCT_REPLY, ct);
2061 EXPORT_SYMBOL_GPL(nf_conntrack_in);
2063 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
2064 implicitly racy: see __nf_conntrack_confirm */
2065 void nf_conntrack_alter_reply(struct nf_conn *ct,
2066 const struct nf_conntrack_tuple *newreply)
2068 struct nf_conn_help *help = nfct_help(ct);
2070 /* Should be unconfirmed, so not in hash table yet */
2071 WARN_ON(nf_ct_is_confirmed(ct));
2073 pr_debug("Altering reply tuple of %p to ", ct);
2074 nf_ct_dump_tuple(newreply);
2076 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
2077 if (ct->master || (help && !hlist_empty(&help->expectations)))
2080 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
2082 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
2083 void __nf_ct_refresh_acct(struct nf_conn *ct,
2084 enum ip_conntrack_info ctinfo,
2085 const struct sk_buff *skb,
2089 /* Only update if this is not a fixed timeout */
2090 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
2093 /* If not in hash table, timer will not be active yet */
2094 if (nf_ct_is_confirmed(ct))
2095 extra_jiffies += nfct_time_stamp;
2097 if (READ_ONCE(ct->timeout) != extra_jiffies)
2098 WRITE_ONCE(ct->timeout, extra_jiffies);
2101 nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), skb->len);
2103 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
2105 bool nf_ct_kill_acct(struct nf_conn *ct,
2106 enum ip_conntrack_info ctinfo,
2107 const struct sk_buff *skb)
2109 nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), skb->len);
2111 return nf_ct_delete(ct, 0, 0);
2113 EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
2115 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
2117 #include <linux/netfilter/nfnetlink.h>
2118 #include <linux/netfilter/nfnetlink_conntrack.h>
2119 #include <linux/mutex.h>
2121 /* Generic function for tcp/udp/sctp/dccp and alike. */
2122 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
2123 const struct nf_conntrack_tuple *tuple)
2125 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
2126 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
2127 goto nla_put_failure;
2133 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
2135 const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
2136 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
2137 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
2139 EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
2141 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
2142 struct nf_conntrack_tuple *t,
2145 if (flags & CTA_FILTER_FLAG(CTA_PROTO_SRC_PORT)) {
2146 if (!tb[CTA_PROTO_SRC_PORT])
2149 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
2152 if (flags & CTA_FILTER_FLAG(CTA_PROTO_DST_PORT)) {
2153 if (!tb[CTA_PROTO_DST_PORT])
2156 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
2161 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
2163 unsigned int nf_ct_port_nlattr_tuple_size(void)
2165 static unsigned int size __read_mostly;
2168 size = nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
2172 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
2175 /* Used by ipt_REJECT and ip6t_REJECT. */
2176 static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
2179 enum ip_conntrack_info ctinfo;
2181 /* This ICMP is in reverse direction to the packet which caused it */
2182 ct = nf_ct_get(skb, &ctinfo);
2183 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
2184 ctinfo = IP_CT_RELATED_REPLY;
2186 ctinfo = IP_CT_RELATED;
2188 /* Attach to new skbuff, and increment count */
2189 nf_ct_set(nskb, ct, ctinfo);
2190 nf_conntrack_get(skb_nfct(nskb));
2193 static int __nf_conntrack_update(struct net *net, struct sk_buff *skb,
2195 enum ip_conntrack_info ctinfo)
2197 const struct nf_nat_hook *nat_hook;
2198 struct nf_conntrack_tuple_hash *h;
2199 struct nf_conntrack_tuple tuple;
2200 unsigned int status;
2205 l3num = nf_ct_l3num(ct);
2207 dataoff = get_l4proto(skb, skb_network_offset(skb), l3num, &l4num);
2211 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
2212 l4num, net, &tuple))
2215 if (ct->status & IPS_SRC_NAT) {
2216 memcpy(tuple.src.u3.all,
2217 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.all,
2218 sizeof(tuple.src.u3.all));
2220 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all;
2223 if (ct->status & IPS_DST_NAT) {
2224 memcpy(tuple.dst.u3.all,
2225 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.all,
2226 sizeof(tuple.dst.u3.all));
2228 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all;
2231 h = nf_conntrack_find_get(net, nf_ct_zone(ct), &tuple);
2235 /* Store status bits of the conntrack that is clashing to re-do NAT
2236 * mangling according to what it has been done already to this packet.
2238 status = ct->status;
2241 ct = nf_ct_tuplehash_to_ctrack(h);
2242 nf_ct_set(skb, ct, ctinfo);
2244 nat_hook = rcu_dereference(nf_nat_hook);
2248 if (status & IPS_SRC_NAT &&
2249 nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_SRC,
2250 IP_CT_DIR_ORIGINAL) == NF_DROP)
2253 if (status & IPS_DST_NAT &&
2254 nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_DST,
2255 IP_CT_DIR_ORIGINAL) == NF_DROP)
2261 /* This packet is coming from userspace via nf_queue, complete the packet
2262 * processing after the helper invocation in nf_confirm().
2264 static int nf_confirm_cthelper(struct sk_buff *skb, struct nf_conn *ct,
2265 enum ip_conntrack_info ctinfo)
2267 const struct nf_conntrack_helper *helper;
2268 const struct nf_conn_help *help;
2271 help = nfct_help(ct);
2275 helper = rcu_dereference(help->helper);
2276 if (!(helper->flags & NF_CT_HELPER_F_USERSPACE))
2279 switch (nf_ct_l3num(ct)) {
2281 protoff = skb_network_offset(skb) + ip_hdrlen(skb);
2283 #if IS_ENABLED(CONFIG_IPV6)
2284 case NFPROTO_IPV6: {
2288 pnum = ipv6_hdr(skb)->nexthdr;
2289 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum,
2291 if (protoff < 0 || (frag_off & htons(~0x7)) != 0)
2300 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
2301 !nf_is_loopback_packet(skb)) {
2302 if (!nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) {
2303 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
2308 /* We've seen it coming out the other side: confirm it */
2309 return nf_conntrack_confirm(skb) == NF_DROP ? - 1 : 0;
2312 static int nf_conntrack_update(struct net *net, struct sk_buff *skb)
2314 enum ip_conntrack_info ctinfo;
2318 ct = nf_ct_get(skb, &ctinfo);
2322 if (!nf_ct_is_confirmed(ct)) {
2323 err = __nf_conntrack_update(net, skb, ct, ctinfo);
2327 ct = nf_ct_get(skb, &ctinfo);
2330 return nf_confirm_cthelper(skb, ct, ctinfo);
2333 static bool nf_conntrack_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
2334 const struct sk_buff *skb)
2336 const struct nf_conntrack_tuple *src_tuple;
2337 const struct nf_conntrack_tuple_hash *hash;
2338 struct nf_conntrack_tuple srctuple;
2339 enum ip_conntrack_info ctinfo;
2342 ct = nf_ct_get(skb, &ctinfo);
2344 src_tuple = nf_ct_tuple(ct, CTINFO2DIR(ctinfo));
2345 memcpy(dst_tuple, src_tuple, sizeof(*dst_tuple));
2349 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
2350 NFPROTO_IPV4, dev_net(skb->dev),
2354 hash = nf_conntrack_find_get(dev_net(skb->dev),
2360 ct = nf_ct_tuplehash_to_ctrack(hash);
2361 src_tuple = nf_ct_tuple(ct, !hash->tuple.dst.dir);
2362 memcpy(dst_tuple, src_tuple, sizeof(*dst_tuple));
2368 /* Bring out ya dead! */
2369 static struct nf_conn *
2370 get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
2371 const struct nf_ct_iter_data *iter_data, unsigned int *bucket)
2373 struct nf_conntrack_tuple_hash *h;
2375 struct hlist_nulls_node *n;
2378 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
2379 struct hlist_nulls_head *hslot = &nf_conntrack_hash[*bucket];
2381 if (hlist_nulls_empty(hslot))
2384 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
2386 nf_conntrack_lock(lockp);
2387 hlist_nulls_for_each_entry(h, n, hslot, hnnode) {
2388 if (NF_CT_DIRECTION(h) != IP_CT_DIR_REPLY)
2390 /* All nf_conn objects are added to hash table twice, one
2391 * for original direction tuple, once for the reply tuple.
2393 * Exception: In the IPS_NAT_CLASH case, only the reply
2394 * tuple is added (the original tuple already existed for
2395 * a different object).
2397 * We only need to call the iterator once for each
2398 * conntrack, so we just use the 'reply' direction
2399 * tuple while iterating.
2401 ct = nf_ct_tuplehash_to_ctrack(h);
2403 if (iter_data->net &&
2404 !net_eq(iter_data->net, nf_ct_net(ct)))
2407 if (iter(ct, iter_data->data))
2417 refcount_inc(&ct->ct_general.use);
2423 static void nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data),
2424 const struct nf_ct_iter_data *iter_data)
2426 unsigned int bucket = 0;
2431 mutex_lock(&nf_conntrack_mutex);
2432 while ((ct = get_next_corpse(iter, iter_data, &bucket)) != NULL) {
2433 /* Time to push up daises... */
2435 nf_ct_delete(ct, iter_data->portid, iter_data->report);
2439 mutex_unlock(&nf_conntrack_mutex);
2442 void nf_ct_iterate_cleanup_net(int (*iter)(struct nf_conn *i, void *data),
2443 const struct nf_ct_iter_data *iter_data)
2445 struct net *net = iter_data->net;
2446 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
2450 if (atomic_read(&cnet->count) == 0)
2453 nf_ct_iterate_cleanup(iter, iter_data);
2455 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup_net);
2458 * nf_ct_iterate_destroy - destroy unconfirmed conntracks and iterate table
2459 * @iter: callback to invoke for each conntrack
2460 * @data: data to pass to @iter
2462 * Like nf_ct_iterate_cleanup, but first marks conntracks on the
2463 * unconfirmed list as dying (so they will not be inserted into
2466 * Can only be called in module exit path.
2469 nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
2471 struct nf_ct_iter_data iter_data = {};
2474 down_read(&net_rwsem);
2476 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
2478 if (atomic_read(&cnet->count) == 0)
2480 nf_queue_nf_hook_drop(net);
2482 up_read(&net_rwsem);
2484 /* Need to wait for netns cleanup worker to finish, if its
2485 * running -- it might have deleted a net namespace from
2486 * the global list, so hook drop above might not have
2487 * affected all namespaces.
2491 /* a skb w. unconfirmed conntrack could have been reinjected just
2492 * before we called nf_queue_nf_hook_drop().
2494 * This makes sure its inserted into conntrack table.
2498 nf_ct_ext_bump_genid();
2499 iter_data.data = data;
2500 nf_ct_iterate_cleanup(iter, &iter_data);
2502 /* Another cpu might be in a rcu read section with
2503 * rcu protected pointer cleared in iter callback
2504 * or hidden via nf_ct_ext_bump_genid() above.
2506 * Wait until those are done.
2510 EXPORT_SYMBOL_GPL(nf_ct_iterate_destroy);
2512 static int kill_all(struct nf_conn *i, void *data)
2517 void nf_conntrack_cleanup_start(void)
2519 conntrack_gc_work.exiting = true;
2522 void nf_conntrack_cleanup_end(void)
2524 RCU_INIT_POINTER(nf_ct_hook, NULL);
2525 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
2526 kvfree(nf_conntrack_hash);
2528 nf_conntrack_proto_fini();
2529 nf_conntrack_helper_fini();
2530 nf_conntrack_expect_fini();
2532 kmem_cache_destroy(nf_conntrack_cachep);
2536 * Mishearing the voices in his head, our hero wonders how he's
2537 * supposed to kill the mall.
2539 void nf_conntrack_cleanup_net(struct net *net)
2543 list_add(&net->exit_list, &single);
2544 nf_conntrack_cleanup_net_list(&single);
2547 void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
2549 struct nf_ct_iter_data iter_data = {};
2554 * This makes sure all current packets have passed through
2555 * netfilter framework. Roll on, two-stage module
2561 list_for_each_entry(net, net_exit_list, exit_list) {
2562 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
2564 iter_data.net = net;
2565 nf_ct_iterate_cleanup_net(kill_all, &iter_data);
2566 if (atomic_read(&cnet->count) != 0)
2571 goto i_see_dead_people;
2574 list_for_each_entry(net, net_exit_list, exit_list) {
2575 nf_conntrack_ecache_pernet_fini(net);
2576 nf_conntrack_expect_pernet_fini(net);
2577 free_percpu(net->ct.stat);
2581 void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
2583 struct hlist_nulls_head *hash;
2584 unsigned int nr_slots, i;
2586 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
2589 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
2590 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
2592 hash = kvcalloc(nr_slots, sizeof(struct hlist_nulls_head), GFP_KERNEL);
2595 for (i = 0; i < nr_slots; i++)
2596 INIT_HLIST_NULLS_HEAD(&hash[i], i);
2600 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
2602 int nf_conntrack_hash_resize(unsigned int hashsize)
2605 unsigned int old_size;
2606 struct hlist_nulls_head *hash, *old_hash;
2607 struct nf_conntrack_tuple_hash *h;
2613 hash = nf_ct_alloc_hashtable(&hashsize, 1);
2617 mutex_lock(&nf_conntrack_mutex);
2618 old_size = nf_conntrack_htable_size;
2619 if (old_size == hashsize) {
2620 mutex_unlock(&nf_conntrack_mutex);
2626 nf_conntrack_all_lock();
2627 write_seqcount_begin(&nf_conntrack_generation);
2629 /* Lookups in the old hash might happen in parallel, which means we
2630 * might get false negatives during connection lookup. New connections
2631 * created because of a false negative won't make it into the hash
2632 * though since that required taking the locks.
2635 for (i = 0; i < nf_conntrack_htable_size; i++) {
2636 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
2637 unsigned int zone_id;
2639 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
2640 struct nf_conntrack_tuple_hash, hnnode);
2641 ct = nf_ct_tuplehash_to_ctrack(h);
2642 hlist_nulls_del_rcu(&h->hnnode);
2644 zone_id = nf_ct_zone_id(nf_ct_zone(ct), NF_CT_DIRECTION(h));
2645 bucket = __hash_conntrack(nf_ct_net(ct),
2646 &h->tuple, zone_id, hashsize);
2647 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
2650 old_hash = nf_conntrack_hash;
2652 nf_conntrack_hash = hash;
2653 nf_conntrack_htable_size = hashsize;
2655 write_seqcount_end(&nf_conntrack_generation);
2656 nf_conntrack_all_unlock();
2659 mutex_unlock(&nf_conntrack_mutex);
2666 int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp)
2668 unsigned int hashsize;
2671 if (current->nsproxy->net_ns != &init_net)
2674 /* On boot, we can set this without any fancy locking. */
2675 if (!nf_conntrack_hash)
2676 return param_set_uint(val, kp);
2678 rc = kstrtouint(val, 0, &hashsize);
2682 return nf_conntrack_hash_resize(hashsize);
2685 int nf_conntrack_init_start(void)
2687 unsigned long nr_pages = totalram_pages();
2692 seqcount_spinlock_init(&nf_conntrack_generation,
2693 &nf_conntrack_locks_all_lock);
2695 for (i = 0; i < CONNTRACK_LOCKS; i++)
2696 spin_lock_init(&nf_conntrack_locks[i]);
2698 if (!nf_conntrack_htable_size) {
2699 nf_conntrack_htable_size
2700 = (((nr_pages << PAGE_SHIFT) / 16384)
2701 / sizeof(struct hlist_head));
2702 if (BITS_PER_LONG >= 64 &&
2703 nr_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
2704 nf_conntrack_htable_size = 262144;
2705 else if (nr_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
2706 nf_conntrack_htable_size = 65536;
2708 if (nf_conntrack_htable_size < 1024)
2709 nf_conntrack_htable_size = 1024;
2710 /* Use a max. factor of one by default to keep the average
2711 * hash chain length at 2 entries. Each entry has to be added
2712 * twice (once for original direction, once for reply).
2713 * When a table size is given we use the old value of 8 to
2714 * avoid implicit reduction of the max entries setting.
2719 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
2720 if (!nf_conntrack_hash)
2723 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
2725 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
2726 sizeof(struct nf_conn),
2728 SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
2729 if (!nf_conntrack_cachep)
2732 ret = nf_conntrack_expect_init();
2736 ret = nf_conntrack_helper_init();
2740 ret = nf_conntrack_proto_init();
2744 conntrack_gc_work_init(&conntrack_gc_work);
2745 queue_delayed_work(system_power_efficient_wq, &conntrack_gc_work.dwork, HZ);
2747 ret = register_nf_conntrack_bpf();
2754 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
2755 nf_conntrack_proto_fini();
2757 nf_conntrack_helper_fini();
2759 nf_conntrack_expect_fini();
2761 kmem_cache_destroy(nf_conntrack_cachep);
2763 kvfree(nf_conntrack_hash);
2767 static const struct nf_ct_hook nf_conntrack_hook = {
2768 .update = nf_conntrack_update,
2769 .destroy = nf_ct_destroy,
2770 .get_tuple_skb = nf_conntrack_get_tuple_skb,
2771 .attach = nf_conntrack_attach,
2774 void nf_conntrack_init_end(void)
2776 RCU_INIT_POINTER(nf_ct_hook, &nf_conntrack_hook);
2780 * We need to use special "null" values, not used in hash table
2782 #define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
2784 int nf_conntrack_init_net(struct net *net)
2786 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
2789 BUILD_BUG_ON(IP_CT_UNTRACKED == IP_CT_NUMBER);
2790 BUILD_BUG_ON_NOT_POWER_OF_2(CONNTRACK_LOCKS);
2791 atomic_set(&cnet->count, 0);
2793 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
2797 ret = nf_conntrack_expect_pernet_init(net);
2801 nf_conntrack_acct_pernet_init(net);
2802 nf_conntrack_tstamp_pernet_init(net);
2803 nf_conntrack_ecache_pernet_init(net);
2804 nf_conntrack_proto_pernet_init(net);
2809 free_percpu(net->ct.stat);
2813 /* ctnetlink code shared by both ctnetlink and nf_conntrack_bpf */
2815 int __nf_ct_change_timeout(struct nf_conn *ct, u64 timeout)
2817 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
2820 __nf_ct_set_timeout(ct, timeout);
2822 if (test_bit(IPS_DYING_BIT, &ct->status))
2827 EXPORT_SYMBOL_GPL(__nf_ct_change_timeout);
2829 void __nf_ct_change_status(struct nf_conn *ct, unsigned long on, unsigned long off)
2833 /* Ignore these unchangable bits */
2834 on &= ~IPS_UNCHANGEABLE_MASK;
2835 off &= ~IPS_UNCHANGEABLE_MASK;
2837 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
2838 if (on & (1 << bit))
2839 set_bit(bit, &ct->status);
2840 else if (off & (1 << bit))
2841 clear_bit(bit, &ct->status);
2844 EXPORT_SYMBOL_GPL(__nf_ct_change_status);
2846 int nf_ct_change_status_common(struct nf_conn *ct, unsigned int status)
2850 d = ct->status ^ status;
2852 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
2856 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2857 /* SEEN_REPLY bit can only be set */
2860 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2861 /* ASSURED bit can only be set */
2864 __nf_ct_change_status(ct, status, 0);
2867 EXPORT_SYMBOL_GPL(nf_ct_change_status_common);