24 Jan 2011, 21:38

Linux IProute - Source based routing


Source based routing is usefull if you want to divert your traffic to different outgoing network interfaces based on their source IP. Of course this is only usefull if your system has more than on IP address and network interface.

The key to source based routing is the concept of multiple routing tables. Each of these routing tables has its own set of rules, including lo and a default gateway. These tables are created using the ip route command with a table NR suffix. The packets enter these tables if they are direct there by ip rules create with the ip rule command.

If you want to handle all traffic from sourceip /netmask via interface ethX, you’d need two rules for this traffic and some default rules for the remaining traffic as show below.

ip rule flush

ip rule add prio 200 from <sourceip/netmask> lookup 250
ip rule add prio 32700 from all lookup main
ip rule add prio 32750 from all lookup default

ip route add dev lo table 250
ip route add destip/net via dev ethX table 250
ip route add default via <defaultgw> table 250 <defaultgw> <sourceip>