Huawei 3G modem on Ubuntu

From Crashcourse Wiki

Jump to: navigation, search

Contents

[edit] Overview

Quick overview of how to set up PPP on a fairly standard Ubuntu laptop using a Huawei E367 3G USB dongle. I'm assuming you have at least an activated SIM card and provider info for the PPP connection.

[edit] The prerequisites

You should obviously have a PPP-enabled kernel, which you can prove by checking for the existence of /dev/ppp:

$ ls -l /dev/ppp
crw------- 1 root root 108, 0 Mar 15 05:43 /dev/ppp
$

If that's not there, go back and fix it.

Also, if you're going to use minicom to configure the modem, make sure it's a recent version as older versions had a known problem cleanly exiting runscripts. In my case:

$ minicom -v
minicom version 2.5 (compiled May  2 2011)
... snip ...
$

That's not the absolutely latest version, but it's new enough to have solved the runscripts problem.

[edit] Configuring minicom

Do whatever it takes to configure minicom to chat with the modem; specifically, make sure minicom will be talking to the port /dev/ttyUSB0, as that's where the modem is going to show up.

[edit] Plugging in the modem

On your Ubuntu host, start watching the kernel log file with:

$ sudo tail -f /var/log/kern.log

and insert the modem, at which point you should see a pile of output as the kernel recognizes the modem and creates the appropriate device files for it:

Mar 15 12:49:26 oneiric kernel: [25561.633901] usb 2-1.3: new high-speed USB device number 7 using ehci_hcd
Mar 15 12:49:26 oneiric kernel: [25561.828562] scsi10 : usb-storage 2-1.3:1.0
Mar 15 12:49:26 oneiric kernel: [25561.829156] scsi11 : usb-storage 2-1.3:1.1
Mar 15 12:49:27 oneiric kernel: [25562.826816] scsi 10:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
Mar 15 12:49:27 oneiric kernel: [25562.827264] scsi 11:0:0:0: Direct-Access     HUAWEI   TF CARD Storage       PQ: 0 ANSI: 2
Mar 15 12:49:27 oneiric kernel: [25562.831341] sr1: scsi-1 drive
Mar 15 12:49:27 oneiric kernel: [25562.831700] sr 10:0:0:0: Attached scsi CD-ROM sr1
Mar 15 12:49:27 oneiric kernel: [25562.831948] sr 10:0:0:0: Attached scsi generic sg3 type 5
Mar 15 12:49:27 oneiric kernel: [25562.832896] sd 11:0:0:0: Attached scsi generic sg4 type 0
Mar 15 12:49:27 oneiric kernel: [25562.836100] sd 11:0:0:0: [sdc] Attached SCSI removable disk
Mar 15 12:49:27 oneiric kernel: [25562.969290] usb 2-1.3: USB disconnect, device number 7
Mar 15 12:49:31 oneiric kernel: [25567.008110] usb 2-1.3: new high-speed USB device number 8 using ehci_hcd
Mar 15 12:49:32 oneiric kernel: [25567.202778] option 2-1.3:1.0: GSM modem (1-port) converter detected
Mar 15 12:49:32 oneiric kernel: [25567.203003] usb 2-1.3: GSM modem (1-port) converter now attached to ttyUSB0
Mar 15 12:49:32 oneiric kernel: [25567.203437] option 2-1.3:1.3: GSM modem (1-port) converter detected
Mar 15 12:49:32 oneiric kernel: [25567.203602] usb 2-1.3: GSM modem (1-port) converter now attached to ttyUSB1
Mar 15 12:49:32 oneiric kernel: [25567.203747] option 2-1.3:1.4: GSM modem (1-port) converter detected
Mar 15 12:49:32 oneiric kernel: [25567.203950] usb 2-1.3: GSM modem (1-port) converter now attached to ttyUSB2
Mar 15 12:49:32 oneiric kernel: [25567.204432] scsi12 : usb-storage 2-1.3:1.5
Mar 15 12:49:32 oneiric kernel: [25567.205012] scsi13 : usb-storage 2-1.3:1.6
Mar 15 12:49:33 oneiric kernel: [25568.205201] scsi 12:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
Mar 15 12:49:33 oneiric kernel: [25568.205964] scsi 13:0:0:0: Direct-Access     HUAWEI   TF CARD Storage       PQ: 0 ANSI: 2
Mar 15 12:49:33 oneiric kernel: [25568.209410] sr1: scsi-1 drive
Mar 15 12:49:33 oneiric kernel: [25568.209805] sr 12:0:0:0: Attached scsi CD-ROM sr1
Mar 15 12:49:33 oneiric kernel: [25568.210120] sr 12:0:0:0: Attached scsi generic sg3 type 5
Mar 15 12:49:33 oneiric kernel: [25568.211762] sd 13:0:0:0: Attached scsi generic sg4 type 0
Mar 15 12:49:33 oneiric kernel: [25568.215232] sd 13:0:0:0: [sdc] Attached SCSI removable disk

