#! /bin/bash
#
# multi-home-route      Set up multi-homed routing for multiple connections to the internet
#
# description: Sets up multi-homed routing to properly handle multiple connections to the Internet.
#

# Source function library.
. /etc/init.d/functions

# See how we were called.
case "$1" in
  start)
        echo -n "Setting up Multi-Homed Routing..."
        ip route flush table connection1
        ip route flush table connection2
        ip route add table connection1 to 192.168.221.0/24 dev eth0
        ip route add table connection2 to 192.168.222.0/24 dev eth1
        ip route add table connection1 to default via 192.168.221.254 dev eth0
        ip route add table connection2 to default via 192.168.222.254 dev eth1
        ip rule add from 192.168.221.0/24 table connection1 priority 10
        ip rule add from 192.168.222.0/24 table connection2 priority 20
        ip route flush cache
        echo
        ;;
  stop)
        echo -n "Removing Multi-Homed Routing..."
        ip route flush table connection1
        ip route flush table connection2
        ip rule del from 192.168.221.0/24 table connection1 priority 10
        ip rule del from 192.168.222.0/24 table connection2 priority 20
        ip route flush cache
        ;;
  restart|reload)
        cd "$CWD"
        $0 stop
        $0 start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload}"
        exit 1
esac

exit 0
