Skip to contentSkip to conversation

table: Embed creating nft version into userdata

PATCH In review Phil Sutter 2 participants 9 messages Aug 13, 2025 – Sep 8, 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 v1:
- Keep version and build timestamp separate in nftversion.h (separate variables) and netlink (separate attributes)
- Drop NFTNL_UDATA_TABLE_NFTVER define, rely upon updated libnftnl headers instead (we need a correct value in NFTNL_UDATA_TABLE_MAX as well, so no point in introducing hacks)
- Perform attribute existence checks in version_cmp() itself, kernel data may contain one or the other or both or none at all
- Minor warning adjustment as requested

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   | 24 ++++++++++++++++++++++++
 include/rule.h |  1 +
 src/mnl.c      | 21 +++++++++++++++------
 src/netlink.c  | 33 +++++++++++++++++++++++++++++++++
 src/rule.c     |  4 ++++
 7 files changed, 81 insertions(+), 6 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 +24 -0
index 550913ef04964..55fecbc56660a 100644
@@ -114,6 +114,30 @@ 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}"
+	echo "};"
+	echo "static char nftbuildstamp[[]] = {"
+	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/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 +15 -6
index 43229f2498e55..ec4d73d12460a 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,32 @@ 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) ||
+	    !nftnl_udata_put(udbuf, NFTNL_UDATA_TABLE_NFTBLD,
+	                     sizeof(nftbuildstamp), nftbuildstamp))
+		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 +33 -0
index 94cbcbfc6c094..b5da33e5a1e53 100644
@@ -10,6 +10,7 @@
  */
 
 #include <nft.h>
+#include <nftversion.h>
 
 #include <errno.h>
 #include <libmnl/libmnl.h>
@@ -799,6 +800,14 @@ 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;
+		case NFTNL_UDATA_TABLE_NFTBLD:
+			if (len != sizeof(nftbuildstamp))
+				return -1;
+			break;
 		default:
 			return 0;
 	}
@@ -806,6 +815,29 @@ 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;
+	size_t i;
+
+	/* netlink attribute lengths checked by table_parse_udata_cb() */
+	if (ud[NFTNL_UDATA_TABLE_NFTVER]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTVER]);
+		for (i = 0; i < sizeof(nftversion); i++) {
+			if (nftversion[i] != udbuf[i])
+				return nftversion[i] - udbuf[i];
+		}
+	}
+	if (ud[NFTNL_UDATA_TABLE_NFTBLD]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTBLD]);
+		for (i = 0; i < sizeof(nftbuildstamp); i++) {
+			if (nftbuildstamp[i] != udbuf[i])
+				return nftbuildstamp[i] - udbuf[i];
+		}
+	}
+	return 0;
+}
+
 struct table *netlink_delinearize_table(struct netlink_ctx *ctx,
 					const struct nftnl_table *nlt)
 {
@@ -830,6 +862,7 @@ 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]));
+		table->is_from_future = version_cmp(ud) < 0;
 	}
 
 	return table;
src/rule.c +4 -0
index 0ad948ea87f2f..398e5bdd4c64e 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
author reviewer
#1 lore raw

On Wed, Aug 13, 2025 at 07:07:19PM +0200, Phil Sutter wrote:
Subject: Re: [nft PATCH] table: Embed creating nft version into userdata

This is actually 'PATCH v2', sorry for the mess.

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Phil Sutter

Hi Phil,

Quoted 6 lines · Phil Sutter

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

Just rebase this and apply, it clashes with my update for --unitdir.

Thanks.

author reviewer
#3 lore raw
Quoted 6 lines · Pablo Neira Ayuso

Hi Phil,

Quoted 3 lines · Phil Sutter

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

Quoted 7 lines
Quoted 3 lines

Just rebase this and apply, it clashes with my update for --unitdir.

Also applied.

#4 lore raw
In reply to Phil Sutter

Hi Phil,

I know this is applied, but one late question.

Quoted 14 lines · Phil Sutter
src/netlink.c +10 -0
@@ -806,6 +815,29 @@ 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;
+	size_t i;
+
+	/* netlink attribute lengths checked by table_parse_udata_cb() */
+	if (ud[NFTNL_UDATA_TABLE_NFTVER]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTVER]);
+		for (i = 0; i < sizeof(nftversion); i++) {
+			if (nftversion[i] != udbuf[i])

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

Quoted 10 lines
src/netlink.c +10 -0
+				return nftversion[i] - udbuf[i];
+		}
+	}
+	if (ud[NFTNL_UDATA_TABLE_NFTBLD]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTBLD]);
+		for (i = 0; i < sizeof(nftbuildstamp); i++) {
+			if (nftbuildstamp[i] != udbuf[i])
+				return nftbuildstamp[i] - udbuf[i];
+		}
+	}

One situation I was considering:

1.0.6.y (build today) in the host
1.1.5 (build n days ago) in the container

This will display the warning.

I suggested to use build time only when version is the same?

If the scenario is nftables in the host injects tables into container, then host binary will likely be updated more often.

IIUC, the build time here will actually determine when the warning is emitted, regardless the version.

