#!/bin/bash # enable packet forwarding echo 1 > /proc/sys/net/ipv4/ip_forward # create the routing tables TABLE1="T1" TABLE2="T2" echo "201 $TABLE1" >> /etc/iproute2/rt_tables echo "202 $TABLE2" >> /etc/iproute2/rt_tables # establish default gateways for the tables DEV1="eth1" DEV2="eth2" GW1="192.168.1.1" GW2="192.168.2.1" ip route add default via $GW1 dev $DEV1 table $TABLE1 ip route add default via $GW2 dev $DEV2 table $TABLE2 # redirect marked packets to the tables ip rule add fwmark 0x1 table $TABLE1 ip rule add fwmark 0x2 table $TABLE2 # mark packets before routing iptables -t mangle -A PREROUTING -s 192.168.3.2 -j MARK --set-mark 0x1 iptables -t mangle -A PREROUTING -s 192.168.3.3 -j MARK --set-mark 0x2 # enable NAT for outgoing packets (tcp/udp/icmp) iptables -t nat -o $DEV1 -A POSTROUTING -p tcp -j MASQUERADE iptables -t nat -o $DEV2 -A POSTROUTING -p tcp -j MASQUERADE iptables -t nat -o $DEV1 -A POSTROUTING -p udp -j MASQUERADE iptables -t nat -o $DEV2 -A POSTROUTING -p udp -j MASQUERADE iptables -t nat -o $DEV1 -A POSTROUTING -p icmp -j MASQUERADE iptables -t nat -o $DEV2 -A POSTROUTING -p icmp -j MASQUERADE