Skip to contentSkip to conversation

Some fixes and trivial improvements

PATCH In review Phil Sutter 1 participant 16 messages Jul 27, 2024 – Jul 31, 2024 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Some fixes and trivial improvements

With my fix for flushing non-existent chains I inadvertently turned chain flushes into nops and broke iptables-restore with input containing a flush early before other commands. The shell testsuite clearly identified all these issues, but I had tested only the problem case. This is fixed by patch 2 with patch 1 as basic work.

Patches 3-7 fix other issues I stumbled upon when working on some approach for forward-compatibility.

The remaining patches are not strictly fixes but trivial enough to just go along with the rest.

Phil Sutter (14):
nft: cache: Annotate faked base chains as such
nft: Fix for zeroing existent builtin chains
extensions: recent: Fix format string for unsigned values
extensions: conntrack: Use the right callbacks
nft: cmd: Init struct nft_cmd::head early
nft: Add potentially missing init_cs calls
arptables: Fix conditional opcode/proto-type printing
xshared: Do not omit all-wildcard interface spec when inverted
extensions: conntrack: Reuse print_state() for old state match
xshared: Make save_iface() static
xshared: Move NULL pointer check into save_iface()
libxtables: Debug: Slightly improve extension ordering debugging
arptables: Introduce print_iface()
ebtables: Omit all-wildcard interface specs from output

 extensions/iptables.t         |  2 ++
 extensions/libarpt_standard.t |  2 ++
 extensions/libebt_standard.t  |  2 ++
 extensions/libxt_conntrack.c  | 46 ++++++--------------------
 extensions/libxt_recent.c     | 12 ++++---
 iptables/nft-arp.c            | 61 +++++++++++++----------------------
 iptables/nft-bridge.c         |  2 +-
 iptables/nft-cache.c          |  6 ++--
 iptables/nft-cache.h          |  2 +-
 iptables/nft-chain.c          |  3 +-
 iptables/nft-chain.h          |  3 +-
 iptables/nft-cmd.c            |  1 +
 iptables/nft.c                | 44 ++++++++++++++++++-------
 iptables/xshared.c            | 16 +++------
 iptables/xshared.h            |  1 -
 libxtables/xtables.c          | 20 +++++++++---
 16 files changed, 109 insertions(+), 114 deletions(-)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH 1/14 nft: cache: Annotate faked base chains as such

To avoid pointless kernel ruleset modifications without too many workarounds in user space, code sometimes adds "fake" base chains to cache. Yet these fake entries happen to prevent base chain creation for a following command which actually requires them. Fix this by annotating the fake entries as such so *_builtin_init() functions may convert them into real ones.

Fixes fd4b9bf08b9eb ("nft: Avoid pointless table/chain creation")
---
 iptables/nft-cache.c |  6 +++---
 iptables/nft-cache.h |  2 +-
 iptables/nft-chain.c |  3 ++-
 iptables/nft-chain.h |  3 ++-
 iptables/nft.c       | 31 ++++++++++++++++++++-----------
 5 files changed, 28 insertions(+), 17 deletions(-)
iptables/nft-cache.c +3 -3
index 91d296709b9de..da2d4d7fd872c 100644
@@ -244,10 +244,10 @@ nft_cache_add_base_chain(struct nft_handle *h, const struct builtin_table *t,
 }
 
 int nft_cache_add_chain(struct nft_handle *h, const struct builtin_table *t,
-			struct nftnl_chain *c)
+			struct nftnl_chain *c, bool fake)
 {
 	const char *cname = nftnl_chain_get_str(c, NFTNL_CHAIN_NAME);
-	struct nft_chain *nc = nft_chain_alloc(c);
+	struct nft_chain *nc = nft_chain_alloc(c, fake);
 	int ret;
 
 	if (nftnl_chain_is_set(c, NFTNL_CHAIN_HOOKNUM)) {
@@ -349,7 +349,7 @@ static int nftnl_chain_list_cb(const struct nlmsghdr *nlh, void *data)
 		goto out;
 	}
 
-	nft_cache_add_chain(h, t, c);
+	nft_cache_add_chain(h, t, c, false);
 	return MNL_CB_OK;
 out:
 	nftnl_chain_free(c);
iptables/nft-cache.h +1 -1
index 29ec6b5c3232b..e9f5755c9561d 100644
@@ -17,7 +17,7 @@ int flush_rule_cache(struct nft_handle *h, const char *table,
 		     struct nft_chain *c);
 void nft_cache_build(struct nft_handle *h);
 int nft_cache_add_chain(struct nft_handle *h, const struct builtin_table *t,
-			struct nftnl_chain *c);
+			struct nftnl_chain *c, bool fake);
 int nft_cache_sort_chains(struct nft_handle *h, const char *table);
 
 struct nft_chain *
iptables/nft-chain.c +2 -1
index e954170fa7312..c24e6c9b346d1 100644
@@ -12,12 +12,13 @@
 
 #include "nft-chain.h"
 
-struct nft_chain *nft_chain_alloc(struct nftnl_chain *nftnl)
+struct nft_chain *nft_chain_alloc(struct nftnl_chain *nftnl, bool fake)
 {
 	struct nft_chain *c = xtables_malloc(sizeof(*c));
 
 	INIT_LIST_HEAD(&c->head);
 	c->nftnl = nftnl;
+	c->fake = fake;
 
 	return c;
 }
iptables/nft-chain.h +2 -1
index 9adf173857420..166504c0c8f95 100644
@@ -11,6 +11,7 @@ struct nft_chain {
 	struct hlist_node	hnode;
 	struct nft_chain	**base_slot;
 	struct nftnl_chain	*nftnl;
+	bool			fake;
 };
 
 #define CHAIN_NAME_HSIZE	512
@@ -20,7 +21,7 @@ struct nft_chain_list {
 	struct hlist_head	names[CHAIN_NAME_HSIZE];
 };
 
-struct nft_chain *nft_chain_alloc(struct nftnl_chain *nftnl);
+struct nft_chain *nft_chain_alloc(struct nftnl_chain *nftnl, bool fake);
 void nft_chain_free(struct nft_chain *c);
 
 struct nft_chain_list *nft_chain_list_alloc(void);
iptables/nft.c +20 -11
index a9d97d4cef8e0..fde3db2a22b79 100644
@@ -721,7 +721,7 @@ static void nft_chain_builtin_add(struct nft_handle *h,
 
 	if (!fake)
 		batch_chain_add(h, NFT_COMPAT_CHAIN_ADD, c);
-	nft_cache_add_chain(h, table, c);
+	nft_cache_add_chain(h, table, c, fake);
 }
 
 /* find if built-in table already exists */
@@ -765,14 +765,19 @@ nft_chain_builtin_find(const struct builtin_table *t, const char *chain)
 static void nft_chain_builtin_init(struct nft_handle *h,
 				   const struct builtin_table *table)
 {
+	struct nft_chain *c;
 	int i;
 
 	/* Initialize built-in chains if they don't exist yet */
 	for (i=0; i < NF_INET_NUMHOOKS && table->chains[i].name != NULL; i++) {
-		if (nft_chain_find(h, table->name, table->chains[i].name))
-			continue;
-
-		nft_chain_builtin_add(h, table, &table->chains[i], false);
+		c = nft_chain_find(h, table->name, table->chains[i].name);
+		if (!c) {
+			nft_chain_builtin_add(h, table,
+					      &table->chains[i], false);
+		} else if (c->fake) {
+			batch_chain_add(h, NFT_COMPAT_CHAIN_ADD, c->nftnl);
+			c->fake = false;
+		}
 	}
 }
 
