netfilter: nf_tables: consolidate set description
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v5 1/4 netfilter: nf_tables: consolidate set description
Add the following fields to the set description:
- key type
- data type
- object type
- policy
- gc_int: garbage collection interval)
- timeout: element timeout
This prepares for stricter set type checks on updates in a follow up patch.
---
v5: no changes
include/net/netfilter/nf_tables.h | 12 +++++++ net/netfilter/nf_tables_api.c | 58 +++++++++++++++---------------- 2 files changed, 40 insertions(+), 30 deletions(-)
include/net/netfilter/nf_tables.h +12 -0
@@ -312,17 +312,29 @@ struct nft_set_iter {/** * struct nft_set_desc - description of set elements * +* @ktype: key type * @klen: key length +* @dtype: data type * @dlen: data length +* @objtype: object type +* @flags: flags * @size: number of set elements +* @policy: set policy +* @gc_int: garbage collector interval * @field_len: length of each field in concatenation, bytes * @field_count: number of concatenated fields in element * @expr: set must support for expressions */ struct nft_set_desc { +u32 ktype;unsigned int klen; +u32 dtype;unsigned int dlen; +u32 objtype;unsigned int size; +u32 policy; +u32 gc_int; +u64 timeout;u8 field_len[NFT_REG32_COUNT ];u8 field_count;bool expr;
net/netfilter/nf_tables_api.c +28 -30
146 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v5 2/4 netfilter: nf_tables: add function to create set stateful expressions
Add a helper function to allocate and initialize the stateful expressions that are defined in a set.
This patch allows to reuse this code from the set update path, to check that type of the update matches the existing set in the kernel.
---
v5: remove struct nft_expr *expr that shadows existing variable of the same name.
net/netfilter/nf_tables_api.c | 106 ++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 38 deletions(-)
net/netfilter/nf_tables_api.c +68 -38
@@ -4388,6 +4388,59 @@ static int nf_tables_set_desc_parse(struct nft_set_desc *desc,return err; } +static int nft_set_expr_alloc (struct nft_ctx *ctx,struct nft_set *set, +const struct nlattr *const *nla, +struct nft_expr **exprs,int *num_exprs, +u32 flags) +{ +struct nft_expr *expr; +int err, i; + +if (nla[NFTA_SET_EXPR ]) { + expr =nft_set_elem_expr_alloc (ctx, set, nla[NFTA_SET_EXPR ]); +if (IS_ERR (expr)) { + err =PTR_ERR (expr); + goto err_set_expr_alloc; + } + exprs[0 ] = expr; + (*num_exprs)++; + }else if (nla[NFTA_SET_EXPRESSIONS ]) { +struct nlattr *tmp; +int left; + +if (!(flags &NFT_SET_EXPR )) { + err = -EINVAL ; + goto err_set_expr_alloc; + } + i =0 ; +nla_for_each_nested (tmp, nla[NFTA_SET_EXPRESSIONS ], left) { +if (i ==NFT_SET_EXPR_MAX ) { + err = -E2BIG ; + goto err_set_expr_alloc; + } +if (nla_type (tmp) !=NFTA_LIST_ELEM ) { + err = -EINVAL ; + goto err_set_expr_alloc; + } + expr =nft_set_elem_expr_alloc (ctx, set, tmp); +if (IS_ERR (expr)) { + err =PTR_ERR (expr); + goto err_set_expr_alloc; + } + exprs[i++] = expr; + (*num_exprs)++; + } + } + +return 0 ; + +err_set_expr_alloc: +for (i =0 ; i < *num_exprs; i++) +nft_expr_destroy (ctx, exprs[i]); + +return err; +} +static int nf_tables_newset (struct sk_buff *skb,const struct nfnl_info *info,const struct nlattr *const nla[]) {
@@ -4395,7 +4448,6 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,u8 genmask =nft_genmask_next (info->net);u8 family = info->nfmsg->nfgen_family;const struct nft_set_ops *ops; -struct nft_expr *expr =NULL ;struct net *net = info->net;struct nft_set_desc desc;struct nft_table *table;
@@ -4403,6 +4455,7 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,struct nft_set *set;struct nft_ctx ctx;size_t alloc_size; +int num_exprs =0 ;char *name;int err, i;u16 udlen;
@@ -4529,6 +4582,8 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,return PTR_ERR (set); } }else { +struct nft_expr *exprs[NFT_SET_EXPR_MAX ] = {}; +if (info->nlh->nlmsg_flags &NLM_F_EXCL ) {NL_SET_BAD_ATTR (extack, nla[NFTA_SET_NAME ]);return -EEXIST ;
@@ -4536,6 +4591,13 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,if (info->nlh->nlmsg_flags &NLM_F_REPLACE )return -EOPNOTSUPP ; + err =nft_set_expr_alloc (&ctx, set, nla, exprs, &num_exprs, flags); +if (err <0 ) +return err; + +for (i =0 ; i < num_exprs; i++) +nft_expr_destroy (&ctx, exprs[i]); +return 0 ; }
@@ -4603,43 +4665,11 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,if (err <0 ) goto err_set_init; -if (nla[NFTA_SET_EXPR ]) { - expr =nft_set_elem_expr_alloc (&ctx, set, nla[NFTA_SET_EXPR ]); -if (IS_ERR (expr)) { - err =PTR_ERR (expr); - goto err_set_expr_alloc; - } - set->exprs[0 ] = expr; - set->num_exprs++; - }else if (nla[NFTA_SET_EXPRESSIONS ]) { -struct nft_expr *expr; -struct nlattr *tmp; -int left; - -if (!(flags &NFT_SET_EXPR )) { - err = -EINVAL ; - goto err_set_expr_alloc; - } - i =0 ; -nla_for_each_nested (tmp, nla[NFTA_SET_EXPRESSIONS ], left) { -if (i ==NFT_SET_EXPR_MAX ) { - err = -E2BIG ; - goto err_set_expr_alloc; - } -if (nla_type (tmp) !=NFTA_LIST_ELEM ) { - err = -EINVAL ; - goto err_set_expr_alloc; - } - expr =nft_set_elem_expr_alloc (&ctx, set, tmp); -if (IS_ERR (expr)) { - err =PTR_ERR (expr); - goto err_set_expr_alloc; - } - set->exprs[i++] = expr; - set->num_exprs++; - } - } + err =nft_set_expr_alloc (&ctx, set, nla, set->exprs, &num_exprs, flags); +if (err <0 ) + goto err_set_destroy; + set->num_exprs = num_exprs; set->handle =nf_tables_alloc_handle (table); err =nft_trans_set_add (&ctx,NFT_MSG_NEWSET , set);
@@ -4653,7 +4683,7 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, err_set_expr_alloc:for (i =0 ; i < set->num_exprs; i++)nft_expr_destroy (&ctx, set->exprs[i]); - +err_set_destroy: ops->destroy (set); err_set_init:kfree (set->name);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v5 3/4 netfilter: nf_tables: perform type checking for existing sets
In reply to Pablo Neira AyusoIf a ruleset declares a set name that matches an existing set in the kernel, then validate that this declaration really refers to the same set, otherwise bail out with EEXIST.
Currently, the kernel reports success when adding a set that already exists in the kernel. This usually results in EINVAL errors at a later stage, when the user adds elements to the set, if the set declaration mismatches the existing set representation in the kernel.
Add a new function to check that the set declaration really refers to the same existing set in the kernel.
---
v5: no changes.
net/netfilter/nf_tables_api.c | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +35 -1
@@ -4441,6 +4441,34 @@ static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set,return err; } +static bool nft_set_is_same (const struct nft_set *set, +const struct nft_set_desc *desc, +struct nft_expr *exprs[],u32 num_exprs,u32 flags) +{ +int i; + +if (set->ktype != desc->ktype || + set->dtype != desc->dtype || + set->flags != flags || + set->klen != desc->klen || + set->dlen != desc->dlen || + set->field_count != desc->field_count || + set->num_exprs != num_exprs) +return false; + +for (i =0 ; i < desc->field_count; i++) { +if (set->field_len[i] != desc->field_len[i]) +return false; + } + +for (i =0 ; i < num_exprs; i++) { +if (set->exprs[i]->ops != exprs[i]->ops) +return false; + } + +return true; +} +static int nf_tables_newset (struct sk_buff *skb,const struct nfnl_info *info,const struct nlattr *const nla[]) {
@@ -4595,10 +4623,16 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,if (err <0 )return err; + err =0 ; +if (!nft_set_is_same (set, &desc, exprs, num_exprs, flags)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_SET_NAME ]); + err = -EEXIST ; + } +for (i =0 ; i < num_exprs; i++)nft_expr_destroy (&ctx, exprs[i]); -return 0 ; +return err; }if (!(info->nlh->nlmsg_flags &NLM_F_CREATE ))
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v5 4/4 netfilter: nf_tables: honor set timeout and garbage collection updates
In reply to Pablo Neira AyusoSet timeout and garbage collection interval updates are ignored on updates. Add transaction to update global set element timeout and garbage collection interval.
---
v5: use READ_ONCE to read gc_int in _fill_info() path.
include/net/netfilter/nf_tables.h | 13 ++++++- net/netfilter/nf_tables_api.c | 59 +++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 19 deletions(-)
include/net/netfilter/nf_tables.h +12 -1
@@ -597,7 +597,9 @@ 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 ; +u32 gc_int =READ_ONCE (set->gc_int); + +return gc_int ?msecs_to_jiffies (gc_int) :HZ ; }/**
@@ -1570,6 +1572,9 @@ struct nft_trans_rule {struct nft_trans_set {struct nft_set *set;u32 set_id; +u32 gc_int; +u64 timeout; +bool update;bool bound; };
@@ -1579,6 +1584,12 @@ struct nft_trans_set { (((struct nft_trans_set *)trans->data)->set_id)#define nft_trans_set_bound (trans) \ (((struct nft_trans_set *)trans->data)->bound) +#define nft_trans_set_update (trans) \ + (((struct nft_trans_set *)trans->data)->update) +#define nft_trans_set_timeout (trans) \ + (((struct nft_trans_set *)trans->data)->timeout) +#define nft_trans_set_gc_int (trans) \ + (((struct nft_trans_set *)trans->data)->gc_int)struct nft_trans_chain {bool update;
net/netfilter/nf_tables_api.c +41 -18
122 lines not on this page · raw message
Signature · 1 line
2.30.2