Skip to contentSkip to conversation

table: Embed creating nft version into userdata

PATCH In review Phil Sutter 2 participants 4 messages Aug 8, 2025 – Aug 12, 2025

Subsystems (2)
lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 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.

---
Changes since RFC:
- Change NFTNL_UDATA_TABLE_NFTVER content from string (PACKAGE_VERSION) to 12Byte binary data consisting of:
- the version 3-tuple
- a stable release number specified at configure-time
- the build time in seconds since epoch (a 64bit value - could scrap some bytes, but maybe worth leaving some space)

---
 .gitignore     |  1 +
 Makefile.am    |  3 +++
 configure.ac   | 22 ++++++++++++++++++++++
 include/nft.h  |  2 ++
 include/rule.h |  1 +
 src/mnl.c      | 19 +++++++++++++------
 src/netlink.c  | 23 ++++++++++++++++++++++-
 src/rule.c     |  4 ++++
 8 files changed, 68 insertions(+), 7 deletions(-)
.gitignore +1 -0
index a62e31f31c6b5..1e3bc5146b2f1 100644
@@ -14,6 +14,7 @@ autom4te.cache
 build-aux/
 libnftables.pc
 libtool
+nftversion.h
 
 # cscope files
 /cscope.*
Makefile.am +3 -0
index b5580b5451fca..ca6af2e393bed 100644
@@ -33,6 +33,7 @@ sbin_PROGRAMS =
 check_PROGRAMS =
 dist_man_MANS =
 CLEANFILES =
+DISTCLEANFILES =
 
 ###############################################################################
 
@@ -106,6 +107,8 @@ noinst_HEADERS = \
 	\
 	$(NULL)
 
+DISTCLEANFILES += nftversion.h
+
 ###############################################################################
 
 AM_CPPFLAGS = \
configure.ac +22 -0
index 550913ef04964..2c68c2b8e0560 100644
@@ -114,6 +114,28 @@ AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [],
 #include <netdb.h>
 ]])
 
+AC_ARG_WITH([stable-release], [AS_HELP_STRING([--with-stable-release],
+            [Stable release number])],
+            [], [with_stable_release=0])
+AC_CONFIG_COMMANDS([stable_release],
+                   [STABLE_RELEASE=$stable_release],
+                   [stable_release=$with_stable_release])
+AC_CONFIG_COMMANDS([nftversion.h], [
+(
+	echo "static char nftversion[[]] = {"
+	echo "	${VERSION}," | tr '.' ','
+	echo "	${STABLE_RELEASE},"
+	for ((i = 56; i >= 0; i-= 8)); do
+		echo "	((uint64_t)MAKE_STAMP >> $i) & 0xff,"
+	done
+	echo "};"
+) >nftversion.h
+])
+# Current date should be fetched exactly once per build,
+# so have 'make' call date and pass the value to every 'gcc' call
+AC_SUBST([MAKE_STAMP], ["\$(shell date +%s)"])
+CFLAGS="${CFLAGS} -DMAKE_STAMP=\${MAKE_STAMP}"
+
 AC_CONFIG_FILES([					\
 		Makefile				\
 		libnftables.pc				\
include/nft.h +2 -0
index a2d62dbf4808a..b406a68ffeb18 100644
@@ -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
index 470ae10754ba5..319f9c39f1107 100644
@@ -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 +13 -6
index 43229f2498e55..67ec60a6fee00 100644
@@ -10,6 +10,7 @@
 
 #include <nft.h>
 #include <iface.h>
+#include <nftversion.h>
 
 #include <libmnl/libmnl.h>
 #include <libnftnl/common.h>
@@ -1074,24 +1075,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(udbuf, NFTNL_UDATA_TABLE_NFTVER,
+			     sizeof(nftversion), nftversion))
+		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 +22 -1
index 94cbcbfc6c094..97a49c08b1e82 100644
@@ -10,6 +10,7 @@
  */
 
 #include <nft.h>
+#include <nftversion.h>
 
 #include <errno.h>
 #include <libmnl/libmnl.h>
@@ -799,6 +800,10 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 			if (value[len - 1] != '\0')
 				return -1;
 			break;
+		case NFTNL_UDATA_TABLE_NFTVER:
+			if (len != sizeof(nftversion))
+				return -1;
+			break;
 		default:
 			return 0;
 	}
