"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Wednesday, 4 May 2016

Place the Raspberry PI at the centre of your network

My recent problems with Internet providers, followed by the need to change my ADSL modem-router made me think if it would be wiser to make my home network less router dependent. Local network at my home, like most home networks, relies on the ADSL modem-router for the Dynamic Host Configuration (DHCP). Every time I changed the router I had so to reconfigure its DHCP server in order to restore my network configuration, often not being able to access to some network devices, like the NAS disk, until the DHCP was properly configured.
While looking for a Raspberry DHCP configuration how-to I literally stumbled into this page about using the Raspberry PI as a wireless router. This also inspired me about using the Raspberry also to provide a backup or private WI-FI access.
Hardware set-up
First things first: I already had a Wi-Fi dongle wandering in my drawers, I installed in the Raspberry PI USB port and checked it worked. Not all the wireless interfaces are able to be used in “access point mode”, to check if the one I had was compatible I installed the “iw” utility:
sudo apt-get install iw
executing the command:
iw list
I got a detailed list of the network interface features, among them in the the supported interfaces modes:
Supported interface modes:
* managed
* AP
* monitor
* mesh point
i got confirm that the interface could work as access point.

Setting a fixed IP address
A DHCP server must have a fixed IP address, it has to be configured in the “/etc/network/interfaces” file:
iface eth0 inet static
iface wlan0 inet static
I also disabled the “hotplug” and “wpa_supplicant” directives
#allow-hotplug wlan0
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
DHCP server set-up
Several DHCP server are available for Raspberry and Raspbian, the how-to I was following suggested using UDHCP but, since I was going to need to configure it for both wired and wireless network I decided to use Isc-DHCP-Server that seems to offer more configuration options.
I installed it using apt-get command:
sudo apt-get install isc-dhcp-server
then edited the configuration file at “/etc/dhcp/dhcpd.conf” configuring one subnet for wireless network:
subnet netmask{
option subnet-mask;
option broadcast-address;
option routers;
and one for the wired one
subnet netmask {
option subnet-mask;
option broadcast-address;
option routers;
here I also set e reservation for a fixed address to be assigned to the NAS disk:
host nas{
hardware ethernet 00:16:67:00:9a:33;
Wireless router set-up
I then installed the Hostapd service:
sudo apt-get install hostap
and configured the “/etc/hostapd/hostapd.conf” configuration file
and I enabled it by editing the “/etc/default/hostapd” file and uncommenting the configuration directive:
Setting-up the NAT
At last I configured a Network Address Translation (NAT) service between the wireless interface and the wired one, following the how-to instructions I enabled IP forwarding editing “/etc/sysctl.conf” configuration file and uncommenting the following line:
# Uncomment the next line to enable packet forwarding for IPv4
then I executed the following “iptables” commands:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED $
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
stored iptables configuration to a configuration file …
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
and eventually I added the following directive to “/etc/network/interfaces” file in order to reload NAT configuration at system reboot
up iptables-restore < /etc/iptables.ipv4.nat
So I restarted the RaspberryPI and everything started working … Not really I went trough quite a lot of trial-and-error, there are a lot of configurations and a lot of mistakes to do even when following how-to page instructions. By the way I got it working, speed is far behind from a moder WIFI access point but still workable.