Linux Netfilter development
 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,v2 2/5] libnftables: split nft_run_cmd_from_buffer() in helper functions
Date: Tue, 23 Jun 2026 19:21:25 +0200	[thread overview]
Message-ID: <20260623172128.401234-3-pablo@netfilter.org> (raw)
In-Reply-To: <20260623172128.401234-1-pablo@netfilter.org>

Add a helper function called nft_run_cmds() to wrap the code that
evaluates the list of commands and serialize them into the netlink
batch.

Add another helper function called nft_finish_cmds() to print the error,
release them, as well as displaying the json output and releasing the
cache.

Add a first user: nft_run_cmd_from_buffer(), there is a follow up patch
which updates nft_run_cmd_from_filename() to use these helper functions.

No functional changes are intended, this comes in preparation to
support several reset commands in a batch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/libnftables.c | 66 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 23 deletions(-)

diff --git a/src/libnftables.c b/src/libnftables.c
index b4735f330075..659f3a99a158 100644
--- a/src/libnftables.c
+++ b/src/libnftables.c
@@ -624,22 +624,13 @@ static void nft_run_cmd_release(struct nft_ctx *nft,
 	}
 }
 
-EXPORT_SYMBOL(nft_run_cmd_from_buffer);
-int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
+static int nft_run_cmds(struct nft_ctx *nft, struct list_head *msgs,
+			struct list_head *cmds, int rc)
 {
-	int rc = -EINVAL, parser_rc;
-	LIST_HEAD(msgs);
-	LIST_HEAD(cmds);
-	char *nlbuf;
-
-	nlbuf = xzalloc(strlen(buf) + 2);
-	sprintf(nlbuf, "%s\n", buf);
+	int parser_rc;
 
-	if (nft_output_json(&nft->output) || nft_input_json(&nft->input))
-		rc = nft_parse_json_buffer(nft, nlbuf, &msgs, &cmds);
-	if (rc == -EINVAL)
-		rc = nft_parse_bison_buffer(nft, nlbuf, &msgs, &cmds,
-					    &indesc_cmdline);
+	if (rc < 0)
+		goto err;
 
 #if HAVE_FUZZER_BUILD
 	if (nft->afl_ctx_stage == NFT_AFL_FUZZER_PARSER)
@@ -647,7 +638,7 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
 #endif
 	parser_rc = rc;
 
-	rc = nft_evaluate(nft, &msgs, &cmds);
+	rc = nft_evaluate(nft, msgs, cmds);
 	if (rc < 0)
 		goto err;
 
@@ -656,17 +647,16 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
 		goto err;
 	}
 
-	if (nft_netlink(nft, &cmds, &msgs) != 0)
+	if (nft_netlink(nft, cmds, msgs) != 0)
 		rc = -1;
 err:
-	nft_run_cmd_release(nft, &msgs, &cmds);
+	return rc;
+}
 
-	iface_cache_release();
-	if (nft->scanner) {
-		scanner_destroy(nft);
-		nft->scanner = NULL;
-	}
-	free(nlbuf);
+static void nft_finish_cmds(struct nft_ctx *nft, struct list_head *msgs,
+			    struct list_head *cmds, int rc)
+{
+	nft_run_cmd_release(nft, msgs, cmds);
 
 	if (!rc &&
 	    nft_output_json(&nft->output) &&
@@ -675,6 +665,36 @@ err:
 
 	if (rc || nft->check)
 		nft_cache_release(&nft->cache);
+}
+
+
+EXPORT_SYMBOL(nft_run_cmd_from_buffer);
+int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
+{
+	int rc = -EINVAL;
+	LIST_HEAD(msgs);
+	LIST_HEAD(cmds);
+	char *nlbuf;
+
+	nlbuf = xzalloc(strlen(buf) + 2);
+	sprintf(nlbuf, "%s\n", buf);
+
+	if (nft_output_json(&nft->output) || nft_input_json(&nft->input))
+		rc = nft_parse_json_buffer(nft, nlbuf, &msgs, &cmds);
+	if (rc == -EINVAL)
+		rc = nft_parse_bison_buffer(nft, nlbuf, &msgs, &cmds,
+					    &indesc_cmdline);
+
+	rc = nft_run_cmds(nft, &msgs, &cmds, rc);
+
+	nft_finish_cmds(nft, &msgs, &cmds, rc);
+
+	free(nlbuf);
+	iface_cache_release();
+	if (nft->scanner) {
+		scanner_destroy(nft);
+		nft->scanner = NULL;
+	}
 
 	return rc;
 }
-- 
2.47.3


  parent reply	other threads:[~2026-06-23 17:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23 17:21 [PATCH nft,v2 0/5] support for several list and reset commands Pablo Neira Ayuso
2026-06-23 17:21 ` [PATCH nft,v2 1/5] libnftables: add nft_run_cmd_release() helper and use it Pablo Neira Ayuso
2026-06-23 17:21 ` Pablo Neira Ayuso [this message]
2026-06-23 17:21 ` [PATCH nft,v2 3/5] libnftables: use nft_run_cmds() in nft_run_cmd_from_filename() Pablo Neira Ayuso
2026-06-23 17:21 ` [PATCH nft,v2 4/5] src: allow reset commands in batch with list and get commands only Pablo Neira Ayuso
2026-06-23 17:21 ` [PATCH nft,v2 5/5] libnftables: support for several reset commands Pablo Neira Ayuso

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=20260623172128.401234-3-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox