Multiwan Linux

From MS Computech
Jump to: navigation, search

Multiwan Linux Source

#!/bin/sh
# dual gateway using 3 nic and 4 ips with one alias

# isp1 on eth0
# isp2 on eth1
# eth2 = for LAN group1
# eth2:1= for LAN group2
#
# Created by mambang 2007

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
IFCONFIG=/sbin/ifconfig
NETWORK=/etc/init.d/networking
IP=/sbin/ip
NAME="MULTI-GATEWAY"
DESC1="ROUTING TABLE"
GATEWAY1=192.168.101.1
GATEWAY2=192.168.102.1
NET1=192.168.101.2  #ip eth0
NET2=192.168.102.2  #ip eth1
LAN1=192.168.1.1  #ip LAN1 = eth2
LAN2=192.168.2.1 #ip LAN2 = eth2:2
SUB1=192.168.101.0/24  #sub local
SUB2=192.168.102.0/24  #sub local
SUB3=192.168.1.0/24 #subnet client LAN1
SUB4=192.168.2.0/24 #subnet client LAN2
DEV1=eth0
DEV2=eth1
DEV3=eth2
DEV4=eth2:1

case "$1" in
    start)
              echo -n "Setup $DESC1: "

              # Setup source IP routing rules for DSL 1

              $IP route add $SUB1 dev $DEV1 src $NET1 table T1
              $IP route add default via $GATEWAY1 table T1
              $IP route add $SUB2 dev $DEV2 src $NET2 table T2
              $IP route add default via $GATEWAY2 table T2
              $IP route add $SUB1 dev $DEV1 src $NET1
              $IP route add $SUB2 dev $DEV2 src $NET2
              $IP route add default via $GATEWAY1
              $IP rule add from $NET1 table T1
              $IP rule add from $NET2 table T2
              $IP route add $SUB3 dev $DEV3 table T1
              $IP route add $SUB2 dev $DEV2 table T1
              $IP route add 127.0.0.0/8 dev lo table T1
              $IP route add $SUB4 dev $DEV4 table T2
              $IP route add $SUB1 dev $DEV1 table T2
              $IP route add 127.0.0.0/8 dev lo table T2

              echo "$NAME1"

              ;;

        stop)

              echo "Stop $DESC1 $NAME ... "
              $IP route flush all
              $IFCONFIG $NET1 down
              $IFCONFIG $NET2 down
              $IFCONFIG $LAN1 down
              $IFCONFIG $LAN2 down
              $NETWORK restart

              echo "Default routing table restored"

              ;;

        *)

              N=/etc/init.d/splitgateway
              echo "Use Command: $N {start|stop}" >&2

              exit 1

              ;;
esac

exit 0

Multiwan Script by Robert Kurjata

http://www.ssi.bg/~ja/#routes

#!/bin/bash
# This script is done by : Robert Kurjata Sep, 2003.
# feel free to use it in any usefull way

# CONFIGURATION
IP=/sbin/ip
PING=/bin/ping

#--------------- LINK PART -----------------
# EXTIFn - interface name
# EXTIPn - outgoing IP
# EXTMn  - netmask length (bits)
# EXTGWn - outgoing gateway
#-------------------------------------------

# LINK 1
EXTIF1=eth1
EXTIP1=
EXTM1=
EXTGW1=

# LINK 2
EXTIF2=eth2
EXTIP2=
EXTM2=
EXTGW2=

#ROUTING PART
# removing old rules and routes

echo "removing old rules"
${IP} rule del prio 50 table main
${IP} rule del prio 201 from ${EXTIP1}/${EXTM1} table 201
${IP} rule del prio 202 from ${EXTIP2}/${EXTM2} table 202
${IP} rule del prio 221 table 221
echo "flushing tables"
${IP} route flush table 201
${IP} route flush table 202
${IP} route flush table 221
echo "removing tables"
${IP} route del table 201
${IP} route del table 202
${IP} route del table 221

# setting new rules
echo "Setting new routing rules"

# main table w/o default gateway here
${IP} rule add prio 50 table main
${IP} route del default table main

# identified routes here
${IP} rule add prio 201 from ${EXTIP1}/${EXTM1} table 201
${IP} rule add prio 202 from ${EXTIP2}/${EXTM2} table 202

${IP} route add default via ${EXTGW1} dev ${EXTIF1} src ${EXTIP1} proto static table 201
${IP} route append prohibit default table 201 metric 1 proto static

${IP} route add default via ${EXTGW2} dev ${EXTIF2} src ${EXTIP2} proto static table 202
${IP} route append prohibit default table 202 metric 1 proto static

# mutipath
${IP} rule add prio 221 table 221

${IP} route add default table 221 proto static \
            nexthop via ${EXTGW1} dev ${EXTIF1} weight 2\
            nexthop via ${EXTGW2} dev ${EXTIF2} weight 3

${IP} route flush cache

while : ; do
  ${PING} -c 1 ${EXTGW1}
  ${PING} -c 1 ${EXTGW2}
  sleep 60
done

Another Routing for multiple uplinks

http://www.debian-administration.org/articles/377

nano /etc/iproute2/rt_tables
#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
200     uplink1
201     uplink2

Network interface config

nano /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
#LAN interface
iface eth0 inet static
        address 10.0.0.1
        netmask 255.255.255.0
#Uplink1
auto eth1
iface eth1 inet static
        address 1.0.0.1
        netmask 255.255.255.0
        post-up ip route add 1.0.0.2/32 dev eth1 src 1.0.0.1 table uplink1
        post-up ip route add default via 1.0.0.2 table uplink1
        post-up ip rule add from 1.0.0.1 table uplink1
        post-down ip rule del from 1.0.0.1 table uplink1
#Uplink2
auto eth2
iface eth2 inet static
        address 2.0.0.1
        netmask 255.255.255.0
        post-up ip route add 2.0.0.2/32 dev eth2 src 2.0.0.1 table uplink2
        post-up ip route add default via 2.0.0.2 table uplink2
        post-up ip rule add from 2.0.0.1 table uplink2
        post-down ip rule del from 2.0.0.1 table uplink2

make it up and balance

ifup -a
ip route add default scope global nexthop via 1.0.0.2 dev eth1 weight 1 nexthop via 2.0.0.2 eth2 weight 1