[PATCH v2] nl80211: wait on udev when creating new device
Jouni Malinen
j at w1.fi
Tue Sep 30 01:36:32 PDT 2025
On Thu, Jun 05, 2025 at 09:22:11AM +0200, Benjamin Berg wrote:
> udev/systemd will process new network device. This can result in various
> issues as for example the MAC address may be randomized. Add the
> appropriate integration to wait for the udev "add" event before
> continuing to use the device.
>
> This resolves race conditions when reading the MAC address during
> interface creation (or even changing it right afterwards when creating a
> P2P device).
>
> Enable this feature by default. Systems that do not use udev need to
> explicitly disable it at compile time.
This feels quite painful.. For example, this broke the build for me due
to no libudev.h being installed on the system. Why would this need to be
hardcoded to be enabled for all builds that include driver_nl80211.c?
> See https://github.com/systemd/systemd/issues/13642
How easy it is to hit this issue in practice? That case mentions hwsim..
Does this happen easily outside testing environment?
> diff --git a/src/drivers/driver_nl80211.c b/src/drivers/driver_nl80211.c
> @@ -6239,6 +6242,43 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
> +#ifndef CONFIG_DRIVER_NL80211_DISABLE_UDEV
> + /*
> + * systemd/udev insist on processing new interfaces and may
> + * randomize the MAC address. We need to avoid race conditions between
> + * hostap reading the MAC address and systemd/udev changing it.
> + * Setup a monitor and wait for an event for a "wlan" "net" device
> + * with the expected IFINDEX.
> + * We are guaranteed to receive an event because we install the monitor
> + * before creating it.
> + */
This feels like something that should be done only if it can be
determined that this issue is present in the system and in particular,
not repeat this for every created interface.
> + udev = udev_new();
> + if (udev) {
> + monitor = udev_monitor_new_from_netlink(udev, "udev");
> + if (!monitor)
> + wpa_printf(MSG_ERROR, "nl80211: Failed to create udev monitor");
> + } else {
> + wpa_printf(MSG_ERROR, "nl80211: Failed to connect to udev");
> + }
Are those really errors on systems that do not use udev?
> @@ -6282,6 +6322,46 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
> +#ifndef CONFIG_DRIVER_NL80211_DISABLE_UDEV
> + if (monitor) {
> + /* Set blocking mode on the FD */
> + int fd = udev_monitor_get_fd(monitor);
> + int flags = fcntl(fd, F_GETFL);
> +
> + fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
> +
> + while (1) {
This is something I would rather not see unless it can be shown that the
issue running this is indeed going to have this issue.
--
Jouni Malinen PGP id EFC895FA
More information about the Hostap
mailing list