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.

11 comments:

  1. Can you tell me the kernel version of your raspbian image. I have the latest (3.10.27) and I get an operation not supported when trying to add wlan0 and eth0 to the bridge. From my research this might have been removed or disabled in later versions of the kernel.

    Thanks

    dann0

    ReplyDelete
    Replies
    1. Sure thing. It's 3.6.11, included in the 2013-09-10 Raspbian image.

      Delete
    2. Slor, thanks for the info. I was able to confirm that this is an issue in the update versions of the distro/kernel. I managed to find a copy of 2013-09-10 Raspian Image, however when I installed bridge-utils it appears the issue is part of that package. Would you mind terribly telling me A)what version of bridge-utils package that you are running B)Anyway you can post link to download that version?

      Thank you again

      -dann0

      Delete
    3. I wish I could. Unfortunately, I rebuilt my Pi not too long ago for another purpose and did not keep an image of this setup.

      Delete
  2. I tried this as well but ran into the bridge-utils issue. I found this guide which I haven't tried yet but others recommended. Read from Aug 9th 2012 post onward. Hope it helps.

    http://sirlagz.net/2012/08/page/2/

    ReplyDelete
  3. why I execute "sudo brctl addif br0 wlan0 eth0"?..can't work

    ReplyDelete
    Replies
    1. According to previous comments, this functionality may have broken in newer kernel versions.

      Delete
  4. hello, I have it working on a Raspberry Pi2 and after reboot it dosent reconnect. I have to run sudo brctl delif br0 wlan0 eth0

    the start at

    # sudo ifdown wlan0
    # sudo ifup wlan0

    # sudo brctl addbr br0
    # sudo brctl addif br0 wlan0 eth0

    after this is connects..


    any idea whats going on here? when its works its amazing, thanks for putting this together.

    ReplyDelete
    Replies
    1. I sure don't. I haven't actually worked with this since the version I posted, and I didn't have any reboot issues that I can recall. Based on previous posts, the later kernels didn't even support it for a while, so I'm glad to see you even got it to work on the newer Pi.

      Delete
  5. Can anyone confirm if this works on the newer Pi models? and on the latest Jessie release of Raspbian. I'm trying to get an original Xbox hooked up to the LAN and internet but there's only Ethernet on board and I'm not able to plug in to the router where the Xbox is now. I was hoping to use a Pi to act as a wireless adapter for it.

    ReplyDelete
  6. Will this work switching the wired and wireless networks? Right now my RPi is connected to my router and I want to broadcast a wireless network for other devices. Is there a way to tweak this to do what I look for?

    ReplyDelete

Note: Only a member of this blog may post a comment.