2

I'm trying to make a very simple setup with my RPi. On the on-board standard wlan0 I want to connect to my home network normally, using a wpa_supplicant file this works very well.

Now I'm having issue because I plugged a wifi dongle in my RPi (Trendnet TEW-649UB) and I want to setup an AP on wlan1. It is properly detected by my RPi and I can ifconfig it.

wlan1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    ether 00:14:d1:be:6a:a3  txqueuelen 1000  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Now before anyone says look at the hundred of posts on the internet, I did and very few of them actually are for Raspbian Stretch, even the ones on the official website do not take into account Stretch.

Just for the record:

pi@race_tracker_gateway:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"

So first here are my wpa_supplicant files. First the wlan0 one, which works fine because it connects at start-up to my network without any issue.

pi@race_tracker_gateway:~ $ cat /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
# allow frontend (e.g., wpa_cli) to be used by all
ctrl_interface=/var/run/wpa_supplicant GROUP=netdev
country=CH
#
# home network; allow all valid ciphers
network={
    ssid="hello*walter"
    psk="XXXX"
    priority=1
    id_str="Home WiFi"
}

I'm not really sure on what to put in the wlan1 supplicant file so I just kept the basics

pi@race_tracker_gateway:~ $ cat /etc/wpa_supplicant/wpa_supplicant-wlan1.conf
ctrl_interface=/var/run/wpa_supplicant GROUP=netdev
country=CH

Searching around on the internet, after a while I found out I should not be modifying my /etc/network/interfaces file, so it is as default.

pi@race_tracker_gateway:~ $ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

Then I used part of this guide to configure dhcpcd : https://www.raspberrypi.org/documentation/configuration/wireless/access-point.md My goal is to set a static ip address to the AP interface so I simply put the two last lines as shown here

pi@race_tracker_gateway:~ $ tail /etc/dhcpcd.conf 
#static ip_address=192.168.1.23/24
#static routers=192.168.1.1
#static domain_name_servers=192.168.1.1

# fallback to static profile on eth0
#interface eth0
#fallback static_eth0

interface wlan1
static ip_address=192.168.4.1/24

I setup the dhcp address pool

pi@race_tracker_gateway:~ $ cat /etc/dnsmasq.conf
interface=wlan1      # Use the require wireless interface - usually wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

Then I configured hostapd and that's where I need help. Here is the content.

pi@race_tracker_gateway:~ $ cat /etc/hostapd/hostapd.conf
interface=wlan1
driver=r8712u
ssid=RaceTrackerGateway
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=internetromero
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

My problem is related to the driver line, because when I start hostapd I get an error.

pi@race_tracker_gateway:~ $ sudo hostapd /etc/hostapd/hostapd.conf 
Configuration file: /etc/hostapd/hostapd.conf
Line 2: invalid/unknown driver 'r8712u'
1 errors found in configuration file '/etc/hostapd/hostapd.conf'
Failed to set up interface with /etc/hostapd/hostapd.conf
Failed to initialize interface

I got the driver name doing the following

pi@race_tracker_gateway:~ $ sudo lshw -C network
...
*-network:1
       description: Wireless interface
       physical id: 3
       bus info: usb@1:1.2
       logical name: wlan1
       serial: 00:14:d1:be:6a:a3
       capabilities: ethernet physical wireless
       configuration: broadcast=yes driver=r8712u multicast=yes wireless=unassociated

However for some reason hostapd refuses to use that driver. I can get more info on the driver doing this.

pi@race_tracker_gateway:~ $ modinfo r8712u
filename:       /lib/modules/4.14.62-v7+/kernel/drivers/staging/rtl8712/r8712u.ko
firmware:       rtlwifi/rtl8712u.bin
author:         Larry Finger
description:    rtl871x wireless lan driver
license:        GPL

So I really don't understand why hostapd refuses this driver. When using the standard nl80211 I get the following error (if you are wondering why I changed driver).

pi@race_tracker_gateway:~ $ sudo hostapd /etc/hostapd/hostapd.conf
Configuration file: /etc/hostapd/hostapd.conf
nl80211: Could not configure driver mode
nl80211: deinit ifname=wlan1 disabled_11b_rates=0
nl80211 driver initialization failed.
wlan1: interface state UNINITIALIZED->DISABLED
wlan1: AP-DISABLED 
hostapd_free_hapd_data: Interface wlan1 wasn't started