@@ -806,10 +811,23 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 	return 0;
 }
 
+static int version_cmp(const struct nftnl_udata *ud)
+{
+	const char *udbuf = nftnl_udata_get(ud);
+	size_t i;
+
+	/* udbuf length checked by table_parse_udata_cb() */
+	for (i = 0; i < sizeof(nftversion); i++) {
+		if (nftversion[i] != udbuf[i])
+			return nftversion[i] - udbuf[i];
+	}
+	return 0;
+}
+
 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] = {};
 	struct table *table;
 	const char *udata;
 	uint32_t ulen;
@@ -830,6 +848,9 @@ 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]));
+		if (ud[NFTNL_UDATA_TABLE_NFTVER] &&
+		    version_cmp(ud[NFTNL_UDATA_TABLE_NFTVER]) < 0)
+			table->is_from_future = true;
 	}
 
 	return table;
src/rule.c +4 -0
index 0ad948ea87f2f..fd69c622cfe75 100644
@@ -1298,6 +1298,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
#1 lore raw

Hi Phil,

Thanks for your patch, comments below.

Quoted 96 lines · Phil Sutter

Upon listing a table which was created by a newer version of nftables, warn about the potentially incomplete content.

---
Changes since RFC:
- Change NFTNL_UDATA_TABLE_NFTVER content from string (PACKAGE_VERSION) to 12Byte binary data consisting of:
- the version 3-tuple
- a stable release number specified at configure-time
- the build time in seconds since epoch (a 64bit value - could scrap some bytes, but maybe worth leaving some space)

---
 .gitignore     |  1 +
 Makefile.am    |  3 +++
 configure.ac   | 22 ++++++++++++++++++++++
 include/nft.h  |  2 ++
 include/rule.h |  1 +
 src/mnl.c      | 19 +++++++++++++------
 src/netlink.c  | 23 ++++++++++++++++++++++-
 src/rule.c     |  4 ++++
 8 files changed, 68 insertions(+), 7 deletions(-)
.gitignore +1 -0
index a62e31f31c6b5..1e3bc5146b2f1 100644
@@ -14,6 +14,7 @@ autom4te.cache
 build-aux/
 libnftables.pc
 libtool
+nftversion.h
 
 # cscope files
 /cscope.*
Makefile.am +3 -0
index b5580b5451fca..ca6af2e393bed 100644
@@ -33,6 +33,7 @@ sbin_PROGRAMS =
 check_PROGRAMS =
 dist_man_MANS =
 CLEANFILES =
+DISTCLEANFILES =
 
 ###############################################################################
 
@@ -106,6 +107,8 @@ noinst_HEADERS = \
 	\
 	$(NULL)
 
+DISTCLEANFILES += nftversion.h
+
 ###############################################################################
 
 AM_CPPFLAGS = \
configure.ac +22 -0
index 550913ef04964..2c68c2b8e0560 100644
@@ -114,6 +114,28 @@ AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [],
 #include <netdb.h>
 ]])
 
