Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: conntrack: small refactoring of conntrack seq_printf

The iteration process is lockless, so we test if the conntrack object is
eligible for printing (e.g. is AF_INET) after obtaining the reference
count.

Once we put all conntracks into same hash table we might see more
entries that need to be skipped.

So add a helper and first perform the test in a lockless fashion
for fast skip.

Once we obtain the reference count, just repeat the check.

Note that this refactoring also includes a missing check for unconfirmed
conntrack entries due to slab rcu object re-usage, so they need to be
skipped since they are not part of the listing.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

authored by

Florian Westphal and committed by
Pablo Neira Ayuso
245cfdca 86804348

+19 -5
+19 -5
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c
··· 114 114 } 115 115 #endif 116 116 117 + static bool ct_seq_should_skip(const struct nf_conn *ct, 118 + const struct nf_conntrack_tuple_hash *hash) 119 + { 120 + /* we only want to print DIR_ORIGINAL */ 121 + if (NF_CT_DIRECTION(hash)) 122 + return true; 123 + 124 + if (nf_ct_l3num(ct) != AF_INET) 125 + return true; 126 + 127 + return false; 128 + } 129 + 117 130 static int ct_seq_show(struct seq_file *s, void *v) 118 131 { 119 132 struct nf_conntrack_tuple_hash *hash = v; ··· 136 123 int ret = 0; 137 124 138 125 NF_CT_ASSERT(ct); 126 + if (ct_seq_should_skip(ct, hash)) 127 + return 0; 128 + 139 129 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use))) 140 130 return 0; 141 131 142 - 143 - /* we only want to print DIR_ORIGINAL */ 144 - if (NF_CT_DIRECTION(hash)) 145 - goto release; 146 - if (nf_ct_l3num(ct) != AF_INET) 132 + /* check if we raced w. object reuse */ 133 + if (!nf_ct_is_confirmed(ct) || 134 + ct_seq_should_skip(ct, hash)) 147 135 goto release; 148 136 149 137 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));