17.3.07

Ad-hoc mode as cheap Access Point replacement

Nowadays most laptops come with a built-in WLAN device. That’s also the case for my Thinkpad. Unfortunately I don’t have a wireless access point and I'm not inclined to shell out 50€ just for occasional web browsing from downstairs. But with a second WLAN device it’s actually possible to share an Internet connection without using a AP. This solution, which I’ll describe below, is based on the less known ad-hoc mode. In this operation mode, two or more WLAN NICs can communicate directly with each other without an intermediary device. However, for a seamless operation it’s also necessary to have DHCP and DNS servers, so that clients can have instant access to the Internet. That’s why I had to In the following, I assume that you have two PCs or laptops at your disposal: a “server” disposing of a wired Internet connection and a WLAN NIC, and a “client” which will use the shared internet connection. Some experience with Linux administration may be helpful. To allow for easy activation and deactivation, the server’s NIC is controlled by a small init script. The only requirements are a working WLAN NIC (i.e. the necessary kernel modules are loaded), iptables, and dnsmasq. The latter is a small DNS cache which can also assume the role of a DHCP server. As such it’s ideal for this project. To make it all work as intended, we have to use two different dnsmasq configurations: one with DNS and DHCP on the WLAN interface and one without. The relevant lines in /etc/dnsmasq.conf are the following:
# by default, only provide DNS on the loopback interface:
interface=lo
# read an additional config file:
conf-file=/etc/dnsmasq-wlan/dhcp.conf
In /etc/dnsmasq-wlan two files need to be created:
# /etc/dnsmasq-wlan/dhcp-on.conf
# re-enable DNS and DHCP for eth1
interface=eth1
dhcp-range=192.168.2.50,192.168.2.150,12h
and
# /etc/dnsmasq-wlan/dhcp-off.conf
# disable DNS and DHCP for eth1
except-interface=eth1
Finally, here’s the init script (/etc/init.d/wlan-server):
#!/bin/sh
# Set up ad-hoc wlan network with WEP, NAT, DNS and DHCP
# TODO: firewalling
# WARNING: WEP encryption is *broken*. Don't rely on it for
# strong security!
#
# Setup:
# echo "conf-file=/etc/dnsmasq-wlan/dhcp.conf" >> /etc/dnsmasq.conf
# mkdir /etc/dnsmasq-wlan
#
# /etc/dnsmasq-wlan/dhcp-on.conf:
# # re-enable DNS and DHCP for eth1
# interface=eth1
# dhcp-range=192.168.2.50,192.168.2.150,12h
#
# /etc/dnsmasq-wlan/dhcp-off.conf:
# # disable DNS and DHCP for eth1
# except-interface=eth1

set -e

WLAN=eth1
LAN=eth0
ADHOCESSID=thinkpadadhoc
IP=192.168.2.2
WEPKEY=1234-5678-90 #needs to be changed...
# some NICs need to be given a channel number
CHANNEL="channel 11"

startwlan() {
# Wireless configuration
iwconfig $WLAN mode Ad-Hoc
iwconfig $WLAN essid "$ADHOCESSID" $CHANNEL key restricted $WEPKEY
ifconfig $WLAN $IP
# Reset iptables NAT settings
iptables -t nat -F
iptables -t mangle -F
# Set up Network Address Translation
iptables -t nat -A POSTROUTING -o $LAN -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
# Enable dnsmasq DHCP server on WLAN
cp /etc/dnsmasq-wlan/dhcp-on.conf /etc/dnsmasq-wlan/dhcp.conf
/etc/init.d/dnsmasq restart
}

stopwlan() {
# TODO: reset wlan interface
# Reset iptables NAT settings
iptables -t nat -F
iptables -t mangle -F
# disable DNS and DHCP on WLAN
cp /etc/dnsmasq-wlan/dhcp-off.conf /etc/dnsmasq-wlan/dhcp.conf
# force reload of config files
/etc/init.d/dnsmasq restart
}

case "$1" in
start)
start-wlan
;;
stop)
stop-wlan
;;
restart|reload|force-reload)
stop-wlan
start-wlan
;;
esac
exit 0
The init script above is written for a Ubuntu system, but it should not be too difficult to adapt it to other systems.

On the client side, setting up the network is quite easy, given that the server automatically assigns IP addresses and transmits the correct DNS parameters. Windows XP has no trouble connecting to this network, you’ll just have to provide the encryption key. Unfortunately, Ubuntu clients still have trouble connecting to ad-hoc networks. You can manually set up the connection with this command:
iwconfig eth1 essid lenovoadhoc mode Ad-Hoc key restricted 1234-5678-90

If you’ve found this guide useful, please tell me so.

Labels: