Linux router with diffent gateways for incomming and outgoing connections

Posted by nkout on Server Fault See other posts from Server Fault or by nkout
Published on 2012-04-05T14:04:12Z Indexed on 2012/04/06 11:33 UTC
Read the original article Hit count: 204

Filed under:
|
|

I have the following topology:

  • LAN Users:192.168.1.2 - 254 (192.168.1.0/24)
  • gateway1: 192.168.2.2/24 used for all outgoing connections of LAN users (default gateway)
  • gateway2: 192.168.3.2/24 used for incoming services (destination NAT, ports 80,443 are forwarded to 192.168.2.1)
  • linux router-server R
    • eth0 192.168.1.1/24: LAN
    • eth1 192.168.2.1/24: WWAN1
    • eth2 192.168.3.1/24: WWAN2

I want to:

  • route all outgoing traffic coming from LAN and R via 192.168.2.2
  • route the responses to incoming connections via 192.168.3.2

My config:

ifconfig eth0 up 192.168.1.1 netmask 255.255.255.0
ifconfig eth1 up 192.168.2.1 netmask 255.255.255.0
ifconfig eth2 up 192.168.3.1 netmask 255.255.255.0
echo 0 >/proc/sys/net/ipv4/ip_forward
route add default gw 192.168.2.2
iptables -t nat -A POSTROUTING -d !192.168.0.0/16 -j MASQUERADE

I want to add iptables rule to mark incoming traffic from WWAN2 and send back the responses to WWAN2, while keeping default gateway on WWAN1

© Server Fault or respective owner

Related posts about linux

Related posts about iptables