Linux and 3G. Not as tough as you’d expect!

8 03 2008

Hi again everyone. My first wonderful nugget of Linux knowledge is to do with the emerging technology of 3G internet connections. I was set by my boss to start the development of a 3G connectivity solution for our business. I fully expected it to be an absolute cow. I was, however, pleasantly surprised.

Basically, the 3G dongle just acts like a serial modem. Issue it dialling and login commands and you’re all done. Simple as that (well, pretty much!).

The first thing which must be done is to see if the USB dongle is picked up. Slackware 10.2 had a few problems but 11 and onwards were plain sailing.

The first command you will need to use is:

lsusb

This will ensure that your machine has detected that the dongle has been plugged in. I found that Slack 10.2 and 11 picked up the vendor and product codes (12d1 for vendor and 1003 for product) but did not pick up the manufacturer, Huawei. It doesn’t matter, it just makes it more difficult for a n00b to spot that the device has been picked up correctly. A good way to check is to look at the output of lsusb before plugging in the device and after. As is obvious if the output is the same you may have some big ‘ol problems, but I’ve never had this happen. The output you should expect from lsusb is something like the following:

Bus 5 Device 1: ID 0000:0000
Bus 2 Device 1: ID 0000:0000
Bus 4 Device 1: ID 0000:0000
Bus 3 Device 1: ID 0000:0000
Bus 1 Device 3: ID 12d1:1003
Bus 1 Device 1: ID 0000:0000

A problem which you may run in to is the OS picking up the dongle as a usb storage device, as some of them contain the Windows drivers and programs on a flash ROM. The commands I used to circumvent this hurdle were as follows:

dmesg

Which will output a whole bunch of really ominous looking junk. You can ignore most of it (thank god!), and just look for the phrase usb-storage. If it says it has ignored it, you are fine, but if it states that usb-storage has been registered then you’ll have to remove it. Do this through the following commands:

rmmod options
rmmod usb-storage
modprobe usbserial

The next step for me was to add the dongle’s settings to my modprobe.conf. This is located in /etc in Slackware. Add the following line to the end:

options usbserial vendor=0x12d1 product=0x1003

Where 12d1 and 1003 should be replaced by the vendor and product numbers from your lsusb. I’m not entirely sure to this day what this allows to happen, but it makes it work.

Another thing I am utterly stumped by is the fact that I can’t get the USB dongle to register on all 3 serial ports when hotplugged. It will only do it when I start the machine up with the USB dongle already plugged in. I’m positive it’s possible to do this, and I’ll update you if I ever find out how. If anybody already knows how to, feel free to let me know in the comments.

The next step is to make sure you have pppd installed. There’s more than one way to complete this next section with various different programs but I only managed it with pppd. A lot of people seem to prefer wvdial, but I couldn’t get it working on all of the systems I was working with.

You will need to add a file (name it whatever you want) in the directory /etc/ppp/peers and one named the same in /etc/ppp/chat. If these directories don’t exist, create them.

Now… You’ll need to enter a whole bunch of code into both of them. If anybody has done it before, it’s just like setting up a serial modem (kinda). The code I have in /etc/ppp/peers/3G-connect is as follows:

/dev/tts/USB0
connect 'chat -f /etc/ppp/chat/3G-connect'
usepeerdns

I found that a lot of the guides point you at /dev/ttyUSB0 through /dev/ttyUSB2 but old OS’ won’t pick these up and will instead have /dev/tts/USB0 through /dev/tts/USB2, which are located on all systems, the ttyUSB being symbolic links anyway.

The contents of /etc/ppp/chat/3G-connect should read as follows (for Vodafone 3G, other providers’ details can be found at the end of the code):

ABORT BUSY
ABORT 'NO CARRIER'
ABORT ERROR
REPORT CONNECT
TIMEOUT 120
"" "AT&F"
OK "ATE1"
# This command checks the signal you are connected to (defined by
# number)
#OK "AT+COPS?"
# This command checks for which signals ARE available - will
# explain the number from the last command
#OK "AT+COPS=?"
# List which AT commands can be used for this device
#OK "AT&V"
# States to prefer UMTS (3G) but connect to GPRS (2G) if
# unavailable
#OK "AT_OPSYS=3"
# Show signal strength
#OK "AT+CSQ"
OK 'AT+CGDCONT=1,"IP","internet"'
#SAY "Calling Vodaphone GPRS"
TIMEOUT 60
OK "ATD*99***1#"
CONNECT c

The reason I have some commands hashed out is that they caused conflicts with my system. Some can be pretty useful while debugging the system, so by all means unhash and give them a shot.

Other UK 3G providers’ connection details can be found here.

Now all you will need to do to connect to the internet is to type:

pppd call 3G-connect

Just replace 3G-connect with whatever you called the file.

You will still need to replace your DNS settings with the new ones and add a default gateway. They are listed at the end of /var/log/messages, but an easier way to do it is just overwrite your /etc/resolv.conf with /etc/ppp/resolv.conf, which contains your new DNS settings. I have written a little bash script to do this. It’s really badly written, but it works for what I need it for. It runs as follows:

#!/usr/bin/env bash
export GATEWAY="10.64.64.64"
export DNSCONFIG="/etc/resolv.conf"
export IFACE="ppp0"
export PPPDNS="/etc/ppp/resolv.conf"
export PPPRUN="pppd call"
export PPPFILE="3G-connect"
echo "Connecting to UMTS/GPRS network"
$PPPRUN $PPPFILE
sleep 8
echo;echo
echo "Updating $DNSCONFIG."
echo;echo
echo "Checking $PPPDNS"
tail -n2 $PPPDNS
cat $PPPDNS > $DNSCONFIG
echo "Addng default gateway of $GATEWAY to $IFACE"
route add default gw $GATEWAY $IFACE
echo;echo
echo "Connection complete"

Anyway… Hopefully that’s all the info you’ll need to get yourself up and running on the very futuristic world of 3G connectivity. All in all, it’s not all that fantastic… Unreliable speed, it’s expensive, and it’s impossible to get a static IP without forking out thousands of pounds for a dedicated server-side AP. Still useful when my Interwebz are down and it seems to impress our customers when we tell them they can have GPRS connectivity on their device. Now I’m going to go and carry on trying to get GIMP installed on my lappy. 3 hours so far!

Advertisements