netfilter: nftables: generalize set expressions support
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v4 1/4 netfilter: nftables: generalize set expressions support
Currently, the set infrastucture allows for one single expressions per element. This patch extends the existing infrastructure to allow for up to two expressions. This is not updating the netlink API yet, this is coming as an initial preparation patch.
---
v4: fix err_set_elem_expr error path in nft_add_set_elem().
Check for set->num_exprs != 1 from within NFTA_SET_ELEM_EXPR parser
in nft_add_set_elem().include/net/netfilter/nf_tables.h | 5 +- net/netfilter/nf_tables_api.c | 90 ++++++++++++++++++++++--------- net/netfilter/nft_dynset.c | 3 +- 3 files changed, 70 insertions(+), 28 deletions(-)
include/net/netfilter/nf_tables.h +4 -1
@@ -396,6 +396,8 @@ struct nft_set_type { };#define to_set_type (o) container_of(o, struct nft_set_type, ops) +#define NFT_SET_EXPR_MAX 2 +/** * struct nft_set - nf_tables set instance *
@@ -448,13 +450,14 @@ struct nft_set {u16 policy;u16 udlen;unsigned char *udata; -struct nft_expr *expr;/* runtime data below here */ const struct nft_set_ops *ops____cacheline_aligned ; u16 flags:14, genmask:2;u8 klen;u8 dlen; +u8 num_exprs; +struct nft_expr *exprs[NFT_SET_EXPR_MAX ]; unsigned char data[] __attribute__((aligned(__alignof__(u64)))); };
net/netfilter/nf_tables_api.c +64 -26
179 lines not on this page · raw message
net/netfilter/nft_dynset.c +2 -1
@@ -188,7 +188,8 @@ static int nft_dynset_init(const struct nft_ctx *ctx,if (IS_ERR (priv->expr))return PTR_ERR (priv->expr); -if (set->expr && set->expr->ops != priv->expr->ops) { +if (set->num_exprs ==1 && + set->exprs[0 ]->ops != priv->expr->ops) { err = -EOPNOTSUPP ; goto err_expr_free; }
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH v4 2/4 netfilter: nftables: move nft_expr before nft_set
Move the nft_expr structure definition before nft_set. Expressions are used by rules and sets, remove unnecessary forward declarations. This comes as preparation to support for multiple expressions per set element.
---
v4: no changes
include/net/netfilter/nf_tables.h | 54 +++++++++++++++---------------- 1 file changed, 26 insertions(+), 28 deletions(-)
include/net/netfilter/nf_tables.h +26 -28
@@ -305,8 +305,33 @@ struct nft_set_estimate {enum nft_set_class space; }; +#define NFT_EXPR_MAXATTR 16 +#define NFT_EXPR_SIZE (size) (sizeof(struct nft_expr) + \ + ALIGN(size, __alignof__(struct nft_expr))) + +/** +* struct nft_expr - nf_tables expression +* +* @ops: expression ops +* @data: expression private data +*/ +struct nft_expr { +const struct nft_expr_ops *ops; +unsigned char data[] + __attribute__((aligned (__alignof__(u64 )))); +}; + +static inline void *nft_expr_priv (const struct nft_expr *expr) +{ +return (void *)expr->data; +} + +int nft_expr_clone (struct nft_expr *dst,struct nft_expr *src); +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); +struct nft_set_ext ; -struct nft_expr ;/** * struct nft_set_ops - nf_tables set operations
@@ -797,7 +822,6 @@ struct nft_offload_ctx;* @validate: validate expression, called during loop detection * @data: extra data to attach to this expression operation */ -struct nft_expr ;struct nft_expr_ops { void (*eval)(const struct nft_expr *expr, struct nft_regs *regs,
@@ -833,32 +857,6 @@ struct nft_expr_ops {void *data; }; -#define NFT_EXPR_MAXATTR 16 -#define NFT_EXPR_SIZE (size) (sizeof(struct nft_expr) + \ - ALIGN(size, __alignof__(struct nft_expr))) - -/** -* struct nft_expr - nf_tables expression -* -* @ops: expression ops -* @data: expression private data -*/ -struct nft_expr { -const struct nft_expr_ops *ops; -unsigned char data[] - __attribute__((aligned (__alignof__(u64 )))); -}; - -static inline void *nft_expr_priv (const struct nft_expr *expr) -{ -return (void *)expr->data; -} - -int nft_expr_clone (struct nft_expr *dst,struct nft_expr *src); -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); -/** * struct nft_rule - nf_tables rule *
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH v4 3/4 netfilter: nftables: generalize set extension to support for several expressions
In reply to Pablo Neira AyusoThis patch replaces NFT_SET_EXPR by NFT_SET_EXT_EXPRESSIONS. This new extension allows to attach several expressions to one set element (not only one single expression as NFT_SET_EXPR provides). This patch prepares for support for several expressions per set element in the netlink userspace API.
---
v4: fix memleak in NFTA_DYNSET_EXPR parser on error path.
fix check for invalid expression type in NFTA_DYNSET_EXPR too.
include/net/netfilter/nf_tables.h | 36 ++++++++--- net/netfilter/nf_tables_api.c | 85 ++++++++++++++++++------ net/netfilter/nft_dynset.c | 104 ++++++++++++++++++++++++------ net/netfilter/nft_set_hash.c | 27 ++++++-- 4 files changed, 197 insertions(+), 55 deletions(-)
include/net/netfilter/nf_tables.h +28 -8
@@ -421,6 +421,20 @@ struct nft_set_type { };#define to_set_type (o) container_of(o, struct nft_set_type, ops) +struct nft_set_elem_expr { +u8 size; +unsigned char data[] + __attribute__((aligned (__alignof__(struct nft_expr )))); +}; + +#define nft_setelem_expr_at (__elem_expr, __offset) \ + ((struct nft_expr *)&__elem_expr->data[__offset]) + +#define nft_setelem_expr_foreach (__expr, __elem_expr, __size) \ + for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \ + __size < (__elem_expr)->size; \ + __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size) +#define NFT_SET_EXPR_MAX 2/**
@@ -547,7 +561,7 @@ void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);* @NFT_SET_EXT_TIMEOUT: element timeout * @NFT_SET_EXT_EXPIRATION: element expiration time * @NFT_SET_EXT_USERDATA: user data associated with the element -* @NFT_SET_EXT_EXPR: expression assiociated with the element +* @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element * @NFT_SET_EXT_OBJREF: stateful object reference associated with element * @NFT_SET_EXT_NUM: number of extension types */
@@ -559,7 +573,7 @@ enum nft_set_extensions {NFT_SET_EXT_TIMEOUT ,NFT_SET_EXT_EXPIRATION ,NFT_SET_EXT_USERDATA , -NFT_SET_EXT_EXPR , +NFT_SET_EXT_EXPRESSIONS ,NFT_SET_EXT_OBJREF , NFT_SET_EXT_NUM };
@@ -677,9 +691,9 @@ static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_extreturn nft_set_ext (ext,NFT_SET_EXT_USERDATA ); } -static inline struct nft_expr *nft_set_ext_expr (const struct nft_set_ext *ext) +static inline struct nft_set_elem_expr *nft_set_ext_expr (const struct nft_set_ext *ext) { -return nft_set_ext (ext,NFT_SET_EXT_EXPR ); +return nft_set_ext (ext,NFT_SET_EXT_EXPRESSIONS ); }static inline bool nft_set_elem_expired (const struct nft_set_ext *ext)
@@ -909,11 +923,17 @@ 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_set_elem_expr *elem_expr;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); +u32 size; + +if (__nft_set_ext_exists (ext,NFT_SET_EXT_EXPRESSIONS )) { + elem_expr =nft_set_ext_expr (ext); + nft_setelem_expr_foreach(expr, elem_expr, size) { + expr->ops->eval (expr, regs, pkt); +if (regs->verdict.code ==NFT_BREAK ) +return ; + } } }
net/netfilter/nf_tables_api.c +65 -20
154 lines not on this page · raw message
net/netfilter/nft_dynset.c +84 -20
173 lines not on this page · raw message
net/netfilter/nft_set_hash.c +20 -7
@@ -293,6 +293,22 @@ static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,rhashtable_walk_exit (&hti); } +static bool nft_rhash_expr_needs_gc_run (const struct nft_set *set, +struct nft_set_ext *ext) +{ +struct nft_set_elem_expr *elem_expr =nft_set_ext_expr (ext); +struct nft_expr *expr; +u32 size; + + nft_setelem_expr_foreach(expr, elem_expr, size) { +if (expr->ops->gc && + expr->ops->gc (read_pnet (&set->net), expr)) +return true; + } + +return false; +} +static void nft_rhash_gc (struct work_struct *work) {struct nft_set *set;
@@ -314,16 +330,13 @@ static void nft_rhash_gc(struct work_struct *work)continue ; } -if (nft_set_ext_exists (&he->ext,NFT_SET_EXT_EXPR )) { -struct nft_expr *expr =nft_set_ext_expr (&he->ext); +if (nft_set_ext_exists (&he->ext,NFT_SET_EXT_EXPRESSIONS ) && +nft_rhash_expr_needs_gc_run (set, &he->ext)) + goto needs_gc_run; -if (expr->ops->gc && - expr->ops->gc (read_pnet (&set->net), expr)) - goto gc; - }if (!nft_set_elem_expired (&he->ext))continue ; -gc: +needs_gc_run:if (nft_set_elem_mark_busy (&he->ext))continue ;
Signature · 1 line
2.20.1
kernel test robot
build botRe: [PATCH nf-next, v4 3/4] netfilter: nftables: generalize set extension to support for several expressions
I love your patch! Perhaps something to improve:
Hi Pablo,
I love your patch! Perhaps something to improve:
[auto build test WARNING on nf-next/master]
[also build test WARNING on v5.10-rc7 next-20201211]
[cannot apply to nf/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Pablo-Neira-Ayuso/netfilter-nftables-generalize-set-expressions-support/20201212-183708 base: https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master compiler: riscv64-linux-gcc (GCC) 9.3.0
If you fix the issue, kindly add following tag as appropriate
"cppcheck warnings: (new ones prefixed by >>)"
>> net/netfilter/nft_dynset.c:337:61: warning: Uninitialized variable: i [uninitvar]
if (nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr_array[i]))
^--
cppcheck possible warnings: (new ones prefixed by >>, may not real problems)
>> net/netfilter/nft_set_hash.c:90:3: warning: Address of local auto-variable assigned to a function parameter. [autoVariables]
*ext = &he->ext;
^
net/netfilter/nft_set_hash.c:149:2: warning: Address of local auto-variable assigned to a function parameter. [autoVariables]
*ext = &he->ext;
^
net/netfilter/nft_set_hash.c:176:3: warning: Address of local auto-variable assigned to a function parameter. [autoVariables]
*ext = &prev->ext;
^
net/netfilter/nft_set_hash.c:448:4: warning: Address of local auto-variable assigned to a function parameter. [autoVariables]
*ext = &he->ext;
^
net/netfilter/nft_set_hash.c:489:4: warning: Address of local auto-variable assigned to a function parameter. [autoVariables]
*ext = &he->ext;
^
net/netfilter/nft_set_hash.c:527:4: warning: Address of local auto-variable assigned to a function parameter. [autoVariables]
*ext = &he->ext;
^vim +337 net/netfilter/nft_dynset.c
316
317 static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
318 {
319 const struct nft_dynset *priv = nft_expr_priv(expr);
320 u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
321 int i;
322
323 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
324 goto nla_put_failure;
325 if (priv->set->flags & NFT_SET_MAP &&
326 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
327 goto nla_put_failure;
328 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
329 goto nla_put_failure;
330 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
331 goto nla_put_failure;
332 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
333 cpu_to_be64(jiffies_to_msecs(priv->timeout)),
334 NFTA_DYNSET_PAD))
335 goto nla_put_failure;
336 if (priv->num_exprs == 1) {
> 337 if (nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr_array[i]))
338 goto nla_put_failure;
339 }
340 if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
341 goto nla_put_failure;
342 return 0;
343
344 nla_put_failure:
345 return -1;
346 }
347
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Pablo Neira Ayuso
maintainerPATCH v4 4/4 netfilter: nftables: netlink support for several set element expressions
In reply to Pablo Neira AyusoThis patch adds three new netlink attribute to encapsulate a list of expressions per set elements:
- NFTA_SET_EXPRESSIONS: this attribute provides the set definition in terms of expressions. New set elements get attached the list of expressions that is specified by this new netlink attribute.
- NFTA_SET_ELEM_EXPRESSIONS: this attribute allows users to restore (or initialize) the stateful information of set elements when adding an element to the set.
- NFTA_DYNSET_EXPRESSIONS: this attribute specifies the list of expressions that the set element gets when it is inserted from the packet path.
---
v4: missing check for NFTA_LIST_ELEM in the new netlink attribute parsers.
fix memleak in error paths of the NFTA_SET_ELEM_EXPRESSIONS and NFTA_DYNSET_EXPRESSIONS parsers.
include/uapi/linux/netfilter/nf_tables.h | 6 ++ net/netfilter/nf_tables_api.c | 92 +++++++++++++++++++++++- net/netfilter/nft_dynset.c | 56 +++++++++++++-- 3 files changed, 147 insertions(+), 7 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +6 -0
@@ -361,6 +361,7 @@ enum nft_set_field_attributes {* @NFTA_SET_OBJ_TYPE: stateful object type (NLA_U32: NFT_OBJECT_*) * @NFTA_SET_HANDLE: set handle (NLA_U64) * @NFTA_SET_EXPR: set expression (NLA_NESTED: nft_expr_attributes) +* @NFTA_SET_EXPRESSIONS: list of expressions (NLA_NESTED: nft_list_attributes) */ enum nft_set_attributes {NFTA_SET_UNSPEC ,
@@ -381,6 +382,7 @@ enum nft_set_attributes {NFTA_SET_OBJ_TYPE ,NFTA_SET_HANDLE ,NFTA_SET_EXPR , +NFTA_SET_EXPRESSIONS , __NFTA_SET_MAX };#define NFTA_SET_MAX (__NFTA_SET_MAX - 1)
@@ -406,6 +408,7 @@ enum nft_set_elem_flags {* @NFTA_SET_ELEM_EXPR: expression (NLA_NESTED: nft_expr_attributes) * @NFTA_SET_ELEM_OBJREF: stateful object reference (NLA_STRING) * @NFTA_SET_ELEM_KEY_END: closing key value (NLA_NESTED: nft_data) +* @NFTA_SET_ELEM_EXPRESSIONS: list of expressions (NLA_NESTED: nft_list_attributes) */ enum nft_set_elem_attributes {NFTA_SET_ELEM_UNSPEC ,
@@ -419,6 +422,7 @@ enum nft_set_elem_attributes {NFTA_SET_ELEM_PAD ,NFTA_SET_ELEM_OBJREF ,NFTA_SET_ELEM_KEY_END , +NFTA_SET_ELEM_EXPRESSIONS , __NFTA_SET_ELEM_MAX };#define NFTA_SET_ELEM_MAX (__NFTA_SET_ELEM_MAX - 1)
@@ -715,6 +719,7 @@ enum nft_dynset_flags {* @NFTA_DYNSET_TIMEOUT: timeout value for the new element (NLA_U64) * @NFTA_DYNSET_EXPR: expression (NLA_NESTED: nft_expr_attributes) * @NFTA_DYNSET_FLAGS: flags (NLA_U32) +* @NFTA_DYNSET_EXPRESSIONS: list of expressions (NLA_NESTED: nft_list_attributes) */ enum nft_dynset_attributes {NFTA_DYNSET_UNSPEC ,
@@ -727,6 +732,7 @@ enum nft_dynset_attributes {NFTA_DYNSET_EXPR ,NFTA_DYNSET_PAD ,NFTA_DYNSET_FLAGS , +NFTA_DYNSET_EXPRESSIONS , __NFTA_DYNSET_MAX, };#define NFTA_DYNSET_MAX (__NFTA_DYNSET_MAX - 1)
net/netfilter/nf_tables_api.c +89 -3
154 lines not on this page · raw message
net/netfilter/nft_dynset.c +52 -4
83 lines not on this page · raw message
Signature · 1 line
2.20.1