+AC_ARG_WITH([stable-release], [AS_HELP_STRING([--with-stable-release],
+            [Stable release number])],
+            [], [with_stable_release=0])
+AC_CONFIG_COMMANDS([stable_release],
+                   [STABLE_RELEASE=$stable_release],
+                   [stable_release=$with_stable_release])
+AC_CONFIG_COMMANDS([nftversion.h], [
+(
+	echo "static char nftversion[[]] = {"
+	echo "	${VERSION}," | tr '.' ','
+	echo "	${STABLE_RELEASE},"
+	for ((i = 56; i >= 0; i-= 8)); do
+		echo "	((uint64_t)MAKE_STAMP >> $i) & 0xff,"
+	done
+	echo "};"
+) >nftversion.h
+])
+# Current date should be fetched exactly once per build,
+# so have 'make' call date and pass the value to every 'gcc' call
+AC_SUBST([MAKE_STAMP], ["\$(shell date +%s)"])
+CFLAGS="${CFLAGS} -DMAKE_STAMP=\${MAKE_STAMP}"
+
 AC_CONFIG_FILES([					\
 		Makefile				\
 		libnftables.pc				\
include/nft.h +0 -0
index a2d62dbf4808a..b406a68ffeb18 100644

include/nft.h line 18

Quoted 4 lines
include/nft.h +1 -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

Better define this in libnftnl?

libnftnl$ git grep NFTNL_UDATA_TABLE_COMMENT
include/libnftnl/udata.h: NFTNL_UDATA_TABLE_COMMENT,

Quoted 35 lines
include/nft.h +0 -0
 #endif /* NFTABLES_NFT_H */
include/rule.h +1 -0
index 470ae10754ba5..319f9c39f1107 100644
@@ -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 +5 -0
index 43229f2498e55..67ec60a6fee00 100644
@@ -10,6 +10,7 @@
 
 #include <nft.h>
 #include <iface.h>
+#include <nftversion.h>
 
 #include <libmnl/libmnl.h>
 #include <libnftnl/common.h>
@@ -1074,24 +1075,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) {

src/mnl.c int mnl_nft_table_add(struct netlink_ctx *ctx, struct cmd *cmd, · line 1088

Quoted 10 lines
src/mnl.c +0 -5
 		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));

I suggest two separated TLVs, one for version and another for build time.

Quoted 44 lines
src/mnl.c +8 -1
-			nftnl_udata_buf_free(udbuf);
 		}
 	} else {
 		nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, 0);
 	}
 
+	if (!nftnl_udata_put(udbuf, NFTNL_UDATA_TABLE_NFTVER,
+			     sizeof(nftversion), nftversion))
+		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 +5 -0
index 94cbcbfc6c094..97a49c08b1e82 100644
@@ -10,6 +10,7 @@
  */
 
 #include <nft.h>
+#include <nftversion.h>
 
 #include <errno.h>
 #include <libmnl/libmnl.h>
@@ -799,6 +800,10 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 			if (value[len - 1] != '\0')
 				return -1;
 			break;
+		case NFTNL_UDATA_TABLE_NFTVER:
+			if (len != sizeof(nftversion))
+				return -1;
+			break;
 		default:
 			return 0;
 	}
@@ -806,10 +811,23 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 	return 0;
 }
 

src/netlink.c static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data) · line 823

Quoted 10 lines
src/netlink.c +10 -0
+static int version_cmp(const struct nftnl_udata *ud)
+{
+	const char *udbuf = nftnl_udata_get(ud);
+	size_t i;
+
+	/* udbuf length checked by table_parse_udata_cb() */
+	for (i = 0; i < sizeof(nftversion); i++) {
+		if (nftversion[i] != udbuf[i])
+			return nftversion[i] - udbuf[i];
+	}

Interesting trick.

Quoted 26 lines
src/netlink.c +7 -1
+	return 0;
+}
+
 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] = {};
 	struct table *table;
 	const char *udata;
 	uint32_t ulen;
@@ -830,6 +848,9 @@ 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]));
+		if (ud[NFTNL_UDATA_TABLE_NFTVER] &&
+		    version_cmp(ud[NFTNL_UDATA_TABLE_NFTVER]) < 0)
+			table->is_from_future = true;
 	}
 
 	return table;
src/rule.c +0 -0
index 0ad948ea87f2f..fd69c622cfe75 100644

src/rule.c static void table_print(const struct table *table, struct output_ctx *octx) · line 1303

Quoted 6 lines
src/rule.c +3 -0
@@ -1298,6 +1298,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",

+ "# Warning: this table %s %s was created by a newer version of nftables? Content may be incomplete!\n",

Maybe rise it as a question? This is just an indication, I was considering you could write a program to push anything in the userdata area. But not a deal breaker if you prefer this sentence.

Quoted 7 lines
src/rule.c +1 -0
+			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
author reviewer
#2 lore raw

Hi Pablo,

Quoted 1 line · Pablo Neira Ayuso

Thanks for your patch, comments below.

Thanks for your review! (Replies below.)

Quoted 100 lines · Phil Sutter
Quoted 99 lines

Upon listing a table which was created by a newer version of nftables, warn about the potentially incomplete content.

---
Changes since RFC:
- Change NFTNL_UDATA_TABLE_NFTVER content from string (PACKAGE_VERSION) to 12Byte binary data consisting of:
- the version 3-tuple
- a stable release number specified at configure-time
- the build time in seconds since epoch (a 64bit value - could scrap some bytes, but maybe worth leaving some space)

---
 .gitignore     |  1 +
 Makefile.am    |  3 +++
 configure.ac   | 22 ++++++++++++++++++++++
 include/nft.h  |  2 ++
 include/rule.h |  1 +
 src/mnl.c      | 19 +++++++++++++------
 src/netlink.c  | 23 ++++++++++++++++++++++-
 src/rule.c     |  4 ++++
 8 files changed, 68 insertions(+), 7 deletions(-)
.gitignore +1 -0
index a62e31f31c6b5..1e3bc5146b2f1 100644
@@ -14,6 +14,7 @@ autom4te.cache
 build-aux/
 libnftables.pc
 libtool
+nftversion.h
 
 # cscope files
 /cscope.*
Makefile.am +3 -0
index b5580b5451fca..ca6af2e393bed 100644
@@ -33,6 +33,7 @@ sbin_PROGRAMS =
 check_PROGRAMS =
 dist_man_MANS =
 CLEANFILES =
+DISTCLEANFILES =
 
 ###############################################################################
 
@@ -106,6 +107,8 @@ noinst_HEADERS = \
 	\
 	$(NULL)
 
+DISTCLEANFILES += nftversion.h
+
 ###############################################################################
 
 AM_CPPFLAGS = \
configure.ac +22 -0
index 550913ef04964..2c68c2b8e0560 100644
@@ -114,6 +114,28 @@ AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [],
 #include <netdb.h>
 ]])
 
+AC_ARG_WITH([stable-release], [AS_HELP_STRING([--with-stable-release],
+            [Stable release number])],
+            [], [with_stable_release=0])
+AC_CONFIG_COMMANDS([stable_release],
+                   [STABLE_RELEASE=$stable_release],
+                   [stable_release=$with_stable_release])
+AC_CONFIG_COMMANDS([nftversion.h], [
+(
+	echo "static char nftversion[[]] = {"
+	echo "	${VERSION}," | tr '.' ','
+	echo "	${STABLE_RELEASE},"
+	for ((i = 56; i >= 0; i-= 8)); do
+		echo "	((uint64_t)MAKE_STAMP >> $i) & 0xff,"
+	done
+	echo "};"
+) >nftversion.h
+])
+# Current date should be fetched exactly once per build,
+# so have 'make' call date and pass the value to every 'gcc' call
+AC_SUBST([MAKE_STAMP], ["\$(shell date +%s)"])
+CFLAGS="${CFLAGS} -DMAKE_STAMP=\${MAKE_STAMP}"
+
 AC_CONFIG_FILES([					\
 		Makefile				\
 		libnftables.pc				\
include/nft.h +0 -0
index a2d62dbf4808a..b406a68ffeb18 100644
@@ -15,4 +15,6 @@
  * something we frequently need to do and it's intentional. */
 #define free_const(ptr) free((void *)(ptr))
 
Quoted 3 lines

include/nft.h line 18

Quoted 1 line
include/nft.h +1 -0
+#define NFTNL_UDATA_TABLE_NFTVER 1

Better define this in libnftnl?

ACK, you found my "quick hack", sorry for that.

Quoted 40 lines

libnftnl$ git grep NFTNL_UDATA_TABLE_COMMENT
include/libnftnl/udata.h: NFTNL_UDATA_TABLE_COMMENT,

Quoted 37 lines
include/nft.h +0 -0
 #endif /* NFTABLES_NFT_H */
include/rule.h +1 -0
index 470ae10754ba5..319f9c39f1107 100644
@@ -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 +5 -0
index 43229f2498e55..67ec60a6fee00 100644
@@ -10,6 +10,7 @@
 
 #include <nft.h>
 #include <iface.h>
+#include <nftversion.h>
 
 #include <libmnl/libmnl.h>
 #include <libnftnl/common.h>
@@ -1074,24 +1075,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);
 
Quoted 10 lines

src/mnl.c int mnl_nft_table_add(struct netlink_ctx *ctx, struct cmd *cmd, · line 1088

Quoted 8 lines
src/mnl.c +0 -5
 		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));

