blob: 72a119d58be8252cc1b4b2ff75f5602eef68da41 [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 Shmidt849734c2016-05-27 09:59:01 -070030#define WNM_SCAN_RESULT_AGE 2 /* 2 seconds */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070031
32/* get the TFS IE from driver */
33static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
34 u16 *buf_len, enum wnm_oper oper)
35{
36 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
37
38 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
39}
40
41
42/* set the TFS IE to driver */
43static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080044 const u8 *addr, const u8 *buf, u16 buf_len,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045 enum wnm_oper oper)
46{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047 u16 len = buf_len;
48
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070049 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
50
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080051 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070052}
53
54
55/* MLME-SLEEPMODE.request */
56int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080057 u8 action, u16 intval, struct wpabuf *tfs_req)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070058{
59 struct ieee80211_mgmt *mgmt;
60 int res;
61 size_t len;
62 struct wnm_sleep_element *wnmsleep_ie;
Hai Shalom74f70d42019-02-11 14:42:39 -080063 u8 *wnmtfs_ie, *oci_ie;
64 u8 wnmsleep_ie_len, oci_ie_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
66 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
67 WNM_SLEEP_TFS_REQ_IE_NONE;
68
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080069 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
70 "action=%s to " MACSTR,
71 action == 0 ? "enter" : "exit",
72 MAC2STR(wpa_s->bssid));
73
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070074 /* WNM-Sleep Mode IE */
75 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
76 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
77 if (wnmsleep_ie == NULL)
78 return -1;
79 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
80 wnmsleep_ie->len = wnmsleep_ie_len - 2;
81 wnmsleep_ie->action_type = action;
82 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080083 wnmsleep_ie->intval = host_to_le16(intval);
84 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
85 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086
87 /* TFS IE(s) */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080088 if (tfs_req) {
89 wnmtfs_ie_len = wpabuf_len(tfs_req);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070090 wnmtfs_ie = os_memdup(wpabuf_head(tfs_req), wnmtfs_ie_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080091 if (wnmtfs_ie == NULL) {
92 os_free(wnmsleep_ie);
93 return -1;
94 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080095 } else {
96 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
97 if (wnmtfs_ie == NULL) {
98 os_free(wnmsleep_ie);
99 return -1;
100 }
101 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
102 tfs_oper)) {
103 wnmtfs_ie_len = 0;
104 os_free(wnmtfs_ie);
105 wnmtfs_ie = NULL;
106 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800108 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
109 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110
Hai Shalom74f70d42019-02-11 14:42:39 -0800111 oci_ie = NULL;
112 oci_ie_len = 0;
113#ifdef CONFIG_OCV
114 if (action == WNM_SLEEP_MODE_EXIT && wpa_sm_ocv_enabled(wpa_s->wpa)) {
115 struct wpa_channel_info ci;
116
117 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
118 wpa_printf(MSG_WARNING,
119 "Failed to get channel info for OCI element in WNM-Sleep Mode frame");
120 os_free(wnmsleep_ie);
121 os_free(wnmtfs_ie);
122 return -1;
123 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700124#ifdef CONFIG_TESTING_OPTIONS
125 if (wpa_s->oci_freq_override_wnm_sleep) {
126 wpa_printf(MSG_INFO,
127 "TEST: Override OCI KDE frequency %d -> %d MHz",
128 ci.frequency,
129 wpa_s->oci_freq_override_wnm_sleep);
130 ci.frequency = wpa_s->oci_freq_override_wnm_sleep;
131 }
132#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800133
134 oci_ie_len = OCV_OCI_EXTENDED_LEN;
135 oci_ie = os_zalloc(oci_ie_len);
136 if (!oci_ie) {
137 wpa_printf(MSG_WARNING,
138 "Failed to allocate buffer for for OCI element in WNM-Sleep Mode frame");
139 os_free(wnmsleep_ie);
140 os_free(wnmtfs_ie);
141 return -1;
142 }
143
144 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
145 os_free(wnmsleep_ie);
146 os_free(wnmtfs_ie);
147 os_free(oci_ie);
148 return -1;
149 }
150 }
151#endif /* CONFIG_OCV */
152
153 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len +
154 oci_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700155 if (mgmt == NULL) {
156 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
157 "WNM-Sleep Request action frame");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800158 os_free(wnmsleep_ie);
159 os_free(wnmtfs_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700160 return -1;
161 }
162
163 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
164 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
165 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
166 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
167 WLAN_FC_STYPE_ACTION);
168 mgmt->u.action.category = WLAN_ACTION_WNM;
169 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800170 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700171 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
172 wnmsleep_ie_len);
173 /* copy TFS IE here */
174 if (wnmtfs_ie_len > 0) {
175 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
176 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
177 }
178
Hai Shalom74f70d42019-02-11 14:42:39 -0800179#ifdef CONFIG_OCV
180 /* copy OCV OCI here */
181 if (oci_ie_len > 0) {
182 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
183 wnmsleep_ie_len + wnmtfs_ie_len, oci_ie, oci_ie_len);
184 }
185#endif /* CONFIG_OCV */
186
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700187 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
Hai Shalom74f70d42019-02-11 14:42:39 -0800188 wnmtfs_ie_len + oci_ie_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700189
190 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
191 wpa_s->own_addr, wpa_s->bssid,
192 &mgmt->u.action.category, len, 0);
193 if (res < 0)
194 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
195 "(action=%d, intval=%d)", action, intval);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800196 else
197 wpa_s->wnmsleep_used = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700198
199 os_free(wnmsleep_ie);
200 os_free(wnmtfs_ie);
Hai Shalom74f70d42019-02-11 14:42:39 -0800201 os_free(oci_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700202 os_free(mgmt);
203
204 return res;
205}
206
207
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800208static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800209 const u8 *tfsresp_ie_start,
210 const u8 *tfsresp_ie_end)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800211{
212 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
213 wpa_s->bssid, NULL, NULL);
214 /* remove GTK/IGTK ?? */
215
216 /* set the TFS Resp IE(s) */
217 if (tfsresp_ie_start && tfsresp_ie_end &&
218 tfsresp_ie_end - tfsresp_ie_start >= 0) {
219 u16 tfsresp_ie_len;
220 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
221 tfsresp_ie_start;
222 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
223 /* pass the TFS Resp IE(s) to driver for processing */
224 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
225 tfsresp_ie_start,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800226 tfsresp_ie_len,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800227 WNM_SLEEP_TFS_RESP_IE_SET))
228 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
229 }
230}
231
232
233static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
234 const u8 *frm, u16 key_len_total)
235{
236 u8 *ptr, *end;
237 u8 gtk_len;
238
239 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
240 NULL, NULL);
241
242 /* Install GTK/IGTK */
243
244 /* point to key data field */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800245 ptr = (u8 *) frm + 1 + 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800246 end = ptr + key_len_total;
247 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
248
Jouni Malinen17de68d2015-11-09 15:25:41 -0800249 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
250 wpa_msg(wpa_s, MSG_INFO,
251 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
252 return;
253 }
254
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800255 while (end - ptr > 1) {
256 if (2 + ptr[1] > end - ptr) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800257 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
258 "length");
259 if (end > ptr) {
260 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
261 ptr, end - ptr);
262 }
263 break;
264 }
265 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
266 if (ptr[1] < 11 + 5) {
267 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
268 "subelem");
269 break;
270 }
271 gtk_len = *(ptr + 4);
272 if (ptr[1] < 11 + gtk_len ||
273 gtk_len < 5 || gtk_len > 32) {
274 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
275 "subelem");
276 break;
277 }
278 wpa_wnmsleep_install_key(
279 wpa_s->wpa,
280 WNM_SLEEP_SUBELEM_GTK,
281 ptr);
282 ptr += 13 + gtk_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800283 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
284 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
285 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
286 "subelem");
287 break;
288 }
289 wpa_wnmsleep_install_key(wpa_s->wpa,
290 WNM_SLEEP_SUBELEM_IGTK, ptr);
291 ptr += 10 + WPA_IGTK_LEN;
Hai Shalomfdcde762020-04-02 11:19:20 -0700292 } else if (*ptr == WNM_SLEEP_SUBELEM_BIGTK) {
293 if (ptr[1] < 2 + 6 + WPA_BIGTK_LEN) {
294 wpa_printf(MSG_DEBUG,
295 "WNM: Too short BIGTK subelem");
296 break;
297 }
298 wpa_wnmsleep_install_key(wpa_s->wpa,
299 WNM_SLEEP_SUBELEM_BIGTK, ptr);
300 ptr += 10 + WPA_BIGTK_LEN;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800301 } else
302 break; /* skip the loop */
303 }
304}
305
306
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700307static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
308 const u8 *frm, int len)
309{
310 /*
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700311 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700312 * WNM-Sleep Mode IE | TFS Response IE
313 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800314 const u8 *pos = frm; /* point to payload after the action field */
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700315 u16 key_len_total;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700316 struct wnm_sleep_element *wnmsleep_ie = NULL;
317 /* multiple TFS Resp IE (assuming consecutive) */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800318 const u8 *tfsresp_ie_start = NULL;
319 const u8 *tfsresp_ie_end = NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -0800320#ifdef CONFIG_OCV
321 const u8 *oci_ie = NULL;
322 u8 oci_ie_len = 0;
323#endif /* CONFIG_OCV */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800324 size_t left;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700325
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800326 if (!wpa_s->wnmsleep_used) {
327 wpa_printf(MSG_DEBUG,
Jouni Malinen90bfa452017-09-22 11:25:02 +0300328 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800329 return;
330 }
331
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700332 if (len < 3)
333 return;
334 key_len_total = WPA_GET_LE16(frm + 1);
335
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800336 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
337 frm[0], key_len_total);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800338 left = len - 3;
339 if (key_len_total > left) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800340 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
341 return;
342 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800343 pos += 3 + key_len_total;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800344 while (pos - frm + 1 < len) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700345 u8 ie_len = *(pos + 1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800346 if (2 + ie_len > frm + len - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800347 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
348 break;
349 }
350 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800351 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700352 wnmsleep_ie = (struct wnm_sleep_element *) pos;
353 else if (*pos == WLAN_EID_TFS_RESP) {
354 if (!tfsresp_ie_start)
355 tfsresp_ie_start = pos;
356 tfsresp_ie_end = pos;
Hai Shalom74f70d42019-02-11 14:42:39 -0800357#ifdef CONFIG_OCV
358 } else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
359 pos[2] == WLAN_EID_EXT_OCV_OCI) {
360 oci_ie = pos + 3;
361 oci_ie_len = ie_len - 1;
362#endif /* CONFIG_OCV */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700363 } else
364 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
365 pos += ie_len + 2;
366 }
367
368 if (!wnmsleep_ie) {
369 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
370 return;
371 }
372
Hai Shalom74f70d42019-02-11 14:42:39 -0800373#ifdef CONFIG_OCV
374 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
375 wpa_sm_ocv_enabled(wpa_s->wpa)) {
376 struct wpa_channel_info ci;
377
378 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
379 wpa_msg(wpa_s, MSG_WARNING,
380 "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
381 return;
382 }
383
384 if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
385 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -0700386 ci.seg1_idx) != OCI_SUCCESS) {
387 wpa_msg(wpa_s, MSG_WARNING, "WNM: OCV failed: %s",
388 ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -0800389 return;
390 }
391 }
392#endif /* CONFIG_OCV */
393
Jouni Malinen90bfa452017-09-22 11:25:02 +0300394 wpa_s->wnmsleep_used = 0;
395
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800396 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
397 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700398 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
399 "frame (action=%d, intval=%d)",
400 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800401 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
402 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
403 tfsresp_ie_end);
404 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
405 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700406 }
407 } else {
408 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
409 "(action=%d, intval=%d)",
410 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800411 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700412 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
413 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800414 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700415 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
416 wpa_s->bssid, NULL, NULL);
417 }
418}
419
420
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700421void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
422{
423 int i;
424
425 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700426 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700427 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
428 }
429
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700430 wpa_s->wnm_num_neighbor_report = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700431 os_free(wpa_s->wnm_neighbor_report_elements);
432 wpa_s->wnm_neighbor_report_elements = NULL;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800433
434 wpabuf_free(wpa_s->coloc_intf_elems);
435 wpa_s->coloc_intf_elems = NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700436}
437
438
439static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
440 u8 id, u8 elen, const u8 *pos)
441{
442 switch (id) {
443 case WNM_NEIGHBOR_TSF:
444 if (elen < 2 + 2) {
445 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
446 break;
447 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800448 rep->tsf_offset = WPA_GET_LE16(pos);
449 rep->beacon_int = WPA_GET_LE16(pos + 2);
450 rep->tsf_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700451 break;
452 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
453 if (elen < 2) {
454 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
455 "country string");
456 break;
457 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800458 os_memcpy(rep->country, pos, 2);
459 rep->country_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700460 break;
461 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
462 if (elen < 1) {
463 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
464 "candidate");
465 break;
466 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800467 rep->preference = pos[0];
468 rep->preference_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700469 break;
470 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
Hai Shalomcb95c3f2019-02-04 12:53:10 -0800471 if (elen < 10) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700472 wpa_printf(MSG_DEBUG,
473 "WNM: Too short BSS termination duration");
Hai Shalomcb95c3f2019-02-04 12:53:10 -0800474 break;
475 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800476 rep->bss_term_tsf = WPA_GET_LE64(pos);
477 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
478 rep->bss_term_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700479 break;
480 case WNM_NEIGHBOR_BEARING:
481 if (elen < 8) {
482 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
483 "bearing");
484 break;
485 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800486 rep->bearing = WPA_GET_LE16(pos);
487 rep->distance = WPA_GET_LE32(pos + 2);
488 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
489 rep->bearing_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700490 break;
491 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700492 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700493 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
494 "pilot");
495 break;
496 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700497 os_free(rep->meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700498 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
499 if (rep->meas_pilot == NULL)
500 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700501 rep->meas_pilot->measurement_pilot = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700502 rep->meas_pilot->subelem_len = elen - 1;
503 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700504 break;
505 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700506 if (elen < 5) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700507 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
508 "capabilities");
509 break;
510 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800511 os_memcpy(rep->rm_capab, pos, 5);
512 rep->rm_capab_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700513 break;
514 case WNM_NEIGHBOR_MULTIPLE_BSSID:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700515 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700516 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
517 break;
518 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700519 os_free(rep->mul_bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700520 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
521 if (rep->mul_bssid == NULL)
522 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700523 rep->mul_bssid->max_bssid_indicator = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700524 rep->mul_bssid->subelem_len = elen - 1;
525 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700526 break;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000527 default:
528 wpa_printf(MSG_DEBUG,
529 "WNM: Unsupported neighbor report subelement id %u",
530 id);
531 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700532 }
533}
534
535
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800536static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
537{
538 struct wpa_bss *bss = wpa_s->current_bss;
539 const char *country = NULL;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800540 int freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800541
542 if (bss) {
543 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
544
545 if (elem && elem[1] >= 2)
546 country = (const char *) (elem + 2);
547 }
548
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800549 freq = ieee80211_chan_to_freq(country, op_class, chan);
550 if (freq <= 0 && op_class == 0) {
551 /*
552 * Some APs do not advertise correct operating class
553 * information. Try to determine the most likely operating
554 * frequency based on the channel number.
555 */
556 if (chan >= 1 && chan <= 13)
557 freq = 2407 + chan * 5;
558 else if (chan == 14)
559 freq = 2484;
Hai Shalom60840252021-02-19 19:02:11 -0800560 else if (chan >= 36 && chan <= 177)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800561 freq = 5000 + chan * 5;
562 }
563 return freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800564}
565
566
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700567static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
568 const u8 *pos, u8 len,
569 struct neighbor_report *rep)
570{
571 u8 left = len;
572
573 if (left < 13) {
574 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
575 return;
576 }
577
578 os_memcpy(rep->bssid, pos, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800579 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700580 rep->regulatory_class = *(pos + 10);
581 rep->channel_number = *(pos + 11);
582 rep->phy_type = *(pos + 12);
583
584 pos += 13;
585 left -= 13;
586
587 while (left >= 2) {
588 u8 id, elen;
589
590 id = *pos++;
591 elen = *pos++;
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700592 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
593 left -= 2;
594 if (elen > left) {
595 wpa_printf(MSG_DEBUG,
596 "WNM: Truncated neighbor report subelement");
597 break;
598 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700599 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700600 left -= elen;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700601 pos += elen;
602 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800603
604 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
605 rep->channel_number);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700606}
607
608
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700609static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700610{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700611 unsigned int i;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700612
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700613 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
614 wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
615}
616
617
618static struct wpa_bss * get_first_acceptable(struct wpa_supplicant *wpa_s)
619{
620 unsigned int i;
621 struct neighbor_report *nei;
622
623 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
624 nei = &wpa_s->wnm_neighbor_report_elements[i];
625 if (nei->acceptable)
626 return wpa_bss_get_bssid(wpa_s, nei->bssid);
627 }
628
629 return NULL;
630}
631
632
633#ifdef CONFIG_MBO
634static struct wpa_bss *
635get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
636 enum mbo_transition_reject_reason *reason)
637{
638 struct wpa_bss *target = NULL;
639 struct wpa_bss_trans_info params;
640 struct wpa_bss_candidate_info *info = NULL;
641 struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
642 u8 *first_candidate_bssid = NULL, *pos;
643 unsigned int i;
644
645 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
646 params.n_candidates = 0;
647 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
648 if (!params.bssid)
649 return NULL;
650
651 pos = params.bssid;
652 for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
653 if (nei->is_first)
654 first_candidate_bssid = nei->bssid;
655 if (!nei->acceptable)
656 continue;
657 os_memcpy(pos, nei->bssid, ETH_ALEN);
658 pos += ETH_ALEN;
659 params.n_candidates++;
660 }
661
662 if (!params.n_candidates)
663 goto end;
664
665 info = wpa_drv_get_bss_trans_status(wpa_s, &params);
666 if (!info) {
667 /* If failed to get candidate BSS transition status from driver,
668 * get the first acceptable candidate from wpa_supplicant.
669 */
670 target = wpa_bss_get_bssid(wpa_s, params.bssid);
671 goto end;
672 }
673
674 /* Get the first acceptable candidate from driver */
675 for (i = 0; i < info->num; i++) {
676 if (info->candidates[i].is_accept) {
677 target = wpa_bss_get_bssid(wpa_s,
678 info->candidates[i].bssid);
679 goto end;
680 }
681 }
682
683 /* If Disassociation Imminent is set and driver rejects all the
684 * candidate select first acceptable candidate which has
685 * rssi > disassoc_imminent_rssi_threshold
686 */
687 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
688 for (i = 0; i < info->num; i++) {
689 target = wpa_bss_get_bssid(wpa_s,
690 info->candidates[i].bssid);
691 if (target &&
692 (target->level <
693 wpa_s->conf->disassoc_imminent_rssi_threshold))
694 continue;
695 goto end;
696 }
697 }
698
699 /* While sending BTM reject use reason code of the first candidate
700 * received in BTM request frame
701 */
702 if (reason) {
703 for (i = 0; i < info->num; i++) {
704 if (first_candidate_bssid &&
705 os_memcmp(first_candidate_bssid,
706 info->candidates[i].bssid, ETH_ALEN) == 0)
707 {
708 *reason = info->candidates[i].reject_reason;
709 break;
710 }
711 }
712 }
713
714 target = NULL;
715
716end:
717 os_free(params.bssid);
718 if (info) {
719 os_free(info->candidates);
720 os_free(info);
721 }
722 return target;
723}
724#endif /* CONFIG_MBO */
725
726
727static struct wpa_bss *
728compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs,
729 enum mbo_transition_reject_reason *reason)
730{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800731 u8 i;
732 struct wpa_bss *bss = wpa_s->current_bss;
733 struct wpa_bss *target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700734
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800735 if (!bss)
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700736 return NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700737
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800738 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800739 MAC2STR(wpa_s->bssid), bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800740
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700741 wnm_clear_acceptable(wpa_s);
742
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800743 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
744 struct neighbor_report *nei;
745
746 nei = &wpa_s->wnm_neighbor_report_elements[i];
747 if (nei->preference_present && nei->preference == 0) {
748 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
749 MAC2STR(nei->bssid));
750 continue;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700751 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800752
753 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
754 if (!target) {
755 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
756 " (pref %d) not found in scan results",
757 MAC2STR(nei->bssid),
758 nei->preference_present ? nei->preference :
759 -1);
760 continue;
761 }
762
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700763 if (age_secs) {
764 struct os_reltime now;
765
766 if (os_get_reltime(&now) == 0 &&
767 os_reltime_expired(&now, &target->last_update,
768 age_secs)) {
769 wpa_printf(MSG_DEBUG,
770 "Candidate BSS is more than %ld seconds old",
771 age_secs);
772 continue;
773 }
774 }
775
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800776 if (bss->ssid_len != target->ssid_len ||
777 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
778 /*
779 * TODO: Could consider allowing transition to another
780 * ESS if PMF was enabled for the association.
781 */
782 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
783 " (pref %d) in different ESS",
784 MAC2STR(nei->bssid),
785 nei->preference_present ? nei->preference :
786 -1);
787 continue;
788 }
789
Dmitry Shmidte4663042016-04-04 10:07:49 -0700790 if (wpa_s->current_ssid &&
791 !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800792 1, 0)) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700793 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
794 " (pref %d) does not match the current network profile",
795 MAC2STR(nei->bssid),
796 nei->preference_present ? nei->preference :
797 -1);
798 continue;
799 }
800
Hai Shalom74f70d42019-02-11 14:42:39 -0800801 if (wpa_is_bss_tmp_disallowed(wpa_s, target)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800802 wpa_printf(MSG_DEBUG,
803 "MBO: Candidate BSS " MACSTR
804 " retry delay is not over yet",
805 MAC2STR(nei->bssid));
806 continue;
807 }
808
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800809 if (target->level < bss->level && target->level < -80) {
810 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
811 " (pref %d) does not have sufficient signal level (%d)",
812 MAC2STR(nei->bssid),
813 nei->preference_present ? nei->preference :
814 -1,
815 target->level);
816 continue;
817 }
818
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700819 nei->acceptable = 1;
820 }
821
822#ifdef CONFIG_MBO
823 if (wpa_s->wnm_mbo_trans_reason_present)
824 target = get_mbo_transition_candidate(wpa_s, reason);
825 else
826 target = get_first_acceptable(wpa_s);
827#else /* CONFIG_MBO */
828 target = get_first_acceptable(wpa_s);
829#endif /* CONFIG_MBO */
830
831 if (target) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800832 wpa_printf(MSG_DEBUG,
833 "WNM: Found an acceptable preferred transition candidate BSS "
834 MACSTR " (RSSI %d)",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700835 MAC2STR(target->bssid), target->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700836 }
837
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700838 return target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700839}
840
841
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800842static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
843{
844 const u8 *ie_a, *ie_b;
845
846 if (!a || !b)
847 return 0;
848
849 ie_a = wpa_bss_get_ie(a, eid);
850 ie_b = wpa_bss_get_ie(b, eid);
851
852 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
853 return 0;
854
855 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
856}
857
858
859static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
860{
861 u32 info = 0;
862
863 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
864
865 /*
866 * Leave the security and key scope bits unset to indicate that the
867 * security information is not available.
868 */
869
870 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
871 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
872 if (bss->caps & WLAN_CAPABILITY_QOS)
873 info |= NEI_REP_BSSID_INFO_QOS;
874 if (bss->caps & WLAN_CAPABILITY_APSD)
875 info |= NEI_REP_BSSID_INFO_APSD;
876 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
877 info |= NEI_REP_BSSID_INFO_RM;
878 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
879 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
880 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
881 info |= NEI_REP_BSSID_INFO_IMM_BA;
882 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
883 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
884 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
885 info |= NEI_REP_BSSID_INFO_HT;
886
887 return info;
888}
889
890
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700891static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
892 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
893 u8 pref)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800894{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700895 if (wpabuf_len(*buf) + 18 >
896 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800897 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700898 "WNM: No room in frame for Neighbor Report element");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800899 return -1;
900 }
901
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700902 if (wpabuf_resize(buf, 18) < 0) {
903 wpa_printf(MSG_DEBUG,
904 "WNM: Failed to allocate memory for Neighbor Report element");
905 return -1;
906 }
907
908 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800909 /* length: 13 for basic neighbor report + 3 for preference subelement */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700910 wpabuf_put_u8(*buf, 16);
911 wpabuf_put_data(*buf, bssid, ETH_ALEN);
912 wpabuf_put_le32(*buf, bss_info);
913 wpabuf_put_u8(*buf, op_class);
914 wpabuf_put_u8(*buf, chan);
915 wpabuf_put_u8(*buf, phy_type);
916 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
917 wpabuf_put_u8(*buf, 1);
918 wpabuf_put_u8(*buf, pref);
919 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800920}
921
922
923static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700924 struct wpa_bss *bss, struct wpabuf **buf,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800925 u8 pref)
926{
927 const u8 *ie;
928 u8 op_class, chan;
Sunil8cd6f4d2022-06-28 18:40:46 +0000929 int sec_chan = 0;
930 enum oper_chan_width vht = CONF_OPER_CHWIDTH_USE_HT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800931 enum phy_type phy_type;
932 u32 info;
933 struct ieee80211_ht_operation *ht_oper = NULL;
934 struct ieee80211_vht_operation *vht_oper = NULL;
935
936 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
937 if (ie && ie[1] >= 2) {
938 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
939
940 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
941 sec_chan = 1;
942 else if (ht_oper->ht_param &
943 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
944 sec_chan = -1;
945 }
946
947 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
948 if (ie && ie[1] >= 1) {
949 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
950
Hai Shalom81f62d82019-07-22 12:10:00 -0700951 if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
952 vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
953 vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800954 vht = vht_oper->vht_op_info_chwidth;
955 }
956
957 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
958 &chan) == NUM_HOSTAPD_MODES) {
959 wpa_printf(MSG_DEBUG,
960 "WNM: Cannot determine operating class and channel");
961 return -2;
962 }
963
964 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
965 (vht_oper != NULL));
966 if (phy_type == PHY_TYPE_UNSPECIFIED) {
967 wpa_printf(MSG_DEBUG,
968 "WNM: Cannot determine BSS phy type for Neighbor Report");
969 return -2;
970 }
971
972 info = wnm_get_bss_info(wpa_s, bss);
973
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700974 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
975 pref);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800976}
977
978
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700979static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800980{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800981 unsigned int i, pref = 255;
982 struct os_reltime now;
983 struct wpa_ssid *ssid = wpa_s->current_ssid;
984
985 if (!ssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700986 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800987
988 /*
989 * TODO: Define when scan results are no longer valid for the candidate
990 * list.
991 */
992 os_get_reltime(&now);
993 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700994 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800995
996 wpa_printf(MSG_DEBUG,
997 "WNM: Add candidate list to BSS Transition Management Response frame");
998 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
999 struct wpa_bss *bss = wpa_s->last_scan_res[i];
1000 int res;
1001
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001002 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001003 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001004 if (res == -2)
1005 continue; /* could not build entry for BSS */
1006 if (res < 0)
1007 break; /* no more room for candidates */
1008 if (pref == 1)
1009 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001010 }
1011 }
1012
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001013 wpa_hexdump_buf(MSG_DEBUG,
1014 "WNM: BSS Transition Management Response candidate list",
1015 *buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001016}
1017
1018
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001019#define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
1020
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001021static void wnm_send_bss_transition_mgmt_resp(
1022 struct wpa_supplicant *wpa_s, u8 dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001023 enum bss_trans_mgmt_status_code status,
1024 enum mbo_transition_reject_reason reason,
1025 u8 delay, const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001026{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001027 struct wpabuf *buf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001028 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001029
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",
1033 MAC2STR(wpa_s->bssid), dialog_token, status, reason, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001034 if (!wpa_s->current_bss) {
1035 wpa_printf(MSG_DEBUG,
1036 "WNM: Current BSS not known - drop response");
1037 return;
1038 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001039
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001040 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
1041 if (!buf) {
1042 wpa_printf(MSG_DEBUG,
1043 "WNM: Failed to allocate memory for BTM response");
1044 return;
1045 }
1046
Hai Shalom74f70d42019-02-11 14:42:39 -08001047 wpa_s->bss_tm_status = status;
1048 wpas_notify_bss_tm_status(wpa_s);
1049
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001050 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1051 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
1052 wpabuf_put_u8(buf, dialog_token);
1053 wpabuf_put_u8(buf, status);
1054 wpabuf_put_u8(buf, delay);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001055 if (target_bssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001056 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001057 } else if (status == WNM_BSS_TM_ACCEPT) {
1058 /*
1059 * P802.11-REVmc clarifies that the Target BSSID field is always
1060 * present when status code is zero, so use a fake value here if
1061 * no BSSID is yet known.
1062 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001063 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001064 }
1065
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001066 if (status == WNM_BSS_TM_ACCEPT)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001067 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001068
1069#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001070 if (status != WNM_BSS_TM_ACCEPT &&
1071 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
1072 u8 mbo[10];
1073 size_t ret;
1074
1075 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
1076 reason);
1077 if (ret) {
1078 if (wpabuf_resize(&buf, ret) < 0) {
1079 wpabuf_free(buf);
1080 wpa_printf(MSG_DEBUG,
1081 "WNM: Failed to allocate memory for MBO IE");
1082 return;
1083 }
1084
1085 wpabuf_put_data(buf, mbo, ret);
1086 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001087 }
1088#endif /* CONFIG_MBO */
1089
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001090 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1091 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001092 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001093 if (res < 0) {
1094 wpa_printf(MSG_DEBUG,
1095 "WNM: Failed to send BSS Transition Management Response");
1096 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001097
1098 wpabuf_free(buf);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001099}
1100
1101
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001102static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
1103 struct wpa_bss *bss, struct wpa_ssid *ssid,
1104 int after_new_scan)
1105{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001106 struct wpa_radio_work *already_connecting;
1107
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001108 wpa_dbg(wpa_s, MSG_DEBUG,
1109 "WNM: Transition to BSS " MACSTR
1110 " based on BSS Transition Management Request (old BSSID "
1111 MACSTR " after_new_scan=%d)",
1112 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1113
1114 /* Send the BSS Management Response - Accept */
1115 if (wpa_s->wnm_reply) {
1116 wpa_s->wnm_reply = 0;
1117 wpa_printf(MSG_DEBUG,
1118 "WNM: Sending successful BSS Transition Management Response");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001119 wnm_send_bss_transition_mgmt_resp(
1120 wpa_s, wpa_s->wnm_dialog_token, WNM_BSS_TM_ACCEPT,
1121 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1122 bss->bssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001123 }
1124
1125 if (bss == wpa_s->current_bss) {
1126 wpa_printf(MSG_DEBUG,
1127 "WNM: Already associated with the preferred candidate");
1128 wnm_deallocate_memory(wpa_s);
1129 return;
1130 }
1131
Hai Shaloma20dcd72022-02-04 13:43:00 -08001132 already_connecting = radio_work_pending(wpa_s, "sme-connect");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001133 wpa_s->reassociate = 1;
1134 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1135 wpa_supplicant_connect(wpa_s, bss, ssid);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001136
1137 /*
1138 * Indicate that a BSS transition is in progress so scan results that
1139 * come in before the 'sme-connect' radio work gets executed do not
1140 * override the original connection attempt.
1141 */
1142 if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1143 wpa_s->bss_trans_mgmt_in_progress = true;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001144 wnm_deallocate_memory(wpa_s);
1145}
1146
1147
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001148int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001149{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001150 struct wpa_bss *bss;
1151 struct wpa_ssid *ssid = wpa_s->current_ssid;
1152 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001153 enum mbo_transition_reject_reason reason =
1154 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001155
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001156 if (!wpa_s->wnm_neighbor_report_elements)
1157 return 0;
1158
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001159 wpa_dbg(wpa_s, MSG_DEBUG,
1160 "WNM: Process scan results for BSS Transition Management");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001161 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
1162 &wpa_s->scan_trigger_time)) {
1163 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1164 wnm_deallocate_memory(wpa_s);
1165 return 0;
1166 }
1167
1168 if (!wpa_s->current_bss ||
1169 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
1170 ETH_ALEN) != 0) {
1171 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
1172 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001173 }
1174
1175 /* Compare the Neighbor Report and scan results */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001176 bss = compare_scan_neighbor_results(wpa_s, 0, &reason);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001177 if (!bss) {
1178 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1179 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1180 goto send_bss_resp_fail;
1181 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001182
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001183 /* Associate to the network */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001184 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001185 return 1;
1186
1187send_bss_resp_fail:
1188 if (!reply_on_fail)
1189 return 0;
1190
1191 /* Send reject response for all the failures */
1192
1193 if (wpa_s->wnm_reply) {
1194 wpa_s->wnm_reply = 0;
1195 wnm_send_bss_transition_mgmt_resp(wpa_s,
1196 wpa_s->wnm_dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001197 status, reason, 0, NULL);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001198 }
1199 wnm_deallocate_memory(wpa_s);
1200
1201 return 0;
1202}
1203
1204
1205static int cand_pref_compar(const void *a, const void *b)
1206{
1207 const struct neighbor_report *aa = a;
1208 const struct neighbor_report *bb = b;
1209
1210 if (!aa->preference_present && !bb->preference_present)
1211 return 0;
1212 if (!aa->preference_present)
1213 return 1;
1214 if (!bb->preference_present)
1215 return -1;
1216 if (bb->preference > aa->preference)
1217 return 1;
1218 if (bb->preference < aa->preference)
1219 return -1;
1220 return 0;
1221}
1222
1223
1224static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1225{
1226 if (!wpa_s->wnm_neighbor_report_elements)
1227 return;
1228 qsort(wpa_s->wnm_neighbor_report_elements,
1229 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1230 cand_pref_compar);
1231}
1232
1233
1234static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1235{
1236 unsigned int i;
1237
1238 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1239 if (!wpa_s->wnm_neighbor_report_elements)
1240 return;
1241 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1242 struct neighbor_report *nei;
1243
1244 nei = &wpa_s->wnm_neighbor_report_elements[i];
1245 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1246 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1247 i, MAC2STR(nei->bssid), nei->bssid_info,
1248 nei->regulatory_class,
1249 nei->channel_number, nei->phy_type,
1250 nei->preference_present ? nei->preference : -1,
1251 nei->freq);
1252 }
1253}
1254
1255
1256static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1257{
1258 unsigned int i;
1259
1260 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1261 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1262 int j;
1263
1264 for (j = 0; j < mode->num_channels; j++) {
1265 struct hostapd_channel_data *chan;
1266
1267 chan = &mode->channels[j];
1268 if (chan->freq == freq &&
1269 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1270 return 1;
1271 }
1272 }
1273
1274 return 0;
1275}
1276
1277
1278static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1279{
1280 int *freqs;
1281 int num_freqs = 0;
1282 unsigned int i;
1283
1284 if (!wpa_s->wnm_neighbor_report_elements)
1285 return;
1286
1287 if (wpa_s->hw.modes == NULL)
1288 return;
1289
1290 os_free(wpa_s->next_scan_freqs);
1291 wpa_s->next_scan_freqs = NULL;
1292
1293 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1294 if (freqs == NULL)
1295 return;
1296
1297 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1298 struct neighbor_report *nei;
1299
1300 nei = &wpa_s->wnm_neighbor_report_elements[i];
1301 if (nei->freq <= 0) {
1302 wpa_printf(MSG_DEBUG,
1303 "WNM: Unknown neighbor operating frequency for "
1304 MACSTR " - scan all channels",
1305 MAC2STR(nei->bssid));
1306 os_free(freqs);
1307 return;
1308 }
1309 if (chan_supported(wpa_s, nei->freq))
1310 add_freq(freqs, &num_freqs, nei->freq);
1311 }
1312
1313 if (num_freqs == 0) {
1314 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001315 return;
1316 }
1317
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001318 wpa_printf(MSG_DEBUG,
1319 "WNM: Scan %d frequencies based on transition candidate list",
1320 num_freqs);
1321 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001322}
1323
1324
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001325static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
1326{
1327 struct wpa_scan_results *scan_res;
1328 struct wpa_bss *bss;
1329 struct wpa_ssid *ssid = wpa_s->current_ssid;
1330 u8 i, found = 0;
1331 size_t j;
1332
1333 wpa_dbg(wpa_s, MSG_DEBUG,
1334 "WNM: Fetch current scan results from the driver for checking transition candidates");
1335 scan_res = wpa_drv_get_scan_results2(wpa_s);
1336 if (!scan_res) {
1337 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
1338 return 0;
1339 }
1340
1341 if (scan_res->fetch_time.sec == 0)
1342 os_get_reltime(&scan_res->fetch_time);
1343
1344 filter_scan_res(wpa_s, scan_res);
1345
1346 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1347 struct neighbor_report *nei;
1348
1349 nei = &wpa_s->wnm_neighbor_report_elements[i];
1350 if (nei->preference_present && nei->preference == 0)
1351 continue;
1352
1353 for (j = 0; j < scan_res->num; j++) {
1354 struct wpa_scan_res *res;
1355 const u8 *ssid_ie;
1356
1357 res = scan_res->res[j];
1358 if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
1359 res->age > WNM_SCAN_RESULT_AGE * 1000)
1360 continue;
1361 bss = wpa_s->current_bss;
1362 ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001363 if (bss && ssid_ie && ssid_ie[1] &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001364 (bss->ssid_len != ssid_ie[1] ||
1365 os_memcmp(bss->ssid, ssid_ie + 2,
1366 bss->ssid_len) != 0))
Hai Shaloma20dcd72022-02-04 13:43:00 -08001367 continue; /* Skip entries for other ESSs */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001368
1369 /* Potential candidate found */
1370 found = 1;
1371 scan_snr(res);
1372 scan_est_throughput(wpa_s, res);
1373 wpa_bss_update_scan_res(wpa_s, res,
1374 &scan_res->fetch_time);
1375 }
1376 }
1377
1378 wpa_scan_results_free(scan_res);
1379 if (!found) {
1380 wpa_dbg(wpa_s, MSG_DEBUG,
1381 "WNM: No transition candidate matches existing scan results");
1382 return 0;
1383 }
1384
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001385 bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE, NULL);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001386 if (!bss) {
1387 wpa_dbg(wpa_s, MSG_DEBUG,
1388 "WNM: Comparison of scan results against transition candidates did not find matches");
1389 return 0;
1390 }
1391
1392 /* Associate to the network */
1393 wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
1394 return 1;
1395}
1396
1397
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001398static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1399 const u8 *pos, const u8 *end,
1400 int reply)
1401{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001402 unsigned int beacon_int;
1403 u8 valid_int;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001404#ifdef CONFIG_MBO
1405 const u8 *vendor;
1406#endif /* CONFIG_MBO */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001407
Hai Shalomc3565922019-10-28 11:58:20 -07001408 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
Hai Shalom81f62d82019-07-22 12:10:00 -07001409 return;
1410
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001411 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001412 return;
1413
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001414#ifdef CONFIG_MBO
1415 wpa_s->wnm_mbo_trans_reason_present = 0;
1416 wpa_s->wnm_mbo_transition_reason = 0;
Sunil Ravi4018d712019-12-06 18:01:21 -08001417 wpa_s->wnm_mbo_cell_pref_present = 0;
1418 wpa_s->wnm_mbo_cell_preference = 0;
1419 wpa_s->wnm_mbo_assoc_retry_delay_present = 0;
1420 wpa_s->wnm_mbo_assoc_retry_delay_sec = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001421#endif /* CONFIG_MBO */
1422
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001423 if (wpa_s->current_bss)
1424 beacon_int = wpa_s->current_bss->beacon_int;
1425 else
1426 beacon_int = 100; /* best guess */
1427
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001428 wpa_s->wnm_dialog_token = pos[0];
1429 wpa_s->wnm_mode = pos[1];
1430 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001431 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001432 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001433
1434 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1435 "dialog_token=%u request_mode=0x%x "
1436 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001437 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001438 wpa_s->wnm_dissoc_timer, valid_int);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001439
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001440#if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1441 if (wpa_s->reject_btm_req_reason) {
1442 wpa_printf(MSG_INFO,
1443 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1444 wpa_s->reject_btm_req_reason);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001445 wnm_send_bss_transition_mgmt_resp(
1446 wpa_s, wpa_s->wnm_dialog_token,
1447 wpa_s->reject_btm_req_reason,
1448 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001449 return;
1450 }
1451#endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1452
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001453 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001454
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001455 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001456 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001457 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1458 return;
1459 }
1460 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001461 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001462 }
1463
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001464 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001465 char url[256];
Sunil8cd6f4d2022-06-28 18:40:46 +00001466 u8 url_len;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001467
Sunil8cd6f4d2022-06-28 18:40:46 +00001468 if (end - pos < 1) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001469 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1470 "Management Request (URL)");
1471 return;
1472 }
Sunil8cd6f4d2022-06-28 18:40:46 +00001473 url_len = *pos++;
1474 if (url_len > end - pos) {
1475 wpa_printf(MSG_DEBUG,
1476 "WNM: Invalid BSS Transition Management Request (URL truncated)");
1477 return;
1478 }
1479 os_memcpy(url, pos, url_len);
1480 url[url_len] = '\0';
1481 pos += url_len;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001482
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001483 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1484 wpa_sm_pmf_enabled(wpa_s->wpa),
1485 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001486 }
1487
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001488#ifdef CONFIG_MBO
1489 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1490 if (vendor) {
1491 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001492 }
1493#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07001494 if (wpa_s->conf->btm_offload) {
1495 wpa_printf(MSG_INFO,
1496 "WNM: BTM offload enabled. Notify status and return");
1497 wpa_s->bss_tm_status = WNM_BSS_TM_ACCEPT;
1498 wpas_notify_bss_tm_status(wpa_s);
1499 return;
1500 }
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001501
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001502 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001503 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001504 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
1505 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001506 /* TODO: mark current BSS less preferred for
1507 * selection */
1508 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
1509 wpa_supplicant_req_scan(wpa_s, 0, 0);
1510 }
1511 }
1512
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001513 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001514 unsigned int valid_ms;
1515
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001516 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001517 wnm_deallocate_memory(wpa_s);
1518 wpa_s->wnm_neighbor_report_elements = os_calloc(
1519 WNM_MAX_NEIGHBOR_REPORT,
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001520 sizeof(struct neighbor_report));
1521 if (wpa_s->wnm_neighbor_report_elements == NULL)
1522 return;
1523
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001524 while (end - pos >= 2 &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001525 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
1526 {
1527 u8 tag = *pos++;
1528 u8 len = *pos++;
1529
1530 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
1531 tag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001532 if (len > end - pos) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001533 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1534 return;
1535 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001536 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1537 struct neighbor_report *rep;
1538 rep = &wpa_s->wnm_neighbor_report_elements[
1539 wpa_s->wnm_num_neighbor_report];
1540 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001541 wpa_s->wnm_num_neighbor_report++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001542#ifdef CONFIG_MBO
1543 if (wpa_s->wnm_mbo_trans_reason_present &&
1544 wpa_s->wnm_num_neighbor_report == 1) {
1545 rep->is_first = 1;
1546 wpa_printf(MSG_DEBUG,
1547 "WNM: First transition candidate is "
1548 MACSTR, MAC2STR(rep->bssid));
1549 }
1550#endif /* CONFIG_MBO */
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001551 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001552
1553 pos += len;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001554 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001555
1556 if (!wpa_s->wnm_num_neighbor_report) {
1557 wpa_printf(MSG_DEBUG,
1558 "WNM: Candidate list included bit is set, but no candidates found");
1559 wnm_send_bss_transition_mgmt_resp(
1560 wpa_s, wpa_s->wnm_dialog_token,
1561 WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001562 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1563 NULL);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001564 return;
1565 }
1566
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001567 wnm_sort_cand_list(wpa_s);
1568 wnm_dump_cand_list(wpa_s);
1569 valid_ms = valid_int * beacon_int * 128 / 125;
1570 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1571 valid_ms);
1572 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1573 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
1574 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
1575 wpa_s->wnm_cand_valid_until.sec +=
1576 wpa_s->wnm_cand_valid_until.usec / 1000000;
1577 wpa_s->wnm_cand_valid_until.usec %= 1000000;
1578 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001579
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001580 /*
1581 * Fetch the latest scan results from the kernel and check for
1582 * candidates based on those results first. This can help in
1583 * finding more up-to-date information should the driver has
1584 * done some internal scanning operations after the last scan
1585 * result update in wpa_supplicant.
1586 */
1587 if (wnm_fetch_scan_results(wpa_s) > 0)
1588 return;
1589
1590 /*
1591 * Try to use previously received scan results, if they are
1592 * recent enough to use for a connection.
1593 */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001594 if (wpa_s->last_scan_res_used > 0) {
1595 struct os_reltime now;
1596
1597 os_get_reltime(&now);
1598 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
1599 wpa_printf(MSG_DEBUG,
1600 "WNM: Try to use recent scan results");
1601 if (wnm_scan_process(wpa_s, 0) > 0)
1602 return;
1603 wpa_printf(MSG_DEBUG,
1604 "WNM: No match in previous scan results - try a new scan");
1605 }
1606 }
1607
1608 wnm_set_scan_freqs(wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001609 if (wpa_s->wnm_num_neighbor_report == 1) {
1610 os_memcpy(wpa_s->next_scan_bssid,
1611 wpa_s->wnm_neighbor_report_elements[0].bssid,
1612 ETH_ALEN);
1613 wpa_printf(MSG_DEBUG,
1614 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1615 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1616 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001617 wpa_supplicant_req_scan(wpa_s, 0, 0);
1618 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001619 enum bss_trans_mgmt_status_code status;
1620 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
1621 status = WNM_BSS_TM_ACCEPT;
1622 else {
1623 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1624 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1625 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001626 wnm_send_bss_transition_mgmt_resp(
1627 wpa_s, wpa_s->wnm_dialog_token, status,
1628 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001629 }
1630}
1631
1632
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001633#define BTM_QUERY_MIN_SIZE 4
1634
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001635int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001636 u8 query_reason,
1637 const char *btm_candidates,
1638 int cand_list)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001639{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001640 struct wpabuf *buf;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001641 int ret;
1642
1643 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001644 MACSTR " query_reason=%u%s",
1645 MAC2STR(wpa_s->bssid), query_reason,
1646 cand_list ? " candidate list" : "");
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001647
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001648 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1649 if (!buf)
1650 return -1;
1651
1652 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1653 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1654 wpabuf_put_u8(buf, 1);
1655 wpabuf_put_u8(buf, query_reason);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001656
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001657 if (cand_list)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001658 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001659
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001660 if (btm_candidates) {
1661 const size_t max_len = 1000;
1662
1663 ret = wpabuf_resize(&buf, max_len);
1664 if (ret < 0) {
1665 wpabuf_free(buf);
1666 return ret;
1667 }
1668
1669 ret = ieee802_11_parse_candidate_list(btm_candidates,
1670 wpabuf_put(buf, 0),
1671 max_len);
1672 if (ret < 0) {
1673 wpabuf_free(buf);
1674 return ret;
1675 }
1676
1677 wpabuf_put(buf, ret);
1678 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001679
1680 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1681 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001682 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001683
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001684 wpabuf_free(buf);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001685 return ret;
1686}
1687
1688
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001689static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1690 const u8 *sa, const u8 *data,
1691 int len)
1692{
1693 const u8 *pos, *end, *next;
1694 u8 ie, ie_len;
1695
1696 pos = data;
1697 end = data + len;
1698
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001699 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001700 ie = *pos++;
1701 ie_len = *pos++;
1702 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1703 ie, ie_len);
1704 if (ie_len > end - pos) {
1705 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1706 "subelement");
1707 break;
1708 }
1709 next = pos + ie_len;
1710 if (ie_len < 4) {
1711 pos = next;
1712 continue;
1713 }
1714 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1715 WPA_GET_BE24(pos), pos[3]);
1716
1717#ifdef CONFIG_HS20
1718 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1719 WPA_GET_BE24(pos) == OUI_WFA &&
1720 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1721 /* Subscription Remediation subelement */
1722 const u8 *ie_end;
1723 u8 url_len;
1724 char *url;
1725 u8 osu_method;
1726
1727 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1728 "subelement");
1729 ie_end = pos + ie_len;
1730 pos += 4;
1731 url_len = *pos++;
1732 if (url_len == 0) {
1733 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1734 url = NULL;
1735 osu_method = 1;
1736 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001737 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001738 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1739 url_len,
1740 (int) (ie_end - pos));
1741 break;
1742 }
1743 url = os_malloc(url_len + 1);
1744 if (url == NULL)
1745 break;
1746 os_memcpy(url, pos, url_len);
1747 url[url_len] = '\0';
1748 osu_method = pos[url_len];
1749 }
1750 hs20_rx_subscription_remediation(wpa_s, url,
1751 osu_method);
1752 os_free(url);
1753 pos = next;
1754 continue;
1755 }
1756
1757 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1758 WPA_GET_BE24(pos) == OUI_WFA &&
1759 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1760 const u8 *ie_end;
1761 u8 url_len;
1762 char *url;
1763 u8 code;
1764 u16 reauth_delay;
1765
1766 ie_end = pos + ie_len;
1767 pos += 4;
1768 code = *pos++;
1769 reauth_delay = WPA_GET_LE16(pos);
1770 pos += 2;
1771 url_len = *pos++;
1772 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1773 "Imminent - Reason Code %u "
1774 "Re-Auth Delay %u URL Length %u",
1775 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001776 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001777 break;
1778 url = os_malloc(url_len + 1);
1779 if (url == NULL)
1780 break;
1781 os_memcpy(url, pos, url_len);
1782 url[url_len] = '\0';
1783 hs20_rx_deauth_imminent_notice(wpa_s, code,
1784 reauth_delay, url);
1785 os_free(url);
1786 pos = next;
1787 continue;
1788 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001789
1790 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1791 WPA_GET_BE24(pos) == OUI_WFA &&
1792 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1793 const u8 *ie_end;
1794 u8 url_len;
1795 char *url;
1796
1797 ie_end = pos + ie_len;
1798 pos += 4;
1799 url_len = *pos++;
1800 wpa_printf(MSG_DEBUG,
1801 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1802 url_len);
1803 if (url_len > ie_end - pos)
1804 break;
1805 url = os_malloc(url_len + 1);
1806 if (!url)
1807 break;
1808 os_memcpy(url, pos, url_len);
1809 url[url_len] = '\0';
1810 hs20_rx_t_c_acceptance(wpa_s, url);
1811 os_free(url);
1812 pos = next;
1813 continue;
1814 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001815#endif /* CONFIG_HS20 */
1816
1817 pos = next;
1818 }
1819}
1820
1821
1822static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1823 const u8 *sa, const u8 *frm, int len)
1824{
1825 const u8 *pos, *end;
1826 u8 dialog_token, type;
1827
1828 /* Dialog Token [1] | Type [1] | Subelements */
1829
1830 if (len < 2 || sa == NULL)
1831 return;
1832 end = frm + len;
1833 pos = frm;
1834 dialog_token = *pos++;
1835 type = *pos++;
1836
1837 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1838 "(dialog_token %u type %u sa " MACSTR ")",
1839 dialog_token, type, MAC2STR(sa));
1840 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1841 pos, end - pos);
1842
1843 if (wpa_s->wpa_state != WPA_COMPLETED ||
1844 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1845 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1846 "from our AP - ignore it");
1847 return;
1848 }
1849
1850 switch (type) {
1851 case 1:
1852 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1853 break;
1854 default:
1855 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1856 "WNM-Notification type %u", type);
1857 break;
1858 }
1859}
1860
1861
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001862static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1863 const u8 *sa, const u8 *frm,
1864 int len)
1865{
1866 u8 dialog_token, req_info, auto_report, timeout;
1867
1868 if (!wpa_s->conf->coloc_intf_reporting)
1869 return;
1870
1871 /* Dialog Token [1] | Request Info [1] */
1872
1873 if (len < 2)
1874 return;
1875 dialog_token = frm[0];
1876 req_info = frm[1];
1877 auto_report = req_info & 0x03;
1878 timeout = req_info >> 2;
1879
1880 wpa_dbg(wpa_s, MSG_DEBUG,
1881 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1882 dialog_token, auto_report, timeout, MAC2STR(sa));
1883
1884 if (dialog_token == 0)
1885 return; /* only nonzero values are used for request */
1886
1887 if (wpa_s->wpa_state != WPA_COMPLETED ||
1888 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1889 wpa_dbg(wpa_s, MSG_DEBUG,
1890 "WNM: Collocated Interference Request frame not from current AP - ignore it");
1891 return;
1892 }
1893
1894 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1895 dialog_token, auto_report, timeout);
1896 wpa_s->coloc_intf_dialog_token = dialog_token;
1897 wpa_s->coloc_intf_auto_report = auto_report;
1898 wpa_s->coloc_intf_timeout = timeout;
1899}
1900
1901
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001902void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001903 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001904{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001905 const u8 *pos, *end;
1906 u8 act;
1907
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001908 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001909 return;
1910
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001911 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001912 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001913 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001914
1915 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001916 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001917 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001918 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001919 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1920 "frame");
1921 return;
1922 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001923
1924 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001925 case WNM_BSS_TRANS_MGMT_REQ:
1926 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001927 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001928 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001929 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001930 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001931 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001932 case WNM_NOTIFICATION_REQ:
1933 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1934 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001935 case WNM_COLLOCATED_INTERFERENCE_REQ:
1936 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
1937 end - pos);
1938 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001939 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001940 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001941 break;
1942 }
1943}
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001944
1945
1946int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
1947 const struct wpabuf *elems)
1948{
1949 struct wpabuf *buf;
1950 int ret;
1951
1952 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
1953 return -1;
1954
1955 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
1956 MACSTR " (dialog token %u)",
1957 MAC2STR(wpa_s->bssid), dialog_token);
1958
1959 buf = wpabuf_alloc(3 + wpabuf_len(elems));
1960 if (!buf)
1961 return -1;
1962
1963 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1964 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
1965 wpabuf_put_u8(buf, dialog_token);
1966 wpabuf_put_buf(buf, elems);
1967
1968 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1969 wpa_s->own_addr, wpa_s->bssid,
1970 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1971 wpabuf_free(buf);
1972 return ret;
1973}
1974
1975
1976void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
1977 struct wpabuf *elems)
1978{
1979 wpabuf_free(wpa_s->coloc_intf_elems);
1980 if (elems && wpabuf_len(elems) == 0) {
1981 wpabuf_free(elems);
1982 elems = NULL;
1983 }
1984 wpa_s->coloc_intf_elems = elems;
1985
1986 if (wpa_s->conf->coloc_intf_reporting && wpa_s->coloc_intf_elems &&
1987 wpa_s->coloc_intf_dialog_token &&
1988 (wpa_s->coloc_intf_auto_report == 1 ||
1989 wpa_s->coloc_intf_auto_report == 3)) {
1990 /* TODO: Check that there has not been less than
1991 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
1992 */
1993 wnm_send_coloc_intf_report(wpa_s,
1994 wpa_s->coloc_intf_dialog_token,
1995 wpa_s->coloc_intf_elems);
1996 }
1997}
1998
1999
2000void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
2001{
2002#ifdef CONFIG_WNM
2003 wpa_s->coloc_intf_dialog_token = 0;
2004 wpa_s->coloc_intf_auto_report = 0;
2005#endif /* CONFIG_WNM */
2006}