src: add support to import json/xml with the new syntax
PATCH v3 2/4 src: add support to import json/xml with the new syntax
This patch adds support to parse the new syntax in xml/json. This patch adds two new different functions nft_ruleset_parse_*_cb. The ruleset contais differents objects (tables, chains, set and rules) of nftables. With these functions, we can pass a callback functions as parameter and we can call for each objects.
These functions use a new structure as parameter called nft_parse_ctx. This structure contains the command associated, the type of the object (table, chain, rule, set or set element) and the object.
Also, with this changes, we support to parse another new ruleset syntax:
{"nftables":[{"add":[{"element":{"name":"blackhole","table":"filter"
"family":"ip","key_type":7,"key_len":4,"set_elem":[{"key":{
"reg":{"type":"value","len":4,"data0":"0x0403a8c0"}}}]}}]}]}With this new "element", we can make incremental changes to set elements.
In short, these functions offer us the possibility to make incremental changes to the ruleset. And this new ruleset import support is more flexible and more easy to extend with new commands.
Moreover, this patch consolidate the code in the ruleset xml/json import support.
Another interesting goal is extend (in the future) the ruleset structure with information like the command associated to each object.
---
[changes in v3]
* Restore a removed EXPORT_SYMBOL to the functions nft_ruleset_attr_unset.
* Enhance the description of the patch.
include/libnftnl/ruleset.h | 31 ++ src/internal.h | 3 + src/libnftnl.map | 5 + src/ruleset.c | 838 ++++++++++++++++++++++++++++---------------- src/set.c | 12 + src/utils.c | 17 + 6 files changed, 597 insertions(+), 309 deletions(-)
include/libnftnl/ruleset.h +31 -0
@@ -25,11 +25,42 @@ enum { NFT_RULESET_ATTR_RULELIST, }; +enum nft_ruleset_type { +NFT_RULESET_UNSPEC =0 , +NFT_RULESET_RULESET , +NFT_RULESET_TABLE , +NFT_RULESET_CHAIN , +NFT_RULESET_RULE , +NFT_RULESET_SET , +NFT_RULESET_SET_ELEMS , +}; +bool nft_ruleset_attr_is_set (const struct nft_ruleset *r,uint16_t attr);void nft_ruleset_attr_unset (struct nft_ruleset *r,uint16_t attr);void nft_ruleset_attr_set (struct nft_ruleset *r,uint16_t attr,void *data);void *nft_ruleset_attr_get (const struct nft_ruleset *r,uint16_t attr); +enum { +NFT_RULESET_CTX_CMD =0 , +NFT_RULESET_CTX_TYPE , +NFT_RULESET_CTX_TABLE , +NFT_RULESET_CTX_CHAIN , +NFT_RULESET_CTX_RULE , +NFT_RULESET_CTX_SET , +}; + +struct nft_parse_ctx ; +bool nft_ruleset_ctx_is_set (const struct nft_parse_ctx *ctx,uint16_t attr); +void *nft_ruleset_ctx_get (const struct nft_parse_ctx *ctx,uint16_t attr); +uint32_t nft_ruleset_ctx_get_u32 (const struct nft_parse_ctx *ctx, +uint16_t attr); + +int nft_ruleset_parse_file_cb (enum nft_parse_type type,FILE *fp, +struct nft_parse_err *err,void *data, +int (*cb)(const struct nft_parse_ctx *ctx)); +int nft_ruleset_parse_buffer_cb (enum nft_parse_type type,const char *buffer, +struct nft_parse_err *err,void *data, +int (*cb)(const struct nft_parse_ctx *ctx));int nft_ruleset_parse (struct nft_ruleset *rs,enum nft_parse_type type,const char *data,struct nft_parse_err *err); int nft_ruleset_parse_file(struct nft_ruleset *rs, enum nft_parse_type type,
src/internal.h +3 -0
@@ -139,6 +139,8 @@ int nft_jansson_parse_rule(struct nft_rule *r, json_t *tree,struct nft_set ;int nft_jansson_parse_set (struct nft_set *s,json_t *tree,struct nft_parse_err *err); +int nft_jansson_parse_elem (struct nft_set *s,json_t *tree, +struct nft_parse_err *err);#endif const char *nft_family2str (uint32_t family);
@@ -148,6 +150,7 @@ const char *nft_verdict2str(uint32_t verdict);int nft_str2verdict (const char *verdict,int *verdict_num);int nft_flag2cmd (uint32_t flags,uint32_t *cmd);int nft_get_value (enum nft_type type,void *val,void *out); +uint32_t nft_str2cmd (const char *cmd);#include <stdio.h> int nft_fprintf(FILE *fp, void *obj, uint32_t cmd, uint32_t type,
src/libnftnl.map +5 -0
@@ -182,8 +182,13 @@ global: nft_ruleset_attr_unset; nft_ruleset_attr_set; nft_ruleset_attr_get; + nft_ruleset_ctx_is_set; + nft_ruleset_ctx_get; + nft_ruleset_ctx_get_u32; nft_ruleset_parse; nft_ruleset_parse_file; + nft_ruleset_parse_file_cb; + nft_ruleset_parse_buffer_cb; nft_ruleset_snprintf; nft_ruleset_fprintf;
src/ruleset.c +529 -309
1,022 lines not on this page · raw message
src/set.c +12 -0
@@ -511,6 +511,18 @@ int nft_jansson_parse_set(struct nft_set *s, json_t *tree,return nft_jansson_parse_set_info (s, root, err); } + +int nft_jansson_parse_elem (struct nft_set *s,json_t *tree, +struct nft_parse_err *err) +{ +json_t *root; + + root =nft_jansson_get_node (tree,"element" , err); +if (root ==NULL ) +return -1 ; + +return nft_jansson_parse_set_info (s, root, err); +}#endif static int nft_set_json_parse(struct nft_set *s, const void *json,
src/utils.c +17 -0
@@ -16,6 +16,7 @@#include <arpa/inet.h> #include <errno.h> #include <inttypes.h> +#include <buffer.h> #include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h>
@@ -193,6 +194,22 @@ int nft_flag2cmd(uint32_t flags, uint32_t *cmd)return -1 ; } +uint32_t nft_str2cmd (const char *cmd) +{ +if (strcmp (cmd,ADD ) ==0 ) +return NFT_CMD_ADD ; +else if (strcmp (cmd,INSERT ) ==0 ) +return NFT_CMD_INSERT ; +else if (strcmp (cmd,DELETE ) ==0 ) +return NFT_CMD_DELETE ; +else if (strcmp (cmd,REPLACE ) ==0 ) +return NFT_CMD_REPLACE ; +else if (strcmp (cmd,FLUSH ) ==0 ) +return NFT_CMD_FLUSH ; + +return NFT_CMD_UNSPEC ; +} +int nft_fprintf (FILE *fp,void *obj,uint32_t cmd,uint32_t type,uint32_t flags,int (*snprintf_cb)(char *buf,size_t bufsiz,void *obj,uint32_t cmd,uint32_t type,uint32_t flags))
Signature · 1 line
1.7.10.4