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
- connect the two NICs in ad-hoc mode
- set up network address translation on the “server”
- set up a small DNS and DHCP server
# by default, only provide DNS on the loopback interface:In /etc/dnsmasq-wlan two files need to be created:
interface=lo
# read an additional config file:
conf-file=/etc/dnsmasq-wlan/dhcp.conf
# /etc/dnsmasq-wlan/dhcp-on.confand
# 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.confFinally, here’s the init script (/etc/init.d/wlan-server):
# disable DNS and DHCP for eth1
except-interface=eth1
#!/bin/shThe init script above is written for a Ubuntu system, but it should not be too difficult to adapt it to other systems.
# 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
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: Linux