Tuesday, January 31, 2006

Instant gateway

I've written a script that will turn my laptop into an instant gateway if I have a connection on the lan port. It uses dnsmasq for dns forwarding and DHCP while iptables provides the forwarding/masquerading for ip.

I use this for providing my Nokia 770 a link when I only have a wired connection, or when I want to ssh into my Nokia 770.

#!/bin/sh
#
# Requires: dnsmasq, iptables debian packages

function start () {
  ifdown --force eth1
  ifconfig eth1 10.2.5.2 netmask 255.255.255.0
  iwconfig eth1 mode ad-hoc
  iwconfig eth1 essid maemo
  /etc/acpi/ibm-wireless.sh on
  iptables -F
  iptables -t nat -F
  iptables -t mangle -F
  iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  echo 1 > /proc/sys/net/ipv4/ip_forward
  /etc/init.d/dnsmasq start
}

function stop () {
  /etc/acpi/ibm-wireless.sh off
  ifdown --force eth1
  iptables -F
  iptables -t nat -F
  iptables -t mangle -F
  echo 0 > /proc/sys/net/ipv4/ip_forward
  /etc/init.d/dnsmasq stop
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: maemo {start|stop|restart}" >&2
    exit 1
    ;;
esac

exit 0