Tuesday, August 11, 2015

Homemade 3G Router/APCombo

This article show you how to make your own 3g router using embedded linux, in my case i'm using a raspberry pi 1 Model B. What you need other then the board it self were 3g modem and wifi dongle. The Wifi dongle that were gonna use, should have multiple tx queue features, one from atheros should do the trick.



Ethernet/LAN was bridged with Wifi. In this way client via ethernet can also connected to the network. The bridge hold the system ip address, and no ip assigned to ethernet or wifi.

3G connection were done by umtskeeper which include sakis3g, AP were done by hostapd working along with dnsmasq for dhcp and brigde util to configure the network bridge.

Assume you had working debian wheezy system running on raspberry pi board.
Lets says our working directory is on /opt/apcombo
mkdir /opt/apcombo
chmod 0777 /opt/apcombo
cd /opt/apcombo


From now, i'm gonna work at this folder
Needed package:
sudo apt-get install wpasupplicant hostapd bridge-utils wireless-regdb iw wireless-tools usbutils nano psmisc crda apt-utils dialog syslog-ng dnsmasq nload usb-modeswitch ppp comgt

Stop dnsmasq daemon
sudo update-rc.d /etc/init.d/dnsmasq remove

UMTSKEEPER
To connect to 3g, were gonna use the umtskeeper, which had great features to keep connection alive. Download the latest from this link:
http://mintakaconciencia.net/squares/umtskeeper1/src/umtskeeper.tar.gz

If no usbreset supplied, you can download older one from my backup. Note that you should recompile the usbreset.c to suit your system.
http://www.mediafire.com/download/gocpzzps8o9u0bz/umtskeeper_206.tar.gz

Follow the instruction from the website to establish connection
http://mintakaconciencia.net/squares/umtskeeper/

Now lets make an executable file called "run3g" in /opt/apcombo folder, so we can call it from rc.local during the boot time.
#----------------------------------------------------------------------------
 #!/bin/bash

cd /opt/apcombo/umtskeeper

##Huawei E173
MODEM_ID=12d1:1c05
MODEM_NAME=Huawei
MODEM_PORT=0

##TRI
APN=3data
USER=3data
PASS=3data

sudo ./umtskeeper --sakisoperators "OTHER='USBMODEM' USBMODEM='$MODEM_ID' USBINTERFACE='$MODEM_PORT' APN='CUSTOM_APN' CUSTOM_APN='$APN' APN_USER='$USER' APN_PASS='$PASS'" --sakisswitches "--sudo --console" --devicename 'Huawei' --nat 'no' --log --silent --httpserver &
#----------------------------------------------------------------------------


Do changes to the parameter to suit yours. Verify while execute "./run3g" succesfully connected to internet by following
tail -f /var/log/umtskeeper

or access the html on:
http://10.1.1.1:8000


NO TTYUSBx on RPI 1 Model B
If you are using Raspberry Pi 1 model B, the usb-modeswitch sometimes did not success convert the USB to serial device due to power surge problem. To solve this, i made simple script to reset the dongle. You can put it at rc.local before calling umtskeeper. Let's we call the script "resetall", placed on /opt/apcombo folder.
#-------------------------------------------------------------------------------------
#!/bin/bash

MODEM_NAME=Huawei
MODEM_PORT=0

echo "   Reset Modem"
echo "   trying to find $MODEM_NAME"

MODEM=$(lsusb | grep $MODEM_NAME)
if [ "$MODEM" == "" ];then
   echo "   $MODEM_NAME not found, exiting"
   exit 0
fi
echo "   $MODEM"

if [ -e "/dev/ttyUSB$MODEM_PORT" ];then
   echo "   /dev/ttyUSB$MODEM_PORT found, no need to resetmodem, exiting!"
   #exit 0
fi
echo "   /dev/ttyUSB$MODEM_PORT not found, need to reset"

cd /opt/apcombo/umtskeeper
./resetusb /dev/bus/usb/001/001


echo "   waiting /dev/ttyUSB$MODEM_PORT to come out"
A=true
B=0
while($A)
do
  if [ -e "/dev/ttyUSB$MODEM_PORT" ];then

     echo "   /dev/ttyUSB$MODEM_PORT found!"
      A=false
  fi

  let "B=B+1"
  if [ "$B" == "20" ];then
     echo "   reset all usb timeout, no modem found!"
     A=false
  fi

  sleep 1
done


exit 0
#-------------------------------------------------------------------------------------

If modem name found but no switched mode occur, first try to remove udev on recognizing cd-rom
sudo rm /etc/udev/rules.d/70-persistent-cd.rules

If above keep failing, then check the modem id whether it supported by usb-modeswitch or not by checking the content of /usr/share/usb_modeswitch/configPack.tar.gz

BRIDGE
To create bridge between eth0 and wlan0 do the following

brctl addbr br0
ip addr add 10.1.1.1/24 dev br0

ip addr add 0.0.0.0/0 dev eth0
brctl addif br0 eth0

ip addr add 0.0.0.0/0 dev wlan0
brctl addif br0 wlan0

ip link set eth0 up
ip link set wlan0 up
ip link set br0 up


HOSTAPD

To create access point we will use hostapd. Create hostapd.conf file in /opt/apcombo folder

#--------------------------------------------------------------------------
ssid=mycomboap
wpa_passphrase=1234567890
interface=wlan0
bridge=br0
auth_algs=3
channel=7
driver=nl80211
hw_mode=g
logger_stdout=-1
logger_stdout_level=2
max_num_sta=5
rsn_pairwise=CCMP
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
#--------------------------------------------------------------------------


Changes the parameter to suit your need.
Execute as follow:
hostapd hotsapd.conf -B

DNSMASQ
To provide the dhcp server, execute dnsmasq as follow
dnsmasq -D -b -i br0 -F 10.1.1.10,10.1.1.100,24h

or you can also create a configuration file "dnsmasq.conf"
#-----------------------------------------------------------------------------
domain-needed
bogus-priv
interface=br0
dhcp-range=10.1.1.10,10.1.1.100,12h
#-----------------------------------------------------------------------------


execute with:
dnsmasq -C dnsmasq.conf

IPTABLES
Internet request from br0 need tobe forwarded to ppp0 using masquarade function as follow:

sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE


RESULT
That's it. Now your system are ready. Try connect to internet from your device using this gateway.

AUTOMATED RUN APCOMBO
To get all run automatically at boot time, create main script call "apcombo"

#----------------------------------------------------------------------------
#!/bin/bash
cd /opt/apcombo

./run3g

brctl addbr br0
ip addr add 10.1.1.1/24 dev br0

ip addr add 0.0.0.0/0 dev eth0
brctl addif br0 eth0

ip addr add 0.0.0.0/0 dev wlan0
brctl addif br0 wlan0

ip link set eth0 up
ip link set wlan0 up
ip link set br0 up

hostapd hostapd.conf -B

dnsmasq -D -b -i br0 -F 10.1.1.10,10.1.1.100,24h

sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
#------------------------------------------------------------------------------------------


now call all function from /etc/rc.local by adding these lines:
cd /opt/apcombo
./resetall    #for rpi 1 model B
./apcombo

TOOLS
Use the famous iftop and iptraf to watch detailed packet.

status umtskeeper
tail -f /var/log/umtskeeper.log

stop umtskeeper
sudo pkill umtskeeper;sudo pkill python

signal strength
while true;do clear;sudo comgt sig -d /dev/ttyUSB2;sleep 3;done

bandwith
nload -m -u H ppp0

view ip leases
cat /var/lib/misc/dnsmasq.leases

reset modem only
#--------------------------------------------------------------------------------
#!/bin/bash

MODEM_NAME=Huawei

echo "   Reset Modem"
echo "   trying to find $MODEM_NAME"

A=$(lsusb | grep $MODEM_NAME)

if [ "$A" == "" ];then
   echo "   $MODEM_NAME not found, exiting"
   exit 0
fi

echo "   $A"
B=${A:4:3}
#echo "$B"
C=${A:15:3}
#echo "$C"

echo "   ./resetusb /dev/bus/usb/$B/$C"

./resetusb /dev/bus/usb/$B/$C
#--------------------------------------------------------------------------------

Additional route
if you don't wont to use bridged adapter, then remove all the bridge part, add iptable rule to make both subnet can talk
iptables -A FORWARD -i eth0 -o wlan0 -s 10.1.1.0/24 -d 10.1.2.0/24 -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth0 -s 10.1.2.0/24 -d 10.1.0.0/24 -j ACCEPT


ENJOY

ref: https://wiki.archlinux.org/index.php/Software_access_point

No comments:

Post a Comment