Raspberry Pi - Huawei HiLink E3256 3G modem to ethernet adapter

This page documents how to configure a Raspberry Pi to act as a USB mobile-broadband-dongle to RJ45
ethernet 'adapter'. This enables mobile-based broadband to feed the 'WAN' port of a commodity home NAT router and then
be shared within the home in exactly the same way as an ADSL or cable-based internet connection.

Introduction

In the absence of a wired broadband connection, it is useful to be able to use a mobile-broadband
dongle with a conventional home router to enable the internet connection to be shared between multiple
devices (including over WiFi), and for those devices to 'see' each other. Dedicated "mobile broadband"
routers into which a 3G/USB dongle can be plugged have been available (e.g. Netgear MBR624G / MBRN3000),
but generally only accept a subset of (obsolete) dongles - and are expensive as they are a limited-market
product. Having recently upgraded to a new Huawei E3256 HiLink mobile-broadband dongle, I set about
configuring a Raspberry Pi as a USB-3G-dongle to RJ45 ethernet 'adapter', which could then feed the
'WAN' port of a commodity home NAT router.

This write-up will start from the very beginning. It assumes you have a Raspberry Pi, a power supply for
it, some kind of a monitor to connect to the Pi, an existing internet connection on an RJ45 ethernet cable
(for setting up), and a Huawei 'HiLink' mobile-broadband dongle. If you have an older non-HiLink dongle then
most of this still applies, but you'll have to seek some additional information on the use of Sakis3G from
elsewhere.

The Rasperry Pi is a small, cheap ($30) computer which runs Linux.
The configuration we require comprises 3 main steps:

Getting the Raspberry Pi to initialise the 3G modem dongle (and thus itself be able to see the internet)

Getting the Raspberry Pi ethernet port configured to act as a DHCP server

Getting the Raspberry Pi to relay internet traffic between the USB dongle and the RJ45 ethernet socket

Setting-up

Preparing a clean Raspian SD-card installation

The SD card image you want is: 2013-02-09-wheezy-raspbian.zip or a later version.
Assuming your main computer is Windows-based, you'll also need to download the Win32DiskImager linked from the same page.
The Win32DiskImager is a bit minimalist and quirky, but does the job.
Use Win32DiskImager to write the Raspian image to your 4GB or greater SD card.
Note that this will irrecoverably overwrite anything that was previously on the SD card!

Running the Pi for the first time

Put the SD card into the Pi

Plug in a USB keyboard into the Pi

Plug a monitor into the Pi (HDMI is preferable, but a composite signal from the phono-socket to a TV/monitor is workable)

If you've already got a home router, then it is strongly preferable to put an ethernet cable between the
Pi ethernet socket and a spare 'LAN' outlet on your router.

Finally, plug in the power-supply/charger - which should be rated for at least 700mA (0.7A)

After some Linux-like boot stuff, you should be greeted by the Raspberry Pi config screen:
Normally you'd want to use the option to expand the SD card to all the space at this point, however this may
complicate backing up the SD card image and restoring it to other cards. If at the moment your main use of the
Pi is as a router, then don't use the expand SD-card image option for now. Exit the config screen when you're done.
If you're using a composite-video TV/monitor then the display may be fairly small/hard to read and you
might want to take a side-track to sort that out before proceeding much further...

Logging in

Username: pi
Password: raspberry

Shutting down the Pi

Like a PC, you shouldn't just switch off the Pi. You're liable to get SD-card corruption if you just
switch off by pulling the power.
Shut down the Pi gracefully by typing:
sudo poweroff
and wait a few seconds for it to shut down, the screen to go blank, and all the LEDs except the red one
on the Pi to go off. Then unplug the 5V power-supply/charger.

Re-running the config program in future

To re-run the config program at any time in future, type:
sudo raspi-config

To initiate a reboot from the command-line

To reboot the Pi from the command-line, type:
sudo reboot

Display tweaks for a basic TV / composite output

When working with a good composite (standard-definition) TV monitor, as opposed to an HDMI monitor, the
following changes to the config.txt file in \boot (or visible when you plug the SD card into a PC) will
make the display a little more readable. Note this is optimised for the PAL system used in the UK; the
overscan params suit my Swann monitor, your mileage may vary. Positive overscan values shrink the image
relative to the TV frame, negative numbers grow it (relative to some arbitrary overscan-enabled nominal).

