Monday, 29 July 2013

Share Ethernet Raspberry Pi

1. Set up interfaces
Your interfaces file could look something like this (we have prepared the eth0 with a static IP of 10.0.0.1 – this is your gateway for all clients).
# sudo vim /etc/network/interfaces
auto lo

iface lo inet loopback

# ETH0
iface eth0 inet static
address 10.0.0.1
netmask 255.255.255.0

# WLAN0 
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
address 192.168.0.160
netmask 255.255.255.0
gateway 192.168.0.1
wpa-essid 
wpa-psk 
2. Install your DHCP server
# sudo apt-get install dnsmasq
# sudo vim /etc/dnsmasq.conf
interface=eth0
dhcp-range=10.0.0.2,10.0.0.9,255.255.255.0,24h
This will make dnsmasq listen to only eth0. It also sets the available IP range to 10.0.0.2 – 10.0.0.9, and the subnet mask to 255.255.255.0. Lease time will be set to 24 hours.

3. Enable packet forwarding in sysctl
sudo vim /etc/sysctl.conf
Uncomment this line:
net.ipv4.ip_forward=1
“Reload” the sysctl with
sysctl -p
5. Create a script that runs everytime the ‘Pi comes online
# sudo vim /etc/network/if-up.d/router.sh
Add this (make sure to change INET_ADDRESS to whatever you set for your wlan0 address)
#!/bin/sh
IPT=/sbin/iptables
LOCAL_IFACE=eth0
INET_IFACE=wlan0
INET_ADDRESS=192.168.1.98

# Flush the tables
$IPT -F INPUT
$IPT -F OUTPUT
$IPT -F FORWARD

$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT

# Allow forwarding packets:
$IPT -A FORWARD -p ALL -i $LOCAL_IFACE -j ACCEPT
$IPT -A FORWARD -i $INET_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT

# Packet masquerading
$IPT -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_ADDRESS
Make it executable
# chmod +x /etc/network/if-up.d/router.sh
Apply rules and export a restorable firewall configuration script
source /etc/network/if-up.d/router.sh
iptables-save > /home/pi/router.fw
Make it start on system boot
sudo vim /etc/rc.local
Add this right above the line with “exit 0″
/sbin/iptables-restore < /home/pi/router.fw
When the script has been run it should forward packets between the LAN and Wifi interfaces.

No comments:

Post a Comment