rhashtable: add rhashtable_lookup_get_insert_key()
Subsystems (3)
Pablo Neira Ayuso
maintainerPATCH 1/2 rhashtable: add rhashtable_lookup_get_insert_key()
This patch modifies __rhashtable_insert_fast() so it returns the existing element that clashes with the one that you want to insert. This adds a new rhashtable_lookup_get_insert_key() interface to fetch this existing element.
nf_tables needs this change to improve handling of EEXIST cases via honoring the NLM_F_EXCL flag and by checking if the data part of the mapping matches what we have.
---
include/linux/rhashtable.h | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-)
include/linux/rhashtable.h +27 -5
@@ -566,7 +566,7 @@ restart:/* Internal function, please use rhashtable_insert_fast() instead */ static inline int __rhashtable_insert_fast (struct rhashtable *ht,const void *key,struct rhash_head *obj, -const struct rhashtable_params params) +const struct rhashtable_params params,void **data) {struct rhashtable_compare_arg arg = { .ht = ht,
@@ -630,8 +630,11 @@ slow_path:if (key &&unlikely (!(params.obj_cmpfn ? params.obj_cmpfn (&arg,rht_obj (ht, head)) : -rhashtable_compare (&arg,rht_obj (ht, head))))) +rhashtable_compare (&arg,rht_obj (ht, head))))) { +if (data) + *data =rht_obj (ht, head); goto out; + }if (!--elasticity) goto slow_path; }
@@ -675,7 +678,7 @@ static inline int rhashtable_insert_fast( struct rhashtable *ht, struct rhash_head *obj, const struct rhashtable_params params) { - return __rhashtable_insert_fast(ht, NULL, obj, params); + return __rhashtable_insert_fast(ht, NULL, obj, params, NULL); }/**
@@ -708,7 +711,7 @@ static inline int rhashtable_lookup_insert_fast(BUG_ON (ht->p.obj_hashfn);return __rhashtable_insert_fast (ht, key + ht->p.key_offset, obj, - params); + params,NULL ); }/**
@@ -739,7 +742,26 @@ static inline int rhashtable_lookup_insert_key( {BUG_ON (!ht->p.obj_hashfn || !key); -return __rhashtable_insert_fast (ht, key, obj, params); +return __rhashtable_insert_fast (ht, key, obj, params,NULL ); +} + +/** +* rhashtable_lookup_get_insert_key - lookup and insert object into hash table +* @ht: hash table +* @obj: pointer to hash head inside object +* @params: hash table parameters +* @data: pointer to element data already in hashes +* +* Just like rhashtable_lookup_insert_key(), but it returns the matching +* element in case that it already exists in the hashes. +*/ +static inline int rhashtable_lookup_get_insert_key ( +struct rhashtable *ht,const void *key,struct rhash_head *obj, +const struct rhashtable_params params,void **data) +{ +BUG_ON (!ht->p.obj_hashfn || !key); + +return __rhashtable_insert_fast (ht, key, obj, params, data); }/* Internal function, please use rhashtable_remove_fast() instead */
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/2 netfilter: nf_tables: honor NLM_F_EXCL flag in set element insertion
If the NLM_F_EXCL flag is set, then new elements that clash with an existing one return EEXIST. In case you try to add an element whose data area differs from what we have, then this returns EBUSY. If no flag is specified at all, then this returns success to userspace.
This patch also update the set insert operation so we can fetch the existing element that clashes with the one you want to add, we need this to make sure the element data doesn't differ.
--- include/net/netfilter/nf_tables.h | 3 ++- net/netfilter/nf_tables_api.c | 20 +++++++++++++++----- net/netfilter/nft_set_hash.c | 11 ++++++++--- net/netfilter/nft_set_rbtree.c | 12 ++++++++---- 4 files changed, 33 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
@@ -251,7 +251,8 @@ struct nft_set_ops {int (*insert)(const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem); +const struct nft_set_elem *elem, +struct nft_set_ext **ext);void (*activate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);
net/netfilter/nf_tables_api.c +15 -5
@@ -3483,12 +3483,12 @@ static int nft_setelem_parse_flags(const struct nft_set *set, }static int nft_add_set_elem (struct nft_ctx *ctx,struct nft_set *set, -const struct nlattr *attr) +const struct nlattr *attr,u32 nlmsg_flags) {struct nlattr *nla[NFTA_SET_ELEM_MAX +1 ];struct nft_data_desc d1, d2;struct nft_set_ext_tmpl tmpl; -struct nft_set_ext *ext; +struct nft_set_ext *ext, *ext2;struct nft_set_elem elem;struct nft_set_binding *binding;struct nft_userdata *udata;
@@ -3615,9 +3615,19 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, goto err4; ext->genmask =nft_genmask_cur (ctx->net) |NFT_SET_ELEM_BUSY_MASK ; - err = set->ops->insert (ctx->net, set, &elem); -if (err <0 ) + err = set->ops->insert (ctx->net, set, &elem, &ext2); +if (err) { +if (err == -EEXIST ) { +if (nft_set_ext_exists (ext,NFT_SET_EXT_DATA ) && +nft_set_ext_exists (ext2,NFT_SET_EXT_DATA ) && +memcmp (nft_set_ext_data (ext), +nft_set_ext_data (ext2), set->dlen) !=0 ) + err = -EBUSY ; +else if (!(nlmsg_flags &NLM_F_EXCL )) + err =0 ; + } goto err5; + }nft_trans_elem (trans) = elem;list_add_tail (&trans->list, &ctx->net->nft.commit_list);
@@ -3673,7 +3683,7 @@ static int nf_tables_newsetelem(struct net *net, struct sock *nlsk, !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact))return -ENFILE ; - err =nft_add_set_elem (&ctx, set, attr); + err =nft_add_set_elem (&ctx, set, attr, nlh->nlmsg_flags);if (err <0 ) {atomic_dec (&set->nelems);break ;
net/netfilter/nft_set_hash.c +8 -3
@@ -126,7 +126,8 @@ err1: }static int nft_hash_insert (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set_elem *elem, +struct nft_set_ext **ext) {struct nft_hash *priv =nft_set_priv (set);struct nft_hash_elem *he = elem->priv;
@@ -135,9 +136,13 @@ static int nft_hash_insert(const struct net *net, const struct nft_set *set, .set = set, .key = elem->key.val.data, }; +struct nft_hash_elem *prev; +int ret; -return rhashtable_lookup_insert_key (&priv->ht, &arg, &he->node, - nft_hash_params); + ret =rhashtable_lookup_get_insert_key (&priv->ht, &arg, &he->node, + nft_hash_params, (void **)&prev); + *ext = &prev->ext; +return ret; } static void nft_hash_activate(const struct net *net, const struct nft_set *set,
net/netfilter/nft_set_rbtree.c +8 -4
@@ -94,7 +94,8 @@ out: }static int __nft_rbtree_insert (const struct net *net,const struct nft_set *set, -struct nft_rbtree_elem *new) +struct nft_rbtree_elem *new, +struct nft_set_ext **ext) {struct nft_rbtree *priv =nft_set_priv (set);u8 genmask =nft_genmask_next (net);
@@ -122,8 +123,10 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,else if (!nft_rbtree_interval_end (rbe) &&nft_rbtree_interval_end (new)) p = &parent->rb_right; -else +else { + *ext = &rbe->ext;return -EEXIST ; + } } } }
@@ -133,13 +136,14 @@ static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set, }static int nft_rbtree_insert (const struct net *net,const struct nft_set *set, -const struct nft_set_elem *elem) +const struct nft_set_elem *elem, +struct nft_set_ext **ext) {struct nft_rbtree_elem *rbe = elem->priv;int err;spin_lock_bh (&nft_rbtree_lock); - err =__nft_rbtree_insert (net, set, rbe); + err =__nft_rbtree_insert (net, set, rbe, ext);spin_unlock_bh (&nft_rbtree_lock);return err;
Signature · 1 line
2.1.4
Herbert Xu
maintainerQuoted 5 lines · Pablo Neira Ayuso
This patch modifies
__rhashtable_insert_fast()so it returns the existing element that clashes with the one that you want to insert. This adds a newrhashtable_lookup_get_insert_key()interface to fetch this existing element.
Quoted 7 lines
nf_tablesneeds this change to improve handling of EEXIST cases via honoring theNLM_F_EXCLflag and by checking if the data part of the mapping matches what we have.Cc Herbert Xu <[email protected]>Cc Thomas Graf <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>
I would prefer an interface where you returned the object if it exists, NULL if it does not and the insertion was successful, and an ERR_PTR otherwise. But it's not a deal-breaker.
However, you missed the rhashtable_insert_slow path. It too needs to be updated to incorporate your new logic.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt