All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH nftables] evaluate: add support for variables in map expressions
@ 2024-03-24 14:59 Jeremy Sowden
  2024-03-24 14:59 ` [PATCH nftables] tests: shell: packetpath/flowtables: open all temporary files in /tmp Jeremy Sowden
  2024-04-02 22:42 ` [PATCH nftables] evaluate: add support for variables in map expressions Pablo Neira Ayuso
  0 siblings, 2 replies; 5+ messages in thread
From: Jeremy Sowden @ 2024-03-24 14:59 UTC (permalink / raw)
  To: Netfilter Devel

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.

Link: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1067161
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 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

diff --git a/src/evaluate.c b/src/evaluate.c
index 1682ba58989e..d49213f8d6bd 100644
--- a/src/evaluate.c
+++ b/src/evaluate.c
@@ -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);
diff --git a/tests/shell/testcases/maps/anonymous_snat_map_1 b/tests/shell/testcases/maps/anonymous_snat_map_1
new file mode 100755
index 000000000000..031de0c1a83f
--- /dev/null
+++ b/tests/shell/testcases/maps/anonymous_snat_map_1
@@ -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"
diff --git a/tests/shell/testcases/maps/dumps/anonymous_snat_map_1.json-nft b/tests/shell/testcases/maps/dumps/anonymous_snat_map_1.json-nft
new file mode 100644
index 000000000000..f4c55706787c
--- /dev/null
+++ b/tests/shell/testcases/maps/dumps/anonymous_snat_map_1.json-nft
@@ -0,0 +1,58 @@
+{
+  "nftables": [
+    {
+      "metainfo": {
+        "version": "VERSION",
+        "release_name": "RELEASE_NAME",
+        "json_schema_version": 1
+      }
+    },
+    {
+      "table": {
+        "family": "ip",
+        "name": "nat",
+        "handle": 0
+      }
+    },
+    {
+      "chain": {
+        "family": "ip",
+        "table": "nat",
+        "name": "postrouting",
+        "handle": 0
+      }
+    },
+    {
+      "rule": {
+        "family": "ip",
+        "table": "nat",
+        "chain": "postrouting",
+        "handle": 0,
+        "expr": [
+          {
+            "snat": {
+              "addr": {
+                "map": {
+                  "key": {
+                    "payload": {
+                      "protocol": "ip",
+                      "field": "saddr"
+                    }
+                  },
+                  "data": {
+                    "set": [
+                      [
+                        "1.1.1.1",
+                        "2.2.2.2"
+                      ]
+                    ]
+                  }
+                }
+              }
+            }
+          }
+        ]
+      }
+    }
+  ]
+}
diff --git a/tests/shell/testcases/maps/dumps/anonymous_snat_map_1.nft b/tests/shell/testcases/maps/dumps/anonymous_snat_map_1.nft
new file mode 100644
index 000000000000..5009560c9d69
--- /dev/null
+++ b/tests/shell/testcases/maps/dumps/anonymous_snat_map_1.nft
@@ -0,0 +1,5 @@
+table ip nat {
+	chain postrouting {
+		snat to ip saddr map { 1.1.1.1 : 2.2.2.2 }
+	}
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH nftables] tests: shell: packetpath/flowtables: open all temporary files in /tmp
  2024-03-24 14:59 [PATCH nftables] evaluate: add support for variables in map expressions Jeremy Sowden
@ 2024-03-24 14:59 ` Jeremy Sowden
  2024-04-01 15:02   ` Pablo Neira Ayuso
  2024-04-02 22:42 ` [PATCH nftables] evaluate: add support for variables in map expressions Pablo Neira Ayuso
  1 sibling, 1 reply; 5+ messages in thread
From: Jeremy Sowden @ 2024-03-24 14:59 UTC (permalink / raw)
  To: Netfilter Devel

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")
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 tests/shell/testcases/packetpath/flowtables | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/shell/testcases/packetpath/flowtables b/tests/shell/testcases/packetpath/flowtables
index 18a57a9b2b72..ec7dfeb75c00 100755
--- a/tests/shell/testcases/packetpath/flowtables
+++ b/tests/shell/testcases/packetpath/flowtables
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH nftables] tests: shell: packetpath/flowtables: open all temporary files in /tmp
  2024-03-24 14:59 ` [PATCH nftables] tests: shell: packetpath/flowtables: open all temporary files in /tmp Jeremy Sowden
@ 2024-04-01 15:02   ` Pablo Neira Ayuso
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Neira Ayuso @ 2024-04-01 15:02 UTC (permalink / raw)
  To: Jeremy Sowden; +Cc: Netfilter Devel

On Sun, Mar 24, 2024 at 02:59:08PM +0000, Jeremy Sowden wrote:
> 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH nftables] evaluate: add support for variables in map expressions
  2024-03-24 14:59 [PATCH nftables] evaluate: add support for variables in map expressions Jeremy Sowden
  2024-03-24 14:59 ` [PATCH nftables] tests: shell: packetpath/flowtables: open all temporary files in /tmp Jeremy Sowden
@ 2024-04-02 22:42 ` Pablo Neira Ayuso
  2024-04-03  7:38   ` Jeremy Sowden
  1 sibling, 1 reply; 5+ messages in thread
From: Pablo Neira Ayuso @ 2024-04-02 22:42 UTC (permalink / raw)
  To: Jeremy Sowden; +Cc: Netfilter Devel

On Sun, Mar 24, 2024 at 02:59:07PM +0000, Jeremy Sowden wrote:
> 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.

Thanks for your patch.

> Link: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1067161
> Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
> ---
>  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
> 
> diff --git a/src/evaluate.c b/src/evaluate.c
> index 1682ba58989e..d49213f8d6bd 100644
> --- a/src/evaluate.c
> +++ b/src/evaluate.c
> @@ -2061,6 +2061,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)

expr_evaluate_objmap() also needs a similar fix.

>  	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);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH nftables] evaluate: add support for variables in map expressions
  2024-04-02 22:42 ` [PATCH nftables] evaluate: add support for variables in map expressions Pablo Neira Ayuso
@ 2024-04-03  7:38   ` Jeremy Sowden
  0 siblings, 0 replies; 5+ messages in thread
From: Jeremy Sowden @ 2024-04-03  7:38 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Netfilter Devel

[-- Attachment #1: Type: text/plain, Size: 2380 bytes --]

On 2024-04-03, at 00:42:59 +0200, Pablo Neira Ayuso wrote:
> On Sun, Mar 24, 2024 at 02:59:07PM +0000, Jeremy Sowden wrote:
> > 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.
> 
> Thanks for your patch.
> 
> > Link: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1067161
> > Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
> > ---
> >  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
> > 
> > diff --git a/src/evaluate.c b/src/evaluate.c
> > index 1682ba58989e..d49213f8d6bd 100644
> > --- a/src/evaluate.c
> > +++ b/src/evaluate.c
> > @@ -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.

> >  	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);

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-03  7:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-24 14:59 [PATCH nftables] evaluate: add support for variables in map expressions Jeremy Sowden
2024-03-24 14:59 ` [PATCH nftables] tests: shell: packetpath/flowtables: open all temporary files in /tmp Jeremy Sowden
2024-04-01 15:02   ` Pablo Neira Ayuso
2024-04-02 22:42 ` [PATCH nftables] evaluate: add support for variables in map expressions Pablo Neira Ayuso
2024-04-03  7:38   ` Jeremy Sowden

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.