Thursday, March 25, 2010

Internet Connection Failover with PPPoE (DSL) and 3G USB Wireless

Prior to signing up my DSL connection, I also have 3g wireless internet access, which comes with a Huawei E220 dongle. "dmesg" confirms the BSD system can detect this dongle correctly:

ugen1.2: HUAWEI Technologies at usbus1
umass0: HUAWEI Technologies HUAWEI Mobile, class 0/0, rev 1.10/0.00, addr 2 on usbus1
cd0: HUAWEI Mass Storage 2.31 Removable CD-ROM SCSI-2 device

In addition, I have also purchased a TP-Link TD8810, 1 port DSL modem to replace the D-Link DSL modem which bricked. TP-Link modem is cheap ($39 from MSY I last checked), and is configured as an DSL bridge.

Hence, I have created two profiles in the ppp daemon configuration (/etc/ppp/ppp.conf) respectively:

dsl:
set device PPPoE:fxp0 # interface where PPPoE session is dialed out.
set authname
set authkey
set dial
set login
add default HISADDR

3g:
set device /dev/cuaU0.0
set speed 57600
set phone *99\#
set authname
set authkey
set ifaddr 10.0.0.1/0 10.0.0.2/0 255.255.255.0
set vj slotcomp off
add default HISADDR

Choose any name you like for both dsl and 3g. Note that for the 3g profile, the line "set ifaddr" is required otherwise dialing ppp gives you an "unable to create IPCP interface" error.

The ppp connection is invoked through "ppp -nat -ddial DSL/3g". Upon successful establishment, "ifconfig" would display a "tun0" interface with two IP addresses (lo0 and public IP).

Add the following to "/etc/rc.conf" so that everytime BSD starts up, it dials PPPoE out of the DSL link:
router_enable="NO" # So BSD does not removes ppp learnt default route
ppp_enable="YES"
ppp_mode="ddial"
ppp_nat="YES" # Original "natd" has been disabled.
ppp_profile="DSL"

I also wished to implement some failover feature. In case if the PPPoE session over DSL gets disconnected, the system would automatically dial out of USB dongle.

By googling failover scripts, I found a very simple script and modified it slightly to suit my needs:

#!/bin/sh
# 1. ping "www.google.com.au" five times. Count the successful pings by grepping the words "bytes from".
count=$(/sbin/ping -c 5 www.google.com.au | grep -c 'bytes from' )

# 2. if ping failed, then stop VoIP and remove "tun0" ppp interface.
# Re-dial ppp session using 3g profile, with "nat" option.
if [ $count -eq 0 ]; then
/usr/local/etc/rc.d/asterisk stop
/etc/rc.d/ppp stop
/sbin/ifconfig tun0 destroy
/usr/sbin/ppp -nat -ddial 3g
fi

I placed this into crontab and runs it once every minute or so. Not the most sophisticated script I admit, although it certainly does the job.


P.S. I have also read about "mpd" multi-protocol daemon being a good choice in implementing what I wish to achieve, and BSD has some firewall packages supporting connection failover or even load sharing (per TCP session, I suppose?). I must admit I have not look deeply into them, so any comments or ideas to improve this implementation is much welcomed.

1 comment:

  1. I have a rasberry pi and i want to make a fallback between dsl and 3g dongle,for this situation i want to write a shell scriptt ,do you have any idea?

    ReplyDelete