netfilter: nftables: rename set element data activation/deactivation functions
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/4 netfilter: nftables: rename set element data activation/deactivation functions
Rename:
- nft_set_elem_activate() to nft_set_elem_data_activate().
- nft_set_elem_deactivate() to nft_set_elem_data_deactivate().
To prepare for updates in the set element infrastructure to add support for the special catch-all element.
--- net/netfilter/nf_tables_api.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
net/netfilter/nf_tables_api.c +11 -11
@@ -5263,8 +5263,8 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem, }EXPORT_SYMBOL_GPL (nft_set_elem_destroy); -/* Only called from commit path, nft_set_elem_deactivate() already deals with -* the refcounting from the preparation phase. +/* Only called from commit path, nft_setelem_data_deactivate() already deals +* with the refcounting from the preparation phase. */ static void nf_tables_set_elem_destroy (const struct nft_ctx *ctx,const struct nft_set *set,void *elem)
@@ -5733,9 +5733,9 @@ void nft_data_hold(const struct nft_data *data, enum nft_data_types type) } } -static void nft_set_elem_activate (const struct net *net, -const struct nft_set *set, -struct nft_set_elem *elem) +static void nft_setelem_data_activate (const struct net *net, +const struct nft_set *set, +struct nft_set_elem *elem) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv);
@@ -5745,9 +5745,9 @@ static void nft_set_elem_activate(const struct net *net, (*nft_set_ext_obj (ext))->use++; } -static void nft_set_elem_deactivate (const struct net *net, -const struct nft_set *set, -struct nft_set_elem *elem) +static void nft_setelem_data_deactivate (const struct net *net, +const struct nft_set *set, +struct nft_set_elem *elem) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv);
@@ -5824,7 +5824,7 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,kfree (elem.priv); elem.priv = priv; -nft_set_elem_deactivate (ctx->net, set, &elem); +nft_setelem_data_deactivate (ctx->net, set, &elem);nft_trans_elem (trans) = elem;nft_trans_commit_list_add_tail (ctx->net, trans);
@@ -5858,7 +5858,7 @@ static int nft_flush_set(const struct nft_ctx *ctx, } set->ndeact++; -nft_set_elem_deactivate (ctx->net, set, elem); +nft_setelem_data_deactivate (ctx->net, set, elem);nft_trans_elem_set (trans) = set;nft_trans_elem (trans) = *elem;nft_trans_commit_list_add_tail (ctx->net, trans);
@@ -8479,7 +8479,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)case NFT_MSG_DELSETELEM : te = (struct nft_trans_elem *)trans->data; -nft_set_elem_activate (net, te->set, &te->elem); +nft_setelem_data_activate (net, te->set, &te->elem); te->set->ops->activate (net, te->set, &te->elem); te->set->ndeact--;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/4 netfilter: nftables: add loop check helper function
This patch adds nft_check_loops() to reuse it in the new catch-all element codebase.
--- net/netfilter/nf_tables_api.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
net/netfilter/nf_tables_api.c +21 -9
@@ -8626,26 +8626,38 @@ EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);static int nf_tables_check_loops (const struct nft_ctx *ctx,const struct nft_chain *chain); +static int nft_check_loops (const struct nft_ctx *ctx, +const struct nft_set_ext *ext) +{ +const struct nft_data *data; +int ret; + + data =nft_set_ext_data (ext); +switch (data->verdict.code) { +case NFT_JUMP : +case NFT_GOTO : + ret =nf_tables_check_loops (ctx, data->verdict.chain); +break ; +default : + ret =0 ; +break ; + } + +return ret; +} +static int nf_tables_loop_check_setelem (const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter,struct nft_set_elem *elem) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv); -const struct nft_data *data;if (nft_set_ext_exists (ext,NFT_SET_EXT_FLAGS ) && *nft_set_ext_flags (ext) &NFT_SET_ELEM_INTERVAL_END )return 0 ; - data =nft_set_ext_data (ext); -switch (data->verdict.code) { -case NFT_JUMP : -case NFT_GOTO : -return nf_tables_check_loops (ctx, data->verdict.chain); -default : -return 0 ; - } +return nft_check_loops (ctx, ext); } static int nf_tables_check_loops(const struct nft_ctx *ctx,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/4 netfilter: nftables: add helper function to flush set elements
In reply to Pablo Neira AyusoThis patch adds nft_set_flush() which prepares for the catch-all element support.
--- net/netfilter/nf_tables_api.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
net/netfilter/nf_tables_api.c +16 -8
@@ -5869,6 +5869,18 @@ static int nft_flush_set(const struct nft_ctx *ctx,return err; } +static int nft_set_flush (struct nft_ctx *ctx,struct nft_set *set,u8 genmask) +{ +struct nft_set_iter iter = { + .genmask = genmask, + .fn = nft_flush_set, + }; + + set->ops->walk (ctx, set, &iter); + +return iter.err; +} +static int nf_tables_delsetelem (struct sk_buff *skb,const struct nfnl_info *info,const struct nlattr *const nla[])
@@ -5892,14 +5904,10 @@ static int nf_tables_delsetelem(struct sk_buff *skb,if (!list_empty (&set->bindings) && set->flags &NFT_SET_CONSTANT )return -EBUSY ; -if (nla[NFTA_SET_ELEM_LIST_ELEMENTS ] ==NULL ) { -struct nft_set_iter iter = { - .genmask = genmask, - .fn = nft_flush_set, - }; - set->ops->walk (&ctx, set, &iter); - -return iter.err; +if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS ]) { + err =nft_set_flush (&ctx, set, genmask); +if (err <0 ) +return err; }nla_for_each_nested (attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS ], rem) {
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v3 4/4 netfilter: nftables: add catch-all set element support
In reply to Pablo Neira AyusoThis patch extends the set infrastructure to add a special catch-all set element. If the lookup fails to find an element (or range) in the set, then the catch-all element is selected. Users can specify a mapping, expression(s) and timeout to be attached to the catch-all element.
This patch adds a catchall list to the set, this list might contain more than one single catch-all element (e.g. in case that the catch-all element is removed and a new one is added in the same transaction). However, most of the time, there will be either one element or no elements at all in this list.
The catch-all element is identified via NFT_SET_ELEM_CATCHALL flag and such special element has no NFTA_SET_ELEM_KEY attribute. There is a new nft_set_elem_catchall object that stores a reference to the dummy catch-all element (catchall->elem) whose layout is the same of the set element type to reuse the existing set element codebase.
The set size does not apply to the catch-all element, users can define a catch-all element even if the set is full.
The check for valid set element flags hava been updates to report EOPNOTSUPP in case userspace requests flags that are not supported when using new userspace nftables and old kernel.
---
v3: - fix catchall get operation.
- fix crash on catch-all element timeout support.
- missing call to activate() in catch-all element.
- missing loop check for catch-all element.
- do not update set->nelems in the catch-all element case.
include/net/netfilter/nf_tables.h | 5 + include/uapi/linux/netfilter/nf_tables.h | 2 + net/netfilter/nf_tables_api.c | 456 ++++++++++++++++++++--- net/netfilter/nft_lookup.c | 12 +- net/netfilter/nft_objref.c | 11 +- net/netfilter/nft_set_hash.c | 6 + net/netfilter/nft_set_pipapo.c | 6 +- net/netfilter/nft_set_rbtree.c | 6 + 8 files changed, 441 insertions(+), 63 deletions(-)
include/net/netfilter/nf_tables.h +5 -0
@@ -497,6 +497,7 @@ struct nft_set {u8 dlen;u8 num_exprs;struct nft_expr *exprs[NFT_SET_EXPR_MAX ]; +struct list_head catchall_list; unsigned char data[] __attribute__((aligned(__alignof__(u64)))); };
@@ -522,6 +523,10 @@ struct nft_set *nft_set_lookup_global(const struct net *net,const struct nlattr *nla_set_id, u8 genmask); +struct nft_set_ext *nft_set_catchall_lookup (const struct net *net, +const struct nft_set *set); +void *nft_set_catchall_gc (const struct nft_set *set); +static inline unsigned long nft_set_gc_interval (const struct nft_set *set) {return set->gc_int ?msecs_to_jiffies (set->gc_int) :HZ ;
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -398,9 +398,11 @@ enum nft_set_attributes {* enum nft_set_elem_flags - nf_tables set element flags * * @NFT_SET_ELEM_INTERVAL_END: element ends the previous interval +* @NFT_SET_ELEM_CATCHALL: special catch-all element */ enum nft_set_elem_flags {NFT_SET_ELEM_INTERVAL_END =0x1 , +NFT_SET_ELEM_CATCHALL =0x2 , };/**
net/netfilter/nf_tables_api.c +402 -54
654 lines not on this page · raw message
net/netfilter/nft_lookup.c +8 -4
@@ -30,13 +30,17 @@ void nft_lookup_eval(const struct nft_expr *expr,const struct nft_lookup *priv =nft_expr_priv (expr);const struct nft_set *set = priv->set;const struct nft_set_ext *ext =NULL ; +const struct net *net =nft_net (pkt);bool found; - found = set->ops->lookup (nft_net (pkt), set, ®s->data[priv->sreg], - &ext) ^ priv->invert; + found = set->ops->lookup (net, set, ®s->data[priv->sreg], &ext) ^ + priv->invert;if (!found) { - regs->verdict.code =NFT_BREAK ; -return ; + ext =nft_set_catchall_lookup (net, set); +if (!ext) { + regs->verdict.code =NFT_BREAK ; +return ; + } }if (ext) {
net/netfilter/nft_objref.c +7 -4
@@ -105,15 +105,18 @@ static void nft_objref_map_eval(const struct nft_expr *expr, {struct nft_objref_map *priv =nft_expr_priv (expr);const struct nft_set *set = priv->set; +struct net *net =nft_net (pkt);const struct nft_set_ext *ext;struct nft_object *obj;bool found; - found = set->ops->lookup (nft_net (pkt), set, ®s->data[priv->sreg], - &ext); + found = set->ops->lookup (net, set, ®s->data[priv->sreg], &ext);if (!found) { - regs->verdict.code =NFT_BREAK ; -return ; + ext =nft_set_catchall_lookup (net, set); +if (!ext) { + regs->verdict.code =NFT_BREAK ; +return ; + } } obj = *nft_set_ext_obj (ext); obj->ops->eval (obj, regs, pkt);
net/netfilter/nft_set_hash.c +6 -0
@@ -350,6 +350,12 @@ static void nft_rhash_gc(struct work_struct *work)rhashtable_walk_stop (&hti);rhashtable_walk_exit (&hti); + he =nft_set_catchall_gc (set); +if (he) { + gcb =nft_set_gc_batch_check (set, gcb,GFP_ATOMIC ); +if (gcb) +nft_set_gc_batch_add (gcb, he); + }nft_set_gc_batch_complete (gcb);queue_delayed_work (system_power_efficient_wq, &priv->gc_work,nft_set_gc_interval (set));
net/netfilter/nft_set_pipapo.c +5 -1
@@ -1529,11 +1529,11 @@ static void pipapo_gc(const struct nft_set *set, struct nft_pipapo_match *m) {struct nft_pipapo *priv =nft_set_priv (set);int rules_f0, first_rule =0 ; +struct nft_pipapo_elem *e;while ((rules_f0 =pipapo_rules_same_key (m->f, first_rule))) {union nft_pipapo_map_bucket rulemap[NFT_PIPAPO_MAX_FIELDS ];struct nft_pipapo_field *f; -struct nft_pipapo_elem *e;int i, start, rules_fx; start = first_rule;
@@ -1569,6 +1569,10 @@ static void pipapo_gc(const struct nft_set *set, struct nft_pipapo_match *m) } } + e =nft_set_catchall_gc (set); +if (e) +nft_set_elem_destroy (set, e, true); + priv->last_gc = jiffies; }
net/netfilter/nft_set_rbtree.c +6 -0
@@ -541,6 +541,12 @@ static void nft_rbtree_gc(struct work_struct *work)write_seqcount_end (&priv->count);write_unlock_bh (&priv->lock); + rbe =nft_set_catchall_gc (set); +if (rbe) { + gcb =nft_set_gc_batch_check (set, gcb,GFP_ATOMIC ); +if (gcb) +nft_set_gc_batch_add (gcb, rbe); + }nft_set_gc_batch_complete (gcb); queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
Signature · 1 line
2.30.2