NAT'ing two sub-nets thru Linux
This page describes how to configure Linux to properly route network traffic between two separate networks. This works the same for wireless and wired connections.
IP Address of Middle
Have a computer that has a NIC with an IP address on each network. I’m not sure if this is called a router or a bridge or something else. For this page, I call this computer the “middle”. Use
ifconfig to verify that each network card is up. Try pinging a host on each network as a second check.
Routing of Middle
Check the routing for each of the cards in the middle. If pinging failed, it was probably because routing is broken. Use
netstat -nr to quickly see the routes to each subnet. Then, just add routes to the appropriate NIC for each subnet. Adding a route will look like this:
route add -net 192.168.0.0 netmask 255.255.255.0 dev eth0 route add -net 192.168.1.0 netmask 255.255.255.0 dev eth1
NAT in Middle
Turn on NAT’ing on the middle:
echo 1 > /proc/sys/net/ipv4/ip_forward
Route the clients
For each client machine, simply adjust the routing to use the middle as a gateway. I’m assuming the middle has the IP address of .5 in each network (eg 192.168.0.5 and 192.168.1.5).
A computer on the 192.168.1.0 network would use the following command to NAT to 192.168.0.0:
route add -net 192.168.0.0 netmask 255.255.255.0 gateway 192.168.1.5 dev eth0
This tells the the client machine that all 192.168.0.x traffic goes thru eth0 to 192.168.1.5 for further routing. At that point it will use the routing tables in middle jump to the 192.168.0.x subnet and go to the target machine.