table: Embed creating nft version into userdata
Phil Sutter
reviewerRFC 1/1 table: Embed creating nft version into userdata
Upon listing a table which was created by a newer version of nftables, warn about the potentially incomplete content.
Suggested-by Florian Westphal <[email protected]>
Cc Dan Winship <[email protected]>
Signed-off-by Phil Sutter <[email protected]>
--- include/nft.h | 2 ++ include/rule.h | 1 + src/mnl.c | 18 ++++++++++++------ src/netlink.c | 25 ++++++++++++++++++++++++- src/rule.c | 4 ++++ 5 files changed, 43 insertions(+), 7 deletions(-)
include/nft.h +2 -0
@@ -15,4 +15,6 @@* something we frequently need to do and it's intentional. */ #define free_const (ptr) free((void *)(ptr)) +#define NFTNL_UDATA_TABLE_NFTVER 1 +#endif /* NFTABLES_NFT_H */
include/rule.h +1 -0
@@ -170,6 +170,7 @@ struct table {uint32_t owner;const char *comment;bool has_xt_stmts; +bool is_from_future; };extern struct table *table_alloc (void );
src/mnl.c +12 -6
@@ -1069,24 +1069,30 @@ int mnl_nft_table_add(struct netlink_ctx *ctx, struct cmd *cmd,if (nlt ==NULL )memory_allocation_error (); + udbuf =nftnl_udata_buf_alloc (NFT_USERDATA_MAXLEN ); +if (!udbuf) +memory_allocation_error (); +nftnl_table_set_u32 (nlt,NFTNL_TABLE_FAMILY , cmd->handle.family);if (cmd->table) {nftnl_table_set_u32 (nlt,NFTNL_TABLE_FLAGS , cmd->table->flags);if (cmd->table->comment) { - udbuf =nftnl_udata_buf_alloc (NFT_USERDATA_MAXLEN ); -if (!udbuf) -memory_allocation_error ();if (!nftnl_udata_put_strz (udbuf,NFTNL_UDATA_TABLE_COMMENT , cmd->table->comment))memory_allocation_error (); -nftnl_table_set_data (nlt,NFTNL_TABLE_USERDATA ,nftnl_udata_buf_data (udbuf), -nftnl_udata_buf_len (udbuf)); -nftnl_udata_buf_free (udbuf); } }else {nftnl_table_set_u32 (nlt,NFTNL_TABLE_FLAGS ,0 ); } +if (!nftnl_udata_put_strz (udbuf,NFTNL_UDATA_TABLE_NFTVER , +PACKAGE_VERSION )) +memory_allocation_error (); +nftnl_table_set_data (nlt,NFTNL_TABLE_USERDATA , +nftnl_udata_buf_data (udbuf), +nftnl_udata_buf_len (udbuf)); +nftnl_udata_buf_free (udbuf); + nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), NFT_MSG_NEWTABLE, cmd->handle.family,
src/netlink.c +24 -1
@@ -751,6 +751,7 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)switch (type) {case NFTNL_UDATA_TABLE_COMMENT : +case NFTNL_UDATA_TABLE_NFTVER :if (value[len -1 ] !='\0' )return -1 ;break ;
@@ -761,10 +762,28 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)return 0 ; } +static int version_cmp (const char *ver_a,const char *ver_b) +{ +int ax, ay, az, bx, by, bz; + +if (sscanf (ver_a,"%d.%d.%d" , &ax, &ay, &az) !=3 || +sscanf (ver_b,"%d.%d.%d" , &bx, &by, &bz) !=3 ) +return 0 ; + +if (ax != bx) +return ax - bx; +if (ay != by) +return ay - by; +if (az != bz) +return az - bz; + +return strcmp (ver_a, ver_b); +} +struct table *netlink_delinearize_table (struct netlink_ctx *ctx,const struct nftnl_table *nlt) { -const struct nftnl_udata *ud[NFTNL_UDATA_TABLE_MAX +1 ] = {}; +const struct nftnl_udata *ud[NFTNL_UDATA_TABLE_MAX +2 ] = {}, *udtmp;struct table *table;const char *udata;uint32_t ulen;
@@ -785,6 +804,10 @@ struct table *netlink_delinearize_table(struct netlink_ctx *ctx, }if (ud[NFTNL_UDATA_TABLE_COMMENT ]) table->comment =xstrdup (nftnl_udata_get (ud[NFTNL_UDATA_TABLE_COMMENT ])); + udtmp = ud[NFTNL_UDATA_TABLE_NFTVER ]; +if (udtmp &&version_cmp (PACKAGE_VERSION , +nftnl_udata_get (udtmp)) <0 ) + table->is_from_future = true; }return table;
src/rule.c +4 -0
@@ -1285,6 +1285,10 @@ static void table_print(const struct table *table, struct output_ctx *octx)fprintf (octx->error_fp,"# Warning: table %s %s is managed by iptables-nft, do not touch!\n" , family, table->handle.table.name); +if (table->is_from_future) +fprintf (octx->error_fp, +"# Warning: table %s %s was created by a newer version of nftables, content may be incomplete!\n" , + family, table->handle.table.name);nft_print (octx,"table %s %s {" , family, table->handle.table.name);if (nft_output_handle (octx) || table->flags &TABLE_F_OWNER )
Signature · 1 line
2.49.0