I suggest two separated TLVs, one for version and another for build time.

This seems wrong to me. Build time is just an extra to the version info. What benefit do you see with splitting them up?

Quoted 44 lines
Quoted 44 lines
src/mnl.c +8 -1
-			nftnl_udata_buf_free(udbuf);
 		}
 	} else {
 		nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, 0);
 	}
 
+	if (!nftnl_udata_put(udbuf, NFTNL_UDATA_TABLE_NFTVER,
+			     sizeof(nftversion), nftversion))
+		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 +5 -0
index 94cbcbfc6c094..97a49c08b1e82 100644
@@ -10,6 +10,7 @@
  */
 
 #include <nft.h>
+#include <nftversion.h>
 
 #include <errno.h>
 #include <libmnl/libmnl.h>
@@ -799,6 +800,10 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 			if (value[len - 1] != '\0')
 				return -1;
 			break;
+		case NFTNL_UDATA_TABLE_NFTVER:
+			if (len != sizeof(nftversion))
+				return -1;
+			break;
 		default:
 			return 0;
 	}
@@ -806,10 +811,23 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 	return 0;
 }
 
Quoted 12 lines

src/netlink.c static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data) · line 823

Quoted 10 lines
src/netlink.c +10 -0
+static int version_cmp(const struct nftnl_udata *ud)
+{
+	const char *udbuf = nftnl_udata_get(ud);
+	size_t i;
+
+	/* udbuf length checked by table_parse_udata_cb() */
+	for (i = 0; i < sizeof(nftversion); i++) {
+		if (nftversion[i] != udbuf[i])
+			return nftversion[i] - udbuf[i];
+	}

Interesting trick.

There's a German proverb about a blind hen and grains. But I like that function, too! :)

Quoted 35 lines
Quoted 32 lines
src/netlink.c +7 -1
+	return 0;
+}
+
 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] = {};
 	struct table *table;
 	const char *udata;
 	uint32_t ulen;
@@ -830,6 +848,9 @@ 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]));
+		if (ud[NFTNL_UDATA_TABLE_NFTVER] &&
+		    version_cmp(ud[NFTNL_UDATA_TABLE_NFTVER]) < 0)
+			table->is_from_future = true;
 	}
 
 	return table;
src/rule.c +3 -0
index 0ad948ea87f2f..fd69c622cfe75 100644
@@ -1298,6 +1298,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",
(quoted diff) +1 -0
+			"# Warning: this table %s %s was created by a newer version of nftables? Content may be incomplete!\n",
Quoted 3 lines

Maybe rise it as a question? This is just an indication, I was considering you could write a program to push anything in the userdata area. But not a deal breaker if you prefer this sentence.

Fine with me!

Thanks, Phil

#3 lore raw
Quoted 107 lines · Phil Sutter

Hi Pablo,

Quoted 1 line · Pablo Neira Ayuso

Thanks for your patch, comments below.

