Tuesday, January 3, 2017

Making a Wireless Bridge from Raspberry Pi

I don't know whether this term "Wireless Bridge" really can be used to this, anyway it does what I want to do which is more like acting as a point to point wireless link. The reason I wanted this is my new networking lab hardware (routers/switches/servers) which do not have a wireless adapter, needs to connect to internet to get updates/ ping internet etc and the home router is in a another room (about 50 feet from my lab). I don't like to mess with Ethernet cables around my home. If I had to lay an Ethernet cable, it should have to be done from outside of the house or else I have to drill holes through my walls.
So I chose to give a wireless solution with the things I have..

Sample setup is like the following..












Assign IP Addresses

I assigned an IP address to Pi's Ethernet interface. I gave 10.1.1.5
Assigned Pi's Ethernet IP address as the default gateway of the devices (ex:- server)
Pi's wireless interface does not need to be manually configured, as it gets a DHCP IP address from the ADSL router.. Only what needed was to give the WiFi password and connect to the network..

Enable IP Forwarding in Pi

IP forwarding should be enable to route traffic between interfaces. Entered the following command to edit the sysctl file..
sudo nano /etc/sysctl.conf

Scrolled down until I found #net.ipv4.ip_forward=1 and uncommented it (removed #) and hit Ctrl + X, Y, Enter to save and quit.


















Entered the following command too..
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Setup Network Address Translation (NAT)

From the IPs comming from the Ethernet interface side of the Pi will not be able to go to Internet and comeback with their own source IP because the real default gateway of my home network, the ADSL router (192.168.1.1) does not no those source IP addresses to route traffic back. It only knows the 192.168.1.0 subnet which it leases from it's DHCP.

Entered the following command..
sudo /sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

Edit IP Tables

Now let's see my Pi's routing table (IP tables)









As I can see there are 2 default routes in the table.
Via Eth0 interface with a metric of 202..
Via Wlan0 interface with a metric of 303..

Eth0 interface will be the preferred path by default because of the lower metric. Here, I'm deleting the 1st default route by entering the following command. Let's see what happens..
sudo route del default








Now only one default route is there and it is the wlan0 interface. So now everything works fine for me..

Save IP Tables

I wanted to save the routing table and the forwarding configurations I made because otherwise every time I power off the Pi, it will go to defaults and I have to config again and again.

This is how I saved my work..

Entered the following commands..
sudo apt-get install iptables-persistent

After installation is done, following commands will save the iptables and start at the bootup..
sudo su
iptables-save >/etc/iptables/rules.v4

Now all done, Bridge works fine..

Note:- Each time I add persistent route to the routing table, I have to save it using the above 2 commands. Then only they will survive after a reboot..
In addition to this setup, if you want to connect to the subnets beyond the Pi's end via the home router (ex:- connect 10.1.1.0 subnet from my laptop) you willl have to put static routes on the home router.
ex:- static route to 10.1.1.0 as the next hop 192.168.1.7

0 comments to “Making a Wireless Bridge from Raspberry Pi”

Post a Comment