SSH connection to the Pi

Assuming you've got an already-working ethernet router with another computer, then it will likely be much
easier to do the customisation of the Pi configuration via a SSH (secure-shell / telnet-like) connection
than from the Pi's own keyboard. If nothing else, this allows you to copy-and-paste commands from this web
page into the Pi, eliminating the possibility of typing errors!

When you boot the Pi, at the end of the boot sequence just before the Username login prompt, it tells you its IP address.
On this release of the R-Pi OS, SSH appears to be enabled by default - if not then re-run raspi-config.

Point your SSH client (PuTTY) to the IP address stated by your Pi ... and you're in. Log in as pi / raspberry.

Although having SSH makes following the instructions on this page much easier (and allows cut'n'paste), realistically
you will still need a monitor and keyboard connected to the Pi while performing the setup described. Not least so you
can see what happens if/when ethernet is reconfigured!

Getting the mobile-broadband dongle working on the Pi

Note on the non-requirement for a powered USB hub!

Early Pi's had very limited power on the USB ports and required the use of a powered USB hub;
new Pi's (since the end of 2012, the ones with two large mounting-holes in the PCB) have removed some of
the current-limiting components on the USB connectors and can now supply adequate power for a
USB 3G modem - provided your main power supply is up to the job. Although a powered hub might be useful
if you have problems, with any luck it won't be necessary.

Brief intro to the concept of modeswitch

Huawei dongles - multi-function / multi-personality USB devices. Present themselves to the system into which
you plug them as variously (read-only) CD-ROM device (typically for the Windows installer), an SD or micro-SD card reader,
and a modem or network-adapter device. In the Linux community you will read much about USB modeswitching - which is about
pursuading the device to swap from one personality to the other so we can use the bit we want.

'HiLink' modems - how this makes life easier (mostly)

Older generation Huawei 3G mobile-broadband dongles were interfaced as modem-like devices - and even
supported an AT command-set! For these you have to explicity run a short script to initialise a
connection by dialling a (pseudo) number etc.
The latest Huawei dongles have 'HiLink' - which means that they connect to the mobile-network
automatically when powered-up/plugged-in, present themselves over USB as a 'network' interface device
(rather than a 'modem') ... and even have their own mini-web-server to provide a 'web interface' status
page etc. One drawback of the new HiLink interface is that its IP address is (as far as we know) fixed -
at 192.168.1.1 (for many of their modems - some models may vary). If you already have home-networks using
the 192.168.1.nnn subnet, then this is likely to cause problems. Of course you can normally reconfigure
your other routers to use a different subnet, but this is a bit annoying. Bear this in mind as we progress
with the setup.

I believe HiLink modems can be operated in legacy (non-HiLink, modem-) mode, but that's beyond the scope of
this page.

Note that the Huawei modem is shown with device ID 12d1:1f01 - which corresponds to its "CD-ROM" personality (not the "network-adapter" personality).

(Note it may be possible to make the next steps somehow just using the usb-modeswitch package, however the s3G-utils
solution below works - and apparently s3G includes the usb-modeswitch package anyway)

Next install the s3g-utils package... enter

sudo apt-get install sg3-utils

(note that your Pi needs access to a working internet connection via its ethernet connection at this point)

and if you're retyping this rather than copy-and-pasting it, note that the brackets around modalias are curly-braces!

Save the modified file (ctrl+O, enter, then ctrl+X to quit the editor),

Next customise the /etc/network/interfaces configuration file:

sudo nano /etc/network/interfaces

and paste in, at the end of the file,

allow-hotplug eth1
iface eth1 inet dhcp

Save the modified file (ctrl+O, enter, then ctrl+X to quit the editor).

Now reboot the Pi with a

sudo reboot

Following the reboot, the Pi should now say "My IP address is xxx.xxx.xx.xx 192.168.1.100", where xxx.xxx.xx.xx = the IP address assigned by your setup to the RJ45 ethernet socket, and 192.168.1.100 is the address assigned to the Pi on the Huawei pseudo 'network adapter'.

If there's any problems, then check the interface adapters using

lsusb

.

