Pi as wireless bridge?


59 posts   Page 1 of 3   1, 2, 3
by SiriusHardware » Tue Sep 18, 2012 11:51 am
This may be a crazy question, but is there any way to easily allow a Pi with a working wi-fi connection to 'share' its internet connection with anything plugged into the ethernet conenctor, in much the same way as Windows computers can be set to do that?

I'm busy trying to convert a PC into a purely Linux box, using an on-disc Debian distro - the problems I'm having there are beyond the scope of this board but I'm hampered by the lack of a wired internet connection for the PC linux box to use while it's installing - it would be great if I could temporarily use the Pi to connect it wirelessly to my network, ie, use the Pi as a wireless bridge. It would be a useful tool to have for many other purposes as well.

Can it be done?
Posts: 437
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by mikerr » Tue Sep 18, 2012 12:04 pm
This would probably do what you want
Code: Select all
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE


(only lasts until a reboot)
Got a Pi Camera? View it in my android app - Raspicam Remote ! No software required on the pi
User avatar
Posts: 1216
Joined: Thu Jan 12, 2012 12:46 pm
Location: NorthWest, UK
by broo0ose » Tue Sep 18, 2012 9:40 pm
You can do it fairly easily if you install bridge-utils

the details are here ..

http://wiki.debian.org/BridgeNetworkCon ... idge_setup
Posts: 240
Joined: Wed Dec 14, 2011 3:59 pm
Location: Wirral, UK
by SiriusHardware » Sat Oct 13, 2012 2:02 pm
Sorry gents, you both came back quickly with good answers which I read more or less immediately but didn't get around to acknowledging until now - they were much appreciated.
Posts: 437
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by Joe Schmoe » Sat Oct 13, 2012 2:49 pm
mikerr wrote:This would probably do what you want
Code: Select all
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE


(only lasts until a reboot)


1) If you do it with iptables (I.e., with masquerading), don't you need to specify somewhere what the new local IP subnet is going to be? I.e., somewhere in the "iptables" command line, it should specify something like 192.168.117.*.

2) If you do it with bridging, be aware that the IP addresses given out will be from the outer network (i.e., no local network is created). This is usually what you want.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2621
Joined: Sun Jan 15, 2012 1:11 pm
by gabeblack » Fri Dec 28, 2012 10:13 pm
I wanted the pi to connect to my access point, and then provide the bridge so that through the ethernet port I could connect a scanner on the same lan as the access point. I also wanted the pi to have an IP on the access point lan as well since via the usb port it would serve up some printers, thus making my printers and scanners connected wirelessly via the pi to the lan.

