Wednesday, July 6, 2016

Configure Raspberry Pi as a router (ethernet and wifi) on Jessie


I used these instructions to create a router with 2 ethernet and 1 wireless connection.  These instructions can be modified for more wireless or more ethernet connections.  I was setting up eth1 as the WAN connection, eth0 and wlan0 as LAN connections.  Wlan0 was to broadcast DHCP from the PI, eth0 would have static IP, and DHCP on this netowrk would be from an external DHCP server.

Requirements:
Pi model B+ running Raspbian Jessie;
Wireless USB dongle;
USB Ethernet Adapter.
In this example I used a Wi-Pi adapter, and a TP-Link UE300
rfkill: a wireless utility
zd1211-firmware: common firmware that works with many Wi-Fi dongles
hostapd: the hostap wireless access point daemon
hostap-utils: supplemental hostap tools
iw: wireless configuration utility
isc-dhcp-server: ISC implementation of DHCP
bridge-utils:  used for connecting multiple Ethernet devices together

Connect to the Pi via SSH

Install requirements by using the command:

sudo apt-get update -y
sudo apt-get install rfkill zd1211-firmware hostapd hostap-utils iw bridge-utils isc-dhcp-server -y

Jessie may have iw preinstalled -- if it does and is updated, running the command above will automatically skip it.

Connect the wifi dongle and USB Ethernet adapter

Use the command lsusb to ensure that the USB wireless dongle is being detected

pi@gw:~ $ lsusb
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub


Use the command iw list to ensure that the adapter has a supported interface mode of AP
Supported interface modes:
         * IBSS
         * managed
         * AP
         * AP/VLAN
         * WDS
         * monitor
         * mesh point


Use ifconfig to identify which interface is which:


image

Based on the IP Addresses, I could identify eth0 as my LAN connection, and eth1 will be my WAN connection.

Create a backup of /etc/network/interfaces, and edit the file:

sudo cp /etc/network/interfaces /etc/network/interfaces.orig
sudo nano /etc/network/interfaces

Modify the file as appropriate. 

source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback

allow-hotplug eth0
auto eth0
iface eth0 inet static
    address 192.168.168.1
    netmask 255.255.255.0

allow-hotplug eth1
auto eth1
iface eth1 inet static
    address 172.16.0.20
    netmask 255.255.0.0
    gateway 172.16.0.1

allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
    address 192.168.200.1
    netmask 255.255.255.0


Take a backup of /etc/hostapd/hostapd.conf and edit the file (it it doesn’t exist, create the file).

sudo cp /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.orig
sudo nano /etc/hostapd/hostapd.conf

Modify the file as appropriate

interface=wlan0
country_code=AU
ctrl_interface=wlan0
ctrl_interface_group=0
ssid=wifissid
hw_mode=g
channel=8
wpa=2
wpa_passphrase=secretwifipassphrase
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP TKIP
rsn_pairwise=CCMP
beacon_int=100
auth_algs=3
macaddr_acl=0
wmm_enabled=1
eap_reauth_period=360000000


Backup and modify /etc/default/hostapd

sudo cp /etc/default/hostapd /etc/default/hostapd.orig
sudo nano /etc/default/hostapd

Edit the DAEMON_CONF=”” line to point to the hostadp.conf file modified earlier.

DAEMON_CONF=”/etc/hostapd/hostapd.conf”

Configure DHCP
Create a backup of /etc/dhcp/dhcpd.conf and edit it

sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.orig
sudo nano /etc/dhcp/dhcpd.conf

Make the following changes as appropriate

authoritative;
option domain-name "dmz.anonit.net";
default-lease-time 86400;
option subnet-mask 255.255.255.0;
max-lease-time 172800;
option broadcast-address 192.168.200.255;
option routers 192.168.200.1;


subnet 192.168.200.0 netmask 255.255.255.0 {
range 192.168.200.50 192.168.200.99;
option domain-name-servers 8.8.8.8, 8.8.4.4;
}


Backup and edit /etc/default/isc-dhcp-server and setup the interface to broadcast DHCP on

sudo cp /etc/default/isc-dhcp-server /etc/default/isc-dhcp-server.orig
sudo nano /etc/default/isc-dhcp-server

Find and edit the INTERFACES line to the interface to broadcast DHCP on.

INTERFACES="wlan0"

(If you wanted to broadcast DHCP on eth0 as well you could add it in here)

Enable routing and Nat

sudo cp /etc/sysctl.conf /etc/sysctl.conf.orig
sudo nano /etc/sysctl.conf

Edit the file to add the line (or uncomment it)

net.ipv4.ip_forward=1

Enable the translation immediately
 
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Configure IPTables for NAT

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

sudo iptables-save

If this is the first time modifying iptables, install iptables-persistent by running

sudo apt-get install iptables-persistent

If prompted to make the rules persistent, select YES
If you already have iptables-persistent installed, you can save the iptables rules by running the command

sudo iptables-save

make the changes permanent (starts hostapd and dhcp on boot)

sudo update-rc.d hostapd enable 
sudo update-rc.d isc-dhcp-server enable
 
save the iptables setup and restore on boot
 
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" 
 
Edit the /etc/network/interfaces file and add the following to the end.
 
up iptables-restore < /etc/iptables.ipv4.nat

Reboot the device and test
 
Reference:
http://jacobsalmela.com/raspberry-pi-and-routing-turning-a-pi-into-a-router/
https://alwaystinkering.wordpress.com/2015/12/29/basic-raspberry-pi-home-wifi-router/
https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf
http://raspberrypihq.com/how-to-turn-a-raspberry-pi-into-a-wifi-router/
https://www.raspberrypi.org/forums/viewtopic.php?t=31227#p293467
https://www.raspberrypi.org/forums/viewtopic.php?f=36&t=63048

No comments:

Post a Comment