blob: 3bb621db8ab4ab789b5be312e6ccdfcfcca847d7 [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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700617static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700618{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700619 unsigned int i;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700620
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700621 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
622 wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
623}
624
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700625#ifdef CONFIG_MBO
626static struct wpa_bss *
627get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
628 enum mbo_transition_reject_reason *reason)
629{
630 struct wpa_bss *target = NULL;
631 struct wpa_bss_trans_info params;
632 struct wpa_bss_candidate_info *info = NULL;
633 struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
634 u8 *first_candidate_bssid = NULL, *pos;
635 unsigned int i;
636
637 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
638 params.n_candidates = 0;
639 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
640 if (!params.bssid)
641 return NULL;
642
643 pos = params.bssid;
644 for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
645 if (nei->is_first)
646 first_candidate_bssid = nei->bssid;
647 if (!nei->acceptable)
648 continue;
649 os_memcpy(pos, nei->bssid, ETH_ALEN);
650 pos += ETH_ALEN;
651 params.n_candidates++;
652 }
653
654 if (!params.n_candidates)
655 goto end;
656
657 info = wpa_drv_get_bss_trans_status(wpa_s, &params);
658 if (!info) {
659 /* If failed to get candidate BSS transition status from driver,
660 * get the first acceptable candidate from wpa_supplicant.
661 */
662 target = wpa_bss_get_bssid(wpa_s, params.bssid);
663 goto end;
664 }
665
666 /* Get the first acceptable candidate from driver */
667 for (i = 0; i < info->num; i++) {
668 if (info->candidates[i].is_accept) {
669 target = wpa_bss_get_bssid(wpa_s,
670 info->candidates[i].bssid);
671 goto end;
672 }
673 }
674
675 /* If Disassociation Imminent is set and driver rejects all the
676 * candidate select first acceptable candidate which has
677 * rssi > disassoc_imminent_rssi_threshold
678 */
679 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
680 for (i = 0; i < info->num; i++) {
681 target = wpa_bss_get_bssid(wpa_s,
682 info->candidates[i].bssid);
683 if (target &&
684 (target->level <
685 wpa_s->conf->disassoc_imminent_rssi_threshold))
686 continue;
687 goto end;
688 }
689 }
690
691 /* While sending BTM reject use reason code of the first candidate
692 * received in BTM request frame
693 */
694 if (reason) {
695 for (i = 0; i < info->num; i++) {
696 if (first_candidate_bssid &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000697 ether_addr_equal(first_candidate_bssid,
698 info->candidates[i].bssid)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700699 *reason = info->candidates[i].reject_reason;
700 break;
701 }
702 }
703 }
704
705 target = NULL;
706
707end:
708 os_free(params.bssid);
709 if (info) {
710 os_free(info->candidates);
711 os_free(info);
712 }
713 return target;
714}
715#endif /* CONFIG_MBO */
716
717
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000718static struct wpa_bss * find_better_target(struct wpa_bss *a,
719 struct wpa_bss *b)
720{
721 if (!a)
722 return b;
723 if (!b)
724 return a;
725
726 if (a->est_throughput > b->est_throughput) {
727 wpa_printf(MSG_DEBUG, "WNM: A is better: " MACSTR
728 " est-tput: %d B: " MACSTR " est-tput: %d",
729 MAC2STR(a->bssid), a->est_throughput,
730 MAC2STR(b->bssid), b->est_throughput);
731 return a;
732 }
733
734 wpa_printf(MSG_DEBUG, "WNM: B is better, A: " MACSTR
735 " est-tput: %d B: " MACSTR " est-tput: %d",
736 MAC2STR(a->bssid), a->est_throughput,
737 MAC2STR(b->bssid), b->est_throughput);
738 return b;
739}
740
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700741static struct wpa_bss *
Sunil Ravic0f5d412024-09-11 22:12:49 +0000742compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700743 enum mbo_transition_reject_reason *reason)
744{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800745 u8 i;
746 struct wpa_bss *bss = wpa_s->current_bss;
747 struct wpa_bss *target;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000748 struct wpa_bss *best_target = NULL;
749 struct wpa_bss *bss_in_list = NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700750
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800751 if (!bss)
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700752 return NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700753
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800754 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800755 MAC2STR(wpa_s->bssid), bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800756
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700757 wnm_clear_acceptable(wpa_s);
758
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800759 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
760 struct neighbor_report *nei;
761
762 nei = &wpa_s->wnm_neighbor_report_elements[i];
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800763
764 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
765 if (!target) {
766 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
767 " (pref %d) not found in scan results",
768 MAC2STR(nei->bssid),
769 nei->preference_present ? nei->preference :
770 -1);
771 continue;
772 }
773
Sunil Ravi99c035e2024-07-12 01:42:03 +0000774 /*
775 * TODO: Could consider allowing transition to another ESS if
776 * PMF was enabled for the association.
777 */
778 if (!wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800779 1, 0)) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700780 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
781 " (pref %d) does not match the current network profile",
782 MAC2STR(nei->bssid),
783 nei->preference_present ? nei->preference :
784 -1);
785 continue;
786 }
787
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800788 if (target->level < bss->level && target->level < -80) {
789 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
790 " (pref %d) does not have sufficient signal level (%d)",
791 MAC2STR(nei->bssid),
792 nei->preference_present ? nei->preference :
793 -1,
794 target->level);
795 continue;
796 }
797
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700798 nei->acceptable = 1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000799
800 best_target = find_better_target(target, best_target);
801 if (target == bss)
802 bss_in_list = bss;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700803 }
804
805#ifdef CONFIG_MBO
806 if (wpa_s->wnm_mbo_trans_reason_present)
807 target = get_mbo_transition_candidate(wpa_s, reason);
808 else
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000809 target = best_target;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700810#else /* CONFIG_MBO */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000811 target = best_target;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700812#endif /* CONFIG_MBO */
813
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000814 if (!target)
815 return NULL;
816
817 wpa_printf(MSG_DEBUG,
818 "WNM: Found an acceptable preferred transition candidate BSS "
819 MACSTR " (RSSI %d, tput: %d bss-tput: %d)",
820 MAC2STR(target->bssid), target->level,
821 target->est_throughput, bss->est_throughput);
822
823 if (!bss_in_list)
824 return target;
825
826 if ((!target->est_throughput && !bss_in_list->est_throughput) ||
827 (target->est_throughput > bss_in_list->est_throughput &&
828 target->est_throughput - bss_in_list->est_throughput >
829 bss_in_list->est_throughput >> 4)) {
830 /* It is more than 100/16 percent better, so switch. */
831 return target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700832 }
833
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000834 wpa_printf(MSG_DEBUG,
835 "WNM: Stay with our current BSS, not enough change in estimated throughput to switch");
836 return bss_in_list;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700837}
838
839
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800840static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
841{
842 const u8 *ie_a, *ie_b;
843
844 if (!a || !b)
845 return 0;
846
847 ie_a = wpa_bss_get_ie(a, eid);
848 ie_b = wpa_bss_get_ie(b, eid);
849
850 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
851 return 0;
852
853 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
854}
855
856
857static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
858{
859 u32 info = 0;
860
861 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
862
863 /*
864 * Leave the security and key scope bits unset to indicate that the
865 * security information is not available.
866 */
867
868 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
869 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
870 if (bss->caps & WLAN_CAPABILITY_QOS)
871 info |= NEI_REP_BSSID_INFO_QOS;
872 if (bss->caps & WLAN_CAPABILITY_APSD)
873 info |= NEI_REP_BSSID_INFO_APSD;
874 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
875 info |= NEI_REP_BSSID_INFO_RM;
876 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
877 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
878 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
879 info |= NEI_REP_BSSID_INFO_IMM_BA;
880 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
881 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
882 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
883 info |= NEI_REP_BSSID_INFO_HT;
884
885 return info;
886}
887
888
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700889static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
890 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
891 u8 pref)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800892{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700893 if (wpabuf_len(*buf) + 18 >
894 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800895 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700896 "WNM: No room in frame for Neighbor Report element");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800897 return -1;
898 }
899
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700900 if (wpabuf_resize(buf, 18) < 0) {
901 wpa_printf(MSG_DEBUG,
902 "WNM: Failed to allocate memory for Neighbor Report element");
903 return -1;
904 }
905
906 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800907 /* length: 13 for basic neighbor report + 3 for preference subelement */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700908 wpabuf_put_u8(*buf, 16);
909 wpabuf_put_data(*buf, bssid, ETH_ALEN);
910 wpabuf_put_le32(*buf, bss_info);
911 wpabuf_put_u8(*buf, op_class);
912 wpabuf_put_u8(*buf, chan);
913 wpabuf_put_u8(*buf, phy_type);
914 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
915 wpabuf_put_u8(*buf, 1);
916 wpabuf_put_u8(*buf, pref);
917 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800918}
919
920
921static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700922 struct wpa_bss *bss, struct wpabuf **buf,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800923 u8 pref)
924{
925 const u8 *ie;
926 u8 op_class, chan;
Sunil8cd6f4d2022-06-28 18:40:46 +0000927 int sec_chan = 0;
928 enum oper_chan_width vht = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800929 enum phy_type phy_type;
930 u32 info;
931 struct ieee80211_ht_operation *ht_oper = NULL;
932 struct ieee80211_vht_operation *vht_oper = NULL;
933
934 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
935 if (ie && ie[1] >= 2) {
936 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
937
938 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
939 sec_chan = 1;
940 else if (ht_oper->ht_param &
941 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
942 sec_chan = -1;
943 }
944
945 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
946 if (ie && ie[1] >= 1) {
947 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
948
Hai Shalom81f62d82019-07-22 12:10:00 -0700949 if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
950 vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
951 vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800952 vht = vht_oper->vht_op_info_chwidth;
953 }
954
955 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
956 &chan) == NUM_HOSTAPD_MODES) {
957 wpa_printf(MSG_DEBUG,
958 "WNM: Cannot determine operating class and channel");
959 return -2;
960 }
961
962 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
963 (vht_oper != NULL));
964 if (phy_type == PHY_TYPE_UNSPECIFIED) {
965 wpa_printf(MSG_DEBUG,
966 "WNM: Cannot determine BSS phy type for Neighbor Report");
967 return -2;
968 }
969
970 info = wnm_get_bss_info(wpa_s, bss);
971
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700972 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
973 pref);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800974}
975
976
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700977static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800978{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800979 unsigned int i, pref = 255;
980 struct os_reltime now;
981 struct wpa_ssid *ssid = wpa_s->current_ssid;
982
983 if (!ssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700984 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800985
986 /*
987 * TODO: Define when scan results are no longer valid for the candidate
988 * list.
989 */
990 os_get_reltime(&now);
991 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700992 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800993
994 wpa_printf(MSG_DEBUG,
995 "WNM: Add candidate list to BSS Transition Management Response frame");
996 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
997 struct wpa_bss *bss = wpa_s->last_scan_res[i];
998 int res;
999
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001000 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001001 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001002 if (res == -2)
1003 continue; /* could not build entry for BSS */
1004 if (res < 0)
1005 break; /* no more room for candidates */
1006 if (pref == 1)
1007 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001008 }
1009 }
1010
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001011 wpa_hexdump_buf(MSG_DEBUG,
1012 "WNM: BSS Transition Management Response candidate list",
1013 *buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001014}
1015
1016
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001017#define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
1018
Sunil Ravi99c035e2024-07-12 01:42:03 +00001019static int wnm_send_bss_transition_mgmt_resp(
1020 struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001021 enum bss_trans_mgmt_status_code status,
1022 enum mbo_transition_reject_reason reason,
1023 u8 delay, const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001024{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001025 struct wpabuf *buf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001026 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001027
Sunil Ravi99c035e2024-07-12 01:42:03 +00001028 wpa_s->wnm_reply = 0;
1029
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001030 wpa_printf(MSG_DEBUG,
1031 "WNM: Send BSS Transition Management Response to " MACSTR
1032 " dialog_token=%u status=%u reason=%u delay=%d",
Sunil Ravi99c035e2024-07-12 01:42:03 +00001033 MAC2STR(wpa_s->bssid), wpa_s->wnm_dialog_token, status,
1034 reason, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001035 if (!wpa_s->current_bss) {
1036 wpa_printf(MSG_DEBUG,
1037 "WNM: Current BSS not known - drop response");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001038 return -1;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001039 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001040
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001041 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
1042 if (!buf) {
1043 wpa_printf(MSG_DEBUG,
1044 "WNM: Failed to allocate memory for BTM response");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001045 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001046 }
1047
Hai Shalom74f70d42019-02-11 14:42:39 -08001048 wpa_s->bss_tm_status = status;
1049 wpas_notify_bss_tm_status(wpa_s);
1050
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001051 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1052 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001053 wpabuf_put_u8(buf, wpa_s->wnm_dialog_token);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001054 wpabuf_put_u8(buf, status);
1055 wpabuf_put_u8(buf, delay);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001056 if (target_bssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001057 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001058 } else if (status == WNM_BSS_TM_ACCEPT) {
1059 /*
1060 * P802.11-REVmc clarifies that the Target BSSID field is always
1061 * present when status code is zero, so use a fake value here if
1062 * no BSSID is yet known.
1063 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001064 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001065 }
1066
Sunil Ravi99c035e2024-07-12 01:42:03 +00001067 if (status == WNM_BSS_TM_ACCEPT && target_bssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001068 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001069
1070#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001071 if (status != WNM_BSS_TM_ACCEPT &&
1072 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
1073 u8 mbo[10];
1074 size_t ret;
1075
1076 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
1077 reason);
1078 if (ret) {
1079 if (wpabuf_resize(&buf, ret) < 0) {
1080 wpabuf_free(buf);
1081 wpa_printf(MSG_DEBUG,
1082 "WNM: Failed to allocate memory for MBO IE");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001083 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001084 }
1085
1086 wpabuf_put_data(buf, mbo, ret);
1087 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001088 }
1089#endif /* CONFIG_MBO */
1090
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001091 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1092 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001093 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001094 if (res < 0) {
1095 wpa_printf(MSG_DEBUG,
1096 "WNM: Failed to send BSS Transition Management Response");
1097 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001098
1099 wpabuf_free(buf);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001100
1101 return res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001102}
1103
1104
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001105static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
1106 struct wpa_bss *bss, struct wpa_ssid *ssid,
1107 int after_new_scan)
1108{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001109 struct wpa_radio_work *already_connecting;
1110
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001111 wpa_dbg(wpa_s, MSG_DEBUG,
1112 "WNM: Transition to BSS " MACSTR
1113 " based on BSS Transition Management Request (old BSSID "
1114 MACSTR " after_new_scan=%d)",
1115 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1116
1117 /* Send the BSS Management Response - Accept */
1118 if (wpa_s->wnm_reply) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001119 wpa_s->wnm_target_bss = bss;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001120 wpa_printf(MSG_DEBUG,
1121 "WNM: Sending successful BSS Transition Management Response");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001122
1123 /* This function will be called again from the TX handler to
1124 * start the actual reassociation after this response has been
1125 * delivered to the current AP. */
1126 if (wnm_send_bss_transition_mgmt_resp(
1127 wpa_s, WNM_BSS_TM_ACCEPT,
1128 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1129 bss->bssid) >= 0)
1130 return;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001131 }
1132
1133 if (bss == wpa_s->current_bss) {
1134 wpa_printf(MSG_DEBUG,
1135 "WNM: Already associated with the preferred candidate");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001136 wnm_btm_reset(wpa_s);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001137 return;
1138 }
1139
Hai Shaloma20dcd72022-02-04 13:43:00 -08001140 already_connecting = radio_work_pending(wpa_s, "sme-connect");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001141 wpa_s->reassociate = 1;
1142 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1143 wpa_supplicant_connect(wpa_s, bss, ssid);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001144
1145 /*
1146 * Indicate that a BSS transition is in progress so scan results that
1147 * come in before the 'sme-connect' radio work gets executed do not
1148 * override the original connection attempt.
1149 */
1150 if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1151 wpa_s->bss_trans_mgmt_in_progress = true;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001152}
1153
1154
Sunil Ravi99c035e2024-07-12 01:42:03 +00001155int wnm_scan_process(struct wpa_supplicant *wpa_s, bool pre_scan_check)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001156{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001157 struct wpa_bss *bss;
1158 struct wpa_ssid *ssid = wpa_s->current_ssid;
1159 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001160 enum mbo_transition_reject_reason reason =
1161 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001162
Sunil Ravi99c035e2024-07-12 01:42:03 +00001163 if (!wpa_s->wnm_dialog_token)
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001164 return 0;
1165
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001166 wpa_dbg(wpa_s, MSG_DEBUG,
1167 "WNM: Process scan results for BSS Transition Management");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001168 if (!pre_scan_check &&
1169 os_reltime_initialized(&wpa_s->wnm_cand_valid_until) &&
1170 os_reltime_before(&wpa_s->wnm_cand_valid_until,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001171 &wpa_s->scan_trigger_time)) {
1172 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
Sunil Ravi99c035e2024-07-12 01:42:03 +00001173 goto send_bss_resp_fail;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001174 }
1175
Sunil Ravic0f5d412024-09-11 22:12:49 +00001176 if (!pre_scan_check && !wpa_s->wnm_transition_scan)
1177 return 0;
1178
1179 wpa_s->wnm_transition_scan = false;
1180
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001181 /* Compare the Neighbor Report and scan results */
Sunil Ravic0f5d412024-09-11 22:12:49 +00001182 bss = compare_scan_neighbor_results(wpa_s, &reason);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001183
1184 /*
1185 * If this is a pre-scan check, returning 0 will trigger a scan and
1186 * another call. In that case, reject "bad" candidates in the hope of
1187 * finding a better candidate after scanning.
1188 *
1189 * Use a simple heuristic to check whether the selection is reasonable
1190 * or a scan is a good idea. For that, we need to have found a
1191 * candidate BSS (which might be the current one), it is up-to-date,
1192 * and we don't want to immediately roam back again.
1193 */
1194 if (pre_scan_check) {
1195 struct os_reltime age;
1196
1197 if (!bss)
1198 return 0;
1199
1200 os_reltime_age(&bss->last_update, &age);
1201 if (age.sec >= 10)
1202 return 0;
1203
1204#ifndef CONFIG_NO_ROAMING
1205 if (wpa_s->current_bss && bss != wpa_s->current_bss &&
1206 wpa_supplicant_need_to_roam_within_ess(wpa_s,
1207 wpa_s->current_bss,
1208 bss))
1209 return 0;
1210#endif /* CONFIG_NO_ROAMING */
1211 }
1212
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001213 if (!bss) {
1214 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1215 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1216 goto send_bss_resp_fail;
1217 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001218
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001219 /* Associate to the network */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001220 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001221 return 1;
1222
1223send_bss_resp_fail:
Sunil Ravic0f5d412024-09-11 22:12:49 +00001224 if (wpa_s->wnm_reply) {
1225 /* If disassoc imminent is set, we must not reject */
1226 if (wpa_s->wnm_mode &
1227 (WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
1228 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)) {
1229 wpa_printf(MSG_DEBUG,
1230 "WNM: Accept BTM request because disassociation imminent bit is set");
1231 status = WNM_BSS_TM_ACCEPT;
1232 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001233
Sunil Ravi99c035e2024-07-12 01:42:03 +00001234 wnm_send_bss_transition_mgmt_resp(wpa_s, status, reason,
1235 0, NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001236 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00001237
1238 wnm_btm_reset(wpa_s);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001239
1240 return 0;
1241}
1242
1243
1244static int cand_pref_compar(const void *a, const void *b)
1245{
1246 const struct neighbor_report *aa = a;
1247 const struct neighbor_report *bb = b;
1248
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001249 if (aa->disassoc_imminent && !bb->disassoc_imminent)
1250 return 1;
1251 if (bb->disassoc_imminent && !aa->disassoc_imminent)
1252 return -1;
1253
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001254 if (!aa->preference_present && !bb->preference_present)
1255 return 0;
1256 if (!aa->preference_present)
1257 return 1;
1258 if (!bb->preference_present)
1259 return -1;
1260 if (bb->preference > aa->preference)
1261 return 1;
1262 if (bb->preference < aa->preference)
1263 return -1;
1264 return 0;
1265}
1266
1267
1268static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1269{
1270 if (!wpa_s->wnm_neighbor_report_elements)
1271 return;
1272 qsort(wpa_s->wnm_neighbor_report_elements,
1273 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1274 cand_pref_compar);
1275}
1276
1277
1278static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1279{
1280 unsigned int i;
1281
1282 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1283 if (!wpa_s->wnm_neighbor_report_elements)
1284 return;
1285 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1286 struct neighbor_report *nei;
1287
1288 nei = &wpa_s->wnm_neighbor_report_elements[i];
1289 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1290 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1291 i, MAC2STR(nei->bssid), nei->bssid_info,
1292 nei->regulatory_class,
1293 nei->channel_number, nei->phy_type,
1294 nei->preference_present ? nei->preference : -1,
1295 nei->freq);
1296 }
1297}
1298
1299
1300static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1301{
1302 unsigned int i;
1303
1304 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1305 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1306 int j;
1307
1308 for (j = 0; j < mode->num_channels; j++) {
1309 struct hostapd_channel_data *chan;
1310
1311 chan = &mode->channels[j];
1312 if (chan->freq == freq &&
1313 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1314 return 1;
1315 }
1316 }
1317
1318 return 0;
1319}
1320
1321
1322static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1323{
1324 int *freqs;
1325 int num_freqs = 0;
1326 unsigned int i;
1327
1328 if (!wpa_s->wnm_neighbor_report_elements)
1329 return;
1330
1331 if (wpa_s->hw.modes == NULL)
1332 return;
1333
1334 os_free(wpa_s->next_scan_freqs);
1335 wpa_s->next_scan_freqs = NULL;
1336
1337 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1338 if (freqs == NULL)
1339 return;
1340
1341 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1342 struct neighbor_report *nei;
1343
1344 nei = &wpa_s->wnm_neighbor_report_elements[i];
Sunil Ravi99c035e2024-07-12 01:42:03 +00001345
1346 if (nei->preference_present && nei->preference == 0)
1347 continue;
1348
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001349 if (nei->freq <= 0) {
1350 wpa_printf(MSG_DEBUG,
1351 "WNM: Unknown neighbor operating frequency for "
1352 MACSTR " - scan all channels",
1353 MAC2STR(nei->bssid));
1354 os_free(freqs);
1355 return;
1356 }
1357 if (chan_supported(wpa_s, nei->freq))
1358 add_freq(freqs, &num_freqs, nei->freq);
1359 }
1360
1361 if (num_freqs == 0) {
1362 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001363 return;
1364 }
1365
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001366 wpa_printf(MSG_DEBUG,
1367 "WNM: Scan %d frequencies based on transition candidate list",
1368 num_freqs);
1369 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001370}
1371
1372
Sunil Ravic0f5d412024-09-11 22:12:49 +00001373static int wnm_parse_candidate_list(struct wpa_supplicant *wpa_s,
1374 const u8 *pos, const u8 *end,
1375 int *num_valid_candidates)
1376{
1377 *num_valid_candidates = 0;
1378
1379 while (end - pos >= 2 &&
1380 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT) {
1381 u8 tag = *pos++;
1382 u8 len = *pos++;
1383
1384 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u", tag);
1385 if (len > end - pos) {
1386 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1387 return -1;
1388 }
1389 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1390 struct neighbor_report *rep;
1391
1392 if (!wpa_s->wnm_num_neighbor_report) {
1393 wpa_s->wnm_neighbor_report_elements = os_calloc(
1394 WNM_MAX_NEIGHBOR_REPORT,
1395 sizeof(struct neighbor_report));
1396 if (!wpa_s->wnm_neighbor_report_elements)
1397 return -1;
1398 }
1399
1400 rep = &wpa_s->wnm_neighbor_report_elements[
1401 wpa_s->wnm_num_neighbor_report];
1402 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
1403 if ((wpa_s->wnm_mode &
1404 WNM_BSS_TM_REQ_DISASSOC_IMMINENT) &&
1405 ether_addr_equal(rep->bssid, wpa_s->bssid))
1406 rep->disassoc_imminent = 1;
1407
1408 if (rep->preference_present && rep->preference)
1409 *num_valid_candidates += 1;
1410
1411 wpa_s->wnm_num_neighbor_report++;
1412#ifdef CONFIG_MBO
1413 if (wpa_s->wnm_mbo_trans_reason_present &&
1414 wpa_s->wnm_num_neighbor_report == 1) {
1415 rep->is_first = 1;
1416 wpa_printf(MSG_DEBUG,
1417 "WNM: First transition candidate is "
1418 MACSTR, MAC2STR(rep->bssid));
1419 }
1420#endif /* CONFIG_MBO */
1421 }
1422
1423 pos += len;
1424 }
1425
1426 return 0;
1427}
1428
1429
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001430static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1431 const u8 *pos, const u8 *end,
1432 int reply)
1433{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001434 unsigned int beacon_int;
1435 u8 valid_int;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001436#ifdef CONFIG_MBO
1437 const u8 *vendor;
1438#endif /* CONFIG_MBO */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001439 bool disassoc_imminent;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001440 int num_valid_candidates;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001441
Hai Shalomc3565922019-10-28 11:58:20 -07001442 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
Hai Shalom81f62d82019-07-22 12:10:00 -07001443 return;
1444
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001445 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001446 return;
1447
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001448#ifdef CONFIG_MBO
Sunil Ravi4018d712019-12-06 18:01:21 -08001449 wpa_s->wnm_mbo_cell_pref_present = 0;
1450 wpa_s->wnm_mbo_cell_preference = 0;
1451 wpa_s->wnm_mbo_assoc_retry_delay_present = 0;
1452 wpa_s->wnm_mbo_assoc_retry_delay_sec = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001453#endif /* CONFIG_MBO */
1454
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001455 if (wpa_s->current_bss)
1456 beacon_int = wpa_s->current_bss->beacon_int;
1457 else
1458 beacon_int = 100; /* best guess */
1459
Sunil Ravi99c035e2024-07-12 01:42:03 +00001460 wnm_btm_reset(wpa_s);
1461
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001462 wpa_s->wnm_dialog_token = pos[0];
1463 wpa_s->wnm_mode = pos[1];
Sunil Ravic0f5d412024-09-11 22:12:49 +00001464 wpa_s->wnm_disassoc_timer = WPA_GET_LE16(pos + 2);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001465 wpa_s->wnm_link_removal = false;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001466 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001467 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001468
1469 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1470 "dialog_token=%u request_mode=0x%x "
1471 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001472 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Sunil Ravic0f5d412024-09-11 22:12:49 +00001473 wpa_s->wnm_disassoc_timer, valid_int);
1474
1475 if (!wpa_s->wnm_dialog_token) {
1476 wpa_printf(MSG_DEBUG, "WNM: Invalid dialog token");
1477 goto reset;
1478 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001479
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001480#if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1481 if (wpa_s->reject_btm_req_reason) {
1482 wpa_printf(MSG_INFO,
1483 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1484 wpa_s->reject_btm_req_reason);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001485 wnm_send_bss_transition_mgmt_resp(
Sunil Ravi99c035e2024-07-12 01:42:03 +00001486 wpa_s, wpa_s->reject_btm_req_reason,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001487 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001488 goto reset;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001489 }
1490#endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1491
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001492 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001493
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001494 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001495 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001496 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
Sunil Ravic0f5d412024-09-11 22:12:49 +00001497 goto reset;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001498 }
1499 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001500 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001501 }
1502
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001503 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001504 char url[256];
Sunil8cd6f4d2022-06-28 18:40:46 +00001505 u8 url_len;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001506
Sunil8cd6f4d2022-06-28 18:40:46 +00001507 if (end - pos < 1) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001508 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1509 "Management Request (URL)");
Sunil Ravic0f5d412024-09-11 22:12:49 +00001510 goto reset;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001511 }
Sunil8cd6f4d2022-06-28 18:40:46 +00001512 url_len = *pos++;
1513 if (url_len > end - pos) {
1514 wpa_printf(MSG_DEBUG,
1515 "WNM: Invalid BSS Transition Management Request (URL truncated)");
Sunil Ravic0f5d412024-09-11 22:12:49 +00001516 goto reset;
Sunil8cd6f4d2022-06-28 18:40:46 +00001517 }
1518 os_memcpy(url, pos, url_len);
1519 url[url_len] = '\0';
1520 pos += url_len;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001521
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001522 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1523 wpa_sm_pmf_enabled(wpa_s->wpa),
Sunil Ravic0f5d412024-09-11 22:12:49 +00001524 wpa_s->wnm_disassoc_timer * beacon_int * 128 / 125,
1525 url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001526 }
1527
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001528#ifdef CONFIG_MBO
1529 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1530 if (vendor) {
1531 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001532 }
1533#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07001534 if (wpa_s->conf->btm_offload) {
1535 wpa_printf(MSG_INFO,
1536 "WNM: BTM offload enabled. Notify status and return");
1537 wpa_s->bss_tm_status = WNM_BSS_TM_ACCEPT;
1538 wpas_notify_bss_tm_status(wpa_s);
1539 return;
1540 }
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001541
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001542 disassoc_imminent = wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1543
1544 /*
1545 * Based on IEEE P802.11be/D5.0, when a station is a non-AP MLD with
1546 * more than one affiliated link, the Link Removal Imminent field is
1547 * set to 1, and the BSS Termination Included field is set to 1, only
1548 * one of the links is removed and the other links remain associated.
1549 * Ignore the Disassociation Imminent field in such a case.
Sunil Ravi99c035e2024-07-12 01:42:03 +00001550 *
1551 * TODO: We should check if the AP has more than one link.
1552 * TODO: We should pass the RX link and use that
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001553 */
Sunil Ravi99c035e2024-07-12 01:42:03 +00001554 if (disassoc_imminent && wpa_s->valid_links &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001555 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_LINK_REMOVAL_IMMINENT) &&
1556 (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED)) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001557 /* If we still have a link, then just accept the request */
1558 if (wpa_s->valid_links & (wpa_s->valid_links - 1)) {
1559 wpa_printf(MSG_INFO,
1560 "WNM: BTM request for a single MLO link - ignore disassociation imminent since other links remain associated");
1561 disassoc_imminent = false;
1562
1563 wnm_send_bss_transition_mgmt_resp(
1564 wpa_s, WNM_BSS_TM_ACCEPT, 0, 0, NULL);
1565
Sunil Ravic0f5d412024-09-11 22:12:49 +00001566 goto reset;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001567 }
1568
1569 /* The last link is being removed (which must be the assoc link)
1570 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001571 wpa_s->wnm_link_removal = true;
Sunil Ravic0f5d412024-09-11 22:12:49 +00001572 wpa_s->wnm_disassoc_mld = false;
1573 os_memcpy(wpa_s->wnm_disassoc_addr,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001574 wpa_s->links[wpa_s->mlo_assoc_link_id].bssid,
1575 ETH_ALEN);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001576 } else if (wpa_s->valid_links) {
1577 wpa_s->wnm_disassoc_mld = true;
1578 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->ap_mld_addr,
1579 ETH_ALEN);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001580 } else {
Sunil Ravic0f5d412024-09-11 22:12:49 +00001581 wpa_s->wnm_disassoc_mld = false;
1582 os_memcpy(wpa_s->wnm_disassoc_addr, wpa_s->bssid, ETH_ALEN);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001583 }
1584
Sunil Ravic0f5d412024-09-11 22:12:49 +00001585 if (disassoc_imminent)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001586 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Sunil Ravic0f5d412024-09-11 22:12:49 +00001587 "Disassociation Timer %u", wpa_s->wnm_disassoc_timer);
1588
1589 if (wnm_parse_candidate_list(wpa_s, pos, end,
1590 &num_valid_candidates) < 0)
1591 goto reset;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001592
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001593 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001594 if (!wpa_s->wnm_num_neighbor_report) {
1595 wpa_printf(MSG_DEBUG,
1596 "WNM: Candidate list included bit is set, but no candidates found");
1597 wnm_send_bss_transition_mgmt_resp(
Sunil Ravi99c035e2024-07-12 01:42:03 +00001598 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001599 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1600 NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001601 goto reset;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001602 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00001603 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
1604 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001605
Sunil Ravic0f5d412024-09-11 22:12:49 +00001606 if (wpa_s->wnm_num_neighbor_report) {
1607 unsigned int valid_ms;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001608
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001609 wnm_sort_cand_list(wpa_s);
1610 wnm_dump_cand_list(wpa_s);
1611 valid_ms = valid_int * beacon_int * 128 / 125;
1612 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1613 valid_ms);
1614 os_get_reltime(&wpa_s->wnm_cand_valid_until);
Sunil Ravic0f5d412024-09-11 22:12:49 +00001615 os_reltime_add_ms(&wpa_s->wnm_cand_valid_until, valid_ms);
1616 } else if (!disassoc_imminent) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001617 enum bss_trans_mgmt_status_code status;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001618
Sunil Ravic0f5d412024-09-11 22:12:49 +00001619 /* No candidate list and disassociation is not imminent */
1620
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001621 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) ||
1622 wpa_s->wnm_link_removal)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001623 status = WNM_BSS_TM_ACCEPT;
1624 else {
1625 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1626 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1627 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00001628
1629 if (reply)
1630 wnm_send_bss_transition_mgmt_resp(
1631 wpa_s, status,
1632 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1633 NULL);
1634
1635 goto reset;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001636 }
Sunil Ravic0f5d412024-09-11 22:12:49 +00001637
1638 /*
1639 * Try fetching the latest scan results from the kernel.
1640 * This can help in finding more up-to-date information should
1641 * the driver have done some internal scanning operations after
1642 * the last scan result update in wpa_supplicant.
1643 *
1644 * It is not a new scan, this does not update the last_scan
1645 * timestamp nor will it expire old BSSs.
1646 */
1647 wpa_supplicant_update_scan_results(wpa_s, NULL);
1648 if (wnm_scan_process(wpa_s, true) > 0)
1649 return;
1650 wpa_printf(MSG_DEBUG,
1651 "WNM: No valid match in previous scan results - try a new scan");
1652
1653 /*
1654 * If we have a fixed BSSID configured, just reject at this point.
1655 * NOTE: We could actually check if we are allowed to stay (and we do
1656 * above if we have scan results available).
1657 */
1658 if (wpa_s->current_ssid && wpa_s->current_ssid->bssid_set) {
1659 wpa_printf(MSG_DEBUG, "WNM: Fixed BSSID, rejecting request");
1660
1661 if (reply)
1662 wnm_send_bss_transition_mgmt_resp(
1663 wpa_s, WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1664 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1665 NULL);
1666
1667 goto reset;
1668 }
1669
1670 wnm_set_scan_freqs(wpa_s);
1671 if (num_valid_candidates == 1) {
1672 /* Any invalid candidate was sorted to the end */
1673 os_memcpy(wpa_s->next_scan_bssid,
1674 wpa_s->wnm_neighbor_report_elements[0].bssid,
1675 ETH_ALEN);
1676 wpa_printf(MSG_DEBUG,
1677 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1678 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1679 }
1680 wpa_s->wnm_transition_scan = true;
1681 wpa_supplicant_req_scan(wpa_s, 0, 0);
1682
1683 /* Continue from scan handler */
1684 return;
1685
1686reset:
1687 wnm_btm_reset(wpa_s);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001688}
1689
1690
Sunil Ravi99c035e2024-07-12 01:42:03 +00001691int wnm_btm_resp_tx_status(struct wpa_supplicant *wpa_s, const u8 *data,
1692 size_t data_len)
1693{
1694 const struct ieee80211_mgmt *frame =
1695 (const struct ieee80211_mgmt *) data;
1696
1697 if (data_len <
1698 IEEE80211_HDRLEN + sizeof(frame->u.action.u.bss_tm_resp) ||
1699 frame->u.action.category != WLAN_ACTION_WNM ||
1700 frame->u.action.u.bss_tm_resp.action != WNM_BSS_TRANS_MGMT_RESP ||
1701 frame->u.action.u.bss_tm_resp.status_code != WNM_BSS_TM_ACCEPT)
1702 return -1;
1703
1704 /*
1705 * If disassoc imminent bit was set in the request, the response may
1706 * indicate accept even if no candidate was found, so bail out here.
1707 */
1708 if (!wpa_s->wnm_target_bss) {
1709 wpa_printf(MSG_DEBUG, "WNM: Target BSS is not set");
1710 return 0;
1711 }
1712
1713 if (!wpa_s->current_ssid)
1714 return 0;
1715
1716 wnm_bss_tm_connect(wpa_s, wpa_s->wnm_target_bss, wpa_s->current_ssid,
1717 0);
1718
1719 wpa_s->wnm_target_bss = NULL;
1720 return 0;
1721}
1722
1723
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001724#define BTM_QUERY_MIN_SIZE 4
1725
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001726int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001727 u8 query_reason,
1728 const char *btm_candidates,
1729 int cand_list)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001730{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001731 struct wpabuf *buf;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001732 int ret;
1733
1734 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001735 MACSTR " query_reason=%u%s",
1736 MAC2STR(wpa_s->bssid), query_reason,
1737 cand_list ? " candidate list" : "");
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001738
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001739 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1740 if (!buf)
1741 return -1;
1742
1743 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1744 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1745 wpabuf_put_u8(buf, 1);
1746 wpabuf_put_u8(buf, query_reason);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001747
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001748 if (cand_list)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001749 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001750
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001751 if (btm_candidates) {
1752 const size_t max_len = 1000;
1753
1754 ret = wpabuf_resize(&buf, max_len);
1755 if (ret < 0) {
1756 wpabuf_free(buf);
1757 return ret;
1758 }
1759
1760 ret = ieee802_11_parse_candidate_list(btm_candidates,
1761 wpabuf_put(buf, 0),
1762 max_len);
1763 if (ret < 0) {
1764 wpabuf_free(buf);
1765 return ret;
1766 }
1767
1768 wpabuf_put(buf, ret);
1769 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001770
1771 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1772 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001773 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001774
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001775 wpabuf_free(buf);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001776 return ret;
1777}
1778
1779
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001780static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1781 const u8 *sa, const u8 *data,
1782 int len)
1783{
1784 const u8 *pos, *end, *next;
1785 u8 ie, ie_len;
1786
1787 pos = data;
1788 end = data + len;
1789
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001790 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001791 ie = *pos++;
1792 ie_len = *pos++;
1793 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1794 ie, ie_len);
1795 if (ie_len > end - pos) {
1796 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1797 "subelement");
1798 break;
1799 }
1800 next = pos + ie_len;
1801 if (ie_len < 4) {
1802 pos = next;
1803 continue;
1804 }
1805 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1806 WPA_GET_BE24(pos), pos[3]);
1807
1808#ifdef CONFIG_HS20
1809 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1810 WPA_GET_BE24(pos) == OUI_WFA &&
1811 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1812 /* Subscription Remediation subelement */
1813 const u8 *ie_end;
1814 u8 url_len;
1815 char *url;
1816 u8 osu_method;
1817
1818 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1819 "subelement");
1820 ie_end = pos + ie_len;
1821 pos += 4;
1822 url_len = *pos++;
1823 if (url_len == 0) {
1824 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1825 url = NULL;
1826 osu_method = 1;
1827 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001828 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001829 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1830 url_len,
1831 (int) (ie_end - pos));
1832 break;
1833 }
1834 url = os_malloc(url_len + 1);
1835 if (url == NULL)
1836 break;
1837 os_memcpy(url, pos, url_len);
1838 url[url_len] = '\0';
1839 osu_method = pos[url_len];
1840 }
1841 hs20_rx_subscription_remediation(wpa_s, url,
1842 osu_method);
1843 os_free(url);
1844 pos = next;
1845 continue;
1846 }
1847
1848 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1849 WPA_GET_BE24(pos) == OUI_WFA &&
1850 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1851 const u8 *ie_end;
1852 u8 url_len;
1853 char *url;
1854 u8 code;
1855 u16 reauth_delay;
1856
1857 ie_end = pos + ie_len;
1858 pos += 4;
1859 code = *pos++;
1860 reauth_delay = WPA_GET_LE16(pos);
1861 pos += 2;
1862 url_len = *pos++;
1863 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1864 "Imminent - Reason Code %u "
1865 "Re-Auth Delay %u URL Length %u",
1866 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001867 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001868 break;
1869 url = os_malloc(url_len + 1);
1870 if (url == NULL)
1871 break;
1872 os_memcpy(url, pos, url_len);
1873 url[url_len] = '\0';
1874 hs20_rx_deauth_imminent_notice(wpa_s, code,
1875 reauth_delay, url);
1876 os_free(url);
1877 pos = next;
1878 continue;
1879 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001880
1881 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1882 WPA_GET_BE24(pos) == OUI_WFA &&
1883 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1884 const u8 *ie_end;
1885 u8 url_len;
1886 char *url;
1887
1888 ie_end = pos + ie_len;
1889 pos += 4;
1890 url_len = *pos++;
1891 wpa_printf(MSG_DEBUG,
1892 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1893 url_len);
1894 if (url_len > ie_end - pos)
1895 break;
1896 url = os_malloc(url_len + 1);
1897 if (!url)
1898 break;
1899 os_memcpy(url, pos, url_len);
1900 url[url_len] = '\0';
1901 hs20_rx_t_c_acceptance(wpa_s, url);
1902 os_free(url);
1903 pos = next;
1904 continue;
1905 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001906#endif /* CONFIG_HS20 */
1907
1908 pos = next;
1909 }
1910}
1911
1912
1913static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1914 const u8 *sa, const u8 *frm, int len)
1915{
1916 const u8 *pos, *end;
1917 u8 dialog_token, type;
1918
1919 /* Dialog Token [1] | Type [1] | Subelements */
1920
1921 if (len < 2 || sa == NULL)
1922 return;
1923 end = frm + len;
1924 pos = frm;
1925 dialog_token = *pos++;
1926 type = *pos++;
1927
1928 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1929 "(dialog_token %u type %u sa " MACSTR ")",
1930 dialog_token, type, MAC2STR(sa));
1931 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1932 pos, end - pos);
1933
1934 if (wpa_s->wpa_state != WPA_COMPLETED ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001935 (!ether_addr_equal(sa, wpa_s->bssid) &&
1936 (!wpa_s->valid_links ||
1937 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001938 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1939 "from our AP - ignore it");
1940 return;
1941 }
1942
1943 switch (type) {
1944 case 1:
1945 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1946 break;
1947 default:
1948 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1949 "WNM-Notification type %u", type);
1950 break;
1951 }
1952}
1953
1954
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001955static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1956 const u8 *sa, const u8 *frm,
1957 int len)
1958{
1959 u8 dialog_token, req_info, auto_report, timeout;
1960
1961 if (!wpa_s->conf->coloc_intf_reporting)
1962 return;
1963
1964 /* Dialog Token [1] | Request Info [1] */
1965
1966 if (len < 2)
1967 return;
1968 dialog_token = frm[0];
1969 req_info = frm[1];
1970 auto_report = req_info & 0x03;
1971 timeout = req_info >> 2;
1972
1973 wpa_dbg(wpa_s, MSG_DEBUG,
1974 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1975 dialog_token, auto_report, timeout, MAC2STR(sa));
1976
1977 if (dialog_token == 0)
1978 return; /* only nonzero values are used for request */
1979
1980 if (wpa_s->wpa_state != WPA_COMPLETED ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001981 (!ether_addr_equal(sa, wpa_s->bssid) &&
1982 (!wpa_s->valid_links ||
1983 !ether_addr_equal(sa, wpa_s->ap_mld_addr)))) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001984 wpa_dbg(wpa_s, MSG_DEBUG,
1985 "WNM: Collocated Interference Request frame not from current AP - ignore it");
1986 return;
1987 }
1988
1989 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1990 dialog_token, auto_report, timeout);
1991 wpa_s->coloc_intf_dialog_token = dialog_token;
1992 wpa_s->coloc_intf_auto_report = auto_report;
1993 wpa_s->coloc_intf_timeout = timeout;
1994}
1995
1996
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001997void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001998 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001999{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002000 const u8 *pos, *end;
2001 u8 act;
2002
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002003 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002004 return;
2005
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002006 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002007 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002008 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002009
2010 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002011 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002012 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002013 (!ether_addr_equal(mgmt->sa, wpa_s->bssid) &&
2014 (!wpa_s->valid_links ||
2015 !ether_addr_equal(mgmt->sa, wpa_s->ap_mld_addr)))) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002016 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
2017 "frame");
2018 return;
2019 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002020
2021 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002022 case WNM_BSS_TRANS_MGMT_REQ:
2023 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002024 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002025 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002026 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002027 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002028 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002029 case WNM_NOTIFICATION_REQ:
2030 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
2031 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002032 case WNM_COLLOCATED_INTERFERENCE_REQ:
2033 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
2034 end - pos);
2035 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002036 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07002037 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002038 break;
2039 }
2040}
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002041
2042
2043int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
2044 const struct wpabuf *elems)
2045{
2046 struct wpabuf *buf;
2047 int ret;
2048
2049 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
2050 return -1;
2051
2052 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
2053 MACSTR " (dialog token %u)",
2054 MAC2STR(wpa_s->bssid), dialog_token);
2055
2056 buf = wpabuf_alloc(3 + wpabuf_len(elems));
2057 if (!buf)
2058 return -1;
2059
2060 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
2061 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
2062 wpabuf_put_u8(buf, dialog_token);
2063 wpabuf_put_buf(buf, elems);
2064
2065 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
2066 wpa_s->own_addr, wpa_s->bssid,
2067 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
2068 wpabuf_free(buf);
2069 return ret;
2070}
2071
2072
2073void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
2074 struct wpabuf *elems)
2075{
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002076 if (elems && wpabuf_len(elems) == 0) {
2077 wpabuf_free(elems);
2078 elems = NULL;
2079 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002080
Sunil Ravi99c035e2024-07-12 01:42:03 +00002081 /* NOTE: The elements are not stored as they are only send out once */
2082
2083 if (wpa_s->conf->coloc_intf_reporting && elems &&
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002084 wpa_s->coloc_intf_dialog_token &&
2085 (wpa_s->coloc_intf_auto_report == 1 ||
2086 wpa_s->coloc_intf_auto_report == 3)) {
2087 /* TODO: Check that there has not been less than
2088 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
2089 */
2090 wnm_send_coloc_intf_report(wpa_s,
2091 wpa_s->coloc_intf_dialog_token,
Sunil Ravi99c035e2024-07-12 01:42:03 +00002092 elems);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002093 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00002094
2095 wpabuf_free(elems);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002096}
2097
2098
2099void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
2100{
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002101 wpa_s->coloc_intf_dialog_token = 0;
2102 wpa_s->coloc_intf_auto_report = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002103}
2104
2105
2106bool wnm_is_bss_excluded(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
2107{
Sunil Ravic0f5d412024-09-11 22:12:49 +00002108 int i;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002109
2110 /*
2111 * In case disassociation imminent is set, do no try to use a BSS to
2112 * which we are connected.
2113 */
Sunil Ravic0f5d412024-09-11 22:12:49 +00002114 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
2115 if (!wpa_s->wnm_disassoc_mld) {
2116 if (ether_addr_equal(bss->bssid,
2117 wpa_s->wnm_disassoc_addr))
2118 return true;
2119 } else {
2120 if (ether_addr_equal(bss->mld_addr,
2121 wpa_s->wnm_disassoc_addr))
2122 return true;
2123 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002124 }
2125
Sunil Ravic0f5d412024-09-11 22:12:49 +00002126 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
2127 struct neighbor_report *nei;
2128
2129 nei = &wpa_s->wnm_neighbor_report_elements[i];
2130 if (!ether_addr_equal(nei->bssid, bss->bssid))
2131 continue;
2132
2133 if (nei->preference_present && nei->preference == 0)
2134 return true;
2135
2136 break;
2137 }
2138
2139 /* If the abridged bit is set, the BSS must be a known neighbor. */
2140 if ((wpa_s->wnm_mode & WNM_BSS_TM_REQ_ABRIDGED) &&
2141 wpa_s->wnm_num_neighbor_report == i)
2142 return true;
2143
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002144 return false;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002145}