Skip to contentSkip to conversation

tests: add icmp/6 test where dependency should be left alone

PATCH In review Florian Westphal 3 participants 4 messages Feb 1, 2021 – Feb 2, 2021 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/2 tests: add icmp/6 test where dependency should be left alone

These tests fail: nft should leave the type as-is.

---
 tests/py/ip/icmp.t                |  2 ++
 tests/py/ip/icmp.t.json           | 28 ++++++++++++++++++++++++++++
 tests/py/ip/icmp.t.payload.ip     |  6 ++++++
 tests/py/ip6/icmpv6.t             |  2 ++
 tests/py/ip6/icmpv6.t.json        | 28 ++++++++++++++++++++++++++++
 tests/py/ip6/icmpv6.t.payload.ip6 |  7 +++++++
 6 files changed, 73 insertions(+)
tests/py/ip/icmp.t +2 -0
index c22b55eb1e3f..11f3662e2b02 100644
@@ -86,3 +86,5 @@ icmp gateway != { 33-55};ok
 icmp gateway != 34;ok
 icmp gateway != { 333, 334};ok
 
+icmp code 1 icmp type 2;ok;icmp type 2 icmp code host-unreachable
+icmp code != 1 icmp type 2 icmp mtu 5;fail
tests/py/ip/icmp.t.json +28 -0
index 9691f0727f5e..12b53b0fe2cc 100644
@@ -1843,3 +1843,31 @@
         }
     }
 ]
+
+# icmp code 1 icmp type 2
+[
+    {
+        "match": {
+            "left": {
+                "payload": {
+                    "field": "type",
+                    "protocol": "icmp"
+                }
+            },
+            "op": "==",
+            "right": 2
+        }
+    },
+    {
+        "match": {
+            "left": {
+                "payload": {
+                    "field": "code",
+                    "protocol": "icmp"
+                }
+            },
+            "op": "==",
+            "right": "host-unreachable"
+        }
+    }
+]
tests/py/ip/icmp.t.payload.ip +6 -0
index d75d12a06125..97464a08379e 100644
@@ -787,3 +787,9 @@ ip test-ip4 input
   [ lookup reg 1 set __set%d ]
   [ immediate reg 0 accept ]
 
+# icmp code 1 icmp type 2
+ip 
+  [ meta load l4proto => reg 1 ]
+  [ cmp eq reg 1 0x00000001 ]
+  [ payload load 2b @ transport header + 0 => reg 1 ]
+  [ cmp eq reg 1 0x00000102 ]
tests/py/ip6/icmpv6.t +2 -0
index 8b411a8bf439..d07c34bd939d 100644
@@ -92,3 +92,5 @@ icmpv6 max-delay {33, 55, 67, 88};ok
 icmpv6 max-delay != {33, 55, 67, 88};ok
 icmpv6 max-delay {33-55};ok
 icmpv6 max-delay != {33-55};ok
+
+icmpv6 type parameter-problem icmpv6 code no-route;ok
tests/py/ip6/icmpv6.t.json +28 -0
index ffc4931c4e0c..e2b25a65444f 100644
@@ -1315,3 +1315,31 @@
         }
     }
 ]
+
+# icmpv6 type parameter-problem icmpv6 code no-route
+[
+    {
+        "match": {
+            "left": {
+                "payload": {
+                    "field": "type",
+                    "protocol": "icmpv6"
+                }
+            },
+            "op": "==",
+            "right": "parameter-problem"
+        }
+    },
+    {
+        "match": {
+            "left": {
+                "payload": {
+                    "field": "code",
+                    "protocol": "icmpv6"
+                }
+            },
+            "op": "==",
+            "right": "no-route"
+        }
+    }
+]
tests/py/ip6/icmpv6.t.payload.ip6 +7 -0
index 171b7eade6d3..448779d16922 100644
@@ -682,3 +682,10 @@ ip6 test-ip6 input
   [ payload load 2b @ transport header + 4 => reg 1 ]
   [ lookup reg 1 set __set%d 0x1 ]
 
+# icmpv6 type parameter-problem icmpv6 code no-route
+ip6 
+  [ meta load l4proto => reg 1 ]
+  [ cmp eq reg 1 0x0000003a ]
+  [ payload load 2b @ transport header + 0 => reg 1 ]
+  [ cmp eq reg 1 0x00000004 ]
+
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH 2/2 payload: check icmp dependency before removing previous icmp expression

nft is too greedy when removing icmp dependencies. 'icmp code 1 type 2' did remove the type when printing.

Be more careful and check that the icmp type dependency of the candidate expression (earlier icmp payload expression) has the same type dependency as the new expression.

Fixes d0f3b9eaab8d77e ("payload: auto-remove simple icmp/icmpv6 dependency expressions")
---
 src/payload.c | 63 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 21 deletions(-)
src/payload.c +42 -21
index 48529bcf5c51..a77ca5500550 100644
@@ -627,6 +627,40 @@ void payload_dependency_release(struct payload_dep_ctx *ctx)
 	ctx->pdep  = NULL;
 }
 
