Skip to contentSkip to conversation

support for several list and reset commands

PATCH New Pablo Neira Ayuso 1 participant 6 messages Jun 23 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

v2 cover support for several list and reset commands

Hi,

Currently, it is not possible to run two reset commands in a batch, eg.

nft 'reset rules ip x y; add rule ip x y counter'

this fails because the cache logic gets confused with the requirements.

This approach runs each reset command separately, fetching one cache for each of them.

This series is a follow up to:

https://patchwork.ozlabs.org/project/netfilter-devel/list/?series=499127

to address an issue reported by Phil Sutter.

In this v2, this round simplifies the logic, allowing the combination of reset commands with list and get commands only by now.

Patch #1 to #3 consolidate common code between nft_run_cmd_from_filename() and nft_run_cmd_from_buffer().

Patch #4 add cmd_batch_add() and validate compatibility of reset command with list and get commands.

Patch #5 adds the cmd_batch shim object to collect sublists of commands to deal with several reset commands.

Pablo Neira Ayuso (5):
libnftables: add nft_run_cmd_release() helper and use it
libnftables: split nft_run_cmd_from_buffer() in helper functions
libnftables: use nft_run_cmds() in nft_run_cmd_from_filename()
src: allow reset commands in batch with list and get commands only
libnftables: support for several reset commands

 include/cmd.h      |   9 +++
 src/cmd.c          |  59 ++++++++++++++++++-
 src/libnftables.c  | 144 ++++++++++++++++++++++++++++-----------------
 src/parser_bison.y |  12 +++-
 src/parser_json.c  |   8 +--
 5 files changed, 170 insertions(+), 62 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v2 1/5 libnftables: add nft_run_cmd_release() helper and use it

Wrap the code to release the list of commands in a helper function, then use it to consolidate codebase.

Not functional changes are intended.

---
 src/libnftables.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
src/libnftables.c +17 -12
index db9ee388adde..b4735f330075 100644
@@ -611,11 +611,23 @@ static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs,
 	return 0;
 }
 
