[PATCH v2 02/11] nl80211: Add a driver op to read all stations

Iegor Sergieienkov iegor at nova-labs.com
Mon Nov 23 04:50:22 PST 2026


Add an optional driver op, read_all_sta_data(), that fetches what
read_sta_data() returns for every station of a BSS with a single request
and passes each station to a callback function. A caller that polls the
statistics of all stations periodically would otherwise send one request
for each station.

The nl80211 implementation sends one NL80211_CMD_GET_STATION dump with
NL80211_ATTR_STA_DUMP_LINK_STATS. With that attribute, the kernel sends
an MLD station as one message with the MLD level statistics followed by
one message for each link, all with the MLD address. The handler thus
collects consecutive messages for one address and calls the callback
function when the address changes or the dump ends. If the dump fails,
the last station is not delivered since it may be incomplete.

A station on an AP_VLAN interface is not included in the dump of the AP
interface, so read_sta_data() is still needed for it.

Signed-off-by: Iegor Sergieienkov <iegor at nova-labs.com>
---
 src/ap/ap_drv_ops.h          | 12 ++++++
 src/drivers/driver.h         | 24 +++++++++++
 src/drivers/driver_nl80211.c | 78 ++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)

diff --git a/src/ap/ap_drv_ops.h b/src/ap/ap_drv_ops.h
index cd86fb762..3536f8a30 100644
--- a/src/ap/ap_drv_ops.h
+++ b/src/ap/ap_drv_ops.h
@@ -229,6 +229,18 @@ static inline int hostapd_drv_read_sta_data(
 	return hapd->driver->read_sta_data(hapd->drv_priv, data, addr);
 }
 
+static inline int hostapd_drv_read_all_sta_data(
+	struct hostapd_data *hapd,
+	void (*cb)(void *ctx, const u8 *addr,
+		   const struct hostap_sta_driver_data *data),
+	void *ctx)
+{
+	if (hapd->driver == NULL || hapd->drv_priv == NULL ||
+	    hapd->driver->read_all_sta_data == NULL)
+		return -1;
+	return hapd->driver->read_all_sta_data(hapd->drv_priv, cb, ctx);
+}
+
 static inline int hostapd_drv_sta_clear_stats(struct hostapd_data *hapd,
 					      const u8 *addr)
 {
diff --git a/src/drivers/driver.h b/src/drivers/driver.h
index cc9fa404c..7b63cdf01 100644
--- a/src/drivers/driver.h
+++ b/src/drivers/driver.h
@@ -4260,6 +4260,30 @@ struct wpa_driver_ops {
 	int (*read_sta_data)(void *priv, struct hostap_sta_driver_data *data,
 			     const u8 *addr);
 
+	/**
+	 * read_all_sta_data - Fetch the data of all stations
+	 * @priv: Private driver interface data
+	 * @cb: Callback function, called once for each station
+	 * @ctx: Context pointer for the callback function
+	 * Returns: 0 on success, negative value on failure
+	 *
+	 * This is an optional function to fetch with a single request what
+	 * read_sta_data() returns for each station of the BSS; for an AP MLD,
+	 * that is the stations of all its links. A station on an AP_VLAN
+	 * interface is not included; read_sta_data() needs to be used for it.
+	 * If the request fails, the callback function may already have been
+	 * called for some of the stations.
+	 *
+	 * The data is valid only for the duration of the callback function,
+	 * which may be called while the response from the driver is still being
+	 * received and thus must not call driver operations.
+	 */
+	int (*read_all_sta_data)(void *priv,
+				 void (*cb)(void *ctx, const u8 *addr,
+					    const struct hostap_sta_driver_data
+					    *data),
+				 void *ctx);
+
 	/**
 	 * tx_control_port - Send a frame over the 802.1X controlled port
 	 * @priv: Private driver interface data
diff --git a/src/drivers/driver_nl80211.c b/src/drivers/driver_nl80211.c
index e137a4ae1..542e0adfd 100644
--- a/src/drivers/driver_nl80211.c
+++ b/src/drivers/driver_nl80211.c
@@ -12139,6 +12139,83 @@ static int driver_nl80211_read_sta_data(void *priv,
 }
 
 
+struct nl80211_all_sta_data_req {
+	void (*cb)(void *ctx, const u8 *addr,
+		   const struct hostap_sta_driver_data *data);
+	void *ctx;
+	struct hostap_sta_driver_data data;
+	u8 addr[ETH_ALEN];
+	bool pending;
+};
+
+
+/*
+ * With NL80211_ATTR_STA_DUMP_LINK_STATS, the kernel follows the message of an
+ * MLD station with one message for each of its links, all for the same
+ * address. A station is thus complete only once the next one starts or the
+ * dump ends.
+ */
+static int get_all_sta_handler(struct nl_msg *msg, void *arg)
+{
+	struct nl80211_all_sta_data_req *req = arg;
+	struct get_sta_arg sta = { .addr = req->addr, .data = &req->data };
+	struct nlattr *tb[NL80211_ATTR_MAX + 1];
+	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+	const u8 *addr;
+
+	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
+		  genlmsg_attrlen(gnlh, 0), NULL);
+	if (!tb[NL80211_ATTR_MAC] ||
+	    nla_len(tb[NL80211_ATTR_MAC]) != ETH_ALEN)
+		return NL_SKIP;
+	addr = nla_data(tb[NL80211_ATTR_MAC]);
+
+	if (req->pending && !ether_addr_equal(addr, req->addr)) {
+		req->cb(req->ctx, req->addr, &req->data);
+		req->pending = false;
+	}
+
+	if (!req->pending) {
+		os_memset(&req->data, 0, sizeof(req->data));
+		os_memcpy(req->addr, addr, ETH_ALEN);
+		req->pending = true;
+	}
+
+	return get_sta_handler(msg, &sta);
+}
+
+
+static int driver_nl80211_read_all_sta_data(
+	void *priv,
+	void (*cb)(void *ctx, const u8 *addr,
+		   const struct hostap_sta_driver_data *data),
+	void *ctx)
+{
+	struct i802_bss *bss = priv;
+	struct nl80211_all_sta_data_req req;
+	struct nl_msg *msg;
+	int ret;
+
+	os_memset(&req, 0, sizeof(req));
+	req.cb = cb;
+	req.ctx = ctx;
+
+	if (!(msg = nl80211_bss_msg(bss, NLM_F_DUMP,
+				    NL80211_CMD_GET_STATION)) ||
+	    nla_put_flag(msg, NL80211_ATTR_STA_DUMP_LINK_STATS)) {
+		nlmsg_free(msg);
+		return -ENOBUFS;
+	}
+
+	ret = send_and_recv_resp(bss->drv, msg, get_all_sta_handler, &req);
+	/* The last station of a failed dump may be missing link messages */
+	if (ret == 0 && req.pending)
+		cb(ctx, req.addr, &req.data);
+
+	return ret;
+}
+
+
 static int driver_nl80211_send_action(void *priv, unsigned int freq,
 				      unsigned int wait_time,
 				      const u8 *dst, const u8 *src,
@@ -17029,6 +17106,7 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
 	.sta_deauth = i802_sta_deauth,
 	.sta_disassoc = i802_sta_disassoc,
 	.read_sta_data = driver_nl80211_read_sta_data,
+	.read_all_sta_data = driver_nl80211_read_all_sta_data,
 	.set_freq = i802_set_freq,
 	.send_action = driver_nl80211_send_action,
 	.send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
-- 
2.43.0




More information about the Hostap mailing list