All of lore.kernel.org
 help / color / mirror / Atom feed
* [iptables PATCH] extensions: libxt_conntrack: Fail on untranslatable state match
@ 2026-10-08 18:23 Phil Sutter
  2026-10-21 10:17 ` Phil Sutter
  0 siblings, 1 reply; 4+ messages in thread
From: Phil Sutter @ 2026-10-08 18:23 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Balazs Gulacsi

Iptables' conntrack match supports combined matching on ct->state bits
as well as the NAT bits in ct->status. The kernel extension matches if
any of the bits is set, i.e. '-m conntrack --ctstate NEW,DNAT' matches
packets in either NEW state or DNAT status or both. Therefore,
translating this sample match into 'ct status dnat ct state new' is
wrong: The nftables rule matches only if both statements match.

The only way to achieve this in nftables seems to be by duplicating the
rule which is not feasible within the xlate infrastructure. So instead
just abort the translation.

This is a partial revert of commit 1a13277f88099 ("extensions:
libxt_conntrack: Fix for ignored ctstates in translation") which turned
an incomplete translation into an incorrect one.

Cc: Balazs Gulacsi <m2gulbal@gmail.com>
Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1826
Fixes: 26ec09bf6b9b6 ("extensions: libxt_conntrack: provide translation for DNAT and SNAT --ctstate")
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
Sorry for the back'n'forth here. While updating nfbz#1826 after pushing
the "fix", I noticed my comment explaining why I didn't submit the patch
back in January. This patch tidies up the loose ends.
---
 extensions/libxt_conntrack.c      | 12 ++++++++----
 extensions/libxt_conntrack.txlate | 15 ---------------
 2 files changed, 8 insertions(+), 19 deletions(-)

diff --git a/extensions/libxt_conntrack.c b/extensions/libxt_conntrack.c
index 4ac4fba00c9c5..ebf8d6ebbee86 100644
--- a/extensions/libxt_conntrack.c
+++ b/extensions/libxt_conntrack.c
@@ -1201,6 +1201,9 @@ static void addr_xlate_print(struct xt_xlate *xl,
 	}
 }
 
+#define XT_CONNTRACK_STATE_SDNAT	( XT_CONNTRACK_STATE_SNAT | \
+					  XT_CONNTRACK_STATE_DNAT )
+
 static int _conntrack3_mt_xlate(struct xt_xlate *xl,
 				const struct xt_xlate_mt_params *params,
 				int family)
@@ -1227,15 +1230,16 @@ static int _conntrack3_mt_xlate(struct xt_xlate *xl,
 	}
 
 	if (sinfo->match_flags & XT_CONNTRACK_STATE) {
-		if ((sinfo->state_mask & XT_CONNTRACK_STATE_SNAT) ||
-		    (sinfo->state_mask & XT_CONNTRACK_STATE_DNAT)) {
+		if (sinfo->state_mask & XT_CONNTRACK_STATE_SDNAT) {
+			if (sinfo->state_mask & ~XT_CONNTRACK_STATE_SDNAT)
+				return 0;
+
 			xt_xlate_add(xl, "%sct status %s%s", space,
 				     sinfo->invert_flags & XT_CONNTRACK_STATE ? "!= " : "",
 				     sinfo->state_mask & XT_CONNTRACK_STATE_SNAT ? "snat" : "dnat");
 			space = " ";
 		}
-		if (sinfo->state_mask & ~(XT_CONNTRACK_STATE_SNAT |
-					  XT_CONNTRACK_STATE_DNAT)) {
+		if (sinfo->state_mask & ~XT_CONNTRACK_STATE_SDNAT) {
 			xt_xlate_add(xl, "%sct state ", space);
 			state_xlate_print(xl, sinfo->state_mask,
 					  sinfo->invert_flags & XT_CONNTRACK_STATE);
diff --git a/extensions/libxt_conntrack.txlate b/extensions/libxt_conntrack.txlate
index e7255e2a516fd..eeb1b1a53d66b 100644
--- a/extensions/libxt_conntrack.txlate
+++ b/extensions/libxt_conntrack.txlate
@@ -63,18 +63,3 @@ nft 'add rule ip filter INPUT ct status dnat counter accept'
 
 iptables-translate -t filter -A INPUT -m conntrack ! --ctstate DNAT -j ACCEPT
 nft 'add rule ip filter INPUT ct status != dnat counter accept'
-
-iptables-translate -t filter -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED,DNAT -j ACCEPT
-nft 'add rule ip filter INPUT ct status dnat ct state related,established counter accept'
-
-iptables-translate -t filter -A INPUT -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT -j ACCEPT
-nft 'add rule ip filter INPUT ct status != dnat ct state ! related,established counter accept'
-
-iptables-translate -t filter -A INPUT -m conntrack --ctstate ESTABLISHED,DNAT --ctstatus CONFIRMED -j ACCEPT
-nft 'add rule ip filter INPUT ct status dnat ct state established ct status confirmed counter accept'
-
-iptables-translate -t filter -A INPUT -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT --ctstatus CONFIRMED -j ACCEPT
-nft 'add rule ip filter INPUT ct status != dnat ct state ! related,established ct status confirmed counter accept'
-
-iptables-translate -t filter -A INPUT -m conntrack ! --ctstate RELATED,ESTABLISHED,DNAT ! --ctstatus CONFIRMED -j ACCEPT
-nft 'add rule ip filter INPUT ct status != dnat ct state ! related,established ct status ! confirmed counter accept'
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-21 13:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-08 18:23 [iptables PATCH] extensions: libxt_conntrack: Fail on untranslatable state match Phil Sutter
2026-10-21 10:17 ` Phil Sutter
2026-10-21 10:31   ` Pablo Neira Ayuso
2026-10-21 13:40     ` Phil Sutter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.