If none of that shows up, you have a problem; most probably, your kernel is not configured properly, so you need to configure and build a new kernel.

[edit] Your minicom runscript

Create a minicom runscript that, at a minimum, accepts a couple enviromnent variables that represent the APN and dialstring you're going to use. Here's my example runscript:

#!/bin/sh
#echo "\n" for shell for modem send "\n" print "" for minicom "runscript" print"\n" do man runscript
#echo "test"
print "\n"
print "Minicom dialing script\n"

send "ATH"
sleep 1

send ATZ

expect {
       "OK"
       timeout 4 exit 1
}
print "\n"

sleep 1

send ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0

expect {
       "OK"
       timeout 4 exit 1
}
print "\n"

sleep 1
send AT+CGDCONT=1,\"IP\",\"$(APN)\"

expect {
       "OK"
       timeout 4 exit 1
}

print "\n"
sleep 1
send ATDT$(DIALSTRING)

expect {
       "CONNECT" goto CONNECT
       timeout 4 exit 1
}

print "\n"
CONNECT:

print "\nModem is connected\n"
exit

[edit] Writing the top-level script

Here's an example of a script you'd actually invoke to configure the modem, then bring up PPP; I named this script 3g.sh:

#!/bin/sh

usage_and_exit () {
        echo "Error, $*, exiting"
        exit 1
}

# Sample invocation of this script:
#
# $ ./3g.sh "randomname" "internet.com" '*99***1#'
#
# The first argument is simply a name you choose for that provider
# that will be useful for upcoming diagnostics; currently, it's
# unused so you can put whatever you want in there.

PROVIDER=$1
APN=$2
DIALSTRING=$3

echo "PROVIDER = ${PROVIDER}"
echo "APN = ${APN}"
echo "DIALSTRING = ${DIALSTRING}"

[ -n "${PROVIDER}" ]            || usage_and_exit "Null PROVIDER string"
[ -n "${APN}" ]                 || usage_and_exit "Null APN string"
[ -n "${DIALSTRING}" ]          || usage_and_exit "Null DIALSTRING string"

[ -c /dev/ppp ]                 || usage_and_exit "/dev/ppp doesn't exist"
[ -c /dev/ttyUSB0 ]             || usage_and_exit "/dev/ttyUSB0 doesn't exist"
[ -f /var/lock/LCK..ttyUSB0 ]   && usage_and_exit "ttyUSB0 already locked"

export APN DIALSTRING

minicom -S dialscript &

sleep 10

echo "Returned from dialscript, let's check if everything looks sane."

if [ -f /var/lock/LCK..ttyUSB0 ] ; then
        echo "Lock file still there, it really shouldn't be."
fi

sudo pppd debug -detach defaultroute /dev/ttyUSB0 38400 &

Note how, once minicom runs the runscript and returns, the script waits a few seconds, then invokes pppd with the appropriate options. If you want, you can always remove that from the calling script and run it manually yourself if you wanted to do any testing in between.

NOTE how, on Ubuntu, if you're not root, you need to invoke pppd with sudo. If you move all of this to a system where you're running as root by default, don't forget to remove the sudo. Depending on how your Ubuntu host is configured, you might have to remove that line from the script and run it manually.

[edit] Your network configuration before starting PPP

Make a note of your current network configuration since, if you're already on the net, bringing up PPP might confuse your routing table and you'll have to clarify your default route. On my host, here are my currently active interfaces:

$ ifconfig -s
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
lo        65536 0     25525      0      0 0         25525      0      0      0 LRU
wlan0      1500 0    463176      0      0 0        371329      0      0      0 BMRU
$

