Skip to contentSkip to conversation

tproxy: Drop artificial port printing restriction

PATCH In review Phil Sutter 2 participants 5 messages Nov 2, 2023 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 tproxy: Drop artificial port printing restriction

It does not make much sense to omit printing the port expression if it's not a value expression: On one hand, input allows for more advanced uses. On the other, if it is in-kernel, best nft can do is to try and print it no matter what. Just ignoring ruleset elements can't be correct.

Fixes 2be1d52644cf7 ("src: Add tproxy support")
---
 src/statement.c                |  2 +-
 tests/py/inet/tproxy.t         |  2 ++
 tests/py/inet/tproxy.t.json    | 35 ++++++++++++++++++++++++++++++++++
 tests/py/inet/tproxy.t.payload | 12 ++++++++++++
 4 files changed, 50 insertions(+), 1 deletion(-)
src/statement.c +1 -1
index 475611664946a..f5176e6d87f95 100644
@@ -989,7 +989,7 @@ static void tproxy_stmt_print(const struct stmt *stmt, struct output_ctx *octx)
 			expr_print(stmt->tproxy.addr, octx);
 		}
 	}
-	if (stmt->tproxy.port && stmt->tproxy.port->etype == EXPR_VALUE) {
+	if (stmt->tproxy.port) {
 		if (!stmt->tproxy.addr)
 			nft_print(octx, " ");
 		nft_print(octx, ":");
tests/py/inet/tproxy.t +2 -0
index d23bbcb56cdcd..9901df75a91a8 100644
@@ -19,3 +19,5 @@ meta l4proto 17 tproxy ip to :50080;ok
 meta l4proto 17 tproxy ip6 to :50080;ok
 meta l4proto 17 tproxy to :50080;ok
 ip daddr 0.0.0.0/0 meta l4proto 6 tproxy ip to :2000;ok
+
+meta l4proto 6 tproxy ip to 127.0.0.1:symhash mod 2 map { 0 : 23, 1 : 42 };ok
tests/py/inet/tproxy.t.json +35 -0
index 7b3b11c49205a..71b6fd2f678dd 100644
@@ -183,3 +183,38 @@
         }
     }
 ]
+
+# meta l4proto 6 tproxy ip to 127.0.0.1:symhash mod 2 map { 0 : 23, 1 : 42 }
+[
+    {
+        "match": {
+            "left": {
+                "meta": {
+                    "key": "l4proto"
+                }
+            },
+            "op": "==",
+            "right": 6
+        }
+    },
+    {
+        "tproxy": {
+            "addr": "127.0.0.1",
+            "family": "ip",
+            "port": {
+                "map": {
+                    "data": {
+                        "set": [
+                            [ 0, 23 ],
+                            [ 1, 42 ]
+                        ]
+                    },
+                    "key": {
+                        "symhash": { "mod": 2 }
+                    }
+                }
+            }
+        }
+    }
+]
+
tests/py/inet/tproxy.t.payload +12 -0
index 24bf8f6002f8f..2f41904261144 100644
@@ -61,3 +61,15 @@ inet x y
   [ immediate reg 1 0x0000d007 ]
   [ tproxy ip port reg 1 ]
 
+# meta l4proto 6 tproxy ip to 127.0.0.1:symhash mod 2 map { 0 : 23, 1 : 42 }
+__map%d x b size 2
+__map%d x 0
+	element 00000000  : 00001700 0 [end]	element 00000001  : 00002a00 0 [end]
+inet x y
+  [ meta load l4proto => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 1 0x0100007f ]
+  [ hash reg 2 = symhash() % mod 2 ]
+  [ lookup reg 2 set __map%d dreg 2 ]
+  [ tproxy ip addr reg 1 port reg 2 ]
+
Signature · 1 line
2.41.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Phil Sutter

It does not make much sense to omit printing the port expression if it's not a value expression: On one hand, input allows for more advanced uses. On the other, if it is in-kernel, best nft can do is to try and print it no matter what. Just ignoring ruleset elements can't be correct.

Fixes 2be1d52644cf7 ("src: Add tproxy support")

Great work Phil.

Quoted 96 lines
---
 src/statement.c                |  2 +-
 tests/py/inet/tproxy.t         |  2 ++
 tests/py/inet/tproxy.t.json    | 35 ++++++++++++++++++++++++++++++++++
 tests/py/inet/tproxy.t.payload | 12 ++++++++++++
 4 files changed, 50 insertions(+), 1 deletion(-)
src/statement.c +1 -1
index 475611664946a..f5176e6d87f95 100644
@@ -989,7 +989,7 @@ static void tproxy_stmt_print(const struct stmt *stmt, struct output_ctx *octx)
 			expr_print(stmt->tproxy.addr, octx);
 		}
 	}