+static void nft_run_cmd_release(struct nft_ctx *nft,
+				struct list_head *msgs,
+				struct list_head *cmds)
+{
+	struct cmd *cmd, *next;
+
+	erec_print_list(&nft->output, msgs, nft->debug_mask);
+	list_for_each_entry_safe(cmd, next, cmds, list) {
+		list_del(&cmd->list);
+		cmd_free(cmd);
+	}
+}
+
 EXPORT_SYMBOL(nft_run_cmd_from_buffer);
 int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
 {
 	int rc = -EINVAL, parser_rc;
-	struct cmd *cmd, *next;
 	LIST_HEAD(msgs);
 	LIST_HEAD(cmds);
 	char *nlbuf;
@@ -647,11 +659,8 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
 	if (nft_netlink(nft, &cmds, &msgs) != 0)
 		rc = -1;
 err:
-	erec_print_list(&nft->output, &msgs, nft->debug_mask);
-	list_for_each_entry_safe(cmd, next, &cmds, list) {
-		list_del(&cmd->list);
-		cmd_free(cmd);
-	}
+	nft_run_cmd_release(nft, &msgs, &cmds);
+
 	iface_cache_release();
 	if (nft->scanner) {
 		scanner_destroy(nft);
@@ -740,7 +749,6 @@ static struct error_record *filename_is_useable(struct nft_ctx *nft, const char
 static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
 {
 	struct error_record *erec;
-	struct cmd *cmd, *next;
 	int rc, parser_rc;
 	LIST_HEAD(msgs);
 	LIST_HEAD(cmds);
@@ -779,11 +787,8 @@ static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename
 	if (nft_netlink(nft, &cmds, &msgs) != 0)
 		rc = -1;
 err:
-	erec_print_list(&nft->output, &msgs, nft->debug_mask);
-	list_for_each_entry_safe(cmd, next, &cmds, list) {
-		list_del(&cmd->list);
-		cmd_free(cmd);
-	}
+	nft_run_cmd_release(nft, &msgs, &cmds);
+
 	iface_cache_release();
 	if (nft->scanner) {
 		scanner_destroy(nft);
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v2 2/5 libnftables: split nft_run_cmd_from_buffer() in helper functions

In reply to Pablo Neira Ayuso

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.

---
 src/libnftables.c | 66 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 23 deletions(-)
src/libnftables.c +43 -23
index b4735f330075..659f3a99a158 100644
@@ -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;
 }
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v2 3/5 libnftables: use nft_run_cmds() in nft_run_cmd_from_filename()

In reply to Pablo Neira Ayuso

Update nft_run_cmd_from_filename() to use this new helper function.

Move err: tag to a later stage, which is run by the variables defined via --define option, this should be safe.

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

---
 src/libnftables.c | 28 +++++-----------------------
 1 file changed, 5 insertions(+), 23 deletions(-)
src/libnftables.c +5 -23
index 659f3a99a158..6e29bb019fb7 100644
@@ -769,9 +769,9 @@ static struct error_record *filename_is_useable(struct nft_ctx *nft, const char
 static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
 {
 	struct error_record *erec;
-	int rc, parser_rc;
 	LIST_HEAD(msgs);
 	LIST_HEAD(cmds);
+	int rc;
 
 	erec = filename_is_useable(nft, filename);
 	if (erec) {
@@ -790,30 +790,19 @@ static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename
 	if (rc == -EINVAL)
 		rc = nft_parse_bison_filename(nft, filename, &msgs, &cmds);
 
-	parser_rc = rc;
-
 	if (nft->optimize_flags)
 		nft_optimize(nft, &cmds);
 
-	rc = nft_evaluate(nft, &msgs, &cmds);
-	if (rc < 0)
-		goto err;
-
-	if (parser_rc) {
-		rc = parser_rc;
-		goto err;
-	}
+	rc = nft_run_cmds(nft, &msgs, &cmds, rc);
 
-	if (nft_netlink(nft, &cmds, &msgs) != 0)
-		rc = -1;
-err:
-	nft_run_cmd_release(nft, &msgs, &cmds);
+	nft_finish_cmds(nft, &msgs, &cmds, rc);
 
 	iface_cache_release();
 	if (nft->scanner) {
 		scanner_destroy(nft);
 		nft->scanner = NULL;
 	}
+err:
 	if (!list_empty(&nft->vars_ctx.indesc_list)) {
 		struct input_descriptor *indesc, *next;
 
@@ -824,15 +813,8 @@ err:
 			free(indesc);
 		}
 	}
-	free_const(nft->vars_ctx.buf);
-
-	if (!rc &&
-	    nft_output_json(&nft->output) &&
-	    nft_output_echo(&nft->output))
-		json_print_echo(nft);
 
-	if (rc || nft->check)
-		nft_cache_release(&nft->cache);
+	free_const(nft->vars_ctx.buf);
 
 	scope_release(nft->state->scopes[0]);
 
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v2 4/5 src: allow reset commands in batch with list and get commands only

In reply to Pablo Neira Ayuso

Currently, only single reset command is working properly, but more than one fail because of the existing cache logic. Bail out in case user mixes reset commands.

The reset command is special because it provides no transaction semantics, like the list command. Allowing the use of the reset command in conjunction with other commands require an implicit end of batch / commit command.

Allow a batch that contains reset commands with list and get command by now.

---
 include/cmd.h      |  2 ++
 src/cmd.c          | 37 +++++++++++++++++++++++++++++++++++++
 src/parser_bison.y | 12 ++++++++++--
 src/parser_json.c  |  8 ++++----
 4 files changed, 53 insertions(+), 6 deletions(-)
include/cmd.h +2 -0
index cf7e43bf46ec..a457364aeeeb 100644
@@ -11,4 +11,6 @@ bool nft_cmd_collapse_elems(enum cmd_ops op, struct list_head *cmds,
 
 void nft_cmd_expand(struct cmd *cmd);
 
+int cmd_batch_add(struct cmd *cmd, struct list_head *cmds);
+
 #endif
src/cmd.c +37 -0
index 9d5544f03c32..7ee2176191b1 100644
@@ -489,3 +489,40 @@ void nft_cmd_expand(struct cmd *cmd)
 		break;
 	}
 }
+
+static bool cmd_valid_mix(const struct cmd *cmd, const struct cmd *last_cmd)
+{
+	switch (cmd->op) {
+	case CMD_RESET:
+		if (last_cmd->op == CMD_RESET ||
+		    last_cmd->op == CMD_LIST ||
+		    last_cmd->op == CMD_GET)
+			return true;
+
+		return false;
+	case CMD_LIST:
+	case CMD_GET:
+		return true;
+	default:
+		if (last_cmd->op == CMD_RESET)
+			return false;
+		break;
+	}
+
+	return true;
+}
+
+int cmd_batch_add(struct cmd *cmd, struct list_head *cmds)
+{
+	struct cmd *last_cmd;
+	int ret = 0;
+
+	if (!list_empty(cmds)) {
+		last_cmd = list_last_entry(cmds, struct cmd, list);
+		if (!cmd_valid_mix(cmd, last_cmd))
+			ret = -1;
+	}
+	list_add_tail(&cmd->list, cmds);
+
+	return ret;
+}
src/parser_bison.y +10 -2
index 5a334bf0c499..48151a419096 100644
@@ -1073,7 +1073,11 @@ input			:	/* empty */
 			{
 				if ($2 != NULL) {
 					$2->location = @2;
-					list_add_tail(&$2->list, state->cmds);
+					if (cmd_batch_add($2, state->cmds) < 0) {
+						erec_queue(error(&@2, "unsupported command mix"),
+							   state->msgs);
+						YYERROR;
+					}
 				}
 			}
 			;
@@ -1210,7 +1214,11 @@ line			:	common_block			{ $$ = NULL; }
 				 */
 				if ($1 != NULL) {
 					$1->location = @1;
-					list_add_tail(&$1->list, state->cmds);
+					if (cmd_batch_add($1, state->cmds) < 0) {
+						erec_queue(error(&@2, "unsupported command mix"),
+							   state->msgs);
+						YYERROR;
+					}
 				}
 				$$ = NULL;
 				YYACCEPT;
src/parser_json.c +4 -4
index f04772a022a0..47acc200ad83 100644
@@ -4492,7 +4492,6 @@ static int __json_parse(struct json_ctx *ctx)
 
 	json_array_foreach(tmp, index, value) {
 		/* this is more or less from parser_bison.y:716 */
-		LIST_HEAD(list);
 		struct cmd *cmd;
 		json_t *tmp2;
 
@@ -4522,9 +4521,10 @@ static int __json_parse(struct json_ctx *ctx)
 			return -1;
 		}
 
-		list_add_tail(&cmd->list, &list);
-
-		list_splice_tail(&list, ctx->cmds);
+		if (cmd_batch_add(cmd, ctx->cmds) < 0) {
+			json_error(ctx, "unsupported command mix");
+			return -1;
+		}
 
 		if (nft_output_echo(&ctx->nft->output))
 			json_cmd_assoc_add(value, cmd);
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH v2 5/5 libnftables: support for several reset commands

In reply to Pablo Neira Ayuso

The cache logic woes with either more than one single reset command, e.g. reset table x; reset table y;

One possibility is to handle this from the cache logic itself through the existing netlink dump filter infrastructure, but it looks fragile because this needs to combine the different dump filtering requirements.

The reset command is not integrated into the 2-phase commit protocol in nf_tables, instead it uses the netlink dump path to fetch (and reset) data from the kernel, this needs to be handled in a different way.

This patch updates the parser to create one cmd_batch shim object that stores the usual add/delete cmd object in a batch. The exception is the CMD_RESET command, which have a single cmd_batch object with a single command. The new approach consists of iterating over the list of cmd_batch shims to handle the list of commands sequentially. This new shim object is released when running the command list.

The new batch structure is a list of lists:

         .-----------.
         | cmd_batch |-> reset cmd
         `-----------'
               |
         .-----------.
         | cmd_batch |-> reset cmd
         `-----------'
               |
               .
               |
         .-----------.
         | cmd_batch |-> reset cmd
         `-----------'

or, what happens more often:

         .-----------.
         | cmd_batch |-> add cmd -> add cmd -> ... -> add cmd
         `-----------'

This patch updates the core function to process each batch in the command list. Every reset command in a batch is run as a singleton command through nft_run_cmds() to fetch a cache that suits the needs of this command.

The previous patch:

("src: bail out if reset commands are mixed with other commands")

ensures that reset commands are only mixed with list and get commands by now, for simplicity.

Fixes dbff26bfba83 ("cache: consolidate reset command")
---
 include/cmd.h     |  7 +++++++
 src/cmd.c         | 26 +++++++++++++++++++++++---
 src/libnftables.c | 39 +++++++++++++++++++++++++++++++++------
 3 files changed, 63 insertions(+), 9 deletions(-)
include/cmd.h +7 -0
index a457364aeeeb..4e1248ced739 100644
@@ -1,6 +1,8 @@
 #ifndef _NFT_CMD_H_
 #define _NFT_CMD_H_
 
+#include <list.h>
+
 void cmd_add_loc(struct cmd *cmd, const struct nlmsghdr *nlh, const struct location *loc);
 struct mnl_err;
 void nft_cmd_error(struct netlink_ctx *ctx, struct cmd *cmd,
@@ -11,6 +13,11 @@ bool nft_cmd_collapse_elems(enum cmd_ops op, struct list_head *cmds,
 
 void nft_cmd_expand(struct cmd *cmd);
 
+struct cmd_batch {
+	struct list_head	list;
+	struct list_head        sublist;
+};
+
 int cmd_batch_add(struct cmd *cmd, struct list_head *cmds);
 
 #endif
src/cmd.c +23 -3
index 7ee2176191b1..407217bfaae6 100644
@@ -384,6 +384,7 @@ static void nft_cmd_expand_chain(struct chain *chain, struct list_head *new_cmds
 bool nft_cmd_collapse_elems(enum cmd_ops op, struct list_head *cmds,
 			    struct handle *handle, struct expr *init)
 {
+	struct cmd_batch *cmd_batch;
 	struct cmd *last_cmd;
 
 	if (list_empty(cmds))
@@ -392,7 +393,9 @@ bool nft_cmd_collapse_elems(enum cmd_ops op, struct list_head *cmds,
 	if (init->etype == EXPR_VARIABLE)
 		return false;
 
-	last_cmd = list_last_entry(cmds, struct cmd, list);
+	cmd_batch = list_last_entry(cmds, struct cmd_batch, list);
+
+	last_cmd = list_last_entry(&cmd_batch->sublist, struct cmd, list);
 	if (last_cmd->op != op ||
 	    last_cmd->obj != CMD_OBJ_ELEMENTS ||
 	    last_cmd->expr->etype == EXPR_VARIABLE ||
@@ -512,17 +515,34 @@ static bool cmd_valid_mix(const struct cmd *cmd, const struct cmd *last_cmd)
 	return true;
 }
 
+static void __cmd_batch_add(struct cmd *cmd, struct list_head *cmds)
+{
+	struct cmd_batch *cmd_batch;
+
+	cmd_batch = xmalloc(sizeof(struct cmd_batch));
+	init_list_head(&cmd_batch->sublist);
+	list_add_tail(&cmd->list, &cmd_batch->sublist);
+	list_add_tail(&cmd_batch->list, cmds);
+}
+
 int cmd_batch_add(struct cmd *cmd, struct list_head *cmds)
 {
+	struct cmd_batch *cmd_batch = NULL;
 	struct cmd *last_cmd;
 	int ret = 0;
 
 	if (!list_empty(cmds)) {
-		last_cmd = list_last_entry(cmds, struct cmd, list);
+		cmd_batch = list_last_entry(cmds, struct cmd_batch, list)
+;
+		last_cmd = list_last_entry(&cmd_batch->sublist, struct cmd, list);
 		if (!cmd_valid_mix(cmd, last_cmd))
 			ret = -1;
 	}
-	list_add_tail(&cmd->list, cmds);
+
+	if (!cmd_batch || cmd->op == CMD_RESET)
+		__cmd_batch_add(cmd, cmds);
+	else
+		list_add_tail(&cmd->list, &cmd_batch->sublist);
 
 	return ret;
 }
src/libnftables.c +33 -6
index 6e29bb019fb7..61695cba0354 100644
@@ -667,10 +667,25 @@ static void nft_finish_cmds(struct nft_ctx *nft, struct list_head *msgs,
 		nft_cache_release(&nft->cache);
 }
 
+static void cmds_batch_next(struct list_head *cmds,
+			    struct list_head *cmds_sublist)
+{
+	struct cmd_batch *cmd_batch;
+
+	if (list_empty(cmds))
+		return;
+
+	cmd_batch = list_first_entry(cmds, struct cmd_batch, list);
+	list_splice_init(&cmd_batch->sublist, cmds_sublist);
+	list_del(&cmd_batch->list);
+	free(cmd_batch);
+}
 
 EXPORT_SYMBOL(nft_run_cmd_from_buffer);
 int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
 {
+	LIST_HEAD(cmds_sublist);
+	LIST_HEAD(cmds_done);
 	int rc = -EINVAL;
 	LIST_HEAD(msgs);
 	LIST_HEAD(cmds);
@@ -685,9 +700,14 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
 		rc = nft_parse_bison_buffer(nft, nlbuf, &msgs, &cmds,
 					    &indesc_cmdline);
 
-	rc = nft_run_cmds(nft, &msgs, &cmds, rc);
+	cmds_batch_next(&cmds, &cmds_sublist);
+	while (!list_empty(&cmds_sublist)) {
+		rc = nft_run_cmds(nft, &msgs, &cmds_sublist, rc);
+		list_splice_tail_init(&cmds_sublist, &cmds_done);
+		cmds_batch_next(&cmds, &cmds_sublist);
+	}
 
-	nft_finish_cmds(nft, &msgs, &cmds, rc);
+	nft_finish_cmds(nft, &msgs, &cmds_done, rc);
 
 	free(nlbuf);
 	iface_cache_release();
@@ -769,6 +789,8 @@ static struct error_record *filename_is_useable(struct nft_ctx *nft, const char
 static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
 {
 	struct error_record *erec;
+	LIST_HEAD(cmds_sublist);
+	LIST_HEAD(cmds_done);
 	LIST_HEAD(msgs);
 	LIST_HEAD(cmds);
 	int rc;
@@ -790,12 +812,17 @@ static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename
 	if (rc == -EINVAL)
 		rc = nft_parse_bison_filename(nft, filename, &msgs, &cmds);
 
-	if (nft->optimize_flags)
-		nft_optimize(nft, &cmds);
+	cmds_batch_next(&cmds, &cmds_sublist);
+	while (!list_empty(&cmds_sublist)) {
+		if (nft->optimize_flags)
+			nft_optimize(nft, &cmds_sublist);
 
-	rc = nft_run_cmds(nft, &msgs, &cmds, rc);
+		rc = nft_run_cmds(nft, &msgs, &cmds_sublist, rc);
+		list_splice_tail_init(&cmds_sublist, &cmds_done);
+		cmds_batch_next(&cmds, &cmds_sublist);
+	}
 
-	nft_finish_cmds(nft, &msgs, &cmds, rc);
+	nft_finish_cmds(nft, &msgs, &cmds_done, rc);
 
 	iface_cache_release();
 	if (nft->scanner) {
Signature · 1 line
2.47.3