Skip to contentSkip to conversation

SNAT/DNAT for ongoing UDP flows

DISCUSSION Shu Tao 2 participants 2 messages Feb 19, 2003 netfilter

lore
Share & tools
lore

Shu Tao

author
#0 lore raw

Hi,

My application scenario is as follows:

A Linux box with Netfilter is connected to two different ISP's and acting like a
gateway to other computers in the LAN. For some reason it needs to switch its
outgoing traffic of a UDP flow from one ISP connection to the other. In the meantime, the switching needs to be transparent to the application (assume
that the process at the other end is a silly UDP receiver and receives whatever
packets coming to it as long as the dst port is the same).

The way of doing that seems to either alter the src or dst address of that flow's packets
so that the routing engine will forward them differently. Is there a way of doing that
with iptables? Obviously, applying SNAT/DNAT will not affect the packets unless
the application reinitialize the flow.

Any suggestion? Many thanks in advance.

S.T.

#1 lore raw
Quoted 5 lines · Shu Tao

A Linux box with Netfilter is connected to two different ISP's and acting like a gateway to other computers in the LAN. For some reason it needs to switch its outgoing traffic of a UDP flow from one ISP connection to the other.

[...]

Quoted 2 lines

The way of doing that seems to either alter the src or dst address of that flow's packets so that the routing engine will forward them differently.

It seems to be that, on the outgoing side, you'd have to switch BOTH source and destination addresses.

Quoted 2 lines

Is there a way of doing that with iptables? Obviously, applying SNAT/DNAT will not affect the packets unless the application reinitialize the flow.

Not quite. Once a rule in the NAT table has selected a particular NATting, that decision is remembered in the individual connection's conntrack entry, until that entry times out due to no traffic. Even if you change the rule in the NAT table, previous connections run unaffected. This is a consious design decision, lying at the core of the implementation, and it's the Right Thing(tm) for practically all real world requirements - except yours :)

So, what you need, is a method to break this permanent association, or, alternatively, a method to avoid the scenario. I'll start with that second alternative, and a proposal that should be "portable":

A) use an application level gateway on the firewall machine, or inside the firewall machine. If sitting on the firewall itself, you use the REDIRECT target to get the packets from the inside to the application level gateway. The application level gateway, using local IP addresses from ISP-A and ISP-B, can easily make two different outgoing connections, without further NATting there. The application level proxy is a simple UDP datagram recv/sender.

This would be my preferred solution, unless the kernel/user/kernel copying is too slow for your application. On a 1Ghz P-III machine that has no other significant CPU usage, the solution should be good for about 10000 packets per second (don't believe me - measure it!)

Back to the other alternative, here are some ways to achive breaking of the existing conntrack:

B) change NAT table rule, stop traffic for 130 seconds (e.g. by suitable DROP rules in the PREROUTING mangle table). This should work, because UDP connections time out after 120 seconds of inactivity. But it's probably undesirable to have such a long wait.
C) Look at the ctnetlink extension in patch-o-matic. It should allow you (I hope) to explicitly purge individual connection tracking entries from userlevel.
D) Write an iptables target extension, probably designed to be used in the mangle table PREROUTING chain, which effects the same connection tracking purge. It will be interesting to see what parameters such a target would have to take.

Hope this helps.

best regards
Patrick