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
Advertisements

Setting Up WiFi Access Point with Edimax EW-7811UN on Raspberry Pi

I am running an image processing algorithm on my Raspberry Pi and I need to access it wirelessly (wired connection is sooo ’90s :p ). So my requirements are following:
– WiFi Access Point for remote access
– no need internet connection
– runs on Raspbian Wheezy

Steps:
1. Configuring network
Edit /etc/network/interfaces, remove anything related to wlan0 then add this lines:

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 defined, sometimes it won’t be applied by the system upon booting. To make sure, add ifup wlan0 inside /etc/rc.local file before exit 0 so it will look like this:

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

...
# add this
ifup wlan0

exit 0

2. Installing DHCP Server and “DNS” Configuration
DHCP is the one that is responsible to give out IP addresses to clients who are connected to the Access Point. Meanwhile, DNS or Name Service is working as some kind of dictionary, that translates numbers of IP address into human-readable names that are easy to remember and to type.

This time I will use dnsmasq for easy and fast DHCP+DNS deployment.

sudo apt-get -y install dnsmasq

After installation it will run the daemon automatically. Stop it and then we work on the configuration:

sudo service dnsmasq stop
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo touch /etc/dnsmasq.conf

Now edit the newly created dnsmasq.conf config file. For minimum setup, this should be the content of the config file:

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

For full configuration options, you can refer to the original config file (dnsmasq.conf.orig).

Although 10.0.0.1 is easy enough to remember, isn’t it easier if we use “raspi” as address? So then you can simply type ssh raspi or http://raspi/. To make this happen, edit /etc/hosts file, append with this:

10.0.0.1        raspi raspberrypi raspi.local raspberrypi.local

3. Installing hostapd
This WiFi USB Dongle from Edimax uses Realtek chipset, it is rtl8192 to be precise. Kindly enough, Realtek has provided hostapd implementation which is specific for this chipset.

For installation instructions, I suggest you to follow Jens Segers’ blog post.

After finishing installation, edit the hostapd configuration file to fit your needs.