Thursday, September 26, 2013

Turning your Raspberri Pi into a wireless bridge

I recently found myself in need of a wireless bridge.  Having also recently purchased a Raspberry Pi and not wanting to spend money on a dedicated bridge (costing as much or more than the Pi), I decided I'd figure out how to make my Pi serve that function for me.  It took a little searching and piecing together of web articles to find the simplest formula to get there, and it works pretty well.

Here's what you need to get started:
- Raspberry Pi (duh) - I recommend model B revision 2.0.
- MicroUSB power supply - The more mA the better.
- USB wireless network adapter - Mine happens to be a generic RTL8192CU-based unit that came as part of a starter kit with my Pi.
- USB keyboard.
- HDMI or video monitor.
- Pi case (optional).
- 4 GB or larger SDHC card - I used a Wintec 16 GB MicroSDHC with adapter.
- Raspbian distribution image - The latest raw image can be obtained from the Raspberry Pi website.
- A way to write Pi images to the SD card - For Windows, Win32 Disk Imager is a great choice.


Step 1: Copy Raspbian image to SD card

If you own a Raspberry Pi, you probably already know how to do this.  Using Win32 Disk Imager, it is a simple matter of browsing to the downloaded Raspbian image file, making sure the correct device is selected to write to, and then clicking the Write button.


Assuming no errors during the write process, you should now have a bootable SD card to put in your Pi.


Step 2: Hook up your system and set up Raspbian

It doesn't get much easier than this.  If you own a Pi, you certainly already know how to hook up your peripherals and power it up.  At the end of first boot, you'll be presented with the raspi-config screen and can do any customization you need there.  I recommend at least using the first item to expand your file system to fill available space.  Once you're done, you'll be able to log into the terminal session.


Step 3: Enable both network interfaces

You'll need to update your network configuration so that both the wired and wireless interfaces are enabled automatically at boot time.  First file to edit (as superuser) is /etc/wpa_supplicant/wpa_supplicant.conf.  Assuming your wireless access point uses WPA2 pre-shared keys, replace the contents of the file with this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="Your SSID Here"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="YourPresharedKeyHere"
}

Make sure you use your own SSID and pre-shared key where appropriate.  If you are not using WPA2, you'll need to find and use the correct parameters for your access point.  Next file you need to mess with is /etc/network/interfaces.  Replace the contents of the file with this:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

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

Before moving on, you should test this configuration to make sure the wireless is connecting properly.  Run these two commands:

# sudo ifdown wlan0
# sudo ifup wlan0

You'll likely see three ioctl warnings during the ifup command.  Those are ok and can be ignored.  From there, you can use the "ifconfig" command to check on status of both eth0 and wlan0 or "iwconfig" command to get detailed status for wlan0.  Finally, if you want to make sure things will be enabled properly at boot time, this is a good time to reboot the Pi and see.


Step 4: Create the bridge

Run the following commands to install bridge-utils and create the bridge br0 between wlan0 and eth0:

# sudo apt-get install bridge-utils
# sudo brctl addbr br0
# sudo brctl addif br0 wlan0 eth0

After making a backup copy of /etc/network/interfaces, update its contents to this:

auto lo
iface lo inet loopback

iface eth0 inet manual

auto wlan0
iface wlan0 inet manual

auto br0
iface br0 inet dhcp
   bridge_ports wlan0 eth0
   bridge_stp off
   bridge_maxwait 5

wpa-iface wlan0
wpa-bridge br0
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

That should do it.  With the wpa-bridge line, you don't need to mess with ebtables or anything to spoof mac incoming and outgoing MAC addresses for the wireless side.


Step 5: Make it so

If you have your wireless and wired ports currently hooked up to the same LAN, unplug the wired connection now.  Reboot.  Once your system reboots, you should have a Raspberry Pi bridge running in your environment.  You can plug a single machine or switch into the wired ethernet port, and anything there will be bridged through the wireless access point on the other side.

Hope this helps someone.  If it doesn't, though, don't blame me.
«      |      ?      |      »