Skip to contentSkip to conversation

evaluate: add support for variables in map expressions

PATCH Superseded Jeremy Sowden 2 participants 5 messages Mar 24, 2024 – Apr 3, 2024 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 evaluate: add support for variables in map expressions

It is possible to use a variable to initialize a map, which is then used in a map statement:

define m = { ::1234 : 5678 }

  table ip6 nat {
    map m {
      typeof ip6 daddr : tcp dport;
      elements = $m
    }
    chain prerouting {
      ip6 nexthdr tcp redirect to ip6 daddr map @m
    }
  }

However, if one tries to use the variable directly in the statement:

define m = { ::1234 : 5678 }

  table ip6 nat {
    chain prerouting {
      ip6 nexthdr tcp redirect to ip6 daddr map $m
    }
  }

nft rejects it:

/space/azazel/tmp/ruleset.1067161.nft:5:47-48: Error: invalid mapping expression variable
ip6 nexthdr tcp redirect to ip6 daddr map $m
~~~~~~~~~ ^^

Extend expr_evaluate_map to allow it.

Add a test-case.

---
 src/evaluate.c                                |  1 +
 .../shell/testcases/maps/anonymous_snat_map_1 | 16 +++++
 .../maps/dumps/anonymous_snat_map_1.json-nft  | 58 +++++++++++++++++++
 .../maps/dumps/anonymous_snat_map_1.nft       |  5 ++
 4 files changed, 80 insertions(+)
 create mode 100755 tests/shell/testcases/maps/anonymous_snat_map_1
 create mode 100644 tests/shell/testcases/maps/dumps/anonymous_snat_map_1.json-nft
 create mode 100644 tests/shell/testcases/maps/dumps/anonymous_snat_map_1.nft
src/evaluate.c +1 -0
index 1682ba58989e..d49213f8d6bd 100644
@@ -2061,6 +2061,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)
 	mappings->set_flags |= NFT_SET_MAP;
 
 	switch (map->mappings->etype) {
+	case EXPR_VARIABLE:
 	case EXPR_SET:
 		if (ctx->ectx.key && ctx->ectx.key->etype == EXPR_CONCAT) {
 			key = expr_clone(ctx->ectx.key);
tests/shell/testcases/maps/anonymous_snat_map_1 new file +16 -0
new file mode 100755
index 000000000000..031de0c1a83f
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+# Variable containing anonymous map can be added to a snat rule
+
+set -e
+
+RULESET='
+define m = {1.1.1.1 : 2.2.2.2}
+table nat {
+  chain postrouting {
+    snat ip saddr map $m
+  }
+}
+'
+
+$NFT -f - <<< "$RULESET"
tests/shell/testcases/maps/dumps/anonymous_snat_map_1.json-nft new file +58 -0

58 lines not on this page · raw message

tests/shell/testcases/maps/dumps/anonymous_snat_map_1.nft new file +5 -0
new file mode 100644
index 000000000000..5009560c9d69
@@ -0,0 +1,5 @@
+table ip nat {
+	chain postrouting {
+		snat to ip saddr map { 1.1.1.1 : 2.2.2.2 }
+	}
+}
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/1 tests: shell: packetpath/flowtables: open all temporary files in /tmp

The test used to do I/O over a named pipe in $PWD, until Phil changed it to create the pipe in /tmp. However, he missed one socat command. Update that too.

Fixes 3a9f29e21726 ("tests: shell: packetpath/flowtables: Avoid spurious EPERM")
---
 tests/shell/testcases/packetpath/flowtables | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/shell/testcases/packetpath/flowtables +1 -1
index 18a57a9b2b72..ec7dfeb75c00 100755
@@ -77,7 +77,7 @@ ip netns exec $R sysctl -w net.netfilter.nf_conntrack_tcp_timeout_established=86
 }
 
 # A trick to control the timing to send a packet
-ip netns exec $S socat TCP6-LISTEN:10001 GOPEN:pipefile,ignoreeof &
+ip netns exec $S socat TCP6-LISTEN:10001 GOPEN:/tmp/pipefile-$rnd,ignoreeof &
 sleep 1
 ip netns exec $C socat -b 2048 PIPE:/tmp/pipefile-$rnd 'TCP:[2001:db8:ffff:22::1]:10001' &
 sleep 1
Signature · 1 line
2.43.0
maintainer Applied
#2 lore raw

Re: [PATCH nftables] tests: shell: packetpath/flowtables: open all temporary files in /tmp

Quoted 3 lines · Jeremy Sowden

The test used to do I/O over a named pipe in $PWD, until Phil changed it to create the pipe in /tmp. However, he missed one socat command. Update that too.

Applied, thanks

#3 lore raw
In reply to Jeremy Sowden
Quoted 27 lines · Jeremy Sowden

It is possible to use a variable to initialize a map, which is then used in a map statement:

define m = { ::1234 : 5678 }

  table ip6 nat {
    map m {
      typeof ip6 daddr : tcp dport;
      elements = $m
    }
    chain prerouting {
      ip6 nexthdr tcp redirect to ip6 daddr map @m
    }
  }

However, if one tries to use the variable directly in the statement:

define m = { ::1234 : 5678 }

  table ip6 nat {
    chain prerouting {
      ip6 nexthdr tcp redirect to ip6 daddr map $m
    }
  }

nft rejects it:

Quoted 7 lines

/space/azazel/tmp/ruleset.1067161.nft:5:47-48: Error: invalid mapping expression variable
ip6 nexthdr tcp redirect to ip6 daddr map $m
~~~~~~~~~ ^^

Extend expr_evaluate_map to allow it.

Add a test-case.

Thanks for your patch.

src/evaluate.c static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)

