Friday, 23 December 2016

Build your own Router with Raspberry Pi 3

Build your own Router with Raspberry Pi 3

This project aims at developing a simple Router device using Raspberry Pi 3. Most of the people don't feel like going out and buying a new router and want something that just fits in your pocket. The process here is actually pretty simple and after installing a few bits of software you'll be ready to go. This setup uses the Raspberry Pi 3. After it's all set up, you can use your Pi as a headless machine so you don't need to deal with a monitor, keyboard and mouse.

How it works

The process is very simple. Raspberry pi 3 is already have Wifi chip on the board so after configuring and installing procedure you just need two things LAN cable and 2-Amps micro Usb power adapter for Raspberry Pi 3.
The Application of Building Raspberry Pi 3 Router include:
  1. Raspberry Pi 3.
  2. SD card with latest Raspbian OS installed.
  3. Power Adapter (5Volts, 2.5 Amps).
  4. Mouse.
  5. Keyboard.
  6. HDMI to VGA adapter.
  7. Standard PC monitor.
Setup:
To setup please follow the steps mentioned bellow:
Prepare the Raspberry Pi-3:
Connect the monitor to Raspberry Pi 3 Via VGA to HDMI converter and also plug in the Usb mouse and keyboard.
Boot up Raspberry Pi 3 you will see Raspberry Pi desktop (assuming you have already installed Raspbian OS) like below fig 1, fig 2, fig 3.

Fig 1:
Fig 2:
​Fig 3:

In order to act Raspberry Pi as Wifi router or access point you need to check weather Raspberry PI is up to date and all the new packages are available for download/installation just open the terminal (fig 4)
and type below commands:
$ sudo apt-get update
$ sudo apt-get upgrade


​Fig 4:

Install and Configure the router softwares:
The first thing you need to do is to configure your wlan0 interface with a static IP.
Note: For this, Raspberry Pi 3 should be connected to Local LAN cable.
In newer Raspian versions, interface configuration is handled by dhcpcd by default. We need to tell to ignore wlan0, as we will configure it with a static IP address. So open the dhcpcd configuration file with
$ sudo nano /etc/dhcpcd.conf
and add the following line to the bottom of the file:
denyinterfaces wlan0

Note:
This must be ABOVE any interface lines you may have added!
allow-hotplug wlan0  
iface wlan0 inet static  
    address 172.24.1.1
    netmask 255.255.255.0
    network 172.24.1.0
    broadcast 172.24.1.255
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Restart dhcpcd with

$ sudo service dhcpcd restart

and then reload the configuration for wlan0 with 

$ sudo ifdown wlan0; sudo ifup wlan0

ISC-DHCP-SERVER:

isc-dhcp-server is the Internet System Consortium’s implementation of a DHCP server. A DHCP server is responsible for assigning addresses to computers/devices connection to the WiFi access point.
To install the DHCP software run the following command:
$ sudo apt-get install hostapd isc-dhcp-server
Note: Hostapd is explained in next steps.
Next file to edit is /etc/default/isc-dhcp-serveryou can open it in nano using this command:
$ sudo nano /etc/default/isc-dhcp-server
Scroll down to the line saying interfaces and update the line to say:
INTERFACES="wlan0"
This will make the DHCP server hand out network addresses on the wireless interface. Save the file and exit nano.

DHCP server:

Be wise and always make a backup of the default config
$ sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf"

Edit the default config file
$ sudo nano /etc/dhcp/dhcpd.conf

Comment the following lines:
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org
And uncomment below line:

authoritative;
Scroll down to bottom of the file and add below lines:
subnet 172.24.1.0 netmask 255.255.255.0 {
        range 172.24.1.10 172.24.1.50;
        option broadcast-address 172.24.1.255;
        option routers 172.24.1.10;
        default-lease-time 600;
        max-lease-time 7200;
        option domain-name "local";
        option domain-name-servers 8.8.8.8, 8.8.4.4;
}

HOSTAPD:


Hostapd is a user space daemon for access point and authentication servers. That means it can turn your Raspberry Pi into a access point that other computers can connect to. It can also handle security such that you can setup a WiFi password.
Next, we need to configure hostapd. Create a new configuration file using:
$ sudo nano /etc/hostapd/hostapd.conf
with the following contents:
# This is the name of the WiFi interface we configured above
interface=wlan0

# Use the nl80211 driver with the brcmfmac driver
driver=nl80211

# This is the name of the network
ssid=RPI3-AP

# Use the 2.4GHz band
hw_mode=g

# Use channel 6
channel=6

# Enable 802.11n
ieee80211n=1

# Enable WMM
wmm_enabled=1

# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]

# Accept all MAC addresses
macaddr_acl=0

# Use WPA authentication
auth_algs=1

# Require clients to know the network name
ignore_broadcast_ssid=0

# Use WPA2
wpa=2

# Use a pre-shared key
wpa_key_mgmt=WPA-PSK

# The network passphrase
wpa_passphrase=raspberry

# Use AES, instead of TKIP
rsn_pairwise=CCMP
We can check whether it's working at this stage by running below command:

$ sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
If it's all gone well thus far, you should be able to see the network RPI3-AP! If you try to connect it, you will see some output from the Raspberry Pi, but you won't receive any IP address until you set up finish. Use Ctrl+C to stop it.
We aren't quite done yet, because we also need to tell hostapd where to look for the config file when it starts up on boot. Open the default configuration file with:
$ sudo nano /etc/default/hostapd
and find the line

#DAEMON_CONF=""
and replace it with
DAEMON_CONF="/etc/hostapd/hostapd.conf".
Enable the NAT using IPV4 FORWARDING:
One that we need to do before we send traffic anywhere is to enable packet forwarding. To do this, open the sysctl.conf file with:

$ sudo nano /etc/sysctl.conf
and remove the # from the beginning of the line containing "net.ipv4.ip_forward=1" This will enable it on the next reboot. but, as we know everyone is impatient and to activate it immediately with:
$ sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
We also need to share our Raspberry Pi's Internet connection to our devices connected over WiFi by configuring a NAT between our wlan0 interface and our eth0 interface. We can do this using the following commands:
$ 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  
However, we need these rules are to be applied every time when we reboot the Raspberry Pi, so run below command to save the rules in a file:
$ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Now we need to run this after each reboot, so open the rc.local file with:
$ sudo nano /etc/rc.local
before "exit 0" add the following line:
iptables-restore < /etc/iptables.ipv4.nat  

Test by Starting your wireless router:

Now you are ready to start the DHCP server and the Hostapd access point application.You can do so by running:
$ sudo service isc-dhcp-server start
$ sudo service hostapd start
At this point you should be able to find your wireless network on your laptop/devices by which you can access the Internet!

Final Steps:

While it is pretty cool, you have your Raspberry Pi running as a wireless access point. You have to login every time when it reboots to start the Hostapd and DHCP software.
To avoid this run the bellow commands, then it will reload even if Raspberry Pi reboots:
$ sudo update-rc.d hostapd enable 
$ sudo update-rc.d isc-dhcp-server enable
At this point try to reboot the raspberry pi just to make sure everything works as intended - you can reboot with the command:
$ sudo reboot

Now you can remove all connections Like keyboard, mouse and also VGA to HDMI converter cable between monitor and Raspberry pi and then restart the Raspberry PI 3 device, and enjoy.