src: add import command
PATCH v3 2/2 src: add import command
A basic way to test this new functionality is:
% cat file.json | nft import json
where the file.json is a ruleset exported in json format.
This new operation allows to import ruleset in json and xml and to make incremental changes using the new parse functions of libnftnl.
Based in a patch of Arturo Borrero.
---
[changes in v3]
* Defined the import enums before to export enums.
* Removed unnecessary NULL checks
* Renamed some functions name and refactored the code to make it more clear
include/rule.h | 12 ++ src/evaluate.c | 1 + src/parser_bison.y | 20 +++- src/rule.c | 337 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/scanner.l | 1 + 5 files changed, 368 insertions(+), 3 deletions(-)
include/rule.h +12 -0
@@ -221,6 +221,7 @@ extern void set_print_plain(const struct set *s);* @CMD_LIST: list container * @CMD_FLUSH: flush container * @CMD_RENAME: rename object +* @CMD_IMPORT: import a ruleset in a given format * @CMD_EXPORT: export the ruleset in a given format * @CMD_MONITOR: event listener * @CMD_DESCRIBE: describe an expression
@@ -234,6 +235,7 @@ enum cmd_ops {CMD_LIST ,CMD_FLUSH ,CMD_RENAME , +CMD_IMPORT ,CMD_EXPORT ,CMD_MONITOR , CMD_DESCRIBE,
@@ -252,6 +254,7 @@ enum cmd_ops {* @CMD_OBJ_RULESET: ruleset * @CMD_OBJ_EXPR: expression * @CMD_OBJ_MONITOR: monitor +* @CMD_OBJ_IMPORT: import * @CMD_OBJ_EXPORT: export */ enum cmd_obj {
@@ -265,6 +268,7 @@ enum cmd_obj {CMD_OBJ_RULESET ,CMD_OBJ_EXPR ,CMD_OBJ_MONITOR , +CMD_OBJ_IMPORT , CMD_OBJ_EXPORT, };
@@ -275,6 +279,13 @@ struct export {struct export *export_alloc (uint32_t format);void export_free (struct export *e); +struct import { +uint32_t format; +}; + +struct import *import_alloc (uint32_t format); +void import_free (struct import *i); +enum {CMD_MONITOR_OBJ_ANY ,CMD_MONITOR_OBJ_TABLES ,
@@ -325,6 +336,7 @@ struct cmd {struct table *table;struct monitor *monitor;struct export *export; +struct import *import; };const void *arg; };
src/evaluate.c +1 -0
@@ -1966,6 +1966,7 @@ int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)case CMD_LIST :case CMD_FLUSH :case CMD_RENAME : +case CMD_IMPORT :case CMD_EXPORT :case CMD_DESCRIBE :return 0 ;
src/parser_bison.y +17 -3
@@ -188,6 +188,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token FLUSH "flush" %token RENAME "rename" %token DESCRIBE "describe" +%token IMPORT "import" %token EXPORT "export" %token MONITOR "monitor"
@@ -402,8 +403,8 @@ static void location_update(struct location *loc, struct location *rhs, int n) %type <cmd> line %destructor { cmd_free($$); } line -%type <cmd> base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd describe_cmd -%destructor { cmd_free($$); } base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd describe_cmd +%type <cmd> base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd describe_cmd import_cmd +%destructor { cmd_free($$); } base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd monitor_cmd describe_cmd import_cmd %type <handle> table_spec tables_spec chain_spec chain_identifier ruleid_spec ruleset_spec %destructor { handle_free(&$$); } table_spec tables_spec chain_spec chain_identifier ruleid_spec ruleset_spec
@@ -535,7 +536,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) %destructor { expr_free($$); } ct_expr %type <val> ct_key -%type <val> export_format +%type <val> export_format import_format %type <string> monitor_event %destructor { xfree($$); } monitor_event %type <val> monitor_object monitor_format
@@ -637,6 +638,7 @@ base_cmd : /* empty */ add_cmd { $$ = $1; } | LIST list_cmd { $$ = $2; } | FLUSH flush_cmd { $$ = $2; } | RENAME rename_cmd { $$ = $2; } + | IMPORT import_cmd { $$ = $2; } | EXPORT export_cmd { $$ = $2; } | MONITOR monitor_cmd { $$ = $2; } | DESCRIBE describe_cmd { $$ = $2; }
@@ -801,6 +803,14 @@ rename_cmd : CHAIN chain_spec identifier } ; +import_cmd : import_format + { + struct handle h = { .family = NFPROTO_UNSPEC }; + struct import *import = import_alloc($1); + $$ = cmd_alloc(CMD_IMPORT, CMD_OBJ_IMPORT, &h, &@$, import); + } + ; + export_cmd : export_format { struct handle h = { .family = NFPROTO_UNSPEC };
@@ -834,6 +844,10 @@ monitor_format : /* empty */ { $$ = NFT_OUTPUT_DEFAULT; } | export_format ; +import_format : XML { $$ = NFT_PARSE_XML; } + | JSON { $$ = NFT_PARSE_JSON; } + ; + export_format : XML { $$ = NFT_OUTPUT_XML; } | JSON { $$ = NFT_OUTPUT_JSON; } ;
src/rule.c +337 -0
367 lines not on this page · raw message
src/scanner.l +1 -0
@@ -261,6 +261,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "list" { return LIST; } "flush" { return FLUSH; } "rename" { return RENAME; } +"import" { return IMPORT; } "export" { return EXPORT; } "monitor" { return MONITOR; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerQuoted 70 lines · Alvaro Neira Ayuso
src/rule.c +41
-0@@ -20,6 +20,7 @@#include <rule.h> #include <utils.h> #include <netlink.h> +#include <mnl.h> #include <libnftnl/common.h> #include <libnftnl/ruleset.h> @@ -555,6 +556,21 @@ void export_free(struct export *e)xfree (e); } +struct import *import_alloc (uint32_t format) +{ +struct import *import; + + import =xmalloc (sizeof (struct import )); + import->format = format; + +return import; +} + +void import_free (struct import *i) +{ +xfree (i); +} +struct monitor *monitor_alloc (uint32_t format,uint32_t type,const char *event) {struct monitor *mon;@@ -599,6 +615,9 @@ void cmd_free(struct cmd *cmd)case CMD_OBJ_MONITOR :monitor_free (cmd->monitor);break ; +case CMD_OBJ_IMPORT : +import_free (cmd->import); +break ;case CMD_OBJ_EXPORT :export_free (cmd->export);break ;@@ -1006,6 +1025,322 @@ static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd)return 0 ; } +struct ruleset_parse { +struct netlink_ctx *nl_ctx; +struct cmd *cmd; +}; + +static int ruleset_parse_setelems (const struct nft_parse_ctx *ctx) +{ +const struct ruleset_parse *rp; +struct nft_set *set; +uint32_t cmd; +int ret =-1 ; + + set =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_SET ); + rp =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_DATA ); + + cmd =nft_ruleset_ctx_get_u32 (ctx,NFT_RULESET_CTX_CMD ); +switch (cmd) { +case NFT_CMD_ADD : + ret =mnl_nft_setelem_batch_add (set,0 , rp->nl_ctx->seqnum); +break ; +case NFT_CMD_DELETE : + ret =mnl_nft_setelem_batch_del (set,0 , rp->nl_ctx->seqnum);
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1059
Quoted 10 lines
src/rule.c +10
-0+break ; +default : + errno =EOPNOTSUPP ; +break ; + } + +if (ret <0 ) +netlink_io_error (rp->nl_ctx, &rp->cmd->location, +"Could not import set_elems: %s" , +strerror (errno));
I think rp->cmd->location is unset, so this will crash. Could you validate this by forcing an error to make sure it works?
Quoted 76 lines · Pablo Neira Ayuso
Quoted 75 lines · Alvaro Neira Ayuso
src/rule.c +46
-0@@ -20,6 +20,7 @@#include <rule.h> #include <utils.h> #include <netlink.h> +#include <mnl.h> #include <libnftnl/common.h> #include <libnftnl/ruleset.h> @@ -555,6 +556,21 @@ void export_free(struct export *e)xfree (e); } +struct import *import_alloc (uint32_t format) +{ +struct import *import; + + import =xmalloc (sizeof (struct import )); + import->format = format; + +return import; +} + +void import_free (struct import *i) +{ +xfree (i); +} +struct monitor *monitor_alloc (uint32_t format,uint32_t type,const char *event) {struct monitor *mon;@@ -599,6 +615,9 @@ void cmd_free(struct cmd *cmd)case CMD_OBJ_MONITOR :monitor_free (cmd->monitor);break ; +case CMD_OBJ_IMPORT : +import_free (cmd->import); +break ;case CMD_OBJ_EXPORT :export_free (cmd->export);break ;@@ -1006,6 +1025,322 @@ static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd)return 0 ; } +struct ruleset_parse { +struct netlink_ctx *nl_ctx; +struct cmd *cmd; +}; + +static int ruleset_parse_setelems (const struct nft_parse_ctx *ctx) +{ +const struct ruleset_parse *rp; +struct nft_set *set; +uint32_t cmd; +int ret =-1 ; + + set =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_SET ); + rp =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_DATA ); + + cmd =nft_ruleset_ctx_get_u32 (ctx,NFT_RULESET_CTX_CMD ); +switch (cmd) { +case NFT_CMD_ADD : + ret =mnl_nft_setelem_batch_add (set,0 , rp->nl_ctx->seqnum); +break ; +case NFT_CMD_DELETE : + ret =mnl_nft_setelem_batch_del (set,0 , rp->nl_ctx->seqnum); +break ; +default : + errno =EOPNOTSUPP ; +break ; + }
Quoted 8 lines
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1059
Quoted 5 lines
src/rule.c +5
-0+ +if (ret <0 ) +netlink_io_error (rp->nl_ctx, &rp->cmd->location, +"Could not import set_elems: %s" , +strerror (errno));I think
rp->cmd->locationis unset, so this will crash. Could you validate this by forcing an error to make sure it works?
It's not unset. If we have an error, the location is in the import command. For example:
Error: Could not import set_elems: Invalid argument
import json
^^^^^^^^^^^
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerQuoted 86 lines · Álvaro Neira Ayuso
Quoted 76 lines · Pablo Neira Ayuso
Quoted 75 lines · Alvaro Neira Ayuso
src/rule.c +46
-0@@ -20,6 +20,7 @@#include <rule.h> #include <utils.h> #include <netlink.h> +#include <mnl.h> #include <libnftnl/common.h> #include <libnftnl/ruleset.h> @@ -555,6 +556,21 @@ void export_free(struct export *e)xfree (e); } +struct import *import_alloc (uint32_t format) +{ +struct import *import; + + import =xmalloc (sizeof (struct import )); + import->format = format; + +return import; +} + +void import_free (struct import *i) +{ +xfree (i); +} +struct monitor *monitor_alloc (uint32_t format,uint32_t type,const char *event) {struct monitor *mon;@@ -599,6 +615,9 @@ void cmd_free(struct cmd *cmd)case CMD_OBJ_MONITOR :monitor_free (cmd->monitor);break ; +case CMD_OBJ_IMPORT : +import_free (cmd->import); +break ;case CMD_OBJ_EXPORT :export_free (cmd->export);break ;@@ -1006,6 +1025,322 @@ static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd)return 0 ; } +struct ruleset_parse { +struct netlink_ctx *nl_ctx; +struct cmd *cmd; +}; + +static int ruleset_parse_setelems (const struct nft_parse_ctx *ctx) +{ +const struct ruleset_parse *rp; +struct nft_set *set; +uint32_t cmd; +int ret =-1 ; + + set =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_SET ); + rp =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_DATA ); + + cmd =nft_ruleset_ctx_get_u32 (ctx,NFT_RULESET_CTX_CMD ); +switch (cmd) { +case NFT_CMD_ADD : + ret =mnl_nft_setelem_batch_add (set,0 , rp->nl_ctx->seqnum); +break ; +case NFT_CMD_DELETE : + ret =mnl_nft_setelem_batch_del (set,0 , rp->nl_ctx->seqnum); +break ; +default : + errno =EOPNOTSUPP ; +break ; + }Quoted 8 lines
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1059
Quoted 5 lines
src/rule.c +5
-0+ +if (ret <0 ) +netlink_io_error (rp->nl_ctx, &rp->cmd->location, +"Could not import set_elems: %s" , +strerror (errno));I think
rp->cmd->locationis unset, so this will crash. Could you validate this by forcing an error to make sure it works?
Quoted 6 lines
It's not unset. If we have an error, the location is in the import command. For example:
Error: Could not import
set_elems: Invalid argument import json
^^^^^^^^^^^
Good, thanks.
BTW, please don't use developer jargon in the error messages, you better say "Could not import set elements" instead of "set_elems". --
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
include/rule.h struct export { · line 287
Quoted 10 lines · Alvaro Neira Ayuso
include/rule.h +6
-0@@ -275,6 +279,13 @@ struct export {struct export *export_alloc (uint32_t format);void export_free (struct export *e); +struct import { +uint32_t format; +}; + +struct import *import_alloc (uint32_t format); +void import_free (struct import *i);
How about a common struct for both commands? "format", "import_export",
...
src/parser_bison.y rename_cmd : CHAIN chain_spec identifier · line 806
Quoted 1 line
src/parser_bison.y +1
-0+import_cmd : import_format
Same here, please change export_format to something common.
Quoted 11 lines
src/rule.c +11
-0+static int ruleset_parse_setelems (const struct nft_parse_ctx *ctx) +{ +const struct ruleset_parse *rp; +struct nft_set *set; +uint32_t cmd; +int ret =-1 ; + + set =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_SET ); + rp =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_DATA ); + + cmd =nft_ruleset_ctx_get_u32 (ctx,NFT_RULESET_CTX_CMD );
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1053
Quoted 10 lines
src/rule.c +10
-0+switch (cmd) { +case NFT_CMD_ADD : + ret =mnl_nft_setelem_batch_add (set,0 , rp->nl_ctx->seqnum); +break ; +case NFT_CMD_DELETE : + ret =mnl_nft_setelem_batch_del (set,0 , rp->nl_ctx->seqnum); +break ; +default : + errno =EOPNOTSUPP ; +break ;
This would be a BUG, no? Same question for all similar cases.
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1328
Quoted 7 lines
src/rule.c +7
-0+static int do_command_import (struct netlink_ctx *ctx,struct cmd *cmd) +{ +int ret; +struct nft_parse_err *err; +struct ruleset_parse rp = { + .nl_ctx = ctx, + .cmd = cmd
Please align, makes it easier to read.
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1338
Quoted 4 lines
src/rule.c +4
-0+ ret =nft_ruleset_parse_file_cb (cmd->import->format, stdin, err, &rp, + ruleset_parse_cb); +if (ret <0 ) +nft_parse_perror ("unable to import. Parsing failed" , err);
I'd suggest a ": parsing failed", makes it clear that its only a single error, not two.
Quoted 5 lines
src/rule.c +5
-0+ +nft_parse_err_free (err); +return ret; +} +
Quoted 5 lines · Patrick McHardy
Quoted 4 lines · Alvaro Neira Ayuso
include/rule.h +0
-0@@ -275,6 +279,13 @@ struct export {struct export *export_alloc (uint32_t format);void export_free (struct export *e);
Quoted 10 lines
include/rule.h struct export { · line 287
Quoted 6 lines
include/rule.h +6
-0+struct import { +uint32_t format; +}; + +struct import *import_alloc (uint32_t format); +void import_free (struct import *i);How about a common struct for both commands? "format", "
import_export",
...
Nice idea Patrick. I'm going to do it.
Quoted 19 lines
src/parser_bison.y rename_cmd : CHAIN chain_spec identifier · line 806
Quoted 1 line
src/parser_bison.y +1
-0+import_cmd : import_formatSame here, please change
export_formatto something common.Quoted 15 lines
src/rule.c +15
-0+static int ruleset_parse_setelems (const struct nft_parse_ctx *ctx) +{ +const struct ruleset_parse *rp; +struct nft_set *set; +uint32_t cmd; +int ret =-1 ; + + set =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_SET ); + rp =nft_ruleset_ctx_get (ctx,NFT_RULESET_CTX_DATA ); + + cmd =nft_ruleset_ctx_get_u32 (ctx,NFT_RULESET_CTX_CMD ); +switch (cmd) { +case NFT_CMD_ADD : + ret =mnl_nft_setelem_batch_add (set,0 , rp->nl_ctx->seqnum); +break ;
Quoted 8 lines
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1053
Quoted 6 lines
src/rule.c +6
-0+case NFT_CMD_DELETE : + ret =mnl_nft_setelem_batch_del (set,0 , rp->nl_ctx->seqnum); +break ; +default : + errno =EOPNOTSUPP ; +break ;This would be a BUG, no? Same question for all similar cases.
I don't think so. If the user are in another kernel that one operation is not supported, we will say that it's a bug and I think it's better to say only that the operation is not supported. Maybe my point of view is wrong, I'm opened to change it if you think that it's better to show a bug message.
Quoted 10 lines
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1328
Quoted 8 lines
src/rule.c +7
-0+static int do_command_import (struct netlink_ctx *ctx,struct cmd *cmd) +{ +int ret; +struct nft_parse_err *err; +struct ruleset_parse rp = { + .nl_ctx = ctx, + .cmd = cmdPlease align, makes it easier to read.
Catched.
Quoted 8 lines
src/rule.c static int do_command_describe(struct netlink_ctx *ctx, struct cmd *cmd) · line 1338
Quoted 5 lines
src/rule.c +4
-0+ ret =nft_ruleset_parse_file_cb (cmd->import->format, stdin, err, &rp, + ruleset_parse_cb); +if (ret <0 ) +nft_parse_perror ("unable to import. Parsing failed" , err);I'd suggest a ": parsing failed", makes it clear that its only a single error, not two.
Perfect, I'm going to change it too.
Thanks for the review to Pablo and Patrick. I'm going to work in the changes. --
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html