blob: 3e27f0c298b249342a3b9cdb24aa6b35065593dd [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"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070015#include "rsn_supp/wpa.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070016#include "config.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080017#include "wpa_supplicant_i.h"
18#include "driver_i.h"
19#include "scan.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070020#include "ctrl_iface.h"
21#include "bss.h"
22#include "wnm_sta.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080023#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070024
25#define MAX_TFS_IE_LEN 1024
Dmitry Shmidt44c95782013-05-17 09:51:35 -070026#define WNM_MAX_NEIGHBOR_REPORT 10
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070027
Dmitry Shmidt849734c2016-05-27 09:59:01 -070028#define WNM_SCAN_RESULT_AGE 2 /* 2 seconds */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070029
30/* get the TFS IE from driver */
31static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
32 u16 *buf_len, enum wnm_oper oper)
33{
34 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
35
36 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
37}
38
39
40/* set the TFS IE to driver */
41static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080042 const u8 *addr, const u8 *buf, u16 buf_len,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070043 enum wnm_oper oper)
44{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080045 u16 len = buf_len;
46
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070047 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
48
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080049 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070050}
51
52
53/* MLME-SLEEPMODE.request */
54int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080055 u8 action, u16 intval, struct wpabuf *tfs_req)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070056{
57 struct ieee80211_mgmt *mgmt;
58 int res;
59 size_t len;
60 struct wnm_sleep_element *wnmsleep_ie;
61 u8 *wnmtfs_ie;
62 u8 wnmsleep_ie_len;
63 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
64 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
65 WNM_SLEEP_TFS_REQ_IE_NONE;
66
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080067 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
68 "action=%s to " MACSTR,
69 action == 0 ? "enter" : "exit",
70 MAC2STR(wpa_s->bssid));
71
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070072 /* WNM-Sleep Mode IE */
73 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
74 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
75 if (wnmsleep_ie == NULL)
76 return -1;
77 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
78 wnmsleep_ie->len = wnmsleep_ie_len - 2;
79 wnmsleep_ie->action_type = action;
80 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080081 wnmsleep_ie->intval = host_to_le16(intval);
82 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
83 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070084
85 /* TFS IE(s) */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080086 if (tfs_req) {
87 wnmtfs_ie_len = wpabuf_len(tfs_req);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070088 wnmtfs_ie = os_memdup(wpabuf_head(tfs_req), wnmtfs_ie_len);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080089 if (wnmtfs_ie == NULL) {
90 os_free(wnmsleep_ie);
91 return -1;
92 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080093 } else {
94 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
95 if (wnmtfs_ie == NULL) {
96 os_free(wnmsleep_ie);
97 return -1;
98 }
99 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
100 tfs_oper)) {
101 wnmtfs_ie_len = 0;
102 os_free(wnmtfs_ie);
103 wnmtfs_ie = NULL;
104 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700105 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800106 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
107 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108
109 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
110 if (mgmt == NULL) {
111 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
112 "WNM-Sleep Request action frame");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800113 os_free(wnmsleep_ie);
114 os_free(wnmtfs_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700115 return -1;
116 }
117
118 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
119 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
120 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
121 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
122 WLAN_FC_STYPE_ACTION);
123 mgmt->u.action.category = WLAN_ACTION_WNM;
124 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800125 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700126 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
127 wnmsleep_ie_len);
128 /* copy TFS IE here */
129 if (wnmtfs_ie_len > 0) {
130 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
131 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
132 }
133
134 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
135 wnmtfs_ie_len;
136
137 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
138 wpa_s->own_addr, wpa_s->bssid,
139 &mgmt->u.action.category, len, 0);
140 if (res < 0)
141 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
142 "(action=%d, intval=%d)", action, intval);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800143 else
144 wpa_s->wnmsleep_used = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700145
146 os_free(wnmsleep_ie);
147 os_free(wnmtfs_ie);
148 os_free(mgmt);
149
150 return res;
151}
152
153
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800154static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800155 const u8 *tfsresp_ie_start,
156 const u8 *tfsresp_ie_end)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800157{
158 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
159 wpa_s->bssid, NULL, NULL);
160 /* remove GTK/IGTK ?? */
161
162 /* set the TFS Resp IE(s) */
163 if (tfsresp_ie_start && tfsresp_ie_end &&
164 tfsresp_ie_end - tfsresp_ie_start >= 0) {
165 u16 tfsresp_ie_len;
166 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
167 tfsresp_ie_start;
168 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
169 /* pass the TFS Resp IE(s) to driver for processing */
170 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
171 tfsresp_ie_start,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800172 tfsresp_ie_len,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800173 WNM_SLEEP_TFS_RESP_IE_SET))
174 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
175 }
176}
177
178
179static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
180 const u8 *frm, u16 key_len_total)
181{
182 u8 *ptr, *end;
183 u8 gtk_len;
184
185 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
186 NULL, NULL);
187
188 /* Install GTK/IGTK */
189
190 /* point to key data field */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800191 ptr = (u8 *) frm + 1 + 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800192 end = ptr + key_len_total;
193 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
194
Jouni Malinen17de68d2015-11-09 15:25:41 -0800195 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
196 wpa_msg(wpa_s, MSG_INFO,
197 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
198 return;
199 }
200
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800201 while (end - ptr > 1) {
202 if (2 + ptr[1] > end - ptr) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800203 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
204 "length");
205 if (end > ptr) {
206 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
207 ptr, end - ptr);
208 }
209 break;
210 }
211 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
212 if (ptr[1] < 11 + 5) {
213 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
214 "subelem");
215 break;
216 }
217 gtk_len = *(ptr + 4);
218 if (ptr[1] < 11 + gtk_len ||
219 gtk_len < 5 || gtk_len > 32) {
220 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
221 "subelem");
222 break;
223 }
224 wpa_wnmsleep_install_key(
225 wpa_s->wpa,
226 WNM_SLEEP_SUBELEM_GTK,
227 ptr);
228 ptr += 13 + gtk_len;
229#ifdef CONFIG_IEEE80211W
230 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
231 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
232 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
233 "subelem");
234 break;
235 }
236 wpa_wnmsleep_install_key(wpa_s->wpa,
237 WNM_SLEEP_SUBELEM_IGTK, ptr);
238 ptr += 10 + WPA_IGTK_LEN;
239#endif /* CONFIG_IEEE80211W */
240 } else
241 break; /* skip the loop */
242 }
243}
244
245
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700246static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
247 const u8 *frm, int len)
248{
249 /*
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700250 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700251 * WNM-Sleep Mode IE | TFS Response IE
252 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800253 const u8 *pos = frm; /* point to payload after the action field */
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700254 u16 key_len_total;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 struct wnm_sleep_element *wnmsleep_ie = NULL;
256 /* multiple TFS Resp IE (assuming consecutive) */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800257 const u8 *tfsresp_ie_start = NULL;
258 const u8 *tfsresp_ie_end = NULL;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800259 size_t left;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800261 if (!wpa_s->wnmsleep_used) {
262 wpa_printf(MSG_DEBUG,
Jouni Malinen90bfa452017-09-22 11:25:02 +0300263 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800264 return;
265 }
266
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700267 if (len < 3)
268 return;
269 key_len_total = WPA_GET_LE16(frm + 1);
270
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800271 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
272 frm[0], key_len_total);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800273 left = len - 3;
274 if (key_len_total > left) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800275 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
276 return;
277 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800278 pos += 3 + key_len_total;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800279 while (pos - frm + 1 < len) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700280 u8 ie_len = *(pos + 1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800281 if (2 + ie_len > frm + len - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800282 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
283 break;
284 }
285 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800286 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287 wnmsleep_ie = (struct wnm_sleep_element *) pos;
288 else if (*pos == WLAN_EID_TFS_RESP) {
289 if (!tfsresp_ie_start)
290 tfsresp_ie_start = pos;
291 tfsresp_ie_end = pos;
292 } else
293 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
294 pos += ie_len + 2;
295 }
296
297 if (!wnmsleep_ie) {
298 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
299 return;
300 }
301
Jouni Malinen90bfa452017-09-22 11:25:02 +0300302 wpa_s->wnmsleep_used = 0;
303
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800304 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
305 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700306 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
307 "frame (action=%d, intval=%d)",
308 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800309 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
310 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
311 tfsresp_ie_end);
312 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
313 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700314 }
315 } else {
316 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
317 "(action=%d, intval=%d)",
318 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800319 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700320 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
321 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800322 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700323 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
324 wpa_s->bssid, NULL, NULL);
325 }
326}
327
328
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700329void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
330{
331 int i;
332
333 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700334 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700335 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
336 }
337
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700338 wpa_s->wnm_num_neighbor_report = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700339 os_free(wpa_s->wnm_neighbor_report_elements);
340 wpa_s->wnm_neighbor_report_elements = NULL;
341}
342
343
344static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
345 u8 id, u8 elen, const u8 *pos)
346{
347 switch (id) {
348 case WNM_NEIGHBOR_TSF:
349 if (elen < 2 + 2) {
350 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
351 break;
352 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800353 rep->tsf_offset = WPA_GET_LE16(pos);
354 rep->beacon_int = WPA_GET_LE16(pos + 2);
355 rep->tsf_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700356 break;
357 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
358 if (elen < 2) {
359 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
360 "country string");
361 break;
362 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800363 os_memcpy(rep->country, pos, 2);
364 rep->country_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700365 break;
366 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
367 if (elen < 1) {
368 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
369 "candidate");
370 break;
371 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800372 rep->preference = pos[0];
373 rep->preference_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700374 break;
375 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
Hai Shalom5e6e3f72019-02-04 12:53:10 -0800376 if (elen < 10) {
377 wpa_printf(MSG_DEBUG, "WNM: Too short bss_term_tsf");
378 break;
379 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800380 rep->bss_term_tsf = WPA_GET_LE64(pos);
381 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
382 rep->bss_term_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700383 break;
384 case WNM_NEIGHBOR_BEARING:
385 if (elen < 8) {
386 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
387 "bearing");
388 break;
389 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800390 rep->bearing = WPA_GET_LE16(pos);
391 rep->distance = WPA_GET_LE32(pos + 2);
392 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
393 rep->bearing_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700394 break;
395 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700396 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700397 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
398 "pilot");
399 break;
400 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700401 os_free(rep->meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700402 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
403 if (rep->meas_pilot == NULL)
404 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700405 rep->meas_pilot->measurement_pilot = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700406 rep->meas_pilot->subelem_len = elen - 1;
407 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700408 break;
409 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700410 if (elen < 5) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700411 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
412 "capabilities");
413 break;
414 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800415 os_memcpy(rep->rm_capab, pos, 5);
416 rep->rm_capab_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700417 break;
418 case WNM_NEIGHBOR_MULTIPLE_BSSID:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700419 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700420 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
421 break;
422 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700423 os_free(rep->mul_bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700424 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
425 if (rep->mul_bssid == NULL)
426 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700427 rep->mul_bssid->max_bssid_indicator = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700428 rep->mul_bssid->subelem_len = elen - 1;
429 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700430 break;
431 }
432}
433
434
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800435static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
436{
437 struct wpa_bss *bss = wpa_s->current_bss;
438 const char *country = NULL;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800439 int freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800440
441 if (bss) {
442 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
443
444 if (elem && elem[1] >= 2)
445 country = (const char *) (elem + 2);
446 }
447
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800448 freq = ieee80211_chan_to_freq(country, op_class, chan);
449 if (freq <= 0 && op_class == 0) {
450 /*
451 * Some APs do not advertise correct operating class
452 * information. Try to determine the most likely operating
453 * frequency based on the channel number.
454 */
455 if (chan >= 1 && chan <= 13)
456 freq = 2407 + chan * 5;
457 else if (chan == 14)
458 freq = 2484;
459 else if (chan >= 36 && chan <= 169)
460 freq = 5000 + chan * 5;
461 }
462 return freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800463}
464
465
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700466static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
467 const u8 *pos, u8 len,
468 struct neighbor_report *rep)
469{
470 u8 left = len;
471
472 if (left < 13) {
473 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
474 return;
475 }
476
477 os_memcpy(rep->bssid, pos, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800478 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700479 rep->regulatory_class = *(pos + 10);
480 rep->channel_number = *(pos + 11);
481 rep->phy_type = *(pos + 12);
482
483 pos += 13;
484 left -= 13;
485
486 while (left >= 2) {
487 u8 id, elen;
488
489 id = *pos++;
490 elen = *pos++;
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700491 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
492 left -= 2;
493 if (elen > left) {
494 wpa_printf(MSG_DEBUG,
495 "WNM: Truncated neighbor report subelement");
496 break;
497 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700498 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700499 left -= elen;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700500 pos += elen;
501 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800502
503 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
504 rep->channel_number);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700505}
506
507
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700508static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700509{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700510 unsigned int i;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700511
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700512 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
513 wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
514}
515
516
517static struct wpa_bss * get_first_acceptable(struct wpa_supplicant *wpa_s)
518{
519 unsigned int i;
520 struct neighbor_report *nei;
521
522 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
523 nei = &wpa_s->wnm_neighbor_report_elements[i];
524 if (nei->acceptable)
525 return wpa_bss_get_bssid(wpa_s, nei->bssid);
526 }
527
528 return NULL;
529}
530
531
532#ifdef CONFIG_MBO
533static struct wpa_bss *
534get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
535 enum mbo_transition_reject_reason *reason)
536{
537 struct wpa_bss *target = NULL;
538 struct wpa_bss_trans_info params;
539 struct wpa_bss_candidate_info *info = NULL;
540 struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
541 u8 *first_candidate_bssid = NULL, *pos;
542 unsigned int i;
543
544 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
545 params.n_candidates = 0;
546 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
547 if (!params.bssid)
548 return NULL;
549
550 pos = params.bssid;
551 for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
552 if (nei->is_first)
553 first_candidate_bssid = nei->bssid;
554 if (!nei->acceptable)
555 continue;
556 os_memcpy(pos, nei->bssid, ETH_ALEN);
557 pos += ETH_ALEN;
558 params.n_candidates++;
559 }
560
561 if (!params.n_candidates)
562 goto end;
563
564 info = wpa_drv_get_bss_trans_status(wpa_s, &params);
565 if (!info) {
566 /* If failed to get candidate BSS transition status from driver,
567 * get the first acceptable candidate from wpa_supplicant.
568 */
569 target = wpa_bss_get_bssid(wpa_s, params.bssid);
570 goto end;
571 }
572
573 /* Get the first acceptable candidate from driver */
574 for (i = 0; i < info->num; i++) {
575 if (info->candidates[i].is_accept) {
576 target = wpa_bss_get_bssid(wpa_s,
577 info->candidates[i].bssid);
578 goto end;
579 }
580 }
581
582 /* If Disassociation Imminent is set and driver rejects all the
583 * candidate select first acceptable candidate which has
584 * rssi > disassoc_imminent_rssi_threshold
585 */
586 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
587 for (i = 0; i < info->num; i++) {
588 target = wpa_bss_get_bssid(wpa_s,
589 info->candidates[i].bssid);
590 if (target &&
591 (target->level <
592 wpa_s->conf->disassoc_imminent_rssi_threshold))
593 continue;
594 goto end;
595 }
596 }
597
598 /* While sending BTM reject use reason code of the first candidate
599 * received in BTM request frame
600 */
601 if (reason) {
602 for (i = 0; i < info->num; i++) {
603 if (first_candidate_bssid &&
604 os_memcmp(first_candidate_bssid,
605 info->candidates[i].bssid, ETH_ALEN) == 0)
606 {
607 *reason = info->candidates[i].reject_reason;
608 break;
609 }
610 }
611 }
612
613 target = NULL;
614
615end:
616 os_free(params.bssid);
617 if (info) {
618 os_free(info->candidates);
619 os_free(info);
620 }
621 return target;
622}
623#endif /* CONFIG_MBO */
624
625
626static struct wpa_bss *
627compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs,
628 enum mbo_transition_reject_reason *reason)
629{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800630 u8 i;
631 struct wpa_bss *bss = wpa_s->current_bss;
632 struct wpa_bss *target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700633
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800634 if (!bss)
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700635 return NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700636
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800637 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800638 MAC2STR(wpa_s->bssid), bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800639
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700640 wnm_clear_acceptable(wpa_s);
641
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800642 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
643 struct neighbor_report *nei;
644
645 nei = &wpa_s->wnm_neighbor_report_elements[i];
646 if (nei->preference_present && nei->preference == 0) {
647 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
648 MAC2STR(nei->bssid));
649 continue;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700650 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800651
652 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
653 if (!target) {
654 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
655 " (pref %d) not found in scan results",
656 MAC2STR(nei->bssid),
657 nei->preference_present ? nei->preference :
658 -1);
659 continue;
660 }
661
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700662 if (age_secs) {
663 struct os_reltime now;
664
665 if (os_get_reltime(&now) == 0 &&
666 os_reltime_expired(&now, &target->last_update,
667 age_secs)) {
668 wpa_printf(MSG_DEBUG,
669 "Candidate BSS is more than %ld seconds old",
670 age_secs);
671 continue;
672 }
673 }
674
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800675 if (bss->ssid_len != target->ssid_len ||
676 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
677 /*
678 * TODO: Could consider allowing transition to another
679 * ESS if PMF was enabled for the association.
680 */
681 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
682 " (pref %d) in different ESS",
683 MAC2STR(nei->bssid),
684 nei->preference_present ? nei->preference :
685 -1);
686 continue;
687 }
688
Dmitry Shmidte4663042016-04-04 10:07:49 -0700689 if (wpa_s->current_ssid &&
690 !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800691 1, 0)) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700692 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
693 " (pref %d) does not match the current network profile",
694 MAC2STR(nei->bssid),
695 nei->preference_present ? nei->preference :
696 -1);
697 continue;
698 }
699
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800700 if (wpa_is_bss_tmp_disallowed(wpa_s, target->bssid)) {
701 wpa_printf(MSG_DEBUG,
702 "MBO: Candidate BSS " MACSTR
703 " retry delay is not over yet",
704 MAC2STR(nei->bssid));
705 continue;
706 }
707
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800708 if (target->level < bss->level && target->level < -80) {
709 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
710 " (pref %d) does not have sufficient signal level (%d)",
711 MAC2STR(nei->bssid),
712 nei->preference_present ? nei->preference :
713 -1,
714 target->level);
715 continue;
716 }
717
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700718 nei->acceptable = 1;
719 }
720
721#ifdef CONFIG_MBO
722 if (wpa_s->wnm_mbo_trans_reason_present)
723 target = get_mbo_transition_candidate(wpa_s, reason);
724 else
725 target = get_first_acceptable(wpa_s);
726#else /* CONFIG_MBO */
727 target = get_first_acceptable(wpa_s);
728#endif /* CONFIG_MBO */
729
730 if (target) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800731 wpa_printf(MSG_DEBUG,
732 "WNM: Found an acceptable preferred transition candidate BSS "
733 MACSTR " (RSSI %d)",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700734 MAC2STR(target->bssid), target->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700735 }
736
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700737 return target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700738}
739
740
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800741static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
742{
743 const u8 *ie_a, *ie_b;
744
745 if (!a || !b)
746 return 0;
747
748 ie_a = wpa_bss_get_ie(a, eid);
749 ie_b = wpa_bss_get_ie(b, eid);
750
751 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
752 return 0;
753
754 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
755}
756
757
758static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
759{
760 u32 info = 0;
761
762 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
763
764 /*
765 * Leave the security and key scope bits unset to indicate that the
766 * security information is not available.
767 */
768
769 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
770 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
771 if (bss->caps & WLAN_CAPABILITY_QOS)
772 info |= NEI_REP_BSSID_INFO_QOS;
773 if (bss->caps & WLAN_CAPABILITY_APSD)
774 info |= NEI_REP_BSSID_INFO_APSD;
775 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
776 info |= NEI_REP_BSSID_INFO_RM;
777 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
778 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
779 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
780 info |= NEI_REP_BSSID_INFO_IMM_BA;
781 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
782 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
783 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
784 info |= NEI_REP_BSSID_INFO_HT;
785
786 return info;
787}
788
789
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700790static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
791 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
792 u8 pref)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800793{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700794 if (wpabuf_len(*buf) + 18 >
795 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800796 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700797 "WNM: No room in frame for Neighbor Report element");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800798 return -1;
799 }
800
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700801 if (wpabuf_resize(buf, 18) < 0) {
802 wpa_printf(MSG_DEBUG,
803 "WNM: Failed to allocate memory for Neighbor Report element");
804 return -1;
805 }
806
807 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800808 /* length: 13 for basic neighbor report + 3 for preference subelement */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700809 wpabuf_put_u8(*buf, 16);
810 wpabuf_put_data(*buf, bssid, ETH_ALEN);
811 wpabuf_put_le32(*buf, bss_info);
812 wpabuf_put_u8(*buf, op_class);
813 wpabuf_put_u8(*buf, chan);
814 wpabuf_put_u8(*buf, phy_type);
815 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
816 wpabuf_put_u8(*buf, 1);
817 wpabuf_put_u8(*buf, pref);
818 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800819}
820
821
822static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700823 struct wpa_bss *bss, struct wpabuf **buf,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800824 u8 pref)
825{
826 const u8 *ie;
827 u8 op_class, chan;
828 int sec_chan = 0, vht = 0;
829 enum phy_type phy_type;
830 u32 info;
831 struct ieee80211_ht_operation *ht_oper = NULL;
832 struct ieee80211_vht_operation *vht_oper = NULL;
833
834 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
835 if (ie && ie[1] >= 2) {
836 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
837
838 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
839 sec_chan = 1;
840 else if (ht_oper->ht_param &
841 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
842 sec_chan = -1;
843 }
844
845 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
846 if (ie && ie[1] >= 1) {
847 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
848
849 if (vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80MHZ ||
850 vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_160MHZ ||
851 vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80P80MHZ)
852 vht = vht_oper->vht_op_info_chwidth;
853 }
854
855 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
856 &chan) == NUM_HOSTAPD_MODES) {
857 wpa_printf(MSG_DEBUG,
858 "WNM: Cannot determine operating class and channel");
859 return -2;
860 }
861
862 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
863 (vht_oper != NULL));
864 if (phy_type == PHY_TYPE_UNSPECIFIED) {
865 wpa_printf(MSG_DEBUG,
866 "WNM: Cannot determine BSS phy type for Neighbor Report");
867 return -2;
868 }
869
870 info = wnm_get_bss_info(wpa_s, bss);
871
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700872 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
873 pref);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800874}
875
876
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700877static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800878{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800879 unsigned int i, pref = 255;
880 struct os_reltime now;
881 struct wpa_ssid *ssid = wpa_s->current_ssid;
882
883 if (!ssid)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700884 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800885
886 /*
887 * TODO: Define when scan results are no longer valid for the candidate
888 * list.
889 */
890 os_get_reltime(&now);
891 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700892 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800893
894 wpa_printf(MSG_DEBUG,
895 "WNM: Add candidate list to BSS Transition Management Response frame");
896 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
897 struct wpa_bss *bss = wpa_s->last_scan_res[i];
898 int res;
899
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800900 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700901 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800902 if (res == -2)
903 continue; /* could not build entry for BSS */
904 if (res < 0)
905 break; /* no more room for candidates */
906 if (pref == 1)
907 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800908 }
909 }
910
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700911 wpa_hexdump_buf(MSG_DEBUG,
912 "WNM: BSS Transition Management Response candidate list",
913 *buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800914}
915
916
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700917#define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
918
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700919static void wnm_send_bss_transition_mgmt_resp(
920 struct wpa_supplicant *wpa_s, u8 dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700921 enum bss_trans_mgmt_status_code status,
922 enum mbo_transition_reject_reason reason,
923 u8 delay, const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800924{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700925 struct wpabuf *buf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800926 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800927
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700928 wpa_printf(MSG_DEBUG,
929 "WNM: Send BSS Transition Management Response to " MACSTR
930 " dialog_token=%u status=%u reason=%u delay=%d",
931 MAC2STR(wpa_s->bssid), dialog_token, status, reason, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800932 if (!wpa_s->current_bss) {
933 wpa_printf(MSG_DEBUG,
934 "WNM: Current BSS not known - drop response");
935 return;
936 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800937
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700938 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
939 if (!buf) {
940 wpa_printf(MSG_DEBUG,
941 "WNM: Failed to allocate memory for BTM response");
942 return;
943 }
944
945 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
946 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
947 wpabuf_put_u8(buf, dialog_token);
948 wpabuf_put_u8(buf, status);
949 wpabuf_put_u8(buf, delay);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800950 if (target_bssid) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700951 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800952 } else if (status == WNM_BSS_TM_ACCEPT) {
953 /*
954 * P802.11-REVmc clarifies that the Target BSSID field is always
955 * present when status code is zero, so use a fake value here if
956 * no BSSID is yet known.
957 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700958 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800959 }
960
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800961 if (status == WNM_BSS_TM_ACCEPT)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700962 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800963
964#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700965 if (status != WNM_BSS_TM_ACCEPT &&
966 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
967 u8 mbo[10];
968 size_t ret;
969
970 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
971 reason);
972 if (ret) {
973 if (wpabuf_resize(&buf, ret) < 0) {
974 wpabuf_free(buf);
975 wpa_printf(MSG_DEBUG,
976 "WNM: Failed to allocate memory for MBO IE");
977 return;
978 }
979
980 wpabuf_put_data(buf, mbo, ret);
981 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800982 }
983#endif /* CONFIG_MBO */
984
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800985 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
986 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700987 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800988 if (res < 0) {
989 wpa_printf(MSG_DEBUG,
990 "WNM: Failed to send BSS Transition Management Response");
991 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700992
993 wpabuf_free(buf);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800994}
995
996
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700997static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
998 struct wpa_bss *bss, struct wpa_ssid *ssid,
999 int after_new_scan)
1000{
1001 wpa_dbg(wpa_s, MSG_DEBUG,
1002 "WNM: Transition to BSS " MACSTR
1003 " based on BSS Transition Management Request (old BSSID "
1004 MACSTR " after_new_scan=%d)",
1005 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1006
1007 /* Send the BSS Management Response - Accept */
1008 if (wpa_s->wnm_reply) {
1009 wpa_s->wnm_reply = 0;
1010 wpa_printf(MSG_DEBUG,
1011 "WNM: Sending successful BSS Transition Management Response");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001012 wnm_send_bss_transition_mgmt_resp(
1013 wpa_s, wpa_s->wnm_dialog_token, WNM_BSS_TM_ACCEPT,
1014 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1015 bss->bssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001016 }
1017
1018 if (bss == wpa_s->current_bss) {
1019 wpa_printf(MSG_DEBUG,
1020 "WNM: Already associated with the preferred candidate");
1021 wnm_deallocate_memory(wpa_s);
1022 return;
1023 }
1024
1025 wpa_s->reassociate = 1;
1026 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1027 wpa_supplicant_connect(wpa_s, bss, ssid);
1028 wnm_deallocate_memory(wpa_s);
1029}
1030
1031
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001032int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001033{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001034 struct wpa_bss *bss;
1035 struct wpa_ssid *ssid = wpa_s->current_ssid;
1036 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001037 enum mbo_transition_reject_reason reason =
1038 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001039
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001040 if (!wpa_s->wnm_neighbor_report_elements)
1041 return 0;
1042
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001043 wpa_dbg(wpa_s, MSG_DEBUG,
1044 "WNM: Process scan results for BSS Transition Management");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001045 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
1046 &wpa_s->scan_trigger_time)) {
1047 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1048 wnm_deallocate_memory(wpa_s);
1049 return 0;
1050 }
1051
1052 if (!wpa_s->current_bss ||
1053 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
1054 ETH_ALEN) != 0) {
1055 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
1056 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001057 }
1058
1059 /* Compare the Neighbor Report and scan results */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001060 bss = compare_scan_neighbor_results(wpa_s, 0, &reason);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001061 if (!bss) {
1062 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1063 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1064 goto send_bss_resp_fail;
1065 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001066
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001067 /* Associate to the network */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001068 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001069 return 1;
1070
1071send_bss_resp_fail:
1072 if (!reply_on_fail)
1073 return 0;
1074
1075 /* Send reject response for all the failures */
1076
1077 if (wpa_s->wnm_reply) {
1078 wpa_s->wnm_reply = 0;
1079 wnm_send_bss_transition_mgmt_resp(wpa_s,
1080 wpa_s->wnm_dialog_token,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001081 status, reason, 0, NULL);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001082 }
1083 wnm_deallocate_memory(wpa_s);
1084
1085 return 0;
1086}
1087
1088
1089static int cand_pref_compar(const void *a, const void *b)
1090{
1091 const struct neighbor_report *aa = a;
1092 const struct neighbor_report *bb = b;
1093
1094 if (!aa->preference_present && !bb->preference_present)
1095 return 0;
1096 if (!aa->preference_present)
1097 return 1;
1098 if (!bb->preference_present)
1099 return -1;
1100 if (bb->preference > aa->preference)
1101 return 1;
1102 if (bb->preference < aa->preference)
1103 return -1;
1104 return 0;
1105}
1106
1107
1108static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1109{
1110 if (!wpa_s->wnm_neighbor_report_elements)
1111 return;
1112 qsort(wpa_s->wnm_neighbor_report_elements,
1113 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1114 cand_pref_compar);
1115}
1116
1117
1118static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1119{
1120 unsigned int i;
1121
1122 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1123 if (!wpa_s->wnm_neighbor_report_elements)
1124 return;
1125 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1126 struct neighbor_report *nei;
1127
1128 nei = &wpa_s->wnm_neighbor_report_elements[i];
1129 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1130 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1131 i, MAC2STR(nei->bssid), nei->bssid_info,
1132 nei->regulatory_class,
1133 nei->channel_number, nei->phy_type,
1134 nei->preference_present ? nei->preference : -1,
1135 nei->freq);
1136 }
1137}
1138
1139
1140static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1141{
1142 unsigned int i;
1143
1144 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1145 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1146 int j;
1147
1148 for (j = 0; j < mode->num_channels; j++) {
1149 struct hostapd_channel_data *chan;
1150
1151 chan = &mode->channels[j];
1152 if (chan->freq == freq &&
1153 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1154 return 1;
1155 }
1156 }
1157
1158 return 0;
1159}
1160
1161
1162static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1163{
1164 int *freqs;
1165 int num_freqs = 0;
1166 unsigned int i;
1167
1168 if (!wpa_s->wnm_neighbor_report_elements)
1169 return;
1170
1171 if (wpa_s->hw.modes == NULL)
1172 return;
1173
1174 os_free(wpa_s->next_scan_freqs);
1175 wpa_s->next_scan_freqs = NULL;
1176
1177 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1178 if (freqs == NULL)
1179 return;
1180
1181 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1182 struct neighbor_report *nei;
1183
1184 nei = &wpa_s->wnm_neighbor_report_elements[i];
1185 if (nei->freq <= 0) {
1186 wpa_printf(MSG_DEBUG,
1187 "WNM: Unknown neighbor operating frequency for "
1188 MACSTR " - scan all channels",
1189 MAC2STR(nei->bssid));
1190 os_free(freqs);
1191 return;
1192 }
1193 if (chan_supported(wpa_s, nei->freq))
1194 add_freq(freqs, &num_freqs, nei->freq);
1195 }
1196
1197 if (num_freqs == 0) {
1198 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001199 return;
1200 }
1201
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001202 wpa_printf(MSG_DEBUG,
1203 "WNM: Scan %d frequencies based on transition candidate list",
1204 num_freqs);
1205 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001206}
1207
1208
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001209static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
1210{
1211 struct wpa_scan_results *scan_res;
1212 struct wpa_bss *bss;
1213 struct wpa_ssid *ssid = wpa_s->current_ssid;
1214 u8 i, found = 0;
1215 size_t j;
1216
1217 wpa_dbg(wpa_s, MSG_DEBUG,
1218 "WNM: Fetch current scan results from the driver for checking transition candidates");
1219 scan_res = wpa_drv_get_scan_results2(wpa_s);
1220 if (!scan_res) {
1221 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
1222 return 0;
1223 }
1224
1225 if (scan_res->fetch_time.sec == 0)
1226 os_get_reltime(&scan_res->fetch_time);
1227
1228 filter_scan_res(wpa_s, scan_res);
1229
1230 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1231 struct neighbor_report *nei;
1232
1233 nei = &wpa_s->wnm_neighbor_report_elements[i];
1234 if (nei->preference_present && nei->preference == 0)
1235 continue;
1236
1237 for (j = 0; j < scan_res->num; j++) {
1238 struct wpa_scan_res *res;
1239 const u8 *ssid_ie;
1240
1241 res = scan_res->res[j];
1242 if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
1243 res->age > WNM_SCAN_RESULT_AGE * 1000)
1244 continue;
1245 bss = wpa_s->current_bss;
1246 ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
1247 if (bss && ssid_ie &&
1248 (bss->ssid_len != ssid_ie[1] ||
1249 os_memcmp(bss->ssid, ssid_ie + 2,
1250 bss->ssid_len) != 0))
1251 continue;
1252
1253 /* Potential candidate found */
1254 found = 1;
1255 scan_snr(res);
1256 scan_est_throughput(wpa_s, res);
1257 wpa_bss_update_scan_res(wpa_s, res,
1258 &scan_res->fetch_time);
1259 }
1260 }
1261
1262 wpa_scan_results_free(scan_res);
1263 if (!found) {
1264 wpa_dbg(wpa_s, MSG_DEBUG,
1265 "WNM: No transition candidate matches existing scan results");
1266 return 0;
1267 }
1268
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001269 bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE, NULL);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001270 if (!bss) {
1271 wpa_dbg(wpa_s, MSG_DEBUG,
1272 "WNM: Comparison of scan results against transition candidates did not find matches");
1273 return 0;
1274 }
1275
1276 /* Associate to the network */
1277 wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
1278 return 1;
1279}
1280
1281
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001282static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1283 const u8 *pos, const u8 *end,
1284 int reply)
1285{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001286 unsigned int beacon_int;
1287 u8 valid_int;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001288#ifdef CONFIG_MBO
1289 const u8 *vendor;
1290#endif /* CONFIG_MBO */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001291
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001292 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001293 return;
1294
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001295#ifdef CONFIG_MBO
1296 wpa_s->wnm_mbo_trans_reason_present = 0;
1297 wpa_s->wnm_mbo_transition_reason = 0;
1298#endif /* CONFIG_MBO */
1299
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001300 if (wpa_s->current_bss)
1301 beacon_int = wpa_s->current_bss->beacon_int;
1302 else
1303 beacon_int = 100; /* best guess */
1304
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001305 wpa_s->wnm_dialog_token = pos[0];
1306 wpa_s->wnm_mode = pos[1];
1307 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001308 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001309 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001310
1311 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1312 "dialog_token=%u request_mode=0x%x "
1313 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001314 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001315 wpa_s->wnm_dissoc_timer, valid_int);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001316
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001317#if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1318 if (wpa_s->reject_btm_req_reason) {
1319 wpa_printf(MSG_INFO,
1320 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1321 wpa_s->reject_btm_req_reason);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001322 wnm_send_bss_transition_mgmt_resp(
1323 wpa_s, wpa_s->wnm_dialog_token,
1324 wpa_s->reject_btm_req_reason,
1325 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001326 return;
1327 }
1328#endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1329
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001330 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001331
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001332 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001333 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001334 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1335 return;
1336 }
1337 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001338 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001339 }
1340
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001341 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001342 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001343
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001344 if (end - pos < 1 || 1 + pos[0] > end - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001345 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1346 "Management Request (URL)");
1347 return;
1348 }
1349 os_memcpy(url, pos + 1, pos[0]);
1350 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001351 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001352
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001353 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1354 wpa_sm_pmf_enabled(wpa_s->wpa),
1355 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001356 }
1357
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001358 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001359 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001360 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
1361 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001362 /* TODO: mark current BSS less preferred for
1363 * selection */
1364 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
1365 wpa_supplicant_req_scan(wpa_s, 0, 0);
1366 }
1367 }
1368
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001369#ifdef CONFIG_MBO
1370 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1371 if (vendor)
1372 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1373#endif /* CONFIG_MBO */
1374
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001375 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001376 unsigned int valid_ms;
1377
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001378 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001379 wnm_deallocate_memory(wpa_s);
1380 wpa_s->wnm_neighbor_report_elements = os_calloc(
1381 WNM_MAX_NEIGHBOR_REPORT,
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001382 sizeof(struct neighbor_report));
1383 if (wpa_s->wnm_neighbor_report_elements == NULL)
1384 return;
1385
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001386 while (end - pos >= 2 &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001387 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
1388 {
1389 u8 tag = *pos++;
1390 u8 len = *pos++;
1391
1392 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
1393 tag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001394 if (len > end - pos) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001395 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1396 return;
1397 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001398 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1399 struct neighbor_report *rep;
1400 rep = &wpa_s->wnm_neighbor_report_elements[
1401 wpa_s->wnm_num_neighbor_report];
1402 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001403 wpa_s->wnm_num_neighbor_report++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001404#ifdef CONFIG_MBO
1405 if (wpa_s->wnm_mbo_trans_reason_present &&
1406 wpa_s->wnm_num_neighbor_report == 1) {
1407 rep->is_first = 1;
1408 wpa_printf(MSG_DEBUG,
1409 "WNM: First transition candidate is "
1410 MACSTR, MAC2STR(rep->bssid));
1411 }
1412#endif /* CONFIG_MBO */
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -07001413 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001414
1415 pos += len;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001416 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001417
1418 if (!wpa_s->wnm_num_neighbor_report) {
1419 wpa_printf(MSG_DEBUG,
1420 "WNM: Candidate list included bit is set, but no candidates found");
1421 wnm_send_bss_transition_mgmt_resp(
1422 wpa_s, wpa_s->wnm_dialog_token,
1423 WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001424 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1425 NULL);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001426 return;
1427 }
1428
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001429 wnm_sort_cand_list(wpa_s);
1430 wnm_dump_cand_list(wpa_s);
1431 valid_ms = valid_int * beacon_int * 128 / 125;
1432 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1433 valid_ms);
1434 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1435 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
1436 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
1437 wpa_s->wnm_cand_valid_until.sec +=
1438 wpa_s->wnm_cand_valid_until.usec / 1000000;
1439 wpa_s->wnm_cand_valid_until.usec %= 1000000;
1440 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001441
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001442 /*
1443 * Fetch the latest scan results from the kernel and check for
1444 * candidates based on those results first. This can help in
1445 * finding more up-to-date information should the driver has
1446 * done some internal scanning operations after the last scan
1447 * result update in wpa_supplicant.
1448 */
1449 if (wnm_fetch_scan_results(wpa_s) > 0)
1450 return;
1451
1452 /*
1453 * Try to use previously received scan results, if they are
1454 * recent enough to use for a connection.
1455 */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001456 if (wpa_s->last_scan_res_used > 0) {
1457 struct os_reltime now;
1458
1459 os_get_reltime(&now);
1460 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
1461 wpa_printf(MSG_DEBUG,
1462 "WNM: Try to use recent scan results");
1463 if (wnm_scan_process(wpa_s, 0) > 0)
1464 return;
1465 wpa_printf(MSG_DEBUG,
1466 "WNM: No match in previous scan results - try a new scan");
1467 }
1468 }
1469
1470 wnm_set_scan_freqs(wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001471 if (wpa_s->wnm_num_neighbor_report == 1) {
1472 os_memcpy(wpa_s->next_scan_bssid,
1473 wpa_s->wnm_neighbor_report_elements[0].bssid,
1474 ETH_ALEN);
1475 wpa_printf(MSG_DEBUG,
1476 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1477 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1478 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001479 wpa_supplicant_req_scan(wpa_s, 0, 0);
1480 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001481 enum bss_trans_mgmt_status_code status;
1482 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
1483 status = WNM_BSS_TM_ACCEPT;
1484 else {
1485 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1486 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1487 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001488 wnm_send_bss_transition_mgmt_resp(
1489 wpa_s, wpa_s->wnm_dialog_token, status,
1490 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001491 }
1492}
1493
1494
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001495#define BTM_QUERY_MIN_SIZE 4
1496
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001497int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001498 u8 query_reason,
1499 const char *btm_candidates,
1500 int cand_list)
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001501{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001502 struct wpabuf *buf;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001503 int ret;
1504
1505 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001506 MACSTR " query_reason=%u%s",
1507 MAC2STR(wpa_s->bssid), query_reason,
1508 cand_list ? " candidate list" : "");
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001509
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001510 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1511 if (!buf)
1512 return -1;
1513
1514 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1515 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1516 wpabuf_put_u8(buf, 1);
1517 wpabuf_put_u8(buf, query_reason);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001518
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001519 if (cand_list)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001520 wnm_add_cand_list(wpa_s, &buf);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001521
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001522 if (btm_candidates) {
1523 const size_t max_len = 1000;
1524
1525 ret = wpabuf_resize(&buf, max_len);
1526 if (ret < 0) {
1527 wpabuf_free(buf);
1528 return ret;
1529 }
1530
1531 ret = ieee802_11_parse_candidate_list(btm_candidates,
1532 wpabuf_put(buf, 0),
1533 max_len);
1534 if (ret < 0) {
1535 wpabuf_free(buf);
1536 return ret;
1537 }
1538
1539 wpabuf_put(buf, ret);
1540 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001541
1542 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1543 wpa_s->own_addr, wpa_s->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001544 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001545
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001546 wpabuf_free(buf);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001547 return ret;
1548}
1549
1550
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001551static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1552 const u8 *sa, const u8 *data,
1553 int len)
1554{
1555 const u8 *pos, *end, *next;
1556 u8 ie, ie_len;
1557
1558 pos = data;
1559 end = data + len;
1560
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001561 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001562 ie = *pos++;
1563 ie_len = *pos++;
1564 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1565 ie, ie_len);
1566 if (ie_len > end - pos) {
1567 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1568 "subelement");
1569 break;
1570 }
1571 next = pos + ie_len;
1572 if (ie_len < 4) {
1573 pos = next;
1574 continue;
1575 }
1576 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1577 WPA_GET_BE24(pos), pos[3]);
1578
1579#ifdef CONFIG_HS20
1580 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1581 WPA_GET_BE24(pos) == OUI_WFA &&
1582 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1583 /* Subscription Remediation subelement */
1584 const u8 *ie_end;
1585 u8 url_len;
1586 char *url;
1587 u8 osu_method;
1588
1589 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1590 "subelement");
1591 ie_end = pos + ie_len;
1592 pos += 4;
1593 url_len = *pos++;
1594 if (url_len == 0) {
1595 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1596 url = NULL;
1597 osu_method = 1;
1598 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001599 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001600 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1601 url_len,
1602 (int) (ie_end - pos));
1603 break;
1604 }
1605 url = os_malloc(url_len + 1);
1606 if (url == NULL)
1607 break;
1608 os_memcpy(url, pos, url_len);
1609 url[url_len] = '\0';
1610 osu_method = pos[url_len];
1611 }
1612 hs20_rx_subscription_remediation(wpa_s, url,
1613 osu_method);
1614 os_free(url);
1615 pos = next;
1616 continue;
1617 }
1618
1619 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1620 WPA_GET_BE24(pos) == OUI_WFA &&
1621 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1622 const u8 *ie_end;
1623 u8 url_len;
1624 char *url;
1625 u8 code;
1626 u16 reauth_delay;
1627
1628 ie_end = pos + ie_len;
1629 pos += 4;
1630 code = *pos++;
1631 reauth_delay = WPA_GET_LE16(pos);
1632 pos += 2;
1633 url_len = *pos++;
1634 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1635 "Imminent - Reason Code %u "
1636 "Re-Auth Delay %u URL Length %u",
1637 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001638 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001639 break;
1640 url = os_malloc(url_len + 1);
1641 if (url == NULL)
1642 break;
1643 os_memcpy(url, pos, url_len);
1644 url[url_len] = '\0';
1645 hs20_rx_deauth_imminent_notice(wpa_s, code,
1646 reauth_delay, url);
1647 os_free(url);
1648 pos = next;
1649 continue;
1650 }
1651#endif /* CONFIG_HS20 */
1652
1653 pos = next;
1654 }
1655}
1656
1657
1658static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1659 const u8 *sa, const u8 *frm, int len)
1660{
1661 const u8 *pos, *end;
1662 u8 dialog_token, type;
1663
1664 /* Dialog Token [1] | Type [1] | Subelements */
1665
1666 if (len < 2 || sa == NULL)
1667 return;
1668 end = frm + len;
1669 pos = frm;
1670 dialog_token = *pos++;
1671 type = *pos++;
1672
1673 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1674 "(dialog_token %u type %u sa " MACSTR ")",
1675 dialog_token, type, MAC2STR(sa));
1676 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1677 pos, end - pos);
1678
1679 if (wpa_s->wpa_state != WPA_COMPLETED ||
1680 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1681 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1682 "from our AP - ignore it");
1683 return;
1684 }
1685
1686 switch (type) {
1687 case 1:
1688 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1689 break;
1690 default:
1691 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1692 "WNM-Notification type %u", type);
1693 break;
1694 }
1695}
1696
1697
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001698void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001699 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001700{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001701 const u8 *pos, *end;
1702 u8 act;
1703
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001704 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001705 return;
1706
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001707 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001708 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001709 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001710
1711 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001712 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001713 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001714 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001715 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1716 "frame");
1717 return;
1718 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001719
1720 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001721 case WNM_BSS_TRANS_MGMT_REQ:
1722 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001723 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001724 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001725 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001726 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001727 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001728 case WNM_NOTIFICATION_REQ:
1729 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1730 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001731 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001732 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001733 break;
1734 }
1735}