Raspberry Pi and Routing: Turning a Pi into A Router

Raspberry Pi and Routing: Turning a Pi into A Router

The Raspberry Pi can be used as a router–great if you need a pocket-sized device to share an Internet connection.  It can also be used in conjunction with a cantenna.

Requirements For This Walkthrough


Materials

  1. Local network
  2. Mac or PC
  3. Raspberry Pi  running Raspbian “wheezy”
  4. Wireless USB Adapter
  5. HDMI Cable (*optional)
  6. Keyboard (*optional)
  7. Mouse (*optional)
  8. Monitor with HDMI input (*optional)

*If the Raspberry Pi is set up as a headless machine, you will not need a monitor, keyboard, or mouse–just another computer, which would be used to access it remotely over the network via SSH.

Downloads

  1. Older hostapd (only needed if the driver via apt  does not work)
  2. Copies of config files on Github (optional or for reference)

Knowledge, Skills, and Abilities

  1. Ability to navigate throughout a computer OS
  2. Knowledge of basic computer terminology
  3. Ability and confidence to enter commands in the Terminal, adjusting them to suit your environment, if necessary
  4. Familiarity with core networking concepts

Resources

  1. http://magpi.techjeeper.com/The-MagPi-issue-11-en.pdf
  2. http://www.pi-point.co.uk/documentation/
  3. http://blog.sip2serve.com/post/48420162196/howto-setup-rtl8188cus-on-rpi-as-an-access-point
  4. https://docs.google.com/file/d/0B3nsVzbJuBHVOWRJRDJacVd0S2s/preview?pli=1
  5. http://sirlagz.net/2012/08/11/how-to-use-the-raspberry-pi-as-a-wireless-access-pointrouter-part-3/
  6. http://www.raspberrypi.org/phpBB3/viewtopic.php?t=39096&p=393810
  7. http://unix.stackexchange.com/questions/119209/hostapd-will-not-start-via-service-but-will-start-directly
  8. http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un-rtl8188cus-chipset/

Conceptual Overview

  1. Install Required Components via apt-get
  2. Verify Wi-Fi Adapter dongle functionality
  3. Configure /etc/network/interfaces
  4. Create /etc/hostapd/hostapd.conf  with desired AP settings
  5. Point hostapd  to the configuration file in step 4 by modifying /etc/default/hostapd
  6. Replace /sbin/hostapd  with the older version (if necessary)
  7. Configure DHCP to allow clients to connect
  8. Enable IPv4 forwarding to reach the Internet

Prepare the RPi

Make certain that the RPi is up-to-date and all the newest packages are available for download/installation.

sudo apt-get update 
sudo apt-get upgrade

Install Required Components

The following components will be required for setting this up as an access point:

  • rfkill: a wireless utility
  • zd1211-firmware: common firmware that works with many Wi-Fi dongles
  • hostapd: the hostap wireless access point daemon
  • hostap-utils: supplemental hostap tools
  • iw: wireless configuration utility
  • dnsmasq: DHCP and DNS utility
  • bridge-utils:  used for connecting multiple Ethernet devices together

Install these components using the command:

sudo apt-get install rfkill zd1211-firmware hostapd hostap-utils iw dnsmasq bridge-utils

Configure the Wireless Dongle

Verify The Dongle is Recognized by the RPi

Check that the RPi recognizes the Wi-Fi dongle.

lsusb

You should see a listing mentioning the adapter you have plugged in.

Verify that the Wi-Fi dongle supports AP mode

If the output of the following command does not show anything about AP mode, then the dongle will not work for this project. But if it does, continue following the step-by-step directions.

iw list

Unfortunately, my dongle did not work and produced the error nl80211 not found , which is OK.  Run this command:

dmesg | grep rtl

if the output is similar to:

[    6.240292] usbcore: registered new interface driver rtl8192cu

then it will probably still work.  This will be fixed in a later step.

Configure the Wireless Interface

Make a backup of /etc/network/interfaces  and then open it in a text editor:

