For the DNAT to work (in the sense that, for the program on to be able to recognize the replies), "reverse NAT" that changes the source port of the replying traffics from 192.168.30.1
(to 192.168.30.3:2001
) from 2003
to 2002
will need to be performed.
However, when there are traffics coming from 192.168.30.1:2003
to 192.168.30.3:2001
that from conntrack's point of view are not a consequence of the DNAT (because as per conntrack entry created, the host is not the one that initiated the connection), the reverse NAT will be inappropriate.
Therefore, netfilter is "forced" to also perform SNAT for the traffics match with the DNAT rule, so that it can differentiate the replying traffics (that is also from 192.168.30.1:2003
) by the destination 192.168.30.3:$random
.
I assume netfilter will either perform reverse NAT for DNAT (which is an SNAT) before reverse NAT for SNAT (which is a DNAT), or manage to use the destination before the reverse NAT for SNAT (i.e. 192.168.30.3:$random
) as matching for the reverse NAT for DNAT, otherwise the forced SNAT will be pointless. (In the non-reversal case, however, neither of these is true AFAIK: DNAT will be performed in PREROUTING before SNAT in INPUT, and destination matching in the SNAT rule, if any, will use the value resulted in the DNAT)
The thing is, the story above / the "problem" in your question hardly make any sense in reality. Take a two-host wireguard VPN as example: suppose you want to have Endpoint=
set on both hosts (so that either of them can initiate the communication) and do not want the values to be "updated" unexpectedly because of the forced SNAT (assuming that could actually be triggered), what you should do is simply an "always-on" SNAT that "complements" DNAT / is equivalent to the reserve NAT:
iptables -t nat -A INPUT -s 192.168.30.1 -d 192.168.30.3 -p udp --sport 2003 --dport 2001 -j SNAT --to-source :2002
which is normally not necessary in the client-server model because of the automatic reverse NAT for the DNAT.
P.S. You are still not supposed to reach 192.168.30.1:2003
by 192.168.30.1:2003
though, otherwise the forced source NAT will also occur if you reach it again by 192.168.30.1:2002
before the conntrack entry of the former is dropped. The additional SNAT rule in INPUT should not cause you extra trouble either.