All of lore.kernel.org
 help / color / mirror / Atom feed
* [nft PATCH] parser_json: support handle for rule positioning in JSON add rule
@ 2025-10-29  0:30 Alexandre Knecht
  2025-10-29 11:18 ` Florian Westphal
  2025-10-29 22:45 ` Alexandre Knecht
  0 siblings, 2 replies; 10+ messages in thread
From: Alexandre Knecht @ 2025-10-29  0:30 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Alexandre Knecht

This patch fixes JSON-based rule positioning when using "add rule" with
a handle parameter. Previously, the handle was deleted before being used
for positioning, causing rules to always be appended at the end of the
chain instead of being placed after the specified rule handle.

The fix follows the same pattern used in json_parse_cmd_replace():
- Parse the handle field from JSON
- Convert handle to position for CMD_ADD operations
- Remove the code that was deleting the handle field

With NLM_F_APPEND set (as it always is for add operations), the kernel
interprets position as "add after this handle", which matches the CLI
behavior of "add rule position X".

Before this fix:
  nft -j add rule ... handle 2  --> rule added at end

After this fix:
  nft -j add rule ... handle 2  --> rule added after handle 2

The CLI version (nft add rule ... position X) was already working
correctly.

Tested with:
  # nft add table inet test
  # nft add chain inet test c
  # nft add rule inet test c tcp dport 80 accept
  # nft add rule inet test c tcp dport 443 accept
  # echo '{"nftables":[{"add":{"rule":{"family":"inet","table":"test","chain":"c","handle":2,"expr":[{"match":{"left":{"payload":{"protocol":"tcp","field":"dport"}},"op":"==","right":8080}},{"accept":null}]}}}]}' | nft -j -f -
  # nft -a list table inet test

Result: Rule with port 8080 correctly placed after handle 2 (port 80).

Signed-off-by: Alexandre Knecht <knecht.alexandre@gmail.com>
---
 src/parser_json.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/parser_json.c b/src/parser_json.c
index 7b4f3384..c974a9e2 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -3197,10 +3197,18 @@ static struct cmd *json_parse_cmd_add_rule(struct json_ctx *ctx, json_t *root,
 		return NULL;
 	}
 
+	/* Parse handle and index (similar to json_parse_cmd_replace) */
+	json_unpack(root, "{s:I}", "handle", &h.handle.id);
 	if (!json_unpack(root, "{s:I}", "index", &h.index.id)) {
 		h.index.id++;
 	}
 
+	/* For CMD_ADD, convert handle to position for rule positioning */
+	if ((op == CMD_ADD || op == CMD_CREATE) && h.handle.id) {
+		h.position.id = h.handle.id;
+		h.handle.id = 0;
+	}
+
 	rule = rule_alloc(int_loc, NULL);
 
 	json_unpack(root, "{s:s}", "comment", &comment);
@@ -3226,9 +3234,6 @@ static struct cmd *json_parse_cmd_add_rule(struct json_ctx *ctx, json_t *root,
 		rule_stmt_append(rule, stmt);
 	}
 
-	if (op == CMD_ADD)
-		json_object_del(root, "handle");
-
 	return cmd_alloc(op, obj, &h, int_loc, rule);
 
 err_free_rule:
-- 
2.51.0


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

end of thread, other threads:[~2025-11-06  8:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-29  0:30 [nft PATCH] parser_json: support handle for rule positioning in JSON add rule Alexandre Knecht
2025-10-29 11:18 ` Florian Westphal
2025-10-29 22:45 ` Alexandre Knecht
2025-10-29 22:45   ` [nft PATCH v2] parser_json: support handle for rule positioning in JSON add rule Alexandre Knecht
2025-10-30 10:44     ` Florian Westphal
2025-10-30 11:34       ` Florian Westphal
2025-10-30 20:48         ` Alexandre Knecht
2025-11-02 12:41           ` Florian Westphal
2025-11-04 11:18             ` Phil Sutter
2025-11-06  8:40               ` Alexandre Knecht

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.