This patch adds the following operation:
PATCH v3 1/1 This patch adds the following operation:
:~# nft export <xml|json>
The XML/JSON output is provided raw by libnftnl, thus without format.
In case of XML, you can give format with the `xmllint' tool from libxml2-tools:
:~# nft list ruleset xml | xmllint --format -
In case of JSON, you can use `json_pp' from perl standar package:
:~# nft list ruleset json | json_pp
A format field is added in struct cmd, and it will be reused in the import operation.
---
v2: address comments by Patrick: use CMD_OBJ_RULESET, move some includes, etc.
v3: rebase against nft's next-3.14 branch.
include/mnl.h | 2 ++ include/netlink.h | 3 ++ include/rule.h | 6 +++++ src/evaluate.c | 1 + src/mnl.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/netlink.c | 17 ++++++++++++++ src/parser.y | 23 +++++++++++++++++-- src/rule.c | 20 +++++++++++++++++ src/scanner.l | 4 +++ 9 files changed, 137 insertions(+), 3 deletions(-)
include/mnl.h +2 -0
@@ -65,4 +65,6 @@ int mnl_nft_setelem_delete(struct mnl_socket *nf_sock, struct nft_set *nls, unsigned int flags);int mnl_nft_setelem_get (struct mnl_socket *nf_sock,struct nft_set *nls); +struct nft_ruleset *mnl_nft_ruleset_dump (struct mnl_socket *nf_sock, +uint32_t family);#endif /* _NFTABLES_MNL_H_ */
include/netlink.h +3 -0
@@ -136,4 +136,7 @@ extern int netlink_batch_send(struct list_head *err_list);extern int netlink_io_error (struct netlink_ctx *ctx,const struct location *loc,const char *fmt, ...); +extern struct nft_ruleset *netlink_dump_ruleset (struct netlink_ctx *ctx, +const struct handle *h, +const struct location *loc);#endif /* NFTABLES_NETLINK_H */
include/rule.h +6 -0
@@ -205,6 +205,7 @@ extern void set_print(const struct set *set);* @CMD_LIST: list container * @CMD_FLUSH: flush container * @CMD_RENAME: rename object +* @CMD_EXPORT: export the ruleset in a given format */ enum cmd_ops {CMD_INVALID ,
@@ -215,6 +216,7 @@ enum cmd_ops {CMD_LIST ,CMD_FLUSH ,CMD_RENAME , + CMD_EXPORT, };/**
@@ -227,6 +229,7 @@ enum cmd_ops {* @CMD_OBJ_RULE: rule * @CMD_OBJ_CHAIN: chain * @CMD_OBJ_TABLE: table +* @CMD_OBJ_RULESET: ruleset */ enum cmd_obj {CMD_OBJ_INVALID ,
@@ -236,6 +239,7 @@ enum cmd_obj {CMD_OBJ_RULE ,CMD_OBJ_CHAIN ,CMD_OBJ_TABLE , + CMD_OBJ_RULESET, };/**
@@ -249,6 +253,7 @@ enum cmd_obj {* @seqnum: sequence number to match netlink errors * @union: object * @arg: argument data +* @format: info about the export/import format */ struct cmd {struct list_head list;
@@ -266,6 +271,7 @@ struct cmd {struct table *table; };const void *arg; +uint32_t format; }; extern struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj,
src/evaluate.c +1 -0
@@ -1405,6 +1405,7 @@ static int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)case CMD_LIST :case CMD_FLUSH :case CMD_RENAME : +case CMD_EXPORT :return 0 ;default :BUG ("invalid command operation %u\n" , cmd->op);
src/mnl.c +63 -1
79 lines not on this page · raw message
src/netlink.c +17 -0
@@ -13,12 +13,15 @@#include <fcntl.h> #include <errno.h> #include <libmnl/libmnl.h> +#include <netinet/in.h> +#include <arpa/inet.h> #include <libnftnl/table.h> #include <libnftnl/chain.h> #include <libnftnl/expr.h> #include <libnftnl/set.h> #include <linux/netfilter/nf_tables.h> +#include <linux/netfilter.h> #include <nftables.h> #include <netlink.h>
@@ -1050,3 +1053,17 @@ int netlink_batch_send(struct list_head *err_list) {return mnl_batch_talk (nf_sock, err_list); } + +struct nft_ruleset *netlink_dump_ruleset (struct netlink_ctx *ctx, +const struct handle *h, +const struct location *loc) +{ +struct nft_ruleset *rs; + + rs =mnl_nft_ruleset_dump (nf_sock, h->family); +if (rs ==NULL ) +netlink_io_error (ctx, loc,"Could not receive ruleset: %s" , +strerror (errno)); + +return rs; +}
src/parser.y +21 -2
@@ -18,6 +18,7 @@ #include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h> #include <linux/netfilter/nf_conntrack_tuple_common.h> +#include <libnftnl/common.h> #include <rule.h> #include <statement.h>
@@ -176,6 +177,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token FLUSH "flush" %token RENAME "rename" %token DESCRIBE "describe" +%token EXPORT "export" %token ACCEPT "accept" %token DROP "drop"
@@ -346,14 +348,17 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token POSITION "position" +%token XML "xml" +%token JSON "json" + %type <string> identifier string %destructor { xfree($$); } identifier string %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 -%destructor { cmd_free($$); } base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd +%type <cmd> base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd +%destructor { cmd_free($$); } base_cmd add_cmd create_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd %type <handle> table_spec tables_spec chain_spec chain_identifier ruleid_spec %destructor { handle_free(&$$); } table_spec tables_spec chain_spec chain_identifier ruleid_spec
@@ -476,6 +481,8 @@ 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 + %% input : /* empty */
@@ -544,6 +551,7 @@ base_cmd : /* empty */ add_cmd { $$ = $1; } | LIST list_cmd { $$ = $2; } | FLUSH flush_cmd { $$ = $2; } | RENAME rename_cmd { $$ = $2; } + | EXPORT export_cmd { $$ = $2; } | DESCRIBE primary_expr { expr_describe($2);
@@ -703,6 +711,14 @@ rename_cmd : CHAIN chain_spec identifier } ; +export_cmd : export_format + { + struct handle h = { .family = NFPROTO_UNSPEC }; + $$ = cmd_alloc(CMD_EXPORT, CMD_OBJ_RULESET, &h, &@$, NULL); + $$->format = $1; + } + ; + table_block_alloc : /* empty */ { $$ = table_alloc();
@@ -1914,4 +1930,7 @@ mh_hdr_field : NEXTHDR { $$ = MHHDR_NEXTHDR; } | CHECKSUM { $$ = MHHDR_CHECKSUM; } ; +export_format : XML { $$ = NFT_OUTPUT_XML; } + | JSON { $$ = NFT_OUTPUT_JSON; } + ; %%
src/rule.c +20 -0
@@ -18,7 +18,10 @@#include <statement.h> #include <rule.h> #include <utils.h> +#include <netlink.h> +#include <libnftnl/common.h> +#include <libnftnl/ruleset.h> #include <netinet/ip.h> #include <linux/netfilter.h> #include <linux/netfilter_arp.h>
@@ -589,6 +592,21 @@ static int do_list_sets(struct netlink_ctx *ctx, const struct location *loc,return 0 ; } +static int do_command_export (struct netlink_ctx *ctx,struct cmd *cmd) +{ +struct nft_ruleset *rs =netlink_dump_ruleset (ctx, &cmd->handle, + &cmd->location); + +if (rs ==NULL ) +return -1 ; + +nft_ruleset_fprintf (stdout, rs, cmd->format,0 ); +fprintf (stdout,"\n" ); + +nft_ruleset_free (rs); +return 0 ; +} +static int do_command_list (struct netlink_ctx *ctx,struct cmd *cmd) {struct table *table =NULL ;
@@ -741,6 +759,8 @@ int do_command(struct netlink_ctx *ctx, struct cmd *cmd)return do_command_flush (ctx, cmd);case CMD_RENAME :return do_command_rename (ctx, cmd); +case CMD_EXPORT : +return do_command_export (ctx, cmd);default :BUG ("invalid command object type %u\n" , cmd->obj); }
src/scanner.l +4 -0
@@ -255,6 +255,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "list" { return LIST; } "flush" { return FLUSH; } "rename" { return RENAME; } +"export" { return EXPORT; } "position" { return POSITION; }
@@ -411,6 +412,9 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "proto-src" { return PROTO_SRC; } "proto-dst" { return PROTO_DST; } +"xml" { return XML; } +"json" { return JSON; } + {addrstring} { yylval->string = xstrdup(yytext); return STRING;
Quoted 10 lines · Arturo Borrero Gonzalez
:~# nft export <xml|json>The XML/JSON output is provided raw by libnftnl, thus without format.
In case of XML, you can give format with the `xmllint' tool from libxml2-tools:
:~# nft list ruleset xml | xmllint --format -In case of JSON, you can use `json_pp' from perl standar package:
:~# nft list ruleset json | json_pp
Quoted 7 lines
A format field is added in struct cmd, and it will be reused in the import operation.
Signed-off-by Arturo Borrero Gonzalez <[email protected]>---
v2: address comments by Patrick: useCMD_OBJ_RULESET, move some includes, etc.
v3: rebase against nft's next-3.14 branch.
ops, I lost the subject line :(
Signature · 5 lines
Arturo Borrero González -- 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
Patrick McHardy
AppliedQuoted 7 lines · Arturo Borrero Gonzalez
:~# nft export <xml|json>The XML/JSON output is provided raw by libnftnl, thus without format.
In case of XML, you can give format with the `xmllint' tool from libxml2-tools:
:~# nft list ruleset xml | xmllint --format -
Quoted 7 lines
In case of JSON, you can use `json_pp' from perl standar package:
:~# nft list ruleset json | json_ppA format field is added in struct cmd, and it will be reused in the import operation.
Signed-off-by Arturo Borrero Gonzalez <[email protected]>
Applied, thanks Arturo.