#!/bin/bash # adhoc_wifi_gateway # (c) Paul Mansfield July 2009 # release under GNU Public License Latest Revision # # History # 20090719 # PM # first release into the wild # ##### CONSTANTS #### # these are all you should need to change WIFI=wlan0 WIFI_ESSID=My_Temporary_Access_Point WIFI_WEP=DEADBEEFDEAFBEEFDEADBEEFDE WAN=ppp0 # if DHCP daemon is running chroot, all files could be under here DHCP_ROOT=/var/lib/dhcp/ DHCP_CONFIG=/etc/dhcpd.adhoc-wifi-ap.conf DHCP_USER=dhcpd DHCP_GROUP=nogroup DHCP_LEASES=/db/dhcpd.wlan0.leases DHCP_PID=/db/dhcpd.wlan0.pid # shouldn't need to change these unless you're already using 172.16.31.0/24 WIFI_IP=172.16.31.1 WIFI_MASK=255.255.255.0 WIFI_NET=172.16.31.0 WIFI_DHCP="172.16.31.2 172.16.31.254" ## ROUTING ## # set up routing echo 1 > /proc/sys/net/ipv4/ip_forward # need to NAT outbound packets iptables -t nat -F # kill existing NAT iptables -t nat -I POSTROUTING -o $WAN -j MASQUERADE # nat to WAN IP ## SECURITY ## # need to permit input so DHCP works iptables -I INPUT -i $WIFI -j ACCEPT # permit forwarding iptables -I FORWARD -i $WIFI -o $WAN -j ACCEPT iptables -I FORWARD -m state --state established,related -j ACCEPT ## WIRELESS ## # set up ad-hoc wifi "access point" iwconfig $WIFI mode ad-hoc iwconfig $WIFI essid $WIFI_ESSID iwconfig $WIFI enc $WIFI_WEP ifconfig $WIFI $WIFI_IP netmask 255.255.255.0 ## SERVICES ## # test if already running a DHCP server ps -ef | grep -v grep | grep "/usr/sbin/dhcpd -cf /etc/dhcpd.adhoc-wifi-ap.conf" > /dev/null if [ $? -ne 0 ] ; then # set up a DHCP server # some dhcp servers need their leases file initialising touch $DHCP_ROOT/$DHCP_LEASES chown dhcpd $DHCP_ROOT/$DHCP_LEASES # create a DHCP server config cat > $DHCP_ROOT/$DHCP_CONFIG << EOF option domain-name-servers 154.32.105.18,154.32.107.18,154.32.109.18; default-lease-time 86400; ddns-update-style none; subnet $WIFI_NET netmask $WIFI_MASK { range dynamic-bootp $WIFI_DHCP; option routers $WIFI_IP; default-lease-time 86400; max-lease-time 172800; } EOF # fire up DHCP daemon with the config /usr/sbin/dhcpd -chroot $DHCP_ROOT \ -cf $DHCP_CONFIG \ -pf $DHCP_PID \ -lf $DHCP_LEASES \ -user $DHCP_USER \ -group $DHCP_GROUP \ $WIFI else echo "dhcpd appears to be already running on wifi, not started" fi