Please use a browser that supports javascript

Bogeskov.dk

How to create an accesspoint using a RealTek 8192cu Usb Wifi Dongle

Goal:

To plug in a usb dongle and create an instant accesspoint.

  • Running a bridge with a dnsmasq to serve dns/dhcp
  • Running a hostapd
  • Plug the wifi interface into the bridge
  • Avoid NertowkManager stealing the interface


Setting up a bridge

adding the following to /etc/network/interfaces:

auto br0
iface br0 inet static
        address 192.168.xxx.1
        netmask 255.255.255.0
        network 192.168.xxx.0
        broadcast 192.168.xxx.255
        bridge_ports none
        bridge_fd 1
        bridge_hello 1
        bridge_stp on

then run ifup br0.

Setting up dnsmasq to serve dhcp:

create the file: /etc/dnsmasq.d/dhcp.conf with this content:

dhcp-range=192.168.xxx.100,192.168.xxx.200,255.255.255.0,12h
dhcp-option=option:router,192.168.xxx.1
dhcp-option=option:dns-server,192.168.xxx.1
dhcp-authoritative

then run sudo service dnsmasq restart.

Getting the "right" 8192cu driver

The kernel supplied driver (tested with 3.11) is not capable of keeping a connection, when running as an accesspoint.

sudo apt-get install linux-headers-...
git clone https://github.com/dz0ny/rt8192cu.git
cd rt8192cu
make
sudo make install

Avoid the supplied driver by blacklisting it in (new file) /etc/modprobe.d/8192cu.conf:

blacklist rtl8192cu

unplug it, and remove the rtl8192cu driver (modprobe -r rtl8192cu)

Ensure NetworkManager doesn't control the interface

in /etc/NetworkManager/NetworkManager.conf:

ensure that:

[main]
plugins=ifupdown,keyfile

and:

[keyfile]
unmanaged-devices=mac:64:66:xx:xx:xx:xx

is set. You get the MAC address from running ifconfig with the dongle plugged in

then restart it sudo service network-manager restart.

Get a hostapd that matches this driver

Download the software from this url: realtek.com the file is called "Linux Kernel 2.6.18~2.6.38 and Kernel 3.0.8..." version: 3.4.4_*

unzip RTL8192xC_USB_linux_*.zip
tar zxvf RTL8188C_8192C_USB_linux_*/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_*.tar.gz
cd wpa_supplicant_hostapd-0.8_*/hostapd/
make
sudo cp hostapd hostapd_cli /usr/local/sbin/

for Raspberry PI you might want to change the line that says CFLAGS=... to "CFLAGS=-MMD -Os" in "Makefile".

Get a config for the hostapd

create the file: /etc/hostapd/hostapd.conf with this content:

ssid=MyAccessPoint
wpa_passphrase=MySecretPassword

ctrl_interface=/var/run/hostapd
interface=@INTERFACE@
bridge=br0
driver=rtl871xdrv
hw_mode=g
channel=6
wpa=2

beacon_int=100
hw_mode=g
ieee80211n=1
wme_enabled=1
ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+]
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
max_num_sta=8
wpa_group_rekey=86400

Make scripts that start/stop the hostapd service

Create the file: /usr/local/sbin/hostapd-start with this content:

#!/bin/bash

sed -e "s/@INTERFACE@/$INTERFACE/g" \
        < /etc/hostapd/hostapd.conf \
        > /etc/hostapd/hostapd-$INTERFACE.conf

if [ ! -d /var/run/hostapd ]; then
        rm -rf /var/run/hostapd
        mkdir /var/run/hostapd
fi

/usr/local/sbin/hostapd -B -P /var/run/hostapd/$INTERFACE.pid /etc/hostapd/hostapd-$INTERFACE.conf
brctl addif br0 $INTERFACE

then run chmod +x /usr/local/sbin/hostapd-start.

Create the file: /usr/local/sbin/hostapd-stop with this content:

#!/bin/bash

if [ -e /var/run/hostapd/$INTERFACE.pid ]; then
        read pid < /var/run/hostapd/$INTERFACE.pid
        if [ x$pid != x ]; then
                kill $pid
        fi
fi

then run chmod +x /usr/local/sbin/hostapd-stop.

Starting up on dongle plugin

To start up use udev

Create the file: /etc/udev/rules.d/98-usb-wifi.rules with this content:

ACTION=="add", SUBSYSTEM=="net", ENV{ID_VENDOR_ID}=="0bda", ENV{ID_MODEL_ID}=="8178", RUN+="/usr/local/sbin/hostapd-start"
ACTION=="remove", SUBSYSTEM=="net", ENV{ID_VENDOR_ID}=="0bda", ENV{ID_MODEL_ID}=="8178", RUN+="/usr/local/sbin/hostapd-stop"

This is for the TL-WN823N, you can get your own vendor/product id from lsusb.