Linux: policy based routing - iproute2

Task: A linux box has two aliaces configured on the interface:
em2: ip 10.10.0.10;
em2:0 - ip 10.10.11.1 and em2:1 - ip 10.10.22.2;

There are two routers that traffic can be forwarded to: 10.10.0.100 and 10.10.0.200. All traffic from 10.10.11.1 should be routed to 10.10.0.100 and traffic from 10.10.22.2 should be forwarded to 10.10.0.200.

IP routing

- rules - routing policy database

# ip rule list 0: from all lookup local 32766: from all lookup main 32767: from all lookup default

- show routing tables:

# ip rule list - generate a new table, called 'mytable1' and 'mytable2': # echo 201 mytable1 >> /etc/iproute2/rt_tables # echo 202 mytable2 >> /etc/iproute2/rt_tables - add a new rule to the table (this like putting an interface into the VRF ): # ip rule add from 10.10.11.1 table mytable1 # ip rule add from 10.10.22.2 table mytable2 - add a default route into our tables: # ip route add default via 10.10.0.100 dev em2 table mytable1 # ip route add default via 10.10.0.200 dev em2 table mytable2

 

-in order to make this persistent and survive after reboot, this line can be added to the interface configs:

[server]# cat ifcfg-em2 DEVICE="em2" BOOTPROTO="static" IPADDR="10.10.0.10" NETMASK="255.255.255.0" ONBOOT="yes" ip route add default via 10.10.0.100 dev em2 table mytable1 ip route add default via 10.10.0.200 dev em2 table mytable2 [server]# cat ifcfg-em2:0 DEVICE=em2:0 IPADDR=10.10.11.1 NETMASK=255.255.255.0 ONBOOT=yes ip rule add from 10.10.11.1 table mytable1 [server]# cat ifcfg-em2:1 DEVICE=em2:0 IPADDR=10.10.22.2 NETMASK=255.255.255.0 ONBOOT=yes ip rule add from 10.10.22.2 table mytable1