I finally was able to do it (I'm using wheezy)

step 1:
Setup the pi so that it is able to connect via wpa_supplicant. Terse version to do that follows, but there is better tutorials online on how to do that.
A
Edit /etc/wpa_supplicant/wpa_supplicant.conf: (you'll need to adjust for your settings):
Code: Select all
ctrl_interface=/var/run/wpa_supplicant
network={
        ssid="Your Wireless SSID Name"
        proto=WPA
        key_mgmt=WPA-PSK
        psk=pskkey
}

B
Test that you can connect via wpa_supplicant:
Code: Select all
wpa_supplicant -Dwext -iwlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

If it looks like you connected, then you are all set. Ctrl-C and go on to step two.

step 2:
edit /etc/network/interfaces to look like this:
Code: Select all
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-driver wext
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

I think the wpa-bridge option takes care of the mac address spoofing so that you don't have to do any iptables or ebtables trick. I did a lot of googling and couldn't find anything that worked. This took me a long time to figure out so I hope it can be useful to someone.
Posts: 1
Joined: Fri Dec 28, 2012 10:00 pm
by scratchyking » Mon Jan 21, 2013 12:42 am
@gabeblack - Thanks for the tip! It worked! I spent all of yesterday figuring out how to set up my iptables and ebtables...

wpa-bridge does the trick.
Posts: 1
Joined: Mon Jan 21, 2013 12:28 am
by kimbp » Sat Feb 02, 2013 8:11 pm
Sounds easy, but doesn't work for me. I get no IP addresse on any of my interfaces.

See ifconfig output:
    br0 Link encap:Ethernet HWaddr b8:27:eb:7b:29:b9
    UP BROADCAST MULTICAST MTU:1500 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)

    eth0 Link encap:Ethernet HWaddr b8:27:eb:7b:29:b9
    UP BROADCAST MULTICAST MTU:1500 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:1000
    RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

    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:4 errors:0 dropped:0 overruns:0 frame:0
    TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:240 (240.0 B) TX bytes:240 (240.0 B)

    wlan0 Link encap:Ethernet HWaddr 7c:dd:90:04:ef:be
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:11 errors:0 dropped:0 overruns:0 frame:0
    TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:918 (918.0 B) TX bytes:288 (288.0 B)


Output of brctl show
    bridge name bridge id STP enabled interfaces
    br0 8000.b827eb7b29b9 no eth0
My wlan isn't added by some reason.
An observation is that the LED in my USB wlan device stays on almost constantly.

Running sudo brctl addif br0 wlan0 gives back an error unless I first execute

Code: Select all
sudo iw dev wlan0 set 4addr on


Unfortunately it doesn't really help me. Any suggestions
Posts: 1
Joined: Sat Feb 02, 2013 6:42 pm
by timb » Sat Apr 13, 2013 8:31 pm
I know this post is a few months old, but it's super helpful and a simple way to get WiFi-to-Ethernet bridging going. I just wanted to say that in order for @gabeblack's instructions to work, you need to have bridge-utils installed, which wasn't clear in his instructions.

Here's the prerequisites:
Code: Select all
# sudo apt-get install bridge-utils
# sudo brctl addbr br0
# sudo brctl addif br0 wlan0 eth0


Then follow @gabeblack's instructions. One change I would make is to not place wpa-driver wext in /etc/network/interfaces as that's not a very stable driver. If you don't put anything at all it will default to the best one for your system.
Posts: 46
Joined: Mon Mar 04, 2013 8:09 pm
by dodu » Wed Apr 24, 2013 6:46 am
Doesnt work for me, i always get
Code: Select all
can't add wlan0 to bridge br0: Operation not supported...


if i call
Code: Select all
iw dev wlan0 set 4addr on


iam able to add wlan0 to br0 but, doesn't work anyway.

At Start:
To install bridge-utils i use wpa_GUI to get connected to my WLan to install bridge-utils.
Afterwards ive done the steps described, but didn't work.

What distro are you using? I used the Wheezy raspbian.
are there some prerequisites?

Iam about to despair on this setup...

Some suggestions?
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by timb » Wed Apr 24, 2013 7:09 am
Okay, what does your /etc/network/interfaces look like? His example had a few errors in it. Try the following the instructions below and tell me if it works or not.

timb's How-To Bridge wlan0 to eth0 (or) How-To Share WiFi with Devices Connected to the RPi's Ethernet Port

1) Setup your wireless with the wpa_supplicant GUI tool so that you can access the internet (and it automatically works on reboot).

2) Install bridge-utils and create the relevant interfaces.
Code: Select all
# sudo apt-get install bridge-utils
# sudo brctl addbr br0
# sudo brctl addif br0 wlan0 eth0


3) After backing up your old /etc/network/interfaces file, replace it with this new one.
Code: Select all
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


4) Reboot. When the system comes back up, it should just work!
Posts: 46
Joined: Mon Mar 04, 2013 8:09 pm
by dodu » Wed Apr 24, 2013 7:14 am
If i follow your tutorial

at step
Code: Select all
# sudo brctl addif br0 wlan0 eth0

It says
Code: Select all
can't add wlan0 to bridge br0: Operation not supported...
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by timb » Wed Apr 24, 2013 7:20 am
Have you used Hexxeh's rpi-update tool?

What wireless adapter are you using?
Posts: 46
Joined: Mon Mar 04, 2013 8:09 pm
by dodu » Wed Apr 24, 2013 7:28 am
i didn't use a update tool.

