Archive for the openwrt Category

I recently worked on making OpenWRT work with a 3G usb modem. Here’s how I did it – hopefully it’ll save you some hours of frustration…

My Setup:

RouterStation Pro
NovaTel MC760Virgin Mobile’s Version
Virgin Mobile’s Broadband2Go – conveniently $40/month – no contract
OpenWRT – Backfire 10.03.1-rc3 – openwrt-ar71xx-ubnt-rspro-squashfs-factory.bin

I chose the RS Pro because it was the most powerful router that I could find (being a work project, I wasn’t pinching pennies). The novatel MC760 supposedly had good linux support so I was crossing my fingers and hoping that the setup would work for me. Virgin Mobile’s no contract was attractive for development. Also, the MC760 is supported on the Sprint and Verizon networks, so when this goes into production, we could easily (theoretically :-P) switch over to one of those ISPs if they proved to be more reliable or cheaper with a contract.

Pre-requisites:
You have to activate your modem first on a windows or OS X machine. It uses some web interface and you put in some numbers that is stored into the modem. Do that first.

Install Latest OpenWRT:
Wrote about that already… Read Upgrading Routerstation Pro Firmware

Detecting USB Device:
# stores repo list in /tmp/ - you must run this everytime you want to use opkg install after a reboot
opkg update
opkg install kmod-usb-uhci kmod-usb-serial kmod-usb-storage usbutils sdparm comgt
opkg install microcom # for testing

The MC760 has this annoying CDROM drive of drivers that must be ejected before the real modem will pop up. If you do:
lsusb
Bus 002 Device 003: ID 1410:5031 Novatel Wireless

You want to see 1410:6002. To get that to work:
/usr/bin/sdparm --command=eject /dev/`dmesg|grep CD-ROM -A1 |grep "type 5"|tail -1|awk '{ print $6 }'`
# searches dmesg for the CDROM device to eject

Make sure it has changed to 6002. If not, your modem is not enabled. After this, time to get the serial device working.
rmmod usbserial
insmod usbserial vendor=0x1410 product=0x6002

dmesg should show:

usbserial_generic 2-1:1.0: generic converter detected
usb 2-1: generic converter now attached to ttyUSB0

Now let’s check if the serial modem works…

microcom -D/dev/ttyUSB0

The screen will look like it’s frozen, type ATZ and hit enter. If you receive an OK, that means we’re doing good! Type ~x to quit.

Dialing the Modem:

We now have to setup the pppd client to dial in and get IP networking going.


mkdir /etc/ppp/peers/

make the following files:

/etc/ppp/peers/virgin

#demand
persist
#idle 300
maxfail 0
/dev/ttyUSB0
460800
noauth
defaultroute
user internet
password internet
usepeerdns
connect ‘/usr/sbin/chat -t3 -f /etc/ppp/peers/virgin_chat’

/etc/ppp/peers/virgin-chat

” ATZ
OK ‘ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0′
TIMEOUT 12
OK ATDT#777
TIMEOUT 22
CONNECT

Let me highlight a few things about the config files… #777, that’s the Sprint, Verizon, and Virgin Mobile dial in number. The username and password is useless, you can put anything in there. Make sure your /dev/ttyUSB* device is the correct device. The persist command will make pppd try indefinitely to reconnect.

After setting the config files, try pppd call virgin. Check logread to see if the connection worked. If it did, you should see:

Aug 19 02:58:02 OpenWrt daemon.notice pppd[1261]: pppd 2.4.4 started by root, uid 0
Aug 19 02:58:05 OpenWrt daemon.info pppd[1261]: Serial connection established.
Aug 19 02:58:05 OpenWrt daemon.info pppd[1261]: Using interface ppp0
Aug 19 02:58:05 OpenWrt daemon.notice pppd[1261]: Connect: ppp0 <--> /dev/ttyUSB0

ifconfig should show a ppp0 device and ping 4.2.2.2 will work.

Sharing the internet with the other computers:
If you try pinging 4.2.2.2 from a computer connected to the LAN ports, you’ll notice that NAT is not working and you get a nasty “Destination Net Unreachable”. This is because we haven’t configured this ppp0 device to work with openwrt’s UCI so iptables does not know to NAT packets to the ppp0 device.

Let’s make ppp0 the default WAN device.

Note: If you want to do load balancing and more complex stuff, you’re on your own.. :-P But if you figure it out, please let me know. I would be interested to know how you set it up.

/etc/config/network – make these changes:

#config interface wan
# option ifname eth0
# option proto dhcp

config interface wan
option ifname ppp0
option proto none

After this, run the following:

ifup wan
#Unfortunately, this wipes out the ip address info for the ppp0 device, so you will need to reset your ppp0 device.
kill `pidof pppd`
pppd call virgin

DNS Settings:

If you didn’t notice, DNS was down. You can fix that by going to /etc/config/dhcp and changing resolvfile to option resolvfile ‘/tmp/resolv.conf.ppp’.

Make Settings Permament:

First, let’s make the usbserial device automatically load.

echo usbserial vendor=0x1410 product=0x6002 > /etc/modules.d/60-usb-serial

There are many ways to start the connection on boot, some make /etc/init.d/ scripts, or bind it to when the usb modem becomes active (in /etc/hotplug.d/usb) or use openwrt network start scripts (we sorta can’t since openwrt isn’t managing our connection). I am paranoid so I made a cronjob that checks every minute if the modem is connected. I also bound this same script to the usb modem inserted hook.

/etc/hotplug.d/usb/20-novatel_modem

#!/bin/sh

# Copyright (C) 2010 bashusr.com
case “$ACTION” in
add)
/etc/scripts/initmodem.sh &
;;
esac

mkdir /etc/scripts and add this:

/etc/scripts/initmodem.sh

#!/bin/sh
## bashusr.com
if kill -0 `cat /tmp/initmodem.lock` 2> /dev/null; then
exit
fi

echo $$ > /tmp/initmodem.lock

if (pidof pppd > /dev/null); then
if (/usr/sbin/lsusb|grep -q ‘Novatel’) && !(/usr/sbin/lsusb|grep -q ‘1410:5031′); then
exit
else
echo “`date` – modem was or is unplugged! killing pppd…” >> /tmp/novatel_modem.log
kill `pidof pppd`
fi
fi

if (/usr/sbin/lsusb | grep -q ‘1410:5031′); then
while (/usr/sbin/lsusb | grep -q ‘1410:5031′); do
sleep 5
if ! [ -e “/dev/`dmesg|grep CD-ROM -A1 |grep “type 5″|tail -1|awk ‘{ print $6 }’`” ]; then
echo “`date` – /dev/`dmesg|grep CD-ROM -A1 |grep “type 5″|tail -1|awk ‘{ print $6 }’` device doesn’t exist! do something
sleep 5
continue
fi
echo “`date` – trying to eject” >> /tmp/novatel_modem.log
/usr/bin/sdparm –command=eject /dev/`dmesg|grep CD-ROM -A1 |grep “type 5″|tail -1|awk ‘{ print $6 }’` 2>> /tmp/novatel_modem
done

while !(/usr/sbin/lsusb | grep -q ‘1410:6002′); do
echo “`date` – waiting for modem device to come online” >> /tmp/novatel_modem.log
sleep 2
done

fi

if (/usr/sbin/lsusb | grep -q ‘1410:6002′); then

if (pidof pppd); then
echo “`date` – pppd already running, not dialing again…” >> /tmp/novatel_modem.log
else
echo “`date` – novatel modem online… dialing!” >> /tmp/novatel_modem.log
ifup wan
ifconfig ppp0 down
pppd call virgin
fi
fi

and last but not least, crontab -e and add

* * * * * /etc/scripts/initmodem.sh

Now you should be able to do anything to your router and if it turns back on, it should connect to the internet by itself. I tried rebooting, pulling the power supply, pulling out and plugging back in the USB modem… as long as it is on and the usb modem is connected it works! I hope this works for you too!

References & Resources:
chatfile from dd-wrt docs (ironically, I couldn’t get this to work under dd-wrt, but their config information was useful!)
Sprint Linux Config Guide
http://josefsson.org/openwrt/dongle.html – this guy’s postings were most useful.

“Seb” wins for putting the instructions for upgrading the firmware in the Routerstation Pro in one simple place.

http://www.usualcoding.eu/post/2010/01/15/Flash-OpenWRT-on-the-Ubiquiti-RouterStation-Pro

If you’re wondering (like I did), which version to use (jffs2, squashfs, factory, or sysupgrade?) I found the right one to use is the squashfs version (because of this) and the factory version. The sysupgrade version is for you to upload into /tmp and use the sysupgrade command. Unfortunately, that doesn’t work on the kamikaze version so you’ll have to tftp it at least once. Make sure you select the factory version if you’re doing tftp!! I haven’t tried, but the sysupgrade version could brick your router if you don’t install it from sysupgrade.

The latest version at the time of this posting is backfire 10.03.1-rc3:

openwrt-ar71xx-ubnt-rspro-squashfs-factory.bin