Skip to contentSkip to conversation

nft: exthdr fixes and improvements

PATCH In review Florian Westphal 2 participants 12 messages Mar 1, 2016 – Mar 2, 2016 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover nft: exthdr fixes and improvements

This adds following enhancements and fixes for the exthdr expression.

#1. Treat exthdr as if user asked for an ip6 protocol header field, i.e. add ipv6 dependency for bridge/netdev/inet family.

#2. Add scaling and masking to handle protocol headers that have non-byte divisible sizes.

Tested briefly with following dummy rules (nf_defrag_ipv6 module not loaded):

frag frag-off 0 counter packets 40 bytes 59840 frag frag-off 131 counter packets 0 bytes 0 frag frag-off 1448 counter packets 0 bytes 0 frag frag-off 181 counter packets 40 bytes 59840 frag frag-off > 0 counter packets 120 bytes 148160

Note that the offsets are *NOT* scaled, i.e. we match the raw values contained in the packet. To match offset of 1448 one thus needs to ask for 181.

This is same behaviour as ip hdrlength, where 5 matches a value of 20 bytes.

 include/expression.h             |    1
 include/exthdr.h                 |    2
 include/payload.h                |    2
 src/evaluate.c                   |   29 +++++++
 src/exthdr.c                     |   33 ++++++++
 src/netlink_delinearize.c        |   36 +++++----
 src/netlink_linearize.c          |   62 +++++++++++----
 src/payload.c                    |   75 ++++++++++++-------
 tests/py/ip6/dst.t.payload.inet  |   30 +++++++
 tests/py/ip6/frag.t              |   63 ++++++++++++++++
 tests/py/ip6/frag.t.payload.inet |  145 +++++++++++++++++++++++++++++++++++++
 tests/py/ip6/frag.t.payload.ip6  |  109 +++++++++++++++++++++++++++
 tests/py/ip6/hbh.t.payload.inet  |   30 +++++++
 tests/py/ip6/mh.t.payload.inet   |   64 ++++++++++++++++
 tests/py/ip6/rt.t.payload.inet   |   58 ++++++++++++++
 tests/py/any/frag.t                |   67 -----------------
 tests/py/any/frag.t.payload        |  109 ---------------------------
 17 files changed, 685 insertions(+), 230 deletions(-)

Note: frag-off match is broken at the moment, I passed a patch to netdev:

https://patchwork.ozlabs.org/patch/590568/

author maintainer
#1 lore raw

PATCH 1/10 evaluate: enforce ip6 proto with exthdr expression

Don't allow use of exthdr with e.g. ip family.
Move frag.t to ip6 directory and don't use it with ipv4 anymore.

This change causes major test failures for all exthdr users since they now fail with inet/bridge/netdev families.

Will be resolved in a later patch -- we need to add an ipv6 dependency for them.

---
 src/evaluate.c                  |  18 ++++++-
 tests/py/any/frag.t             |  67 ------------------------
 tests/py/any/frag.t.payload     | 109 ----------------------------------------
 tests/py/ip6/frag.t             |  63 +++++++++++++++++++++++
 tests/py/ip6/frag.t.payload.ip6 | 109 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 189 insertions(+), 177 deletions(-)
 delete mode 100644 tests/py/any/frag.t
 delete mode 100644 tests/py/any/frag.t.payload
 create mode 100644 tests/py/ip6/frag.t
 create mode 100644 tests/py/ip6/frag.t.payload.ip6
src/evaluate.c +17 -1
index a49cdd9..47a1f8c 100644
@@ -343,6 +343,21 @@ conflict_resolution_gen_dependency(struct eval_ctx *ctx, int protocol,
 	return 0;
 }
 
