Add a helper function to wrap the code that evaluates the list of commands and serialize them into the netlink batch. Add a first user: nft_run_cmd_from_buffer(), there is a follow up patch which will do the same for nft_run_cmd_from_filename(). No functional changes are intended, this comes in preparation to support several list/reset commands in a batch. Signed-off-by: Pablo Neira Ayuso --- src/libnftables.c | 61 ++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/libnftables.c b/src/libnftables.c index 46d9c0df590b..5471ccf6f789 100644 --- a/src/libnftables.c +++ b/src/libnftables.c @@ -618,22 +618,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_eval_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; + int parser_rc; - 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); + if (rc < 0) + goto err; #if HAVE_FUZZER_BUILD if (nft->afl_ctx_stage == NFT_AFL_FUZZER_PARSER) @@ -641,7 +632,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) { if (errno == EPERM) { fprintf(stderr, "%s (you must be root)\n", @@ -655,17 +646,10 @@ 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); - - iface_cache_release(); - if (nft->scanner) { - scanner_destroy(nft); - nft->scanner = NULL; - } - free(nlbuf); + nft_run_cmd_release(nft, msgs, cmds); if (!rc && nft_output_json(&nft->output) && @@ -678,6 +662,35 @@ err: return rc; } +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_eval_run_cmds(nft, &msgs, &cmds, rc); + + free(nlbuf); + iface_cache_release(); + if (nft->scanner) { + scanner_destroy(nft); + nft->scanner = NULL; + } + + return rc; +} + static int load_cmdline_vars(struct nft_ctx *ctx, struct list_head *msgs) { unsigned int bufsize, ret, i, offset = 0; -- 2.47.3