Friday, 12 April 2013

RPi as Wireless Access Point.

A project I am working on will be deployed in a location without internet access.  The Pi will have a 3G modem connected, and I will detail that setup in another post.

What I want to be able to do here is to use the WI-FI dongle attached to the Pi to allow devices to connect, and potentially use the internet via the 3G dongle. - The first step in that is just getting the AP setup so that we can connect. - Taking this blog as a guide -

Before I started, I plugged the Pi into a wired connection rather than it's more normal wireless connection, found the IP address and connected to that so I'm logged in over wired and won't lose connection when the hostapd kicks in.  I also ran an image backkup of the SD card so that I could at least revert back to previous configuration if necessary!

sudo apt-get install iw
sudo iw -list - shows that this supports AP mode correctly.

sudo apt-get install hostapd udhcpd

Give static IP to wlan0 - in /etc/network/interfaces:-

allow-hotplug wlan0
iface wlan0 inet static


pi@raspberrypi /etc/network $ grep -v "^#" /etc/udhcpd.conf |grep -v "^$"
start     #default:
end      #default:
interface       wlan0           #default: eth0
remaining       yes             #default: yes
opt     dns
option  subnet
opt     router
option  lease   864000          # 10 days of seconds

pi@raspberrypi ~ $ cat /etc/hostapd/hostapd.conf

edit /etc/default/hostapd - change the following to point to the hostapd.conf file:-


edit /etc/sysctl.conf:-

# Uncomment the next line to enable packet forwarding for IPv4

sudo sysctl -p (to re-read the setting)

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

sudo service hostapd start
sudo service udhcpd start

At this point, we are working- hurray.  Now make the IP tables setup permanent.

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Edit /etc/network/interfaces:-

I didn't find these last commands necessary, since the install enabled the daemons, but this  is to enable the daemons on boot.

sudo update-rc.d hostapd enable
sudo update-rc.d udhcpd enable

Excellent - so, now I have an AP which will route traffic through eth0, and assign an IP address to my connected device.
DNS works fine.
I can also connect to the eth0 IP address when I'm on that network, so all appears to be working fine....

OK - this didn't work on reboot unless the ethernet cable was plugged in... Bizzare. - It appeared that the plugd thought that the wlan0 interface was already configured, so I commented out the line in interfaces:-

#allow-hotplug wlan0

And now, the Pi starts up, issues IP addresses, and seems to work fine even when the eth0 isn't plugged on power up. - Cool.

Now - to setup for 'ppp0' via the USB modem.
- Change eth0 to ppp0 in /etc/iptables.ipv4.nat
- This is my 'connect' script using sakis3g.

echo "Connect attempt at `date`" >>$LOGFILE
/home/pi/3G/sakis3g connect FORCE_APN="" MODEM="OTHER" OTHER="USBMODEM" USBMODEM="12d1:1003" DIAL="*99#" >>$LOGFILE 2>&1
/home/pi/3G/sakis3g status >> $LOGFILE 2>&1
echo "Restoring iptables" >> $LOGFILE 2>&1
sudo /sbin/iptables-restore < /etc/iptables.ipv4.nat >> $LOGFILE 2>&1
echo "Connect attempt complete at `date`" >> $LOGFILE

Note here that I need to re-apply the iptables rules - it doesn't seem to work applying these at boot time when the ppp0 interface does not exist.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.