[PATCH 3/3] netfilter: ip6t_NPT: Adding conntrack compatibility to NPT sessions

From: Nikolai Merinov

Date: Sun Nov 01 2026 - 15:39:50 EST


From: Andrey Kuznetsov <a.kuznetsov@xxxxxxxxxxxxxxxxxx>

The ip6t_NPT was implemented without connection tracking support and
official netfilter documentation for libip6t_DNPT recommends to use
NOTRACK target to disable connection tracking for translated
flow. Such solution blocks possibility to use the NPTv6 implementation
on systems with stateful IPv6 firewall.

Add function that alters conntrack reply tuple for expected replies in
both SNPT and DNPT targets. Add optional dependency on NF_CONNTRACK.

Co-developed-by: Andrey Erokhin <a.erokhin@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Andrey Erokhin <a.erokhin@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Andrey Kuznetsov <a.kuznetsov@xxxxxxxxxxxxxxxxxx>
Co-developed-by: Nikolai Merinov <n.merinov@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Nikolai Merinov <n.merinov@xxxxxxxxxxxxxxxxxx>
---
net/ipv6/netfilter/Kconfig | 1 +
net/ipv6/netfilter/ip6t_NPT.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+)

diff --git a/net/ipv6/netfilter/Kconfig b/net/ipv6/netfilter/Kconfig
index 81daf82ddc2d..f5fa62541db8 100644
--- a/net/ipv6/netfilter/Kconfig
+++ b/net/ipv6/netfilter/Kconfig
@@ -290,6 +290,7 @@ config IP6_NF_TARGET_MASQUERADE
config IP6_NF_TARGET_NPT
tristate "NPT (Network Prefix translation) target support"
depends on IP6_NF_NAT || NFT_COMPAT
+ depends on NF_CONNTRACK || !NF_CONNTRACK
help
This option adds the `SNPT' and `DNPT' target, which perform
stateless IPv6-to-IPv6 Network Prefix Translation per RFC 6296.
diff --git a/net/ipv6/netfilter/ip6t_NPT.c b/net/ipv6/netfilter/ip6t_NPT.c
index d8cc0cc0339e..6ffe53a9572b 100644
--- a/net/ipv6/netfilter/ip6t_NPT.c
+++ b/net/ipv6/netfilter/ip6t_NPT.c
@@ -12,6 +12,10 @@
#include <linux/netfilter_ipv6/ip6t_NPT.h>
#include <linux/netfilter/x_tables.h>

+#if IS_ENABLED(CONFIG_NF_CONNTRACK)
+#include <net/netfilter/nf_conntrack.h>
+#endif
+
static int ip6t_npt_checkentry(const struct xt_tgchk_param *par)
{
struct ip6t_npt_tginfo *npt = par->targinfo;
@@ -121,6 +125,30 @@ static struct ipv6hdr *icmpv6_bounced_ipv6hdr(struct sk_buff *skb,
_bounced_hdr);
}

+static void ip6t_npt_ct(struct sk_buff *skb, const struct xt_action_param *par)
+{
+#if IS_ENABLED(CONFIG_NF_CONNTRACK)
+ struct nf_conn *ct;
+ enum ip_conntrack_info ctinfo;
+ struct nf_conntrack_tuple new_tuple;
+
+ ct = nf_ct_get(skb, &ctinfo);
+
+ if (!ct || nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct))
+ return;
+
+ new_tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
+
+ if (xt_hooknum(par) == NF_INET_PRE_ROUTING ||
+ xt_hooknum(par) == NF_INET_LOCAL_OUT)
+ new_tuple.src.u3 = (union nf_inet_addr)ipv6_hdr(skb)->daddr;
+ else
+ new_tuple.dst.u3 = (union nf_inet_addr)ipv6_hdr(skb)->saddr;
+
+ nf_conntrack_alter_reply(ct, &new_tuple);
+#endif
+}
+
static unsigned int
ip6t_snpt_tg(struct sk_buff *skb, const struct xt_action_param *par)
{
@@ -151,6 +179,7 @@ ip6t_snpt_tg(struct sk_buff *skb, const struct xt_action_param *par)
ip6t_npt_map_pfx(npt, &bounced_hdr->daddr);
}

+ ip6t_npt_ct(skb, par);
return XT_CONTINUE;
}

@@ -184,6 +213,7 @@ ip6t_dnpt_tg(struct sk_buff *skb, const struct xt_action_param *par)
ip6t_npt_map_pfx(npt, &bounced_hdr->saddr);
}

+ ip6t_npt_ct(skb, par);
return XT_CONTINUE;
}

--
2.55.0