Policy Based Routing

From fakedWiki
Jump to: navigation, search

If you need to have two default gateways on two network interfaces in two separate subnets, you can set up "Policy Based Routing". This will route the replies for incoming traffic out on the same interface, and use the default gateway for e.g. internet access.

Configure both network interfaces, first interface as the one to use the default gateway.

auto eth0 eth1

interface eth0 inet static
  address 192.168.1.1
  netmask 255.255.255.0
  gateway 192.168.1.254

interface eth1 inet static
  address 192.168.2.1
  netmask 255.255.255.0

Create routing tables for each interface

echo "1 incoming" >> /etc/iproute2/rt_tables
echo "2 outgoing" >> /etc/iproute2/rt_tables

Set up routing and rules for the first interface

ip route add 192.168.1.0/24 dev eth0 src 192.168.1.1 table incoming
ip route add default via 192.168.1.254 dev eth0 table incoming
ip rule add from 192.168.1.1/32 table incoming
ip rule add to 192.168.1.1/32 table incoming

Set up routing and rules for the second interface

ip route add 192.168.2.0/24 dev eth1 src 192.168.2.1 table outgoing
ip route add default via 192.168.2.254 dev eth1 table outgoing
ip rule add from 192.168.2.1/32 table outgoing
ip rule add to 192.168.2.1/32 table outgoing