0

I need to set up my Raspberry Pi to have a WiFi client connection to a wireless network as well as act as its own DHCP WiFi hotspot that can route traffic to either the Ethernet or over the WiFi client connection if present.

There will be a locally hosted web server on the Pi that will allow a user to change the WiFi client settings (SSID). It is intended that these settings will be changed while the user is connected via the Ethernet or WiFi hotspot connection with resulting feedback shown as to whether or not a WiFi client connection was established or failed. Therefore, after the modified WiFi client details are written to a wpa_supplicant file, I cannot restart any services that will cause an interruption to either the Ethernet or hotspot connection.

I previously had an implementation that worked by using a combination of dhcpcd, hostapd, and dnsmasq. This setup allowed for web access on all connection methods but upon restarting the dhcpcd service (so that the changed wlan0 wpa_supplicant file would take effect) the hotspot would restart kicking a user off that connection making visibility to the connection status feedback require a reconnection (not desired).

I then started a fresh implementation using this tutorial that utilizes systemd-networkd: https://hackaday.io/project/162164/instructions?page=1

I was able to establish an Ethernet (eth0) and WiFi client (wlan0) connection to my local network. I then added a virtual interface (ap0) and set it up to act as a DHCP server with a defined IP range.

/etc/systemd/network/wlan0.network:
[Match]
Name=wlan0

[Network]
DHCP=ipv4
IPForward=yes
/etc/systemd/network/ap0.network:
[Match]
Name=ap0

[Network]
Address=192.168.11.254/28
DHCPServer=yes
IPForward=yes
/etc/hostapd/hostapd-ap0.conf:
interface=ap0
country_code=US
hw_mode=g

ssid=raspberrypi
channel=6

auth_algs=1
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wmm_enabled=0

This starts up and broadcasts the SSID "raspberrypi" but I noticed a couple of issues right away. When connecting another device to the Pi's WiFi hotspot the device does not have access to the internet. I believe this is due to not having routing set up correctly between ap0 and wlan0. I'm unsure exactly how to go about fixing that as it seems there are a lot of different methods mentioned in forums.

The bigger issue arises when I restart the wlan0 service after updating the connection settings. My client device connected to the WiFi hotspot connection (ap0) loses connection to the hotspot meaning the wlan0 and ap0 interface are still coupled and both restarting.

I believe this makes sense as the ap0 virtual interface (I think) would be a child of the main wlan0 service. I think to get around this I would need another virtual interface (wlan0_client) that would connect to my local network with WiFi. Then I could restart that process without interrupting the ap0 interface. Similar to what is mentioned here but this is not using systemd-networkd: https://wiki.archlinux.org/title/software_access_point

"If you want to use the capability/feature, perhaps because an Ethernet connection is not available, you need to create two separate virtual interfaces for using it. Virtual interfaces for a physical device wlan0 can be created as follows"

I have found bits and pieces suggesting different methods but I have not found anything that seems to be exactly what I am looking for that uses systemd-networkd.

Summary of questions:

  1. Routing traffic from the ap0 virtual interface to wlan0 and eth0
  2. Decoupling of wlan0 and ap0 services so restart does not affect the other

Any detailed assistance would be appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.