blob: b77e21bd15e34234e040ee166bff2b71b7455d6b [file] [log] [blame]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001/*
2 * hostapd - WNM
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08003 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080012#include "utils/eloop.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070013#include "common/ieee802_11_defs.h"
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080014#include "common/wpa_ctrl.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080015#include "common/ocv.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070016#include "ap/hostapd.h"
17#include "ap/sta_info.h"
18#include "ap/ap_config.h"
19#include "ap/ap_drv_ops.h"
20#include "ap/wpa_auth.h"
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080021#include "mbo_ap.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070022#include "wnm_ap.h"
23
24#define MAX_TFS_IE_LEN 1024
25
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026
27/* get the TFS IE from driver */
28static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
29 u8 *buf, u16 *buf_len, enum wnm_oper oper)
30{
31 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
32
33 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
34}
35
36
37/* set the TFS IE to driver */
38static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
39 u8 *buf, u16 *buf_len, enum wnm_oper oper)
40{
41 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
42
43 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
44}
45
46
Sunil Ravib0ac25f2024-07-12 01:42:03 +000047static const u8 * wnm_ap_get_own_addr(struct hostapd_data *hapd,
48 struct sta_info *sta)
49{
50 const u8 *own_addr = hapd->own_addr;
51
52#ifdef CONFIG_IEEE80211BE
53 if (hapd->conf->mld_ap && (!sta || ap_sta_is_mld(hapd, sta)))
54 own_addr = hapd->mld_addr;
55#endif /* CONFIG_IEEE80211BE */
56
57 return own_addr;
58}
59
60
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070061/* MLME-SLEEPMODE.response */
62static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
63 const u8 *addr, u8 dialog_token,
64 u8 action_type, u16 intval)
65{
66 struct ieee80211_mgmt *mgmt;
67 int res;
68 size_t len;
69 size_t gtk_elem_len = 0;
70 size_t igtk_elem_len = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -070071 size_t bigtk_elem_len = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070072 struct wnm_sleep_element wnmsleep_ie;
Hai Shalom74f70d42019-02-11 14:42:39 -080073 u8 *wnmtfs_ie, *oci_ie;
74 u8 wnmsleep_ie_len, oci_ie_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070075 u16 wnmtfs_ie_len;
76 u8 *pos;
77 struct sta_info *sta;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080078 enum wnm_oper tfs_oper = action_type == WNM_SLEEP_MODE_ENTER ?
79 WNM_SLEEP_TFS_RESP_IE_ADD : WNM_SLEEP_TFS_RESP_IE_NONE;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000080 const u8 *own_addr;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070081
82 sta = ap_get_sta(hapd, addr);
83 if (sta == NULL) {
84 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
85 return -EINVAL;
86 }
87
88 /* WNM-Sleep Mode IE */
89 os_memset(&wnmsleep_ie, 0, sizeof(struct wnm_sleep_element));
90 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
91 wnmsleep_ie.eid = WLAN_EID_WNMSLEEP;
92 wnmsleep_ie.len = wnmsleep_ie_len - 2;
93 wnmsleep_ie.action_type = action_type;
94 wnmsleep_ie.status = WNM_STATUS_SLEEP_ACCEPT;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080095 wnmsleep_ie.intval = host_to_le16(intval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070096
97 /* TFS IE(s) */
98 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
99 if (wnmtfs_ie == NULL)
100 return -1;
101 if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
102 tfs_oper)) {
103 wnmtfs_ie_len = 0;
104 os_free(wnmtfs_ie);
105 wnmtfs_ie = NULL;
106 }
107
Hai Shalom74f70d42019-02-11 14:42:39 -0800108 oci_ie = NULL;
109 oci_ie_len = 0;
110#ifdef CONFIG_OCV
111 if (action_type == WNM_SLEEP_MODE_EXIT &&
112 wpa_auth_uses_ocv(sta->wpa_sm)) {
113 struct wpa_channel_info ci;
114
115 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
116 wpa_printf(MSG_WARNING,
117 "Failed to get channel info for OCI element in WNM-Sleep Mode frame");
118 os_free(wnmtfs_ie);
119 return -1;
120 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700121#ifdef CONFIG_TESTING_OPTIONS
122 if (hapd->conf->oci_freq_override_wnm_sleep) {
123 wpa_printf(MSG_INFO,
124 "TEST: Override OCI frequency %d -> %u MHz",
125 ci.frequency,
126 hapd->conf->oci_freq_override_wnm_sleep);
127 ci.frequency = hapd->conf->oci_freq_override_wnm_sleep;
128 }
129#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800130
131 oci_ie_len = OCV_OCI_EXTENDED_LEN;
132 oci_ie = os_zalloc(oci_ie_len);
133 if (!oci_ie) {
134 wpa_printf(MSG_WARNING,
135 "Failed to allocate buffer for OCI element in WNM-Sleep Mode frame");
136 os_free(wnmtfs_ie);
137 return -1;
138 }
139
140 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
141 os_free(wnmtfs_ie);
142 os_free(oci_ie);
143 return -1;
144 }
145 }
146#endif /* CONFIG_OCV */
147
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700148#define MAX_GTK_SUBELEM_LEN 45
149#define MAX_IGTK_SUBELEM_LEN 26
Hai Shalomfdcde762020-04-02 11:19:20 -0700150#define MAX_BIGTK_SUBELEM_LEN 26
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700151 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len +
Hai Shalom74f70d42019-02-11 14:42:39 -0800152 MAX_GTK_SUBELEM_LEN + MAX_IGTK_SUBELEM_LEN +
Hai Shalomfdcde762020-04-02 11:19:20 -0700153 MAX_BIGTK_SUBELEM_LEN +
Hai Shalom74f70d42019-02-11 14:42:39 -0800154 oci_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700155 if (mgmt == NULL) {
156 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
157 "WNM-Sleep Response action frame");
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800158 res = -1;
159 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700160 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000161
162 own_addr = wnm_ap_get_own_addr(hapd, sta);
163
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700164 os_memcpy(mgmt->da, addr, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000165 os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
166 os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700167 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
168 WLAN_FC_STYPE_ACTION);
169 mgmt->u.action.category = WLAN_ACTION_WNM;
170 mgmt->u.action.u.wnm_sleep_resp.action = WNM_SLEEP_MODE_RESP;
171 mgmt->u.action.u.wnm_sleep_resp.dialogtoken = dialog_token;
172 pos = (u8 *)mgmt->u.action.u.wnm_sleep_resp.variable;
173 /* add key data if MFP is enabled */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800174 if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
Roshan Pius3a1667e2018-07-03 15:17:14 -0700175 hapd->conf->wnm_sleep_mode_no_keys ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800176 action_type != WNM_SLEEP_MODE_EXIT) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700177 mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
178 } else {
179 gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
180 pos += gtk_elem_len;
181 wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
182 (int) gtk_elem_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700183 res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800184 if (res < 0)
185 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700186 igtk_elem_len = res;
187 pos += igtk_elem_len;
188 wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
189 (int) igtk_elem_len);
Hai Shalom60840252021-02-19 19:02:11 -0800190 if (hapd->conf->beacon_prot &&
191 (hapd->iface->drv_flags &
192 WPA_DRIVER_FLAGS_BEACON_PROTECTION)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700193 res = wpa_wnmsleep_bigtk_subelem(sta->wpa_sm, pos);
194 if (res < 0)
195 goto fail;
196 bigtk_elem_len = res;
197 pos += bigtk_elem_len;
198 wpa_printf(MSG_DEBUG, "Pass 4 bigtk_len = %d",
199 (int) bigtk_elem_len);
200 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700201
202 WPA_PUT_LE16((u8 *)
203 &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
Hai Shalomfdcde762020-04-02 11:19:20 -0700204 gtk_elem_len + igtk_elem_len + bigtk_elem_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700205 }
206 os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
207 /* copy TFS IE here */
208 pos += wnmsleep_ie_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800209 if (wnmtfs_ie) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800210 os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
Hai Shalom74f70d42019-02-11 14:42:39 -0800211 pos += wnmtfs_ie_len;
212 }
213#ifdef CONFIG_OCV
214 /* copy OCV OCI here */
215 if (oci_ie_len > 0)
216 os_memcpy(pos, oci_ie, oci_ie_len);
217#endif /* CONFIG_OCV */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700218
219 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
Hai Shalomfdcde762020-04-02 11:19:20 -0700220 igtk_elem_len + bigtk_elem_len +
221 wnmsleep_ie_len + wnmtfs_ie_len + oci_ie_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700222
223 /* In driver, response frame should be forced to sent when STA is in
224 * PS mode */
225 res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
226 mgmt->da, &mgmt->u.action.category, len);
227
228 if (!res) {
229 wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
230 "frame");
231
232 /* when entering wnmsleep
233 * 1. pause the node in driver
Hai Shalomfdcde762020-04-02 11:19:20 -0700234 * 2. mark the node so that AP won't update GTK/IGTK/BIGTK
235 * during WNM Sleep
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700236 */
237 if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800238 wnmsleep_ie.action_type == WNM_SLEEP_MODE_ENTER) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800239 sta->flags |= WLAN_STA_WNM_SLEEP_MODE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700240 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
241 addr, NULL, NULL);
242 wpa_set_wnmsleep(sta->wpa_sm, 1);
243 }
244 /* when exiting wnmsleep
245 * 1. unmark the node
Hai Shalomfdcde762020-04-02 11:19:20 -0700246 * 2. start GTK/IGTK/BIGTK update if MFP is not used
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700247 * 3. unpause the node in driver
248 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800249 if ((wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT ||
250 wnmsleep_ie.status ==
251 WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) &&
252 wnmsleep_ie.action_type == WNM_SLEEP_MODE_EXIT) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800253 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700254 wpa_set_wnmsleep(sta->wpa_sm, 0);
255 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
256 addr, NULL, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700257 if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
258 hapd->conf->wnm_sleep_mode_no_keys)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700259 wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
260 }
261 } else
262 wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
263
264#undef MAX_GTK_SUBELEM_LEN
265#undef MAX_IGTK_SUBELEM_LEN
Hai Shalomfdcde762020-04-02 11:19:20 -0700266#undef MAX_BIGTK_SUBELEM_LEN
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800267fail:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700268 os_free(wnmtfs_ie);
Hai Shalom74f70d42019-02-11 14:42:39 -0800269 os_free(oci_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700270 os_free(mgmt);
271 return res;
272}
273
274
275static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
276 const u8 *addr, const u8 *frm, int len)
277{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800278 /* Dialog Token [1] | WNM-Sleep Mode IE | TFS Response IE */
279 const u8 *pos = frm;
280 u8 dialog_token;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700281 struct wnm_sleep_element *wnmsleep_ie = NULL;
282 /* multiple TFS Req IE (assuming consecutive) */
283 u8 *tfsreq_ie_start = NULL;
284 u8 *tfsreq_ie_end = NULL;
285 u16 tfsreq_ie_len = 0;
Hai Shalom74f70d42019-02-11 14:42:39 -0800286#ifdef CONFIG_OCV
287 struct sta_info *sta;
288 const u8 *oci_ie = NULL;
289 u8 oci_ie_len = 0;
290#endif /* CONFIG_OCV */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700291
Roshan Pius3a1667e2018-07-03 15:17:14 -0700292 if (!hapd->conf->wnm_sleep_mode) {
293 wpa_printf(MSG_DEBUG, "Ignore WNM-Sleep Mode Request from "
294 MACSTR " since WNM-Sleep Mode is disabled",
295 MAC2STR(addr));
296 return;
297 }
298
Jouni Malinen83e54022018-10-29 20:48:07 +0200299 if (len < 1) {
300 wpa_printf(MSG_DEBUG,
301 "WNM: Ignore too short WNM-Sleep Mode Request from "
302 MACSTR, MAC2STR(addr));
303 return;
304 }
305
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800306 dialog_token = *pos++;
307 while (pos + 1 < frm + len) {
308 u8 ie_len = pos[1];
309 if (pos + 2 + ie_len > frm + len)
310 break;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800311 if (*pos == WLAN_EID_WNMSLEEP &&
312 ie_len >= (int) sizeof(*wnmsleep_ie) - 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800313 wnmsleep_ie = (struct wnm_sleep_element *) pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700314 else if (*pos == WLAN_EID_TFS_REQ) {
315 if (!tfsreq_ie_start)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800316 tfsreq_ie_start = (u8 *) pos;
317 tfsreq_ie_end = (u8 *) pos;
Hai Shalom74f70d42019-02-11 14:42:39 -0800318#ifdef CONFIG_OCV
319 } else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
320 pos[2] == WLAN_EID_EXT_OCV_OCI) {
321 oci_ie = pos + 3;
322 oci_ie_len = ie_len - 1;
323#endif /* CONFIG_OCV */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700324 } else
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800325 wpa_printf(MSG_DEBUG, "WNM: EID %d not recognized",
326 *pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700327 pos += ie_len + 2;
328 }
329
330 if (!wnmsleep_ie) {
331 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
332 return;
333 }
334
Hai Shalom74f70d42019-02-11 14:42:39 -0800335#ifdef CONFIG_OCV
336 sta = ap_get_sta(hapd, addr);
337 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
338 sta && wpa_auth_uses_ocv(sta->wpa_sm)) {
339 struct wpa_channel_info ci;
340
341 if (hostapd_drv_channel_info(hapd, &ci) != 0) {
342 wpa_printf(MSG_WARNING,
343 "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
344 return;
345 }
346
347 if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
348 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -0700349 ci.seg1_idx) != OCI_SUCCESS) {
350 wpa_msg(hapd, MSG_WARNING, "WNM: OCV failed: %s",
351 ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -0800352 return;
353 }
354 }
355#endif /* CONFIG_OCV */
356
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800357 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER &&
358 tfsreq_ie_start && tfsreq_ie_end &&
359 tfsreq_ie_end - tfsreq_ie_start >= 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
361 tfsreq_ie_start;
362 wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
363 /* pass the TFS Req IE(s) to driver for processing */
364 if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
365 &tfsreq_ie_len,
366 WNM_SLEEP_TFS_REQ_IE_SET))
367 wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
368 }
369
370 ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
371 wnmsleep_ie->action_type,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 le_to_host16(wnmsleep_ie->intval));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700373
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800374 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700375 /* clear the tfs after sending the resp frame */
376 ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
377 &tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
378 }
379}
380
381
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800382static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
383 const u8 *addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800384 u8 dialog_token)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700385{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800386 struct ieee80211_mgmt *mgmt;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000387 const u8 *own_addr;
388 struct sta_info *sta;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800389 size_t len;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 u8 *pos;
391 int res;
392
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800393 mgmt = os_zalloc(sizeof(*mgmt));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800394 if (mgmt == NULL)
395 return -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000396
397 sta = ap_get_sta(hapd, addr);
398 own_addr = wnm_ap_get_own_addr(hapd, sta);
399
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800400 os_memcpy(mgmt->da, addr, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000401 os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
402 os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800403 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
404 WLAN_FC_STYPE_ACTION);
405 mgmt->u.action.category = WLAN_ACTION_WNM;
406 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
407 mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
408 mgmt->u.action.u.bss_tm_req.req_mode = 0;
409 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
410 mgmt->u.action.u.bss_tm_req.validity_interval = 1;
411 pos = mgmt->u.action.u.bss_tm_req.variable;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800412
413 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
414 MACSTR " dialog_token=%u req_mode=0x%x disassoc_timer=%u "
415 "validity_interval=%u",
416 MAC2STR(addr), dialog_token,
417 mgmt->u.action.u.bss_tm_req.req_mode,
418 le_to_host16(mgmt->u.action.u.bss_tm_req.disassoc_timer),
419 mgmt->u.action.u.bss_tm_req.validity_interval);
420
421 len = pos - &mgmt->u.action.category;
422 res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
423 mgmt->da, &mgmt->u.action.category, len);
424 os_free(mgmt);
425 return res;
426}
427
428
429static void ieee802_11_rx_bss_trans_mgmt_query(struct hostapd_data *hapd,
430 const u8 *addr, const u8 *frm,
431 size_t len)
432{
433 u8 dialog_token, reason;
434 const u8 *pos, *end;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700435 int enabled = hapd->conf->bss_transition;
Sunil Ravia04bd252022-05-02 22:54:18 -0700436 char *hex = NULL;
437 size_t hex_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700438
439#ifdef CONFIG_MBO
440 if (hapd->conf->mbo_enabled)
441 enabled = 1;
442#endif /* CONFIG_MBO */
443 if (!enabled) {
444 wpa_printf(MSG_DEBUG,
445 "Ignore BSS Transition Management Query from "
446 MACSTR
447 " since BSS Transition Management is disabled",
448 MAC2STR(addr));
449 return;
450 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800451
452 if (len < 2) {
453 wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Query from "
454 MACSTR, MAC2STR(addr));
455 return;
456 }
457
458 pos = frm;
459 end = pos + len;
460 dialog_token = *pos++;
461 reason = *pos++;
462
463 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Query from "
464 MACSTR " dialog_token=%u reason=%u",
465 MAC2STR(addr), dialog_token, reason);
466
467 wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
468 pos, end - pos);
469
Sunil Ravia04bd252022-05-02 22:54:18 -0700470 hex_len = 2 * (end - pos) + 1;
471 if (hex_len > 1) {
472 hex = os_malloc(hex_len);
473 if (hex)
474 wpa_snprintf_hex(hex, hex_len, pos, end - pos);
475 }
476 wpa_msg(hapd->msg_ctx, MSG_INFO,
477 BSS_TM_QUERY MACSTR " reason=%u%s%s",
478 MAC2STR(addr), reason, hex ? " neighbor=" : "", hex);
479 os_free(hex);
480
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800481 ieee802_11_send_bss_trans_mgmt_request(hapd, addr, dialog_token);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800482}
483
484
Roshan Pius3a1667e2018-07-03 15:17:14 -0700485void ap_sta_reset_steer_flag_timer(void *eloop_ctx, void *timeout_ctx)
486{
487 struct hostapd_data *hapd = eloop_ctx;
488 struct sta_info *sta = timeout_ctx;
489
490 if (sta->agreed_to_steer) {
491 wpa_printf(MSG_DEBUG, "%s: Reset steering flag for STA " MACSTR,
492 hapd->conf->iface, MAC2STR(sta->addr));
493 sta->agreed_to_steer = 0;
494 }
495}
496
497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800498static void ieee802_11_rx_bss_trans_mgmt_resp(struct hostapd_data *hapd,
499 const u8 *addr, const u8 *frm,
500 size_t len)
501{
502 u8 dialog_token, status_code, bss_termination_delay;
503 const u8 *pos, *end;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700504 int enabled = hapd->conf->bss_transition;
505 struct sta_info *sta;
506
507#ifdef CONFIG_MBO
508 if (hapd->conf->mbo_enabled)
509 enabled = 1;
510#endif /* CONFIG_MBO */
511 if (!enabled) {
512 wpa_printf(MSG_DEBUG,
513 "Ignore BSS Transition Management Response from "
514 MACSTR
515 " since BSS Transition Management is disabled",
516 MAC2STR(addr));
517 return;
518 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800519
520 if (len < 3) {
521 wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Response from "
522 MACSTR, MAC2STR(addr));
523 return;
524 }
525
526 pos = frm;
527 end = pos + len;
528 dialog_token = *pos++;
529 status_code = *pos++;
530 bss_termination_delay = *pos++;
531
532 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Response from "
533 MACSTR " dialog_token=%u status_code=%u "
534 "bss_termination_delay=%u", MAC2STR(addr), dialog_token,
535 status_code, bss_termination_delay);
536
Roshan Pius3a1667e2018-07-03 15:17:14 -0700537 sta = ap_get_sta(hapd, addr);
538 if (!sta) {
539 wpa_printf(MSG_DEBUG, "Station " MACSTR
540 " not found for received BSS TM Response",
541 MAC2STR(addr));
542 return;
543 }
544
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800545 if (status_code == WNM_BSS_TM_ACCEPT) {
546 if (end - pos < ETH_ALEN) {
547 wpa_printf(MSG_DEBUG, "WNM: not enough room for Target BSSID field");
548 return;
549 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700550 sta->agreed_to_steer = 1;
551 eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
552 eloop_register_timeout(2, 0, ap_sta_reset_steer_flag_timer,
553 hapd, sta);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800554 wpa_printf(MSG_DEBUG, "WNM: Target BSSID: " MACSTR,
555 MAC2STR(pos));
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800556 wpa_msg(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
557 " status_code=%u bss_termination_delay=%u target_bssid="
558 MACSTR,
559 MAC2STR(addr), status_code, bss_termination_delay,
560 MAC2STR(pos));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800561 pos += ETH_ALEN;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800562 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -0700563 sta->agreed_to_steer = 0;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800564 wpa_msg(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
565 " status_code=%u bss_termination_delay=%u",
566 MAC2STR(addr), status_code, bss_termination_delay);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800567 }
568
569 wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
570 pos, end - pos);
571}
572
573
Hai Shalomfdcde762020-04-02 11:19:20 -0700574static void wnm_beacon_protection_failure(struct hostapd_data *hapd,
575 const u8 *addr)
576{
577 struct sta_info *sta;
578
Hai Shalom60840252021-02-19 19:02:11 -0800579 if (!hapd->conf->beacon_prot ||
580 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION))
Hai Shalomfdcde762020-04-02 11:19:20 -0700581 return;
582
583 sta = ap_get_sta(hapd, addr);
584 if (!sta || !(sta->flags & WLAN_STA_AUTHORIZED)) {
585 wpa_printf(MSG_DEBUG, "Station " MACSTR
586 " not found for received WNM-Notification Request",
587 MAC2STR(addr));
588 return;
589 }
590
591 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
592 HOSTAPD_LEVEL_INFO,
593 "Beacon protection failure reported");
594 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_UNPROT_BEACON "reporter="
595 MACSTR, MAC2STR(addr));
596}
597
598
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800599static void ieee802_11_rx_wnm_notification_req(struct hostapd_data *hapd,
600 const u8 *addr, const u8 *buf,
601 size_t len)
602{
603 u8 dialog_token, type;
604
605 if (len < 2)
606 return;
607 dialog_token = *buf++;
608 type = *buf++;
609 len -= 2;
610
611 wpa_printf(MSG_DEBUG,
612 "WNM: Received WNM Notification Request frame from "
613 MACSTR " (dialog_token=%u type=%u)",
614 MAC2STR(addr), dialog_token, type);
615 wpa_hexdump(MSG_MSGDUMP, "WNM: Notification Request subelements",
616 buf, len);
Hai Shalomfdcde762020-04-02 11:19:20 -0700617 switch (type) {
618 case WNM_NOTIF_TYPE_BEACON_PROTECTION_FAILURE:
619 wnm_beacon_protection_failure(hapd, addr);
620 break;
621 case WNM_NOTIF_TYPE_VENDOR_SPECIFIC:
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800622 mbo_ap_wnm_notification_req(hapd, addr, buf, len);
Hai Shalomfdcde762020-04-02 11:19:20 -0700623 break;
624 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800625}
626
627
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800628static void ieee802_11_rx_wnm_coloc_intf_report(struct hostapd_data *hapd,
629 const u8 *addr, const u8 *buf,
630 size_t len)
631{
632 u8 dialog_token;
633 char *hex;
634 size_t hex_len;
635
636 if (!hapd->conf->coloc_intf_reporting) {
637 wpa_printf(MSG_DEBUG,
638 "WNM: Ignore unexpected Collocated Interference Report from "
639 MACSTR, MAC2STR(addr));
640 return;
641 }
642
643 if (len < 1) {
644 wpa_printf(MSG_DEBUG,
645 "WNM: Ignore too short Collocated Interference Report from "
646 MACSTR, MAC2STR(addr));
647 return;
648 }
649 dialog_token = *buf++;
650 len--;
651
652 wpa_printf(MSG_DEBUG,
653 "WNM: Received Collocated Interference Report frame from "
654 MACSTR " (dialog_token=%u)",
655 MAC2STR(addr), dialog_token);
656 wpa_hexdump(MSG_MSGDUMP, "WNM: Collocated Interference Report Elements",
657 buf, len);
658
659 hex_len = 2 * len + 1;
660 hex = os_malloc(hex_len);
661 if (!hex)
662 return;
663 wpa_snprintf_hex(hex, hex_len, buf, len);
664 wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, COLOC_INTF_REPORT MACSTR " %d %s",
665 MAC2STR(addr), dialog_token, hex);
666 os_free(hex);
667}
668
669
Sunil Raviaf8751c2023-03-29 11:35:17 -0700670
671static const char * wnm_event_type2str(enum wnm_event_report_type wtype)
672{
673#define W2S(wtype) case WNM_EVENT_TYPE_ ## wtype: return #wtype;
674 switch (wtype) {
675 W2S(TRANSITION)
676 W2S(RSNA)
677 W2S(P2P_LINK)
678 W2S(WNM_LOG)
679 W2S(BSS_COLOR_COLLISION)
680 W2S(BSS_COLOR_IN_USE)
681 }
682 return "UNKNOWN";
683#undef W2S
684}
685
686
687static void ieee802_11_rx_wnm_event_report(struct hostapd_data *hapd,
688 const u8 *addr, const u8 *buf,
689 size_t len)
690{
691 struct sta_info *sta;
692 u8 dialog_token;
693 struct wnm_event_report_element *report_ie;
694 const u8 *pos = buf, *end = buf + len;
695 const size_t fixed_field_len = 3; /* Event Token/Type/Report Status */
696#ifdef CONFIG_IEEE80211AX
697 const size_t tsf_len = 8;
698 u8 color;
699 u64 bitmap;
700#endif /* CONFIG_IEEE80211AX */
701
702 if (end - pos < 1 + 2) {
703 wpa_printf(MSG_DEBUG,
704 "WNM: Ignore too short WNM Event Report frame from "
705 MACSTR, MAC2STR(addr));
706 return;
707 }
708
709 dialog_token = *pos++;
710 report_ie = (struct wnm_event_report_element *) pos;
711
712 if (end - pos < 2 + report_ie->len ||
713 report_ie->len < fixed_field_len) {
714 wpa_printf(MSG_DEBUG,
715 "WNM: Ignore truncated WNM Event Report frame from "
716 MACSTR, MAC2STR(addr));
717 return;
718 }
719
720 if (report_ie->eid != WLAN_EID_EVENT_REPORT ||
721 report_ie->status != WNM_STATUS_SUCCESSFUL)
722 return;
723
724 wpa_printf(MSG_DEBUG, "WNM: Received WNM Event Report frame from "
725 MACSTR " dialog_token=%u event_token=%u type=%d (%s)",
726 MAC2STR(addr), dialog_token, report_ie->token,
727 report_ie->type, wnm_event_type2str(report_ie->type));
728
729 pos += 2 + fixed_field_len;
730 wpa_hexdump(MSG_MSGDUMP, "WNM: Event Report", pos, end - pos);
731
732 sta = ap_get_sta(hapd, addr);
733 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
734 wpa_printf(MSG_DEBUG, "Station " MACSTR
735 " not found for received WNM Event Report",
736 MAC2STR(addr));
737 return;
738 }
739
740 switch (report_ie->type) {
741#ifdef CONFIG_IEEE80211AX
742 case WNM_EVENT_TYPE_BSS_COLOR_COLLISION:
743 if (!hapd->iconf->ieee80211ax || hapd->conf->disable_11ax)
744 return;
745 if (report_ie->len <
746 fixed_field_len + tsf_len + 8) {
747 wpa_printf(MSG_DEBUG,
748 "WNM: Too short BSS color collision event report from "
749 MACSTR, MAC2STR(addr));
750 return;
751 }
752 bitmap = WPA_GET_LE64(report_ie->u.bss_color_collision.color_bitmap);
753 wpa_printf(MSG_DEBUG,
754 "WNM: BSS color collision bitmap 0x%llx reported by "
755 MACSTR, (unsigned long long) bitmap, MAC2STR(addr));
756 hostapd_switch_color(hapd->iface->bss[0], bitmap);
757 break;
758 case WNM_EVENT_TYPE_BSS_COLOR_IN_USE:
759 if (!hapd->iconf->ieee80211ax || hapd->conf->disable_11ax)
760 return;
761 if (report_ie->len < fixed_field_len + tsf_len + 1) {
762 wpa_printf(MSG_DEBUG,
763 "WNM: Too short BSS color in use event report from "
764 MACSTR, MAC2STR(addr));
765 return;
766 }
767 color = report_ie->u.bss_color_in_use.color;
768 if (color > 63) {
769 wpa_printf(MSG_DEBUG,
770 "WNM: Invalid BSS color %u report from "
771 MACSTR, color, MAC2STR(addr));
772 return;
773 }
774 if (color == 0) {
775 wpa_printf(MSG_DEBUG,
776 "WNM: BSS color use report canceled by "
777 MACSTR, MAC2STR(addr));
778 /* TODO: Clear stored color from the collision bitmap
779 * if there are no other users for it. */
780 return;
781 }
782 wpa_printf(MSG_DEBUG, "WNM: BSS color %u use report by "
783 MACSTR, color, MAC2STR(addr));
784 hapd->color_collision_bitmap |= 1ULL << color;
785 break;
786#endif /* CONFIG_IEEE80211AX */
787 default:
788 wpa_printf(MSG_DEBUG,
789 "WNM Event Report type=%d (%s) not supported",
790 report_ie->type,
791 wnm_event_type2str(report_ie->type));
792 break;
793 }
794}
795
796
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800797int ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
798 const struct ieee80211_mgmt *mgmt, size_t len)
799{
800 u8 action;
801 const u8 *payload;
802 size_t plen;
803
804 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800805 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700806
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700807 payload = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800808 action = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700809 plen = len - IEEE80211_HDRLEN - 2;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800810
811 switch (action) {
Sunil Raviaf8751c2023-03-29 11:35:17 -0700812 case WNM_EVENT_REPORT:
813 ieee802_11_rx_wnm_event_report(hapd, mgmt->sa, payload,
814 plen);
815 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800816 case WNM_BSS_TRANS_MGMT_QUERY:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800817 ieee802_11_rx_bss_trans_mgmt_query(hapd, mgmt->sa, payload,
818 plen);
819 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800820 case WNM_BSS_TRANS_MGMT_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800821 ieee802_11_rx_bss_trans_mgmt_resp(hapd, mgmt->sa, payload,
822 plen);
823 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700824 case WNM_SLEEP_MODE_REQ:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800825 ieee802_11_rx_wnmsleep_req(hapd, mgmt->sa, payload, plen);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800826 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800827 case WNM_NOTIFICATION_REQ:
828 ieee802_11_rx_wnm_notification_req(hapd, mgmt->sa, payload,
829 plen);
830 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800831 case WNM_COLLOCATED_INTERFERENCE_REPORT:
832 ieee802_11_rx_wnm_coloc_intf_report(hapd, mgmt->sa, payload,
833 plen);
834 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700835 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700836
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800837 wpa_printf(MSG_DEBUG, "WNM: Unsupported WNM Action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800838 action, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800839 return -1;
840}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800841
842
843int wnm_send_disassoc_imminent(struct hostapd_data *hapd,
844 struct sta_info *sta, int disassoc_timer)
845{
846 u8 buf[1000], *pos;
847 struct ieee80211_mgmt *mgmt;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000848 const u8 *own_addr = wnm_ap_get_own_addr(hapd, sta);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800849
850 os_memset(buf, 0, sizeof(buf));
851 mgmt = (struct ieee80211_mgmt *) buf;
852 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
853 WLAN_FC_STYPE_ACTION);
854 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000855 os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
856 os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800857 mgmt->u.action.category = WLAN_ACTION_WNM;
858 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
859 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
860 mgmt->u.action.u.bss_tm_req.req_mode =
861 WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
862 mgmt->u.action.u.bss_tm_req.disassoc_timer =
863 host_to_le16(disassoc_timer);
864 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
865
866 pos = mgmt->u.action.u.bss_tm_req.variable;
867
868 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request frame to indicate imminent disassociation (disassoc_timer=%d) to "
869 MACSTR, disassoc_timer, MAC2STR(sta->addr));
Hai Shalomfdcde762020-04-02 11:19:20 -0700870 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800871 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
872 "Management Request frame");
873 return -1;
874 }
875
876 return 0;
877}
878
879
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800880static void set_disassoc_timer(struct hostapd_data *hapd, struct sta_info *sta,
881 int disassoc_timer)
882{
883 int timeout, beacon_int;
884
885 /*
886 * Prevent STA from reconnecting using cached PMKSA to force
887 * full authentication with the authentication server (which may
888 * decide to reject the connection),
889 */
890 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
891
892 beacon_int = hapd->iconf->beacon_int;
893 if (beacon_int < 1)
894 beacon_int = 100; /* best guess */
895 /* Calculate timeout in ms based on beacon_int in TU */
896 timeout = disassoc_timer * beacon_int * 128 / 125;
897 wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR
898 " set to %d ms", MAC2STR(sta->addr), timeout);
899
900 sta->timeout_next = STA_DISASSOC_FROM_CLI;
901 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
902 eloop_register_timeout(timeout / 1000,
903 timeout % 1000 * 1000,
904 ap_handle_timer, hapd, sta);
905}
906
907
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800908int wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd,
909 struct sta_info *sta, const char *url,
910 int disassoc_timer)
911{
912 u8 buf[1000], *pos;
913 struct ieee80211_mgmt *mgmt;
914 size_t url_len;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000915 const u8 *own_addr = wnm_ap_get_own_addr(hapd, sta);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800916
917 os_memset(buf, 0, sizeof(buf));
918 mgmt = (struct ieee80211_mgmt *) buf;
919 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
920 WLAN_FC_STYPE_ACTION);
921 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000922 os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
923 os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800924 mgmt->u.action.category = WLAN_ACTION_WNM;
925 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
926 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
927 mgmt->u.action.u.bss_tm_req.req_mode =
928 WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
929 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
930 mgmt->u.action.u.bss_tm_req.disassoc_timer =
931 host_to_le16(disassoc_timer);
932 mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
933
934 pos = mgmt->u.action.u.bss_tm_req.variable;
935
936 /* Session Information URL */
937 url_len = os_strlen(url);
938 if (url_len > 255)
939 return -1;
940 *pos++ = url_len;
941 os_memcpy(pos, url, url_len);
942 pos += url_len;
943
Hai Shalomfdcde762020-04-02 11:19:20 -0700944 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800945 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
946 "Management Request frame");
947 return -1;
948 }
949
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800950 if (disassoc_timer) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800951 /* send disassociation frame after time-out */
952 set_disassoc_timer(hapd, sta, disassoc_timer);
953 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800954
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800955 return 0;
956}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800957
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800958
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800959int wnm_send_bss_tm_req(struct hostapd_data *hapd, struct sta_info *sta,
960 u8 req_mode, int disassoc_timer, u8 valid_int,
Hai Shaloma20dcd72022-02-04 13:43:00 -0800961 const u8 *bss_term_dur, u8 dialog_token,
962 const char *url, const u8 *nei_rep, size_t nei_rep_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800963 const u8 *mbo_attrs, size_t mbo_len)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800964{
965 u8 *buf, *pos;
966 struct ieee80211_mgmt *mgmt;
967 size_t url_len;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000968 const u8 *own_addr = wnm_ap_get_own_addr(hapd, sta);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800969
970 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
Hai Shaloma20dcd72022-02-04 13:43:00 -0800971 MACSTR
972 " req_mode=0x%x disassoc_timer=%d valid_int=0x%x dialog_token=%u",
973 MAC2STR(sta->addr), req_mode, disassoc_timer, valid_int,
974 dialog_token);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800975 buf = os_zalloc(1000 + nei_rep_len + mbo_len);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800976 if (buf == NULL)
977 return -1;
978 mgmt = (struct ieee80211_mgmt *) buf;
979 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
980 WLAN_FC_STYPE_ACTION);
981 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000982 os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
983 os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800984 mgmt->u.action.category = WLAN_ACTION_WNM;
985 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800986 mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800987 mgmt->u.action.u.bss_tm_req.req_mode = req_mode;
988 mgmt->u.action.u.bss_tm_req.disassoc_timer =
989 host_to_le16(disassoc_timer);
990 mgmt->u.action.u.bss_tm_req.validity_interval = valid_int;
991
992 pos = mgmt->u.action.u.bss_tm_req.variable;
993
994 if ((req_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) &&
995 bss_term_dur) {
996 os_memcpy(pos, bss_term_dur, 12);
997 pos += 12;
998 }
999
1000 if (url) {
1001 /* Session Information URL */
1002 url_len = os_strlen(url);
Dmitry Shmidt432d6032015-01-21 13:19:05 -08001003 if (url_len > 255) {
1004 os_free(buf);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001005 return -1;
Dmitry Shmidt432d6032015-01-21 13:19:05 -08001006 }
1007
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001008 *pos++ = url_len;
1009 os_memcpy(pos, url, url_len);
1010 pos += url_len;
1011 }
1012
1013 if (nei_rep) {
1014 os_memcpy(pos, nei_rep, nei_rep_len);
1015 pos += nei_rep_len;
1016 }
1017
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001018 if (mbo_len > 0) {
1019 pos += mbo_add_ie(pos, buf + sizeof(buf) - pos, mbo_attrs,
1020 mbo_len);
1021 }
1022
Hai Shalomfdcde762020-04-02 11:19:20 -07001023 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001024 wpa_printf(MSG_DEBUG,
1025 "Failed to send BSS Transition Management Request frame");
1026 os_free(buf);
1027 return -1;
1028 }
1029 os_free(buf);
1030
1031 if (disassoc_timer) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001032#ifdef CONFIG_IEEE80211BE
1033 if (ap_sta_is_mld(hapd, sta)) {
1034 int i;
1035 unsigned int links = 0;
1036
1037 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
1038 if (sta->mld_info.links[i].valid)
1039 links++;
1040 }
1041
1042 if (links > 1) {
1043 wpa_printf(MSG_DEBUG,
1044 "WNM: Only terminating one link - other links remains associated for "
1045 MACSTR,
1046 MAC2STR(sta->mld_info.common_info.mld_addr));
1047 return 0;
1048 }
1049 }
1050#endif /* CONFIG_IEEE80211BE */
1051
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001052 /* send disassociation frame after time-out */
1053 set_disassoc_timer(hapd, sta, disassoc_timer);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001054 }
1055
1056 return 0;
1057}
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001058
1059
1060int wnm_send_coloc_intf_req(struct hostapd_data *hapd, struct sta_info *sta,
1061 unsigned int auto_report, unsigned int timeout)
1062{
1063 u8 buf[100], *pos;
1064 struct ieee80211_mgmt *mgmt;
1065 u8 dialog_token = 1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001066 const u8 *own_addr = wnm_ap_get_own_addr(hapd, sta);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001067
1068 if (auto_report > 3 || timeout > 63)
1069 return -1;
1070 os_memset(buf, 0, sizeof(buf));
1071 mgmt = (struct ieee80211_mgmt *) buf;
1072 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1073 WLAN_FC_STYPE_ACTION);
1074 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001075 os_memcpy(mgmt->sa, own_addr, ETH_ALEN);
1076 os_memcpy(mgmt->bssid, own_addr, ETH_ALEN);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001077 mgmt->u.action.category = WLAN_ACTION_WNM;
1078 mgmt->u.action.u.coloc_intf_req.action =
1079 WNM_COLLOCATED_INTERFERENCE_REQ;
1080 mgmt->u.action.u.coloc_intf_req.dialog_token = dialog_token;
1081 mgmt->u.action.u.coloc_intf_req.req_info = auto_report | (timeout << 2);
1082 pos = &mgmt->u.action.u.coloc_intf_req.req_info;
1083 pos++;
1084
1085 wpa_printf(MSG_DEBUG, "WNM: Sending Collocated Interference Request to "
1086 MACSTR " (dialog_token=%u auto_report=%u timeout=%u)",
1087 MAC2STR(sta->addr), dialog_token, auto_report, timeout);
Hai Shalomfdcde762020-04-02 11:19:20 -07001088 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001089 wpa_printf(MSG_DEBUG,
1090 "WNM: Failed to send Collocated Interference Request frame");
1091 return -1;
1092 }
1093
1094 return 0;
1095}