Bridging wlan0 to eth0 (wifi to lan) with a Raspberry Pi

Posted: Friday 7 November 2014

Edit:

As mentioned by +Gareth Williams, this guide will result in routing the Raspberry Pi's Internet connection, not bridging it.

Click here to skip to the actual guide.

I've had a Raspberry Pi for a few months now, and although I'm not impressed about its performance, I am however, impressed by its versatility.

The Scenario

I required an external device to connect to the internet, and there wasn't a way to do this except via ethernet - effectively a lan connection - unfortunately for me, my internet router is in another room and I don't have access to a lan cable that can reach that far.
However, wifi is a medium that is used to connect the raspberry pi and other portable devices in the house and unfortunately, this particular device cannot connect to wifi in anyway (lets say that USB wifi dongles do not work).
Take note that this device can be any device, an XBOX, even, or that non-smart TV of yours - as long as you have access to a few settings.
I already had a spare laptop that could do this.

With Windows, creating a network bridge is as simple as dragging two interfaces together, and contextually creating a bridge.

and that was it...
Well, the Raspberry Pi uses an OS called "Raspbian" which corrupts the very SD card that it runs its operations from when you expand the filesystem, so that's what we're dealing with.

The Failed Steps

This was one of the challenges that involved a lot of mistakes. So, listed below are methods that I tried, but did not work...

  • Creating an actual bridge between the wlan0 and eth0.
    • Basically, what this involved was the following commands:
      sudo brctl addbr br0
      sudo brctl addif br0 wlan0
      sudo brctl addif br0 eth0
      

      Which gives this:
      Can't add wlan0 to bridge br0: Operation not supported

      Which can be resolved by setting:
      sudo iw dev wlan0 set 4addr on
      and rebridging the wlan0 interface to the br0 bridge:
      sudo brctl addif br0 wlan0
      But funnily enough, the "bridge" didn't actually produce the desirable outcome, which was unfortunate, considering the fact that it appears that the bridge is up and working - even in "ifconfig" it appeared that everything was working...
      nope.
  • Explicitly setting the ipv4 packets to be forwarded.
    • /etc/sysctl.conf
      was already properly configured.
      Yet:
      echo 1 > /proc/sys/net/ipv4/ip_forward
      gives out an error message
      bash: /proc/sys/net/ipv4/ip_forward: Permission denied.
      To resolve this, the command was entered from the perspective from "root" and not just the default "pi" user:
      sudo su -
      Yet, even after that ordeal, wifi did not route to lan.
  • IP tables?
    • You mean the following mess right?
      sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
      sudo iptables -A FORWARD --in-interface eth0 -j ACCEPT
      sudo iptables -F
      sudo iptables -t nat -F
      sudo iptables -t mangle -F
      sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
      sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
      sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT
  • Doing it all (almost) manually
    • Oh, and the times I had looking at
      /etc/network/interfaces
      With Raspbian wpa_supplicant.conf is auto configured with their "WPA-GUI" application.
      So really, you don't have to do a thing involving wifi configurations. Nevertheless, it doesn't work as you'd expect it. And it ultimately failed to produce the wifi-lan bridging at the level of Windows 7.

The Solution

Hold on to your hats, because this was quite the rollercoaster ride!

Edit:

Please note that you end up routing the Rasberry Pi's internet connection not bridge it, hence the device connected to the Raspberry Pi cannot communicate with other devices from your primary wireless router - though said device has Internet access.

A technical "bridge" is not possible.



The solution is to use "NetworkManager" in order to bridge route the connection between Wifi and LAN, and it's not a simple one-step process.
To install "NetworkManager", simple apt-get it!
sudo apt-get install network-manager-gnome
Note that NetworkManager/Network Manager is the default networking utility that you get in Ubuntu, so already we're dealing with issues that Ubuntu users have faced in the past...
From here on in, is where things start to get really complicated and mangled. You might get an error message "Not authorized to control networking".
So you have to use the
sudo su -
command to elevate as "root" and then a
startx
command to initiate the X-session, as root.
From here on, it's GUI (unless you can handle NetworkManager through its gui/command line version, good luck with that though!).
Connect to Wifi and Ethernet with the little task-tray icon in the lower right hand corner of the screen.


I don't exactly have a screenshot from the Pi itself for the next step, but you "right click" the tray icon and click on "Edit Connections".

Where you'll get a GUI just like this:

From here, select the "Wired connection", and then click on the "Edit" button in order to edit the settings the Ethernet connection.
Where you should select the "IPv4 Settings" tab and change the "Method" to "Shared to other computers".

Then the "IPv6 Settings" tab and the "Method" to "ignore"

Then run the command:
sudo killall dnsmasq
if you haven't already, and you're at the point where Wifi should be routed to Ethernet (if not, then at the next reboot!).

As you can see above, I ran a little "ifconfig" command again. Keep track of a few values, namely those on the "eth0" section.
You'll need to use these in your target device:

IP Address = eth0 inet addr (plus 1 or two)
Subnet Mask = eth0 mask

DNS is the setting that gets interesting. Mine was set to a local DNS server, as it was the setting in my router. Feel free to use whatever DNS you please. If you're stuck, you can use Google's public DNS at 8.8.8.8 and/or 8.8.4.4
So enjoy your internet connection!









Tags:
This post will show you how to
Make a Raspberry Pi bridge wlan0 to eth0
Connect computer to internet via Raspberry Pi
Make a Raspberry Pi tunnel wifi to an ethernet device
Connect an XBOX through a Raspberry Pi's WIFI
Use the Raspberry Pi as a router
Share the raspberry pi's internet to another device
br0 wlan0 eth0
Bridge Wifi with Ethernet LAN