E220 mobile internet

Using a Huawei E220 on Slackware to connect to Telia

This page describes how Henrik Carlqvist connects to Telia mobile internet from Slackware 12.0 Linux using a Huawei E220 HSDPA USB modem.

Safety first!

Before I used this modem my laptop had allways lived safely behind different kind of NAT firewalls when connected to internet. As it now will be connected directly to Telia I decided it would need some local firewall settings. Slackware 12.0 comes without any default firewall settings, but the startup files look for a file /etc/rc.d/rc.firewall which will be run if it exists and is executable. I found a simple rc.firewall on the web which blocks everything except ssh which is allowed only on eth0. The USB modem will use ppp0 so everything incoming will be blocked there:

# rc.firewall for
# Basic Slackware Security

# These two rules set the default policies, i.e. what to do
# if a packet doesn't match any other rule, to drop any
# packet coming into (INPUT) or routing through (FORWARD)
# the box.
iptables -P INPUT DROP
iptables -P FORWARD DROP

# These rules are added (-A) to the INPUT chain.  They allow
# packets from any previously established connections and
# accept anything from the loopback interface.
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -s -d -i lo -j ACCEPT

# This rule added to the INPUT chain accepts any
# ssh connections.
iptables -A INPUT -p tcp --dport 22 -i eth0 -j ACCEPT
E220 SIM card

No PIN code

Most pages on the web about this USB modem seems to agree that the modem is simplier to set up and works more reliable with the PIN code disabled. I used my mobile phone to disable the PIN code of the SIM card.

Activate modem

Initially when connected to the computer the USB device looks like some kind of CDROM. There is also a serial device /dev/ttyUSB0, but it doesn't respond to any Hayes AT commands. To make the USB device behave as a modem a special program, huaweiAktBbo is needed. This program also creates a new device /dev/ttyUSB1 which can be used to query the modem about some statistics. The file huaweiAktBbo.c should be compiled like this:
gcc -lusb -o huaweiAktBbo huaweiAktBbo.c
I copied the compiled huaweiAktBbo to /usr/local/sbin. When run, the program writes some text in a foreign language to stdout, but the important thing is that it creates /dev/ttyUSB0, /dev/ttyUSB1 and /dev/ttyUSB2 which can be seen with dmesg:
Output from dmesg
usb 1-2: new full speed USB device using uhci_hcd and address 2
usb 1-2: configuration #1 chosen from 1 choice
usbcore: registered new interface driver usbserial
drivers/usb/serial/usb-serial.c: USB Serial support registered for generic
usbcore: registered new interface driver usbserial_generic
drivers/usb/serial/usb-serial.c: USB Serial Driver core
drivers/usb/serial/usb-serial.c: USB Serial support registered for GSM modem (1-port)
option 1-2:1.0: GSM modem (1-port) converter detected
usb 1-2: GSM modem (1-port) converter now attached to ttyUSB0
usbcore: registered new interface driver option
drivers/usb/serial/option.c: USB Driver for GSM modems: v0.7.1
Initializing USB Mass Storage driver...
usbcore: registered new interface driver usb-storage
USB Mass Storage support registered.
usb 1-2: USB disconnect, address 2
option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
option 1-2:1.0: device disconnected
usb 1-2: new full speed USB device using uhci_hcd and address 3
usb 1-2: configuration #1 chosen from 1 choice
option 1-2:1.0: GSM modem (1-port) converter detected
usb 1-2: GSM modem (1-port) converter now attached to ttyUSB0
option 1-2:1.1: GSM modem (1-port) converter detected
usb 1-2: GSM modem (1-port) converter now attached to ttyUSB1
option 1-2:1.2: GSM modem (1-port) converter detected
usb 1-2: GSM modem (1-port) converter now attached to ttyUSB2


The important things to know when configuring ppp is that the modem is on /dev/ttyUSB0, the number to dial is *99# and PAP should be used for authentication, but username and password doesn't matter. I used pppsetup to configure ppp, but then made some minor changes to the created files. As /dev/ttyUSB0 wasn't among the choices in pppsetup I had to edit the created files manually afterwards. This is what my files look like:
First pppscript is used to dial *99#:
"" "AT&FH0"
OK "atdt*99#"
Among the options we have the more or less random username www:
# General configuration options for PPPD:
# Uncomment the line below for more verbose error reporting:
# If you have a default route already, pppd may require the other side
# to authenticate itself, which most ISPs will not do.  To work around this,
# uncomment the line below.  Note that this may have negative side effects
# on system security if you allow PPP dialins.  See the docs in /usr/doc/ppp*
# for more information.
asyncmap 0
name "www"
In pap-secrets the random username also has a rather random password 3g:
# PAP authentication file: /etc/ppp/pap-secrets
# This file should have a permission of 600.
# ~# chmod 600 /etc/ppp/pap-secrets
# Username      Server      Password      IP addresses
"www"   *   "3g"
The ip-up script was modified to copy some contents to /etc/resolv.conf. When connecting to other networks I have other scripts writing other DNS servers to that file.
# This file /etc/ppp/ip-up is run by pppd when there's a
# successful ppp connection.
# Put any commands you want run after a successful connection
# in this file.
# Any commands you want printed to the screen should be directed
# to: >/dev/tty0
# Other commands should not be directed to: >/dev/tty0
# The companion file is /etc/ppp/ip-down, it's run when the PPP
# connection ends.
# This file is created when you run pppsetup: Wed Jul  9 23:38:21 CEST 2008
# The environment is cleared before executing this script
# so the path must be reset.
export PATH

