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

netfilter: conntrack: don't fold port numbers into addresses before hashing

Originally this used jhash2() over tuple and folded the zone id,
the pernet hash value, destination port and l4 protocol number into the
32bit seed value.

When the switch to siphash was done, I used an on-stack temporary
buffer to build a suitable key to be hashed via siphash().

But this showed up as performance regression, so I got rid of
the temporary copy and collected to-be-hashed data in 4 u64 variables.

This makes it easy to build tuples that produce the same hash, which isn't
desirable even though chain lengths are limited.

Switch back to plain siphash, but just like with jhash2(), take advantage
of the fact that most of to-be-hashed data is already in a suitable order.

Use an empty struct as annotation in 'struct nf_conntrack_tuple' to mark
last member that can be used as hash input.

The only remaining data that isn't present in the tuple structure are the
zone identifier and the pernet hash: fold those into the key.

Fixes: d2c806abcf0b ("netfilter: conntrack: use siphash_4u64")
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
eaf9e719 6eef7a2b

+10 -13
+3
include/net/netfilter/nf_conntrack_tuple.h
··· 67 67 /* The protocol. */ 68 68 u_int8_t protonum; 69 69 70 + /* The direction must be ignored for the tuplehash */ 71 + struct { } __nfct_hash_offsetend; 72 + 70 73 /* The direction (for tuplehash) */ 71 74 u_int8_t dir; 72 75 } dst;
+7 -13
net/netfilter/nf_conntrack_core.c
··· 211 211 unsigned int zoneid, 212 212 const struct net *net) 213 213 { 214 - u64 a, b, c, d; 214 + siphash_key_t key; 215 215 216 216 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd)); 217 217 218 - /* The direction must be ignored, handle usable tuplehash members manually */ 219 - a = (u64)tuple->src.u3.all[0] << 32 | tuple->src.u3.all[3]; 220 - b = (u64)tuple->dst.u3.all[0] << 32 | tuple->dst.u3.all[3]; 218 + key = nf_conntrack_hash_rnd; 221 219 222 - c = (__force u64)tuple->src.u.all << 32 | (__force u64)tuple->dst.u.all << 16; 223 - c |= tuple->dst.protonum; 220 + key.key[0] ^= zoneid; 221 + key.key[1] ^= net_hash_mix(net); 224 222 225 - d = (u64)zoneid << 32 | net_hash_mix(net); 226 - 227 - /* IPv4: u3.all[1,2,3] == 0 */ 228 - c ^= (u64)tuple->src.u3.all[1] << 32 | tuple->src.u3.all[2]; 229 - d += (u64)tuple->dst.u3.all[1] << 32 | tuple->dst.u3.all[2]; 230 - 231 - return (u32)siphash_4u64(a, b, c, d, &nf_conntrack_hash_rnd); 223 + return siphash((void *)tuple, 224 + offsetofend(struct nf_conntrack_tuple, dst.__nfct_hash_offsetend), 225 + &key); 232 226 } 233 227 234 228 static u32 scale_hash(u32 hash)