From: Andrey Kuznetsov The RFC 6296 section 3.2 have the following requirement: > If the internal subnet number has no mapping, such as being 0xFFFF or > simply not mapped, discard the datagram. This requirement should be applied in two cases: 1. Both source and destination prefixes less than 48, and addr->s6_addr16[3] equal to 0xFFFF. 2. When source prefix shorter than destination prefix, which means, according to RFC 6296 section 3.7, that the shorter prefix should be zero-extended to the length of the longer, all addresses from source prefix that does not match zero-extended prefix is untranslatable. Change code in manner to return false from ip6t_npt_map_pfx() function. The callers interpret the false value as a flag to drop the packet. Signed-off-by: Andrey Kuznetsov Co-developed-by: Nikolai Merinov Signed-off-by: Nikolai Merinov --- net/ipv6/netfilter/ip6t_NPT.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/net/ipv6/netfilter/ip6t_NPT.c b/net/ipv6/netfilter/ip6t_NPT.c index 787c74aa85e3..6a939e1a9fbc 100644 --- a/net/ipv6/netfilter/ip6t_NPT.c +++ b/net/ipv6/netfilter/ip6t_NPT.c @@ -52,13 +52,18 @@ static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt, mask = htonl((1 << (i - pfx_len + 32)) - 1); idx = i / 32; + if ((addr->s6_addr32[idx] & ~mask) != npt->src_pfx.in6.s6_addr32[idx]) + return false; + addr->s6_addr32[idx] &= mask; addr->s6_addr32[idx] |= ~mask & npt->dst_pfx.in6.s6_addr32[idx]; } - if (pfx_len <= 48) + if (pfx_len <= 48) { idx = 3; - else { + if ((__force __sum16)addr->s6_addr16[idx] == CSUM_MANGLED_0) + return false; + } else { for (idx = 4; idx < ARRAY_SIZE(addr->s6_addr16); idx++) { if ((__force __sum16)addr->s6_addr16[idx] != CSUM_MANGLED_0) -- 2.55.0 From: Andrey Kuznetsov The RFC 6296 has the following two requirements: 1. In section 3.2 > If the internal subnet number has no mapping, such as being 0xFFFF or > simply not mapped, discard the datagram. This SHOULD result in an ICMP > Destination Unreachable. 2. In section 3.7 > Although any 16-bit portion of an IPv6 IID could contain 0xFFFF, an > IID of all-ones is a reserved anycast identifier that should not be > used on the network [RFC2526]. If an NPTv6 Translator discovers a > datagram with an IID of all-zeros while performing address mapping, > that datagram MUST be dropped, and an ICMPv6 Parameter Problem error > SHOULD be generated [RFC4443]. Provide the following changes to ip6t_npt_map_pfx() implementation: 1. Change type from bool to u8. Function returns 0 on success and required ICMPv6 message code in case of failure. 2. Return ICMPV6_PARAMPROB when all IPv6 IID parts equal to CSUM_MANGLED_0. 3. Return ICMPV6_DEST_UNREACH when subnet equal to 0xFFFF or packet not mapped. 4. Restore partially modified packet before sending ICMP message reply. Add code to send correct ICMP messages in SNMP and DNPT targets in case of ICMPV6_PARAMPROB and ICMPV6_DEST_UNREACH error statuses. Suggested-by: Alena Romanova Signed-off-by: Andrey Kuznetsov Co-developed-by: Nikolai Merinov Signed-off-by: Nikolai Merinov --- net/ipv6/netfilter/ip6t_NPT.c | 76 ++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/net/ipv6/netfilter/ip6t_NPT.c b/net/ipv6/netfilter/ip6t_NPT.c index 6a939e1a9fbc..d8cc0cc0339e 100644 --- a/net/ipv6/netfilter/ip6t_NPT.c +++ b/net/ipv6/netfilter/ip6t_NPT.c @@ -36,13 +36,14 @@ static int ip6t_npt_checkentry(const struct xt_tgchk_param *par) return 0; } -static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt, - struct in6_addr *addr) +static u8 ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt, + struct in6_addr *addr) { unsigned int pfx_len; - unsigned int i, idx; + unsigned int i, j, idx; __be32 mask; __sum16 sum; + u8 ret = 0; pfx_len = max(npt->src_pfx_len, npt->dst_pfx_len); for (i = 0; i < pfx_len; i += 32) { @@ -52,8 +53,11 @@ static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt, mask = htonl((1 << (i - pfx_len + 32)) - 1); idx = i / 32; - if ((addr->s6_addr32[idx] & ~mask) != npt->src_pfx.in6.s6_addr32[idx]) - return false; + + if ((addr->s6_addr32[idx] & ~mask) != npt->src_pfx.in6.s6_addr32[idx]) { + ret = ICMPV6_DEST_UNREACH; + goto err; + } addr->s6_addr32[idx] &= mask; addr->s6_addr32[idx] |= ~mask & npt->dst_pfx.in6.s6_addr32[idx]; @@ -61,16 +65,21 @@ static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt, if (pfx_len <= 48) { idx = 3; - if ((__force __sum16)addr->s6_addr16[idx] == CSUM_MANGLED_0) - return false; + if ((__force __sum16)addr->s6_addr16[idx] == CSUM_MANGLED_0) { + ret = ICMPV6_DEST_UNREACH; + goto err; + } + } else { for (idx = 4; idx < ARRAY_SIZE(addr->s6_addr16); idx++) { if ((__force __sum16)addr->s6_addr16[idx] != CSUM_MANGLED_0) break; } - if (idx == ARRAY_SIZE(addr->s6_addr16)) - return false; + if (idx == ARRAY_SIZE(addr->s6_addr16)) { + ret = ICMPV6_PARAMPROB; + goto err; + } } sum = ~csum_fold(csum_add(csum_unfold((__force __sum16)addr->s6_addr16[idx]), @@ -79,7 +88,22 @@ static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt, sum = 0; *(__force __sum16 *)&addr->s6_addr16[idx] = sum; - return true; + return 0; + +err: + /* Restore address value before sending ICMP error */ + for (j = 0; j < i; j += 32) { + if (pfx_len - j >= 32) + mask = 0; + else + mask = htonl((1 << (j - pfx_len + 32)) - 1); + + idx = j / 32; + + addr->s6_addr32[idx] &= mask; + addr->s6_addr32[idx] |= ~mask & npt->src_pfx.in6.s6_addr32[idx]; + } + return ret; } static struct ipv6hdr *icmpv6_bounced_ipv6hdr(struct sk_buff *skb, @@ -105,10 +129,18 @@ ip6t_snpt_tg(struct sk_buff *skb, const struct xt_action_param *par) struct ipv6hdr *bounced_hdr; struct in6_addr bounced_pfx; - if (!ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->saddr)) { - icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD, - offsetof(struct ipv6hdr, saddr)); - return NF_DROP; + switch (ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->saddr)) { + case ICMPV6_DEST_UNREACH: + icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); + return NF_DROP; + break; + case ICMPV6_PARAMPROB: + icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD, + offsetof(struct ipv6hdr, saddr)); + return NF_DROP; + break; + default: + break; } /* rewrite dst addr of bounced packet which was sent to dst range */ @@ -130,10 +162,18 @@ ip6t_dnpt_tg(struct sk_buff *skb, const struct xt_action_param *par) struct ipv6hdr *bounced_hdr; struct in6_addr bounced_pfx; - if (!ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->daddr)) { - icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD, - offsetof(struct ipv6hdr, daddr)); - return NF_DROP; + switch (ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->daddr)) { + case ICMPV6_DEST_UNREACH: + icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); + return NF_DROP; + break; + case ICMPV6_PARAMPROB: + icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD, + offsetof(struct ipv6hdr, daddr)); + return NF_DROP; + break; + default: + break; } /* rewrite src addr of bounced packet which was sent from dst range */ -- 2.55.0 From: Andrey Kuznetsov 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 Signed-off-by: Andrey Erokhin Signed-off-by: Andrey Kuznetsov Co-developed-by: Nikolai Merinov Signed-off-by: Nikolai Merinov --- 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 #include +#if IS_ENABLED(CONFIG_NF_CONNTRACK) +#include +#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