Skip to contentSkip to conversation

bitwise shift support

PATCH Superseded Jeremy Sowden 1 participant 8 messages Jan 10, 2020 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 2cb1e2afd45c..6b37b1237037 100644
@@ -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
#2 lore raw

PATCH 2/7 src: white-space fixes.

In reply to Jeremy Sowden

Remove 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
index 34e4473e4c9a..6af681f0f7a8 100644
@@ -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
index 154353b8161a..387e4b046c6b 100644
@@ -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
index 498326d0087a..d5e177d5e75c 100644
@@ -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
#3 lore raw

PATCH 3/7 netlink_delinearize: fix typo.

In reply to Jeremy Sowden

s/Of/If/ in comment describing function.

---
 src/netlink_delinearize.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/netlink_delinearize.c +1 -1
index 387e4b046c6b..8b9b5c808384 100644
@@ -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
#4 lore raw

PATCH 4/7 netlink_delinearize: remove commented out pr_debug statement.

In reply to Jeremy Sowden

The statement doesn't compile, so remove it.

---
 src/netlink_delinearize.c | 2 --
 1 file changed, 2 deletions(-)
src/netlink_delinearize.c +0 -2
index 8b9b5c808384..8f2a5dfacd3e 100644
@@ -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
#5 lore raw

PATCH 5/7 parser: add parenthesized statement expressions.

In reply to Jeremy Sowden

Primary 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
index 799f7a308b07..45cc013cfe28 100644
@@ -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
#6 lore raw

PATCH 6/7 netlink: add support for handling shift expressions.

In reply to Jeremy Sowden

The 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
index c556ccd3dbf7..12dbb2adbbdf 100644
@@ -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
index 8f2a5dfacd3e..a45ad924b216 100644
@@ -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
#7 lore raw

PATCH 7/7 tests: shell: add bit-shift tests.

In reply to Jeremy Sowden

Add 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
new file mode 100755
index 000000000000..b40ee2dd5278
@@ -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
new file mode 100755
index 000000000000..b609f5ef10ad
@@ -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
new file mode 100644
index 000000000000..4df4391111c5
@@ -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
new file mode 100644
index 000000000000..d4db9622387e
@@ -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