netfilter: bitwise: support boolean operations with variable RHS operands
Subsystems (2)
v4 cover netfilter: bitwise: support boolean operations with variable RHS operands
Currently bitwise boolean operations (AND, OR and XOR) can only have one variable operand. They are converted in user space into mask-and-xor operations on one register and two immediate values which are evaluated by the kernel. We add support for evaluating these operations directly in kernel space on one register and either an immediate value or a second register.
* Patch 1 renames functions and an enum constant related to the current mask-and-xor implementation in anticipation of adding support for directly evaluating AND, OR and XOR operations.
* Patch 2 adds support for directly evaluating AND, OR and XOR operations.
Changes since v3
* The patch to keep track of the bit-length of boolean expressions is no longer needed and has been dropped.
Changes since v2
* Increase size of nbits to u16 and correct checking of maximum
value (`U8_MAX * BITS_PER_BYTE`).
Changes since v1
* New patch added to keep track of the bit-length of boolean expressions.
* In v1, all boolean operations were still expected to be mask-and-xor operations, but the mask and xor values could be passed in registers.
Jeremy Sowden (2):
netfilter: bitwise: rename some boolean operation functions
netfilter: bitwise: add support for doing AND, OR and XOR directly
include/uapi/linux/netfilter/nf_tables.h | 19 ++- net/netfilter/nft_bitwise.c | 164 +++++++++++++++++++---- 2 files changed, 154 insertions(+), 29 deletions(-)
Signature · 1 line
2.39.2
PATCH v4 1/2 netfilter: bitwise: rename some boolean operation functions
In the next patch we add support for doing AND, OR and XOR operations directly in the kernel, so rename some functions and an enum constant related to mask-and-xor boolean operations.
--- include/uapi/linux/netfilter/nf_tables.h | 10 ++++--- net/netfilter/nft_bitwise.c | 34 ++++++++++++------------ 2 files changed, 24 insertions(+), 20 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +7 -3
@@ -555,16 +555,20 @@ enum nft_immediate_attributes {/** * enum nft_bitwise_ops - nf_tables bitwise operations * -* @NFT_BITWISE_BOOL: mask-and-xor operation used to implement NOT, AND, OR and -* XOR boolean operations +* @NFT_BITWISE_MASK_XOR: mask-and-xor operation used to implement NOT, AND, OR +* and XOR boolean operations * @NFT_BITWISE_LSHIFT: left-shift operation * @NFT_BITWISE_RSHIFT: right-shift operation */ enum nft_bitwise_ops { -NFT_BITWISE_BOOL , +NFT_BITWISE_MASK_XOR ,NFT_BITWISE_LSHIFT ,NFT_BITWISE_RSHIFT , }; +/* +* Old name for NFT_BITWISE_MASK_XOR. Retained for backwards-compatibility. +*/ +#define NFT_BITWISE_BOOL NFT_BITWISE_MASK_XOR/** * enum nft_bitwise_attributes - nf_tables bitwise expression netlink attributes
net/netfilter/nft_bitwise.c +17 -17
@@ -25,8 +25,8 @@ struct nft_bitwise {struct nft_data data; }; -static void nft_bitwise_eval_bool (u32 *dst,const u32 *src, -const struct nft_bitwise *priv) +static void nft_bitwise_eval_mask_xor (u32 *dst,const u32 *src, +const struct nft_bitwise *priv) {unsigned int i;
@@ -68,8 +68,8 @@ void nft_bitwise_eval(const struct nft_expr *expr,u32 *dst = ®s->data[priv->dreg];switch (priv->op) { -case NFT_BITWISE_BOOL : -nft_bitwise_eval_bool (dst, src, priv); +case NFT_BITWISE_MASK_XOR : +nft_bitwise_eval_mask_xor (dst, src, priv);break ;case NFT_BITWISE_LSHIFT :nft_bitwise_eval_lshift (dst, src, priv);
@@ -90,8 +90,8 @@ static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = { [NFTA_BITWISE_DATA] = { .type = NLA_NESTED }, }; -static int nft_bitwise_init_bool (struct nft_bitwise *priv, -const struct nlattr *const tb[]) +static int nft_bitwise_init_mask_xor (struct nft_bitwise *priv, +const struct nlattr *const tb[]) {struct nft_data_desc mask = { .type =NFT_DATA_VALUE ,
@@ -185,7 +185,7 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,if (tb[NFTA_BITWISE_OP ]) { priv->op =ntohl (nla_get_be32 (tb[NFTA_BITWISE_OP ]));switch (priv->op) { -case NFT_BITWISE_BOOL : +case NFT_BITWISE_MASK_XOR :case NFT_BITWISE_LSHIFT :case NFT_BITWISE_RSHIFT :break ;
@@ -193,12 +193,12 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,return -EOPNOTSUPP ; } }else { - priv->op =NFT_BITWISE_BOOL ; + priv->op =NFT_BITWISE_MASK_XOR ; }switch (priv->op) { -case NFT_BITWISE_BOOL : - err =nft_bitwise_init_bool (priv, tb); +case NFT_BITWISE_MASK_XOR : + err =nft_bitwise_init_mask_xor (priv, tb);break ;case NFT_BITWISE_LSHIFT :case NFT_BITWISE_RSHIFT :
@@ -209,8 +209,8 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,return err; } -static int nft_bitwise_dump_bool (struct sk_buff *skb, -const struct nft_bitwise *priv) +static int nft_bitwise_dump_mask_xor (struct sk_buff *skb, +const struct nft_bitwise *priv) {if (nft_data_dump (skb,NFTA_BITWISE_MASK , &priv->mask,NFT_DATA_VALUE , priv->len) <0 )
@@ -248,8 +248,8 @@ static int nft_bitwise_dump(struct sk_buff *skb,return -1 ;switch (priv->op) { -case NFT_BITWISE_BOOL : - err =nft_bitwise_dump_bool (skb, priv); +case NFT_BITWISE_MASK_XOR : + err =nft_bitwise_dump_mask_xor (skb, priv);break ;case NFT_BITWISE_LSHIFT :case NFT_BITWISE_RSHIFT :
@@ -269,7 +269,7 @@ static int nft_bitwise_offload(struct nft_offload_ctx *ctx,const struct nft_bitwise *priv =nft_expr_priv (expr);struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; -if (priv->op !=NFT_BITWISE_BOOL ) +if (priv->op !=NFT_BITWISE_MASK_XOR )return -EOPNOTSUPP ; if (memcmp(&priv->xor, &zero, sizeof(priv->xor)) ||
@@ -406,7 +406,7 @@ nft_bitwise_fast_dump(struct sk_buff *skb,return -1 ;if (nla_put_be32 (skb,NFTA_BITWISE_LEN ,htonl (sizeof (u32))))return -1 ; -if (nla_put_be32 (skb,NFTA_BITWISE_OP ,htonl (NFT_BITWISE_BOOL ))) +if (nla_put_be32 (skb,NFTA_BITWISE_OP ,htonl (NFT_BITWISE_MASK_XOR )))return -1 ; data.data[0 ] = priv->mask;
@@ -501,7 +501,7 @@ nft_bitwise_select_ops(const struct nft_ctx *ctx,return &nft_bitwise_ops;if (tb[NFTA_BITWISE_OP ] && -ntohl (nla_get_be32 (tb[NFTA_BITWISE_OP ])) !=NFT_BITWISE_BOOL ) +ntohl (nla_get_be32 (tb[NFTA_BITWISE_OP ])) !=NFT_BITWISE_MASK_XOR )return &nft_bitwise_ops;return &nft_bitwise_fast_ops;
Signature · 1 line
2.39.2
PATCH v4 2/2 netfilter: bitwise: add support for doing AND, OR and XOR directly
In reply to Jeremy SowdenHitherto, these operations have been converted in user space to mask-and-xor operations on one register and two immediate values, and it is the latter which have been evaluated by the kernel. We add support for evaluating these operations directly in kernel space on one register and either an immediate value or a second register.
--- include/uapi/linux/netfilter/nf_tables.h | 11 +- net/netfilter/nft_bitwise.c | 132 +++++++++++++++++++++-- 2 files changed, 132 insertions(+), 11 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +10 -1
@@ -559,14 +559,21 @@ enum nft_immediate_attributes {* and XOR boolean operations * @NFT_BITWISE_LSHIFT: left-shift operation * @NFT_BITWISE_RSHIFT: right-shift operation +* @NFT_BITWISE_AND: and operation +* @NFT_BITWISE_OR: or operation +* @NFT_BITWISE_XOR: xor operation */ enum nft_bitwise_ops {NFT_BITWISE_MASK_XOR ,NFT_BITWISE_LSHIFT ,NFT_BITWISE_RSHIFT , +NFT_BITWISE_AND , +NFT_BITWISE_OR , +NFT_BITWISE_XOR , };/* -* Old name for NFT_BITWISE_MASK_XOR. Retained for backwards-compatibility. +* Old name for NFT_BITWISE_MASK_XOR, predating the addition of NFT_BITWISE_AND, +* NFT_BITWISE_OR and NFT_BITWISE_XOR. Retained for backwards-compatibility. */ #define NFT_BITWISE_BOOL NFT_BITWISE_MASK_XOR
@@ -581,6 +588,7 @@ enum nft_bitwise_ops {* @NFTA_BITWISE_OP: type of operation (NLA_U32: nft_bitwise_ops) * @NFTA_BITWISE_DATA: argument for non-boolean operations * (NLA_NESTED: nft_data_attributes) +* @NFTA_BITWISE_SREG2: second source register (NLA_U32: nft_registers) * * The bitwise expression supports boolean and shift operations. It implements * the boolean operations by performing the following operation:
@@ -604,6 +612,7 @@ enum nft_bitwise_attributes {NFTA_BITWISE_XOR ,NFTA_BITWISE_OP ,NFTA_BITWISE_DATA , +NFTA_BITWISE_SREG2 , __NFTA_BITWISE_MAX };#define NFTA_BITWISE_MAX (__NFTA_BITWISE_MAX - 1)
net/netfilter/nft_bitwise.c +122 -10
206 lines not on this page · raw message
Signature · 1 line
2.39.2