Nav links

Saturday, 23 February 2013

Raspberry Pi wireless bridge

Raspberry PIPost updated 4 Jan 2014 with expanded technical details.

I got a Raspberry Pi for Christmas. After connecting it up and checking that it worked I wanted to find something productive for it to do. Saving power and removing clutter are always good goals, so I decided to replace the old Windows XP laptop sitting under the TV currently being used, occasionally, as a wireless bridge for our PVR. Using the standard Raspbian Linux distribution this was relatively easy to set up, though there's a bit to learn if you haven't fiddled with networking in Linux for a while.

Our PVR has an ethernet port but no wireless capabilities, so a wireless bridge is needed to connect it to the internet. Tiny $25 routers, such as the TP-Link TL-WR702N, can easily be configured as wireless bridges, so if I didn't already have a Pi or a spare laptop I'd go for one of them. That router, like the Pi, can be powered via USB, which means the power can come from a recent TV or PVR with a spare USB port, rather than a less efficient bulky power brick.

Note: These instructions were written for and tested on 2013-02-09-wheezy-raspbian. Other versions may not work identically.

After you first boot Raspbian you'll need to tell it how to connect to your wireless network. If you are in xwindows then you should see a WiFi Config icon. Using that should be straightforward, although if you're connected to a display via composite rather than HDMI then you may find the resolution of the screen too low to see the necessary elements of the various WiFi Config windows. If you hold down alt then you can click anywhere within the window to drag it around, thereby showing the crucial bits.

The most important command to learn is ifconfig. This will tell you the current status of your network, so use it liberally. Some sample output from this command is below.

The WiFi config icon calls wpa_gui, the graphical front-end for wpa_supplicant. wpa_supplicant is the backend code coordinating the wireless. Being Linux, there is also a console interface, called wpa_cli. Within wpa_cli I ran the following commands to configure the box to see my wireless network, telling it the SSID and wireless password.

status

add_network

set_network 0 ssid "my-wifi-network-ssid-name"

set_network 0 psk "my-wifi-password"

enable_network 0

scan

scan_results
bssid / frequency / signal level / flags / ssid
11:11:11:11:11:11       2462    70      [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]     my-wifi-network-ssid-name

12:11:11:11:11:11       2462    26      [WPA-PSK-TKIP][ESS]     ANOTHER-NETWORK

reassociate

save_config

Don't forget to do the save_config so that you don't have to go through this all again when you reboot.
At this point running status should return something like:


> status

bssid=11:11:11:11:11:11

ssid=my-wifi-network-ssid-name

id=0

mode=station

pairwise_cipher=CCMP

group_cipher=CCMP

key_mgmt=WPA2-PSK

wpa_state=COMPLETED

address=11:11:11:11:11:11

Exit wpa_client with quit.

The changes you have made will be visible in your config file:


pi@raspberrypi ~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf

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

update_config=1

network={
        ssid="my-wifi-network-ssid-name"
        psk="my-wifi-password"
}


You should now have full internet connectivity, so pinging a machine should return something like:


> pi@raspberrypi /etc/network $ ping google.com

PING google.com (74.125.237.169) 56(84) bytes of data.

64 bytes from syd01s18-in-f9.1e100.net (74.125.237.169): icmp_req=1 ttl=57 time=81.7 ms


Now that wireless is set up it's time to move onto bridging. Right now you can ssh into your Pi and your Pi can see the internet. After setting up bridging any device connected via ethernet to the Pi will also be able to be connected to, and that device will see the internet. Note that you can set up transparent bridging, where the bridge itself is not visible, but in my case I wanted to connect to the Pi, so I didn't go that route.

I found the following sites useful in working out how to set this up:

These led me to the following. Firstly, install the bridging software: apt-get install bridge-utils

Bring down your networking with sudo ifdown -a

Then edit your /etc/network/interfaces file to become:
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_fd 0
        bridge_waitport 0

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

Now bring up the network with sudo ifup -a I was having trouble with the Pi sometimes taking a while to receive an IP address, which I suspect is related to our temperamental router, so I've since changed to static ip rather than dhcp, as follows, remembering to manually bring down the network before making the changes, then up again afterwards:

pi@raspberrypi ~ $ cat /etc/network/interfaces   
auto lo

iface lo inet loopback
iface eth0 inet manual

auto wlan0
iface wlan0 inet manual

auto br0
iface br0 inet static
        address 10.1.1.13
        gateway 10.1.1.1
        netmask 255.255.255.0
        bridge-ports wlan0 eth0
        bridge-stp off
        bridge-fd 0
        bridge-waitport 0

wpa-iface wlan0
wpa-bridge br0
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Running ifstatus should get the following:
pi@raspberrypi ~ $ ifstatus
*** Usage of ifstatus is deprecated, use ifplugstatus instead! ***
br0: link beat detected
wlan0: link beat detected
lo: link beat detected
eth0: link beat detected
And ifconfig should now show something like:

pi@raspberrypi /etc/network $ ifconfig

br0       Link encap:Ethernet  HWaddr 11:11:11:11:11:11
          inet addr:10.1.1.13  Bcast:10.1.1.255  Mask:255.255.255.0
          inet6 addr: 2222::2222:2222:2222:2222/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:151 errors:0 dropped:1 overruns:0 frame:0
          TX packets:132 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:12263 (11.9 KiB)  TX bytes:15361 (15.0 KiB)

eth0      Link encap:Ethernet  HWaddr 11:11:11:11:11:11
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:166 (166.0 B)  TX bytes:1563 (1.5 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 11:11:11:11:11:11
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:329 errors:0 dropped:346 overruns:0 frame:0
          TX packets:136 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:84768 (82.7 KiB)  TX bytes:19563 (19.1 KiB)
The device on the end of the eth0 cable should now be able to see the Pi and anything else on the internet. The Pi too should be able to see the device on ethernet and anything on the internet. And other devices on your internal network should be able to see both the Pi and the ethernet-connected device, over the Wifi.