@@ -799,6 +804,7 @@ static int nft_xt_builtin_init(struct nft_handle *h, const char *table,
 {
 	const struct builtin_table *t;
 	const struct builtin_chain *c;
+	struct nft_chain *nc;
 
 	if (!h->cache_init)
 		return 0;
@@ -819,10 +825,13 @@ static int nft_xt_builtin_init(struct nft_handle *h, const char *table,
 	if (!c)
 		return -1;
 
-	if (h->cache->table[t->type].base_chains[c->hook])
-		return 0;
-
-	nft_chain_builtin_add(h, t, c, false);
+	nc = h->cache->table[t->type].base_chains[c->hook];
+	if (!nc) {
+		nft_chain_builtin_add(h, t, c, false);
+	} else if (nc->fake) {
+		batch_chain_add(h, NFT_COMPAT_CHAIN_ADD, nc->nftnl);
+		nc->fake = false;
+	}
 	return 0;
 }
 
@@ -2091,7 +2100,7 @@ int nft_chain_user_add(struct nft_handle *h, const char *chain, const char *tabl
 	if (!batch_chain_add(h, NFT_COMPAT_CHAIN_USER_ADD, c))
 		return 0;
 
-	nft_cache_add_chain(h, t, c);
+	nft_cache_add_chain(h, t, c, false);
 
 	/* the core expects 1 for success and 0 for error */
 	return 1;
@@ -2118,7 +2127,7 @@ int nft_chain_restore(struct nft_handle *h, const char *chain, const char *table
 		nftnl_chain_set_str(c, NFTNL_CHAIN_NAME, chain);
 		created = true;
 
-		nft_cache_add_chain(h, t, c);
+		nft_cache_add_chain(h, t, c, false);
 	} else {
 		c = nc->nftnl;
 
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH 2/14 nft: Fix for zeroing existent builtin chains

In reply to Phil Sutter

Previous attempt at fixing for non-existent chains actually broke functionality by adding a check for NFTNL_CHAIN_HANDLE right after unsetting the attribute.

The approach was flawed for another reason, too: Base chains added in the same batch (cf. iptables-restore) have no handle either but zeroing them may still be sensible.

Instead, make use of the new fake chain annotation which identifies fakes more reliably.

Fixes f462975fb8049 ("nft: Fix for zeroing non-existent builtin chains")
---
 iptables/nft.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
iptables/nft.c +1 -1
index fde3db2a22b79..243b794f3d826 100644
@@ -3853,7 +3853,7 @@ static int __nft_chain_zero_counters(struct nft_chain *nc, void *data)
 		if (!o)
 			return -1;
 		/* may skip if it is a fake entry */
-		o->skip = !nftnl_chain_is_set(c, NFTNL_CHAIN_HANDLE);
+		o->skip = nc->fake;
 	}
 
 	iter = nftnl_rule_iter_create(c);
Signature · 1 line
2.43.0
author reviewer
#3 lore raw

PATCH 3/14 extensions: recent: Fix format string for unsigned values

In reply to Phil Sutter

Both fields 'seconds' and 'hit_count' are unsigned, use '%u' accordingly. While being at it, also fix coding-style in those lines.

Basically a day-1 bug, have Fixes: point at a reasonably old commit.

Fixes af1660fe0e88c ("Move libipt_recent to libxt_recent")
---
 extensions/libxt_recent.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
extensions/libxt_recent.c +8 -4
index 055ae35080346..0221d4462408f 100644
@@ -193,10 +193,12 @@ static void recent_print(const void *ip, const struct xt_entry_match *match,
 		printf(" UPDATE");
 	if (info->check_set & XT_RECENT_REMOVE)
 		printf(" REMOVE");
-	if(info->seconds) printf(" seconds: %d", info->seconds);
+	if (info->seconds)
+		printf(" seconds: %u", info->seconds);
 	if (info->check_set & XT_RECENT_REAP)
 		printf(" reap");
-	if(info->hit_count) printf(" hit_count: %d", info->hit_count);
+	if (info->hit_count)
+		printf(" hit_count: %u", info->hit_count);
 	if (info->check_set & XT_RECENT_TTL)
 		printf(" TTL-Match");
 	printf(" name: %s", info->name);
@@ -233,10 +235,12 @@ static void recent_save(const void *ip, const struct xt_entry_match *match,
 		printf(" --update");
 	if (info->check_set & XT_RECENT_REMOVE)
 		printf(" --remove");
-	if(info->seconds) printf(" --seconds %d", info->seconds);
+	if (info->seconds)
+		printf(" --seconds %u", info->seconds);
 	if (info->check_set & XT_RECENT_REAP)
 		printf(" --reap");
-	if(info->hit_count) printf(" --hitcount %d", info->hit_count);
+	if (info->hit_count)
+		printf(" --hitcount %u", info->hit_count);
 	if (info->check_set & XT_RECENT_TTL)
 		printf(" --rttl");
 	printf(" --name %s",info->name);
Signature · 1 line
2.43.0
author reviewer
#4 lore raw

PATCH 4/14 extensions: conntrack: Use the right callbacks

In reply to Phil Sutter

These version-agnostic conntrack match aliases emulating the 'state' extension introduced by commit 0d70163162589 ("libxt_state: replace as an alias to xt_conntrack") had incompatible print and save callbacks assigned. These callbacks expected struct xt_state_info in match->data which is incompatible to any of the actual xt_conntrack_mtinfo* structs used.

Fixes b28d4dcc9f555 ("iptables: state match incompatibilty across versions")
---
 extensions/libxt_conntrack.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
extensions/libxt_conntrack.c +6 -6
index ffbc7467bbf2e..ccbf731de7c4d 100644
@@ -1502,8 +1502,8 @@ static struct xtables_match conntrack_mt_reg[] = {
 		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
 		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
 		.help          = state_help,
-		.print         = state_print,
-		.save          = state_save,
+		.print         = conntrack1_mt4_print,
+		.save          = conntrack1_mt4_save,
 		.x6_parse      = state_ct1_parse,
 		.x6_options    = state_opts,
 	},
@@ -1517,8 +1517,8 @@ static struct xtables_match conntrack_mt_reg[] = {
 		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
 		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
 		.help          = state_help,
-		.print         = state_print,
-		.save          = state_save,
+		.print         = conntrack2_mt_print,
+		.save          = conntrack2_mt_save,
 		.x6_parse      = state_ct23_parse,
 		.x6_options    = state_opts,
 	},
@@ -1532,8 +1532,8 @@ static struct xtables_match conntrack_mt_reg[] = {
 		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
 		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
 		.help          = state_help,
-		.print         = state_print,
-		.save          = state_save,
+		.print         = conntrack3_mt_print,
+		.save          = conntrack3_mt_save,
 		.x6_parse      = state_ct23_parse,
 		.x6_options    = state_opts,
 		.xlate         = state_xlate,
Signature · 1 line
2.43.0
author reviewer
#5 lore raw

PATCH 5/14 nft: cmd: Init struct nft_cmd::head early

In reply to Phil Sutter

Calling nft_cmd_free() in error case segfaults otherwise if the to be freed object is not part of a list yet.

Exposed by commit eab75ed36a4f2 ("nft: Avoid memleak in error path of nft_cmd_new()"), but belongs to commit a7f1e208cdf9c (and may go well along with it).

Fixes a7f1e208cdf9c ("nft: split parsing from netlink commands")
---
 iptables/nft-cmd.c | 1 +
 1 file changed, 1 insertion(+)
iptables/nft-cmd.c +1 -0
index b38da9bdc1c0b..58d5aa11e90d2 100644
@@ -28,6 +28,7 @@ struct nft_cmd *nft_cmd_new(struct nft_handle *h, int command,
 	struct nft_cmd *cmd;
 
 	cmd = xtables_calloc(1, sizeof(struct nft_cmd));
+	INIT_LIST_HEAD(&cmd->head);
 	cmd->error.lineno = h->error.lineno;
 	cmd->command = command;
 	cmd->table = xtables_strdup(table);
Signature · 1 line
2.43.0
author reviewer
#6 lore raw

PATCH 6/14 nft: Add potentially missing init_cs calls

In reply to Phil Sutter

The callback is there for arptables only, so other family specific code does not need it. Not calling it from family-agnostic code is wrong though, as is ignoring it in arptables-specific code.

Fixes cfdda18044d81 ("nft-shared: Introduce init_cs family ops callback")
---
 iptables/nft-arp.c |  3 +++
 iptables/nft.c     | 11 +++++++++++
 2 files changed, 14 insertions(+)
iptables/nft-arp.c +3 -0
index 5d66e271720ec..2784f12ae33a9 100644
@@ -356,6 +356,8 @@ nft_arp_save_rule(const struct iptables_command_state *cs, unsigned int format)
 	printf("\n");
 }
 
+static void nft_arp_init_cs(struct iptables_command_state *cs);
+
 static void
 nft_arp_print_rule(struct nft_handle *h, struct nftnl_rule *r,
 		   unsigned int num, unsigned int format)
@@ -365,6 +367,7 @@ nft_arp_print_rule(struct nft_handle *h, struct nftnl_rule *r,
 	if (format & FMT_LINENUMBERS)
 		printf("%u ", num);
 
+	nft_arp_init_cs(&cs);
 	nft_rule_to_iptables_command_state(h, r, &cs);
 
 	nft_arp_print_rule_details(&cs, format);
iptables/nft.c +11 -0
index 243b794f3d826..8b1803181b207 100644
@@ -1797,6 +1797,8 @@ nft_rule_print_save(struct nft_handle *h, const struct nftnl_rule *r,
 	struct nft_family_ops *ops = h->ops;
 	bool ret;
 
+	if (ops->init_cs)
+		ops->init_cs(&cs);
 	ret = ops->rule_to_cs(h, r, &cs);
 
 	if (!(format & (FMT_NOCOUNTS | FMT_C_COUNTS)))
@@ -2395,6 +2397,11 @@ static bool nft_rule_cmp(struct nft_handle *h, struct nftnl_rule *r,
 	struct iptables_command_state _cs = {}, this = {}, *cs = &_cs;
 	bool ret = false, ret_this, ret_that;
 
+	if (h->ops->init_cs) {
+		h->ops->init_cs(&this);
+		h->ops->init_cs(cs);
+	}
+
 	ret_this = h->ops->rule_to_cs(h, r, &this);
 	ret_that = h->ops->rule_to_cs(h, rule, cs);
 
@@ -2679,6 +2686,8 @@ static int nft_rule_change_counters(struct nft_handle *h, const char *table,
 		(unsigned long long)
 		nftnl_rule_get_u64(r, NFTNL_RULE_HANDLE));
 
+	if (h->ops->init_cs)
+		h->ops->init_cs(&cs);
 	h->ops->rule_to_cs(h, r, &cs);
 
 	if (counter_op & CTR_OP_INC_PKTS)
@@ -2976,6 +2985,8 @@ int nft_rule_zero_counters(struct nft_handle *h, const char *chain,
 		goto error;
 	}
 
+	if (h->ops->init_cs)
+		h->ops->init_cs(&cs);
 	h->ops->rule_to_cs(h, r, &cs);
 	cs.counters.pcnt = cs.counters.bcnt = 0;
 	new_rule = nft_rule_new(h, &ctx, chain, table, &cs);
Signature · 1 line
2.43.0
author reviewer
#7 lore raw

PATCH 7/14 arptables: Fix conditional opcode/proto-type printing

In reply to Phil Sutter

The checks were wrong: nft_arp_init_cs() initializes masks to 65535, not
0. This went on unnoticed because nft_arp_add() does it right and init_cs callback was not used in e.g. nft_arp_print_rule(). The last patch adding init_cs() calls in potentially required spots exposed this though.

Fixes 84909d171585d ("xtables: bootstrap ARP compatibility layer for nftables")
---
 iptables/nft-arp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
iptables/nft-arp.c +4 -2
index 2784f12ae33a9..c73833270f0e8 100644
@@ -299,7 +299,8 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
 		sep = " ";
 	}
 
-	if (fw->arp.arpop_mask != 0) {
+	if (fw->arp.arpop_mask != 65535 || fw->arp.arpop != 0 ||
+	    fw->arp.invflags & IPT_INV_ARPOP) {
 		int tmp = ntohs(fw->arp.arpop);
 
 		printf("%s%s", sep, fw->arp.invflags & IPT_INV_ARPOP
@@ -329,7 +330,8 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
 		sep = " ";
 	}
 
-	if (fw->arp.arpro_mask != 0) {
+	if (fw->arp.arpro_mask != 65535 || fw->arp.arpro != 0 ||
+	    fw->arp.invflags & IPT_INV_PROTO) {
 		int tmp = ntohs(fw->arp.arpro);
 
 		printf("%s%s", sep, fw->arp.invflags & IPT_INV_PROTO
Signature · 1 line
2.43.0
author reviewer
#8 lore raw

PATCH 8/14 xshared: Do not omit all-wildcard interface spec when inverted

In reply to Phil Sutter

The rule parses correctly, but the (never matching) part is lost on output.

Looks like a day-1 bug, make it fix the change after which it applies cleanly.

Fixes b2197e7834f77 ("xshared: Entirely ignore interface masks when saving rules")
---
 extensions/iptables.t         | 2 ++
 extensions/libarpt_standard.t | 2 ++
 extensions/libebt_standard.t  | 2 ++
 iptables/xshared.c            | 2 +-
 4 files changed, 7 insertions(+), 1 deletion(-)
extensions/iptables.t +2 -0
index 5d6d3d15cc5fd..2817c3fb8437f 100644
@@ -4,6 +4,8 @@
 -i eth+ -o alongifacename+;=;OK
 ! -i eth0;=;OK
 ! -o eth+;=;OK
+-i + -j ACCEPT;-j ACCEPT;OK
+! -i +;=;OK
 -c "";;FAIL
 -c ,3;;FAIL
 -c 3,;;FAIL
extensions/libarpt_standard.t +2 -0
index 153540903f786..d6eaced3bd5e3 100644
@@ -9,6 +9,8 @@
 -j ACCEPT ! -i lo;=;OK
 -i ppp+;=;OK
 ! -i ppp+;=;OK
+-i + -j ACCEPT;-j ACCEPT;OK
+! -i +;=;OK
 -i lo --destination-mac 11:22:33:44:55:66;-i lo --dst-mac 11:22:33:44:55:66;OK
 --source-mac Unicast;--src-mac 00:00:00:00:00:00/01:00:00:00:00:00;OK
 ! --src-mac Multicast;! --src-mac 01:00:00:00:00:00/01:00:00:00:00:00;OK
extensions/libebt_standard.t +2 -0
index 3f1a459cb9814..4cf1f4cfa3ae5 100644
@@ -17,8 +17,10 @@
 --logical-out br1;=;FAIL
 -i + -d 00:0f:ee:d0:ba:be;-d 00:0f:ee:d0:ba:be;OK
 -i + -p ip;-p IPv4;OK
+! -i +;=;OK
 --logical-in + -d 00:0f:ee:d0:ba:be;-d 00:0f:ee:d0:ba:be;OK
 --logical-in + -p ip;-p IPv4;OK
+! --logical-in +;=;OK
 :FORWARD
 -i foobar;=;OK
 -o foobar;=;OK
iptables/xshared.c +1 -1
index b1997ea35f8f8..8c7df3c986eed 100644
@@ -759,7 +759,7 @@ void print_ifaces(const char *iniface, const char *outiface, uint8_t invflags,
 
 void save_iface(char letter, const char *iface, int invert)
 {
-	if (!strlen(iface) || !strcmp(iface, "+"))
+	if (!strlen(iface) || (!strcmp(iface, "+") && !invert))
 		return;
 
 	printf("%s -%c %s", invert ? " !" : "", letter, iface);
Signature · 1 line
2.43.0
author reviewer
#9 lore raw

PATCH 9/14 extensions: conntrack: Reuse print_state() for old state match

In reply to Phil Sutter

The extra bits supported by print_state() won't be set by the parser, no functional change expected.

---
 extensions/libxt_conntrack.c | 34 ++++------------------------------
 1 file changed, 4 insertions(+), 30 deletions(-)
extensions/libxt_conntrack.c +4 -30
index ccbf731de7c4d..04940154eb314 100644
@@ -1102,32 +1102,6 @@ static void state_ct23_parse(struct xt_option_call *cb)
 		sinfo->invert_flags |= XT_CONNTRACK_STATE;
 }
 
-static void state_print_state(unsigned int statemask)
-{
-	const char *sep = "";
-
-	if (statemask & XT_CONNTRACK_STATE_INVALID) {
-		printf("%sINVALID", sep);
-		sep = ",";
-	}
-	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_NEW)) {
-		printf("%sNEW", sep);
-		sep = ",";
-	}
-	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_RELATED)) {
-		printf("%sRELATED", sep);
-		sep = ",";
-	}
-	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED)) {
-		printf("%sESTABLISHED", sep);
-		sep = ",";
-	}
-	if (statemask & XT_CONNTRACK_STATE_UNTRACKED) {
-		printf("%sUNTRACKED", sep);
-		sep = ",";
-	}
-}
-
 static void
 state_print(const void *ip,
       const struct xt_entry_match *match,
@@ -1135,16 +1109,16 @@ state_print(const void *ip,
 {
 	const struct xt_state_info *sinfo = (const void *)match->data;
 
-	printf(" state ");
-	state_print_state(sinfo->statemask);
+	printf(" state");
+	print_state(sinfo->statemask);
 }
 
 static void state_save(const void *ip, const struct xt_entry_match *match)
 {
 	const struct xt_state_info *sinfo = (const void *)match->data;
 
-	printf(" --state ");
-	state_print_state(sinfo->statemask);
+	printf(" --state");
+	print_state(sinfo->statemask);
 }
 
 static void state_xlate_print(struct xt_xlate *xl, unsigned int statemask, int inverted)
Signature · 1 line
2.43.0
author reviewer
#10 lore raw

PATCH 10/14 xshared: Make save_iface() static

In reply to Phil Sutter

Since commit 22f2e1fca127b ("xshared: Share save_rule_details() with legacy"), there are no callers outside of xshared.c anymore.

---
 iptables/xshared.c | 2 +-
 iptables/xshared.h | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
iptables/xshared.c +1 -1
index 8c7df3c986eed..1070fea42c8cf 100644
@@ -757,7 +757,7 @@ void print_ifaces(const char *iniface, const char *outiface, uint8_t invflags,
 	printf(FMT("%-6s ", "out %s "), iface);
 }
 
-void save_iface(char letter, const char *iface, int invert)
+static void save_iface(char letter, const char *iface, int invert)
 {
 	if (!strlen(iface) || (!strcmp(iface, "+") && !invert))
 		return;
iptables/xshared.h +0 -1
index 26c492ebee9ec..0018b7c70bd83 100644
@@ -216,7 +216,6 @@ void save_ipv6_addr(char letter, const struct in6_addr *addr,
 
 void print_ifaces(const char *iniface, const char *outiface, uint8_t invflags,
 		  unsigned int format);
-void save_iface(char letter, const char *iface, int invert);
 
 void print_fragment(unsigned int flags, unsigned int invflags,
 		    unsigned int format, bool fake);
Signature · 1 line
2.43.0
author reviewer
#11 lore raw

PATCH 11/14 xshared: Move NULL pointer check into save_iface()

In reply to Phil Sutter

Simplify callers a bit, the function tests other conditions disqualifying any output already.

While being at it, invert the conditional - it is more readable this way.

---
 iptables/xshared.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
iptables/xshared.c +4 -10
index 1070fea42c8cf..2a5eef09c75de 100644
@@ -759,10 +759,8 @@ void print_ifaces(const char *iniface, const char *outiface, uint8_t invflags,
 
 static void save_iface(char letter, const char *iface, int invert)
 {
-	if (!strlen(iface) || (!strcmp(iface, "+") && !invert))
-		return;
-
-	printf("%s -%c %s", invert ? " !" : "", letter, iface);
+	if (iface && strlen(iface) && (strcmp(iface, "+") || invert))
+		printf("%s -%c %s", invert ? " !" : "", letter, iface);
 }
 
 static void command_match(struct iptables_command_state *cs, bool invert)
@@ -1095,12 +1093,8 @@ void print_rule_details(unsigned int linenum, const struct xt_counters *ctrs,
 void save_rule_details(const char *iniface, const char *outiface,
 		       uint16_t proto, int frag, uint8_t invflags)
 {
-	if (iniface != NULL) {
-		save_iface('i', iniface, invflags & IPT_INV_VIA_IN);
-	}
-	if (outiface != NULL) {
-		save_iface('o', outiface, invflags & IPT_INV_VIA_OUT);
-	}
+	save_iface('i', iniface, invflags & IPT_INV_VIA_IN);
+	save_iface('o', outiface, invflags & IPT_INV_VIA_OUT);
 
 	if (proto > 0) {
 		const char *pname = proto_to_name(proto, true);
Signature · 1 line
2.43.0
author reviewer
#12 lore raw

PATCH 12/14 libxtables: Debug: Slightly improve extension ordering debugging

In reply to Phil Sutter

Print the extension's real name (if present) and prefix the extension list by a position number for clarity.

---
 libxtables/xtables.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
libxtables/xtables.c +15 -5
index 7b370d486f888..7d54540b73b73 100644
@@ -1171,11 +1171,21 @@ void xtables_register_match(struct xtables_match *me)
 	me->next = *pos;
 	*pos = me;
 #ifdef DEBUG
-	printf("%s: inserted match %s (family %d, revision %d):\n",
-			__func__, me->name, me->family, me->revision);
-	for (pos = &xtables_pending_matches; *pos; pos = &(*pos)->next) {
-		printf("%s:\tmatch %s (family %d, revision %d)\n", __func__,
-		       (*pos)->name, (*pos)->family, (*pos)->revision);
+#define printmatch(m, sfx)						\
+	printf("match %s (", (m)->name);				\
+	if ((m)->real_name)						\
+		printf("alias %s, ", (m)->real_name);			\
+	printf("family %d, revision %d)%s", (m)->family, (m)->revision, sfx);
+
+	{
+		int i = 1;
+
+		printf("%s: inserted ", __func__);
+		printmatch(me, ":\n");
+		for (pos = &xtables_pending_matches; *pos; pos = &(*pos)->next) {
+			printf("pos %d:\t", i++);
+			printmatch(*pos, "\n");
+		}
 	}
 #endif
 }
Signature · 1 line
2.43.0
author reviewer
#13 lore raw

PATCH 13/14 arptables: Introduce print_iface()

In reply to Phil Sutter

Merge conditional interface printing code for input and output interface into a function.

---
 iptables/nft-arp.c | 52 ++++++++++++++--------------------------------
 1 file changed, 16 insertions(+), 36 deletions(-)
iptables/nft-arp.c +16 -36
index c73833270f0e8..264864c3fb2b2 100644
@@ -197,13 +197,23 @@ static void nft_arp_print_header(unsigned int format, const char *chain,
 	}
 }
 
+static void print_iface(char letter, const char *iface,
+			unsigned int format, bool invert, const char **sep)
+{
+	if (iface[0] == '\0' || (!strcmp(iface, "+") && !invert)) {
+		if (!(format & FMT_VIA))
+			return;
+		iface = (format & FMT_NUMERIC) ? "*" : "any";
+	}
+	printf("%s%s-%c %s", *sep, invert ? "! " : "", letter, iface);
+	*sep = " ";
+}
+
 static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
 				       unsigned int format)
 {
 	const struct arpt_entry *fw = &cs->arp;
-	char iface[IFNAMSIZ+2];
 	const char *sep = "";
-	int print_iface = 0;
 	int i;
 
 	if (strlen(cs->jumpto)) {
@@ -211,40 +221,10 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
 		sep = " ";
 	}
 
-	iface[0] = '\0';
-
-	if (fw->arp.iniface[0] != '\0') {
-		strcat(iface, fw->arp.iniface);
-		print_iface = 1;
-	}
-	else if (format & FMT_VIA) {
-		print_iface = 1;
-		if (format & FMT_NUMERIC) strcat(iface, "*");
-		else strcat(iface, "any");
-	}
-	if (print_iface) {
-		printf("%s%s-i %s", sep, fw->arp.invflags & IPT_INV_VIA_IN ?
-				   "! " : "", iface);
-		sep = " ";
-	}
-
-	print_iface = 0;
-	iface[0] = '\0';
-
-	if (fw->arp.outiface[0] != '\0') {
-		strcat(iface, fw->arp.outiface);
-		print_iface = 1;
-	}
-	else if (format & FMT_VIA) {
-		print_iface = 1;
-		if (format & FMT_NUMERIC) strcat(iface, "*");
-		else strcat(iface, "any");
-	}
-	if (print_iface) {
-		printf("%s%s-o %s", sep, fw->arp.invflags & IPT_INV_VIA_OUT ?
-				   "! " : "", iface);
-		sep = " ";
-	}
+	print_iface('i', fw->arp.iniface, format,
+		    fw->arp.invflags & IPT_INV_VIA_IN, &sep);
+	print_iface('o', fw->arp.outiface, format,
+		    fw->arp.invflags & IPT_INV_VIA_OUT, &sep);
 
 	if (fw->arp.smsk.s_addr != 0L) {
 		printf("%s%s-s %s", sep,
Signature · 1 line
2.43.0
author reviewer
#14 lore raw

PATCH 14/14 ebtables: Omit all-wildcard interface specs from output

In reply to Phil Sutter

Regular code path doesn't hit this because the conversion to libnftnl_rule takes care of it already. Future changes though will cause iptables_command_state objects to be printed directly, making this relevant.

---
 iptables/nft-bridge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
iptables/nft-bridge.c +1 -1
index f4a3c69ac1660..0f85e21861cde 100644
@@ -212,7 +212,7 @@ static bool nft_rule_to_ebtables_command_state(struct nft_handle *h,
 
 static void print_iface(const char *option, const char *name, bool invert)
 {
-	if (*name)
+	if (*name && (strcmp(name, "+") || invert))
 		printf("%s%s %s ", invert ? "! " : "", option, name);
 }
 
Signature · 1 line
2.43.0
author reviewer
#15 lore raw
In reply to Phil Sutter
Quoted 12 lines · Phil Sutter

With my fix for flushing non-existent chains I inadvertently turned chain flushes into nops and broke iptables-restore with input containing a flush early before other commands. The shell testsuite clearly identified all these issues, but I had tested only the problem case. This is fixed by patch 2 with patch 1 as basic work.

Patches 3-7 fix other issues I stumbled upon when working on some approach for forward-compatibility.

The remaining patches are not strictly fixes but trivial enough to just go along with the rest.

Quoted 15 lines

Phil Sutter (14):
nft: cache: Annotate faked base chains as such
nft: Fix for zeroing existent builtin chains
extensions: recent: Fix format string for unsigned values
extensions: conntrack: Use the right callbacks
nft: cmd: Init struct nft_cmd::head early
nft: Add potentially missing init_cs calls
arptables: Fix conditional opcode/proto-type printing
xshared: Do not omit all-wildcard interface spec when inverted
extensions: conntrack: Reuse print_state() for old state match
xshared: Make save_iface() static
xshared: Move NULL pointer check into save_iface()
libxtables: Debug: Slightly improve extension ordering debugging
arptables: Introduce print_iface()
ebtables: Omit all-wildcard interface specs from output

Series applied.