Thursday, November 1, 2012

Raspberry Router



I wanted to set up my Raspberry Pi as a 3G router for my small home network,  and as a file server too. It worked out nicely, so here are some notes and links that helped me.  My internet connection is provided by a USB GSM modem (Huawei E173), so the first task is to get this device running on the Pi. This was surprisingly easy, thanks to the sakis3g script.

I learned that once you start messing with the network, downloading new stuff gets tricky - so it's best to get everything at once.
sudo apt-get install ppp isc-dhcp-server usb-modeswitch 
and download sakis3g
wget "http://www.sakis3g.org/versions/latest/armv4t/sakis3g.gz"

EDIT: The Sakis3G web page is down at the moment, but a local copy of the script is here: sakis3g.gz (version 0.2.0.e).

The Pi can probably not supply enough current for the 3G modem through the USB ports (thanks for the tip), so I connected the modem through a powered hub. Without the hub, the modem did not work.



The sakis script was easy to use, it asked me to select my modem and then the network name (APN). When the connection works, one can write the selections in a configuration file to avoid having to enter them again. In sakis: "More options..." -> "Generate success report" shows a lot of info. Last in the list, there are the options needed to connect. Copy these (except  --console and --interactive) to the file /etc/sakis3g.conf with one option per line. Sakis does not like any extra whitespace on these lines. Now it should be possible to connect with  a simple sudo sakis3g connect.

I found a nice guide to setting up the pi as a router. This worked well for me, I just had to change which network interface is connected to the local network (eth0 for me) and which is connected to the outside (ppp0).






A small complication has turned up with the GSM modem. The modem is a multimode USB device, and must be switched into the correct mode before actually working as a modem. Sakis handles this automatically (using the usbmodeswitch program I installed above). The trouble is, that when the modem is switched, it changes its USB device ID. So the device option to pass to sakis is different the first time and any later time sakis is run without unplugging or powering down the modem in between.
The broadband company seems to drop the connection after 24 h. I think I will put a cron script for reconnecting at 5 in the morning or so. I came up with the following command for finding the current USB ID of  the modem and passing it to sakis.
sudo sakis3g connect USBMODEM=\
`lsusb | grep Huawei | sed 's/.*ID \([a-f0-9:]*\) Huawei.*/\1/'`
sed is a strange creature! grep finds the relevant line, and the sed command extracts the ID number between 'ID ' and ' Huawei'. This is of course assuming that exactly one huawei device is present.

I post some of the configuration files here. In this later post, I added a WLAN stick and got the Pi set up as a WLAN access point, these files are for a WLAN device as well. If you don't have one, just ignore those sections.
my /etc/networks/interfaces:

auto lo
iface lo inet loopback

auto eth0 #static address on eth0
iface eth0 inet static
address 192.168.50.1
netmask 255.255.255.0
network 192.168.50.0
broadcast 192.168.50.255
gateway 192.168.50.1

auto wlan0 #static address on wlan0
iface wlan0 inet static
address 10.10.0.1
netmask 255.255.255.0

And then /etc/dhcp/dhcpd.conf

ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;

#for the eth0 subnet
subnet 192.168.50.0 netmask 255.255.255.0 {
range 192.168.50.10 192.168.50.250;
option broadcast-address 192.168.50.255;
option routers 192.168.50.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4; #Google DNS
interface eth0;
}

#for the wireless network on wlan0
subnet 10.10.0.0 netmask 255.255.255.0 {
        range 10.10.0.25 10.10.0.50;
        option domain-name-servers 8.8.8.8, 8.8.4.4;
        option routers 10.10.0.1;
        interface wlan0;
}