+/*
+ * Exthdr expression: check whether dependencies are fulfilled.
+ */
+static int expr_evaluate_exthdr(struct eval_ctx *ctx, struct expr **expr)
+{
+	const struct proto_desc *base;
+
+	base = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc;
+	if (base == &proto_ip6)
+		return expr_evaluate_primary(ctx, expr);
+
+	return expr_error(ctx->msgs, *expr,
+			  "exthdr can only be used with ipv6");
+}
+
 /* dependency supersede.
  *
  * 'inet' is a 'phony' l2 dependeny used by NFPROTO_INET to fulfill network
@@ -1320,8 +1335,9 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
 		return 0;
 	case EXPR_VALUE:
 		return expr_evaluate_value(ctx, expr);
-	case EXPR_VERDICT:
 	case EXPR_EXTHDR:
+		return expr_evaluate_exthdr(ctx, expr);
+	case EXPR_VERDICT:
 	case EXPR_META:
 		return expr_evaluate_primary(ctx, expr);
 	case EXPR_PAYLOAD:
tests/py/any/frag.t deleted +0 -67

67 lines not on this page · raw message

tests/py/any/frag.t.payload deleted +0 -109

109 lines not on this page · raw message

tests/py/ip6/frag.t new file +63 -0

63 lines not on this page · raw message

tests/py/ip6/frag.t.payload.ip6 new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.4.10
author maintainer
#2 lore raw

PATCH 2/10 netlink: split generic part of netlink_gen_payload_mask into helper

In reply to Florian Westphal

netlink_gen_payload_mask assumes expr is a payload expression, but most of this function would work fine with exthdr too.

So split the gernic part into a helper, followup patch will add netlink_gen_exthdr_mask.

---
 src/netlink_linearize.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
src/netlink_linearize.c +23 -12
index 86b49c6..7715a28 100644
@@ -103,12 +103,13 @@ static void netlink_gen_concat(struct netlink_linearize_ctx *ctx,
 	}
 }
 
-static unsigned int payload_shift_calc(const struct expr *expr)
+static unsigned int payload_shift_calc(const struct expr *expr,
+				       unsigned int offset)
 {
-	unsigned int offset, len;
+	unsigned int len;
 	int shift;
 
-	offset = expr->payload.offset % BITS_PER_BYTE;
+	offset %= BITS_PER_BYTE;
 	len = round_up(expr->len, BITS_PER_BYTE);
 	shift = len - (offset + expr->len);
 	assert(shift >= 0);
@@ -116,19 +117,16 @@ static unsigned int payload_shift_calc(const struct expr *expr)
 	return shift;
 }
 
-static void netlink_gen_payload_mask(struct netlink_linearize_ctx *ctx,
-				     const struct expr *expr,
-				     enum nft_registers dreg)
+static void netlink_gen_mask(struct netlink_linearize_ctx *ctx,
+			     const struct expr *expr,
+			     unsigned int shift,
+			     enum nft_registers dreg)
 {
 	struct nft_data_linearize nld, zero = {};
-	unsigned int shift, len, masklen;
+	unsigned int len, masklen;
 	struct nftnl_expr *nle;
 	mpz_t mask;
 
-	shift = payload_shift_calc(expr);
-	if (!shift && expr->payload.offset % BITS_PER_BYTE == 0)
-		return;
-
 	masklen = expr->len + shift;
 	assert(masklen <= NFT_REG_SIZE * BITS_PER_BYTE);
 	mpz_init2(mask, masklen);
@@ -151,6 +149,18 @@ static void netlink_gen_payload_mask(struct netlink_linearize_ctx *ctx,
 	nftnl_rule_add_expr(ctx->nlr, nle);
 }
 
+static void netlink_gen_payload_mask(struct netlink_linearize_ctx *ctx,
+				     const struct expr *expr,
+				     enum nft_registers dreg)
+{
+	unsigned int shift, offset;
+
+	offset = expr->payload.offset % BITS_PER_BYTE;
+	shift = payload_shift_calc(expr, offset);
+	if (shift || offset)
+		netlink_gen_mask(ctx, expr, shift, dreg);
+}
+
 static void netlink_gen_payload(struct netlink_linearize_ctx *ctx,
 				const struct expr *expr,
 				enum nft_registers dreg)
@@ -300,7 +310,8 @@ static void payload_shift_value(const struct expr *left, struct expr *right)
 	    left->ops->type != EXPR_PAYLOAD)
 		return;
 
-	mpz_lshift_ui(right->value, payload_shift_calc(left));
+	mpz_lshift_ui(right->value,
+			payload_shift_calc(left, left->payload.offset));
 }
 
 static struct expr *netlink_gen_prefix(struct netlink_linearize_ctx *ctx,
Signature · 1 line
2.4.10
author maintainer
#3 lore raw

PATCH 3/10 netlink: add and use netlink_gen_exthdr_mask

In reply to Florian Westphal

rule ip6 filter input frag frag-off 33

before patch:
[ exthdr load 1b @ 44 + 2 => reg 1 ]
[ cmp eq reg 1 0x00002100 ]

We truncated 13bit field to 1 byte.

after patch:
[ exthdr load 2b @ 44 + 2 => reg 1 ]
[ bitwise reg 1 = (reg=1 & 0x0000f8ff ) ^ 0x00000000 ] [ cmp eq reg 1 0x00000801 ]

- ask for 2 bytes
- mask out the 3 lower bits
- shift the value by 3 so equality test will pass for 33

This causes test failures, will be fixed up in a later patch (the test suite expects the old, broken input).

It also misses the reverse translation to remove the binop, find the right template and undo the shift of the value.

---
 src/netlink_linearize.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
src/netlink_linearize.c +24 -3
index 7715a28..7ff3b00 100644
@@ -181,6 +181,18 @@ static void netlink_gen_payload(struct netlink_linearize_ctx *ctx,
 	netlink_gen_payload_mask(ctx, expr, dreg);
 }
 
+static void netlink_gen_exthdr_mask(struct netlink_linearize_ctx *ctx,
+				    const struct expr *expr,
+				    enum nft_registers dreg)
+{
+	unsigned int shift, offset;
+
+	offset = expr->exthdr.tmpl->offset % BITS_PER_BYTE;
+	shift = payload_shift_calc(expr, offset);
+	if (shift || offset)
+		netlink_gen_mask(ctx, expr, shift, dreg);
+}
+
 static void netlink_gen_exthdr(struct netlink_linearize_ctx *ctx,
 			       const struct expr *expr,
 			       enum nft_registers dreg)
@@ -194,8 +206,10 @@ static void netlink_gen_exthdr(struct netlink_linearize_ctx *ctx,
 	nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_OFFSET,
 			   expr->exthdr.tmpl->offset / BITS_PER_BYTE);
 	nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_LEN,
-			   expr->len / BITS_PER_BYTE);
+			   div_round_up(expr->len, BITS_PER_BYTE));
 	nftnl_rule_add_expr(ctx->nlr, nle);
+
+	netlink_gen_exthdr_mask(ctx, expr, dreg);
 }
 
 static void netlink_gen_meta(struct netlink_linearize_ctx *ctx,
@@ -306,10 +320,17 @@ static void netlink_gen_range(struct netlink_linearize_ctx *ctx,
 
 static void payload_shift_value(const struct expr *left, struct expr *right)
 {
-	if (right->ops->type != EXPR_VALUE ||
-	    left->ops->type != EXPR_PAYLOAD)
+	if (right->ops->type != EXPR_VALUE)
 		return;
 
+	switch (left->ops->type) {
+	case EXPR_PAYLOAD:
+	case EXPR_EXTHDR:
+		break;
+	default:
+		return;
+	}
+
 	mpz_lshift_ui(right->value,
 			payload_shift_calc(left, left->payload.offset));
 }
Signature · 1 line
2.4.10
author maintainer
#4 lore raw

PATCH 4/10 payload: move payload_gen_dependency generic part to helper

In reply to Florian Westphal

We should treat exthdr just as if user asked for e.g. ip6 saddr and inject the needed dependency statement.

---
 src/payload.c | 66 ++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 38 insertions(+), 28 deletions(-)
src/payload.c +38 -28
index 6a977e8..8f67b6e 100644
@@ -162,6 +162,43 @@ struct stmt *payload_stmt_alloc(const struct location *loc,
 	return stmt;
 }
 
+static int payload_add_dependency(struct eval_ctx *ctx,
+				  const struct proto_desc *desc,
+				  const struct proto_desc *upper,
+				  const struct expr *expr,
+				  struct stmt **res)
+{
+	const struct proto_hdr_template *tmpl;
+	struct expr *dep, *left, *right;
+	struct stmt *stmt;
+	int protocol = proto_find_num(desc, upper);
+
+	if (protocol < 0)
+		return expr_error(ctx->msgs, expr,
+				  "conflicting protocols specified: %s vs. %s",
+				  desc->name, upper->name);
+
+	tmpl = &desc->templates[desc->protocol_key];
+	if (tmpl->meta_key)
+		left = meta_expr_alloc(&expr->location, tmpl->meta_key);
+	else
+		left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
+
+	right = constant_expr_alloc(&expr->location, tmpl->dtype,
+				    tmpl->dtype->byteorder, tmpl->len,
+				    constant_data_ptr(protocol, tmpl->len));
+
+	dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
+	stmt = expr_stmt_alloc(&dep->location, dep);
+	if (stmt_evaluate(ctx, stmt) < 0) {
+		return expr_error(ctx->msgs, expr,
+					  "dependency statement is invalid");
+	}
+	left->ops->pctx_update(&ctx->pctx, dep);
+	*res = stmt;
+	return 0;
+}
+
 /**
  * payload_gen_dependency - generate match expression on payload dependency
  *
@@ -190,10 +227,7 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
 {
 	const struct hook_proto_desc *h = &hook_proto_desc[ctx->pctx.family];
 	const struct proto_desc *desc;
-	const struct proto_hdr_template *tmpl;
-	struct expr *dep, *left, *right;
 	struct stmt *stmt;
-	int protocol;
 	uint16_t type;
 
 	if (expr->payload.base < h->base) {
@@ -265,31 +299,7 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
 				  "no %s protocol specified",
 				  proto_base_names[expr->payload.base - 1]);
 
-	protocol = proto_find_num(desc, expr->payload.desc);
-	if (protocol < 0)
-		return expr_error(ctx->msgs, expr,
-				  "conflicting protocols specified: %s vs. %s",
-				  desc->name, expr->payload.desc->name);
-
-	tmpl = &desc->templates[desc->protocol_key];
-	if (tmpl->meta_key)
-		left = meta_expr_alloc(&expr->location, tmpl->meta_key);
-	else
-		left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
-
-	right = constant_expr_alloc(&expr->location, tmpl->dtype,
-				    tmpl->dtype->byteorder, tmpl->len,
-				    constant_data_ptr(protocol, tmpl->len));
-
-	dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
-	stmt = expr_stmt_alloc(&dep->location, dep);
-	if (stmt_evaluate(ctx, stmt) < 0) {
-		return expr_error(ctx->msgs, expr,
-					  "dependency statement is invalid");
-	}
-	left->ops->pctx_update(&ctx->pctx, dep);
-	*res = stmt;
-	return 0;
+	return payload_add_dependency(ctx, desc, expr->payload.desc, expr, res);
 }
 
 /**
Signature · 1 line
2.4.10
author maintainer
#5 lore raw

PATCH 5/10 exthdr: generate dependencies for inet/bridge/netdev family

In reply to Florian Westphal

Should treat this as if user would have asked to match ipv6 header field.

---
 include/payload.h |  2 ++
 src/evaluate.c    | 17 ++++++++++++++---
 src/payload.c     | 15 +++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
include/payload.h +2 -0
index 9192d6e..a19e690 100644
@@ -14,6 +14,8 @@ struct eval_ctx;
 struct stmt;
 extern int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
 				  struct stmt **res);
+extern int exthdr_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
+				  struct stmt **res);
 
 extern bool payload_is_adjacent(const struct expr *e1, const struct expr *e2);
 extern struct expr *payload_expr_join(const struct expr *e1,
src/evaluate.c +14 -3
index 47a1f8c..28e17cb 100644
@@ -344,18 +344,29 @@ conflict_resolution_gen_dependency(struct eval_ctx *ctx, int protocol,
 }
 
 /*
- * Exthdr expression: check whether dependencies are fulfilled.
+ * Exthdr expression: check whether dependencies are fulfilled, otherwise
+ * generate the necessary relational expression and prepend it to the current
+ * statement.
  */
 static int expr_evaluate_exthdr(struct eval_ctx *ctx, struct expr **expr)
 {
 	const struct proto_desc *base;
+	struct stmt *nstmt;
 
 	base = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc;
 	if (base == &proto_ip6)
 		return expr_evaluate_primary(ctx, expr);
 
-	return expr_error(ctx->msgs, *expr,
-			  "exthdr can only be used with ipv6");
+	if (base)
+		return expr_error(ctx->msgs, *expr,
+				  "cannot use exthdr with %s", base->name);
+
+	if (exthdr_gen_dependency(ctx, *expr, &nstmt) < 0)
+		return -1;
+
+	list_add(&nstmt->list, &ctx->rule->stmts);
+
+	return expr_evaluate_primary(ctx, expr);
 }
 
 /* dependency supersede.
src/payload.c +15 -0
index 8f67b6e..7e38061 100644
@@ -302,6 +302,21 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
 	return payload_add_dependency(ctx, desc, expr->payload.desc, expr, res);
 }
 
+int exthdr_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
+			  struct stmt **res)
+{
+	const struct proto_desc *desc;
+
+	desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
+	if (desc == NULL)
+		return expr_error(ctx->msgs, expr,
+				  "Cannot generate dependency: "
+				  "no %s protocol specified",
+				  proto_base_names[PROTO_BASE_LL_HDR]);
+
+	return payload_add_dependency(ctx, desc, &proto_ip6, expr, res);
+}
+
 /**
  * payload_expr_complete - fill in type information of a raw payload expr
  *
Signature · 1 line
2.4.10
author maintainer
#6 lore raw

PATCH 6/10 tests: add/fix inet+exthdr tests

In reply to Florian Westphal

exhdr needs to be treated as if we'd test an ipv6 header field, i.e. inet, bridge, netdev need to add a dependency on ipv6 protocol.

---
 tests/py/ip6/dst.t.payload.inet  |  30 ++++++++
 tests/py/ip6/frag.t.payload.inet | 145 +++++++++++++++++++++++++++++++++++++++
 tests/py/ip6/hbh.t.payload.inet  |  30 ++++++++
 tests/py/ip6/mh.t.payload.inet   |  64 +++++++++++++++++
 tests/py/ip6/rt.t.payload.inet   |  58 ++++++++++++++++
 5 files changed, 327 insertions(+)
 create mode 100644 tests/py/ip6/frag.t.payload.inet
tests/py/ip6/dst.t.payload.inet +30 -0
index 7a219f4..348506d 100644
@@ -1,21 +1,29 @@
 # dst nexthdr 22
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ cmp eq reg 1 0x00000016 ]
 
 # dst nexthdr != 233
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ cmp neq reg 1 0x000000e9 ]
 
 # dst nexthdr 33-45
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ cmp gte reg 1 0x00000021 ]
   [ cmp lte reg 1 0x0000002d ]
 
 # dst nexthdr != 33-45
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ cmp lt reg 1 0x00000021 ]
   [ cmp gt reg 1 0x0000002d ]
@@ -25,6 +33,8 @@ set%d test-inet 3
 set%d test-inet 0
 	element 00000021  : 0 [end]	element 00000037  : 0 [end]	element 00000043  : 0 [end]	element 00000058  : 0 [end]
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
@@ -33,6 +43,8 @@ set%d test-inet 7
 set%d test-inet 0
 	element 00000000  : 1 [end]	element 00000021  : 0 [end]	element 00000038  : 1 [end]
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
@@ -41,37 +53,51 @@ set%d test-inet 3
 set%d test-inet 0
 	element 00000088  : 0 [end]	element 0000006c  : 0 [end]	element 00000011  : 0 [end]	element 00000033  : 0 [end]	element 00000084  : 0 [end]	element 00000032  : 0 [end]	element 00000021  : 0 [end]	element 00000006  : 0 [end]	element 0000003a  : 0 [end]
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
 # dst nexthdr icmp
 inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ cmp eq reg 1 0x00000001 ]
 
 # dst nexthdr != icmp
 ip6 test-ip6 input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 0 => reg 1 ]
   [ cmp neq reg 1 0x00000001 ]
 
 # dst hdrlength 22
 ip6 test-ip6 input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 1 => reg 1 ]
   [ cmp eq reg 1 0x00000016 ]
 
 # dst hdrlength != 233
 ip6 test-ip6 input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 1 => reg 1 ]
   [ cmp neq reg 1 0x000000e9 ]
 
 # dst hdrlength 33-45
 ip6 test-ip6 input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 1 => reg 1 ]
   [ cmp gte reg 1 0x00000021 ]
   [ cmp lte reg 1 0x0000002d ]
 
 # dst hdrlength != 33-45
 ip6 test-ip6 input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 1 => reg 1 ]
   [ cmp lt reg 1 0x00000021 ]
   [ cmp gt reg 1 0x0000002d ]
@@ -81,6 +107,8 @@ set%d test-ip6 3
 set%d test-ip6 0
 	element 00000021  : 0 [end]	element 00000037  : 0 [end]	element 00000043  : 0 [end]	element 00000058  : 0 [end]
 ip6 test-ip6 input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 1 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
@@ -89,6 +117,8 @@ set%d test-ip6 7
 set%d test-ip6 0
 	element 00000000  : 1 [end]	element 00000021  : 0 [end]	element 00000038  : 1 [end]
 ip6 test-ip6 input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 60 + 1 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
tests/py/ip6/frag.t.payload.inet new file +145 -0

145 lines not on this page · raw message

tests/py/ip6/hbh.t.payload.inet +30 -0
index 2b4c9c7..e9a583f 100644
@@ -1,21 +1,29 @@
 # hbh hdrlength 22
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 1 => reg 1 ]
   [ cmp eq reg 1 0x00000016 ]
 
 # hbh hdrlength != 233
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 1 => reg 1 ]
   [ cmp neq reg 1 0x000000e9 ]
 
 # hbh hdrlength 33-45
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 1 => reg 1 ]
   [ cmp gte reg 1 0x00000021 ]
   [ cmp lte reg 1 0x0000002d ]
 
 # hbh hdrlength != 33-45
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 1 => reg 1 ]
   [ cmp lt reg 1 0x00000021 ]
   [ cmp gt reg 1 0x0000002d ]
@@ -25,6 +33,8 @@ set%d test-inet 3
 set%d test-inet 0
 	element 00000021  : 0 [end]	element 00000037  : 0 [end]	element 00000043  : 0 [end]	element 00000058  : 0 [end]
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 1 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
@@ -33,6 +43,8 @@ set%d test-inet 7
 set%d test-inet 0
 	element 00000000  : 1 [end]	element 00000021  : 0 [end]	element 00000038  : 1 [end]
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 1 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
@@ -41,27 +53,37 @@ set%d test-inet 3
 set%d test-inet 0
 	element 00000032  : 0 [end]	element 00000033  : 0 [end]	element 0000006c  : 0 [end]	element 00000011  : 0 [end]	element 00000088  : 0 [end]	element 00000006  : 0 [end]	element 00000021  : 0 [end]	element 00000084  : 0 [end]	element 0000003a  : 0 [end]
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
 # hbh nexthdr 22
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ cmp eq reg 1 0x00000016 ]
 
 # hbh nexthdr != 233
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ cmp neq reg 1 0x000000e9 ]
 
 # hbh nexthdr 33-45
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ cmp gte reg 1 0x00000021 ]
   [ cmp lte reg 1 0x0000002d ]
 
 # hbh nexthdr != 33-45
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ cmp lt reg 1 0x00000021 ]
   [ cmp gt reg 1 0x0000002d ]
@@ -71,6 +93,8 @@ set%d test-inet 3
 set%d test-inet 0
 	element 00000021  : 0 [end]	element 00000037  : 0 [end]	element 00000043  : 0 [end]	element 00000058  : 0 [end]
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
@@ -79,16 +103,22 @@ set%d test-inet 7
 set%d test-inet 0
 	element 00000000  : 1 [end]	element 00000021  : 0 [end]	element 00000038  : 1 [end]
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
 # hbh nexthdr ip
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ cmp eq reg 1 0x00000000 ]
 
 # hbh nexthdr != ip
 inet test-inet filter-input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
   [ exthdr load 1b @ 0 + 0 => reg 1 ]
   [ cmp neq reg 1 0x00000000 ]
 
tests/py/ip6/mh.t.payload.inet +64 -0

238 lines not on this page · raw message

tests/py/ip6/rt.t.payload.inet +58 -0

216 lines not on this page · raw message

Signature · 1 line
2.4.10
author maintainer
#7 lore raw

PATCH 7/10 exthdr: remove implicit dependencies

In reply to Florian Westphal

exthdr expression requires a dependency on ipv6; we can thus remove an ipv6 protocol test if its present.

---
 src/netlink_delinearize.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
src/netlink_delinearize.c +10 -3
index ae6abb0..eec7d0f 100644
@@ -982,10 +982,10 @@ struct rule_pp_ctx {
 /*
  * Kill a redundant payload dependecy that is implied by a higher layer payload expression.
  */
