WLAN Tethering

WLAN tethering is a mechanism to share a mobile connection. This is very handy if someone has multiple devices, but only one 3G device with a reasonable data plan. Since a mobile (smart) phones typically have WLAN and 3G, it is possible to connect to the Internet with 3G and set WLAN to act as an access point. Since I went to vacations with 3 devices with WLAN capabilities (2 Android phones, 1 laptop), I wanted to use the 3G network for all my devices without changing SIM cards all the time.

I investigated how to get my Android phone to act as an access point. I soon found out that without a rooted phone, it is a hassle. For a rooted phone, you have to have a custom kernel with iptables support to use this. Furthermore, the phone gets hot. Other solutions do not require a rooted phone, such as installing a proxy on the Android phone, and using the USB cable to forward packets. All you need to do, is to install the Android SDK, enable USB debugging on your phone, and you have your proxy. The downside is that it is only a proxy, and for each application you have to configure the proxy settings manually.

Thus, I did not use my Android phone for WLAN tethering, but opted for a mobile access point with a 3G USB stick with my laptop.

Mobile Access Point

To use your laptop as a mobile access point, you need to install hostapd, which is a user space daemon for access points. It supports standards, such as 802.1X/WPA/EAP. It is important to know that you most likely need a recent wifi driver, which is mac80211 based, for an easy installation. If you have such a new driver, you can set your hostapd.conf to

interface=wlan0
driver=nl80211
ssid=test
channel=1

I recommend also to use some kind of encryption, because if other people are using your 3G connection and you are paying on a bandwidth basis, it can get expensive. With /etc/init.d/hostapd start you can start the daemon and test if it works. The next thing I did was to create a script in /etc/NetworkManager/dispatcher.d/, which starts the access point automatically when you insert the USB stick and the laptop was on AC power. Make sure you disable “Wireless Networking” in the Network Manager!

#!/bin/bash
# Script to dispatch NetworkManager events
#
# Starts a mobile access point if on AC power

if [ -z "$1" ]; then
    echo "$0: called with no interface" 1>&2
    exit 1;
fi

IFACE="$1"
MODE=`cat /proc/acpi/ac_adapter/ACAD/state | cut -d':' -f2 | tr -d ' '`

# Run the right scripts
case "$2" in
    up)
        if [ "$IFACE" == "ppp0" ] && [ "$MODE" == "on-line" ]; then
                ifconfig wlan0 192.168.0.1 netmask 255.255.255.0 up
                iwconfig wlan0 channel 6
                iwconfig wlan0 txpower 20
                /etc/init.d/dnsmasq start
                echo "1" > /proc/sys/net/ipv4/ip_forward
                iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
                /etc/init.d/hostapd start
        fi
        ;;
    down)
        if [ "$IFACE" == "ppp0" ] ; then
                /etc/init.d/hostapd stop
                /etc/init.d/dnsmasq stop
                echo "0" > /proc/sys/net/ipv4/ip_forward
                iptables -t nat -F
                iwconfig wlan0 mode managed
        fi
        ;;
    *)
        echo "$0: called with unknown action \`$2'" 1>&2
        exit 1
        ;;
esac

Make the 3G connection start automatically in “Network Manager”, plug in the 3G USB stick and your laptop is an WLAN access point that connects with 3G to the Internet.