Skip to contentSkip to conversation

nftables: bitmask and prefix fixes

PATCH New Patrick McHardy 1 participant 5 messages Feb 17, 2014 netfilter

lore
Share & tools
lore
#0 lore raw

cover nftables: bitmask and prefix fixes

These patches fix a problem with bitmask types outside of a flag comparison noticed by Florian, as well as a couple of bugs in related areas:

- always use FLAGCMP op if RHS of a relational expression has basetype bitmask
- add parens in output of binops when necessary
- fix netlink prefix expression handling

Unless there are objections, I'll push them shortly.

Patrick McHardy (4):
evaluate: use flagcmp for single RHS bitmask expression
binop: take care of operator precedence when printing binop arguments
netlink_delinarize: convert *all* bitmask values into individual bit values
netlink: fix prefix expression handling

 include/expression.h      |   8 ++++
 src/evaluate.c            |   6 ++-
 src/expression.c          |  88 +++++++++++++++++++++++++++++++++++++--
 src/netlink.c             |  27 ------------
 src/netlink_delinearize.c | 104 ++++++++++++++++++++++++++--------------------
 src/netlink_linearize.c   |  11 ++++-
 6 files changed, 165 insertions(+), 79 deletions(-)
#1 lore raw

PATCH 1/4 evaluate: use flagcmp for single RHS bitmask expression

Always use flagcmp for RHS bitmask expressions, independant of whether only one or an entire list of bitmask expression is specified.

This makes sure that f.i. "tcp flags ack" will match any combinations of ACK instead of ACK and only ACK.

---
 src/evaluate.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/evaluate.c +5 -1
index 8e51a63..f10d0d9 100644
@@ -885,7 +885,11 @@ static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)
 			rel->op = OP_FLAGCMP;
 			break;
 		default:
-			rel->op = OP_EQ;
+			if (right->dtype->basetype != NULL &&
+			    right->dtype->basetype->type == TYPE_BITMASK)
+				rel->op = OP_FLAGCMP;
+			else
+				rel->op = OP_EQ;
 			break;
 		}
 	}
Signature · 1 line
1.8.5.3
#2 lore raw

PATCH 2/4 binop: take care of operator precedence when printing binop arguments

In reply to Patrick McHardy

When the argument of a binop is a binop itself, we may need to add parens if the precedence of the argument is lower then the binop.

Before:

tcp flags & syn | ack == syn | ack
tcp flags & syn | ack != syn | ack

After:

tcp flags & (syn | ack) == syn | ack
tcp flags & (syn | ack) != syn | ack
---
 include/expression.h |  2 ++
 src/expression.c     | 30 ++++++++++++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)
include/expression.h +2 -0
index 0633102..5128a5b 100644
@@ -80,7 +80,9 @@ enum ops {
 	OP_FLAGCMP,
 	/* Set lookup */
 	OP_LOOKUP,
+	__OP_MAX
 };
+#define OP_MAX		(__OP_MAX - 1)
 
 extern const char *expr_op_symbols[];
 
src/expression.c +28 -2
index c856622..6cc79b2 100644
@@ -404,16 +404,42 @@ struct expr *unary_expr_alloc(const struct location *loc,
 	return expr;
 }
 
+static uint8_t expr_binop_precedence[OP_MAX + 1] = {
+	[OP_LSHIFT]	= 1,
+	[OP_RSHIFT]	= 1,
+	[OP_AND]	= 2,
+	[OP_XOR]	= 3,
+	[OP_OR]		= 4,
+};
+
+static void binop_arg_print(const struct expr *op, const struct expr *arg)
+{
+	bool prec = false;
+
+	if (arg->ops->type == EXPR_BINOP &&
+	    expr_binop_precedence[op->op] != 0 &&
+	    expr_binop_precedence[op->op] < expr_binop_precedence[arg->op])
+		prec = 1;
+
+	if (prec)
+		printf("(");
+	expr_print(arg);
+	if (prec)
+		printf(")");
+}
+
 static void binop_expr_print(const struct expr *expr)
 {
-	expr_print(expr->left);
+	binop_arg_print(expr, expr->left);
+
 	if (expr_op_symbols[expr->op] &&
 	    (expr->op != OP_EQ ||
 	     expr->left->ops->type == EXPR_BINOP))
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
-	expr_print(expr->right);
+
+	binop_arg_print(expr, expr->right);
 }
 
 static void binop_expr_clone(struct expr *new, const struct expr *expr)
Signature · 1 line
1.8.5.3
#3 lore raw

PATCH 3/4 netlink_delinarize: convert *all* bitmask values into individual bit values

In reply to Patrick McHardy

We're currently only converting bitmask types as direct argument to a relational expression in the form of a flagcmp (expr & mask neq 0) back into a list of bit values. This means expressions like:

tcp flags & (syn | ack) == syn | ack

won't be shown symbolically. Convert *all* bitmask values back to a sequence of inclusive or expressions of the individual bits. In case of a flagcmp, this sequence is further converted to a list (tcp flags syn,ack).