Thanks for your review! (Replies below.)

Quoted 100 lines · Phil Sutter
Quoted 99 lines

Upon listing a table which was created by a newer version of nftables, warn about the potentially incomplete content.

---
Changes since RFC:
- Change NFTNL_UDATA_TABLE_NFTVER content from string (PACKAGE_VERSION) to 12Byte binary data consisting of:
- the version 3-tuple
- a stable release number specified at configure-time
- the build time in seconds since epoch (a 64bit value - could scrap some bytes, but maybe worth leaving some space)

---
 .gitignore     |  1 +
 Makefile.am    |  3 +++
 configure.ac   | 22 ++++++++++++++++++++++
 include/nft.h  |  2 ++
 include/rule.h |  1 +
 src/mnl.c      | 19 +++++++++++++------
 src/netlink.c  | 23 ++++++++++++++++++++++-
 src/rule.c     |  4 ++++
 8 files changed, 68 insertions(+), 7 deletions(-)
.gitignore +1 -0
index a62e31f31c6b5..1e3bc5146b2f1 100644
@@ -14,6 +14,7 @@ autom4te.cache
 build-aux/
 libnftables.pc
 libtool
+nftversion.h
 
 # cscope files
 /cscope.*
Makefile.am +3 -0
index b5580b5451fca..ca6af2e393bed 100644
@@ -33,6 +33,7 @@ sbin_PROGRAMS =
 check_PROGRAMS =
 dist_man_MANS =
 CLEANFILES =
+DISTCLEANFILES =
 
 ###############################################################################
 
@@ -106,6 +107,8 @@ noinst_HEADERS = \
 	\
 	$(NULL)
 
+DISTCLEANFILES += nftversion.h
+
 ###############################################################################
 
 AM_CPPFLAGS = \
configure.ac +22 -0
index 550913ef04964..2c68c2b8e0560 100644
@@ -114,6 +114,28 @@ AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r, getservbyport_r], [], [],
 #include <netdb.h>
 ]])
 
+AC_ARG_WITH([stable-release], [AS_HELP_STRING([--with-stable-release],
+            [Stable release number])],
+            [], [with_stable_release=0])
+AC_CONFIG_COMMANDS([stable_release],
+                   [STABLE_RELEASE=$stable_release],
+                   [stable_release=$with_stable_release])
+AC_CONFIG_COMMANDS([nftversion.h], [
+(
+	echo "static char nftversion[[]] = {"
+	echo "	${VERSION}," | tr '.' ','
+	echo "	${STABLE_RELEASE},"
+	for ((i = 56; i >= 0; i-= 8)); do
+		echo "	((uint64_t)MAKE_STAMP >> $i) & 0xff,"
+	done
+	echo "};"
+) >nftversion.h
+])
+# Current date should be fetched exactly once per build,
+# so have 'make' call date and pass the value to every 'gcc' call
+AC_SUBST([MAKE_STAMP], ["\$(shell date +%s)"])
+CFLAGS="${CFLAGS} -DMAKE_STAMP=\${MAKE_STAMP}"
+
 AC_CONFIG_FILES([					\
 		Makefile				\
 		libnftables.pc				\
include/nft.h +0 -0
index a2d62dbf4808a..b406a68ffeb18 100644
@@ -15,4 +15,6 @@
  * something we frequently need to do and it's intentional. */
 #define free_const(ptr) free((void *)(ptr))
 
Quoted 5 lines
Quoted 3 lines

include/nft.h line 18

Quoted 1 line
include/nft.h +1 -0
+#define NFTNL_UDATA_TABLE_NFTVER 1

Better define this in libnftnl?

ACK, you found my "quick hack", sorry for that.

If you prefer easier backporting, then add an #ifdef here to skip the libnftnl dependency.

Quoted 51 lines
Quoted 40 lines

libnftnl$ git grep NFTNL_UDATA_TABLE_COMMENT
include/libnftnl/udata.h: NFTNL_UDATA_TABLE_COMMENT,

Quoted 37 lines
include/nft.h +0 -0
 #endif /* NFTABLES_NFT_H */
include/rule.h +1 -0
index 470ae10754ba5..319f9c39f1107 100644
@@ -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 +5 -0
index 43229f2498e55..67ec60a6fee00 100644
@@ -10,6 +10,7 @@
 
 #include <nft.h>
 #include <iface.h>
+#include <nftversion.h>
 
 #include <libmnl/libmnl.h>
 #include <libnftnl/common.h>
@@ -1074,24 +1075,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);
 
Quoted 10 lines

src/mnl.c int mnl_nft_table_add(struct netlink_ctx *ctx, struct cmd *cmd, · line 1088

Quoted 8 lines
src/mnl.c +0 -5
 		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));