If you now unplug any RJ45 network that was plugged into the Pi (eg for SSHing in) and reboot the Pi using
the keyboard, and log in using the keyboard... (if you don't unplug the ethernet, the Pi may try to contact the
internet via that route)
You should be able to ping your favorite webserver eg bbc.co.uk and get a response. Congrats - your Pi is
connecting to the internet over your Huawei HiLink dongle. (If you ran startx you could use a webbrowser in there)

We're now well on the way with this project!

Next steps...

So to make the Pi work as a standalone router, there's a bit more config to do. Essentially we need to make it
act as a DHCP server (so it allocates an IP address to whatever it's ethernet port is plugged in to) and to route
traffic between the dongle and its ethernet port.

However this page: http://qcktech.blogspot.co.uk/2012/08/raspberry-pi-as-router.html
seems clearer and more informative in some respects, although their project is using the Pi as a wireless router
(creating a wireless-LAN using a WiFi USB dongle) served with internet from the ethernet socket ... so they're sending
the traffic the other way. My guide below is essentially a hybrid of these two resources.

In this configuration, eth0, which is the Pi's ethernet socket, is configured to receive an IP address from the DHCP
server on whatever it is connected to.

Going forward however, we want to give the ethernet port a static (LAN) IP address
and make the Pi run a DHCP server on that port. This allows the Pi to take charge when it is connected via an RJ45
ethernet cable either directly to a PC or to the WAN-side of a domestic router.

Before making the changes, which may at least temporarily lock us out of conveniently SSHing in, download and install
the DHCP server package using:

Note also that I've extended the default and maximum DHCP lease times to well-above the 600second (10-minute)
default. You can read up about tuning lease-times elsewhere, but setting them too short may cause network hiccups
especially with streaming connections such as Skype!

And restart the DHCP server sudo /etc/init.d/isc-dhcp-server restart

Actually you probably need to have set the network interface up first, to avoid errors!

sudo nano /etc/network/interfaces

Again we're using the same 192.168.50.xxx address space - as set up in the DHCP section above.

If I'm honest, I'm not 100% sure of the all the implications of using "auto eth0" and/or "auto eth1" in the
/etc/network/interfaces file ... but it doesn't seem to work as well (at all?) with the auto lines active, so they're
either not present, or are commented out in my file. I'm also not entirely sure what "iface default inet dhcp"
will do, and whether that might add any unwanted uncertainty.

Setting up the routing tables to route traffic between the Pi's ethernet port and the dongle

To make this temporary change permanent, write the present (temporary) iptables configuration to a file

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Now we need to use a script to restore those tables when the Pi is booted. There's more than one way of doing this,
but an easy way is to add it into the end of the /etc/network/interfaces configuration file. Open the
file for editing:

sudo nano /etc/network/interfaces

And then at the end of the file, insert the line:

up iptables-restore < /etc/iptables.ipv4.nat

Save the file and exit the editor.

Reboot now, and your computer should then be able to access the internet via the Pi!

to be continued/tidied up... (May 2013) ...

Note to self: Ahhhh probably need to limit DHCP serving to eth0 to avoid problems, then can set to authoritative.
Otherwise can get away with non-authorativite without limiting ports

Future /further refinements/enhancements

These are things I'd like to do, but haven't yet

Ad-hoc powerdown-safe

Would be better if we didn't have to sudo poweroff all the time. Suggested that if we made it a 'read only'
installation, with no logging etc then it'd be safe to pull the power ad-hoc.

Support a second dongle with some load-sharing or redundancy/fallback

I have some older (non-HiLink) dongles, and SIM cards for other networks. It'd be useful to support the
older modems and/or to have a setup where the system would automatically use the second dongle/connection
if the first suffered a hiccup. A true real-time load-balencing system would be nice, but I think (unfortunately,
in the general case) this would require cooperation from a server elsewhere on the net to re-combine the
pipes at the other end.

Make a 3G WiFi 'hotspot'

I'm not particularly interested in doing this as I'm happy to use the extra functionality of a
fully fledged wireless/router, but you could put a USB WiFi device in the Pi and share the internet
connection from the mobile-broadband dongle over WiFi. Others have done this - see hints at...

Other future possible Pi projects

USB-WiFi interface for a hard-drive (and maybe media server?) - to have in a different room from my WiFi router

USB satellite TV-receiver decoder/display - the Pi has a very powerful graphics engine