So I guess here is my story, this is driving crazy, also it's driving me crazy because I've been trying to solve this problem for days and at some point I even managed to make it work, I could see my AP wifi, wlan1 was assigned the correct IP but after a reboot of the RPi and probably some more changing of settings it doesn't work anymore.

If someone could help me I'd be really grateful because I'm starting to think I'm crazy. I suspect something is wrong with the dongle because everytime I reboot my Pi I have the physically unplug and replug the dongle for it to show up as wlan1 so I guess something must be wrong.

Leo
  • 123
  • 1
  • 3
  • I finally fixed it... I guess writing all of this out was good! The problem was related to a bug in the Linux Kernel, I did a sudo rpi-update and it's now working after reboot. Still having to restart hostapd after a reboot but that's a minor annoyance compared to not having it work at all. – Leo Aug 29 '18 at 15:53

1 Answers1

6

Update:
I have found that it is better to use wlan0 as interface for the access point and wlan1 as client up-link. But this other way around does not answer the question. So I have made a new question Access point as WiFi repeater with additional WiFi-dongle. Look there for an up to date configuration. This answer here will not maintained anymore.



The real issue isn't a bug in the kernel. I assume that's why to much different components must play together.

Btw. it isn't a good idea to execute rpi-update unless yo exactly know why to do this or told by a developer to do it for testing. On github - rpi-update You will find:

Even on Raspbian you should only use this with a good reason. This gets you the latest bleeding edge kernel/firmware. There is always the possibility of regressions. Bug fixes and improvements will eventually make their way into new Raspbian releases and apt-get when they are considered sufficiently well tested. A good reason for using this would be if you like to help with the testing effort, and are happy to risk breakages and submit bug reports. These testers are welcome.

Do you know that the Raspberry Pi can span an access point and connect as client to another wifi network with its on board wifi chip? You do not need a second wifi dongle. How to do that you can look at Access point as WiFi repeater, optional with bridge.

But using a second wifi dongle is really very easy. With systemd-networkd and wpa_supplicant we have everything on the Raspberry Pi to setup what you want in a consistent way. There is no need to install additional software and fiddle with hostapd and dnsmasq. You have to switch to systemd-networkd and then simply set up wpa_supplicant one time for wlan0 as client and one time for wlan1 as access point. Then configure the interfaces and it's done. Let's do it. For reference I use Raspbian Stretch Lite 2018-11-13 updated with sudo apt update && sudo apt full-upgrade && sudo reboot done at 2019-02-02.


Enable systemd-networkd

For detailed information look at (1). Here only in short. Execute these commands:

# disable classic networking
rpi ~$ sudo -Es
rpi ~# systemctl mask networking.service
rpi ~# systemctl mask dhcpcd.service
rpi ~# mv /etc/network/interfaces /etc/network/interfaces~
rpi ~# sed -i '1i resolvconf=NO' /etc/resolvconf.conf

# enable systemd-networkd
rpi ~# systemctl enable systemd-networkd.service
rpi ~# systemctl enable systemd-resolved.service
rpi ~# ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

Configure wpa_supplicant as client and as access point

To configure wpa_supplicant create these two files with your settings for country=, ssid=, psk= and maybe frequency= You can just copy and paste this in one block to your command line beginning with cat and including EOF (delimiter EOF will not get part of the file):

A) For client connection:

rpi ~# cat > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="wlan@hoeft-online.de"
    psk="verySecretPassword"
}
EOF

rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
rpi ~# systemctl enable wpa_supplicant@wlan0.service

For the connection to the internet router we use network address translation (NAT). To setup it we extend the service for wlan0 with:

rpi ~# systemctl edit wpa_supplicant@wlan0.service

In the empty editor insert these statements, save them and quit the editor:

[Service]
ExecStartPre=/sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
ExecStopPost=-/sbin/iptables -t nat -D POSTROUTING -o wlan0 -j MASQUERADE

B) As access point:

rpi ~# cat > /etc/wpa_supplicant/wpa_supplicant-wlan1.conf <<EOF
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="RPiNet"
    mode=2
    key_mgmt=WPA-PSK
    psk="anotherPassword"
    frequency=2412
}
EOF

rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan1.conf
rpi ~# systemctl enable wpa_supplicant@wlan1.service

Configure interfaces

Create these two files:

rpi ~# cat > /etc/systemd/network/08-wlan0.network <<EOF
[Match]
Name=wlan0
[Network]
DHCP=yes
EOF

