blob: 4c7e6dcdefcb6a71370429b428451b9683577d70 [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;
527 }
528}
529
530
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800531static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
532{
533 struct wpa_bss *bss = wpa_s->current_bss;
534 const char *country = NULL;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800535 int freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800536
537 if (bss) {
538 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
539
540 if (elem && elem[1] >= 2)
541 country = (const char *) (elem + 2);
542 }
543
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800544 freq = ieee80211_chan_to_freq(country, op_class, chan);
545 if (freq <= 0 && op_class == 0) {
546 /*
547 * Some APs do not advertise correct operating class
548 * information. Try to determine the most likely operating
549 * frequency based on the channel number.
550 */
551 if (chan >= 1 && chan <= 13)
552 freq = 2407 + chan * 5;
553 else if (chan == 14)
554 freq = 2484;
Hai Shalom60840252021-02-19 19:02:11 -0800555 else if (chan >= 36 && chan <= 177)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800556 freq = 5000 + chan * 5;
557 }
558 return freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800559}
560
561
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700562static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
563 const u8 *pos, u8 len,
564 struct neighbor_report *rep)
565{
566 u8 left = len;
567
568 if (left < 13) {
569 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
570 return;
571 }
572
573 os_memcpy(rep->bssid, pos, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800574 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700575 rep->regulatory_class = *(pos + 10);
576 rep->channel_number = *(pos + 11);
577 rep->phy_type = *(pos + 12);
578
579 pos += 13;
580 left -= 13;
581
582 while (left >= 2) {
583 u8 id, elen;
584
585 id = *pos++;
586 elen = *pos++;
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700587 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
588 left -= 2;
589 if (elen > left) {
590 wpa_printf(MSG_DEBUG,
591 "WNM: Truncated neighbor report subelement");
592 break;
593 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700594 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700595 left -= elen;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700596 pos += elen;
597 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800598
599 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
600 rep->channel_number);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700601}
602
603
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700604static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700605{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700606 unsigned int i;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700607
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700608 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
609 wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
610}
611
612
613static struct wpa_bss * get_first_acceptable(struct wpa_supplicant *wpa_s)
614{
615 unsigned int i;
616 struct neighbor_report *nei;
617
618 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
619 nei = &wpa_s->wnm_neighbor_report_elements[i];
620 if (nei->acceptable)
621 return wpa_bss_get_bssid(wpa_s, nei->bssid);
622 }
623
624 return NULL;
625}
626
627
628#ifdef CONFIG_MBO
629static struct wpa_bss *
630get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
631 enum mbo_transition_reject_reason *reason)
632{
633 struct wpa_bss *target = NULL;
634 struct wpa_bss_trans_info params;
635 struct wpa_bss_candidate_info *info = NULL;
636 struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
637 u8 *first_candidate_bssid = NULL, *pos;
638 unsigned int i;
639
640 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
641 params.n_candidates = 0;
642 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
643 if (!params.bssid)
644 return NULL;
645
646 pos = params.bssid;
647 for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
648 if (nei->is_first)
649 first_candidate_bssid = nei->bssid;
650 if (!nei->acceptable)
651 continue;
652 os_memcpy(pos, nei->bssid, ETH_ALEN);
653 pos += ETH_ALEN;
654 params.n_candidates++;
655 }
656
657 if (!params.n_candidates)
658 goto end;
659
660 info = wpa_drv_get_bss_trans_status(wpa_s, &params);
661 if (!info) {
662 /* If failed to get candidate BSS transition status from driver,
663 * get the first acceptable candidate from wpa_supplicant.
664 */
665 target = wpa_bss_get_bssid(wpa_s, params.bssid);
666 goto end;
667 }
668
669 /* Get the first acceptable candidate from driver */
670 for (i = 0; i < info->num; i++) {
671 if (info->candidates[i].is_accept) {
672 target = wpa_bss_get_bssid(wpa_s,
673 info->candidates[i].bssid);
674 goto end;
675 }
676 }
677
678 /* If Disassociation Imminent is set and driver rejects all the
679 * candidate select first acceptable candidate which has
680 * rssi > disassoc_imminent_rssi_threshold
681 */
682 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
683 for (i = 0; i < info->num; i++) {
684 target = wpa_bss_get_bssid(wpa_s,
685 info->candidates[i].bssid);
686 if (target &&
687 (target->level <
688 wpa_s->conf->disassoc_imminent_rssi_threshold))
689 continue;
690 goto end;
691 }
692 }
693
694 /* While sending BTM reject use reason code of the first candidate
695 * received in BTM request frame
696 */
697 if (reason) {
698 for (i = 0; i < info->num; i++) {
699 if (first_candidate_bssid &&
700 os_memcmp(first_candidate_bssid,
701 info->candidates[i].bssid, ETH_ALEN) == 0)
702 {
703 *reason = info->candidates[i].reject_reason;
704 break;
705 }
706 }
707 }
708
709 target = NULL;
710
711end:
712 os_free(params.bssid);
713 if (info) {
714 os_free(info->candidates);
715 os_free(info);
716 }
717 return target;
718}
719#endif /* CONFIG_MBO */
720
721
722static struct wpa_bss *
723compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs,
724 enum mbo_transition_reject_reason *reason)
725{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800726 u8 i;
727 struct wpa_bss *bss = wpa_s->current_bss;
728 struct wpa_bss *target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700729
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800730 if (!bss)
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700731 return NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700732
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800733 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800734 MAC2STR(wpa_s->bssid), bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800735
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700736 wnm_clear_acceptable(wpa_s);
737
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800738 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
739 struct neighbor_report *nei;
740
741 nei = &wpa_s->wnm_neighbor_report_elements[i];
742 if (nei->preference_present && nei->preference == 0) {
743 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
744 MAC2STR(nei->bssid));
745 continue;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700746 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800747
748 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
749 if (!target) {
750 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
751 " (pref %d) not found in scan results",
752 MAC2STR(nei->bssid),
753 nei->preference_present ? nei->preference :
754 -1);
755 continue;
756 }
757
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700758 if (age_secs) {
759 struct os_reltime now;
760
761 if (os_get_reltime(&now) == 0 &&
762 os_reltime_expired(&now, &target->last_update,
763 age_secs)) {
764 wpa_printf(MSG_DEBUG,
765 "Candidate BSS is more than %ld seconds old",
766 age_secs);
767 continue;
768 }
769 }
770
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800771 if (bss->ssid_len != target->ssid_len ||
772 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
773 /*
774 * TODO: Could consider allowing transition to another
775 * ESS if PMF was enabled for the association.
776 */
777 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
778 " (pref %d) in different ESS",
779 MAC2STR(nei->bssid),
780 nei->preference_present ? nei->preference :
781 -1);
782 continue;
783 }
784
Dmitry Shmidte4663042016-04-04 10:07:49 -0700785 if (wpa_s->current_ssid &&
786 !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800787 1, 0)) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700788 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
789 " (pref %d) does not match the current network profile",
790 MAC2STR(nei->bssid),
791 nei->preference_present ? nei->preference :
792 -1);
793 continue;
794 }
795
Hai Shalom74f70d42019-02-11 14:42:39 -0800796 if (wpa_is_bss_tmp_disallowed(wpa_s, target)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800797 wpa_printf(MSG_DEBUG,
798 "MBO: Candidate BSS " MACSTR
799 " retry delay is not over yet",
800 MAC2STR(nei->bssid));
801 continue;
802 }
803
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800804 if (target->level < bss->level && target->level < -80) {
805 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
806 " (pref %d) does not have sufficient signal level (%d)",
807 MAC2STR(nei->bssid),
808 nei->preference_present ? nei->preference :
809 -1,
810 target->level);
811 continue;
812 }
813
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700814 nei->acceptable = 1;
815 }
816
817#ifdef CONFIG_MBO
818 if (wpa_s->wnm_mbo_trans_reason_present)
819 target = get_mbo_transition_candidate(wpa_s, reason);
820 else
821 target = get_first_acceptable(wpa_s);
822#else /* CONFIG_MBO */
823 target = get_first_acceptable(wpa_s);
824#endif /* CONFIG_MBO */
825
826 if (target) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800827 wpa_printf(MSG_DEBUG,
828 "WNM: Found an acceptable preferred transition candidate BSS "
829 MACSTR " (RSSI %d)",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700830 MAC2STR(target->bssid), target->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700831 }
832
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700833 return target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700834}
835
836
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800837static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
838{
839 const u8 *ie_a, *ie_b;
840
841 if (!a || !b)
842 return 0;
843
844 ie_a = wpa_bss_get_ie(a, eid);
845 ie_b = wpa_bss_get_ie(b, eid);
846
847 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
848 return 0;
849
850 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
851}
852
853
854static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
855{
856 u32 info = 0;
857
858 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
859
860 /*
861 * Leave the security and key scope bits unset to indicate that the
862 * security information is not available.
863 */
864
865 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
866 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
867 if (bss->caps & WLAN_CAPABILITY_QOS)
868 info |= NEI_REP_BSSID_INFO_QOS;
869 if (bss->caps & WLAN_CAPABILITY_APSD)
870 info |= NEI_REP_BSSID_INFO_APSD;
871 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
872 info |= NEI_REP_BSSID_INFO_RM;
873 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
874 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
875 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
876 info |= NEI_REP_BSSID_INFO_IMM_BA;
877 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
878 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
879 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
880 info |= NEI_REP_BSSID_INFO_HT;
881
882 return info;
883}
884
885
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700886static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
887 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
888 u8 pref)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800889{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700890 if (wpabuf_len(*buf) + 18 >
891 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800892 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700893 "WNM: No room in frame for Neighbor Report element");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800894 return -1;
895 }
896
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700897 if (wpabuf_resize(buf, 18) < 0) {
898 wpa_printf(MSG_DEBUG,
899 "WNM: Failed to allocate memory for Neighbor Report element");
900 return -1;
901 }
902
903 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800904 /* length: 13 for basic neighbor report + 3 for preference subelement */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700905 wpabuf_put_u8(*buf, 16);
906 wpabuf_put_data(*buf, bssid, ETH_ALEN);
907 wpabuf_put_le32(*buf, bss_info);
908 wpabuf_put_u8(*buf, op_class);
909 wpabuf_put_u8(*buf, chan);
910 wpabuf_put_u8(*buf, phy_type);
911 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
912 wpabuf_put_u8(*buf, 1);
913 wpabuf_put_u8(*buf, pref);
914 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800915}
916
917
918static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700919 struct wpa_bss *bss, struct wpabuf **buf,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800920 u8 pref)
921{
922 const u8 *ie;
923 u8 op_class, chan;
924 int sec_chan = 0, vht = 0;
925 enum phy_type phy_type;
926 u32 info;
927 struct ieee80211_ht_operation *ht_oper = NULL;
928 struct ieee80211_vht_operation *vht_oper = NULL;
929
930 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
931 if (ie && ie[1] >= 2) {
932 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
933
934 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
935 sec_chan = 1;
936 else if (ht_oper->ht_param &
937 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
938 sec_chan = -1;
939 }
940
941 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
942 if (ie && ie[1] >= 1) {
943 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
944
Hai Shalom81f62d82019-07-22 12:10:00 -0700945 if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
946 vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
947 vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800948 vht = vht_oper->vht_op_info_chwidth;
949 }
950
951 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
952 &chan) == NUM_HOSTAPD_MODES) {
953 wpa_printf(MSG_DEBUG,
954 "WNM: Cannot determine operating class and channel");
955 return -2;
956 }
957
958 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
959 (vht_oper != NULL));
960 if (phy_type == PHY_TYPE_UNSPECIFIED) {
961 wpa_printf(MSG_DEBUG,
962 "WNM: Cannot determine BSS phy type for Neighbor Report");
963 return -2;
964 }
965
966 info = wnm_get_bss_info(wpa_s, bss);
967
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700968 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
969 pref);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800970}
971
972
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700973static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800974{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800975 unsigned int i, pref = 255;
976 struct os_reltime now;
977 struct wpa_ssid *ssid = wpa_s->current_ssid;
978
979 if (!ssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700980 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800981
982 /*
983 * TODO: Define when scan results are no longer valid for the candidate
984 * list.
985 */
986 os_get_reltime(&now);
987 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700988 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800989
990 wpa_printf(MSG_DEBUG,
991 "WNM: Add candidate list to BSS Transition Management Response frame");
992 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
993 struct wpa_bss *bss = wpa_s->last_scan_res[i];
994 int res;
995
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800996 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700997 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800998 if (res == -2)
999 continue; /* could not build entry for BSS */
1000 if (res < 0)
1001 break; /* no more room for candidates */
1002 if (pref == 1)
1003 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001004 }
1005 }
1006
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001007 wpa_hexdump_buf(MSG_DEBUG,
1008 "WNM: BSS Transition Management Response candidate list",
1009 *buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001010}
1011
1012
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001013#define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
1014
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001015static void wnm_send_bss_transition_mgmt_resp(
1016 struct wpa_supplicant *wpa_s, u8 dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001017 enum bss_trans_mgmt_status_code status,
1018 enum mbo_transition_reject_reason reason,
1019 u8 delay, const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001020{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001021 struct wpabuf *buf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001022 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001023
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001024 wpa_printf(MSG_DEBUG,
1025 "WNM: Send BSS Transition Management Response to " MACSTR
1026 " dialog_token=%u status=%u reason=%u delay=%d",
1027 MAC2STR(wpa_s->bssid), dialog_token, status, reason, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001028 if (!wpa_s->current_bss) {
1029 wpa_printf(MSG_DEBUG,
1030 "WNM: Current BSS not known - drop response");
1031 return;
1032 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001033
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001034 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
1035 if (!buf) {
1036 wpa_printf(MSG_DEBUG,
1037 "WNM: Failed to allocate memory for BTM response");
1038 return;
1039 }
1040
Hai Shalom74f70d42019-02-11 14:42:39 -08001041 wpa_s->bss_tm_status = status;
1042 wpas_notify_bss_tm_status(wpa_s);
1043
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001044 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1045 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
1046 wpabuf_put_u8(buf, dialog_token);
1047 wpabuf_put_u8(buf, status);
1048 wpabuf_put_u8(buf, delay);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001049 if (target_bssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001050 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001051 } else if (status == WNM_BSS_TM_ACCEPT) {
1052 /*
1053 * P802.11-REVmc clarifies that the Target BSSID field is always
1054 * present when status code is zero, so use a fake value here if
1055 * no BSSID is yet known.
1056 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001057 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001058 }
1059
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001060 if (status == WNM_BSS_TM_ACCEPT)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001061 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001062
1063#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001064 if (status != WNM_BSS_TM_ACCEPT &&
1065 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
1066 u8 mbo[10];
1067 size_t ret;
1068
1069 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
1070 reason);
1071 if (ret) {
1072 if (wpabuf_resize(&buf, ret) < 0) {
1073 wpabuf_free(buf);
1074 wpa_printf(MSG_DEBUG,
1075 "WNM: Failed to allocate memory for MBO IE");
1076 return;
1077 }
1078
1079 wpabuf_put_data(buf, mbo, ret);
1080 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001081 }
1082#endif /* CONFIG_MBO */
1083
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001084 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1085 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001086 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001087 if (res < 0) {
1088 wpa_printf(MSG_DEBUG,
1089 "WNM: Failed to send BSS Transition Management Response");
1090 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001091
1092 wpabuf_free(buf);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001093}
1094
1095
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001096static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
1097 struct wpa_bss *bss, struct wpa_ssid *ssid,
1098 int after_new_scan)
1099{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001100 struct wpa_radio_work *already_connecting;
1101
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001102 wpa_dbg(wpa_s, MSG_DEBUG,
1103 "WNM: Transition to BSS " MACSTR
1104 " based on BSS Transition Management Request (old BSSID "
1105 MACSTR " after_new_scan=%d)",
1106 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1107
1108 /* Send the BSS Management Response - Accept */
1109 if (wpa_s->wnm_reply) {
1110 wpa_s->wnm_reply = 0;
1111 wpa_printf(MSG_DEBUG,
1112 "WNM: Sending successful BSS Transition Management Response");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001113 wnm_send_bss_transition_mgmt_resp(
1114 wpa_s, wpa_s->wnm_dialog_token, WNM_BSS_TM_ACCEPT,
1115 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1116 bss->bssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001117 }
1118
1119 if (bss == wpa_s->current_bss) {
1120 wpa_printf(MSG_DEBUG,
1121 "WNM: Already associated with the preferred candidate");
1122 wnm_deallocate_memory(wpa_s);
1123 return;
1124 }
1125
Hai Shaloma20dcd72022-02-04 13:43:00 -08001126 already_connecting = radio_work_pending(wpa_s, "sme-connect");
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001127 wpa_s->reassociate = 1;
1128 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1129 wpa_supplicant_connect(wpa_s, bss, ssid);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001130
1131 /*
1132 * Indicate that a BSS transition is in progress so scan results that
1133 * come in before the 'sme-connect' radio work gets executed do not
1134 * override the original connection attempt.
1135 */
1136 if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1137 wpa_s->bss_trans_mgmt_in_progress = true;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001138 wnm_deallocate_memory(wpa_s);
1139}
1140
1141
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001142int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001143{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001144 struct wpa_bss *bss;
1145 struct wpa_ssid *ssid = wpa_s->current_ssid;
1146 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001147 enum mbo_transition_reject_reason reason =
1148 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001149
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001150 if (!wpa_s->wnm_neighbor_report_elements)
1151 return 0;
1152
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001153 wpa_dbg(wpa_s, MSG_DEBUG,
1154 "WNM: Process scan results for BSS Transition Management");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001155 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
1156 &wpa_s->scan_trigger_time)) {
1157 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1158 wnm_deallocate_memory(wpa_s);
1159 return 0;
1160 }
1161
1162 if (!wpa_s->current_bss ||
1163 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
1164 ETH_ALEN) != 0) {
1165 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
1166 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001167 }
1168
1169 /* Compare the Neighbor Report and scan results */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001170 bss = compare_scan_neighbor_results(wpa_s, 0, &reason);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001171 if (!bss) {
1172 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1173 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1174 goto send_bss_resp_fail;
1175 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001176
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001177 /* Associate to the network */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001178 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001179 return 1;
1180
1181send_bss_resp_fail:
1182 if (!reply_on_fail)
1183 return 0;
1184
1185 /* Send reject response for all the failures */
1186
1187 if (wpa_s->wnm_reply) {
1188 wpa_s->wnm_reply = 0;
1189 wnm_send_bss_transition_mgmt_resp(wpa_s,
1190 wpa_s->wnm_dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001191 status, reason, 0, NULL);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001192 }
1193 wnm_deallocate_memory(wpa_s);
1194
1195 return 0;
1196}
1197
1198
1199static int cand_pref_compar(const void *a, const void *b)
1200{
1201 const struct neighbor_report *aa = a;
1202 const struct neighbor_report *bb = b;
1203
1204 if (!aa->preference_present && !bb->preference_present)
1205 return 0;
1206 if (!aa->preference_present)
1207 return 1;
1208 if (!bb->preference_present)
1209 return -1;
1210 if (bb->preference > aa->preference)
1211 return 1;
1212 if (bb->preference < aa->preference)
1213 return -1;
1214 return 0;
1215}
1216
1217
1218static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1219{
1220 if (!wpa_s->wnm_neighbor_report_elements)
1221 return;
1222 qsort(wpa_s->wnm_neighbor_report_elements,
1223 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1224 cand_pref_compar);
1225}
1226
1227
1228static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1229{
1230 unsigned int i;
1231
1232 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1233 if (!wpa_s->wnm_neighbor_report_elements)
1234 return;
1235 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1236 struct neighbor_report *nei;
1237
1238 nei = &wpa_s->wnm_neighbor_report_elements[i];
1239 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1240 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1241 i, MAC2STR(nei->bssid), nei->bssid_info,
1242 nei->regulatory_class,
1243 nei->channel_number, nei->phy_type,
1244 nei->preference_present ? nei->preference : -1,
1245 nei->freq);
1246 }
1247}
1248
1249
1250static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1251{
1252 unsigned int i;
1253
1254 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1255 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1256 int j;
1257
1258 for (j = 0; j < mode->num_channels; j++) {
1259 struct hostapd_channel_data *chan;
1260
1261 chan = &mode->channels[j];
1262 if (chan->freq == freq &&
1263 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1264 return 1;
1265 }
1266 }
1267
1268 return 0;
1269}
1270
1271
1272static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1273{
1274 int *freqs;
1275 int num_freqs = 0;
1276 unsigned int i;
1277
1278 if (!wpa_s->wnm_neighbor_report_elements)
1279 return;
1280
1281 if (wpa_s->hw.modes == NULL)
1282 return;
1283
1284 os_free(wpa_s->next_scan_freqs);
1285 wpa_s->next_scan_freqs = NULL;
1286
1287 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1288 if (freqs == NULL)
1289 return;
1290
1291 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1292 struct neighbor_report *nei;
1293
1294 nei = &wpa_s->wnm_neighbor_report_elements[i];
1295 if (nei->freq <= 0) {
1296 wpa_printf(MSG_DEBUG,
1297 "WNM: Unknown neighbor operating frequency for "
1298 MACSTR " - scan all channels",
1299 MAC2STR(nei->bssid));
1300 os_free(freqs);
1301 return;
1302 }
1303 if (chan_supported(wpa_s, nei->freq))
1304 add_freq(freqs, &num_freqs, nei->freq);
1305 }
1306
1307 if (num_freqs == 0) {
1308 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001309 return;
1310 }
1311
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001312 wpa_printf(MSG_DEBUG,
1313 "WNM: Scan %d frequencies based on transition candidate list",
1314 num_freqs);
1315 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001316}
1317
1318
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001319static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
1320{
1321 struct wpa_scan_results *scan_res;
1322 struct wpa_bss *bss;
1323 struct wpa_ssid *ssid = wpa_s->current_ssid;
1324 u8 i, found = 0;
1325 size_t j;
1326
1327 wpa_dbg(wpa_s, MSG_DEBUG,
1328 "WNM: Fetch current scan results from the driver for checking transition candidates");
1329 scan_res = wpa_drv_get_scan_results2(wpa_s);
1330 if (!scan_res) {
1331 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
1332 return 0;
1333 }
1334
1335 if (scan_res->fetch_time.sec == 0)
1336 os_get_reltime(&scan_res->fetch_time);
1337
1338 filter_scan_res(wpa_s, scan_res);
1339
1340 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1341 struct neighbor_report *nei;
1342
1343 nei = &wpa_s->wnm_neighbor_report_elements[i];
1344 if (nei->preference_present && nei->preference == 0)
1345 continue;
1346
1347 for (j = 0; j < scan_res->num; j++) {
1348 struct wpa_scan_res *res;
1349 const u8 *ssid_ie;
1350
1351 res = scan_res->res[j];
1352 if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
1353 res->age > WNM_SCAN_RESULT_AGE * 1000)
1354 continue;
1355 bss = wpa_s->current_bss;
1356 ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001357 if (bss && ssid_ie && ssid_ie[1] &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001358 (bss->ssid_len != ssid_ie[1] ||
1359 os_memcmp(bss->ssid, ssid_ie + 2,
1360 bss->ssid_len) != 0))
Hai Shaloma20dcd72022-02-04 13:43:00 -08001361 continue; /* Skip entries for other ESSs */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001362
1363 /* Potential candidate found */
1364 found = 1;
1365 scan_snr(res);
1366 scan_est_throughput(wpa_s, res);
1367 wpa_bss_update_scan_res(wpa_s, res,
1368 &scan_res->fetch_time);
1369 }
1370 }
1371
1372 wpa_scan_results_free(scan_res);
1373 if (!found) {
1374 wpa_dbg(wpa_s, MSG_DEBUG,
1375 "WNM: No transition candidate matches existing scan results");
1376 return 0;
1377 }
1378
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001379 bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE, NULL);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001380 if (!bss) {
1381 wpa_dbg(wpa_s, MSG_DEBUG,
1382 "WNM: Comparison of scan results against transition candidates did not find matches");
1383 return 0;
1384 }
1385
1386 /* Associate to the network */
1387 wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
1388 return 1;
1389}
1390
1391
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001392static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1393 const u8 *pos, const u8 *end,
1394 int reply)
1395{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001396 unsigned int beacon_int;
1397 u8 valid_int;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001398#ifdef CONFIG_MBO
1399 const u8 *vendor;
1400#endif /* CONFIG_MBO */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001401
Hai Shalomc3565922019-10-28 11:58:20 -07001402 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
Hai Shalom81f62d82019-07-22 12:10:00 -07001403 return;
1404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001405 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001406 return;
1407
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001408#ifdef CONFIG_MBO
1409 wpa_s->wnm_mbo_trans_reason_present = 0;
1410 wpa_s->wnm_mbo_transition_reason = 0;
Sunil Ravi4018d712019-12-06 18:01:21 -08001411 wpa_s->wnm_mbo_cell_pref_present = 0;
1412 wpa_s->wnm_mbo_cell_preference = 0;
1413 wpa_s->wnm_mbo_assoc_retry_delay_present = 0;
1414 wpa_s->wnm_mbo_assoc_retry_delay_sec = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001415#endif /* CONFIG_MBO */
1416
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001417 if (wpa_s->current_bss)
1418 beacon_int = wpa_s->current_bss->beacon_int;
1419 else
1420 beacon_int = 100; /* best guess */
1421
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001422 wpa_s->wnm_dialog_token = pos[0];
1423 wpa_s->wnm_mode = pos[1];
1424 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001425 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001426 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001427
1428 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1429 "dialog_token=%u request_mode=0x%x "
1430 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001431 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001432 wpa_s->wnm_dissoc_timer, valid_int);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001433
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001434#if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1435 if (wpa_s->reject_btm_req_reason) {
1436 wpa_printf(MSG_INFO,
1437 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1438 wpa_s->reject_btm_req_reason);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001439 wnm_send_bss_transition_mgmt_resp(
1440 wpa_s, wpa_s->wnm_dialog_token,
1441 wpa_s->reject_btm_req_reason,
1442 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001443 return;
1444 }
1445#endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1446
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001447 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001448
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001449 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001450 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001451 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1452 return;
1453 }
1454 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001455 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001456 }
1457
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001458 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001459 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001460
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001461 if (end - pos < 1 || 1 + pos[0] > end - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001462 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1463 "Management Request (URL)");
1464 return;
1465 }
1466 os_memcpy(url, pos + 1, pos[0]);
1467 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001468 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001469
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001470 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1471 wpa_sm_pmf_enabled(wpa_s->wpa),
1472 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001473 }
1474
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001475#ifdef CONFIG_MBO
1476 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1477 if (vendor) {
1478 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001479 }
1480#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07001481 if (wpa_s->conf->btm_offload) {
1482 wpa_printf(MSG_INFO,
1483 "WNM: BTM offload enabled. Notify status and return");
1484 wpa_s->bss_tm_status = WNM_BSS_TM_ACCEPT;
1485 wpas_notify_bss_tm_status(wpa_s);
1486 return;
1487 }
Dennis Jeone2cb56b2020-10-23 21:23:01 +09001488
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001489 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001490 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001491 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
1492 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001493 /* TODO: mark current BSS less preferred for
1494 * selection */
1495 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
1496 wpa_supplicant_req_scan(wpa_s, 0, 0);
1497 }
1498 }
1499
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001500 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001501 unsigned int valid_ms;
1502
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001503 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001504 wnm_deallocate_memory(wpa_s);
1505 wpa_s->wnm_neighbor_report_elements = os_calloc(
1506 WNM_MAX_NEIGHBOR_REPORT,
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001507 sizeof(struct neighbor_report));
1508 if (wpa_s->wnm_neighbor_report_elements == NULL)
1509 return;
1510
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001511 while (end - pos >= 2 &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001512 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
1513 {
1514 u8 tag = *pos++;
1515 u8 len = *pos++;
1516
1517 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
1518 tag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001519 if (len > end - pos) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001520 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1521 return;
1522 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001523 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1524 struct neighbor_report *rep;
1525 rep = &wpa_s->wnm_neighbor_report_elements[
1526 wpa_s->wnm_num_neighbor_report];
1527 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001528 wpa_s->wnm_num_neighbor_report++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001529#ifdef CONFIG_MBO
1530 if (wpa_s->wnm_mbo_trans_reason_present &&
1531 wpa_s->wnm_num_neighbor_report == 1) {
1532 rep->is_first = 1;
1533 wpa_printf(MSG_DEBUG,
1534 "WNM: First transition candidate is "
1535 MACSTR, MAC2STR(rep->bssid));
1536 }
1537#endif /* CONFIG_MBO */
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001538 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001539
1540 pos += len;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001541 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001542
1543 if (!wpa_s->wnm_num_neighbor_report) {
1544 wpa_printf(MSG_DEBUG,
1545 "WNM: Candidate list included bit is set, but no candidates found");
1546 wnm_send_bss_transition_mgmt_resp(
1547 wpa_s, wpa_s->wnm_dialog_token,
1548 WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001549 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1550 NULL);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001551 return;
1552 }
1553
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001554 wnm_sort_cand_list(wpa_s);
1555 wnm_dump_cand_list(wpa_s);
1556 valid_ms = valid_int * beacon_int * 128 / 125;
1557 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1558 valid_ms);
1559 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1560 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
1561 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
1562 wpa_s->wnm_cand_valid_until.sec +=
1563 wpa_s->wnm_cand_valid_until.usec / 1000000;
1564 wpa_s->wnm_cand_valid_until.usec %= 1000000;
1565 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001566
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001567 /*
1568 * Fetch the latest scan results from the kernel and check for
1569 * candidates based on those results first. This can help in
1570 * finding more up-to-date information should the driver has
1571 * done some internal scanning operations after the last scan
1572 * result update in wpa_supplicant.
1573 */
1574 if (wnm_fetch_scan_results(wpa_s) > 0)
1575 return;
1576
1577 /*
1578 * Try to use previously received scan results, if they are
1579 * recent enough to use for a connection.
1580 */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001581 if (wpa_s->last_scan_res_used > 0) {
1582 struct os_reltime now;
1583
1584 os_get_reltime(&now);
1585 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
1586 wpa_printf(MSG_DEBUG,
1587 "WNM: Try to use recent scan results");
1588 if (wnm_scan_process(wpa_s, 0) > 0)
1589 return;
1590 wpa_printf(MSG_DEBUG,
1591 "WNM: No match in previous scan results - try a new scan");
1592 }
1593 }
1594
1595 wnm_set_scan_freqs(wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001596 if (wpa_s->wnm_num_neighbor_report == 1) {
1597 os_memcpy(wpa_s->next_scan_bssid,
1598 wpa_s->wnm_neighbor_report_elements[0].bssid,
1599 ETH_ALEN);
1600 wpa_printf(MSG_DEBUG,
1601 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1602 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1603 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001604 wpa_supplicant_req_scan(wpa_s, 0, 0);
1605 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001606 enum bss_trans_mgmt_status_code status;
1607 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
1608 status = WNM_BSS_TM_ACCEPT;
1609 else {
1610 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1611 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1612 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001613 wnm_send_bss_transition_mgmt_resp(
1614 wpa_s, wpa_s->wnm_dialog_token, status,
1615 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001616 }
1617}
1618
1619
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001620#define BTM_QUERY_MIN_SIZE 4
1621
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001622int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001623 u8 query_reason,
1624 const char *btm_candidates,
1625 int cand_list)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001626{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001627 struct wpabuf *buf;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001628 int ret;
1629
1630 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001631 MACSTR " query_reason=%u%s",
1632 MAC2STR(wpa_s->bssid), query_reason,
1633 cand_list ? " candidate list" : "");
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001634
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001635 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1636 if (!buf)
1637 return -1;
1638
1639 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1640 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1641 wpabuf_put_u8(buf, 1);
1642 wpabuf_put_u8(buf, query_reason);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001643
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001644 if (cand_list)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001645 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001646
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001647 if (btm_candidates) {
1648 const size_t max_len = 1000;
1649
1650 ret = wpabuf_resize(&buf, max_len);
1651 if (ret < 0) {
1652 wpabuf_free(buf);
1653 return ret;
1654 }
1655
1656 ret = ieee802_11_parse_candidate_list(btm_candidates,
1657 wpabuf_put(buf, 0),
1658 max_len);
1659 if (ret < 0) {
1660 wpabuf_free(buf);
1661 return ret;
1662 }
1663
1664 wpabuf_put(buf, ret);
1665 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001666
1667 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1668 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001669 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001670
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001671 wpabuf_free(buf);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001672 return ret;
1673}
1674
1675
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001676static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1677 const u8 *sa, const u8 *data,
1678 int len)
1679{
1680 const u8 *pos, *end, *next;
1681 u8 ie, ie_len;
1682
1683 pos = data;
1684 end = data + len;
1685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001686 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001687 ie = *pos++;
1688 ie_len = *pos++;
1689 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1690 ie, ie_len);
1691 if (ie_len > end - pos) {
1692 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1693 "subelement");
1694 break;
1695 }
1696 next = pos + ie_len;
1697 if (ie_len < 4) {
1698 pos = next;
1699 continue;
1700 }
1701 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1702 WPA_GET_BE24(pos), pos[3]);
1703
1704#ifdef CONFIG_HS20
1705 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1706 WPA_GET_BE24(pos) == OUI_WFA &&
1707 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1708 /* Subscription Remediation subelement */
1709 const u8 *ie_end;
1710 u8 url_len;
1711 char *url;
1712 u8 osu_method;
1713
1714 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1715 "subelement");
1716 ie_end = pos + ie_len;
1717 pos += 4;
1718 url_len = *pos++;
1719 if (url_len == 0) {
1720 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1721 url = NULL;
1722 osu_method = 1;
1723 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001724 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001725 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1726 url_len,
1727 (int) (ie_end - pos));
1728 break;
1729 }
1730 url = os_malloc(url_len + 1);
1731 if (url == NULL)
1732 break;
1733 os_memcpy(url, pos, url_len);
1734 url[url_len] = '\0';
1735 osu_method = pos[url_len];
1736 }
1737 hs20_rx_subscription_remediation(wpa_s, url,
1738 osu_method);
1739 os_free(url);
1740 pos = next;
1741 continue;
1742 }
1743
1744 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1745 WPA_GET_BE24(pos) == OUI_WFA &&
1746 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1747 const u8 *ie_end;
1748 u8 url_len;
1749 char *url;
1750 u8 code;
1751 u16 reauth_delay;
1752
1753 ie_end = pos + ie_len;
1754 pos += 4;
1755 code = *pos++;
1756 reauth_delay = WPA_GET_LE16(pos);
1757 pos += 2;
1758 url_len = *pos++;
1759 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1760 "Imminent - Reason Code %u "
1761 "Re-Auth Delay %u URL Length %u",
1762 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001763 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001764 break;
1765 url = os_malloc(url_len + 1);
1766 if (url == NULL)
1767 break;
1768 os_memcpy(url, pos, url_len);
1769 url[url_len] = '\0';
1770 hs20_rx_deauth_imminent_notice(wpa_s, code,
1771 reauth_delay, url);
1772 os_free(url);
1773 pos = next;
1774 continue;
1775 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001776
1777 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1778 WPA_GET_BE24(pos) == OUI_WFA &&
1779 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1780 const u8 *ie_end;
1781 u8 url_len;
1782 char *url;
1783
1784 ie_end = pos + ie_len;
1785 pos += 4;
1786 url_len = *pos++;
1787 wpa_printf(MSG_DEBUG,
1788 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1789 url_len);
1790 if (url_len > ie_end - pos)
1791 break;
1792 url = os_malloc(url_len + 1);
1793 if (!url)
1794 break;
1795 os_memcpy(url, pos, url_len);
1796 url[url_len] = '\0';
1797 hs20_rx_t_c_acceptance(wpa_s, url);
1798 os_free(url);
1799 pos = next;
1800 continue;
1801 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001802#endif /* CONFIG_HS20 */
1803
1804 pos = next;
1805 }
1806}
1807
1808
1809static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1810 const u8 *sa, const u8 *frm, int len)
1811{
1812 const u8 *pos, *end;
1813 u8 dialog_token, type;
1814
1815 /* Dialog Token [1] | Type [1] | Subelements */
1816
1817 if (len < 2 || sa == NULL)
1818 return;
1819 end = frm + len;
1820 pos = frm;
1821 dialog_token = *pos++;
1822 type = *pos++;
1823
1824 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1825 "(dialog_token %u type %u sa " MACSTR ")",
1826 dialog_token, type, MAC2STR(sa));
1827 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1828 pos, end - pos);
1829
1830 if (wpa_s->wpa_state != WPA_COMPLETED ||
1831 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1832 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1833 "from our AP - ignore it");
1834 return;
1835 }
1836
1837 switch (type) {
1838 case 1:
1839 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1840 break;
1841 default:
1842 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1843 "WNM-Notification type %u", type);
1844 break;
1845 }
1846}
1847
1848
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001849static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1850 const u8 *sa, const u8 *frm,
1851 int len)
1852{
1853 u8 dialog_token, req_info, auto_report, timeout;
1854
1855 if (!wpa_s->conf->coloc_intf_reporting)
1856 return;
1857
1858 /* Dialog Token [1] | Request Info [1] */
1859
1860 if (len < 2)
1861 return;
1862 dialog_token = frm[0];
1863 req_info = frm[1];
1864 auto_report = req_info & 0x03;
1865 timeout = req_info >> 2;
1866
1867 wpa_dbg(wpa_s, MSG_DEBUG,
1868 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1869 dialog_token, auto_report, timeout, MAC2STR(sa));
1870
1871 if (dialog_token == 0)
1872 return; /* only nonzero values are used for request */
1873
1874 if (wpa_s->wpa_state != WPA_COMPLETED ||
1875 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1876 wpa_dbg(wpa_s, MSG_DEBUG,
1877 "WNM: Collocated Interference Request frame not from current AP - ignore it");
1878 return;
1879 }
1880
1881 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1882 dialog_token, auto_report, timeout);
1883 wpa_s->coloc_intf_dialog_token = dialog_token;
1884 wpa_s->coloc_intf_auto_report = auto_report;
1885 wpa_s->coloc_intf_timeout = timeout;
1886}
1887
1888
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001889void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001890 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001891{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001892 const u8 *pos, *end;
1893 u8 act;
1894
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001895 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001896 return;
1897
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001898 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001899 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001900 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001901
1902 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001903 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001904 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001905 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001906 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1907 "frame");
1908 return;
1909 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001910
1911 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001912 case WNM_BSS_TRANS_MGMT_REQ:
1913 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001914 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001915 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001916 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001917 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001918 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001919 case WNM_NOTIFICATION_REQ:
1920 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1921 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001922 case WNM_COLLOCATED_INTERFERENCE_REQ:
1923 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
1924 end - pos);
1925 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001926 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001927 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001928 break;
1929 }
1930}
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001931
1932
1933int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
1934 const struct wpabuf *elems)
1935{
1936 struct wpabuf *buf;
1937 int ret;
1938
1939 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
1940 return -1;
1941
1942 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
1943 MACSTR " (dialog token %u)",
1944 MAC2STR(wpa_s->bssid), dialog_token);
1945
1946 buf = wpabuf_alloc(3 + wpabuf_len(elems));
1947 if (!buf)
1948 return -1;
1949
1950 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1951 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
1952 wpabuf_put_u8(buf, dialog_token);
1953 wpabuf_put_buf(buf, elems);
1954
1955 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1956 wpa_s->own_addr, wpa_s->bssid,
1957 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1958 wpabuf_free(buf);
1959 return ret;
1960}
1961
1962
1963void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
1964 struct wpabuf *elems)
1965{
1966 wpabuf_free(wpa_s->coloc_intf_elems);
1967 if (elems && wpabuf_len(elems) == 0) {
1968 wpabuf_free(elems);
1969 elems = NULL;
1970 }
1971 wpa_s->coloc_intf_elems = elems;
1972
1973 if (wpa_s->conf->coloc_intf_reporting && wpa_s->coloc_intf_elems &&
1974 wpa_s->coloc_intf_dialog_token &&
1975 (wpa_s->coloc_intf_auto_report == 1 ||
1976 wpa_s->coloc_intf_auto_report == 3)) {
1977 /* TODO: Check that there has not been less than
1978 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
1979 */
1980 wnm_send_coloc_intf_report(wpa_s,
1981 wpa_s->coloc_intf_dialog_token,
1982 wpa_s->coloc_intf_elems);
1983 }
1984}
1985
1986
1987void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
1988{
1989#ifdef CONFIG_WNM
1990 wpa_s->coloc_intf_dialog_token = 0;
1991 wpa_s->coloc_intf_auto_report = 0;
1992#endif /* CONFIG_WNM */
1993}