I'am using Tenda W311MI Wireless N150 Pico USB Adapter
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by timb » Wed Apr 24, 2013 7:56 am
Try grabbing the tool and updating. It might be the wireless driver (solved by updating) or it could be your card doesn't support the mode needed for this type of bridging.
Posts: 46
Joined: Mon Mar 04, 2013 8:09 pm
by dodu » Wed Apr 24, 2013 8:44 am
Do you know which mode exactly? Maybe there is a way to manually test it?
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by dodu » Fri Apr 26, 2013 3:21 pm
nearly got it,
setup works corretly but, afterwards pc on eth0 cant ping anything in network connected to wlan0.

br0 hast ip 192.168.178.37 via DHCP

from Rpi i can ping my router 192.168.178.1 and google.de successfully but the connected pc on eth0 doesnt even receive dhcp ip and static ip doens't work either.

seems that the bridge is up but the connection doesn't work?
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by SirLagz » Fri Apr 26, 2013 3:27 pm
Once the bridge is up, can you then renew the IP address ?
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Posts: 1704
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
by dodu » Fri Apr 26, 2013 3:49 pm
nop.

I currently tried to ad ebtables (iptables) and now it works, but without iptables it doesn't :/
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by SirLagz » Fri Apr 26, 2013 4:05 pm
iptablse or ebtables ?
2 different things...
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Posts: 1704
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
by dodu » Fri Apr 26, 2013 4:20 pm
ebtables...

but the guides say ebtables or Iptables are not necessary...
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by dodu » Fri Apr 26, 2013 4:21 pm
it works with ebtables setup but it doens't work without it :( i meant
Posts: 18
Joined: Thu Oct 11, 2012 7:38 am
Location: Germany
by SirLagz » Fri Apr 26, 2013 4:31 pm
Righto, probably means that the bridge isn't doing what it's meant to do. Packets are not getting the MAC spoofed when they go through the Pi.
Not sure why that's happening though...
I'll try it myself and I'll see what I get.
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Posts: 1704
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
by SirLagz » Mon Apr 29, 2013 4:05 am
gabeblack wrote:I wanted the pi to connect to my access point, and then provide the bridge so that through the ethernet port I could connect a scanner on the same lan as the access point. I also wanted the pi to have an IP on the access point lan as well since via the usb port it would serve up some printers, thus making my printers and scanners connected wirelessly via the pi to the lan.

I finally was able to do it (I'm using wheezy)

step 1:
Setup the pi so that it is able to connect via wpa_supplicant. Terse version to do that follows, but there is better tutorials online on how to do that.
A
Edit /etc/wpa_supplicant/wpa_supplicant.conf: (you'll need to adjust for your settings):
Code: Select all
ctrl_interface=/var/run/wpa_supplicant
network={
        ssid="Your Wireless SSID Name"
        proto=WPA
        key_mgmt=WPA-PSK
        psk=pskkey
}

B
Test that you can connect via wpa_supplicant:
Code: Select all
wpa_supplicant -Dwext -iwlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

If it looks like you connected, then you are all set. Ctrl-C and go on to step two.

step 2:
edit /etc/network/interfaces to look like this:
Code: Select all
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-driver wext
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

I think the wpa-bridge option takes care of the mac address spoofing so that you don't have to do any iptables or ebtables trick. I did a lot of googling and couldn't find anything that worked. This took me a long time to figure out so I hope it can be useful to someone.


I tried all of that but didn't work for me.
Might be useful to specify which WiFi adapter you're using ?
I couldn't even get my br0 interface to get an IP address.
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Posts: 1704
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
by timb » Mon Apr 29, 2013 4:09 am
SirLagz wrote:I tried all of that but didn't work for me.
Might be useful to specify which WiFi adapter you're using ?
I couldn't even get my br0 interface to get an IP address.


Did you try my modified instructions? viewtopic.php?p=337452#p337452
Posts: 46
Joined: Mon Mar 04, 2013 8:09 pm