Pages

Monday, March 21, 2011

Multigateway routing in debian

Multigateway routing in debian

Enable forwarding by echoing to ip_forward

echo "1" >> /proc/sys/net/ipv4/ip_forward

create the table entries in rt_table by typing following

echo "10 chitti" >> /etc/iproute2/rt_table
echo "20 bhitti" >> /etc/iproute2/rt_table


You can add the routing table by following snippet


ip r show | grep -Ev ^default | while read ROUTE; \
do; \
ip r a $ROUTE table chitti;\
ip r a $ROUTE table bhitti;\
done;

Now set the default gateways for each table

ip r a default via chittigw table chitti
ip r a default via bhittigw table bhitti


Need to mark the packets coming from the proper source networks

iptables -t mangle -A PREROUTING -s ip.of.chitti.network/netmask -d 0.0.0.0/0 -j MARK --set-MARK 10
iptables -t mangle -A PREROUTING -s ip.of.bhitti.network/netmask -d 0.0.0.0/0 -j MARK --set-MARK 20

NAT them with whatever interface

iptables -t nat -A POSTROUTING -s ip.of.chitti.network/netmask -d 0.0.0.0/0 -j SNAT --to ip.of.chittii.interface
iptables -t nat -A POSTROUTING -s ip.of.bhitti.network/netmask -d 0.0.0.0/0 -j SNAT --to ip.of.bhittii.interface

Add the fwmark from the table in route

ip rule add from fwmark 10 table chitti
ip rule add from fwmark 20 table bhitti

Bingo..

Multiple gateways should be working now.

PS : If you have setup the WANs on a single interface with aliases the case is much easier..

Reference : http://linux-ip.net/html/adv-multi-internet.html

No comments:

Post a Comment