All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: phil@nwl.cc
Subject: [PATCH nft] evaluate: set key byteorder when evaluating mapping
Date: Wed, 25 Nov 2026 00:53:06 +0100	[thread overview]
Message-ID: <20261124235306.1777-1-pablo@netfilter.org> (raw)

set->key->dtype is TYPE_INTEGER (see integer_type definition) which uses
BYTEORDER_INVALID by default, this is used to set up the context for
parsing the symbols in the mapping. Set explicit byteorder context to
address this bug.

Fixes: 144d97e151adb ("src: normalize set element with EXPR_MAPPING")
Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/evaluate.c                                |   3 +-
 .../maps/dumps/nat_addr_port.json-nft         | 134 ++++++++++++++++++
 .../testcases/maps/dumps/nat_addr_port.nft    |  18 +++
 tests/shell/testcases/maps/nat_addr_port      |  12 ++
 4 files changed, 166 insertions(+), 1 deletion(-)

diff --git a/src/evaluate.c b/src/evaluate.c
index 8ce37e57a836..61372dd80cdd 100644
--- a/src/evaluate.c
+++ b/src/evaluate.c
@@ -2393,7 +2393,8 @@ static int expr_evaluate_mapping(struct eval_ctx *ctx, struct expr **expr)
 	if (!set_is_map(set->flags))
 		return set_error(ctx, set, "set is not a map");
 
-	expr_set_context(&ctx->ectx, set->key->dtype, set->key->len);
+	__expr_set_context(&ctx->ectx, set->key->dtype, set->key->byteorder, set->key->len, 0);
+
 	ctx->ectx.key = key;
 
 	if (expr_evaluate(ctx, &mapping->left) < 0)
diff --git a/tests/shell/testcases/maps/dumps/nat_addr_port.json-nft b/tests/shell/testcases/maps/dumps/nat_addr_port.json-nft
index 216761c6f150..288178710387 100644
--- a/tests/shell/testcases/maps/dumps/nat_addr_port.json-nft
+++ b/tests/shell/testcases/maps/dumps/nat_addr_port.json-nft
@@ -65,6 +65,42 @@
         ]
       }
     },
+    {
+      "map": {
+        "family": "ip",
+        "name": "w",
+        "table": "ipfoo",
+        "type": {
+          "typeof": {
+            "numgen": {
+              "mode": "inc",
+              "mod": 3,
+              "offset": 0
+            }
+          }
+        },
+        "handle": 0,
+        "map": "mark",
+        "elem": [
+          [
+            0,
+            9
+          ],
+          [
+            1,
+            8
+          ],
+          [
+            2,
+            7
+          ],
+          [
+            3,
+            6
+          ]
+        ]
+      }
+    },
     {
       "map": {
         "family": "ip",
@@ -411,6 +447,37 @@
         ]
       }
     },
+    {
+      "rule": {
+        "family": "ip",
+        "table": "ipfoo",
+        "chain": "c",
+        "handle": 0,
+        "expr": [
+          {
+            "mangle": {
+              "key": {
+                "meta": {
+                  "key": "mark"
+                }
+              },
+              "value": {
+                "map": {
+                  "key": {
+                    "numgen": {
+                      "mode": "inc",
+                      "mod": 2000,
+                      "offset": 0
+                    }
+                  },
+                  "data": "@w"
+                }
+              }
+            }
+          }
+        ]
+      }
+    },
     {
       "table": {
         "family": "ip6",
@@ -469,6 +536,42 @@
         ]
       }
     },
+    {
+      "map": {
+        "family": "ip6",
+        "name": "w",
+        "table": "ip6foo",
+        "type": {
+          "typeof": {
+            "numgen": {
+              "mode": "inc",
+              "mod": 3,
+              "offset": 0
+            }
+          }
+        },
+        "handle": 0,
+        "map": "mark",
+        "elem": [
+          [
+            0,
+            9
+          ],
+          [
+            1,
+            8
+          ],
+          [
+            2,
+            7
+          ],
+          [
+            3,
+            6
+          ]
+        ]
+      }
+    },
     {
       "map": {
         "family": "ip6",
@@ -788,6 +891,37 @@
         ]
       }
     },
