"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:
* IBSS
* managed
* AP
* AP/VLAN
* WDS
* 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
address 192.168.0.111
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
iface wlan0 inet static
address 192.168.1.111
netmask 255.255.255.0
broadcast 192.168.1.255
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 192.168.1.0 netmask 255.255.255.0{
range 192.168.1.10 192.168.1.100;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option routers 192.168.1.111;
}
and one for the wired one
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.10 192.168.0.100;
range 192.168.0.150 192.168.0.200;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.0.255;
option routers 192.168.0.1;
}
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;
fixed-address 192.168.0.110;
}
Wireless router set-up
I then installed the Hostapd service:
sudo apt-get install hostap
and configured the “/etc/hostapd/hostapd.conf” configuration file
interface=wlan0
driver=nl80211
ssid=Raspberry
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Secret
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
and I enabled it by editing the “/etc/default/hostapd” file and uncommenting the configuration directive:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
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
net.ipv4.ip_forward=1
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
Conclusions
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.