Because we don't have a bridge, so we need two different subnets. Be aware that the static ip address for the access point wlan1 belongs to another subnet than that from wlan0.

rpi ~# cat > /etc/systemd/network/12-wlan1.network <<EOF
[Match]
Name=wlan1
[Network]
Address=192.168.4.1/24
IPForward=yes
DHCPServer=yes
[DHCPServer]
DNS=84.200.69.80 84.200.70.40
EOF

Reboot.
That's it.


references:
[1] Howto migrate from networking to systemd-networkd with dynamic failover

Ingo
  • 40,606
  • 15
  • 76
  • 189
  • First of all, thanks a lot for the comment, it helps a lot! I really don't understand why all tutorials keep talking about /etc/interfaces etc... This method is way easier in my opinion. Anyway I wasn't aware I didn't need a dongle to do what I wanted so I followed the instructions and everything seem to be set properly, I have my wlan0 connect to my network properly and can access internet, then I have an ap0 interface which also get the correct static ip address however I can never see the AP from my mobile phone. – Leo Sep 03 '18 at 16:22
  • 1
    After looking around I finally found the problem... I had setup a passphrase of only 4 characters which is not enough so hostapd refused to start. I wish this problem would be easier to find! – Leo Sep 03 '18 at 16:48
  • @Leo Thanks for the hint. I will update that answer to share it with the community. – Ingo Sep 03 '18 at 18:31
  • No problem, you were really of a tremendous help thanks a lot! Now it works, I have one issue left now is that somehow I cannot reach my postgresql from the ap interface but I can from the wlan0 which is weird... – Leo Sep 03 '18 at 19:06
  • @Leo we are talking about [Raspberry Pi Zero W as a WiFi repeater](https://raspberrypi.stackexchange.com/a/87506/79866) which is not the answer above. Please make a new question just as short as your comment with some small additional pointers. – Ingo 4 mins ago – Ingo Sep 03 '18 at 19:37
  • I followed these instructions and now my raspberry pi is able to connect to the internet, and it also broadcasts an AP that other devices are able to connect to. However, the devices connected to the rpi AP are not connected to the internet. What could I be missing/where should I look to debug this? – spinningarrow Feb 02 '19 at 14:09
  • @spinningarrow Maybe your stations does not get ip addresses for the DNS server. I have updated the answer for this. Look at `/etc/systemd/network/12-wlan1.network`. Please from the RasPi first check connection to the internet without DNS name resolution with `ping -I wlan0 8.8.8.8`. If you get replies then NAT is working. Then `ping -I wlan1 8.8.8.8`. If you get replies then routing (ip forwarding) is working. Now try `ping google.com` from the RasPi and from a station. To be on the save side first reboot the RasPi, then the station. – Ingo Feb 02 '19 at 19:03
  • @Ingo `ping -I wlan0 8.8.8.8` works but `ping -I wlan1 8.8.8.8` times out. I guess that means ip forwarding is not working, hmm. – spinningarrow Feb 02 '19 at 22:12
  • Found the problem… it was a typo (surprise!). I had `ExecStartPost` instead of `ExecStopPost` which was probably disabling the forwarding immediately after enabling it. EDIT: FWIW, `ping -I wlan1 8.8.8.8` still times out; no idea why. – spinningarrow Feb 02 '19 at 22:53
  • I'm just looking for it. I guess it may be the default route (`ip route`). – Ingo Feb 02 '19 at 23:00
  • 1
    Just checked: `ping -I wlan1 8.8.8.8` doesn't work because it tries to get through the AP (wlan1) to the internet. My assumption with ip forwarding was wrong. I do `ping 8.8.8.8` from my mobile phone (associated to the AP) and I get replies. Also browsing the internet works. Btw. it is a good idea to install helper `sudo apt install rng-tools` to get random numbers quicker for encryption. – Ingo Feb 02 '19 at 23:25
  • @spinningarrow Forgot to address you. Have checked it, see my previous comment. Please tell me if it's working. – Ingo Feb 03 '19 at 01:44
  • @Ingo I haven't tried the rng-tools yet, but yes it works now! Thank you! – spinningarrow Feb 04 '19 at 09:51
  • @spinningarrow Glad that's working now :-) If does my answer helped you it would be nice if you could upvote it? – Ingo Feb 04 '19 at 10:23