+static uint8_t icmp_dep_to_type(enum icmp_hdr_field_type t)
+{
+	switch (t) {
+	case PROTO_ICMP_ANY:
+		BUG("Invalid map for simple dependency");
+	case PROTO_ICMP_ECHO: return ICMP_ECHO;
+	case PROTO_ICMP6_ECHO: return ICMP6_ECHO_REQUEST;
+	case PROTO_ICMP_MTU: return ICMP_DEST_UNREACH;
+	case PROTO_ICMP_ADDRESS: return ICMP_REDIRECT;
+	case PROTO_ICMP6_MTU: return ICMP6_PACKET_TOO_BIG;
+	case PROTO_ICMP6_MGMQ: return MLD_LISTENER_QUERY;
+	case PROTO_ICMP6_PPTR: return ICMP6_PARAM_PROB;
+	}
+
+	BUG("Missing icmp type mapping");
+}
+
+static bool payload_may_dependency_kill_icmp(struct payload_dep_ctx *ctx, struct expr *expr)
+{
+	const struct expr *dep = ctx->pdep->expr;
+	uint8_t icmp_type;
+
+	icmp_type = expr->payload.tmpl->icmp_dep;
+	if (icmp_type == PROTO_ICMP_ANY)
+		return false;
+
+	if (dep->left->payload.desc != expr->payload.desc)
+		return false;
+
+	icmp_type = icmp_dep_to_type(expr->payload.tmpl->icmp_dep);
+
+	return ctx->icmp_type == icmp_type;
+}
+
 static bool payload_may_dependency_kill(struct payload_dep_ctx *ctx,
 					unsigned int family, struct expr *expr)
 {
@@ -661,6 +695,14 @@ static bool payload_may_dependency_kill(struct payload_dep_ctx *ctx,
 		break;
 	}
 
+	if (expr->payload.base == PROTO_BASE_TRANSPORT_HDR &&
+	    dep->left->payload.base == PROTO_BASE_TRANSPORT_HDR) {
+		if (dep->left->payload.desc == &proto_icmp)
+			return payload_may_dependency_kill_icmp(ctx, expr);
+		if (dep->left->payload.desc == &proto_icmp6)
+			return payload_may_dependency_kill_icmp(ctx, expr);
+	}
+
 	return true;
 }
 
@@ -680,10 +722,6 @@ void payload_dependency_kill(struct payload_dep_ctx *ctx, struct expr *expr,
 	if (payload_dependency_exists(ctx, expr->payload.base) &&
 	    payload_may_dependency_kill(ctx, family, expr))
 		payload_dependency_release(ctx);
-	else if (ctx->icmp_type && ctx->pdep) {
-		fprintf(stderr, "Did not kill \n");
-		payload_dependency_release(ctx);
-	}
 }
 
 void exthdr_dependency_kill(struct payload_dep_ctx *ctx, struct expr *expr,
@@ -707,23 +745,6 @@ void exthdr_dependency_kill(struct payload_dep_ctx *ctx, struct expr *expr,
 	}
 }
 
-static uint8_t icmp_dep_to_type(enum icmp_hdr_field_type t)
-{
-	switch (t) {
-	case PROTO_ICMP_ANY:
-		BUG("Invalid map for simple dependency");
-	case PROTO_ICMP_ECHO: return ICMP_ECHO;
-	case PROTO_ICMP6_ECHO: return ICMP6_ECHO_REQUEST;
-	case PROTO_ICMP_MTU: return ICMP_DEST_UNREACH;
-	case PROTO_ICMP_ADDRESS: return ICMP_REDIRECT;
-	case PROTO_ICMP6_MTU: return ICMP6_PACKET_TOO_BIG;
-	case PROTO_ICMP6_MGMQ: return MLD_LISTENER_QUERY;
-	case PROTO_ICMP6_PPTR: return ICMP6_PARAM_PROB;
-	}
-
-	BUG("Missing icmp type mapping");
-}
-
 /**
  * payload_expr_complete - fill in type information of a raw payload expr
  *
Signature · 1 line
2.26.2
Tested-by on v1 2/2
#2 lore raw

Re: [PATCH nft 2/2] payload: check icmp dependency before removing previous icmp expression

Quoted 7 lines · Florian Westphal

nft is too greedy when removing icmp dependencies. 'icmp code 1 type 2' did remove the type when printing.

Be more careful and check that the icmp type dependency of the candidate expression (earlier icmp payload expression) has the same type dependency as the new expression.

Quoted 5 lines
Fixes d0f3b9eaab8d77e ("payload: auto-remove simple icmp/icmpv6 dependency expressions")

---

Thanks Florian. This fixes the issue [1] reported against firewalld.

[1]: https://github.com/firewalld/firewalld/issues/752

--->8---

tmp/autopkgtest.PRXtPH/build.yiS/src/src/tests/testsuite.dir/at-groups/97/stdout renamed from - +1 -1
@@ -1,6 +1,6 @@
 table inet firewalld {
 chain filter_IN_public_deny {
-icmp type destination-unreachable icmp code host-prohibited reject with icmpx type admin-prohibited
+icmp code host-prohibited reject with icmpx type admin-prohibited
 }
 }
 
#3 lore raw

Re: [PATCH nft 2/2] payload: check icmp dependency before removing previous icmp expression

Am 02.02.21 um 14:21 schrieb Eric Garver:

Quoted 14 lines · Florian Westphal
Quoted 7 lines

nft is too greedy when removing icmp dependencies. 'icmp code 1 type 2' did remove the type when printing.

Be more careful and check that the icmp type dependency of the candidate expression (earlier icmp payload expression) has the same type dependency as the new expression.

Quoted 5 lines
Fixes d0f3b9eaab8d77e ("payload: auto-remove simple icmp/icmpv6 dependency expressions")

---

Quoted 5 lines

Thanks Florian. This fixes the issue [1] reported against firewalld.

[1]: https://github.com/firewalld/firewalld/issues/752

I can confirm that as well.

Regards,
Michael