I want an Ubuntu server machine to act as a bidirectional NAT. It has 2 NICs, each connected to a different network. I want it to forward connections received on eth1 at certain ports to certain addresses/ports at the network connected to eth2; and vice versa. I managed to do this by configuring DNAT in the PREROUTING of nat table, and SNAT in the POSTROUTING. The problem begins when both networks happen to be in the same subnet, e.g. 192.168.x.x. I want to make sure that packets received at eth1 are forwarded via eth2 and vice versa, even if there is a single IP address used as a destination address for 2 different machines, one in each network. I thought of using packet marking at the mangle table, in order to set a mark representing the original incoming interface, and then use fwmark to route packets with different marks through different outgoing interfaces. However, I'm not sure whether using this technique really let me choose the outgoing interface, and not only the gateway.
Does any one have an idea how to get this done?