# along with this program. If not, see <http://www.gnu.org/licenses/>.
INTERFACE=$1
-ADDR=$2
-DHCP_START=$3
-DHCP_END=$4
+WAN=$2
+ADDRESS=$3
+NETMASK=$4
+DHCP_START=$5
+DHCP_END=$6
-echo "Setting up routing for: $INTERFACE $ADDR $DHCP_START $DHCP_END" >> /tmp/mtetherd-net-setup.log
+log() {
+ echo $@
+ echo $(date): $@ >> /tmp/mtetherd-script.log
+}
+
+die() {
+ echo $@ >&2
+ echo $(date): $@ >> /tmp/mtetherd-script.log
+ exit 1
+}
+
+if [ "${INTERFACE}" = "" -o "${WAN}" = "" -o "${ADDRESS}" = "" -o "${NETMASK}" = "" -o "${DHCP_START}" = "" -o "${DHCP_END}" = "" ]; then
+ die "Usage: $0 <interface> <wan interface> <address> <netmask> <dhcp range start> <dhcp range end>"
+fi
+
+log "Setting up routing for: $INTERFACE $WAN $ADDRESS $NETMASK $DHCP_START $DHCP_END"
+
+echo "${INTERFACE}" | grep -E -q '^[a-zA-Z]+[0-9]+$' || die "Invalid interface name"
+echo "${WAN}" | grep -E -q '^[a-zA-Z]+[0-9]+$' || die "Invalid WANÂ interface name"
+echo "${ADDRESS}" | grep -E -q '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || die "Invalid address"
+echo "${NETMASK}" | grep -E -q '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || die "Invalid netmask"
+echo "${DHCP_START}" | grep -E -q '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || die "Invalid DHCP start address"
+echo "${DHCP_END}" | grep -E -q '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || die "Invalid DHCP end address"
+
+RUNFILE="/var/run/mtetherd.${INTERFACE}.pid"
+log "PID file = ${RUNFILE}"
+
+log "/sbin/ifconfig ${INTERFACE} ${ADDRESS} netmask ${NETMASK}"
+msg=$(/sbin/ifconfig ${INTERFACE} ${ADDRESS} netmask ${NETMASK} 2>&1)
+log $msg
+log "/sbin/modprobe ipt_MASQUERADE"
+msg=$(/sbin/modprobe ipt_MASQUERADE 2>&1)
+log $msg
+log "/usr/sbin/iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE"
+msg=$(/usr/sbin/iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE 2>&1)
+log $msg
+#-a, --listen-address=ipaddr Specify local address(es) to listen on.
+#-d, --no-daemon Do NOT fork into the background: run in debug mode.
+#-D, --domain-needed Do NOT forward queries with no domain part.
+#-f, --filterwin2k Don't forward spurious DNS requests from Windows hosts.
+#-F, --dhcp-range=ipaddr,ipaddr,time Enable DHCP in the range given with lease duration.
+#-g, --group=groupname Change to this group after startup (defaults to dip).
+#-h, --no-hosts Do NOT load /etc/hosts file.
+#-i, --interface=interface Specify interface(s) to listen on.
+#-I, --except-interface=int Specify interface(s) NOT to listen on.
+#-k, --keep-in-foreground Do NOT fork into the background, do NOT run in debug mode.
+#-K, --dhcp-authoritative Assume we are the only DHCP server on the local network.
+#-l, --dhcp-leasefile=path Specify where to store DHCP leases (defaults to /var/lib/misc/dnsmasq.leases).
+#-n, --no-poll Do NOT poll /etc/resolv.conf file, reload only on SIGHUP.
+#-N, --no-negcache Do NOT cache failed search results.
+#-o, --strict-order Use nameservers strictly in the order given in /etc/resolv.conf.
+#-p, --port=number Specify port to listen for DNS requests on (defaults to 53).
+#-q, --log-queries Log DNS queries.
+#-Q, --query-port=number Force the originating port for upstream DNS queries.
+#-R, --no-resolv Do NOT read resolv.conf.
+#-s, --domain=<domain> Specify the domain to be assigned in DHCP leases.
+#-u, --user=username Change to this user after startup. (defaults to nobody).
+#-x, --pid-file=path Specify path of PID file (defaults to /var/run/dnsmasq.pid).
+#-X, --dhcp-lease-max=number Specify maximum number of DHCP leases (defaults to 150).
+#-z, --bind-interfaces Bind only to interfaces in use.
+#-Z, --read-ethers Read DHCP static host information from /etc/ethers.
+#-1, --enable-dbus Enable the DBus interface for setting upstream servers, etc.
+#-2, --no-dhcp-interface=interface Do not provide DHCP on this interface, only provide DNS.
+#-9, --leasefile-ro Do not use leasefile.
+# --log-dhcp Extra logging for DHCP.
+# --min-port=<port> Specify lowest port available for DNS query transmission.
+log "/sbin/start-stop-daemon -S -p \"${RUNFILE}\" -b -x /usr/sbin/dnsmasq -- -x \"${RUNFILE}\" -k -I lo -i ${INTERFACE} -a ${ADDRESS} -z -F ${DHCP_START},${DHCP_END},3600"
+msg=$(/sbin/start-stop-daemon -S -p "${RUNFILE}" -b -x /usr/sbin/dnsmasq -- -x "${RUNFILE}" -k -I lo -i ${INTERFACE} -a ${ADDRESS} -z -F ${DHCP_START},${DHCP_END},3600 2>&1)
+log $msg
+log "echo 1 > /proc/sys/net/ipv4/conf/${INTERFACE}/forwarding"
+echo 1 > /proc/sys/net/ipv4/conf/${INTERFACE}/forwarding
+log "echo 1 > /proc/sys/net/ipv4/conf/${WAN}/forwarding"
+echo 1 > /proc/sys/net/ipv4/conf/${WAN}/forwarding
+
+log "Finished setting up routing for $INTERFACE"