netfilter: nft_set_hash: fix lookups with fixed size hash on big endian
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/3 netfilter: nft_set_hash: fix lookups with fixed size hash on big endian
Call jhash_1word() for the 4-bytes key case from the insertion and deactivation path, otherwise big endian arch set lookups fail.
Fixes 446a8268b7f5 ("netfilter: nft_set_hash: add lookup variant for fixed size hashtable")
Reported-by Florian Westphal <[email protected]>
Tested-by Florian Westphal <[email protected]>
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- net/netfilter/nft_set_hash.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
net/netfilter/nft_set_hash.c +19 -4
@@ -474,6 +474,23 @@ static bool nft_hash_lookup_fast(const struct net *net,return false; } +static u32 nft_jhash (const struct nft_set *set,const struct nft_hash *priv, +const struct nft_set_ext *ext) +{ +const struct nft_data *key =nft_set_ext_key (ext); +u32 hash, k1; + +if (set->klen ==4 ) { + k1 = *(u32 *)key; + hash =jhash_1word (k1, priv->seed); + }else { + hash =jhash (key, set->klen, priv->seed); + } + hash =reciprocal_scale (hash, priv->buckets); + +return hash; +} +static int nft_hash_insert (const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem,struct nft_set_ext **ext)
@@ -483,8 +500,7 @@ static int nft_hash_insert(const struct net *net, const struct nft_set *set,u8 genmask =nft_genmask_next (net);u32 hash; - hash =jhash (nft_set_ext_key (&this->ext), set->klen, priv->seed); - hash =reciprocal_scale (hash, priv->buckets); + hash =nft_jhash (set, priv, &this->ext);hlist_for_each_entry (he, &priv->table[hash], node) { if (!memcmp(nft_set_ext_key(&this->ext), nft_set_ext_key(&he->ext), set->klen) &&
@@ -523,8 +539,7 @@ static void *nft_hash_deactivate(const struct net *net,u8 genmask =nft_genmask_next (net);u32 hash; - hash =jhash (nft_set_ext_key (&this->ext), set->klen, priv->seed); - hash =reciprocal_scale (hash, priv->buckets); + hash =nft_jhash (set, priv, &this->ext);hlist_for_each_entry (he, &priv->table[hash], node) { if (!memcmp(nft_set_ext_key(&this->ext), &elem->key.val, set->klen) &&
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/3 netfilter: nft_set_hash: bogus element self comparison from deactivation path
Use the element from the loop iteration, not the same element we want to deactivate otherwise this branch always evaluates true.
Fixes 6c03ae210ce3 ("netfilter: nft_set_hash: add non-resizable hashtable implementation")
Reported-by Florian Westphal <[email protected]>
Tested-by Florian Westphal <[email protected]>
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- net/netfilter/nft_set_hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_set_hash.c +1 -1
@@ -541,7 +541,7 @@ static void *nft_hash_deactivate(const struct net *net, hash =nft_jhash (set, priv, &this->ext);hlist_for_each_entry (he, &priv->table[hash], node) { -if (!memcmp (nft_set_ext_key (&this->ext), &elem->key.val, +if (!memcmp (nft_set_ext_key (&he->ext), &elem->key.val, set->klen) &&nft_set_elem_active (&he->ext, genmask)) {nft_set_elem_change_active (net, set, &he->ext);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/3 netfilter: nft_set_hash: remove nft_hash_key()
In reply to Pablo Neira Ayusohashtable is never used for 2-byte keys, remove nft_hash_key().
Fixes e240cd0df481 ("netfilter: nf_tables: place all set backends in one single module")
Reported-by Florian Westphal <[email protected]>
Tested-by Florian Westphal <[email protected]>
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- net/netfilter/nft_set_hash.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
net/netfilter/nft_set_hash.c +2 -11
@@ -442,15 +442,6 @@ static void *nft_hash_get(const struct net *net, const struct nft_set *set,return ERR_PTR (-ENOENT ); } -/* nft_hash_select_ops() makes sure key size can be either 2 or 4 bytes . */ -static inline u32 nft_hash_key (const u32 *key,u32 klen) -{ -if (klen ==4 ) -return *key; - -return *(u16 *)key; -} -static bool nft_hash_lookup_fast (const struct net *net,const struct nft_set *set,const u32 *key,const struct nft_set_ext **ext)
@@ -460,11 +451,11 @@ static bool nft_hash_lookup_fast(const struct net *net,const struct nft_hash_elem *he;u32 hash, k1, k2; - k1 =nft_hash_key (key, set->klen); + k1 = *key; hash =jhash_1word (k1, priv->seed); hash =reciprocal_scale (hash, priv->buckets);hlist_for_each_entry_rcu (he, &priv->table[hash], node) { - k2 =nft_hash_key (nft_set_ext_key (&he->ext)->data, set->klen); + k2 = *(u32 *)nft_set_ext_key (&he->ext)->data;if (k1 == k2 &&nft_set_elem_active (&he->ext, genmask)) { *ext = &he->ext;
Signature · 1 line
2.11.0