Hostp2pd
Wi-Fi Direct Session Manager, implementing a host AP daemon in Wi-Fi Direct mode, including P2P WPS enrolment
Install / Use
/learn @Ircama/Hostp2pdREADME
hostp2pd
The Wi-Fi Direct Session Manager
hostp2pd implements a soft host Access Point (AP) software in Wi-Fi Direct mode, enabling a wireless network interface card to act as Ad hoc access point and Wi-Fi Protected Setup (WPS) authentication server. It features basic functionalities roughly similar to hostapd (with its hostapd.conf configuration file), which is the common AP software integrated with wpa_supplicant, generally used for infrastructure mode networking. When implementing a P2P persistent group, wpa_supplicant offers the P2P-GO features enabled by hostp2pd to connect P2P Clients like Android smartphones, as well as provide the standard infrastructure AP mode to the same P2P-GO group, without the need of hostapd.
In order to accept Wi-Fi Direct connections from P2P Clients, to activate a local P2P-GO (Wi-Fi Direct Group Owner) and to perform WPS authentication, hostp2pd fully relies on wpa_supplicant, interfacing it through the wpa_cli command-line interface (CLI): wpa_cli is run in background and p2p commands are piped via pseudo-tty communication, while events returned by wpa_cli are read and processed.
hostp2pd includes a command-line interface mode for monitoring and controlling; it can be executed as a batch or as a daemon and provides an API for integration into other Python programs.
Connecting via Wi-Fi Direct with Android devices
Wi-Fi Direct (formerly named Wi-Fi Peer-to-Peer, or P2P) allows devices to connect directly to each other, without the need for a traditional Wireless Access Point (AP). The role of the access point is replaced by the so-called Group Owner (GO), either negotiated during the connection setup, or autonomously created.
An advantage of Wi-Fi Direct with Android is that it can coexist with a traditional Wi-Fi connection as well as with a cellular connection: it means that an Android smartphone can be connected to a mobile network, or to an infrastructure-mode Wi-Fi AP with internet access and at the same time connect to a UNIX device via Wi-Fi Direct, without losing the routing to the mobile (or AP) network. This is because with Android, differently from the standard infrastructure-mode Wi-Fi AP connection where an active Wi-Fi session always takes routing priority to the mobile network for its internal Android routing configuration that disables mobile routing, Wi-Fi Direct does not interfere with the routing table.
Apple iOS devices do not support Wi-Fi Direct, but can concurrently connect to a P2P persistent group in AP mode the same way as for traditional infrastructure-mode Access Points managed by hostapd. Differently from Android phones, if the persistent group does not configure a default router, iOS does not change the routing tables of the cellular network, which is by consequence not lost.
Installation
Check that the Python version is 3.6 or higher (python3 -V), then install hostp2pd with the following command:
python3 -m pip install hostp2pd
To install from GitHub:
sudo apt-get install git
python3 -m pip install git+https://github.com/Ircama/hostp2pd
To uninstall:
python3 -m pip uninstall -y hostp2pd
Prerequisite components (already included in the installation procedure): pyyaml, python-daemon.
Usage
To run hostp2pd in interactive mode, use the following command:
python3 -m hostp2pd
or simply:
hostp2pd
The above command uses the automatically detected P2P-Device interface and the internal default configuration file.
Using a P2P-Device interface and a configuration file:
hostp2pd -i p2p-dev-wlan0 -c /etc/hostp2pd.yaml
-ioption: The P2P-Device interface used by hostp2pd is created by wpa_supplicant over the physical wlan interface (if default options are used). Useiw devto list the available wlan interfaces. An unnamed/non-netdev interface with type P2P-device should be found. If no P2P-Device is shown (e.g., only the physical phy#0 Interface wlan0 is present), either wpa_supplicant is not active or it is not appropriately compiled/configured. With wlan0 as physical interface (ref.iw dev), to get the name of the P2P-Interface use the commandwpa_cli -i wlan0 interface: it should return the interface device wlan0 and the P2P-device (e.g., p2p-dev-wlan0). Use this name as argument to the-ioption of hostp2pd. Notice also that, if a P2P-Device is configured,wpa_cliwithout option should automatically point to this interface. If-ioption is not used, hostp2pd tries to automatically detect the right interface.-coption: a YAML configuration file (here an example) is not strictly necessary to start a first test; a minimum parameter would be the PIN, which can be alternatively defined using a shell Here Document expression:hostp2pd -i p2p-dev-wlan0 -c - <<\eof pin: "00000000" eof
In this documentation, the UNIX system is where hostp2pd is installed and run, generally acting as P2P-GO. The P2P device is generally an Android smartphone (or another Linux/Windows system).
To start a Wi-Fi Direct connection of an Android smartphone and connect a UNIX system running hostp2pd, tap Settings > Wi-Fi > Advanced settings > Wi-Fi Direct and wait for the peer UNIX device to appear. Select it, optionally type the PIN and wait for connection established. If the default configuration is used, which exploits a predefined persistent group, any subsequent reconnection to this group is done without repeating the WPS authorization process. As previously explained, through this process the mobile/cellular connection is not disabled while the Wi-Fi Direct connection is active.
Depending on the capabilities of the wlan device driver, the AP virtual interface has to be stopped before creating a P2P-GO group. As already mentioned, a persistent P2P-GO group can provide AP capabilities together with the Wi-Fi Direct functionalities.
Check the supported interface modes with this command:
iw list | grep "Supported interface modes" -A 8
It should return one line including P2P-GO (together with P2P-device). If only STA and managed are returned, the device driver of the board (or the hw itself) does not support creating a P2P-GO interface.
As an example, this is the output of the Raspberry Pi 4:
Supported interface modes:
* IBSS
* managed
* AP
* P2P-client
* P2P-GO
* P2P-device
Use this command to check the allowed combination options:
iw list | grep "valid interface combinations" -A 8
Every line contains alternative combinations. For instance, with the Broadcom BCM2711 SoC included in a Raspberry Pi 4 B, we get the following:
valid interface combinations:
* #{ managed } <= 1, #{ P2P-device } <= 1, #{ P2P-client, P2P-GO } <= 1,
total <= 3, #channels <= 2
* #{ managed } <= 1, #{ AP } <= 1, #{ P2P-client } <= 1, #{ P2P-device } <= 1,
total <= 4, #channels <= 1
Device supports scan flush.
Device supports randomizing MAC-addr in sched scans.
Supported extended features:
* [ 4WAY_HANDSHAKE_STA_PSK ]: 4-way handshake with PSK in station mode
It means that not more than one AP or P2P-GO interface can be configured at the same time, with a single P2P-GO group supported.
Same for the Intel Wireless-AC 9560 Ubuntu driver:
valid interface combinations:
* #{ managed } <= 1, #{ AP, P2P-client, P2P-GO } <= 1, #{ P2P-device } <= 1,
total <= 3, #channels <= 2
Optionally, hostp2pd allows the -p option, which defines an external program to be run with specific arguments each time preconfigured events occur, like activating or deactivating a group; this for instance allows controlling external AP resources before groups are created or after groups are removed.
Preconfigured events:
- "started": executed at hostp2pd startup
- "terminated": executed at hostp2pd termination
- "start_group": executed before creating a P2P GO group
- "stop_group": executed after removing a P2P GO group
- "connect": executed after a station connects to a group
- "disconnect": executed after a station disconnects from a group
Events might have additional arguments, which are used to add rela