sudo cp /etc/network/interfaces /etc/network/interfaces.orig 
sudo vi /etc/network/interfaces

Modify the file as below (highlighted lines are the additions):

auto lo
auto br0

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
allow-hotplug eth0
iface wlan0 inet manual

iface br0 inet dhcp
        bridge_fd 1
        bridge_hello 3
        bridge_maxage 10
        bridge_stp off
        bridge_ports eth0 wlan0

Restart the wlan0  interface:

sudo ifdown wlan0 
sudo ifup wlan0

Configure hostapd Settings

Next, make a backup of the /etc/hostapd/hostap.conf  file and open it in a text editor to make some changes (ignore the first command if the file does not exist yet).

sudo cp /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.orig 
sudo vi /etc/hostapd/hostapd.conf

If using a different adapter than the one I used, just modify the file by adjusting any unique items (i.e. country_code , ssid , etc).  If the iw list command mentioned previously reported the error nl80211 not found, then modify the file as follows:

interface=wlan0
bridge=br0
driver=rtl871xdrv
country_code=US
ctrl_interface=wlan0
ctrl_interface_group=0
ssid=RPiAP
hw_mode=g
channel=1
wpa=3
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
beacon_int=100
auth_algs=3
macaddr_acl=0
wmm_enabled=1
eap_reauth_period=360000000

If the iw list  command reported the error, you should also to make a backup of the /etc/default/hostapd  file (if it exists),

sudo cp /etc/default/hostapd /etc/default/hostapd.orig 
sudo vi /etc/default/hostapd

and then modify the highlighted line below (be sure to uncomment the line):

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#       -d   show more debug messages (-dd for even more)
#       -K   include key data in debug messages
#       -t   include timestamps in some debug messages

If you are using the same dongle I did, you will need to take another additional step and replace /usr/sbin/hostapd with a newer version, which you can get here (but we will use a command to download it next).  Following our best practice, make a backup first and then edit it:

sudo cp /usr/sbin/hostapd /usr/sbin/hostapd.orig

Delete the original file, which will be replaced by the one downloaded via the command below:

cd /usr/sbin
sudo rm -f hostapd
sudo wget http://dl.dropbox.com/u/1663660/hostapd/hostapd

Set the proper permissions on the new file and make sure it is executable:

sudo chown root:root hostapd 
sudo chmod 755 hostapd

Restart the hostapd service to activate the settings.

sudo service networking restart 
sudo service hostapd restart

If the hostapd restart did not work.  Try running this to view the output and see if you can find where the problem is in the config file and fix it:

sudo hostapd -d /etc/hostapd/hostapd.conf

The wireless network should show up if you were to scan for it from another device.  However, a few other settings need to be configured before anyone can connect to it.  In particular, dnsmasq .

Configure dnsmasq Settings

Following best practices, make a backup of the config file before editing it and then open the file in a text editor.

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.orig 
sudo vi /etc/dnsmasq.conf

Uncomment the following lines and adjust them to your environment:

domain-needed interface=wlan0 
dhcp-range=192.168.2.1,192.168.2.254,12h

After saving the file, you can easily verify all the settings were correct with the following command (it just parses out the comments and blank lines):

cat /etc/dnsmasq.conf | grep -v "#" | sed '/^$/d'

Restart the dnsmasq service to apply the settings:

sudo service dnsmasq restart

Attempt to connect to the network using another device.  You should be able to connect, but you won’t be able to get out the the Internet yet.

Enable Forwarding to Reach the Internet

Enable IPv4 forwarding

sudo sysctl -w net.ipv4.ip_forward=1

Enable NAT

sudo iptables -t nat -A POSTROUTING -j MASQUERADE

Other Dongles and Other Uses

The main reason I chose the Wi-Fi dongle that I did was so that I could connect my cantenna to it and blast the signal a long distance.  It would have certainly been easier to use an adapter that worked right out of the box, but now I have a lot more flexibility with antenna types.