Skip to contentSkip to conversation

payload mangling enhancements

PATCH New Pablo Neira Ayuso 1 participant 5 messages Feb 28, 2025 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover payload mangling enhancements

Hi,

This series improves payload statement support for mangling bitfields, eg.

 # nft add rule x y tcp flags set tcp flags & (fin | syn | rst | psh | ack | urg)
 inet test-inet input
   [ meta load l4proto => reg 1 ]
   [ cmp eq reg 1 0x00000006 ]
   [ payload load 2b @ transport header + 12 => reg 1 ]
   [ bitwise reg 1 = ( reg 1 & 0x00003fff ) ^ 0x00000000 ]
   [ payload write reg 1 => 2b @ transport header + 12 csum_type 1 csum_off 16 csum_flags 0x0 ]

This requires no kernel upgrade.

Related to: https://bugzilla.netfilter.org/show_bug.cgi?id=1698

I did not implement the shortcut to compact flags as described by the bugzilla ticket.

Pablo Neira Ayuso (4):
evaluate: payload statement evaluation for bitfields
evaluate: reject unsupported expressions in payload statement for bitfields
evaluate: support for bitfield payload statement with binary operation
netlink_delinearize: support for bitfield payload statement with
binary operation

 include/payload.h               |   2 +
 src/evaluate.c                  |  81 +++++--
 src/netlink_delinearize.c       |  15 +-
 src/payload.c                   | 168 +++++++++++++++
 tests/py/ip/ip.t                |  17 ++
 tests/py/ip/ip.t.json           | 336 +++++++++++++++++++++++++++++
 tests/py/ip/ip.t.payload        |  82 +++++++
 tests/py/ip/ip.t.payload.bridge | 365 ++++++++++++++++++++++++++++++++
 tests/py/ip/ip.t.payload.inet   | 280 ++++++++++++++++++++++++
 tests/py/ip/ip.t.payload.netdev |  88 ++++++++
 10 files changed, 1419 insertions(+), 15 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/4 evaluate: payload statement evaluation for bitfields

Instead of allocating a lshift expression and relying on the binary operation transfer propagate this to the mask value, lshift the mask value immediately.

---
 src/evaluate.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
src/evaluate.c +7 -14
index f79667bd41ea..c090aebe2cca 100644
@@ -3345,20 +3345,13 @@ static int stmt_evaluate_payload(struct eval_ctx *ctx, struct stmt *stmt)
 		}
 	}
 
-	if (shift_imm) {
-		struct expr *off, *lshift;
-
-		off = constant_expr_alloc(&payload->location,
-					  expr_basetype(payload),
-					  BYTEORDER_HOST_ENDIAN,
-					  sizeof(shift_imm), &shift_imm);
-
-		lshift = binop_expr_alloc(&payload->location, OP_LSHIFT,
-					  stmt->payload.val, off);
-		lshift->dtype     = payload->dtype;
-		lshift->byteorder = payload->byteorder;
-
-		stmt->payload.val = lshift;
+	switch (stmt->payload.val->etype) {
+	case EXPR_VALUE:
+		if (shift_imm)
+			mpz_lshift_ui(stmt->payload.val->value, shift_imm);
+		break;
+	default:
+		break;
 	}
 
 	masklen = payload_byte_size * BITS_PER_BYTE;
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/4 evaluate: reject unsupported expressions in payload statement for bitfields

In reply to Pablo Neira Ayuso

The payload statement evaluation pretends that it can handle any expression for bitfields, but the existing evaluation code only knows how to handle value expression.

---
 src/evaluate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/evaluate.c +2 -1
index c090aebe2cca..d7915ed19d59 100644
@@ -3351,7 +3351,8 @@ static int stmt_evaluate_payload(struct eval_ctx *ctx, struct stmt *stmt)
 			mpz_lshift_ui(stmt->payload.val->value, shift_imm);
 		break;
 	default:
-		break;
+		return expr_error(ctx->msgs, stmt->payload.val,
+				  "payload statement for this expression is not supported");
 	}
 
 	masklen = payload_byte_size * BITS_PER_BYTE;
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH 3/4 evaluate: support for bitfield payload statement with binary operation

In reply to Pablo Neira Ayuso

Update bitfield payload statement support to allow for bitwise and/or/xor updates. Adjust payload expression to fetch 16-bits for mangling while leaving unmodified bits intact.

 # nft --debug=netlink add rule x y ip dscp set ip dscp or 0x1
 ip x y
   [ payload load 2b @ network header + 0 => reg 1 ]
   [ bitwise reg 1 = ( reg 1 & 0x0000fbff ) ^ 0x00000400 ]
   [ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 csum_flags 0x0 ]

Skip expr_evaluate_bits() transformation since these are only useful for payload matching and set lookups.