author reviewer
#5 lore raw
Quoted 37 lines · Pablo Neira Ayuso

Hi Phil,

I know this is applied, but one late question.

Quoted 14 lines · Phil Sutter
src/netlink.c +10 -0
@@ -806,6 +815,29 @@ 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;
+	size_t i;
+
+	/* netlink attribute lengths checked by table_parse_udata_cb() */
+	if (ud[NFTNL_UDATA_TABLE_NFTVER]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTVER]);
+		for (i = 0; i < sizeof(nftversion); i++) {
+			if (nftversion[i] != udbuf[i])

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

Quoted 10 lines
src/netlink.c +10 -0
+				return nftversion[i] - udbuf[i];
+		}
+	}
+	if (ud[NFTNL_UDATA_TABLE_NFTBLD]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTBLD]);
+		for (i = 0; i < sizeof(nftbuildstamp); i++) {
+			if (nftbuildstamp[i] != udbuf[i])
+				return nftbuildstamp[i] - udbuf[i];
+		}
+	}

One situation I was considering:

1.0.6.y (build today) in the host
1.1.5 (build n days ago) in the container

This will display the warning.

Quoted 7 lines

I suggested to use build time only when version is the same?

If the scenario is nftables in the host injects tables into container, then host binary will likely be updated more often.

IIUC, the build time here will actually determine when the warning is emitted, regardless the version.

It should not:

Here's version_cmp() pseudo-code:

| for attr in NFTNL_UDATA_TABLE_NFTVER, NFTNL_UDATA_TABLE_NFTBLD:
| 	for idx in len(attr):
| 		if local_data[idx] != attr[idx]:
| 			return local_data[idx] - attr[idx];

This algorithm considers following bytes only if all previous ones were identical. Precedence is from highest order version bytes to lowest order build bytes (data is therefore stored in Big Endian).

So your version 1.1.5 will always be "newer" than 1.0.6.y, no matter the build date, due to minor version 1 > 0.

Cheers, Phil

#6 lore raw
Quoted 55 lines · Phil Sutter
Quoted 37 lines · Pablo Neira Ayuso

Hi Phil,

I know this is applied, but one late question.

Quoted 14 lines · Phil Sutter
src/netlink.c +10 -0
@@ -806,6 +815,29 @@ 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;
+	size_t i;
+
+	/* netlink attribute lengths checked by table_parse_udata_cb() */
+	if (ud[NFTNL_UDATA_TABLE_NFTVER]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTVER]);
+		for (i = 0; i < sizeof(nftversion); i++) {
+			if (nftversion[i] != udbuf[i])

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

Quoted 10 lines
src/netlink.c +10 -0
+				return nftversion[i] - udbuf[i];
+		}
+	}
+	if (ud[NFTNL_UDATA_TABLE_NFTBLD]) {
+		udbuf = nftnl_udata_get(ud[NFTNL_UDATA_TABLE_NFTBLD]);
+		for (i = 0; i < sizeof(nftbuildstamp); i++) {
+			if (nftbuildstamp[i] != udbuf[i])
+				return nftbuildstamp[i] - udbuf[i];
+		}
+	}

One situation I was considering:

1.0.6.y (build today) in the host
1.1.5 (build n days ago) in the container

This will display the warning.

Quoted 7 lines

I suggested to use build time only when version is the same?

If the scenario is nftables in the host injects tables into container, then host binary will likely be updated more often.

IIUC, the build time here will actually determine when the warning is emitted, regardless the version.

It should not:

Here's version_cmp() pseudo-code:

| for attr in NFTNL_UDATA_TABLE_NFTVER, NFTNL_UDATA_TABLE_NFTBLD:
| 	for idx in len(attr):
| 		if local_data[idx] != attr[idx]:
| 			return local_data[idx] - attr[idx];
Quoted 6 lines

This algorithm considers following bytes only if all previous ones were identical. Precedence is from highest order version bytes to lowest order build bytes (data is therefore stored in Big Endian).

So your version 1.1.5 will always be "newer" than 1.0.6.y, no matter the build date, due to minor version 1 > 0.

Ah, I misread this smart function, thanks for clarifying.

#7 lore raw
In reply to Phil Sutter
Quoted 2 lines · Phil Sutter

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

Compilation breaks for me here:

In file included from src/netlink.c:13:
./nftversion.h:6:20: error: 'MAKE_STAMP' undeclared here (not in a function)

    6 |         ((uint64_t)MAKE_STAMP >> 56) & 0xff,
      |                    ^~~~~~~~~~
#8 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

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

Compilation breaks for me here:

In file included from src/netlink.c:13:
./nftversion.h:6:20: error: 'MAKE_STAMP' undeclared here (not in a function)

    6 |         ((uint64_t)MAKE_STAMP >> 56) & 0xff,
      |                    ^~~~~~~~~~

It breaks when I compile with:

make V=1 CFLAGS+="-fsanitize=address -fsanitize=undefined" -j 16

which I usually do, including valgrind runs, to catch for memory issues.

Could you look into restoring this? Thanks!