]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wnm_sta.c
Find correct driver for interface additions/removals
[thirdparty/hostap.git] / wpa_supplicant / wnm_sta.c
CommitLineData
75cad1a0
XC
1/*
2 * wpa_supplicant - WNM
ae8535b6 3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
75cad1a0
XC
4 *
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"
e1117c1c 13#include "common/ieee802_11_common.h"
ae8535b6 14#include "common/wpa_ctrl.h"
75cad1a0 15#include "rsn_supp/wpa.h"
61c54976
JM
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
2049a875 18#include "scan.h"
e27d20bb
VK
19#include "ctrl_iface.h"
20#include "bss.h"
21#include "wnm_sta.h"
95a3ea94 22#include "hs20_supplicant.h"
75cad1a0
XC
23
24#define MAX_TFS_IE_LEN 1024
e27d20bb 25#define WNM_MAX_NEIGHBOR_REPORT 10
75cad1a0 26
75cad1a0
XC
27
28/* get the TFS IE from driver */
29static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
30 u16 *buf_len, enum wnm_oper oper)
31{
32 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
33
34 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
35}
36
37
38/* set the TFS IE to driver */
39static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
dacd789f 40 const u8 *addr, const u8 *buf, u16 buf_len,
75cad1a0
XC
41 enum wnm_oper oper)
42{
dacd789f
JM
43 u16 len = buf_len;
44
75cad1a0
XC
45 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
46
dacd789f 47 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
75cad1a0
XC
48}
49
50
51/* MLME-SLEEPMODE.request */
52int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
cd0ef657 53 u8 action, u16 intval, struct wpabuf *tfs_req)
75cad1a0
XC
54{
55 struct ieee80211_mgmt *mgmt;
56 int res;
57 size_t len;
58 struct wnm_sleep_element *wnmsleep_ie;
59 u8 *wnmtfs_ie;
60 u8 wnmsleep_ie_len;
61 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
62 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
63 WNM_SLEEP_TFS_REQ_IE_NONE;
64
cd0ef657
JM
65 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
66 "action=%s to " MACSTR,
67 action == 0 ? "enter" : "exit",
68 MAC2STR(wpa_s->bssid));
69
75cad1a0
XC
70 /* WNM-Sleep Mode IE */
71 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
72 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
73 if (wnmsleep_ie == NULL)
74 return -1;
75 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
76 wnmsleep_ie->len = wnmsleep_ie_len - 2;
77 wnmsleep_ie->action_type = action;
78 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
e9199e31 79 wnmsleep_ie->intval = host_to_le16(intval);
cd0ef657
JM
80 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
81 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
75cad1a0
XC
82
83 /* TFS IE(s) */
cd0ef657
JM
84 if (tfs_req) {
85 wnmtfs_ie_len = wpabuf_len(tfs_req);
86 wnmtfs_ie = os_malloc(wnmtfs_ie_len);
87 if (wnmtfs_ie == NULL) {
88 os_free(wnmsleep_ie);
89 return -1;
90 }
91 os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
92 } else {
93 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
94 if (wnmtfs_ie == NULL) {
95 os_free(wnmsleep_ie);
96 return -1;
97 }
98 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
99 tfs_oper)) {
100 wnmtfs_ie_len = 0;
101 os_free(wnmtfs_ie);
102 wnmtfs_ie = NULL;
103 }
75cad1a0 104 }
cd0ef657
JM
105 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
106 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
75cad1a0
XC
107
108 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
109 if (mgmt == NULL) {
110 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
111 "WNM-Sleep Request action frame");
14df897c
JM
112 os_free(wnmsleep_ie);
113 os_free(wnmtfs_ie);
75cad1a0
XC
114 return -1;
115 }
116
117 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
118 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
119 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
120 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
121 WLAN_FC_STYPE_ACTION);
122 mgmt->u.action.category = WLAN_ACTION_WNM;
123 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
e0c54a15 124 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
75cad1a0
XC
125 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
126 wnmsleep_ie_len);
127 /* copy TFS IE here */
128 if (wnmtfs_ie_len > 0) {
129 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
130 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
131 }
132
133 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
134 wnmtfs_ie_len;
135
136 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
137 wpa_s->own_addr, wpa_s->bssid,
138 &mgmt->u.action.category, len, 0);
139 if (res < 0)
140 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
141 "(action=%d, intval=%d)", action, intval);
03ed0a52
JM
142 else
143 wpa_s->wnmsleep_used = 1;
75cad1a0
XC
144
145 os_free(wnmsleep_ie);
146 os_free(wnmtfs_ie);
147 os_free(mgmt);
148
149 return res;
150}
151
152
74b4a360 153static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
dacd789f
JM
154 const u8 *tfsresp_ie_start,
155 const u8 *tfsresp_ie_end)
74b4a360
JM
156{
157 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
158 wpa_s->bssid, NULL, NULL);
159 /* remove GTK/IGTK ?? */
160
161 /* set the TFS Resp IE(s) */
162 if (tfsresp_ie_start && tfsresp_ie_end &&
163 tfsresp_ie_end - tfsresp_ie_start >= 0) {
164 u16 tfsresp_ie_len;
165 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
166 tfsresp_ie_start;
167 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
168 /* pass the TFS Resp IE(s) to driver for processing */
169 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
170 tfsresp_ie_start,
dacd789f 171 tfsresp_ie_len,
74b4a360
JM
172 WNM_SLEEP_TFS_RESP_IE_SET))
173 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
174 }
175}
176
177
178static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
179 const u8 *frm, u16 key_len_total)
180{
181 u8 *ptr, *end;
182 u8 gtk_len;
183
184 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
185 NULL, NULL);
186
187 /* Install GTK/IGTK */
188
189 /* point to key data field */
dbfb8e82 190 ptr = (u8 *) frm + 1 + 2;
74b4a360
JM
191 end = ptr + key_len_total;
192 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
193
2cb28a4c
JM
194 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
195 wpa_msg(wpa_s, MSG_INFO,
196 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
197 return;
198 }
199
bdce45b8
JM
200 while (end - ptr > 1) {
201 if (2 + ptr[1] > end - ptr) {
74b4a360
JM
202 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
203 "length");
204 if (end > ptr) {
205 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
206 ptr, end - ptr);
207 }
208 break;
209 }
210 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
211 if (ptr[1] < 11 + 5) {
212 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
213 "subelem");
214 break;
215 }
216 gtk_len = *(ptr + 4);
217 if (ptr[1] < 11 + gtk_len ||
218 gtk_len < 5 || gtk_len > 32) {
219 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
220 "subelem");
221 break;
222 }
223 wpa_wnmsleep_install_key(
224 wpa_s->wpa,
225 WNM_SLEEP_SUBELEM_GTK,
226 ptr);
227 ptr += 13 + gtk_len;
228#ifdef CONFIG_IEEE80211W
229 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
230 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
231 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
232 "subelem");
233 break;
234 }
235 wpa_wnmsleep_install_key(wpa_s->wpa,
236 WNM_SLEEP_SUBELEM_IGTK, ptr);
237 ptr += 10 + WPA_IGTK_LEN;
238#endif /* CONFIG_IEEE80211W */
239 } else
240 break; /* skip the loop */
241 }
242}
243
244
75cad1a0
XC
245static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
246 const u8 *frm, int len)
247{
248 /*
9a147ba1 249 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
75cad1a0
XC
250 * WNM-Sleep Mode IE | TFS Response IE
251 */
dacd789f 252 const u8 *pos = frm; /* point to payload after the action field */
9a147ba1 253 u16 key_len_total;
75cad1a0
XC
254 struct wnm_sleep_element *wnmsleep_ie = NULL;
255 /* multiple TFS Resp IE (assuming consecutive) */
dacd789f
JM
256 const u8 *tfsresp_ie_start = NULL;
257 const u8 *tfsresp_ie_end = NULL;
fecc09ed 258 size_t left;
75cad1a0 259
03ed0a52
JM
260 if (!wpa_s->wnmsleep_used) {
261 wpa_printf(MSG_DEBUG,
262 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode has not been used in this association");
263 return;
264 }
265
9a147ba1
JM
266 if (len < 3)
267 return;
268 key_len_total = WPA_GET_LE16(frm + 1);
269
dbfb8e82
JM
270 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
271 frm[0], key_len_total);
fecc09ed
JM
272 left = len - 3;
273 if (key_len_total > left) {
68db9ab0
JM
274 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
275 return;
276 }
fecc09ed 277 pos += 3 + key_len_total;
bdce45b8 278 while (pos - frm + 1 < len) {
75cad1a0 279 u8 ie_len = *(pos + 1);
bdce45b8 280 if (2 + ie_len > frm + len - pos) {
68db9ab0
JM
281 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
282 break;
283 }
284 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
8acbe7f2 285 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
75cad1a0
XC
286 wnmsleep_ie = (struct wnm_sleep_element *) pos;
287 else if (*pos == WLAN_EID_TFS_RESP) {
288 if (!tfsresp_ie_start)
289 tfsresp_ie_start = pos;
290 tfsresp_ie_end = pos;
291 } else
292 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
293 pos += ie_len + 2;
294 }
295
296 if (!wnmsleep_ie) {
297 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
298 return;
299 }
300
62f6fbb4
JM
301 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
302 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
75cad1a0
XC
303 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
304 "frame (action=%d, intval=%d)",
305 wnmsleep_ie->action_type, wnmsleep_ie->intval);
df80a0cc 306 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
74b4a360
JM
307 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
308 tfsresp_ie_end);
df80a0cc 309 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
74b4a360 310 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
75cad1a0
XC
311 }
312 } else {
313 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
314 "(action=%d, intval=%d)",
315 wnmsleep_ie->action_type, wnmsleep_ie->intval);
df80a0cc 316 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
75cad1a0
XC
317 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
318 wpa_s->bssid, NULL, NULL);
df80a0cc 319 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
75cad1a0
XC
320 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
321 wpa_s->bssid, NULL, NULL);
322 }
323}
324
325
e27d20bb
VK
326void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
327{
328 int i;
329
330 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
e27d20bb 331 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
e27d20bb
VK
332 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
333 }
334
ec331d09 335 wpa_s->wnm_num_neighbor_report = 0;
e27d20bb
VK
336 os_free(wpa_s->wnm_neighbor_report_elements);
337 wpa_s->wnm_neighbor_report_elements = NULL;
338}
339
340
341static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
342 u8 id, u8 elen, const u8 *pos)
343{
344 switch (id) {
345 case WNM_NEIGHBOR_TSF:
346 if (elen < 2 + 2) {
347 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
348 break;
349 }
09322678
JM
350 rep->tsf_offset = WPA_GET_LE16(pos);
351 rep->beacon_int = WPA_GET_LE16(pos + 2);
352 rep->tsf_present = 1;
e27d20bb
VK
353 break;
354 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
355 if (elen < 2) {
356 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
357 "country string");
358 break;
359 }
09322678
JM
360 os_memcpy(rep->country, pos, 2);
361 rep->country_present = 1;
e27d20bb
VK
362 break;
363 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
364 if (elen < 1) {
365 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
366 "candidate");
367 break;
368 }
09322678
JM
369 rep->preference = pos[0];
370 rep->preference_present = 1;
e27d20bb
VK
371 break;
372 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
09322678
JM
373 rep->bss_term_tsf = WPA_GET_LE64(pos);
374 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
375 rep->bss_term_present = 1;
e27d20bb
VK
376 break;
377 case WNM_NEIGHBOR_BEARING:
378 if (elen < 8) {
379 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
380 "bearing");
381 break;
382 }
09322678
JM
383 rep->bearing = WPA_GET_LE16(pos);
384 rep->distance = WPA_GET_LE32(pos + 2);
385 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
386 rep->bearing_present = 1;
e27d20bb
VK
387 break;
388 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
f6ce70dc 389 if (elen < 1) {
e27d20bb
VK
390 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
391 "pilot");
392 break;
393 }
e9cb7b92 394 os_free(rep->meas_pilot);
e27d20bb
VK
395 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
396 if (rep->meas_pilot == NULL)
397 break;
e27d20bb 398 rep->meas_pilot->measurement_pilot = pos[0];
f6ce70dc
JM
399 rep->meas_pilot->subelem_len = elen - 1;
400 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
e27d20bb
VK
401 break;
402 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
f6ce70dc 403 if (elen < 5) {
e27d20bb
VK
404 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
405 "capabilities");
406 break;
407 }
09322678
JM
408 os_memcpy(rep->rm_capab, pos, 5);
409 rep->rm_capab_present = 1;
e27d20bb
VK
410 break;
411 case WNM_NEIGHBOR_MULTIPLE_BSSID:
f6ce70dc 412 if (elen < 1) {
e27d20bb
VK
413 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
414 break;
415 }
e9cb7b92 416 os_free(rep->mul_bssid);
e27d20bb
VK
417 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
418 if (rep->mul_bssid == NULL)
419 break;
e27d20bb 420 rep->mul_bssid->max_bssid_indicator = pos[0];
f6ce70dc
JM
421 rep->mul_bssid->subelem_len = elen - 1;
422 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
e27d20bb
VK
423 break;
424 }
425}
426
427
e1117c1c
JM
428static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
429{
158211b2
JM
430 struct wpa_bss *bss = wpa_s->current_bss;
431 const char *country = NULL;
80ce804e 432 int freq;
158211b2
JM
433
434 if (bss) {
435 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
436
437 if (elem && elem[1] >= 2)
438 country = (const char *) (elem + 2);
439 }
440
80ce804e
JM
441 freq = ieee80211_chan_to_freq(country, op_class, chan);
442 if (freq <= 0 && op_class == 0) {
443 /*
444 * Some APs do not advertise correct operating class
445 * information. Try to determine the most likely operating
446 * frequency based on the channel number.
447 */
448 if (chan >= 1 && chan <= 13)
449 freq = 2407 + chan * 5;
450 else if (chan == 14)
451 freq = 2484;
452 else if (chan >= 36 && chan <= 169)
453 freq = 5000 + chan * 5;
454 }
455 return freq;
e1117c1c
JM
456}
457
458
e27d20bb
VK
459static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
460 const u8 *pos, u8 len,
461 struct neighbor_report *rep)
462{
463 u8 left = len;
464
465 if (left < 13) {
466 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
467 return;
468 }
469
470 os_memcpy(rep->bssid, pos, ETH_ALEN);
4c381f0d 471 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
e27d20bb
VK
472 rep->regulatory_class = *(pos + 10);
473 rep->channel_number = *(pos + 11);
474 rep->phy_type = *(pos + 12);
475
476 pos += 13;
477 left -= 13;
478
479 while (left >= 2) {
480 u8 id, elen;
481
482 id = *pos++;
483 elen = *pos++;
1aa6f953
JM
484 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
485 left -= 2;
486 if (elen > left) {
487 wpa_printf(MSG_DEBUG,
488 "WNM: Truncated neighbor report subelement");
489 break;
490 }
e27d20bb 491 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
1aa6f953 492 left -= elen;
e27d20bb
VK
493 pos += elen;
494 }
e1117c1c
JM
495
496 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
497 rep->channel_number);
e27d20bb
VK
498}
499
500
d0b9ab69
JM
501static struct wpa_bss *
502compare_scan_neighbor_results(struct wpa_supplicant *wpa_s)
e27d20bb
VK
503{
504
d0b9ab69 505 u8 i;
027454d2 506 struct wpa_bss *bss = wpa_s->current_bss;
d0b9ab69 507 struct wpa_bss *target;
e27d20bb 508
d0b9ab69 509 if (!bss)
e27d20bb
VK
510 return 0;
511
3c1060ff 512 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
027454d2 513 MAC2STR(wpa_s->bssid), bss->level);
3c1060ff 514
d0b9ab69
JM
515 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
516 struct neighbor_report *nei;
027454d2 517
d0b9ab69 518 nei = &wpa_s->wnm_neighbor_report_elements[i];
027454d2
JM
519 if (nei->preference_present && nei->preference == 0) {
520 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
521 MAC2STR(nei->bssid));
522 continue;
523 }
524
d0b9ab69
JM
525 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
526 if (!target) {
027454d2
JM
527 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
528 " (pref %d) not found in scan results",
529 MAC2STR(nei->bssid),
530 nei->preference_present ? nei->preference :
531 -1);
532 continue;
533 }
534
d0b9ab69
JM
535 if (bss->ssid_len != target->ssid_len ||
536 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
027454d2
JM
537 /*
538 * TODO: Could consider allowing transition to another
539 * ESS if PMF was enabled for the association.
540 */
541 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
542 " (pref %d) in different ESS",
543 MAC2STR(nei->bssid),
544 nei->preference_present ? nei->preference :
545 -1);
546 continue;
547 }
548
dd599908
AS
549 if (wpa_is_bss_tmp_disallowed(wpa_s, target->bssid)) {
550 wpa_printf(MSG_DEBUG,
551 "MBO: Candidate BSS " MACSTR
552 " retry delay is not over yet",
553 MAC2STR(nei->bssid));
554 continue;
555 }
556
d0b9ab69 557 if (target->level < bss->level && target->level < -80) {
027454d2
JM
558 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
559 " (pref %d) does not have sufficient signal level (%d)",
560 MAC2STR(nei->bssid),
561 nei->preference_present ? nei->preference :
562 -1,
d0b9ab69 563 target->level);
027454d2
JM
564 continue;
565 }
566
567 wpa_printf(MSG_DEBUG,
d0b9ab69 568 "WNM: Found an acceptable preferred transition candidate BSS "
027454d2 569 MACSTR " (RSSI %d)",
d0b9ab69
JM
570 MAC2STR(nei->bssid), target->level);
571 return target;
e27d20bb
VK
572 }
573
d0b9ab69 574 return NULL;
e27d20bb
VK
575}
576
577
84d1c0fd
AS
578static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
579{
580 const u8 *ie_a, *ie_b;
581
582 if (!a || !b)
583 return 0;
584
585 ie_a = wpa_bss_get_ie(a, eid);
586 ie_b = wpa_bss_get_ie(b, eid);
587
588 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
589 return 0;
590
591 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
592}
593
594
595static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
596{
597 u32 info = 0;
598
599 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
600
601 /*
602 * Leave the security and key scope bits unset to indicate that the
603 * security information is not available.
604 */
605
606 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
607 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
608 if (bss->caps & WLAN_CAPABILITY_QOS)
609 info |= NEI_REP_BSSID_INFO_QOS;
610 if (bss->caps & WLAN_CAPABILITY_APSD)
611 info |= NEI_REP_BSSID_INFO_APSD;
612 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
613 info |= NEI_REP_BSSID_INFO_RM;
614 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
615 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
616 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
617 info |= NEI_REP_BSSID_INFO_IMM_BA;
618 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
619 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
620 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
621 info |= NEI_REP_BSSID_INFO_HT;
622
623 return info;
624}
625
626
627static int wnm_add_nei_rep(u8 *buf, size_t len, const u8 *bssid, u32 bss_info,
628 u8 op_class, u8 chan, u8 phy_type, u8 pref)
629{
630 u8 *pos = buf;
631
632 if (len < 18) {
633 wpa_printf(MSG_DEBUG,
634 "WNM: Not enough room for Neighbor Report element");
635 return -1;
636 }
637
638 *pos++ = WLAN_EID_NEIGHBOR_REPORT;
639 /* length: 13 for basic neighbor report + 3 for preference subelement */
640 *pos++ = 16;
641 os_memcpy(pos, bssid, ETH_ALEN);
642 pos += ETH_ALEN;
643 WPA_PUT_LE32(pos, bss_info);
644 pos += 4;
645 *pos++ = op_class;
646 *pos++ = chan;
647 *pos++ = phy_type;
648 *pos++ = WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE;
649 *pos++ = 1;
650 *pos++ = pref;
651 return pos - buf;
652}
653
654
655static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
656 struct wpa_bss *bss, u8 *buf, size_t len,
657 u8 pref)
658{
659 const u8 *ie;
660 u8 op_class, chan;
661 int sec_chan = 0, vht = 0;
662 enum phy_type phy_type;
663 u32 info;
664 struct ieee80211_ht_operation *ht_oper = NULL;
665 struct ieee80211_vht_operation *vht_oper = NULL;
666
667 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
668 if (ie && ie[1] >= 2) {
669 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
670
671 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
672 sec_chan = 1;
673 else if (ht_oper->ht_param &
674 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
675 sec_chan = -1;
676 }
677
678 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
679 if (ie && ie[1] >= 1) {
680 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
681
682 if (vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80MHZ ||
683 vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_160MHZ ||
684 vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80P80MHZ)
685 vht = vht_oper->vht_op_info_chwidth;
686 }
687
688 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
689 &chan) == NUM_HOSTAPD_MODES) {
690 wpa_printf(MSG_DEBUG,
691 "WNM: Cannot determine operating class and channel");
692 return -2;
693 }
694
695 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
696 (vht_oper != NULL));
697 if (phy_type == PHY_TYPE_UNSPECIFIED) {
698 wpa_printf(MSG_DEBUG,
699 "WNM: Cannot determine BSS phy type for Neighbor Report");
700 return -2;
701 }
702
703 info = wnm_get_bss_info(wpa_s, bss);
704
705 return wnm_add_nei_rep(buf, len, bss->bssid, info, op_class, chan,
706 phy_type, pref);
707}
708
709
710static int wnm_add_cand_list(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
711{
712 u8 *pos = buf;
713 unsigned int i, pref = 255;
714 struct os_reltime now;
715 struct wpa_ssid *ssid = wpa_s->current_ssid;
716
717 if (!ssid)
718 return 0;
719
720 /*
721 * TODO: Define when scan results are no longer valid for the candidate
722 * list.
723 */
724 os_get_reltime(&now);
725 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
726 return 0;
727
728 wpa_printf(MSG_DEBUG,
729 "WNM: Add candidate list to BSS Transition Management Response frame");
730 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
731 struct wpa_bss *bss = wpa_s->last_scan_res[i];
732 int res;
733
734 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1)) {
735 res = wnm_nei_rep_add_bss(wpa_s, bss, pos, len, pref--);
736 if (res == -2)
737 continue; /* could not build entry for BSS */
738 if (res < 0)
739 break; /* no more room for candidates */
740 if (pref == 1)
741 break;
742
743 pos += res;
744 len -= res;
745 }
746 }
747
748 wpa_hexdump(MSG_DEBUG,
749 "WNM: BSS Transition Management Response candidate list",
750 buf, pos - buf);
751
752 return pos - buf;
753}
754
755
7b53acd3
JM
756static void wnm_send_bss_transition_mgmt_resp(
757 struct wpa_supplicant *wpa_s, u8 dialog_token,
758 enum bss_trans_mgmt_status_code status, u8 delay,
759 const u8 *target_bssid)
2049a875 760{
84d1c0fd 761 u8 buf[2000], *pos;
2049a875
JM
762 struct ieee80211_mgmt *mgmt;
763 size_t len;
f5f3728a 764 int res;
2049a875
JM
765
766 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
767 "to " MACSTR " dialog_token=%u status=%u delay=%d",
768 MAC2STR(wpa_s->bssid), dialog_token, status, delay);
d0b9ab69
JM
769 if (!wpa_s->current_bss) {
770 wpa_printf(MSG_DEBUG,
771 "WNM: Current BSS not known - drop response");
772 return;
773 }
2049a875
JM
774
775 mgmt = (struct ieee80211_mgmt *) buf;
776 os_memset(&buf, 0, sizeof(buf));
777 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
778 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
779 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
780 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
781 WLAN_FC_STYPE_ACTION);
782 mgmt->u.action.category = WLAN_ACTION_WNM;
783 mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
784 mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
785 mgmt->u.action.u.bss_tm_resp.status_code = status;
786 mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
787 pos = mgmt->u.action.u.bss_tm_resp.variable;
788 if (target_bssid) {
789 os_memcpy(pos, target_bssid, ETH_ALEN);
790 pos += ETH_ALEN;
2cd0f6a4
JM
791 } else if (status == WNM_BSS_TM_ACCEPT) {
792 /*
793 * P802.11-REVmc clarifies that the Target BSSID field is always
794 * present when status code is zero, so use a fake value here if
795 * no BSSID is yet known.
796 */
797 os_memset(pos, 0, ETH_ALEN);
798 pos += ETH_ALEN;
2049a875
JM
799 }
800
84d1c0fd
AS
801 if (status == WNM_BSS_TM_ACCEPT)
802 pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
803
c8082d2b
AS
804#ifdef CONFIG_MBO
805 if (status != WNM_BSS_TM_ACCEPT) {
806 pos += wpas_mbo_ie_bss_trans_reject(
807 wpa_s, pos, buf + sizeof(buf) - pos,
808 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED);
809 }
810#endif /* CONFIG_MBO */
811
2049a875
JM
812 len = pos - (u8 *) &mgmt->u.action.category;
813
f5f3728a
JM
814 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
815 wpa_s->own_addr, wpa_s->bssid,
816 &mgmt->u.action.category, len, 0);
817 if (res < 0) {
818 wpa_printf(MSG_DEBUG,
819 "WNM: Failed to send BSS Transition Management Response");
820 }
2049a875
JM
821}
822
823
75d65857 824int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
e27d20bb 825{
d0b9ab69
JM
826 struct wpa_bss *bss;
827 struct wpa_ssid *ssid = wpa_s->current_ssid;
828 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
e27d20bb 829
d0b9ab69
JM
830 if (!wpa_s->wnm_neighbor_report_elements)
831 return 0;
832
833 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
834 &wpa_s->scan_trigger_time)) {
835 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
836 wnm_deallocate_memory(wpa_s);
837 return 0;
838 }
839
840 if (!wpa_s->current_bss ||
841 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
842 ETH_ALEN) != 0) {
843 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
844 return 0;
e27d20bb
VK
845 }
846
847 /* Compare the Neighbor Report and scan results */
d0b9ab69
JM
848 bss = compare_scan_neighbor_results(wpa_s);
849 if (!bss) {
850 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
851 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
852 goto send_bss_resp_fail;
853 }
e27d20bb 854
d0b9ab69
JM
855 /* Associate to the network */
856 /* Send the BSS Management Response - Accept */
857 if (wpa_s->wnm_reply) {
858 wpa_s->wnm_reply = 0;
859 wnm_send_bss_transition_mgmt_resp(wpa_s,
e27d20bb 860 wpa_s->wnm_dialog_token,
7b53acd3 861 WNM_BSS_TM_ACCEPT,
d0b9ab69
JM
862 0, bss->bssid);
863 }
e27d20bb 864
d0b9ab69
JM
865 if (bss == wpa_s->current_bss) {
866 wpa_printf(MSG_DEBUG,
867 "WNM: Already associated with the preferred candidate");
1e74ae4d 868 wnm_deallocate_memory(wpa_s);
d0b9ab69 869 return 1;
e27d20bb
VK
870 }
871
d0b9ab69
JM
872 wpa_s->reassociate = 1;
873 wpa_supplicant_connect(wpa_s, bss, ssid);
874 wnm_deallocate_memory(wpa_s);
875 return 1;
876
e27d20bb 877send_bss_resp_fail:
75d65857
JM
878 if (!reply_on_fail)
879 return 0;
880
881 /* Send reject response for all the failures */
882
e27d20bb 883 if (wpa_s->wnm_reply) {
d0b9ab69 884 wpa_s->wnm_reply = 0;
e27d20bb
VK
885 wnm_send_bss_transition_mgmt_resp(wpa_s,
886 wpa_s->wnm_dialog_token,
d0b9ab69 887 status, 0, NULL);
e27d20bb 888 }
d0b9ab69
JM
889 wnm_deallocate_memory(wpa_s);
890
891 return 0;
e27d20bb
VK
892}
893
894
ff2c5758
JM
895static int cand_pref_compar(const void *a, const void *b)
896{
897 const struct neighbor_report *aa = a;
898 const struct neighbor_report *bb = b;
899
900 if (!aa->preference_present && !bb->preference_present)
901 return 0;
902 if (!aa->preference_present)
903 return 1;
904 if (!bb->preference_present)
905 return -1;
906 if (bb->preference > aa->preference)
907 return 1;
908 if (bb->preference < aa->preference)
909 return -1;
910 return 0;
911}
912
913
914static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
915{
916 if (!wpa_s->wnm_neighbor_report_elements)
917 return;
918 qsort(wpa_s->wnm_neighbor_report_elements,
919 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
920 cand_pref_compar);
921}
922
923
8040dc53
JM
924static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
925{
926 unsigned int i;
927
928 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
929 if (!wpa_s->wnm_neighbor_report_elements)
930 return;
931 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
932 struct neighbor_report *nei;
933
934 nei = &wpa_s->wnm_neighbor_report_elements[i];
935 wpa_printf(MSG_DEBUG, "%u: " MACSTR
e1117c1c 936 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
4c381f0d
JM
937 i, MAC2STR(nei->bssid), nei->bssid_info,
938 nei->regulatory_class,
8040dc53 939 nei->channel_number, nei->phy_type,
e1117c1c
JM
940 nei->preference_present ? nei->preference : -1,
941 nei->freq);
942 }
943}
944
945
946static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
947{
948 unsigned int i;
949
950 for (i = 0; i < wpa_s->hw.num_modes; i++) {
951 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
952 int j;
953
954 for (j = 0; j < mode->num_channels; j++) {
955 struct hostapd_channel_data *chan;
956
957 chan = &mode->channels[j];
958 if (chan->freq == freq &&
959 !(chan->flag & HOSTAPD_CHAN_DISABLED))
960 return 1;
961 }
962 }
963
964 return 0;
965}
966
967
968static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
969{
970 int *freqs;
971 int num_freqs = 0;
972 unsigned int i;
973
974 if (!wpa_s->wnm_neighbor_report_elements)
975 return;
976
977 if (wpa_s->hw.modes == NULL)
978 return;
979
980 os_free(wpa_s->next_scan_freqs);
981 wpa_s->next_scan_freqs = NULL;
982
983 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
984 if (freqs == NULL)
985 return;
986
987 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
988 struct neighbor_report *nei;
989
990 nei = &wpa_s->wnm_neighbor_report_elements[i];
991 if (nei->freq <= 0) {
992 wpa_printf(MSG_DEBUG,
993 "WNM: Unknown neighbor operating frequency for "
994 MACSTR " - scan all channels",
995 MAC2STR(nei->bssid));
996 os_free(freqs);
997 return;
998 }
999 if (chan_supported(wpa_s, nei->freq))
1000 add_freq(freqs, &num_freqs, nei->freq);
8040dc53 1001 }
e1117c1c
JM
1002
1003 if (num_freqs == 0) {
1004 os_free(freqs);
1005 return;
1006 }
1007
1008 wpa_printf(MSG_DEBUG,
1009 "WNM: Scan %d frequencies based on transition candidate list",
1010 num_freqs);
1011 wpa_s->next_scan_freqs = freqs;
8040dc53
JM
1012}
1013
1014
2049a875
JM
1015static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1016 const u8 *pos, const u8 *end,
1017 int reply)
1018{
8c9af762
JM
1019 unsigned int beacon_int;
1020 u8 valid_int;
dd599908
AS
1021#ifdef CONFIG_MBO
1022 const u8 *vendor;
1023#endif /* CONFIG_MBO */
8c9af762 1024
bdce45b8 1025 if (end - pos < 5)
2049a875
JM
1026 return;
1027
8c9af762
JM
1028 if (wpa_s->current_bss)
1029 beacon_int = wpa_s->current_bss->beacon_int;
1030 else
1031 beacon_int = 100; /* best guess */
1032
e27d20bb
VK
1033 wpa_s->wnm_dialog_token = pos[0];
1034 wpa_s->wnm_mode = pos[1];
1035 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
8c9af762 1036 valid_int = pos[4];
e27d20bb 1037 wpa_s->wnm_reply = reply;
2049a875
JM
1038
1039 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1040 "dialog_token=%u request_mode=0x%x "
1041 "disassoc_timer=%u validity_interval=%u",
e27d20bb 1042 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
8c9af762 1043 wpa_s->wnm_dissoc_timer, valid_int);
e27d20bb 1044
2049a875 1045 pos += 5;
e27d20bb 1046
7b53acd3 1047 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
bdce45b8 1048 if (end - pos < 12) {
e27d20bb
VK
1049 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1050 return;
1051 }
1052 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
2049a875 1053 pos += 12; /* BSS Termination Duration */
e27d20bb
VK
1054 }
1055
7b53acd3 1056 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
2049a875 1057 char url[256];
ae8535b6 1058
bdce45b8 1059 if (end - pos < 1 || 1 + pos[0] > end - pos) {
2049a875
JM
1060 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1061 "Management Request (URL)");
1062 return;
1063 }
1064 os_memcpy(url, pos + 1, pos[0]);
1065 url[pos[0]] = '\0';
e27d20bb 1066 pos += 1 + pos[0];
ae8535b6 1067
ae8535b6
JM
1068 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1069 wpa_sm_pmf_enabled(wpa_s->wpa),
1070 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
2049a875
JM
1071 }
1072
7b53acd3 1073 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
2049a875 1074 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
e27d20bb
VK
1075 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
1076 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
2049a875
JM
1077 /* TODO: mark current BSS less preferred for
1078 * selection */
1079 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
1080 wpa_supplicant_req_scan(wpa_s, 0, 0);
1081 }
1082 }
1083
dd599908
AS
1084#ifdef CONFIG_MBO
1085 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1086 if (vendor)
1087 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1088#endif /* CONFIG_MBO */
1089
7b53acd3 1090 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
8c9af762
JM
1091 unsigned int valid_ms;
1092
e27d20bb 1093 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
279b5486 1094 wnm_deallocate_memory(wpa_s);
faebdeaa
JM
1095 wpa_s->wnm_neighbor_report_elements = os_calloc(
1096 WNM_MAX_NEIGHBOR_REPORT,
e27d20bb
VK
1097 sizeof(struct neighbor_report));
1098 if (wpa_s->wnm_neighbor_report_elements == NULL)
1099 return;
1100
bdce45b8 1101 while (end - pos >= 2 &&
e27d20bb
VK
1102 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
1103 {
1104 u8 tag = *pos++;
1105 u8 len = *pos++;
1106
1107 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
1108 tag);
bdce45b8 1109 if (len > end - pos) {
e27d20bb
VK
1110 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1111 return;
1112 }
1aa6f953
JM
1113 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1114 struct neighbor_report *rep;
1115 rep = &wpa_s->wnm_neighbor_report_elements[
1116 wpa_s->wnm_num_neighbor_report];
1117 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
f420577f 1118 wpa_s->wnm_num_neighbor_report++;
1aa6f953 1119 }
e27d20bb
VK
1120
1121 pos += len;
e27d20bb 1122 }
d14e63a2
AS
1123
1124 if (!wpa_s->wnm_num_neighbor_report) {
1125 wpa_printf(MSG_DEBUG,
1126 "WNM: Candidate list included bit is set, but no candidates found");
1127 wnm_send_bss_transition_mgmt_resp(
1128 wpa_s, wpa_s->wnm_dialog_token,
1129 WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1130 0, NULL);
1131 return;
1132 }
1133
ff2c5758 1134 wnm_sort_cand_list(wpa_s);
8040dc53 1135 wnm_dump_cand_list(wpa_s);
8c9af762
JM
1136 valid_ms = valid_int * beacon_int * 128 / 125;
1137 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1138 valid_ms);
1139 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1140 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
1141 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
1142 wpa_s->wnm_cand_valid_until.sec +=
1143 wpa_s->wnm_cand_valid_until.usec / 1000000;
1144 wpa_s->wnm_cand_valid_until.usec %= 1000000;
d0b9ab69 1145 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
e27d20bb 1146
75d65857
JM
1147 if (wpa_s->last_scan_res_used > 0) {
1148 struct os_reltime now;
1149
1150 os_get_reltime(&now);
1151 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
1152 wpa_printf(MSG_DEBUG,
1153 "WNM: Try to use recent scan results");
1154 if (wnm_scan_process(wpa_s, 0) > 0)
1155 return;
1156 wpa_printf(MSG_DEBUG,
1157 "WNM: No match in previous scan results - try a new scan");
1158 }
1159 }
1160
e1117c1c 1161 wnm_set_scan_freqs(wpa_s);
0645492e
JM
1162 if (wpa_s->wnm_num_neighbor_report == 1) {
1163 os_memcpy(wpa_s->next_scan_bssid,
1164 wpa_s->wnm_neighbor_report_elements[0].bssid,
1165 ETH_ALEN);
1166 wpa_printf(MSG_DEBUG,
1167 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1168 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1169 }
e27d20bb
VK
1170 wpa_supplicant_req_scan(wpa_s, 0, 0);
1171 } else if (reply) {
6df634fa
JM
1172 enum bss_trans_mgmt_status_code status;
1173 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
1174 status = WNM_BSS_TM_ACCEPT;
1175 else {
1176 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1177 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1178 }
e27d20bb
VK
1179 wnm_send_bss_transition_mgmt_resp(wpa_s,
1180 wpa_s->wnm_dialog_token,
6df634fa 1181 status, 0, NULL);
2049a875
JM
1182 }
1183}
1184
1185
65bcd0a9 1186int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
9a493fab 1187 u8 query_reason, int cand_list)
65bcd0a9 1188{
9a493fab 1189 u8 buf[2000], *pos;
65bcd0a9
VK
1190 struct ieee80211_mgmt *mgmt;
1191 size_t len;
1192 int ret;
1193
1194 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
9a493fab
AS
1195 MACSTR " query_reason=%u%s",
1196 MAC2STR(wpa_s->bssid), query_reason,
1197 cand_list ? " candidate list" : "");
65bcd0a9
VK
1198
1199 mgmt = (struct ieee80211_mgmt *) buf;
1200 os_memset(&buf, 0, sizeof(buf));
1201 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
1202 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
1203 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
1204 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1205 WLAN_FC_STYPE_ACTION);
1206 mgmt->u.action.category = WLAN_ACTION_WNM;
1207 mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
a8a6a35f 1208 mgmt->u.action.u.bss_tm_query.dialog_token = 1;
65bcd0a9
VK
1209 mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
1210 pos = mgmt->u.action.u.bss_tm_query.variable;
1211
9a493fab
AS
1212 if (cand_list)
1213 pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
1214
65bcd0a9
VK
1215 len = pos - (u8 *) &mgmt->u.action.category;
1216
1217 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1218 wpa_s->own_addr, wpa_s->bssid,
1219 &mgmt->u.action.category, len, 0);
1220
1221 return ret;
1222}
1223
1224
95a3ea94
JM
1225static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1226 const u8 *sa, const u8 *data,
1227 int len)
1228{
7ef69479 1229 const u8 *pos, *end, *next;
95a3ea94
JM
1230 u8 ie, ie_len;
1231
1232 pos = data;
1233 end = data + len;
1234
bdce45b8 1235 while (end - pos > 1) {
95a3ea94
JM
1236 ie = *pos++;
1237 ie_len = *pos++;
1238 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1239 ie, ie_len);
1240 if (ie_len > end - pos) {
1241 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1242 "subelement");
1243 break;
1244 }
7ef69479
JM
1245 next = pos + ie_len;
1246 if (ie_len < 4) {
1247 pos = next;
1248 continue;
1249 }
1250 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1251 WPA_GET_BE24(pos), pos[3]);
95a3ea94
JM
1252
1253#ifdef CONFIG_HS20
1254 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1255 WPA_GET_BE24(pos) == OUI_WFA &&
1256 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1257 /* Subscription Remediation subelement */
1258 const u8 *ie_end;
1259 u8 url_len;
1260 char *url;
1261 u8 osu_method;
1262
1263 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1264 "subelement");
1265 ie_end = pos + ie_len;
1266 pos += 4;
1267 url_len = *pos++;
1268 if (url_len == 0) {
1269 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1270 url = NULL;
1271 osu_method = 1;
1272 } else {
bdce45b8 1273 if (url_len + 1 > ie_end - pos) {
95a3ea94
JM
1274 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1275 url_len,
1276 (int) (ie_end - pos));
1277 break;
1278 }
1279 url = os_malloc(url_len + 1);
1280 if (url == NULL)
1281 break;
1282 os_memcpy(url, pos, url_len);
1283 url[url_len] = '\0';
1284 osu_method = pos[url_len];
1285 }
1286 hs20_rx_subscription_remediation(wpa_s, url,
1287 osu_method);
1288 os_free(url);
7ef69479
JM
1289 pos = next;
1290 continue;
1291 }
1292
1293 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1294 WPA_GET_BE24(pos) == OUI_WFA &&
1295 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1296 const u8 *ie_end;
1297 u8 url_len;
1298 char *url;
1299 u8 code;
1300 u16 reauth_delay;
1301
1302 ie_end = pos + ie_len;
1303 pos += 4;
1304 code = *pos++;
1305 reauth_delay = WPA_GET_LE16(pos);
1306 pos += 2;
1307 url_len = *pos++;
1308 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1309 "Imminent - Reason Code %u "
1310 "Re-Auth Delay %u URL Length %u",
1311 code, reauth_delay, url_len);
bdce45b8 1312 if (url_len > ie_end - pos)
7ef69479
JM
1313 break;
1314 url = os_malloc(url_len + 1);
1315 if (url == NULL)
1316 break;
1317 os_memcpy(url, pos, url_len);
1318 url[url_len] = '\0';
1319 hs20_rx_deauth_imminent_notice(wpa_s, code,
1320 reauth_delay, url);
1321 os_free(url);
1322 pos = next;
1323 continue;
95a3ea94
JM
1324 }
1325#endif /* CONFIG_HS20 */
1326
7ef69479 1327 pos = next;
95a3ea94
JM
1328 }
1329}
1330
1331
1332static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1333 const u8 *sa, const u8 *frm, int len)
1334{
1335 const u8 *pos, *end;
1336 u8 dialog_token, type;
1337
1338 /* Dialog Token [1] | Type [1] | Subelements */
1339
1340 if (len < 2 || sa == NULL)
1341 return;
1342 end = frm + len;
1343 pos = frm;
1344 dialog_token = *pos++;
1345 type = *pos++;
1346
1347 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1348 "(dialog_token %u type %u sa " MACSTR ")",
1349 dialog_token, type, MAC2STR(sa));
1350 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1351 pos, end - pos);
1352
1353 if (wpa_s->wpa_state != WPA_COMPLETED ||
1354 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1355 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1356 "from our AP - ignore it");
1357 return;
1358 }
1359
1360 switch (type) {
1361 case 1:
1362 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1363 break;
1364 default:
1365 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1366 "WNM-Notification type %u", type);
1367 break;
1368 }
1369}
1370
1371
75cad1a0 1372void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
dbfb8e82 1373 const struct ieee80211_mgmt *mgmt, size_t len)
75cad1a0 1374{
27c77751 1375 const u8 *pos, *end;
2049a875 1376 u8 act;
27c77751 1377
dbfb8e82 1378 if (len < IEEE80211_HDRLEN + 2)
27c77751
JM
1379 return;
1380
2703fb4a 1381 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
27c77751 1382 act = *pos++;
dbfb8e82 1383 end = ((const u8 *) mgmt) + len;
27c77751
JM
1384
1385 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
dbfb8e82 1386 act, MAC2STR(mgmt->sa));
2049a875 1387 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
dbfb8e82 1388 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
2049a875
JM
1389 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1390 "frame");
1391 return;
1392 }
75cad1a0
XC
1393
1394 switch (act) {
27c77751 1395 case WNM_BSS_TRANS_MGMT_REQ:
2049a875 1396 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
dbfb8e82 1397 !(mgmt->da[0] & 0x01));
27c77751 1398 break;
75cad1a0 1399 case WNM_SLEEP_MODE_RESP:
dbfb8e82 1400 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
75cad1a0 1401 break;
95a3ea94
JM
1402 case WNM_NOTIFICATION_REQ:
1403 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1404 break;
75cad1a0 1405 default:
e27d20bb 1406 wpa_printf(MSG_ERROR, "WNM: Unknown request");
75cad1a0
XC
1407 break;
1408 }
1409}