Skip to contentSkip to conversation

netfilter: nat: restore default DNAT behavior

RFC PATCH Superseded Kyle Swenson 2 participants 3 messages Jan 29, 2024 – Feb 7, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover netfilter: nat: restore default DNAT behavior

Hello,

We have noticed what appears to be a regression from v4.4 in the DNAT port selection logic in nf_nat_core.c. Specifically, we're expecting an iptables command like

    iptables -t nat -A PREROUTING -p tcp -d 10.0.0.2 -m tcp --dport 32000:32010
    -j DNAT --to-destination 192.168.0.10:21000-21010

to DNAT traffic sent to 10.0.0.2:32000-32010 to 192.168.0.10:21000-21010. We use the range on the LAN side to handle tuple collisions that might occur with a single port specified via --to-destination.

The behavior we're seeing currently, however, is the behavior we'd expect if we were passing --random to iptables (except without passing --random): the DNAT'd port is random within the range.

Through my naive debugging, I've arrived at this RFC patch and have included it mostly to illustrate the behavior our userspace is expecting with the above iptables command. The hope is that I can be educated with what other folks expect the behavior to be, or what I can change in our iptables command to get the behavior we're expecting.

Thanks so much for your time,
Kyle

--
v1 -> v2:
- Restrict to NF_NAT_MANIP_DST so that we don't have predictable source port conflict resolution, without changing the --range and --random override behavior.

#1 lore raw

RFC PATCH v2 1/1 netfilter: nat: restore default DNAT behavior

When a DNAT rule is configured via iptables with different port ranges,

iptables -t nat -A PREROUTING -p tcp -d 10.0.0.2 -m tcp --dport 32000:32010 -j DNAT --to-destination 192.168.0.10:21000-21010

we seem to be DNATing to some random port on the LAN side. While this is expected if --random is passed to the iptables command, it is not expected without passing --random. The expected behavior (and the observed behavior in v4.4) is the traffic will be DNAT'd to 192.168.0.10:21000 unless there is a tuple collision with that destination. In that case, we expect the traffic to be instead DNAT'd to 192.168.0.10:21001, so on so forth until the end of the range.

This patch is a naive attempt to restore the behavior seen in v4.4. I'm hopeful folks will point out problems and regressions this could cause elsewhere, since I've little experience in the net tree.

---
 net/netfilter/nf_nat_core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/nf_nat_core.c +4 -1
index c3d7ecbc777c..016c816d91cb 100644
@@ -549,12 +549,15 @@ static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
 	}
 
 find_free_id:
 	if (range->flags & NF_NAT_RANGE_PROTO_OFFSET)
 		off = (ntohs(*keyptr) - ntohs(range->base_proto.all));
-	else
+	else if ((range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL) ||
+		 maniptype != NF_NAT_MANIP_DST)
 		off = get_random_u16();
+	else
+		off = 0;
 
 	attempts = range_size;
 	if (attempts > NF_NAT_MAX_ATTEMPTS)
 		attempts = NF_NAT_MAX_ATTEMPTS;
 
Signature · 1 line
2.43.0
#2 lore raw
Quoted 13 lines · Kyle Swenson

When a DNAT rule is configured via iptables with different port ranges,

iptables -t nat -A PREROUTING -p tcp -d 10.0.0.2 -m tcp --dport 32000:32010 -j DNAT --to-destination 192.168.0.10:21000-21010

we seem to be DNATing to some random port on the LAN side. While this is expected if --random is passed to the iptables command, it is not expected without passing --random. The expected behavior (and the observed behavior in v4.4) is the traffic will be DNAT'd to 192.168.0.10:21000 unless there is a tuple collision with that destination. In that case, we expect the traffic to be instead DNAT'd to 192.168.0.10:21001, so on so forth until the end of the range.

Quoted 3 lines

This patch is a naive attempt to restore the behavior seen in v4.4. I'm hopeful folks will point out problems and regressions this could cause elsewhere, since I've little experience in the net tree.

Would you post this without RFC tag and add provide a Fixes: tag.

Thanks.