Quoted 17 lines
---
 src/evaluate.c                                |  1 +
 .../shell/testcases/maps/anonymous_snat_map_1 | 16 +++++
 .../maps/dumps/anonymous_snat_map_1.json-nft  | 58 +++++++++++++++++++
 .../maps/dumps/anonymous_snat_map_1.nft       |  5 ++
 4 files changed, 80 insertions(+)
 create mode 100755 tests/shell/testcases/maps/anonymous_snat_map_1
 create mode 100644 tests/shell/testcases/maps/dumps/anonymous_snat_map_1.json-nft
 create mode 100644 tests/shell/testcases/maps/dumps/anonymous_snat_map_1.nft
src/evaluate.c +0 -0
index 1682ba58989e..d49213f8d6bd 100644
@@ -2061,6 +2061,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)

expr_evaluate_objmap() also needs a similar fix.

Quoted 7 lines
src/evaluate.c +1 -0
 	mappings->set_flags |= NFT_SET_MAP;
 
 	switch (map->mappings->etype) {
+	case EXPR_VARIABLE:
 	case EXPR_SET:
 		if (ctx->ectx.key && ctx->ectx.key->etype == EXPR_CONCAT) {
 			key = expr_clone(ctx->ectx.key);
#4 lore raw
Quoted 50 lines · Pablo Neira Ayuso
Quoted 27 lines · Jeremy Sowden

It is possible to use a variable to initialize a map, which is then used in a map statement:

define m = { ::1234 : 5678 }

  table ip6 nat {
    map m {
      typeof ip6 daddr : tcp dport;
      elements = $m
    }
    chain prerouting {
      ip6 nexthdr tcp redirect to ip6 daddr map @m
    }
  }

However, if one tries to use the variable directly in the statement:

define m = { ::1234 : 5678 }

  table ip6 nat {
    chain prerouting {
      ip6 nexthdr tcp redirect to ip6 daddr map $m
    }
  }

nft rejects it:

Quoted 7 lines

/space/azazel/tmp/ruleset.1067161.nft:5:47-48: Error: invalid mapping expression variable
ip6 nexthdr tcp redirect to ip6 daddr map $m
~~~~~~~~~ ^^

Extend expr_evaluate_map to allow it.

Add a test-case.

Thanks for your patch.

Quoted 12 lines
---
 src/evaluate.c                                |  1 +
 .../shell/testcases/maps/anonymous_snat_map_1 | 16 +++++
 .../maps/dumps/anonymous_snat_map_1.json-nft  | 58 +++++++++++++++++++
 .../maps/dumps/anonymous_snat_map_1.nft       |  5 ++
 4 files changed, 80 insertions(+)
 create mode 100755 tests/shell/testcases/maps/anonymous_snat_map_1
 create mode 100644 tests/shell/testcases/maps/dumps/anonymous_snat_map_1.json-nft
 create mode 100644 tests/shell/testcases/maps/dumps/anonymous_snat_map_1.nft
Quoted 7 lines

src/evaluate.c static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)

Quoted 5 lines
src/evaluate.c +0 -0
index 1682ba58989e..d49213f8d6bd 100644
@@ -2061,6 +2061,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)

expr_evaluate_objmap() also needs a similar fix.

Cool. Will update and resend.

J.

Quoted 7 lines
Quoted 7 lines
src/evaluate.c +1 -0
 	mappings->set_flags |= NFT_SET_MAP;
 
 	switch (map->mappings->etype) {
+	case EXPR_VARIABLE:
 	case EXPR_SET:
 		if (ctx->ectx.key && ctx->ectx.key->etype == EXPR_CONCAT) {
 			key = expr_clone(ctx->ectx.key);