Tuesday, September 8, 2015

Raspberry Pi2 as a Router with Network Failover

This experiment is all about using Pi2 as a Home router with network failover. Assume you’ve both wired internet (Dialup Modem), and a mobile 3G internet (eg. 3G USB Dongle). Your wired internet can be considered as the primary and mobile 3G internet as backup or secondary.

The requirement is to, provide internet through your primary by default. As soon as your primary network is down, switch to your secondary. Your network clients (i.e the PC, smartphones, laptop which are connected to your local network through Ethernet and WIFI), should not be reconfigured, they should be automatically routed to the secondary network for accessing the internet, when the primary goes down.

Here Pi2 will be configured as a single gateway for all the network clients. By default it will route packets designated to internet to the primary network. Whenever primary is not available, it will dial in secondary network and route packets through secondary. When Primary is back online, Pi2 will disconnect the secondary, and route packets back to Primary. All packets from the attached clients will be routed to Pi2 first, where the actual routing will happen. Pi2 has been running with Ubuntu.14.04.

The conceptual diagram is shown below.

image

The configuration steps has been detailed below.

1.  Primary internet will be available through the ADSL modem, which is connected to a Wireless router (192.168.1.2)

2.  DHCP Server is disabled in the Wireless router, since DHCP requests will be served by the PI2

3.  Pi2 (192.168.1.1) is connected to the Wireless Router through Ethernet through ‘eth0’ interface. It is the default gateway for the LAN connected devices

4.  DNSMASQ utility has been configured to run on Pi2, as a DHCP service, which will serve IP Address for the LAN connected devices.

Range: 192.168.1.20 – 192.168.1.50, Gateway: 192.168.1.1 (The Pi2 itself)

So that clients will route the packets to the Pi2.

5.  Pi2 has set the default route to the Wireless Router (192.168.1.2) for internet, in the IP Routing table)

i.e. route add default gw 192.168.1.2

6.  Pi2 has also connected with a 3G USB Modem, which has been configured with ‘Wvdial’ utility, this will be secondary network. It will be recognized as ‘ppp0’ interface in Pi2.

7.  Pi2 is configured to Forward packets from eth0 to ppp0 through NAT (Using IP Tables)

8.  When internet is not available through primary (i.e through eth0), Pi2 has to dial ppp0

9.  Pi2 has to delete the default route through Wireless Router to internet, so that from now on packets will be routed to ppp0, to reach internet

Note: You can check internet connectivity through any interface using ‘ping’ command, where you can optionally give the interface name as well. In this case eth0

10.  Once the Primary network is back, ppp0 will be brought down. The default route will be added back to Wireless router, so that packets will be routed through it from now on.

11.  Steps 8-9, will be done through a shell script running in the background.

Monday, September 7, 2015

Setting up Wifi using Command Line–Raspberry Pi2 (Ubuntu 14.04)

Note: This tutorial is based on Ubuntu.14.04 (For RaspberryPi2, It’s the ARM version of Ubuntu14.04. The installation procedure is detailed here).

Though we can rely on Network Manager GUI, setting up Wifi using command line having some obvious advantages in certain scenarios. For e.g. Bridging your Ethernet and wifi interfaces.

The below tutorial will discuss about setting up the Wifi using command line along. Before moving further, Hope you have setup your WIfi Card in Ubuntu, with necessary firmware and driver. This tutorial is highly recommended, if you’ve not.

The first thing you’ve to do is to install certain packages, that makes the wifi setup a breeze under Debian environment. Install the following packages.

sudo apt-get install wireless-tools
sudo apt-get install wpa_supplicant

Once done, lets find the wifi interface which is ready to be used from command line. Issue the below command.

iwconfig

A sample output is given below:

br0       no wireless extensions.

eth0      no wireless extensions.

lo        no wireless extensions.

wlan0 unassociated  Nickname:"rtl_wifi"
          Mode:Managed  Access Point: Not-Associated   Sensitivity:0/0 
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality:0  Signal level:0  Noise level:0
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

The result shows that wlan0, is the wireless interface that  can be used. Now lets scan for wireless networks using any of the below command. The former will scan for every available wireless networks in the range. If you specifically know the SSID of the Wifi, to which you want to connect, to you can use the latter command, which specifies the exact SSID to connect. 

iwlist wlan0 scanning
sudo iwlist wlan0 scanning essid "Your Wifi SSID"

The below is a sample output.

wlan0     Scan completed :
                   Cell 01 - Address: AC:F1:DF:CD:2B:D4
                    ESSID:"My Wifi SSID"
                    Protocol:IEEE 802.11bgn
                    Mode:Master
                    Frequency:2.412 GHz (Channel 1)
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
                    Extra:wpa_ie=dd1a0050f20101000050f20202000050f2020050f20401000050f202
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    Extra:rsn_ie=30180100000fac020200000fac02000fac040100000fac020000
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    Signal level=58/100
 

Identify and note the words which are marked in green. We require those details to setup ‘wpa_supplicant’ configurations, which we will discuss shortly.

Now we’ve all details which are necessary to build our ‘wpa_supplicant.conf’, except one. We’ve to now generate the passphrase psk, using the Wifi SSID and the secret passphrase used to connect to your wifi router. The below command generate it for you.

wpa_passphrase "YourWifiSSID" "YourWifiSecretPassPhrase"
 
This will generate the below output, that can be copied to your ‘wpa_supplicant.conf’ file.
 
network={
    ssid="YourWifiSSID"
    #psk="YourWifiSecretPassPhrase"
     psk=5f7cad3eed901ebb12345f5b2f307680a758bfa264ab13cbd374095a34f80e25
}

Now using the scan result and the passphrase psk details, fill in the ‘/etc/wpa_supplicant.conf’ file. Use the below commands.

sudo leafpad /etc/wpa_supplicant.conf

Now update this with the above details. A sample is given below;

ap_scan=1
ctrl_interface=/var/run/wpa_supplicant

network={
        ssid="YourWifiSSID"
        #psk="YourWifiSecretPassPhrase"
        psk=5f7cad3eed901ebb12345f5b2f307680a758bfa264ab13cbd374095a34f80e25
        scan_ssid=1
        mode=0
        key_mgmt=WPA-PSK
        proto=RSN
        pairwise=CCMP
        group=TKIP
}

To know about the different configurations options in ‘wpa_supplicant.conf’, refer this FreeBSD reference manual.

Save the file. Now we’ve all set, and issue the below commands to test connecting to our Wifi router.

sudo wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf

(or use this command if the above driver not supported by your device
sudo wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplica.conf)

You can use the –d or –dd switch with the above command to get the detailed log of the connection. If everything went will, you can see the logs on the console, which says association got succeeded. Now you’re connected to your wifi router. Now from another terminal issue the below command.

sudo dhclient wlan0

This will initiate DHCP requests to the router, and wifi interface (wlan0) will be configured with a dynamic IP addressed issued by the wifi router. Once done, try accessing internet, other machines in your network to test whether you’re actually connected.

To make this settings permanent (So that you are automatically connected on bootup), edit the network interfaces file and add the below content.

sudo leafpad /etc/network/interfaces

Append the below configuration.

auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant.conf

Now during every boot, your machine will automatically get connected to wifi. Happy surfing!