Code and Stuff

Aug 11, 2011

Sharing Ethernet over WLAN (Buggy)

Requirements: A capable WIFI card, Ubuntu 11.04
Packages: hostapd, dhcp server


In an attempt to share my wired network to my android cell phone using wifi I found the following solution. It is still quite rough and not very stable, but fits my needs.

The solution is composed of 3 parts: hostapd, dhcp and a script.

hostapd

Hostapd is a software to create an accesspoint with a wifi card (exactly what I need). The configuration is quite simple and well documented. The config I used is the following (/etc/hostapd/hostapd.conf):
interface=wlan0
channel=1
driver=nl80211
ssid=THE_SSID

auth_algs=1

wpa=3
wpa_passphrase=THE_PASSWORD_GOES_HERE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
You might have to change it a little to match your config (Especially the driver, interface, ssid and wpa_passphrase). You can get a list of the interfaces with ifconfig

DHCP server

Next thing is to setup the DHCP server. This will give devices an IP when they connect to the WIFI network. My config file (/etc/dhcp/dhcpd.conf) looks like this:
# listen on wlan0
DHCPDARGS=wlan0; 

ddns-update-style none;

# change this according to your needs
option domain-name-servers 192.168.1.1;

default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 192.168.9.0 netmask 255.255.255.0 {
  range 192.168.9.10 192.168.9.254;
  option broadcast-address 192.168.9.254;
  option routers 192.168.9.1;
}
You might have to change it a little to match your environment especially the DNS. I'm using the 192.168.9.0/24 network for the Wireless network. The machine that is running the server has IP 192.168.9.1. The wlan interface has to be configured to this IP, or you will get an error.

The script

The last part is the script that starts it all. I've placed it in the /usr/local/sbin folder and set my path to look in there for executables.
#!/bin/bash

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# set the interface's IP
ifconfig wlan0 inet 192.168.9.1 netmask 255.255.255.0

# start the AP
hostapd /etc/hostapd/hostapd.conf > /dev/null &

# restart the DHCP server
/etc/init.d/isc-dhcp-server restart

here as well you will have to change the IP and interfaces if needed. This script could also be linked from some runlevel folder to be started at boot (you probably have to add the start/stop cases.

Problems

One major problem of this approach is when the computer goes into sleep mode. I never checked what happens exactly, but looks like the interface goes down and hostapd isn't that happy about it.

Other solution

I read on various forums that ubuntu's NetworkManager should be able to share the LAN as well.


No comments: