evaluate: add support for variables in map expressions
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
@@ -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
@@ -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
@@ -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
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.
--- tests/shell/testcases/packetpath/flowtables | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/shell/testcases/packetpath/flowtables +1 -1
@@ -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
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
socatcommand. Update that too.
Applied, thanks
Pablo Neira Ayuso
maintainerQuoted 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_mapto 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
Signed-off-by Jeremy Sowden <[email protected]>--- 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.nftsrc/evaluate.c +0
-0@@ -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
-0mappings->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);
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_mapto allow it.Add a test-case.
Thanks for your patch.
Quoted 12 lines
Signed-off-by Jeremy Sowden <[email protected]>--- 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@@ -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
-0mappings->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);