-static void payload_dependency_kill(struct rule_pp_ctx *ctx, struct expr *expr)
+static void __payload_dependency_kill(struct rule_pp_ctx *ctx, enum proto_bases base)
 {
 	if (ctx->pbase != PROTO_BASE_INVALID &&
-	    ctx->pbase == expr->payload.base &&
+	    ctx->pbase == base &&
 	    ctx->pdep != NULL) {
 		list_del(&ctx->pdep->list);
 		stmt_free(ctx->pdep);
@@ -996,6 +996,11 @@ static void payload_dependency_kill(struct rule_pp_ctx *ctx, struct expr *expr)
 	}
 }
 
+static void payload_dependency_kill(struct rule_pp_ctx *ctx, const struct expr *expr)
+{
+	__payload_dependency_kill(ctx, expr->payload.base);
+}
+
 static void payload_dependency_store(struct rule_pp_ctx *ctx,
 				     struct stmt *stmt,
 				     enum proto_bases base)
@@ -1505,8 +1510,10 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 	case EXPR_SET_ELEM:
 		expr_postprocess(ctx, &expr->key);
 		break;
-	case EXPR_SET_REF:
 	case EXPR_EXTHDR:
+		__payload_dependency_kill(ctx, PROTO_BASE_NETWORK_HDR);
+		break;
+	case EXPR_SET_REF:
 	case EXPR_META:
 	case EXPR_VERDICT:
 		break;