---
 include/expression.h      |  6 +++++
 src/expression.c          | 58 +++++++++++++++++++++++++++++++++++++++++++++--
 src/netlink_delinearize.c | 54 +++++++++++++++++++++++++++++--------------
 3 files changed, 99 insertions(+), 19 deletions(-)
include/expression.h +6 -0
index 5128a5b..ac6a4f4 100644
@@ -316,6 +316,12 @@ extern struct expr *constant_expr_join(const struct expr *e1,
 				       const struct expr *e2);
 extern struct expr *constant_expr_splice(struct expr *expr, unsigned int len);
 
+extern struct expr *flag_expr_alloc(const struct location *loc,
+				    const struct datatype *dtype,
+				    enum byteorder byteorder,
+				    unsigned int len, unsigned long n);
+extern struct expr *bitmask_expr_to_binops(struct expr *expr);
+
 extern struct expr *prefix_expr_alloc(const struct location *loc,
 				      struct expr *expr,
 				      unsigned int prefix_len);
src/expression.c +56 -2

76 lines not on this page · raw message

src/netlink_delinearize.c +37 -17

71 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#4 lore raw

PATCH 4/4 netlink: fix prefix expression handling

In reply to Patrick McHardy

The prefix expression handling is full of bugs:

- netlink_gen_data() is used to construct the prefix mask from the full prefix expression. This is both conceptually wrong, the prefix expression is *not* data, and buggy, it only assumes network masks and thus only handles big endian types.

- Prefix expression reconstruction doesn't check whether the mask is a valid prefix and reconstructs crap otherwise. It doesn't reconstruct prefixes for anything but network addresses. On top of that its needlessly complicated, using the mpz values directly its a simple matter of finding the sequence of 1's that extend up to the full width.

- Unnecessary cloning of expressions where a simple refcount increase would suffice.

Rewrite that code properly.

---
 src/netlink.c             | 27 ------------------------
 src/netlink_delinearize.c | 52 +++++++++++++++++++++--------------------------
 src/netlink_linearize.c   | 11 ++++++++--
 3 files changed, 32 insertions(+), 58 deletions(-)
src/netlink.c +0 -27
index 6e797dc..07af1cb 100644
@@ -252,31 +252,6 @@ static void netlink_gen_verdict(const struct expr *expr,
 	}
 }
 
-static void netlink_gen_prefix(const struct expr *expr,
-			       struct nft_data_linearize *data)
-{
-	uint32_t idx;
-	int32_t i, cidr;
-	uint32_t mask;
-
-	assert(expr->ops->type == EXPR_PREFIX);
-
-	data->len = div_round_up(expr->prefix->len, BITS_PER_BYTE);
-	cidr = expr->prefix_len;
-
-	for (i = 0; (uint32_t)i / BITS_PER_BYTE < data->len; i += 32) {
-		if (cidr - i >= 32)
-			mask = 0xffffffff;
-		else if (cidr - i > 0)
-			mask = (1 << (cidr - i)) - 1;
-		else
-			mask = 0;
-
-		idx = i / 32;
-		data->value[idx] = mask;
-	}
-}
-
 void netlink_gen_data(const struct expr *expr, struct nft_data_linearize *data)
 {
 	switch (expr->ops->type) {
@@ -286,8 +261,6 @@ void netlink_gen_data(const struct expr *expr, struct nft_data_linearize *data)
 		return netlink_gen_concat_data(expr, data);
 	case EXPR_VERDICT:
 		return netlink_gen_verdict(expr, data);
-	case EXPR_PREFIX:
-		return netlink_gen_prefix(expr, data);
 	default:
 		BUG("invalid data expression type %s\n", expr->ops->name);
 	}
src/netlink_delinearize.c +23 -29

69 lines not on this page · raw message

src/netlink_linearize.c +9 -2
index e5fb536..9d59374 100644
@@ -193,9 +193,14 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,
 	netlink_gen_expr(ctx, expr->left, sreg);
 
 	if (expr->right->ops->type == EXPR_PREFIX) {
-		right = expr->right->prefix;
+		mpz_t mask;
+
+		mpz_init(mask);
+		mpz_prefixmask(mask, expr->right->len, expr->right->prefix_len);
+		netlink_gen_raw_data(mask, expr->right->byteorder,
+				     expr->right->len / BITS_PER_BYTE, &nld);
+		mpz_clear(mask);
 
-		netlink_gen_data(expr->right, &nld);
 		zero.len = nld.len;
 
 		nle = alloc_nft_expr("bitwise");
@@ -205,6 +210,8 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,
 		nft_rule_expr_set(nle, NFT_EXPR_BITWISE_MASK, &nld.value, nld.len);
 		nft_rule_expr_set(nle, NFT_EXPR_BITWISE_XOR, &zero.value, zero.len);
 		nft_rule_add_expr(ctx->nlr, nle);
+
+		right = expr->right->prefix;
 	} else {
 		right = expr->right;
 	}
Signature · 1 line
1.8.5.3