Skip to content

Instantly share code, notes, and snippets.

@JamesHagerman
Created June 4, 2017 21:48
Show Gist options
  • Save JamesHagerman/49c8264b0968aa449a97d5ae521691e6 to your computer and use it in GitHub Desktop.
Save JamesHagerman/49c8264b0968aa449a97d5ae521691e6 to your computer and use it in GitHub Desktop.
Simple script to enable ip forwarding for basic routing
#!/bin/bash
logger "Telling kernel to turn on ipv4 ip_forwarding"
echo 1 > /proc/sys/net/ipv4/ip_forward
logger "Done. Setting up iptables rules to allow FORWARDING"
DOWNSTREAM=wlan0 # wlan0 is client network (running hostapd)
UPSTREAM=eth0 # eth0 is upstream network (internet)
# Allow IP Masquerading (NAT) of packets from clients (downstream) to upstream network (internet)
iptables -t nat -A POSTROUTING -o $UPSTREAM -j MASQUERADE
# Forward packets from downstream clients to the upstream internet
iptables -A FORWARD -i $DOWNSTREAM -o $UPSTREAM -j ACCEPT
# Forward packers from the internet to clients IF THE CONNECTION IS ALREADY OPEN!
iptables -A FORWARD -i $UPSTREAM -o $DOWNSTREAM -m state --state RELATED,ESTABLISHED -j ACCEPT
logger "Done setting up iptables rules. Forwarding enabled"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment