QuickLinks
Part 1 – How to create a wireless network on your RPi
Part 2 – How to make your RPi into a Wireless Access Point
Part 3A – Issues with HostAPD ? Click here
Part 3B – Issues with HostAPD ? Click here!
In Part 3, we will turn the Raspberry Pi into a router. The Pi will use the ethernet port as the “WAN” port, and the wireless adapter as the “LAN” side of things.
First thing we need to do is to enable packet forwarding.
In the file /etc/sysctl.conf, we need to uncomment the following line (should be line 28).
#net.ipv4.ip_forward=1
After changing that, run this command to re-read the sysctl.conf file
sysctl -p
We will also need to install the iptables utilities if they are not already installed, and alter the dnsmasq.conf file so that dnsmasq will assign a gateway to the computers.
Firstly we install iptables.
apt-get install iptables
Then we will add this line to dnsmasq.conf.
The IP address is the same one we gave to the WiFi adapter back in Part 1
dhcp-option=3,10.0.0.1
We will need to create an iptables script to tell the Raspberry Pi to forward packets from the WiFi interface to the LAN interface. This script will need to run at startup in order for it to be a router.
I have created the following file in /etc/network/if-up.d/ called router. By placing the script in that directory, it will be run every time the Raspberry Pi comes online.
This file contains the following lines
iptables -F
iptables -X
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -i wlan0 -j ACCEPT
iptables -A OUTPUT -o wlan0 -j ACCEPT
iptables -A POSTROUTING -t nat -o eth0 -j MASQUERADE
iptables -A FORWARD -i wlan0 -j ACCEPT
After creating this file, make it executable by using this command
chmod +x /etc/network/if-up.d/router
Once that router.sh file has been run, it should start forwarding traffic from the WiFi Interface to the LAN interface !
** UPDATE **
As a few people have found out, the IP address of the Pi does not get set correctly until you take down wlan0 and bring it back up after hostapd starts.
I have not found out why yet, but the workaround I am currently using consists of editing /etc/init.d/hostapd, and modifying the start case statement to include ifdown wlan0 && ifup wlan0 at the end of it.
I failed to copy the statement I am talking about, but if you’re familiar with scripting it shouldn’t be too hard to figure out. Otherwise I’ll get the code I modified up here soon.
** UPDATE 2 **
Looks like a Pi user has successfully gotten hostapd running on the realtek devices.
Info can be found here
If you have found this post useful, please consider donating by pressing on the button below. Donating will help keep this site up and running 🙂