blob: 31d1007ad5074ffae24af0bcaf9b3f48876af250 [file] [log] [blame]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001/*
2 * wpa_supplicant - WNM
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003 * Copyright (c) 2011-2013, 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"
12#include "common/ieee802_11_defs.h"
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080013#include "common/ieee802_11_common.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070014#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 "rsn_supp/wpa.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070017#include "config.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080018#include "wpa_supplicant_i.h"
19#include "driver_i.h"
20#include "scan.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070021#include "ctrl_iface.h"
22#include "bss.h"
23#include "wnm_sta.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080024#include "notify.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080025#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026
27#define MAX_TFS_IE_LEN 1024
Dmitry Shmidt44c95782013-05-17 09:51:35 -070028#define WNM_MAX_NEIGHBOR_REPORT 10
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070029
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070030
31/* get the TFS IE from driver */
32static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
33 u16 *buf_len, enum wnm_oper oper)
34{
35 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
36
37 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
38}
39
40
41/* set the TFS IE to driver */
42static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080043 const u8 *addr, const u8 *buf, u16 buf_len,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070044 enum wnm_oper oper)
45{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080046 u16 len = buf_len;
47
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070048 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070051}
52
53
54/* MLME-SLEEPMODE.request */
55int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080056 u8 action, u16 intval, struct wpabuf *tfs_req)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070057{
58 struct ieee80211_mgmt *mgmt;
59 int res;
60 size_t len;
61 struct wnm_sleep_element *wnmsleep_ie;
Hai Shalom74f70d42019-02-11 14:42:39 -080062 u8 *wnmtfs_ie, *oci_ie;
63 u8 wnmsleep_ie_len, oci_ie_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070064 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
65 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
66 WNM_SLEEP_TFS_REQ_IE_NONE;
67
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080068 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
69 "action=%s to " MACSTR,
70 action == 0 ? "enter" : "exit",
71 MAC2STR(wpa_s->bssid));
72
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070073 /* WNM-Sleep Mode IE */
74 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
75 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
76 if (wnmsleep_ie == NULL)
77 return -1;
78 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
79 wnmsleep_ie->len = wnmsleep_ie_len - 2;
80 wnmsleep_ie->action_type = action;
81 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080082 wnmsleep_ie->intval = host_to_le16(intval);
83 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
84 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070085
86 /* TFS IE(s) */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080087 if (tfs_req) {
88 wnmtfs_ie_len = wpabuf_len(tfs_req);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070089 wnmtfs_ie = os_memdup(wpabuf_head(tfs_req), wnmtfs_ie_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080090 if (wnmtfs_ie == NULL) {
91 os_free(wnmsleep_ie);
92 return -1;
93 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080094 } else {
95 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
96 if (wnmtfs_ie == NULL) {
97 os_free(wnmsleep_ie);
98 return -1;
99 }
100 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
101 tfs_oper)) {
102 wnmtfs_ie_len = 0;
103 os_free(wnmtfs_ie);
104 wnmtfs_ie = NULL;
105 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800107 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
108 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700109
Hai Shalom74f70d42019-02-11 14:42:39 -0800110 oci_ie = NULL;
111 oci_ie_len = 0;
112#ifdef CONFIG_OCV
113 if (action == WNM_SLEEP_MODE_EXIT && wpa_sm_ocv_enabled(wpa_s->wpa)) {
114 struct wpa_channel_info ci;
115
116 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
117 wpa_printf(MSG_WARNING,
118 "Failed to get channel info for OCI element in WNM-Sleep Mode frame");
119 os_free(wnmsleep_ie);
120 os_free(wnmtfs_ie);
121 return -1;
122 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700123#ifdef CONFIG_TESTING_OPTIONS
124 if (wpa_s->oci_freq_override_wnm_sleep) {
125 wpa_printf(MSG_INFO,
126 "TEST: Override OCI KDE frequency %d -> %d MHz",
127 ci.frequency,
128 wpa_s->oci_freq_override_wnm_sleep);
129 ci.frequency = wpa_s->oci_freq_override_wnm_sleep;
130 }
131#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800132
133 oci_ie_len = OCV_OCI_EXTENDED_LEN;
134 oci_ie = os_zalloc(oci_ie_len);
135 if (!oci_ie) {
136 wpa_printf(MSG_WARNING,
137 "Failed to allocate buffer for for OCI element in WNM-Sleep Mode frame");
138 os_free(wnmsleep_ie);
139 os_free(wnmtfs_ie);
140 return -1;
141 }
142
143 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
144 os_free(wnmsleep_ie);
145 os_free(wnmtfs_ie);
146 os_free(oci_ie);
147 return -1;
148 }
149 }
150#endif /* CONFIG_OCV */
151
152 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len +
153 oci_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700154 if (mgmt == NULL) {
155 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
156 "WNM-Sleep Request action frame");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800157 os_free(wnmsleep_ie);
158 os_free(wnmtfs_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700159 return -1;
160 }
161
162 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
163 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
164 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
165 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
166 WLAN_FC_STYPE_ACTION);
167 mgmt->u.action.category = WLAN_ACTION_WNM;
168 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800169 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700170 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
171 wnmsleep_ie_len);
172 /* copy TFS IE here */
173 if (wnmtfs_ie_len > 0) {
174 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
175 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
176 }
177
Hai Shalom74f70d42019-02-11 14:42:39 -0800178#ifdef CONFIG_OCV
179 /* copy OCV OCI here */
180 if (oci_ie_len > 0) {
181 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
182 wnmsleep_ie_len + wnmtfs_ie_len, oci_ie, oci_ie_len);
183 }
184#endif /* CONFIG_OCV */
185
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700186 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
Hai Shalom74f70d42019-02-11 14:42:39 -0800187 wnmtfs_ie_len + oci_ie_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700188
189 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
190 wpa_s->own_addr, wpa_s->bssid,
191 &mgmt->u.action.category, len, 0);
192 if (res < 0)
193 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
194 "(action=%d, intval=%d)", action, intval);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800195 else
196 wpa_s->wnmsleep_used = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700197
198 os_free(wnmsleep_ie);
199 os_free(wnmtfs_ie);
Hai Shalom74f70d42019-02-11 14:42:39 -0800200 os_free(oci_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700201 os_free(mgmt);
202
203 return res;
204}
205
206
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800207static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800208 const u8 *tfsresp_ie_start,
209 const u8 *tfsresp_ie_end)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800210{
211 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
212 wpa_s->bssid, NULL, NULL);
213 /* remove GTK/IGTK ?? */
214
215 /* set the TFS Resp IE(s) */
216 if (tfsresp_ie_start && tfsresp_ie_end &&
217 tfsresp_ie_end - tfsresp_ie_start >= 0) {
218 u16 tfsresp_ie_len;
219 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
220 tfsresp_ie_start;
221 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
222 /* pass the TFS Resp IE(s) to driver for processing */
223 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
224 tfsresp_ie_start,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800225 tfsresp_ie_len,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800226 WNM_SLEEP_TFS_RESP_IE_SET))
227 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
228 }
229}
230
231
232static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
233 const u8 *frm, u16 key_len_total)
234{
235 u8 *ptr, *end;
236 u8 gtk_len;
237
238 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
239 NULL, NULL);
240
241 /* Install GTK/IGTK */
242
243 /* point to key data field */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800244 ptr = (u8 *) frm + 1 + 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800245 end = ptr + key_len_total;
246 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
247
Jouni Malinen17de68d2015-11-09 15:25:41 -0800248 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
249 wpa_msg(wpa_s, MSG_INFO,
250 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
251 return;
252 }
253
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800254 while (end - ptr > 1) {
255 if (2 + ptr[1] > end - ptr) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800256 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
257 "length");
258 if (end > ptr) {
259 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
260 ptr, end - ptr);
261 }
262 break;
263 }
264 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
265 if (ptr[1] < 11 + 5) {
266 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
267 "subelem");
268 break;
269 }
270 gtk_len = *(ptr + 4);
271 if (ptr[1] < 11 + gtk_len ||
272 gtk_len < 5 || gtk_len > 32) {
273 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
274 "subelem");
275 break;
276 }
277 wpa_wnmsleep_install_key(
278 wpa_s->wpa,
279 WNM_SLEEP_SUBELEM_GTK,
280 ptr);
281 ptr += 13 + gtk_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800282 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
283 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
284 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
285 "subelem");
286 break;
287 }
288 wpa_wnmsleep_install_key(wpa_s->wpa,
289 WNM_SLEEP_SUBELEM_IGTK, ptr);
290 ptr += 10 + WPA_IGTK_LEN;
Hai Shalomfdcde762020-04-02 11:19:20 -0700291 } else if (*ptr == WNM_SLEEP_SUBELEM_BIGTK) {
292 if (ptr[1] < 2 + 6 + WPA_BIGTK_LEN) {
293 wpa_printf(MSG_DEBUG,
294 "WNM: Too short BIGTK subelem");
295 break;
296 }
297 wpa_wnmsleep_install_key(wpa_s->wpa,
298 WNM_SLEEP_SUBELEM_BIGTK, ptr);
299 ptr += 10 + WPA_BIGTK_LEN;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800300 } else
301 break; /* skip the loop */
302 }
303}
304
305
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700306static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
307 const u8 *frm, int len)
308{
309 /*
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700310 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700311 * WNM-Sleep Mode IE | TFS Response IE
312 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800313 const u8 *pos = frm; /* point to payload after the action field */
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700314 u16 key_len_total;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700315 struct wnm_sleep_element *wnmsleep_ie = NULL;
316 /* multiple TFS Resp IE (assuming consecutive) */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800317 const u8 *tfsresp_ie_start = NULL;
318 const u8 *tfsresp_ie_end = NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -0800319#ifdef CONFIG_OCV
320 const u8 *oci_ie = NULL;
321 u8 oci_ie_len = 0;
322#endif /* CONFIG_OCV */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800323 size_t left;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700324
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800325 if (!wpa_s->wnmsleep_used) {
326 wpa_printf(MSG_DEBUG,
Jouni Malinen90bfa452017-09-22 11:25:02 +0300327 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800328 return;
329 }
330
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700331 if (len < 3)
332 return;
333 key_len_total = WPA_GET_LE16(frm + 1);
334
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800335 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
336 frm[0], key_len_total);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800337 left = len - 3;
338 if (key_len_total > left) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800339 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
340 return;
341 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800342 pos += 3 + key_len_total;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800343 while (pos - frm + 1 < len) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700344 u8 ie_len = *(pos + 1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800345 if (2 + ie_len > frm + len - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800346 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
347 break;
348 }
349 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800350 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700351 wnmsleep_ie = (struct wnm_sleep_element *) pos;
352 else if (*pos == WLAN_EID_TFS_RESP) {
353 if (!tfsresp_ie_start)
354 tfsresp_ie_start = pos;
355 tfsresp_ie_end = pos;
Hai Shalom74f70d42019-02-11 14:42:39 -0800356#ifdef CONFIG_OCV
357 } else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
358 pos[2] == WLAN_EID_EXT_OCV_OCI) {
359 oci_ie = pos + 3;
360 oci_ie_len = ie_len - 1;
361#endif /* CONFIG_OCV */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700362 } else
363 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
364 pos += ie_len + 2;
365 }
366
367 if (!wnmsleep_ie) {
368 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
369 return;
370 }
371
Hai Shalom74f70d42019-02-11 14:42:39 -0800372#ifdef CONFIG_OCV
373 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
374 wpa_sm_ocv_enabled(wpa_s->wpa)) {
375 struct wpa_channel_info ci;
376
377 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
378 wpa_msg(wpa_s, MSG_WARNING,
379 "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
380 return;
381 }
382
383 if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
384 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -0700385 ci.seg1_idx) != OCI_SUCCESS) {
386 wpa_msg(wpa_s, MSG_WARNING, "WNM: OCV failed: %s",
387 ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -0800388 return;
389 }
390 }
391#endif /* CONFIG_OCV */
392
Jouni Malinen90bfa452017-09-22 11:25:02 +0300393 wpa_s->wnmsleep_used = 0;
394
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800395 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
396 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700397 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
398 "frame (action=%d, intval=%d)",
399 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800400 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
401 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
402 tfsresp_ie_end);
403 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
404 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700405 }
406 } else {
407 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
408 "(action=%d, intval=%d)",
409 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800410 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700411 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
412 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800413 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700414 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
415 wpa_s->bssid, NULL, NULL);
416 }
417}
418
419
Sunil Ravi99c035e2024-07-12 01:42:03 +0000420void wnm_btm_reset(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700421{
422 int i;
423
424 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700425 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700426 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
427 }
428
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700429 wpa_s->wnm_num_neighbor_report = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700430 os_free(wpa_s->wnm_neighbor_report_elements);
431 wpa_s->wnm_neighbor_report_elements = NULL;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800432
Sunil Ravi99c035e2024-07-12 01:42:03 +0000433 wpa_s->wnm_cand_valid_until.sec = 0;
434 wpa_s->wnm_cand_valid_until.usec = 0;
435
436 wpa_s->wnm_mode = 0;
437 wpa_s->wnm_dialog_token = 0;
438 wpa_s->wnm_reply = 0;
439
440#ifdef CONFIG_MBO
441 wpa_s->wnm_mbo_trans_reason_present = 0;
442 wpa_s->wnm_mbo_transition_reason = 0;
443#endif /* CONFIG_MBO */
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700444}
445
446
447static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
448 u8 id, u8 elen, const u8 *pos)
449{
450 switch (id) {
451 case WNM_NEIGHBOR_TSF:
452 if (elen < 2 + 2) {
453 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
454 break;
455 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800456 rep->tsf_offset = WPA_GET_LE16(pos);
457 rep->beacon_int = WPA_GET_LE16(pos + 2);
458 rep->tsf_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700459 break;
460 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
461 if (elen < 2) {
462 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
463 "country string");
464 break;
465 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800466 os_memcpy(rep->country, pos, 2);
467 rep->country_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700468 break;
469 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
470 if (elen < 1) {
471 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
472 "candidate");
473 break;
474 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800475 rep->preference = pos[0];
476 rep->preference_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700477 break;
478 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
Hai Shalomcb95c3f2019-02-04 12:53:10 -0800479 if (elen < 10) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700480 wpa_printf(MSG_DEBUG,
481 "WNM: Too short BSS termination duration");
Hai Shalomcb95c3f2019-02-04 12:53:10 -0800482 break;
483 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800484 rep->bss_term_tsf = WPA_GET_LE64(pos);
485 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
486 rep->bss_term_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700487 break;
488 case WNM_NEIGHBOR_BEARING:
489 if (elen < 8) {
490 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
491 "bearing");
492 break;
493 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800494 rep->bearing = WPA_GET_LE16(pos);
495 rep->distance = WPA_GET_LE32(pos + 2);
496 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
497 rep->bearing_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700498 break;
499 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700500 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700501 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
502 "pilot");
503 break;
504 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700505 os_free(rep->meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700506 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
507 if (rep->meas_pilot == NULL)
508 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700509 rep->meas_pilot->measurement_pilot = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700510 rep->meas_pilot->subelem_len = elen - 1;
511 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700512 break;
513 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700514 if (elen < 5) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700515 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
516 "capabilities");
517 break;
518 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800519 os_memcpy(rep->rm_capab, pos, 5);
520 rep->rm_capab_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700521 break;
522 case WNM_NEIGHBOR_MULTIPLE_BSSID:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700523 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700524 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
525 break;
526 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700527 os_free(rep->mul_bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700528 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
529 if (rep->mul_bssid == NULL)
530 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700531 rep->mul_bssid->max_bssid_indicator = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700532 rep->mul_bssid->subelem_len = elen - 1;
533 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700534 break;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000535 default:
536 wpa_printf(MSG_DEBUG,
537 "WNM: Unsupported neighbor report subelement id %u",
538 id);
539 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700540 }
541}
542
543
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800544static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
545{
546 struct wpa_bss *bss = wpa_s->current_bss;
547 const char *country = NULL;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800548 int freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800549
550 if (bss) {
551 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
552
553 if (elem && elem[1] >= 2)
554 country = (const char *) (elem + 2);
555 }
556
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800557 freq = ieee80211_chan_to_freq(country, op_class, chan);
Sunil Ravic0f5d412024-09-11 22:12:49 +0000558 if (freq <= 0 && (op_class == 0 || op_class == 255)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800559 /*
560 * Some APs do not advertise correct operating class
561 * information. Try to determine the most likely operating
562 * frequency based on the channel number.
563 */
564 if (chan >= 1 && chan <= 13)
565 freq = 2407 + chan * 5;
566 else if (chan == 14)
567 freq = 2484;
Hai Shalom60840252021-02-19 19:02:11 -0800568 else if (chan >= 36 && chan <= 177)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800569 freq = 5000 + chan * 5;
570 }
571 return freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800572}
573
574
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700575static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
576 const u8 *pos, u8 len,
577 struct neighbor_report *rep)
578{
579 u8 left = len;
580
581 if (left < 13) {
582 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
583 return;
584 }
585
586 os_memcpy(rep->bssid, pos, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800587 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700588 rep->regulatory_class = *(pos + 10);
589 rep->channel_number = *(pos + 11);
590 rep->phy_type = *(pos + 12);
591
592 pos += 13;
593 left -= 13;
594
595 while (left >= 2) {
596 u8 id, elen;
597
598 id = *pos++;
599 elen = *pos++;
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700600 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
601 left -= 2;
602 if (elen > left) {
603 wpa_printf(MSG_DEBUG,
604 "WNM: Truncated neighbor report subelement");
605 break;
606 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700607 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700608 left -= elen;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700609 pos += elen;
610 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800611
612 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
613 rep->channel_number);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700614}
615
616
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000617static void
618fetch_drv_mbo_candidate_info(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700619 enum mbo_transition_reject_reason *reason)
620{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000621#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700622 struct wpa_bss_trans_info params;
623 struct wpa_bss_candidate_info *info = NULL;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000624 struct neighbor_report *nei;
625 u8 *pos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700626 unsigned int i;
627
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000628 if (!wpa_s->wnm_mbo_trans_reason_present)
629 return;
630
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700631 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
632 params.n_candidates = 0;
633 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
634 if (!params.bssid)
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000635 return;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700636
637 pos = params.bssid;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000638 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
639 nei = &wpa_s->wnm_neighbor_report_elements[i];
640
641 nei->drv_mbo_reject = 0;
642
643 if (nei->preference_present && nei->preference == 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700644 continue;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000645
646 /* Should we query BSSIDs that we reject for other reasons? */
647
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700648 os_memcpy(pos, nei->bssid, ETH_ALEN);
649 pos += ETH_ALEN;
650 params.n_candidates++;
651 }
652
653 if (!params.n_candidates)
654 goto end;
655
656 info = wpa_drv_get_bss_trans_status(wpa_s, &params);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000657 if (!info)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700658 goto end;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700659
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700660 for (i = 0; i < info->num; i++) {
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000661 int j;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700662
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000663 for (j = 0; j < wpa_s->wnm_num_neighbor_report; j++) {
664 nei = &wpa_s->wnm_neighbor_report_elements[j];
665
666 if (!ether_addr_equal(info->candidates[i].bssid,
667 nei->bssid))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700668 continue;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700669
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000670 nei->drv_mbo_reject = !info->candidates[i].is_accept;
671
672 /* Use the reject reason from the first candidate */
673 if (i == 0 && nei->drv_mbo_reject)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700674 *reason = info->candidates[i].reject_reason;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000675
676 break;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700677 }
678 }
679
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700680end:
681 os_free(params.bssid);
682 if (info) {
683 os_free(info->candidates);
684 os_free(info);
685 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700686#endif /* CONFIG_MBO */
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700687}
688
689
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800690static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
691{
692 const u8 *ie_a, *ie_b;
693
694 if (!a || !b)
695 return 0;
696
697 ie_a = wpa_bss_get_ie(a, eid);
698 ie_b = wpa_bss_get_ie(b, eid);
699
700 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
701 return 0;
702
703 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
704}
705
706
707static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
708{
709 u32 info = 0;
710
711 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
712
713 /*
714 * Leave the security and key scope bits unset to indicate that the
715 * security information is not available.
716 */
717
718 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
719 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
720 if (bss->caps & WLAN_CAPABILITY_QOS)
721 info |= NEI_REP_BSSID_INFO_QOS;
722 if (bss->caps & WLAN_CAPABILITY_APSD)
723 info |= NEI_REP_BSSID_INFO_APSD;
724 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
725 info |= NEI_REP_BSSID_INFO_RM;
726 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
727 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
728 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
729 info |= NEI_REP_BSSID_INFO_IMM_BA;
730 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
731 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
732 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
733 info |= NEI_REP_BSSID_INFO_HT;
734
735 return info;
736}
737
738
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700739static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
740 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
741 u8 pref)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800742{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700743 if (wpabuf_len(*buf) + 18 >
744 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800745 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700746 "WNM: No room in frame for Neighbor Report element");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800747 return -1;
748 }
749
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700750 if (wpabuf_resize(buf, 18) < 0) {
751 wpa_printf(MSG_DEBUG,
752 "WNM: Failed to allocate memory for Neighbor Report element");
753 return -1;
754 }
755
756 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800757 /* length: 13 for basic neighbor report + 3 for preference subelement */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700758 wpabuf_put_u8(*buf, 16);
759 wpabuf_put_data(*buf, bssid, ETH_ALEN);
760 wpabuf_put_le32(*buf, bss_info);
761 wpabuf_put_u8(*buf, op_class);
762 wpabuf_put_u8(*buf, chan);
763 wpabuf_put_u8(*buf, phy_type);
764 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
765 wpabuf_put_u8(*buf, 1);
766 wpabuf_put_u8(*buf, pref);
767 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800768}
769
770
771static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700772 struct wpa_bss *bss, struct wpabuf **buf,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800773 u8 pref)
774{
775 const u8 *ie;
776 u8 op_class, chan;
Sunil8cd6f4d2022-06-28 18:40:46 +0000777 int sec_chan = 0;
778 enum oper_chan_width vht = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800779 enum phy_type phy_type;
780 u32 info;
781 struct ieee80211_ht_operation *ht_oper = NULL;
782 struct ieee80211_vht_operation *vht_oper = NULL;
783
784 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
785 if (ie && ie[1] >= 2) {
786 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
787
788 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
789 sec_chan = 1;
790 else if (ht_oper->ht_param &
791 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
792 sec_chan = -1;
793 }
794
795 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
796 if (ie && ie[1] >= 1) {
797 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
798
Hai Shalom81f62d82019-07-22 12:10:00 -0700799 if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
800 vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
801 vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800802 vht = vht_oper->vht_op_info_chwidth;
803 }
804
805 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
806 &chan) == NUM_HOSTAPD_MODES) {
807 wpa_printf(MSG_DEBUG,
808 "WNM: Cannot determine operating class and channel");
809 return -2;
810 }
811
812 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
813 (vht_oper != NULL));
814 if (phy_type == PHY_TYPE_UNSPECIFIED) {
815 wpa_printf(MSG_DEBUG,
816 "WNM: Cannot determine BSS phy type for Neighbor Report");
817 return -2;
818 }
819
820 info = wnm_get_bss_info(wpa_s, bss);
821
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700822 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
823 pref);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800824}
825
826
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700827static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800828{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800829 unsigned int i, pref = 255;
830 struct os_reltime now;
831 struct wpa_ssid *ssid = wpa_s->current_ssid;
832
833 if (!ssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700834 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800835
836 /*
837 * TODO: Define when scan results are no longer valid for the candidate
838 * list.
839 */
840 os_get_reltime(&now);
841 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700842 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800843
844 wpa_printf(MSG_DEBUG,
845 "WNM: Add candidate list to BSS Transition Management Response frame");
846 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
847 struct wpa_bss *bss = wpa_s->last_scan_res[i];
848 int res;
849
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800850 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700851 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800852 if (res == -2)
853 continue; /* could not build entry for BSS */
854 if (res < 0)
855 break; /* no more room for candidates */
856 if (pref == 1)
857 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800858 }
859 }
860
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700861 wpa_hexdump_buf(MSG_DEBUG,
862 "WNM: BSS Transition Management Response candidate list",
863 *buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800864}
865
866
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700867#define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
868
Sunil Ravi99c035e2024-07-12 01:42:03 +0000869static int wnm_send_bss_transition_mgmt_resp(
870 struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700871 enum bss_trans_mgmt_status_code status,
872 enum mbo_transition_reject_reason reason,
873 u8 delay, const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800874{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700875 struct wpabuf *buf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800876 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800877
Sunil Ravi99c035e2024-07-12 01:42:03 +0000878 wpa_s->wnm_reply = 0;
879
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700880 wpa_printf(MSG_DEBUG,
881 "WNM: Send BSS Transition Management Response to " MACSTR
882 " dialog_token=%u status=%u reason=%u delay=%d",
Sunil Ravi99c035e2024-07-12 01:42:03 +0000883 MAC2STR(wpa_s->bssid), wpa_s->wnm_dialog_token, status,
884 reason, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800885 if (!wpa_s->current_bss) {
886 wpa_printf(MSG_DEBUG,
887 "WNM: Current BSS not known - drop response");
Sunil Ravi99c035e2024-07-12 01:42:03 +0000888 return -1;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800889 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800890
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700891 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
892 if (!buf) {
893 wpa_printf(MSG_DEBUG,
894 "WNM: Failed to allocate memory for BTM response");
Sunil Ravi99c035e2024-07-12 01:42:03 +0000895 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700896 }
897
Hai Shalom74f70d42019-02-11 14:42:39 -0800898 wpa_s->bss_tm_status = status;
899 wpas_notify_bss_tm_status(wpa_s);
900
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700901 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
902 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000903 wpabuf_put_u8(buf, wpa_s->wnm_dialog_token);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700904 wpabuf_put_u8(buf, status);
905 wpabuf_put_u8(buf, delay);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800906 if (target_bssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700907 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800908 } else if (status == WNM_BSS_TM_ACCEPT) {
909 /*
910 * P802.11-REVmc clarifies that the Target BSSID field is always
911 * present when status code is zero, so use a fake value here if
912 * no BSSID is yet known.
913 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700914 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800915 }
916
Sunil Ravi99c035e2024-07-12 01:42:03 +0000917 if (status == WNM_BSS_TM_ACCEPT && target_bssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700918 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800919
920#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700921 if (status != WNM_BSS_TM_ACCEPT &&
922 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
923 u8 mbo[10];
924 size_t ret;
925
926 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
927 reason);
928 if (ret) {
929 if (wpabuf_resize(&buf, ret) < 0) {
930 wpabuf_free(buf);
931 wpa_printf(MSG_DEBUG,
932 "WNM: Failed to allocate memory for MBO IE");
Sunil Ravi99c035e2024-07-12 01:42:03 +0000933 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700934 }
935
936 wpabuf_put_data(buf, mbo, ret);
937 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800938 }
939#endif /* CONFIG_MBO */
940
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800941 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
942 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700943 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800944 if (res < 0) {
945 wpa_printf(MSG_DEBUG,
946 "WNM: Failed to send BSS Transition Management Response");
947 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700948
949 wpabuf_free(buf);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000950
951 return res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800952}
953
954
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700955static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
956 struct wpa_bss *bss, struct wpa_ssid *ssid,
957 int after_new_scan)
958{
Hai Shaloma20dcd72022-02-04 13:43:00 -0800959 struct wpa_radio_work *already_connecting;
960
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700961 wpa_dbg(wpa_s, MSG_DEBUG,
962 "WNM: Transition to BSS " MACSTR
963 " based on BSS Transition Management Request (old BSSID "
964 MACSTR " after_new_scan=%d)",
965 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
966
967 /* Send the BSS Management Response - Accept */
968 if (wpa_s->wnm_reply) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000969 wpa_s->wnm_target_bss = bss;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700970 wpa_printf(MSG_DEBUG,
971 "WNM: Sending successful BSS Transition Management Response");
Sunil Ravi99c035e2024-07-12 01:42:03 +0000972
973 /* This function will be called again from the TX handler to
974 * start the actual reassociation after this response has been
975 * delivered to the current AP. */
976 if (wnm_send_bss_transition_mgmt_resp(
977 wpa_s, WNM_BSS_TM_ACCEPT,
978 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
979 bss->bssid) >= 0)
980 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700981 }
982
983 if (bss == wpa_s->current_bss) {
984 wpa_printf(MSG_DEBUG,
985 "WNM: Already associated with the preferred candidate");
Sunil Ravi99c035e2024-07-12 01:42:03 +0000986 wnm_btm_reset(wpa_s);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700987 return;
988 }
989
Hai Shaloma20dcd72022-02-04 13:43:00 -0800990 already_connecting = radio_work_pending(wpa_s, "sme-connect");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700991 wpa_s->reassociate = 1;
992 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
993 wpa_supplicant_connect(wpa_s, bss, ssid);
Hai Shaloma20dcd72022-02-04 13:43:00 -0800994
995 /*
996 * Indicate that a BSS transition is in progress so scan results that
997 * come in before the 'sme-connect' radio work gets executed do not
998 * override the original connection attempt.
999 */
1000 if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1001 wpa_s->bss_trans_mgmt_in_progress = true;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001002}
1003
1004
Sunil Ravi99c035e2024-07-12 01:42:03 +00001005int wnm_scan_process(struct wpa_supplicant *wpa_s, bool pre_scan_check)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001006{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001007 struct wpa_bss *bss, *current_bss = wpa_s->current_bss;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001008 struct wpa_ssid *ssid = wpa_s->current_ssid;
1009 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001010 enum mbo_transition_reject_reason reason =
1011 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001012 struct wpa_ssid *selected_ssid = NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001013
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001014 if (!ssid || !wpa_s->wnm_dialog_token)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001015 return 0;
1016
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001017 wpa_dbg(wpa_s, MSG_DEBUG,
1018 "WNM: Process scan results for BSS Transition Management");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001019 if (!pre_scan_check &&
1020 os_reltime_initialized(&wpa_s->wnm_cand_valid_until) &&
1021 os_reltime_before(&wpa_s->wnm_cand_valid_until,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001022 &wpa_s->scan_trigger_time)) {
1023 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001024 goto send_bss_resp_fail;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001025 }
1026
Sunil Ravic0f5d412024-09-11 22:12:49 +00001027 if (!pre_scan_check && !wpa_s->wnm_transition_scan)
1028 return 0;
1029
1030 wpa_s->wnm_transition_scan = false;
1031
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001032 /* Fetch MBO transition candidate rejection information from driver */
1033 fetch_drv_mbo_candidate_info(wpa_s, &reason);
1034
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001035 /* Compare the Neighbor Report and scan results */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001036 bss = wpa_supplicant_select_bss(wpa_s, ssid, &selected_ssid, 1);
1037#ifdef CONFIG_MBO
1038 if (!bss && wpa_s->wnm_mbo_trans_reason_present &&
1039 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT)) {
1040 int i;
1041 bool changed = false;
1042
1043 /*
1044 * We didn't find any candidate, the driver had a say about
1045 * which targets to reject and disassociation is immiment.
1046 *
1047 * We should still try to roam, so retry after ignoring the
1048 * driver reject for any BSS that has an RSSI better than
1049 * disassoc_imminent_rssi_threshold.
1050 */
1051 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1052 struct neighbor_report *nei;
1053
1054 nei = &wpa_s->wnm_neighbor_report_elements[i];
1055 bss = wpa_bss_get_bssid(wpa_s, nei->bssid);
1056 if (bss && bss->level >
1057 wpa_s->conf->disassoc_imminent_rssi_threshold) {
1058 nei->drv_mbo_reject = 0;
1059 changed = true;
1060 }
1061 }
1062
1063 if (changed) {
1064 wpa_printf(MSG_DEBUG,
1065 "WNM: Ignore driver rejection due to imminent disassociation and acceptable RSSI");
1066 bss = wpa_supplicant_select_bss(wpa_s, ssid,
1067 &selected_ssid, 1);
1068 }
1069 }
1070#endif /* CONFIG_MBO */
Sunil Ravi99c035e2024-07-12 01:42:03 +00001071
1072 /*
1073 * If this is a pre-scan check, returning 0 will trigger a scan and
1074 * another call. In that case, reject "bad" candidates in the hope of
1075 * finding a better candidate after scanning.
1076 *
1077 * Use a simple heuristic to check whether the selection is reasonable
1078 * or a scan is a good idea. For that, we need to have found a
1079 * candidate BSS (which might be the current one), it is up-to-date,
1080 * and we don't want to immediately roam back again.
1081 */
1082 if (pre_scan_check) {
1083 struct os_reltime age;
1084
1085 if (!bss)
1086 return 0;
1087
1088 os_reltime_age(&bss->last_update, &age);
1089 if (age.sec >= 10)
1090 return 0;
1091
1092#ifndef CONFIG_NO_ROAMING
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001093 if (current_bss && bss != current_bss &&
1094 wpa_supplicant_need_to_roam_within_ess(wpa_s, bss,
1095 current_bss, false))
Sunil Ravi99c035e2024-07-12 01:42:03 +00001096 return 0;
1097#endif /* CONFIG_NO_ROAMING */
1098 }
1099
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001100#ifndef CONFIG_NO_ROAMING
1101 /* Apply normal roaming rules if we can stay with the current BSS */
1102 if (current_bss && bss != current_bss &&
1103 wpa_scan_res_match(wpa_s, 0, current_bss, wpa_s->current_ssid,
1104 1, 0) &&
1105 !wpa_supplicant_need_to_roam_within_ess(wpa_s, current_bss, bss,
1106 true))
1107 bss = current_bss;
1108#endif /* CONFIG_NO_ROAMING */
1109
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001110 if (!bss) {
1111 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1112 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1113 goto send_bss_resp_fail;
1114 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001115
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001116 wpa_printf(MSG_DEBUG,
1117 "WNM: Found an acceptable preferred transition candidate BSS "
1118 MACSTR " (RSSI %d, tput: %d bss-tput: %d)",
1119 MAC2STR(bss->bssid), bss->level, bss->est_throughput,
1120 current_bss ? (int) current_bss->est_throughput : -1);
1121
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001122 /* Associate to the network */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001123 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001124 return 1;
1125
1126send_bss_resp_fail:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001127 if (wpa_s->wnm_reply) {
1128 /* If disassoc imminent is set, we must not reject */
1129 if (wpa_s->wnm_mode &
1130 (WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
1131 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)) {
1132 wpa_printf(MSG_DEBUG,
1133 "WNM: Accept BTM request because disassociation imminent bit is set");
1134 status = WNM_BSS_TM_ACCEPT;
1135 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001136
Sunil Ravi99c035e2024-07-12 01:42:03 +00001137 wnm_send_bss_transition_mgmt_resp(wpa_s, status, reason,
1138 0, NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001139 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00001140
1141 wnm_btm_reset(wpa_s);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001142
1143 return 0;
1144}
1145
1146
1147static int cand_pref_compar(const void *a, const void *b)
1148{
1149 const struct neighbor_report *aa = a;
1150 const struct neighbor_report *bb = b;
1151
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001152 if (aa->disassoc_imminent && !bb->disassoc_imminent)
1153 return 1;
1154 if (bb->disassoc_imminent && !aa->disassoc_imminent)
1155 return -1;
1156
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001157 if (!aa->preference_present && !bb->preference_present)
1158 return 0;
1159 if (!aa->preference_present)
1160 return 1;
1161 if (!bb->preference_present)
1162 return -1;
1163 if (bb->preference > aa->preference)
1164 return 1;
1165 if (bb->preference < aa->preference)
1166 return -1;
1167 return 0;
1168}
1169
1170
1171static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1172{
1173 if (!wpa_s->wnm_neighbor_report_elements)
1174 return;
1175 qsort(wpa_s->wnm_neighbor_report_elements,
1176 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1177 cand_pref_compar);
1178}
1179
1180
1181static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1182{
1183 unsigned int i;
1184
1185 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1186 if (!wpa_s->wnm_neighbor_report_elements)
1187 return;
1188 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1189 struct neighbor_report *nei;
1190
1191 nei = &wpa_s->wnm_neighbor_report_elements[i];
1192 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1193 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1194 i, MAC2STR(nei->bssid), nei->bssid_info,
1195 nei->regulatory_class,
1196 nei->channel_number, nei->phy_type,
1197 nei->preference_present ? nei->preference : -1,
1198 nei->freq);
1199 }
1200}
1201
1202
1203static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1204{
1205 unsigned int i;
1206
1207 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1208 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1209 int j;
1210
1211 for (j = 0; j < mode->num_channels; j++) {
1212 struct hostapd_channel_data *chan;
1213
1214 chan = &mode->channels[j];
1215 if (chan->freq == freq &&
1216 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1217 return 1;
1218 }
1219 }
1220
1221 return 0;
1222}
1223
1224
1225static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1226{
1227 int *freqs;
1228 int num_freqs = 0;
1229 unsigned int i;
1230
1231 if (!wpa_s->wnm_neighbor_report_elements)
1232 return;
1233
1234 if (wpa_s->hw.modes == NULL)
1235 return;
1236
1237 os_free(wpa_s->next_scan_freqs);
1238 wpa_s->next_scan_freqs = NULL;
1239
1240 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1241 if (freqs == NULL)
1242 return;
1243
1244 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1245 struct neighbor_report *nei;
1246
1247 nei = &wpa_s->wnm_neighbor_report_elements[i];
Sunil Ravi99c035e2024-07-12 01:42:03 +00001248
1249 if (nei->preference_present && nei->preference == 0)
1250 continue;
1251
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001252 if (nei->freq <= 0) {
1253 wpa_printf(MSG_DEBUG,
1254 "WNM: Unknown neighbor operating frequency for "
1255 MACSTR " - scan all channels",
1256 MAC2STR(nei->bssid));
1257 os_free(freqs);
1258 return;
1259 }
1260 if (chan_supported(wpa_s, nei->freq))
1261 add_freq(freqs, &num_freqs, nei->freq);
1262 }
1263
1264 if (num_freqs == 0) {
1265 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001266 return;
1267 }
1268
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001269 wpa_printf(MSG_DEBUG,
1270 "WNM: Scan %d frequencies based on transition candidate list",
1271 num_freqs);
1272 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001273}
1274
1275
Sunil Ravic0f5d412024-09-11 22:12:49 +00001276static int wnm_parse_candidate_list(struct wpa_supplicant *wpa_s,
1277 const u8 *pos, const u8 *end,
1278 int *num_valid_candidates)
1279{
1280 *num_valid_candidates = 0;
1281
1282 while (end - pos >= 2 &&
1283 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT) {
1284 u8 tag = *pos++;
1285 u8 len = *pos++;
1286
1287 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u", tag);
1288 if (len > end - pos) {
1289 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1290 return -1;
1291 }
1292 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1293 struct neighbor_report *rep;
1294
1295 if (!wpa_s->wnm_num_neighbor_report) {
1296 wpa_s->wnm_neighbor_report_elements = os_calloc(
1297 WNM_MAX_NEIGHBOR_REPORT,
1298 sizeof(struct neighbor_report));
1299 if (!wpa_s->wnm_neighbor_report_elements)
1300 return -1;
1301 }
1302
1303 rep = &wpa_s->wnm_neighbor_report_elements[
1304 wpa_s->wnm_num_neighbor_report];
1305 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
1306 if ((wpa_s->wnm_mode &
1307 WNM_BSS_TM_REQ_DISASSOC_IMMINENT) &&
1308 ether_addr_equal(rep->bssid, wpa_s->bssid))
1309 rep->disassoc_imminent = 1;
1310
1311 if (rep->preference_present && rep->preference)
1312 *num_valid_candidates += 1;
1313
1314 wpa_s->wnm_num_neighbor_report++;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001315 }
1316
1317 pos += len;
1318 }
1319
1320 return 0;
1321}
1322
1323
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001324static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1325 const u8 *pos, const u8 *end,
1326 int reply)
1327{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001328 unsigned int beacon_int;
1329 u8 valid_int;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001330#ifdef CONFIG_MBO
1331 const u8 *vendor;
1332#endif /* CONFIG_MBO */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001333 bool disassoc_imminent;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001334 int num_valid_candidates;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001335
Hai Shalomc3565922019-10-28 11:58:20 -07001336 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
Hai Shalom81f62d82019-07-22 12:10:00 -07001337 return;
1338
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001339 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001340 return;
1341
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001342#ifdef CONFIG_MBO
Sunil Ravi4018d712019-12-06 18:01:21 -08001343 wpa_s->wnm_mbo_cell_pref_present = 0;
1344 wpa_s->wnm_mbo_cell_preference = 0;
1345 wpa_s->wnm_mbo_assoc_retry_delay_present = 0;
1346 wpa_s->wnm_mbo_assoc_retry_delay_sec = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001347#endif /* CONFIG_MBO */
1348
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001349 if (wpa_s->current_bss)
1350 beacon_int = wpa_s->current_bss->beacon_int;
1351 else
1352 beacon_int = 100; /* best guess */
1353
Sunil Ravi99c035e2024-07-12 01:42:03 +00001354 wnm_btm_reset(wpa_s);
1355
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001356 wpa_s->wnm_dialog_token = pos[0];
1357 wpa_s->wnm_mode = pos[1];
Sunil Ravic0f5d412024-09-11 22:12:49 +00001358 wpa_s->wnm_disassoc_timer = WPA_GET_LE16(pos + 2);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001359 wpa_s->wnm_link_removal = false;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001360 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001361 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001362
1363 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1364 "dialog_token=%u request_mode=0x%x "
1365 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001366 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Sunil Ravic0f5d412024-09-11 22:12:49 +00001367 wpa_s->wnm_disassoc_timer, valid_int);
1368
1369 if (!wpa_s->wnm_dialog_token) {
1370 wpa_printf(MSG_DEBUG, "WNM: Invalid dialog token");
1371 goto reset;
1372 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001373
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001374#if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1375 if (wpa_s->reject_btm_req_reason) {
1376 wpa_printf(MSG_INFO,
1377 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1378 wpa_s->reject_btm_req_reason);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001379 wnm_send_bss_transition_mgmt_resp(
Sunil Ravi99c035e2024-07-12 01:42:03 +00001380 wpa_s, wpa_s->reject_btm_req_reason,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001381 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001382 goto reset;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001383 }
1384#endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1385
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001386 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001387
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001388 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001389 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001390 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
Sunil Ravic0f5d412024-09-11 22:12:49 +00001391 goto reset;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001392 }
1393 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001394 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001395 }
1396
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001397 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001398 char url[256];
Sunil8cd6f4d2022-06-28 18:40:46 +00001399 u8 url_len;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001400
Sunil8cd6f4d2022-06-28 18:40:46 +00001401 if (end - pos < 1) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001402 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1403 "Management Request (URL)");
Sunil Ravic0f5d412024-09-11 22:12:49 +00001404 goto reset;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001405 }
Sunil8cd6f4d2022-06-28 18:40:46 +00001406 url_len = *pos++;
1407 if (url_len > end - pos) {
1408 wpa_printf(MSG_DEBUG,
1409 "WNM: Invalid BSS Transition Management Request (URL truncated)");
Sunil Ravic0f5d412024-09-11 22:12:49 +00001410 goto reset;
Sunil8cd6f4d2022-06-28 18:40:46 +00001411 }
1412 os_memcpy(url, pos, url_len);
1413 url[url_len] = '\0';
1414 pos += url_len;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001415
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001416 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1417 wpa_sm_pmf_enabled(wpa_s->wpa),
Sunil Ravic0f5d412024-09-11 22:12:49 +00001418 wpa_s->wnm_disassoc_timer * beacon_int * 128 / 125,
1419 url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001420 }
1421
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001422#ifdef CONFIG_MBO
1423 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1424 if (vendor) {
1425 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001426 }
1427#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07001428 if (wpa_s->conf->btm_offload) {
1429 wpa_printf(MSG_INFO,
1430 "WNM: BTM offload enabled. Notify status and return");
1431 wpa_s->bss_tm_status = WNM_BSS_TM_ACCEPT;
1432 wpas_notify_bss_tm_status(wpa_s);
1433 return;
1434 }
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001435
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001436 disassoc_imminent = wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1437
1438 /*
1439 * Based on IEEE P802.11be/D5.0, when a station is a non-AP MLD with
1440 * more than one affiliated link, the Link Removal Imminent field is
1441 * set to 1, and the BSS Termination Included field is set to 1, only
1442 * one of the links is removed and the other links remain associated.
1443 * Ignore the Disassociation Imminent field in such a case.
Sunil Ravi99c035e2024-07-12 01:42:03 +00001444 *
1445 * TODO: We should check if the AP has more than one link.
1446 * TODO: We should pass the RX link and use that
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001447 */
Sunil Ravi99c035e2024-07-12 01:42:03 +00001448 if (disassoc_imminent && wpa_s->valid_links &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001449 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT) &&
1450 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED)) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001451 /* If we still have a link, then just accept the request */
1452 if (wpa_s->valid_links & (wpa_s->valid_links - 1)) {
1453 wpa_printf(MSG_INFO,
1454 "WNM: BTM request for a single MLO link - ignore disassociation imminent since other links remain associated");
1455 disassoc_imminent = false;
1456
1457 wnm_send_bss_transition_mgmt_resp(
1458 wpa_s, WNM_BSS_TM_ACCEPT, 0, 0, NULL);
1459
Sunil Ravic0f5d412024-09-11 22:12:49 +00001460 goto reset;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001461 }
1462
1463 /* The last link is being removed (which must be the assoc link)
1464 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001465 wpa_s->wnm_link_removal = true;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001466 wpa_s->wnm_disassoc_mld = false;
1467 os_memcpy(wpa_s->wnm_disassoc_addr,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001468 wpa_s->links[wpa_s->mlo_assoc_link_id].bssid,
1469 ETH_ALEN);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001470 } else if (wpa_s->valid_links) {
1471 wpa_s->wnm_disassoc_mld = true;
1472 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->ap_mld_addr,
1473 ETH_ALEN);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001474 } else {
Sunil Ravic0f5d412024-09-11 22:12:49 +00001475 wpa_s->wnm_disassoc_mld = false;
1476 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->bssid, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001477 }
1478
Sunil Ravic0f5d412024-09-11 22:12:49 +00001479 if (disassoc_imminent)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001480 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Sunil Ravic0f5d412024-09-11 22:12:49 +00001481 "Disassociation Timer %u", wpa_s->wnm_disassoc_timer);
1482
1483 if (wnm_parse_candidate_list(wpa_s, pos, end,
1484 &num_valid_candidates) < 0)
1485 goto reset;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001486
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001487 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001488 if (!wpa_s->wnm_num_neighbor_report) {
1489 wpa_printf(MSG_DEBUG,
1490 "WNM: Candidate list included bit is set, but no candidates found");
1491 wnm_send_bss_transition_mgmt_resp(
Sunil Ravi99c035e2024-07-12 01:42:03 +00001492 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001493 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1494 NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001495 goto reset;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001496 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00001497 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
1498 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001499
Sunil Ravic0f5d412024-09-11 22:12:49 +00001500 if (wpa_s->wnm_num_neighbor_report) {
1501 unsigned int valid_ms;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001502
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001503 wnm_sort_cand_list(wpa_s);
1504 wnm_dump_cand_list(wpa_s);
1505 valid_ms = valid_int * beacon_int * 128 / 125;
1506 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1507 valid_ms);
1508 os_get_reltime(&wpa_s->wnm_cand_valid_until);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001509 os_reltime_add_ms(&wpa_s->wnm_cand_valid_until, valid_ms);
1510 } else if (!disassoc_imminent) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001511 enum bss_trans_mgmt_status_code status;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001512
Sunil Ravic0f5d412024-09-11 22:12:49 +00001513 /* No candidate list and disassociation is not imminent */
1514
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001515 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) ||
1516 wpa_s->wnm_link_removal)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001517 status = WNM_BSS_TM_ACCEPT;
1518 else {
1519 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1520 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1521 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00001522
1523 if (reply)
1524 wnm_send_bss_transition_mgmt_resp(
1525 wpa_s, status,
1526 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1527 NULL);
1528
1529 goto reset;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001530 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00001531
1532 /*
1533 * Try fetching the latest scan results from the kernel.
1534 * This can help in finding more up-to-date information should
1535 * the driver have done some internal scanning operations after
1536 * the last scan result update in wpa_supplicant.
1537 *
1538 * It is not a new scan, this does not update the last_scan
1539 * timestamp nor will it expire old BSSs.
1540 */
1541 wpa_supplicant_update_scan_results(wpa_s, NULL);
1542 if (wnm_scan_process(wpa_s, true) > 0)
1543 return;
1544 wpa_printf(MSG_DEBUG,
1545 "WNM: No valid match in previous scan results - try a new scan");
1546
1547 /*
1548 * If we have a fixed BSSID configured, just reject at this point.
1549 * NOTE: We could actually check if we are allowed to stay (and we do
1550 * above if we have scan results available).
1551 */
1552 if (wpa_s->current_ssid && wpa_s->current_ssid->bssid_set) {
1553 wpa_printf(MSG_DEBUG, "WNM: Fixed BSSID, rejecting request");
1554
1555 if (reply)
1556 wnm_send_bss_transition_mgmt_resp(
1557 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1558 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1559 NULL);
1560
1561 goto reset;
1562 }
1563
1564 wnm_set_scan_freqs(wpa_s);
1565 if (num_valid_candidates == 1) {
1566 /* Any invalid candidate was sorted to the end */
1567 os_memcpy(wpa_s->next_scan_bssid,
1568 wpa_s->wnm_neighbor_report_elements[0].bssid,
1569 ETH_ALEN);
1570 wpa_printf(MSG_DEBUG,
1571 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1572 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1573 }
1574 wpa_s->wnm_transition_scan = true;
1575 wpa_supplicant_req_scan(wpa_s, 0, 0);
1576
1577 /* Continue from scan handler */
1578 return;
1579
1580reset:
1581 wnm_btm_reset(wpa_s);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001582}
1583
1584
Sunil Ravi99c035e2024-07-12 01:42:03 +00001585int wnm_btm_resp_tx_status(struct wpa_supplicant *wpa_s, const u8 *data,
1586 size_t data_len)
1587{
1588 const struct ieee80211_mgmt *frame =
1589 (const struct ieee80211_mgmt *) data;
1590
1591 if (data_len <
1592 IEEE80211_HDRLEN + sizeof(frame->u.action.u.bss_tm_resp) ||
1593 frame->u.action.category != WLAN_ACTION_WNM ||
1594 frame->u.action.u.bss_tm_resp.action != WNM_BSS_TRANS_MGMT_RESP ||
1595 frame->u.action.u.bss_tm_resp.status_code != WNM_BSS_TM_ACCEPT)
1596 return -1;
1597
1598 /*
1599 * If disassoc imminent bit was set in the request, the response may
1600 * indicate accept even if no candidate was found, so bail out here.
1601 */
1602 if (!wpa_s->wnm_target_bss) {
1603 wpa_printf(MSG_DEBUG, "WNM: Target BSS is not set");
1604 return 0;
1605 }
1606
1607 if (!wpa_s->current_ssid)
1608 return 0;
1609
1610 wnm_bss_tm_connect(wpa_s, wpa_s->wnm_target_bss, wpa_s->current_ssid,
1611 0);
1612
1613 wpa_s->wnm_target_bss = NULL;
1614 return 0;
1615}
1616
1617
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001618#define BTM_QUERY_MIN_SIZE 4
1619
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001620int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001621 u8 query_reason,
1622 const char *btm_candidates,
1623 int cand_list)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001624{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001625 struct wpabuf *buf;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001626 int ret;
1627
1628 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001629 MACSTR " query_reason=%u%s",
1630 MAC2STR(wpa_s->bssid), query_reason,
1631 cand_list ? " candidate list" : "");
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001632
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001633 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1634 if (!buf)
1635 return -1;
1636
1637 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1638 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1639 wpabuf_put_u8(buf, 1);
1640 wpabuf_put_u8(buf, query_reason);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001641
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001642 if (cand_list)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001643 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001644
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001645 if (btm_candidates) {
1646 const size_t max_len = 1000;
1647
1648 ret = wpabuf_resize(&buf, max_len);
1649 if (ret < 0) {
1650 wpabuf_free(buf);
1651 return ret;
1652 }
1653
1654 ret = ieee802_11_parse_candidate_list(btm_candidates,
1655 wpabuf_put(buf, 0),
1656 max_len);
1657 if (ret < 0) {
1658 wpabuf_free(buf);
1659 return ret;
1660 }
1661
1662 wpabuf_put(buf, ret);
1663 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001664
1665 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1666 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001667 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001668
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001669 wpabuf_free(buf);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001670 return ret;
1671}
1672
1673
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001674static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1675 const u8 *sa, const u8 *data,
1676 int len)
1677{
1678 const u8 *pos, *end, *next;
1679 u8 ie, ie_len;
1680
1681 pos = data;
1682 end = data + len;
1683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001684 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001685 ie = *pos++;
1686 ie_len = *pos++;
1687 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1688 ie, ie_len);
1689 if (ie_len > end - pos) {
1690 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1691 "subelement");
1692 break;
1693 }
1694 next = pos + ie_len;
1695 if (ie_len < 4) {
1696 pos = next;
1697 continue;
1698 }
1699 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1700 WPA_GET_BE24(pos), pos[3]);
1701
1702#ifdef CONFIG_HS20
1703 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1704 WPA_GET_BE24(pos) == OUI_WFA &&
1705 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1706 /* Subscription Remediation subelement */
1707 const u8 *ie_end;
1708 u8 url_len;
1709 char *url;
1710 u8 osu_method;
1711
1712 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1713 "subelement");
1714 ie_end = pos + ie_len;
1715 pos += 4;
1716 url_len = *pos++;
1717 if (url_len == 0) {
1718 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1719 url = NULL;
1720 osu_method = 1;
1721 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001722 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001723 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1724 url_len,
1725 (int) (ie_end - pos));
1726 break;
1727 }
1728 url = os_malloc(url_len + 1);
1729 if (url == NULL)
1730 break;
1731 os_memcpy(url, pos, url_len);
1732 url[url_len] = '\0';
1733 osu_method = pos[url_len];
1734 }
1735 hs20_rx_subscription_remediation(wpa_s, url,
1736 osu_method);
1737 os_free(url);
1738 pos = next;
1739 continue;
1740 }
1741
1742 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1743 WPA_GET_BE24(pos) == OUI_WFA &&
1744 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1745 const u8 *ie_end;
1746 u8 url_len;
1747 char *url;
1748 u8 code;
1749 u16 reauth_delay;
1750
1751 ie_end = pos + ie_len;
1752 pos += 4;
1753 code = *pos++;
1754 reauth_delay = WPA_GET_LE16(pos);
1755 pos += 2;
1756 url_len = *pos++;
1757 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1758 "Imminent - Reason Code %u "
1759 "Re-Auth Delay %u URL Length %u",
1760 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001761 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001762 break;
1763 url = os_malloc(url_len + 1);
1764 if (url == NULL)
1765 break;
1766 os_memcpy(url, pos, url_len);
1767 url[url_len] = '\0';
1768 hs20_rx_deauth_imminent_notice(wpa_s, code,
1769 reauth_delay, url);
1770 os_free(url);
1771 pos = next;
1772 continue;
1773 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001774
1775 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1776 WPA_GET_BE24(pos) == OUI_WFA &&
1777 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1778 const u8 *ie_end;
1779 u8 url_len;
1780 char *url;
1781
1782 ie_end = pos + ie_len;
1783 pos += 4;
1784 url_len = *pos++;
1785 wpa_printf(MSG_DEBUG,
1786 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1787 url_len);
1788 if (url_len > ie_end - pos)
1789 break;
1790 url = os_malloc(url_len + 1);
1791 if (!url)
1792 break;
1793 os_memcpy(url, pos, url_len);
1794 url[url_len] = '\0';
1795 hs20_rx_t_c_acceptance(wpa_s, url);
1796 os_free(url);
1797 pos = next;
1798 continue;
1799 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001800#endif /* CONFIG_HS20 */
1801
1802 pos = next;
1803 }
1804}
1805
1806
1807static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1808 const u8 *sa, const u8 *frm, int len)
1809{
1810 const u8 *pos, *end;
1811 u8 dialog_token, type;
1812
1813 /* Dialog Token [1] | Type [1] | Subelements */
1814
1815 if (len < 2 || sa == NULL)
1816 return;
1817 end = frm + len;
1818 pos = frm;
1819 dialog_token = *pos++;
1820 type = *pos++;
1821
1822 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1823 "(dialog_token %u type %u sa " MACSTR ")",
1824 dialog_token, type, MAC2STR(sa));
1825 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1826 pos, end - pos);
1827
1828 if (wpa_s->wpa_state != WPA_COMPLETED ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001829 (!ether_addr_equal(sa, wpa_s->bssid) &&
1830 (!wpa_s->valid_links ||
1831 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001832 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1833 "from our AP - ignore it");
1834 return;
1835 }
1836
1837 switch (type) {
1838 case 1:
1839 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1840 break;
1841 default:
1842 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1843 "WNM-Notification type %u", type);
1844 break;
1845 }
1846}
1847
1848
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001849static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1850 const u8 *sa, const u8 *frm,
1851 int len)
1852{
1853 u8 dialog_token, req_info, auto_report, timeout;
1854
1855 if (!wpa_s->conf->coloc_intf_reporting)
1856 return;
1857
1858 /* Dialog Token [1] | Request Info [1] */
1859
1860 if (len < 2)
1861 return;
1862 dialog_token = frm[0];
1863 req_info = frm[1];
1864 auto_report = req_info & 0x03;
1865 timeout = req_info >> 2;
1866
1867 wpa_dbg(wpa_s, MSG_DEBUG,
1868 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1869 dialog_token, auto_report, timeout, MAC2STR(sa));
1870
1871 if (dialog_token == 0)
1872 return; /* only nonzero values are used for request */
1873
1874 if (wpa_s->wpa_state != WPA_COMPLETED ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001875 (!ether_addr_equal(sa, wpa_s->bssid) &&
1876 (!wpa_s->valid_links ||
1877 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001878 wpa_dbg(wpa_s, MSG_DEBUG,
1879 "WNM: Collocated Interference Request frame not from current AP - ignore it");
1880 return;
1881 }
1882
1883 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1884 dialog_token, auto_report, timeout);
1885 wpa_s->coloc_intf_dialog_token = dialog_token;
1886 wpa_s->coloc_intf_auto_report = auto_report;
1887 wpa_s->coloc_intf_timeout = timeout;
1888}
1889
1890
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001891void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001892 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001893{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001894 const u8 *pos, *end;
1895 u8 act;
1896
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001897 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001898 return;
1899
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001900 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001901 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001902 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001903
1904 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001905 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001906 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001907 (!ether_addr_equal(mgmt->sa, wpa_s->bssid) &&
1908 (!wpa_s->valid_links ||
1909 !ether_addr_equal(mgmt->sa, wpa_s->ap_mld_addr)))) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001910 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1911 "frame");
1912 return;
1913 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001914
1915 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001916 case WNM_BSS_TRANS_MGMT_REQ:
1917 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001918 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001919 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001920 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001921 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001922 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001923 case WNM_NOTIFICATION_REQ:
1924 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1925 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001926 case WNM_COLLOCATED_INTERFERENCE_REQ:
1927 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
1928 end - pos);
1929 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001930 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001931 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001932 break;
1933 }
1934}
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001935
1936
1937int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
1938 const struct wpabuf *elems)
1939{
1940 struct wpabuf *buf;
1941 int ret;
1942
1943 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
1944 return -1;
1945
1946 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
1947 MACSTR " (dialog token %u)",
1948 MAC2STR(wpa_s->bssid), dialog_token);
1949
1950 buf = wpabuf_alloc(3 + wpabuf_len(elems));
1951 if (!buf)
1952 return -1;
1953
1954 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1955 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
1956 wpabuf_put_u8(buf, dialog_token);
1957 wpabuf_put_buf(buf, elems);
1958
1959 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1960 wpa_s->own_addr, wpa_s->bssid,
1961 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1962 wpabuf_free(buf);
1963 return ret;
1964}
1965
1966
1967void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
1968 struct wpabuf *elems)
1969{
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001970 if (elems && wpabuf_len(elems) == 0) {
1971 wpabuf_free(elems);
1972 elems = NULL;
1973 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001974
Sunil Ravi99c035e2024-07-12 01:42:03 +00001975 /* NOTE: The elements are not stored as they are only send out once */
1976
1977 if (wpa_s->conf->coloc_intf_reporting && elems &&
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001978 wpa_s->coloc_intf_dialog_token &&
1979 (wpa_s->coloc_intf_auto_report == 1 ||
1980 wpa_s->coloc_intf_auto_report == 3)) {
1981 /* TODO: Check that there has not been less than
1982 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
1983 */
1984 wnm_send_coloc_intf_report(wpa_s,
1985 wpa_s->coloc_intf_dialog_token,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001986 elems);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001987 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00001988
1989 wpabuf_free(elems);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001990}
1991
1992
1993void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
1994{
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001995 wpa_s->coloc_intf_dialog_token = 0;
1996 wpa_s->coloc_intf_auto_report = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001997}
1998
1999
2000bool wnm_is_bss_excluded(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2001{
Sunil Ravic0f5d412024-09-11 22:12:49 +00002002 int i;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002003
2004 /*
2005 * In case disassociation imminent is set, do no try to use a BSS to
2006 * which we are connected.
2007 */
Sunil Ravic0f5d412024-09-11 22:12:49 +00002008 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
2009 if (!wpa_s->wnm_disassoc_mld) {
2010 if (ether_addr_equal(bss->bssid,
2011 wpa_s->wnm_disassoc_addr))
2012 return true;
2013 } else {
2014 if (ether_addr_equal(bss->mld_addr,
2015 wpa_s->wnm_disassoc_addr))
2016 return true;
2017 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002018 }
2019
Sunil Ravic0f5d412024-09-11 22:12:49 +00002020 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
2021 struct neighbor_report *nei;
2022
2023 nei = &wpa_s->wnm_neighbor_report_elements[i];
2024 if (!ether_addr_equal(nei->bssid, bss->bssid))
2025 continue;
2026
2027 if (nei->preference_present && nei->preference == 0)
2028 return true;
2029
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002030#ifdef CONFIG_MBO
2031 if (nei->drv_mbo_reject)
2032 return true;
2033#endif /* CONFIG_MBO */
2034
Sunil Ravic0f5d412024-09-11 22:12:49 +00002035 break;
2036 }
2037
2038 /* If the abridged bit is set, the BSS must be a known neighbor. */
2039 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ABRIDGED) &&
2040 wpa_s->wnm_num_neighbor_report == i)
2041 return true;
2042
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002043 return false;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002044}