-	if (stmt->tproxy.port && stmt->tproxy.port->etype == EXPR_VALUE) {
+	if (stmt->tproxy.port) {
 		if (!stmt->tproxy.addr)
 			nft_print(octx, " ");
 		nft_print(octx, ":");
tests/py/inet/tproxy.t +2 -0
index d23bbcb56cdcd..9901df75a91a8 100644
@@ -19,3 +19,5 @@ meta l4proto 17 tproxy ip to :50080;ok
 meta l4proto 17 tproxy ip6 to :50080;ok
 meta l4proto 17 tproxy to :50080;ok
 ip daddr 0.0.0.0/0 meta l4proto 6 tproxy ip to :2000;ok
+
+meta l4proto 6 tproxy ip to 127.0.0.1:symhash mod 2 map { 0 : 23, 1 : 42 };ok
tests/py/inet/tproxy.t.json +35 -0
index 7b3b11c49205a..71b6fd2f678dd 100644
@@ -183,3 +183,38 @@
         }
     }
 ]
+
+# meta l4proto 6 tproxy ip to 127.0.0.1:symhash mod 2 map { 0 : 23, 1 : 42 }
+[
+    {
+        "match": {
+            "left": {
+                "meta": {
+                    "key": "l4proto"
+                }
+            },
+            "op": "==",
+            "right": 6
+        }
+    },
+    {
+        "tproxy": {
+            "addr": "127.0.0.1",
+            "family": "ip",
+            "port": {
+                "map": {
+                    "data": {
+                        "set": [
+                            [ 0, 23 ],
+                            [ 1, 42 ]
+                        ]
+                    },
+                    "key": {
+                        "symhash": { "mod": 2 }
+                    }
+                }
+            }
+        }
+    }
+]
+
tests/py/inet/tproxy.t.payload +12 -0
index 24bf8f6002f8f..2f41904261144 100644
@@ -61,3 +61,15 @@ inet x y
   [ immediate reg 1 0x0000d007 ]
   [ tproxy ip port reg 1 ]
 
+# meta l4proto 6 tproxy ip to 127.0.0.1:symhash mod 2 map { 0 : 23, 1 : 42 }
+__map%d x b size 2
+__map%d x 0
+	element 00000000  : 00001700 0 [end]	element 00000001  : 00002a00 0 [end]
+inet x y
+  [ meta load l4proto => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 1 0x0100007f ]
+  [ hash reg 2 = symhash() % mod 2 ]
+  [ lookup reg 2 set __map%d dreg 2 ]
+  [ tproxy ip addr reg 1 port reg 2 ]
+
Signature · 1 line
2.41.0
#2 lore raw
Quoted 1 line · Pablo Neira Ayuso

On Thu, Nov 02, 2023 at 02:52:58PM +0100, Phil Sutter wrote:

[...]

Quoted 10 lines

src/statement.c static void tproxy_stmt_print(const struct stmt *stmt, struct output_ctx *octx) · line 992

Quoted 10 lines
src/statement.c +1 -1
index 475611664946a..f5176e6d87f95 100644
@@ -989,7 +989,7 @@ static void tproxy_stmt_print(const struct stmt *stmt, struct output_ctx *octx)
 			expr_print(stmt->tproxy.addr, octx);
 		}
 	}
-	if (stmt->tproxy.port && stmt->tproxy.port->etype == EXPR_VALUE) {
+	if (stmt->tproxy.port) {

Question: is this pattern used elsewhere?

The original author of this might have taken (copied) this code from an existing statement?

author reviewer
#3 lore raw
Quoted 16 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

On Thu, Nov 02, 2023 at 02:52:58PM +0100, Phil Sutter wrote:

[...]

Quoted 10 lines

src/statement.c static void tproxy_stmt_print(const struct stmt *stmt, struct output_ctx *octx) · line 992

Quoted 10 lines
src/statement.c +1 -1
index 475611664946a..f5176e6d87f95 100644
@@ -989,7 +989,7 @@ static void tproxy_stmt_print(const struct stmt *stmt, struct output_ctx *octx)
 			expr_print(stmt->tproxy.addr, octx);
 		}
 	}
-	if (stmt->tproxy.port && stmt->tproxy.port->etype == EXPR_VALUE) {
+	if (stmt->tproxy.port) {

Question: is this pattern used elsewhere?

Quoted 2 lines

The original author of this might have taken (copied) this code from an existing statement?

A quick grep for EXPR_VALUE didn't turn up anything suspicious.

Maybe Máté recalls why he did things this way back in 2018. FWIW, the EXPR_VALUE check was already there in v1 of his patch.

Cheers, Phil

author reviewer
#4 lore raw
In reply to Pablo Neira Ayuso
Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Phil Sutter

It does not make much sense to omit printing the port expression if it's not a value expression: On one hand, input allows for more advanced uses. On the other, if it is in-kernel, best nft can do is to try and print it no matter what. Just ignoring ruleset elements can't be correct.

Quoted 5 lines
Quoted 3 lines
Fixes 2be1d52644cf7 ("src: Add tproxy support")

Great work Phil.

Hey, I'm just deleting code which gets in the way. ;)

Quoted 1 line

Patch applied, thanks for your review.