I suggest two separated TLVs, one for version and another for build time.

Quoted 2 lines

This seems wrong to me. Build time is just an extra to the version info. What benefit do you see with splitting them up?

It is the same as in Netlink. If someone shows up saying built time and version is not enough, a new attribute should be sufficient.

I cannot see how that can happen, but I have regretted everytime I added TLVs that embed multiple fields to save a header.

Quoted 57 lines
Quoted 44 lines
Quoted 44 lines
src/mnl.c +8 -1
-			nftnl_udata_buf_free(udbuf);
 		}
 	} else {
 		nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, 0);
 	}
 
+	if (!nftnl_udata_put(udbuf, NFTNL_UDATA_TABLE_NFTVER,
+			     sizeof(nftversion), nftversion))
+		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 +5 -0
index 94cbcbfc6c094..97a49c08b1e82 100644
@@ -10,6 +10,7 @@
  */
 
 #include <nft.h>
+#include <nftversion.h>
 
 #include <errno.h>
 #include <libmnl/libmnl.h>
@@ -799,6 +800,10 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 			if (value[len - 1] != '\0')
 				return -1;
 			break;
+		case NFTNL_UDATA_TABLE_NFTVER:
+			if (len != sizeof(nftversion))
+				return -1;
+			break;
 		default:
 			return 0;
 	}
@@ -806,10 +811,23 @@ static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 	return 0;
 }
 
Quoted 12 lines

src/netlink.c static int table_parse_udata_cb(const struct nftnl_udata *attr, void *data) · line 823

Quoted 10 lines
src/netlink.c +10 -0
+static int version_cmp(const struct nftnl_udata *ud)
+{
+	const char *udbuf = nftnl_udata_get(ud);
+	size_t i;
+
+	/* udbuf length checked by table_parse_udata_cb() */
+	for (i = 0; i < sizeof(nftversion); i++) {
+		if (nftversion[i] != udbuf[i])
+			return nftversion[i] - udbuf[i];
+	}

Interesting trick.

Quoted 2 lines

There's a German proverb about a blind hen and grains. But I like that function, too! :)

We don't have such proverb in Spanish :)

Quoted 35 lines
Quoted 35 lines
Quoted 32 lines
src/netlink.c +7 -1
+	return 0;
+}
+
 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] = {};
 	struct table *table;
 	const char *udata;
 	uint32_t ulen;
@@ -830,6 +848,9 @@ 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]));
+		if (ud[NFTNL_UDATA_TABLE_NFTVER] &&
+		    version_cmp(ud[NFTNL_UDATA_TABLE_NFTVER]) < 0)
+			table->is_from_future = true;
 	}
 
 	return table;
src/rule.c +3 -0
index 0ad948ea87f2f..fd69c622cfe75 100644
@@ -1298,6 +1298,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",
(quoted diff) +1 -0
+			"# Warning: this table %s %s was created by a newer version of nftables? Content may be incomplete!\n",
Quoted 5 lines
Quoted 3 lines

Maybe rise it as a question? This is just an indication, I was considering you could write a program to push anything in the userdata area. But not a deal breaker if you prefer this sentence.

Fine with me!

Thanks.