How to (incomplete): Make a RasPi AP with a RTL8xxx Wifi Adaptor

UPDATE 5/8/2022: I no longer have any working RTL8xxx WiFi adaptors, so unfortunately it doesn’t look like I’ll be completing this post for the foreseeable future. I will come back and update it later if I can, though.

Hi everyone. So, you may be wondering why I’m writing this oddly-specific post. Well, I happen to have one of these (seriously annoying) wifi adaptors, and I want to make a raspberry-pi based drone with my old Raspberry Pi 1 B+. Why? Because it’s fun. Well, not this bit…

Didn’t you mention having done this before?

Yep, and it didn’t work very well either :D. I was trying to use it as a stationary access point, but it always crashed after a few hours. I never found out why, but it was probably a driver issue. Sadly, after spending ages making it work and then giving up, I deleted my configuration, because I’m an idiot.

In this post, then, I’ll be doing this again on modern Raspbian, and hopefully the result will be good enough to work for my short-term pi drone desires. This will be more difficult than before, as it seems the legacy wireless extensions have been removed from the kernel, and I will need to patch and rebuild it. I will also need to rebuild hostapd, with the patch for the special rtl871x driver.

Why is this such a big deal?

Well, for most Wifi adaptors, it doesn’t seem to be. However, the RTL8xxx series of chips have some issues on Linux, but can be made to work as access points. There are a few tutorials for this, but they all run on old versions of Raspbian for around 2012-2015 and hence are now quite out of date. I’ll link to anything I found helpful at the end, but essentially this is why I’m writing this post. This may also work with other similar adaptors like the RTL8192 series, though my specific device is an RTL8188CUS.

Is this worth it?

Maybe. For me, it has been a learning experience, and after the drone is working, I probably won’t continue to update the software. For you, rebuilding the kernel regularly will take ages on a raspberry pi, so you may want to simply find another wifi adaptor that is better supported. Again, though, for an offline system, this should be fine. RTL8188CUS-based adaptors are very cheap and highly available (link for Ew-7811un), as are RTL8192-based ones, so I feel it’s still worth sharing the knowledge I have found from these resources in one place.

NOTE: This tutorial is not for the faint of heart – there is a lot of compiling and configuring needed, and you should expect it to take a significant amount of time to get everything set up. I’ve tried this with two RTL8188CUS adaptors and one RTL8192CU adaptor. There’s another tutorial coming soon which covers setting up other wifi adaptors – much quicker and easier.

NOTE 2: This tutorial is incomplete and currently doesn’t work. At the end, the remaining issues are described for anyone clever & brave enough to figure it out. I’d love to get this working fully, but I’ve run out of time.

NOTE 3: I do eventually hope to get this finished, but for now I’d advise you go with a Ralink RT5370 or similar chipset for your WiFi adaptor – those seem to be much easier to get going. I will make a tutorial for those shortly.

RTL8xxx – Setting it up

Checking adaptor

First, you need to make sure you have an RTL chip in your wifi adaptor. The easiest way is to plug it into a Linux machine and run lsusb in a terminal. You should see something like this in the output:

Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter

Note that the model doesn’t matter too much – the main deal is that as long as you see RTL8xxx in the output, you should be good. If RTL8xxx isn’t somewhere in the output, double check the adaptor is plugged in and working (some will light up). Otherwise, you’ll need to find another tutorial to help you set up your access point. I’ve linked to a lot of these at the end of this article, so perhaps one of those will help you.

Now, you need to find the device name for your wifi adaptor. Remove any other wifi adaptors and run: iwconfig.

wlan0     IEEE 802.11  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=20 dBm   
          Retry short limit:7   RTS thr=2347 B   Fragment thr:off
          Power Management:off
          
lo        no wireless extensions.

eth0      no wireless extensions.

Ignore any devices that have no wireless extensions, and you should be left with only one device, the wifi adaptor. It may have a long name starting with “wlx”. It may also simply be “wlan0” as seen in my output. Make a note of this device name because we’ll be needing it later.

Checking adaptor capabilities

Run iw list. This will output a lot of information, but all you need to check is whether “AP” is listed under “Supported interface modes”, like this:

Supported interface modes:
	 * IBSS
	 * managed
	 * AP
	 * AP/VLAN
	 * monitor
	 * mesh point
	 * P2P-client
	 * P2P-GO

Again, it only matters that “AP” shows up here. Everything else can be ignored. If “AP” doesn’t show up, then unfortunately your adaptor cannot be used as an access point, and you’ll need to source another one. This is quite a useful resource.

Installing software

Make sure your Raspbian installation is fully up to date. I tested these instructions with Raspbian 10 “Buster”. Run the following command to install the software.

sudo apt update
sudo apt install iw hostapd isc-dhcp-server iptables wpasupplicant

Patching and building the kernel

This loosely follows the tutorial at https://www.raspberrypi.org/documentation/linux/kernel/building.md but simplifies things by telling you exactly what to run. I compiled the kernel on my Pi for simplicity, but be aware this will take a long time on older Pis (32 hours on my Pi 1 B+ at stock speeds, and 18 hours with turbo overclock!).

Install build dependencies:

sudo apt install git bc bison flex libssl-dev make

Download the kernel sources (takes a few minutes, ~ 200MB download). This will fetch the latest stable kernel (4.19 at the time of writing):

git clone --depth=1 https://github.com/raspberrypi/linux

Next, patch the kernel:

wget https://pastebin.com/raw/2nmLS4d6/ -O rtlpatch.c
cd linux
patch -Np2 < ../rtlpatch.c

Ignore the message about the patch unexpectedly ending. You should see the message:

Hunk #7 succeeded at 1110 with fuzz 2.

Next, prepare the kernel, using the right commands for your model of Pi.

Raspberry Pi 1, Pi Zero, Pi Zero W, and Compute Module:

KERNEL=kernel
make bcmrpi_defconfig

Pi 2, Pi 3, Pi 3+, and Compute Module 3

KERNEL=kernel7
make bcm2709_defconfig

Raspberry Pi 4

KERNEL=kernel7l
make bcm2711_defconfig

Finally, compile the kernel:

make -j4 zImage modules dtbs
sudo make modules_install
sudo cp arch/arm/boot/dts/*.dtb /boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
sudo cp arch/arm/boot/zImage /boot/$KERNEL-rtlpatched.img

Building 8192cu driver

Instructions adapted from https://github.com/pvaret/rtl8192cu-fixes

Install requirements:

sudo apt install git build-essential dkms

Download the code from the git repository, build and install:

git clone https://github.com/pvaret/rtl8192cu-fixes.git
sudo dkms add ./rtl8192cu-fixes
sudo dkms install 8192cu/1.11
sudo depmod -a
sudo cp ./rtl8192cu-fixes/blacklist-native-rtl8192.conf /etc/modprobe.d/

Patching and building hostapd

NOTE: I ended up having to use a pre-built hostapd. You can download and install this with:

wget "http://raspberry-at-home.com/files/hostapd.gz"
sudo gzip -d hostapd.gz
sudo chmod 755 hostapd
sudo cp hostapd /usr/sbin/

A copy of that version of hostapd is available at https://www.hamishmb.com/blog/wp-content/uploads/2020/02/hostapd.gz in case the original goes offline.

If you want to try to build your own hostapd (probably extra configuration needed), see below.

Adapted from https://charlesz-p.blogspot.com/2015/07/hosting-wi-fi-access-point-on-debian.html and https://github.com/pritambaral/hostapd-rtl871xdrv#installation.

Install requirements:

sudo apt install libnl-3-dev libnl-genl-3-dev

Download hostapd: https://w1.fi/hostapd/. I tried version 2.9, but it may work better with other versions. Patch and build hostapd (takes around 40 mins to build on a Pi 1 B+):

tar xvf <hostapd-tarball.tar.gz>
git clone https://github.com/pritambaral/hostapd-rtl871xdrv
cd hostapd-<version>
patch -Np1 -i </path/to/rtlxdrv.patch>
cd hostapd
cp -v defconfig .config
echo CONFIG_DRIVER_RTW=y >> .config

make
sudo cp hostapd /usr/sbin/hostapd

RTL8xxx – Configuration

This is adapted from https://charlesz-p.blogspot.com/2015/07/hosting-wi-fi-access-point-on-debian.html.

hostapd

This is the service that manages your access point. To edit its configuration, type sudo nano /etc/hostapd/hostapd.conf, and give it the following contents:

interface=wlan0
ssid=RPI_TEST
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=ubuntu123
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
driver=rtl871xdrv
hw_mode=g
#ieee80211n=1

Uncomment the last line for 802.11n support. Be very careful typing this, and preferably copy-and-paste, because it can result in various confusing errors if something is wrong. Comments and whitespace can both cause problems. One of the key differences to the usual here is the line driver=rtl871x, instead of the usual driver=nl80211. This is used here because the RTL chips need a special driver to work.

Next, run sudo nano /etc/default/hostapd and give it the following contents:

DAEMON_CONF="/etc/hostapd/hostpd.conf"

Static IP Configuration for the WiFi adaptor

Run sudo nano /etc/network/interfaces.d/00-ap and give it the following contents, substituting wlan0 with the name of your WiFi adaptor:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.2.1
netmask 255.255.255.0

DHCP Server (optional)

First run sudo nano /etc/default/isc-dhcp-server and give it the following contents:

# Defaults for isc-dhcp-server (sourced by /etc/init.d/isc-dhcp-server)

# Path to dhcpd's config file (default: /etc/dhcp/dhcpd.conf).
DHCPDv4_CONF=/etc/dhcp/dhcpd.conf
#DHCPDv6_CONF=/etc/dhcp/dhcpd6.conf

# Path to dhcpd's PID file (default: /var/run/dhcpd.pid).
#DHCPDv4_PID=/var/run/dhcpd.pid
#DHCPDv6_PID=/var/run/dhcpd6.pid

# Additional options to start dhcpd with.
#       Don't use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=""

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#       Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACESv4="wlan0"
INTERFACESv6=""

Next run sudo nano /etc/dhcp/dhcpd.conf and give it the following contents (you can change the IP and network range to whatever you want, but this is fine for me and probably doesn’t conflict with your home network):

default-lease-time 84600;
max-lease-time 84600;
ddns-update-style none;
subnet 192.168.2.0 netmask 255.255.255.0 {
  range 192.168.2.130 192.168.2.254;
  option broadcast-address 192.168.2.255;
  option routers 192.168.2.1;
  option domain-name "local";
  option domain-name-servers 192.168.2.1;
}

RTL8xxx – Remaining Problems

As I said at the start, this tutorial is incomplete. It turns out it’s much more complicated to setup an RTL8xxx-based adaptor than I thought it would be. The final issue is that, while the access point starts, there’s an instant kernel panic (Fatal exception in interrupt) as soon as anything tries to connect :/.

I will be happy to provide logs and any other information to help get this tutorial finished, I just need someone with time and knowledge of the kernel and drivers to help me 🙂

Also note that hostapd does seem to start on boot, but this can be resolved by manually running:

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

However, I’ve heard that this isn’t a problem with Linux Kernel 4.15.x (https://github.com/raspberrypi/linux/issues/2979), and a few people have somehow got it working, so that might be worth a try for someone brave who has time. Without the custom kernel or the 8192cu driver (instead of rtl8192), it doesn’t work at all unfortunately.

Summary

If you made it this far, well done! 🙂 It’s a total pain to get rtl chips working in Access Point mode. I hope this tutorial, though incomplete, can help anyone on the way to figuring this out. If you get this working or have some ideas for fixing the final few issues, please get in touch so I can get this tutorial completed.

I do eventually hope to get this finished, but for now I’d advise you go with a Ralink RT5370 or similar chipset for your WiFi adaptor – those seem to be much easier to get going. I will make a tutorial for those shortly.

RTL8xxx Resources

Here is a list of resources I found helpful when writing this tutorial, in no particular order.

Tagged , , , , , , , . Bookmark the permalink.

About Hamish McIntyre-Bhatty

I'm a self-employed software developer working on Free Software projects, as well as studying for my degree with the Open University. Being pedantic when it comes to detail is fortunately useful for both of these things! A strong believer in free software, I have a few pay-for programs available under the GPLv3 and enjoy reporting bugs and helping to improve various open source projects, including volunteering at Wimborne Model Town to work on their river control system.

Leave a Reply

Your email address will not be published. Required fields are marked *