bitwise shift support
v3 cover bitwise shift support
The kernel supports bitwise shift operations. This patch-set adds the support to nft. There are a few preliminary housekeeping patches.
Changes since v2:
* set the type and byte-order of righthand shift operands to integer
and host-endian during delinearization;
* always set the length of righthand shift operands to 32 bits during linearization.
Changes since v1:
* update to the final kernel and libnftnl API's;
* update nf_tables.h in a separate patch;
* change byte-order of payload shifts generated by expr_evaluate_bits.
Jeremy Sowden (9):
Update gitignore.
src: white-space fixes.
netlink_delinearize: fix typo.
netlink_delinearize: remove commented out pr_debug statement.
parser: add parenthesized statement expressions.
evaluate: change shift byte-order to host-endian.
include: update nf_tables.h.
netlink: add support for handling shift expressions.
tests: shell: add bit-shift tests.
.gitignore | 9 ++ include/linux/netfilter/nf_tables.h | 23 +++++ src/evaluate.c | 13 ++- src/netlink_delinearize.c | 93 +++++++++++++++---- src/netlink_linearize.c | 52 ++++++++++- src/parser_bison.y | 25 ++--- tests/shell/testcases/chains/0040mark_shift_0 | 11 +++ tests/shell/testcases/chains/0040mark_shift_1 | 11 +++ .../chains/dumps/0040mark_shift_0.nft | 6 ++ .../chains/dumps/0040mark_shift_1.nft | 6 ++ 10 files changed, 208 insertions(+), 41 deletions(-) create mode 100755 tests/shell/testcases/chains/0040mark_shift_0 create mode 100755 tests/shell/testcases/chains/0040mark_shift_1 create mode 100644 tests/shell/testcases/chains/dumps/0040mark_shift_0.nft create mode 100644 tests/shell/testcases/chains/dumps/0040mark_shift_1.nft
Signature · 1 line
2.24.1
PATCH v3 1/9 Update gitignore.
Add ctags and etags tag files, and Emacs back-up files.
--- .gitignore | 9 +++++++++ 1 file changed, 9 insertions(+)
.gitignore +9 -0
@@ -19,3 +19,12 @@ libtool # Debian package build temporary files build-stamp + +# Tag files for Vim and Emacs. +TAGS +tags + +# Emacs back-up files. +*~ +\#*\# +.\#*
Signature · 1 line
2.24.1
PATCH v3 2/9 src: white-space fixes.
In reply to Jeremy SowdenRemove some trailing white-space and fix some indentation.
--- src/evaluate.c | 11 +++++------ src/netlink_delinearize.c | 2 +- src/netlink_linearize.c | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-)
src/evaluate.c +5 -6
@@ -2336,14 +2336,13 @@ static int stmt_evaluate_meta(struct eval_ctx *ctx, struct stmt *stmt)static int stmt_evaluate_ct (struct eval_ctx *ctx,struct stmt *stmt) {if (stmt_evaluate_arg (ctx, stmt, - stmt->ct.tmpl->dtype, - stmt->ct.tmpl->len, - stmt->ct.tmpl->byteorder, - &stmt->ct.expr) <0 ) + stmt->ct.tmpl->dtype, + stmt->ct.tmpl->len, + stmt->ct.tmpl->byteorder, + &stmt->ct.expr) <0 )return -1 ; -if (stmt->ct.key ==NFT_CT_SECMARK && -expr_is_constant (stmt->ct.expr)) +if (stmt->ct.key ==NFT_CT_SECMARK &&expr_is_constant (stmt->ct.expr))return stmt_error (ctx, stmt,"ct secmark must not be set to constant value" );
src/netlink_delinearize.c +1 -1
@@ -171,7 +171,7 @@ static void netlink_parse_immediate(struct netlink_parse_ctx *ctx,struct expr *expr;if (nftnl_expr_is_set (nle,NFTNL_EXPR_IMM_VERDICT )) { - nld.verdict =nftnl_expr_get_u32 (nle,NFTNL_EXPR_IMM_VERDICT ); + nld.verdict =nftnl_expr_get_u32 (nle,NFTNL_EXPR_IMM_VERDICT );if (nftnl_expr_is_set (nle,NFTNL_EXPR_IMM_CHAIN )) { nld.chain =nftnl_expr_get (nle,NFTNL_EXPR_IMM_CHAIN , &nld.len);
src/netlink_linearize.c +1 -1
@@ -1243,7 +1243,7 @@ static void netlink_gen_queue_stmt(struct netlink_linearize_ctx *ctx, }static void netlink_gen_ct_stmt (struct netlink_linearize_ctx *ctx, -const struct stmt *stmt) +const struct stmt *stmt) {struct nftnl_expr *nle;enum nft_registers sreg;
Signature · 1 line
2.24.1
PATCH v3 3/9 netlink_delinearize: fix typo.
In reply to Jeremy Sowdens/Of/If/ in comment describing function.
--- src/netlink_delinearize.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/netlink_delinearize.c +1 -1
@@ -2352,7 +2352,7 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop)* the original payload expression because it has an odd size or * a non-byte divisible offset/length. * -* Of that was the case, the 'value' expression is not a value but +* If that was the case, the 'value' expression is not a value but * a binop expression with a munged payload expression on the left * and a mask to clear the real payload offset/length. *
Signature · 1 line
2.24.1
PATCH v3 4/9 netlink_delinearize: remove commented out pr_debug statement.
In reply to Jeremy SowdenThe statement doesn't compile, so remove it.
--- src/netlink_delinearize.c | 2 -- 1 file changed, 2 deletions(-)
src/netlink_delinearize.c +0 -2
@@ -2047,8 +2047,6 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp) {struct expr *expr = *exprp, *i; -//pr_debug("%s len %u\n", expr->ops->name, expr->len); -switch (expr->etype) {case EXPR_MAP :switch (expr->map->etype) {
Signature · 1 line
2.24.1
PATCH v3 5/9 parser: add parenthesized statement expressions.
In reply to Jeremy SowdenPrimary and primary RHS expressions support parenthesized basic and basic RHS expressions. However, primary statement expressions do not support parenthesized basic statement expressions. Add them.
--- src/parser_bison.y | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
src/parser_bison.y +13 -12
@@ -2992,18 +2992,19 @@ synproxy_sack : /* empty */ { $$ = 0; } } ; -primary_stmt_expr : symbol_expr { $$ = $1; } - | integer_expr { $$ = $1; } - | boolean_expr { $$ = $1; } - | meta_expr { $$ = $1; } - | rt_expr { $$ = $1; } - | ct_expr { $$ = $1; } - | numgen_expr { $$ = $1; } - | hash_expr { $$ = $1; } - | payload_expr { $$ = $1; } - | keyword_expr { $$ = $1; } - | socket_expr { $$ = $1; } - | osf_expr { $$ = $1; } +primary_stmt_expr : symbol_expr { $$ = $1; } + | integer_expr { $$ = $1; } + | boolean_expr { $$ = $1; } + | meta_expr { $$ = $1; } + | rt_expr { $$ = $1; } + | ct_expr { $$ = $1; } + | numgen_expr { $$ = $1; } + | hash_expr { $$ = $1; } + | payload_expr { $$ = $1; } + | keyword_expr { $$ = $1; } + | socket_expr { $$ = $1; } + | osf_expr { $$ = $1; } + | '(' basic_stmt_expr ')' { $$ = $2; } ; shift_stmt_expr : primary_stmt_expr
Signature · 1 line
2.24.1
PATCH v3 6/9 evaluate: change shift byte-order to host-endian.
In reply to Jeremy SowdenThe byte-order of the righthand operands of the right-shifts generated for payload and exthdr expressions is big-endian. However, all right shift operands should be host-endian.
--- src/evaluate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/evaluate.c +1 -1
@@ -487,7 +487,7 @@ static void expr_evaluate_bits(struct eval_ctx *ctx, struct expr **exprp)if (shift) { off =constant_expr_alloc (&expr->location,expr_basetype (expr), -BYTEORDER_BIG_ENDIAN , +BYTEORDER_HOST_ENDIAN ,sizeof (shift), &shift); lshift =binop_expr_alloc (&expr->location,OP_RSHIFT , and, off);
Signature · 1 line
2.24.1
PATCH v3 7/9 include: update nf_tables.h.
In reply to Jeremy SowdenThe kernel UAPI header includes a couple of new bitwise netlink attributes and an enum.
--- include/linux/netfilter/nf_tables.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
include/linux/netfilter/nf_tables.h +23 -0
@@ -144,12 +144,14 @@ enum nft_list_attributes {* @NFTA_HOOK_HOOKNUM: netfilter hook number (NLA_U32) * @NFTA_HOOK_PRIORITY: netfilter hook priority (NLA_U32) * @NFTA_HOOK_DEV: netdevice name (NLA_STRING) +* @NFTA_HOOK_DEVS: list of netdevices (NLA_NESTED) */ enum nft_hook_attributes {NFTA_HOOK_UNSPEC ,NFTA_HOOK_HOOKNUM ,NFTA_HOOK_PRIORITY ,NFTA_HOOK_DEV , +NFTA_HOOK_DEVS , __NFTA_HOOK_MAX };#define NFTA_HOOK_MAX (__NFTA_HOOK_MAX - 1)
@@ -482,6 +484,20 @@ enum nft_immediate_attributes { };#define NFTA_IMMEDIATE_MAX (__NFTA_IMMEDIATE_MAX - 1) +/** +* 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_LSHIFT: left-shift operation +* @NFT_BITWISE_RSHIFT: right-shift operation +*/ +enum nft_bitwise_ops { +NFT_BITWISE_BOOL , +NFT_BITWISE_LSHIFT , +NFT_BITWISE_RSHIFT , +}; +/** * enum nft_bitwise_attributes - nf_tables bitwise expression netlink attributes *
@@ -490,6 +506,9 @@ enum nft_immediate_attributes {* @NFTA_BITWISE_LEN: length of operands (NLA_U32) * @NFTA_BITWISE_MASK: mask value (NLA_NESTED: nft_data_attributes) * @NFTA_BITWISE_XOR: xor value (NLA_NESTED: nft_data_attributes) +* @NFTA_BITWISE_OP: type of operation (NLA_U32: nft_bitwise_ops) +* @NFTA_BITWISE_DATA: argument for non-boolean operations +* (NLA_NESTED: nft_data_attributes) * * The bitwise expression supports boolean and shift operations. It implements * the boolean operations by performing the following operation:
@@ -511,6 +530,8 @@ enum nft_bitwise_attributes {NFTA_BITWISE_LEN ,NFTA_BITWISE_MASK ,NFTA_BITWISE_XOR , +NFTA_BITWISE_OP , +NFTA_BITWISE_DATA , __NFTA_BITWISE_MAX };#define NFTA_BITWISE_MAX (__NFTA_BITWISE_MAX - 1)
@@ -1521,6 +1542,7 @@ enum nft_object_attributes {* @NFTA_FLOWTABLE_HOOK: netfilter hook configuration(NLA_U32) * @NFTA_FLOWTABLE_USE: number of references to this flow table (NLA_U32) * @NFTA_FLOWTABLE_HANDLE: object handle (NLA_U64) +* @NFTA_FLOWTABLE_FLAGS: flags (NLA_U32) */ enum nft_flowtable_attributes {NFTA_FLOWTABLE_UNSPEC ,
@@ -1530,6 +1552,7 @@ enum nft_flowtable_attributes {NFTA_FLOWTABLE_USE ,NFTA_FLOWTABLE_HANDLE ,NFTA_FLOWTABLE_PAD , +NFTA_FLOWTABLE_FLAGS , __NFTA_FLOWTABLE_MAX };#define NFTA_FLOWTABLE_MAX (__NFTA_FLOWTABLE_MAX - 1)
Signature · 1 line
2.24.1
PATCH v3 8/9 netlink: add support for handling shift expressions.
In reply to Jeremy SowdenThe kernel supports bitwise shift operations, so add support to the netlink linearization and delinearization code. The number of bits (the righthand operand) is expected to be a 32-bit value in host endianness.
--- src/netlink_delinearize.c | 87 ++++++++++++++++++++++++++++++++------- src/netlink_linearize.c | 50 ++++++++++++++++++++-- 2 files changed, 120 insertions(+), 17 deletions(-)
src/netlink_delinearize.c +73 -14
@@ -356,22 +356,17 @@ static void netlink_parse_lookup(struct netlink_parse_ctx *ctx, ctx->stmt =expr_stmt_alloc (loc, expr); } -static void netlink_parse_bitwise (struct netlink_parse_ctx *ctx, -const struct location *loc, -const struct nftnl_expr *nle) +static struct expr *netlink_parse_bitwise_bool (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nftnl_expr *nle, +enum nft_registers sreg, +struct expr *left) + {struct nft_data_delinearize nld; -enum nft_registers sreg, dreg; -struct expr *expr, *left, *mask, *xor, *or; +struct expr *expr, *mask, *xor, *or;mpz_t m, x, o; - sreg =netlink_parse_register (nle,NFTNL_EXPR_BITWISE_SREG ); - left =netlink_get_register (ctx, loc, sreg); -if (left ==NULL ) -return netlink_error (ctx, loc, -"Bitwise expression has no left " -"hand side" ); - expr = left; nld.value =nftnl_expr_get (nle,NFTNL_EXPR_BITWISE_MASK , &nld.len);
@@ -423,6 +418,62 @@ static void netlink_parse_bitwise(struct netlink_parse_ctx *ctx,mpz_clear (x);mpz_clear (o); +return expr; +} + +static struct expr *netlink_parse_bitwise_shift (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nftnl_expr *nle, +enum ops op, +enum nft_registers sreg, +struct expr *left) +{ +struct nft_data_delinearize nld; +struct expr *expr, *right; + + nld.value =nftnl_expr_get (nle,NFTNL_EXPR_BITWISE_DATA , &nld.len); + right =netlink_alloc_value (loc, &nld); + + expr =binop_expr_alloc (loc, op, left, right); + expr->len = left->len; + +return expr; +} + +static void netlink_parse_bitwise (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nftnl_expr *nle) +{ +enum nft_registers sreg, dreg; +struct expr *expr, *left; +enum nft_bitwise_ops op; + + sreg =netlink_parse_register (nle,NFTNL_EXPR_BITWISE_SREG ); + left =netlink_get_register (ctx, loc, sreg); +if (left ==NULL ) +return netlink_error (ctx, loc, +"Bitwise expression has no left " +"hand side" ); + + op =nftnl_expr_get_u32 (nle,NFTNL_EXPR_BITWISE_OP ); + +switch (op) { +case NFT_BITWISE_BOOL : + expr =netlink_parse_bitwise_bool (ctx, loc, nle, sreg, + left); +break ; +case NFT_BITWISE_LSHIFT : + expr =netlink_parse_bitwise_shift (ctx, loc, nle,OP_LSHIFT , + sreg, left); +break ; +case NFT_BITWISE_RSHIFT : + expr =netlink_parse_bitwise_shift (ctx, loc, nle,OP_RSHIFT , + sreg, left); +break ; +default : +BUG ("invalid bitwise operation %u\n" , op); + } + dreg =netlink_parse_register (nle,NFTNL_EXPR_BITWISE_DREG );netlink_set_register (ctx, dreg, expr); }
@@ -2091,8 +2142,16 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)break ;case EXPR_BINOP :expr_postprocess (ctx, &expr->left); -expr_set_type (expr->right, expr->left->dtype, - expr->left->byteorder); +switch (expr->op) { +case OP_LSHIFT : +case OP_RSHIFT : +expr_set_type (expr->right, &integer_type, +BYTEORDER_HOST_ENDIAN ); +break ; +default : +expr_set_type (expr->right, expr->left->dtype, + expr->left->byteorder); + }expr_postprocess (ctx, &expr->right); expr_set_type(expr, expr->left->dtype,
src/netlink_linearize.c +47 -3
76 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH v3 9/9 tests: shell: add bit-shift tests.
In reply to Jeremy SowdenAdd a couple of tests for setting the CT mark to a bitwise expression derived from the packet mark and vice versa.
--- tests/shell/testcases/chains/0040mark_shift_0 | 11 +++++++++++ tests/shell/testcases/chains/0040mark_shift_1 | 11 +++++++++++ .../shell/testcases/chains/dumps/0040mark_shift_0.nft | 6 ++++++ .../shell/testcases/chains/dumps/0040mark_shift_1.nft | 6 ++++++ 4 files changed, 34 insertions(+) create mode 100755 tests/shell/testcases/chains/0040mark_shift_0 create mode 100755 tests/shell/testcases/chains/0040mark_shift_1 create mode 100644 tests/shell/testcases/chains/dumps/0040mark_shift_0.nft create mode 100644 tests/shell/testcases/chains/dumps/0040mark_shift_1.nft
tests/shell/testcases/chains/0040mark_shift_0 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table t + add chain t c { type filter hook output priority mangle; } + add rule t c oif lo ct mark set meta mark << 8 | 0x10 +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/chains/0040mark_shift_1 new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +RULESET=" + add table t + add chain t c { type filter hook input priority mangle; } + add rule t c iif lo ct mark & 0xff 0x10 meta mark set ct mark >> 8 +" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/chains/dumps/0040mark_shift_0.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip t { + chain c { + type filter hook output priority mangle; policy accept; + oif "lo" ct mark set meta mark << 8 | 0x00000010 + } +}
tests/shell/testcases/chains/dumps/0040mark_shift_1.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip t { + chain c { + type filter hook input priority mangle; policy accept; + iif "lo" ct mark & 0x000000ff == 0x00000010 meta mark set ct mark >> 8 + } +}
Signature · 1 line
2.24.1
Pablo Neira Ayuso
maintainerRe: [PATCH nft v3 9/9] tests: shell: add bit-shift tests.
Quoted 2 lines · Jeremy Sowden
Add a couple of tests for setting the CT mark to a bitwise expression derived from the packet mark and vice versa.
Probably tests/py for this instead?
It also checks for the netlink bytecode, which is good to catch for regressions in the future.
Thanks.
Re: [PATCH nft v3 9/9] tests: shell: add bit-shift tests.
Quoted 8 lines · Pablo Neira Ayuso
Quoted 2 lines · Jeremy Sowden
Add a couple of tests for setting the CT mark to a bitwise expression derived from the packet mark and vice versa.
Probably tests/py for this instead?
It also checks for the netlink bytecode, which is good to catch for regressions in the future.
Will do.
J.
Pablo Neira Ayuso
maintainerQuoted 2 lines · Jeremy Sowden
The kernel supports bitwise shift operations. This patch-set adds the support to nft. There are a few preliminary housekeeping patches.
Actually, this batch goes in the direction of adding the basic lshift/right support.
# nft --debug=netlink add rule x y tcp dport set tcp dport lshift 1 ip x y [ meta load l4proto => reg 1 ] [ cmp eq reg 1 0x00000006 ] [ payload load 2b @ transport header + 2 => reg 1 ] [ byteorder reg 1 = ntoh(reg 1, 2, 2) ] [ bitwise reg 1 = ( reg 1 << 0x00000001 ) ] [ payload write reg 1 => 2b @ transport header + 2 csum_type 1 csum_off 16 csum_flags 0x0 ]
I'm applying patches 1, 2, 3, 4, 7 and 8.
Regarding patch 5, it would be good to restore the parens when listing.
Patch 6, I guess it will break something else. Did you run tests/py to check this?
Patch 9, I'm skipping until 5 and 6 are sorted out.
Thanks.
Quoted 18 lines · Pablo Neira Ayuso
Quoted 3 lines · Jeremy Sowden
The kernel supports bitwise shift operations. This patch-set adds the support to nft. There are a few preliminary housekeeping patches.
Actually, this batch goes in the direction of adding the basic lshift/right support.
# nft --debug=netlink add rule x y tcp dport set tcp dport lshift 1 ip x y [ meta load l4proto => reg 1 ] [ cmp eq reg 1 0x00000006 ] [ payload load 2b @ transport header + 2 => reg 1 ] [ byteorder reg 1 = ntoh(reg 1, 2, 2) ] [ bitwise reg 1 = ( reg 1 << 0x00000001 ) ] [ payload write reg 1 => 2b @ transport header + 2 csum_type 1 csum_off 16 csum_flags 0x0 ]
Quoted 4 lines
I'm applying patches 1, 2, 3, 4, 7 and 8.
Regarding patch 5, it would be good to restore the parens when listing.
Will do.
Quoted 2 lines
Patch 6, I guess it will break something else. Did you run tests/py to check this?
I did and I got the same results before and after applying it. I'll take another look.
Quoted 1 line
Patch 9, I'm skipping until 5 and 6 are sorted out.
J.
Quoted 19 lines · Jeremy Sowden
Quoted 18 lines · Pablo Neira Ayuso
Quoted 3 lines · Jeremy Sowden
The kernel supports bitwise shift operations. This patch-set adds the support to nft. There are a few preliminary housekeeping patches.
Actually, this batch goes in the direction of adding the basic lshift/right support.
# nft --debug=netlink add rule x y tcp dport set tcp dport lshift 1 ip x y [ meta load l4proto => reg 1 ] [ cmp eq reg 1 0x00000006 ] [ payload load 2b @ transport header + 2 => reg 1 ] [ byteorder reg 1 = ntoh(reg 1, 2, 2) ] [ bitwise reg 1 = ( reg 1 << 0x00000001 ) ] [ payload write reg 1 => 2b @ transport header + 2 csum_type 1 csum_off 16 csum_flags 0x0 ]
Quoted 6 lines
Quoted 4 lines
I'm applying patches 1, 2, 3, 4, 7 and 8.
Regarding patch 5, it would be good to restore the parens when listing.
Will do.
This is already handled by the same code that does it for the other parenthesized expressions (src/expression.c, ll. 600ff.):
static void binop_arg_print(const struct expr *op, const struct expr *arg,
struct output_ctx *octx)
{
bool prec = false; if (arg->etype == EXPR_BINOP &&
expr_binop_precedence[op->op] != 0 &&
expr_binop_precedence[op->op] < expr_binop_precedence[arg->op])
prec = 1;
if (prec)
nft_print(octx, "(");
expr_print(arg, octx);
if (prec)
nft_print(octx, ")");}
Quoted 5 lines
Quoted 2 lines
Patch 6, I guess it will break something else. Did you run tests/py to check this?
I did and I got the same results before and after applying it. I'll take another look.
Evaluation of the shift expression inserts a byte-order conversion if necessary to enforce host endianness, so by changing it we just avoid the addition of the extra operation. I've rewritten the commit message.
Quoted 1 line
Quoted 1 line
Patch 9, I'm skipping until 5 and 6 are sorted out.
I've tweaked the shell test-cases to include a parenthesized expression, and added some matching Python ones.
I'll send a new version out soon.
J.