blob: 1c0399f4822e27c0a9db74b77c5b6e4c4fc54556 [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 }
124
125 oci_ie_len = OCV_OCI_EXTENDED_LEN;
126 oci_ie = os_zalloc(oci_ie_len);
127 if (!oci_ie) {
128 wpa_printf(MSG_WARNING,
129 "Failed to allocate buffer for for OCI element in WNM-Sleep Mode frame");
130 os_free(wnmsleep_ie);
131 os_free(wnmtfs_ie);
132 return -1;
133 }
134
135 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
136 os_free(wnmsleep_ie);
137 os_free(wnmtfs_ie);
138 os_free(oci_ie);
139 return -1;
140 }
141 }
142#endif /* CONFIG_OCV */
143
144 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len +
145 oci_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700146 if (mgmt == NULL) {
147 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
148 "WNM-Sleep Request action frame");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800149 os_free(wnmsleep_ie);
150 os_free(wnmtfs_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700151 return -1;
152 }
153
154 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
155 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
156 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
157 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
158 WLAN_FC_STYPE_ACTION);
159 mgmt->u.action.category = WLAN_ACTION_WNM;
160 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800161 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700162 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
163 wnmsleep_ie_len);
164 /* copy TFS IE here */
165 if (wnmtfs_ie_len > 0) {
166 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
167 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
168 }
169
Hai Shalom74f70d42019-02-11 14:42:39 -0800170#ifdef CONFIG_OCV
171 /* copy OCV OCI here */
172 if (oci_ie_len > 0) {
173 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
174 wnmsleep_ie_len + wnmtfs_ie_len, oci_ie, oci_ie_len);
175 }
176#endif /* CONFIG_OCV */
177
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700178 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
Hai Shalom74f70d42019-02-11 14:42:39 -0800179 wnmtfs_ie_len + oci_ie_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700180
181 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
182 wpa_s->own_addr, wpa_s->bssid,
183 &mgmt->u.action.category, len, 0);
184 if (res < 0)
185 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
186 "(action=%d, intval=%d)", action, intval);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800187 else
188 wpa_s->wnmsleep_used = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700189
190 os_free(wnmsleep_ie);
191 os_free(wnmtfs_ie);
Hai Shalom74f70d42019-02-11 14:42:39 -0800192 os_free(oci_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700193 os_free(mgmt);
194
195 return res;
196}
197
198
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800199static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800200 const u8 *tfsresp_ie_start,
201 const u8 *tfsresp_ie_end)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800202{
203 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
204 wpa_s->bssid, NULL, NULL);
205 /* remove GTK/IGTK ?? */
206
207 /* set the TFS Resp IE(s) */
208 if (tfsresp_ie_start && tfsresp_ie_end &&
209 tfsresp_ie_end - tfsresp_ie_start >= 0) {
210 u16 tfsresp_ie_len;
211 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
212 tfsresp_ie_start;
213 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
214 /* pass the TFS Resp IE(s) to driver for processing */
215 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
216 tfsresp_ie_start,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800217 tfsresp_ie_len,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800218 WNM_SLEEP_TFS_RESP_IE_SET))
219 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
220 }
221}
222
223
224static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
225 const u8 *frm, u16 key_len_total)
226{
227 u8 *ptr, *end;
228 u8 gtk_len;
229
230 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
231 NULL, NULL);
232
233 /* Install GTK/IGTK */
234
235 /* point to key data field */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800236 ptr = (u8 *) frm + 1 + 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800237 end = ptr + key_len_total;
238 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
239
Jouni Malinen17de68d2015-11-09 15:25:41 -0800240 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
241 wpa_msg(wpa_s, MSG_INFO,
242 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
243 return;
244 }
245
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800246 while (end - ptr > 1) {
247 if (2 + ptr[1] > end - ptr) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800248 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
249 "length");
250 if (end > ptr) {
251 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
252 ptr, end - ptr);
253 }
254 break;
255 }
256 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
257 if (ptr[1] < 11 + 5) {
258 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
259 "subelem");
260 break;
261 }
262 gtk_len = *(ptr + 4);
263 if (ptr[1] < 11 + gtk_len ||
264 gtk_len < 5 || gtk_len > 32) {
265 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
266 "subelem");
267 break;
268 }
269 wpa_wnmsleep_install_key(
270 wpa_s->wpa,
271 WNM_SLEEP_SUBELEM_GTK,
272 ptr);
273 ptr += 13 + gtk_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800274 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
275 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
276 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
277 "subelem");
278 break;
279 }
280 wpa_wnmsleep_install_key(wpa_s->wpa,
281 WNM_SLEEP_SUBELEM_IGTK, ptr);
282 ptr += 10 + WPA_IGTK_LEN;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800283 } else
284 break; /* skip the loop */
285 }
286}
287
288
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700289static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
290 const u8 *frm, int len)
291{
292 /*
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700293 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700294 * WNM-Sleep Mode IE | TFS Response IE
295 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800296 const u8 *pos = frm; /* point to payload after the action field */
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700297 u16 key_len_total;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700298 struct wnm_sleep_element *wnmsleep_ie = NULL;
299 /* multiple TFS Resp IE (assuming consecutive) */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800300 const u8 *tfsresp_ie_start = NULL;
301 const u8 *tfsresp_ie_end = NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -0800302#ifdef CONFIG_OCV
303 const u8 *oci_ie = NULL;
304 u8 oci_ie_len = 0;
305#endif /* CONFIG_OCV */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800306 size_t left;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700307
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800308 if (!wpa_s->wnmsleep_used) {
309 wpa_printf(MSG_DEBUG,
Jouni Malinen90bfa452017-09-22 11:25:02 +0300310 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800311 return;
312 }
313
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700314 if (len < 3)
315 return;
316 key_len_total = WPA_GET_LE16(frm + 1);
317
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
319 frm[0], key_len_total);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800320 left = len - 3;
321 if (key_len_total > left) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800322 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
323 return;
324 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800325 pos += 3 + key_len_total;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800326 while (pos - frm + 1 < len) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700327 u8 ie_len = *(pos + 1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800328 if (2 + ie_len > frm + len - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800329 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
330 break;
331 }
332 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800333 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700334 wnmsleep_ie = (struct wnm_sleep_element *) pos;
335 else if (*pos == WLAN_EID_TFS_RESP) {
336 if (!tfsresp_ie_start)
337 tfsresp_ie_start = pos;
338 tfsresp_ie_end = pos;
Hai Shalom74f70d42019-02-11 14:42:39 -0800339#ifdef CONFIG_OCV
340 } else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
341 pos[2] == WLAN_EID_EXT_OCV_OCI) {
342 oci_ie = pos + 3;
343 oci_ie_len = ie_len - 1;
344#endif /* CONFIG_OCV */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700345 } else
346 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
347 pos += ie_len + 2;
348 }
349
350 if (!wnmsleep_ie) {
351 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
352 return;
353 }
354
Hai Shalom74f70d42019-02-11 14:42:39 -0800355#ifdef CONFIG_OCV
356 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
357 wpa_sm_ocv_enabled(wpa_s->wpa)) {
358 struct wpa_channel_info ci;
359
360 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
361 wpa_msg(wpa_s, MSG_WARNING,
362 "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
363 return;
364 }
365
366 if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
367 channel_width_to_int(ci.chanwidth),
368 ci.seg1_idx) != 0) {
369 wpa_msg(wpa_s, MSG_WARNING, "WNM: %s", ocv_errorstr);
370 return;
371 }
372 }
373#endif /* CONFIG_OCV */
374
Jouni Malinen90bfa452017-09-22 11:25:02 +0300375 wpa_s->wnmsleep_used = 0;
376
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800377 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
378 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700379 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
380 "frame (action=%d, intval=%d)",
381 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800382 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
383 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
384 tfsresp_ie_end);
385 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
386 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700387 }
388 } else {
389 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
390 "(action=%d, intval=%d)",
391 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800392 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700393 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
394 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800395 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700396 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
397 wpa_s->bssid, NULL, NULL);
398 }
399}
400
401
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700402void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
403{
404 int i;
405
406 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700407 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700408 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
409 }
410
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700411 wpa_s->wnm_num_neighbor_report = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700412 os_free(wpa_s->wnm_neighbor_report_elements);
413 wpa_s->wnm_neighbor_report_elements = NULL;
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800414
415 wpabuf_free(wpa_s->coloc_intf_elems);
416 wpa_s->coloc_intf_elems = NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700417}
418
419
420static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
421 u8 id, u8 elen, const u8 *pos)
422{
423 switch (id) {
424 case WNM_NEIGHBOR_TSF:
425 if (elen < 2 + 2) {
426 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
427 break;
428 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800429 rep->tsf_offset = WPA_GET_LE16(pos);
430 rep->beacon_int = WPA_GET_LE16(pos + 2);
431 rep->tsf_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700432 break;
433 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
434 if (elen < 2) {
435 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
436 "country string");
437 break;
438 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800439 os_memcpy(rep->country, pos, 2);
440 rep->country_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700441 break;
442 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
443 if (elen < 1) {
444 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
445 "candidate");
446 break;
447 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800448 rep->preference = pos[0];
449 rep->preference_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700450 break;
451 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
Hai Shalomcb95c3f2019-02-04 12:53:10 -0800452 if (elen < 10) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700453 wpa_printf(MSG_DEBUG,
454 "WNM: Too short BSS termination duration");
Hai Shalomcb95c3f2019-02-04 12:53:10 -0800455 break;
456 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800457 rep->bss_term_tsf = WPA_GET_LE64(pos);
458 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
459 rep->bss_term_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700460 break;
461 case WNM_NEIGHBOR_BEARING:
462 if (elen < 8) {
463 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
464 "bearing");
465 break;
466 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800467 rep->bearing = WPA_GET_LE16(pos);
468 rep->distance = WPA_GET_LE32(pos + 2);
469 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
470 rep->bearing_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700471 break;
472 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700473 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700474 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
475 "pilot");
476 break;
477 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700478 os_free(rep->meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700479 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
480 if (rep->meas_pilot == NULL)
481 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700482 rep->meas_pilot->measurement_pilot = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700483 rep->meas_pilot->subelem_len = elen - 1;
484 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700485 break;
486 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700487 if (elen < 5) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700488 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
489 "capabilities");
490 break;
491 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800492 os_memcpy(rep->rm_capab, pos, 5);
493 rep->rm_capab_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700494 break;
495 case WNM_NEIGHBOR_MULTIPLE_BSSID:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700496 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700497 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
498 break;
499 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700500 os_free(rep->mul_bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700501 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
502 if (rep->mul_bssid == NULL)
503 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700504 rep->mul_bssid->max_bssid_indicator = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700505 rep->mul_bssid->subelem_len = elen - 1;
506 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700507 break;
508 }
509}
510
511
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800512static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
513{
514 struct wpa_bss *bss = wpa_s->current_bss;
515 const char *country = NULL;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800516 int freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800517
518 if (bss) {
519 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
520
521 if (elem && elem[1] >= 2)
522 country = (const char *) (elem + 2);
523 }
524
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800525 freq = ieee80211_chan_to_freq(country, op_class, chan);
526 if (freq <= 0 && op_class == 0) {
527 /*
528 * Some APs do not advertise correct operating class
529 * information. Try to determine the most likely operating
530 * frequency based on the channel number.
531 */
532 if (chan >= 1 && chan <= 13)
533 freq = 2407 + chan * 5;
534 else if (chan == 14)
535 freq = 2484;
536 else if (chan >= 36 && chan <= 169)
537 freq = 5000 + chan * 5;
538 }
539 return freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800540}
541
542
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700543static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
544 const u8 *pos, u8 len,
545 struct neighbor_report *rep)
546{
547 u8 left = len;
548
549 if (left < 13) {
550 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
551 return;
552 }
553
554 os_memcpy(rep->bssid, pos, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800555 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700556 rep->regulatory_class = *(pos + 10);
557 rep->channel_number = *(pos + 11);
558 rep->phy_type = *(pos + 12);
559
560 pos += 13;
561 left -= 13;
562
563 while (left >= 2) {
564 u8 id, elen;
565
566 id = *pos++;
567 elen = *pos++;
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700568 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
569 left -= 2;
570 if (elen > left) {
571 wpa_printf(MSG_DEBUG,
572 "WNM: Truncated neighbor report subelement");
573 break;
574 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700575 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700576 left -= elen;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700577 pos += elen;
578 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800579
580 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
581 rep->channel_number);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700582}
583
584
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700585static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700586{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700587 unsigned int i;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700588
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700589 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
590 wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
591}
592
593
594static struct wpa_bss * get_first_acceptable(struct wpa_supplicant *wpa_s)
595{
596 unsigned int i;
597 struct neighbor_report *nei;
598
599 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
600 nei = &wpa_s->wnm_neighbor_report_elements[i];
601 if (nei->acceptable)
602 return wpa_bss_get_bssid(wpa_s, nei->bssid);
603 }
604
605 return NULL;
606}
607
608
609#ifdef CONFIG_MBO
610static struct wpa_bss *
611get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
612 enum mbo_transition_reject_reason *reason)
613{
614 struct wpa_bss *target = NULL;
615 struct wpa_bss_trans_info params;
616 struct wpa_bss_candidate_info *info = NULL;
617 struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
618 u8 *first_candidate_bssid = NULL, *pos;
619 unsigned int i;
620
621 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
622 params.n_candidates = 0;
623 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
624 if (!params.bssid)
625 return NULL;
626
627 pos = params.bssid;
628 for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
629 if (nei->is_first)
630 first_candidate_bssid = nei->bssid;
631 if (!nei->acceptable)
632 continue;
633 os_memcpy(pos, nei->bssid, ETH_ALEN);
634 pos += ETH_ALEN;
635 params.n_candidates++;
636 }
637
638 if (!params.n_candidates)
639 goto end;
640
641 info = wpa_drv_get_bss_trans_status(wpa_s, &params);
642 if (!info) {
643 /* If failed to get candidate BSS transition status from driver,
644 * get the first acceptable candidate from wpa_supplicant.
645 */
646 target = wpa_bss_get_bssid(wpa_s, params.bssid);
647 goto end;
648 }
649
650 /* Get the first acceptable candidate from driver */
651 for (i = 0; i < info->num; i++) {
652 if (info->candidates[i].is_accept) {
653 target = wpa_bss_get_bssid(wpa_s,
654 info->candidates[i].bssid);
655 goto end;
656 }
657 }
658
659 /* If Disassociation Imminent is set and driver rejects all the
660 * candidate select first acceptable candidate which has
661 * rssi > disassoc_imminent_rssi_threshold
662 */
663 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
664 for (i = 0; i < info->num; i++) {
665 target = wpa_bss_get_bssid(wpa_s,
666 info->candidates[i].bssid);
667 if (target &&
668 (target->level <
669 wpa_s->conf->disassoc_imminent_rssi_threshold))
670 continue;
671 goto end;
672 }
673 }
674
675 /* While sending BTM reject use reason code of the first candidate
676 * received in BTM request frame
677 */
678 if (reason) {
679 for (i = 0; i < info->num; i++) {
680 if (first_candidate_bssid &&
681 os_memcmp(first_candidate_bssid,
682 info->candidates[i].bssid, ETH_ALEN) == 0)
683 {
684 *reason = info->candidates[i].reject_reason;
685 break;
686 }
687 }
688 }
689
690 target = NULL;
691
692end:
693 os_free(params.bssid);
694 if (info) {
695 os_free(info->candidates);
696 os_free(info);
697 }
698 return target;
699}
700#endif /* CONFIG_MBO */
701
702
703static struct wpa_bss *
704compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs,
705 enum mbo_transition_reject_reason *reason)
706{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800707 u8 i;
708 struct wpa_bss *bss = wpa_s->current_bss;
709 struct wpa_bss *target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700710
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800711 if (!bss)
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700712 return NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700713
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800714 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800715 MAC2STR(wpa_s->bssid), bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800716
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700717 wnm_clear_acceptable(wpa_s);
718
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800719 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
720 struct neighbor_report *nei;
721
722 nei = &wpa_s->wnm_neighbor_report_elements[i];
723 if (nei->preference_present && nei->preference == 0) {
724 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
725 MAC2STR(nei->bssid));
726 continue;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700727 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800728
729 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
730 if (!target) {
731 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
732 " (pref %d) not found in scan results",
733 MAC2STR(nei->bssid),
734 nei->preference_present ? nei->preference :
735 -1);
736 continue;
737 }
738
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700739 if (age_secs) {
740 struct os_reltime now;
741
742 if (os_get_reltime(&now) == 0 &&
743 os_reltime_expired(&now, &target->last_update,
744 age_secs)) {
745 wpa_printf(MSG_DEBUG,
746 "Candidate BSS is more than %ld seconds old",
747 age_secs);
748 continue;
749 }
750 }
751
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800752 if (bss->ssid_len != target->ssid_len ||
753 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
754 /*
755 * TODO: Could consider allowing transition to another
756 * ESS if PMF was enabled for the association.
757 */
758 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
759 " (pref %d) in different ESS",
760 MAC2STR(nei->bssid),
761 nei->preference_present ? nei->preference :
762 -1);
763 continue;
764 }
765
Dmitry Shmidte4663042016-04-04 10:07:49 -0700766 if (wpa_s->current_ssid &&
767 !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800768 1, 0)) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700769 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
770 " (pref %d) does not match the current network profile",
771 MAC2STR(nei->bssid),
772 nei->preference_present ? nei->preference :
773 -1);
774 continue;
775 }
776
Hai Shalom74f70d42019-02-11 14:42:39 -0800777 if (wpa_is_bss_tmp_disallowed(wpa_s, target)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800778 wpa_printf(MSG_DEBUG,
779 "MBO: Candidate BSS " MACSTR
780 " retry delay is not over yet",
781 MAC2STR(nei->bssid));
782 continue;
783 }
784
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800785 if (target->level < bss->level && target->level < -80) {
786 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
787 " (pref %d) does not have sufficient signal level (%d)",
788 MAC2STR(nei->bssid),
789 nei->preference_present ? nei->preference :
790 -1,
791 target->level);
792 continue;
793 }
794
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700795 nei->acceptable = 1;
796 }
797
798#ifdef CONFIG_MBO
799 if (wpa_s->wnm_mbo_trans_reason_present)
800 target = get_mbo_transition_candidate(wpa_s, reason);
801 else
802 target = get_first_acceptable(wpa_s);
803#else /* CONFIG_MBO */
804 target = get_first_acceptable(wpa_s);
805#endif /* CONFIG_MBO */
806
807 if (target) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800808 wpa_printf(MSG_DEBUG,
809 "WNM: Found an acceptable preferred transition candidate BSS "
810 MACSTR " (RSSI %d)",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700811 MAC2STR(target->bssid), target->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700812 }
813
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700814 return target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700815}
816
817
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800818static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
819{
820 const u8 *ie_a, *ie_b;
821
822 if (!a || !b)
823 return 0;
824
825 ie_a = wpa_bss_get_ie(a, eid);
826 ie_b = wpa_bss_get_ie(b, eid);
827
828 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
829 return 0;
830
831 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
832}
833
834
835static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
836{
837 u32 info = 0;
838
839 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
840
841 /*
842 * Leave the security and key scope bits unset to indicate that the
843 * security information is not available.
844 */
845
846 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
847 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
848 if (bss->caps & WLAN_CAPABILITY_QOS)
849 info |= NEI_REP_BSSID_INFO_QOS;
850 if (bss->caps & WLAN_CAPABILITY_APSD)
851 info |= NEI_REP_BSSID_INFO_APSD;
852 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
853 info |= NEI_REP_BSSID_INFO_RM;
854 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
855 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
856 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
857 info |= NEI_REP_BSSID_INFO_IMM_BA;
858 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
859 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
860 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
861 info |= NEI_REP_BSSID_INFO_HT;
862
863 return info;
864}
865
866
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700867static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
868 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
869 u8 pref)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800870{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700871 if (wpabuf_len(*buf) + 18 >
872 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800873 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700874 "WNM: No room in frame for Neighbor Report element");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800875 return -1;
876 }
877
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700878 if (wpabuf_resize(buf, 18) < 0) {
879 wpa_printf(MSG_DEBUG,
880 "WNM: Failed to allocate memory for Neighbor Report element");
881 return -1;
882 }
883
884 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800885 /* length: 13 for basic neighbor report + 3 for preference subelement */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700886 wpabuf_put_u8(*buf, 16);
887 wpabuf_put_data(*buf, bssid, ETH_ALEN);
888 wpabuf_put_le32(*buf, bss_info);
889 wpabuf_put_u8(*buf, op_class);
890 wpabuf_put_u8(*buf, chan);
891 wpabuf_put_u8(*buf, phy_type);
892 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
893 wpabuf_put_u8(*buf, 1);
894 wpabuf_put_u8(*buf, pref);
895 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800896}
897
898
899static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700900 struct wpa_bss *bss, struct wpabuf **buf,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800901 u8 pref)
902{
903 const u8 *ie;
904 u8 op_class, chan;
905 int sec_chan = 0, vht = 0;
906 enum phy_type phy_type;
907 u32 info;
908 struct ieee80211_ht_operation *ht_oper = NULL;
909 struct ieee80211_vht_operation *vht_oper = NULL;
910
911 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
912 if (ie && ie[1] >= 2) {
913 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
914
915 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
916 sec_chan = 1;
917 else if (ht_oper->ht_param &
918 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
919 sec_chan = -1;
920 }
921
922 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
923 if (ie && ie[1] >= 1) {
924 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
925
Hai Shalom81f62d82019-07-22 12:10:00 -0700926 if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
927 vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
928 vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800929 vht = vht_oper->vht_op_info_chwidth;
930 }
931
932 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
933 &chan) == NUM_HOSTAPD_MODES) {
934 wpa_printf(MSG_DEBUG,
935 "WNM: Cannot determine operating class and channel");
936 return -2;
937 }
938
939 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
940 (vht_oper != NULL));
941 if (phy_type == PHY_TYPE_UNSPECIFIED) {
942 wpa_printf(MSG_DEBUG,
943 "WNM: Cannot determine BSS phy type for Neighbor Report");
944 return -2;
945 }
946
947 info = wnm_get_bss_info(wpa_s, bss);
948
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700949 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
950 pref);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800951}
952
953
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700954static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800955{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800956 unsigned int i, pref = 255;
957 struct os_reltime now;
958 struct wpa_ssid *ssid = wpa_s->current_ssid;
959
960 if (!ssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700961 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800962
963 /*
964 * TODO: Define when scan results are no longer valid for the candidate
965 * list.
966 */
967 os_get_reltime(&now);
968 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700969 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800970
971 wpa_printf(MSG_DEBUG,
972 "WNM: Add candidate list to BSS Transition Management Response frame");
973 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
974 struct wpa_bss *bss = wpa_s->last_scan_res[i];
975 int res;
976
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800977 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700978 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800979 if (res == -2)
980 continue; /* could not build entry for BSS */
981 if (res < 0)
982 break; /* no more room for candidates */
983 if (pref == 1)
984 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800985 }
986 }
987
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700988 wpa_hexdump_buf(MSG_DEBUG,
989 "WNM: BSS Transition Management Response candidate list",
990 *buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800991}
992
993
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700994#define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
995
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700996static void wnm_send_bss_transition_mgmt_resp(
997 struct wpa_supplicant *wpa_s, u8 dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700998 enum bss_trans_mgmt_status_code status,
999 enum mbo_transition_reject_reason reason,
1000 u8 delay, const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001001{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001002 struct wpabuf *buf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001003 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001004
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001005 wpa_printf(MSG_DEBUG,
1006 "WNM: Send BSS Transition Management Response to " MACSTR
1007 " dialog_token=%u status=%u reason=%u delay=%d",
1008 MAC2STR(wpa_s->bssid), dialog_token, status, reason, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001009 if (!wpa_s->current_bss) {
1010 wpa_printf(MSG_DEBUG,
1011 "WNM: Current BSS not known - drop response");
1012 return;
1013 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001014
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001015 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
1016 if (!buf) {
1017 wpa_printf(MSG_DEBUG,
1018 "WNM: Failed to allocate memory for BTM response");
1019 return;
1020 }
1021
Hai Shalom74f70d42019-02-11 14:42:39 -08001022 wpa_s->bss_tm_status = status;
1023 wpas_notify_bss_tm_status(wpa_s);
1024
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001025 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1026 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
1027 wpabuf_put_u8(buf, dialog_token);
1028 wpabuf_put_u8(buf, status);
1029 wpabuf_put_u8(buf, delay);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001030 if (target_bssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001031 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001032 } else if (status == WNM_BSS_TM_ACCEPT) {
1033 /*
1034 * P802.11-REVmc clarifies that the Target BSSID field is always
1035 * present when status code is zero, so use a fake value here if
1036 * no BSSID is yet known.
1037 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001038 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001039 }
1040
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001041 if (status == WNM_BSS_TM_ACCEPT)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001042 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001043
1044#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001045 if (status != WNM_BSS_TM_ACCEPT &&
1046 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
1047 u8 mbo[10];
1048 size_t ret;
1049
1050 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
1051 reason);
1052 if (ret) {
1053 if (wpabuf_resize(&buf, ret) < 0) {
1054 wpabuf_free(buf);
1055 wpa_printf(MSG_DEBUG,
1056 "WNM: Failed to allocate memory for MBO IE");
1057 return;
1058 }
1059
1060 wpabuf_put_data(buf, mbo, ret);
1061 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001062 }
1063#endif /* CONFIG_MBO */
1064
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001065 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1066 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001067 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001068 if (res < 0) {
1069 wpa_printf(MSG_DEBUG,
1070 "WNM: Failed to send BSS Transition Management Response");
1071 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001072
1073 wpabuf_free(buf);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001074}
1075
1076
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001077static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
1078 struct wpa_bss *bss, struct wpa_ssid *ssid,
1079 int after_new_scan)
1080{
1081 wpa_dbg(wpa_s, MSG_DEBUG,
1082 "WNM: Transition to BSS " MACSTR
1083 " based on BSS Transition Management Request (old BSSID "
1084 MACSTR " after_new_scan=%d)",
1085 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1086
1087 /* Send the BSS Management Response - Accept */
1088 if (wpa_s->wnm_reply) {
1089 wpa_s->wnm_reply = 0;
1090 wpa_printf(MSG_DEBUG,
1091 "WNM: Sending successful BSS Transition Management Response");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001092 wnm_send_bss_transition_mgmt_resp(
1093 wpa_s, wpa_s->wnm_dialog_token, WNM_BSS_TM_ACCEPT,
1094 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1095 bss->bssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001096 }
1097
1098 if (bss == wpa_s->current_bss) {
1099 wpa_printf(MSG_DEBUG,
1100 "WNM: Already associated with the preferred candidate");
1101 wnm_deallocate_memory(wpa_s);
1102 return;
1103 }
1104
1105 wpa_s->reassociate = 1;
1106 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1107 wpa_supplicant_connect(wpa_s, bss, ssid);
1108 wnm_deallocate_memory(wpa_s);
1109}
1110
1111
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001112int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001113{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001114 struct wpa_bss *bss;
1115 struct wpa_ssid *ssid = wpa_s->current_ssid;
1116 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001117 enum mbo_transition_reject_reason reason =
1118 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001119
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001120 if (!wpa_s->wnm_neighbor_report_elements)
1121 return 0;
1122
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001123 wpa_dbg(wpa_s, MSG_DEBUG,
1124 "WNM: Process scan results for BSS Transition Management");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001125 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
1126 &wpa_s->scan_trigger_time)) {
1127 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1128 wnm_deallocate_memory(wpa_s);
1129 return 0;
1130 }
1131
1132 if (!wpa_s->current_bss ||
1133 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
1134 ETH_ALEN) != 0) {
1135 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
1136 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001137 }
1138
1139 /* Compare the Neighbor Report and scan results */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001140 bss = compare_scan_neighbor_results(wpa_s, 0, &reason);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001141 if (!bss) {
1142 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1143 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1144 goto send_bss_resp_fail;
1145 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001146
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001147 /* Associate to the network */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001148 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001149 return 1;
1150
1151send_bss_resp_fail:
1152 if (!reply_on_fail)
1153 return 0;
1154
1155 /* Send reject response for all the failures */
1156
1157 if (wpa_s->wnm_reply) {
1158 wpa_s->wnm_reply = 0;
1159 wnm_send_bss_transition_mgmt_resp(wpa_s,
1160 wpa_s->wnm_dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001161 status, reason, 0, NULL);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001162 }
1163 wnm_deallocate_memory(wpa_s);
1164
1165 return 0;
1166}
1167
1168
1169static int cand_pref_compar(const void *a, const void *b)
1170{
1171 const struct neighbor_report *aa = a;
1172 const struct neighbor_report *bb = b;
1173
1174 if (!aa->preference_present && !bb->preference_present)
1175 return 0;
1176 if (!aa->preference_present)
1177 return 1;
1178 if (!bb->preference_present)
1179 return -1;
1180 if (bb->preference > aa->preference)
1181 return 1;
1182 if (bb->preference < aa->preference)
1183 return -1;
1184 return 0;
1185}
1186
1187
1188static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1189{
1190 if (!wpa_s->wnm_neighbor_report_elements)
1191 return;
1192 qsort(wpa_s->wnm_neighbor_report_elements,
1193 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1194 cand_pref_compar);
1195}
1196
1197
1198static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1199{
1200 unsigned int i;
1201
1202 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1203 if (!wpa_s->wnm_neighbor_report_elements)
1204 return;
1205 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1206 struct neighbor_report *nei;
1207
1208 nei = &wpa_s->wnm_neighbor_report_elements[i];
1209 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1210 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1211 i, MAC2STR(nei->bssid), nei->bssid_info,
1212 nei->regulatory_class,
1213 nei->channel_number, nei->phy_type,
1214 nei->preference_present ? nei->preference : -1,
1215 nei->freq);
1216 }
1217}
1218
1219
1220static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1221{
1222 unsigned int i;
1223
1224 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1225 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1226 int j;
1227
1228 for (j = 0; j < mode->num_channels; j++) {
1229 struct hostapd_channel_data *chan;
1230
1231 chan = &mode->channels[j];
1232 if (chan->freq == freq &&
1233 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1234 return 1;
1235 }
1236 }
1237
1238 return 0;
1239}
1240
1241
1242static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1243{
1244 int *freqs;
1245 int num_freqs = 0;
1246 unsigned int i;
1247
1248 if (!wpa_s->wnm_neighbor_report_elements)
1249 return;
1250
1251 if (wpa_s->hw.modes == NULL)
1252 return;
1253
1254 os_free(wpa_s->next_scan_freqs);
1255 wpa_s->next_scan_freqs = NULL;
1256
1257 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1258 if (freqs == NULL)
1259 return;
1260
1261 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1262 struct neighbor_report *nei;
1263
1264 nei = &wpa_s->wnm_neighbor_report_elements[i];
1265 if (nei->freq <= 0) {
1266 wpa_printf(MSG_DEBUG,
1267 "WNM: Unknown neighbor operating frequency for "
1268 MACSTR " - scan all channels",
1269 MAC2STR(nei->bssid));
1270 os_free(freqs);
1271 return;
1272 }
1273 if (chan_supported(wpa_s, nei->freq))
1274 add_freq(freqs, &num_freqs, nei->freq);
1275 }
1276
1277 if (num_freqs == 0) {
1278 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001279 return;
1280 }
1281
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001282 wpa_printf(MSG_DEBUG,
1283 "WNM: Scan %d frequencies based on transition candidate list",
1284 num_freqs);
1285 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001286}
1287
1288
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001289static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
1290{
1291 struct wpa_scan_results *scan_res;
1292 struct wpa_bss *bss;
1293 struct wpa_ssid *ssid = wpa_s->current_ssid;
1294 u8 i, found = 0;
1295 size_t j;
1296
1297 wpa_dbg(wpa_s, MSG_DEBUG,
1298 "WNM: Fetch current scan results from the driver for checking transition candidates");
1299 scan_res = wpa_drv_get_scan_results2(wpa_s);
1300 if (!scan_res) {
1301 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
1302 return 0;
1303 }
1304
1305 if (scan_res->fetch_time.sec == 0)
1306 os_get_reltime(&scan_res->fetch_time);
1307
1308 filter_scan_res(wpa_s, scan_res);
1309
1310 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1311 struct neighbor_report *nei;
1312
1313 nei = &wpa_s->wnm_neighbor_report_elements[i];
1314 if (nei->preference_present && nei->preference == 0)
1315 continue;
1316
1317 for (j = 0; j < scan_res->num; j++) {
1318 struct wpa_scan_res *res;
1319 const u8 *ssid_ie;
1320
1321 res = scan_res->res[j];
1322 if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
1323 res->age > WNM_SCAN_RESULT_AGE * 1000)
1324 continue;
1325 bss = wpa_s->current_bss;
1326 ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
1327 if (bss && ssid_ie &&
1328 (bss->ssid_len != ssid_ie[1] ||
1329 os_memcmp(bss->ssid, ssid_ie + 2,
1330 bss->ssid_len) != 0))
1331 continue;
1332
1333 /* Potential candidate found */
1334 found = 1;
1335 scan_snr(res);
1336 scan_est_throughput(wpa_s, res);
1337 wpa_bss_update_scan_res(wpa_s, res,
1338 &scan_res->fetch_time);
1339 }
1340 }
1341
1342 wpa_scan_results_free(scan_res);
1343 if (!found) {
1344 wpa_dbg(wpa_s, MSG_DEBUG,
1345 "WNM: No transition candidate matches existing scan results");
1346 return 0;
1347 }
1348
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001349 bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE, NULL);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001350 if (!bss) {
1351 wpa_dbg(wpa_s, MSG_DEBUG,
1352 "WNM: Comparison of scan results against transition candidates did not find matches");
1353 return 0;
1354 }
1355
1356 /* Associate to the network */
1357 wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
1358 return 1;
1359}
1360
1361
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001362static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1363 const u8 *pos, const u8 *end,
1364 int reply)
1365{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001366 unsigned int beacon_int;
1367 u8 valid_int;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001368#ifdef CONFIG_MBO
1369 const u8 *vendor;
1370#endif /* CONFIG_MBO */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001371
Hai Shalomc3565922019-10-28 11:58:20 -07001372 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
Hai Shalom81f62d82019-07-22 12:10:00 -07001373 return;
1374
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001375 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001376 return;
1377
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001378#ifdef CONFIG_MBO
1379 wpa_s->wnm_mbo_trans_reason_present = 0;
1380 wpa_s->wnm_mbo_transition_reason = 0;
Sunil Ravi4018d712019-12-06 18:01:21 -08001381 wpa_s->wnm_mbo_cell_pref_present = 0;
1382 wpa_s->wnm_mbo_cell_preference = 0;
1383 wpa_s->wnm_mbo_assoc_retry_delay_present = 0;
1384 wpa_s->wnm_mbo_assoc_retry_delay_sec = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001385#endif /* CONFIG_MBO */
1386
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001387 if (wpa_s->current_bss)
1388 beacon_int = wpa_s->current_bss->beacon_int;
1389 else
1390 beacon_int = 100; /* best guess */
1391
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001392 wpa_s->wnm_dialog_token = pos[0];
1393 wpa_s->wnm_mode = pos[1];
1394 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001395 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001396 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001397
1398 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1399 "dialog_token=%u request_mode=0x%x "
1400 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001401 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001402 wpa_s->wnm_dissoc_timer, valid_int);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001403
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001404#if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1405 if (wpa_s->reject_btm_req_reason) {
1406 wpa_printf(MSG_INFO,
1407 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1408 wpa_s->reject_btm_req_reason);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001409 wnm_send_bss_transition_mgmt_resp(
1410 wpa_s, wpa_s->wnm_dialog_token,
1411 wpa_s->reject_btm_req_reason,
1412 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001413 return;
1414 }
1415#endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1416
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001417 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001418
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001419 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001420 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001421 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1422 return;
1423 }
1424 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001425 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001426 }
1427
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001428 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001429 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001430
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001431 if (end - pos < 1 || 1 + pos[0] > end - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001432 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1433 "Management Request (URL)");
1434 return;
1435 }
1436 os_memcpy(url, pos + 1, pos[0]);
1437 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001438 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001439
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001440 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1441 wpa_sm_pmf_enabled(wpa_s->wpa),
1442 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001443 }
1444
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001445 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001446 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001447 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
1448 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001449 /* TODO: mark current BSS less preferred for
1450 * selection */
1451 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
1452 wpa_supplicant_req_scan(wpa_s, 0, 0);
1453 }
1454 }
1455
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001456#ifdef CONFIG_MBO
1457 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1458 if (vendor)
1459 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1460#endif /* CONFIG_MBO */
1461
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001462 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001463 unsigned int valid_ms;
1464
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001465 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001466 wnm_deallocate_memory(wpa_s);
1467 wpa_s->wnm_neighbor_report_elements = os_calloc(
1468 WNM_MAX_NEIGHBOR_REPORT,
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001469 sizeof(struct neighbor_report));
1470 if (wpa_s->wnm_neighbor_report_elements == NULL)
1471 return;
1472
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001473 while (end - pos >= 2 &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001474 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
1475 {
1476 u8 tag = *pos++;
1477 u8 len = *pos++;
1478
1479 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
1480 tag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001481 if (len > end - pos) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001482 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1483 return;
1484 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001485 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1486 struct neighbor_report *rep;
1487 rep = &wpa_s->wnm_neighbor_report_elements[
1488 wpa_s->wnm_num_neighbor_report];
1489 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001490 wpa_s->wnm_num_neighbor_report++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001491#ifdef CONFIG_MBO
1492 if (wpa_s->wnm_mbo_trans_reason_present &&
1493 wpa_s->wnm_num_neighbor_report == 1) {
1494 rep->is_first = 1;
1495 wpa_printf(MSG_DEBUG,
1496 "WNM: First transition candidate is "
1497 MACSTR, MAC2STR(rep->bssid));
1498 }
1499#endif /* CONFIG_MBO */
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001500 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001501
1502 pos += len;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001503 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001504
1505 if (!wpa_s->wnm_num_neighbor_report) {
1506 wpa_printf(MSG_DEBUG,
1507 "WNM: Candidate list included bit is set, but no candidates found");
1508 wnm_send_bss_transition_mgmt_resp(
1509 wpa_s, wpa_s->wnm_dialog_token,
1510 WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001511 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1512 NULL);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001513 return;
1514 }
1515
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001516 wnm_sort_cand_list(wpa_s);
1517 wnm_dump_cand_list(wpa_s);
1518 valid_ms = valid_int * beacon_int * 128 / 125;
1519 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1520 valid_ms);
1521 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1522 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
1523 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
1524 wpa_s->wnm_cand_valid_until.sec +=
1525 wpa_s->wnm_cand_valid_until.usec / 1000000;
1526 wpa_s->wnm_cand_valid_until.usec %= 1000000;
1527 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001528
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001529 /*
1530 * Fetch the latest scan results from the kernel and check for
1531 * candidates based on those results first. This can help in
1532 * finding more up-to-date information should the driver has
1533 * done some internal scanning operations after the last scan
1534 * result update in wpa_supplicant.
1535 */
1536 if (wnm_fetch_scan_results(wpa_s) > 0)
1537 return;
1538
1539 /*
1540 * Try to use previously received scan results, if they are
1541 * recent enough to use for a connection.
1542 */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001543 if (wpa_s->last_scan_res_used > 0) {
1544 struct os_reltime now;
1545
1546 os_get_reltime(&now);
1547 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
1548 wpa_printf(MSG_DEBUG,
1549 "WNM: Try to use recent scan results");
1550 if (wnm_scan_process(wpa_s, 0) > 0)
1551 return;
1552 wpa_printf(MSG_DEBUG,
1553 "WNM: No match in previous scan results - try a new scan");
1554 }
1555 }
1556
1557 wnm_set_scan_freqs(wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001558 if (wpa_s->wnm_num_neighbor_report == 1) {
1559 os_memcpy(wpa_s->next_scan_bssid,
1560 wpa_s->wnm_neighbor_report_elements[0].bssid,
1561 ETH_ALEN);
1562 wpa_printf(MSG_DEBUG,
1563 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1564 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1565 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001566 wpa_supplicant_req_scan(wpa_s, 0, 0);
1567 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001568 enum bss_trans_mgmt_status_code status;
1569 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
1570 status = WNM_BSS_TM_ACCEPT;
1571 else {
1572 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1573 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1574 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001575 wnm_send_bss_transition_mgmt_resp(
1576 wpa_s, wpa_s->wnm_dialog_token, status,
1577 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001578 }
1579}
1580
1581
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001582#define BTM_QUERY_MIN_SIZE 4
1583
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001584int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001585 u8 query_reason,
1586 const char *btm_candidates,
1587 int cand_list)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001588{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001589 struct wpabuf *buf;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001590 int ret;
1591
1592 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001593 MACSTR " query_reason=%u%s",
1594 MAC2STR(wpa_s->bssid), query_reason,
1595 cand_list ? " candidate list" : "");
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001596
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001597 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1598 if (!buf)
1599 return -1;
1600
1601 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1602 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1603 wpabuf_put_u8(buf, 1);
1604 wpabuf_put_u8(buf, query_reason);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001605
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001606 if (cand_list)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001607 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001608
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001609 if (btm_candidates) {
1610 const size_t max_len = 1000;
1611
1612 ret = wpabuf_resize(&buf, max_len);
1613 if (ret < 0) {
1614 wpabuf_free(buf);
1615 return ret;
1616 }
1617
1618 ret = ieee802_11_parse_candidate_list(btm_candidates,
1619 wpabuf_put(buf, 0),
1620 max_len);
1621 if (ret < 0) {
1622 wpabuf_free(buf);
1623 return ret;
1624 }
1625
1626 wpabuf_put(buf, ret);
1627 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001628
1629 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1630 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001631 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001632
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001633 wpabuf_free(buf);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001634 return ret;
1635}
1636
1637
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001638static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1639 const u8 *sa, const u8 *data,
1640 int len)
1641{
1642 const u8 *pos, *end, *next;
1643 u8 ie, ie_len;
1644
1645 pos = data;
1646 end = data + len;
1647
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001648 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001649 ie = *pos++;
1650 ie_len = *pos++;
1651 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1652 ie, ie_len);
1653 if (ie_len > end - pos) {
1654 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1655 "subelement");
1656 break;
1657 }
1658 next = pos + ie_len;
1659 if (ie_len < 4) {
1660 pos = next;
1661 continue;
1662 }
1663 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1664 WPA_GET_BE24(pos), pos[3]);
1665
1666#ifdef CONFIG_HS20
1667 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1668 WPA_GET_BE24(pos) == OUI_WFA &&
1669 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1670 /* Subscription Remediation subelement */
1671 const u8 *ie_end;
1672 u8 url_len;
1673 char *url;
1674 u8 osu_method;
1675
1676 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1677 "subelement");
1678 ie_end = pos + ie_len;
1679 pos += 4;
1680 url_len = *pos++;
1681 if (url_len == 0) {
1682 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1683 url = NULL;
1684 osu_method = 1;
1685 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001686 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001687 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1688 url_len,
1689 (int) (ie_end - pos));
1690 break;
1691 }
1692 url = os_malloc(url_len + 1);
1693 if (url == NULL)
1694 break;
1695 os_memcpy(url, pos, url_len);
1696 url[url_len] = '\0';
1697 osu_method = pos[url_len];
1698 }
1699 hs20_rx_subscription_remediation(wpa_s, url,
1700 osu_method);
1701 os_free(url);
1702 pos = next;
1703 continue;
1704 }
1705
1706 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1707 WPA_GET_BE24(pos) == OUI_WFA &&
1708 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1709 const u8 *ie_end;
1710 u8 url_len;
1711 char *url;
1712 u8 code;
1713 u16 reauth_delay;
1714
1715 ie_end = pos + ie_len;
1716 pos += 4;
1717 code = *pos++;
1718 reauth_delay = WPA_GET_LE16(pos);
1719 pos += 2;
1720 url_len = *pos++;
1721 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1722 "Imminent - Reason Code %u "
1723 "Re-Auth Delay %u URL Length %u",
1724 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001725 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001726 break;
1727 url = os_malloc(url_len + 1);
1728 if (url == NULL)
1729 break;
1730 os_memcpy(url, pos, url_len);
1731 url[url_len] = '\0';
1732 hs20_rx_deauth_imminent_notice(wpa_s, code,
1733 reauth_delay, url);
1734 os_free(url);
1735 pos = next;
1736 continue;
1737 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001738
1739 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1740 WPA_GET_BE24(pos) == OUI_WFA &&
1741 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1742 const u8 *ie_end;
1743 u8 url_len;
1744 char *url;
1745
1746 ie_end = pos + ie_len;
1747 pos += 4;
1748 url_len = *pos++;
1749 wpa_printf(MSG_DEBUG,
1750 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1751 url_len);
1752 if (url_len > ie_end - pos)
1753 break;
1754 url = os_malloc(url_len + 1);
1755 if (!url)
1756 break;
1757 os_memcpy(url, pos, url_len);
1758 url[url_len] = '\0';
1759 hs20_rx_t_c_acceptance(wpa_s, url);
1760 os_free(url);
1761 pos = next;
1762 continue;
1763 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001764#endif /* CONFIG_HS20 */
1765
1766 pos = next;
1767 }
1768}
1769
1770
1771static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1772 const u8 *sa, const u8 *frm, int len)
1773{
1774 const u8 *pos, *end;
1775 u8 dialog_token, type;
1776
1777 /* Dialog Token [1] | Type [1] | Subelements */
1778
1779 if (len < 2 || sa == NULL)
1780 return;
1781 end = frm + len;
1782 pos = frm;
1783 dialog_token = *pos++;
1784 type = *pos++;
1785
1786 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1787 "(dialog_token %u type %u sa " MACSTR ")",
1788 dialog_token, type, MAC2STR(sa));
1789 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1790 pos, end - pos);
1791
1792 if (wpa_s->wpa_state != WPA_COMPLETED ||
1793 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1794 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1795 "from our AP - ignore it");
1796 return;
1797 }
1798
1799 switch (type) {
1800 case 1:
1801 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1802 break;
1803 default:
1804 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1805 "WNM-Notification type %u", type);
1806 break;
1807 }
1808}
1809
1810
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001811static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1812 const u8 *sa, const u8 *frm,
1813 int len)
1814{
1815 u8 dialog_token, req_info, auto_report, timeout;
1816
1817 if (!wpa_s->conf->coloc_intf_reporting)
1818 return;
1819
1820 /* Dialog Token [1] | Request Info [1] */
1821
1822 if (len < 2)
1823 return;
1824 dialog_token = frm[0];
1825 req_info = frm[1];
1826 auto_report = req_info & 0x03;
1827 timeout = req_info >> 2;
1828
1829 wpa_dbg(wpa_s, MSG_DEBUG,
1830 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1831 dialog_token, auto_report, timeout, MAC2STR(sa));
1832
1833 if (dialog_token == 0)
1834 return; /* only nonzero values are used for request */
1835
1836 if (wpa_s->wpa_state != WPA_COMPLETED ||
1837 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1838 wpa_dbg(wpa_s, MSG_DEBUG,
1839 "WNM: Collocated Interference Request frame not from current AP - ignore it");
1840 return;
1841 }
1842
1843 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1844 dialog_token, auto_report, timeout);
1845 wpa_s->coloc_intf_dialog_token = dialog_token;
1846 wpa_s->coloc_intf_auto_report = auto_report;
1847 wpa_s->coloc_intf_timeout = timeout;
1848}
1849
1850
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001851void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001852 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001853{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001854 const u8 *pos, *end;
1855 u8 act;
1856
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001857 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001858 return;
1859
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001860 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001861 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001862 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001863
1864 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001865 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001866 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001867 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001868 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1869 "frame");
1870 return;
1871 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001872
1873 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001874 case WNM_BSS_TRANS_MGMT_REQ:
1875 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001876 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001877 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001878 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001879 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001880 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001881 case WNM_NOTIFICATION_REQ:
1882 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1883 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001884 case WNM_COLLOCATED_INTERFERENCE_REQ:
1885 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
1886 end - pos);
1887 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001888 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001889 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001890 break;
1891 }
1892}
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001893
1894
1895int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
1896 const struct wpabuf *elems)
1897{
1898 struct wpabuf *buf;
1899 int ret;
1900
1901 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
1902 return -1;
1903
1904 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
1905 MACSTR " (dialog token %u)",
1906 MAC2STR(wpa_s->bssid), dialog_token);
1907
1908 buf = wpabuf_alloc(3 + wpabuf_len(elems));
1909 if (!buf)
1910 return -1;
1911
1912 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1913 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
1914 wpabuf_put_u8(buf, dialog_token);
1915 wpabuf_put_buf(buf, elems);
1916
1917 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1918 wpa_s->own_addr, wpa_s->bssid,
1919 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1920 wpabuf_free(buf);
1921 return ret;
1922}
1923
1924
1925void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
1926 struct wpabuf *elems)
1927{
1928 wpabuf_free(wpa_s->coloc_intf_elems);
1929 if (elems && wpabuf_len(elems) == 0) {
1930 wpabuf_free(elems);
1931 elems = NULL;
1932 }
1933 wpa_s->coloc_intf_elems = elems;
1934
1935 if (wpa_s->conf->coloc_intf_reporting && wpa_s->coloc_intf_elems &&
1936 wpa_s->coloc_intf_dialog_token &&
1937 (wpa_s->coloc_intf_auto_report == 1 ||
1938 wpa_s->coloc_intf_auto_report == 3)) {
1939 /* TODO: Check that there has not been less than
1940 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
1941 */
1942 wnm_send_coloc_intf_report(wpa_s,
1943 wpa_s->coloc_intf_dialog_token,
1944 wpa_s->coloc_intf_elems);
1945 }
1946}
1947
1948
1949void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
1950{
1951#ifdef CONFIG_WNM
1952 wpa_s->coloc_intf_dialog_token = 0;
1953 wpa_s->coloc_intf_auto_report = 0;
1954#endif /* CONFIG_WNM */
1955}