From 15ad64734a544a9af033e54d232f112971072c15 Mon Sep 17 00:00:00 2001 From: Alvaro Neira Ayuso Date: Mon, 9 Feb 2015 21:09:53 +0100 Subject: src: add command tag in JSON/XML export support Currently, we can't do incremental updates via JSON/XML. This patch enriches the existing output to indicate the kind of update that you want to perform. So, if we have a ruleset like: table ip filter { chain input { type filter hook input priority 0; } } The new output looks like: {"nftables":[{"add":[{"table":{"name":"filter",...}}]}]} ^^^^^ Where we explicitly indicate that we want to add a table. We support all the actions that we can do with nft, they are: - Add, delete and flush tables and chains. - Add, delete, replace and insert rules. - Add and delete sets. - Add and delete set elements. - Flush ruleset. You only need to add the command tag: {"nftables":[{"delete":[{...}, {...},...}]}]} ^^^^^^^^ The possible command tags that you can use are "add", "delete", "insert", "replace" and "flush". - Flush table or chain, eg.: {"nftables":[{"flush":[{"table":{"name":...}}]}]} - Delete table, chain, set or rule: {"nftables":[{"delete":[{"chain":{"name":...}]}]} - Replace a rule (you have to specify the handle): {"nftables":[{"replace":[{"rule":{...}}]}]} - Insert a rule: {"nftables":[{"insert":[{"rule":{...}}]}]} Signed-off-by: Alvaro Neira Ayuso Signed-off-by: Pablo Neira Ayuso --- src/rule.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/rule.c') diff --git a/src/rule.c b/src/rule.c index ac5136c..7f4d049 100644 --- a/src/rule.c +++ b/src/rule.c @@ -963,15 +963,15 @@ static int nft_rule_snprintf_default(char *buf, size_t size, struct nft_rule *r, return offset; } -int nft_rule_snprintf(char *buf, size_t size, struct nft_rule *r, - uint32_t type, uint32_t flags) +static int nft_rule_cmd_snprintf(char *buf, size_t size, struct nft_rule *r, + uint32_t cmd, uint32_t type, uint32_t flags) { int ret, len = size, offset = 0; uint32_t inner_flags = flags; inner_flags &= ~NFT_OF_EVENT_ANY; - ret = nft_event_header_snprintf(buf+offset, len, type, flags); + ret = nft_cmd_header_snprintf(buf + offset, len, cmd, type, flags); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); switch(type) { @@ -993,15 +993,23 @@ int nft_rule_snprintf(char *buf, size_t size, struct nft_rule *r, SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - ret = nft_event_footer_snprintf(buf+offset, len, type, flags); + ret = nft_cmd_footer_snprintf(buf + offset, len, cmd, type, flags); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); return offset; } + +int nft_rule_snprintf(char *buf, size_t size, struct nft_rule *r, + uint32_t type, uint32_t flags) +{ + return nft_rule_cmd_snprintf(buf, size, r, nft_flag2cmd(flags), type, + flags); +} EXPORT_SYMBOL(nft_rule_snprintf); static inline int nft_rule_do_snprintf(char *buf, size_t size, void *r, - uint32_t type, uint32_t flags) + uint32_t cmd, uint32_t type, + uint32_t flags) { return nft_rule_snprintf(buf, size, r, type, flags); } @@ -1009,7 +1017,8 @@ static inline int nft_rule_do_snprintf(char *buf, size_t size, void *r, int nft_rule_fprintf(FILE *fp, struct nft_rule *r, uint32_t type, uint32_t flags) { - return nft_fprintf(fp, r, type, flags, nft_rule_do_snprintf); + return nft_fprintf(fp, r, NFT_CMD_UNSPEC, type, flags, + nft_rule_do_snprintf); } EXPORT_SYMBOL(nft_rule_fprintf); -- cgit v1.2.3