WiFi Access Point with TP-Link TL-WN722n on Ubuntu 12.04

My system’s configuration:
– Ubuntu 12.04 Precise Pangolin
– TP-Link TL-WN722n USB WiFi Dongle

TO DO:
– Slow network performance, getting only 3-10Mbit/s
– With N-Mode activated, connection drops after a while

Sources:
http://forum.doozan.com/read.php?2,6300

Steps:

1. Network configuration

First edit /etc/network/interfaces, remove anything related to wlan0 (or whatever your device’s name) and add this lines to set the IP address of the wlan device as static:

auto wlan0
iface wlan0 inet static
    address 10.0.0.1
    network 10.0.0.0
    netmask 255.255.255.0
    broadcast 10.0.0.255

Although the configuration is already set, but in my system somehow it fails to set the IP address. So to make sure the setting will work, add ifup wlan0 inside /etc/rc.local file just before the exit 0:

#!/bin/sh -e
#
# rc.local

...

ifup wlan0

exit 0

2. hostapd installation

hostapd is the daemon who is responsible for broadcasting the access point and managing the connections. Rather than using hostapd package provided by Ubuntu repository, I would prefer to get the latest revision of hostapd from the developer’s git, then compile it manually. I do this because I want to enable the WiFi N-mode support.

But, installation from source will only give hostapd binaries, while we need several config files to run hostapd automatically as a service. I am no linux/Debian expert so I will just install hostapd from the repository, just to get the service configuration at /etc/default/, /etc/init.d/, and other places, then uninstall or remove the hostapd binaries.

git clone git://w1.fi/srv/git/hostap.git
cd hostap/hostapd

Then inside this folder, copy the defconfig file into a new file named .config, then open it using your favorite editor. For my setup I only need to activate (by uncommenting) the nl80211 driver and the N-Mode:

...
CONFIG_DRIVER_NL80211=y
...
CONFIG_IEEE80211N=y

Before installing hostapd, we need to install some dependencies:

sudo apt-get install libnl1 libnl1-dev

Then after that, compile and install hostapd:

make
sudo make install

The resulting binaries are hostapd and hostapd_cli located by default at /usr/local/bin/.

Now we can do a test run. Edit the config file /etc/hostapd/hostapd.conf:

interface=wlan0
ssid=MyAccessPoint
hw_mode=g
channel=6
auth_algs=1

# to enable N-Mode
# UPDATE: N-Mode is still problematic
#ieee80211n=1
#wmm_enabled=1

# config for WPA security
macaddr_acl=0
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=MyPassphrase123
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Make sure you don’t have trailing whitespaces, because hostapd is very sensitive. Now run hostapd:

sudo hostapd -dd /etc/hostapd/hostapd.conf

If nl80211 driver fails to start, quite possibly because NetworkManager is still managing the wireless network. Turn off the wlan management from NetworkManager by doing this on terminal (ref.):

sudo nmcli nm wifi off
sudo rfkill unblock wlan

If no problem exist, the output would be somehow like this:

random: Trying to read entropy from /dev/random
Configuration file: /etc/hostapd/hostapd.conf
nl80211: interface wlan0 in phy phy0
rfkill: initial event: idx=0 type=1 op=0 soft=0 hard=0
nl80211: Using driver-based off-channel TX
nl80211: Add own interface ifindex 4
nl80211: Set mode ifindex 4 iftype 3 (AP)
nl80211: Setup AP - device_ap_sme=0 use_monitor=1
nl80211: Create interface iftype 6 (MONITOR)
nl80211: New interface mon.wlan0 created: ifindex=7
nl80211: Add own interface ifindex 7
BSS count 1, BSSID mask 00:00:00:00:00:00 (0 bits)
nl80211: Regulatory information - country=CN
nl80211: 2402-2482 @ 40 MHz
nl80211: 5735-5835 @ 40 MHz
nl80211: Added 802.11b mode based on 802.11g information
Allowed channel: mode=1 chan=1 freq=2412 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=2 freq=2417 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=3 freq=2422 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=4 freq=2427 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=5 freq=2432 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=6 freq=2437 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=7 freq=2442 MHz max_tx_power=20 dBm
Allowed channel: mode=1 chan=8 freq=2447 MHz max_tx_power=20 dBm


....

nl80211: ifindex=4
nl80211: beacon_int=100
nl80211: dtim_period=2
nl80211: ssid - hexdump_ascii(len=8):
     59 6f 75 42 6f 74 41 50                           MyAccessPoint        
nl80211: hidden SSID not in use
nl80211: privacy=1
nl80211: auth_algs=0x1
nl80211: wpa_version=0x2
nl80211: key_mgmt_suites=0x2
nl80211: pairwise_ciphers=0x10
nl80211: group_cipher=0x10
wlan0: Event RX_MGMT (20) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event TX_STATUS (18) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received

This lines

wlan0: Event TX_STATUS (18) received
wlan0: Event TX_STATUS (18) received
wlan0: Event RX_MGMT (20) received
wlan0: Event RX_MGMT (20) received

Will show up when another PC/client is probing or scanning our Access Point. This is a good sign. Now quit the test run by pressing Ctrl-C

The next step to configure the Ubuntu services so hostapd could start automatically upon booting. First, edit /etc/init.d/hostapd file, and make sure the variables are correct:

...
DAEMON_SBIN=/usr/local/bin/hostapd
DAEMON_CONF=/etc/hostapd/hostapd.conf
...

Second, edit /etc/default/hostapd

...
RUN_DAEMON="yes"
DAEMON_CONF="/etc/hostapd/hostapd.conf";
DAEMON_OPTS="-dd -t";
...

And now we can manage the hostapd through Ubuntu’s service, and it will run automatically upon booting.

sudo service hostapd restart

For more information about managing the service, this could be a good reading.

3. DHCP and DNS

For DHCP purpose we are going to use dnsmasq because of the simplicity of configuration. Please note that dnsmasq might be not suitable for big network.

But one thing worth noting is that Ubuntu 12.04 (Network-Manager) is using dnsmasq as DNS resolver. We have to disable it so we can use dnsmasq as our DHCP and “DNS” server. To do this, edit /etc/NetworkManager/NetworkManager.conf file and put a # (comment sign) in front of dns=dnsmasq so it will look like this:

...
#dns=dnsmasq
...

For more information about this, check this out.

Now we can configure dnsmasq to fit our need

sudo apt-get -y install dnsmasq

It will create default complete config file /etc/dnsmasq.conf, which is very useful for reference. For the sake of readability let’s just rename that file and make a new empty config file:

cd /etc/
sudo mv dnsmasq.conf dnsmasq.conf.orig
sudo touch dnsmasq.conf

Then edit the newly created file, and fill that with this basic configuration:

interface=wlan0
expand-hosts
domain=local
dhcp-range=10.0.0.10,10.0.0.20,24h
dhcp-option=3,10.0.0.1

Now we’re done. Restart the server/PC and make sure everything works. We can check the log at /var/log/syslog anytime to see our DHCP server in action.

Sometimes I got problem that dnsmasq is not running automatically upon booting, this could be because the runlevel of dnsmasq is called before the networking devices are up. The easiest hack is to put the service restart command in the /etc/rc.local:

#!/bin/sh -e
#
# rc.local

...

ifup wlan0

sleep 5
service dnsmasq restart

exit 0