bitwise shift support
cover bitwise shift support
This patch-set adds support for bitwise shift operations to nft. There are a few preliminary patches containing miscellaneous fixes as well.
Jeremy Sowden (7):
Update gitignore.
src: white-space fixes.
netlink_delinearize: fix typo.
netlink_delinearize: remove commented out pr_debug statement.
parser: add parenthesized statement expressions.
netlink: add support for handling shift expressions.
tests: shell: add bit-shift tests.
.gitignore | 9 ++++ include/linux/netfilter/nf_tables.h | 4 ++ src/evaluate.c | 11 +++-- src/netlink_delinearize.c | 25 +++++++++-- src/netlink_linearize.c | 44 +++++++++++++++++-- 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, 126 insertions(+), 26 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 1/7 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 2/7 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
@@ -2343,14 +2343,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 3/7 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 4/7 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 5/7 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 6/7 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.
--- include/linux/netfilter/nf_tables.h | 4 +++ src/netlink_delinearize.c | 19 +++++++++++++ src/netlink_linearize.c | 42 ++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 3 deletions(-)
include/linux/netfilter/nf_tables.h +4 -0
@@ -490,6 +490,8 @@ 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_LSHIFT: left shift value (NLA_U32) +* @NFTA_BITWISE_RSHIFT: right shift value (NLA_U32) * * The bitwise expression performs the following operation: *
@@ -510,6 +512,8 @@ enum nft_bitwise_attributes {NFTA_BITWISE_LEN ,NFTA_BITWISE_MASK ,NFTA_BITWISE_XOR , +NFTA_BITWISE_LSHIFT , +NFTA_BITWISE_RSHIFT , __NFTA_BITWISE_MAX };#define NFTA_BITWISE_MAX (__NFTA_BITWISE_MAX - 1)
src/netlink_delinearize.c +19 -0
@@ -372,6 +372,24 @@ static void netlink_parse_bitwise(struct netlink_parse_ctx *ctx,"Bitwise expression has no left " "hand side"); + nld.value =nftnl_expr_get (nle,NFTNL_EXPR_BITWISE_LSHIFT , &nld.len); +if (nld.value !=NULL ) { +struct expr *right =netlink_alloc_value (loc, &nld); + + expr =binop_expr_alloc (loc,OP_LSHIFT , left, right); + expr->len = left->len; + goto dreg; + } + + nld.value =nftnl_expr_get (nle,NFTNL_EXPR_BITWISE_RSHIFT , &nld.len); +if (nld.value !=NULL ) { +struct expr *right =netlink_alloc_value (loc, &nld); + + expr =binop_expr_alloc (loc,OP_RSHIFT , left, right); + expr->len = left->len; + goto dreg; + } + expr = left; nld.value =nftnl_expr_get (nle,NFTNL_EXPR_BITWISE_MASK , &nld.len);
@@ -423,6 +441,7 @@ static void netlink_parse_bitwise(struct netlink_parse_ctx *ctx,mpz_clear (x);mpz_clear (o); +dreg: dreg =netlink_parse_register (nle,NFTNL_EXPR_BITWISE_DREG );netlink_set_register (ctx, dreg, expr); }
src/netlink_linearize.c +39 -3
62 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH 7/7 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 << 0x00000008 | 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 >> 0x00000008 + } +}
Signature · 1 line
2.24.1