20 comments:

  1. Why did you use a HUB? Creating more collision domains.

    ReplyDelete
  2. OHHHH USB Hub, Disregard my comment -.-

    ReplyDelete
    Replies
    1. Yes, it is a powered USB hub, necessary since the raspberry does not provide enough power on the USB ports. For the ethernet connections, a switch is definitely preferable. Ours is the cheapest 100M switch I could find :)

      Delete
  3. Saksi seems to have removed his site from the web, is there a mirror for the script or something? please help

    ReplyDelete
    Replies
    1. Indeed, the Sakis website http://www.sakis3g.org/ is down at the moment. I hope it comes back, as the script is really great. I don't know a mirror, but can share the version I use myself. I'll try to put up a link later today, when I get back to my Pi.

      Delete
    2. I just put my copy of the sakis script here: http://users.abo.fi/fjansson/blog/sakis3g.gz
      This is version 0.2.0.e which I am using on my Pi.

      Delete
  4. Im doing exactly the same thing, but i keep on getting [....] Running /etc/init.d/networking restart is deprecated because it may not r[warnble some interfaces ... (warning).
    [....] Reconfiguring network interfaces...Internet Systems Consortium DHCP Client 4.2.2
    Copyright 2004-2011 Internet Systems Consortium.
    All rights reserved.
    For info, please visit https://www.isc.org/software/dhcp/

    Listening on LPF/eth0/b8:27:eb:a6:0c:bf
    Sending on LPF/eth0/b8:27:eb:a6:0c:bf
    Sending on Socket/fallback
    DHCPREQUEST on eth0 to 255.255.255.255 port 67
    DHCPNAK from 192.168.10.11
    DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6
    DHCPREQUEST on eth0 to 255.255.255.255 port 67
    DHCPOFFER from 192.168.10.11
    DHCPACK from 192.168.10.11
    bound to 192.168.10.152 -- renewal in 377683 seconds.
    RTNETLINK answers: File exists
    Failed to bring up ppp0.
    done.

    thus the DHCP client returns a failed error msg, can you tell me what im doing wrong?

    Thank you

    ReplyDelete
  5. Its giving me this error now,
    [warn] Running /etc/init.d/networking restart is deprecated because it may not re-enable some interfaces ... (warning).
    [....] Reconfiguring network interfaces...Internet Systems Consortium DHCP Client 4.2.2
    Copyright 2004-2011 Internet Systems Consortium.
    All rights reserved.
    For info, please visit https://www.isc.org/software/dhcp/

    Unsupported device type 512 for "ppp0"
    Failed to bring up ppp0.
    RTNETLINK answers: File exists
    Failed to bring up eth0.
    done.

    heres what my interfaces file looks like

    # interfaces(5) file used by ifup(8) and ifdown(8)

    auto lo
    iface lo inet loopback

    #USB for connecting to the Internet
    auto ppp0
    iface ppp0 inet dhcp

    #onboard eth serving as internal gateway
    auto eth0
    iface eth0 inet static
    address 192.168.10.1
    netmask 255.255.255.0
    network 192.168.10.0
    broadcast 192.168.10.255
    gateway 192.168.10.1

    any help would be much appreciated thanks

    ReplyDelete
    Replies
    1. I don't mention ppp0 in my /etc/network/interfaces at all. Sakis handles everything for ppp0 when one asks it to connect. I think DHCP does not work over ppp links, ip addresses are handled in some other way there, by pppd. So just remove the ppp0 lines and try again, trust sakis to get it right.

      Delete
  6. Great! thanks, ill try that tonight, and get back to you with the results.
    Seriously, thanks

    ReplyDelete
  7. Hi,

    Sakis doesn't work for my 3g dongle but I connect the modem with wvdial (ppp connexion with etho).

    But for share the connexion, with my Raspberry, i can't found eth1. Do you have a solution for add a eth1 ?

    Thanks

    ReplyDelete
  8. im still having some issues connecting the pi to my router, can you please post your interfaces file and the dhcp.conf
    i dont know what im doing wrong here, Sakis handles my LTE modem just fine, its the pi thats giving me issues connecting to the router.

    Thank you.

    ReplyDelete
    Replies
    1. I added the two files to the post. Do you mean you have a dedicated router in your network? I have just a simple network switch in mine, the Pi does all the routing and dhcp serving.

      Delete
  9. Hi,

    I fixed my problem, i found eth1 and built a dhcp server. I can connect external computer with dhcp but the connexion break down after few seconds (between the raspberry and the others machine conncet to the network) only when i connect the modem

    Do you have the same problem ?

    Thanks

    ReplyDelete
    Replies
    1. What network devices do you have? For me, the GSM modem is ppp0, and the Pi:s onboard ethernet is eth0. No eth1. Eth1 appears in the router blog I linked to, where the routing is between two ethernets. Are you sure vwdial touches eth0 at all?

      Anything interesting in the logs, for example /var/log/syslog? I don't have the same problem, sakis just worked out of the box for me.

      Another thing, try adding an "interface eth0;" (or eth1) line in your dhcpd.conf like in my newly added example in the post. When I played with WLAN, I saw that dhcpd got unhappy when it did not know which network interface to use.

      Delete
  10. Hello,
    I resolved all the problems today. The connexion breaking down because iptables wasn't configurate for sharing all information between ppp and eth1.
    In my apply, wvdial start the comunication with the modem with pppo who is attached to eth0. I need a ethernet/USB dongle for create eth1 for the dhcp server port).
    My dhcpd.conf and network/interfaces are different of yours but the result is the same !

    Now i just need to make the system autonomous when i power up the raspberry.

    Thanks for this page, it's helpful

    ReplyDelete
    Replies
    1. Great, glad to hear it! For automating, I found the qcktech blog helpful, at least for iptables.

      Delete
  11. Thank you,
    The information you shared is very informative.

    ReplyDelete
  12. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. 192.168.49

    ReplyDelete

Newer Older Home
Related Posts Plugin for WordPress, Blogger...