Sets of a table are kept in a linked list, which is walked for every lookup by name. This happens when creating a set (duplicate check), when adding, deleting or getting set elements and when a lookup, dynset or objref expression refers to a set by name. Loading a ruleset with many named sets therefore takes time quadratic in the number of sets. Add an rhltable keyed by the set name to struct nft_table, as is already done for chains. An rhltable is needed since a deleted set and a new set of the same name can coexist until the transaction is committed; only one of them is active in a given generation. The list is kept for stable walks during dumps and table flushes. Use the hashtable in the duplicate name check of nf_tables_set_alloc_name() as well. Like for chains and objects, the name is now copied with nla_strscpy() before the lookup, which cuts it off at the first NUL byte. A name like "foo\0bar" now refers to an existing set "foo" instead of matching no set. Creating a set with such a name used to fail with -ENFILE if "foo" exists, since the name of a new set is cut off at the NUL as well. It is now handled like a request for "foo". Signed-off-by: Stefan Hanreich --- include/net/netfilter/nf_tables.h | 8 ++- net/netfilter/nf_tables_api.c | 114 ++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 24 deletions(-) diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h index 9d597482363d..d09959eea8ba 100644 --- a/include/net/netfilter/nf_tables.h +++ b/include/net/netfilter/nf_tables.h @@ -545,6 +545,7 @@ struct nft_set_elem_expr { * struct nft_set - nf_tables set instance * * @list: table set list node + * @rhlhead: table set hashtable node * @bindings: list of set bindings * @refs: internal refcounting for async set destruction * @table: table this set belongs to @@ -580,6 +581,7 @@ struct nft_set_elem_expr { */ struct nft_set { struct list_head list; + struct rhlist_head rhlhead; struct list_head bindings; refcount_t refs; struct nft_table *table; @@ -642,7 +644,7 @@ static inline struct nft_set *nft_set_container_of(const void *priv) } struct nft_set *nft_set_lookup_global(const struct net *net, - const struct nft_table *table, + struct nft_table *table, const struct nlattr *nla_set_name, const struct nlattr *nla_set_id, u8 genmask); @@ -1289,7 +1291,8 @@ static inline void nft_use_inc_restore(u32 *use) * @list: used internally * @chains_ht: chains in the table * @chains: same, for stable walks - * @sets: sets in the table + * @sets_ht: sets in the table + * @sets: same, for stable walks * @objects: stateful objects in the table * @flowtables: flow tables in the table * @objname_ht: hashtable for objects lookup by name @@ -1309,6 +1312,7 @@ struct nft_table { struct list_head list; struct rhltable chains_ht; struct list_head chains; + struct rhltable sets_ht; struct list_head sets; struct list_head objects; struct list_head flowtables; diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index 59ac623e6805..9fe234e329df 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -49,6 +49,9 @@ static u32 nft_chain_hash(const void *data, u32 len, u32 seed); static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); +static u32 nft_set_hash_obj(const void *data, u32 len, u32 seed); +static int nft_set_hash_cmp(struct rhashtable_compare_arg *, const void *); + static u32 nft_objname_hash(const void *data, u32 len, u32 seed); static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed); static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *); @@ -62,6 +65,15 @@ static const struct rhashtable_params nft_chain_ht_params = { .automatic_shrinking = true, }; +static const struct rhashtable_params nft_set_ht_params = { + .head_offset = offsetof(struct nft_set, rhlhead), + .key_offset = offsetof(struct nft_set, name), + .hashfn = nft_chain_hash, + .obj_hashfn = nft_set_hash_obj, + .obj_cmpfn = nft_set_hash_cmp, + .automatic_shrinking = true, +}; + static const struct rhashtable_params nft_objname_ht_params = { .head_offset = offsetof(struct nft_object, rhlhead), .key_offset = offsetof(struct nft_object, key), @@ -1556,6 +1568,22 @@ static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, return strcmp(chain->name, name); } +static u32 nft_set_hash_obj(const void *data, u32 len, u32 seed) +{ + const struct nft_set *set = data; + + return nft_chain_hash(set->name, 0, seed); +} + +static int nft_set_hash_cmp(struct rhashtable_compare_arg *arg, + const void *ptr) +{ + const struct nft_set *set = ptr; + const char *name = arg->key; + + return strcmp(set->name, name); +} + static u32 nft_objname_hash(const void *data, u32 len, u32 seed) { const struct nft_object_hash_key *k = data; @@ -1677,6 +1705,10 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, if (err < 0) goto err_obj_ht; + err = rhltable_init(&table->sets_ht, &nft_set_ht_params); + if (err < 0) + goto err_set_ht; + INIT_LIST_HEAD(&table->chains); INIT_LIST_HEAD(&table->sets); INIT_LIST_HEAD(&table->objects); @@ -1695,6 +1727,8 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, list_add_tail_rcu(&table->list, &nft_net->tables); return 0; err_trans: + rhltable_destroy(&table->sets_ht); +err_set_ht: rhltable_destroy(&table->objname_ht); err_obj_ht: rhltable_destroy(&table->chains_ht); @@ -1864,6 +1898,7 @@ static void nf_tables_table_destroy(struct nft_table *table) rhltable_destroy(&table->chains_ht); rhltable_destroy(&table->objname_ht); + rhltable_destroy(&table->sets_ht); kfree(table->name); kfree(table->udata); kfree(table); @@ -4815,22 +4850,50 @@ static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { [NFTA_SET_DESC_CONCAT] = NLA_POLICY_NESTED_ARRAY(nft_concat_policy), }; +static struct nft_set *__nft_set_lookup(const struct net *net, + struct nft_table *table, + const char *name, u8 genmask) +{ + struct rhlist_head *tmp, *list; + struct nft_set *set; + + WARN_ON(!rcu_read_lock_held() && + !lockdep_commit_lock_is_held(net)); + + set = ERR_PTR(-ENOENT); + rcu_read_lock(); + list = rhltable_lookup(&table->sets_ht, name, nft_set_ht_params); + if (!list) + goto out_unlock; + + rhl_for_each_entry_rcu(set, tmp, list, rhlhead) { + if (nft_active_genmask(set, genmask)) + goto out_unlock; + } + set = ERR_PTR(-ENOENT); +out_unlock: + rcu_read_unlock(); + return set; +} + static struct nft_set *nft_set_lookup(const struct net *net, - const struct nft_table *table, + struct nft_table *table, const struct nlattr *nla, u8 genmask) { - struct nft_set *set; + char search[NFT_SET_MAXNAMELEN]; if (nla == NULL) return ERR_PTR(-EINVAL); - list_for_each_entry_rcu(set, &table->sets, list, - lockdep_commit_lock_is_held(net)) { - if (!nla_strcmp(nla, set->name) && - nft_active_genmask(set, genmask)) - return set; - } - return ERR_PTR(-ENOENT); + nla_strscpy(search, nla, sizeof(search)); + + return __nft_set_lookup(net, table, search, genmask); +} + +static void nft_set_del(struct nft_set *set) +{ + rhltable_remove(&set->table->sets_ht, &set->rhlhead, nft_set_ht_params); + list_del_rcu(&set->list); } static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, @@ -4868,7 +4931,7 @@ static struct nft_set *nft_set_lookup_byid(const struct net *net, } struct nft_set *nft_set_lookup_global(const struct net *net, - const struct nft_table *table, + struct nft_table *table, const struct nlattr *nla_set_name, const struct nlattr *nla_set_id, u8 genmask) @@ -4931,14 +4994,11 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, if (!set->name) return -ENOMEM; - list_for_each_entry(i, &ctx->table->sets, list) { - if (!nft_is_active_next(ctx->net, i)) - continue; - if (!strcmp(set->name, i->name)) { - kfree(set->name); - set->name = NULL; - return -ENFILE; - } + if (!IS_ERR(__nft_set_lookup(ctx->net, ctx->table, set->name, + nft_genmask_next(ctx->net)))) { + kfree(set->name); + set->name = NULL; + return -ENFILE; } return 0; } @@ -5757,10 +5817,20 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, if (err < 0) goto err_set_expr_alloc; + err = rhltable_insert_key(&table->sets_ht, set->name, &set->rhlhead, + nft_set_ht_params); + if (err < 0) + goto err_set_ht; + list_add_tail_rcu(&set->list, &table->sets); return 0; +err_set_ht: + /* queued in transaction log */ + INIT_LIST_HEAD(&set->list); + return err; + err_set_expr_alloc: for (i = 0; i < set->num_exprs; i++) nft_expr_destroy(&ctx, set->exprs[i]); @@ -5963,7 +6033,7 @@ static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, list_del_rcu(&binding->list); if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { - list_del_rcu(&set->list); + nft_set_del(set); set->dead = 1; if (event) nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, @@ -11172,7 +11242,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) case NFT_MSG_DELSET: case NFT_MSG_DESTROYSET: nft_trans_set(trans)->dead = 1; - list_del_rcu(&nft_trans_set(trans)->list); + nft_set_del(nft_trans_set(trans)); nf_tables_set_notify(&ctx, nft_trans_set(trans), trans->msg_type, GFP_KERNEL); break; @@ -11447,7 +11517,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) break; } nft_trans_set(trans)->dead = 1; - list_del_rcu(&nft_trans_set(trans)->list); + nft_set_del(nft_trans_set(trans)); break; case NFT_MSG_DELSET: case NFT_MSG_DESTROYSET: @@ -12102,7 +12172,7 @@ static void __nft_release_table(struct net *net, struct nft_table *table) nf_tables_flowtable_destroy(flowtable); } list_for_each_entry_safe(set, ns, &table->sets, list) { - list_del(&set->list); + nft_set_del(set); nft_use_dec(&table->use); if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) nft_map_deactivate(&ctx, set); -- 2.47.3