+    {
+      "rule": {
+        "family": "ip6",
+        "table": "ip6foo",
+        "chain": "c",
+        "handle": 0,
+        "expr": [
+          {
+            "mangle": {
+              "key": {
+                "meta": {
+                  "key": "mark"
+                }
+              },
+              "value": {
+                "map": {
+                  "key": {
+                    "numgen": {
+                      "mode": "inc",
+                      "mod": 2000,
+                      "offset": 0
+                    }
+                  },
+                  "data": "@w"
+                }
+              }
+            }
+          }
+        ]
+      }
+    },
     {
       "table": {
         "family": "inet",
diff --git a/tests/shell/testcases/maps/dumps/nat_addr_port.nft b/tests/shell/testcases/maps/dumps/nat_addr_port.nft
index f4a952e9db42..24315b2981f5 100644
--- a/tests/shell/testcases/maps/dumps/nat_addr_port.nft
+++ b/tests/shell/testcases/maps/dumps/nat_addr_port.nft
@@ -7,6 +7,14 @@ table ip ipfoo {
 		typeof numgen inc mod 2 : ip daddr . tcp dport
 	}
 
+	map w {
+		typeof numgen inc mod 3 : meta mark
+		elements = { 0 : 0x00000009,
+			     1 : 0x00000008,
+			     2 : 0x00000007,
+			     3 : 0x00000006 }
+	}
+
 	map x {
 		type ipv4_addr : ipv4_addr
 	}
@@ -32,6 +40,7 @@ table ip ipfoo {
 		meta l4proto 6 dnat to 10.2.3.4:4242
 		dnat to numgen inc mod 2 map @t1
 		meta l4proto 6 dnat ip to numgen inc mod 2 map @t2
+		meta mark set numgen inc mod 2000 map @w
 	}
 }
 table ip6 ip6foo {
@@ -43,6 +52,14 @@ table ip6 ip6foo {
 		typeof numgen inc mod 2 : ip6 daddr . tcp dport
 	}
 
+	map w {
+		typeof numgen inc mod 3 : meta mark
+		elements = { 0 : 0x00000009,
+			     1 : 0x00000008,
+			     2 : 0x00000007,
+			     3 : 0x00000006 }
+	}
+
 	map x {
 		type ipv6_addr : ipv6_addr
 	}
@@ -66,6 +83,7 @@ table ip6 ip6foo {
 		meta l4proto 6 dnat to [fd00:10:244::4]:6443
 		dnat to numgen inc mod 2 map @t1
 		meta l4proto 6 dnat ip6 to numgen inc mod 2 map @t2
+		meta mark set numgen inc mod 2000 map @w
 	}
 }
 table inet inetfoo {
diff --git a/tests/shell/testcases/maps/nat_addr_port b/tests/shell/testcases/maps/nat_addr_port
index 5f6ff1c49cc0..b1a458d601c6 100755
--- a/tests/shell/testcases/maps/nat_addr_port
+++ b/tests/shell/testcases/maps/nat_addr_port
@@ -11,6 +11,11 @@ table ip ipfoo {
 		typeof numgen inc mod 2 : ip daddr . tcp dport
 	}
 
+	map w {
+		typeof numgen inc mod 3 : meta mark
+		elements = { 0 : 9, 1 : 8, 2 : 7, 3 : 6 }
+	}
+
 	map x {
 		type ipv4_addr : ipv4_addr
 	}
@@ -34,6 +39,7 @@ table ip ipfoo {
 		meta l4proto tcp dnat ip addr . port to 10.2.3.4 . 4242
 		dnat ip to numgen inc mod 2 map @t1
 		meta l4proto tcp dnat ip addr . port to numgen inc mod 2 map @t2
+		meta mark set numgen inc mod 2000 map @w
 	}
 }
 EOF
@@ -55,6 +61,11 @@ table ip6 ip6foo {
 		typeof numgen inc mod 2 : ip6 daddr . tcp dport
 	}
 
+	map w {
+		typeof numgen inc mod 3 : meta mark
+		elements = { 0 : 9, 1 : 8, 2 : 7, 3 : 6 }
+	}
+
 	map x {
 		type ipv6_addr : ipv6_addr
 	}
@@ -76,6 +87,7 @@ table ip6 ip6foo {
 		meta l4proto tcp dnat ip6 addr . port to fd00:10:244::4 . 6443
 		dnat ip6 to numgen inc mod 2 map @t1
 		meta l4proto tcp dnat ip6 addr . port to numgen inc mod 2 map @t2
+		meta mark set numgen inc mod 2000 map @w
 	}
 }
 EOF
-- 
2.47.3


             reply	other threads:[~2026-11-24 23:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 23:53 Pablo Neira Ayuso [this message]
2026-11-25 10:31 ` [PATCH nft] evaluate: set key byteorder when evaluating mapping Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124235306.1777-1-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=phil@nwl.cc \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.