Listing still shows a raw expression:

  # nft list ruleset
    ...
                    @nh,8,5 set 0x0

The follow up patch completes it:

("netlink_delinearize: support for bitfield payload statement with binary operation")

---
 src/evaluate.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 63 insertions(+), 2 deletions(-)
src/evaluate.c +63 -2
index d7915ed19d59..c9c56588cee4 100644
@@ -569,6 +569,13 @@ static int expr_evaluate_bits(struct eval_ctx *ctx, struct expr **exprp)
 	uint8_t shift;
 	mpz_t bitmask;
 
+	/* payload statement with relational expression as a value does not
+	 * require the transformations that are needed for payload matching,
+	 * skip this.
+	 */
+	if (ctx->stmt && ctx->stmt->ops->type == STMT_PAYLOAD)
+		return 0;
+
 	switch (expr->etype) {
 	case EXPR_PAYLOAD:
 		shift = expr_offset_shift(expr, expr->payload.offset,
@@ -3281,10 +3288,10 @@ static int stmt_evaluate_exthdr(struct eval_ctx *ctx, struct stmt *stmt)
 
 static int stmt_evaluate_payload(struct eval_ctx *ctx, struct stmt *stmt)
 {
-	struct expr *mask, *and, *xor, *payload_bytes;
-	unsigned int masklen, extra_len = 0;
+	struct expr *mask, *and, *xor, *expr, *payload_bytes;
 	unsigned int payload_byte_size, payload_byte_offset;
 	uint8_t shift_imm, data[NFT_REG_SIZE];
+	unsigned int masklen, extra_len = 0;
 	struct expr *payload;
 	mpz_t bitmask, ff;
 	bool need_csum;
@@ -3350,6 +3357,60 @@ static int stmt_evaluate_payload(struct eval_ctx *ctx, struct stmt *stmt)
 		if (shift_imm)
 			mpz_lshift_ui(stmt->payload.val->value, shift_imm);
 		break;
+	case EXPR_BINOP:
+		expr = stmt->payload.val;
+		while (expr->left->etype == EXPR_BINOP)
+			expr = expr->left;
+
+		if (expr->left->etype != EXPR_PAYLOAD)
+			break;
+
+		if (!payload_expr_cmp(payload, expr->left))
+			break;
+
+		/* Adjust payload to fetch 16-bits. */
+		expr->left->payload.offset = payload_byte_offset * BITS_PER_BYTE;
+		expr->left->len = payload_byte_size * BITS_PER_BYTE;
+		expr->left->payload.is_raw = 1;
+
+		switch (expr->right->etype) {
+		case EXPR_VALUE:
+			if (shift_imm)
+				mpz_lshift_ui(expr->right->value, shift_imm);
+
+			/* build bitmask to keep unmodified bits intact */
+			if (expr->op == OP_AND) {
+				masklen = payload_byte_size * BITS_PER_BYTE;
+				mpz_init_bitmask(ff, masklen);
+
+				mpz_init2(bitmask, masklen);
+				mpz_bitmask(bitmask, payload->len);
+				mpz_lshift_ui(bitmask, shift_imm);
+
+				mpz_xor(bitmask, ff, bitmask);
+				mpz_clear(ff);
+
+				mpz_ior(bitmask, expr->right->value, bitmask);
+				mpz_set(expr->right->value, bitmask);
+
+				mpz_clear(bitmask);
+			}
+			break;
+		default:
+			return expr_error(ctx->msgs, expr->right,
+					  "payload statement for this expression is not supported");
+		}
+
+		expr_free(stmt->payload.expr);
+		/* statement payload is the same in expr and value, update it. */
+		stmt->payload.expr = expr_clone(expr->left);
+		payload = stmt->payload.expr;
+		ctx->stmt_len = stmt->payload.expr->len;
+
+		if (expr_evaluate(ctx, &stmt->payload.val) < 0)
+			return -1;
+
+		return 0;
 	default:
 		return expr_error(ctx->msgs, stmt->payload.val,
 				  "payload statement for this expression is not supported");
Signature · 1 line
2.30.2
author maintainer
#4 lore raw

PATCH 4/4 netlink_delinearize: support for bitfield payload statement with binary operation

In reply to Pablo Neira Ayuso

Add a new function to deal with payload statement delinearization with binop expression.

Infer the payload offset from the mask, then walk the template list to determine if estimated offset falls within a matching header field. If so, then validate that this is not a raw expression but an actual bitfield matching. Finally, trim the payload expression length accordingly and adjust the payload offset.

instead of:

	@nh,8,5 set 0x0

it displays:

	ip dscp and 0x1

Update tests/py to cover for this enhancement.

---
 include/payload.h               |   2 +
 src/netlink_delinearize.c       |  15 +-
 src/payload.c                   | 168 +++++++++++++++
 tests/py/ip/ip.t                |  17 ++
 tests/py/ip/ip.t.json           | 336 +++++++++++++++++++++++++++++
 tests/py/ip/ip.t.payload        |  82 +++++++
 tests/py/ip/ip.t.payload.bridge | 365 ++++++++++++++++++++++++++++++++
 tests/py/ip/ip.t.payload.inet   | 280 ++++++++++++++++++++++++
 tests/py/ip/ip.t.payload.netdev |  88 ++++++++
 9 files changed, 1351 insertions(+), 2 deletions(-)
include/payload.h +2 -0
index 6685dad6f9f7..4c01465457a9 100644
@@ -64,6 +64,8 @@ bool payload_expr_trim(struct expr *expr, struct expr *mask,
 		       const struct proto_ctx *ctx, unsigned int *shift);
 bool payload_expr_trim_force(struct expr *expr, struct expr *mask,
 			     unsigned int *shift);
+bool stmt_payload_expr_trim(struct stmt *stmt, struct expr *expr,
+			    const struct proto_ctx *ctx);
 extern void payload_expr_expand(struct list_head *list, struct expr *expr,
 				const struct proto_ctx *ctx);
 extern void payload_expr_complete(struct expr *expr,
src/netlink_delinearize.c +13 -2
index 2ac835b4ffd3..c245137a5f12 100644
@@ -3183,7 +3183,8 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop)
  * decoding changed '(payload & mask) ^ bits_to_set' into
  * 'payload | bits_to_set', discarding the redundant "& 0xfff...".
  */
-static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx)
+static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx,
+					   const struct proto_ctx *pctx)
 {
 	struct expr *expr, *binop, *payload, *value, *mask;
 	struct stmt *stmt = ctx->stmt;
@@ -3264,6 +3265,9 @@ static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx)
 			unsigned int shift_unused;
 			mpz_t tmp;
 
+			if (stmt_payload_expr_trim(stmt, expr, pctx))
+				return;
+
 			mpz_init(tmp);
 			mpz_set(tmp, mask->value);
 
@@ -3293,9 +3297,16 @@ static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx)
 		}
 		case OP_OR:  /* IIb */
 			stmt_payload_binop_pp(ctx, expr);
