]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/wnm_sta.c
WNM: Add option to configure candidates for BTM query candidate list
[thirdparty/hostap.git] / wpa_supplicant / wnm_sta.c
1 /*
2 * wpa_supplicant - WNM
3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
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"
13 #include "common/ieee802_11_common.h"
14 #include "common/wpa_ctrl.h"
15 #include "rsn_supp/wpa.h"
16 #include "config.h"
17 #include "wpa_supplicant_i.h"
18 #include "driver_i.h"
19 #include "scan.h"
20 #include "ctrl_iface.h"
21 #include "bss.h"
22 #include "wnm_sta.h"
23 #include "hs20_supplicant.h"
24
25 #define MAX_TFS_IE_LEN 1024
26 #define WNM_MAX_NEIGHBOR_REPORT 10
27
28 #define WNM_SCAN_RESULT_AGE 2 /* 2 seconds */
29
30 /* get the TFS IE from driver */
31 static 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 */
41 static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
42 const u8 *addr, const u8 *buf, u16 buf_len,
43 enum wnm_oper oper)
44 {
45 u16 len = buf_len;
46
47 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
48
49 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
50 }
51
52
53 /* MLME-SLEEPMODE.request */
54 int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
55 u8 action, u16 intval, struct wpabuf *tfs_req)
56 {
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
67 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
72 /* 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;
81 wnmsleep_ie->intval = host_to_le16(intval);
82 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
83 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
84
85 /* TFS IE(s) */
86 if (tfs_req) {
87 wnmtfs_ie_len = wpabuf_len(tfs_req);
88 wnmtfs_ie = os_memdup(wpabuf_head(tfs_req), wnmtfs_ie_len);
89 if (wnmtfs_ie == NULL) {
90 os_free(wnmsleep_ie);
91 return -1;
92 }
93 } 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 }
105 }
106 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
107 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
108
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");
113 os_free(wnmsleep_ie);
114 os_free(wnmtfs_ie);
115 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;
125 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
126 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);
143 else
144 wpa_s->wnmsleep_used = 1;
145
146 os_free(wnmsleep_ie);
147 os_free(wnmtfs_ie);
148 os_free(mgmt);
149
150 return res;
151 }
152
153
154 static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
155 const u8 *tfsresp_ie_start,
156 const u8 *tfsresp_ie_end)
157 {
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,
172 tfsresp_ie_len,
173 WNM_SLEEP_TFS_RESP_IE_SET))
174 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
175 }
176 }
177
178
179 static 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 */
191 ptr = (u8 *) frm + 1 + 2;
192 end = ptr + key_len_total;
193 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
194
195 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
201 while (end - ptr > 1) {
202 if (2 + ptr[1] > end - ptr) {
203 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
246 static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
247 const u8 *frm, int len)
248 {
249 /*
250 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
251 * WNM-Sleep Mode IE | TFS Response IE
252 */
253 const u8 *pos = frm; /* point to payload after the action field */
254 u16 key_len_total;
255 struct wnm_sleep_element *wnmsleep_ie = NULL;
256 /* multiple TFS Resp IE (assuming consecutive) */
257 const u8 *tfsresp_ie_start = NULL;
258 const u8 *tfsresp_ie_end = NULL;
259 size_t left;
260
261 if (!wpa_s->wnmsleep_used) {
262 wpa_printf(MSG_DEBUG,
263 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode has not been used in this association");
264 return;
265 }
266
267 if (len < 3)
268 return;
269 key_len_total = WPA_GET_LE16(frm + 1);
270
271 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
272 frm[0], key_len_total);
273 left = len - 3;
274 if (key_len_total > left) {
275 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
276 return;
277 }
278 pos += 3 + key_len_total;
279 while (pos - frm + 1 < len) {
280 u8 ie_len = *(pos + 1);
281 if (2 + ie_len > frm + len - pos) {
282 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);
286 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
287 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
302 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
303 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
304 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
305 "frame (action=%d, intval=%d)",
306 wnmsleep_ie->action_type, wnmsleep_ie->intval);
307 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
308 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
309 tfsresp_ie_end);
310 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
311 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
312 }
313 } else {
314 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
315 "(action=%d, intval=%d)",
316 wnmsleep_ie->action_type, wnmsleep_ie->intval);
317 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
318 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
319 wpa_s->bssid, NULL, NULL);
320 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
321 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
322 wpa_s->bssid, NULL, NULL);
323 }
324 }
325
326
327 void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
328 {
329 int i;
330
331 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
332 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
333 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
334 }
335
336 wpa_s->wnm_num_neighbor_report = 0;
337 os_free(wpa_s->wnm_neighbor_report_elements);
338 wpa_s->wnm_neighbor_report_elements = NULL;
339 }
340
341
342 static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
343 u8 id, u8 elen, const u8 *pos)
344 {
345 switch (id) {
346 case WNM_NEIGHBOR_TSF:
347 if (elen < 2 + 2) {
348 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
349 break;
350 }
351 rep->tsf_offset = WPA_GET_LE16(pos);
352 rep->beacon_int = WPA_GET_LE16(pos + 2);
353 rep->tsf_present = 1;
354 break;
355 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
356 if (elen < 2) {
357 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
358 "country string");
359 break;
360 }
361 os_memcpy(rep->country, pos, 2);
362 rep->country_present = 1;
363 break;
364 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
365 if (elen < 1) {
366 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
367 "candidate");
368 break;
369 }
370 rep->preference = pos[0];
371 rep->preference_present = 1;
372 break;
373 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
374 rep->bss_term_tsf = WPA_GET_LE64(pos);
375 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
376 rep->bss_term_present = 1;
377 break;
378 case WNM_NEIGHBOR_BEARING:
379 if (elen < 8) {
380 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
381 "bearing");
382 break;
383 }
384 rep->bearing = WPA_GET_LE16(pos);
385 rep->distance = WPA_GET_LE32(pos + 2);
386 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
387 rep->bearing_present = 1;
388 break;
389 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
390 if (elen < 1) {
391 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
392 "pilot");
393 break;
394 }
395 os_free(rep->meas_pilot);
396 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
397 if (rep->meas_pilot == NULL)
398 break;
399 rep->meas_pilot->measurement_pilot = pos[0];
400 rep->meas_pilot->subelem_len = elen - 1;
401 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
402 break;
403 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
404 if (elen < 5) {
405 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
406 "capabilities");
407 break;
408 }
409 os_memcpy(rep->rm_capab, pos, 5);
410 rep->rm_capab_present = 1;
411 break;
412 case WNM_NEIGHBOR_MULTIPLE_BSSID:
413 if (elen < 1) {
414 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
415 break;
416 }
417 os_free(rep->mul_bssid);
418 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
419 if (rep->mul_bssid == NULL)
420 break;
421 rep->mul_bssid->max_bssid_indicator = pos[0];
422 rep->mul_bssid->subelem_len = elen - 1;
423 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
424 break;
425 }
426 }
427
428
429 static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
430 {
431 struct wpa_bss *bss = wpa_s->current_bss;
432 const char *country = NULL;
433 int freq;
434
435 if (bss) {
436 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
437
438 if (elem && elem[1] >= 2)
439 country = (const char *) (elem + 2);
440 }
441
442 freq = ieee80211_chan_to_freq(country, op_class, chan);
443 if (freq <= 0 && op_class == 0) {
444 /*
445 * Some APs do not advertise correct operating class
446 * information. Try to determine the most likely operating
447 * frequency based on the channel number.
448 */
449 if (chan >= 1 && chan <= 13)
450 freq = 2407 + chan * 5;
451 else if (chan == 14)
452 freq = 2484;
453 else if (chan >= 36 && chan <= 169)
454 freq = 5000 + chan * 5;
455 }
456 return freq;
457 }
458
459
460 static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
461 const u8 *pos, u8 len,
462 struct neighbor_report *rep)
463 {
464 u8 left = len;
465
466 if (left < 13) {
467 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
468 return;
469 }
470
471 os_memcpy(rep->bssid, pos, ETH_ALEN);
472 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
473 rep->regulatory_class = *(pos + 10);
474 rep->channel_number = *(pos + 11);
475 rep->phy_type = *(pos + 12);
476
477 pos += 13;
478 left -= 13;
479
480 while (left >= 2) {
481 u8 id, elen;
482
483 id = *pos++;
484 elen = *pos++;
485 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
486 left -= 2;
487 if (elen > left) {
488 wpa_printf(MSG_DEBUG,
489 "WNM: Truncated neighbor report subelement");
490 break;
491 }
492 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
493 left -= elen;
494 pos += elen;
495 }
496
497 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
498 rep->channel_number);
499 }
500
501
502 static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
503 {
504 unsigned int i;
505
506 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
507 wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
508 }
509
510
511 static struct wpa_bss * get_first_acceptable(struct wpa_supplicant *wpa_s)
512 {
513 unsigned int i;
514 struct neighbor_report *nei;
515
516 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
517 nei = &wpa_s->wnm_neighbor_report_elements[i];
518 if (nei->acceptable)
519 return wpa_bss_get_bssid(wpa_s, nei->bssid);
520 }
521
522 return NULL;
523 }
524
525
526 #ifdef CONFIG_MBO
527 static struct wpa_bss *
528 get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
529 enum mbo_transition_reject_reason *reason)
530 {
531 struct wpa_bss *target = NULL;
532 struct wpa_bss_trans_info params;
533 struct wpa_bss_candidate_info *info = NULL;
534 struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
535 u8 *first_candidate_bssid = NULL, *pos;
536 unsigned int i;
537
538 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
539 params.n_candidates = 0;
540 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
541 if (!params.bssid)
542 return NULL;
543
544 pos = params.bssid;
545 for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
546 if (nei->is_first)
547 first_candidate_bssid = nei->bssid;
548 if (!nei->acceptable)
549 continue;
550 os_memcpy(pos, nei->bssid, ETH_ALEN);
551 pos += ETH_ALEN;
552 params.n_candidates++;
553 }
554
555 if (!params.n_candidates)
556 goto end;
557
558 info = wpa_drv_get_bss_trans_status(wpa_s, &params);
559 if (!info) {
560 /* If failed to get candidate BSS transition status from driver,
561 * get the first acceptable candidate from wpa_supplicant.
562 */
563 target = wpa_bss_get_bssid(wpa_s, params.bssid);
564 goto end;
565 }
566
567 /* Get the first acceptable candidate from driver */
568 for (i = 0; i < info->num; i++) {
569 if (info->candidates[i].is_accept) {
570 target = wpa_bss_get_bssid(wpa_s,
571 info->candidates[i].bssid);
572 goto end;
573 }
574 }
575
576 /* If Disassociation Imminent is set and driver rejects all the
577 * candidate select first acceptable candidate which has
578 * rssi > disassoc_imminent_rssi_threshold
579 */
580 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
581 for (i = 0; i < info->num; i++) {
582 target = wpa_bss_get_bssid(wpa_s,
583 info->candidates[i].bssid);
584 if (target->level <
585 wpa_s->conf->disassoc_imminent_rssi_threshold)
586 continue;
587 goto end;
588 }
589 }
590
591 /* While sending BTM reject use reason code of the first candidate
592 * received in BTM request frame
593 */
594 if (reason) {
595 for (i = 0; i < info->num; i++) {
596 if (first_candidate_bssid &&
597 os_memcmp(first_candidate_bssid,
598 info->candidates[i].bssid, ETH_ALEN) == 0)
599 {
600 *reason = info->candidates[i].reject_reason;
601 break;
602 }
603 }
604 }
605
606 target = NULL;
607
608 end:
609 os_free(params.bssid);
610 if (info) {
611 os_free(info->candidates);
612 os_free(info);
613 }
614 return target;
615 }
616 #endif /* CONFIG_MBO */
617
618
619 static struct wpa_bss *
620 compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs,
621 enum mbo_transition_reject_reason *reason)
622 {
623 u8 i;
624 struct wpa_bss *bss = wpa_s->current_bss;
625 struct wpa_bss *target;
626
627 if (!bss)
628 return NULL;
629
630 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
631 MAC2STR(wpa_s->bssid), bss->level);
632
633 wnm_clear_acceptable(wpa_s);
634
635 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
636 struct neighbor_report *nei;
637
638 nei = &wpa_s->wnm_neighbor_report_elements[i];
639 if (nei->preference_present && nei->preference == 0) {
640 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
641 MAC2STR(nei->bssid));
642 continue;
643 }
644
645 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
646 if (!target) {
647 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
648 " (pref %d) not found in scan results",
649 MAC2STR(nei->bssid),
650 nei->preference_present ? nei->preference :
651 -1);
652 continue;
653 }
654
655 if (age_secs) {
656 struct os_reltime now;
657
658 if (os_get_reltime(&now) == 0 &&
659 os_reltime_expired(&now, &target->last_update,
660 age_secs)) {
661 wpa_printf(MSG_DEBUG,
662 "Candidate BSS is more than %ld seconds old",
663 age_secs);
664 continue;
665 }
666 }
667
668 if (bss->ssid_len != target->ssid_len ||
669 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
670 /*
671 * TODO: Could consider allowing transition to another
672 * ESS if PMF was enabled for the association.
673 */
674 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
675 " (pref %d) in different ESS",
676 MAC2STR(nei->bssid),
677 nei->preference_present ? nei->preference :
678 -1);
679 continue;
680 }
681
682 if (wpa_s->current_ssid &&
683 !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
684 1, 0)) {
685 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
686 " (pref %d) does not match the current network profile",
687 MAC2STR(nei->bssid),
688 nei->preference_present ? nei->preference :
689 -1);
690 continue;
691 }
692
693 if (wpa_is_bss_tmp_disallowed(wpa_s, target->bssid)) {
694 wpa_printf(MSG_DEBUG,
695 "MBO: Candidate BSS " MACSTR
696 " retry delay is not over yet",
697 MAC2STR(nei->bssid));
698 continue;
699 }
700
701 if (target->level < bss->level && target->level < -80) {
702 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
703 " (pref %d) does not have sufficient signal level (%d)",
704 MAC2STR(nei->bssid),
705 nei->preference_present ? nei->preference :
706 -1,
707 target->level);
708 continue;
709 }
710
711 nei->acceptable = 1;
712 }
713
714 #ifdef CONFIG_MBO
715 if (wpa_s->wnm_mbo_trans_reason_present)
716 target = get_mbo_transition_candidate(wpa_s, reason);
717 else
718 target = get_first_acceptable(wpa_s);
719 #else /* CONFIG_MBO */
720 target = get_first_acceptable(wpa_s);
721 #endif /* CONFIG_MBO */
722
723 if (target) {
724 wpa_printf(MSG_DEBUG,
725 "WNM: Found an acceptable preferred transition candidate BSS "
726 MACSTR " (RSSI %d)",
727 MAC2STR(target->bssid), target->level);
728 }
729
730 return target;
731 }
732
733
734 static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
735 {
736 const u8 *ie_a, *ie_b;
737
738 if (!a || !b)
739 return 0;
740
741 ie_a = wpa_bss_get_ie(a, eid);
742 ie_b = wpa_bss_get_ie(b, eid);
743
744 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
745 return 0;
746
747 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
748 }
749
750
751 static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
752 {
753 u32 info = 0;
754
755 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
756
757 /*
758 * Leave the security and key scope bits unset to indicate that the
759 * security information is not available.
760 */
761
762 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
763 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
764 if (bss->caps & WLAN_CAPABILITY_QOS)
765 info |= NEI_REP_BSSID_INFO_QOS;
766 if (bss->caps & WLAN_CAPABILITY_APSD)
767 info |= NEI_REP_BSSID_INFO_APSD;
768 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
769 info |= NEI_REP_BSSID_INFO_RM;
770 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
771 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
772 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
773 info |= NEI_REP_BSSID_INFO_IMM_BA;
774 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
775 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
776 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
777 info |= NEI_REP_BSSID_INFO_HT;
778
779 return info;
780 }
781
782
783 static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
784 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
785 u8 pref)
786 {
787 if (wpabuf_len(*buf) + 18 >
788 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
789 wpa_printf(MSG_DEBUG,
790 "WNM: No room in frame for Neighbor Report element");
791 return -1;
792 }
793
794 if (wpabuf_resize(buf, 18) < 0) {
795 wpa_printf(MSG_DEBUG,
796 "WNM: Failed to allocate memory for Neighbor Report element");
797 return -1;
798 }
799
800 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
801 /* length: 13 for basic neighbor report + 3 for preference subelement */
802 wpabuf_put_u8(*buf, 16);
803 wpabuf_put_data(*buf, bssid, ETH_ALEN);
804 wpabuf_put_le32(*buf, bss_info);
805 wpabuf_put_u8(*buf, op_class);
806 wpabuf_put_u8(*buf, chan);
807 wpabuf_put_u8(*buf, phy_type);
808 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
809 wpabuf_put_u8(*buf, 1);
810 wpabuf_put_u8(*buf, pref);
811 return 0;
812 }
813
814
815 static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
816 struct wpa_bss *bss, struct wpabuf **buf,
817 u8 pref)
818 {
819 const u8 *ie;
820 u8 op_class, chan;
821 int sec_chan = 0, vht = 0;
822 enum phy_type phy_type;
823 u32 info;
824 struct ieee80211_ht_operation *ht_oper = NULL;
825 struct ieee80211_vht_operation *vht_oper = NULL;
826
827 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
828 if (ie && ie[1] >= 2) {
829 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
830
831 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
832 sec_chan = 1;
833 else if (ht_oper->ht_param &
834 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
835 sec_chan = -1;
836 }
837
838 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
839 if (ie && ie[1] >= 1) {
840 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
841
842 if (vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80MHZ ||
843 vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_160MHZ ||
844 vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80P80MHZ)
845 vht = vht_oper->vht_op_info_chwidth;
846 }
847
848 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
849 &chan) == NUM_HOSTAPD_MODES) {
850 wpa_printf(MSG_DEBUG,
851 "WNM: Cannot determine operating class and channel");
852 return -2;
853 }
854
855 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
856 (vht_oper != NULL));
857 if (phy_type == PHY_TYPE_UNSPECIFIED) {
858 wpa_printf(MSG_DEBUG,
859 "WNM: Cannot determine BSS phy type for Neighbor Report");
860 return -2;
861 }
862
863 info = wnm_get_bss_info(wpa_s, bss);
864
865 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
866 pref);
867 }
868
869
870 static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
871 {
872 unsigned int i, pref = 255;
873 struct os_reltime now;
874 struct wpa_ssid *ssid = wpa_s->current_ssid;
875
876 if (!ssid)
877 return;
878
879 /*
880 * TODO: Define when scan results are no longer valid for the candidate
881 * list.
882 */
883 os_get_reltime(&now);
884 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
885 return;
886
887 wpa_printf(MSG_DEBUG,
888 "WNM: Add candidate list to BSS Transition Management Response frame");
889 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
890 struct wpa_bss *bss = wpa_s->last_scan_res[i];
891 int res;
892
893 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
894 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
895 if (res == -2)
896 continue; /* could not build entry for BSS */
897 if (res < 0)
898 break; /* no more room for candidates */
899 if (pref == 1)
900 break;
901 }
902 }
903
904 wpa_hexdump_buf(MSG_DEBUG,
905 "WNM: BSS Transition Management Response candidate list",
906 *buf);
907 }
908
909
910 #define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
911
912 static void wnm_send_bss_transition_mgmt_resp(
913 struct wpa_supplicant *wpa_s, u8 dialog_token,
914 enum bss_trans_mgmt_status_code status,
915 enum mbo_transition_reject_reason reason,
916 u8 delay, const u8 *target_bssid)
917 {
918 struct wpabuf *buf;
919 int res;
920
921 wpa_printf(MSG_DEBUG,
922 "WNM: Send BSS Transition Management Response to " MACSTR
923 " dialog_token=%u status=%u reason=%u delay=%d",
924 MAC2STR(wpa_s->bssid), dialog_token, status, reason, delay);
925 if (!wpa_s->current_bss) {
926 wpa_printf(MSG_DEBUG,
927 "WNM: Current BSS not known - drop response");
928 return;
929 }
930
931 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
932 if (!buf) {
933 wpa_printf(MSG_DEBUG,
934 "WNM: Failed to allocate memory for BTM response");
935 return;
936 }
937
938 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
939 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
940 wpabuf_put_u8(buf, dialog_token);
941 wpabuf_put_u8(buf, status);
942 wpabuf_put_u8(buf, delay);
943 if (target_bssid) {
944 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
945 } else if (status == WNM_BSS_TM_ACCEPT) {
946 /*
947 * P802.11-REVmc clarifies that the Target BSSID field is always
948 * present when status code is zero, so use a fake value here if
949 * no BSSID is yet known.
950 */
951 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
952 }
953
954 if (status == WNM_BSS_TM_ACCEPT)
955 wnm_add_cand_list(wpa_s, &buf);
956
957 #ifdef CONFIG_MBO
958 if (status != WNM_BSS_TM_ACCEPT &&
959 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
960 u8 mbo[10];
961 size_t ret;
962
963 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
964 reason);
965 if (ret) {
966 if (wpabuf_resize(&buf, ret) < 0) {
967 wpabuf_free(buf);
968 wpa_printf(MSG_DEBUG,
969 "WNM: Failed to allocate memory for MBO IE");
970 return;
971 }
972
973 wpabuf_put_data(buf, mbo, ret);
974 }
975 }
976 #endif /* CONFIG_MBO */
977
978 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
979 wpa_s->own_addr, wpa_s->bssid,
980 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
981 if (res < 0) {
982 wpa_printf(MSG_DEBUG,
983 "WNM: Failed to send BSS Transition Management Response");
984 }
985
986 wpabuf_free(buf);
987 }
988
989
990 static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
991 struct wpa_bss *bss, struct wpa_ssid *ssid,
992 int after_new_scan)
993 {
994 wpa_dbg(wpa_s, MSG_DEBUG,
995 "WNM: Transition to BSS " MACSTR
996 " based on BSS Transition Management Request (old BSSID "
997 MACSTR " after_new_scan=%d)",
998 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
999
1000 /* Send the BSS Management Response - Accept */
1001 if (wpa_s->wnm_reply) {
1002 wpa_s->wnm_reply = 0;
1003 wpa_printf(MSG_DEBUG,
1004 "WNM: Sending successful BSS Transition Management Response");
1005 wnm_send_bss_transition_mgmt_resp(
1006 wpa_s, wpa_s->wnm_dialog_token, WNM_BSS_TM_ACCEPT,
1007 MBO_TRANSITION_REASON_UNSPECIFIED, 0, bss->bssid);
1008 }
1009
1010 if (bss == wpa_s->current_bss) {
1011 wpa_printf(MSG_DEBUG,
1012 "WNM: Already associated with the preferred candidate");
1013 wnm_deallocate_memory(wpa_s);
1014 return;
1015 }
1016
1017 wpa_s->reassociate = 1;
1018 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1019 wpa_supplicant_connect(wpa_s, bss, ssid);
1020 wnm_deallocate_memory(wpa_s);
1021 }
1022
1023
1024 int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
1025 {
1026 struct wpa_bss *bss;
1027 struct wpa_ssid *ssid = wpa_s->current_ssid;
1028 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1029 enum mbo_transition_reject_reason reason =
1030 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
1031
1032 if (!wpa_s->wnm_neighbor_report_elements)
1033 return 0;
1034
1035 wpa_dbg(wpa_s, MSG_DEBUG,
1036 "WNM: Process scan results for BSS Transition Management");
1037 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
1038 &wpa_s->scan_trigger_time)) {
1039 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1040 wnm_deallocate_memory(wpa_s);
1041 return 0;
1042 }
1043
1044 if (!wpa_s->current_bss ||
1045 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
1046 ETH_ALEN) != 0) {
1047 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
1048 return 0;
1049 }
1050
1051 /* Compare the Neighbor Report and scan results */
1052 bss = compare_scan_neighbor_results(wpa_s, 0, &reason);
1053 if (!bss) {
1054 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1055 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1056 goto send_bss_resp_fail;
1057 }
1058
1059 /* Associate to the network */
1060 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
1061 return 1;
1062
1063 send_bss_resp_fail:
1064 if (!reply_on_fail)
1065 return 0;
1066
1067 /* Send reject response for all the failures */
1068
1069 if (wpa_s->wnm_reply) {
1070 wpa_s->wnm_reply = 0;
1071 wnm_send_bss_transition_mgmt_resp(wpa_s,
1072 wpa_s->wnm_dialog_token,
1073 status, reason, 0, NULL);
1074 }
1075 wnm_deallocate_memory(wpa_s);
1076
1077 return 0;
1078 }
1079
1080
1081 static int cand_pref_compar(const void *a, const void *b)
1082 {
1083 const struct neighbor_report *aa = a;
1084 const struct neighbor_report *bb = b;
1085
1086 if (!aa->preference_present && !bb->preference_present)
1087 return 0;
1088 if (!aa->preference_present)
1089 return 1;
1090 if (!bb->preference_present)
1091 return -1;
1092 if (bb->preference > aa->preference)
1093 return 1;
1094 if (bb->preference < aa->preference)
1095 return -1;
1096 return 0;
1097 }
1098
1099
1100 static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1101 {
1102 if (!wpa_s->wnm_neighbor_report_elements)
1103 return;
1104 qsort(wpa_s->wnm_neighbor_report_elements,
1105 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1106 cand_pref_compar);
1107 }
1108
1109
1110 static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1111 {
1112 unsigned int i;
1113
1114 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1115 if (!wpa_s->wnm_neighbor_report_elements)
1116 return;
1117 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1118 struct neighbor_report *nei;
1119
1120 nei = &wpa_s->wnm_neighbor_report_elements[i];
1121 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1122 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1123 i, MAC2STR(nei->bssid), nei->bssid_info,
1124 nei->regulatory_class,
1125 nei->channel_number, nei->phy_type,
1126 nei->preference_present ? nei->preference : -1,
1127 nei->freq);
1128 }
1129 }
1130
1131
1132 static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1133 {
1134 unsigned int i;
1135
1136 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1137 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1138 int j;
1139
1140 for (j = 0; j < mode->num_channels; j++) {
1141 struct hostapd_channel_data *chan;
1142
1143 chan = &mode->channels[j];
1144 if (chan->freq == freq &&
1145 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1146 return 1;
1147 }
1148 }
1149
1150 return 0;
1151 }
1152
1153
1154 static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1155 {
1156 int *freqs;
1157 int num_freqs = 0;
1158 unsigned int i;
1159
1160 if (!wpa_s->wnm_neighbor_report_elements)
1161 return;
1162
1163 if (wpa_s->hw.modes == NULL)
1164 return;
1165
1166 os_free(wpa_s->next_scan_freqs);
1167 wpa_s->next_scan_freqs = NULL;
1168
1169 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1170 if (freqs == NULL)
1171 return;
1172
1173 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1174 struct neighbor_report *nei;
1175
1176 nei = &wpa_s->wnm_neighbor_report_elements[i];
1177 if (nei->freq <= 0) {
1178 wpa_printf(MSG_DEBUG,
1179 "WNM: Unknown neighbor operating frequency for "
1180 MACSTR " - scan all channels",
1181 MAC2STR(nei->bssid));
1182 os_free(freqs);
1183 return;
1184 }
1185 if (chan_supported(wpa_s, nei->freq))
1186 add_freq(freqs, &num_freqs, nei->freq);
1187 }
1188
1189 if (num_freqs == 0) {
1190 os_free(freqs);
1191 return;
1192 }
1193
1194 wpa_printf(MSG_DEBUG,
1195 "WNM: Scan %d frequencies based on transition candidate list",
1196 num_freqs);
1197 wpa_s->next_scan_freqs = freqs;
1198 }
1199
1200
1201 static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
1202 {
1203 struct wpa_scan_results *scan_res;
1204 struct wpa_bss *bss;
1205 struct wpa_ssid *ssid = wpa_s->current_ssid;
1206 u8 i, found = 0;
1207 size_t j;
1208
1209 wpa_dbg(wpa_s, MSG_DEBUG,
1210 "WNM: Fetch current scan results from the driver for checking transition candidates");
1211 scan_res = wpa_drv_get_scan_results2(wpa_s);
1212 if (!scan_res) {
1213 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
1214 return 0;
1215 }
1216
1217 if (scan_res->fetch_time.sec == 0)
1218 os_get_reltime(&scan_res->fetch_time);
1219
1220 filter_scan_res(wpa_s, scan_res);
1221
1222 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1223 struct neighbor_report *nei;
1224
1225 nei = &wpa_s->wnm_neighbor_report_elements[i];
1226 if (nei->preference_present && nei->preference == 0)
1227 continue;
1228
1229 for (j = 0; j < scan_res->num; j++) {
1230 struct wpa_scan_res *res;
1231 const u8 *ssid_ie;
1232
1233 res = scan_res->res[j];
1234 if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
1235 res->age > WNM_SCAN_RESULT_AGE * 1000)
1236 continue;
1237 bss = wpa_s->current_bss;
1238 ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
1239 if (bss && ssid_ie &&
1240 (bss->ssid_len != ssid_ie[1] ||
1241 os_memcmp(bss->ssid, ssid_ie + 2,
1242 bss->ssid_len) != 0))
1243 continue;
1244
1245 /* Potential candidate found */
1246 found = 1;
1247 scan_snr(res);
1248 scan_est_throughput(wpa_s, res);
1249 wpa_bss_update_scan_res(wpa_s, res,
1250 &scan_res->fetch_time);
1251 }
1252 }
1253
1254 wpa_scan_results_free(scan_res);
1255 if (!found) {
1256 wpa_dbg(wpa_s, MSG_DEBUG,
1257 "WNM: No transition candidate matches existing scan results");
1258 return 0;
1259 }
1260
1261 bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE, NULL);
1262 if (!bss) {
1263 wpa_dbg(wpa_s, MSG_DEBUG,
1264 "WNM: Comparison of scan results against transition candidates did not find matches");
1265 return 0;
1266 }
1267
1268 /* Associate to the network */
1269 wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
1270 return 1;
1271 }
1272
1273
1274 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1275 const u8 *pos, const u8 *end,
1276 int reply)
1277 {
1278 unsigned int beacon_int;
1279 u8 valid_int;
1280 #ifdef CONFIG_MBO
1281 const u8 *vendor;
1282 #endif /* CONFIG_MBO */
1283
1284 if (end - pos < 5)
1285 return;
1286
1287 #ifdef CONFIG_MBO
1288 wpa_s->wnm_mbo_trans_reason_present = 0;
1289 wpa_s->wnm_mbo_transition_reason = 0;
1290 #endif /* CONFIG_MBO */
1291
1292 if (wpa_s->current_bss)
1293 beacon_int = wpa_s->current_bss->beacon_int;
1294 else
1295 beacon_int = 100; /* best guess */
1296
1297 wpa_s->wnm_dialog_token = pos[0];
1298 wpa_s->wnm_mode = pos[1];
1299 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
1300 valid_int = pos[4];
1301 wpa_s->wnm_reply = reply;
1302
1303 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1304 "dialog_token=%u request_mode=0x%x "
1305 "disassoc_timer=%u validity_interval=%u",
1306 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
1307 wpa_s->wnm_dissoc_timer, valid_int);
1308
1309 #if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1310 if (wpa_s->reject_btm_req_reason) {
1311 wpa_printf(MSG_INFO,
1312 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1313 wpa_s->reject_btm_req_reason);
1314 wnm_send_bss_transition_mgmt_resp(
1315 wpa_s, wpa_s->wnm_dialog_token,
1316 wpa_s->reject_btm_req_reason,
1317 MBO_TRANSITION_REASON_UNSPECIFIED, 0, NULL);
1318 return;
1319 }
1320 #endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1321
1322 pos += 5;
1323
1324 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
1325 if (end - pos < 12) {
1326 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1327 return;
1328 }
1329 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
1330 pos += 12; /* BSS Termination Duration */
1331 }
1332
1333 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
1334 char url[256];
1335
1336 if (end - pos < 1 || 1 + pos[0] > end - pos) {
1337 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1338 "Management Request (URL)");
1339 return;
1340 }
1341 os_memcpy(url, pos + 1, pos[0]);
1342 url[pos[0]] = '\0';
1343 pos += 1 + pos[0];
1344
1345 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1346 wpa_sm_pmf_enabled(wpa_s->wpa),
1347 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
1348 }
1349
1350 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
1351 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
1352 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
1353 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
1354 /* TODO: mark current BSS less preferred for
1355 * selection */
1356 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
1357 wpa_supplicant_req_scan(wpa_s, 0, 0);
1358 }
1359 }
1360
1361 #ifdef CONFIG_MBO
1362 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1363 if (vendor)
1364 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1365 #endif /* CONFIG_MBO */
1366
1367 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
1368 unsigned int valid_ms;
1369
1370 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
1371 wnm_deallocate_memory(wpa_s);
1372 wpa_s->wnm_neighbor_report_elements = os_calloc(
1373 WNM_MAX_NEIGHBOR_REPORT,
1374 sizeof(struct neighbor_report));
1375 if (wpa_s->wnm_neighbor_report_elements == NULL)
1376 return;
1377
1378 while (end - pos >= 2 &&
1379 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
1380 {
1381 u8 tag = *pos++;
1382 u8 len = *pos++;
1383
1384 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
1385 tag);
1386 if (len > end - pos) {
1387 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1388 return;
1389 }
1390 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1391 struct neighbor_report *rep;
1392 rep = &wpa_s->wnm_neighbor_report_elements[
1393 wpa_s->wnm_num_neighbor_report];
1394 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
1395 wpa_s->wnm_num_neighbor_report++;
1396 #ifdef CONFIG_MBO
1397 if (wpa_s->wnm_mbo_trans_reason_present &&
1398 wpa_s->wnm_num_neighbor_report == 1) {
1399 rep->is_first = 1;
1400 wpa_printf(MSG_DEBUG,
1401 "WNM: First transition candidate is "
1402 MACSTR, MAC2STR(rep->bssid));
1403 }
1404 #endif /* CONFIG_MBO */
1405 }
1406
1407 pos += len;
1408 }
1409
1410 if (!wpa_s->wnm_num_neighbor_report) {
1411 wpa_printf(MSG_DEBUG,
1412 "WNM: Candidate list included bit is set, but no candidates found");
1413 wnm_send_bss_transition_mgmt_resp(
1414 wpa_s, wpa_s->wnm_dialog_token,
1415 WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1416 MBO_TRANSITION_REASON_UNSPECIFIED, 0, NULL);
1417 return;
1418 }
1419
1420 wnm_sort_cand_list(wpa_s);
1421 wnm_dump_cand_list(wpa_s);
1422 valid_ms = valid_int * beacon_int * 128 / 125;
1423 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1424 valid_ms);
1425 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1426 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
1427 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
1428 wpa_s->wnm_cand_valid_until.sec +=
1429 wpa_s->wnm_cand_valid_until.usec / 1000000;
1430 wpa_s->wnm_cand_valid_until.usec %= 1000000;
1431 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
1432
1433 /*
1434 * Fetch the latest scan results from the kernel and check for
1435 * candidates based on those results first. This can help in
1436 * finding more up-to-date information should the driver has
1437 * done some internal scanning operations after the last scan
1438 * result update in wpa_supplicant.
1439 */
1440 if (wnm_fetch_scan_results(wpa_s) > 0)
1441 return;
1442
1443 /*
1444 * Try to use previously received scan results, if they are
1445 * recent enough to use for a connection.
1446 */
1447 if (wpa_s->last_scan_res_used > 0) {
1448 struct os_reltime now;
1449
1450 os_get_reltime(&now);
1451 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
1452 wpa_printf(MSG_DEBUG,
1453 "WNM: Try to use recent scan results");
1454 if (wnm_scan_process(wpa_s, 0) > 0)
1455 return;
1456 wpa_printf(MSG_DEBUG,
1457 "WNM: No match in previous scan results - try a new scan");
1458 }
1459 }
1460
1461 wnm_set_scan_freqs(wpa_s);
1462 if (wpa_s->wnm_num_neighbor_report == 1) {
1463 os_memcpy(wpa_s->next_scan_bssid,
1464 wpa_s->wnm_neighbor_report_elements[0].bssid,
1465 ETH_ALEN);
1466 wpa_printf(MSG_DEBUG,
1467 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1468 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1469 }
1470 wpa_supplicant_req_scan(wpa_s, 0, 0);
1471 } else if (reply) {
1472 enum bss_trans_mgmt_status_code status;
1473 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
1474 status = WNM_BSS_TM_ACCEPT;
1475 else {
1476 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1477 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1478 }
1479 wnm_send_bss_transition_mgmt_resp(
1480 wpa_s, wpa_s->wnm_dialog_token, status,
1481 MBO_TRANSITION_REASON_UNSPECIFIED, 0, NULL);
1482 }
1483 }
1484
1485
1486 #define BTM_QUERY_MIN_SIZE 4
1487
1488 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
1489 u8 query_reason,
1490 const char *btm_candidates,
1491 int cand_list)
1492 {
1493 struct wpabuf *buf;
1494 int ret;
1495
1496 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
1497 MACSTR " query_reason=%u%s",
1498 MAC2STR(wpa_s->bssid), query_reason,
1499 cand_list ? " candidate list" : "");
1500
1501 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1502 if (!buf)
1503 return -1;
1504
1505 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1506 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1507 wpabuf_put_u8(buf, 1);
1508 wpabuf_put_u8(buf, query_reason);
1509
1510 if (cand_list)
1511 wnm_add_cand_list(wpa_s, &buf);
1512
1513 if (btm_candidates) {
1514 const size_t max_len = 1000;
1515
1516 ret = wpabuf_resize(&buf, max_len);
1517 if (ret < 0) {
1518 wpabuf_free(buf);
1519 return ret;
1520 }
1521
1522 ret = ieee802_11_parse_candidate_list(btm_candidates,
1523 wpabuf_put(buf, 0),
1524 max_len);
1525 if (ret < 0) {
1526 wpabuf_free(buf);
1527 return ret;
1528 }
1529
1530 wpabuf_put(buf, ret);
1531 }
1532
1533 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1534 wpa_s->own_addr, wpa_s->bssid,
1535 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1536
1537 wpabuf_free(buf);
1538 return ret;
1539 }
1540
1541
1542 static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1543 const u8 *sa, const u8 *data,
1544 int len)
1545 {
1546 const u8 *pos, *end, *next;
1547 u8 ie, ie_len;
1548
1549 pos = data;
1550 end = data + len;
1551
1552 while (end - pos > 1) {
1553 ie = *pos++;
1554 ie_len = *pos++;
1555 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1556 ie, ie_len);
1557 if (ie_len > end - pos) {
1558 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1559 "subelement");
1560 break;
1561 }
1562 next = pos + ie_len;
1563 if (ie_len < 4) {
1564 pos = next;
1565 continue;
1566 }
1567 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1568 WPA_GET_BE24(pos), pos[3]);
1569
1570 #ifdef CONFIG_HS20
1571 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1572 WPA_GET_BE24(pos) == OUI_WFA &&
1573 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1574 /* Subscription Remediation subelement */
1575 const u8 *ie_end;
1576 u8 url_len;
1577 char *url;
1578 u8 osu_method;
1579
1580 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1581 "subelement");
1582 ie_end = pos + ie_len;
1583 pos += 4;
1584 url_len = *pos++;
1585 if (url_len == 0) {
1586 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1587 url = NULL;
1588 osu_method = 1;
1589 } else {
1590 if (url_len + 1 > ie_end - pos) {
1591 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1592 url_len,
1593 (int) (ie_end - pos));
1594 break;
1595 }
1596 url = os_malloc(url_len + 1);
1597 if (url == NULL)
1598 break;
1599 os_memcpy(url, pos, url_len);
1600 url[url_len] = '\0';
1601 osu_method = pos[url_len];
1602 }
1603 hs20_rx_subscription_remediation(wpa_s, url,
1604 osu_method);
1605 os_free(url);
1606 pos = next;
1607 continue;
1608 }
1609
1610 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1611 WPA_GET_BE24(pos) == OUI_WFA &&
1612 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1613 const u8 *ie_end;
1614 u8 url_len;
1615 char *url;
1616 u8 code;
1617 u16 reauth_delay;
1618
1619 ie_end = pos + ie_len;
1620 pos += 4;
1621 code = *pos++;
1622 reauth_delay = WPA_GET_LE16(pos);
1623 pos += 2;
1624 url_len = *pos++;
1625 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1626 "Imminent - Reason Code %u "
1627 "Re-Auth Delay %u URL Length %u",
1628 code, reauth_delay, url_len);
1629 if (url_len > ie_end - pos)
1630 break;
1631 url = os_malloc(url_len + 1);
1632 if (url == NULL)
1633 break;
1634 os_memcpy(url, pos, url_len);
1635 url[url_len] = '\0';
1636 hs20_rx_deauth_imminent_notice(wpa_s, code,
1637 reauth_delay, url);
1638 os_free(url);
1639 pos = next;
1640 continue;
1641 }
1642 #endif /* CONFIG_HS20 */
1643
1644 pos = next;
1645 }
1646 }
1647
1648
1649 static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1650 const u8 *sa, const u8 *frm, int len)
1651 {
1652 const u8 *pos, *end;
1653 u8 dialog_token, type;
1654
1655 /* Dialog Token [1] | Type [1] | Subelements */
1656
1657 if (len < 2 || sa == NULL)
1658 return;
1659 end = frm + len;
1660 pos = frm;
1661 dialog_token = *pos++;
1662 type = *pos++;
1663
1664 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1665 "(dialog_token %u type %u sa " MACSTR ")",
1666 dialog_token, type, MAC2STR(sa));
1667 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1668 pos, end - pos);
1669
1670 if (wpa_s->wpa_state != WPA_COMPLETED ||
1671 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1672 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1673 "from our AP - ignore it");
1674 return;
1675 }
1676
1677 switch (type) {
1678 case 1:
1679 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1680 break;
1681 default:
1682 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1683 "WNM-Notification type %u", type);
1684 break;
1685 }
1686 }
1687
1688
1689 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
1690 const struct ieee80211_mgmt *mgmt, size_t len)
1691 {
1692 const u8 *pos, *end;
1693 u8 act;
1694
1695 if (len < IEEE80211_HDRLEN + 2)
1696 return;
1697
1698 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
1699 act = *pos++;
1700 end = ((const u8 *) mgmt) + len;
1701
1702 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
1703 act, MAC2STR(mgmt->sa));
1704 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
1705 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
1706 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1707 "frame");
1708 return;
1709 }
1710
1711 switch (act) {
1712 case WNM_BSS_TRANS_MGMT_REQ:
1713 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
1714 !(mgmt->da[0] & 0x01));
1715 break;
1716 case WNM_SLEEP_MODE_RESP:
1717 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
1718 break;
1719 case WNM_NOTIFICATION_REQ:
1720 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1721 break;
1722 default:
1723 wpa_printf(MSG_ERROR, "WNM: Unknown request");
1724 break;
1725 }
1726 }