# This will print to the screen the local & remote IP address when you
# make a successful ppp connection.  $4 = Local IP $5 = Remote IP
# The CARRIER speed at which you connected will be reported, if it's in
# the /var/log/messages file.  You also need the programs "tail" "cut"
# "tr" "grep" and "syslogd" running for this to work.
# You may have to add S95=46 to your modem init string
# to get your modem to report the DCE = CARRIER speed.
# Example: AT&FS95=46

if [ -s /var/log/messages ] && ( ps xc 2>/dev/null | grep -q syslogd 2>/dev/null ); then
S=`tail -n 30 /var/log/messages 2>/dev/null | grep "CARRIER[^)]" 2>/dev/null | tr -d "^M" 2>/dev/null | cut -d: -f4 2>/dev/null`
echo -n "$S" >/dev/tty0
echo " Local: $4 -> Remote: $5 $1" >/dev/tty0
echo " Local: $4 -> Remote: $5 $1" >/dev/tty0

# If you want to ping the other end to keep the connection open.
# The output from ping will goto >/dev/null, you won't see it.
# Ping -i 60 = send ping every 60 seconds to remote = $5.

#(ping -i 60 $5 &) >/dev/null 2>&1

# If you want sendmail to send any mail in /var/spool/mqueue when
# you connect, remove the # below.

#sendmail -q

# If you want fetchmail to get your mail when you connect and check
# every 300 seconds = 5 minutes for mail, remove the # below.

#fetchmail -d 300

cp /etc/ppp/resolv.conf.ppp /etc/resolv.conf

# End...
The DNS servers could be taken from environment variables in ip-up where they have been passed by the ppp server. However, these DNS servers seem to always work with telia:
search telia.com


To be able to connect as a normal user I used visudo to add the following lines:

lines added to /etc/sudoers with visudo
henca ALL=(ALL) NOPASSWD: /usr/sbin/ppp-on
henca ALL=(ALL) NOPASSWD: /usr/sbin/ppp-off
This allows the user henca to run "sudo /usr/sbin/ppp-on" and "sudo /usr/sbin/ppp-off" with root privileges. Using visudo might be a bit tricky unless you are used to the vi editor. It might be a good idea to read some nice vi tutorial on the web before starting visudo.

It really isn't necessary to run ppp-off manually, simply unplugging the USB modem also works fine for disconnecting. If everything works as expected with udev it is also not necessary to run ppp-on manually as udev can be configured to do this. However, when in an area with bad 3g coverage the USB modem sometimes hangs up and then it is quicker to manually restart ppp-on than disconnecting and reconnect the USB modem.


For everything to simply work automagically when the USB modem is plugged in I created an udev rule:

SUBSYSTEM=="usb", SYSFS{idProduct}=="1003", SYSFS{idVendor}=="12d1", RUN+="/usr/local/sbin/udev-huawei-ppp.sh"
The udev rule file calls a shell script looking like this:
sleep 5
(sleep 5 ; /usr/sbin/ppp-on ; chmod a+rw /dev/tts/USB1) &
The script does all that is needed to connect with ppp, it also allows non root users to access ttyUSB1 which can be used by tools to query the modem about statistics.

With this udev rule it is possible to get connected to internet by simply plugging the USB modem into the computer. Unfortunately the modem is rather slow to identify itself, it usually takes about 20 seconds to get a useful internet connection after the modem has been plugged in.

Ubuntu warning

Once I had everything working perfectly my father borrowed the USB modem for a quick test on his machine. During the quick test on his Ubuntu machine he was unable to get the modem to work. However, once I got the modem back it no longer worked on my machine. The modem started to connect and authenticate with PAP, but then disconnected before ppp0 had gotten any IP address.Without success I tried different Hayes AT strings to reset the modem settings but nothing helped.

I got the USB modem back to the Telia shop where they installed it in a machine running Windows vista. The modem then worked in the Telia shop and after that it also worked in Slackware again. I got the advice to reinstall the Windows drivers if anything like that happens again, but I have no Windows XP or Windows Vista installation were I can do that.

I really don't know what kind of setting in the modem was changed when connected to my fathers Ubuntu machine and I don't want to experiment by once again connecting it to that machine. I didn't find anything about this kind of problem with google, all ubuntu pages about this modem only says that it more or less works out of the box with ubuntu.
Last modified: Sun May 23 21:02:10 CEST 2010