Signature · 1 line
2.4.10
author maintainer
#8 lore raw

PATCH 8/10 exthdr: store offset for later use

In reply to Florian Westphal

Its possible that we cannot find the template without also considering an implicit mask. For this we need to store the offset.

---
 include/expression.h | 1 +
 src/exthdr.c         | 2 ++
 2 files changed, 3 insertions(+)
include/expression.h +1 -0
index eacbb2d..72b6f69 100644
@@ -264,6 +264,7 @@ struct expr {
 			/* EXPR_EXTHDR */
 			const struct exthdr_desc	*desc;
 			const struct proto_hdr_template	*tmpl;
+			unsigned int			offset;
 		} exthdr;
 		struct {
 			/* EXPR_META */
src/exthdr.c +2 -0
index 9ed0b6a..512de0a 100644
@@ -37,6 +37,7 @@ static void exthdr_expr_clone(struct expr *new, const struct expr *expr)
 {
 	new->exthdr.desc = expr->exthdr.desc;
 	new->exthdr.tmpl = expr->exthdr.tmpl;
+	new->exthdr.offset = expr->exthdr.offset;
 }
 
 static const struct expr_ops exthdr_expr_ops = {
@@ -86,6 +87,7 @@ void exthdr_init_raw(struct expr *expr, uint8_t type,
 	assert(expr->ops->type == EXPR_EXTHDR);
 
 	expr->len = len;
+	expr->exthdr.offset = offset;
 	expr->exthdr.desc = exthdr_protocols[type];
 	assert(expr->exthdr.desc != NULL);
 
Signature · 1 line
2.4.10
author maintainer
#9 lore raw

PATCH 9/10 netlink_delinearize: prepare binop_postprocess for exthdr demux

In reply to Florian Westphal

binop_postprocess takes care of removing masks if we're dealing with payload expressions that have non-byte divisible sizes or offsets.

Same can happen when matching some extension header fields, i.e. this also needs to handle exthdr expression, not just payload.

So rename payload to left and move test for left type to binop_postprocess.

---
 src/netlink_delinearize.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
src/netlink_delinearize.c +7 -8
index eec7d0f..30c5f62 100644
@@ -1213,12 +1213,12 @@ static struct expr *binop_tree_to_list(struct expr *list, struct expr *expr)
 static void binop_postprocess(struct rule_pp_ctx *ctx, struct expr *expr)
 {
 	struct expr *binop = expr->left, *value = expr->right;
-
-	struct expr *payload = binop->left;
+	struct expr *left = binop->left;
 	struct expr *mask = binop->right;
 	unsigned int shift;
 
-	if (payload_expr_trim(payload, mask, &ctx->pctx, &shift)) {
+	if ((left->ops->type == EXPR_PAYLOAD &&
+	    payload_expr_trim(left, mask, &ctx->pctx, &shift))) {
 		/* mask is implicit, binop needs to be removed.
 		 *
 		 * Fix all values of the expression according to the mask
@@ -1231,15 +1231,15 @@ static void binop_postprocess(struct rule_pp_ctx *ctx, struct expr *expr)
 		if (value->ops->type == EXPR_VALUE) {
 			assert(value->len >= expr->left->right->len);
 			mpz_rshift_ui(value->value, shift);
-			value->len = payload->len;
+			value->len = left->len;
 		}
 
 		assert(expr->left->ops->type == EXPR_BINOP);
-		assert(binop->left == payload);
-		expr->left = expr_get(payload);
+		assert(binop->left == left);
+		expr->left = expr_get(left);
 		expr_free(binop);
 
-		payload_match_postprocess(ctx, expr, payload);
+		payload_match_postprocess(ctx, expr, left);
 	}
 }
 
@@ -1285,7 +1285,6 @@ static void relational_binop_postprocess(struct rule_pp_ctx *ctx, struct expr *e
 		expr_free(value);
 		expr_free(binop);
 	} else if (binop->op == OP_AND &&
-		   binop->left->ops->type == EXPR_PAYLOAD &&
 		   binop->right->ops->type == EXPR_VALUE) {
 		/*
 		 * This *might* be a payload match testing header fields that
Signature · 1 line
2.4.10
author maintainer
#10 lore raw

PATCH 10/10 netlink_delinearize: handle extension header templates with odd sizes

In reply to Florian Westphal

This enables nft to display
frag frag-off 33

... by considering a mask during binop postprocess in case the initial template lookup done when the exthdr expression was created did not yield a match.

In the above example, kernel netlink data specifies 16bits, but the frag field is only 13bits wide.

We use the implicit binop mask to re-do the template lookup with corrected offset and size information.

---
 include/exthdr.h          |  2 ++
 src/exthdr.c              | 31 +++++++++++++++++++++++++++++++
 src/netlink_delinearize.c | 12 ++++++++----
 3 files changed, 41 insertions(+), 4 deletions(-)
include/exthdr.h +2 -0
index 87c4285..d17841b 100644
@@ -23,6 +23,8 @@ extern struct expr *exthdr_expr_alloc(const struct location *loc,
 extern void exthdr_init_raw(struct expr *expr, uint8_t type,
 			    unsigned int offset, unsigned int len);
 
+extern bool exthdr_find_template(struct expr *expr, const struct expr *mask,
+				 unsigned int *shift);
 
 enum hbh_hdr_fields {
 	HBHHDR_INVALID,
src/exthdr.c +31 -0
index 512de0a..f392cff 100644
@@ -102,6 +102,37 @@ void exthdr_init_raw(struct expr *expr, uint8_t type,
 	}
 }
 
+static unsigned int mask_length(const struct expr *mask)
+{
+	unsigned long off = mpz_scan1(mask->value, 0);
+
+	return mpz_scan0(mask->value, off + 1);
+}
+
+bool exthdr_find_template(struct expr *expr, const struct expr *mask, unsigned int *shift)
+{
+	unsigned int off, mask_offset, mask_len;
+
+	if (expr->exthdr.tmpl != &exthdr_unknown_template)
+		return false;
+
+	mask_offset = mpz_scan1(mask->value, 0);
+	mask_len = mask_length(mask);
+
+	off = expr->exthdr.offset;
+	off += round_up(mask->len, BITS_PER_BYTE) - mask_len;
+
+	exthdr_init_raw(expr, expr->exthdr.desc->type,
+			off, mask_len - mask_offset);
+
+	/* still failed to find a template... Bug. */
+	if (expr->exthdr.tmpl == &exthdr_unknown_template)
+		return false;
+
+	*shift = mask_offset;
+	return true;
+}
+
 #define HDR_TEMPLATE(__name, __dtype, __type, __member)			\
 	PROTO_HDR_TEMPLATE(__name, __dtype,				\
 			   BYTEORDER_BIG_ENDIAN,			\
src/netlink_delinearize.c +8 -4
index 30c5f62..fae6e33 100644
@@ -1218,7 +1218,9 @@ static void binop_postprocess(struct rule_pp_ctx *ctx, struct expr *expr)
 	unsigned int shift;
 
 	if ((left->ops->type == EXPR_PAYLOAD &&
-	    payload_expr_trim(left, mask, &ctx->pctx, &shift))) {
+	    payload_expr_trim(left, mask, &ctx->pctx, &shift)) ||
+	    (left->ops->type == EXPR_EXTHDR &&
+	     exthdr_find_template(left, mask, &shift))) {
 		/* mask is implicit, binop needs to be removed.
 		 *
 		 * Fix all values of the expression according to the mask
@@ -1226,7 +1228,7 @@ static void binop_postprocess(struct rule_pp_ctx *ctx, struct expr *expr)
 		 * sizes and offsets we're interested in.
 		 *
 		 * Finally, convert the expression to 1) by replacing
-		 * the binop with the binop payload expr.
+		 * the binop with the binop payload/exthdr expression.
 		 */
 		if (value->ops->type == EXPR_VALUE) {
 			assert(value->len >= expr->left->right->len);
@@ -1238,8 +1240,10 @@ static void binop_postprocess(struct rule_pp_ctx *ctx, struct expr *expr)
 		assert(binop->left == left);
 		expr->left = expr_get(left);
 		expr_free(binop);
-
-		payload_match_postprocess(ctx, expr, left);
+		if (left->ops->type == EXPR_PAYLOAD)
+			payload_match_postprocess(ctx, expr, left);
+		else if (left->ops->type == EXPR_EXTHDR)
+			expr_set_type(expr->right, left->dtype, left->byteorder);
 	}
 }
 
Signature · 1 line
2.4.10
#11 lore raw
In reply to Florian Westphal

Hi Florian,

Quoted 17 lines · Florian Westphal

This adds following enhancements and fixes for the exthdr expression.

#1. Treat exthdr as if user asked for an ip6 protocol header field, i.e. add ipv6 dependency for bridge/netdev/inet family.

#2. Add scaling and masking to handle protocol headers that have non-byte divisible sizes.

Tested briefly with following dummy rules (nf_defrag_ipv6 module not loaded):

frag frag-off 0 counter packets 40 bytes 59840 frag frag-off 131 counter packets 0 bytes 0 frag frag-off 1448 counter packets 0 bytes 0 frag frag-off 181 counter packets 40 bytes 59840 frag frag-off > 0 counter packets 120 bytes 148160

Quoted 5 lines

Note that the offsets are *NOT* scaled, i.e. we match the raw values contained in the packet. To match offset of 1448 one thus needs to ask for 181.

This is same behaviour as ip hdrlength, where 5 matches a value of 20 bytes.

This looks good to me.

I still think we should move part of this to the evaluation phase so we catch other corner cases, but we can revisit that later on. Willing to have a look at this at some point.

Thanks.