+			if (stmt_payload_expr_trim(stmt, expr, pctx))
+				return;
 			if (!payload_is_known(expr->left))
 				return;
 			break;
+		case OP_XOR:
+			if (stmt_payload_expr_trim(stmt, expr, pctx))
+				return;
+
+			return;
 		default: /* No idea what to do */
 			return;
 		}
@@ -3317,7 +3328,7 @@ static void stmt_payload_postprocess(struct rule_pp_ctx *ctx)
 
 	payload_expr_complete(stmt->payload.expr, &dl->pctx);
 	if (!payload_is_known(stmt->payload.expr))
-		stmt_payload_binop_postprocess(ctx);
+		stmt_payload_binop_postprocess(ctx, &dl->pctx);
 
 	expr_postprocess(ctx, &stmt->payload.expr);
 
src/payload.c +168 -0

174 lines not on this page · raw message

tests/py/ip/ip.t +17 -0
index e6999c29478b..47262d9a4361 100644
@@ -135,3 +135,20 @@ ip saddr 1.2.3.4 ip daddr 3.4.5.6;ok
 ip saddr 1.2.3.4 counter ip daddr 3.4.5.6;ok
 
 ip dscp 1/6;ok;ip dscp & 0x3f == lephb
+
+ip ecn set ip ecn | ect0;ok
+ip ecn set ip ecn | ect1;ok
+ip ecn set ip ecn & ect0;ok
+ip ecn set ip ecn & ect1;ok
+tcp flags set tcp flags & (fin | syn | rst | psh | ack | urg);ok
+tcp flags set tcp flags | ecn | cwr;ok
+ip dscp set ip dscp | lephb;ok
+ip dscp set ip dscp & lephb;ok
+ip dscp set ip dscp & 0x1f;ok
+ip dscp set ip dscp & 0x4f;fail
+ip version set ip version | 1;ok
+ip version set ip version & 1;ok
+ip version set ip version | 0x1f;fail
+ip hdrlength set ip hdrlength | 1;ok
+ip hdrlength set ip hdrlength & 1;ok
+ip hdrlength set ip hdrlength | 0x1f;fail
tests/py/ip/ip.t.json +336 -0

339 lines not on this page · raw message

tests/py/ip/ip.t.payload +82 -0

85 lines not on this page · raw message

tests/py/ip/ip.t.payload.bridge +365 -0

368 lines not on this page · raw message

tests/py/ip/ip.t.payload.inet +280 -0

283 lines not on this page · raw message

tests/py/ip/ip.t.payload.netdev +88 -0

91 lines not on this page · raw message

Signature · 1 line
2.30.2