So you can see I have an active wireless interface. And here's my routing table, with the wireless interface as the default (actual IP addresses hidden):

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         XX.XX.XX.XX     0.0.0.0         UG    0      0        0 wlan0
XX.XX.XX.XX     0.0.0.0         255.255.0.0     U     1000   0        0 wlan
XX.XX.XX.XX     0.0.0.0         255.255.255.192 U     2      0        0 wlan0
$

[edit] Starting PPP

If I choose to start PPP manually at the command line, as long as I've supplied valid APN and dialstring, I should see something like this (again, IP addresses masked for security):

Using interface ppp0
Connect: ppp0 <--> /dev/ttyUSB0
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0xd30119b6> <pcomp> <accomp>]
rcvd [LCP ConfReq id=0x0 <asyncmap 0x0> <auth chap MD5> <magic 0x3081b9e> <pcomp> <accomp>]
sent [LCP ConfNak id=0x0 <auth pap>]
rcvd [LCP ConfAck id=0x1 <asyncmap 0x0> <magic 0xd30119b6> <pcomp> <accomp>]
rcvd [LCP ConfReq id=0x1 <asyncmap 0x0> <auth pap> <magic 0x3081b9e> <pcomp> <accomp>]
sent [LCP ConfAck id=0x1 <asyncmap 0x0> <auth pap> <magic 0x3081b9e> <pcomp> <accomp>]
sent [LCP EchoReq id=0x0 magic=0xd30119b6]
sent [PAP AuthReq id=0x1 user="oneiric" password=<hidden>]
rcvd [LCP DiscReq id=0x2 magic=0x3081b9e]
rcvd [LCP EchoRep id=0x0 magic=0x3081b9e d3 01 19 b6]
rcvd [PAP AuthAck id=0local  IP address 25.48.119.191
remote IP address 10.64.64.64
primary   DNS address 64.71.255.205
secondary DNS address 64.71.255.253
Script /etc/ppp/ip-up started (pid 19535)
Script /etc/ppp/ip-up finished (pid 19535), status = 0x0
local  IP address 25.48.119.191
remote IP address 10.64.64.64
primary   DNS address 64.71.255.205
secondary DNS address 64.71.255.253
Script /etc/ppp/ip-up started (pid 19535)
Script /etc/ppp/ip-up finished (pid 19535), status = 0x0
PAP authentication succeeded
... snip ...
local  IP address xx.xx.xx.xx
remote IP address xx.xx.xx.xx
primary   DNS address xx.xx.xx.xx
secondary DNS address xx.xx.xx.xx
Script /etc/ppp/ip-up started (pid 19535)
Script /etc/ppp/ip-up finished (pid 19535), status = 0x0

And some other things to verify. First, confirm pppd is running:

$ ps -ef | grep pppd
root     19517  3236  0 13:06 pts/0    00:00:00 sudo pppd debug -detach defaultroute /dev/ttyUSB0 38400
root     19518 19517  0 13:06 ttyUSB0  00:00:00 pppd debug -detach defaultroute /dev/ttyUSB0 38400
rpjday   19634  3236  0 13:10 pts/0    00:00:00 grep --color=auto pppd
$

And make sure the interface is up:

$ ifconfig ppp0
ppp0      Link encap:Point-to-Point Protocol  
          inet addr:xx.xx.xx.xx  P-t-P:xx.xx.xx.xx  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:94 (94.0 B)  TX bytes:145 (145.0 B)

$

All that looks good, almost done.

[edit] Fixing the routing table

If your PPP connection is the only active interface, it should already be set as the default route in your routing table. However, if you're doing this on a Ubuntu host that's already on the network, your routing table might look like this:

$ route -n
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.231.195  0.0.0.0         UG    0      0        0 wlan0
10.64.64.64     0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 wlan0
172.22.231.192  0.0.0.0         255.255.255.192 U     2      0        0 wlan0
$

To truly test your PPP connectivity, you can drop the other net connection or set PPP as your default (or both):

$ sudo route add default dev ppp0

So once you verify that you have *only* a PPP connection:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 ppp0
xx.xx.xx.xx     0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
$

that's a good time to check that you're online and that you've set up the 3G modem properly:

$ ping yahoo.com
PING yahoo.com (98.138.253.109) 56(84) bytes of data.
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=1 ttl=46 time=449 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=2 ttl=46 time=478 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=3 ttl=46 time=487 ms
... snip ...
Personal tools