3 # /usr/bin/tethering.sh
5 # Enable tethering on USB network and Bluetooth PAN.
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.
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
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.
23 HALDEV="$(echo $UDI | sed 's#.*/\([0-9a-zA-Z_]*\)#\1#')"
24 LOG="/tmp/tethering.log"
26 RUNFILE="/var/run/tethering.$HALDEV.pid"
27 IFACEFILE="/var/run/tethering.$HALDEV.iface"
29 if [ "$EVENT" = "DeviceAdded" ]; then
30 IFACE=$(hal-get-property --udi $UDI --key net.interface)
33 IF_ADDRESS=192.168.254.254
34 IF_RANGE=192.168.254.1,192.168.254.254
37 IF_ADDRESS=192.168.253.254
38 IF_RANGE=192.168.253.1,192.168.253.254
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 start-stop-daemon -S -p "$RUNFILE" -m -b -x /usr/sbin/dnsmasq -- -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
55 if [ "$EVENT" = "DeviceRemoved" ]; then
56 if [ ! -f "$IFACEFILE" ]; then
59 IFACE="$(cat $IFACEFILE)"
61 echo "$(date) Disabling tethering on device $IFACE" >> $LOG
62 echo 0 > /proc/sys/net/ipv4/conf/$INETDEV/forwarding
63 start-stop-daemon -K -p "$RUNFILE" -x /usr/sbin/dnsmasq
64 /usr/sbin/iptables -t nat -D POSTROUTING -o $INETDEV -j MASQUERADE