enhance stateful expression support
Subsystems (2)
Pablo Neira Ayuso
maintainerv2 cover enhance stateful expression support
This patchset allows users to add and to restore stateful expressions of set elements, e.g.
table ip test {
set test {
type ipv4_addr
size 65535
flags dynamic,timeout
timeout 30d
gc-interval 1d
elements = { 192.168.10.13 expires 19d23h52m27s576ms counter packets 51 bytes 17265 }
}
chain output {
type filter hook output priority 0;
update @test { ip saddr }
}}
You can also add counters to elements from the control place, ie.
table ip test {
set test {
type ipv4_addr
size 65535
elements = { 192.168.2.1 counter packets 75 bytes 19043 }
}
chain output {
type filter hook output priority filter; policy accept;
ip daddr @test
}}
v2: Missing patch to add nft_set_elem_expr_alloc() helper function.
Pablo Neira Ayuso (5):
netfilter: nf_tables: add nft_set_elem_expr_alloc()
netfilter: nf_tables: remove EXPORT_SYMBOL_GPL for nft_expr_init()
netfilter: nf_tables: add elements with stateful expressions
netfilter: nf_tables: add nft_set_elem_update_expr() helper function
netfilter: nft_lookup: update element stateful expression
include/net/netfilter/nf_tables.h | 18 +++++++++++-- net/netfilter/nf_tables_api.c | 55 ++++++++++++++++++++++++++++++++++++--- net/netfilter/nft_dynset.c | 23 +++------------- net/netfilter/nft_lookup.c | 1 + 4 files changed, 72 insertions(+), 25 deletions(-)
--
2.11.0
Pablo Neira Ayuso
maintainerPATCH v2 1/5 netfilter: nf_tables: add nft_set_elem_expr_alloc()
Add helper function to create stateful expression.
--- include/net/netfilter/nf_tables.h | 4 ++++ net/netfilter/nf_tables_api.c | 30 ++++++++++++++++++++++++++++++ net/netfilter/nft_dynset.c | 15 ++------------- 3 files changed, 36 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables.h +4 -0
@@ -673,6 +673,10 @@ static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)return nft_set_ext (ext,NFT_SET_EXT_OBJREF ); } +struct nft_expr *nft_set_elem_expr_alloc (const struct nft_ctx *ctx, +const struct nft_set *set, +const struct nlattr *attr); + void *nft_set_elem_init(const struct nft_set *set, const struct nft_set_ext_tmpl *tmpl, const u32 *key, const u32 *key_end, const u32 *data,
net/netfilter/nf_tables_api.c +30 -0
@@ -4801,6 +4801,36 @@ static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,return trans; } +struct nft_expr *nft_set_elem_expr_alloc (const struct nft_ctx *ctx, +const struct nft_set *set, +const struct nlattr *attr) +{ +struct nft_expr *expr; +int err; + + expr =nft_expr_init (ctx, attr); +if (IS_ERR (expr)) +return expr; + + err = -EOPNOTSUPP ; +if (!(expr->ops->type->flags &NFT_EXPR_STATEFUL )) + goto err_set_elem_expr; + +if (expr->ops->type->flags &NFT_EXPR_GC ) { +if (set->flags &NFT_SET_TIMEOUT ) + goto err_set_elem_expr; +if (!set->ops->gc_init) + goto err_set_elem_expr; + set->ops->gc_init (set); + } + +return expr; + +err_set_elem_expr: +nft_expr_destroy (ctx, expr); +return ERR_PTR (err); +} + void *nft_set_elem_init(const struct nft_set *set, const struct nft_set_ext_tmpl *tmpl, const u32 *key, const u32 *key_end,
net/netfilter/nft_dynset.c +2 -13
@@ -206,21 +206,10 @@ static int nft_dynset_init(const struct nft_ctx *ctx,if (!(set->flags &NFT_SET_EVAL ))return -EINVAL ; - priv->expr =nft_expr_init (ctx, tb[NFTA_DYNSET_EXPR ]); + priv->expr =nft_set_elem_expr_alloc (ctx, set, + tb[NFTA_DYNSET_EXPR ]);if (IS_ERR (priv->expr))return PTR_ERR (priv->expr); - - err = -EOPNOTSUPP ; -if (!(priv->expr->ops->type->flags &NFT_EXPR_STATEFUL )) - goto err1; - -if (priv->expr->ops->type->flags &NFT_EXPR_GC ) { -if (set->flags &NFT_SET_TIMEOUT ) - goto err1; -if (!set->ops->gc_init) - goto err1; - set->ops->gc_init (set); - } }nft_set_ext_prepare (&priv->tmpl);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH v2 2/5 netfilter: nf_tables: remove EXPORT_SYMBOL_GPL for nft_expr_init()
In reply to Pablo Neira AyusoNot exposed anymore to modules, remove it.
--- include/net/netfilter/nf_tables.h | 2 -- net/netfilter/nf_tables_api.c | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-)
include/net/netfilter/nf_tables.h +0 -2
@@ -853,8 +853,6 @@ static inline void *nft_expr_priv(const struct nft_expr *expr)return (void *)expr->data; } -struct nft_expr *nft_expr_init (const struct nft_ctx *ctx, -const struct nlattr *nla);void nft_expr_destroy (const struct nft_ctx *ctx,struct nft_expr *expr);int nft_expr_dump (struct sk_buff *skb,unsigned int attr,const struct nft_expr *expr);
net/netfilter/nf_tables_api.c +2 -2
@@ -2523,8 +2523,8 @@ static void nf_tables_expr_destroy(const struct nft_ctx *ctx,module_put (type->owner); } -struct nft_expr *nft_expr_init (const struct nft_ctx *ctx, -const struct nlattr *nla) +static struct nft_expr *nft_expr_init (const struct nft_ctx *ctx, +const struct nlattr *nla) {struct nft_expr_info info;struct nft_expr *expr;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerRe: [PATCH 2/5] netfilter: nf_tables: remove EXPORT_SYMBOL_GPL for nft_expr_init()
Quoted 1 line · Pablo Neira Ayuso
Not exposed anymore to modules, remove it.
This patch subject is not correct, there is no EXPORT_SYMBOL_GPL removal.
Instead, this statifies the nft_expr_init() symbol.
I'm going to fix this here before applying.
Pablo Neira Ayuso
maintainerPATCH v2 3/5 netfilter: nf_tables: add elements with stateful expressions
In reply to Pablo Neira AyusoUpdate nft_add_set_elem() to handle the NFTA_SET_ELEM_EXPR netlink attribute. This patch allows users to to add elements with stateful expressions.
--- net/netfilter/nf_tables_api.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +20 -1
@@ -4913,6 +4913,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,struct nft_set_elem elem;struct nft_set_binding *binding;struct nft_object *obj =NULL ; +struct nft_expr *expr =NULL ;struct nft_userdata *udata;struct nft_data_desc desc;struct nft_data data;
@@ -4980,10 +4981,17 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,return err; } +if (nla[NFTA_SET_ELEM_EXPR ] !=NULL ) { + expr =nft_set_elem_expr_alloc (ctx, set, + nla[NFTA_SET_ELEM_EXPR ]); +if (IS_ERR (expr)) +return PTR_ERR (expr); + } + err =nft_setelem_parse_key (ctx, set, &elem.key.val, nla[NFTA_SET_ELEM_KEY ]);if (err <0 ) -return err; + goto err_set_elem_expr;nft_set_ext_add_length (&tmpl,NFT_SET_EXT_KEY , set->klen);
@@ -5002,6 +5010,10 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,nft_set_ext_add (&tmpl,NFT_SET_EXT_TIMEOUT ); } +if (expr) +nft_set_ext_add_length (&tmpl,NFT_SET_EXT_EXPR , + expr->ops->size); +if (nla[NFTA_SET_ELEM_OBJREF ] !=NULL ) {if (!(set->flags &NFT_SET_OBJECT )) { err = -EINVAL ;
@@ -5086,6 +5098,10 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, *nft_set_ext_obj (ext) = obj; obj->use++; } +if (expr) { +memcpy (nft_set_ext_expr (ext), expr, expr->ops->size); +kfree (expr); + } trans =nft_trans_elem_alloc (ctx,NFT_MSG_NEWSETELEM , set);if (trans ==NULL )
@@ -5141,6 +5157,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,nft_data_release (&elem.key_end.val,NFT_DATA_VALUE ); err_parse_key:nft_data_release (&elem.key.val,NFT_DATA_VALUE ); +err_set_elem_expr: +if (expr !=NULL ) +nft_expr_destroy (ctx, expr);return err; }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH v2 4/5 netfilter: nf_tables: add nft_set_elem_update_expr() helper function
In reply to Pablo Neira AyusoThis helper function runs the eval path of the stateful expression of an existing set element.
--- include/net/netfilter/nf_tables.h | 12 ++++++++++++ net/netfilter/nft_dynset.c | 8 +------- 2 files changed, 13 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables.h +12 -0
@@ -897,6 +897,18 @@ static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)return (void *)&rule->data[rule->dlen]; } +static inline void nft_set_elem_update_expr (const struct nft_set_ext *ext, +struct nft_regs *regs, +const struct nft_pktinfo *pkt) +{ +struct nft_expr *expr; + +if (nft_set_ext_exists (ext,NFT_SET_EXT_EXPR )) { + expr =nft_set_ext_expr (ext); + expr->ops->eval (expr, regs, pkt); + } +} +/* * The last pointer isn't really necessary, but the compiler isn't able to * determine that the result of nft_expr_last() is always the same since it
net/netfilter/nft_dynset.c +1 -7
@@ -81,7 +81,6 @@ void nft_dynset_eval(const struct nft_expr *expr,const struct nft_dynset *priv =nft_expr_priv (expr);struct nft_set *set = priv->set;const struct nft_set_ext *ext; -const struct nft_expr *sexpr;u64 timeout;if (priv->op ==NFT_DYNSET_OP_DELETE ) {
@@ -91,18 +90,13 @@ void nft_dynset_eval(const struct nft_expr *expr,if (set->ops->update (set, ®s->data[priv->sreg_key], nft_dynset_new, expr, regs, &ext)) { - sexpr =NULL ; -if (nft_set_ext_exists (ext,NFT_SET_EXT_EXPR )) - sexpr =nft_set_ext_expr (ext); -if (priv->op ==NFT_DYNSET_OP_UPDATE &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPIRATION )) { timeout = priv->timeout ? : set->timeout; *nft_set_ext_expiration (ext) =get_jiffies_64 () + timeout; } -if (sexpr !=NULL ) - sexpr->ops->eval (sexpr, regs, pkt); +nft_set_elem_update_expr (ext, regs, pkt);if (priv->invert) regs->verdict.code =NFT_BREAK ;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH v2 5/5 netfilter: nft_lookup: update element stateful expression
In reply to Pablo Neira AyusoIf the set element comes with an stateful expression, update it.
--- net/netfilter/nft_lookup.c | 1 + 1 file changed, 1 insertion(+)
net/netfilter/nft_lookup.c +1 -0
@@ -43,6 +43,7 @@ void nft_lookup_eval(const struct nft_expr *expr,nft_data_copy (®s->data[priv->dreg],nft_set_ext_data (ext), set->dlen); +nft_set_elem_update_expr (ext, regs, pkt); }static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX +1 ] = {
Signature · 1 line
2.11.0