Sample script for internet connection failover between WAN and 3G on OpenWRT / Gargoyle

7 02 2013

First you need to check if your modem is properly recognized by OpenWRT.
Then you need to configure your 3G interface. Edit /etc/config/network:

# vi /etc/config/network

...
config 'interface' '3g'
option 'proto' '3g'
option 'auto' '0'
option 'ifname' '3g'
option 'device' '/dev/ttyUSB0'
option 'service' 'umts'
option 'apn' 'your_APN_name'
option 'username' ''
option 'password' ''
option 'defaultroute' '0'
option 'peerdns' '0'
option 'keepalive' '3'
option 'maxwait' '20'
option 'dns' '8.8.8.8'
...

You need to customize your firewall config file in order to apply your rules to both WAN and 3G interfaces. To do so edit /etc/config/firewall:

# vi /etc/config/firewall

...
config 'zone'
option 'name' 'wan'
option 'network' 'wan 3g'
option 'input' 'REJECT'
option 'output' 'ACCEPT'
option 'forward' 'REJECT'
option 'masq' '1'
option 'mtu_fix' '1'
...

Below you can find sample bash script that is responsible for moving internet connection between WAN and 3G interface in case of failure.

# cat /etc/net_failover.sh

#!/bin/bash
#
#set -x
#### configuration ####
DEVPATH=/sys/bus/usb/devices
VID=12d1
PID=1506
WAN_IF='eth0.2' # enter name of WAN interface
WAN_GW='192.168.211.1' # enter IP for default gateway on WAN interface
WAN_IP='192.168.211.33' # enter IP for WAN interface
WAN_NETMASK='255.255.255.0' # enter netmask for WAN interface
GSM_IF='3g-3g' # enter name of 3G interface
GSM_GW='10.64.64.64' # enter IP for default gateway on 3G interface
PING_HOST='8.8.8.8' # enter IP of host that you want to check,
                    # if you lose connection to this host failover occurs
#### EOF configuration ####

#### functions ####
function 3g_connect
{
        # this function connects modem to 3G network -
        # usually used when we lose internet connection on WAN interface and failover occurs
        # or when 3G connection is disconnected by operator (example: Aero2 operator in Poland)
        for dev in $DEVPATH/*; do
                cvid=$([ -f $dev/idVendor ] && cat $dev/idVendor)
                cpid=$([ -f $dev/idVendor ] && cat $dev/idProduct)
                #echo "$dev, $cvid:$cpid"
                [ "$VID" = "$cvid" ] && [ "$PID" = "$cpid" ] && devdir=$dev
        done
        echo 0 > $devdir/bConfigurationValue
        sleep 3
        echo 1 > $devdir/bConfigurationValue
        sleep 5
        logger -t wan_failover "Connecting to 3G network..."
        ifup 3g
        sleep 15
}

function 3g_disconnect
{
        # this function disconnects modem from 3G network -
        # usually used when we recover internet connection on WAN interface
        for dev in $DEVPATH/*; do
                cvid=$([ -f $dev/idVendor ] && cat $dev/idVendor)
                cpid=$([ -f $dev/idVendor ] && cat $dev/idProduct)
                #echo "$dev, $cvid:$cpid"
                [ "$VID" = "$cvid" ] && [ "$PID" = "$cpid" ] && devdir=$dev
        done
        echo 0 > $devdir/bConfigurationValue
        sleep 5
        echo 1 > $devdir/bConfigurationValue
        sleep 5
        logger -t wan_failover "Disconnecting from 3G network..."
        ifdown 3g
        sleep 5
}

function 3g_gateway
{
        # this function add default gateway for 3G interface -
        # usually executed when moving internet connection from WAN to 3G interface
        logger -t wan_failover "Moving internet connection to $GSM_IF (3G) via gateway $GSM_GW..."
        route add default gw $GSM_GW
}

function wan_gateway
{
        # this function remove default gateway for 3G interface -
        # usually executed when moving internet connection from 3G to WAN interface
        logger -t wan_failover "Moving internet connection to $WAN_IF (WAN) via gateway $WAN_GW..."
        route del default gw $GSM_GW
}

#### EOF functions ####

modem_chk=`lsusb |grep ${VID}:${PID}`
wan_chk=`ifconfig |grep "$WAN_IF"`

if [[ ! "$modem_chk" && "$wan_chk" ]]; then
        # case info: 3G modem not found, WAN interface is up and running - exiting
        # you can put some actions here, if needed
        exit 1
elif [ ! "$wan_chk" ]; then
        # case info: WAN interface down or not configured - trying to up interface and exit
        # you can put some actions here, if needed
        ifup wan
        exit 2
fi

wan_ping=`ping -q -4 -c 3 -I $WAN_IF $PING_HOST | grep received |awk '{print $4}'`
gsm_chk=`ifconfig |grep "$GSM_IF"`

if [[ "$wan_ping" -gt 0 && ! "$gsm_chk" ]]; then
        # if there is ping reply from PING_HOST IP on WAN interface and 3G interface is down then exit
        # you can put some actions here, if needed
        exit 0
elif [ ! "$gsm_chk" ]; then
        # else if 3G interface is down (and there is no connection PING_HOST IP on WAN interface)
        # then establish 3G connection
        # you can put some actions here, if needed
        3g_connect
fi

gsm_ping=`ping -q -4 -c 3 -I $GSM_IF $PING_HOST | grep received |awk '{print $4}'`

if [[ "$wan_ping" -eq 0 && "$gsm_ping" -eq 0 ]]; then
        # if there is no connection to PING_HOST from WAN nor 3G interface then exit
        # you can put some actions here, if needed
        exit 5 # brak pingu do hosta przez obydwa interfejsy
fi

default_gw=`ip route|grep default|head -n1|awk '{print $3}'`

if [ -z $default_gw ]; then
        # if default gateway not set then add gateway IP from WAN interface,
        # NOTE: ifconfig is used here to make sure that WAN interface is up and running.
        # you can put some actions here, if needed
        ifconfig $WAN_IF $WAN_IP netmask $WAN_NETMASK
        route add default gw $WAN_GW
fi

if [[ "$wan_ping" -eq 0 && "$gsm_ping" -gt 0 && "$default_gw" = "$WAN_GW" ]]; then
        # case info: move internet connection from WAN to 3G interface.
        # if connection to PING_HOST is: broken on WAN, available on 3G.
        # and default gateway IP is set to WAN interface then add gateway from 3G interface
        # you can put some actions here, if needed
        3g_gateway
elif [[ "$wan_ping" -gt 0 && "$default_gw" = "$GSM_GW" ]]; then
        # case info: move internet connection from 3G to WAN interface only if default gw set to 3G.
        # if connection to PING_HOST is: available on WAN and default gateway IP is set to 3G interface
        # then remove gateway from 3G interface and disconnect 3G modem
        # you can put some actions here, if needed
        wan_gateway
        3g_disconnect
elif [[ "$wan_ping" -gt 0 && "$gsm_ping" -gt 0 ]]; then
        # case info: move internet connection from 3G to WAN interface if PING_HOST is available on both.
        # if connection to PING_HOST is: available on WAN and 3G interface
        # then disconnect 3G modem
        # you can put some actions here, if needed
        3g_disconnect
fi
Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: