Updated documentation for 0.2 release
[mtetherd] / tethering.sh
1 #!/bin/sh
2 #
3 # /usr/bin/tethering.sh
4 #
5 # Enable tethering on USB network and Bluetooth PAN.
6 #
7 # Note that the INETDEV must be up and running when this script is
8 # started, or forwarding will not be enabled.
9 # Having tethering on multiple devices concurrently is possible,
10 # but forwarding will be disabled once the first device disconnetcs.
11 # A better check is necessary.
12 #
13 # Put this into /etc/dbus-scripts/tethering :
14 # /usr/bin/tethering.sh * * org.freedesktop.Hal.Manager DeviceAdded
15 # /usr/bin/tethering.sh * * org.freedesktop.Hal.Manager DeviceRemoved
16 #
17 # dbus-scripts doesn't give us the event path, so we have to use a more
18 # convoluted method of getting the network interface.
19 #
20
21 EVENT="$4"
22 UDI="$5"
23 HALDEV="$(echo $UDI | sed 's#.*/\([0-9a-zA-Z_]*\)#\1#')"
24 LOG="/tmp/tethering.log"
25 INETDEV="gprs0"
26 RUNFILE="/var/run/tethering.$HALDEV.pid"
27 IFACEFILE="/var/run/tethering.$HALDEV.iface"
28
29 if [ "$EVENT" = "DeviceAdded" ]; then
30         IFACE=$(hal-get-property --udi $UDI --key net.interface)
31         case $IFACE in
32                 bnep0 )
33                         IF_ADDRESS=192.168.254.254
34                         IF_RANGE=192.168.254.1,192.168.254.254
35                 ;;
36                 usb0 )
37                         IF_ADDRESS=192.168.253.254
38                         IF_RANGE=192.168.253.1,192.168.253.254
39                 ;;
40                 * )
41                         exit 0
42                 ;;
43         esac
44
45         echo "$(date) Enabling tethering on interface $IFACE" >> $LOG
46         echo "$IFACE" > $IFACEFILE
47         ifconfig "$IFACE" "$IF_ADDRESS"
48         /sbin/modprobe ipt_MASQUERADE
49         /usr/sbin/iptables -t nat -A POSTROUTING -o $INETDEV -j MASQUERADE
50         /usr/sbin/start-stop-daemon -S -p "$RUNFILE" -b -x /usr/sbin/dnsmasq -- -x "$RUNFILE" -k -I lo -i "$IFACE" -a $IF_ADDRESS -z -F $IF_RANGE,3600
51         echo 1 > /proc/sys/net/ipv4/conf/$IFACE/forwarding
52         echo 1 > /proc/sys/net/ipv4/conf/$INETDEV/forwarding
53 fi
54
55 if [ "$EVENT" = "DeviceRemoved" ]; then
56         if [ ! -f "$IFACEFILE" ]; then
57                 exit 0
58         fi
59         IFACE="$(cat $IFACEFILE)"
60         rm "$IFACEFILE"
61         echo "$(date) Disabling tethering on device $IFACE" >> $LOG
62         echo 0 > /proc/sys/net/ipv4/conf/$INETDEV/forwarding
63         /usr/sbin/start-stop-daemon -K -p "$RUNFILE" -x /usr/sbin/dnsmasq
64         /usr/sbin/iptables -t nat -D POSTROUTING -o $INETDEV -j MASQUERADE
65 fi
66
67 exit 0